blob: dcc96cb8614b3eabb18542cdf831a24402b28d4c [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 Smith827adaf2012-05-15 21:01:51 +00001710 if (DSC != DSC_type_specifier && DSC != DSC_trailing) {
1711 // Look ahead to the next token to try to figure out what this declaration
1712 // was supposed to be.
1713 switch (NextToken().getKind()) {
1714 case tok::comma:
1715 case tok::equal:
1716 case tok::kw_asm:
1717 case tok::l_brace:
1718 case tok::l_square:
1719 case tok::semi:
1720 // This looks like a variable declaration. The type is probably missing.
1721 // We're done parsing decl-specifiers.
1722 return false;
1723
1724 case tok::l_paren: {
1725 // static x(4); // 'x' is not a type
1726 // x(int n); // 'x' is not a type
1727 // x (*p)[]; // 'x' is a type
1728 //
1729 // Since we're in an error case (or the rare 'implicit int in C++' MS
1730 // extension), we can afford to perform a tentative parse to determine
1731 // which case we're in.
1732 TentativeParsingAction PA(*this);
1733 ConsumeToken();
1734 TPResult TPR = TryParseDeclarator(/*mayBeAbstract*/false);
1735 PA.Revert();
1736 if (TPR == TPResult::False())
1737 return false;
1738 // The identifier is followed by a parenthesized declarator.
1739 // It's supposed to be a type.
1740 break;
1741 }
1742
1743 default:
1744 // This is probably supposed to be a type. This includes cases like:
1745 // int f(itn);
1746 // struct S { unsinged : 4; };
1747 break;
1748 }
1749 }
1750
Douglas Gregora786fdb2009-10-13 23:27:22 +00001751 // This is almost certainly an invalid type name. Let the action emit a
1752 // diagnostic and attempt to recover.
John McCallb3d87482010-08-24 05:47:05 +00001753 ParsedType T;
Douglas Gregora786fdb2009-10-13 23:27:22 +00001754 if (Actions.DiagnoseUnknownTypeName(*Tok.getIdentifierInfo(), Loc,
Douglas Gregor23c94db2010-07-02 17:43:08 +00001755 getCurScope(), SS, T)) {
Douglas Gregora786fdb2009-10-13 23:27:22 +00001756 // The action emitted a diagnostic, so we don't have to.
1757 if (T) {
1758 // The action has suggested that the type T could be used. Set that as
1759 // the type in the declaration specifiers, consume the would-be type
1760 // name token, and we're done.
1761 const char *PrevSpec;
1762 unsigned DiagID;
John McCallb3d87482010-08-24 05:47:05 +00001763 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID, T);
Douglas Gregora786fdb2009-10-13 23:27:22 +00001764 DS.SetRangeEnd(Tok.getLocation());
1765 ConsumeToken();
1766
1767 // There may be other declaration specifiers after this.
1768 return true;
1769 }
1770
1771 // Fall through; the action had no suggestion for us.
1772 } else {
1773 // The action did not emit a diagnostic, so emit one now.
1774 SourceRange R;
1775 if (SS) R = SS->getRange();
1776 Diag(Loc, diag::err_unknown_typename) << Tok.getIdentifierInfo() << R;
1777 }
Mike Stump1eb44332009-09-09 15:08:12 +00001778
Douglas Gregora786fdb2009-10-13 23:27:22 +00001779 // Mark this as an error.
Richard Smith69730c12012-03-12 07:56:15 +00001780 DS.SetTypeSpecError();
Chris Lattnere40c2952009-04-14 21:34:55 +00001781 DS.SetRangeEnd(Tok.getLocation());
1782 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001783
Chris Lattnere40c2952009-04-14 21:34:55 +00001784 // TODO: Could inject an invalid typedef decl in an enclosing scope to
1785 // avoid rippling error messages on subsequent uses of the same type,
1786 // could be useful if #include was forgotten.
1787 return false;
1788}
1789
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001790/// \brief Determine the declaration specifier context from the declarator
1791/// context.
1792///
1793/// \param Context the declarator context, which is one of the
1794/// Declarator::TheContext enumerator values.
1795Parser::DeclSpecContext
1796Parser::getDeclSpecContextFromDeclaratorContext(unsigned Context) {
1797 if (Context == Declarator::MemberContext)
1798 return DSC_class;
1799 if (Context == Declarator::FileContext)
1800 return DSC_top_level;
Richard Smith6d96d3a2012-03-15 01:02:11 +00001801 if (Context == Declarator::TrailingReturnContext)
1802 return DSC_trailing;
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001803 return DSC_normal;
1804}
1805
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001806/// ParseAlignArgument - Parse the argument to an alignment-specifier.
1807///
1808/// FIXME: Simply returns an alignof() expression if the argument is a
1809/// type. Ideally, the type should be propagated directly into Sema.
1810///
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00001811/// [C11] type-id
1812/// [C11] constant-expression
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00001813/// [C++0x] type-id ...[opt]
1814/// [C++0x] assignment-expression ...[opt]
1815ExprResult Parser::ParseAlignArgument(SourceLocation Start,
1816 SourceLocation &EllipsisLoc) {
1817 ExprResult ER;
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001818 if (isTypeIdInParens()) {
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001819 SourceLocation TypeLoc = Tok.getLocation();
1820 ParsedType Ty = ParseTypeName().get();
1821 SourceRange TypeRange(Start, Tok.getLocation());
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00001822 ER = Actions.ActOnUnaryExprOrTypeTraitExpr(TypeLoc, UETT_AlignOf, true,
1823 Ty.getAsOpaquePtr(), TypeRange);
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001824 } else
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00001825 ER = ParseConstantExpression();
1826
David Blaikie4e4d0842012-03-11 07:00:24 +00001827 if (getLangOpts().CPlusPlus0x && Tok.is(tok::ellipsis))
Peter Collingbournefe9b2a82011-10-24 17:56:00 +00001828 EllipsisLoc = ConsumeToken();
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00001829
1830 return ER;
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001831}
1832
1833/// ParseAlignmentSpecifier - Parse an alignment-specifier, and add the
1834/// attribute to Attrs.
1835///
1836/// alignment-specifier:
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00001837/// [C11] '_Alignas' '(' type-id ')'
1838/// [C11] '_Alignas' '(' constant-expression ')'
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00001839/// [C++0x] 'alignas' '(' type-id ...[opt] ')'
1840/// [C++0x] 'alignas' '(' assignment-expression ...[opt] ')'
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001841void Parser::ParseAlignmentSpecifier(ParsedAttributes &Attrs,
1842 SourceLocation *endLoc) {
1843 assert((Tok.is(tok::kw_alignas) || Tok.is(tok::kw__Alignas)) &&
1844 "Not an alignment-specifier!");
1845
1846 SourceLocation KWLoc = Tok.getLocation();
1847 ConsumeToken();
1848
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001849 BalancedDelimiterTracker T(*this, tok::l_paren);
1850 if (T.expectAndConsume(diag::err_expected_lparen))
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001851 return;
1852
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00001853 SourceLocation EllipsisLoc;
1854 ExprResult ArgExpr = ParseAlignArgument(T.getOpenLocation(), EllipsisLoc);
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001855 if (ArgExpr.isInvalid()) {
1856 SkipUntil(tok::r_paren);
1857 return;
1858 }
1859
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001860 T.consumeClose();
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001861 if (endLoc)
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001862 *endLoc = T.getCloseLocation();
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001863
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00001864 // FIXME: Handle pack-expansions here.
1865 if (EllipsisLoc.isValid()) {
1866 Diag(EllipsisLoc, diag::err_alignas_pack_exp_unsupported);
1867 return;
1868 }
1869
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001870 ExprVector ArgExprs(Actions);
1871 ArgExprs.push_back(ArgExpr.release());
1872 Attrs.addNew(PP.getIdentifierInfo("aligned"), KWLoc, 0, KWLoc,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001873 0, T.getOpenLocation(), ArgExprs.take(), 1, false, true);
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001874}
1875
Reid Spencer5f016e22007-07-11 17:01:13 +00001876/// ParseDeclarationSpecifiers
1877/// declaration-specifiers: [C99 6.7]
1878/// storage-class-specifier declaration-specifiers[opt]
1879/// type-specifier declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00001880/// [C99] function-specifier declaration-specifiers[opt]
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00001881/// [C11] alignment-specifier declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00001882/// [GNU] attributes declaration-specifiers[opt]
Douglas Gregor8d267c52011-09-09 02:06:17 +00001883/// [Clang] '__module_private__' declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00001884///
1885/// storage-class-specifier: [C99 6.7.1]
1886/// 'typedef'
1887/// 'extern'
1888/// 'static'
1889/// 'auto'
1890/// 'register'
Sebastian Redl669d5d72008-11-14 23:42:31 +00001891/// [C++] 'mutable'
Reid Spencer5f016e22007-07-11 17:01:13 +00001892/// [GNU] '__thread'
Reid Spencer5f016e22007-07-11 17:01:13 +00001893/// function-specifier: [C99 6.7.4]
1894/// [C99] 'inline'
Douglas Gregorb48fe382008-10-31 09:07:45 +00001895/// [C++] 'virtual'
1896/// [C++] 'explicit'
Peter Collingbournef315fa82011-02-14 01:42:53 +00001897/// [OpenCL] '__kernel'
Anders Carlssonf47f7a12009-05-06 04:46:28 +00001898/// 'friend': [C++ dcl.friend]
Sebastian Redl2ac67232009-11-05 15:47:02 +00001899/// 'constexpr': [C++0x dcl.constexpr]
Anders Carlssonf47f7a12009-05-06 04:46:28 +00001900
Reid Spencer5f016e22007-07-11 17:01:13 +00001901///
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +00001902void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001903 const ParsedTemplateInfo &TemplateInfo,
John McCall67d1a672009-08-06 02:15:43 +00001904 AccessSpecifier AS,
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +00001905 DeclSpecContext DSContext,
1906 LateParsedAttrList *LateAttrs) {
Douglas Gregor312eadb2011-04-24 05:37:28 +00001907 if (DS.getSourceRange().isInvalid()) {
1908 DS.SetRangeStart(Tok.getLocation());
1909 DS.SetRangeEnd(Tok.getLocation());
1910 }
1911
Douglas Gregorefaa93a2011-11-07 17:33:42 +00001912 bool EnteringContext = (DSContext == DSC_class || DSContext == DSC_top_level);
Reid Spencer5f016e22007-07-11 17:01:13 +00001913 while (1) {
John McCallfec54012009-08-03 20:12:06 +00001914 bool isInvalid = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001915 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00001916 unsigned DiagID = 0;
1917
Reid Spencer5f016e22007-07-11 17:01:13 +00001918 SourceLocation Loc = Tok.getLocation();
Douglas Gregor12e083c2008-11-07 15:42:26 +00001919
Reid Spencer5f016e22007-07-11 17:01:13 +00001920 switch (Tok.getKind()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001921 default:
Chris Lattnerbce61352008-07-26 00:20:22 +00001922 DoneWithDeclSpec:
Peter Collingbournef1907682011-09-29 18:03:57 +00001923 // [C++0x] decl-specifier-seq: decl-specifier attribute-specifier-seq[opt]
1924 MaybeParseCXX0XAttributes(DS.getAttributes());
1925
Reid Spencer5f016e22007-07-11 17:01:13 +00001926 // If this is not a declaration specifier token, we're done reading decl
1927 // specifiers. First verify that DeclSpec's are consistent.
Douglas Gregor9b3064b2009-04-01 22:41:11 +00001928 DS.Finish(Diags, PP);
Reid Spencer5f016e22007-07-11 17:01:13 +00001929 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001930
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001931 case tok::code_completion: {
John McCallf312b1e2010-08-26 23:41:50 +00001932 Sema::ParserCompletionContext CCC = Sema::PCC_Namespace;
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001933 if (DS.hasTypeSpecifier()) {
1934 bool AllowNonIdentifiers
1935 = (getCurScope()->getFlags() & (Scope::ControlScope |
1936 Scope::BlockScope |
1937 Scope::TemplateParamScope |
1938 Scope::FunctionPrototypeScope |
1939 Scope::AtCatchScope)) == 0;
1940 bool AllowNestedNameSpecifiers
1941 = DSContext == DSC_top_level ||
1942 (DSContext == DSC_class && DS.isFriendSpecified());
1943
Douglas Gregorc7b6d882010-09-16 15:14:18 +00001944 Actions.CodeCompleteDeclSpec(getCurScope(), DS,
1945 AllowNonIdentifiers,
1946 AllowNestedNameSpecifiers);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001947 return cutOffParsing();
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001948 }
1949
Douglas Gregor68e3c2e2011-02-15 20:33:25 +00001950 if (getCurScope()->getFnParent() || getCurScope()->getBlockParent())
1951 CCC = Sema::PCC_LocalDeclarationSpecifiers;
1952 else if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate)
John McCallf312b1e2010-08-26 23:41:50 +00001953 CCC = DSContext == DSC_class? Sema::PCC_MemberTemplate
1954 : Sema::PCC_Template;
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001955 else if (DSContext == DSC_class)
John McCallf312b1e2010-08-26 23:41:50 +00001956 CCC = Sema::PCC_Class;
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +00001957 else if (CurParsedObjCImpl)
John McCallf312b1e2010-08-26 23:41:50 +00001958 CCC = Sema::PCC_ObjCImplementation;
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001959
1960 Actions.CodeCompleteOrdinaryName(getCurScope(), CCC);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001961 return cutOffParsing();
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001962 }
1963
Chris Lattner5e02c472009-01-05 00:07:25 +00001964 case tok::coloncolon: // ::foo::bar
John McCall9ba61662010-02-26 08:45:28 +00001965 // C++ scope specifier. Annotate and loop, or bail out on error.
1966 if (TryAnnotateCXXScopeToken(true)) {
1967 if (!DS.hasTypeSpecifier())
1968 DS.SetTypeSpecError();
1969 goto DoneWithDeclSpec;
1970 }
John McCall2e0a7152010-03-01 18:20:46 +00001971 if (Tok.is(tok::coloncolon)) // ::new or ::delete
1972 goto DoneWithDeclSpec;
John McCall9ba61662010-02-26 08:45:28 +00001973 continue;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001974
1975 case tok::annot_cxxscope: {
Richard Smithf63eee72012-05-09 18:56:43 +00001976 if (DS.hasTypeSpecifier() || DS.isTypeAltiVecVector())
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001977 goto DoneWithDeclSpec;
1978
John McCallaa87d332009-12-12 11:40:51 +00001979 CXXScopeSpec SS;
Douglas Gregorc34348a2011-02-24 17:54:50 +00001980 Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(),
1981 Tok.getAnnotationRange(),
1982 SS);
John McCallaa87d332009-12-12 11:40:51 +00001983
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001984 // We are looking for a qualified typename.
Douglas Gregor9135c722009-03-25 15:40:00 +00001985 Token Next = NextToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001986 if (Next.is(tok::annot_template_id) &&
Douglas Gregor9135c722009-03-25 15:40:00 +00001987 static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue())
Douglas Gregorc45c2322009-03-31 00:43:58 +00001988 ->Kind == TNK_Type_template) {
Douglas Gregor9135c722009-03-25 15:40:00 +00001989 // We have a qualified template-id, e.g., N::A<int>
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001990
1991 // C++ [class.qual]p2:
1992 // In a lookup in which the constructor is an acceptable lookup
1993 // result and the nested-name-specifier nominates a class C:
1994 //
1995 // - if the name specified after the
1996 // nested-name-specifier, when looked up in C, is the
1997 // injected-class-name of C (Clause 9), or
1998 //
1999 // - if the name specified after the nested-name-specifier
2000 // is the same as the identifier or the
2001 // simple-template-id's template-name in the last
2002 // component of the nested-name-specifier,
2003 //
2004 // the name is instead considered to name the constructor of
2005 // class C.
2006 //
2007 // Thus, if the template-name is actually the constructor
2008 // name, then the code is ill-formed; this interpretation is
2009 // reinforced by the NAD status of core issue 635.
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +00002010 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Next);
John McCallba9d8532010-04-13 06:39:49 +00002011 if ((DSContext == DSC_top_level ||
2012 (DSContext == DSC_class && DS.isFriendSpecified())) &&
2013 TemplateId->Name &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00002014 Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) {
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002015 if (isConstructorDeclarator()) {
2016 // The user meant this to be an out-of-line constructor
2017 // definition, but template arguments are not allowed
2018 // there. Just allow this as a constructor; we'll
2019 // complain about it later.
2020 goto DoneWithDeclSpec;
2021 }
2022
2023 // The user meant this to name a type, but it actually names
2024 // a constructor with some extraneous template
2025 // arguments. Complain, then parse it as a type as the user
2026 // intended.
2027 Diag(TemplateId->TemplateNameLoc,
2028 diag::err_out_of_line_template_id_names_constructor)
2029 << TemplateId->Name;
2030 }
2031
John McCallaa87d332009-12-12 11:40:51 +00002032 DS.getTypeSpecScope() = SS;
2033 ConsumeToken(); // The C++ scope.
Mike Stump1eb44332009-09-09 15:08:12 +00002034 assert(Tok.is(tok::annot_template_id) &&
Douglas Gregor9135c722009-03-25 15:40:00 +00002035 "ParseOptionalCXXScopeSpecifier not working");
Douglas Gregor059101f2011-03-02 00:47:37 +00002036 AnnotateTemplateIdTokenAsType();
Douglas Gregor9135c722009-03-25 15:40:00 +00002037 continue;
2038 }
2039
Douglas Gregor9d7b3532009-09-28 07:26:33 +00002040 if (Next.is(tok::annot_typename)) {
John McCallaa87d332009-12-12 11:40:51 +00002041 DS.getTypeSpecScope() = SS;
2042 ConsumeToken(); // The C++ scope.
John McCallb3d87482010-08-24 05:47:05 +00002043 if (Tok.getAnnotationValue()) {
2044 ParsedType T = getTypeAnnotation(Tok);
Nico Weber253e80b2010-11-22 10:30:56 +00002045 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename,
2046 Tok.getAnnotationEndLoc(),
John McCallb3d87482010-08-24 05:47:05 +00002047 PrevSpec, DiagID, T);
2048 }
Douglas Gregor9d7b3532009-09-28 07:26:33 +00002049 else
2050 DS.SetTypeSpecError();
2051 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
2052 ConsumeToken(); // The typename
2053 }
2054
Douglas Gregor9135c722009-03-25 15:40:00 +00002055 if (Next.isNot(tok::identifier))
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002056 goto DoneWithDeclSpec;
2057
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002058 // If we're in a context where the identifier could be a class name,
2059 // check whether this is a constructor declaration.
John McCallba9d8532010-04-13 06:39:49 +00002060 if ((DSContext == DSC_top_level ||
2061 (DSContext == DSC_class && DS.isFriendSpecified())) &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00002062 Actions.isCurrentClassName(*Next.getIdentifierInfo(), getCurScope(),
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002063 &SS)) {
2064 if (isConstructorDeclarator())
2065 goto DoneWithDeclSpec;
2066
2067 // As noted in C++ [class.qual]p2 (cited above), when the name
2068 // of the class is qualified in a context where it could name
2069 // a constructor, its a constructor name. However, we've
2070 // looked at the declarator, and the user probably meant this
2071 // to be a type. Complain that it isn't supposed to be treated
2072 // as a type, then proceed to parse it as a type.
2073 Diag(Next.getLocation(), diag::err_out_of_line_type_names_constructor)
2074 << Next.getIdentifierInfo();
2075 }
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002076
John McCallb3d87482010-08-24 05:47:05 +00002077 ParsedType TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(),
2078 Next.getLocation(),
Douglas Gregor9e876872011-03-01 18:12:44 +00002079 getCurScope(), &SS,
2080 false, false, ParsedType(),
Abramo Bagnarafad03b72012-01-27 08:46:19 +00002081 /*IsCtorOrDtorName=*/false,
Douglas Gregor9e876872011-03-01 18:12:44 +00002082 /*NonTrivialSourceInfo=*/true);
Douglas Gregor55f6b142009-02-09 18:46:07 +00002083
Chris Lattnerf4382f52009-04-14 22:17:06 +00002084 // If the referenced identifier is not a type, then this declspec is
2085 // erroneous: We already checked about that it has no type specifier, and
2086 // C++ doesn't have implicit int. Diagnose it as a typo w.r.t. to the
Mike Stump1eb44332009-09-09 15:08:12 +00002087 // typename.
Chris Lattnerf4382f52009-04-14 22:17:06 +00002088 if (TypeRep == 0) {
2089 ConsumeToken(); // Eat the scope spec so the identifier is current.
Richard Smith69730c12012-03-12 07:56:15 +00002090 if (ParseImplicitInt(DS, &SS, TemplateInfo, AS, DSContext)) continue;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002091 goto DoneWithDeclSpec;
Chris Lattnerf4382f52009-04-14 22:17:06 +00002092 }
Mike Stump1eb44332009-09-09 15:08:12 +00002093
John McCallaa87d332009-12-12 11:40:51 +00002094 DS.getTypeSpecScope() = SS;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002095 ConsumeToken(); // The C++ scope.
2096
Douglas Gregor1a51b4a2009-02-09 15:09:02 +00002097 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00002098 DiagID, TypeRep);
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002099 if (isInvalid)
2100 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002101
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002102 DS.SetRangeEnd(Tok.getLocation());
2103 ConsumeToken(); // The typename.
2104
2105 continue;
2106 }
Mike Stump1eb44332009-09-09 15:08:12 +00002107
Chris Lattner80d0c892009-01-21 19:48:37 +00002108 case tok::annot_typename: {
John McCallb3d87482010-08-24 05:47:05 +00002109 if (Tok.getAnnotationValue()) {
2110 ParsedType T = getTypeAnnotation(Tok);
Nico Weberc43271e2010-11-22 12:50:03 +00002111 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallb3d87482010-08-24 05:47:05 +00002112 DiagID, T);
2113 } else
Douglas Gregor31a19b62009-04-01 21:51:26 +00002114 DS.SetTypeSpecError();
Chris Lattner5c5db552010-04-05 18:18:31 +00002115
2116 if (isInvalid)
2117 break;
2118
Chris Lattner80d0c892009-01-21 19:48:37 +00002119 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
2120 ConsumeToken(); // The typename
Mike Stump1eb44332009-09-09 15:08:12 +00002121
Chris Lattner80d0c892009-01-21 19:48:37 +00002122 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
2123 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00002124 // Objective-C interface.
David Blaikie4e4d0842012-03-11 07:00:24 +00002125 if (Tok.is(tok::less) && getLangOpts().ObjC1)
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00002126 ParseObjCProtocolQualifiers(DS);
2127
Chris Lattner80d0c892009-01-21 19:48:37 +00002128 continue;
2129 }
Mike Stump1eb44332009-09-09 15:08:12 +00002130
Douglas Gregorbfad9152011-04-28 15:48:45 +00002131 case tok::kw___is_signed:
2132 // GNU libstdc++ 4.4 uses __is_signed as an identifier, but Clang
2133 // typically treats it as a trait. If we see __is_signed as it appears
2134 // in libstdc++, e.g.,
2135 //
2136 // static const bool __is_signed;
2137 //
2138 // then treat __is_signed as an identifier rather than as a keyword.
2139 if (DS.getTypeSpecType() == TST_bool &&
2140 DS.getTypeQualifiers() == DeclSpec::TQ_const &&
2141 DS.getStorageClassSpec() == DeclSpec::SCS_static) {
2142 Tok.getIdentifierInfo()->RevertTokenIDToIdentifier();
2143 Tok.setKind(tok::identifier);
2144 }
2145
2146 // We're done with the declaration-specifiers.
2147 goto DoneWithDeclSpec;
2148
Chris Lattner3bd934a2008-07-26 01:18:38 +00002149 // typedef-name
David Blaikie42d6d0c2011-12-04 05:04:18 +00002150 case tok::kw_decltype:
Chris Lattner3bd934a2008-07-26 01:18:38 +00002151 case tok::identifier: {
Chris Lattner5e02c472009-01-05 00:07:25 +00002152 // In C++, check to see if this is a scope specifier like foo::bar::, if
2153 // so handle it as such. This is important for ctor parsing.
David Blaikie4e4d0842012-03-11 07:00:24 +00002154 if (getLangOpts().CPlusPlus) {
John McCall9ba61662010-02-26 08:45:28 +00002155 if (TryAnnotateCXXScopeToken(true)) {
2156 if (!DS.hasTypeSpecifier())
2157 DS.SetTypeSpecError();
2158 goto DoneWithDeclSpec;
2159 }
2160 if (!Tok.is(tok::identifier))
2161 continue;
2162 }
Mike Stump1eb44332009-09-09 15:08:12 +00002163
Chris Lattner3bd934a2008-07-26 01:18:38 +00002164 // This identifier can only be a typedef name if we haven't already seen
2165 // a type-specifier. Without this check we misparse:
2166 // typedef int X; struct Y { short X; }; as 'short int'.
2167 if (DS.hasTypeSpecifier())
2168 goto DoneWithDeclSpec;
Mike Stump1eb44332009-09-09 15:08:12 +00002169
John Thompson82287d12010-02-05 00:12:22 +00002170 // Check for need to substitute AltiVec keyword tokens.
2171 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
2172 break;
2173
Richard Smithf63eee72012-05-09 18:56:43 +00002174 // [AltiVec] 2.2: [If the 'vector' specifier is used] The syntax does not
2175 // allow the use of a typedef name as a type specifier.
2176 if (DS.isTypeAltiVecVector())
2177 goto DoneWithDeclSpec;
2178
John McCallb3d87482010-08-24 05:47:05 +00002179 ParsedType TypeRep =
2180 Actions.getTypeName(*Tok.getIdentifierInfo(),
2181 Tok.getLocation(), getCurScope());
Douglas Gregor55f6b142009-02-09 18:46:07 +00002182
Chris Lattnerc199ab32009-04-12 20:42:31 +00002183 // If this is not a typedef name, don't parse it as part of the declspec,
2184 // it must be an implicit int or an error.
John McCallb3d87482010-08-24 05:47:05 +00002185 if (!TypeRep) {
Richard Smith69730c12012-03-12 07:56:15 +00002186 if (ParseImplicitInt(DS, 0, TemplateInfo, AS, DSContext)) continue;
Chris Lattner3bd934a2008-07-26 01:18:38 +00002187 goto DoneWithDeclSpec;
Chris Lattnerc199ab32009-04-12 20:42:31 +00002188 }
Douglas Gregor55f6b142009-02-09 18:46:07 +00002189
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002190 // If we're in a context where the identifier could be a class name,
2191 // check whether this is a constructor declaration.
David Blaikie4e4d0842012-03-11 07:00:24 +00002192 if (getLangOpts().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00002193 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), getCurScope()) &&
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002194 isConstructorDeclarator())
Douglas Gregorb48fe382008-10-31 09:07:45 +00002195 goto DoneWithDeclSpec;
2196
Douglas Gregor1a51b4a2009-02-09 15:09:02 +00002197 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00002198 DiagID, TypeRep);
Chris Lattner3bd934a2008-07-26 01:18:38 +00002199 if (isInvalid)
2200 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002201
Chris Lattner3bd934a2008-07-26 01:18:38 +00002202 DS.SetRangeEnd(Tok.getLocation());
2203 ConsumeToken(); // The identifier
2204
2205 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
2206 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00002207 // Objective-C interface.
David Blaikie4e4d0842012-03-11 07:00:24 +00002208 if (Tok.is(tok::less) && getLangOpts().ObjC1)
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00002209 ParseObjCProtocolQualifiers(DS);
2210
Steve Naroff4f9b9f12008-09-22 10:28:57 +00002211 // Need to support trailing type qualifiers (e.g. "id<p> const").
2212 // If a type specifier follows, it will be diagnosed elsewhere.
2213 continue;
Chris Lattner3bd934a2008-07-26 01:18:38 +00002214 }
Douglas Gregor39a8de12009-02-25 19:37:18 +00002215
2216 // type-name
2217 case tok::annot_template_id: {
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +00002218 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregorc45c2322009-03-31 00:43:58 +00002219 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor39a8de12009-02-25 19:37:18 +00002220 // This template-id does not refer to a type name, so we're
2221 // done with the type-specifiers.
2222 goto DoneWithDeclSpec;
2223 }
2224
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002225 // If we're in a context where the template-id could be a
2226 // constructor name or specialization, check whether this is a
2227 // constructor declaration.
David Blaikie4e4d0842012-03-11 07:00:24 +00002228 if (getLangOpts().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00002229 Actions.isCurrentClassName(*TemplateId->Name, getCurScope()) &&
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002230 isConstructorDeclarator())
2231 goto DoneWithDeclSpec;
2232
Douglas Gregor39a8de12009-02-25 19:37:18 +00002233 // Turn the template-id annotation token into a type annotation
2234 // token, then try again to parse it as a type-specifier.
Douglas Gregor31a19b62009-04-01 21:51:26 +00002235 AnnotateTemplateIdTokenAsType();
Douglas Gregor39a8de12009-02-25 19:37:18 +00002236 continue;
2237 }
2238
Reid Spencer5f016e22007-07-11 17:01:13 +00002239 // GNU attributes support.
2240 case tok::kw___attribute:
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +00002241 ParseGNUAttributes(DS.getAttributes(), 0, LateAttrs);
Reid Spencer5f016e22007-07-11 17:01:13 +00002242 continue;
Steve Narofff59e17e2008-12-24 20:59:21 +00002243
2244 // Microsoft declspec support.
2245 case tok::kw___declspec:
John McCall7f040a92010-12-24 02:08:15 +00002246 ParseMicrosoftDeclSpec(DS.getAttributes());
Steve Narofff59e17e2008-12-24 20:59:21 +00002247 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00002248
Steve Naroff239f0732008-12-25 14:16:32 +00002249 // Microsoft single token adornments.
Steve Naroff86bc6cf2008-12-25 14:41:26 +00002250 case tok::kw___forceinline:
Eli Friedman290eeb02009-06-08 23:27:34 +00002251 // FIXME: Add handling here!
2252 break;
2253
2254 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00002255 case tok::kw___ptr32:
Steve Naroff86bc6cf2008-12-25 14:41:26 +00002256 case tok::kw___w64:
Steve Naroff239f0732008-12-25 14:16:32 +00002257 case tok::kw___cdecl:
2258 case tok::kw___stdcall:
2259 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00002260 case tok::kw___thiscall:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00002261 case tok::kw___unaligned:
John McCall7f040a92010-12-24 02:08:15 +00002262 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman290eeb02009-06-08 23:27:34 +00002263 continue;
2264
Dawn Perchik52fc3142010-09-03 01:29:35 +00002265 // Borland single token adornments.
2266 case tok::kw___pascal:
John McCall7f040a92010-12-24 02:08:15 +00002267 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik52fc3142010-09-03 01:29:35 +00002268 continue;
2269
Peter Collingbournef315fa82011-02-14 01:42:53 +00002270 // OpenCL single token adornments.
2271 case tok::kw___kernel:
2272 ParseOpenCLAttributes(DS.getAttributes());
2273 continue;
2274
Reid Spencer5f016e22007-07-11 17:01:13 +00002275 // storage-class-specifier
2276 case tok::kw_typedef:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002277 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_typedef, Loc,
2278 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002279 break;
2280 case tok::kw_extern:
2281 if (DS.isThreadSpecified())
Chris Lattner1ab3b962008-11-18 07:48:38 +00002282 Diag(Tok, diag::ext_thread_before) << "extern";
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002283 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_extern, Loc,
2284 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002285 break;
Steve Naroff8d54bf22007-12-18 00:16:02 +00002286 case tok::kw___private_extern__:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002287 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_private_extern,
2288 Loc, PrevSpec, DiagID);
Steve Naroff8d54bf22007-12-18 00:16:02 +00002289 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00002290 case tok::kw_static:
2291 if (DS.isThreadSpecified())
Chris Lattner1ab3b962008-11-18 07:48:38 +00002292 Diag(Tok, diag::ext_thread_before) << "static";
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002293 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_static, Loc,
2294 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002295 break;
2296 case tok::kw_auto:
David Blaikie4e4d0842012-03-11 07:00:24 +00002297 if (getLangOpts().CPlusPlus0x) {
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002298 if (isKnownToBeTypeSpecifier(GetLookAheadToken(1))) {
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002299 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc,
2300 PrevSpec, DiagID);
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002301 if (!isInvalid)
Richard Smith8f4fb192011-09-04 19:54:14 +00002302 Diag(Tok, diag::ext_auto_storage_class)
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002303 << FixItHint::CreateRemoval(DS.getStorageClassSpecLoc());
Richard Smith8f4fb192011-09-04 19:54:14 +00002304 } else
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002305 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec,
2306 DiagID);
Richard Smith8f4fb192011-09-04 19:54:14 +00002307 } else
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002308 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc,
2309 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002310 break;
2311 case tok::kw_register:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002312 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_register, Loc,
2313 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002314 break;
Sebastian Redl669d5d72008-11-14 23:42:31 +00002315 case tok::kw_mutable:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002316 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_mutable, Loc,
2317 PrevSpec, DiagID);
Sebastian Redl669d5d72008-11-14 23:42:31 +00002318 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00002319 case tok::kw___thread:
John McCallfec54012009-08-03 20:12:06 +00002320 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002321 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002322
Reid Spencer5f016e22007-07-11 17:01:13 +00002323 // function-specifier
2324 case tok::kw_inline:
John McCallfec54012009-08-03 20:12:06 +00002325 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002326 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +00002327 case tok::kw_virtual:
John McCallfec54012009-08-03 20:12:06 +00002328 isInvalid = DS.SetFunctionSpecVirtual(Loc, PrevSpec, DiagID);
Douglas Gregorb48fe382008-10-31 09:07:45 +00002329 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +00002330 case tok::kw_explicit:
John McCallfec54012009-08-03 20:12:06 +00002331 isInvalid = DS.SetFunctionSpecExplicit(Loc, PrevSpec, DiagID);
Douglas Gregorb48fe382008-10-31 09:07:45 +00002332 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002333
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002334 // alignment-specifier
2335 case tok::kw__Alignas:
David Blaikie4e4d0842012-03-11 07:00:24 +00002336 if (!getLangOpts().C11)
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00002337 Diag(Tok, diag::ext_c11_alignas);
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002338 ParseAlignmentSpecifier(DS.getAttributes());
2339 continue;
2340
Anders Carlssonf47f7a12009-05-06 04:46:28 +00002341 // friend
2342 case tok::kw_friend:
John McCall67d1a672009-08-06 02:15:43 +00002343 if (DSContext == DSC_class)
2344 isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID);
2345 else {
2346 PrevSpec = ""; // not actually used by the diagnostic
2347 DiagID = diag::err_friend_invalid_in_context;
2348 isInvalid = true;
2349 }
Anders Carlssonf47f7a12009-05-06 04:46:28 +00002350 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002351
Douglas Gregor8d267c52011-09-09 02:06:17 +00002352 // Modules
2353 case tok::kw___module_private__:
2354 isInvalid = DS.setModulePrivateSpec(Loc, PrevSpec, DiagID);
2355 break;
2356
Sebastian Redl2ac67232009-11-05 15:47:02 +00002357 // constexpr
2358 case tok::kw_constexpr:
2359 isInvalid = DS.SetConstexprSpec(Loc, PrevSpec, DiagID);
2360 break;
2361
Chris Lattner80d0c892009-01-21 19:48:37 +00002362 // type-specifier
2363 case tok::kw_short:
John McCallfec54012009-08-03 20:12:06 +00002364 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec,
2365 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002366 break;
2367 case tok::kw_long:
2368 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCallfec54012009-08-03 20:12:06 +00002369 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
2370 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002371 else
John McCallfec54012009-08-03 20:12:06 +00002372 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2373 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002374 break;
Francois Pichet338d7f72011-04-28 01:59:37 +00002375 case tok::kw___int64:
2376 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2377 DiagID);
2378 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002379 case tok::kw_signed:
John McCallfec54012009-08-03 20:12:06 +00002380 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec,
2381 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002382 break;
2383 case tok::kw_unsigned:
John McCallfec54012009-08-03 20:12:06 +00002384 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
2385 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002386 break;
2387 case tok::kw__Complex:
John McCallfec54012009-08-03 20:12:06 +00002388 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
2389 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002390 break;
2391 case tok::kw__Imaginary:
John McCallfec54012009-08-03 20:12:06 +00002392 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
2393 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002394 break;
2395 case tok::kw_void:
John McCallfec54012009-08-03 20:12:06 +00002396 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec,
2397 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002398 break;
2399 case tok::kw_char:
John McCallfec54012009-08-03 20:12:06 +00002400 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec,
2401 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002402 break;
2403 case tok::kw_int:
John McCallfec54012009-08-03 20:12:06 +00002404 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec,
2405 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002406 break;
Richard Smith5a5a9712012-04-04 06:24:32 +00002407 case tok::kw___int128:
2408 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int128, Loc, PrevSpec,
2409 DiagID);
2410 break;
2411 case tok::kw_half:
2412 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_half, Loc, PrevSpec,
2413 DiagID);
2414 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002415 case tok::kw_float:
John McCallfec54012009-08-03 20:12:06 +00002416 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec,
2417 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002418 break;
2419 case tok::kw_double:
John McCallfec54012009-08-03 20:12:06 +00002420 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec,
2421 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002422 break;
2423 case tok::kw_wchar_t:
John McCallfec54012009-08-03 20:12:06 +00002424 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec,
2425 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002426 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002427 case tok::kw_char16_t:
John McCallfec54012009-08-03 20:12:06 +00002428 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec,
2429 DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002430 break;
2431 case tok::kw_char32_t:
John McCallfec54012009-08-03 20:12:06 +00002432 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec,
2433 DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002434 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002435 case tok::kw_bool:
2436 case tok::kw__Bool:
Argyrios Kyrtzidis4383e182010-11-16 18:18:13 +00002437 if (Tok.is(tok::kw_bool) &&
2438 DS.getTypeSpecType() != DeclSpec::TST_unspecified &&
2439 DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
2440 PrevSpec = ""; // Not used by the diagnostic.
2441 DiagID = diag::err_bool_redeclaration;
Fariborz Jahaniane106a0b2011-04-19 21:42:37 +00002442 // For better error recovery.
2443 Tok.setKind(tok::identifier);
Argyrios Kyrtzidis4383e182010-11-16 18:18:13 +00002444 isInvalid = true;
2445 } else {
2446 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec,
2447 DiagID);
2448 }
Chris Lattner80d0c892009-01-21 19:48:37 +00002449 break;
2450 case tok::kw__Decimal32:
John McCallfec54012009-08-03 20:12:06 +00002451 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
2452 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002453 break;
2454 case tok::kw__Decimal64:
John McCallfec54012009-08-03 20:12:06 +00002455 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
2456 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002457 break;
2458 case tok::kw__Decimal128:
John McCallfec54012009-08-03 20:12:06 +00002459 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
2460 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002461 break;
John Thompson82287d12010-02-05 00:12:22 +00002462 case tok::kw___vector:
2463 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
2464 break;
2465 case tok::kw___pixel:
2466 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
2467 break;
John McCalla5fc4722011-04-09 22:50:59 +00002468 case tok::kw___unknown_anytype:
2469 isInvalid = DS.SetTypeSpecType(TST_unknown_anytype, Loc,
2470 PrevSpec, DiagID);
2471 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002472
2473 // class-specifier:
2474 case tok::kw_class:
2475 case tok::kw_struct:
Chris Lattner4c97d762009-04-12 21:49:30 +00002476 case tok::kw_union: {
2477 tok::TokenKind Kind = Tok.getKind();
2478 ConsumeToken();
Richard Smith69730c12012-03-12 07:56:15 +00002479 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS,
2480 EnteringContext, DSContext);
Chris Lattner80d0c892009-01-21 19:48:37 +00002481 continue;
Chris Lattner4c97d762009-04-12 21:49:30 +00002482 }
Chris Lattner80d0c892009-01-21 19:48:37 +00002483
2484 // enum-specifier:
2485 case tok::kw_enum:
Chris Lattner4c97d762009-04-12 21:49:30 +00002486 ConsumeToken();
Richard Smith69730c12012-03-12 07:56:15 +00002487 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS, DSContext);
Chris Lattner80d0c892009-01-21 19:48:37 +00002488 continue;
2489
2490 // cv-qualifier:
2491 case tok::kw_const:
John McCallfec54012009-08-03 20:12:06 +00002492 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec, DiagID,
David Blaikie4e4d0842012-03-11 07:00:24 +00002493 getLangOpts());
Chris Lattner80d0c892009-01-21 19:48:37 +00002494 break;
2495 case tok::kw_volatile:
John McCallfec54012009-08-03 20:12:06 +00002496 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
David Blaikie4e4d0842012-03-11 07:00:24 +00002497 getLangOpts());
Chris Lattner80d0c892009-01-21 19:48:37 +00002498 break;
2499 case tok::kw_restrict:
John McCallfec54012009-08-03 20:12:06 +00002500 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
David Blaikie4e4d0842012-03-11 07:00:24 +00002501 getLangOpts());
Chris Lattner80d0c892009-01-21 19:48:37 +00002502 break;
2503
Douglas Gregord57959a2009-03-27 23:10:48 +00002504 // C++ typename-specifier:
2505 case tok::kw_typename:
John McCall9ba61662010-02-26 08:45:28 +00002506 if (TryAnnotateTypeOrScopeToken()) {
2507 DS.SetTypeSpecError();
2508 goto DoneWithDeclSpec;
2509 }
2510 if (!Tok.is(tok::kw_typename))
Douglas Gregord57959a2009-03-27 23:10:48 +00002511 continue;
2512 break;
2513
Chris Lattner80d0c892009-01-21 19:48:37 +00002514 // GNU typeof support.
2515 case tok::kw_typeof:
2516 ParseTypeofSpecifier(DS);
2517 continue;
2518
David Blaikie42d6d0c2011-12-04 05:04:18 +00002519 case tok::annot_decltype:
Anders Carlsson6fd634f2009-06-24 17:47:40 +00002520 ParseDecltypeSpecifier(DS);
2521 continue;
2522
Sean Huntdb5d44b2011-05-19 05:37:45 +00002523 case tok::kw___underlying_type:
2524 ParseUnderlyingTypeSpecifier(DS);
Eli Friedmanb001de72011-10-06 23:00:33 +00002525 continue;
2526
2527 case tok::kw__Atomic:
2528 ParseAtomicSpecifier(DS);
2529 continue;
Sean Huntdb5d44b2011-05-19 05:37:45 +00002530
Peter Collingbourne207f4d82011-03-18 22:38:29 +00002531 // OpenCL qualifiers:
2532 case tok::kw_private:
David Blaikie4e4d0842012-03-11 07:00:24 +00002533 if (!getLangOpts().OpenCL)
Peter Collingbourne207f4d82011-03-18 22:38:29 +00002534 goto DoneWithDeclSpec;
2535 case tok::kw___private:
2536 case tok::kw___global:
2537 case tok::kw___local:
2538 case tok::kw___constant:
2539 case tok::kw___read_only:
2540 case tok::kw___write_only:
2541 case tok::kw___read_write:
2542 ParseOpenCLQualifiers(DS);
2543 break;
2544
Steve Naroffd3ded1f2008-06-05 00:02:44 +00002545 case tok::less:
Chris Lattner3bd934a2008-07-26 01:18:38 +00002546 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattnerbce61352008-07-26 00:20:22 +00002547 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
2548 // but we support it.
David Blaikie4e4d0842012-03-11 07:00:24 +00002549 if (DS.hasTypeSpecifier() || !getLangOpts().ObjC1)
Chris Lattnerbce61352008-07-26 00:20:22 +00002550 goto DoneWithDeclSpec;
Mike Stump1eb44332009-09-09 15:08:12 +00002551
Douglas Gregor46f936e2010-11-19 17:10:50 +00002552 if (!ParseObjCProtocolQualifiers(DS))
2553 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
2554 << FixItHint::CreateInsertion(Loc, "id")
2555 << SourceRange(Loc, DS.getSourceRange().getEnd());
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00002556
2557 // Need to support trailing type qualifiers (e.g. "id<p> const").
2558 // If a type specifier follows, it will be diagnosed elsewhere.
2559 continue;
Reid Spencer5f016e22007-07-11 17:01:13 +00002560 }
John McCallfec54012009-08-03 20:12:06 +00002561 // If the specifier wasn't legal, issue a diagnostic.
Reid Spencer5f016e22007-07-11 17:01:13 +00002562 if (isInvalid) {
2563 assert(PrevSpec && "Method did not return previous specifier!");
John McCallfec54012009-08-03 20:12:06 +00002564 assert(DiagID);
Douglas Gregorae2fb142010-08-23 14:34:43 +00002565
2566 if (DiagID == diag::ext_duplicate_declspec)
2567 Diag(Tok, DiagID)
2568 << PrevSpec << FixItHint::CreateRemoval(Tok.getLocation());
2569 else
2570 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00002571 }
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002572
Chris Lattner81c018d2008-03-13 06:29:04 +00002573 DS.SetRangeEnd(Tok.getLocation());
Fariborz Jahaniane106a0b2011-04-19 21:42:37 +00002574 if (DiagID != diag::err_bool_redeclaration)
2575 ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00002576 }
2577}
Douglas Gregoradcac882008-12-01 23:54:00 +00002578
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002579/// ParseStructDeclaration - Parse a struct declaration without the terminating
2580/// semicolon.
2581///
Reid Spencer5f016e22007-07-11 17:01:13 +00002582/// struct-declaration:
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002583/// specifier-qualifier-list struct-declarator-list
Reid Spencer5f016e22007-07-11 17:01:13 +00002584/// [GNU] __extension__ struct-declaration
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002585/// [GNU] specifier-qualifier-list
Reid Spencer5f016e22007-07-11 17:01:13 +00002586/// struct-declarator-list:
2587/// struct-declarator
2588/// struct-declarator-list ',' struct-declarator
2589/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
2590/// struct-declarator:
2591/// declarator
2592/// [GNU] declarator attributes[opt]
2593/// declarator[opt] ':' constant-expression
2594/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
2595///
Chris Lattnere1359422008-04-10 06:46:29 +00002596void Parser::
John McCallbdd563e2009-11-03 02:38:08 +00002597ParseStructDeclaration(DeclSpec &DS, FieldCallback &Fields) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002598
Chris Lattnerc46d1a12008-10-20 06:45:43 +00002599 if (Tok.is(tok::kw___extension__)) {
2600 // __extension__ silences extension warnings in the subexpression.
2601 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroff28a7ca82007-08-20 22:28:22 +00002602 ConsumeToken();
Chris Lattnerc46d1a12008-10-20 06:45:43 +00002603 return ParseStructDeclaration(DS, Fields);
2604 }
Mike Stump1eb44332009-09-09 15:08:12 +00002605
Steve Naroff28a7ca82007-08-20 22:28:22 +00002606 // Parse the common specifier-qualifiers-list piece.
Steve Naroff28a7ca82007-08-20 22:28:22 +00002607 ParseSpecifierQualifierList(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00002608
Douglas Gregor4920f1f2009-01-12 22:49:06 +00002609 // If there are no declarators, this is a free-standing declaration
2610 // specifier. Let the actions module cope with it.
Chris Lattner04d66662007-10-09 17:33:22 +00002611 if (Tok.is(tok::semi)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00002612 Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none, DS);
Steve Naroff28a7ca82007-08-20 22:28:22 +00002613 return;
2614 }
2615
2616 // Read struct-declarators until we find the semicolon.
John McCallbdd563e2009-11-03 02:38:08 +00002617 bool FirstDeclarator = true;
Richard Smith7984de32012-01-12 23:53:29 +00002618 SourceLocation CommaLoc;
Steve Naroff28a7ca82007-08-20 22:28:22 +00002619 while (1) {
John McCall92576642012-05-07 06:16:41 +00002620 ParsingDeclRAIIObject PD(*this, ParsingDeclRAIIObject::NoParent);
John McCallbdd563e2009-11-03 02:38:08 +00002621 FieldDeclarator DeclaratorInfo(DS);
Richard Smith7984de32012-01-12 23:53:29 +00002622 DeclaratorInfo.D.setCommaLoc(CommaLoc);
John McCallbdd563e2009-11-03 02:38:08 +00002623
2624 // Attributes are only allowed here on successive declarators.
John McCall7f040a92010-12-24 02:08:15 +00002625 if (!FirstDeclarator)
2626 MaybeParseGNUAttributes(DeclaratorInfo.D);
Mike Stump1eb44332009-09-09 15:08:12 +00002627
Steve Naroff28a7ca82007-08-20 22:28:22 +00002628 /// struct-declarator: declarator
2629 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattnera1efc8c2009-12-10 01:59:24 +00002630 if (Tok.isNot(tok::colon)) {
2631 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
2632 ColonProtectionRAIIObject X(*this);
Chris Lattnere1359422008-04-10 06:46:29 +00002633 ParseDeclarator(DeclaratorInfo.D);
Chris Lattnera1efc8c2009-12-10 01:59:24 +00002634 }
Mike Stump1eb44332009-09-09 15:08:12 +00002635
Chris Lattner04d66662007-10-09 17:33:22 +00002636 if (Tok.is(tok::colon)) {
Steve Naroff28a7ca82007-08-20 22:28:22 +00002637 ConsumeToken();
John McCall60d7b3a2010-08-24 06:29:42 +00002638 ExprResult Res(ParseConstantExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002639 if (Res.isInvalid())
Steve Naroff28a7ca82007-08-20 22:28:22 +00002640 SkipUntil(tok::semi, true, true);
Chris Lattner60b1e3e2008-04-10 06:15:14 +00002641 else
Sebastian Redleffa8d12008-12-10 00:02:53 +00002642 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroff28a7ca82007-08-20 22:28:22 +00002643 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00002644
Steve Naroff28a7ca82007-08-20 22:28:22 +00002645 // If attributes exist after the declarator, parse them.
John McCall7f040a92010-12-24 02:08:15 +00002646 MaybeParseGNUAttributes(DeclaratorInfo.D);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002647
John McCallbdd563e2009-11-03 02:38:08 +00002648 // We're done with this declarator; invoke the callback.
John McCalld226f652010-08-21 09:40:31 +00002649 Decl *D = Fields.invoke(DeclaratorInfo);
John McCall54abf7d2009-11-04 02:18:39 +00002650 PD.complete(D);
John McCallbdd563e2009-11-03 02:38:08 +00002651
Steve Naroff28a7ca82007-08-20 22:28:22 +00002652 // If we don't have a comma, it is either the end of the list (a ';')
2653 // or an error, bail out.
Chris Lattner04d66662007-10-09 17:33:22 +00002654 if (Tok.isNot(tok::comma))
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002655 return;
Sebastian Redlab197ba2009-02-09 18:23:29 +00002656
Steve Naroff28a7ca82007-08-20 22:28:22 +00002657 // Consume the comma.
Richard Smith7984de32012-01-12 23:53:29 +00002658 CommaLoc = ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00002659
John McCallbdd563e2009-11-03 02:38:08 +00002660 FirstDeclarator = false;
Steve Naroff28a7ca82007-08-20 22:28:22 +00002661 }
Steve Naroff28a7ca82007-08-20 22:28:22 +00002662}
2663
2664/// ParseStructUnionBody
2665/// struct-contents:
2666/// struct-declaration-list
2667/// [EXT] empty
2668/// [GNU] "struct-declaration-list" without terminatoring ';'
2669/// struct-declaration-list:
2670/// struct-declaration
2671/// struct-declaration-list struct-declaration
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002672/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroff28a7ca82007-08-20 22:28:22 +00002673///
Reid Spencer5f016e22007-07-11 17:01:13 +00002674void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
John McCalld226f652010-08-21 09:40:31 +00002675 unsigned TagType, Decl *TagDecl) {
John McCallf312b1e2010-08-26 23:41:50 +00002676 PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc,
2677 "parsing struct/union body");
Mike Stump1eb44332009-09-09 15:08:12 +00002678
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002679 BalancedDelimiterTracker T(*this, tok::l_brace);
2680 if (T.consumeOpen())
2681 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002682
Douglas Gregor3218c4b2009-01-09 22:42:13 +00002683 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
Douglas Gregor23c94db2010-07-02 17:43:08 +00002684 Actions.ActOnTagStartDefinition(getCurScope(), TagDecl);
Douglas Gregor72de6672009-01-08 20:45:30 +00002685
Reid Spencer5f016e22007-07-11 17:01:13 +00002686 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
2687 // C++.
David Blaikie4e4d0842012-03-11 07:00:24 +00002688 if (Tok.is(tok::r_brace) && !getLangOpts().CPlusPlus) {
Richard Smithd7c56e12011-12-29 21:57:33 +00002689 Diag(Tok, diag::ext_empty_struct_union) << (TagType == TST_union);
2690 Diag(Tok, diag::warn_empty_struct_union_compat) << (TagType == TST_union);
2691 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002692
Chris Lattner5f9e2722011-07-23 10:55:15 +00002693 SmallVector<Decl *, 32> FieldDecls;
Chris Lattnere1359422008-04-10 06:46:29 +00002694
Reid Spencer5f016e22007-07-11 17:01:13 +00002695 // While we still have something to read, read the declarations in the struct.
Chris Lattner04d66662007-10-09 17:33:22 +00002696 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002697 // Each iteration of this loop reads one struct-declaration.
Mike Stump1eb44332009-09-09 15:08:12 +00002698
Reid Spencer5f016e22007-07-11 17:01:13 +00002699 // Check for extraneous top-level semicolon.
Chris Lattner04d66662007-10-09 17:33:22 +00002700 if (Tok.is(tok::semi)) {
Douglas Gregor9b3064b2009-04-01 22:41:11 +00002701 Diag(Tok, diag::ext_extra_struct_semi)
Douglas Gregorf13ca062010-06-16 23:08:59 +00002702 << DeclSpec::getSpecifierName((DeclSpec::TST)TagType)
Douglas Gregor849b2432010-03-31 17:46:05 +00002703 << FixItHint::CreateRemoval(Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00002704 ConsumeToken();
2705 continue;
2706 }
Chris Lattnere1359422008-04-10 06:46:29 +00002707
2708 // Parse all the comma separated declarators.
John McCall0b7e6782011-03-24 11:26:52 +00002709 DeclSpec DS(AttrFactory);
Mike Stump1eb44332009-09-09 15:08:12 +00002710
John McCallbdd563e2009-11-03 02:38:08 +00002711 if (!Tok.is(tok::at)) {
2712 struct CFieldCallback : FieldCallback {
2713 Parser &P;
John McCalld226f652010-08-21 09:40:31 +00002714 Decl *TagDecl;
Chris Lattner5f9e2722011-07-23 10:55:15 +00002715 SmallVectorImpl<Decl *> &FieldDecls;
John McCallbdd563e2009-11-03 02:38:08 +00002716
John McCalld226f652010-08-21 09:40:31 +00002717 CFieldCallback(Parser &P, Decl *TagDecl,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002718 SmallVectorImpl<Decl *> &FieldDecls) :
John McCallbdd563e2009-11-03 02:38:08 +00002719 P(P), TagDecl(TagDecl), FieldDecls(FieldDecls) {}
2720
John McCalld226f652010-08-21 09:40:31 +00002721 virtual Decl *invoke(FieldDeclarator &FD) {
John McCallbdd563e2009-11-03 02:38:08 +00002722 // Install the declarator into the current TagDecl.
John McCalld226f652010-08-21 09:40:31 +00002723 Decl *Field = P.Actions.ActOnField(P.getCurScope(), TagDecl,
John McCall4ba39712009-11-03 21:13:47 +00002724 FD.D.getDeclSpec().getSourceRange().getBegin(),
2725 FD.D, FD.BitfieldSize);
John McCallbdd563e2009-11-03 02:38:08 +00002726 FieldDecls.push_back(Field);
2727 return Field;
Douglas Gregor91a28862009-08-26 14:27:30 +00002728 }
John McCallbdd563e2009-11-03 02:38:08 +00002729 } Callback(*this, TagDecl, FieldDecls);
2730
2731 ParseStructDeclaration(DS, Callback);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002732 } else { // Handle @defs
2733 ConsumeToken();
2734 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
2735 Diag(Tok, diag::err_unexpected_at);
Chris Lattner3e156ad2010-02-02 00:37:27 +00002736 SkipUntil(tok::semi, true);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002737 continue;
2738 }
2739 ConsumeToken();
2740 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
2741 if (!Tok.is(tok::identifier)) {
2742 Diag(Tok, diag::err_expected_ident);
Chris Lattner3e156ad2010-02-02 00:37:27 +00002743 SkipUntil(tok::semi, true);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002744 continue;
2745 }
Chris Lattner5f9e2722011-07-23 10:55:15 +00002746 SmallVector<Decl *, 16> Fields;
Douglas Gregor23c94db2010-07-02 17:43:08 +00002747 Actions.ActOnDefs(getCurScope(), TagDecl, Tok.getLocation(),
Douglas Gregor44b43212008-12-11 16:49:14 +00002748 Tok.getIdentifierInfo(), Fields);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002749 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
2750 ConsumeToken();
2751 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
Mike Stump1eb44332009-09-09 15:08:12 +00002752 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002753
Chris Lattner04d66662007-10-09 17:33:22 +00002754 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002755 ConsumeToken();
Chris Lattner04d66662007-10-09 17:33:22 +00002756 } else if (Tok.is(tok::r_brace)) {
Chris Lattner3e156ad2010-02-02 00:37:27 +00002757 ExpectAndConsume(tok::semi, diag::ext_expected_semi_decl_list);
Reid Spencer5f016e22007-07-11 17:01:13 +00002758 break;
2759 } else {
Chris Lattner3e156ad2010-02-02 00:37:27 +00002760 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
2761 // Skip to end of block or statement to avoid ext-warning on extra ';'.
Reid Spencer5f016e22007-07-11 17:01:13 +00002762 SkipUntil(tok::r_brace, true, true);
Chris Lattner3e156ad2010-02-02 00:37:27 +00002763 // If we stopped at a ';', eat it.
2764 if (Tok.is(tok::semi)) ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00002765 }
2766 }
Mike Stump1eb44332009-09-09 15:08:12 +00002767
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002768 T.consumeClose();
Mike Stump1eb44332009-09-09 15:08:12 +00002769
John McCall0b7e6782011-03-24 11:26:52 +00002770 ParsedAttributes attrs(AttrFactory);
Reid Spencer5f016e22007-07-11 17:01:13 +00002771 // If attributes exist after struct contents, parse them.
John McCall7f040a92010-12-24 02:08:15 +00002772 MaybeParseGNUAttributes(attrs);
Daniel Dunbar1bfe1c22008-10-03 02:03:53 +00002773
Douglas Gregor23c94db2010-07-02 17:43:08 +00002774 Actions.ActOnFields(getCurScope(),
David Blaikie77b6de02011-09-22 02:58:26 +00002775 RecordLoc, TagDecl, FieldDecls,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002776 T.getOpenLocation(), T.getCloseLocation(),
John McCall7f040a92010-12-24 02:08:15 +00002777 attrs.getList());
Douglas Gregor72de6672009-01-08 20:45:30 +00002778 StructScope.Exit();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002779 Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl,
2780 T.getCloseLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00002781}
2782
Reid Spencer5f016e22007-07-11 17:01:13 +00002783/// ParseEnumSpecifier
2784/// enum-specifier: [C99 6.7.2.2]
2785/// 'enum' identifier[opt] '{' enumerator-list '}'
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002786///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Reid Spencer5f016e22007-07-11 17:01:13 +00002787/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
2788/// '}' attributes[opt]
Aaron Ballman6454a022012-03-01 04:09:28 +00002789/// [MS] 'enum' __declspec[opt] identifier[opt] '{' enumerator-list ',' [opt]
2790/// '}'
Reid Spencer5f016e22007-07-11 17:01:13 +00002791/// 'enum' identifier
2792/// [GNU] 'enum' attributes[opt] identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002793///
Richard Smith1af83c42012-03-23 03:33:32 +00002794/// [C++11] enum-head '{' enumerator-list[opt] '}'
2795/// [C++11] enum-head '{' enumerator-list ',' '}'
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002796///
Richard Smith1af83c42012-03-23 03:33:32 +00002797/// enum-head: [C++11]
2798/// enum-key attribute-specifier-seq[opt] identifier[opt] enum-base[opt]
2799/// enum-key attribute-specifier-seq[opt] nested-name-specifier
2800/// identifier enum-base[opt]
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002801///
Richard Smith1af83c42012-03-23 03:33:32 +00002802/// enum-key: [C++11]
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002803/// 'enum'
2804/// 'enum' 'class'
2805/// 'enum' 'struct'
2806///
Richard Smith1af83c42012-03-23 03:33:32 +00002807/// enum-base: [C++11]
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002808/// ':' type-specifier-seq
2809///
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002810/// [C++] elaborated-type-specifier:
2811/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
2812///
Chris Lattner4c97d762009-04-12 21:49:30 +00002813void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregor9b9edd62010-03-02 17:53:14 +00002814 const ParsedTemplateInfo &TemplateInfo,
Richard Smith69730c12012-03-12 07:56:15 +00002815 AccessSpecifier AS, DeclSpecContext DSC) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002816 // Parse the tag portion of this.
Douglas Gregor374929f2009-09-18 15:37:17 +00002817 if (Tok.is(tok::code_completion)) {
2818 // Code completion for an enum name.
Douglas Gregor23c94db2010-07-02 17:43:08 +00002819 Actions.CodeCompleteTag(getCurScope(), DeclSpec::TST_enum);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002820 return cutOffParsing();
Douglas Gregor374929f2009-09-18 15:37:17 +00002821 }
John McCall57c13002011-07-06 05:58:41 +00002822
Richard Smithbdad7a22012-01-10 01:33:14 +00002823 SourceLocation ScopedEnumKWLoc;
John McCall57c13002011-07-06 05:58:41 +00002824 bool IsScopedUsingClassTag = false;
2825
David Blaikie4e4d0842012-03-11 07:00:24 +00002826 if (getLangOpts().CPlusPlus0x &&
John McCall57c13002011-07-06 05:58:41 +00002827 (Tok.is(tok::kw_class) || Tok.is(tok::kw_struct))) {
Richard Smith7fe62082011-10-15 05:09:34 +00002828 Diag(Tok, diag::warn_cxx98_compat_scoped_enum);
John McCall57c13002011-07-06 05:58:41 +00002829 IsScopedUsingClassTag = Tok.is(tok::kw_class);
Richard Smithbdad7a22012-01-10 01:33:14 +00002830 ScopedEnumKWLoc = ConsumeToken();
John McCall57c13002011-07-06 05:58:41 +00002831 }
Richard Smith1af83c42012-03-23 03:33:32 +00002832
John McCall13489672012-05-07 06:16:58 +00002833 // C++11 [temp.explicit]p12:
2834 // The usual access controls do not apply to names used to specify
2835 // explicit instantiations.
2836 // We extend this to also cover explicit specializations. Note that
2837 // we don't suppress if this turns out to be an elaborated type
2838 // specifier.
2839 bool shouldDelayDiagsInTag =
2840 (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation ||
2841 TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization);
2842 SuppressAccessChecks diagsFromTag(*this, shouldDelayDiagsInTag);
Richard Smith1af83c42012-03-23 03:33:32 +00002843
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002844 // If attributes exist after tag, parse them.
John McCall0b7e6782011-03-24 11:26:52 +00002845 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00002846 MaybeParseGNUAttributes(attrs);
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002847
Aaron Ballman6454a022012-03-01 04:09:28 +00002848 // If declspecs exist after tag, parse them.
2849 while (Tok.is(tok::kw___declspec))
2850 ParseMicrosoftDeclSpec(attrs);
2851
Richard Smith7796eb52012-03-12 08:56:40 +00002852 // Enum definitions should not be parsed in a trailing-return-type.
2853 bool AllowDeclaration = DSC != DSC_trailing;
2854
2855 bool AllowFixedUnderlyingType = AllowDeclaration &&
2856 (getLangOpts().CPlusPlus0x || getLangOpts().MicrosoftExt ||
2857 getLangOpts().ObjC2);
John McCall57c13002011-07-06 05:58:41 +00002858
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00002859 CXXScopeSpec &SS = DS.getTypeSpecScope();
David Blaikie4e4d0842012-03-11 07:00:24 +00002860 if (getLangOpts().CPlusPlus) {
John McCall57c13002011-07-06 05:58:41 +00002861 // "enum foo : bar;" is not a potential typo for "enum foo::bar;"
2862 // if a fixed underlying type is allowed.
2863 ColonProtectionRAIIObject X(*this, AllowFixedUnderlyingType);
2864
Douglas Gregorefaa93a2011-11-07 17:33:42 +00002865 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
2866 /*EnteringContext=*/false))
John McCall9ba61662010-02-26 08:45:28 +00002867 return;
2868
2869 if (SS.isSet() && Tok.isNot(tok::identifier)) {
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002870 Diag(Tok, diag::err_expected_ident);
2871 if (Tok.isNot(tok::l_brace)) {
2872 // Has no name and is not a definition.
2873 // Skip the rest of this declarator, up until the comma or semicolon.
2874 SkipUntil(tok::comma, true);
2875 return;
2876 }
2877 }
2878 }
Mike Stump1eb44332009-09-09 15:08:12 +00002879
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002880 // Must have either 'enum name' or 'enum {...}'.
Douglas Gregorb9075602011-02-22 02:55:24 +00002881 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace) &&
Richard Smith7796eb52012-03-12 08:56:40 +00002882 !(AllowFixedUnderlyingType && Tok.is(tok::colon))) {
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002883 Diag(Tok, diag::err_expected_ident_lbrace);
Mike Stump1eb44332009-09-09 15:08:12 +00002884
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002885 // Skip the rest of this declarator, up until the comma or semicolon.
2886 SkipUntil(tok::comma, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00002887 return;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002888 }
Mike Stump1eb44332009-09-09 15:08:12 +00002889
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002890 // If an identifier is present, consume and remember it.
2891 IdentifierInfo *Name = 0;
2892 SourceLocation NameLoc;
2893 if (Tok.is(tok::identifier)) {
2894 Name = Tok.getIdentifierInfo();
2895 NameLoc = ConsumeToken();
2896 }
Mike Stump1eb44332009-09-09 15:08:12 +00002897
Richard Smithbdad7a22012-01-10 01:33:14 +00002898 if (!Name && ScopedEnumKWLoc.isValid()) {
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002899 // C++0x 7.2p2: The optional identifier shall not be omitted in the
2900 // declaration of a scoped enumeration.
2901 Diag(Tok, diag::err_scoped_enum_missing_identifier);
Richard Smithbdad7a22012-01-10 01:33:14 +00002902 ScopedEnumKWLoc = SourceLocation();
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00002903 IsScopedUsingClassTag = false;
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002904 }
2905
John McCall13489672012-05-07 06:16:58 +00002906 // Okay, end the suppression area. We'll decide whether to emit the
2907 // diagnostics in a second.
2908 if (shouldDelayDiagsInTag)
2909 diagsFromTag.done();
Richard Smith1af83c42012-03-23 03:33:32 +00002910
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002911 TypeResult BaseType;
2912
Douglas Gregora61b3e72010-12-01 17:42:47 +00002913 // Parse the fixed underlying type.
Douglas Gregorb9075602011-02-22 02:55:24 +00002914 if (AllowFixedUnderlyingType && Tok.is(tok::colon)) {
Douglas Gregora61b3e72010-12-01 17:42:47 +00002915 bool PossibleBitfield = false;
2916 if (getCurScope()->getFlags() & Scope::ClassScope) {
2917 // If we're in class scope, this can either be an enum declaration with
2918 // an underlying type, or a declaration of a bitfield member. We try to
2919 // use a simple disambiguation scheme first to catch the common cases
2920 // (integer literal, sizeof); if it's still ambiguous, we then consider
2921 // anything that's a simple-type-specifier followed by '(' as an
2922 // expression. This suffices because function types are not valid
2923 // underlying types anyway.
2924 TPResult TPR = isExpressionOrTypeSpecifierSimple(NextToken().getKind());
2925 // If the next token starts an expression, we know we're parsing a
2926 // bit-field. This is the common case.
2927 if (TPR == TPResult::True())
2928 PossibleBitfield = true;
2929 // If the next token starts a type-specifier-seq, it may be either a
2930 // a fixed underlying type or the start of a function-style cast in C++;
2931 // lookahead one more token to see if it's obvious that we have a
2932 // fixed underlying type.
2933 else if (TPR == TPResult::False() &&
2934 GetLookAheadToken(2).getKind() == tok::semi) {
2935 // Consume the ':'.
2936 ConsumeToken();
2937 } else {
2938 // We have the start of a type-specifier-seq, so we have to perform
2939 // tentative parsing to determine whether we have an expression or a
2940 // type.
2941 TentativeParsingAction TPA(*this);
2942
2943 // Consume the ':'.
2944 ConsumeToken();
Richard Smithd81e9612012-02-23 01:36:12 +00002945
2946 // If we see a type specifier followed by an open-brace, we have an
2947 // ambiguity between an underlying type and a C++11 braced
2948 // function-style cast. Resolve this by always treating it as an
2949 // underlying type.
2950 // FIXME: The standard is not entirely clear on how to disambiguate in
2951 // this case.
David Blaikie4e4d0842012-03-11 07:00:24 +00002952 if ((getLangOpts().CPlusPlus &&
Richard Smithd81e9612012-02-23 01:36:12 +00002953 isCXXDeclarationSpecifier(TPResult::True()) != TPResult::True()) ||
David Blaikie4e4d0842012-03-11 07:00:24 +00002954 (!getLangOpts().CPlusPlus && !isDeclarationSpecifier(true))) {
Douglas Gregora61b3e72010-12-01 17:42:47 +00002955 // We'll parse this as a bitfield later.
2956 PossibleBitfield = true;
2957 TPA.Revert();
2958 } else {
2959 // We have a type-specifier-seq.
2960 TPA.Commit();
2961 }
2962 }
2963 } else {
2964 // Consume the ':'.
2965 ConsumeToken();
2966 }
2967
2968 if (!PossibleBitfield) {
2969 SourceRange Range;
2970 BaseType = ParseTypeName(&Range);
Douglas Gregor86f208c2011-02-22 20:32:04 +00002971
David Blaikie4e4d0842012-03-11 07:00:24 +00002972 if (!getLangOpts().CPlusPlus0x && !getLangOpts().ObjC2)
Douglas Gregor86f208c2011-02-22 20:32:04 +00002973 Diag(StartLoc, diag::ext_ms_enum_fixed_underlying_type)
2974 << Range;
David Blaikie4e4d0842012-03-11 07:00:24 +00002975 if (getLangOpts().CPlusPlus0x)
Richard Smith7fe62082011-10-15 05:09:34 +00002976 Diag(StartLoc, diag::warn_cxx98_compat_enum_fixed_underlying_type);
Douglas Gregora61b3e72010-12-01 17:42:47 +00002977 }
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002978 }
2979
Richard Smithbdad7a22012-01-10 01:33:14 +00002980 // There are four options here. If we have 'friend enum foo;' then this is a
2981 // friend declaration, and cannot have an accompanying definition. If we have
2982 // 'enum foo;', then this is a forward declaration. If we have
2983 // 'enum foo {...' then this is a definition. Otherwise we have something
2984 // like 'enum foo xyz', a reference.
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002985 //
2986 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
2987 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
2988 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
2989 //
John McCallf312b1e2010-08-26 23:41:50 +00002990 Sema::TagUseKind TUK;
John McCall13489672012-05-07 06:16:58 +00002991 if (!AllowDeclaration) {
Richard Smith7796eb52012-03-12 08:56:40 +00002992 TUK = Sema::TUK_Reference;
John McCall13489672012-05-07 06:16:58 +00002993 } else if (Tok.is(tok::l_brace)) {
2994 if (DS.isFriendSpecified()) {
2995 Diag(Tok.getLocation(), diag::err_friend_decl_defines_type)
2996 << SourceRange(DS.getFriendSpecLoc());
2997 ConsumeBrace();
2998 SkipUntil(tok::r_brace);
2999 TUK = Sema::TUK_Friend;
3000 } else {
3001 TUK = Sema::TUK_Definition;
3002 }
3003 } else if (Tok.is(tok::semi) && DSC != DSC_type_specifier) {
3004 TUK = (DS.isFriendSpecified() ? Sema::TUK_Friend : Sema::TUK_Declaration);
3005 } else {
John McCallf312b1e2010-08-26 23:41:50 +00003006 TUK = Sema::TUK_Reference;
John McCall13489672012-05-07 06:16:58 +00003007 }
3008
3009 // If this is an elaborated type specifier, and we delayed
3010 // diagnostics before, just merge them into the current pool.
3011 if (TUK == Sema::TUK_Reference && shouldDelayDiagsInTag) {
3012 diagsFromTag.redelay();
3013 }
Richard Smith1af83c42012-03-23 03:33:32 +00003014
3015 MultiTemplateParamsArg TParams;
Douglas Gregor8fc6d232010-05-03 17:48:54 +00003016 if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate &&
John McCallf312b1e2010-08-26 23:41:50 +00003017 TUK != Sema::TUK_Reference) {
Richard Smith1af83c42012-03-23 03:33:32 +00003018 if (!getLangOpts().CPlusPlus0x || !SS.isSet()) {
3019 // Skip the rest of this declarator, up until the comma or semicolon.
3020 Diag(Tok, diag::err_enum_template);
3021 SkipUntil(tok::comma, true);
3022 return;
3023 }
3024
3025 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) {
3026 // Enumerations can't be explicitly instantiated.
3027 DS.SetTypeSpecError();
3028 Diag(StartLoc, diag::err_explicit_instantiation_enum);
3029 return;
3030 }
3031
3032 assert(TemplateInfo.TemplateParams && "no template parameters");
3033 TParams = MultiTemplateParamsArg(TemplateInfo.TemplateParams->data(),
3034 TemplateInfo.TemplateParams->size());
Douglas Gregor8fc6d232010-05-03 17:48:54 +00003035 }
Richard Smith1af83c42012-03-23 03:33:32 +00003036
Douglas Gregorb9075602011-02-22 02:55:24 +00003037 if (!Name && TUK != Sema::TUK_Definition) {
3038 Diag(Tok, diag::err_enumerator_unnamed_no_def);
Richard Smith1af83c42012-03-23 03:33:32 +00003039
Douglas Gregorb9075602011-02-22 02:55:24 +00003040 // Skip the rest of this declarator, up until the comma or semicolon.
3041 SkipUntil(tok::comma, true);
3042 return;
3043 }
Richard Smith1af83c42012-03-23 03:33:32 +00003044
Douglas Gregor402abb52009-05-28 23:31:59 +00003045 bool Owned = false;
John McCallc4e70192009-09-11 04:59:25 +00003046 bool IsDependent = false;
Douglas Gregor48c89f42010-04-24 16:38:41 +00003047 const char *PrevSpec = 0;
3048 unsigned DiagID;
John McCalld226f652010-08-21 09:40:31 +00003049 Decl *TagDecl = Actions.ActOnTag(getCurScope(), DeclSpec::TST_enum, TUK,
John McCall7f040a92010-12-24 02:08:15 +00003050 StartLoc, SS, Name, NameLoc, attrs.getList(),
Richard Smith1af83c42012-03-23 03:33:32 +00003051 AS, DS.getModulePrivateSpecLoc(), TParams,
Richard Smithbdad7a22012-01-10 01:33:14 +00003052 Owned, IsDependent, ScopedEnumKWLoc,
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00003053 IsScopedUsingClassTag, BaseType);
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003054
Douglas Gregor48c89f42010-04-24 16:38:41 +00003055 if (IsDependent) {
3056 // This enum has a dependent nested-name-specifier. Handle it as a
3057 // dependent tag.
3058 if (!Name) {
3059 DS.SetTypeSpecError();
3060 Diag(Tok, diag::err_expected_type_name_after_typename);
3061 return;
3062 }
3063
Douglas Gregor23c94db2010-07-02 17:43:08 +00003064 TypeResult Type = Actions.ActOnDependentTag(getCurScope(), DeclSpec::TST_enum,
Douglas Gregor48c89f42010-04-24 16:38:41 +00003065 TUK, SS, Name, StartLoc,
3066 NameLoc);
3067 if (Type.isInvalid()) {
3068 DS.SetTypeSpecError();
3069 return;
3070 }
3071
Abramo Bagnara0daaf322011-03-16 20:16:18 +00003072 if (DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc,
3073 NameLoc.isValid() ? NameLoc : StartLoc,
3074 PrevSpec, DiagID, Type.get()))
Douglas Gregor48c89f42010-04-24 16:38:41 +00003075 Diag(StartLoc, DiagID) << PrevSpec;
3076
3077 return;
3078 }
Mike Stump1eb44332009-09-09 15:08:12 +00003079
John McCalld226f652010-08-21 09:40:31 +00003080 if (!TagDecl) {
Douglas Gregor48c89f42010-04-24 16:38:41 +00003081 // The action failed to produce an enumeration tag. If this is a
3082 // definition, consume the entire definition.
Richard Smith7796eb52012-03-12 08:56:40 +00003083 if (Tok.is(tok::l_brace) && TUK != Sema::TUK_Reference) {
Douglas Gregor48c89f42010-04-24 16:38:41 +00003084 ConsumeBrace();
3085 SkipUntil(tok::r_brace);
3086 }
3087
3088 DS.SetTypeSpecError();
3089 return;
3090 }
Richard Smithbdad7a22012-01-10 01:33:14 +00003091
Richard Smith7796eb52012-03-12 08:56:40 +00003092 if (Tok.is(tok::l_brace) && TUK != Sema::TUK_Reference) {
John McCall13489672012-05-07 06:16:58 +00003093 ParseEnumBody(StartLoc, TagDecl);
Richard Smithbdad7a22012-01-10 01:33:14 +00003094 }
Mike Stump1eb44332009-09-09 15:08:12 +00003095
Abramo Bagnara0daaf322011-03-16 20:16:18 +00003096 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc,
3097 NameLoc.isValid() ? NameLoc : StartLoc,
3098 PrevSpec, DiagID, TagDecl, Owned))
John McCallfec54012009-08-03 20:12:06 +00003099 Diag(StartLoc, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00003100}
3101
3102/// ParseEnumBody - Parse a {} enclosed enumerator-list.
3103/// enumerator-list:
3104/// enumerator
3105/// enumerator-list ',' enumerator
3106/// enumerator:
3107/// enumeration-constant
3108/// enumeration-constant '=' constant-expression
3109/// enumeration-constant:
3110/// identifier
3111///
John McCalld226f652010-08-21 09:40:31 +00003112void Parser::ParseEnumBody(SourceLocation StartLoc, Decl *EnumDecl) {
Douglas Gregor074149e2009-01-05 19:45:36 +00003113 // Enter the scope of the enum body and start the definition.
3114 ParseScope EnumScope(this, Scope::DeclScope);
Douglas Gregor23c94db2010-07-02 17:43:08 +00003115 Actions.ActOnTagStartDefinition(getCurScope(), EnumDecl);
Douglas Gregor074149e2009-01-05 19:45:36 +00003116
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003117 BalancedDelimiterTracker T(*this, tok::l_brace);
3118 T.consumeOpen();
Mike Stump1eb44332009-09-09 15:08:12 +00003119
Chris Lattner7946dd32007-08-27 17:24:30 +00003120 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
David Blaikie4e4d0842012-03-11 07:00:24 +00003121 if (Tok.is(tok::r_brace) && !getLangOpts().CPlusPlus)
Fariborz Jahanian05115522010-05-28 22:23:22 +00003122 Diag(Tok, diag::error_empty_enum);
Mike Stump1eb44332009-09-09 15:08:12 +00003123
Chris Lattner5f9e2722011-07-23 10:55:15 +00003124 SmallVector<Decl *, 32> EnumConstantDecls;
Reid Spencer5f016e22007-07-11 17:01:13 +00003125
John McCalld226f652010-08-21 09:40:31 +00003126 Decl *LastEnumConstDecl = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00003127
Reid Spencer5f016e22007-07-11 17:01:13 +00003128 // Parse the enumerator-list.
Chris Lattner04d66662007-10-09 17:33:22 +00003129 while (Tok.is(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003130 IdentifierInfo *Ident = Tok.getIdentifierInfo();
3131 SourceLocation IdentLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00003132
John McCall5b629aa2010-10-22 23:36:17 +00003133 // If attributes exist after the enumerator, parse them.
John McCall0b7e6782011-03-24 11:26:52 +00003134 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00003135 MaybeParseGNUAttributes(attrs);
John McCall5b629aa2010-10-22 23:36:17 +00003136
Reid Spencer5f016e22007-07-11 17:01:13 +00003137 SourceLocation EqualLoc;
John McCall60d7b3a2010-08-24 06:29:42 +00003138 ExprResult AssignedVal;
John McCall92576642012-05-07 06:16:41 +00003139 ParsingDeclRAIIObject PD(*this, ParsingDeclRAIIObject::NoParent);
Fariborz Jahanian5a477db2011-12-09 01:15:54 +00003140
Chris Lattner04d66662007-10-09 17:33:22 +00003141 if (Tok.is(tok::equal)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003142 EqualLoc = ConsumeToken();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00003143 AssignedVal = ParseConstantExpression();
3144 if (AssignedVal.isInvalid())
Reid Spencer5f016e22007-07-11 17:01:13 +00003145 SkipUntil(tok::comma, tok::r_brace, true, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00003146 }
Mike Stump1eb44332009-09-09 15:08:12 +00003147
Reid Spencer5f016e22007-07-11 17:01:13 +00003148 // Install the enumerator constant into EnumDecl.
John McCalld226f652010-08-21 09:40:31 +00003149 Decl *EnumConstDecl = Actions.ActOnEnumConstant(getCurScope(), EnumDecl,
3150 LastEnumConstDecl,
3151 IdentLoc, Ident,
John McCall7f040a92010-12-24 02:08:15 +00003152 attrs.getList(), EqualLoc,
John McCalld226f652010-08-21 09:40:31 +00003153 AssignedVal.release());
Fariborz Jahanian5a477db2011-12-09 01:15:54 +00003154 PD.complete(EnumConstDecl);
3155
Reid Spencer5f016e22007-07-11 17:01:13 +00003156 EnumConstantDecls.push_back(EnumConstDecl);
3157 LastEnumConstDecl = EnumConstDecl;
Mike Stump1eb44332009-09-09 15:08:12 +00003158
Douglas Gregor751f6922010-09-07 14:51:08 +00003159 if (Tok.is(tok::identifier)) {
3160 // We're missing a comma between enumerators.
3161 SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation);
3162 Diag(Loc, diag::err_enumerator_list_missing_comma)
3163 << FixItHint::CreateInsertion(Loc, ", ");
3164 continue;
3165 }
3166
Chris Lattner04d66662007-10-09 17:33:22 +00003167 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +00003168 break;
3169 SourceLocation CommaLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00003170
Richard Smith7fe62082011-10-15 05:09:34 +00003171 if (Tok.isNot(tok::identifier)) {
David Blaikie4e4d0842012-03-11 07:00:24 +00003172 if (!getLangOpts().C99 && !getLangOpts().CPlusPlus0x)
Richard Smith7fe62082011-10-15 05:09:34 +00003173 Diag(CommaLoc, diag::ext_enumerator_list_comma)
David Blaikie4e4d0842012-03-11 07:00:24 +00003174 << getLangOpts().CPlusPlus
Richard Smith7fe62082011-10-15 05:09:34 +00003175 << FixItHint::CreateRemoval(CommaLoc);
David Blaikie4e4d0842012-03-11 07:00:24 +00003176 else if (getLangOpts().CPlusPlus0x)
Richard Smith7fe62082011-10-15 05:09:34 +00003177 Diag(CommaLoc, diag::warn_cxx98_compat_enumerator_list_comma)
3178 << FixItHint::CreateRemoval(CommaLoc);
3179 }
Reid Spencer5f016e22007-07-11 17:01:13 +00003180 }
Mike Stump1eb44332009-09-09 15:08:12 +00003181
Reid Spencer5f016e22007-07-11 17:01:13 +00003182 // Eat the }.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003183 T.consumeClose();
Reid Spencer5f016e22007-07-11 17:01:13 +00003184
Reid Spencer5f016e22007-07-11 17:01:13 +00003185 // If attributes exist after the identifier list, parse them.
John McCall0b7e6782011-03-24 11:26:52 +00003186 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00003187 MaybeParseGNUAttributes(attrs);
Douglas Gregor72de6672009-01-08 20:45:30 +00003188
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003189 Actions.ActOnEnumBody(StartLoc, T.getOpenLocation(), T.getCloseLocation(),
3190 EnumDecl, EnumConstantDecls.data(),
3191 EnumConstantDecls.size(), getCurScope(),
3192 attrs.getList());
Mike Stump1eb44332009-09-09 15:08:12 +00003193
Douglas Gregor72de6672009-01-08 20:45:30 +00003194 EnumScope.Exit();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003195 Actions.ActOnTagFinishDefinition(getCurScope(), EnumDecl,
3196 T.getCloseLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00003197}
3198
3199/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff5f8aa692008-02-11 23:15:56 +00003200/// start of a type-qualifier-list.
3201bool Parser::isTypeQualifier() const {
3202 switch (Tok.getKind()) {
3203 default: return false;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003204
3205 // type-qualifier only in OpenCL
3206 case tok::kw_private:
David Blaikie4e4d0842012-03-11 07:00:24 +00003207 return getLangOpts().OpenCL;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003208
Steve Naroff5f8aa692008-02-11 23:15:56 +00003209 // type-qualifier
3210 case tok::kw_const:
3211 case tok::kw_volatile:
3212 case tok::kw_restrict:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003213 case tok::kw___private:
3214 case tok::kw___local:
3215 case tok::kw___global:
3216 case tok::kw___constant:
3217 case tok::kw___read_only:
3218 case tok::kw___read_write:
3219 case tok::kw___write_only:
Steve Naroff5f8aa692008-02-11 23:15:56 +00003220 return true;
3221 }
3222}
3223
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003224/// isKnownToBeTypeSpecifier - Return true if we know that the specified token
3225/// is definitely a type-specifier. Return false if it isn't part of a type
3226/// specifier or if we're not sure.
3227bool Parser::isKnownToBeTypeSpecifier(const Token &Tok) const {
3228 switch (Tok.getKind()) {
3229 default: return false;
3230 // type-specifiers
3231 case tok::kw_short:
3232 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00003233 case tok::kw___int64:
Richard Smith5a5a9712012-04-04 06:24:32 +00003234 case tok::kw___int128:
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003235 case tok::kw_signed:
3236 case tok::kw_unsigned:
3237 case tok::kw__Complex:
3238 case tok::kw__Imaginary:
3239 case tok::kw_void:
3240 case tok::kw_char:
3241 case tok::kw_wchar_t:
3242 case tok::kw_char16_t:
3243 case tok::kw_char32_t:
3244 case tok::kw_int:
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00003245 case tok::kw_half:
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003246 case tok::kw_float:
3247 case tok::kw_double:
3248 case tok::kw_bool:
3249 case tok::kw__Bool:
3250 case tok::kw__Decimal32:
3251 case tok::kw__Decimal64:
3252 case tok::kw__Decimal128:
3253 case tok::kw___vector:
3254
3255 // struct-or-union-specifier (C99) or class-specifier (C++)
3256 case tok::kw_class:
3257 case tok::kw_struct:
3258 case tok::kw_union:
3259 // enum-specifier
3260 case tok::kw_enum:
3261
3262 // typedef-name
3263 case tok::annot_typename:
3264 return true;
3265 }
3266}
3267
Steve Naroff5f8aa692008-02-11 23:15:56 +00003268/// isTypeSpecifierQualifier - Return true if the current token could be the
Reid Spencer5f016e22007-07-11 17:01:13 +00003269/// start of a specifier-qualifier-list.
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003270bool Parser::isTypeSpecifierQualifier() {
Reid Spencer5f016e22007-07-11 17:01:13 +00003271 switch (Tok.getKind()) {
3272 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00003273
Chris Lattner166a8fc2009-01-04 23:41:41 +00003274 case tok::identifier: // foo::bar
John Thompson82287d12010-02-05 00:12:22 +00003275 if (TryAltiVecVectorToken())
3276 return true;
3277 // Fall through.
Douglas Gregord57959a2009-03-27 23:10:48 +00003278 case tok::kw_typename: // typename T::type
Chris Lattner166a8fc2009-01-04 23:41:41 +00003279 // Annotate typenames and C++ scope specifiers. If we get one, just
3280 // recurse to handle whatever we get.
3281 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003282 return true;
3283 if (Tok.is(tok::identifier))
3284 return false;
3285 return isTypeSpecifierQualifier();
Douglas Gregord57959a2009-03-27 23:10:48 +00003286
Chris Lattner166a8fc2009-01-04 23:41:41 +00003287 case tok::coloncolon: // ::foo::bar
3288 if (NextToken().is(tok::kw_new) || // ::new
3289 NextToken().is(tok::kw_delete)) // ::delete
3290 return false;
3291
Chris Lattner166a8fc2009-01-04 23:41:41 +00003292 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003293 return true;
3294 return isTypeSpecifierQualifier();
Mike Stump1eb44332009-09-09 15:08:12 +00003295
Reid Spencer5f016e22007-07-11 17:01:13 +00003296 // GNU attributes support.
3297 case tok::kw___attribute:
Steve Naroffd1861fd2007-07-31 12:34:36 +00003298 // GNU typeof support.
3299 case tok::kw_typeof:
Mike Stump1eb44332009-09-09 15:08:12 +00003300
Reid Spencer5f016e22007-07-11 17:01:13 +00003301 // type-specifiers
3302 case tok::kw_short:
3303 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00003304 case tok::kw___int64:
Richard Smith5a5a9712012-04-04 06:24:32 +00003305 case tok::kw___int128:
Reid Spencer5f016e22007-07-11 17:01:13 +00003306 case tok::kw_signed:
3307 case tok::kw_unsigned:
3308 case tok::kw__Complex:
3309 case tok::kw__Imaginary:
3310 case tok::kw_void:
3311 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00003312 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00003313 case tok::kw_char16_t:
3314 case tok::kw_char32_t:
Reid Spencer5f016e22007-07-11 17:01:13 +00003315 case tok::kw_int:
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00003316 case tok::kw_half:
Reid Spencer5f016e22007-07-11 17:01:13 +00003317 case tok::kw_float:
3318 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00003319 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00003320 case tok::kw__Bool:
3321 case tok::kw__Decimal32:
3322 case tok::kw__Decimal64:
3323 case tok::kw__Decimal128:
John Thompson82287d12010-02-05 00:12:22 +00003324 case tok::kw___vector:
Mike Stump1eb44332009-09-09 15:08:12 +00003325
Chris Lattner99dc9142008-04-13 18:59:07 +00003326 // struct-or-union-specifier (C99) or class-specifier (C++)
3327 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00003328 case tok::kw_struct:
3329 case tok::kw_union:
3330 // enum-specifier
3331 case tok::kw_enum:
Mike Stump1eb44332009-09-09 15:08:12 +00003332
Reid Spencer5f016e22007-07-11 17:01:13 +00003333 // type-qualifier
3334 case tok::kw_const:
3335 case tok::kw_volatile:
3336 case tok::kw_restrict:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003337
3338 // typedef-name
Chris Lattnerb31757b2009-01-06 05:06:21 +00003339 case tok::annot_typename:
Reid Spencer5f016e22007-07-11 17:01:13 +00003340 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00003341
Chris Lattner7c186be2008-10-20 00:25:30 +00003342 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
3343 case tok::less:
David Blaikie4e4d0842012-03-11 07:00:24 +00003344 return getLangOpts().ObjC1;
Mike Stump1eb44332009-09-09 15:08:12 +00003345
Steve Naroff239f0732008-12-25 14:16:32 +00003346 case tok::kw___cdecl:
3347 case tok::kw___stdcall:
3348 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00003349 case tok::kw___thiscall:
Eli Friedman290eeb02009-06-08 23:27:34 +00003350 case tok::kw___w64:
3351 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00003352 case tok::kw___ptr32:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003353 case tok::kw___pascal:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00003354 case tok::kw___unaligned:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003355
3356 case tok::kw___private:
3357 case tok::kw___local:
3358 case tok::kw___global:
3359 case tok::kw___constant:
3360 case tok::kw___read_only:
3361 case tok::kw___read_write:
3362 case tok::kw___write_only:
3363
Eli Friedman290eeb02009-06-08 23:27:34 +00003364 return true;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003365
3366 case tok::kw_private:
David Blaikie4e4d0842012-03-11 07:00:24 +00003367 return getLangOpts().OpenCL;
Eli Friedmanb001de72011-10-06 23:00:33 +00003368
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00003369 // C11 _Atomic()
Eli Friedmanb001de72011-10-06 23:00:33 +00003370 case tok::kw__Atomic:
3371 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00003372 }
3373}
3374
3375/// isDeclarationSpecifier() - Return true if the current token is part of a
3376/// declaration specifier.
Douglas Gregor9497a732010-09-16 01:51:54 +00003377///
3378/// \param DisambiguatingWithExpression True to indicate that the purpose of
3379/// this check is to disambiguate between an expression and a declaration.
3380bool Parser::isDeclarationSpecifier(bool DisambiguatingWithExpression) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003381 switch (Tok.getKind()) {
3382 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00003383
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003384 case tok::kw_private:
David Blaikie4e4d0842012-03-11 07:00:24 +00003385 return getLangOpts().OpenCL;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003386
Chris Lattner166a8fc2009-01-04 23:41:41 +00003387 case tok::identifier: // foo::bar
Steve Naroff61f72cb2009-03-09 21:12:44 +00003388 // Unfortunate hack to support "Class.factoryMethod" notation.
David Blaikie4e4d0842012-03-11 07:00:24 +00003389 if (getLangOpts().ObjC1 && NextToken().is(tok::period))
Steve Naroff61f72cb2009-03-09 21:12:44 +00003390 return false;
John Thompson82287d12010-02-05 00:12:22 +00003391 if (TryAltiVecVectorToken())
3392 return true;
3393 // Fall through.
David Blaikie42d6d0c2011-12-04 05:04:18 +00003394 case tok::kw_decltype: // decltype(T())::type
Douglas Gregord57959a2009-03-27 23:10:48 +00003395 case tok::kw_typename: // typename T::type
Chris Lattner166a8fc2009-01-04 23:41:41 +00003396 // Annotate typenames and C++ scope specifiers. If we get one, just
3397 // recurse to handle whatever we get.
3398 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003399 return true;
3400 if (Tok.is(tok::identifier))
3401 return false;
Douglas Gregor9497a732010-09-16 01:51:54 +00003402
3403 // If we're in Objective-C and we have an Objective-C class type followed
3404 // by an identifier and then either ':' or ']', in a place where an
3405 // expression is permitted, then this is probably a class message send
3406 // missing the initial '['. In this case, we won't consider this to be
3407 // the start of a declaration.
3408 if (DisambiguatingWithExpression &&
3409 isStartOfObjCClassMessageMissingOpenBracket())
3410 return false;
3411
John McCall9ba61662010-02-26 08:45:28 +00003412 return isDeclarationSpecifier();
3413
Chris Lattner166a8fc2009-01-04 23:41:41 +00003414 case tok::coloncolon: // ::foo::bar
3415 if (NextToken().is(tok::kw_new) || // ::new
3416 NextToken().is(tok::kw_delete)) // ::delete
3417 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00003418
Chris Lattner166a8fc2009-01-04 23:41:41 +00003419 // Annotate typenames and C++ scope specifiers. If we get one, just
3420 // recurse to handle whatever we get.
3421 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003422 return true;
3423 return isDeclarationSpecifier();
Mike Stump1eb44332009-09-09 15:08:12 +00003424
Reid Spencer5f016e22007-07-11 17:01:13 +00003425 // storage-class-specifier
3426 case tok::kw_typedef:
3427 case tok::kw_extern:
Steve Naroff8d54bf22007-12-18 00:16:02 +00003428 case tok::kw___private_extern__:
Reid Spencer5f016e22007-07-11 17:01:13 +00003429 case tok::kw_static:
3430 case tok::kw_auto:
3431 case tok::kw_register:
3432 case tok::kw___thread:
Mike Stump1eb44332009-09-09 15:08:12 +00003433
Douglas Gregor8d267c52011-09-09 02:06:17 +00003434 // Modules
3435 case tok::kw___module_private__:
3436
Reid Spencer5f016e22007-07-11 17:01:13 +00003437 // type-specifiers
3438 case tok::kw_short:
3439 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00003440 case tok::kw___int64:
Richard Smith5a5a9712012-04-04 06:24:32 +00003441 case tok::kw___int128:
Reid Spencer5f016e22007-07-11 17:01:13 +00003442 case tok::kw_signed:
3443 case tok::kw_unsigned:
3444 case tok::kw__Complex:
3445 case tok::kw__Imaginary:
3446 case tok::kw_void:
3447 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00003448 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00003449 case tok::kw_char16_t:
3450 case tok::kw_char32_t:
3451
Reid Spencer5f016e22007-07-11 17:01:13 +00003452 case tok::kw_int:
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00003453 case tok::kw_half:
Reid Spencer5f016e22007-07-11 17:01:13 +00003454 case tok::kw_float:
3455 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00003456 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00003457 case tok::kw__Bool:
3458 case tok::kw__Decimal32:
3459 case tok::kw__Decimal64:
3460 case tok::kw__Decimal128:
John Thompson82287d12010-02-05 00:12:22 +00003461 case tok::kw___vector:
Mike Stump1eb44332009-09-09 15:08:12 +00003462
Chris Lattner99dc9142008-04-13 18:59:07 +00003463 // struct-or-union-specifier (C99) or class-specifier (C++)
3464 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00003465 case tok::kw_struct:
3466 case tok::kw_union:
3467 // enum-specifier
3468 case tok::kw_enum:
Mike Stump1eb44332009-09-09 15:08:12 +00003469
Reid Spencer5f016e22007-07-11 17:01:13 +00003470 // type-qualifier
3471 case tok::kw_const:
3472 case tok::kw_volatile:
3473 case tok::kw_restrict:
Steve Naroffd1861fd2007-07-31 12:34:36 +00003474
Reid Spencer5f016e22007-07-11 17:01:13 +00003475 // function-specifier
3476 case tok::kw_inline:
Douglas Gregorb48fe382008-10-31 09:07:45 +00003477 case tok::kw_virtual:
3478 case tok::kw_explicit:
Chris Lattnerd6c7c182007-08-09 16:40:21 +00003479
Peter Collingbournec6eb44b2011-04-15 00:35:57 +00003480 // static_assert-declaration
3481 case tok::kw__Static_assert:
3482
Chris Lattner1ef08762007-08-09 17:01:07 +00003483 // GNU typeof support.
3484 case tok::kw_typeof:
Mike Stump1eb44332009-09-09 15:08:12 +00003485
Chris Lattner1ef08762007-08-09 17:01:07 +00003486 // GNU attributes.
Chris Lattnerd6c7c182007-08-09 16:40:21 +00003487 case tok::kw___attribute:
Reid Spencer5f016e22007-07-11 17:01:13 +00003488 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00003489
Francois Pichete3d49b42011-06-19 08:02:06 +00003490 // C++0x decltype.
David Blaikie42d6d0c2011-12-04 05:04:18 +00003491 case tok::annot_decltype:
Francois Pichete3d49b42011-06-19 08:02:06 +00003492 return true;
3493
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00003494 // C11 _Atomic()
Eli Friedmanb001de72011-10-06 23:00:33 +00003495 case tok::kw__Atomic:
3496 return true;
3497
Chris Lattnerf3948c42008-07-26 03:38:44 +00003498 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
3499 case tok::less:
David Blaikie4e4d0842012-03-11 07:00:24 +00003500 return getLangOpts().ObjC1;
Mike Stump1eb44332009-09-09 15:08:12 +00003501
Douglas Gregord9d75e52011-04-27 05:41:15 +00003502 // typedef-name
3503 case tok::annot_typename:
3504 return !DisambiguatingWithExpression ||
3505 !isStartOfObjCClassMessageMissingOpenBracket();
3506
Steve Naroff47f52092009-01-06 19:34:12 +00003507 case tok::kw___declspec:
Steve Naroff239f0732008-12-25 14:16:32 +00003508 case tok::kw___cdecl:
3509 case tok::kw___stdcall:
3510 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00003511 case tok::kw___thiscall:
Eli Friedman290eeb02009-06-08 23:27:34 +00003512 case tok::kw___w64:
3513 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00003514 case tok::kw___ptr32:
Eli Friedman290eeb02009-06-08 23:27:34 +00003515 case tok::kw___forceinline:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003516 case tok::kw___pascal:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00003517 case tok::kw___unaligned:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003518
3519 case tok::kw___private:
3520 case tok::kw___local:
3521 case tok::kw___global:
3522 case tok::kw___constant:
3523 case tok::kw___read_only:
3524 case tok::kw___read_write:
3525 case tok::kw___write_only:
3526
Eli Friedman290eeb02009-06-08 23:27:34 +00003527 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00003528 }
3529}
3530
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003531bool Parser::isConstructorDeclarator() {
3532 TentativeParsingAction TPA(*this);
3533
3534 // Parse the C++ scope specifier.
3535 CXXScopeSpec SS;
Douglas Gregorefaa93a2011-11-07 17:33:42 +00003536 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
3537 /*EnteringContext=*/true)) {
John McCall9ba61662010-02-26 08:45:28 +00003538 TPA.Revert();
3539 return false;
3540 }
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003541
3542 // Parse the constructor name.
3543 if (Tok.is(tok::identifier) || Tok.is(tok::annot_template_id)) {
3544 // We already know that we have a constructor name; just consume
3545 // the token.
3546 ConsumeToken();
3547 } else {
3548 TPA.Revert();
3549 return false;
3550 }
3551
Richard Smith22592862012-03-27 23:05:05 +00003552 // Current class name must be followed by a left parenthesis.
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003553 if (Tok.isNot(tok::l_paren)) {
3554 TPA.Revert();
3555 return false;
3556 }
3557 ConsumeParen();
3558
Richard Smith22592862012-03-27 23:05:05 +00003559 // A right parenthesis, or ellipsis followed by a right parenthesis signals
3560 // that we have a constructor.
3561 if (Tok.is(tok::r_paren) ||
3562 (Tok.is(tok::ellipsis) && NextToken().is(tok::r_paren))) {
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003563 TPA.Revert();
3564 return true;
3565 }
3566
3567 // If we need to, enter the specified scope.
3568 DeclaratorScopeObj DeclScopeObj(*this, SS);
Douglas Gregor23c94db2010-07-02 17:43:08 +00003569 if (SS.isSet() && Actions.ShouldEnterDeclaratorScope(getCurScope(), SS))
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003570 DeclScopeObj.EnterDeclaratorScope();
3571
Francois Pichetdfaa5fb2011-01-31 04:54:32 +00003572 // Optionally skip Microsoft attributes.
John McCall0b7e6782011-03-24 11:26:52 +00003573 ParsedAttributes Attrs(AttrFactory);
Francois Pichetdfaa5fb2011-01-31 04:54:32 +00003574 MaybeParseMicrosoftAttributes(Attrs);
3575
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003576 // Check whether the next token(s) are part of a declaration
3577 // specifier, in which case we have the start of a parameter and,
3578 // therefore, we know that this is a constructor.
Richard Smith412e0cc2012-03-27 00:56:56 +00003579 bool IsConstructor = false;
3580 if (isDeclarationSpecifier())
3581 IsConstructor = true;
3582 else if (Tok.is(tok::identifier) ||
3583 (Tok.is(tok::annot_cxxscope) && NextToken().is(tok::identifier))) {
3584 // We've seen "C ( X" or "C ( X::Y", but "X" / "X::Y" is not a type.
3585 // This might be a parenthesized member name, but is more likely to
3586 // be a constructor declaration with an invalid argument type. Keep
3587 // looking.
3588 if (Tok.is(tok::annot_cxxscope))
3589 ConsumeToken();
3590 ConsumeToken();
3591
3592 // If this is not a constructor, we must be parsing a declarator,
Richard Smith5d8388c2012-03-27 01:42:32 +00003593 // which must have one of the following syntactic forms (see the
3594 // grammar extract at the start of ParseDirectDeclarator):
Richard Smith412e0cc2012-03-27 00:56:56 +00003595 switch (Tok.getKind()) {
3596 case tok::l_paren:
3597 // C(X ( int));
3598 case tok::l_square:
3599 // C(X [ 5]);
3600 // C(X [ [attribute]]);
3601 case tok::coloncolon:
3602 // C(X :: Y);
3603 // C(X :: *p);
3604 case tok::r_paren:
3605 // C(X )
3606 // Assume this isn't a constructor, rather than assuming it's a
3607 // constructor with an unnamed parameter of an ill-formed type.
3608 break;
3609
3610 default:
3611 IsConstructor = true;
3612 break;
3613 }
3614 }
3615
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003616 TPA.Revert();
3617 return IsConstructor;
3618}
Reid Spencer5f016e22007-07-11 17:01:13 +00003619
3620/// ParseTypeQualifierListOpt
Dawn Perchik52fc3142010-09-03 01:29:35 +00003621/// type-qualifier-list: [C99 6.7.5]
3622/// type-qualifier
3623/// [vendor] attributes
3624/// [ only if VendorAttributesAllowed=true ]
3625/// type-qualifier-list type-qualifier
3626/// [vendor] type-qualifier-list attributes
3627/// [ only if VendorAttributesAllowed=true ]
3628/// [C++0x] attribute-specifier[opt] is allowed before cv-qualifier-seq
3629/// [ only if CXX0XAttributesAllowed=true ]
3630/// Note: vendor can be GNU, MS, etc.
Reid Spencer5f016e22007-07-11 17:01:13 +00003631///
Dawn Perchik52fc3142010-09-03 01:29:35 +00003632void Parser::ParseTypeQualifierListOpt(DeclSpec &DS,
3633 bool VendorAttributesAllowed,
Richard Smithc56298d2012-04-10 03:25:07 +00003634 bool CXX11AttributesAllowed) {
3635 if (getLangOpts().CPlusPlus0x && CXX11AttributesAllowed &&
Richard Smith6ee326a2012-04-10 01:32:12 +00003636 isCXX11AttributeSpecifier()) {
John McCall0b7e6782011-03-24 11:26:52 +00003637 ParsedAttributesWithRange attrs(AttrFactory);
Richard Smithc56298d2012-04-10 03:25:07 +00003638 ParseCXX11Attributes(attrs);
Richard Smith6ee326a2012-04-10 01:32:12 +00003639 DS.takeAttributesFrom(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00003640 }
Abramo Bagnara796aa442011-03-12 11:17:06 +00003641
3642 SourceLocation EndLoc;
3643
Reid Spencer5f016e22007-07-11 17:01:13 +00003644 while (1) {
John McCallfec54012009-08-03 20:12:06 +00003645 bool isInvalid = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00003646 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00003647 unsigned DiagID = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00003648 SourceLocation Loc = Tok.getLocation();
3649
3650 switch (Tok.getKind()) {
Douglas Gregor1a480c42010-08-27 17:35:51 +00003651 case tok::code_completion:
3652 Actions.CodeCompleteTypeQualifiers(DS);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00003653 return cutOffParsing();
Douglas Gregor1a480c42010-08-27 17:35:51 +00003654
Reid Spencer5f016e22007-07-11 17:01:13 +00003655 case tok::kw_const:
John McCallfec54012009-08-03 20:12:06 +00003656 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, DiagID,
David Blaikie4e4d0842012-03-11 07:00:24 +00003657 getLangOpts());
Reid Spencer5f016e22007-07-11 17:01:13 +00003658 break;
3659 case tok::kw_volatile:
John McCallfec54012009-08-03 20:12:06 +00003660 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
David Blaikie4e4d0842012-03-11 07:00:24 +00003661 getLangOpts());
Reid Spencer5f016e22007-07-11 17:01:13 +00003662 break;
3663 case tok::kw_restrict:
John McCallfec54012009-08-03 20:12:06 +00003664 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
David Blaikie4e4d0842012-03-11 07:00:24 +00003665 getLangOpts());
Reid Spencer5f016e22007-07-11 17:01:13 +00003666 break;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003667
3668 // OpenCL qualifiers:
3669 case tok::kw_private:
David Blaikie4e4d0842012-03-11 07:00:24 +00003670 if (!getLangOpts().OpenCL)
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003671 goto DoneWithTypeQuals;
3672 case tok::kw___private:
3673 case tok::kw___global:
3674 case tok::kw___local:
3675 case tok::kw___constant:
3676 case tok::kw___read_only:
3677 case tok::kw___write_only:
3678 case tok::kw___read_write:
3679 ParseOpenCLQualifiers(DS);
3680 break;
3681
Eli Friedman290eeb02009-06-08 23:27:34 +00003682 case tok::kw___w64:
Steve Naroff86bc6cf2008-12-25 14:41:26 +00003683 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00003684 case tok::kw___ptr32:
Steve Naroff239f0732008-12-25 14:16:32 +00003685 case tok::kw___cdecl:
3686 case tok::kw___stdcall:
3687 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00003688 case tok::kw___thiscall:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00003689 case tok::kw___unaligned:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003690 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00003691 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman290eeb02009-06-08 23:27:34 +00003692 continue;
3693 }
3694 goto DoneWithTypeQuals;
Dawn Perchik52fc3142010-09-03 01:29:35 +00003695 case tok::kw___pascal:
3696 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00003697 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik52fc3142010-09-03 01:29:35 +00003698 continue;
3699 }
3700 goto DoneWithTypeQuals;
Reid Spencer5f016e22007-07-11 17:01:13 +00003701 case tok::kw___attribute:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003702 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00003703 ParseGNUAttributes(DS.getAttributes());
Chris Lattner5a69d1c2008-12-18 07:02:59 +00003704 continue; // do *not* consume the next token!
3705 }
3706 // otherwise, FALL THROUGH!
3707 default:
Steve Naroff239f0732008-12-25 14:16:32 +00003708 DoneWithTypeQuals:
Chris Lattner5a69d1c2008-12-18 07:02:59 +00003709 // If this is not a type-qualifier token, we're done reading type
3710 // qualifiers. First verify that DeclSpec's are consistent.
Douglas Gregor9b3064b2009-04-01 22:41:11 +00003711 DS.Finish(Diags, PP);
Abramo Bagnara796aa442011-03-12 11:17:06 +00003712 if (EndLoc.isValid())
3713 DS.SetRangeEnd(EndLoc);
Chris Lattner5a69d1c2008-12-18 07:02:59 +00003714 return;
Reid Spencer5f016e22007-07-11 17:01:13 +00003715 }
Chris Lattnera1fcbad2008-12-18 06:50:14 +00003716
Reid Spencer5f016e22007-07-11 17:01:13 +00003717 // If the specifier combination wasn't legal, issue a diagnostic.
3718 if (isInvalid) {
3719 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner1ab3b962008-11-18 07:48:38 +00003720 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00003721 }
Abramo Bagnara796aa442011-03-12 11:17:06 +00003722 EndLoc = ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00003723 }
3724}
3725
3726
3727/// ParseDeclarator - Parse and verify a newly-initialized declarator.
3728///
3729void Parser::ParseDeclarator(Declarator &D) {
3730 /// This implements the 'declarator' production in the C grammar, then checks
3731 /// for well-formedness and issues diagnostics.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003732 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Reid Spencer5f016e22007-07-11 17:01:13 +00003733}
3734
Richard Smith9988f282012-03-29 01:16:42 +00003735static bool isPtrOperatorToken(tok::TokenKind Kind, const LangOptions &Lang) {
3736 if (Kind == tok::star || Kind == tok::caret)
3737 return true;
3738
3739 // We parse rvalue refs in C++03, because otherwise the errors are scary.
3740 if (!Lang.CPlusPlus)
3741 return false;
3742
3743 return Kind == tok::amp || Kind == tok::ampamp;
3744}
3745
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003746/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
3747/// is parsed by the function passed to it. Pass null, and the direct-declarator
3748/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003749/// ptr-operator production.
3750///
Richard Smith0706df42011-10-19 21:33:05 +00003751/// If the grammar of this construct is extended, matching changes must also be
Richard Smith5d8388c2012-03-27 01:42:32 +00003752/// made to TryParseDeclarator and MightBeDeclarator, and possibly to
3753/// isConstructorDeclarator.
Richard Smith0706df42011-10-19 21:33:05 +00003754///
Sebastian Redlf30208a2009-01-24 21:16:55 +00003755/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
3756/// [C] pointer[opt] direct-declarator
3757/// [C++] direct-declarator
3758/// [C++] ptr-operator declarator
Reid Spencer5f016e22007-07-11 17:01:13 +00003759///
3760/// pointer: [C99 6.7.5]
3761/// '*' type-qualifier-list[opt]
3762/// '*' type-qualifier-list[opt] pointer
3763///
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003764/// ptr-operator:
3765/// '*' cv-qualifier-seq[opt]
3766/// '&'
Sebastian Redl05532f22009-03-15 22:02:01 +00003767/// [C++0x] '&&'
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003768/// [GNU] '&' restrict[opt] attributes[opt]
Sebastian Redl05532f22009-03-15 22:02:01 +00003769/// [GNU?] '&&' restrict[opt] attributes[opt]
Sebastian Redlf30208a2009-01-24 21:16:55 +00003770/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003771void Parser::ParseDeclaratorInternal(Declarator &D,
3772 DirectDeclParseFunction DirectDeclParser) {
Douglas Gregor91a28862009-08-26 14:27:30 +00003773 if (Diags.hasAllExtensionsSilenced())
3774 D.setExtension();
Douglas Gregor2ccccb32010-08-23 18:23:48 +00003775
Sebastian Redlf30208a2009-01-24 21:16:55 +00003776 // C++ member pointers start with a '::' or a nested-name.
3777 // Member pointers get special handling, since there's no place for the
3778 // scope spec in the generic path below.
David Blaikie4e4d0842012-03-11 07:00:24 +00003779 if (getLangOpts().CPlusPlus &&
Chris Lattnerf919bfe2009-03-24 17:04:48 +00003780 (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) ||
3781 Tok.is(tok::annot_cxxscope))) {
Douglas Gregorefaa93a2011-11-07 17:33:42 +00003782 bool EnteringContext = D.getContext() == Declarator::FileContext ||
3783 D.getContext() == Declarator::MemberContext;
Sebastian Redlf30208a2009-01-24 21:16:55 +00003784 CXXScopeSpec SS;
Douglas Gregorefaa93a2011-11-07 17:33:42 +00003785 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), EnteringContext);
John McCall9ba61662010-02-26 08:45:28 +00003786
Jeffrey Yasskinedc28772010-04-07 23:29:58 +00003787 if (SS.isNotEmpty()) {
Mike Stump1eb44332009-09-09 15:08:12 +00003788 if (Tok.isNot(tok::star)) {
Sebastian Redlf30208a2009-01-24 21:16:55 +00003789 // The scope spec really belongs to the direct-declarator.
3790 D.getCXXScopeSpec() = SS;
3791 if (DirectDeclParser)
3792 (this->*DirectDeclParser)(D);
3793 return;
3794 }
3795
3796 SourceLocation Loc = ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00003797 D.SetRangeEnd(Loc);
John McCall0b7e6782011-03-24 11:26:52 +00003798 DeclSpec DS(AttrFactory);
Sebastian Redlf30208a2009-01-24 21:16:55 +00003799 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00003800 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00003801
3802 // Recurse to parse whatever is left.
3803 ParseDeclaratorInternal(D, DirectDeclParser);
3804
3805 // Sema will have to catch (syntactically invalid) pointers into global
3806 // scope. It has to catch pointers into namespace scope anyway.
3807 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
John McCall0b7e6782011-03-24 11:26:52 +00003808 Loc),
3809 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003810 /* Don't replace range end. */SourceLocation());
Sebastian Redlf30208a2009-01-24 21:16:55 +00003811 return;
3812 }
3813 }
3814
3815 tok::TokenKind Kind = Tok.getKind();
Steve Naroff5618bd42008-08-27 16:04:49 +00003816 // Not a pointer, C++ reference, or block.
Richard Smith9988f282012-03-29 01:16:42 +00003817 if (!isPtrOperatorToken(Kind, getLangOpts())) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003818 if (DirectDeclParser)
3819 (this->*DirectDeclParser)(D);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003820 return;
3821 }
Sebastian Redlf30208a2009-01-24 21:16:55 +00003822
Sebastian Redl05532f22009-03-15 22:02:01 +00003823 // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference,
3824 // '&&' -> rvalue reference
Sebastian Redl743de1f2009-03-23 00:00:23 +00003825 SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&.
Sebastian Redlab197ba2009-02-09 18:23:29 +00003826 D.SetRangeEnd(Loc);
Reid Spencer5f016e22007-07-11 17:01:13 +00003827
Chris Lattner9af55002009-03-27 04:18:06 +00003828 if (Kind == tok::star || Kind == tok::caret) {
Chris Lattner76549142008-02-21 01:32:26 +00003829 // Is a pointer.
John McCall0b7e6782011-03-24 11:26:52 +00003830 DeclSpec DS(AttrFactory);
Sebastian Redlf30208a2009-01-24 21:16:55 +00003831
Richard Smith6ee326a2012-04-10 01:32:12 +00003832 // FIXME: GNU attributes are not allowed here in a new-type-id.
Reid Spencer5f016e22007-07-11 17:01:13 +00003833 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00003834 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00003835
Reid Spencer5f016e22007-07-11 17:01:13 +00003836 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003837 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroff5618bd42008-08-27 16:04:49 +00003838 if (Kind == tok::star)
3839 // Remember that we parsed a pointer type, and remember the type-quals.
3840 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
Chandler Carruthd067c072011-02-23 18:51:59 +00003841 DS.getConstSpecLoc(),
3842 DS.getVolatileSpecLoc(),
John McCall0b7e6782011-03-24 11:26:52 +00003843 DS.getRestrictSpecLoc()),
3844 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003845 SourceLocation());
Steve Naroff5618bd42008-08-27 16:04:49 +00003846 else
3847 // Remember that we parsed a Block type, and remember the type-quals.
Mike Stump1eb44332009-09-09 15:08:12 +00003848 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
John McCall0b7e6782011-03-24 11:26:52 +00003849 Loc),
3850 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003851 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00003852 } else {
3853 // Is a reference
John McCall0b7e6782011-03-24 11:26:52 +00003854 DeclSpec DS(AttrFactory);
Reid Spencer5f016e22007-07-11 17:01:13 +00003855
Sebastian Redl743de1f2009-03-23 00:00:23 +00003856 // Complain about rvalue references in C++03, but then go on and build
3857 // the declarator.
Richard Smith7fe62082011-10-15 05:09:34 +00003858 if (Kind == tok::ampamp)
David Blaikie4e4d0842012-03-11 07:00:24 +00003859 Diag(Loc, getLangOpts().CPlusPlus0x ?
Richard Smith7fe62082011-10-15 05:09:34 +00003860 diag::warn_cxx98_compat_rvalue_reference :
3861 diag::ext_rvalue_reference);
Sebastian Redl743de1f2009-03-23 00:00:23 +00003862
Richard Smith6ee326a2012-04-10 01:32:12 +00003863 // GNU-style and C++11 attributes are allowed here, as is restrict.
3864 ParseTypeQualifierListOpt(DS);
3865 D.ExtendWithDeclSpec(DS);
3866
Reid Spencer5f016e22007-07-11 17:01:13 +00003867 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
3868 // cv-qualifiers are introduced through the use of a typedef or of a
3869 // template type argument, in which case the cv-qualifiers are ignored.
Reid Spencer5f016e22007-07-11 17:01:13 +00003870 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
3871 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
3872 Diag(DS.getConstSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00003873 diag::err_invalid_reference_qualifier_application) << "const";
Reid Spencer5f016e22007-07-11 17:01:13 +00003874 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
3875 Diag(DS.getVolatileSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00003876 diag::err_invalid_reference_qualifier_application) << "volatile";
Reid Spencer5f016e22007-07-11 17:01:13 +00003877 }
3878
3879 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003880 ParseDeclaratorInternal(D, DirectDeclParser);
Reid Spencer5f016e22007-07-11 17:01:13 +00003881
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00003882 if (D.getNumTypeObjects() > 0) {
3883 // C++ [dcl.ref]p4: There shall be no references to references.
3884 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
3885 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattnerda83bac2008-11-19 07:37:42 +00003886 if (const IdentifierInfo *II = D.getIdentifier())
3887 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
3888 << II;
3889 else
3890 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
3891 << "type name";
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00003892
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003893 // Once we've complained about the reference-to-reference, we
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00003894 // can go ahead and build the (technically ill-formed)
3895 // declarator: reference collapsing will take care of it.
3896 }
3897 }
3898
Reid Spencer5f016e22007-07-11 17:01:13 +00003899 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner76549142008-02-21 01:32:26 +00003900 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
Sebastian Redl05532f22009-03-15 22:02:01 +00003901 Kind == tok::amp),
John McCall0b7e6782011-03-24 11:26:52 +00003902 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003903 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00003904 }
3905}
3906
Richard Smith9988f282012-03-29 01:16:42 +00003907static void diagnoseMisplacedEllipsis(Parser &P, Declarator &D,
3908 SourceLocation EllipsisLoc) {
3909 if (EllipsisLoc.isValid()) {
3910 FixItHint Insertion;
3911 if (!D.getEllipsisLoc().isValid()) {
3912 Insertion = FixItHint::CreateInsertion(D.getIdentifierLoc(), "...");
3913 D.setEllipsisLoc(EllipsisLoc);
3914 }
3915 P.Diag(EllipsisLoc, diag::err_misplaced_ellipsis_in_declaration)
3916 << FixItHint::CreateRemoval(EllipsisLoc) << Insertion << !D.hasName();
3917 }
3918}
3919
Reid Spencer5f016e22007-07-11 17:01:13 +00003920/// ParseDirectDeclarator
3921/// direct-declarator: [C99 6.7.5]
Douglas Gregor42a552f2008-11-05 20:51:48 +00003922/// [C99] identifier
Reid Spencer5f016e22007-07-11 17:01:13 +00003923/// '(' declarator ')'
3924/// [GNU] '(' attributes declarator ')'
3925/// [C90] direct-declarator '[' constant-expression[opt] ']'
3926/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
3927/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
3928/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
3929/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Richard Smith6ee326a2012-04-10 01:32:12 +00003930/// [C++11] direct-declarator '[' constant-expression[opt] ']'
3931/// attribute-specifier-seq[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00003932/// direct-declarator '(' parameter-type-list ')'
3933/// direct-declarator '(' identifier-list[opt] ')'
3934/// [GNU] direct-declarator '(' parameter-forward-declarations
3935/// parameter-type-list[opt] ')'
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003936/// [C++] direct-declarator '(' parameter-declaration-clause ')'
3937/// cv-qualifier-seq[opt] exception-specification[opt]
Richard Smith6ee326a2012-04-10 01:32:12 +00003938/// [C++11] direct-declarator '(' parameter-declaration-clause ')'
3939/// attribute-specifier-seq[opt] cv-qualifier-seq[opt]
3940/// ref-qualifier[opt] exception-specification[opt]
Douglas Gregorb48fe382008-10-31 09:07:45 +00003941/// [C++] declarator-id
Richard Smith6ee326a2012-04-10 01:32:12 +00003942/// [C++11] declarator-id attribute-specifier-seq[opt]
Douglas Gregor42a552f2008-11-05 20:51:48 +00003943///
3944/// declarator-id: [C++ 8]
Douglas Gregora8bc8c92010-12-23 22:44:42 +00003945/// '...'[opt] id-expression
Douglas Gregor42a552f2008-11-05 20:51:48 +00003946/// '::'[opt] nested-name-specifier[opt] type-name
3947///
3948/// id-expression: [C++ 5.1]
3949/// unqualified-id
Douglas Gregordb422df2009-09-25 21:45:23 +00003950/// qualified-id
Douglas Gregor42a552f2008-11-05 20:51:48 +00003951///
3952/// unqualified-id: [C++ 5.1]
Mike Stump1eb44332009-09-09 15:08:12 +00003953/// identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003954/// operator-function-id
Douglas Gregordb422df2009-09-25 21:45:23 +00003955/// conversion-function-id
Mike Stump1eb44332009-09-09 15:08:12 +00003956/// '~' class-name
Douglas Gregor39a8de12009-02-25 19:37:18 +00003957/// template-id
Argyrios Kyrtzidisc7ed9c62008-11-07 22:02:30 +00003958///
Richard Smith5d8388c2012-03-27 01:42:32 +00003959/// Note, any additional constructs added here may need corresponding changes
3960/// in isConstructorDeclarator.
Reid Spencer5f016e22007-07-11 17:01:13 +00003961void Parser::ParseDirectDeclarator(Declarator &D) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00003962 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003963
David Blaikie4e4d0842012-03-11 07:00:24 +00003964 if (getLangOpts().CPlusPlus && D.mayHaveIdentifier()) {
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003965 // ParseDeclaratorInternal might already have parsed the scope.
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00003966 if (D.getCXXScopeSpec().isEmpty()) {
Douglas Gregorefaa93a2011-11-07 17:33:42 +00003967 bool EnteringContext = D.getContext() == Declarator::FileContext ||
3968 D.getContext() == Declarator::MemberContext;
3969 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec(), ParsedType(),
3970 EnteringContext);
John McCall9ba61662010-02-26 08:45:28 +00003971 }
3972
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00003973 if (D.getCXXScopeSpec().isValid()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00003974 if (Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
John McCalle7e278b2009-12-11 20:04:54 +00003975 // Change the declaration context for name lookup, until this function
3976 // is exited (and the declarator has been parsed).
3977 DeclScopeObj.EnterDeclaratorScope();
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00003978 }
3979
Douglas Gregora8bc8c92010-12-23 22:44:42 +00003980 // C++0x [dcl.fct]p14:
3981 // There is a syntactic ambiguity when an ellipsis occurs at the end
3982 // of a parameter-declaration-clause without a preceding comma. In
3983 // this case, the ellipsis is parsed as part of the
3984 // abstract-declarator if the type of the parameter names a template
3985 // parameter pack that has not been expanded; otherwise, it is parsed
3986 // as part of the parameter-declaration-clause.
Richard Smith9988f282012-03-29 01:16:42 +00003987 if (Tok.is(tok::ellipsis) && D.getCXXScopeSpec().isEmpty() &&
Douglas Gregora8bc8c92010-12-23 22:44:42 +00003988 !((D.getContext() == Declarator::PrototypeContext ||
3989 D.getContext() == Declarator::BlockLiteralContext) &&
Douglas Gregora8bc8c92010-12-23 22:44:42 +00003990 NextToken().is(tok::r_paren) &&
Richard Smith9988f282012-03-29 01:16:42 +00003991 !Actions.containsUnexpandedParameterPacks(D))) {
3992 SourceLocation EllipsisLoc = ConsumeToken();
3993 if (isPtrOperatorToken(Tok.getKind(), getLangOpts())) {
3994 // The ellipsis was put in the wrong place. Recover, and explain to
3995 // the user what they should have done.
3996 ParseDeclarator(D);
3997 diagnoseMisplacedEllipsis(*this, D, EllipsisLoc);
3998 return;
3999 } else
4000 D.setEllipsisLoc(EllipsisLoc);
4001
4002 // The ellipsis can't be followed by a parenthesized declarator. We
4003 // check for that in ParseParenDeclarator, after we have disambiguated
4004 // the l_paren token.
4005 }
4006
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004007 if (Tok.is(tok::identifier) || Tok.is(tok::kw_operator) ||
4008 Tok.is(tok::annot_template_id) || Tok.is(tok::tilde)) {
4009 // We found something that indicates the start of an unqualified-id.
4010 // Parse that unqualified-id.
John McCallba9d8532010-04-13 06:39:49 +00004011 bool AllowConstructorName;
4012 if (D.getDeclSpec().hasTypeSpecifier())
4013 AllowConstructorName = false;
4014 else if (D.getCXXScopeSpec().isSet())
4015 AllowConstructorName =
4016 (D.getContext() == Declarator::FileContext ||
4017 (D.getContext() == Declarator::MemberContext &&
4018 D.getDeclSpec().isFriendSpecified()));
4019 else
4020 AllowConstructorName = (D.getContext() == Declarator::MemberContext);
4021
Abramo Bagnarae4b92762012-01-27 09:46:47 +00004022 SourceLocation TemplateKWLoc;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004023 if (ParseUnqualifiedId(D.getCXXScopeSpec(),
4024 /*EnteringContext=*/true,
4025 /*AllowDestructorName=*/true,
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004026 AllowConstructorName,
John McCallb3d87482010-08-24 05:47:05 +00004027 ParsedType(),
Abramo Bagnarae4b92762012-01-27 09:46:47 +00004028 TemplateKWLoc,
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00004029 D.getName()) ||
4030 // Once we're past the identifier, if the scope was bad, mark the
4031 // whole declarator bad.
4032 D.getCXXScopeSpec().isInvalid()) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00004033 D.SetIdentifier(0, Tok.getLocation());
4034 D.setInvalidType(true);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004035 } else {
4036 // Parsed the unqualified-id; update range information and move along.
4037 if (D.getSourceRange().getBegin().isInvalid())
4038 D.SetRangeBegin(D.getName().getSourceRange().getBegin());
4039 D.SetRangeEnd(D.getName().getSourceRange().getEnd());
Douglas Gregor2f1bc522008-11-07 20:08:42 +00004040 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004041 goto PastIdentifier;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00004042 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004043 } else if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
David Blaikie4e4d0842012-03-11 07:00:24 +00004044 assert(!getLangOpts().CPlusPlus &&
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00004045 "There's a C++-specific check for tok::identifier above");
4046 assert(Tok.getIdentifierInfo() && "Not an identifier?");
4047 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
4048 ConsumeToken();
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004049 goto PastIdentifier;
4050 }
Richard Smith9988f282012-03-29 01:16:42 +00004051
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004052 if (Tok.is(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00004053 // direct-declarator: '(' declarator ')'
4054 // direct-declarator: '(' attributes declarator ')'
4055 // Example: 'char (*X)' or 'int (*XX)(void)'
4056 ParseParenDeclarator(D);
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004057
4058 // If the declarator was parenthesized, we entered the declarator
4059 // scope when parsing the parenthesized declarator, then exited
4060 // the scope already. Re-enter the scope, if we need to.
4061 if (D.getCXXScopeSpec().isSet()) {
Fariborz Jahanian46877cd2010-08-17 23:50:37 +00004062 // If there was an error parsing parenthesized declarator, declarator
Richard Smith9988f282012-03-29 01:16:42 +00004063 // scope may have been entered before. Don't do it again.
Fariborz Jahanian46877cd2010-08-17 23:50:37 +00004064 if (!D.isInvalidType() &&
4065 Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004066 // Change the declaration context for name lookup, until this function
4067 // is exited (and the declarator has been parsed).
Fariborz Jahanian46877cd2010-08-17 23:50:37 +00004068 DeclScopeObj.EnterDeclaratorScope();
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004069 }
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00004070 } else if (D.mayOmitIdentifier()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00004071 // This could be something simple like "int" (in which case the declarator
4072 // portion is empty), if an abstract-declarator is allowed.
4073 D.SetIdentifier(0, Tok.getLocation());
4074 } else {
Douglas Gregore950d4b2009-03-06 23:28:18 +00004075 if (D.getContext() == Declarator::MemberContext)
4076 Diag(Tok, diag::err_expected_member_name_or_semi)
4077 << D.getDeclSpec().getSourceRange();
David Blaikie4e4d0842012-03-11 07:00:24 +00004078 else if (getLangOpts().CPlusPlus)
4079 Diag(Tok, diag::err_expected_unqualified_id) << getLangOpts().CPlusPlus;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00004080 else
Chris Lattner1ab3b962008-11-18 07:48:38 +00004081 Diag(Tok, diag::err_expected_ident_lparen);
Reid Spencer5f016e22007-07-11 17:01:13 +00004082 D.SetIdentifier(0, Tok.getLocation());
Chris Lattner1f6f54b2008-11-11 06:13:16 +00004083 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00004084 }
Mike Stump1eb44332009-09-09 15:08:12 +00004085
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00004086 PastIdentifier:
Reid Spencer5f016e22007-07-11 17:01:13 +00004087 assert(D.isPastIdentifier() &&
4088 "Haven't past the location of the identifier yet?");
Mike Stump1eb44332009-09-09 15:08:12 +00004089
Richard Smith6ee326a2012-04-10 01:32:12 +00004090 // Don't parse attributes unless we have parsed an unparenthesized name.
4091 if (D.hasName() && !D.getNumTypeObjects())
John McCall7f040a92010-12-24 02:08:15 +00004092 MaybeParseCXX0XAttributes(D);
Sean Huntbbd37c62009-11-21 08:43:09 +00004093
Reid Spencer5f016e22007-07-11 17:01:13 +00004094 while (1) {
Chris Lattner04d66662007-10-09 17:33:22 +00004095 if (Tok.is(tok::l_paren)) {
David Blaikie42d6d0c2011-12-04 05:04:18 +00004096 // Enter function-declaration scope, limiting any declarators to the
4097 // function prototype scope, including parameter declarators.
4098 ParseScope PrototypeScope(this,
4099 Scope::FunctionPrototypeScope|Scope::DeclScope);
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00004100 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
4101 // In such a case, check if we actually have a function declarator; if it
4102 // is not, the declarator has been fully parsed.
David Blaikie4e4d0842012-03-11 07:00:24 +00004103 if (getLangOpts().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
Chris Lattner7399ee02008-10-20 02:05:46 +00004104 // When not in file scope, warn for ambiguous function declarators, just
4105 // in case the author intended it as a variable definition.
4106 bool warnIfAmbiguous = D.getContext() != Declarator::FileContext;
4107 if (!isCXXFunctionDeclarator(warnIfAmbiguous))
4108 break;
4109 }
John McCall0b7e6782011-03-24 11:26:52 +00004110 ParsedAttributes attrs(AttrFactory);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004111 BalancedDelimiterTracker T(*this, tok::l_paren);
4112 T.consumeOpen();
4113 ParseFunctionDeclarator(D, attrs, T);
David Blaikie42d6d0c2011-12-04 05:04:18 +00004114 PrototypeScope.Exit();
Chris Lattner04d66662007-10-09 17:33:22 +00004115 } else if (Tok.is(tok::l_square)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00004116 ParseBracketDeclarator(D);
4117 } else {
4118 break;
4119 }
4120 }
David Blaikie42d6d0c2011-12-04 05:04:18 +00004121}
Reid Spencer5f016e22007-07-11 17:01:13 +00004122
Chris Lattneref4715c2008-04-06 05:45:57 +00004123/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
4124/// only called before the identifier, so these are most likely just grouping
Mike Stump1eb44332009-09-09 15:08:12 +00004125/// parens for precedence. If we find that these are actually function
Chris Lattneref4715c2008-04-06 05:45:57 +00004126/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
4127///
4128/// direct-declarator:
4129/// '(' declarator ')'
4130/// [GNU] '(' attributes declarator ')'
Chris Lattner7399ee02008-10-20 02:05:46 +00004131/// direct-declarator '(' parameter-type-list ')'
4132/// direct-declarator '(' identifier-list[opt] ')'
4133/// [GNU] direct-declarator '(' parameter-forward-declarations
4134/// parameter-type-list[opt] ')'
Chris Lattneref4715c2008-04-06 05:45:57 +00004135///
4136void Parser::ParseParenDeclarator(Declarator &D) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004137 BalancedDelimiterTracker T(*this, tok::l_paren);
4138 T.consumeOpen();
4139
Chris Lattneref4715c2008-04-06 05:45:57 +00004140 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
Mike Stump1eb44332009-09-09 15:08:12 +00004141
Chris Lattner7399ee02008-10-20 02:05:46 +00004142 // Eat any attributes before we look at whether this is a grouping or function
4143 // declarator paren. If this is a grouping paren, the attribute applies to
4144 // the type being built up, for example:
4145 // int (__attribute__(()) *x)(long y)
4146 // If this ends up not being a grouping paren, the attribute applies to the
4147 // first argument, for example:
4148 // int (__attribute__(()) int x)
4149 // In either case, we need to eat any attributes to be able to determine what
4150 // sort of paren this is.
4151 //
John McCall0b7e6782011-03-24 11:26:52 +00004152 ParsedAttributes attrs(AttrFactory);
Chris Lattner7399ee02008-10-20 02:05:46 +00004153 bool RequiresArg = false;
4154 if (Tok.is(tok::kw___attribute)) {
John McCall7f040a92010-12-24 02:08:15 +00004155 ParseGNUAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00004156
Chris Lattner7399ee02008-10-20 02:05:46 +00004157 // We require that the argument list (if this is a non-grouping paren) be
4158 // present even if the attribute list was empty.
4159 RequiresArg = true;
4160 }
Steve Naroff239f0732008-12-25 14:16:32 +00004161 // Eat any Microsoft extensions.
Eli Friedman290eeb02009-06-08 23:27:34 +00004162 if (Tok.is(tok::kw___cdecl) || Tok.is(tok::kw___stdcall) ||
Douglas Gregorf813a2c2010-05-18 16:57:00 +00004163 Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___fastcall) ||
Francois Pichet3bd9aa42011-08-18 09:59:55 +00004164 Tok.is(tok::kw___w64) || Tok.is(tok::kw___ptr64) ||
Francois Pichet58fd97a2011-08-25 00:36:46 +00004165 Tok.is(tok::kw___ptr32) || Tok.is(tok::kw___unaligned)) {
John McCall7f040a92010-12-24 02:08:15 +00004166 ParseMicrosoftTypeAttributes(attrs);
Eli Friedman290eeb02009-06-08 23:27:34 +00004167 }
Dawn Perchik52fc3142010-09-03 01:29:35 +00004168 // Eat any Borland extensions.
Ted Kremenek8113ecf2010-11-10 05:59:39 +00004169 if (Tok.is(tok::kw___pascal))
John McCall7f040a92010-12-24 02:08:15 +00004170 ParseBorlandTypeAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00004171
Chris Lattneref4715c2008-04-06 05:45:57 +00004172 // If we haven't past the identifier yet (or where the identifier would be
4173 // stored, if this is an abstract declarator), then this is probably just
4174 // grouping parens. However, if this could be an abstract-declarator, then
4175 // this could also be the start of function arguments (consider 'void()').
4176 bool isGrouping;
Mike Stump1eb44332009-09-09 15:08:12 +00004177
Chris Lattneref4715c2008-04-06 05:45:57 +00004178 if (!D.mayOmitIdentifier()) {
4179 // If this can't be an abstract-declarator, this *must* be a grouping
4180 // paren, because we haven't seen the identifier yet.
4181 isGrouping = true;
4182 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Richard Smith22592862012-03-27 23:05:05 +00004183 (getLangOpts().CPlusPlus && Tok.is(tok::ellipsis) &&
4184 NextToken().is(tok::r_paren)) || // C++ int(...)
Richard Smith6ce48a72012-04-11 04:01:28 +00004185 isDeclarationSpecifier() || // 'int(int)' is a function.
4186 isCXX11AttributeSpecifier()) { // 'int([[]]int)' is a function.
Chris Lattneref4715c2008-04-06 05:45:57 +00004187 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
4188 // considered to be a type, not a K&R identifier-list.
4189 isGrouping = false;
4190 } else {
4191 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
4192 isGrouping = true;
4193 }
Mike Stump1eb44332009-09-09 15:08:12 +00004194
Chris Lattneref4715c2008-04-06 05:45:57 +00004195 // If this is a grouping paren, handle:
4196 // direct-declarator: '(' declarator ')'
4197 // direct-declarator: '(' attributes declarator ')'
4198 if (isGrouping) {
Richard Smith9988f282012-03-29 01:16:42 +00004199 SourceLocation EllipsisLoc = D.getEllipsisLoc();
4200 D.setEllipsisLoc(SourceLocation());
4201
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00004202 bool hadGroupingParens = D.hasGroupingParens();
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00004203 D.setGroupingParens(true);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00004204 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattneref4715c2008-04-06 05:45:57 +00004205 // Match the ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004206 T.consumeClose();
4207 D.AddTypeInfo(DeclaratorChunk::getParen(T.getOpenLocation(),
4208 T.getCloseLocation()),
4209 attrs, T.getCloseLocation());
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00004210
4211 D.setGroupingParens(hadGroupingParens);
Richard Smith9988f282012-03-29 01:16:42 +00004212
4213 // An ellipsis cannot be placed outside parentheses.
4214 if (EllipsisLoc.isValid())
4215 diagnoseMisplacedEllipsis(*this, D, EllipsisLoc);
4216
Chris Lattneref4715c2008-04-06 05:45:57 +00004217 return;
4218 }
Mike Stump1eb44332009-09-09 15:08:12 +00004219
Chris Lattneref4715c2008-04-06 05:45:57 +00004220 // Okay, if this wasn't a grouping paren, it must be the start of a function
4221 // argument list. Recognize that this declarator will never have an
Chris Lattner7399ee02008-10-20 02:05:46 +00004222 // identifier (and remember where it would have been), then call into
4223 // ParseFunctionDeclarator to handle of argument list.
Chris Lattneref4715c2008-04-06 05:45:57 +00004224 D.SetIdentifier(0, Tok.getLocation());
4225
David Blaikie42d6d0c2011-12-04 05:04:18 +00004226 // Enter function-declaration scope, limiting any declarators to the
4227 // function prototype scope, including parameter declarators.
4228 ParseScope PrototypeScope(this,
4229 Scope::FunctionPrototypeScope|Scope::DeclScope);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004230 ParseFunctionDeclarator(D, attrs, T, RequiresArg);
David Blaikie42d6d0c2011-12-04 05:04:18 +00004231 PrototypeScope.Exit();
Chris Lattneref4715c2008-04-06 05:45:57 +00004232}
4233
4234/// ParseFunctionDeclarator - We are after the identifier and have parsed the
4235/// declarator D up to a paren, which indicates that we are parsing function
4236/// arguments.
Reid Spencer5f016e22007-07-11 17:01:13 +00004237///
Richard Smith6ee326a2012-04-10 01:32:12 +00004238/// If FirstArgAttrs is non-null, then the caller parsed those arguments
4239/// immediately after the open paren - they should be considered to be the
4240/// first argument of a parameter.
Chris Lattner7399ee02008-10-20 02:05:46 +00004241///
Richard Smith6ee326a2012-04-10 01:32:12 +00004242/// If RequiresArg is true, then the first argument of the function is required
4243/// to be present and required to not be an identifier list.
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004244///
Richard Smith6ee326a2012-04-10 01:32:12 +00004245/// For C++, after the parameter-list, it also parses the cv-qualifier-seq[opt],
4246/// (C++11) ref-qualifier[opt], exception-specification[opt],
4247/// (C++11) attribute-specifier-seq[opt], and (C++11) trailing-return-type[opt].
4248///
4249/// [C++11] exception-specification:
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004250/// dynamic-exception-specification
4251/// noexcept-specification
4252///
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004253void Parser::ParseFunctionDeclarator(Declarator &D,
Richard Smith6ee326a2012-04-10 01:32:12 +00004254 ParsedAttributes &FirstArgAttrs,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004255 BalancedDelimiterTracker &Tracker,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004256 bool RequiresArg) {
David Blaikie42d6d0c2011-12-04 05:04:18 +00004257 assert(getCurScope()->isFunctionPrototypeScope() &&
4258 "Should call from a Function scope");
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004259 // lparen is already consumed!
4260 assert(D.isPastIdentifier() && "Should not call before identifier!");
4261
4262 // This should be true when the function has typed arguments.
4263 // Otherwise, it is treated as a K&R-style function.
4264 bool HasProto = false;
4265 // Build up an array of information about the parsed arguments.
Chris Lattner5f9e2722011-07-23 10:55:15 +00004266 SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004267 // Remember where we see an ellipsis, if any.
4268 SourceLocation EllipsisLoc;
4269
4270 DeclSpec DS(AttrFactory);
4271 bool RefQualifierIsLValueRef = true;
4272 SourceLocation RefQualifierLoc;
Douglas Gregor43f51032011-10-19 06:04:55 +00004273 SourceLocation ConstQualifierLoc;
4274 SourceLocation VolatileQualifierLoc;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004275 ExceptionSpecificationType ESpecType = EST_None;
4276 SourceRange ESpecRange;
Chris Lattner5f9e2722011-07-23 10:55:15 +00004277 SmallVector<ParsedType, 2> DynamicExceptions;
4278 SmallVector<SourceRange, 2> DynamicExceptionRanges;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004279 ExprResult NoexceptExpr;
Richard Smith6ee326a2012-04-10 01:32:12 +00004280 ParsedAttributes FnAttrs(AttrFactory);
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004281 ParsedType TrailingReturnType;
Richard Smith6ee326a2012-04-10 01:32:12 +00004282
James Molloy16f1f712012-02-29 10:24:19 +00004283 Actions.ActOnStartFunctionDeclarator();
4284
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004285 SourceLocation EndLoc;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004286 if (isFunctionDeclaratorIdentifierList()) {
4287 if (RequiresArg)
4288 Diag(Tok, diag::err_argument_required_after_attribute);
4289
4290 ParseFunctionDeclaratorIdentifierList(D, ParamInfo);
4291
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004292 Tracker.consumeClose();
4293 EndLoc = Tracker.getCloseLocation();
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004294 } else {
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004295 if (Tok.isNot(tok::r_paren))
Richard Smith6ee326a2012-04-10 01:32:12 +00004296 ParseParameterDeclarationClause(D, FirstArgAttrs, ParamInfo, EllipsisLoc);
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004297 else if (RequiresArg)
4298 Diag(Tok, diag::err_argument_required_after_attribute);
4299
David Blaikie4e4d0842012-03-11 07:00:24 +00004300 HasProto = ParamInfo.size() || getLangOpts().CPlusPlus;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004301
4302 // If we have the closing ')', eat it.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004303 Tracker.consumeClose();
4304 EndLoc = Tracker.getCloseLocation();
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004305
David Blaikie4e4d0842012-03-11 07:00:24 +00004306 if (getLangOpts().CPlusPlus) {
Richard Smith6ee326a2012-04-10 01:32:12 +00004307 // FIXME: Accept these components in any order, and produce fixits to
4308 // correct the order if the user gets it wrong. Ideally we should deal
4309 // with the virt-specifier-seq and pure-specifier in the same way.
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004310
4311 // Parse cv-qualifier-seq[opt].
Richard Smith6ee326a2012-04-10 01:32:12 +00004312 ParseTypeQualifierListOpt(DS, false /*no attributes*/, false);
4313 if (!DS.getSourceRange().getEnd().isInvalid()) {
4314 EndLoc = DS.getSourceRange().getEnd();
4315 ConstQualifierLoc = DS.getConstSpecLoc();
4316 VolatileQualifierLoc = DS.getVolatileSpecLoc();
4317 }
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004318
4319 // Parse ref-qualifier[opt].
4320 if (Tok.is(tok::amp) || Tok.is(tok::ampamp)) {
David Blaikie4e4d0842012-03-11 07:00:24 +00004321 Diag(Tok, getLangOpts().CPlusPlus0x ?
Richard Smith7fe62082011-10-15 05:09:34 +00004322 diag::warn_cxx98_compat_ref_qualifier :
4323 diag::ext_ref_qualifier);
Richard Smith6ee326a2012-04-10 01:32:12 +00004324
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004325 RefQualifierIsLValueRef = Tok.is(tok::amp);
4326 RefQualifierLoc = ConsumeToken();
4327 EndLoc = RefQualifierLoc;
4328 }
4329
Douglas Gregorcefc3af2012-04-16 07:05:22 +00004330 // C++11 [expr.prim.general]p3:
4331 // If a declaration declares a member function or member function
4332 // template of a class X, the expression this is a prvalue of type
4333 // "pointer to cv-qualifier-seq X" between the optional cv-qualifer-seq
4334 // and the end of the function-definition, member-declarator, or
4335 // declarator.
4336 bool IsCXX11MemberFunction =
4337 getLangOpts().CPlusPlus0x &&
4338 (D.getContext() == Declarator::MemberContext ||
4339 (D.getContext() == Declarator::FileContext &&
4340 D.getCXXScopeSpec().isValid() &&
4341 Actions.CurContext->isRecord()));
4342 Sema::CXXThisScopeRAII ThisScope(Actions,
4343 dyn_cast<CXXRecordDecl>(Actions.CurContext),
4344 DS.getTypeQualifiers(),
4345 IsCXX11MemberFunction);
Richard Smitha058fd42012-05-02 22:22:32 +00004346
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004347 // Parse exception-specification[opt].
Richard Smitha058fd42012-05-02 22:22:32 +00004348 ESpecType = tryParseExceptionSpecification(ESpecRange,
Douglas Gregor74e2fc32012-04-16 18:27:27 +00004349 DynamicExceptions,
4350 DynamicExceptionRanges,
Richard Smitha058fd42012-05-02 22:22:32 +00004351 NoexceptExpr);
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004352 if (ESpecType != EST_None)
4353 EndLoc = ESpecRange.getEnd();
4354
Richard Smith6ee326a2012-04-10 01:32:12 +00004355 // Parse attribute-specifier-seq[opt]. Per DR 979 and DR 1297, this goes
4356 // after the exception-specification.
4357 MaybeParseCXX0XAttributes(FnAttrs);
4358
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004359 // Parse trailing-return-type[opt].
David Blaikie4e4d0842012-03-11 07:00:24 +00004360 if (getLangOpts().CPlusPlus0x && Tok.is(tok::arrow)) {
Richard Smith7fe62082011-10-15 05:09:34 +00004361 Diag(Tok, diag::warn_cxx98_compat_trailing_return_type);
Douglas Gregorae7902c2011-08-04 15:30:47 +00004362 SourceRange Range;
4363 TrailingReturnType = ParseTrailingReturnType(Range).get();
4364 if (Range.getEnd().isValid())
4365 EndLoc = Range.getEnd();
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004366 }
4367 }
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004368 }
4369
4370 // Remember that we parsed a function type, and remember the attributes.
4371 D.AddTypeInfo(DeclaratorChunk::getFunction(HasProto,
4372 /*isVariadic=*/EllipsisLoc.isValid(),
4373 EllipsisLoc,
4374 ParamInfo.data(), ParamInfo.size(),
4375 DS.getTypeQualifiers(),
4376 RefQualifierIsLValueRef,
Douglas Gregor43f51032011-10-19 06:04:55 +00004377 RefQualifierLoc, ConstQualifierLoc,
4378 VolatileQualifierLoc,
Douglas Gregor90ebed02011-07-13 21:47:47 +00004379 /*MutableLoc=*/SourceLocation(),
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004380 ESpecType, ESpecRange.getBegin(),
4381 DynamicExceptions.data(),
4382 DynamicExceptionRanges.data(),
4383 DynamicExceptions.size(),
4384 NoexceptExpr.isUsable() ?
4385 NoexceptExpr.get() : 0,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004386 Tracker.getOpenLocation(),
4387 EndLoc, D,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004388 TrailingReturnType),
Richard Smith6ee326a2012-04-10 01:32:12 +00004389 FnAttrs, EndLoc);
James Molloy16f1f712012-02-29 10:24:19 +00004390
4391 Actions.ActOnEndFunctionDeclarator();
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004392}
4393
4394/// isFunctionDeclaratorIdentifierList - This parameter list may have an
4395/// identifier list form for a K&R-style function: void foo(a,b,c)
4396///
4397/// Note that identifier-lists are only allowed for normal declarators, not for
4398/// abstract-declarators.
4399bool Parser::isFunctionDeclaratorIdentifierList() {
David Blaikie4e4d0842012-03-11 07:00:24 +00004400 return !getLangOpts().CPlusPlus
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004401 && Tok.is(tok::identifier)
4402 && !TryAltiVecVectorToken()
4403 // K&R identifier lists can't have typedefs as identifiers, per C99
4404 // 6.7.5.3p11.
4405 && (TryAnnotateTypeOrScopeToken() || !Tok.is(tok::annot_typename))
4406 // Identifier lists follow a really simple grammar: the identifiers can
4407 // be followed *only* by a ", identifier" or ")". However, K&R
4408 // identifier lists are really rare in the brave new modern world, and
4409 // it is very common for someone to typo a type in a non-K&R style
4410 // list. If we are presented with something like: "void foo(intptr x,
4411 // float y)", we don't want to start parsing the function declarator as
4412 // though it is a K&R style declarator just because intptr is an
4413 // invalid type.
4414 //
4415 // To handle this, we check to see if the token after the first
4416 // identifier is a "," or ")". Only then do we parse it as an
4417 // identifier list.
4418 && (NextToken().is(tok::comma) || NextToken().is(tok::r_paren));
4419}
4420
4421/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
4422/// we found a K&R-style identifier list instead of a typed parameter list.
4423///
4424/// After returning, ParamInfo will hold the parsed parameters.
4425///
4426/// identifier-list: [C99 6.7.5]
4427/// identifier
4428/// identifier-list ',' identifier
4429///
4430void Parser::ParseFunctionDeclaratorIdentifierList(
4431 Declarator &D,
Chris Lattner5f9e2722011-07-23 10:55:15 +00004432 SmallVector<DeclaratorChunk::ParamInfo, 16> &ParamInfo) {
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004433 // If there was no identifier specified for the declarator, either we are in
4434 // an abstract-declarator, or we are in a parameter declarator which was found
4435 // to be abstract. In abstract-declarators, identifier lists are not valid:
4436 // diagnose this.
4437 if (!D.getIdentifier())
4438 Diag(Tok, diag::ext_ident_list_in_param);
4439
4440 // Maintain an efficient lookup of params we have seen so far.
4441 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
4442
4443 while (1) {
4444 // If this isn't an identifier, report the error and skip until ')'.
4445 if (Tok.isNot(tok::identifier)) {
4446 Diag(Tok, diag::err_expected_ident);
4447 SkipUntil(tok::r_paren, /*StopAtSemi=*/true, /*DontConsume=*/true);
4448 // Forget we parsed anything.
4449 ParamInfo.clear();
4450 return;
4451 }
4452
4453 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
4454
4455 // Reject 'typedef int y; int test(x, y)', but continue parsing.
4456 if (Actions.getTypeName(*ParmII, Tok.getLocation(), getCurScope()))
4457 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
4458
4459 // Verify that the argument identifier has not already been mentioned.
4460 if (!ParamsSoFar.insert(ParmII)) {
4461 Diag(Tok, diag::err_param_redefinition) << ParmII;
4462 } else {
4463 // Remember this identifier in ParamInfo.
4464 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
4465 Tok.getLocation(),
4466 0));
4467 }
4468
4469 // Eat the identifier.
4470 ConsumeToken();
4471
4472 // The list continues if we see a comma.
4473 if (Tok.isNot(tok::comma))
4474 break;
4475 ConsumeToken();
4476 }
4477}
4478
4479/// ParseParameterDeclarationClause - Parse a (possibly empty) parameter-list
4480/// after the opening parenthesis. This function will not parse a K&R-style
4481/// identifier list.
4482///
Richard Smith6ce48a72012-04-11 04:01:28 +00004483/// D is the declarator being parsed. If FirstArgAttrs is non-null, then the
4484/// caller parsed those arguments immediately after the open paren - they should
4485/// be considered to be part of the first parameter.
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004486///
4487/// After returning, ParamInfo will hold the parsed parameters. EllipsisLoc will
4488/// be the location of the ellipsis, if any was parsed.
4489///
Reid Spencer5f016e22007-07-11 17:01:13 +00004490/// parameter-type-list: [C99 6.7.5]
4491/// parameter-list
4492/// parameter-list ',' '...'
Douglas Gregored5d6512009-09-22 21:41:40 +00004493/// [C++] parameter-list '...'
Reid Spencer5f016e22007-07-11 17:01:13 +00004494///
4495/// parameter-list: [C99 6.7.5]
4496/// parameter-declaration
4497/// parameter-list ',' parameter-declaration
4498///
4499/// parameter-declaration: [C99 6.7.5]
4500/// declaration-specifiers declarator
Chris Lattner04421082008-04-08 04:40:51 +00004501/// [C++] declaration-specifiers declarator '=' assignment-expression
Sebastian Redl84407ba2012-03-14 15:54:00 +00004502/// [C++11] initializer-clause
Reid Spencer5f016e22007-07-11 17:01:13 +00004503/// [GNU] declaration-specifiers declarator attributes
Sebastian Redl50de12f2009-03-24 22:27:57 +00004504/// declaration-specifiers abstract-declarator[opt]
4505/// [C++] declaration-specifiers abstract-declarator[opt]
Chris Lattner8123a952008-04-10 02:22:51 +00004506/// '=' assignment-expression
Reid Spencer5f016e22007-07-11 17:01:13 +00004507/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
Richard Smith6ce48a72012-04-11 04:01:28 +00004508/// [C++11] attribute-specifier-seq parameter-declaration
Reid Spencer5f016e22007-07-11 17:01:13 +00004509///
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004510void Parser::ParseParameterDeclarationClause(
4511 Declarator &D,
Richard Smith6ce48a72012-04-11 04:01:28 +00004512 ParsedAttributes &FirstArgAttrs,
Chris Lattner5f9e2722011-07-23 10:55:15 +00004513 SmallVector<DeclaratorChunk::ParamInfo, 16> &ParamInfo,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004514 SourceLocation &EllipsisLoc) {
Mike Stump1eb44332009-09-09 15:08:12 +00004515
Chris Lattnerf97409f2008-04-06 06:57:35 +00004516 while (1) {
4517 if (Tok.is(tok::ellipsis)) {
Richard Smith6ce48a72012-04-11 04:01:28 +00004518 // FIXME: Issue a diagnostic if we parsed an attribute-specifier-seq
4519 // before deciding this was a parameter-declaration-clause.
Douglas Gregor965acbb2009-02-18 07:07:28 +00004520 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chris Lattnerf97409f2008-04-06 06:57:35 +00004521 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00004522 }
Mike Stump1eb44332009-09-09 15:08:12 +00004523
Chris Lattnerf97409f2008-04-06 06:57:35 +00004524 // Parse the declaration-specifiers.
John McCall54abf7d2009-11-04 02:18:39 +00004525 // Just use the ParsingDeclaration "scope" of the declarator.
John McCall0b7e6782011-03-24 11:26:52 +00004526 DeclSpec DS(AttrFactory);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004527
Richard Smith6ce48a72012-04-11 04:01:28 +00004528 // Parse any C++11 attributes.
4529 MaybeParseCXX0XAttributes(DS.getAttributes());
4530
John McCall7f040a92010-12-24 02:08:15 +00004531 // Skip any Microsoft attributes before a param.
David Blaikie4e4d0842012-03-11 07:00:24 +00004532 if (getLangOpts().MicrosoftExt && Tok.is(tok::l_square))
John McCall7f040a92010-12-24 02:08:15 +00004533 ParseMicrosoftAttributes(DS.getAttributes());
4534
4535 SourceLocation DSStart = Tok.getLocation();
Chris Lattner7399ee02008-10-20 02:05:46 +00004536
4537 // If the caller parsed attributes for the first argument, add them now.
John McCall7f040a92010-12-24 02:08:15 +00004538 // Take them so that we only apply the attributes to the first parameter.
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004539 // FIXME: If we can leave the attributes in the token stream somehow, we can
Richard Smith6ce48a72012-04-11 04:01:28 +00004540 // get rid of a parameter (FirstArgAttrs) and this statement. It might be
4541 // too much hassle.
4542 DS.takeAttributesFrom(FirstArgAttrs);
John McCall7f040a92010-12-24 02:08:15 +00004543
Chris Lattnere64c5492009-02-27 18:38:20 +00004544 ParseDeclarationSpecifiers(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00004545
Chris Lattnerf97409f2008-04-06 06:57:35 +00004546 // Parse the declarator. This is "PrototypeContext", because we must
4547 // accept either 'declarator' or 'abstract-declarator' here.
4548 Declarator ParmDecl(DS, Declarator::PrototypeContext);
4549 ParseDeclarator(ParmDecl);
4550
4551 // Parse GNU attributes, if present.
John McCall7f040a92010-12-24 02:08:15 +00004552 MaybeParseGNUAttributes(ParmDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00004553
Chris Lattnerf97409f2008-04-06 06:57:35 +00004554 // Remember this parsed parameter in ParamInfo.
4555 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
Mike Stump1eb44332009-09-09 15:08:12 +00004556
Douglas Gregor72b505b2008-12-16 21:30:33 +00004557 // DefArgToks is used when the parsing of default arguments needs
4558 // to be delayed.
4559 CachedTokens *DefArgToks = 0;
4560
Chris Lattnerf97409f2008-04-06 06:57:35 +00004561 // If no parameter was specified, verify that *something* was specified,
4562 // otherwise we have a missing type and identifier.
Chris Lattnere64c5492009-02-27 18:38:20 +00004563 if (DS.isEmpty() && ParmDecl.getIdentifier() == 0 &&
4564 ParmDecl.getNumTypeObjects() == 0) {
Chris Lattnerf97409f2008-04-06 06:57:35 +00004565 // Completely missing, emit error.
4566 Diag(DSStart, diag::err_missing_param);
4567 } else {
4568 // Otherwise, we have something. Add it and let semantic analysis try
4569 // to grok it and add the result to the ParamInfo we are building.
Mike Stump1eb44332009-09-09 15:08:12 +00004570
Chris Lattnerf97409f2008-04-06 06:57:35 +00004571 // Inform the actions module about the parameter declarator, so it gets
4572 // added to the current scope.
John McCalld226f652010-08-21 09:40:31 +00004573 Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl);
Chris Lattner04421082008-04-08 04:40:51 +00004574
4575 // Parse the default argument, if any. We parse the default
4576 // arguments in all dialects; the semantic analysis in
4577 // ActOnParamDefaultArgument will reject the default argument in
4578 // C.
4579 if (Tok.is(tok::equal)) {
Douglas Gregor61366e92008-12-24 00:01:03 +00004580 SourceLocation EqualLoc = Tok.getLocation();
4581
Chris Lattner04421082008-04-08 04:40:51 +00004582 // Parse the default argument
Douglas Gregor72b505b2008-12-16 21:30:33 +00004583 if (D.getContext() == Declarator::MemberContext) {
4584 // If we're inside a class definition, cache the tokens
4585 // corresponding to the default argument. We'll actually parse
4586 // them when we see the end of the class definition.
Douglas Gregor72b505b2008-12-16 21:30:33 +00004587 // FIXME: Can we use a smart pointer for Toks?
4588 DefArgToks = new CachedTokens;
4589
Mike Stump1eb44332009-09-09 15:08:12 +00004590 if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks,
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +00004591 /*StopAtSemi=*/true,
4592 /*ConsumeFinalToken=*/false)) {
Douglas Gregor72b505b2008-12-16 21:30:33 +00004593 delete DefArgToks;
4594 DefArgToks = 0;
Douglas Gregor61366e92008-12-24 00:01:03 +00004595 Actions.ActOnParamDefaultArgumentError(Param);
Argyrios Kyrtzidis2b602ad2010-08-06 09:47:24 +00004596 } else {
4597 // Mark the end of the default argument so that we know when to
4598 // stop when we parse it later on.
4599 Token DefArgEnd;
4600 DefArgEnd.startToken();
4601 DefArgEnd.setKind(tok::cxx_defaultarg_end);
4602 DefArgEnd.setLocation(Tok.getLocation());
4603 DefArgToks->push_back(DefArgEnd);
Mike Stump1eb44332009-09-09 15:08:12 +00004604 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc,
Anders Carlsson5e300d12009-06-12 16:51:40 +00004605 (*DefArgToks)[1].getLocation());
Argyrios Kyrtzidis2b602ad2010-08-06 09:47:24 +00004606 }
Chris Lattner04421082008-04-08 04:40:51 +00004607 } else {
Douglas Gregor72b505b2008-12-16 21:30:33 +00004608 // Consume the '='.
Douglas Gregor61366e92008-12-24 00:01:03 +00004609 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00004610
Douglas Gregorbe0f7bd2010-09-11 20:24:53 +00004611 // The argument isn't actually potentially evaluated unless it is
4612 // used.
4613 EnterExpressionEvaluationContext Eval(Actions,
Douglas Gregorccc1b5e2012-02-21 00:37:24 +00004614 Sema::PotentiallyEvaluatedIfUsed,
4615 Param);
Douglas Gregorbe0f7bd2010-09-11 20:24:53 +00004616
Sebastian Redl84407ba2012-03-14 15:54:00 +00004617 ExprResult DefArgResult;
Sebastian Redl3e280b52012-03-18 22:25:45 +00004618 if (getLangOpts().CPlusPlus0x && Tok.is(tok::l_brace)) {
4619 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
Sebastian Redl84407ba2012-03-14 15:54:00 +00004620 DefArgResult = ParseBraceInitializer();
Sebastian Redl3e280b52012-03-18 22:25:45 +00004621 } else
Sebastian Redl84407ba2012-03-14 15:54:00 +00004622 DefArgResult = ParseAssignmentExpression();
Douglas Gregor72b505b2008-12-16 21:30:33 +00004623 if (DefArgResult.isInvalid()) {
4624 Actions.ActOnParamDefaultArgumentError(Param);
4625 SkipUntil(tok::comma, tok::r_paren, true, true);
4626 } else {
4627 // Inform the actions module about the default argument
4628 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
John McCall9ae2f072010-08-23 23:25:46 +00004629 DefArgResult.take());
Douglas Gregor72b505b2008-12-16 21:30:33 +00004630 }
Chris Lattner04421082008-04-08 04:40:51 +00004631 }
4632 }
Mike Stump1eb44332009-09-09 15:08:12 +00004633
4634 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
4635 ParmDecl.getIdentifierLoc(), Param,
Douglas Gregor72b505b2008-12-16 21:30:33 +00004636 DefArgToks));
Chris Lattnerf97409f2008-04-06 06:57:35 +00004637 }
4638
4639 // If the next token is a comma, consume it and keep reading arguments.
Douglas Gregored5d6512009-09-22 21:41:40 +00004640 if (Tok.isNot(tok::comma)) {
4641 if (Tok.is(tok::ellipsis)) {
Douglas Gregored5d6512009-09-22 21:41:40 +00004642 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
4643
David Blaikie4e4d0842012-03-11 07:00:24 +00004644 if (!getLangOpts().CPlusPlus) {
Douglas Gregored5d6512009-09-22 21:41:40 +00004645 // We have ellipsis without a preceding ',', which is ill-formed
4646 // in C. Complain and provide the fix.
4647 Diag(EllipsisLoc, diag::err_missing_comma_before_ellipsis)
Douglas Gregor849b2432010-03-31 17:46:05 +00004648 << FixItHint::CreateInsertion(EllipsisLoc, ", ");
Douglas Gregored5d6512009-09-22 21:41:40 +00004649 }
4650 }
4651
4652 break;
4653 }
Mike Stump1eb44332009-09-09 15:08:12 +00004654
Chris Lattnerf97409f2008-04-06 06:57:35 +00004655 // Consume the comma.
4656 ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00004657 }
Mike Stump1eb44332009-09-09 15:08:12 +00004658
Chris Lattner66d28652008-04-06 06:34:08 +00004659}
Chris Lattneref4715c2008-04-06 05:45:57 +00004660
Reid Spencer5f016e22007-07-11 17:01:13 +00004661/// [C90] direct-declarator '[' constant-expression[opt] ']'
4662/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
4663/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
4664/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
4665/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Richard Smith6ee326a2012-04-10 01:32:12 +00004666/// [C++11] direct-declarator '[' constant-expression[opt] ']'
4667/// attribute-specifier-seq[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00004668void Parser::ParseBracketDeclarator(Declarator &D) {
Richard Smith6ee326a2012-04-10 01:32:12 +00004669 if (CheckProhibitedCXX11Attribute())
4670 return;
4671
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004672 BalancedDelimiterTracker T(*this, tok::l_square);
4673 T.consumeOpen();
Mike Stump1eb44332009-09-09 15:08:12 +00004674
Chris Lattner378c7e42008-12-18 07:27:21 +00004675 // C array syntax has many features, but by-far the most common is [] and [4].
4676 // This code does a fast path to handle some of the most obvious cases.
4677 if (Tok.getKind() == tok::r_square) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004678 T.consumeClose();
John McCall0b7e6782011-03-24 11:26:52 +00004679 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00004680 MaybeParseCXX0XAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00004681
Chris Lattner378c7e42008-12-18 07:27:21 +00004682 // Remember that we parsed the empty array type.
John McCall60d7b3a2010-08-24 06:29:42 +00004683 ExprResult NumElements;
John McCall0b7e6782011-03-24 11:26:52 +00004684 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004685 T.getOpenLocation(),
4686 T.getCloseLocation()),
4687 attrs, T.getCloseLocation());
Chris Lattner378c7e42008-12-18 07:27:21 +00004688 return;
4689 } else if (Tok.getKind() == tok::numeric_constant &&
4690 GetLookAheadToken(1).is(tok::r_square)) {
4691 // [4] is very common. Parse the numeric constant expression.
Richard Smith36f5cfe2012-03-09 08:00:36 +00004692 ExprResult ExprRes(Actions.ActOnNumericConstant(Tok, getCurScope()));
Chris Lattner378c7e42008-12-18 07:27:21 +00004693 ConsumeToken();
4694
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004695 T.consumeClose();
John McCall0b7e6782011-03-24 11:26:52 +00004696 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00004697 MaybeParseCXX0XAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00004698
Chris Lattner378c7e42008-12-18 07:27:21 +00004699 // Remember that we parsed a array type, and remember its features.
John McCall0b7e6782011-03-24 11:26:52 +00004700 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, 0,
John McCall7f040a92010-12-24 02:08:15 +00004701 ExprRes.release(),
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004702 T.getOpenLocation(),
4703 T.getCloseLocation()),
4704 attrs, T.getCloseLocation());
Chris Lattner378c7e42008-12-18 07:27:21 +00004705 return;
4706 }
Mike Stump1eb44332009-09-09 15:08:12 +00004707
Reid Spencer5f016e22007-07-11 17:01:13 +00004708 // If valid, this location is the position where we read the 'static' keyword.
4709 SourceLocation StaticLoc;
Chris Lattner04d66662007-10-09 17:33:22 +00004710 if (Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00004711 StaticLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00004712
Reid Spencer5f016e22007-07-11 17:01:13 +00004713 // If there is a type-qualifier-list, read it now.
Chris Lattnera1fcbad2008-12-18 06:50:14 +00004714 // Type qualifiers in an array subscript are a C99 feature.
John McCall0b7e6782011-03-24 11:26:52 +00004715 DeclSpec DS(AttrFactory);
Chris Lattner5a69d1c2008-12-18 07:02:59 +00004716 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Mike Stump1eb44332009-09-09 15:08:12 +00004717
Reid Spencer5f016e22007-07-11 17:01:13 +00004718 // If we haven't already read 'static', check to see if there is one after the
4719 // type-qualifier-list.
Chris Lattner04d66662007-10-09 17:33:22 +00004720 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00004721 StaticLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00004722
Reid Spencer5f016e22007-07-11 17:01:13 +00004723 // Handle "direct-declarator [ type-qual-list[opt] * ]".
4724 bool isStar = false;
John McCall60d7b3a2010-08-24 06:29:42 +00004725 ExprResult NumElements;
Mike Stump1eb44332009-09-09 15:08:12 +00004726
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00004727 // Handle the case where we have '[*]' as the array size. However, a leading
4728 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
4729 // the the token after the star is a ']'. Since stars in arrays are
4730 // infrequent, use of lookahead is not costly here.
4731 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattnera711dd02008-04-06 05:27:21 +00004732 ConsumeToken(); // Eat the '*'.
Reid Spencer5f016e22007-07-11 17:01:13 +00004733
Chris Lattnera1fcbad2008-12-18 06:50:14 +00004734 if (StaticLoc.isValid()) {
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00004735 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattnera1fcbad2008-12-18 06:50:14 +00004736 StaticLoc = SourceLocation(); // Drop the static.
4737 }
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00004738 isStar = true;
Chris Lattner04d66662007-10-09 17:33:22 +00004739 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner378c7e42008-12-18 07:27:21 +00004740 // Note, in C89, this production uses the constant-expr production instead
4741 // of assignment-expr. The only difference is that assignment-expr allows
4742 // things like '=' and '*='. Sema rejects these in C89 mode because they
4743 // are not i-c-e's, so we don't need to distinguish between the two here.
Mike Stump1eb44332009-09-09 15:08:12 +00004744
Douglas Gregore0762c92009-06-19 23:52:42 +00004745 // Parse the constant-expression or assignment-expression now (depending
4746 // on dialect).
David Blaikie4e4d0842012-03-11 07:00:24 +00004747 if (getLangOpts().CPlusPlus) {
Douglas Gregore0762c92009-06-19 23:52:42 +00004748 NumElements = ParseConstantExpression();
Eli Friedman71b8fb52012-01-21 01:01:51 +00004749 } else {
4750 EnterExpressionEvaluationContext Unevaluated(Actions,
4751 Sema::ConstantEvaluated);
Douglas Gregore0762c92009-06-19 23:52:42 +00004752 NumElements = ParseAssignmentExpression();
Eli Friedman71b8fb52012-01-21 01:01:51 +00004753 }
Reid Spencer5f016e22007-07-11 17:01:13 +00004754 }
Mike Stump1eb44332009-09-09 15:08:12 +00004755
Reid Spencer5f016e22007-07-11 17:01:13 +00004756 // If there was an error parsing the assignment-expression, recover.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00004757 if (NumElements.isInvalid()) {
Chris Lattner5cb10d32009-04-24 22:30:50 +00004758 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00004759 // If the expression was invalid, skip it.
4760 SkipUntil(tok::r_square);
4761 return;
4762 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00004763
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004764 T.consumeClose();
Sebastian Redlab197ba2009-02-09 18:23:29 +00004765
John McCall0b7e6782011-03-24 11:26:52 +00004766 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00004767 MaybeParseCXX0XAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00004768
Chris Lattner378c7e42008-12-18 07:27:21 +00004769 // Remember that we parsed a array type, and remember its features.
John McCall0b7e6782011-03-24 11:26:52 +00004770 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
Reid Spencer5f016e22007-07-11 17:01:13 +00004771 StaticLoc.isValid(), isStar,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00004772 NumElements.release(),
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004773 T.getOpenLocation(),
4774 T.getCloseLocation()),
4775 attrs, T.getCloseLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00004776}
4777
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004778/// [GNU] typeof-specifier:
4779/// typeof ( expressions )
4780/// typeof ( type-name )
4781/// [GNU/C++] typeof unary-expression
Steve Naroffd1861fd2007-07-31 12:34:36 +00004782///
4783void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner04d66662007-10-09 17:33:22 +00004784 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004785 Token OpTok = Tok;
Steve Naroffd1861fd2007-07-31 12:34:36 +00004786 SourceLocation StartLoc = ConsumeToken();
4787
John McCallcfb708c2010-01-13 20:03:27 +00004788 const bool hasParens = Tok.is(tok::l_paren);
4789
Eli Friedman71b8fb52012-01-21 01:01:51 +00004790 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated);
4791
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004792 bool isCastExpr;
John McCallb3d87482010-08-24 05:47:05 +00004793 ParsedType CastTy;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004794 SourceRange CastRange;
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00004795 ExprResult Operand = ParseExprAfterUnaryExprOrTypeTrait(OpTok, isCastExpr,
4796 CastTy, CastRange);
John McCallcfb708c2010-01-13 20:03:27 +00004797 if (hasParens)
4798 DS.setTypeofParensRange(CastRange);
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004799
4800 if (CastRange.getEnd().isInvalid())
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004801 // FIXME: Not accurate, the range gets one token more than it should.
4802 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004803 else
4804 DS.SetRangeEnd(CastRange.getEnd());
Mike Stump1eb44332009-09-09 15:08:12 +00004805
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004806 if (isCastExpr) {
4807 if (!CastTy) {
4808 DS.SetTypeSpecError();
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004809 return;
Douglas Gregor809070a2009-02-18 17:45:20 +00004810 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004811
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004812 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00004813 unsigned DiagID;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004814 // Check for duplicate type specifiers (e.g. "int typeof(int)").
4815 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00004816 DiagID, CastTy))
4817 Diag(StartLoc, DiagID) << PrevSpec;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004818 return;
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004819 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004820
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004821 // If we get here, the operand to the typeof was an expresion.
4822 if (Operand.isInvalid()) {
4823 DS.SetTypeSpecError();
Steve Naroff9dfa7b42007-08-02 02:53:48 +00004824 return;
Steve Naroffd1861fd2007-07-31 12:34:36 +00004825 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004826
Eli Friedman71b8fb52012-01-21 01:01:51 +00004827 // We might need to transform the operand if it is potentially evaluated.
4828 Operand = Actions.HandleExprEvaluationContextForTypeof(Operand.get());
4829 if (Operand.isInvalid()) {
4830 DS.SetTypeSpecError();
4831 return;
4832 }
4833
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004834 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00004835 unsigned DiagID;
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004836 // Check for duplicate type specifiers (e.g. "int typeof(int)").
4837 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
John McCallb3d87482010-08-24 05:47:05 +00004838 DiagID, Operand.get()))
John McCallfec54012009-08-03 20:12:06 +00004839 Diag(StartLoc, DiagID) << PrevSpec;
Steve Naroffd1861fd2007-07-31 12:34:36 +00004840}
Chris Lattner1b492422010-02-28 18:33:55 +00004841
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00004842/// [C11] atomic-specifier:
Eli Friedmanb001de72011-10-06 23:00:33 +00004843/// _Atomic ( type-name )
4844///
4845void Parser::ParseAtomicSpecifier(DeclSpec &DS) {
4846 assert(Tok.is(tok::kw__Atomic) && "Not an atomic specifier");
4847
4848 SourceLocation StartLoc = ConsumeToken();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004849 BalancedDelimiterTracker T(*this, tok::l_paren);
4850 if (T.expectAndConsume(diag::err_expected_lparen_after, "_Atomic")) {
Eli Friedmanb001de72011-10-06 23:00:33 +00004851 SkipUntil(tok::r_paren);
4852 return;
4853 }
4854
4855 TypeResult Result = ParseTypeName();
4856 if (Result.isInvalid()) {
4857 SkipUntil(tok::r_paren);
4858 return;
4859 }
4860
4861 // Match the ')'
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004862 T.consumeClose();
Eli Friedmanb001de72011-10-06 23:00:33 +00004863
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004864 if (T.getCloseLocation().isInvalid())
Eli Friedmanb001de72011-10-06 23:00:33 +00004865 return;
4866
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004867 DS.setTypeofParensRange(T.getRange());
4868 DS.SetRangeEnd(T.getCloseLocation());
Eli Friedmanb001de72011-10-06 23:00:33 +00004869
4870 const char *PrevSpec = 0;
4871 unsigned DiagID;
4872 if (DS.SetTypeSpecType(DeclSpec::TST_atomic, StartLoc, PrevSpec,
4873 DiagID, Result.release()))
4874 Diag(StartLoc, DiagID) << PrevSpec;
4875}
4876
Chris Lattner1b492422010-02-28 18:33:55 +00004877
4878/// TryAltiVecVectorTokenOutOfLine - Out of line body that should only be called
4879/// from TryAltiVecVectorToken.
4880bool Parser::TryAltiVecVectorTokenOutOfLine() {
4881 Token Next = NextToken();
4882 switch (Next.getKind()) {
4883 default: return false;
4884 case tok::kw_short:
4885 case tok::kw_long:
4886 case tok::kw_signed:
4887 case tok::kw_unsigned:
4888 case tok::kw_void:
4889 case tok::kw_char:
4890 case tok::kw_int:
4891 case tok::kw_float:
4892 case tok::kw_double:
4893 case tok::kw_bool:
4894 case tok::kw___pixel:
4895 Tok.setKind(tok::kw___vector);
4896 return true;
4897 case tok::identifier:
4898 if (Next.getIdentifierInfo() == Ident_pixel) {
4899 Tok.setKind(tok::kw___vector);
4900 return true;
4901 }
4902 return false;
4903 }
4904}
4905
4906bool Parser::TryAltiVecTokenOutOfLine(DeclSpec &DS, SourceLocation Loc,
4907 const char *&PrevSpec, unsigned &DiagID,
4908 bool &isInvalid) {
4909 if (Tok.getIdentifierInfo() == Ident_vector) {
4910 Token Next = NextToken();
4911 switch (Next.getKind()) {
4912 case tok::kw_short:
4913 case tok::kw_long:
4914 case tok::kw_signed:
4915 case tok::kw_unsigned:
4916 case tok::kw_void:
4917 case tok::kw_char:
4918 case tok::kw_int:
4919 case tok::kw_float:
4920 case tok::kw_double:
4921 case tok::kw_bool:
4922 case tok::kw___pixel:
4923 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
4924 return true;
4925 case tok::identifier:
4926 if (Next.getIdentifierInfo() == Ident_pixel) {
4927 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
4928 return true;
4929 }
4930 break;
4931 default:
4932 break;
4933 }
Douglas Gregora8f031f2010-06-16 15:28:57 +00004934 } else if ((Tok.getIdentifierInfo() == Ident_pixel) &&
Chris Lattner1b492422010-02-28 18:33:55 +00004935 DS.isTypeAltiVecVector()) {
4936 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
4937 return true;
4938 }
4939 return false;
4940}