blob: 36e30137019c4adac836df666302694eeb8520a5 [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);
Douglas Gregorcefc3af2012-04-16 07:05:22 +0000745 {
746 // 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)) {
Chris Lattner5c5db552010-04-05 18:18:31 +00001053 if (RequireSemi) ConsumeToken();
John McCalld226f652010-08-21 09:40:31 +00001054 Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none,
Douglas Gregor312eadb2011-04-24 05:37:28 +00001055 DS);
John McCall54abf7d2009-11-04 02:18:39 +00001056 DS.complete(TheDecl);
Chris Lattner682bf922009-03-29 16:50:03 +00001057 return Actions.ConvertDeclToDeclGroup(TheDecl);
Reid Spencer5f016e22007-07-11 17:01:13 +00001058 }
Douglas Gregor312eadb2011-04-24 05:37:28 +00001059
1060 return ParseDeclGroup(DS, Context, /*FunctionDefs=*/ false, &DeclEnd, FRI);
John McCalld8ac0572009-11-03 19:26:08 +00001061}
Mike Stump1eb44332009-09-09 15:08:12 +00001062
Richard Smith0706df42011-10-19 21:33:05 +00001063/// Returns true if this might be the start of a declarator, or a common typo
1064/// for a declarator.
1065bool Parser::MightBeDeclarator(unsigned Context) {
1066 switch (Tok.getKind()) {
1067 case tok::annot_cxxscope:
1068 case tok::annot_template_id:
1069 case tok::caret:
1070 case tok::code_completion:
1071 case tok::coloncolon:
1072 case tok::ellipsis:
1073 case tok::kw___attribute:
1074 case tok::kw_operator:
1075 case tok::l_paren:
1076 case tok::star:
1077 return true;
1078
1079 case tok::amp:
1080 case tok::ampamp:
David Blaikie4e4d0842012-03-11 07:00:24 +00001081 return getLangOpts().CPlusPlus;
Richard Smith0706df42011-10-19 21:33:05 +00001082
Richard Smith1c94c162012-01-09 22:31:44 +00001083 case tok::l_square: // Might be an attribute on an unnamed bit-field.
David Blaikie4e4d0842012-03-11 07:00:24 +00001084 return Context == Declarator::MemberContext && getLangOpts().CPlusPlus0x &&
Richard Smith1c94c162012-01-09 22:31:44 +00001085 NextToken().is(tok::l_square);
1086
1087 case tok::colon: // Might be a typo for '::' or an unnamed bit-field.
David Blaikie4e4d0842012-03-11 07:00:24 +00001088 return Context == Declarator::MemberContext || getLangOpts().CPlusPlus;
Richard Smith1c94c162012-01-09 22:31:44 +00001089
Richard Smith0706df42011-10-19 21:33:05 +00001090 case tok::identifier:
1091 switch (NextToken().getKind()) {
1092 case tok::code_completion:
1093 case tok::coloncolon:
1094 case tok::comma:
1095 case tok::equal:
1096 case tok::equalequal: // Might be a typo for '='.
1097 case tok::kw_alignas:
1098 case tok::kw_asm:
1099 case tok::kw___attribute:
1100 case tok::l_brace:
1101 case tok::l_paren:
1102 case tok::l_square:
1103 case tok::less:
1104 case tok::r_brace:
1105 case tok::r_paren:
1106 case tok::r_square:
1107 case tok::semi:
1108 return true;
1109
1110 case tok::colon:
1111 // At namespace scope, 'identifier:' is probably a typo for 'identifier::'
Richard Smith1c94c162012-01-09 22:31:44 +00001112 // and in block scope it's probably a label. Inside a class definition,
1113 // this is a bit-field.
1114 return Context == Declarator::MemberContext ||
David Blaikie4e4d0842012-03-11 07:00:24 +00001115 (getLangOpts().CPlusPlus && Context == Declarator::FileContext);
Richard Smith1c94c162012-01-09 22:31:44 +00001116
1117 case tok::identifier: // Possible virt-specifier.
David Blaikie4e4d0842012-03-11 07:00:24 +00001118 return getLangOpts().CPlusPlus0x && isCXX0XVirtSpecifier(NextToken());
Richard Smith0706df42011-10-19 21:33:05 +00001119
1120 default:
1121 return false;
1122 }
1123
1124 default:
1125 return false;
1126 }
1127}
1128
Richard Smith994d73f2012-04-11 20:59:20 +00001129/// Skip until we reach something which seems like a sensible place to pick
1130/// up parsing after a malformed declaration. This will sometimes stop sooner
1131/// than SkipUntil(tok::r_brace) would, but will never stop later.
1132void Parser::SkipMalformedDecl() {
1133 while (true) {
1134 switch (Tok.getKind()) {
1135 case tok::l_brace:
1136 // Skip until matching }, then stop. We've probably skipped over
1137 // a malformed class or function definition or similar.
1138 ConsumeBrace();
1139 SkipUntil(tok::r_brace, /*StopAtSemi*/false);
1140 if (Tok.is(tok::comma) || Tok.is(tok::l_brace) || Tok.is(tok::kw_try)) {
1141 // This declaration isn't over yet. Keep skipping.
1142 continue;
1143 }
1144 if (Tok.is(tok::semi))
1145 ConsumeToken();
1146 return;
1147
1148 case tok::l_square:
1149 ConsumeBracket();
1150 SkipUntil(tok::r_square, /*StopAtSemi*/false);
1151 continue;
1152
1153 case tok::l_paren:
1154 ConsumeParen();
1155 SkipUntil(tok::r_paren, /*StopAtSemi*/false);
1156 continue;
1157
1158 case tok::r_brace:
1159 return;
1160
1161 case tok::semi:
1162 ConsumeToken();
1163 return;
1164
1165 case tok::kw_inline:
1166 // 'inline namespace' at the start of a line is almost certainly
1167 // a good place to pick back up parsing.
1168 if (Tok.isAtStartOfLine() && NextToken().is(tok::kw_namespace))
1169 return;
1170 break;
1171
1172 case tok::kw_namespace:
1173 // 'namespace' at the start of a line is almost certainly a good
1174 // place to pick back up parsing.
1175 if (Tok.isAtStartOfLine())
1176 return;
1177 break;
1178
1179 case tok::eof:
1180 return;
1181
1182 default:
1183 break;
1184 }
1185
1186 ConsumeAnyToken();
1187 }
1188}
1189
John McCalld8ac0572009-11-03 19:26:08 +00001190/// ParseDeclGroup - Having concluded that this is either a function
1191/// definition or a group of object declarations, actually parse the
1192/// result.
John McCall54abf7d2009-11-04 02:18:39 +00001193Parser::DeclGroupPtrTy Parser::ParseDeclGroup(ParsingDeclSpec &DS,
1194 unsigned Context,
John McCalld8ac0572009-11-03 19:26:08 +00001195 bool AllowFunctionDefinitions,
Richard Smithad762fc2011-04-14 22:09:26 +00001196 SourceLocation *DeclEnd,
1197 ForRangeInit *FRI) {
John McCalld8ac0572009-11-03 19:26:08 +00001198 // Parse the first declarator.
John McCall54abf7d2009-11-04 02:18:39 +00001199 ParsingDeclarator D(*this, DS, static_cast<Declarator::TheContext>(Context));
John McCalld8ac0572009-11-03 19:26:08 +00001200 ParseDeclarator(D);
Chris Lattnercd147752009-03-29 17:27:48 +00001201
John McCalld8ac0572009-11-03 19:26:08 +00001202 // Bail out if the first declarator didn't seem well-formed.
1203 if (!D.hasName() && !D.mayOmitIdentifier()) {
Richard Smith994d73f2012-04-11 20:59:20 +00001204 SkipMalformedDecl();
John McCalld8ac0572009-11-03 19:26:08 +00001205 return DeclGroupPtrTy();
Chris Lattner23c4b182009-03-29 17:18:04 +00001206 }
Mike Stump1eb44332009-09-09 15:08:12 +00001207
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001208 // Save late-parsed attributes for now; they need to be parsed in the
1209 // appropriate function scope after the function Decl has been constructed.
1210 LateParsedAttrList LateParsedAttrs;
1211 if (D.isFunctionDeclarator())
1212 MaybeParseGNUAttributes(D, &LateParsedAttrs);
1213
Chris Lattnerc82daef2010-07-11 22:24:20 +00001214 // Check to see if we have a function *definition* which must have a body.
1215 if (AllowFunctionDefinitions && D.isFunctionDeclarator() &&
1216 // Look at the next token to make sure that this isn't a function
1217 // declaration. We have to check this because __attribute__ might be the
1218 // start of a function definition in GCC-extended K&R C.
1219 !isDeclarationAfterDeclarator()) {
Richard Smith58196dc2011-11-30 23:45:35 +00001220
Chris Lattner004659a2010-07-11 22:42:07 +00001221 if (isStartOfFunctionDefinition(D)) {
John McCalld8ac0572009-11-03 19:26:08 +00001222 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
1223 Diag(Tok, diag::err_function_declared_typedef);
1224
1225 // Recover by treating the 'typedef' as spurious.
1226 DS.ClearStorageClassSpecs();
1227 }
1228
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001229 Decl *TheDecl =
1230 ParseFunctionDefinition(D, ParsedTemplateInfo(), &LateParsedAttrs);
John McCalld8ac0572009-11-03 19:26:08 +00001231 return Actions.ConvertDeclToDeclGroup(TheDecl);
Chris Lattner004659a2010-07-11 22:42:07 +00001232 }
1233
1234 if (isDeclarationSpecifier()) {
1235 // If there is an invalid declaration specifier right after the function
1236 // prototype, then we must be in a missing semicolon case where this isn't
1237 // actually a body. Just fall through into the code that handles it as a
1238 // prototype, and let the top-level code handle the erroneous declspec
1239 // where it would otherwise expect a comma or semicolon.
John McCalld8ac0572009-11-03 19:26:08 +00001240 } else {
1241 Diag(Tok, diag::err_expected_fn_body);
1242 SkipUntil(tok::semi);
1243 return DeclGroupPtrTy();
1244 }
1245 }
1246
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001247 if (ParseAsmAttributesAfterDeclarator(D))
Richard Smithad762fc2011-04-14 22:09:26 +00001248 return DeclGroupPtrTy();
1249
1250 // C++0x [stmt.iter]p1: Check if we have a for-range-declarator. If so, we
1251 // must parse and analyze the for-range-initializer before the declaration is
1252 // analyzed.
1253 if (FRI && Tok.is(tok::colon)) {
1254 FRI->ColonLoc = ConsumeToken();
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001255 if (Tok.is(tok::l_brace))
1256 FRI->RangeExpr = ParseBraceInitializer();
1257 else
1258 FRI->RangeExpr = ParseExpression();
Richard Smithad762fc2011-04-14 22:09:26 +00001259 Decl *ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
1260 Actions.ActOnCXXForRangeDecl(ThisDecl);
1261 Actions.FinalizeDeclaration(ThisDecl);
John McCall6895a642012-01-27 01:29:43 +00001262 D.complete(ThisDecl);
Richard Smithad762fc2011-04-14 22:09:26 +00001263 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS, &ThisDecl, 1);
1264 }
1265
Chris Lattner5f9e2722011-07-23 10:55:15 +00001266 SmallVector<Decl *, 8> DeclsInGroup;
Richard Smithad762fc2011-04-14 22:09:26 +00001267 Decl *FirstDecl = ParseDeclarationAfterDeclaratorAndAttributes(D);
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001268 if (LateParsedAttrs.size() > 0)
1269 ParseLexedAttributeList(LateParsedAttrs, FirstDecl, true, false);
John McCall54abf7d2009-11-04 02:18:39 +00001270 D.complete(FirstDecl);
John McCalld226f652010-08-21 09:40:31 +00001271 if (FirstDecl)
John McCalld8ac0572009-11-03 19:26:08 +00001272 DeclsInGroup.push_back(FirstDecl);
1273
Richard Smith0706df42011-10-19 21:33:05 +00001274 bool ExpectSemi = Context != Declarator::ForContext;
1275
John McCalld8ac0572009-11-03 19:26:08 +00001276 // If we don't have a comma, it is either the end of the list (a ';') or an
1277 // error, bail out.
1278 while (Tok.is(tok::comma)) {
Richard Smith0706df42011-10-19 21:33:05 +00001279 SourceLocation CommaLoc = ConsumeToken();
1280
1281 if (Tok.isAtStartOfLine() && ExpectSemi && !MightBeDeclarator(Context)) {
1282 // This comma was followed by a line-break and something which can't be
1283 // the start of a declarator. The comma was probably a typo for a
1284 // semicolon.
1285 Diag(CommaLoc, diag::err_expected_semi_declaration)
1286 << FixItHint::CreateReplacement(CommaLoc, ";");
1287 ExpectSemi = false;
1288 break;
1289 }
John McCalld8ac0572009-11-03 19:26:08 +00001290
1291 // Parse the next declarator.
1292 D.clear();
Richard Smith7984de32012-01-12 23:53:29 +00001293 D.setCommaLoc(CommaLoc);
John McCalld8ac0572009-11-03 19:26:08 +00001294
1295 // Accept attributes in an init-declarator. In the first declarator in a
1296 // declaration, these would be part of the declspec. In subsequent
1297 // declarators, they become part of the declarator itself, so that they
1298 // don't apply to declarators after *this* one. Examples:
1299 // short __attribute__((common)) var; -> declspec
1300 // short var __attribute__((common)); -> declarator
1301 // short x, __attribute__((common)) var; -> declarator
John McCall7f040a92010-12-24 02:08:15 +00001302 MaybeParseGNUAttributes(D);
John McCalld8ac0572009-11-03 19:26:08 +00001303
1304 ParseDeclarator(D);
Fariborz Jahanian9baf39d2012-01-13 00:14:12 +00001305 if (!D.isInvalidType()) {
1306 Decl *ThisDecl = ParseDeclarationAfterDeclarator(D);
1307 D.complete(ThisDecl);
1308 if (ThisDecl)
1309 DeclsInGroup.push_back(ThisDecl);
1310 }
John McCalld8ac0572009-11-03 19:26:08 +00001311 }
1312
1313 if (DeclEnd)
1314 *DeclEnd = Tok.getLocation();
1315
Richard Smith0706df42011-10-19 21:33:05 +00001316 if (ExpectSemi &&
Chris Lattner8bb21d32012-04-28 16:12:17 +00001317 ExpectAndConsumeSemi(Context == Declarator::FileContext
1318 ? diag::err_invalid_token_after_toplevel_declarator
1319 : diag::err_expected_semi_declaration)) {
Chris Lattner004659a2010-07-11 22:42:07 +00001320 // Okay, there was no semicolon and one was expected. If we see a
1321 // declaration specifier, just assume it was missing and continue parsing.
1322 // Otherwise things are very confused and we skip to recover.
1323 if (!isDeclarationSpecifier()) {
1324 SkipUntil(tok::r_brace, true, true);
1325 if (Tok.is(tok::semi))
1326 ConsumeToken();
1327 }
John McCalld8ac0572009-11-03 19:26:08 +00001328 }
1329
Douglas Gregor23c94db2010-07-02 17:43:08 +00001330 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS,
John McCalld8ac0572009-11-03 19:26:08 +00001331 DeclsInGroup.data(),
1332 DeclsInGroup.size());
Reid Spencer5f016e22007-07-11 17:01:13 +00001333}
1334
Richard Smithad762fc2011-04-14 22:09:26 +00001335/// Parse an optional simple-asm-expr and attributes, and attach them to a
1336/// declarator. Returns true on an error.
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001337bool Parser::ParseAsmAttributesAfterDeclarator(Declarator &D) {
Richard Smithad762fc2011-04-14 22:09:26 +00001338 // If a simple-asm-expr is present, parse it.
1339 if (Tok.is(tok::kw_asm)) {
1340 SourceLocation Loc;
1341 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
1342 if (AsmLabel.isInvalid()) {
1343 SkipUntil(tok::semi, true, true);
1344 return true;
1345 }
1346
1347 D.setAsmLabel(AsmLabel.release());
1348 D.SetRangeEnd(Loc);
1349 }
1350
1351 MaybeParseGNUAttributes(D);
1352 return false;
1353}
1354
Douglas Gregor1426e532009-05-12 21:31:51 +00001355/// \brief Parse 'declaration' after parsing 'declaration-specifiers
1356/// declarator'. This method parses the remainder of the declaration
1357/// (including any attributes or initializer, among other things) and
1358/// finalizes the declaration.
Reid Spencer5f016e22007-07-11 17:01:13 +00001359///
Reid Spencer5f016e22007-07-11 17:01:13 +00001360/// init-declarator: [C99 6.7]
1361/// declarator
1362/// declarator '=' initializer
1363/// [GNU] declarator simple-asm-expr[opt] attributes[opt]
1364/// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00001365/// [C++] declarator initializer[opt]
1366///
1367/// [C++] initializer:
1368/// [C++] '=' initializer-clause
1369/// [C++] '(' expression-list ')'
Sebastian Redl50de12f2009-03-24 22:27:57 +00001370/// [C++0x] '=' 'default' [TODO]
1371/// [C++0x] '=' 'delete'
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001372/// [C++0x] braced-init-list
Sebastian Redl50de12f2009-03-24 22:27:57 +00001373///
1374/// According to the standard grammar, =default and =delete are function
1375/// definitions, but that definitely doesn't fit with the parser here.
Reid Spencer5f016e22007-07-11 17:01:13 +00001376///
John McCalld226f652010-08-21 09:40:31 +00001377Decl *Parser::ParseDeclarationAfterDeclarator(Declarator &D,
Douglas Gregore542c862009-06-23 23:11:28 +00001378 const ParsedTemplateInfo &TemplateInfo) {
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001379 if (ParseAsmAttributesAfterDeclarator(D))
Richard Smithad762fc2011-04-14 22:09:26 +00001380 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001381
Richard Smithad762fc2011-04-14 22:09:26 +00001382 return ParseDeclarationAfterDeclaratorAndAttributes(D, TemplateInfo);
1383}
Mike Stump1eb44332009-09-09 15:08:12 +00001384
Richard Smithad762fc2011-04-14 22:09:26 +00001385Decl *Parser::ParseDeclarationAfterDeclaratorAndAttributes(Declarator &D,
1386 const ParsedTemplateInfo &TemplateInfo) {
Douglas Gregor1426e532009-05-12 21:31:51 +00001387 // Inform the current actions module that we just parsed this declarator.
John McCalld226f652010-08-21 09:40:31 +00001388 Decl *ThisDecl = 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +00001389 switch (TemplateInfo.Kind) {
1390 case ParsedTemplateInfo::NonTemplate:
Douglas Gregor23c94db2010-07-02 17:43:08 +00001391 ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
Douglas Gregord5a423b2009-09-25 18:43:00 +00001392 break;
1393
1394 case ParsedTemplateInfo::Template:
1395 case ParsedTemplateInfo::ExplicitSpecialization:
Douglas Gregor23c94db2010-07-02 17:43:08 +00001396 ThisDecl = Actions.ActOnTemplateDeclarator(getCurScope(),
John McCallf312b1e2010-08-26 23:41:50 +00001397 MultiTemplateParamsArg(Actions,
Douglas Gregore542c862009-06-23 23:11:28 +00001398 TemplateInfo.TemplateParams->data(),
1399 TemplateInfo.TemplateParams->size()),
Douglas Gregord5a423b2009-09-25 18:43:00 +00001400 D);
1401 break;
1402
1403 case ParsedTemplateInfo::ExplicitInstantiation: {
John McCalld226f652010-08-21 09:40:31 +00001404 DeclResult ThisRes
Douglas Gregor23c94db2010-07-02 17:43:08 +00001405 = Actions.ActOnExplicitInstantiation(getCurScope(),
Douglas Gregord5a423b2009-09-25 18:43:00 +00001406 TemplateInfo.ExternLoc,
1407 TemplateInfo.TemplateLoc,
1408 D);
1409 if (ThisRes.isInvalid()) {
1410 SkipUntil(tok::semi, true, true);
John McCalld226f652010-08-21 09:40:31 +00001411 return 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +00001412 }
1413
1414 ThisDecl = ThisRes.get();
1415 break;
1416 }
1417 }
Mike Stump1eb44332009-09-09 15:08:12 +00001418
Richard Smith34b41d92011-02-20 03:19:35 +00001419 bool TypeContainsAuto =
1420 D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto;
1421
Douglas Gregor1426e532009-05-12 21:31:51 +00001422 // Parse declarator '=' initializer.
Richard Trieud6c7c672012-01-18 22:54:52 +00001423 // If a '==' or '+=' is found, suggest a fixit to '='.
Richard Trieufcaf27e2012-01-19 22:01:51 +00001424 if (isTokenEqualOrEqualTypo()) {
Douglas Gregor1426e532009-05-12 21:31:51 +00001425 ConsumeToken();
Anders Carlsson37bf9d22010-09-24 21:25:25 +00001426 if (Tok.is(tok::kw_delete)) {
Sean Hunte4246a62011-05-12 06:15:49 +00001427 if (D.isFunctionDeclarator())
1428 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
1429 << 1 /* delete */;
1430 else
1431 Diag(ConsumeToken(), diag::err_deleted_non_function);
Sean Huntfe2695e2011-05-06 01:42:00 +00001432 } else if (Tok.is(tok::kw_default)) {
Sean Hunte4246a62011-05-12 06:15:49 +00001433 if (D.isFunctionDeclarator())
Sebastian Redlecfcd562012-02-11 23:51:21 +00001434 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
1435 << 0 /* default */;
Sean Hunte4246a62011-05-12 06:15:49 +00001436 else
1437 Diag(ConsumeToken(), diag::err_default_special_members);
Douglas Gregor1426e532009-05-12 21:31:51 +00001438 } else {
David Blaikie4e4d0842012-03-11 07:00:24 +00001439 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
John McCall731ad842009-12-19 09:28:58 +00001440 EnterScope(0);
Douglas Gregor23c94db2010-07-02 17:43:08 +00001441 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
John McCall731ad842009-12-19 09:28:58 +00001442 }
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +00001443
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +00001444 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001445 Actions.CodeCompleteInitializer(getCurScope(), ThisDecl);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001446 cutOffParsing();
1447 return 0;
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +00001448 }
1449
John McCall60d7b3a2010-08-24 06:29:42 +00001450 ExprResult Init(ParseInitializer());
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +00001451
David Blaikie4e4d0842012-03-11 07:00:24 +00001452 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001453 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
John McCall731ad842009-12-19 09:28:58 +00001454 ExitScope();
1455 }
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +00001456
Douglas Gregor1426e532009-05-12 21:31:51 +00001457 if (Init.isInvalid()) {
Douglas Gregor00225542010-03-01 18:27:54 +00001458 SkipUntil(tok::comma, true, true);
1459 Actions.ActOnInitializerError(ThisDecl);
1460 } else
Richard Smith34b41d92011-02-20 03:19:35 +00001461 Actions.AddInitializerToDecl(ThisDecl, Init.take(),
1462 /*DirectInit=*/false, TypeContainsAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +00001463 }
1464 } else if (Tok.is(tok::l_paren)) {
1465 // Parse C++ direct initializer: '(' expression-list ')'
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001466 BalancedDelimiterTracker T(*this, tok::l_paren);
1467 T.consumeOpen();
1468
Douglas Gregor1426e532009-05-12 21:31:51 +00001469 ExprVector Exprs(Actions);
1470 CommaLocsTy CommaLocs;
1471
David Blaikie4e4d0842012-03-11 07:00:24 +00001472 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregorb4debae2009-12-22 17:47:17 +00001473 EnterScope(0);
Douglas Gregor23c94db2010-07-02 17:43:08 +00001474 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001475 }
1476
Douglas Gregor1426e532009-05-12 21:31:51 +00001477 if (ParseExpressionList(Exprs, CommaLocs)) {
1478 SkipUntil(tok::r_paren);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001479
David Blaikie4e4d0842012-03-11 07:00:24 +00001480 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001481 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001482 ExitScope();
1483 }
Douglas Gregor1426e532009-05-12 21:31:51 +00001484 } else {
1485 // Match the ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001486 T.consumeClose();
Douglas Gregor1426e532009-05-12 21:31:51 +00001487
1488 assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() &&
1489 "Unexpected number of commas!");
Douglas Gregorb4debae2009-12-22 17:47:17 +00001490
David Blaikie4e4d0842012-03-11 07:00:24 +00001491 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001492 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001493 ExitScope();
1494 }
1495
Sebastian Redl5b9cc5d2012-02-11 23:51:47 +00001496 ExprResult Initializer = Actions.ActOnParenListExpr(T.getOpenLocation(),
1497 T.getCloseLocation(),
1498 move_arg(Exprs));
1499 Actions.AddInitializerToDecl(ThisDecl, Initializer.take(),
1500 /*DirectInit=*/true, TypeContainsAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +00001501 }
David Blaikie4e4d0842012-03-11 07:00:24 +00001502 } else if (getLangOpts().CPlusPlus0x && Tok.is(tok::l_brace)) {
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001503 // Parse C++0x braced-init-list.
Richard Smith7fe62082011-10-15 05:09:34 +00001504 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
1505
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001506 if (D.getCXXScopeSpec().isSet()) {
1507 EnterScope(0);
1508 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
1509 }
1510
1511 ExprResult Init(ParseBraceInitializer());
1512
1513 if (D.getCXXScopeSpec().isSet()) {
1514 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
1515 ExitScope();
1516 }
1517
1518 if (Init.isInvalid()) {
1519 Actions.ActOnInitializerError(ThisDecl);
1520 } else
1521 Actions.AddInitializerToDecl(ThisDecl, Init.take(),
1522 /*DirectInit=*/true, TypeContainsAuto);
1523
Douglas Gregor1426e532009-05-12 21:31:51 +00001524 } else {
Richard Smith34b41d92011-02-20 03:19:35 +00001525 Actions.ActOnUninitializedDecl(ThisDecl, TypeContainsAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +00001526 }
1527
Richard Smith483b9f32011-02-21 20:05:19 +00001528 Actions.FinalizeDeclaration(ThisDecl);
1529
Douglas Gregor1426e532009-05-12 21:31:51 +00001530 return ThisDecl;
1531}
1532
Reid Spencer5f016e22007-07-11 17:01:13 +00001533/// ParseSpecifierQualifierList
1534/// specifier-qualifier-list:
1535/// type-specifier specifier-qualifier-list[opt]
1536/// type-qualifier specifier-qualifier-list[opt]
1537/// [GNU] attributes specifier-qualifier-list[opt]
1538///
Richard Smith69730c12012-03-12 07:56:15 +00001539void Parser::ParseSpecifierQualifierList(DeclSpec &DS, AccessSpecifier AS,
1540 DeclSpecContext DSC) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001541 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
1542 /// parse declaration-specifiers and complain about extra stuff.
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001543 /// TODO: diagnose attribute-specifiers and alignment-specifiers.
Richard Smith69730c12012-03-12 07:56:15 +00001544 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS, DSC);
Mike Stump1eb44332009-09-09 15:08:12 +00001545
Reid Spencer5f016e22007-07-11 17:01:13 +00001546 // Validate declspec for type-name.
1547 unsigned Specs = DS.getParsedSpecifiers();
Richard Smitha971d242012-05-09 20:55:26 +00001548 if ((DSC == DSC_type_specifier || DSC == DSC_trailing) &&
1549 !DS.hasTypeSpecifier()) {
Richard Smith69730c12012-03-12 07:56:15 +00001550 Diag(Tok, diag::err_expected_type);
1551 DS.SetTypeSpecError();
1552 } else if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers() &&
1553 !DS.hasAttributes()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001554 Diag(Tok, diag::err_typename_requires_specqual);
Richard Smith69730c12012-03-12 07:56:15 +00001555 if (!DS.hasTypeSpecifier())
1556 DS.SetTypeSpecError();
1557 }
Mike Stump1eb44332009-09-09 15:08:12 +00001558
Reid Spencer5f016e22007-07-11 17:01:13 +00001559 // Issue diagnostic and remove storage class if present.
1560 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
1561 if (DS.getStorageClassSpecLoc().isValid())
1562 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
1563 else
1564 Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass);
1565 DS.ClearStorageClassSpecs();
1566 }
Mike Stump1eb44332009-09-09 15:08:12 +00001567
Reid Spencer5f016e22007-07-11 17:01:13 +00001568 // Issue diagnostic and remove function specfier if present.
1569 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Douglas Gregorb48fe382008-10-31 09:07:45 +00001570 if (DS.isInlineSpecified())
1571 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
1572 if (DS.isVirtualSpecified())
1573 Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec);
1574 if (DS.isExplicitSpecified())
1575 Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec);
Reid Spencer5f016e22007-07-11 17:01:13 +00001576 DS.ClearFunctionSpecs();
1577 }
Richard Smith69730c12012-03-12 07:56:15 +00001578
1579 // Issue diagnostic and remove constexpr specfier if present.
1580 if (DS.isConstexprSpecified()) {
1581 Diag(DS.getConstexprSpecLoc(), diag::err_typename_invalid_constexpr);
1582 DS.ClearConstexprSpec();
1583 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001584}
1585
Chris Lattnerc199ab32009-04-12 20:42:31 +00001586/// isValidAfterIdentifierInDeclaratorAfterDeclSpec - Return true if the
1587/// specified token is valid after the identifier in a declarator which
1588/// immediately follows the declspec. For example, these things are valid:
1589///
1590/// int x [ 4]; // direct-declarator
1591/// int x ( int y); // direct-declarator
1592/// int(int x ) // direct-declarator
1593/// int x ; // simple-declaration
1594/// int x = 17; // init-declarator-list
1595/// int x , y; // init-declarator-list
1596/// int x __asm__ ("foo"); // init-declarator-list
Chris Lattnerb6645dd2009-04-14 21:16:09 +00001597/// int x : 4; // struct-declarator
Chris Lattnerc83c27a2009-04-12 22:29:43 +00001598/// int x { 5}; // C++'0x unified initializers
Chris Lattnerc199ab32009-04-12 20:42:31 +00001599///
1600/// This is not, because 'x' does not immediately follow the declspec (though
1601/// ')' happens to be valid anyway).
1602/// int (x)
1603///
1604static bool isValidAfterIdentifierInDeclarator(const Token &T) {
1605 return T.is(tok::l_square) || T.is(tok::l_paren) || T.is(tok::r_paren) ||
1606 T.is(tok::semi) || T.is(tok::comma) || T.is(tok::equal) ||
Chris Lattnerb6645dd2009-04-14 21:16:09 +00001607 T.is(tok::kw_asm) || T.is(tok::l_brace) || T.is(tok::colon);
Chris Lattnerc199ab32009-04-12 20:42:31 +00001608}
1609
Chris Lattnere40c2952009-04-14 21:34:55 +00001610
1611/// ParseImplicitInt - This method is called when we have an non-typename
1612/// identifier in a declspec (which normally terminates the decl spec) when
1613/// the declspec has no type specifier. In this case, the declspec is either
1614/// malformed or is "implicit int" (in K&R and C89).
1615///
1616/// This method handles diagnosing this prettily and returns false if the
1617/// declspec is done being processed. If it recovers and thinks there may be
1618/// other pieces of declspec after it, it returns true.
1619///
Chris Lattnerf4382f52009-04-14 22:17:06 +00001620bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001621 const ParsedTemplateInfo &TemplateInfo,
Richard Smith69730c12012-03-12 07:56:15 +00001622 AccessSpecifier AS, DeclSpecContext DSC) {
Chris Lattnerf4382f52009-04-14 22:17:06 +00001623 assert(Tok.is(tok::identifier) && "should have identifier");
Mike Stump1eb44332009-09-09 15:08:12 +00001624
Chris Lattnere40c2952009-04-14 21:34:55 +00001625 SourceLocation Loc = Tok.getLocation();
1626 // If we see an identifier that is not a type name, we normally would
1627 // parse it as the identifer being declared. However, when a typename
1628 // is typo'd or the definition is not included, this will incorrectly
1629 // parse the typename as the identifier name and fall over misparsing
1630 // later parts of the diagnostic.
1631 //
1632 // As such, we try to do some look-ahead in cases where this would
1633 // otherwise be an "implicit-int" case to see if this is invalid. For
1634 // example: "static foo_t x = 4;" In this case, if we parsed foo_t as
1635 // an identifier with implicit int, we'd get a parse error because the
1636 // next token is obviously invalid for a type. Parse these as a case
1637 // with an invalid type specifier.
1638 assert(!DS.hasTypeSpecifier() && "Type specifier checked above");
Mike Stump1eb44332009-09-09 15:08:12 +00001639
Chris Lattnere40c2952009-04-14 21:34:55 +00001640 // Since we know that this either implicit int (which is rare) or an
Richard Smith827adaf2012-05-15 21:01:51 +00001641 // error, do lookahead to try to do better recovery. This never applies
1642 // within a type specifier. Outside of C++, we allow this even if the
1643 // language doesn't "officially" support implicit int -- we support
1644 // implicit int as an extension in C99 and C11. Allegedly, MS also
1645 // supports implicit int in C++ mode.
Richard Smitha971d242012-05-09 20:55:26 +00001646 if (DSC != DSC_type_specifier && DSC != DSC_trailing &&
Richard Smith827adaf2012-05-15 21:01:51 +00001647 (!getLangOpts().CPlusPlus || getLangOpts().MicrosoftExt) &&
Richard Smith69730c12012-03-12 07:56:15 +00001648 isValidAfterIdentifierInDeclarator(NextToken())) {
Chris Lattnere40c2952009-04-14 21:34:55 +00001649 // If this token is valid for implicit int, e.g. "static x = 4", then
1650 // we just avoid eating the identifier, so it will be parsed as the
1651 // identifier in the declarator.
1652 return false;
1653 }
Mike Stump1eb44332009-09-09 15:08:12 +00001654
Richard Smith827adaf2012-05-15 21:01:51 +00001655 if (getLangOpts().CPlusPlus &&
1656 DS.getStorageClassSpec() == DeclSpec::SCS_auto) {
1657 // Don't require a type specifier if we have the 'auto' storage class
1658 // specifier in C++98 -- we'll promote it to a type specifier.
1659 return false;
1660 }
1661
Chris Lattnere40c2952009-04-14 21:34:55 +00001662 // Otherwise, if we don't consume this token, we are going to emit an
1663 // error anyway. Try to recover from various common problems. Check
1664 // to see if this was a reference to a tag name without a tag specified.
1665 // This is a common problem in C (saying 'foo' instead of 'struct foo').
Chris Lattnerf4382f52009-04-14 22:17:06 +00001666 //
1667 // C++ doesn't need this, and isTagName doesn't take SS.
1668 if (SS == 0) {
Argyrios Kyrtzidisb8a9d3b2011-04-21 17:29:47 +00001669 const char *TagName = 0, *FixitTagName = 0;
Chris Lattnerf4382f52009-04-14 22:17:06 +00001670 tok::TokenKind TagKind = tok::unknown;
Mike Stump1eb44332009-09-09 15:08:12 +00001671
Douglas Gregor23c94db2010-07-02 17:43:08 +00001672 switch (Actions.isTagName(*Tok.getIdentifierInfo(), getCurScope())) {
Chris Lattnere40c2952009-04-14 21:34:55 +00001673 default: break;
Argyrios Kyrtzidisb8a9d3b2011-04-21 17:29:47 +00001674 case DeclSpec::TST_enum:
1675 TagName="enum" ; FixitTagName = "enum " ; TagKind=tok::kw_enum ;break;
1676 case DeclSpec::TST_union:
1677 TagName="union" ; FixitTagName = "union " ;TagKind=tok::kw_union ;break;
1678 case DeclSpec::TST_struct:
1679 TagName="struct"; FixitTagName = "struct ";TagKind=tok::kw_struct;break;
1680 case DeclSpec::TST_class:
1681 TagName="class" ; FixitTagName = "class " ;TagKind=tok::kw_class ;break;
Chris Lattnere40c2952009-04-14 21:34:55 +00001682 }
Mike Stump1eb44332009-09-09 15:08:12 +00001683
Chris Lattnerf4382f52009-04-14 22:17:06 +00001684 if (TagName) {
Kaelyn Uhrainaec2ac62012-04-26 23:36:17 +00001685 IdentifierInfo *TokenName = Tok.getIdentifierInfo();
1686 LookupResult R(Actions, TokenName, SourceLocation(),
1687 Sema::LookupOrdinaryName);
1688
Chris Lattnerf4382f52009-04-14 22:17:06 +00001689 Diag(Loc, diag::err_use_of_tag_name_without_tag)
Kaelyn Uhrainaec2ac62012-04-26 23:36:17 +00001690 << TokenName << TagName << getLangOpts().CPlusPlus
1691 << FixItHint::CreateInsertion(Tok.getLocation(), FixitTagName);
1692
1693 if (Actions.LookupParsedName(R, getCurScope(), SS)) {
1694 for (LookupResult::iterator I = R.begin(), IEnd = R.end();
1695 I != IEnd; ++I)
Kaelyn Uhrain392b3f52012-04-27 18:26:49 +00001696 Diag((*I)->getLocation(), diag::note_decl_hiding_tag_type)
Kaelyn Uhrainaec2ac62012-04-26 23:36:17 +00001697 << TokenName << TagName;
1698 }
Mike Stump1eb44332009-09-09 15:08:12 +00001699
Chris Lattnerf4382f52009-04-14 22:17:06 +00001700 // Parse this as a tag as if the missing tag were present.
1701 if (TagKind == tok::kw_enum)
Richard Smith69730c12012-03-12 07:56:15 +00001702 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS, DSC_normal);
Chris Lattnerf4382f52009-04-14 22:17:06 +00001703 else
Richard Smith69730c12012-03-12 07:56:15 +00001704 ParseClassSpecifier(TagKind, Loc, DS, TemplateInfo, AS,
1705 /*EnteringContext*/ false, DSC_normal);
Chris Lattnerf4382f52009-04-14 22:17:06 +00001706 return true;
1707 }
Chris Lattnere40c2952009-04-14 21:34:55 +00001708 }
Mike Stump1eb44332009-09-09 15:08:12 +00001709
Richard Smith8f0a7e72012-05-15 21:29:55 +00001710 // Determine whether this identifier could plausibly be the name of something
1711 // being declared (with a missign type).
1712 if (DSC != DSC_type_specifier && DSC != DSC_trailing &&
1713 (!SS || DSC == DSC_top_level || DSC == DSC_class)) {
Richard Smith827adaf2012-05-15 21:01:51 +00001714 // Look ahead to the next token to try to figure out what this declaration
1715 // was supposed to be.
1716 switch (NextToken().getKind()) {
1717 case tok::comma:
1718 case tok::equal:
1719 case tok::kw_asm:
1720 case tok::l_brace:
1721 case tok::l_square:
1722 case tok::semi:
1723 // This looks like a variable declaration. The type is probably missing.
1724 // We're done parsing decl-specifiers.
1725 return false;
1726
1727 case tok::l_paren: {
1728 // static x(4); // 'x' is not a type
1729 // x(int n); // 'x' is not a type
1730 // x (*p)[]; // 'x' is a type
1731 //
1732 // Since we're in an error case (or the rare 'implicit int in C++' MS
1733 // extension), we can afford to perform a tentative parse to determine
1734 // which case we're in.
1735 TentativeParsingAction PA(*this);
1736 ConsumeToken();
1737 TPResult TPR = TryParseDeclarator(/*mayBeAbstract*/false);
1738 PA.Revert();
1739 if (TPR == TPResult::False())
1740 return false;
1741 // The identifier is followed by a parenthesized declarator.
1742 // It's supposed to be a type.
1743 break;
1744 }
1745
1746 default:
1747 // This is probably supposed to be a type. This includes cases like:
1748 // int f(itn);
1749 // struct S { unsinged : 4; };
1750 break;
1751 }
1752 }
1753
Douglas Gregora786fdb2009-10-13 23:27:22 +00001754 // This is almost certainly an invalid type name. Let the action emit a
1755 // diagnostic and attempt to recover.
John McCallb3d87482010-08-24 05:47:05 +00001756 ParsedType T;
Douglas Gregora786fdb2009-10-13 23:27:22 +00001757 if (Actions.DiagnoseUnknownTypeName(*Tok.getIdentifierInfo(), Loc,
Douglas Gregor23c94db2010-07-02 17:43:08 +00001758 getCurScope(), SS, T)) {
Douglas Gregora786fdb2009-10-13 23:27:22 +00001759 // The action emitted a diagnostic, so we don't have to.
1760 if (T) {
1761 // The action has suggested that the type T could be used. Set that as
1762 // the type in the declaration specifiers, consume the would-be type
1763 // name token, and we're done.
1764 const char *PrevSpec;
1765 unsigned DiagID;
John McCallb3d87482010-08-24 05:47:05 +00001766 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID, T);
Douglas Gregora786fdb2009-10-13 23:27:22 +00001767 DS.SetRangeEnd(Tok.getLocation());
1768 ConsumeToken();
1769
1770 // There may be other declaration specifiers after this.
1771 return true;
1772 }
1773
1774 // Fall through; the action had no suggestion for us.
1775 } else {
1776 // The action did not emit a diagnostic, so emit one now.
1777 SourceRange R;
1778 if (SS) R = SS->getRange();
1779 Diag(Loc, diag::err_unknown_typename) << Tok.getIdentifierInfo() << R;
1780 }
Mike Stump1eb44332009-09-09 15:08:12 +00001781
Douglas Gregora786fdb2009-10-13 23:27:22 +00001782 // Mark this as an error.
Richard Smith69730c12012-03-12 07:56:15 +00001783 DS.SetTypeSpecError();
Chris Lattnere40c2952009-04-14 21:34:55 +00001784 DS.SetRangeEnd(Tok.getLocation());
1785 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001786
Chris Lattnere40c2952009-04-14 21:34:55 +00001787 // TODO: Could inject an invalid typedef decl in an enclosing scope to
1788 // avoid rippling error messages on subsequent uses of the same type,
1789 // could be useful if #include was forgotten.
1790 return false;
1791}
1792
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001793/// \brief Determine the declaration specifier context from the declarator
1794/// context.
1795///
1796/// \param Context the declarator context, which is one of the
1797/// Declarator::TheContext enumerator values.
1798Parser::DeclSpecContext
1799Parser::getDeclSpecContextFromDeclaratorContext(unsigned Context) {
1800 if (Context == Declarator::MemberContext)
1801 return DSC_class;
1802 if (Context == Declarator::FileContext)
1803 return DSC_top_level;
Richard Smith6d96d3a2012-03-15 01:02:11 +00001804 if (Context == Declarator::TrailingReturnContext)
1805 return DSC_trailing;
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001806 return DSC_normal;
1807}
1808
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001809/// ParseAlignArgument - Parse the argument to an alignment-specifier.
1810///
1811/// FIXME: Simply returns an alignof() expression if the argument is a
1812/// type. Ideally, the type should be propagated directly into Sema.
1813///
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00001814/// [C11] type-id
1815/// [C11] constant-expression
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00001816/// [C++0x] type-id ...[opt]
1817/// [C++0x] assignment-expression ...[opt]
1818ExprResult Parser::ParseAlignArgument(SourceLocation Start,
1819 SourceLocation &EllipsisLoc) {
1820 ExprResult ER;
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001821 if (isTypeIdInParens()) {
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001822 SourceLocation TypeLoc = Tok.getLocation();
1823 ParsedType Ty = ParseTypeName().get();
1824 SourceRange TypeRange(Start, Tok.getLocation());
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00001825 ER = Actions.ActOnUnaryExprOrTypeTraitExpr(TypeLoc, UETT_AlignOf, true,
1826 Ty.getAsOpaquePtr(), TypeRange);
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001827 } else
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00001828 ER = ParseConstantExpression();
1829
David Blaikie4e4d0842012-03-11 07:00:24 +00001830 if (getLangOpts().CPlusPlus0x && Tok.is(tok::ellipsis))
Peter Collingbournefe9b2a82011-10-24 17:56:00 +00001831 EllipsisLoc = ConsumeToken();
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00001832
1833 return ER;
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001834}
1835
1836/// ParseAlignmentSpecifier - Parse an alignment-specifier, and add the
1837/// attribute to Attrs.
1838///
1839/// alignment-specifier:
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00001840/// [C11] '_Alignas' '(' type-id ')'
1841/// [C11] '_Alignas' '(' constant-expression ')'
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00001842/// [C++0x] 'alignas' '(' type-id ...[opt] ')'
1843/// [C++0x] 'alignas' '(' assignment-expression ...[opt] ')'
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001844void Parser::ParseAlignmentSpecifier(ParsedAttributes &Attrs,
1845 SourceLocation *endLoc) {
1846 assert((Tok.is(tok::kw_alignas) || Tok.is(tok::kw__Alignas)) &&
1847 "Not an alignment-specifier!");
1848
1849 SourceLocation KWLoc = Tok.getLocation();
1850 ConsumeToken();
1851
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001852 BalancedDelimiterTracker T(*this, tok::l_paren);
1853 if (T.expectAndConsume(diag::err_expected_lparen))
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001854 return;
1855
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00001856 SourceLocation EllipsisLoc;
1857 ExprResult ArgExpr = ParseAlignArgument(T.getOpenLocation(), EllipsisLoc);
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001858 if (ArgExpr.isInvalid()) {
1859 SkipUntil(tok::r_paren);
1860 return;
1861 }
1862
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001863 T.consumeClose();
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001864 if (endLoc)
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001865 *endLoc = T.getCloseLocation();
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001866
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00001867 // FIXME: Handle pack-expansions here.
1868 if (EllipsisLoc.isValid()) {
1869 Diag(EllipsisLoc, diag::err_alignas_pack_exp_unsupported);
1870 return;
1871 }
1872
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001873 ExprVector ArgExprs(Actions);
1874 ArgExprs.push_back(ArgExpr.release());
1875 Attrs.addNew(PP.getIdentifierInfo("aligned"), KWLoc, 0, KWLoc,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001876 0, T.getOpenLocation(), ArgExprs.take(), 1, false, true);
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001877}
1878
Reid Spencer5f016e22007-07-11 17:01:13 +00001879/// ParseDeclarationSpecifiers
1880/// declaration-specifiers: [C99 6.7]
1881/// storage-class-specifier declaration-specifiers[opt]
1882/// type-specifier declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00001883/// [C99] function-specifier declaration-specifiers[opt]
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00001884/// [C11] alignment-specifier declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00001885/// [GNU] attributes declaration-specifiers[opt]
Douglas Gregor8d267c52011-09-09 02:06:17 +00001886/// [Clang] '__module_private__' declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00001887///
1888/// storage-class-specifier: [C99 6.7.1]
1889/// 'typedef'
1890/// 'extern'
1891/// 'static'
1892/// 'auto'
1893/// 'register'
Sebastian Redl669d5d72008-11-14 23:42:31 +00001894/// [C++] 'mutable'
Reid Spencer5f016e22007-07-11 17:01:13 +00001895/// [GNU] '__thread'
Reid Spencer5f016e22007-07-11 17:01:13 +00001896/// function-specifier: [C99 6.7.4]
1897/// [C99] 'inline'
Douglas Gregorb48fe382008-10-31 09:07:45 +00001898/// [C++] 'virtual'
1899/// [C++] 'explicit'
Peter Collingbournef315fa82011-02-14 01:42:53 +00001900/// [OpenCL] '__kernel'
Anders Carlssonf47f7a12009-05-06 04:46:28 +00001901/// 'friend': [C++ dcl.friend]
Sebastian Redl2ac67232009-11-05 15:47:02 +00001902/// 'constexpr': [C++0x dcl.constexpr]
Anders Carlssonf47f7a12009-05-06 04:46:28 +00001903
Reid Spencer5f016e22007-07-11 17:01:13 +00001904///
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +00001905void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001906 const ParsedTemplateInfo &TemplateInfo,
John McCall67d1a672009-08-06 02:15:43 +00001907 AccessSpecifier AS,
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +00001908 DeclSpecContext DSContext,
1909 LateParsedAttrList *LateAttrs) {
Douglas Gregor312eadb2011-04-24 05:37:28 +00001910 if (DS.getSourceRange().isInvalid()) {
1911 DS.SetRangeStart(Tok.getLocation());
1912 DS.SetRangeEnd(Tok.getLocation());
1913 }
1914
Douglas Gregorefaa93a2011-11-07 17:33:42 +00001915 bool EnteringContext = (DSContext == DSC_class || DSContext == DSC_top_level);
Reid Spencer5f016e22007-07-11 17:01:13 +00001916 while (1) {
John McCallfec54012009-08-03 20:12:06 +00001917 bool isInvalid = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001918 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00001919 unsigned DiagID = 0;
1920
Reid Spencer5f016e22007-07-11 17:01:13 +00001921 SourceLocation Loc = Tok.getLocation();
Douglas Gregor12e083c2008-11-07 15:42:26 +00001922
Reid Spencer5f016e22007-07-11 17:01:13 +00001923 switch (Tok.getKind()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001924 default:
Chris Lattnerbce61352008-07-26 00:20:22 +00001925 DoneWithDeclSpec:
Peter Collingbournef1907682011-09-29 18:03:57 +00001926 // [C++0x] decl-specifier-seq: decl-specifier attribute-specifier-seq[opt]
1927 MaybeParseCXX0XAttributes(DS.getAttributes());
1928
Reid Spencer5f016e22007-07-11 17:01:13 +00001929 // If this is not a declaration specifier token, we're done reading decl
1930 // specifiers. First verify that DeclSpec's are consistent.
Douglas Gregor9b3064b2009-04-01 22:41:11 +00001931 DS.Finish(Diags, PP);
Reid Spencer5f016e22007-07-11 17:01:13 +00001932 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001933
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001934 case tok::code_completion: {
John McCallf312b1e2010-08-26 23:41:50 +00001935 Sema::ParserCompletionContext CCC = Sema::PCC_Namespace;
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001936 if (DS.hasTypeSpecifier()) {
1937 bool AllowNonIdentifiers
1938 = (getCurScope()->getFlags() & (Scope::ControlScope |
1939 Scope::BlockScope |
1940 Scope::TemplateParamScope |
1941 Scope::FunctionPrototypeScope |
1942 Scope::AtCatchScope)) == 0;
1943 bool AllowNestedNameSpecifiers
1944 = DSContext == DSC_top_level ||
1945 (DSContext == DSC_class && DS.isFriendSpecified());
1946
Douglas Gregorc7b6d882010-09-16 15:14:18 +00001947 Actions.CodeCompleteDeclSpec(getCurScope(), DS,
1948 AllowNonIdentifiers,
1949 AllowNestedNameSpecifiers);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001950 return cutOffParsing();
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001951 }
1952
Douglas Gregor68e3c2e2011-02-15 20:33:25 +00001953 if (getCurScope()->getFnParent() || getCurScope()->getBlockParent())
1954 CCC = Sema::PCC_LocalDeclarationSpecifiers;
1955 else if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate)
John McCallf312b1e2010-08-26 23:41:50 +00001956 CCC = DSContext == DSC_class? Sema::PCC_MemberTemplate
1957 : Sema::PCC_Template;
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001958 else if (DSContext == DSC_class)
John McCallf312b1e2010-08-26 23:41:50 +00001959 CCC = Sema::PCC_Class;
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +00001960 else if (CurParsedObjCImpl)
John McCallf312b1e2010-08-26 23:41:50 +00001961 CCC = Sema::PCC_ObjCImplementation;
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001962
1963 Actions.CodeCompleteOrdinaryName(getCurScope(), CCC);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001964 return cutOffParsing();
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001965 }
1966
Chris Lattner5e02c472009-01-05 00:07:25 +00001967 case tok::coloncolon: // ::foo::bar
John McCall9ba61662010-02-26 08:45:28 +00001968 // C++ scope specifier. Annotate and loop, or bail out on error.
1969 if (TryAnnotateCXXScopeToken(true)) {
1970 if (!DS.hasTypeSpecifier())
1971 DS.SetTypeSpecError();
1972 goto DoneWithDeclSpec;
1973 }
John McCall2e0a7152010-03-01 18:20:46 +00001974 if (Tok.is(tok::coloncolon)) // ::new or ::delete
1975 goto DoneWithDeclSpec;
John McCall9ba61662010-02-26 08:45:28 +00001976 continue;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001977
1978 case tok::annot_cxxscope: {
Richard Smithf63eee72012-05-09 18:56:43 +00001979 if (DS.hasTypeSpecifier() || DS.isTypeAltiVecVector())
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001980 goto DoneWithDeclSpec;
1981
John McCallaa87d332009-12-12 11:40:51 +00001982 CXXScopeSpec SS;
Douglas Gregorc34348a2011-02-24 17:54:50 +00001983 Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(),
1984 Tok.getAnnotationRange(),
1985 SS);
John McCallaa87d332009-12-12 11:40:51 +00001986
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001987 // We are looking for a qualified typename.
Douglas Gregor9135c722009-03-25 15:40:00 +00001988 Token Next = NextToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001989 if (Next.is(tok::annot_template_id) &&
Douglas Gregor9135c722009-03-25 15:40:00 +00001990 static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue())
Douglas Gregorc45c2322009-03-31 00:43:58 +00001991 ->Kind == TNK_Type_template) {
Douglas Gregor9135c722009-03-25 15:40:00 +00001992 // We have a qualified template-id, e.g., N::A<int>
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001993
1994 // C++ [class.qual]p2:
1995 // In a lookup in which the constructor is an acceptable lookup
1996 // result and the nested-name-specifier nominates a class C:
1997 //
1998 // - if the name specified after the
1999 // nested-name-specifier, when looked up in C, is the
2000 // injected-class-name of C (Clause 9), or
2001 //
2002 // - if the name specified after the nested-name-specifier
2003 // is the same as the identifier or the
2004 // simple-template-id's template-name in the last
2005 // component of the nested-name-specifier,
2006 //
2007 // the name is instead considered to name the constructor of
2008 // class C.
2009 //
2010 // Thus, if the template-name is actually the constructor
2011 // name, then the code is ill-formed; this interpretation is
2012 // reinforced by the NAD status of core issue 635.
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +00002013 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Next);
John McCallba9d8532010-04-13 06:39:49 +00002014 if ((DSContext == DSC_top_level ||
2015 (DSContext == DSC_class && DS.isFriendSpecified())) &&
2016 TemplateId->Name &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00002017 Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) {
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002018 if (isConstructorDeclarator()) {
2019 // The user meant this to be an out-of-line constructor
2020 // definition, but template arguments are not allowed
2021 // there. Just allow this as a constructor; we'll
2022 // complain about it later.
2023 goto DoneWithDeclSpec;
2024 }
2025
2026 // The user meant this to name a type, but it actually names
2027 // a constructor with some extraneous template
2028 // arguments. Complain, then parse it as a type as the user
2029 // intended.
2030 Diag(TemplateId->TemplateNameLoc,
2031 diag::err_out_of_line_template_id_names_constructor)
2032 << TemplateId->Name;
2033 }
2034
John McCallaa87d332009-12-12 11:40:51 +00002035 DS.getTypeSpecScope() = SS;
2036 ConsumeToken(); // The C++ scope.
Mike Stump1eb44332009-09-09 15:08:12 +00002037 assert(Tok.is(tok::annot_template_id) &&
Douglas Gregor9135c722009-03-25 15:40:00 +00002038 "ParseOptionalCXXScopeSpecifier not working");
Douglas Gregor059101f2011-03-02 00:47:37 +00002039 AnnotateTemplateIdTokenAsType();
Douglas Gregor9135c722009-03-25 15:40:00 +00002040 continue;
2041 }
2042
Douglas Gregor9d7b3532009-09-28 07:26:33 +00002043 if (Next.is(tok::annot_typename)) {
John McCallaa87d332009-12-12 11:40:51 +00002044 DS.getTypeSpecScope() = SS;
2045 ConsumeToken(); // The C++ scope.
John McCallb3d87482010-08-24 05:47:05 +00002046 if (Tok.getAnnotationValue()) {
2047 ParsedType T = getTypeAnnotation(Tok);
Nico Weber253e80b2010-11-22 10:30:56 +00002048 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename,
2049 Tok.getAnnotationEndLoc(),
John McCallb3d87482010-08-24 05:47:05 +00002050 PrevSpec, DiagID, T);
2051 }
Douglas Gregor9d7b3532009-09-28 07:26:33 +00002052 else
2053 DS.SetTypeSpecError();
2054 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
2055 ConsumeToken(); // The typename
2056 }
2057
Douglas Gregor9135c722009-03-25 15:40:00 +00002058 if (Next.isNot(tok::identifier))
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002059 goto DoneWithDeclSpec;
2060
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002061 // If we're in a context where the identifier could be a class name,
2062 // check whether this is a constructor declaration.
John McCallba9d8532010-04-13 06:39:49 +00002063 if ((DSContext == DSC_top_level ||
2064 (DSContext == DSC_class && DS.isFriendSpecified())) &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00002065 Actions.isCurrentClassName(*Next.getIdentifierInfo(), getCurScope(),
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002066 &SS)) {
2067 if (isConstructorDeclarator())
2068 goto DoneWithDeclSpec;
2069
2070 // As noted in C++ [class.qual]p2 (cited above), when the name
2071 // of the class is qualified in a context where it could name
2072 // a constructor, its a constructor name. However, we've
2073 // looked at the declarator, and the user probably meant this
2074 // to be a type. Complain that it isn't supposed to be treated
2075 // as a type, then proceed to parse it as a type.
2076 Diag(Next.getLocation(), diag::err_out_of_line_type_names_constructor)
2077 << Next.getIdentifierInfo();
2078 }
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002079
John McCallb3d87482010-08-24 05:47:05 +00002080 ParsedType TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(),
2081 Next.getLocation(),
Douglas Gregor9e876872011-03-01 18:12:44 +00002082 getCurScope(), &SS,
2083 false, false, ParsedType(),
Abramo Bagnarafad03b72012-01-27 08:46:19 +00002084 /*IsCtorOrDtorName=*/false,
Douglas Gregor9e876872011-03-01 18:12:44 +00002085 /*NonTrivialSourceInfo=*/true);
Douglas Gregor55f6b142009-02-09 18:46:07 +00002086
Chris Lattnerf4382f52009-04-14 22:17:06 +00002087 // If the referenced identifier is not a type, then this declspec is
2088 // erroneous: We already checked about that it has no type specifier, and
2089 // C++ doesn't have implicit int. Diagnose it as a typo w.r.t. to the
Mike Stump1eb44332009-09-09 15:08:12 +00002090 // typename.
Chris Lattnerf4382f52009-04-14 22:17:06 +00002091 if (TypeRep == 0) {
2092 ConsumeToken(); // Eat the scope spec so the identifier is current.
Richard Smith69730c12012-03-12 07:56:15 +00002093 if (ParseImplicitInt(DS, &SS, TemplateInfo, AS, DSContext)) continue;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002094 goto DoneWithDeclSpec;
Chris Lattnerf4382f52009-04-14 22:17:06 +00002095 }
Mike Stump1eb44332009-09-09 15:08:12 +00002096
John McCallaa87d332009-12-12 11:40:51 +00002097 DS.getTypeSpecScope() = SS;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002098 ConsumeToken(); // The C++ scope.
2099
Douglas Gregor1a51b4a2009-02-09 15:09:02 +00002100 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00002101 DiagID, TypeRep);
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002102 if (isInvalid)
2103 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002104
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002105 DS.SetRangeEnd(Tok.getLocation());
2106 ConsumeToken(); // The typename.
2107
2108 continue;
2109 }
Mike Stump1eb44332009-09-09 15:08:12 +00002110
Chris Lattner80d0c892009-01-21 19:48:37 +00002111 case tok::annot_typename: {
John McCallb3d87482010-08-24 05:47:05 +00002112 if (Tok.getAnnotationValue()) {
2113 ParsedType T = getTypeAnnotation(Tok);
Nico Weberc43271e2010-11-22 12:50:03 +00002114 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallb3d87482010-08-24 05:47:05 +00002115 DiagID, T);
2116 } else
Douglas Gregor31a19b62009-04-01 21:51:26 +00002117 DS.SetTypeSpecError();
Chris Lattner5c5db552010-04-05 18:18:31 +00002118
2119 if (isInvalid)
2120 break;
2121
Chris Lattner80d0c892009-01-21 19:48:37 +00002122 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
2123 ConsumeToken(); // The typename
Mike Stump1eb44332009-09-09 15:08:12 +00002124
Chris Lattner80d0c892009-01-21 19:48:37 +00002125 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
2126 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00002127 // Objective-C interface.
David Blaikie4e4d0842012-03-11 07:00:24 +00002128 if (Tok.is(tok::less) && getLangOpts().ObjC1)
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00002129 ParseObjCProtocolQualifiers(DS);
2130
Chris Lattner80d0c892009-01-21 19:48:37 +00002131 continue;
2132 }
Mike Stump1eb44332009-09-09 15:08:12 +00002133
Douglas Gregorbfad9152011-04-28 15:48:45 +00002134 case tok::kw___is_signed:
2135 // GNU libstdc++ 4.4 uses __is_signed as an identifier, but Clang
2136 // typically treats it as a trait. If we see __is_signed as it appears
2137 // in libstdc++, e.g.,
2138 //
2139 // static const bool __is_signed;
2140 //
2141 // then treat __is_signed as an identifier rather than as a keyword.
2142 if (DS.getTypeSpecType() == TST_bool &&
2143 DS.getTypeQualifiers() == DeclSpec::TQ_const &&
2144 DS.getStorageClassSpec() == DeclSpec::SCS_static) {
2145 Tok.getIdentifierInfo()->RevertTokenIDToIdentifier();
2146 Tok.setKind(tok::identifier);
2147 }
2148
2149 // We're done with the declaration-specifiers.
2150 goto DoneWithDeclSpec;
2151
Chris Lattner3bd934a2008-07-26 01:18:38 +00002152 // typedef-name
David Blaikie42d6d0c2011-12-04 05:04:18 +00002153 case tok::kw_decltype:
Chris Lattner3bd934a2008-07-26 01:18:38 +00002154 case tok::identifier: {
Chris Lattner5e02c472009-01-05 00:07:25 +00002155 // In C++, check to see if this is a scope specifier like foo::bar::, if
2156 // so handle it as such. This is important for ctor parsing.
David Blaikie4e4d0842012-03-11 07:00:24 +00002157 if (getLangOpts().CPlusPlus) {
John McCall9ba61662010-02-26 08:45:28 +00002158 if (TryAnnotateCXXScopeToken(true)) {
2159 if (!DS.hasTypeSpecifier())
2160 DS.SetTypeSpecError();
2161 goto DoneWithDeclSpec;
2162 }
2163 if (!Tok.is(tok::identifier))
2164 continue;
2165 }
Mike Stump1eb44332009-09-09 15:08:12 +00002166
Chris Lattner3bd934a2008-07-26 01:18:38 +00002167 // This identifier can only be a typedef name if we haven't already seen
2168 // a type-specifier. Without this check we misparse:
2169 // typedef int X; struct Y { short X; }; as 'short int'.
2170 if (DS.hasTypeSpecifier())
2171 goto DoneWithDeclSpec;
Mike Stump1eb44332009-09-09 15:08:12 +00002172
John Thompson82287d12010-02-05 00:12:22 +00002173 // Check for need to substitute AltiVec keyword tokens.
2174 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
2175 break;
2176
Richard Smithf63eee72012-05-09 18:56:43 +00002177 // [AltiVec] 2.2: [If the 'vector' specifier is used] The syntax does not
2178 // allow the use of a typedef name as a type specifier.
2179 if (DS.isTypeAltiVecVector())
2180 goto DoneWithDeclSpec;
2181
John McCallb3d87482010-08-24 05:47:05 +00002182 ParsedType TypeRep =
2183 Actions.getTypeName(*Tok.getIdentifierInfo(),
2184 Tok.getLocation(), getCurScope());
Douglas Gregor55f6b142009-02-09 18:46:07 +00002185
Chris Lattnerc199ab32009-04-12 20:42:31 +00002186 // If this is not a typedef name, don't parse it as part of the declspec,
2187 // it must be an implicit int or an error.
John McCallb3d87482010-08-24 05:47:05 +00002188 if (!TypeRep) {
Richard Smith69730c12012-03-12 07:56:15 +00002189 if (ParseImplicitInt(DS, 0, TemplateInfo, AS, DSContext)) continue;
Chris Lattner3bd934a2008-07-26 01:18:38 +00002190 goto DoneWithDeclSpec;
Chris Lattnerc199ab32009-04-12 20:42:31 +00002191 }
Douglas Gregor55f6b142009-02-09 18:46:07 +00002192
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002193 // If we're in a context where the identifier could be a class name,
2194 // check whether this is a constructor declaration.
David Blaikie4e4d0842012-03-11 07:00:24 +00002195 if (getLangOpts().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00002196 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), getCurScope()) &&
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002197 isConstructorDeclarator())
Douglas Gregorb48fe382008-10-31 09:07:45 +00002198 goto DoneWithDeclSpec;
2199
Douglas Gregor1a51b4a2009-02-09 15:09:02 +00002200 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00002201 DiagID, TypeRep);
Chris Lattner3bd934a2008-07-26 01:18:38 +00002202 if (isInvalid)
2203 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002204
Chris Lattner3bd934a2008-07-26 01:18:38 +00002205 DS.SetRangeEnd(Tok.getLocation());
2206 ConsumeToken(); // The identifier
2207
2208 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
2209 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00002210 // Objective-C interface.
David Blaikie4e4d0842012-03-11 07:00:24 +00002211 if (Tok.is(tok::less) && getLangOpts().ObjC1)
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00002212 ParseObjCProtocolQualifiers(DS);
2213
Steve Naroff4f9b9f12008-09-22 10:28:57 +00002214 // Need to support trailing type qualifiers (e.g. "id<p> const").
2215 // If a type specifier follows, it will be diagnosed elsewhere.
2216 continue;
Chris Lattner3bd934a2008-07-26 01:18:38 +00002217 }
Douglas Gregor39a8de12009-02-25 19:37:18 +00002218
2219 // type-name
2220 case tok::annot_template_id: {
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +00002221 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregorc45c2322009-03-31 00:43:58 +00002222 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor39a8de12009-02-25 19:37:18 +00002223 // This template-id does not refer to a type name, so we're
2224 // done with the type-specifiers.
2225 goto DoneWithDeclSpec;
2226 }
2227
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002228 // If we're in a context where the template-id could be a
2229 // constructor name or specialization, check whether this is a
2230 // constructor declaration.
David Blaikie4e4d0842012-03-11 07:00:24 +00002231 if (getLangOpts().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00002232 Actions.isCurrentClassName(*TemplateId->Name, getCurScope()) &&
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002233 isConstructorDeclarator())
2234 goto DoneWithDeclSpec;
2235
Douglas Gregor39a8de12009-02-25 19:37:18 +00002236 // Turn the template-id annotation token into a type annotation
2237 // token, then try again to parse it as a type-specifier.
Douglas Gregor31a19b62009-04-01 21:51:26 +00002238 AnnotateTemplateIdTokenAsType();
Douglas Gregor39a8de12009-02-25 19:37:18 +00002239 continue;
2240 }
2241
Reid Spencer5f016e22007-07-11 17:01:13 +00002242 // GNU attributes support.
2243 case tok::kw___attribute:
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +00002244 ParseGNUAttributes(DS.getAttributes(), 0, LateAttrs);
Reid Spencer5f016e22007-07-11 17:01:13 +00002245 continue;
Steve Narofff59e17e2008-12-24 20:59:21 +00002246
2247 // Microsoft declspec support.
2248 case tok::kw___declspec:
John McCall7f040a92010-12-24 02:08:15 +00002249 ParseMicrosoftDeclSpec(DS.getAttributes());
Steve Narofff59e17e2008-12-24 20:59:21 +00002250 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00002251
Steve Naroff239f0732008-12-25 14:16:32 +00002252 // Microsoft single token adornments.
Steve Naroff86bc6cf2008-12-25 14:41:26 +00002253 case tok::kw___forceinline:
Eli Friedman290eeb02009-06-08 23:27:34 +00002254 // FIXME: Add handling here!
2255 break;
2256
2257 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00002258 case tok::kw___ptr32:
Steve Naroff86bc6cf2008-12-25 14:41:26 +00002259 case tok::kw___w64:
Steve Naroff239f0732008-12-25 14:16:32 +00002260 case tok::kw___cdecl:
2261 case tok::kw___stdcall:
2262 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00002263 case tok::kw___thiscall:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00002264 case tok::kw___unaligned:
John McCall7f040a92010-12-24 02:08:15 +00002265 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman290eeb02009-06-08 23:27:34 +00002266 continue;
2267
Dawn Perchik52fc3142010-09-03 01:29:35 +00002268 // Borland single token adornments.
2269 case tok::kw___pascal:
John McCall7f040a92010-12-24 02:08:15 +00002270 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik52fc3142010-09-03 01:29:35 +00002271 continue;
2272
Peter Collingbournef315fa82011-02-14 01:42:53 +00002273 // OpenCL single token adornments.
2274 case tok::kw___kernel:
2275 ParseOpenCLAttributes(DS.getAttributes());
2276 continue;
2277
Reid Spencer5f016e22007-07-11 17:01:13 +00002278 // storage-class-specifier
2279 case tok::kw_typedef:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002280 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_typedef, Loc,
2281 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002282 break;
2283 case tok::kw_extern:
2284 if (DS.isThreadSpecified())
Chris Lattner1ab3b962008-11-18 07:48:38 +00002285 Diag(Tok, diag::ext_thread_before) << "extern";
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002286 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_extern, Loc,
2287 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002288 break;
Steve Naroff8d54bf22007-12-18 00:16:02 +00002289 case tok::kw___private_extern__:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002290 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_private_extern,
2291 Loc, PrevSpec, DiagID);
Steve Naroff8d54bf22007-12-18 00:16:02 +00002292 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00002293 case tok::kw_static:
2294 if (DS.isThreadSpecified())
Chris Lattner1ab3b962008-11-18 07:48:38 +00002295 Diag(Tok, diag::ext_thread_before) << "static";
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002296 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_static, Loc,
2297 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002298 break;
2299 case tok::kw_auto:
David Blaikie4e4d0842012-03-11 07:00:24 +00002300 if (getLangOpts().CPlusPlus0x) {
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002301 if (isKnownToBeTypeSpecifier(GetLookAheadToken(1))) {
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002302 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc,
2303 PrevSpec, DiagID);
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002304 if (!isInvalid)
Richard Smith8f4fb192011-09-04 19:54:14 +00002305 Diag(Tok, diag::ext_auto_storage_class)
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002306 << FixItHint::CreateRemoval(DS.getStorageClassSpecLoc());
Richard Smith8f4fb192011-09-04 19:54:14 +00002307 } else
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002308 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec,
2309 DiagID);
Richard Smith8f4fb192011-09-04 19:54:14 +00002310 } else
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002311 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc,
2312 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002313 break;
2314 case tok::kw_register:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002315 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_register, Loc,
2316 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002317 break;
Sebastian Redl669d5d72008-11-14 23:42:31 +00002318 case tok::kw_mutable:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002319 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_mutable, Loc,
2320 PrevSpec, DiagID);
Sebastian Redl669d5d72008-11-14 23:42:31 +00002321 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00002322 case tok::kw___thread:
John McCallfec54012009-08-03 20:12:06 +00002323 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002324 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002325
Reid Spencer5f016e22007-07-11 17:01:13 +00002326 // function-specifier
2327 case tok::kw_inline:
John McCallfec54012009-08-03 20:12:06 +00002328 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002329 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +00002330 case tok::kw_virtual:
John McCallfec54012009-08-03 20:12:06 +00002331 isInvalid = DS.SetFunctionSpecVirtual(Loc, PrevSpec, DiagID);
Douglas Gregorb48fe382008-10-31 09:07:45 +00002332 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +00002333 case tok::kw_explicit:
John McCallfec54012009-08-03 20:12:06 +00002334 isInvalid = DS.SetFunctionSpecExplicit(Loc, PrevSpec, DiagID);
Douglas Gregorb48fe382008-10-31 09:07:45 +00002335 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002336
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002337 // alignment-specifier
2338 case tok::kw__Alignas:
David Blaikie4e4d0842012-03-11 07:00:24 +00002339 if (!getLangOpts().C11)
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00002340 Diag(Tok, diag::ext_c11_alignas);
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002341 ParseAlignmentSpecifier(DS.getAttributes());
2342 continue;
2343
Anders Carlssonf47f7a12009-05-06 04:46:28 +00002344 // friend
2345 case tok::kw_friend:
John McCall67d1a672009-08-06 02:15:43 +00002346 if (DSContext == DSC_class)
2347 isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID);
2348 else {
2349 PrevSpec = ""; // not actually used by the diagnostic
2350 DiagID = diag::err_friend_invalid_in_context;
2351 isInvalid = true;
2352 }
Anders Carlssonf47f7a12009-05-06 04:46:28 +00002353 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002354
Douglas Gregor8d267c52011-09-09 02:06:17 +00002355 // Modules
2356 case tok::kw___module_private__:
2357 isInvalid = DS.setModulePrivateSpec(Loc, PrevSpec, DiagID);
2358 break;
2359
Sebastian Redl2ac67232009-11-05 15:47:02 +00002360 // constexpr
2361 case tok::kw_constexpr:
2362 isInvalid = DS.SetConstexprSpec(Loc, PrevSpec, DiagID);
2363 break;
2364
Chris Lattner80d0c892009-01-21 19:48:37 +00002365 // type-specifier
2366 case tok::kw_short:
John McCallfec54012009-08-03 20:12:06 +00002367 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec,
2368 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002369 break;
2370 case tok::kw_long:
2371 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCallfec54012009-08-03 20:12:06 +00002372 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
2373 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002374 else
John McCallfec54012009-08-03 20:12:06 +00002375 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2376 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002377 break;
Francois Pichet338d7f72011-04-28 01:59:37 +00002378 case tok::kw___int64:
2379 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2380 DiagID);
2381 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002382 case tok::kw_signed:
John McCallfec54012009-08-03 20:12:06 +00002383 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec,
2384 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002385 break;
2386 case tok::kw_unsigned:
John McCallfec54012009-08-03 20:12:06 +00002387 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
2388 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002389 break;
2390 case tok::kw__Complex:
John McCallfec54012009-08-03 20:12:06 +00002391 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
2392 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002393 break;
2394 case tok::kw__Imaginary:
John McCallfec54012009-08-03 20:12:06 +00002395 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
2396 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002397 break;
2398 case tok::kw_void:
John McCallfec54012009-08-03 20:12:06 +00002399 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec,
2400 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002401 break;
2402 case tok::kw_char:
John McCallfec54012009-08-03 20:12:06 +00002403 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec,
2404 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002405 break;
2406 case tok::kw_int:
John McCallfec54012009-08-03 20:12:06 +00002407 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec,
2408 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002409 break;
Richard Smith5a5a9712012-04-04 06:24:32 +00002410 case tok::kw___int128:
2411 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int128, Loc, PrevSpec,
2412 DiagID);
2413 break;
2414 case tok::kw_half:
2415 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_half, Loc, PrevSpec,
2416 DiagID);
2417 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002418 case tok::kw_float:
John McCallfec54012009-08-03 20:12:06 +00002419 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec,
2420 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002421 break;
2422 case tok::kw_double:
John McCallfec54012009-08-03 20:12:06 +00002423 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec,
2424 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002425 break;
2426 case tok::kw_wchar_t:
John McCallfec54012009-08-03 20:12:06 +00002427 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec,
2428 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002429 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002430 case tok::kw_char16_t:
John McCallfec54012009-08-03 20:12:06 +00002431 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec,
2432 DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002433 break;
2434 case tok::kw_char32_t:
John McCallfec54012009-08-03 20:12:06 +00002435 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec,
2436 DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002437 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002438 case tok::kw_bool:
2439 case tok::kw__Bool:
Argyrios Kyrtzidis4383e182010-11-16 18:18:13 +00002440 if (Tok.is(tok::kw_bool) &&
2441 DS.getTypeSpecType() != DeclSpec::TST_unspecified &&
2442 DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
2443 PrevSpec = ""; // Not used by the diagnostic.
2444 DiagID = diag::err_bool_redeclaration;
Fariborz Jahaniane106a0b2011-04-19 21:42:37 +00002445 // For better error recovery.
2446 Tok.setKind(tok::identifier);
Argyrios Kyrtzidis4383e182010-11-16 18:18:13 +00002447 isInvalid = true;
2448 } else {
2449 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec,
2450 DiagID);
2451 }
Chris Lattner80d0c892009-01-21 19:48:37 +00002452 break;
2453 case tok::kw__Decimal32:
John McCallfec54012009-08-03 20:12:06 +00002454 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
2455 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002456 break;
2457 case tok::kw__Decimal64:
John McCallfec54012009-08-03 20:12:06 +00002458 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
2459 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002460 break;
2461 case tok::kw__Decimal128:
John McCallfec54012009-08-03 20:12:06 +00002462 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
2463 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002464 break;
John Thompson82287d12010-02-05 00:12:22 +00002465 case tok::kw___vector:
2466 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
2467 break;
2468 case tok::kw___pixel:
2469 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
2470 break;
John McCalla5fc4722011-04-09 22:50:59 +00002471 case tok::kw___unknown_anytype:
2472 isInvalid = DS.SetTypeSpecType(TST_unknown_anytype, Loc,
2473 PrevSpec, DiagID);
2474 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002475
2476 // class-specifier:
2477 case tok::kw_class:
2478 case tok::kw_struct:
Chris Lattner4c97d762009-04-12 21:49:30 +00002479 case tok::kw_union: {
2480 tok::TokenKind Kind = Tok.getKind();
2481 ConsumeToken();
Richard Smith69730c12012-03-12 07:56:15 +00002482 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS,
2483 EnteringContext, DSContext);
Chris Lattner80d0c892009-01-21 19:48:37 +00002484 continue;
Chris Lattner4c97d762009-04-12 21:49:30 +00002485 }
Chris Lattner80d0c892009-01-21 19:48:37 +00002486
2487 // enum-specifier:
2488 case tok::kw_enum:
Chris Lattner4c97d762009-04-12 21:49:30 +00002489 ConsumeToken();
Richard Smith69730c12012-03-12 07:56:15 +00002490 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS, DSContext);
Chris Lattner80d0c892009-01-21 19:48:37 +00002491 continue;
2492
2493 // cv-qualifier:
2494 case tok::kw_const:
John McCallfec54012009-08-03 20:12:06 +00002495 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec, DiagID,
David Blaikie4e4d0842012-03-11 07:00:24 +00002496 getLangOpts());
Chris Lattner80d0c892009-01-21 19:48:37 +00002497 break;
2498 case tok::kw_volatile:
John McCallfec54012009-08-03 20:12:06 +00002499 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
David Blaikie4e4d0842012-03-11 07:00:24 +00002500 getLangOpts());
Chris Lattner80d0c892009-01-21 19:48:37 +00002501 break;
2502 case tok::kw_restrict:
John McCallfec54012009-08-03 20:12:06 +00002503 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
David Blaikie4e4d0842012-03-11 07:00:24 +00002504 getLangOpts());
Chris Lattner80d0c892009-01-21 19:48:37 +00002505 break;
2506
Douglas Gregord57959a2009-03-27 23:10:48 +00002507 // C++ typename-specifier:
2508 case tok::kw_typename:
John McCall9ba61662010-02-26 08:45:28 +00002509 if (TryAnnotateTypeOrScopeToken()) {
2510 DS.SetTypeSpecError();
2511 goto DoneWithDeclSpec;
2512 }
2513 if (!Tok.is(tok::kw_typename))
Douglas Gregord57959a2009-03-27 23:10:48 +00002514 continue;
2515 break;
2516
Chris Lattner80d0c892009-01-21 19:48:37 +00002517 // GNU typeof support.
2518 case tok::kw_typeof:
2519 ParseTypeofSpecifier(DS);
2520 continue;
2521
David Blaikie42d6d0c2011-12-04 05:04:18 +00002522 case tok::annot_decltype:
Anders Carlsson6fd634f2009-06-24 17:47:40 +00002523 ParseDecltypeSpecifier(DS);
2524 continue;
2525
Sean Huntdb5d44b2011-05-19 05:37:45 +00002526 case tok::kw___underlying_type:
2527 ParseUnderlyingTypeSpecifier(DS);
Eli Friedmanb001de72011-10-06 23:00:33 +00002528 continue;
2529
2530 case tok::kw__Atomic:
2531 ParseAtomicSpecifier(DS);
2532 continue;
Sean Huntdb5d44b2011-05-19 05:37:45 +00002533
Peter Collingbourne207f4d82011-03-18 22:38:29 +00002534 // OpenCL qualifiers:
2535 case tok::kw_private:
David Blaikie4e4d0842012-03-11 07:00:24 +00002536 if (!getLangOpts().OpenCL)
Peter Collingbourne207f4d82011-03-18 22:38:29 +00002537 goto DoneWithDeclSpec;
2538 case tok::kw___private:
2539 case tok::kw___global:
2540 case tok::kw___local:
2541 case tok::kw___constant:
2542 case tok::kw___read_only:
2543 case tok::kw___write_only:
2544 case tok::kw___read_write:
2545 ParseOpenCLQualifiers(DS);
2546 break;
2547
Steve Naroffd3ded1f2008-06-05 00:02:44 +00002548 case tok::less:
Chris Lattner3bd934a2008-07-26 01:18:38 +00002549 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattnerbce61352008-07-26 00:20:22 +00002550 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
2551 // but we support it.
David Blaikie4e4d0842012-03-11 07:00:24 +00002552 if (DS.hasTypeSpecifier() || !getLangOpts().ObjC1)
Chris Lattnerbce61352008-07-26 00:20:22 +00002553 goto DoneWithDeclSpec;
Mike Stump1eb44332009-09-09 15:08:12 +00002554
Douglas Gregor46f936e2010-11-19 17:10:50 +00002555 if (!ParseObjCProtocolQualifiers(DS))
2556 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
2557 << FixItHint::CreateInsertion(Loc, "id")
2558 << SourceRange(Loc, DS.getSourceRange().getEnd());
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00002559
2560 // Need to support trailing type qualifiers (e.g. "id<p> const").
2561 // If a type specifier follows, it will be diagnosed elsewhere.
2562 continue;
Reid Spencer5f016e22007-07-11 17:01:13 +00002563 }
John McCallfec54012009-08-03 20:12:06 +00002564 // If the specifier wasn't legal, issue a diagnostic.
Reid Spencer5f016e22007-07-11 17:01:13 +00002565 if (isInvalid) {
2566 assert(PrevSpec && "Method did not return previous specifier!");
John McCallfec54012009-08-03 20:12:06 +00002567 assert(DiagID);
Douglas Gregorae2fb142010-08-23 14:34:43 +00002568
2569 if (DiagID == diag::ext_duplicate_declspec)
2570 Diag(Tok, DiagID)
2571 << PrevSpec << FixItHint::CreateRemoval(Tok.getLocation());
2572 else
2573 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00002574 }
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002575
Chris Lattner81c018d2008-03-13 06:29:04 +00002576 DS.SetRangeEnd(Tok.getLocation());
Fariborz Jahaniane106a0b2011-04-19 21:42:37 +00002577 if (DiagID != diag::err_bool_redeclaration)
2578 ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00002579 }
2580}
Douglas Gregoradcac882008-12-01 23:54:00 +00002581
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002582/// ParseStructDeclaration - Parse a struct declaration without the terminating
2583/// semicolon.
2584///
Reid Spencer5f016e22007-07-11 17:01:13 +00002585/// struct-declaration:
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002586/// specifier-qualifier-list struct-declarator-list
Reid Spencer5f016e22007-07-11 17:01:13 +00002587/// [GNU] __extension__ struct-declaration
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002588/// [GNU] specifier-qualifier-list
Reid Spencer5f016e22007-07-11 17:01:13 +00002589/// struct-declarator-list:
2590/// struct-declarator
2591/// struct-declarator-list ',' struct-declarator
2592/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
2593/// struct-declarator:
2594/// declarator
2595/// [GNU] declarator attributes[opt]
2596/// declarator[opt] ':' constant-expression
2597/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
2598///
Chris Lattnere1359422008-04-10 06:46:29 +00002599void Parser::
John McCallbdd563e2009-11-03 02:38:08 +00002600ParseStructDeclaration(DeclSpec &DS, FieldCallback &Fields) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002601
Chris Lattnerc46d1a12008-10-20 06:45:43 +00002602 if (Tok.is(tok::kw___extension__)) {
2603 // __extension__ silences extension warnings in the subexpression.
2604 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroff28a7ca82007-08-20 22:28:22 +00002605 ConsumeToken();
Chris Lattnerc46d1a12008-10-20 06:45:43 +00002606 return ParseStructDeclaration(DS, Fields);
2607 }
Mike Stump1eb44332009-09-09 15:08:12 +00002608
Steve Naroff28a7ca82007-08-20 22:28:22 +00002609 // Parse the common specifier-qualifiers-list piece.
Steve Naroff28a7ca82007-08-20 22:28:22 +00002610 ParseSpecifierQualifierList(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00002611
Douglas Gregor4920f1f2009-01-12 22:49:06 +00002612 // If there are no declarators, this is a free-standing declaration
2613 // specifier. Let the actions module cope with it.
Chris Lattner04d66662007-10-09 17:33:22 +00002614 if (Tok.is(tok::semi)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00002615 Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none, DS);
Steve Naroff28a7ca82007-08-20 22:28:22 +00002616 return;
2617 }
2618
2619 // Read struct-declarators until we find the semicolon.
John McCallbdd563e2009-11-03 02:38:08 +00002620 bool FirstDeclarator = true;
Richard Smith7984de32012-01-12 23:53:29 +00002621 SourceLocation CommaLoc;
Steve Naroff28a7ca82007-08-20 22:28:22 +00002622 while (1) {
John McCall92576642012-05-07 06:16:41 +00002623 ParsingDeclRAIIObject PD(*this, ParsingDeclRAIIObject::NoParent);
John McCallbdd563e2009-11-03 02:38:08 +00002624 FieldDeclarator DeclaratorInfo(DS);
Richard Smith7984de32012-01-12 23:53:29 +00002625 DeclaratorInfo.D.setCommaLoc(CommaLoc);
John McCallbdd563e2009-11-03 02:38:08 +00002626
2627 // Attributes are only allowed here on successive declarators.
John McCall7f040a92010-12-24 02:08:15 +00002628 if (!FirstDeclarator)
2629 MaybeParseGNUAttributes(DeclaratorInfo.D);
Mike Stump1eb44332009-09-09 15:08:12 +00002630
Steve Naroff28a7ca82007-08-20 22:28:22 +00002631 /// struct-declarator: declarator
2632 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattnera1efc8c2009-12-10 01:59:24 +00002633 if (Tok.isNot(tok::colon)) {
2634 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
2635 ColonProtectionRAIIObject X(*this);
Chris Lattnere1359422008-04-10 06:46:29 +00002636 ParseDeclarator(DeclaratorInfo.D);
Chris Lattnera1efc8c2009-12-10 01:59:24 +00002637 }
Mike Stump1eb44332009-09-09 15:08:12 +00002638
Chris Lattner04d66662007-10-09 17:33:22 +00002639 if (Tok.is(tok::colon)) {
Steve Naroff28a7ca82007-08-20 22:28:22 +00002640 ConsumeToken();
John McCall60d7b3a2010-08-24 06:29:42 +00002641 ExprResult Res(ParseConstantExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002642 if (Res.isInvalid())
Steve Naroff28a7ca82007-08-20 22:28:22 +00002643 SkipUntil(tok::semi, true, true);
Chris Lattner60b1e3e2008-04-10 06:15:14 +00002644 else
Sebastian Redleffa8d12008-12-10 00:02:53 +00002645 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroff28a7ca82007-08-20 22:28:22 +00002646 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00002647
Steve Naroff28a7ca82007-08-20 22:28:22 +00002648 // If attributes exist after the declarator, parse them.
John McCall7f040a92010-12-24 02:08:15 +00002649 MaybeParseGNUAttributes(DeclaratorInfo.D);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002650
John McCallbdd563e2009-11-03 02:38:08 +00002651 // We're done with this declarator; invoke the callback.
John McCalld226f652010-08-21 09:40:31 +00002652 Decl *D = Fields.invoke(DeclaratorInfo);
John McCall54abf7d2009-11-04 02:18:39 +00002653 PD.complete(D);
John McCallbdd563e2009-11-03 02:38:08 +00002654
Steve Naroff28a7ca82007-08-20 22:28:22 +00002655 // If we don't have a comma, it is either the end of the list (a ';')
2656 // or an error, bail out.
Chris Lattner04d66662007-10-09 17:33:22 +00002657 if (Tok.isNot(tok::comma))
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002658 return;
Sebastian Redlab197ba2009-02-09 18:23:29 +00002659
Steve Naroff28a7ca82007-08-20 22:28:22 +00002660 // Consume the comma.
Richard Smith7984de32012-01-12 23:53:29 +00002661 CommaLoc = ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00002662
John McCallbdd563e2009-11-03 02:38:08 +00002663 FirstDeclarator = false;
Steve Naroff28a7ca82007-08-20 22:28:22 +00002664 }
Steve Naroff28a7ca82007-08-20 22:28:22 +00002665}
2666
2667/// ParseStructUnionBody
2668/// struct-contents:
2669/// struct-declaration-list
2670/// [EXT] empty
2671/// [GNU] "struct-declaration-list" without terminatoring ';'
2672/// struct-declaration-list:
2673/// struct-declaration
2674/// struct-declaration-list struct-declaration
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002675/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroff28a7ca82007-08-20 22:28:22 +00002676///
Reid Spencer5f016e22007-07-11 17:01:13 +00002677void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
John McCalld226f652010-08-21 09:40:31 +00002678 unsigned TagType, Decl *TagDecl) {
John McCallf312b1e2010-08-26 23:41:50 +00002679 PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc,
2680 "parsing struct/union body");
Mike Stump1eb44332009-09-09 15:08:12 +00002681
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002682 BalancedDelimiterTracker T(*this, tok::l_brace);
2683 if (T.consumeOpen())
2684 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002685
Douglas Gregor3218c4b2009-01-09 22:42:13 +00002686 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
Douglas Gregor23c94db2010-07-02 17:43:08 +00002687 Actions.ActOnTagStartDefinition(getCurScope(), TagDecl);
Douglas Gregor72de6672009-01-08 20:45:30 +00002688
Reid Spencer5f016e22007-07-11 17:01:13 +00002689 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
2690 // C++.
David Blaikie4e4d0842012-03-11 07:00:24 +00002691 if (Tok.is(tok::r_brace) && !getLangOpts().CPlusPlus) {
Richard Smithd7c56e12011-12-29 21:57:33 +00002692 Diag(Tok, diag::ext_empty_struct_union) << (TagType == TST_union);
2693 Diag(Tok, diag::warn_empty_struct_union_compat) << (TagType == TST_union);
2694 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002695
Chris Lattner5f9e2722011-07-23 10:55:15 +00002696 SmallVector<Decl *, 32> FieldDecls;
Chris Lattnere1359422008-04-10 06:46:29 +00002697
Reid Spencer5f016e22007-07-11 17:01:13 +00002698 // While we still have something to read, read the declarations in the struct.
Chris Lattner04d66662007-10-09 17:33:22 +00002699 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002700 // Each iteration of this loop reads one struct-declaration.
Mike Stump1eb44332009-09-09 15:08:12 +00002701
Reid Spencer5f016e22007-07-11 17:01:13 +00002702 // Check for extraneous top-level semicolon.
Chris Lattner04d66662007-10-09 17:33:22 +00002703 if (Tok.is(tok::semi)) {
Douglas Gregor9b3064b2009-04-01 22:41:11 +00002704 Diag(Tok, diag::ext_extra_struct_semi)
Douglas Gregorf13ca062010-06-16 23:08:59 +00002705 << DeclSpec::getSpecifierName((DeclSpec::TST)TagType)
Douglas Gregor849b2432010-03-31 17:46:05 +00002706 << FixItHint::CreateRemoval(Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00002707 ConsumeToken();
2708 continue;
2709 }
Chris Lattnere1359422008-04-10 06:46:29 +00002710
2711 // Parse all the comma separated declarators.
John McCall0b7e6782011-03-24 11:26:52 +00002712 DeclSpec DS(AttrFactory);
Mike Stump1eb44332009-09-09 15:08:12 +00002713
John McCallbdd563e2009-11-03 02:38:08 +00002714 if (!Tok.is(tok::at)) {
2715 struct CFieldCallback : FieldCallback {
2716 Parser &P;
John McCalld226f652010-08-21 09:40:31 +00002717 Decl *TagDecl;
Chris Lattner5f9e2722011-07-23 10:55:15 +00002718 SmallVectorImpl<Decl *> &FieldDecls;
John McCallbdd563e2009-11-03 02:38:08 +00002719
John McCalld226f652010-08-21 09:40:31 +00002720 CFieldCallback(Parser &P, Decl *TagDecl,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002721 SmallVectorImpl<Decl *> &FieldDecls) :
John McCallbdd563e2009-11-03 02:38:08 +00002722 P(P), TagDecl(TagDecl), FieldDecls(FieldDecls) {}
2723
John McCalld226f652010-08-21 09:40:31 +00002724 virtual Decl *invoke(FieldDeclarator &FD) {
John McCallbdd563e2009-11-03 02:38:08 +00002725 // Install the declarator into the current TagDecl.
John McCalld226f652010-08-21 09:40:31 +00002726 Decl *Field = P.Actions.ActOnField(P.getCurScope(), TagDecl,
John McCall4ba39712009-11-03 21:13:47 +00002727 FD.D.getDeclSpec().getSourceRange().getBegin(),
2728 FD.D, FD.BitfieldSize);
John McCallbdd563e2009-11-03 02:38:08 +00002729 FieldDecls.push_back(Field);
2730 return Field;
Douglas Gregor91a28862009-08-26 14:27:30 +00002731 }
John McCallbdd563e2009-11-03 02:38:08 +00002732 } Callback(*this, TagDecl, FieldDecls);
2733
2734 ParseStructDeclaration(DS, Callback);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002735 } else { // Handle @defs
2736 ConsumeToken();
2737 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
2738 Diag(Tok, diag::err_unexpected_at);
Chris Lattner3e156ad2010-02-02 00:37:27 +00002739 SkipUntil(tok::semi, true);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002740 continue;
2741 }
2742 ConsumeToken();
2743 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
2744 if (!Tok.is(tok::identifier)) {
2745 Diag(Tok, diag::err_expected_ident);
Chris Lattner3e156ad2010-02-02 00:37:27 +00002746 SkipUntil(tok::semi, true);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002747 continue;
2748 }
Chris Lattner5f9e2722011-07-23 10:55:15 +00002749 SmallVector<Decl *, 16> Fields;
Douglas Gregor23c94db2010-07-02 17:43:08 +00002750 Actions.ActOnDefs(getCurScope(), TagDecl, Tok.getLocation(),
Douglas Gregor44b43212008-12-11 16:49:14 +00002751 Tok.getIdentifierInfo(), Fields);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002752 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
2753 ConsumeToken();
2754 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
Mike Stump1eb44332009-09-09 15:08:12 +00002755 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002756
Chris Lattner04d66662007-10-09 17:33:22 +00002757 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002758 ConsumeToken();
Chris Lattner04d66662007-10-09 17:33:22 +00002759 } else if (Tok.is(tok::r_brace)) {
Chris Lattner3e156ad2010-02-02 00:37:27 +00002760 ExpectAndConsume(tok::semi, diag::ext_expected_semi_decl_list);
Reid Spencer5f016e22007-07-11 17:01:13 +00002761 break;
2762 } else {
Chris Lattner3e156ad2010-02-02 00:37:27 +00002763 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
2764 // Skip to end of block or statement to avoid ext-warning on extra ';'.
Reid Spencer5f016e22007-07-11 17:01:13 +00002765 SkipUntil(tok::r_brace, true, true);
Chris Lattner3e156ad2010-02-02 00:37:27 +00002766 // If we stopped at a ';', eat it.
2767 if (Tok.is(tok::semi)) ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00002768 }
2769 }
Mike Stump1eb44332009-09-09 15:08:12 +00002770
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002771 T.consumeClose();
Mike Stump1eb44332009-09-09 15:08:12 +00002772
John McCall0b7e6782011-03-24 11:26:52 +00002773 ParsedAttributes attrs(AttrFactory);
Reid Spencer5f016e22007-07-11 17:01:13 +00002774 // If attributes exist after struct contents, parse them.
John McCall7f040a92010-12-24 02:08:15 +00002775 MaybeParseGNUAttributes(attrs);
Daniel Dunbar1bfe1c22008-10-03 02:03:53 +00002776
Douglas Gregor23c94db2010-07-02 17:43:08 +00002777 Actions.ActOnFields(getCurScope(),
David Blaikie77b6de02011-09-22 02:58:26 +00002778 RecordLoc, TagDecl, FieldDecls,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002779 T.getOpenLocation(), T.getCloseLocation(),
John McCall7f040a92010-12-24 02:08:15 +00002780 attrs.getList());
Douglas Gregor72de6672009-01-08 20:45:30 +00002781 StructScope.Exit();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002782 Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl,
2783 T.getCloseLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00002784}
2785
Reid Spencer5f016e22007-07-11 17:01:13 +00002786/// ParseEnumSpecifier
2787/// enum-specifier: [C99 6.7.2.2]
2788/// 'enum' identifier[opt] '{' enumerator-list '}'
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002789///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Reid Spencer5f016e22007-07-11 17:01:13 +00002790/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
2791/// '}' attributes[opt]
Aaron Ballman6454a022012-03-01 04:09:28 +00002792/// [MS] 'enum' __declspec[opt] identifier[opt] '{' enumerator-list ',' [opt]
2793/// '}'
Reid Spencer5f016e22007-07-11 17:01:13 +00002794/// 'enum' identifier
2795/// [GNU] 'enum' attributes[opt] identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002796///
Richard Smith1af83c42012-03-23 03:33:32 +00002797/// [C++11] enum-head '{' enumerator-list[opt] '}'
2798/// [C++11] enum-head '{' enumerator-list ',' '}'
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002799///
Richard Smith1af83c42012-03-23 03:33:32 +00002800/// enum-head: [C++11]
2801/// enum-key attribute-specifier-seq[opt] identifier[opt] enum-base[opt]
2802/// enum-key attribute-specifier-seq[opt] nested-name-specifier
2803/// identifier enum-base[opt]
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002804///
Richard Smith1af83c42012-03-23 03:33:32 +00002805/// enum-key: [C++11]
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002806/// 'enum'
2807/// 'enum' 'class'
2808/// 'enum' 'struct'
2809///
Richard Smith1af83c42012-03-23 03:33:32 +00002810/// enum-base: [C++11]
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002811/// ':' type-specifier-seq
2812///
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002813/// [C++] elaborated-type-specifier:
2814/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
2815///
Chris Lattner4c97d762009-04-12 21:49:30 +00002816void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregor9b9edd62010-03-02 17:53:14 +00002817 const ParsedTemplateInfo &TemplateInfo,
Richard Smith69730c12012-03-12 07:56:15 +00002818 AccessSpecifier AS, DeclSpecContext DSC) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002819 // Parse the tag portion of this.
Douglas Gregor374929f2009-09-18 15:37:17 +00002820 if (Tok.is(tok::code_completion)) {
2821 // Code completion for an enum name.
Douglas Gregor23c94db2010-07-02 17:43:08 +00002822 Actions.CodeCompleteTag(getCurScope(), DeclSpec::TST_enum);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002823 return cutOffParsing();
Douglas Gregor374929f2009-09-18 15:37:17 +00002824 }
John McCall57c13002011-07-06 05:58:41 +00002825
Richard Smithbdad7a22012-01-10 01:33:14 +00002826 SourceLocation ScopedEnumKWLoc;
John McCall57c13002011-07-06 05:58:41 +00002827 bool IsScopedUsingClassTag = false;
2828
David Blaikie4e4d0842012-03-11 07:00:24 +00002829 if (getLangOpts().CPlusPlus0x &&
John McCall57c13002011-07-06 05:58:41 +00002830 (Tok.is(tok::kw_class) || Tok.is(tok::kw_struct))) {
Richard Smith7fe62082011-10-15 05:09:34 +00002831 Diag(Tok, diag::warn_cxx98_compat_scoped_enum);
John McCall57c13002011-07-06 05:58:41 +00002832 IsScopedUsingClassTag = Tok.is(tok::kw_class);
Richard Smithbdad7a22012-01-10 01:33:14 +00002833 ScopedEnumKWLoc = ConsumeToken();
John McCall57c13002011-07-06 05:58:41 +00002834 }
Richard Smith1af83c42012-03-23 03:33:32 +00002835
John McCall13489672012-05-07 06:16:58 +00002836 // C++11 [temp.explicit]p12:
2837 // The usual access controls do not apply to names used to specify
2838 // explicit instantiations.
2839 // We extend this to also cover explicit specializations. Note that
2840 // we don't suppress if this turns out to be an elaborated type
2841 // specifier.
2842 bool shouldDelayDiagsInTag =
2843 (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation ||
2844 TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization);
2845 SuppressAccessChecks diagsFromTag(*this, shouldDelayDiagsInTag);
Richard Smith1af83c42012-03-23 03:33:32 +00002846
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002847 // If attributes exist after tag, parse them.
John McCall0b7e6782011-03-24 11:26:52 +00002848 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00002849 MaybeParseGNUAttributes(attrs);
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002850
Aaron Ballman6454a022012-03-01 04:09:28 +00002851 // If declspecs exist after tag, parse them.
2852 while (Tok.is(tok::kw___declspec))
2853 ParseMicrosoftDeclSpec(attrs);
2854
Richard Smith7796eb52012-03-12 08:56:40 +00002855 // Enum definitions should not be parsed in a trailing-return-type.
2856 bool AllowDeclaration = DSC != DSC_trailing;
2857
2858 bool AllowFixedUnderlyingType = AllowDeclaration &&
2859 (getLangOpts().CPlusPlus0x || getLangOpts().MicrosoftExt ||
2860 getLangOpts().ObjC2);
John McCall57c13002011-07-06 05:58:41 +00002861
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00002862 CXXScopeSpec &SS = DS.getTypeSpecScope();
David Blaikie4e4d0842012-03-11 07:00:24 +00002863 if (getLangOpts().CPlusPlus) {
John McCall57c13002011-07-06 05:58:41 +00002864 // "enum foo : bar;" is not a potential typo for "enum foo::bar;"
2865 // if a fixed underlying type is allowed.
2866 ColonProtectionRAIIObject X(*this, AllowFixedUnderlyingType);
2867
Douglas Gregorefaa93a2011-11-07 17:33:42 +00002868 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
2869 /*EnteringContext=*/false))
John McCall9ba61662010-02-26 08:45:28 +00002870 return;
2871
2872 if (SS.isSet() && Tok.isNot(tok::identifier)) {
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002873 Diag(Tok, diag::err_expected_ident);
2874 if (Tok.isNot(tok::l_brace)) {
2875 // Has no name and is not a definition.
2876 // Skip the rest of this declarator, up until the comma or semicolon.
2877 SkipUntil(tok::comma, true);
2878 return;
2879 }
2880 }
2881 }
Mike Stump1eb44332009-09-09 15:08:12 +00002882
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002883 // Must have either 'enum name' or 'enum {...}'.
Douglas Gregorb9075602011-02-22 02:55:24 +00002884 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace) &&
Richard Smith7796eb52012-03-12 08:56:40 +00002885 !(AllowFixedUnderlyingType && Tok.is(tok::colon))) {
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002886 Diag(Tok, diag::err_expected_ident_lbrace);
Mike Stump1eb44332009-09-09 15:08:12 +00002887
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002888 // Skip the rest of this declarator, up until the comma or semicolon.
2889 SkipUntil(tok::comma, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00002890 return;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002891 }
Mike Stump1eb44332009-09-09 15:08:12 +00002892
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002893 // If an identifier is present, consume and remember it.
2894 IdentifierInfo *Name = 0;
2895 SourceLocation NameLoc;
2896 if (Tok.is(tok::identifier)) {
2897 Name = Tok.getIdentifierInfo();
2898 NameLoc = ConsumeToken();
2899 }
Mike Stump1eb44332009-09-09 15:08:12 +00002900
Richard Smithbdad7a22012-01-10 01:33:14 +00002901 if (!Name && ScopedEnumKWLoc.isValid()) {
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002902 // C++0x 7.2p2: The optional identifier shall not be omitted in the
2903 // declaration of a scoped enumeration.
2904 Diag(Tok, diag::err_scoped_enum_missing_identifier);
Richard Smithbdad7a22012-01-10 01:33:14 +00002905 ScopedEnumKWLoc = SourceLocation();
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00002906 IsScopedUsingClassTag = false;
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002907 }
2908
John McCall13489672012-05-07 06:16:58 +00002909 // Okay, end the suppression area. We'll decide whether to emit the
2910 // diagnostics in a second.
2911 if (shouldDelayDiagsInTag)
2912 diagsFromTag.done();
Richard Smith1af83c42012-03-23 03:33:32 +00002913
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002914 TypeResult BaseType;
2915
Douglas Gregora61b3e72010-12-01 17:42:47 +00002916 // Parse the fixed underlying type.
Douglas Gregorb9075602011-02-22 02:55:24 +00002917 if (AllowFixedUnderlyingType && Tok.is(tok::colon)) {
Douglas Gregora61b3e72010-12-01 17:42:47 +00002918 bool PossibleBitfield = false;
2919 if (getCurScope()->getFlags() & Scope::ClassScope) {
2920 // If we're in class scope, this can either be an enum declaration with
2921 // an underlying type, or a declaration of a bitfield member. We try to
2922 // use a simple disambiguation scheme first to catch the common cases
2923 // (integer literal, sizeof); if it's still ambiguous, we then consider
2924 // anything that's a simple-type-specifier followed by '(' as an
2925 // expression. This suffices because function types are not valid
2926 // underlying types anyway.
2927 TPResult TPR = isExpressionOrTypeSpecifierSimple(NextToken().getKind());
2928 // If the next token starts an expression, we know we're parsing a
2929 // bit-field. This is the common case.
2930 if (TPR == TPResult::True())
2931 PossibleBitfield = true;
2932 // If the next token starts a type-specifier-seq, it may be either a
2933 // a fixed underlying type or the start of a function-style cast in C++;
2934 // lookahead one more token to see if it's obvious that we have a
2935 // fixed underlying type.
2936 else if (TPR == TPResult::False() &&
2937 GetLookAheadToken(2).getKind() == tok::semi) {
2938 // Consume the ':'.
2939 ConsumeToken();
2940 } else {
2941 // We have the start of a type-specifier-seq, so we have to perform
2942 // tentative parsing to determine whether we have an expression or a
2943 // type.
2944 TentativeParsingAction TPA(*this);
2945
2946 // Consume the ':'.
2947 ConsumeToken();
Richard Smithd81e9612012-02-23 01:36:12 +00002948
2949 // If we see a type specifier followed by an open-brace, we have an
2950 // ambiguity between an underlying type and a C++11 braced
2951 // function-style cast. Resolve this by always treating it as an
2952 // underlying type.
2953 // FIXME: The standard is not entirely clear on how to disambiguate in
2954 // this case.
David Blaikie4e4d0842012-03-11 07:00:24 +00002955 if ((getLangOpts().CPlusPlus &&
Richard Smithd81e9612012-02-23 01:36:12 +00002956 isCXXDeclarationSpecifier(TPResult::True()) != TPResult::True()) ||
David Blaikie4e4d0842012-03-11 07:00:24 +00002957 (!getLangOpts().CPlusPlus && !isDeclarationSpecifier(true))) {
Douglas Gregora61b3e72010-12-01 17:42:47 +00002958 // We'll parse this as a bitfield later.
2959 PossibleBitfield = true;
2960 TPA.Revert();
2961 } else {
2962 // We have a type-specifier-seq.
2963 TPA.Commit();
2964 }
2965 }
2966 } else {
2967 // Consume the ':'.
2968 ConsumeToken();
2969 }
2970
2971 if (!PossibleBitfield) {
2972 SourceRange Range;
2973 BaseType = ParseTypeName(&Range);
Douglas Gregor86f208c2011-02-22 20:32:04 +00002974
David Blaikie4e4d0842012-03-11 07:00:24 +00002975 if (!getLangOpts().CPlusPlus0x && !getLangOpts().ObjC2)
Douglas Gregor86f208c2011-02-22 20:32:04 +00002976 Diag(StartLoc, diag::ext_ms_enum_fixed_underlying_type)
2977 << Range;
David Blaikie4e4d0842012-03-11 07:00:24 +00002978 if (getLangOpts().CPlusPlus0x)
Richard Smith7fe62082011-10-15 05:09:34 +00002979 Diag(StartLoc, diag::warn_cxx98_compat_enum_fixed_underlying_type);
Douglas Gregora61b3e72010-12-01 17:42:47 +00002980 }
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002981 }
2982
Richard Smithbdad7a22012-01-10 01:33:14 +00002983 // There are four options here. If we have 'friend enum foo;' then this is a
2984 // friend declaration, and cannot have an accompanying definition. If we have
2985 // 'enum foo;', then this is a forward declaration. If we have
2986 // 'enum foo {...' then this is a definition. Otherwise we have something
2987 // like 'enum foo xyz', a reference.
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002988 //
2989 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
2990 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
2991 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
2992 //
John McCallf312b1e2010-08-26 23:41:50 +00002993 Sema::TagUseKind TUK;
John McCall13489672012-05-07 06:16:58 +00002994 if (!AllowDeclaration) {
Richard Smith7796eb52012-03-12 08:56:40 +00002995 TUK = Sema::TUK_Reference;
John McCall13489672012-05-07 06:16:58 +00002996 } else if (Tok.is(tok::l_brace)) {
2997 if (DS.isFriendSpecified()) {
2998 Diag(Tok.getLocation(), diag::err_friend_decl_defines_type)
2999 << SourceRange(DS.getFriendSpecLoc());
3000 ConsumeBrace();
3001 SkipUntil(tok::r_brace);
3002 TUK = Sema::TUK_Friend;
3003 } else {
3004 TUK = Sema::TUK_Definition;
3005 }
3006 } else if (Tok.is(tok::semi) && DSC != DSC_type_specifier) {
3007 TUK = (DS.isFriendSpecified() ? Sema::TUK_Friend : Sema::TUK_Declaration);
3008 } else {
John McCallf312b1e2010-08-26 23:41:50 +00003009 TUK = Sema::TUK_Reference;
John McCall13489672012-05-07 06:16:58 +00003010 }
3011
3012 // If this is an elaborated type specifier, and we delayed
3013 // diagnostics before, just merge them into the current pool.
3014 if (TUK == Sema::TUK_Reference && shouldDelayDiagsInTag) {
3015 diagsFromTag.redelay();
3016 }
Richard Smith1af83c42012-03-23 03:33:32 +00003017
3018 MultiTemplateParamsArg TParams;
Douglas Gregor8fc6d232010-05-03 17:48:54 +00003019 if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate &&
John McCallf312b1e2010-08-26 23:41:50 +00003020 TUK != Sema::TUK_Reference) {
Richard Smith1af83c42012-03-23 03:33:32 +00003021 if (!getLangOpts().CPlusPlus0x || !SS.isSet()) {
3022 // Skip the rest of this declarator, up until the comma or semicolon.
3023 Diag(Tok, diag::err_enum_template);
3024 SkipUntil(tok::comma, true);
3025 return;
3026 }
3027
3028 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) {
3029 // Enumerations can't be explicitly instantiated.
3030 DS.SetTypeSpecError();
3031 Diag(StartLoc, diag::err_explicit_instantiation_enum);
3032 return;
3033 }
3034
3035 assert(TemplateInfo.TemplateParams && "no template parameters");
3036 TParams = MultiTemplateParamsArg(TemplateInfo.TemplateParams->data(),
3037 TemplateInfo.TemplateParams->size());
Douglas Gregor8fc6d232010-05-03 17:48:54 +00003038 }
Richard Smith1af83c42012-03-23 03:33:32 +00003039
Douglas Gregorb9075602011-02-22 02:55:24 +00003040 if (!Name && TUK != Sema::TUK_Definition) {
3041 Diag(Tok, diag::err_enumerator_unnamed_no_def);
Richard Smith1af83c42012-03-23 03:33:32 +00003042
Douglas Gregorb9075602011-02-22 02:55:24 +00003043 // Skip the rest of this declarator, up until the comma or semicolon.
3044 SkipUntil(tok::comma, true);
3045 return;
3046 }
Richard Smith1af83c42012-03-23 03:33:32 +00003047
Douglas Gregor402abb52009-05-28 23:31:59 +00003048 bool Owned = false;
John McCallc4e70192009-09-11 04:59:25 +00003049 bool IsDependent = false;
Douglas Gregor48c89f42010-04-24 16:38:41 +00003050 const char *PrevSpec = 0;
3051 unsigned DiagID;
John McCalld226f652010-08-21 09:40:31 +00003052 Decl *TagDecl = Actions.ActOnTag(getCurScope(), DeclSpec::TST_enum, TUK,
John McCall7f040a92010-12-24 02:08:15 +00003053 StartLoc, SS, Name, NameLoc, attrs.getList(),
Richard Smith1af83c42012-03-23 03:33:32 +00003054 AS, DS.getModulePrivateSpecLoc(), TParams,
Richard Smithbdad7a22012-01-10 01:33:14 +00003055 Owned, IsDependent, ScopedEnumKWLoc,
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00003056 IsScopedUsingClassTag, BaseType);
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003057
Douglas Gregor48c89f42010-04-24 16:38:41 +00003058 if (IsDependent) {
3059 // This enum has a dependent nested-name-specifier. Handle it as a
3060 // dependent tag.
3061 if (!Name) {
3062 DS.SetTypeSpecError();
3063 Diag(Tok, diag::err_expected_type_name_after_typename);
3064 return;
3065 }
3066
Douglas Gregor23c94db2010-07-02 17:43:08 +00003067 TypeResult Type = Actions.ActOnDependentTag(getCurScope(), DeclSpec::TST_enum,
Douglas Gregor48c89f42010-04-24 16:38:41 +00003068 TUK, SS, Name, StartLoc,
3069 NameLoc);
3070 if (Type.isInvalid()) {
3071 DS.SetTypeSpecError();
3072 return;
3073 }
3074
Abramo Bagnara0daaf322011-03-16 20:16:18 +00003075 if (DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc,
3076 NameLoc.isValid() ? NameLoc : StartLoc,
3077 PrevSpec, DiagID, Type.get()))
Douglas Gregor48c89f42010-04-24 16:38:41 +00003078 Diag(StartLoc, DiagID) << PrevSpec;
3079
3080 return;
3081 }
Mike Stump1eb44332009-09-09 15:08:12 +00003082
John McCalld226f652010-08-21 09:40:31 +00003083 if (!TagDecl) {
Douglas Gregor48c89f42010-04-24 16:38:41 +00003084 // The action failed to produce an enumeration tag. If this is a
3085 // definition, consume the entire definition.
Richard Smith7796eb52012-03-12 08:56:40 +00003086 if (Tok.is(tok::l_brace) && TUK != Sema::TUK_Reference) {
Douglas Gregor48c89f42010-04-24 16:38:41 +00003087 ConsumeBrace();
3088 SkipUntil(tok::r_brace);
3089 }
3090
3091 DS.SetTypeSpecError();
3092 return;
3093 }
Richard Smithbdad7a22012-01-10 01:33:14 +00003094
Richard Smith7796eb52012-03-12 08:56:40 +00003095 if (Tok.is(tok::l_brace) && TUK != Sema::TUK_Reference) {
John McCall13489672012-05-07 06:16:58 +00003096 ParseEnumBody(StartLoc, TagDecl);
Richard Smithbdad7a22012-01-10 01:33:14 +00003097 }
Mike Stump1eb44332009-09-09 15:08:12 +00003098
Abramo Bagnara0daaf322011-03-16 20:16:18 +00003099 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc,
3100 NameLoc.isValid() ? NameLoc : StartLoc,
3101 PrevSpec, DiagID, TagDecl, Owned))
John McCallfec54012009-08-03 20:12:06 +00003102 Diag(StartLoc, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00003103}
3104
3105/// ParseEnumBody - Parse a {} enclosed enumerator-list.
3106/// enumerator-list:
3107/// enumerator
3108/// enumerator-list ',' enumerator
3109/// enumerator:
3110/// enumeration-constant
3111/// enumeration-constant '=' constant-expression
3112/// enumeration-constant:
3113/// identifier
3114///
John McCalld226f652010-08-21 09:40:31 +00003115void Parser::ParseEnumBody(SourceLocation StartLoc, Decl *EnumDecl) {
Douglas Gregor074149e2009-01-05 19:45:36 +00003116 // Enter the scope of the enum body and start the definition.
3117 ParseScope EnumScope(this, Scope::DeclScope);
Douglas Gregor23c94db2010-07-02 17:43:08 +00003118 Actions.ActOnTagStartDefinition(getCurScope(), EnumDecl);
Douglas Gregor074149e2009-01-05 19:45:36 +00003119
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003120 BalancedDelimiterTracker T(*this, tok::l_brace);
3121 T.consumeOpen();
Mike Stump1eb44332009-09-09 15:08:12 +00003122
Chris Lattner7946dd32007-08-27 17:24:30 +00003123 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
David Blaikie4e4d0842012-03-11 07:00:24 +00003124 if (Tok.is(tok::r_brace) && !getLangOpts().CPlusPlus)
Fariborz Jahanian05115522010-05-28 22:23:22 +00003125 Diag(Tok, diag::error_empty_enum);
Mike Stump1eb44332009-09-09 15:08:12 +00003126
Chris Lattner5f9e2722011-07-23 10:55:15 +00003127 SmallVector<Decl *, 32> EnumConstantDecls;
Reid Spencer5f016e22007-07-11 17:01:13 +00003128
John McCalld226f652010-08-21 09:40:31 +00003129 Decl *LastEnumConstDecl = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00003130
Reid Spencer5f016e22007-07-11 17:01:13 +00003131 // Parse the enumerator-list.
Chris Lattner04d66662007-10-09 17:33:22 +00003132 while (Tok.is(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003133 IdentifierInfo *Ident = Tok.getIdentifierInfo();
3134 SourceLocation IdentLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00003135
John McCall5b629aa2010-10-22 23:36:17 +00003136 // If attributes exist after the enumerator, parse them.
John McCall0b7e6782011-03-24 11:26:52 +00003137 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00003138 MaybeParseGNUAttributes(attrs);
John McCall5b629aa2010-10-22 23:36:17 +00003139
Reid Spencer5f016e22007-07-11 17:01:13 +00003140 SourceLocation EqualLoc;
John McCall60d7b3a2010-08-24 06:29:42 +00003141 ExprResult AssignedVal;
John McCall92576642012-05-07 06:16:41 +00003142 ParsingDeclRAIIObject PD(*this, ParsingDeclRAIIObject::NoParent);
Fariborz Jahanian5a477db2011-12-09 01:15:54 +00003143
Chris Lattner04d66662007-10-09 17:33:22 +00003144 if (Tok.is(tok::equal)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003145 EqualLoc = ConsumeToken();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00003146 AssignedVal = ParseConstantExpression();
3147 if (AssignedVal.isInvalid())
Reid Spencer5f016e22007-07-11 17:01:13 +00003148 SkipUntil(tok::comma, tok::r_brace, true, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00003149 }
Mike Stump1eb44332009-09-09 15:08:12 +00003150
Reid Spencer5f016e22007-07-11 17:01:13 +00003151 // Install the enumerator constant into EnumDecl.
John McCalld226f652010-08-21 09:40:31 +00003152 Decl *EnumConstDecl = Actions.ActOnEnumConstant(getCurScope(), EnumDecl,
3153 LastEnumConstDecl,
3154 IdentLoc, Ident,
John McCall7f040a92010-12-24 02:08:15 +00003155 attrs.getList(), EqualLoc,
John McCalld226f652010-08-21 09:40:31 +00003156 AssignedVal.release());
Fariborz Jahanian5a477db2011-12-09 01:15:54 +00003157 PD.complete(EnumConstDecl);
3158
Reid Spencer5f016e22007-07-11 17:01:13 +00003159 EnumConstantDecls.push_back(EnumConstDecl);
3160 LastEnumConstDecl = EnumConstDecl;
Mike Stump1eb44332009-09-09 15:08:12 +00003161
Douglas Gregor751f6922010-09-07 14:51:08 +00003162 if (Tok.is(tok::identifier)) {
3163 // We're missing a comma between enumerators.
3164 SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation);
3165 Diag(Loc, diag::err_enumerator_list_missing_comma)
3166 << FixItHint::CreateInsertion(Loc, ", ");
3167 continue;
3168 }
3169
Chris Lattner04d66662007-10-09 17:33:22 +00003170 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +00003171 break;
3172 SourceLocation CommaLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00003173
Richard Smith7fe62082011-10-15 05:09:34 +00003174 if (Tok.isNot(tok::identifier)) {
David Blaikie4e4d0842012-03-11 07:00:24 +00003175 if (!getLangOpts().C99 && !getLangOpts().CPlusPlus0x)
Richard Smith7fe62082011-10-15 05:09:34 +00003176 Diag(CommaLoc, diag::ext_enumerator_list_comma)
David Blaikie4e4d0842012-03-11 07:00:24 +00003177 << getLangOpts().CPlusPlus
Richard Smith7fe62082011-10-15 05:09:34 +00003178 << FixItHint::CreateRemoval(CommaLoc);
David Blaikie4e4d0842012-03-11 07:00:24 +00003179 else if (getLangOpts().CPlusPlus0x)
Richard Smith7fe62082011-10-15 05:09:34 +00003180 Diag(CommaLoc, diag::warn_cxx98_compat_enumerator_list_comma)
3181 << FixItHint::CreateRemoval(CommaLoc);
3182 }
Reid Spencer5f016e22007-07-11 17:01:13 +00003183 }
Mike Stump1eb44332009-09-09 15:08:12 +00003184
Reid Spencer5f016e22007-07-11 17:01:13 +00003185 // Eat the }.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003186 T.consumeClose();
Reid Spencer5f016e22007-07-11 17:01:13 +00003187
Reid Spencer5f016e22007-07-11 17:01:13 +00003188 // If attributes exist after the identifier list, parse them.
John McCall0b7e6782011-03-24 11:26:52 +00003189 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00003190 MaybeParseGNUAttributes(attrs);
Douglas Gregor72de6672009-01-08 20:45:30 +00003191
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003192 Actions.ActOnEnumBody(StartLoc, T.getOpenLocation(), T.getCloseLocation(),
3193 EnumDecl, EnumConstantDecls.data(),
3194 EnumConstantDecls.size(), getCurScope(),
3195 attrs.getList());
Mike Stump1eb44332009-09-09 15:08:12 +00003196
Douglas Gregor72de6672009-01-08 20:45:30 +00003197 EnumScope.Exit();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003198 Actions.ActOnTagFinishDefinition(getCurScope(), EnumDecl,
3199 T.getCloseLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00003200}
3201
3202/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff5f8aa692008-02-11 23:15:56 +00003203/// start of a type-qualifier-list.
3204bool Parser::isTypeQualifier() const {
3205 switch (Tok.getKind()) {
3206 default: return false;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003207
3208 // type-qualifier only in OpenCL
3209 case tok::kw_private:
David Blaikie4e4d0842012-03-11 07:00:24 +00003210 return getLangOpts().OpenCL;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003211
Steve Naroff5f8aa692008-02-11 23:15:56 +00003212 // type-qualifier
3213 case tok::kw_const:
3214 case tok::kw_volatile:
3215 case tok::kw_restrict:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003216 case tok::kw___private:
3217 case tok::kw___local:
3218 case tok::kw___global:
3219 case tok::kw___constant:
3220 case tok::kw___read_only:
3221 case tok::kw___read_write:
3222 case tok::kw___write_only:
Steve Naroff5f8aa692008-02-11 23:15:56 +00003223 return true;
3224 }
3225}
3226
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003227/// isKnownToBeTypeSpecifier - Return true if we know that the specified token
3228/// is definitely a type-specifier. Return false if it isn't part of a type
3229/// specifier or if we're not sure.
3230bool Parser::isKnownToBeTypeSpecifier(const Token &Tok) const {
3231 switch (Tok.getKind()) {
3232 default: return false;
3233 // type-specifiers
3234 case tok::kw_short:
3235 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00003236 case tok::kw___int64:
Richard Smith5a5a9712012-04-04 06:24:32 +00003237 case tok::kw___int128:
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003238 case tok::kw_signed:
3239 case tok::kw_unsigned:
3240 case tok::kw__Complex:
3241 case tok::kw__Imaginary:
3242 case tok::kw_void:
3243 case tok::kw_char:
3244 case tok::kw_wchar_t:
3245 case tok::kw_char16_t:
3246 case tok::kw_char32_t:
3247 case tok::kw_int:
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00003248 case tok::kw_half:
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003249 case tok::kw_float:
3250 case tok::kw_double:
3251 case tok::kw_bool:
3252 case tok::kw__Bool:
3253 case tok::kw__Decimal32:
3254 case tok::kw__Decimal64:
3255 case tok::kw__Decimal128:
3256 case tok::kw___vector:
3257
3258 // struct-or-union-specifier (C99) or class-specifier (C++)
3259 case tok::kw_class:
3260 case tok::kw_struct:
3261 case tok::kw_union:
3262 // enum-specifier
3263 case tok::kw_enum:
3264
3265 // typedef-name
3266 case tok::annot_typename:
3267 return true;
3268 }
3269}
3270
Steve Naroff5f8aa692008-02-11 23:15:56 +00003271/// isTypeSpecifierQualifier - Return true if the current token could be the
Reid Spencer5f016e22007-07-11 17:01:13 +00003272/// start of a specifier-qualifier-list.
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003273bool Parser::isTypeSpecifierQualifier() {
Reid Spencer5f016e22007-07-11 17:01:13 +00003274 switch (Tok.getKind()) {
3275 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00003276
Chris Lattner166a8fc2009-01-04 23:41:41 +00003277 case tok::identifier: // foo::bar
John Thompson82287d12010-02-05 00:12:22 +00003278 if (TryAltiVecVectorToken())
3279 return true;
3280 // Fall through.
Douglas Gregord57959a2009-03-27 23:10:48 +00003281 case tok::kw_typename: // typename T::type
Chris Lattner166a8fc2009-01-04 23:41:41 +00003282 // Annotate typenames and C++ scope specifiers. If we get one, just
3283 // recurse to handle whatever we get.
3284 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003285 return true;
3286 if (Tok.is(tok::identifier))
3287 return false;
3288 return isTypeSpecifierQualifier();
Douglas Gregord57959a2009-03-27 23:10:48 +00003289
Chris Lattner166a8fc2009-01-04 23:41:41 +00003290 case tok::coloncolon: // ::foo::bar
3291 if (NextToken().is(tok::kw_new) || // ::new
3292 NextToken().is(tok::kw_delete)) // ::delete
3293 return false;
3294
Chris Lattner166a8fc2009-01-04 23:41:41 +00003295 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003296 return true;
3297 return isTypeSpecifierQualifier();
Mike Stump1eb44332009-09-09 15:08:12 +00003298
Reid Spencer5f016e22007-07-11 17:01:13 +00003299 // GNU attributes support.
3300 case tok::kw___attribute:
Steve Naroffd1861fd2007-07-31 12:34:36 +00003301 // GNU typeof support.
3302 case tok::kw_typeof:
Mike Stump1eb44332009-09-09 15:08:12 +00003303
Reid Spencer5f016e22007-07-11 17:01:13 +00003304 // type-specifiers
3305 case tok::kw_short:
3306 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00003307 case tok::kw___int64:
Richard Smith5a5a9712012-04-04 06:24:32 +00003308 case tok::kw___int128:
Reid Spencer5f016e22007-07-11 17:01:13 +00003309 case tok::kw_signed:
3310 case tok::kw_unsigned:
3311 case tok::kw__Complex:
3312 case tok::kw__Imaginary:
3313 case tok::kw_void:
3314 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00003315 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00003316 case tok::kw_char16_t:
3317 case tok::kw_char32_t:
Reid Spencer5f016e22007-07-11 17:01:13 +00003318 case tok::kw_int:
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00003319 case tok::kw_half:
Reid Spencer5f016e22007-07-11 17:01:13 +00003320 case tok::kw_float:
3321 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00003322 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00003323 case tok::kw__Bool:
3324 case tok::kw__Decimal32:
3325 case tok::kw__Decimal64:
3326 case tok::kw__Decimal128:
John Thompson82287d12010-02-05 00:12:22 +00003327 case tok::kw___vector:
Mike Stump1eb44332009-09-09 15:08:12 +00003328
Chris Lattner99dc9142008-04-13 18:59:07 +00003329 // struct-or-union-specifier (C99) or class-specifier (C++)
3330 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00003331 case tok::kw_struct:
3332 case tok::kw_union:
3333 // enum-specifier
3334 case tok::kw_enum:
Mike Stump1eb44332009-09-09 15:08:12 +00003335
Reid Spencer5f016e22007-07-11 17:01:13 +00003336 // type-qualifier
3337 case tok::kw_const:
3338 case tok::kw_volatile:
3339 case tok::kw_restrict:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003340
3341 // typedef-name
Chris Lattnerb31757b2009-01-06 05:06:21 +00003342 case tok::annot_typename:
Reid Spencer5f016e22007-07-11 17:01:13 +00003343 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00003344
Chris Lattner7c186be2008-10-20 00:25:30 +00003345 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
3346 case tok::less:
David Blaikie4e4d0842012-03-11 07:00:24 +00003347 return getLangOpts().ObjC1;
Mike Stump1eb44332009-09-09 15:08:12 +00003348
Steve Naroff239f0732008-12-25 14:16:32 +00003349 case tok::kw___cdecl:
3350 case tok::kw___stdcall:
3351 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00003352 case tok::kw___thiscall:
Eli Friedman290eeb02009-06-08 23:27:34 +00003353 case tok::kw___w64:
3354 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00003355 case tok::kw___ptr32:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003356 case tok::kw___pascal:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00003357 case tok::kw___unaligned:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003358
3359 case tok::kw___private:
3360 case tok::kw___local:
3361 case tok::kw___global:
3362 case tok::kw___constant:
3363 case tok::kw___read_only:
3364 case tok::kw___read_write:
3365 case tok::kw___write_only:
3366
Eli Friedman290eeb02009-06-08 23:27:34 +00003367 return true;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003368
3369 case tok::kw_private:
David Blaikie4e4d0842012-03-11 07:00:24 +00003370 return getLangOpts().OpenCL;
Eli Friedmanb001de72011-10-06 23:00:33 +00003371
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00003372 // C11 _Atomic()
Eli Friedmanb001de72011-10-06 23:00:33 +00003373 case tok::kw__Atomic:
3374 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00003375 }
3376}
3377
3378/// isDeclarationSpecifier() - Return true if the current token is part of a
3379/// declaration specifier.
Douglas Gregor9497a732010-09-16 01:51:54 +00003380///
3381/// \param DisambiguatingWithExpression True to indicate that the purpose of
3382/// this check is to disambiguate between an expression and a declaration.
3383bool Parser::isDeclarationSpecifier(bool DisambiguatingWithExpression) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003384 switch (Tok.getKind()) {
3385 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00003386
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003387 case tok::kw_private:
David Blaikie4e4d0842012-03-11 07:00:24 +00003388 return getLangOpts().OpenCL;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003389
Chris Lattner166a8fc2009-01-04 23:41:41 +00003390 case tok::identifier: // foo::bar
Steve Naroff61f72cb2009-03-09 21:12:44 +00003391 // Unfortunate hack to support "Class.factoryMethod" notation.
David Blaikie4e4d0842012-03-11 07:00:24 +00003392 if (getLangOpts().ObjC1 && NextToken().is(tok::period))
Steve Naroff61f72cb2009-03-09 21:12:44 +00003393 return false;
John Thompson82287d12010-02-05 00:12:22 +00003394 if (TryAltiVecVectorToken())
3395 return true;
3396 // Fall through.
David Blaikie42d6d0c2011-12-04 05:04:18 +00003397 case tok::kw_decltype: // decltype(T())::type
Douglas Gregord57959a2009-03-27 23:10:48 +00003398 case tok::kw_typename: // typename T::type
Chris Lattner166a8fc2009-01-04 23:41:41 +00003399 // Annotate typenames and C++ scope specifiers. If we get one, just
3400 // recurse to handle whatever we get.
3401 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003402 return true;
3403 if (Tok.is(tok::identifier))
3404 return false;
Douglas Gregor9497a732010-09-16 01:51:54 +00003405
3406 // If we're in Objective-C and we have an Objective-C class type followed
3407 // by an identifier and then either ':' or ']', in a place where an
3408 // expression is permitted, then this is probably a class message send
3409 // missing the initial '['. In this case, we won't consider this to be
3410 // the start of a declaration.
3411 if (DisambiguatingWithExpression &&
3412 isStartOfObjCClassMessageMissingOpenBracket())
3413 return false;
3414
John McCall9ba61662010-02-26 08:45:28 +00003415 return isDeclarationSpecifier();
3416
Chris Lattner166a8fc2009-01-04 23:41:41 +00003417 case tok::coloncolon: // ::foo::bar
3418 if (NextToken().is(tok::kw_new) || // ::new
3419 NextToken().is(tok::kw_delete)) // ::delete
3420 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00003421
Chris Lattner166a8fc2009-01-04 23:41:41 +00003422 // Annotate typenames and C++ scope specifiers. If we get one, just
3423 // recurse to handle whatever we get.
3424 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003425 return true;
3426 return isDeclarationSpecifier();
Mike Stump1eb44332009-09-09 15:08:12 +00003427
Reid Spencer5f016e22007-07-11 17:01:13 +00003428 // storage-class-specifier
3429 case tok::kw_typedef:
3430 case tok::kw_extern:
Steve Naroff8d54bf22007-12-18 00:16:02 +00003431 case tok::kw___private_extern__:
Reid Spencer5f016e22007-07-11 17:01:13 +00003432 case tok::kw_static:
3433 case tok::kw_auto:
3434 case tok::kw_register:
3435 case tok::kw___thread:
Mike Stump1eb44332009-09-09 15:08:12 +00003436
Douglas Gregor8d267c52011-09-09 02:06:17 +00003437 // Modules
3438 case tok::kw___module_private__:
3439
Reid Spencer5f016e22007-07-11 17:01:13 +00003440 // type-specifiers
3441 case tok::kw_short:
3442 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00003443 case tok::kw___int64:
Richard Smith5a5a9712012-04-04 06:24:32 +00003444 case tok::kw___int128:
Reid Spencer5f016e22007-07-11 17:01:13 +00003445 case tok::kw_signed:
3446 case tok::kw_unsigned:
3447 case tok::kw__Complex:
3448 case tok::kw__Imaginary:
3449 case tok::kw_void:
3450 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00003451 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00003452 case tok::kw_char16_t:
3453 case tok::kw_char32_t:
3454
Reid Spencer5f016e22007-07-11 17:01:13 +00003455 case tok::kw_int:
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00003456 case tok::kw_half:
Reid Spencer5f016e22007-07-11 17:01:13 +00003457 case tok::kw_float:
3458 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00003459 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00003460 case tok::kw__Bool:
3461 case tok::kw__Decimal32:
3462 case tok::kw__Decimal64:
3463 case tok::kw__Decimal128:
John Thompson82287d12010-02-05 00:12:22 +00003464 case tok::kw___vector:
Mike Stump1eb44332009-09-09 15:08:12 +00003465
Chris Lattner99dc9142008-04-13 18:59:07 +00003466 // struct-or-union-specifier (C99) or class-specifier (C++)
3467 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00003468 case tok::kw_struct:
3469 case tok::kw_union:
3470 // enum-specifier
3471 case tok::kw_enum:
Mike Stump1eb44332009-09-09 15:08:12 +00003472
Reid Spencer5f016e22007-07-11 17:01:13 +00003473 // type-qualifier
3474 case tok::kw_const:
3475 case tok::kw_volatile:
3476 case tok::kw_restrict:
Steve Naroffd1861fd2007-07-31 12:34:36 +00003477
Reid Spencer5f016e22007-07-11 17:01:13 +00003478 // function-specifier
3479 case tok::kw_inline:
Douglas Gregorb48fe382008-10-31 09:07:45 +00003480 case tok::kw_virtual:
3481 case tok::kw_explicit:
Chris Lattnerd6c7c182007-08-09 16:40:21 +00003482
Peter Collingbournec6eb44b2011-04-15 00:35:57 +00003483 // static_assert-declaration
3484 case tok::kw__Static_assert:
3485
Chris Lattner1ef08762007-08-09 17:01:07 +00003486 // GNU typeof support.
3487 case tok::kw_typeof:
Mike Stump1eb44332009-09-09 15:08:12 +00003488
Chris Lattner1ef08762007-08-09 17:01:07 +00003489 // GNU attributes.
Chris Lattnerd6c7c182007-08-09 16:40:21 +00003490 case tok::kw___attribute:
Reid Spencer5f016e22007-07-11 17:01:13 +00003491 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00003492
Francois Pichete3d49b42011-06-19 08:02:06 +00003493 // C++0x decltype.
David Blaikie42d6d0c2011-12-04 05:04:18 +00003494 case tok::annot_decltype:
Francois Pichete3d49b42011-06-19 08:02:06 +00003495 return true;
3496
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00003497 // C11 _Atomic()
Eli Friedmanb001de72011-10-06 23:00:33 +00003498 case tok::kw__Atomic:
3499 return true;
3500
Chris Lattnerf3948c42008-07-26 03:38:44 +00003501 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
3502 case tok::less:
David Blaikie4e4d0842012-03-11 07:00:24 +00003503 return getLangOpts().ObjC1;
Mike Stump1eb44332009-09-09 15:08:12 +00003504
Douglas Gregord9d75e52011-04-27 05:41:15 +00003505 // typedef-name
3506 case tok::annot_typename:
3507 return !DisambiguatingWithExpression ||
3508 !isStartOfObjCClassMessageMissingOpenBracket();
3509
Steve Naroff47f52092009-01-06 19:34:12 +00003510 case tok::kw___declspec:
Steve Naroff239f0732008-12-25 14:16:32 +00003511 case tok::kw___cdecl:
3512 case tok::kw___stdcall:
3513 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00003514 case tok::kw___thiscall:
Eli Friedman290eeb02009-06-08 23:27:34 +00003515 case tok::kw___w64:
3516 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00003517 case tok::kw___ptr32:
Eli Friedman290eeb02009-06-08 23:27:34 +00003518 case tok::kw___forceinline:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003519 case tok::kw___pascal:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00003520 case tok::kw___unaligned:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003521
3522 case tok::kw___private:
3523 case tok::kw___local:
3524 case tok::kw___global:
3525 case tok::kw___constant:
3526 case tok::kw___read_only:
3527 case tok::kw___read_write:
3528 case tok::kw___write_only:
3529
Eli Friedman290eeb02009-06-08 23:27:34 +00003530 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00003531 }
3532}
3533
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003534bool Parser::isConstructorDeclarator() {
3535 TentativeParsingAction TPA(*this);
3536
3537 // Parse the C++ scope specifier.
3538 CXXScopeSpec SS;
Douglas Gregorefaa93a2011-11-07 17:33:42 +00003539 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
3540 /*EnteringContext=*/true)) {
John McCall9ba61662010-02-26 08:45:28 +00003541 TPA.Revert();
3542 return false;
3543 }
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003544
3545 // Parse the constructor name.
3546 if (Tok.is(tok::identifier) || Tok.is(tok::annot_template_id)) {
3547 // We already know that we have a constructor name; just consume
3548 // the token.
3549 ConsumeToken();
3550 } else {
3551 TPA.Revert();
3552 return false;
3553 }
3554
Richard Smith22592862012-03-27 23:05:05 +00003555 // Current class name must be followed by a left parenthesis.
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003556 if (Tok.isNot(tok::l_paren)) {
3557 TPA.Revert();
3558 return false;
3559 }
3560 ConsumeParen();
3561
Richard Smith22592862012-03-27 23:05:05 +00003562 // A right parenthesis, or ellipsis followed by a right parenthesis signals
3563 // that we have a constructor.
3564 if (Tok.is(tok::r_paren) ||
3565 (Tok.is(tok::ellipsis) && NextToken().is(tok::r_paren))) {
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003566 TPA.Revert();
3567 return true;
3568 }
3569
3570 // If we need to, enter the specified scope.
3571 DeclaratorScopeObj DeclScopeObj(*this, SS);
Douglas Gregor23c94db2010-07-02 17:43:08 +00003572 if (SS.isSet() && Actions.ShouldEnterDeclaratorScope(getCurScope(), SS))
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003573 DeclScopeObj.EnterDeclaratorScope();
3574
Francois Pichetdfaa5fb2011-01-31 04:54:32 +00003575 // Optionally skip Microsoft attributes.
John McCall0b7e6782011-03-24 11:26:52 +00003576 ParsedAttributes Attrs(AttrFactory);
Francois Pichetdfaa5fb2011-01-31 04:54:32 +00003577 MaybeParseMicrosoftAttributes(Attrs);
3578
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003579 // Check whether the next token(s) are part of a declaration
3580 // specifier, in which case we have the start of a parameter and,
3581 // therefore, we know that this is a constructor.
Richard Smith412e0cc2012-03-27 00:56:56 +00003582 bool IsConstructor = false;
3583 if (isDeclarationSpecifier())
3584 IsConstructor = true;
3585 else if (Tok.is(tok::identifier) ||
3586 (Tok.is(tok::annot_cxxscope) && NextToken().is(tok::identifier))) {
3587 // We've seen "C ( X" or "C ( X::Y", but "X" / "X::Y" is not a type.
3588 // This might be a parenthesized member name, but is more likely to
3589 // be a constructor declaration with an invalid argument type. Keep
3590 // looking.
3591 if (Tok.is(tok::annot_cxxscope))
3592 ConsumeToken();
3593 ConsumeToken();
3594
3595 // If this is not a constructor, we must be parsing a declarator,
Richard Smith5d8388c2012-03-27 01:42:32 +00003596 // which must have one of the following syntactic forms (see the
3597 // grammar extract at the start of ParseDirectDeclarator):
Richard Smith412e0cc2012-03-27 00:56:56 +00003598 switch (Tok.getKind()) {
3599 case tok::l_paren:
3600 // C(X ( int));
3601 case tok::l_square:
3602 // C(X [ 5]);
3603 // C(X [ [attribute]]);
3604 case tok::coloncolon:
3605 // C(X :: Y);
3606 // C(X :: *p);
3607 case tok::r_paren:
3608 // C(X )
3609 // Assume this isn't a constructor, rather than assuming it's a
3610 // constructor with an unnamed parameter of an ill-formed type.
3611 break;
3612
3613 default:
3614 IsConstructor = true;
3615 break;
3616 }
3617 }
3618
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003619 TPA.Revert();
3620 return IsConstructor;
3621}
Reid Spencer5f016e22007-07-11 17:01:13 +00003622
3623/// ParseTypeQualifierListOpt
Dawn Perchik52fc3142010-09-03 01:29:35 +00003624/// type-qualifier-list: [C99 6.7.5]
3625/// type-qualifier
3626/// [vendor] attributes
3627/// [ only if VendorAttributesAllowed=true ]
3628/// type-qualifier-list type-qualifier
3629/// [vendor] type-qualifier-list attributes
3630/// [ only if VendorAttributesAllowed=true ]
3631/// [C++0x] attribute-specifier[opt] is allowed before cv-qualifier-seq
3632/// [ only if CXX0XAttributesAllowed=true ]
3633/// Note: vendor can be GNU, MS, etc.
Reid Spencer5f016e22007-07-11 17:01:13 +00003634///
Dawn Perchik52fc3142010-09-03 01:29:35 +00003635void Parser::ParseTypeQualifierListOpt(DeclSpec &DS,
3636 bool VendorAttributesAllowed,
Richard Smithc56298d2012-04-10 03:25:07 +00003637 bool CXX11AttributesAllowed) {
3638 if (getLangOpts().CPlusPlus0x && CXX11AttributesAllowed &&
Richard Smith6ee326a2012-04-10 01:32:12 +00003639 isCXX11AttributeSpecifier()) {
John McCall0b7e6782011-03-24 11:26:52 +00003640 ParsedAttributesWithRange attrs(AttrFactory);
Richard Smithc56298d2012-04-10 03:25:07 +00003641 ParseCXX11Attributes(attrs);
Richard Smith6ee326a2012-04-10 01:32:12 +00003642 DS.takeAttributesFrom(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00003643 }
Abramo Bagnara796aa442011-03-12 11:17:06 +00003644
3645 SourceLocation EndLoc;
3646
Reid Spencer5f016e22007-07-11 17:01:13 +00003647 while (1) {
John McCallfec54012009-08-03 20:12:06 +00003648 bool isInvalid = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00003649 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00003650 unsigned DiagID = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00003651 SourceLocation Loc = Tok.getLocation();
3652
3653 switch (Tok.getKind()) {
Douglas Gregor1a480c42010-08-27 17:35:51 +00003654 case tok::code_completion:
3655 Actions.CodeCompleteTypeQualifiers(DS);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00003656 return cutOffParsing();
Douglas Gregor1a480c42010-08-27 17:35:51 +00003657
Reid Spencer5f016e22007-07-11 17:01:13 +00003658 case tok::kw_const:
John McCallfec54012009-08-03 20:12:06 +00003659 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, DiagID,
David Blaikie4e4d0842012-03-11 07:00:24 +00003660 getLangOpts());
Reid Spencer5f016e22007-07-11 17:01:13 +00003661 break;
3662 case tok::kw_volatile:
John McCallfec54012009-08-03 20:12:06 +00003663 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
David Blaikie4e4d0842012-03-11 07:00:24 +00003664 getLangOpts());
Reid Spencer5f016e22007-07-11 17:01:13 +00003665 break;
3666 case tok::kw_restrict:
John McCallfec54012009-08-03 20:12:06 +00003667 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
David Blaikie4e4d0842012-03-11 07:00:24 +00003668 getLangOpts());
Reid Spencer5f016e22007-07-11 17:01:13 +00003669 break;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003670
3671 // OpenCL qualifiers:
3672 case tok::kw_private:
David Blaikie4e4d0842012-03-11 07:00:24 +00003673 if (!getLangOpts().OpenCL)
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003674 goto DoneWithTypeQuals;
3675 case tok::kw___private:
3676 case tok::kw___global:
3677 case tok::kw___local:
3678 case tok::kw___constant:
3679 case tok::kw___read_only:
3680 case tok::kw___write_only:
3681 case tok::kw___read_write:
3682 ParseOpenCLQualifiers(DS);
3683 break;
3684
Eli Friedman290eeb02009-06-08 23:27:34 +00003685 case tok::kw___w64:
Steve Naroff86bc6cf2008-12-25 14:41:26 +00003686 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00003687 case tok::kw___ptr32:
Steve Naroff239f0732008-12-25 14:16:32 +00003688 case tok::kw___cdecl:
3689 case tok::kw___stdcall:
3690 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00003691 case tok::kw___thiscall:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00003692 case tok::kw___unaligned:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003693 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00003694 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman290eeb02009-06-08 23:27:34 +00003695 continue;
3696 }
3697 goto DoneWithTypeQuals;
Dawn Perchik52fc3142010-09-03 01:29:35 +00003698 case tok::kw___pascal:
3699 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00003700 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik52fc3142010-09-03 01:29:35 +00003701 continue;
3702 }
3703 goto DoneWithTypeQuals;
Reid Spencer5f016e22007-07-11 17:01:13 +00003704 case tok::kw___attribute:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003705 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00003706 ParseGNUAttributes(DS.getAttributes());
Chris Lattner5a69d1c2008-12-18 07:02:59 +00003707 continue; // do *not* consume the next token!
3708 }
3709 // otherwise, FALL THROUGH!
3710 default:
Steve Naroff239f0732008-12-25 14:16:32 +00003711 DoneWithTypeQuals:
Chris Lattner5a69d1c2008-12-18 07:02:59 +00003712 // If this is not a type-qualifier token, we're done reading type
3713 // qualifiers. First verify that DeclSpec's are consistent.
Douglas Gregor9b3064b2009-04-01 22:41:11 +00003714 DS.Finish(Diags, PP);
Abramo Bagnara796aa442011-03-12 11:17:06 +00003715 if (EndLoc.isValid())
3716 DS.SetRangeEnd(EndLoc);
Chris Lattner5a69d1c2008-12-18 07:02:59 +00003717 return;
Reid Spencer5f016e22007-07-11 17:01:13 +00003718 }
Chris Lattnera1fcbad2008-12-18 06:50:14 +00003719
Reid Spencer5f016e22007-07-11 17:01:13 +00003720 // If the specifier combination wasn't legal, issue a diagnostic.
3721 if (isInvalid) {
3722 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner1ab3b962008-11-18 07:48:38 +00003723 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00003724 }
Abramo Bagnara796aa442011-03-12 11:17:06 +00003725 EndLoc = ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00003726 }
3727}
3728
3729
3730/// ParseDeclarator - Parse and verify a newly-initialized declarator.
3731///
3732void Parser::ParseDeclarator(Declarator &D) {
3733 /// This implements the 'declarator' production in the C grammar, then checks
3734 /// for well-formedness and issues diagnostics.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003735 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Reid Spencer5f016e22007-07-11 17:01:13 +00003736}
3737
Richard Smith9988f282012-03-29 01:16:42 +00003738static bool isPtrOperatorToken(tok::TokenKind Kind, const LangOptions &Lang) {
3739 if (Kind == tok::star || Kind == tok::caret)
3740 return true;
3741
3742 // We parse rvalue refs in C++03, because otherwise the errors are scary.
3743 if (!Lang.CPlusPlus)
3744 return false;
3745
3746 return Kind == tok::amp || Kind == tok::ampamp;
3747}
3748
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003749/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
3750/// is parsed by the function passed to it. Pass null, and the direct-declarator
3751/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003752/// ptr-operator production.
3753///
Richard Smith0706df42011-10-19 21:33:05 +00003754/// If the grammar of this construct is extended, matching changes must also be
Richard Smith5d8388c2012-03-27 01:42:32 +00003755/// made to TryParseDeclarator and MightBeDeclarator, and possibly to
3756/// isConstructorDeclarator.
Richard Smith0706df42011-10-19 21:33:05 +00003757///
Sebastian Redlf30208a2009-01-24 21:16:55 +00003758/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
3759/// [C] pointer[opt] direct-declarator
3760/// [C++] direct-declarator
3761/// [C++] ptr-operator declarator
Reid Spencer5f016e22007-07-11 17:01:13 +00003762///
3763/// pointer: [C99 6.7.5]
3764/// '*' type-qualifier-list[opt]
3765/// '*' type-qualifier-list[opt] pointer
3766///
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003767/// ptr-operator:
3768/// '*' cv-qualifier-seq[opt]
3769/// '&'
Sebastian Redl05532f22009-03-15 22:02:01 +00003770/// [C++0x] '&&'
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003771/// [GNU] '&' restrict[opt] attributes[opt]
Sebastian Redl05532f22009-03-15 22:02:01 +00003772/// [GNU?] '&&' restrict[opt] attributes[opt]
Sebastian Redlf30208a2009-01-24 21:16:55 +00003773/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003774void Parser::ParseDeclaratorInternal(Declarator &D,
3775 DirectDeclParseFunction DirectDeclParser) {
Douglas Gregor91a28862009-08-26 14:27:30 +00003776 if (Diags.hasAllExtensionsSilenced())
3777 D.setExtension();
Douglas Gregor2ccccb32010-08-23 18:23:48 +00003778
Sebastian Redlf30208a2009-01-24 21:16:55 +00003779 // C++ member pointers start with a '::' or a nested-name.
3780 // Member pointers get special handling, since there's no place for the
3781 // scope spec in the generic path below.
David Blaikie4e4d0842012-03-11 07:00:24 +00003782 if (getLangOpts().CPlusPlus &&
Chris Lattnerf919bfe2009-03-24 17:04:48 +00003783 (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) ||
3784 Tok.is(tok::annot_cxxscope))) {
Douglas Gregorefaa93a2011-11-07 17:33:42 +00003785 bool EnteringContext = D.getContext() == Declarator::FileContext ||
3786 D.getContext() == Declarator::MemberContext;
Sebastian Redlf30208a2009-01-24 21:16:55 +00003787 CXXScopeSpec SS;
Douglas Gregorefaa93a2011-11-07 17:33:42 +00003788 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), EnteringContext);
John McCall9ba61662010-02-26 08:45:28 +00003789
Jeffrey Yasskinedc28772010-04-07 23:29:58 +00003790 if (SS.isNotEmpty()) {
Mike Stump1eb44332009-09-09 15:08:12 +00003791 if (Tok.isNot(tok::star)) {
Sebastian Redlf30208a2009-01-24 21:16:55 +00003792 // The scope spec really belongs to the direct-declarator.
3793 D.getCXXScopeSpec() = SS;
3794 if (DirectDeclParser)
3795 (this->*DirectDeclParser)(D);
3796 return;
3797 }
3798
3799 SourceLocation Loc = ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00003800 D.SetRangeEnd(Loc);
John McCall0b7e6782011-03-24 11:26:52 +00003801 DeclSpec DS(AttrFactory);
Sebastian Redlf30208a2009-01-24 21:16:55 +00003802 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00003803 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00003804
3805 // Recurse to parse whatever is left.
3806 ParseDeclaratorInternal(D, DirectDeclParser);
3807
3808 // Sema will have to catch (syntactically invalid) pointers into global
3809 // scope. It has to catch pointers into namespace scope anyway.
3810 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
John McCall0b7e6782011-03-24 11:26:52 +00003811 Loc),
3812 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003813 /* Don't replace range end. */SourceLocation());
Sebastian Redlf30208a2009-01-24 21:16:55 +00003814 return;
3815 }
3816 }
3817
3818 tok::TokenKind Kind = Tok.getKind();
Steve Naroff5618bd42008-08-27 16:04:49 +00003819 // Not a pointer, C++ reference, or block.
Richard Smith9988f282012-03-29 01:16:42 +00003820 if (!isPtrOperatorToken(Kind, getLangOpts())) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003821 if (DirectDeclParser)
3822 (this->*DirectDeclParser)(D);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003823 return;
3824 }
Sebastian Redlf30208a2009-01-24 21:16:55 +00003825
Sebastian Redl05532f22009-03-15 22:02:01 +00003826 // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference,
3827 // '&&' -> rvalue reference
Sebastian Redl743de1f2009-03-23 00:00:23 +00003828 SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&.
Sebastian Redlab197ba2009-02-09 18:23:29 +00003829 D.SetRangeEnd(Loc);
Reid Spencer5f016e22007-07-11 17:01:13 +00003830
Chris Lattner9af55002009-03-27 04:18:06 +00003831 if (Kind == tok::star || Kind == tok::caret) {
Chris Lattner76549142008-02-21 01:32:26 +00003832 // Is a pointer.
John McCall0b7e6782011-03-24 11:26:52 +00003833 DeclSpec DS(AttrFactory);
Sebastian Redlf30208a2009-01-24 21:16:55 +00003834
Richard Smith6ee326a2012-04-10 01:32:12 +00003835 // FIXME: GNU attributes are not allowed here in a new-type-id.
Reid Spencer5f016e22007-07-11 17:01:13 +00003836 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00003837 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00003838
Reid Spencer5f016e22007-07-11 17:01:13 +00003839 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003840 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroff5618bd42008-08-27 16:04:49 +00003841 if (Kind == tok::star)
3842 // Remember that we parsed a pointer type, and remember the type-quals.
3843 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
Chandler Carruthd067c072011-02-23 18:51:59 +00003844 DS.getConstSpecLoc(),
3845 DS.getVolatileSpecLoc(),
John McCall0b7e6782011-03-24 11:26:52 +00003846 DS.getRestrictSpecLoc()),
3847 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003848 SourceLocation());
Steve Naroff5618bd42008-08-27 16:04:49 +00003849 else
3850 // Remember that we parsed a Block type, and remember the type-quals.
Mike Stump1eb44332009-09-09 15:08:12 +00003851 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
John McCall0b7e6782011-03-24 11:26:52 +00003852 Loc),
3853 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003854 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00003855 } else {
3856 // Is a reference
John McCall0b7e6782011-03-24 11:26:52 +00003857 DeclSpec DS(AttrFactory);
Reid Spencer5f016e22007-07-11 17:01:13 +00003858
Sebastian Redl743de1f2009-03-23 00:00:23 +00003859 // Complain about rvalue references in C++03, but then go on and build
3860 // the declarator.
Richard Smith7fe62082011-10-15 05:09:34 +00003861 if (Kind == tok::ampamp)
David Blaikie4e4d0842012-03-11 07:00:24 +00003862 Diag(Loc, getLangOpts().CPlusPlus0x ?
Richard Smith7fe62082011-10-15 05:09:34 +00003863 diag::warn_cxx98_compat_rvalue_reference :
3864 diag::ext_rvalue_reference);
Sebastian Redl743de1f2009-03-23 00:00:23 +00003865
Richard Smith6ee326a2012-04-10 01:32:12 +00003866 // GNU-style and C++11 attributes are allowed here, as is restrict.
3867 ParseTypeQualifierListOpt(DS);
3868 D.ExtendWithDeclSpec(DS);
3869
Reid Spencer5f016e22007-07-11 17:01:13 +00003870 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
3871 // cv-qualifiers are introduced through the use of a typedef or of a
3872 // template type argument, in which case the cv-qualifiers are ignored.
Reid Spencer5f016e22007-07-11 17:01:13 +00003873 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
3874 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
3875 Diag(DS.getConstSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00003876 diag::err_invalid_reference_qualifier_application) << "const";
Reid Spencer5f016e22007-07-11 17:01:13 +00003877 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
3878 Diag(DS.getVolatileSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00003879 diag::err_invalid_reference_qualifier_application) << "volatile";
Reid Spencer5f016e22007-07-11 17:01:13 +00003880 }
3881
3882 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003883 ParseDeclaratorInternal(D, DirectDeclParser);
Reid Spencer5f016e22007-07-11 17:01:13 +00003884
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00003885 if (D.getNumTypeObjects() > 0) {
3886 // C++ [dcl.ref]p4: There shall be no references to references.
3887 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
3888 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattnerda83bac2008-11-19 07:37:42 +00003889 if (const IdentifierInfo *II = D.getIdentifier())
3890 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
3891 << II;
3892 else
3893 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
3894 << "type name";
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00003895
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003896 // Once we've complained about the reference-to-reference, we
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00003897 // can go ahead and build the (technically ill-formed)
3898 // declarator: reference collapsing will take care of it.
3899 }
3900 }
3901
Reid Spencer5f016e22007-07-11 17:01:13 +00003902 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner76549142008-02-21 01:32:26 +00003903 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
Sebastian Redl05532f22009-03-15 22:02:01 +00003904 Kind == tok::amp),
John McCall0b7e6782011-03-24 11:26:52 +00003905 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003906 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00003907 }
3908}
3909
Richard Smith9988f282012-03-29 01:16:42 +00003910static void diagnoseMisplacedEllipsis(Parser &P, Declarator &D,
3911 SourceLocation EllipsisLoc) {
3912 if (EllipsisLoc.isValid()) {
3913 FixItHint Insertion;
3914 if (!D.getEllipsisLoc().isValid()) {
3915 Insertion = FixItHint::CreateInsertion(D.getIdentifierLoc(), "...");
3916 D.setEllipsisLoc(EllipsisLoc);
3917 }
3918 P.Diag(EllipsisLoc, diag::err_misplaced_ellipsis_in_declaration)
3919 << FixItHint::CreateRemoval(EllipsisLoc) << Insertion << !D.hasName();
3920 }
3921}
3922
Reid Spencer5f016e22007-07-11 17:01:13 +00003923/// ParseDirectDeclarator
3924/// direct-declarator: [C99 6.7.5]
Douglas Gregor42a552f2008-11-05 20:51:48 +00003925/// [C99] identifier
Reid Spencer5f016e22007-07-11 17:01:13 +00003926/// '(' declarator ')'
3927/// [GNU] '(' attributes declarator ')'
3928/// [C90] direct-declarator '[' constant-expression[opt] ']'
3929/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
3930/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
3931/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
3932/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Richard Smith6ee326a2012-04-10 01:32:12 +00003933/// [C++11] direct-declarator '[' constant-expression[opt] ']'
3934/// attribute-specifier-seq[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00003935/// direct-declarator '(' parameter-type-list ')'
3936/// direct-declarator '(' identifier-list[opt] ')'
3937/// [GNU] direct-declarator '(' parameter-forward-declarations
3938/// parameter-type-list[opt] ')'
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003939/// [C++] direct-declarator '(' parameter-declaration-clause ')'
3940/// cv-qualifier-seq[opt] exception-specification[opt]
Richard Smith6ee326a2012-04-10 01:32:12 +00003941/// [C++11] direct-declarator '(' parameter-declaration-clause ')'
3942/// attribute-specifier-seq[opt] cv-qualifier-seq[opt]
3943/// ref-qualifier[opt] exception-specification[opt]
Douglas Gregorb48fe382008-10-31 09:07:45 +00003944/// [C++] declarator-id
Richard Smith6ee326a2012-04-10 01:32:12 +00003945/// [C++11] declarator-id attribute-specifier-seq[opt]
Douglas Gregor42a552f2008-11-05 20:51:48 +00003946///
3947/// declarator-id: [C++ 8]
Douglas Gregora8bc8c92010-12-23 22:44:42 +00003948/// '...'[opt] id-expression
Douglas Gregor42a552f2008-11-05 20:51:48 +00003949/// '::'[opt] nested-name-specifier[opt] type-name
3950///
3951/// id-expression: [C++ 5.1]
3952/// unqualified-id
Douglas Gregordb422df2009-09-25 21:45:23 +00003953/// qualified-id
Douglas Gregor42a552f2008-11-05 20:51:48 +00003954///
3955/// unqualified-id: [C++ 5.1]
Mike Stump1eb44332009-09-09 15:08:12 +00003956/// identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003957/// operator-function-id
Douglas Gregordb422df2009-09-25 21:45:23 +00003958/// conversion-function-id
Mike Stump1eb44332009-09-09 15:08:12 +00003959/// '~' class-name
Douglas Gregor39a8de12009-02-25 19:37:18 +00003960/// template-id
Argyrios Kyrtzidisc7ed9c62008-11-07 22:02:30 +00003961///
Richard Smith5d8388c2012-03-27 01:42:32 +00003962/// Note, any additional constructs added here may need corresponding changes
3963/// in isConstructorDeclarator.
Reid Spencer5f016e22007-07-11 17:01:13 +00003964void Parser::ParseDirectDeclarator(Declarator &D) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00003965 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003966
David Blaikie4e4d0842012-03-11 07:00:24 +00003967 if (getLangOpts().CPlusPlus && D.mayHaveIdentifier()) {
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003968 // ParseDeclaratorInternal might already have parsed the scope.
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00003969 if (D.getCXXScopeSpec().isEmpty()) {
Douglas Gregorefaa93a2011-11-07 17:33:42 +00003970 bool EnteringContext = D.getContext() == Declarator::FileContext ||
3971 D.getContext() == Declarator::MemberContext;
3972 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec(), ParsedType(),
3973 EnteringContext);
John McCall9ba61662010-02-26 08:45:28 +00003974 }
3975
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00003976 if (D.getCXXScopeSpec().isValid()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00003977 if (Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
John McCalle7e278b2009-12-11 20:04:54 +00003978 // Change the declaration context for name lookup, until this function
3979 // is exited (and the declarator has been parsed).
3980 DeclScopeObj.EnterDeclaratorScope();
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00003981 }
3982
Douglas Gregora8bc8c92010-12-23 22:44:42 +00003983 // C++0x [dcl.fct]p14:
3984 // There is a syntactic ambiguity when an ellipsis occurs at the end
3985 // of a parameter-declaration-clause without a preceding comma. In
3986 // this case, the ellipsis is parsed as part of the
3987 // abstract-declarator if the type of the parameter names a template
3988 // parameter pack that has not been expanded; otherwise, it is parsed
3989 // as part of the parameter-declaration-clause.
Richard Smith9988f282012-03-29 01:16:42 +00003990 if (Tok.is(tok::ellipsis) && D.getCXXScopeSpec().isEmpty() &&
Douglas Gregora8bc8c92010-12-23 22:44:42 +00003991 !((D.getContext() == Declarator::PrototypeContext ||
3992 D.getContext() == Declarator::BlockLiteralContext) &&
Douglas Gregora8bc8c92010-12-23 22:44:42 +00003993 NextToken().is(tok::r_paren) &&
Richard Smith9988f282012-03-29 01:16:42 +00003994 !Actions.containsUnexpandedParameterPacks(D))) {
3995 SourceLocation EllipsisLoc = ConsumeToken();
3996 if (isPtrOperatorToken(Tok.getKind(), getLangOpts())) {
3997 // The ellipsis was put in the wrong place. Recover, and explain to
3998 // the user what they should have done.
3999 ParseDeclarator(D);
4000 diagnoseMisplacedEllipsis(*this, D, EllipsisLoc);
4001 return;
4002 } else
4003 D.setEllipsisLoc(EllipsisLoc);
4004
4005 // The ellipsis can't be followed by a parenthesized declarator. We
4006 // check for that in ParseParenDeclarator, after we have disambiguated
4007 // the l_paren token.
4008 }
4009
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004010 if (Tok.is(tok::identifier) || Tok.is(tok::kw_operator) ||
4011 Tok.is(tok::annot_template_id) || Tok.is(tok::tilde)) {
4012 // We found something that indicates the start of an unqualified-id.
4013 // Parse that unqualified-id.
John McCallba9d8532010-04-13 06:39:49 +00004014 bool AllowConstructorName;
4015 if (D.getDeclSpec().hasTypeSpecifier())
4016 AllowConstructorName = false;
4017 else if (D.getCXXScopeSpec().isSet())
4018 AllowConstructorName =
4019 (D.getContext() == Declarator::FileContext ||
4020 (D.getContext() == Declarator::MemberContext &&
4021 D.getDeclSpec().isFriendSpecified()));
4022 else
4023 AllowConstructorName = (D.getContext() == Declarator::MemberContext);
4024
Abramo Bagnarae4b92762012-01-27 09:46:47 +00004025 SourceLocation TemplateKWLoc;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004026 if (ParseUnqualifiedId(D.getCXXScopeSpec(),
4027 /*EnteringContext=*/true,
4028 /*AllowDestructorName=*/true,
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004029 AllowConstructorName,
John McCallb3d87482010-08-24 05:47:05 +00004030 ParsedType(),
Abramo Bagnarae4b92762012-01-27 09:46:47 +00004031 TemplateKWLoc,
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00004032 D.getName()) ||
4033 // Once we're past the identifier, if the scope was bad, mark the
4034 // whole declarator bad.
4035 D.getCXXScopeSpec().isInvalid()) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00004036 D.SetIdentifier(0, Tok.getLocation());
4037 D.setInvalidType(true);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004038 } else {
4039 // Parsed the unqualified-id; update range information and move along.
4040 if (D.getSourceRange().getBegin().isInvalid())
4041 D.SetRangeBegin(D.getName().getSourceRange().getBegin());
4042 D.SetRangeEnd(D.getName().getSourceRange().getEnd());
Douglas Gregor2f1bc522008-11-07 20:08:42 +00004043 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004044 goto PastIdentifier;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00004045 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004046 } else if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
David Blaikie4e4d0842012-03-11 07:00:24 +00004047 assert(!getLangOpts().CPlusPlus &&
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00004048 "There's a C++-specific check for tok::identifier above");
4049 assert(Tok.getIdentifierInfo() && "Not an identifier?");
4050 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
4051 ConsumeToken();
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004052 goto PastIdentifier;
4053 }
Richard Smith9988f282012-03-29 01:16:42 +00004054
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004055 if (Tok.is(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00004056 // direct-declarator: '(' declarator ')'
4057 // direct-declarator: '(' attributes declarator ')'
4058 // Example: 'char (*X)' or 'int (*XX)(void)'
4059 ParseParenDeclarator(D);
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004060
4061 // If the declarator was parenthesized, we entered the declarator
4062 // scope when parsing the parenthesized declarator, then exited
4063 // the scope already. Re-enter the scope, if we need to.
4064 if (D.getCXXScopeSpec().isSet()) {
Fariborz Jahanian46877cd2010-08-17 23:50:37 +00004065 // If there was an error parsing parenthesized declarator, declarator
Richard Smith9988f282012-03-29 01:16:42 +00004066 // scope may have been entered before. Don't do it again.
Fariborz Jahanian46877cd2010-08-17 23:50:37 +00004067 if (!D.isInvalidType() &&
4068 Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004069 // Change the declaration context for name lookup, until this function
4070 // is exited (and the declarator has been parsed).
Fariborz Jahanian46877cd2010-08-17 23:50:37 +00004071 DeclScopeObj.EnterDeclaratorScope();
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004072 }
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00004073 } else if (D.mayOmitIdentifier()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00004074 // This could be something simple like "int" (in which case the declarator
4075 // portion is empty), if an abstract-declarator is allowed.
4076 D.SetIdentifier(0, Tok.getLocation());
4077 } else {
Douglas Gregore950d4b2009-03-06 23:28:18 +00004078 if (D.getContext() == Declarator::MemberContext)
4079 Diag(Tok, diag::err_expected_member_name_or_semi)
4080 << D.getDeclSpec().getSourceRange();
David Blaikie4e4d0842012-03-11 07:00:24 +00004081 else if (getLangOpts().CPlusPlus)
4082 Diag(Tok, diag::err_expected_unqualified_id) << getLangOpts().CPlusPlus;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00004083 else
Chris Lattner1ab3b962008-11-18 07:48:38 +00004084 Diag(Tok, diag::err_expected_ident_lparen);
Reid Spencer5f016e22007-07-11 17:01:13 +00004085 D.SetIdentifier(0, Tok.getLocation());
Chris Lattner1f6f54b2008-11-11 06:13:16 +00004086 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00004087 }
Mike Stump1eb44332009-09-09 15:08:12 +00004088
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00004089 PastIdentifier:
Reid Spencer5f016e22007-07-11 17:01:13 +00004090 assert(D.isPastIdentifier() &&
4091 "Haven't past the location of the identifier yet?");
Mike Stump1eb44332009-09-09 15:08:12 +00004092
Richard Smith6ee326a2012-04-10 01:32:12 +00004093 // Don't parse attributes unless we have parsed an unparenthesized name.
4094 if (D.hasName() && !D.getNumTypeObjects())
John McCall7f040a92010-12-24 02:08:15 +00004095 MaybeParseCXX0XAttributes(D);
Sean Huntbbd37c62009-11-21 08:43:09 +00004096
Reid Spencer5f016e22007-07-11 17:01:13 +00004097 while (1) {
Chris Lattner04d66662007-10-09 17:33:22 +00004098 if (Tok.is(tok::l_paren)) {
David Blaikie42d6d0c2011-12-04 05:04:18 +00004099 // Enter function-declaration scope, limiting any declarators to the
4100 // function prototype scope, including parameter declarators.
4101 ParseScope PrototypeScope(this,
4102 Scope::FunctionPrototypeScope|Scope::DeclScope);
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00004103 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
4104 // In such a case, check if we actually have a function declarator; if it
4105 // is not, the declarator has been fully parsed.
David Blaikie4e4d0842012-03-11 07:00:24 +00004106 if (getLangOpts().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
Chris Lattner7399ee02008-10-20 02:05:46 +00004107 // When not in file scope, warn for ambiguous function declarators, just
4108 // in case the author intended it as a variable definition.
4109 bool warnIfAmbiguous = D.getContext() != Declarator::FileContext;
4110 if (!isCXXFunctionDeclarator(warnIfAmbiguous))
4111 break;
4112 }
John McCall0b7e6782011-03-24 11:26:52 +00004113 ParsedAttributes attrs(AttrFactory);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004114 BalancedDelimiterTracker T(*this, tok::l_paren);
4115 T.consumeOpen();
4116 ParseFunctionDeclarator(D, attrs, T);
David Blaikie42d6d0c2011-12-04 05:04:18 +00004117 PrototypeScope.Exit();
Chris Lattner04d66662007-10-09 17:33:22 +00004118 } else if (Tok.is(tok::l_square)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00004119 ParseBracketDeclarator(D);
4120 } else {
4121 break;
4122 }
4123 }
David Blaikie42d6d0c2011-12-04 05:04:18 +00004124}
Reid Spencer5f016e22007-07-11 17:01:13 +00004125
Chris Lattneref4715c2008-04-06 05:45:57 +00004126/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
4127/// only called before the identifier, so these are most likely just grouping
Mike Stump1eb44332009-09-09 15:08:12 +00004128/// parens for precedence. If we find that these are actually function
Chris Lattneref4715c2008-04-06 05:45:57 +00004129/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
4130///
4131/// direct-declarator:
4132/// '(' declarator ')'
4133/// [GNU] '(' attributes declarator ')'
Chris Lattner7399ee02008-10-20 02:05:46 +00004134/// direct-declarator '(' parameter-type-list ')'
4135/// direct-declarator '(' identifier-list[opt] ')'
4136/// [GNU] direct-declarator '(' parameter-forward-declarations
4137/// parameter-type-list[opt] ')'
Chris Lattneref4715c2008-04-06 05:45:57 +00004138///
4139void Parser::ParseParenDeclarator(Declarator &D) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004140 BalancedDelimiterTracker T(*this, tok::l_paren);
4141 T.consumeOpen();
4142
Chris Lattneref4715c2008-04-06 05:45:57 +00004143 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
Mike Stump1eb44332009-09-09 15:08:12 +00004144
Chris Lattner7399ee02008-10-20 02:05:46 +00004145 // Eat any attributes before we look at whether this is a grouping or function
4146 // declarator paren. If this is a grouping paren, the attribute applies to
4147 // the type being built up, for example:
4148 // int (__attribute__(()) *x)(long y)
4149 // If this ends up not being a grouping paren, the attribute applies to the
4150 // first argument, for example:
4151 // int (__attribute__(()) int x)
4152 // In either case, we need to eat any attributes to be able to determine what
4153 // sort of paren this is.
4154 //
John McCall0b7e6782011-03-24 11:26:52 +00004155 ParsedAttributes attrs(AttrFactory);
Chris Lattner7399ee02008-10-20 02:05:46 +00004156 bool RequiresArg = false;
4157 if (Tok.is(tok::kw___attribute)) {
John McCall7f040a92010-12-24 02:08:15 +00004158 ParseGNUAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00004159
Chris Lattner7399ee02008-10-20 02:05:46 +00004160 // We require that the argument list (if this is a non-grouping paren) be
4161 // present even if the attribute list was empty.
4162 RequiresArg = true;
4163 }
Steve Naroff239f0732008-12-25 14:16:32 +00004164 // Eat any Microsoft extensions.
Eli Friedman290eeb02009-06-08 23:27:34 +00004165 if (Tok.is(tok::kw___cdecl) || Tok.is(tok::kw___stdcall) ||
Douglas Gregorf813a2c2010-05-18 16:57:00 +00004166 Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___fastcall) ||
Francois Pichet3bd9aa42011-08-18 09:59:55 +00004167 Tok.is(tok::kw___w64) || Tok.is(tok::kw___ptr64) ||
Francois Pichet58fd97a2011-08-25 00:36:46 +00004168 Tok.is(tok::kw___ptr32) || Tok.is(tok::kw___unaligned)) {
John McCall7f040a92010-12-24 02:08:15 +00004169 ParseMicrosoftTypeAttributes(attrs);
Eli Friedman290eeb02009-06-08 23:27:34 +00004170 }
Dawn Perchik52fc3142010-09-03 01:29:35 +00004171 // Eat any Borland extensions.
Ted Kremenek8113ecf2010-11-10 05:59:39 +00004172 if (Tok.is(tok::kw___pascal))
John McCall7f040a92010-12-24 02:08:15 +00004173 ParseBorlandTypeAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00004174
Chris Lattneref4715c2008-04-06 05:45:57 +00004175 // If we haven't past the identifier yet (or where the identifier would be
4176 // stored, if this is an abstract declarator), then this is probably just
4177 // grouping parens. However, if this could be an abstract-declarator, then
4178 // this could also be the start of function arguments (consider 'void()').
4179 bool isGrouping;
Mike Stump1eb44332009-09-09 15:08:12 +00004180
Chris Lattneref4715c2008-04-06 05:45:57 +00004181 if (!D.mayOmitIdentifier()) {
4182 // If this can't be an abstract-declarator, this *must* be a grouping
4183 // paren, because we haven't seen the identifier yet.
4184 isGrouping = true;
4185 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Richard Smith22592862012-03-27 23:05:05 +00004186 (getLangOpts().CPlusPlus && Tok.is(tok::ellipsis) &&
4187 NextToken().is(tok::r_paren)) || // C++ int(...)
Richard Smith6ce48a72012-04-11 04:01:28 +00004188 isDeclarationSpecifier() || // 'int(int)' is a function.
4189 isCXX11AttributeSpecifier()) { // 'int([[]]int)' is a function.
Chris Lattneref4715c2008-04-06 05:45:57 +00004190 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
4191 // considered to be a type, not a K&R identifier-list.
4192 isGrouping = false;
4193 } else {
4194 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
4195 isGrouping = true;
4196 }
Mike Stump1eb44332009-09-09 15:08:12 +00004197
Chris Lattneref4715c2008-04-06 05:45:57 +00004198 // If this is a grouping paren, handle:
4199 // direct-declarator: '(' declarator ')'
4200 // direct-declarator: '(' attributes declarator ')'
4201 if (isGrouping) {
Richard Smith9988f282012-03-29 01:16:42 +00004202 SourceLocation EllipsisLoc = D.getEllipsisLoc();
4203 D.setEllipsisLoc(SourceLocation());
4204
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00004205 bool hadGroupingParens = D.hasGroupingParens();
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00004206 D.setGroupingParens(true);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00004207 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattneref4715c2008-04-06 05:45:57 +00004208 // Match the ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004209 T.consumeClose();
4210 D.AddTypeInfo(DeclaratorChunk::getParen(T.getOpenLocation(),
4211 T.getCloseLocation()),
4212 attrs, T.getCloseLocation());
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00004213
4214 D.setGroupingParens(hadGroupingParens);
Richard Smith9988f282012-03-29 01:16:42 +00004215
4216 // An ellipsis cannot be placed outside parentheses.
4217 if (EllipsisLoc.isValid())
4218 diagnoseMisplacedEllipsis(*this, D, EllipsisLoc);
4219
Chris Lattneref4715c2008-04-06 05:45:57 +00004220 return;
4221 }
Mike Stump1eb44332009-09-09 15:08:12 +00004222
Chris Lattneref4715c2008-04-06 05:45:57 +00004223 // Okay, if this wasn't a grouping paren, it must be the start of a function
4224 // argument list. Recognize that this declarator will never have an
Chris Lattner7399ee02008-10-20 02:05:46 +00004225 // identifier (and remember where it would have been), then call into
4226 // ParseFunctionDeclarator to handle of argument list.
Chris Lattneref4715c2008-04-06 05:45:57 +00004227 D.SetIdentifier(0, Tok.getLocation());
4228
David Blaikie42d6d0c2011-12-04 05:04:18 +00004229 // Enter function-declaration scope, limiting any declarators to the
4230 // function prototype scope, including parameter declarators.
4231 ParseScope PrototypeScope(this,
4232 Scope::FunctionPrototypeScope|Scope::DeclScope);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004233 ParseFunctionDeclarator(D, attrs, T, RequiresArg);
David Blaikie42d6d0c2011-12-04 05:04:18 +00004234 PrototypeScope.Exit();
Chris Lattneref4715c2008-04-06 05:45:57 +00004235}
4236
4237/// ParseFunctionDeclarator - We are after the identifier and have parsed the
4238/// declarator D up to a paren, which indicates that we are parsing function
4239/// arguments.
Reid Spencer5f016e22007-07-11 17:01:13 +00004240///
Richard Smith6ee326a2012-04-10 01:32:12 +00004241/// If FirstArgAttrs is non-null, then the caller parsed those arguments
4242/// immediately after the open paren - they should be considered to be the
4243/// first argument of a parameter.
Chris Lattner7399ee02008-10-20 02:05:46 +00004244///
Richard Smith6ee326a2012-04-10 01:32:12 +00004245/// If RequiresArg is true, then the first argument of the function is required
4246/// to be present and required to not be an identifier list.
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004247///
Richard Smith6ee326a2012-04-10 01:32:12 +00004248/// For C++, after the parameter-list, it also parses the cv-qualifier-seq[opt],
4249/// (C++11) ref-qualifier[opt], exception-specification[opt],
4250/// (C++11) attribute-specifier-seq[opt], and (C++11) trailing-return-type[opt].
4251///
4252/// [C++11] exception-specification:
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004253/// dynamic-exception-specification
4254/// noexcept-specification
4255///
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004256void Parser::ParseFunctionDeclarator(Declarator &D,
Richard Smith6ee326a2012-04-10 01:32:12 +00004257 ParsedAttributes &FirstArgAttrs,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004258 BalancedDelimiterTracker &Tracker,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004259 bool RequiresArg) {
David Blaikie42d6d0c2011-12-04 05:04:18 +00004260 assert(getCurScope()->isFunctionPrototypeScope() &&
4261 "Should call from a Function scope");
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004262 // lparen is already consumed!
4263 assert(D.isPastIdentifier() && "Should not call before identifier!");
4264
4265 // This should be true when the function has typed arguments.
4266 // Otherwise, it is treated as a K&R-style function.
4267 bool HasProto = false;
4268 // Build up an array of information about the parsed arguments.
Chris Lattner5f9e2722011-07-23 10:55:15 +00004269 SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004270 // Remember where we see an ellipsis, if any.
4271 SourceLocation EllipsisLoc;
4272
4273 DeclSpec DS(AttrFactory);
4274 bool RefQualifierIsLValueRef = true;
4275 SourceLocation RefQualifierLoc;
Douglas Gregor43f51032011-10-19 06:04:55 +00004276 SourceLocation ConstQualifierLoc;
4277 SourceLocation VolatileQualifierLoc;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004278 ExceptionSpecificationType ESpecType = EST_None;
4279 SourceRange ESpecRange;
Chris Lattner5f9e2722011-07-23 10:55:15 +00004280 SmallVector<ParsedType, 2> DynamicExceptions;
4281 SmallVector<SourceRange, 2> DynamicExceptionRanges;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004282 ExprResult NoexceptExpr;
Richard Smith6ee326a2012-04-10 01:32:12 +00004283 ParsedAttributes FnAttrs(AttrFactory);
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004284 ParsedType TrailingReturnType;
Richard Smith6ee326a2012-04-10 01:32:12 +00004285
James Molloy16f1f712012-02-29 10:24:19 +00004286 Actions.ActOnStartFunctionDeclarator();
4287
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004288 SourceLocation EndLoc;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004289 if (isFunctionDeclaratorIdentifierList()) {
4290 if (RequiresArg)
4291 Diag(Tok, diag::err_argument_required_after_attribute);
4292
4293 ParseFunctionDeclaratorIdentifierList(D, ParamInfo);
4294
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004295 Tracker.consumeClose();
4296 EndLoc = Tracker.getCloseLocation();
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004297 } else {
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004298 if (Tok.isNot(tok::r_paren))
Richard Smith6ee326a2012-04-10 01:32:12 +00004299 ParseParameterDeclarationClause(D, FirstArgAttrs, ParamInfo, EllipsisLoc);
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004300 else if (RequiresArg)
4301 Diag(Tok, diag::err_argument_required_after_attribute);
4302
David Blaikie4e4d0842012-03-11 07:00:24 +00004303 HasProto = ParamInfo.size() || getLangOpts().CPlusPlus;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004304
4305 // If we have the closing ')', eat it.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004306 Tracker.consumeClose();
4307 EndLoc = Tracker.getCloseLocation();
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004308
David Blaikie4e4d0842012-03-11 07:00:24 +00004309 if (getLangOpts().CPlusPlus) {
Richard Smith6ee326a2012-04-10 01:32:12 +00004310 // FIXME: Accept these components in any order, and produce fixits to
4311 // correct the order if the user gets it wrong. Ideally we should deal
4312 // with the virt-specifier-seq and pure-specifier in the same way.
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004313
4314 // Parse cv-qualifier-seq[opt].
Richard Smith6ee326a2012-04-10 01:32:12 +00004315 ParseTypeQualifierListOpt(DS, false /*no attributes*/, false);
4316 if (!DS.getSourceRange().getEnd().isInvalid()) {
4317 EndLoc = DS.getSourceRange().getEnd();
4318 ConstQualifierLoc = DS.getConstSpecLoc();
4319 VolatileQualifierLoc = DS.getVolatileSpecLoc();
4320 }
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004321
4322 // Parse ref-qualifier[opt].
4323 if (Tok.is(tok::amp) || Tok.is(tok::ampamp)) {
David Blaikie4e4d0842012-03-11 07:00:24 +00004324 Diag(Tok, getLangOpts().CPlusPlus0x ?
Richard Smith7fe62082011-10-15 05:09:34 +00004325 diag::warn_cxx98_compat_ref_qualifier :
4326 diag::ext_ref_qualifier);
Richard Smith6ee326a2012-04-10 01:32:12 +00004327
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004328 RefQualifierIsLValueRef = Tok.is(tok::amp);
4329 RefQualifierLoc = ConsumeToken();
4330 EndLoc = RefQualifierLoc;
4331 }
4332
Douglas Gregorcefc3af2012-04-16 07:05:22 +00004333 // C++11 [expr.prim.general]p3:
4334 // If a declaration declares a member function or member function
4335 // template of a class X, the expression this is a prvalue of type
4336 // "pointer to cv-qualifier-seq X" between the optional cv-qualifer-seq
4337 // and the end of the function-definition, member-declarator, or
4338 // declarator.
4339 bool IsCXX11MemberFunction =
4340 getLangOpts().CPlusPlus0x &&
4341 (D.getContext() == Declarator::MemberContext ||
4342 (D.getContext() == Declarator::FileContext &&
4343 D.getCXXScopeSpec().isValid() &&
4344 Actions.CurContext->isRecord()));
4345 Sema::CXXThisScopeRAII ThisScope(Actions,
4346 dyn_cast<CXXRecordDecl>(Actions.CurContext),
4347 DS.getTypeQualifiers(),
4348 IsCXX11MemberFunction);
Richard Smitha058fd42012-05-02 22:22:32 +00004349
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004350 // Parse exception-specification[opt].
Richard Smitha058fd42012-05-02 22:22:32 +00004351 ESpecType = tryParseExceptionSpecification(ESpecRange,
Douglas Gregor74e2fc32012-04-16 18:27:27 +00004352 DynamicExceptions,
4353 DynamicExceptionRanges,
Richard Smitha058fd42012-05-02 22:22:32 +00004354 NoexceptExpr);
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004355 if (ESpecType != EST_None)
4356 EndLoc = ESpecRange.getEnd();
4357
Richard Smith6ee326a2012-04-10 01:32:12 +00004358 // Parse attribute-specifier-seq[opt]. Per DR 979 and DR 1297, this goes
4359 // after the exception-specification.
4360 MaybeParseCXX0XAttributes(FnAttrs);
4361
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004362 // Parse trailing-return-type[opt].
David Blaikie4e4d0842012-03-11 07:00:24 +00004363 if (getLangOpts().CPlusPlus0x && Tok.is(tok::arrow)) {
Richard Smith7fe62082011-10-15 05:09:34 +00004364 Diag(Tok, diag::warn_cxx98_compat_trailing_return_type);
Douglas Gregorae7902c2011-08-04 15:30:47 +00004365 SourceRange Range;
4366 TrailingReturnType = ParseTrailingReturnType(Range).get();
4367 if (Range.getEnd().isValid())
4368 EndLoc = Range.getEnd();
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004369 }
4370 }
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004371 }
4372
4373 // Remember that we parsed a function type, and remember the attributes.
4374 D.AddTypeInfo(DeclaratorChunk::getFunction(HasProto,
4375 /*isVariadic=*/EllipsisLoc.isValid(),
4376 EllipsisLoc,
4377 ParamInfo.data(), ParamInfo.size(),
4378 DS.getTypeQualifiers(),
4379 RefQualifierIsLValueRef,
Douglas Gregor43f51032011-10-19 06:04:55 +00004380 RefQualifierLoc, ConstQualifierLoc,
4381 VolatileQualifierLoc,
Douglas Gregor90ebed02011-07-13 21:47:47 +00004382 /*MutableLoc=*/SourceLocation(),
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004383 ESpecType, ESpecRange.getBegin(),
4384 DynamicExceptions.data(),
4385 DynamicExceptionRanges.data(),
4386 DynamicExceptions.size(),
4387 NoexceptExpr.isUsable() ?
4388 NoexceptExpr.get() : 0,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004389 Tracker.getOpenLocation(),
4390 EndLoc, D,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004391 TrailingReturnType),
Richard Smith6ee326a2012-04-10 01:32:12 +00004392 FnAttrs, EndLoc);
James Molloy16f1f712012-02-29 10:24:19 +00004393
4394 Actions.ActOnEndFunctionDeclarator();
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004395}
4396
4397/// isFunctionDeclaratorIdentifierList - This parameter list may have an
4398/// identifier list form for a K&R-style function: void foo(a,b,c)
4399///
4400/// Note that identifier-lists are only allowed for normal declarators, not for
4401/// abstract-declarators.
4402bool Parser::isFunctionDeclaratorIdentifierList() {
David Blaikie4e4d0842012-03-11 07:00:24 +00004403 return !getLangOpts().CPlusPlus
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004404 && Tok.is(tok::identifier)
4405 && !TryAltiVecVectorToken()
4406 // K&R identifier lists can't have typedefs as identifiers, per C99
4407 // 6.7.5.3p11.
4408 && (TryAnnotateTypeOrScopeToken() || !Tok.is(tok::annot_typename))
4409 // Identifier lists follow a really simple grammar: the identifiers can
4410 // be followed *only* by a ", identifier" or ")". However, K&R
4411 // identifier lists are really rare in the brave new modern world, and
4412 // it is very common for someone to typo a type in a non-K&R style
4413 // list. If we are presented with something like: "void foo(intptr x,
4414 // float y)", we don't want to start parsing the function declarator as
4415 // though it is a K&R style declarator just because intptr is an
4416 // invalid type.
4417 //
4418 // To handle this, we check to see if the token after the first
4419 // identifier is a "," or ")". Only then do we parse it as an
4420 // identifier list.
4421 && (NextToken().is(tok::comma) || NextToken().is(tok::r_paren));
4422}
4423
4424/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
4425/// we found a K&R-style identifier list instead of a typed parameter list.
4426///
4427/// After returning, ParamInfo will hold the parsed parameters.
4428///
4429/// identifier-list: [C99 6.7.5]
4430/// identifier
4431/// identifier-list ',' identifier
4432///
4433void Parser::ParseFunctionDeclaratorIdentifierList(
4434 Declarator &D,
Chris Lattner5f9e2722011-07-23 10:55:15 +00004435 SmallVector<DeclaratorChunk::ParamInfo, 16> &ParamInfo) {
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004436 // If there was no identifier specified for the declarator, either we are in
4437 // an abstract-declarator, or we are in a parameter declarator which was found
4438 // to be abstract. In abstract-declarators, identifier lists are not valid:
4439 // diagnose this.
4440 if (!D.getIdentifier())
4441 Diag(Tok, diag::ext_ident_list_in_param);
4442
4443 // Maintain an efficient lookup of params we have seen so far.
4444 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
4445
4446 while (1) {
4447 // If this isn't an identifier, report the error and skip until ')'.
4448 if (Tok.isNot(tok::identifier)) {
4449 Diag(Tok, diag::err_expected_ident);
4450 SkipUntil(tok::r_paren, /*StopAtSemi=*/true, /*DontConsume=*/true);
4451 // Forget we parsed anything.
4452 ParamInfo.clear();
4453 return;
4454 }
4455
4456 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
4457
4458 // Reject 'typedef int y; int test(x, y)', but continue parsing.
4459 if (Actions.getTypeName(*ParmII, Tok.getLocation(), getCurScope()))
4460 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
4461
4462 // Verify that the argument identifier has not already been mentioned.
4463 if (!ParamsSoFar.insert(ParmII)) {
4464 Diag(Tok, diag::err_param_redefinition) << ParmII;
4465 } else {
4466 // Remember this identifier in ParamInfo.
4467 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
4468 Tok.getLocation(),
4469 0));
4470 }
4471
4472 // Eat the identifier.
4473 ConsumeToken();
4474
4475 // The list continues if we see a comma.
4476 if (Tok.isNot(tok::comma))
4477 break;
4478 ConsumeToken();
4479 }
4480}
4481
4482/// ParseParameterDeclarationClause - Parse a (possibly empty) parameter-list
4483/// after the opening parenthesis. This function will not parse a K&R-style
4484/// identifier list.
4485///
Richard Smith6ce48a72012-04-11 04:01:28 +00004486/// D is the declarator being parsed. If FirstArgAttrs is non-null, then the
4487/// caller parsed those arguments immediately after the open paren - they should
4488/// be considered to be part of the first parameter.
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004489///
4490/// After returning, ParamInfo will hold the parsed parameters. EllipsisLoc will
4491/// be the location of the ellipsis, if any was parsed.
4492///
Reid Spencer5f016e22007-07-11 17:01:13 +00004493/// parameter-type-list: [C99 6.7.5]
4494/// parameter-list
4495/// parameter-list ',' '...'
Douglas Gregored5d6512009-09-22 21:41:40 +00004496/// [C++] parameter-list '...'
Reid Spencer5f016e22007-07-11 17:01:13 +00004497///
4498/// parameter-list: [C99 6.7.5]
4499/// parameter-declaration
4500/// parameter-list ',' parameter-declaration
4501///
4502/// parameter-declaration: [C99 6.7.5]
4503/// declaration-specifiers declarator
Chris Lattner04421082008-04-08 04:40:51 +00004504/// [C++] declaration-specifiers declarator '=' assignment-expression
Sebastian Redl84407ba2012-03-14 15:54:00 +00004505/// [C++11] initializer-clause
Reid Spencer5f016e22007-07-11 17:01:13 +00004506/// [GNU] declaration-specifiers declarator attributes
Sebastian Redl50de12f2009-03-24 22:27:57 +00004507/// declaration-specifiers abstract-declarator[opt]
4508/// [C++] declaration-specifiers abstract-declarator[opt]
Chris Lattner8123a952008-04-10 02:22:51 +00004509/// '=' assignment-expression
Reid Spencer5f016e22007-07-11 17:01:13 +00004510/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
Richard Smith6ce48a72012-04-11 04:01:28 +00004511/// [C++11] attribute-specifier-seq parameter-declaration
Reid Spencer5f016e22007-07-11 17:01:13 +00004512///
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004513void Parser::ParseParameterDeclarationClause(
4514 Declarator &D,
Richard Smith6ce48a72012-04-11 04:01:28 +00004515 ParsedAttributes &FirstArgAttrs,
Chris Lattner5f9e2722011-07-23 10:55:15 +00004516 SmallVector<DeclaratorChunk::ParamInfo, 16> &ParamInfo,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004517 SourceLocation &EllipsisLoc) {
Mike Stump1eb44332009-09-09 15:08:12 +00004518
Chris Lattnerf97409f2008-04-06 06:57:35 +00004519 while (1) {
4520 if (Tok.is(tok::ellipsis)) {
Richard Smith6ce48a72012-04-11 04:01:28 +00004521 // FIXME: Issue a diagnostic if we parsed an attribute-specifier-seq
4522 // before deciding this was a parameter-declaration-clause.
Douglas Gregor965acbb2009-02-18 07:07:28 +00004523 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chris Lattnerf97409f2008-04-06 06:57:35 +00004524 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00004525 }
Mike Stump1eb44332009-09-09 15:08:12 +00004526
Chris Lattnerf97409f2008-04-06 06:57:35 +00004527 // Parse the declaration-specifiers.
John McCall54abf7d2009-11-04 02:18:39 +00004528 // Just use the ParsingDeclaration "scope" of the declarator.
John McCall0b7e6782011-03-24 11:26:52 +00004529 DeclSpec DS(AttrFactory);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004530
Richard Smith6ce48a72012-04-11 04:01:28 +00004531 // Parse any C++11 attributes.
4532 MaybeParseCXX0XAttributes(DS.getAttributes());
4533
John McCall7f040a92010-12-24 02:08:15 +00004534 // Skip any Microsoft attributes before a param.
David Blaikie4e4d0842012-03-11 07:00:24 +00004535 if (getLangOpts().MicrosoftExt && Tok.is(tok::l_square))
John McCall7f040a92010-12-24 02:08:15 +00004536 ParseMicrosoftAttributes(DS.getAttributes());
4537
4538 SourceLocation DSStart = Tok.getLocation();
Chris Lattner7399ee02008-10-20 02:05:46 +00004539
4540 // If the caller parsed attributes for the first argument, add them now.
John McCall7f040a92010-12-24 02:08:15 +00004541 // Take them so that we only apply the attributes to the first parameter.
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004542 // FIXME: If we can leave the attributes in the token stream somehow, we can
Richard Smith6ce48a72012-04-11 04:01:28 +00004543 // get rid of a parameter (FirstArgAttrs) and this statement. It might be
4544 // too much hassle.
4545 DS.takeAttributesFrom(FirstArgAttrs);
John McCall7f040a92010-12-24 02:08:15 +00004546
Chris Lattnere64c5492009-02-27 18:38:20 +00004547 ParseDeclarationSpecifiers(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00004548
Chris Lattnerf97409f2008-04-06 06:57:35 +00004549 // Parse the declarator. This is "PrototypeContext", because we must
4550 // accept either 'declarator' or 'abstract-declarator' here.
4551 Declarator ParmDecl(DS, Declarator::PrototypeContext);
4552 ParseDeclarator(ParmDecl);
4553
4554 // Parse GNU attributes, if present.
John McCall7f040a92010-12-24 02:08:15 +00004555 MaybeParseGNUAttributes(ParmDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00004556
Chris Lattnerf97409f2008-04-06 06:57:35 +00004557 // Remember this parsed parameter in ParamInfo.
4558 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
Mike Stump1eb44332009-09-09 15:08:12 +00004559
Douglas Gregor72b505b2008-12-16 21:30:33 +00004560 // DefArgToks is used when the parsing of default arguments needs
4561 // to be delayed.
4562 CachedTokens *DefArgToks = 0;
4563
Chris Lattnerf97409f2008-04-06 06:57:35 +00004564 // If no parameter was specified, verify that *something* was specified,
4565 // otherwise we have a missing type and identifier.
Chris Lattnere64c5492009-02-27 18:38:20 +00004566 if (DS.isEmpty() && ParmDecl.getIdentifier() == 0 &&
4567 ParmDecl.getNumTypeObjects() == 0) {
Chris Lattnerf97409f2008-04-06 06:57:35 +00004568 // Completely missing, emit error.
4569 Diag(DSStart, diag::err_missing_param);
4570 } else {
4571 // Otherwise, we have something. Add it and let semantic analysis try
4572 // to grok it and add the result to the ParamInfo we are building.
Mike Stump1eb44332009-09-09 15:08:12 +00004573
Chris Lattnerf97409f2008-04-06 06:57:35 +00004574 // Inform the actions module about the parameter declarator, so it gets
4575 // added to the current scope.
John McCalld226f652010-08-21 09:40:31 +00004576 Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl);
Chris Lattner04421082008-04-08 04:40:51 +00004577
4578 // Parse the default argument, if any. We parse the default
4579 // arguments in all dialects; the semantic analysis in
4580 // ActOnParamDefaultArgument will reject the default argument in
4581 // C.
4582 if (Tok.is(tok::equal)) {
Douglas Gregor61366e92008-12-24 00:01:03 +00004583 SourceLocation EqualLoc = Tok.getLocation();
4584
Chris Lattner04421082008-04-08 04:40:51 +00004585 // Parse the default argument
Douglas Gregor72b505b2008-12-16 21:30:33 +00004586 if (D.getContext() == Declarator::MemberContext) {
4587 // If we're inside a class definition, cache the tokens
4588 // corresponding to the default argument. We'll actually parse
4589 // them when we see the end of the class definition.
Douglas Gregor72b505b2008-12-16 21:30:33 +00004590 // FIXME: Can we use a smart pointer for Toks?
4591 DefArgToks = new CachedTokens;
4592
Mike Stump1eb44332009-09-09 15:08:12 +00004593 if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks,
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +00004594 /*StopAtSemi=*/true,
4595 /*ConsumeFinalToken=*/false)) {
Douglas Gregor72b505b2008-12-16 21:30:33 +00004596 delete DefArgToks;
4597 DefArgToks = 0;
Douglas Gregor61366e92008-12-24 00:01:03 +00004598 Actions.ActOnParamDefaultArgumentError(Param);
Argyrios Kyrtzidis2b602ad2010-08-06 09:47:24 +00004599 } else {
4600 // Mark the end of the default argument so that we know when to
4601 // stop when we parse it later on.
4602 Token DefArgEnd;
4603 DefArgEnd.startToken();
4604 DefArgEnd.setKind(tok::cxx_defaultarg_end);
4605 DefArgEnd.setLocation(Tok.getLocation());
4606 DefArgToks->push_back(DefArgEnd);
Mike Stump1eb44332009-09-09 15:08:12 +00004607 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc,
Anders Carlsson5e300d12009-06-12 16:51:40 +00004608 (*DefArgToks)[1].getLocation());
Argyrios Kyrtzidis2b602ad2010-08-06 09:47:24 +00004609 }
Chris Lattner04421082008-04-08 04:40:51 +00004610 } else {
Douglas Gregor72b505b2008-12-16 21:30:33 +00004611 // Consume the '='.
Douglas Gregor61366e92008-12-24 00:01:03 +00004612 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00004613
Douglas Gregorbe0f7bd2010-09-11 20:24:53 +00004614 // The argument isn't actually potentially evaluated unless it is
4615 // used.
4616 EnterExpressionEvaluationContext Eval(Actions,
Douglas Gregorccc1b5e2012-02-21 00:37:24 +00004617 Sema::PotentiallyEvaluatedIfUsed,
4618 Param);
Douglas Gregorbe0f7bd2010-09-11 20:24:53 +00004619
Sebastian Redl84407ba2012-03-14 15:54:00 +00004620 ExprResult DefArgResult;
Sebastian Redl3e280b52012-03-18 22:25:45 +00004621 if (getLangOpts().CPlusPlus0x && Tok.is(tok::l_brace)) {
4622 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
Sebastian Redl84407ba2012-03-14 15:54:00 +00004623 DefArgResult = ParseBraceInitializer();
Sebastian Redl3e280b52012-03-18 22:25:45 +00004624 } else
Sebastian Redl84407ba2012-03-14 15:54:00 +00004625 DefArgResult = ParseAssignmentExpression();
Douglas Gregor72b505b2008-12-16 21:30:33 +00004626 if (DefArgResult.isInvalid()) {
4627 Actions.ActOnParamDefaultArgumentError(Param);
4628 SkipUntil(tok::comma, tok::r_paren, true, true);
4629 } else {
4630 // Inform the actions module about the default argument
4631 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
John McCall9ae2f072010-08-23 23:25:46 +00004632 DefArgResult.take());
Douglas Gregor72b505b2008-12-16 21:30:33 +00004633 }
Chris Lattner04421082008-04-08 04:40:51 +00004634 }
4635 }
Mike Stump1eb44332009-09-09 15:08:12 +00004636
4637 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
4638 ParmDecl.getIdentifierLoc(), Param,
Douglas Gregor72b505b2008-12-16 21:30:33 +00004639 DefArgToks));
Chris Lattnerf97409f2008-04-06 06:57:35 +00004640 }
4641
4642 // If the next token is a comma, consume it and keep reading arguments.
Douglas Gregored5d6512009-09-22 21:41:40 +00004643 if (Tok.isNot(tok::comma)) {
4644 if (Tok.is(tok::ellipsis)) {
Douglas Gregored5d6512009-09-22 21:41:40 +00004645 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
4646
David Blaikie4e4d0842012-03-11 07:00:24 +00004647 if (!getLangOpts().CPlusPlus) {
Douglas Gregored5d6512009-09-22 21:41:40 +00004648 // We have ellipsis without a preceding ',', which is ill-formed
4649 // in C. Complain and provide the fix.
4650 Diag(EllipsisLoc, diag::err_missing_comma_before_ellipsis)
Douglas Gregor849b2432010-03-31 17:46:05 +00004651 << FixItHint::CreateInsertion(EllipsisLoc, ", ");
Douglas Gregored5d6512009-09-22 21:41:40 +00004652 }
4653 }
4654
4655 break;
4656 }
Mike Stump1eb44332009-09-09 15:08:12 +00004657
Chris Lattnerf97409f2008-04-06 06:57:35 +00004658 // Consume the comma.
4659 ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00004660 }
Mike Stump1eb44332009-09-09 15:08:12 +00004661
Chris Lattner66d28652008-04-06 06:34:08 +00004662}
Chris Lattneref4715c2008-04-06 05:45:57 +00004663
Reid Spencer5f016e22007-07-11 17:01:13 +00004664/// [C90] direct-declarator '[' constant-expression[opt] ']'
4665/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
4666/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
4667/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
4668/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Richard Smith6ee326a2012-04-10 01:32:12 +00004669/// [C++11] direct-declarator '[' constant-expression[opt] ']'
4670/// attribute-specifier-seq[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00004671void Parser::ParseBracketDeclarator(Declarator &D) {
Richard Smith6ee326a2012-04-10 01:32:12 +00004672 if (CheckProhibitedCXX11Attribute())
4673 return;
4674
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004675 BalancedDelimiterTracker T(*this, tok::l_square);
4676 T.consumeOpen();
Mike Stump1eb44332009-09-09 15:08:12 +00004677
Chris Lattner378c7e42008-12-18 07:27:21 +00004678 // C array syntax has many features, but by-far the most common is [] and [4].
4679 // This code does a fast path to handle some of the most obvious cases.
4680 if (Tok.getKind() == tok::r_square) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004681 T.consumeClose();
John McCall0b7e6782011-03-24 11:26:52 +00004682 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00004683 MaybeParseCXX0XAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00004684
Chris Lattner378c7e42008-12-18 07:27:21 +00004685 // Remember that we parsed the empty array type.
John McCall60d7b3a2010-08-24 06:29:42 +00004686 ExprResult NumElements;
John McCall0b7e6782011-03-24 11:26:52 +00004687 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004688 T.getOpenLocation(),
4689 T.getCloseLocation()),
4690 attrs, T.getCloseLocation());
Chris Lattner378c7e42008-12-18 07:27:21 +00004691 return;
4692 } else if (Tok.getKind() == tok::numeric_constant &&
4693 GetLookAheadToken(1).is(tok::r_square)) {
4694 // [4] is very common. Parse the numeric constant expression.
Richard Smith36f5cfe2012-03-09 08:00:36 +00004695 ExprResult ExprRes(Actions.ActOnNumericConstant(Tok, getCurScope()));
Chris Lattner378c7e42008-12-18 07:27:21 +00004696 ConsumeToken();
4697
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004698 T.consumeClose();
John McCall0b7e6782011-03-24 11:26:52 +00004699 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00004700 MaybeParseCXX0XAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00004701
Chris Lattner378c7e42008-12-18 07:27:21 +00004702 // Remember that we parsed a array type, and remember its features.
John McCall0b7e6782011-03-24 11:26:52 +00004703 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, 0,
John McCall7f040a92010-12-24 02:08:15 +00004704 ExprRes.release(),
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004705 T.getOpenLocation(),
4706 T.getCloseLocation()),
4707 attrs, T.getCloseLocation());
Chris Lattner378c7e42008-12-18 07:27:21 +00004708 return;
4709 }
Mike Stump1eb44332009-09-09 15:08:12 +00004710
Reid Spencer5f016e22007-07-11 17:01:13 +00004711 // If valid, this location is the position where we read the 'static' keyword.
4712 SourceLocation StaticLoc;
Chris Lattner04d66662007-10-09 17:33:22 +00004713 if (Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00004714 StaticLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00004715
Reid Spencer5f016e22007-07-11 17:01:13 +00004716 // If there is a type-qualifier-list, read it now.
Chris Lattnera1fcbad2008-12-18 06:50:14 +00004717 // Type qualifiers in an array subscript are a C99 feature.
John McCall0b7e6782011-03-24 11:26:52 +00004718 DeclSpec DS(AttrFactory);
Chris Lattner5a69d1c2008-12-18 07:02:59 +00004719 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Mike Stump1eb44332009-09-09 15:08:12 +00004720
Reid Spencer5f016e22007-07-11 17:01:13 +00004721 // If we haven't already read 'static', check to see if there is one after the
4722 // type-qualifier-list.
Chris Lattner04d66662007-10-09 17:33:22 +00004723 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00004724 StaticLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00004725
Reid Spencer5f016e22007-07-11 17:01:13 +00004726 // Handle "direct-declarator [ type-qual-list[opt] * ]".
4727 bool isStar = false;
John McCall60d7b3a2010-08-24 06:29:42 +00004728 ExprResult NumElements;
Mike Stump1eb44332009-09-09 15:08:12 +00004729
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00004730 // Handle the case where we have '[*]' as the array size. However, a leading
4731 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
4732 // the the token after the star is a ']'. Since stars in arrays are
4733 // infrequent, use of lookahead is not costly here.
4734 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattnera711dd02008-04-06 05:27:21 +00004735 ConsumeToken(); // Eat the '*'.
Reid Spencer5f016e22007-07-11 17:01:13 +00004736
Chris Lattnera1fcbad2008-12-18 06:50:14 +00004737 if (StaticLoc.isValid()) {
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00004738 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattnera1fcbad2008-12-18 06:50:14 +00004739 StaticLoc = SourceLocation(); // Drop the static.
4740 }
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00004741 isStar = true;
Chris Lattner04d66662007-10-09 17:33:22 +00004742 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner378c7e42008-12-18 07:27:21 +00004743 // Note, in C89, this production uses the constant-expr production instead
4744 // of assignment-expr. The only difference is that assignment-expr allows
4745 // things like '=' and '*='. Sema rejects these in C89 mode because they
4746 // are not i-c-e's, so we don't need to distinguish between the two here.
Mike Stump1eb44332009-09-09 15:08:12 +00004747
Douglas Gregore0762c92009-06-19 23:52:42 +00004748 // Parse the constant-expression or assignment-expression now (depending
4749 // on dialect).
David Blaikie4e4d0842012-03-11 07:00:24 +00004750 if (getLangOpts().CPlusPlus) {
Douglas Gregore0762c92009-06-19 23:52:42 +00004751 NumElements = ParseConstantExpression();
Eli Friedman71b8fb52012-01-21 01:01:51 +00004752 } else {
4753 EnterExpressionEvaluationContext Unevaluated(Actions,
4754 Sema::ConstantEvaluated);
Douglas Gregore0762c92009-06-19 23:52:42 +00004755 NumElements = ParseAssignmentExpression();
Eli Friedman71b8fb52012-01-21 01:01:51 +00004756 }
Reid Spencer5f016e22007-07-11 17:01:13 +00004757 }
Mike Stump1eb44332009-09-09 15:08:12 +00004758
Reid Spencer5f016e22007-07-11 17:01:13 +00004759 // If there was an error parsing the assignment-expression, recover.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00004760 if (NumElements.isInvalid()) {
Chris Lattner5cb10d32009-04-24 22:30:50 +00004761 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00004762 // If the expression was invalid, skip it.
4763 SkipUntil(tok::r_square);
4764 return;
4765 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00004766
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004767 T.consumeClose();
Sebastian Redlab197ba2009-02-09 18:23:29 +00004768
John McCall0b7e6782011-03-24 11:26:52 +00004769 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00004770 MaybeParseCXX0XAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00004771
Chris Lattner378c7e42008-12-18 07:27:21 +00004772 // Remember that we parsed a array type, and remember its features.
John McCall0b7e6782011-03-24 11:26:52 +00004773 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
Reid Spencer5f016e22007-07-11 17:01:13 +00004774 StaticLoc.isValid(), isStar,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00004775 NumElements.release(),
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004776 T.getOpenLocation(),
4777 T.getCloseLocation()),
4778 attrs, T.getCloseLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00004779}
4780
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004781/// [GNU] typeof-specifier:
4782/// typeof ( expressions )
4783/// typeof ( type-name )
4784/// [GNU/C++] typeof unary-expression
Steve Naroffd1861fd2007-07-31 12:34:36 +00004785///
4786void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner04d66662007-10-09 17:33:22 +00004787 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004788 Token OpTok = Tok;
Steve Naroffd1861fd2007-07-31 12:34:36 +00004789 SourceLocation StartLoc = ConsumeToken();
4790
John McCallcfb708c2010-01-13 20:03:27 +00004791 const bool hasParens = Tok.is(tok::l_paren);
4792
Eli Friedman71b8fb52012-01-21 01:01:51 +00004793 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated);
4794
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004795 bool isCastExpr;
John McCallb3d87482010-08-24 05:47:05 +00004796 ParsedType CastTy;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004797 SourceRange CastRange;
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00004798 ExprResult Operand = ParseExprAfterUnaryExprOrTypeTrait(OpTok, isCastExpr,
4799 CastTy, CastRange);
John McCallcfb708c2010-01-13 20:03:27 +00004800 if (hasParens)
4801 DS.setTypeofParensRange(CastRange);
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004802
4803 if (CastRange.getEnd().isInvalid())
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004804 // FIXME: Not accurate, the range gets one token more than it should.
4805 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004806 else
4807 DS.SetRangeEnd(CastRange.getEnd());
Mike Stump1eb44332009-09-09 15:08:12 +00004808
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004809 if (isCastExpr) {
4810 if (!CastTy) {
4811 DS.SetTypeSpecError();
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004812 return;
Douglas Gregor809070a2009-02-18 17:45:20 +00004813 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004814
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004815 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00004816 unsigned DiagID;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004817 // Check for duplicate type specifiers (e.g. "int typeof(int)").
4818 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00004819 DiagID, CastTy))
4820 Diag(StartLoc, DiagID) << PrevSpec;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004821 return;
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004822 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004823
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004824 // If we get here, the operand to the typeof was an expresion.
4825 if (Operand.isInvalid()) {
4826 DS.SetTypeSpecError();
Steve Naroff9dfa7b42007-08-02 02:53:48 +00004827 return;
Steve Naroffd1861fd2007-07-31 12:34:36 +00004828 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004829
Eli Friedman71b8fb52012-01-21 01:01:51 +00004830 // We might need to transform the operand if it is potentially evaluated.
4831 Operand = Actions.HandleExprEvaluationContextForTypeof(Operand.get());
4832 if (Operand.isInvalid()) {
4833 DS.SetTypeSpecError();
4834 return;
4835 }
4836
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004837 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00004838 unsigned DiagID;
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004839 // Check for duplicate type specifiers (e.g. "int typeof(int)").
4840 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
John McCallb3d87482010-08-24 05:47:05 +00004841 DiagID, Operand.get()))
John McCallfec54012009-08-03 20:12:06 +00004842 Diag(StartLoc, DiagID) << PrevSpec;
Steve Naroffd1861fd2007-07-31 12:34:36 +00004843}
Chris Lattner1b492422010-02-28 18:33:55 +00004844
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00004845/// [C11] atomic-specifier:
Eli Friedmanb001de72011-10-06 23:00:33 +00004846/// _Atomic ( type-name )
4847///
4848void Parser::ParseAtomicSpecifier(DeclSpec &DS) {
4849 assert(Tok.is(tok::kw__Atomic) && "Not an atomic specifier");
4850
4851 SourceLocation StartLoc = ConsumeToken();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004852 BalancedDelimiterTracker T(*this, tok::l_paren);
4853 if (T.expectAndConsume(diag::err_expected_lparen_after, "_Atomic")) {
Eli Friedmanb001de72011-10-06 23:00:33 +00004854 SkipUntil(tok::r_paren);
4855 return;
4856 }
4857
4858 TypeResult Result = ParseTypeName();
4859 if (Result.isInvalid()) {
4860 SkipUntil(tok::r_paren);
4861 return;
4862 }
4863
4864 // Match the ')'
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004865 T.consumeClose();
Eli Friedmanb001de72011-10-06 23:00:33 +00004866
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004867 if (T.getCloseLocation().isInvalid())
Eli Friedmanb001de72011-10-06 23:00:33 +00004868 return;
4869
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004870 DS.setTypeofParensRange(T.getRange());
4871 DS.SetRangeEnd(T.getCloseLocation());
Eli Friedmanb001de72011-10-06 23:00:33 +00004872
4873 const char *PrevSpec = 0;
4874 unsigned DiagID;
4875 if (DS.SetTypeSpecType(DeclSpec::TST_atomic, StartLoc, PrevSpec,
4876 DiagID, Result.release()))
4877 Diag(StartLoc, DiagID) << PrevSpec;
4878}
4879
Chris Lattner1b492422010-02-28 18:33:55 +00004880
4881/// TryAltiVecVectorTokenOutOfLine - Out of line body that should only be called
4882/// from TryAltiVecVectorToken.
4883bool Parser::TryAltiVecVectorTokenOutOfLine() {
4884 Token Next = NextToken();
4885 switch (Next.getKind()) {
4886 default: return false;
4887 case tok::kw_short:
4888 case tok::kw_long:
4889 case tok::kw_signed:
4890 case tok::kw_unsigned:
4891 case tok::kw_void:
4892 case tok::kw_char:
4893 case tok::kw_int:
4894 case tok::kw_float:
4895 case tok::kw_double:
4896 case tok::kw_bool:
4897 case tok::kw___pixel:
4898 Tok.setKind(tok::kw___vector);
4899 return true;
4900 case tok::identifier:
4901 if (Next.getIdentifierInfo() == Ident_pixel) {
4902 Tok.setKind(tok::kw___vector);
4903 return true;
4904 }
4905 return false;
4906 }
4907}
4908
4909bool Parser::TryAltiVecTokenOutOfLine(DeclSpec &DS, SourceLocation Loc,
4910 const char *&PrevSpec, unsigned &DiagID,
4911 bool &isInvalid) {
4912 if (Tok.getIdentifierInfo() == Ident_vector) {
4913 Token Next = NextToken();
4914 switch (Next.getKind()) {
4915 case tok::kw_short:
4916 case tok::kw_long:
4917 case tok::kw_signed:
4918 case tok::kw_unsigned:
4919 case tok::kw_void:
4920 case tok::kw_char:
4921 case tok::kw_int:
4922 case tok::kw_float:
4923 case tok::kw_double:
4924 case tok::kw_bool:
4925 case tok::kw___pixel:
4926 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
4927 return true;
4928 case tok::identifier:
4929 if (Next.getIdentifierInfo() == Ident_pixel) {
4930 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
4931 return true;
4932 }
4933 break;
4934 default:
4935 break;
4936 }
Douglas Gregora8f031f2010-06-16 15:28:57 +00004937 } else if ((Tok.getIdentifierInfo() == Ident_pixel) &&
Chris Lattner1b492422010-02-28 18:33:55 +00004938 DS.isTypeAltiVecVector()) {
4939 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
4940 return true;
4941 }
4942 return false;
4943}