blob: e57db6ff9b26ee2de2847043da3722f6a0e46cd3 [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 Smith7796eb52012-03-12 08:56:40 +000041
Reid Spencer5f016e22007-07-11 17:01:13 +000042 // Parse the common declaration-specifiers piece.
John McCall0b7e6782011-03-24 11:26:52 +000043 DeclSpec DS(AttrFactory);
Richard Smith7796eb52012-03-12 08:56:40 +000044 ParseSpecifierQualifierList(DS, AS, DSC);
Richard Smithc89edf52011-07-01 19:46:12 +000045 if (OwnedType)
46 *OwnedType = DS.isTypeSpecOwned() ? DS.getRepAsDecl() : 0;
Sebastian Redlef65f062009-05-29 18:02:33 +000047
Reid Spencer5f016e22007-07-11 17:01:13 +000048 // Parse the abstract-declarator, if present.
Douglas Gregor683a81f2011-01-31 16:09:46 +000049 Declarator DeclaratorInfo(DS, Context);
Reid Spencer5f016e22007-07-11 17:01:13 +000050 ParseDeclarator(DeclaratorInfo);
Sebastian Redlef65f062009-05-29 18:02:33 +000051 if (Range)
52 *Range = DeclaratorInfo.getSourceRange();
53
Chris Lattnereaaebc72009-04-25 08:06:05 +000054 if (DeclaratorInfo.isInvalidType())
Douglas Gregor809070a2009-02-18 17:45:20 +000055 return true;
56
Douglas Gregor23c94db2010-07-02 17:43:08 +000057 return Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
Reid Spencer5f016e22007-07-11 17:01:13 +000058}
59
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +000060
61/// isAttributeLateParsed - Return true if the attribute has arguments that
62/// require late parsing.
63static bool isAttributeLateParsed(const IdentifierInfo &II) {
64 return llvm::StringSwitch<bool>(II.getName())
65#include "clang/Parse/AttrLateParsed.inc"
66 .Default(false);
67}
68
69
Sean Huntbbd37c62009-11-21 08:43:09 +000070/// ParseGNUAttributes - Parse a non-empty attributes list.
Reid Spencer5f016e22007-07-11 17:01:13 +000071///
72/// [GNU] attributes:
73/// attribute
74/// attributes attribute
75///
76/// [GNU] attribute:
77/// '__attribute__' '(' '(' attribute-list ')' ')'
78///
79/// [GNU] attribute-list:
80/// attrib
81/// attribute_list ',' attrib
82///
83/// [GNU] attrib:
84/// empty
85/// attrib-name
86/// attrib-name '(' identifier ')'
87/// attrib-name '(' identifier ',' nonempty-expr-list ')'
88/// attrib-name '(' argument-expression-list [C99 6.5.2] ')'
89///
90/// [GNU] attrib-name:
91/// identifier
92/// typespec
93/// typequal
94/// storageclass
Mike Stump1eb44332009-09-09 15:08:12 +000095///
Reid Spencer5f016e22007-07-11 17:01:13 +000096/// FIXME: The GCC grammar/code for this construct implies we need two
Mike Stump1eb44332009-09-09 15:08:12 +000097/// token lookahead. Comment from gcc: "If they start with an identifier
98/// which is followed by a comma or close parenthesis, then the arguments
Reid Spencer5f016e22007-07-11 17:01:13 +000099/// start with that identifier; otherwise they are an expression list."
100///
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000101/// GCC does not require the ',' between attribs in an attribute-list.
102///
Reid Spencer5f016e22007-07-11 17:01:13 +0000103/// At the moment, I am not doing 2 token lookahead. I am also unaware of
104/// any attributes that don't work (based on my limited testing). Most
105/// attributes are very simple in practice. Until we find a bug, I don't see
106/// a pressing need to implement the 2 token lookahead.
107
John McCall7f040a92010-12-24 02:08:15 +0000108void Parser::ParseGNUAttributes(ParsedAttributes &attrs,
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000109 SourceLocation *endLoc,
110 LateParsedAttrList *LateAttrs) {
Sean Huntbbd37c62009-11-21 08:43:09 +0000111 assert(Tok.is(tok::kw___attribute) && "Not a GNU attribute list!");
Mike Stump1eb44332009-09-09 15:08:12 +0000112
Chris Lattner04d66662007-10-09 17:33:22 +0000113 while (Tok.is(tok::kw___attribute)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000114 ConsumeToken();
115 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
116 "attribute")) {
117 SkipUntil(tok::r_paren, true); // skip until ) or ;
John McCall7f040a92010-12-24 02:08:15 +0000118 return;
Reid Spencer5f016e22007-07-11 17:01:13 +0000119 }
120 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, "(")) {
121 SkipUntil(tok::r_paren, true); // skip until ) or ;
John McCall7f040a92010-12-24 02:08:15 +0000122 return;
Reid Spencer5f016e22007-07-11 17:01:13 +0000123 }
124 // Parse the attribute-list. e.g. __attribute__(( weak, alias("__f") ))
Chris Lattner04d66662007-10-09 17:33:22 +0000125 while (Tok.is(tok::identifier) || isDeclarationSpecifier() ||
126 Tok.is(tok::comma)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000127 if (Tok.is(tok::comma)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000128 // allows for empty/non-empty attributes. ((__vector_size__(16),,,,))
129 ConsumeToken();
130 continue;
131 }
132 // we have an identifier or declaration specifier (const, int, etc.)
133 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
134 SourceLocation AttrNameLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000135
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000136 if (Tok.is(tok::l_paren)) {
137 // handle "parameterized" attributes
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000138 if (LateAttrs && isAttributeLateParsed(*AttrName)) {
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000139 LateParsedAttribute *LA =
140 new LateParsedAttribute(this, *AttrName, AttrNameLoc);
141 LateAttrs->push_back(LA);
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000142
143 // Attributes in a class are parsed at the end of the class, along
144 // with other late-parsed declarations.
145 if (!ClassStack.empty())
146 getCurrentClass().LateParsedDeclarations.push_back(LA);
Mike Stump1eb44332009-09-09 15:08:12 +0000147
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000148 // consume everything up to and including the matching right parens
149 ConsumeAndStoreUntil(tok::r_paren, LA->Toks, true, false);
Mike Stump1eb44332009-09-09 15:08:12 +0000150
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000151 Token Eof;
152 Eof.startToken();
153 Eof.setLocation(Tok.getLocation());
154 LA->Toks.push_back(Eof);
155 } else {
156 ParseGNUAttributeArgs(AttrName, AttrNameLoc, attrs, endLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000157 }
158 } else {
John McCall0b7e6782011-03-24 11:26:52 +0000159 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc,
160 0, SourceLocation(), 0, 0);
Reid Spencer5f016e22007-07-11 17:01:13 +0000161 }
162 }
163 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
Reid Spencer5f016e22007-07-11 17:01:13 +0000164 SkipUntil(tok::r_paren, false);
Sean Huntbbd37c62009-11-21 08:43:09 +0000165 SourceLocation Loc = Tok.getLocation();
Sebastian Redlab197ba2009-02-09 18:23:29 +0000166 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) {
167 SkipUntil(tok::r_paren, false);
168 }
John McCall7f040a92010-12-24 02:08:15 +0000169 if (endLoc)
170 *endLoc = Loc;
Reid Spencer5f016e22007-07-11 17:01:13 +0000171 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000172}
173
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000174
175/// Parse the arguments to a parameterized GNU attribute
176void Parser::ParseGNUAttributeArgs(IdentifierInfo *AttrName,
177 SourceLocation AttrNameLoc,
178 ParsedAttributes &Attrs,
179 SourceLocation *EndLoc) {
180
181 assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('");
182
183 // Availability attributes have their own grammar.
184 if (AttrName->isStr("availability")) {
185 ParseAvailabilityAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc);
186 return;
187 }
188 // Thread safety attributes fit into the FIXME case above, so we
189 // just parse the arguments as a list of expressions
190 if (IsThreadSafetyAttribute(AttrName->getName())) {
191 ParseThreadSafetyAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc);
192 return;
193 }
194
195 ConsumeParen(); // ignore the left paren loc for now
196
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000197 IdentifierInfo *ParmName = 0;
198 SourceLocation ParmLoc;
199 bool BuiltinType = false;
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000200
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000201 switch (Tok.getKind()) {
202 case tok::kw_char:
203 case tok::kw_wchar_t:
204 case tok::kw_char16_t:
205 case tok::kw_char32_t:
206 case tok::kw_bool:
207 case tok::kw_short:
208 case tok::kw_int:
209 case tok::kw_long:
210 case tok::kw___int64:
Richard Smith5a5a9712012-04-04 06:24:32 +0000211 case tok::kw___int128:
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000212 case tok::kw_signed:
213 case tok::kw_unsigned:
214 case tok::kw_float:
215 case tok::kw_double:
216 case tok::kw_void:
217 case tok::kw_typeof:
218 // __attribute__(( vec_type_hint(char) ))
219 // FIXME: Don't just discard the builtin type token.
220 ConsumeToken();
221 BuiltinType = true;
222 break;
223
224 case tok::identifier:
225 ParmName = Tok.getIdentifierInfo();
226 ParmLoc = ConsumeToken();
227 break;
228
229 default:
230 break;
231 }
232
233 ExprVector ArgExprs(Actions);
234
235 if (!BuiltinType &&
236 (ParmLoc.isValid() ? Tok.is(tok::comma) : Tok.isNot(tok::r_paren))) {
237 // Eat the comma.
238 if (ParmLoc.isValid())
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000239 ConsumeToken();
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000240
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000241 // Parse the non-empty comma-separated list of expressions.
242 while (1) {
243 ExprResult ArgExpr(ParseAssignmentExpression());
244 if (ArgExpr.isInvalid()) {
245 SkipUntil(tok::r_paren);
246 return;
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000247 }
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000248 ArgExprs.push_back(ArgExpr.release());
249 if (Tok.isNot(tok::comma))
250 break;
251 ConsumeToken(); // Eat the comma, move to the next argument
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000252 }
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000253 }
Fariborz Jahanian7a81e412011-10-18 17:11:10 +0000254 else if (Tok.is(tok::less) && AttrName->isStr("iboutletcollection")) {
255 if (!ExpectAndConsume(tok::less, diag::err_expected_less_after, "<",
256 tok::greater)) {
Fariborz Jahanianb2243432011-10-18 23:13:50 +0000257 while (Tok.is(tok::identifier)) {
258 ConsumeToken();
259 if (Tok.is(tok::greater))
260 break;
261 if (Tok.is(tok::comma)) {
262 ConsumeToken();
263 continue;
264 }
265 }
266 if (Tok.isNot(tok::greater))
267 Diag(Tok, diag::err_iboutletcollection_with_protocol);
Fariborz Jahanian7a81e412011-10-18 17:11:10 +0000268 SkipUntil(tok::r_paren, false, true); // skip until ')'
269 }
270 }
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000271
272 SourceLocation RParen = Tok.getLocation();
273 if (!ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) {
274 AttributeList *attr =
Argyrios Kyrtzidisffcc3102011-09-13 16:05:53 +0000275 Attrs.addNew(AttrName, SourceRange(AttrNameLoc, RParen), 0, AttrNameLoc,
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000276 ParmName, ParmLoc, ArgExprs.take(), ArgExprs.size());
Michael Hane53ac8a2012-03-07 00:12:16 +0000277 if (BuiltinType && attr->getKind() == AttributeList::AT_iboutletcollection)
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000278 Diag(Tok, diag::err_iboutletcollection_builtintype);
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000279 }
280}
281
282
Eli Friedmana23b4852009-06-08 07:21:15 +0000283/// ParseMicrosoftDeclSpec - Parse an __declspec construct
284///
285/// [MS] decl-specifier:
286/// __declspec ( extended-decl-modifier-seq )
287///
288/// [MS] extended-decl-modifier-seq:
289/// extended-decl-modifier[opt]
290/// extended-decl-modifier extended-decl-modifier-seq
291
John McCall7f040a92010-12-24 02:08:15 +0000292void Parser::ParseMicrosoftDeclSpec(ParsedAttributes &attrs) {
Steve Narofff59e17e2008-12-24 20:59:21 +0000293 assert(Tok.is(tok::kw___declspec) && "Not a declspec!");
Eli Friedmana23b4852009-06-08 07:21:15 +0000294
Steve Narofff59e17e2008-12-24 20:59:21 +0000295 ConsumeToken();
Eli Friedmana23b4852009-06-08 07:21:15 +0000296 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
297 "declspec")) {
298 SkipUntil(tok::r_paren, true); // skip until ) or ;
John McCall7f040a92010-12-24 02:08:15 +0000299 return;
Eli Friedmana23b4852009-06-08 07:21:15 +0000300 }
Francois Pichet373197b2011-05-07 19:04:49 +0000301
Eli Friedman290eeb02009-06-08 23:27:34 +0000302 while (Tok.getIdentifierInfo()) {
Eli Friedmana23b4852009-06-08 07:21:15 +0000303 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
304 SourceLocation AttrNameLoc = ConsumeToken();
Francois Pichet373197b2011-05-07 19:04:49 +0000305
306 // FIXME: Remove this when we have proper __declspec(property()) support.
307 // Just skip everything inside property().
308 if (AttrName->getName() == "property") {
309 ConsumeParen();
310 SkipUntil(tok::r_paren);
311 }
Eli Friedmana23b4852009-06-08 07:21:15 +0000312 if (Tok.is(tok::l_paren)) {
313 ConsumeParen();
314 // FIXME: This doesn't parse __declspec(property(get=get_func_name))
315 // correctly.
John McCall60d7b3a2010-08-24 06:29:42 +0000316 ExprResult ArgExpr(ParseAssignmentExpression());
Eli Friedmana23b4852009-06-08 07:21:15 +0000317 if (!ArgExpr.isInvalid()) {
John McCallca0408f2010-08-23 06:44:23 +0000318 Expr *ExprList = ArgExpr.take();
John McCall0b7e6782011-03-24 11:26:52 +0000319 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
320 SourceLocation(), &ExprList, 1, true);
Eli Friedmana23b4852009-06-08 07:21:15 +0000321 }
322 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
323 SkipUntil(tok::r_paren, false);
324 } else {
John McCall0b7e6782011-03-24 11:26:52 +0000325 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc,
326 0, SourceLocation(), 0, 0, true);
Eli Friedmana23b4852009-06-08 07:21:15 +0000327 }
328 }
329 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
330 SkipUntil(tok::r_paren, false);
John McCall7f040a92010-12-24 02:08:15 +0000331 return;
Eli Friedman290eeb02009-06-08 23:27:34 +0000332}
333
John McCall7f040a92010-12-24 02:08:15 +0000334void Parser::ParseMicrosoftTypeAttributes(ParsedAttributes &attrs) {
Eli Friedman290eeb02009-06-08 23:27:34 +0000335 // Treat these like attributes
336 // FIXME: Allow Sema to distinguish between these and real attributes!
337 while (Tok.is(tok::kw___fastcall) || Tok.is(tok::kw___stdcall) ||
Douglas Gregorf813a2c2010-05-18 16:57:00 +0000338 Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___cdecl) ||
Francois Pichet3bd9aa42011-08-18 09:59:55 +0000339 Tok.is(tok::kw___ptr64) || Tok.is(tok::kw___w64) ||
Francois Pichet58fd97a2011-08-25 00:36:46 +0000340 Tok.is(tok::kw___ptr32) ||
Francois Pichet3bd9aa42011-08-18 09:59:55 +0000341 Tok.is(tok::kw___unaligned)) {
Eli Friedman290eeb02009-06-08 23:27:34 +0000342 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
343 SourceLocation AttrNameLoc = ConsumeToken();
Francois Pichet58fd97a2011-08-25 00:36:46 +0000344 if (Tok.is(tok::kw___ptr64) || Tok.is(tok::kw___w64) ||
345 Tok.is(tok::kw___ptr32))
Eli Friedman290eeb02009-06-08 23:27:34 +0000346 // FIXME: Support these properly!
347 continue;
John McCall0b7e6782011-03-24 11:26:52 +0000348 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
349 SourceLocation(), 0, 0, true);
Eli Friedman290eeb02009-06-08 23:27:34 +0000350 }
Steve Narofff59e17e2008-12-24 20:59:21 +0000351}
352
John McCall7f040a92010-12-24 02:08:15 +0000353void Parser::ParseBorlandTypeAttributes(ParsedAttributes &attrs) {
Dawn Perchik52fc3142010-09-03 01:29:35 +0000354 // Treat these like attributes
355 while (Tok.is(tok::kw___pascal)) {
356 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
357 SourceLocation AttrNameLoc = ConsumeToken();
John McCall0b7e6782011-03-24 11:26:52 +0000358 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
359 SourceLocation(), 0, 0, true);
Dawn Perchik52fc3142010-09-03 01:29:35 +0000360 }
John McCall7f040a92010-12-24 02:08:15 +0000361}
362
Peter Collingbournef315fa82011-02-14 01:42:53 +0000363void Parser::ParseOpenCLAttributes(ParsedAttributes &attrs) {
364 // Treat these like attributes
365 while (Tok.is(tok::kw___kernel)) {
366 SourceLocation AttrNameLoc = ConsumeToken();
John McCall0b7e6782011-03-24 11:26:52 +0000367 attrs.addNew(PP.getIdentifierInfo("opencl_kernel_function"),
368 AttrNameLoc, 0, AttrNameLoc, 0,
369 SourceLocation(), 0, 0, false);
Peter Collingbournef315fa82011-02-14 01:42:53 +0000370 }
371}
372
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000373void Parser::ParseOpenCLQualifiers(DeclSpec &DS) {
374 SourceLocation Loc = Tok.getLocation();
375 switch(Tok.getKind()) {
376 // OpenCL qualifiers:
377 case tok::kw___private:
378 case tok::kw_private:
John McCall0b7e6782011-03-24 11:26:52 +0000379 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000380 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000381 PP.getIdentifierInfo("address_space"), Loc, 0);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000382 break;
383
384 case tok::kw___global:
John McCall0b7e6782011-03-24 11:26:52 +0000385 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000386 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000387 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_global);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000388 break;
389
390 case tok::kw___local:
John McCall0b7e6782011-03-24 11:26:52 +0000391 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000392 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000393 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_local);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000394 break;
395
396 case tok::kw___constant:
John McCall0b7e6782011-03-24 11:26:52 +0000397 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000398 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000399 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_constant);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000400 break;
401
402 case tok::kw___read_only:
John McCall0b7e6782011-03-24 11:26:52 +0000403 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000404 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000405 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_read_only);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000406 break;
407
408 case tok::kw___write_only:
John McCall0b7e6782011-03-24 11:26:52 +0000409 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000410 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000411 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_write_only);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000412 break;
413
414 case tok::kw___read_write:
John McCall0b7e6782011-03-24 11:26:52 +0000415 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000416 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000417 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_read_write);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000418 break;
419 default: break;
420 }
421}
422
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000423/// \brief Parse a version number.
424///
425/// version:
426/// simple-integer
427/// simple-integer ',' simple-integer
428/// simple-integer ',' simple-integer ',' simple-integer
429VersionTuple Parser::ParseVersionTuple(SourceRange &Range) {
430 Range = Tok.getLocation();
431
432 if (!Tok.is(tok::numeric_constant)) {
433 Diag(Tok, diag::err_expected_version);
434 SkipUntil(tok::comma, tok::r_paren, true, true, true);
435 return VersionTuple();
436 }
437
438 // Parse the major (and possibly minor and subminor) versions, which
439 // are stored in the numeric constant. We utilize a quirk of the
440 // lexer, which is that it handles something like 1.2.3 as a single
441 // numeric constant, rather than two separate tokens.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000442 SmallString<512> Buffer;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000443 Buffer.resize(Tok.getLength()+1);
444 const char *ThisTokBegin = &Buffer[0];
445
446 // Get the spelling of the token, which eliminates trigraphs, etc.
447 bool Invalid = false;
448 unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin, &Invalid);
449 if (Invalid)
450 return VersionTuple();
451
452 // Parse the major version.
453 unsigned AfterMajor = 0;
454 unsigned Major = 0;
455 while (AfterMajor < ActualLength && isdigit(ThisTokBegin[AfterMajor])) {
456 Major = Major * 10 + ThisTokBegin[AfterMajor] - '0';
457 ++AfterMajor;
458 }
459
460 if (AfterMajor == 0) {
461 Diag(Tok, diag::err_expected_version);
462 SkipUntil(tok::comma, tok::r_paren, true, true, true);
463 return VersionTuple();
464 }
465
466 if (AfterMajor == ActualLength) {
467 ConsumeToken();
468
469 // We only had a single version component.
470 if (Major == 0) {
471 Diag(Tok, diag::err_zero_version);
472 return VersionTuple();
473 }
474
475 return VersionTuple(Major);
476 }
477
478 if (ThisTokBegin[AfterMajor] != '.' || (AfterMajor + 1 == ActualLength)) {
479 Diag(Tok, diag::err_expected_version);
480 SkipUntil(tok::comma, tok::r_paren, true, true, true);
481 return VersionTuple();
482 }
483
484 // Parse the minor version.
485 unsigned AfterMinor = AfterMajor + 1;
486 unsigned Minor = 0;
487 while (AfterMinor < ActualLength && isdigit(ThisTokBegin[AfterMinor])) {
488 Minor = Minor * 10 + ThisTokBegin[AfterMinor] - '0';
489 ++AfterMinor;
490 }
491
492 if (AfterMinor == ActualLength) {
493 ConsumeToken();
494
495 // We had major.minor.
496 if (Major == 0 && Minor == 0) {
497 Diag(Tok, diag::err_zero_version);
498 return VersionTuple();
499 }
500
501 return VersionTuple(Major, Minor);
502 }
503
504 // If what follows is not a '.', we have a problem.
505 if (ThisTokBegin[AfterMinor] != '.') {
506 Diag(Tok, diag::err_expected_version);
507 SkipUntil(tok::comma, tok::r_paren, true, true, true);
508 return VersionTuple();
509 }
510
511 // Parse the subminor version.
512 unsigned AfterSubminor = AfterMinor + 1;
513 unsigned Subminor = 0;
514 while (AfterSubminor < ActualLength && isdigit(ThisTokBegin[AfterSubminor])) {
515 Subminor = Subminor * 10 + ThisTokBegin[AfterSubminor] - '0';
516 ++AfterSubminor;
517 }
518
519 if (AfterSubminor != ActualLength) {
520 Diag(Tok, diag::err_expected_version);
521 SkipUntil(tok::comma, tok::r_paren, true, true, true);
522 return VersionTuple();
523 }
524 ConsumeToken();
525 return VersionTuple(Major, Minor, Subminor);
526}
527
528/// \brief Parse the contents of the "availability" attribute.
529///
530/// availability-attribute:
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000531/// 'availability' '(' platform ',' version-arg-list, opt-message')'
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000532///
533/// platform:
534/// identifier
535///
536/// version-arg-list:
537/// version-arg
538/// version-arg ',' version-arg-list
539///
540/// version-arg:
541/// 'introduced' '=' version
542/// 'deprecated' '=' version
Douglas Gregor93a70672012-03-11 04:53:21 +0000543/// 'obsoleted' = version
Douglas Gregorb53e4172011-03-26 03:35:55 +0000544/// 'unavailable'
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000545/// opt-message:
546/// 'message' '=' <string>
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000547void Parser::ParseAvailabilityAttribute(IdentifierInfo &Availability,
548 SourceLocation AvailabilityLoc,
549 ParsedAttributes &attrs,
550 SourceLocation *endLoc) {
551 SourceLocation PlatformLoc;
552 IdentifierInfo *Platform = 0;
553
554 enum { Introduced, Deprecated, Obsoleted, Unknown };
555 AvailabilityChange Changes[Unknown];
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000556 ExprResult MessageExpr;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000557
558 // Opening '('.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000559 BalancedDelimiterTracker T(*this, tok::l_paren);
560 if (T.consumeOpen()) {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000561 Diag(Tok, diag::err_expected_lparen);
562 return;
563 }
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000564
565 // Parse the platform name,
566 if (Tok.isNot(tok::identifier)) {
567 Diag(Tok, diag::err_availability_expected_platform);
568 SkipUntil(tok::r_paren);
569 return;
570 }
571 Platform = Tok.getIdentifierInfo();
572 PlatformLoc = ConsumeToken();
573
574 // Parse the ',' following the platform name.
575 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "", tok::r_paren))
576 return;
577
578 // If we haven't grabbed the pointers for the identifiers
579 // "introduced", "deprecated", and "obsoleted", do so now.
580 if (!Ident_introduced) {
581 Ident_introduced = PP.getIdentifierInfo("introduced");
582 Ident_deprecated = PP.getIdentifierInfo("deprecated");
583 Ident_obsoleted = PP.getIdentifierInfo("obsoleted");
Douglas Gregorb53e4172011-03-26 03:35:55 +0000584 Ident_unavailable = PP.getIdentifierInfo("unavailable");
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000585 Ident_message = PP.getIdentifierInfo("message");
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000586 }
587
588 // Parse the set of introductions/deprecations/removals.
Douglas Gregorb53e4172011-03-26 03:35:55 +0000589 SourceLocation UnavailableLoc;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000590 do {
591 if (Tok.isNot(tok::identifier)) {
592 Diag(Tok, diag::err_availability_expected_change);
593 SkipUntil(tok::r_paren);
594 return;
595 }
596 IdentifierInfo *Keyword = Tok.getIdentifierInfo();
597 SourceLocation KeywordLoc = ConsumeToken();
598
Douglas Gregorb53e4172011-03-26 03:35:55 +0000599 if (Keyword == Ident_unavailable) {
600 if (UnavailableLoc.isValid()) {
601 Diag(KeywordLoc, diag::err_availability_redundant)
602 << Keyword << SourceRange(UnavailableLoc);
603 }
604 UnavailableLoc = KeywordLoc;
605
606 if (Tok.isNot(tok::comma))
607 break;
608
609 ConsumeToken();
610 continue;
611 }
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000612
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000613 if (Tok.isNot(tok::equal)) {
614 Diag(Tok, diag::err_expected_equal_after)
615 << Keyword;
616 SkipUntil(tok::r_paren);
617 return;
618 }
619 ConsumeToken();
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000620 if (Keyword == Ident_message) {
621 if (!isTokenStringLiteral()) {
622 Diag(Tok, diag::err_expected_string_literal);
623 SkipUntil(tok::r_paren);
624 return;
625 }
626 MessageExpr = ParseStringLiteralExpression();
627 break;
628 }
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000629
630 SourceRange VersionRange;
631 VersionTuple Version = ParseVersionTuple(VersionRange);
632
633 if (Version.empty()) {
634 SkipUntil(tok::r_paren);
635 return;
636 }
637
638 unsigned Index;
639 if (Keyword == Ident_introduced)
640 Index = Introduced;
641 else if (Keyword == Ident_deprecated)
642 Index = Deprecated;
643 else if (Keyword == Ident_obsoleted)
644 Index = Obsoleted;
645 else
646 Index = Unknown;
647
648 if (Index < Unknown) {
649 if (!Changes[Index].KeywordLoc.isInvalid()) {
650 Diag(KeywordLoc, diag::err_availability_redundant)
651 << Keyword
652 << SourceRange(Changes[Index].KeywordLoc,
653 Changes[Index].VersionRange.getEnd());
654 }
655
656 Changes[Index].KeywordLoc = KeywordLoc;
657 Changes[Index].Version = Version;
658 Changes[Index].VersionRange = VersionRange;
659 } else {
660 Diag(KeywordLoc, diag::err_availability_unknown_change)
661 << Keyword << VersionRange;
662 }
663
664 if (Tok.isNot(tok::comma))
665 break;
666
667 ConsumeToken();
668 } while (true);
669
670 // Closing ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000671 if (T.consumeClose())
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000672 return;
673
674 if (endLoc)
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000675 *endLoc = T.getCloseLocation();
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000676
Douglas Gregorb53e4172011-03-26 03:35:55 +0000677 // The 'unavailable' availability cannot be combined with any other
678 // availability changes. Make sure that hasn't happened.
679 if (UnavailableLoc.isValid()) {
680 bool Complained = false;
681 for (unsigned Index = Introduced; Index != Unknown; ++Index) {
682 if (Changes[Index].KeywordLoc.isValid()) {
683 if (!Complained) {
684 Diag(UnavailableLoc, diag::warn_availability_and_unavailable)
685 << SourceRange(Changes[Index].KeywordLoc,
686 Changes[Index].VersionRange.getEnd());
687 Complained = true;
688 }
689
690 // Clear out the availability.
691 Changes[Index] = AvailabilityChange();
692 }
693 }
694 }
695
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000696 // Record this attribute
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000697 attrs.addNew(&Availability,
698 SourceRange(AvailabilityLoc, T.getCloseLocation()),
Fariborz Jahanianf96708d2012-01-23 23:38:32 +0000699 0, AvailabilityLoc,
John McCall0b7e6782011-03-24 11:26:52 +0000700 Platform, PlatformLoc,
701 Changes[Introduced],
702 Changes[Deprecated],
Douglas Gregorb53e4172011-03-26 03:35:55 +0000703 Changes[Obsoleted],
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000704 UnavailableLoc, MessageExpr.take(),
705 false, false);
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000706}
707
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000708
709// Late Parsed Attributes:
710// See other examples of late parsing in lib/Parse/ParseCXXInlineMethods
711
712void Parser::LateParsedDeclaration::ParseLexedAttributes() {}
713
714void Parser::LateParsedClass::ParseLexedAttributes() {
715 Self->ParseLexedAttributes(*Class);
716}
717
718void Parser::LateParsedAttribute::ParseLexedAttributes() {
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000719 Self->ParseLexedAttribute(*this, true, false);
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000720}
721
722/// Wrapper class which calls ParseLexedAttribute, after setting up the
723/// scope appropriately.
724void Parser::ParseLexedAttributes(ParsingClass &Class) {
725 // Deal with templates
726 // FIXME: Test cases to make sure this does the right thing for templates.
727 bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope;
728 ParseScope ClassTemplateScope(this, Scope::TemplateParamScope,
729 HasTemplateScope);
730 if (HasTemplateScope)
731 Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate);
732
Douglas Gregorcefc3af2012-04-16 07:05:22 +0000733 // Set or update the scope flags.
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000734 bool AlreadyHasClassScope = Class.TopLevelClass;
Douglas Gregorcefc3af2012-04-16 07:05:22 +0000735 unsigned ScopeFlags = Scope::ClassScope|Scope::DeclScope;
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000736 ParseScope ClassScope(this, ScopeFlags, !AlreadyHasClassScope);
737 ParseScopeFlags ClassScopeFlags(this, ScopeFlags, AlreadyHasClassScope);
738
DeLesley Hutchinscf2fa2f2012-04-06 15:10:17 +0000739 // Enter the scope of nested classes
740 if (!AlreadyHasClassScope)
741 Actions.ActOnStartDelayedMemberDeclarations(getCurScope(),
742 Class.TagOrTemplate);
Douglas Gregorcefc3af2012-04-16 07:05:22 +0000743 {
744 // Allow 'this' within late-parsed attributes.
745 Sema::CXXThisScopeRAII ThisScope(Actions, Class.TagOrTemplate,
746 /*TypeQuals=*/0);
747
748 for (unsigned i = 0, ni = Class.LateParsedDeclarations.size(); i < ni; ++i){
749 Class.LateParsedDeclarations[i]->ParseLexedAttributes();
750 }
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000751 }
Douglas Gregorcefc3af2012-04-16 07:05:22 +0000752
DeLesley Hutchinscf2fa2f2012-04-06 15:10:17 +0000753 if (!AlreadyHasClassScope)
754 Actions.ActOnFinishDelayedMemberDeclarations(getCurScope(),
755 Class.TagOrTemplate);
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000756}
757
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000758
759/// \brief Parse all attributes in LAs, and attach them to Decl D.
760void Parser::ParseLexedAttributeList(LateParsedAttrList &LAs, Decl *D,
761 bool EnterScope, bool OnDefinition) {
762 for (unsigned i = 0, ni = LAs.size(); i < ni; ++i) {
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +0000763 LAs[i]->addDecl(D);
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000764 ParseLexedAttribute(*LAs[i], EnterScope, OnDefinition);
Benjamin Kramerd306cf72012-04-14 12:44:47 +0000765 delete LAs[i];
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000766 }
767 LAs.clear();
768}
769
770
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000771/// \brief Finish parsing an attribute for which parsing was delayed.
772/// This will be called at the end of parsing a class declaration
773/// for each LateParsedAttribute. We consume the saved tokens and
774/// create an attribute with the arguments filled in. We add this
775/// to the Attribute list for the decl.
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000776void Parser::ParseLexedAttribute(LateParsedAttribute &LA,
777 bool EnterScope, bool OnDefinition) {
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000778 // Save the current token position.
779 SourceLocation OrigLoc = Tok.getLocation();
780
781 // Append the current token at the end of the new token stream so that it
782 // doesn't get lost.
783 LA.Toks.push_back(Tok);
784 PP.EnterTokenStream(LA.Toks.data(), LA.Toks.size(), true, false);
785 // Consume the previously pushed token.
786 ConsumeAnyToken();
787
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000788 if (OnDefinition && !IsThreadSafetyAttribute(LA.AttrName.getName())) {
789 Diag(Tok, diag::warn_attribute_on_function_definition)
790 << LA.AttrName.getName();
791 }
792
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000793 ParsedAttributes Attrs(AttrFactory);
794 SourceLocation endLoc;
795
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +0000796 if (LA.Decls.size() == 1) {
797 Decl *D = LA.Decls[0];
Caitlin Sadowskied9d84a2011-09-08 17:42:31 +0000798
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +0000799 // If the Decl is templatized, add template parameters to scope.
800 bool HasTemplateScope = EnterScope && D->isTemplateDecl();
801 ParseScope TempScope(this, Scope::TemplateParamScope, HasTemplateScope);
802 if (HasTemplateScope)
803 Actions.ActOnReenterTemplateScope(Actions.CurScope, D);
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000804
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +0000805 // If the Decl is on a function, add function parameters to the scope.
806 bool HasFunctionScope = EnterScope && D->isFunctionOrFunctionTemplate();
807 ParseScope FnScope(this, Scope::FnScope|Scope::DeclScope, HasFunctionScope);
808 if (HasFunctionScope)
809 Actions.ActOnReenterFunctionContext(Actions.CurScope, D);
810
811 ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, &endLoc);
812
813 if (HasFunctionScope) {
814 Actions.ActOnExitFunctionContext();
815 FnScope.Exit(); // Pop scope, and remove Decls from IdResolver
816 }
817 if (HasTemplateScope) {
818 TempScope.Exit();
819 }
DeLesley Hutchins7ec419a2012-03-02 22:29:50 +0000820 } else if (LA.Decls.size() > 0) {
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +0000821 // If there are multiple decls, then the decl cannot be within the
822 // function scope.
823 ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, &endLoc);
DeLesley Hutchins7ec419a2012-03-02 22:29:50 +0000824 } else {
825 Diag(Tok, diag::warn_attribute_no_decl) << LA.AttrName.getName();
Caitlin Sadowskied9d84a2011-09-08 17:42:31 +0000826 }
827
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +0000828 for (unsigned i = 0, ni = LA.Decls.size(); i < ni; ++i) {
829 Actions.ActOnFinishDelayedAttribute(getCurScope(), LA.Decls[i], Attrs);
830 }
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000831
832 if (Tok.getLocation() != OrigLoc) {
833 // Due to a parsing error, we either went over the cached tokens or
834 // there are still cached tokens left, so we skip the leftover tokens.
835 // Since this is an uncommon situation that should be avoided, use the
836 // expensive isBeforeInTranslationUnit call.
837 if (PP.getSourceManager().isBeforeInTranslationUnit(Tok.getLocation(),
838 OrigLoc))
839 while (Tok.getLocation() != OrigLoc && Tok.isNot(tok::eof))
Douglas Gregord78ef5b2012-03-08 01:00:17 +0000840 ConsumeAnyToken();
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000841 }
842}
843
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000844/// \brief Wrapper around a case statement checking if AttrName is
845/// one of the thread safety attributes
846bool Parser::IsThreadSafetyAttribute(llvm::StringRef AttrName){
847 return llvm::StringSwitch<bool>(AttrName)
848 .Case("guarded_by", true)
849 .Case("guarded_var", true)
850 .Case("pt_guarded_by", true)
851 .Case("pt_guarded_var", true)
852 .Case("lockable", true)
853 .Case("scoped_lockable", true)
854 .Case("no_thread_safety_analysis", true)
855 .Case("acquired_after", true)
856 .Case("acquired_before", true)
857 .Case("exclusive_lock_function", true)
858 .Case("shared_lock_function", true)
859 .Case("exclusive_trylock_function", true)
860 .Case("shared_trylock_function", true)
861 .Case("unlock_function", true)
862 .Case("lock_returned", true)
863 .Case("locks_excluded", true)
864 .Case("exclusive_locks_required", true)
865 .Case("shared_locks_required", true)
866 .Default(false);
867}
868
869/// \brief Parse the contents of thread safety attributes. These
870/// should always be parsed as an expression list.
871///
872/// We need to special case the parsing due to the fact that if the first token
873/// of the first argument is an identifier, the main parse loop will store
874/// that token as a "parameter" and the rest of
875/// the arguments will be added to a list of "arguments". However,
876/// subsequent tokens in the first argument are lost. We instead parse each
877/// argument as an expression and add all arguments to the list of "arguments".
878/// In future, we will take advantage of this special case to also
879/// deal with some argument scoping issues here (for example, referring to a
880/// function parameter in the attribute on that function).
881void Parser::ParseThreadSafetyAttribute(IdentifierInfo &AttrName,
882 SourceLocation AttrNameLoc,
883 ParsedAttributes &Attrs,
884 SourceLocation *EndLoc) {
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000885 assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('");
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000886
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000887 BalancedDelimiterTracker T(*this, tok::l_paren);
888 T.consumeOpen();
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000889
890 ExprVector ArgExprs(Actions);
891 bool ArgExprsOk = true;
892
893 // now parse the list of expressions
DeLesley Hutchins4805f152011-12-14 19:36:06 +0000894 while (Tok.isNot(tok::r_paren)) {
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000895 ExprResult ArgExpr(ParseAssignmentExpression());
896 if (ArgExpr.isInvalid()) {
897 ArgExprsOk = false;
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000898 T.consumeClose();
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000899 break;
900 } else {
901 ArgExprs.push_back(ArgExpr.release());
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000902 }
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000903 if (Tok.isNot(tok::comma))
904 break;
905 ConsumeToken(); // Eat the comma, move to the next argument
906 }
907 // Match the ')'.
DeLesley Hutchins23323e02012-01-20 22:50:54 +0000908 if (ArgExprsOk && !T.consumeClose()) {
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000909 Attrs.addNew(&AttrName, AttrNameLoc, 0, AttrNameLoc, 0, SourceLocation(),
910 ArgExprs.take(), ArgExprs.size());
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000911 }
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000912 if (EndLoc)
913 *EndLoc = T.getCloseLocation();
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000914}
915
Richard Smith6ee326a2012-04-10 01:32:12 +0000916/// DiagnoseProhibitedCXX11Attribute - We have found the opening square brackets
917/// of a C++11 attribute-specifier in a location where an attribute is not
918/// permitted. By C++11 [dcl.attr.grammar]p6, this is ill-formed. Diagnose this
919/// situation.
920///
921/// \return \c true if we skipped an attribute-like chunk of tokens, \c false if
922/// this doesn't appear to actually be an attribute-specifier, and the caller
923/// should try to parse it.
924bool Parser::DiagnoseProhibitedCXX11Attribute() {
925 assert(Tok.is(tok::l_square) && NextToken().is(tok::l_square));
926
927 switch (isCXX11AttributeSpecifier(/*Disambiguate*/true)) {
928 case CAK_NotAttributeSpecifier:
929 // No diagnostic: we're in Obj-C++11 and this is not actually an attribute.
930 return false;
931
932 case CAK_InvalidAttributeSpecifier:
933 Diag(Tok.getLocation(), diag::err_l_square_l_square_not_attribute);
934 return false;
935
936 case CAK_AttributeSpecifier:
937 // Parse and discard the attributes.
938 SourceLocation BeginLoc = ConsumeBracket();
939 ConsumeBracket();
940 SkipUntil(tok::r_square, /*StopAtSemi*/ false);
941 assert(Tok.is(tok::r_square) && "isCXX11AttributeSpecifier lied");
942 SourceLocation EndLoc = ConsumeBracket();
943 Diag(BeginLoc, diag::err_attributes_not_allowed)
944 << SourceRange(BeginLoc, EndLoc);
945 return true;
946 }
Chandler Carruth2c6dbd72012-04-10 16:03:08 +0000947 llvm_unreachable("All cases handled above.");
Richard Smith6ee326a2012-04-10 01:32:12 +0000948}
949
John McCall7f040a92010-12-24 02:08:15 +0000950void Parser::DiagnoseProhibitedAttributes(ParsedAttributesWithRange &attrs) {
951 Diag(attrs.Range.getBegin(), diag::err_attributes_not_allowed)
952 << attrs.Range;
Dawn Perchik52fc3142010-09-03 01:29:35 +0000953}
954
Reid Spencer5f016e22007-07-11 17:01:13 +0000955/// ParseDeclaration - Parse a full 'declaration', which consists of
956/// declaration-specifiers, some number of declarators, and a semicolon.
Chris Lattner97144fc2009-04-02 04:16:50 +0000957/// 'Context' should be a Declarator::TheContext value. This returns the
958/// location of the semicolon in DeclEnd.
Chris Lattner8f08cb72007-08-25 06:57:03 +0000959///
960/// declaration: [C99 6.7]
961/// block-declaration ->
962/// simple-declaration
963/// others [FIXME]
Douglas Gregoradcac882008-12-01 23:54:00 +0000964/// [C++] template-declaration
Chris Lattner8f08cb72007-08-25 06:57:03 +0000965/// [C++] namespace-definition
Douglas Gregorf780abc2008-12-30 03:27:21 +0000966/// [C++] using-directive
Douglas Gregord7f37bf2009-06-22 23:06:13 +0000967/// [C++] using-declaration
Richard Smith534986f2012-04-14 00:33:13 +0000968/// [C++11/C11] static_assert-declaration
Chris Lattner8f08cb72007-08-25 06:57:03 +0000969/// others... [FIXME]
970///
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +0000971Parser::DeclGroupPtrTy Parser::ParseDeclaration(StmtVector &Stmts,
972 unsigned Context,
Sean Huntbbd37c62009-11-21 08:43:09 +0000973 SourceLocation &DeclEnd,
John McCall7f040a92010-12-24 02:08:15 +0000974 ParsedAttributesWithRange &attrs) {
Argyrios Kyrtzidis36d36802010-06-17 10:52:18 +0000975 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Fariborz Jahaniane8cff362011-08-30 17:10:52 +0000976 // Must temporarily exit the objective-c container scope for
977 // parsing c none objective-c decls.
978 ObjCDeclContextSwitch ObjCDC(*this);
Argyrios Kyrtzidis36d36802010-06-17 10:52:18 +0000979
John McCalld226f652010-08-21 09:40:31 +0000980 Decl *SingleDecl = 0;
Richard Smithc89edf52011-07-01 19:46:12 +0000981 Decl *OwnedType = 0;
Chris Lattner8f08cb72007-08-25 06:57:03 +0000982 switch (Tok.getKind()) {
Douglas Gregoradcac882008-12-01 23:54:00 +0000983 case tok::kw_template:
Douglas Gregor1426e532009-05-12 21:31:51 +0000984 case tok::kw_export:
John McCall7f040a92010-12-24 02:08:15 +0000985 ProhibitAttributes(attrs);
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000986 SingleDecl = ParseDeclarationStartingWithTemplate(Context, DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +0000987 break;
Sebastian Redld078e642010-08-27 23:12:46 +0000988 case tok::kw_inline:
Sebastian Redl88e64ca2010-08-31 00:36:45 +0000989 // Could be the start of an inline namespace. Allowed as an ext in C++03.
David Blaikie4e4d0842012-03-11 07:00:24 +0000990 if (getLangOpts().CPlusPlus && NextToken().is(tok::kw_namespace)) {
John McCall7f040a92010-12-24 02:08:15 +0000991 ProhibitAttributes(attrs);
Sebastian Redld078e642010-08-27 23:12:46 +0000992 SourceLocation InlineLoc = ConsumeToken();
993 SingleDecl = ParseNamespace(Context, DeclEnd, InlineLoc);
994 break;
995 }
John McCall7f040a92010-12-24 02:08:15 +0000996 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs,
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +0000997 true);
Chris Lattner8f08cb72007-08-25 06:57:03 +0000998 case tok::kw_namespace:
John McCall7f040a92010-12-24 02:08:15 +0000999 ProhibitAttributes(attrs);
Chris Lattner97144fc2009-04-02 04:16:50 +00001000 SingleDecl = ParseNamespace(Context, DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +00001001 break;
Douglas Gregorf780abc2008-12-30 03:27:21 +00001002 case tok::kw_using:
John McCall78b81052010-11-10 02:40:36 +00001003 SingleDecl = ParseUsingDirectiveOrDeclaration(Context, ParsedTemplateInfo(),
Richard Smithc89edf52011-07-01 19:46:12 +00001004 DeclEnd, attrs, &OwnedType);
Chris Lattner682bf922009-03-29 16:50:03 +00001005 break;
Anders Carlsson511d7ab2009-03-11 16:27:10 +00001006 case tok::kw_static_assert:
Peter Collingbournec6eb44b2011-04-15 00:35:57 +00001007 case tok::kw__Static_assert:
John McCall7f040a92010-12-24 02:08:15 +00001008 ProhibitAttributes(attrs);
Chris Lattner97144fc2009-04-02 04:16:50 +00001009 SingleDecl = ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +00001010 break;
Chris Lattner8f08cb72007-08-25 06:57:03 +00001011 default:
John McCall7f040a92010-12-24 02:08:15 +00001012 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs, true);
Chris Lattner8f08cb72007-08-25 06:57:03 +00001013 }
Sean Huntbbd37c62009-11-21 08:43:09 +00001014
Chris Lattner682bf922009-03-29 16:50:03 +00001015 // This routine returns a DeclGroup, if the thing we parsed only contains a
Richard Smithc89edf52011-07-01 19:46:12 +00001016 // single decl, convert it now. Alias declarations can also declare a type;
1017 // include that too if it is present.
1018 return Actions.ConvertDeclToDeclGroup(SingleDecl, OwnedType);
Chris Lattner8f08cb72007-08-25 06:57:03 +00001019}
1020
1021/// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl]
1022/// declaration-specifiers init-declarator-list[opt] ';'
1023///[C90/C++]init-declarator-list ';' [TODO]
1024/// [OMP] threadprivate-directive [TODO]
Chris Lattnercd147752009-03-29 17:27:48 +00001025///
Richard Smithad762fc2011-04-14 22:09:26 +00001026/// for-range-declaration: [C++0x 6.5p1: stmt.ranged]
1027/// attribute-specifier-seq[opt] type-specifier-seq declarator
1028///
Chris Lattnercd147752009-03-29 17:27:48 +00001029/// If RequireSemi is false, this does not check for a ';' at the end of the
Chris Lattner5c5db552010-04-05 18:18:31 +00001030/// declaration. If it is true, it checks for and eats it.
Richard Smithad762fc2011-04-14 22:09:26 +00001031///
1032/// If FRI is non-null, we might be parsing a for-range-declaration instead
1033/// of a simple-declaration. If we find that we are, we also parse the
1034/// for-range-initializer, and place it here.
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +00001035Parser::DeclGroupPtrTy Parser::ParseSimpleDeclaration(StmtVector &Stmts,
1036 unsigned Context,
Sean Huntbbd37c62009-11-21 08:43:09 +00001037 SourceLocation &DeclEnd,
John McCall7f040a92010-12-24 02:08:15 +00001038 ParsedAttributes &attrs,
Richard Smithad762fc2011-04-14 22:09:26 +00001039 bool RequireSemi,
1040 ForRangeInit *FRI) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001041 // Parse the common declaration-specifiers piece.
John McCall54abf7d2009-11-04 02:18:39 +00001042 ParsingDeclSpec DS(*this);
John McCall7f040a92010-12-24 02:08:15 +00001043 DS.takeAttributesFrom(attrs);
Douglas Gregor312eadb2011-04-24 05:37:28 +00001044
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001045 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS_none,
Richard Smith34b41d92011-02-20 03:19:35 +00001046 getDeclSpecContextFromDeclaratorContext(Context));
Abramo Bagnara06284c12012-01-07 10:52:36 +00001047
Reid Spencer5f016e22007-07-11 17:01:13 +00001048 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
1049 // declaration-specifiers init-declarator-list[opt] ';'
Chris Lattner04d66662007-10-09 17:33:22 +00001050 if (Tok.is(tok::semi)) {
Chris Lattner5c5db552010-04-05 18:18:31 +00001051 if (RequireSemi) ConsumeToken();
John McCalld226f652010-08-21 09:40:31 +00001052 Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none,
Douglas Gregor312eadb2011-04-24 05:37:28 +00001053 DS);
John McCall54abf7d2009-11-04 02:18:39 +00001054 DS.complete(TheDecl);
Chris Lattner682bf922009-03-29 16:50:03 +00001055 return Actions.ConvertDeclToDeclGroup(TheDecl);
Reid Spencer5f016e22007-07-11 17:01:13 +00001056 }
Douglas Gregor312eadb2011-04-24 05:37:28 +00001057
1058 return ParseDeclGroup(DS, Context, /*FunctionDefs=*/ false, &DeclEnd, FRI);
John McCalld8ac0572009-11-03 19:26:08 +00001059}
Mike Stump1eb44332009-09-09 15:08:12 +00001060
Richard Smith0706df42011-10-19 21:33:05 +00001061/// Returns true if this might be the start of a declarator, or a common typo
1062/// for a declarator.
1063bool Parser::MightBeDeclarator(unsigned Context) {
1064 switch (Tok.getKind()) {
1065 case tok::annot_cxxscope:
1066 case tok::annot_template_id:
1067 case tok::caret:
1068 case tok::code_completion:
1069 case tok::coloncolon:
1070 case tok::ellipsis:
1071 case tok::kw___attribute:
1072 case tok::kw_operator:
1073 case tok::l_paren:
1074 case tok::star:
1075 return true;
1076
1077 case tok::amp:
1078 case tok::ampamp:
David Blaikie4e4d0842012-03-11 07:00:24 +00001079 return getLangOpts().CPlusPlus;
Richard Smith0706df42011-10-19 21:33:05 +00001080
Richard Smith1c94c162012-01-09 22:31:44 +00001081 case tok::l_square: // Might be an attribute on an unnamed bit-field.
David Blaikie4e4d0842012-03-11 07:00:24 +00001082 return Context == Declarator::MemberContext && getLangOpts().CPlusPlus0x &&
Richard Smith1c94c162012-01-09 22:31:44 +00001083 NextToken().is(tok::l_square);
1084
1085 case tok::colon: // Might be a typo for '::' or an unnamed bit-field.
David Blaikie4e4d0842012-03-11 07:00:24 +00001086 return Context == Declarator::MemberContext || getLangOpts().CPlusPlus;
Richard Smith1c94c162012-01-09 22:31:44 +00001087
Richard Smith0706df42011-10-19 21:33:05 +00001088 case tok::identifier:
1089 switch (NextToken().getKind()) {
1090 case tok::code_completion:
1091 case tok::coloncolon:
1092 case tok::comma:
1093 case tok::equal:
1094 case tok::equalequal: // Might be a typo for '='.
1095 case tok::kw_alignas:
1096 case tok::kw_asm:
1097 case tok::kw___attribute:
1098 case tok::l_brace:
1099 case tok::l_paren:
1100 case tok::l_square:
1101 case tok::less:
1102 case tok::r_brace:
1103 case tok::r_paren:
1104 case tok::r_square:
1105 case tok::semi:
1106 return true;
1107
1108 case tok::colon:
1109 // At namespace scope, 'identifier:' is probably a typo for 'identifier::'
Richard Smith1c94c162012-01-09 22:31:44 +00001110 // and in block scope it's probably a label. Inside a class definition,
1111 // this is a bit-field.
1112 return Context == Declarator::MemberContext ||
David Blaikie4e4d0842012-03-11 07:00:24 +00001113 (getLangOpts().CPlusPlus && Context == Declarator::FileContext);
Richard Smith1c94c162012-01-09 22:31:44 +00001114
1115 case tok::identifier: // Possible virt-specifier.
David Blaikie4e4d0842012-03-11 07:00:24 +00001116 return getLangOpts().CPlusPlus0x && isCXX0XVirtSpecifier(NextToken());
Richard Smith0706df42011-10-19 21:33:05 +00001117
1118 default:
1119 return false;
1120 }
1121
1122 default:
1123 return false;
1124 }
1125}
1126
Richard Smith994d73f2012-04-11 20:59:20 +00001127/// Skip until we reach something which seems like a sensible place to pick
1128/// up parsing after a malformed declaration. This will sometimes stop sooner
1129/// than SkipUntil(tok::r_brace) would, but will never stop later.
1130void Parser::SkipMalformedDecl() {
1131 while (true) {
1132 switch (Tok.getKind()) {
1133 case tok::l_brace:
1134 // Skip until matching }, then stop. We've probably skipped over
1135 // a malformed class or function definition or similar.
1136 ConsumeBrace();
1137 SkipUntil(tok::r_brace, /*StopAtSemi*/false);
1138 if (Tok.is(tok::comma) || Tok.is(tok::l_brace) || Tok.is(tok::kw_try)) {
1139 // This declaration isn't over yet. Keep skipping.
1140 continue;
1141 }
1142 if (Tok.is(tok::semi))
1143 ConsumeToken();
1144 return;
1145
1146 case tok::l_square:
1147 ConsumeBracket();
1148 SkipUntil(tok::r_square, /*StopAtSemi*/false);
1149 continue;
1150
1151 case tok::l_paren:
1152 ConsumeParen();
1153 SkipUntil(tok::r_paren, /*StopAtSemi*/false);
1154 continue;
1155
1156 case tok::r_brace:
1157 return;
1158
1159 case tok::semi:
1160 ConsumeToken();
1161 return;
1162
1163 case tok::kw_inline:
1164 // 'inline namespace' at the start of a line is almost certainly
1165 // a good place to pick back up parsing.
1166 if (Tok.isAtStartOfLine() && NextToken().is(tok::kw_namespace))
1167 return;
1168 break;
1169
1170 case tok::kw_namespace:
1171 // 'namespace' at the start of a line is almost certainly a good
1172 // place to pick back up parsing.
1173 if (Tok.isAtStartOfLine())
1174 return;
1175 break;
1176
1177 case tok::eof:
1178 return;
1179
1180 default:
1181 break;
1182 }
1183
1184 ConsumeAnyToken();
1185 }
1186}
1187
John McCalld8ac0572009-11-03 19:26:08 +00001188/// ParseDeclGroup - Having concluded that this is either a function
1189/// definition or a group of object declarations, actually parse the
1190/// result.
John McCall54abf7d2009-11-04 02:18:39 +00001191Parser::DeclGroupPtrTy Parser::ParseDeclGroup(ParsingDeclSpec &DS,
1192 unsigned Context,
John McCalld8ac0572009-11-03 19:26:08 +00001193 bool AllowFunctionDefinitions,
Richard Smithad762fc2011-04-14 22:09:26 +00001194 SourceLocation *DeclEnd,
1195 ForRangeInit *FRI) {
John McCalld8ac0572009-11-03 19:26:08 +00001196 // Parse the first declarator.
John McCall54abf7d2009-11-04 02:18:39 +00001197 ParsingDeclarator D(*this, DS, static_cast<Declarator::TheContext>(Context));
John McCalld8ac0572009-11-03 19:26:08 +00001198 ParseDeclarator(D);
Chris Lattnercd147752009-03-29 17:27:48 +00001199
John McCalld8ac0572009-11-03 19:26:08 +00001200 // Bail out if the first declarator didn't seem well-formed.
1201 if (!D.hasName() && !D.mayOmitIdentifier()) {
Richard Smith994d73f2012-04-11 20:59:20 +00001202 SkipMalformedDecl();
John McCalld8ac0572009-11-03 19:26:08 +00001203 return DeclGroupPtrTy();
Chris Lattner23c4b182009-03-29 17:18:04 +00001204 }
Mike Stump1eb44332009-09-09 15:08:12 +00001205
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001206 // Save late-parsed attributes for now; they need to be parsed in the
1207 // appropriate function scope after the function Decl has been constructed.
1208 LateParsedAttrList LateParsedAttrs;
1209 if (D.isFunctionDeclarator())
1210 MaybeParseGNUAttributes(D, &LateParsedAttrs);
1211
Chris Lattnerc82daef2010-07-11 22:24:20 +00001212 // Check to see if we have a function *definition* which must have a body.
1213 if (AllowFunctionDefinitions && D.isFunctionDeclarator() &&
1214 // Look at the next token to make sure that this isn't a function
1215 // declaration. We have to check this because __attribute__ might be the
1216 // start of a function definition in GCC-extended K&R C.
1217 !isDeclarationAfterDeclarator()) {
Richard Smith58196dc2011-11-30 23:45:35 +00001218
Chris Lattner004659a2010-07-11 22:42:07 +00001219 if (isStartOfFunctionDefinition(D)) {
John McCalld8ac0572009-11-03 19:26:08 +00001220 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
1221 Diag(Tok, diag::err_function_declared_typedef);
1222
1223 // Recover by treating the 'typedef' as spurious.
1224 DS.ClearStorageClassSpecs();
1225 }
1226
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001227 Decl *TheDecl =
1228 ParseFunctionDefinition(D, ParsedTemplateInfo(), &LateParsedAttrs);
John McCalld8ac0572009-11-03 19:26:08 +00001229 return Actions.ConvertDeclToDeclGroup(TheDecl);
Chris Lattner004659a2010-07-11 22:42:07 +00001230 }
1231
1232 if (isDeclarationSpecifier()) {
1233 // If there is an invalid declaration specifier right after the function
1234 // prototype, then we must be in a missing semicolon case where this isn't
1235 // actually a body. Just fall through into the code that handles it as a
1236 // prototype, and let the top-level code handle the erroneous declspec
1237 // where it would otherwise expect a comma or semicolon.
John McCalld8ac0572009-11-03 19:26:08 +00001238 } else {
1239 Diag(Tok, diag::err_expected_fn_body);
1240 SkipUntil(tok::semi);
1241 return DeclGroupPtrTy();
1242 }
1243 }
1244
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001245 if (ParseAsmAttributesAfterDeclarator(D))
Richard Smithad762fc2011-04-14 22:09:26 +00001246 return DeclGroupPtrTy();
1247
1248 // C++0x [stmt.iter]p1: Check if we have a for-range-declarator. If so, we
1249 // must parse and analyze the for-range-initializer before the declaration is
1250 // analyzed.
1251 if (FRI && Tok.is(tok::colon)) {
1252 FRI->ColonLoc = ConsumeToken();
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001253 if (Tok.is(tok::l_brace))
1254 FRI->RangeExpr = ParseBraceInitializer();
1255 else
1256 FRI->RangeExpr = ParseExpression();
Richard Smithad762fc2011-04-14 22:09:26 +00001257 Decl *ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
1258 Actions.ActOnCXXForRangeDecl(ThisDecl);
1259 Actions.FinalizeDeclaration(ThisDecl);
John McCall6895a642012-01-27 01:29:43 +00001260 D.complete(ThisDecl);
Richard Smithad762fc2011-04-14 22:09:26 +00001261 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS, &ThisDecl, 1);
1262 }
1263
Chris Lattner5f9e2722011-07-23 10:55:15 +00001264 SmallVector<Decl *, 8> DeclsInGroup;
Richard Smithad762fc2011-04-14 22:09:26 +00001265 Decl *FirstDecl = ParseDeclarationAfterDeclaratorAndAttributes(D);
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001266 if (LateParsedAttrs.size() > 0)
1267 ParseLexedAttributeList(LateParsedAttrs, FirstDecl, true, false);
John McCall54abf7d2009-11-04 02:18:39 +00001268 D.complete(FirstDecl);
John McCalld226f652010-08-21 09:40:31 +00001269 if (FirstDecl)
John McCalld8ac0572009-11-03 19:26:08 +00001270 DeclsInGroup.push_back(FirstDecl);
1271
Richard Smith0706df42011-10-19 21:33:05 +00001272 bool ExpectSemi = Context != Declarator::ForContext;
1273
John McCalld8ac0572009-11-03 19:26:08 +00001274 // If we don't have a comma, it is either the end of the list (a ';') or an
1275 // error, bail out.
1276 while (Tok.is(tok::comma)) {
Richard Smith0706df42011-10-19 21:33:05 +00001277 SourceLocation CommaLoc = ConsumeToken();
1278
1279 if (Tok.isAtStartOfLine() && ExpectSemi && !MightBeDeclarator(Context)) {
1280 // This comma was followed by a line-break and something which can't be
1281 // the start of a declarator. The comma was probably a typo for a
1282 // semicolon.
1283 Diag(CommaLoc, diag::err_expected_semi_declaration)
1284 << FixItHint::CreateReplacement(CommaLoc, ";");
1285 ExpectSemi = false;
1286 break;
1287 }
John McCalld8ac0572009-11-03 19:26:08 +00001288
1289 // Parse the next declarator.
1290 D.clear();
Richard Smith7984de32012-01-12 23:53:29 +00001291 D.setCommaLoc(CommaLoc);
John McCalld8ac0572009-11-03 19:26:08 +00001292
1293 // Accept attributes in an init-declarator. In the first declarator in a
1294 // declaration, these would be part of the declspec. In subsequent
1295 // declarators, they become part of the declarator itself, so that they
1296 // don't apply to declarators after *this* one. Examples:
1297 // short __attribute__((common)) var; -> declspec
1298 // short var __attribute__((common)); -> declarator
1299 // short x, __attribute__((common)) var; -> declarator
John McCall7f040a92010-12-24 02:08:15 +00001300 MaybeParseGNUAttributes(D);
John McCalld8ac0572009-11-03 19:26:08 +00001301
1302 ParseDeclarator(D);
Fariborz Jahanian9baf39d2012-01-13 00:14:12 +00001303 if (!D.isInvalidType()) {
1304 Decl *ThisDecl = ParseDeclarationAfterDeclarator(D);
1305 D.complete(ThisDecl);
1306 if (ThisDecl)
1307 DeclsInGroup.push_back(ThisDecl);
1308 }
John McCalld8ac0572009-11-03 19:26:08 +00001309 }
1310
1311 if (DeclEnd)
1312 *DeclEnd = Tok.getLocation();
1313
Richard Smith0706df42011-10-19 21:33:05 +00001314 if (ExpectSemi &&
Chris Lattner8bb21d32012-04-28 16:12:17 +00001315 ExpectAndConsumeSemi(Context == Declarator::FileContext
1316 ? diag::err_invalid_token_after_toplevel_declarator
1317 : diag::err_expected_semi_declaration)) {
Chris Lattner004659a2010-07-11 22:42:07 +00001318 // Okay, there was no semicolon and one was expected. If we see a
1319 // declaration specifier, just assume it was missing and continue parsing.
1320 // Otherwise things are very confused and we skip to recover.
1321 if (!isDeclarationSpecifier()) {
1322 SkipUntil(tok::r_brace, true, true);
1323 if (Tok.is(tok::semi))
1324 ConsumeToken();
1325 }
John McCalld8ac0572009-11-03 19:26:08 +00001326 }
1327
Douglas Gregor23c94db2010-07-02 17:43:08 +00001328 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS,
John McCalld8ac0572009-11-03 19:26:08 +00001329 DeclsInGroup.data(),
1330 DeclsInGroup.size());
Reid Spencer5f016e22007-07-11 17:01:13 +00001331}
1332
Richard Smithad762fc2011-04-14 22:09:26 +00001333/// Parse an optional simple-asm-expr and attributes, and attach them to a
1334/// declarator. Returns true on an error.
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001335bool Parser::ParseAsmAttributesAfterDeclarator(Declarator &D) {
Richard Smithad762fc2011-04-14 22:09:26 +00001336 // If a simple-asm-expr is present, parse it.
1337 if (Tok.is(tok::kw_asm)) {
1338 SourceLocation Loc;
1339 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
1340 if (AsmLabel.isInvalid()) {
1341 SkipUntil(tok::semi, true, true);
1342 return true;
1343 }
1344
1345 D.setAsmLabel(AsmLabel.release());
1346 D.SetRangeEnd(Loc);
1347 }
1348
1349 MaybeParseGNUAttributes(D);
1350 return false;
1351}
1352
Douglas Gregor1426e532009-05-12 21:31:51 +00001353/// \brief Parse 'declaration' after parsing 'declaration-specifiers
1354/// declarator'. This method parses the remainder of the declaration
1355/// (including any attributes or initializer, among other things) and
1356/// finalizes the declaration.
Reid Spencer5f016e22007-07-11 17:01:13 +00001357///
Reid Spencer5f016e22007-07-11 17:01:13 +00001358/// init-declarator: [C99 6.7]
1359/// declarator
1360/// declarator '=' initializer
1361/// [GNU] declarator simple-asm-expr[opt] attributes[opt]
1362/// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00001363/// [C++] declarator initializer[opt]
1364///
1365/// [C++] initializer:
1366/// [C++] '=' initializer-clause
1367/// [C++] '(' expression-list ')'
Sebastian Redl50de12f2009-03-24 22:27:57 +00001368/// [C++0x] '=' 'default' [TODO]
1369/// [C++0x] '=' 'delete'
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001370/// [C++0x] braced-init-list
Sebastian Redl50de12f2009-03-24 22:27:57 +00001371///
1372/// According to the standard grammar, =default and =delete are function
1373/// definitions, but that definitely doesn't fit with the parser here.
Reid Spencer5f016e22007-07-11 17:01:13 +00001374///
John McCalld226f652010-08-21 09:40:31 +00001375Decl *Parser::ParseDeclarationAfterDeclarator(Declarator &D,
Douglas Gregore542c862009-06-23 23:11:28 +00001376 const ParsedTemplateInfo &TemplateInfo) {
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001377 if (ParseAsmAttributesAfterDeclarator(D))
Richard Smithad762fc2011-04-14 22:09:26 +00001378 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001379
Richard Smithad762fc2011-04-14 22:09:26 +00001380 return ParseDeclarationAfterDeclaratorAndAttributes(D, TemplateInfo);
1381}
Mike Stump1eb44332009-09-09 15:08:12 +00001382
Richard Smithad762fc2011-04-14 22:09:26 +00001383Decl *Parser::ParseDeclarationAfterDeclaratorAndAttributes(Declarator &D,
1384 const ParsedTemplateInfo &TemplateInfo) {
Douglas Gregor1426e532009-05-12 21:31:51 +00001385 // Inform the current actions module that we just parsed this declarator.
John McCalld226f652010-08-21 09:40:31 +00001386 Decl *ThisDecl = 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +00001387 switch (TemplateInfo.Kind) {
1388 case ParsedTemplateInfo::NonTemplate:
Douglas Gregor23c94db2010-07-02 17:43:08 +00001389 ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
Douglas Gregord5a423b2009-09-25 18:43:00 +00001390 break;
1391
1392 case ParsedTemplateInfo::Template:
1393 case ParsedTemplateInfo::ExplicitSpecialization:
Douglas Gregor23c94db2010-07-02 17:43:08 +00001394 ThisDecl = Actions.ActOnTemplateDeclarator(getCurScope(),
John McCallf312b1e2010-08-26 23:41:50 +00001395 MultiTemplateParamsArg(Actions,
Douglas Gregore542c862009-06-23 23:11:28 +00001396 TemplateInfo.TemplateParams->data(),
1397 TemplateInfo.TemplateParams->size()),
Douglas Gregord5a423b2009-09-25 18:43:00 +00001398 D);
1399 break;
1400
1401 case ParsedTemplateInfo::ExplicitInstantiation: {
John McCalld226f652010-08-21 09:40:31 +00001402 DeclResult ThisRes
Douglas Gregor23c94db2010-07-02 17:43:08 +00001403 = Actions.ActOnExplicitInstantiation(getCurScope(),
Douglas Gregord5a423b2009-09-25 18:43:00 +00001404 TemplateInfo.ExternLoc,
1405 TemplateInfo.TemplateLoc,
1406 D);
1407 if (ThisRes.isInvalid()) {
1408 SkipUntil(tok::semi, true, true);
John McCalld226f652010-08-21 09:40:31 +00001409 return 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +00001410 }
1411
1412 ThisDecl = ThisRes.get();
1413 break;
1414 }
1415 }
Mike Stump1eb44332009-09-09 15:08:12 +00001416
Richard Smith34b41d92011-02-20 03:19:35 +00001417 bool TypeContainsAuto =
1418 D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto;
1419
Douglas Gregor1426e532009-05-12 21:31:51 +00001420 // Parse declarator '=' initializer.
Richard Trieud6c7c672012-01-18 22:54:52 +00001421 // If a '==' or '+=' is found, suggest a fixit to '='.
Richard Trieufcaf27e2012-01-19 22:01:51 +00001422 if (isTokenEqualOrEqualTypo()) {
Douglas Gregor1426e532009-05-12 21:31:51 +00001423 ConsumeToken();
Anders Carlsson37bf9d22010-09-24 21:25:25 +00001424 if (Tok.is(tok::kw_delete)) {
Sean Hunte4246a62011-05-12 06:15:49 +00001425 if (D.isFunctionDeclarator())
1426 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
1427 << 1 /* delete */;
1428 else
1429 Diag(ConsumeToken(), diag::err_deleted_non_function);
Sean Huntfe2695e2011-05-06 01:42:00 +00001430 } else if (Tok.is(tok::kw_default)) {
Sean Hunte4246a62011-05-12 06:15:49 +00001431 if (D.isFunctionDeclarator())
Sebastian Redlecfcd562012-02-11 23:51:21 +00001432 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
1433 << 0 /* default */;
Sean Hunte4246a62011-05-12 06:15:49 +00001434 else
1435 Diag(ConsumeToken(), diag::err_default_special_members);
Douglas Gregor1426e532009-05-12 21:31:51 +00001436 } else {
David Blaikie4e4d0842012-03-11 07:00:24 +00001437 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
John McCall731ad842009-12-19 09:28:58 +00001438 EnterScope(0);
Douglas Gregor23c94db2010-07-02 17:43:08 +00001439 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
John McCall731ad842009-12-19 09:28:58 +00001440 }
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +00001441
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +00001442 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001443 Actions.CodeCompleteInitializer(getCurScope(), ThisDecl);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001444 cutOffParsing();
1445 return 0;
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +00001446 }
1447
John McCall60d7b3a2010-08-24 06:29:42 +00001448 ExprResult Init(ParseInitializer());
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +00001449
David Blaikie4e4d0842012-03-11 07:00:24 +00001450 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001451 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
John McCall731ad842009-12-19 09:28:58 +00001452 ExitScope();
1453 }
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +00001454
Douglas Gregor1426e532009-05-12 21:31:51 +00001455 if (Init.isInvalid()) {
Douglas Gregor00225542010-03-01 18:27:54 +00001456 SkipUntil(tok::comma, true, true);
1457 Actions.ActOnInitializerError(ThisDecl);
1458 } else
Richard Smith34b41d92011-02-20 03:19:35 +00001459 Actions.AddInitializerToDecl(ThisDecl, Init.take(),
1460 /*DirectInit=*/false, TypeContainsAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +00001461 }
1462 } else if (Tok.is(tok::l_paren)) {
1463 // Parse C++ direct initializer: '(' expression-list ')'
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001464 BalancedDelimiterTracker T(*this, tok::l_paren);
1465 T.consumeOpen();
1466
Douglas Gregor1426e532009-05-12 21:31:51 +00001467 ExprVector Exprs(Actions);
1468 CommaLocsTy CommaLocs;
1469
David Blaikie4e4d0842012-03-11 07:00:24 +00001470 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregorb4debae2009-12-22 17:47:17 +00001471 EnterScope(0);
Douglas Gregor23c94db2010-07-02 17:43:08 +00001472 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001473 }
1474
Douglas Gregor1426e532009-05-12 21:31:51 +00001475 if (ParseExpressionList(Exprs, CommaLocs)) {
1476 SkipUntil(tok::r_paren);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001477
David Blaikie4e4d0842012-03-11 07:00:24 +00001478 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001479 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001480 ExitScope();
1481 }
Douglas Gregor1426e532009-05-12 21:31:51 +00001482 } else {
1483 // Match the ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001484 T.consumeClose();
Douglas Gregor1426e532009-05-12 21:31:51 +00001485
1486 assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() &&
1487 "Unexpected number of commas!");
Douglas Gregorb4debae2009-12-22 17:47:17 +00001488
David Blaikie4e4d0842012-03-11 07:00:24 +00001489 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001490 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001491 ExitScope();
1492 }
1493
Sebastian Redl5b9cc5d2012-02-11 23:51:47 +00001494 ExprResult Initializer = Actions.ActOnParenListExpr(T.getOpenLocation(),
1495 T.getCloseLocation(),
1496 move_arg(Exprs));
1497 Actions.AddInitializerToDecl(ThisDecl, Initializer.take(),
1498 /*DirectInit=*/true, TypeContainsAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +00001499 }
David Blaikie4e4d0842012-03-11 07:00:24 +00001500 } else if (getLangOpts().CPlusPlus0x && Tok.is(tok::l_brace)) {
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001501 // Parse C++0x braced-init-list.
Richard Smith7fe62082011-10-15 05:09:34 +00001502 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
1503
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001504 if (D.getCXXScopeSpec().isSet()) {
1505 EnterScope(0);
1506 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
1507 }
1508
1509 ExprResult Init(ParseBraceInitializer());
1510
1511 if (D.getCXXScopeSpec().isSet()) {
1512 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
1513 ExitScope();
1514 }
1515
1516 if (Init.isInvalid()) {
1517 Actions.ActOnInitializerError(ThisDecl);
1518 } else
1519 Actions.AddInitializerToDecl(ThisDecl, Init.take(),
1520 /*DirectInit=*/true, TypeContainsAuto);
1521
Douglas Gregor1426e532009-05-12 21:31:51 +00001522 } else {
Richard Smith34b41d92011-02-20 03:19:35 +00001523 Actions.ActOnUninitializedDecl(ThisDecl, TypeContainsAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +00001524 }
1525
Richard Smith483b9f32011-02-21 20:05:19 +00001526 Actions.FinalizeDeclaration(ThisDecl);
1527
Douglas Gregor1426e532009-05-12 21:31:51 +00001528 return ThisDecl;
1529}
1530
Reid Spencer5f016e22007-07-11 17:01:13 +00001531/// ParseSpecifierQualifierList
1532/// specifier-qualifier-list:
1533/// type-specifier specifier-qualifier-list[opt]
1534/// type-qualifier specifier-qualifier-list[opt]
1535/// [GNU] attributes specifier-qualifier-list[opt]
1536///
Richard Smith69730c12012-03-12 07:56:15 +00001537void Parser::ParseSpecifierQualifierList(DeclSpec &DS, AccessSpecifier AS,
1538 DeclSpecContext DSC) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001539 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
1540 /// parse declaration-specifiers and complain about extra stuff.
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001541 /// TODO: diagnose attribute-specifiers and alignment-specifiers.
Richard Smith69730c12012-03-12 07:56:15 +00001542 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS, DSC);
Mike Stump1eb44332009-09-09 15:08:12 +00001543
Reid Spencer5f016e22007-07-11 17:01:13 +00001544 // Validate declspec for type-name.
1545 unsigned Specs = DS.getParsedSpecifiers();
Richard Smith69730c12012-03-12 07:56:15 +00001546 if (DSC == DSC_type_specifier && !DS.hasTypeSpecifier()) {
1547 Diag(Tok, diag::err_expected_type);
1548 DS.SetTypeSpecError();
1549 } else if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers() &&
1550 !DS.hasAttributes()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001551 Diag(Tok, diag::err_typename_requires_specqual);
Richard Smith69730c12012-03-12 07:56:15 +00001552 if (!DS.hasTypeSpecifier())
1553 DS.SetTypeSpecError();
1554 }
Mike Stump1eb44332009-09-09 15:08:12 +00001555
Reid Spencer5f016e22007-07-11 17:01:13 +00001556 // Issue diagnostic and remove storage class if present.
1557 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
1558 if (DS.getStorageClassSpecLoc().isValid())
1559 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
1560 else
1561 Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass);
1562 DS.ClearStorageClassSpecs();
1563 }
Mike Stump1eb44332009-09-09 15:08:12 +00001564
Reid Spencer5f016e22007-07-11 17:01:13 +00001565 // Issue diagnostic and remove function specfier if present.
1566 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Douglas Gregorb48fe382008-10-31 09:07:45 +00001567 if (DS.isInlineSpecified())
1568 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
1569 if (DS.isVirtualSpecified())
1570 Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec);
1571 if (DS.isExplicitSpecified())
1572 Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec);
Reid Spencer5f016e22007-07-11 17:01:13 +00001573 DS.ClearFunctionSpecs();
1574 }
Richard Smith69730c12012-03-12 07:56:15 +00001575
1576 // Issue diagnostic and remove constexpr specfier if present.
1577 if (DS.isConstexprSpecified()) {
1578 Diag(DS.getConstexprSpecLoc(), diag::err_typename_invalid_constexpr);
1579 DS.ClearConstexprSpec();
1580 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001581}
1582
Chris Lattnerc199ab32009-04-12 20:42:31 +00001583/// isValidAfterIdentifierInDeclaratorAfterDeclSpec - Return true if the
1584/// specified token is valid after the identifier in a declarator which
1585/// immediately follows the declspec. For example, these things are valid:
1586///
1587/// int x [ 4]; // direct-declarator
1588/// int x ( int y); // direct-declarator
1589/// int(int x ) // direct-declarator
1590/// int x ; // simple-declaration
1591/// int x = 17; // init-declarator-list
1592/// int x , y; // init-declarator-list
1593/// int x __asm__ ("foo"); // init-declarator-list
Chris Lattnerb6645dd2009-04-14 21:16:09 +00001594/// int x : 4; // struct-declarator
Chris Lattnerc83c27a2009-04-12 22:29:43 +00001595/// int x { 5}; // C++'0x unified initializers
Chris Lattnerc199ab32009-04-12 20:42:31 +00001596///
1597/// This is not, because 'x' does not immediately follow the declspec (though
1598/// ')' happens to be valid anyway).
1599/// int (x)
1600///
1601static bool isValidAfterIdentifierInDeclarator(const Token &T) {
1602 return T.is(tok::l_square) || T.is(tok::l_paren) || T.is(tok::r_paren) ||
1603 T.is(tok::semi) || T.is(tok::comma) || T.is(tok::equal) ||
Chris Lattnerb6645dd2009-04-14 21:16:09 +00001604 T.is(tok::kw_asm) || T.is(tok::l_brace) || T.is(tok::colon);
Chris Lattnerc199ab32009-04-12 20:42:31 +00001605}
1606
Chris Lattnere40c2952009-04-14 21:34:55 +00001607
1608/// ParseImplicitInt - This method is called when we have an non-typename
1609/// identifier in a declspec (which normally terminates the decl spec) when
1610/// the declspec has no type specifier. In this case, the declspec is either
1611/// malformed or is "implicit int" (in K&R and C89).
1612///
1613/// This method handles diagnosing this prettily and returns false if the
1614/// declspec is done being processed. If it recovers and thinks there may be
1615/// other pieces of declspec after it, it returns true.
1616///
Chris Lattnerf4382f52009-04-14 22:17:06 +00001617bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001618 const ParsedTemplateInfo &TemplateInfo,
Richard Smith69730c12012-03-12 07:56:15 +00001619 AccessSpecifier AS, DeclSpecContext DSC) {
Chris Lattnerf4382f52009-04-14 22:17:06 +00001620 assert(Tok.is(tok::identifier) && "should have identifier");
Mike Stump1eb44332009-09-09 15:08:12 +00001621
Chris Lattnere40c2952009-04-14 21:34:55 +00001622 SourceLocation Loc = Tok.getLocation();
1623 // If we see an identifier that is not a type name, we normally would
1624 // parse it as the identifer being declared. However, when a typename
1625 // is typo'd or the definition is not included, this will incorrectly
1626 // parse the typename as the identifier name and fall over misparsing
1627 // later parts of the diagnostic.
1628 //
1629 // As such, we try to do some look-ahead in cases where this would
1630 // otherwise be an "implicit-int" case to see if this is invalid. For
1631 // example: "static foo_t x = 4;" In this case, if we parsed foo_t as
1632 // an identifier with implicit int, we'd get a parse error because the
1633 // next token is obviously invalid for a type. Parse these as a case
1634 // with an invalid type specifier.
1635 assert(!DS.hasTypeSpecifier() && "Type specifier checked above");
Mike Stump1eb44332009-09-09 15:08:12 +00001636
Chris Lattnere40c2952009-04-14 21:34:55 +00001637 // Since we know that this either implicit int (which is rare) or an
Richard Smith69730c12012-03-12 07:56:15 +00001638 // error, do lookahead to try to do better recovery. This never applies within
1639 // a type specifier.
1640 // FIXME: Don't bail out here in languages with no implicit int (like
1641 // C++ with no -fms-extensions). This is much more likely to be an undeclared
1642 // type or typo than a use of implicit int.
1643 if (DSC != DSC_type_specifier &&
1644 isValidAfterIdentifierInDeclarator(NextToken())) {
Chris Lattnere40c2952009-04-14 21:34:55 +00001645 // If this token is valid for implicit int, e.g. "static x = 4", then
1646 // we just avoid eating the identifier, so it will be parsed as the
1647 // identifier in the declarator.
1648 return false;
1649 }
Mike Stump1eb44332009-09-09 15:08:12 +00001650
Chris Lattnere40c2952009-04-14 21:34:55 +00001651 // Otherwise, if we don't consume this token, we are going to emit an
1652 // error anyway. Try to recover from various common problems. Check
1653 // to see if this was a reference to a tag name without a tag specified.
1654 // This is a common problem in C (saying 'foo' instead of 'struct foo').
Chris Lattnerf4382f52009-04-14 22:17:06 +00001655 //
1656 // C++ doesn't need this, and isTagName doesn't take SS.
1657 if (SS == 0) {
Argyrios Kyrtzidisb8a9d3b2011-04-21 17:29:47 +00001658 const char *TagName = 0, *FixitTagName = 0;
Chris Lattnerf4382f52009-04-14 22:17:06 +00001659 tok::TokenKind TagKind = tok::unknown;
Mike Stump1eb44332009-09-09 15:08:12 +00001660
Douglas Gregor23c94db2010-07-02 17:43:08 +00001661 switch (Actions.isTagName(*Tok.getIdentifierInfo(), getCurScope())) {
Chris Lattnere40c2952009-04-14 21:34:55 +00001662 default: break;
Argyrios Kyrtzidisb8a9d3b2011-04-21 17:29:47 +00001663 case DeclSpec::TST_enum:
1664 TagName="enum" ; FixitTagName = "enum " ; TagKind=tok::kw_enum ;break;
1665 case DeclSpec::TST_union:
1666 TagName="union" ; FixitTagName = "union " ;TagKind=tok::kw_union ;break;
1667 case DeclSpec::TST_struct:
1668 TagName="struct"; FixitTagName = "struct ";TagKind=tok::kw_struct;break;
1669 case DeclSpec::TST_class:
1670 TagName="class" ; FixitTagName = "class " ;TagKind=tok::kw_class ;break;
Chris Lattnere40c2952009-04-14 21:34:55 +00001671 }
Mike Stump1eb44332009-09-09 15:08:12 +00001672
Chris Lattnerf4382f52009-04-14 22:17:06 +00001673 if (TagName) {
Kaelyn Uhrainaec2ac62012-04-26 23:36:17 +00001674 IdentifierInfo *TokenName = Tok.getIdentifierInfo();
1675 LookupResult R(Actions, TokenName, SourceLocation(),
1676 Sema::LookupOrdinaryName);
1677
Chris Lattnerf4382f52009-04-14 22:17:06 +00001678 Diag(Loc, diag::err_use_of_tag_name_without_tag)
Kaelyn Uhrainaec2ac62012-04-26 23:36:17 +00001679 << TokenName << TagName << getLangOpts().CPlusPlus
1680 << FixItHint::CreateInsertion(Tok.getLocation(), FixitTagName);
1681
1682 if (Actions.LookupParsedName(R, getCurScope(), SS)) {
1683 for (LookupResult::iterator I = R.begin(), IEnd = R.end();
1684 I != IEnd; ++I)
Kaelyn Uhrain392b3f52012-04-27 18:26:49 +00001685 Diag((*I)->getLocation(), diag::note_decl_hiding_tag_type)
Kaelyn Uhrainaec2ac62012-04-26 23:36:17 +00001686 << TokenName << TagName;
1687 }
Mike Stump1eb44332009-09-09 15:08:12 +00001688
Chris Lattnerf4382f52009-04-14 22:17:06 +00001689 // Parse this as a tag as if the missing tag were present.
1690 if (TagKind == tok::kw_enum)
Richard Smith69730c12012-03-12 07:56:15 +00001691 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS, DSC_normal);
Chris Lattnerf4382f52009-04-14 22:17:06 +00001692 else
Richard Smith69730c12012-03-12 07:56:15 +00001693 ParseClassSpecifier(TagKind, Loc, DS, TemplateInfo, AS,
1694 /*EnteringContext*/ false, DSC_normal);
Chris Lattnerf4382f52009-04-14 22:17:06 +00001695 return true;
1696 }
Chris Lattnere40c2952009-04-14 21:34:55 +00001697 }
Mike Stump1eb44332009-09-09 15:08:12 +00001698
Douglas Gregora786fdb2009-10-13 23:27:22 +00001699 // This is almost certainly an invalid type name. Let the action emit a
1700 // diagnostic and attempt to recover.
John McCallb3d87482010-08-24 05:47:05 +00001701 ParsedType T;
Douglas Gregora786fdb2009-10-13 23:27:22 +00001702 if (Actions.DiagnoseUnknownTypeName(*Tok.getIdentifierInfo(), Loc,
Douglas Gregor23c94db2010-07-02 17:43:08 +00001703 getCurScope(), SS, T)) {
Douglas Gregora786fdb2009-10-13 23:27:22 +00001704 // The action emitted a diagnostic, so we don't have to.
1705 if (T) {
1706 // The action has suggested that the type T could be used. Set that as
1707 // the type in the declaration specifiers, consume the would-be type
1708 // name token, and we're done.
1709 const char *PrevSpec;
1710 unsigned DiagID;
John McCallb3d87482010-08-24 05:47:05 +00001711 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID, T);
Douglas Gregora786fdb2009-10-13 23:27:22 +00001712 DS.SetRangeEnd(Tok.getLocation());
1713 ConsumeToken();
1714
1715 // There may be other declaration specifiers after this.
1716 return true;
1717 }
1718
1719 // Fall through; the action had no suggestion for us.
1720 } else {
1721 // The action did not emit a diagnostic, so emit one now.
1722 SourceRange R;
1723 if (SS) R = SS->getRange();
1724 Diag(Loc, diag::err_unknown_typename) << Tok.getIdentifierInfo() << R;
1725 }
Mike Stump1eb44332009-09-09 15:08:12 +00001726
Douglas Gregora786fdb2009-10-13 23:27:22 +00001727 // Mark this as an error.
Richard Smith69730c12012-03-12 07:56:15 +00001728 DS.SetTypeSpecError();
Chris Lattnere40c2952009-04-14 21:34:55 +00001729 DS.SetRangeEnd(Tok.getLocation());
1730 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001731
Chris Lattnere40c2952009-04-14 21:34:55 +00001732 // TODO: Could inject an invalid typedef decl in an enclosing scope to
1733 // avoid rippling error messages on subsequent uses of the same type,
1734 // could be useful if #include was forgotten.
1735 return false;
1736}
1737
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001738/// \brief Determine the declaration specifier context from the declarator
1739/// context.
1740///
1741/// \param Context the declarator context, which is one of the
1742/// Declarator::TheContext enumerator values.
1743Parser::DeclSpecContext
1744Parser::getDeclSpecContextFromDeclaratorContext(unsigned Context) {
1745 if (Context == Declarator::MemberContext)
1746 return DSC_class;
1747 if (Context == Declarator::FileContext)
1748 return DSC_top_level;
Richard Smith6d96d3a2012-03-15 01:02:11 +00001749 if (Context == Declarator::TrailingReturnContext)
1750 return DSC_trailing;
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001751 return DSC_normal;
1752}
1753
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001754/// ParseAlignArgument - Parse the argument to an alignment-specifier.
1755///
1756/// FIXME: Simply returns an alignof() expression if the argument is a
1757/// type. Ideally, the type should be propagated directly into Sema.
1758///
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00001759/// [C11] type-id
1760/// [C11] constant-expression
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00001761/// [C++0x] type-id ...[opt]
1762/// [C++0x] assignment-expression ...[opt]
1763ExprResult Parser::ParseAlignArgument(SourceLocation Start,
1764 SourceLocation &EllipsisLoc) {
1765 ExprResult ER;
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001766 if (isTypeIdInParens()) {
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001767 SourceLocation TypeLoc = Tok.getLocation();
1768 ParsedType Ty = ParseTypeName().get();
1769 SourceRange TypeRange(Start, Tok.getLocation());
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00001770 ER = Actions.ActOnUnaryExprOrTypeTraitExpr(TypeLoc, UETT_AlignOf, true,
1771 Ty.getAsOpaquePtr(), TypeRange);
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001772 } else
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00001773 ER = ParseConstantExpression();
1774
David Blaikie4e4d0842012-03-11 07:00:24 +00001775 if (getLangOpts().CPlusPlus0x && Tok.is(tok::ellipsis))
Peter Collingbournefe9b2a82011-10-24 17:56:00 +00001776 EllipsisLoc = ConsumeToken();
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00001777
1778 return ER;
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001779}
1780
1781/// ParseAlignmentSpecifier - Parse an alignment-specifier, and add the
1782/// attribute to Attrs.
1783///
1784/// alignment-specifier:
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00001785/// [C11] '_Alignas' '(' type-id ')'
1786/// [C11] '_Alignas' '(' constant-expression ')'
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00001787/// [C++0x] 'alignas' '(' type-id ...[opt] ')'
1788/// [C++0x] 'alignas' '(' assignment-expression ...[opt] ')'
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001789void Parser::ParseAlignmentSpecifier(ParsedAttributes &Attrs,
1790 SourceLocation *endLoc) {
1791 assert((Tok.is(tok::kw_alignas) || Tok.is(tok::kw__Alignas)) &&
1792 "Not an alignment-specifier!");
1793
1794 SourceLocation KWLoc = Tok.getLocation();
1795 ConsumeToken();
1796
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001797 BalancedDelimiterTracker T(*this, tok::l_paren);
1798 if (T.expectAndConsume(diag::err_expected_lparen))
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001799 return;
1800
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00001801 SourceLocation EllipsisLoc;
1802 ExprResult ArgExpr = ParseAlignArgument(T.getOpenLocation(), EllipsisLoc);
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001803 if (ArgExpr.isInvalid()) {
1804 SkipUntil(tok::r_paren);
1805 return;
1806 }
1807
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001808 T.consumeClose();
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001809 if (endLoc)
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001810 *endLoc = T.getCloseLocation();
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001811
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00001812 // FIXME: Handle pack-expansions here.
1813 if (EllipsisLoc.isValid()) {
1814 Diag(EllipsisLoc, diag::err_alignas_pack_exp_unsupported);
1815 return;
1816 }
1817
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001818 ExprVector ArgExprs(Actions);
1819 ArgExprs.push_back(ArgExpr.release());
1820 Attrs.addNew(PP.getIdentifierInfo("aligned"), KWLoc, 0, KWLoc,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001821 0, T.getOpenLocation(), ArgExprs.take(), 1, false, true);
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001822}
1823
Reid Spencer5f016e22007-07-11 17:01:13 +00001824/// ParseDeclarationSpecifiers
1825/// declaration-specifiers: [C99 6.7]
1826/// storage-class-specifier declaration-specifiers[opt]
1827/// type-specifier declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00001828/// [C99] function-specifier declaration-specifiers[opt]
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00001829/// [C11] alignment-specifier declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00001830/// [GNU] attributes declaration-specifiers[opt]
Douglas Gregor8d267c52011-09-09 02:06:17 +00001831/// [Clang] '__module_private__' declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00001832///
1833/// storage-class-specifier: [C99 6.7.1]
1834/// 'typedef'
1835/// 'extern'
1836/// 'static'
1837/// 'auto'
1838/// 'register'
Sebastian Redl669d5d72008-11-14 23:42:31 +00001839/// [C++] 'mutable'
Reid Spencer5f016e22007-07-11 17:01:13 +00001840/// [GNU] '__thread'
Reid Spencer5f016e22007-07-11 17:01:13 +00001841/// function-specifier: [C99 6.7.4]
1842/// [C99] 'inline'
Douglas Gregorb48fe382008-10-31 09:07:45 +00001843/// [C++] 'virtual'
1844/// [C++] 'explicit'
Peter Collingbournef315fa82011-02-14 01:42:53 +00001845/// [OpenCL] '__kernel'
Anders Carlssonf47f7a12009-05-06 04:46:28 +00001846/// 'friend': [C++ dcl.friend]
Sebastian Redl2ac67232009-11-05 15:47:02 +00001847/// 'constexpr': [C++0x dcl.constexpr]
Anders Carlssonf47f7a12009-05-06 04:46:28 +00001848
Reid Spencer5f016e22007-07-11 17:01:13 +00001849///
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +00001850void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001851 const ParsedTemplateInfo &TemplateInfo,
John McCall67d1a672009-08-06 02:15:43 +00001852 AccessSpecifier AS,
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +00001853 DeclSpecContext DSContext,
1854 LateParsedAttrList *LateAttrs) {
Douglas Gregor312eadb2011-04-24 05:37:28 +00001855 if (DS.getSourceRange().isInvalid()) {
1856 DS.SetRangeStart(Tok.getLocation());
1857 DS.SetRangeEnd(Tok.getLocation());
1858 }
1859
Douglas Gregorefaa93a2011-11-07 17:33:42 +00001860 bool EnteringContext = (DSContext == DSC_class || DSContext == DSC_top_level);
Reid Spencer5f016e22007-07-11 17:01:13 +00001861 while (1) {
John McCallfec54012009-08-03 20:12:06 +00001862 bool isInvalid = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001863 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00001864 unsigned DiagID = 0;
1865
Reid Spencer5f016e22007-07-11 17:01:13 +00001866 SourceLocation Loc = Tok.getLocation();
Douglas Gregor12e083c2008-11-07 15:42:26 +00001867
Reid Spencer5f016e22007-07-11 17:01:13 +00001868 switch (Tok.getKind()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001869 default:
Chris Lattnerbce61352008-07-26 00:20:22 +00001870 DoneWithDeclSpec:
Peter Collingbournef1907682011-09-29 18:03:57 +00001871 // [C++0x] decl-specifier-seq: decl-specifier attribute-specifier-seq[opt]
1872 MaybeParseCXX0XAttributes(DS.getAttributes());
1873
Reid Spencer5f016e22007-07-11 17:01:13 +00001874 // If this is not a declaration specifier token, we're done reading decl
1875 // specifiers. First verify that DeclSpec's are consistent.
Douglas Gregor9b3064b2009-04-01 22:41:11 +00001876 DS.Finish(Diags, PP);
Reid Spencer5f016e22007-07-11 17:01:13 +00001877 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001878
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001879 case tok::code_completion: {
John McCallf312b1e2010-08-26 23:41:50 +00001880 Sema::ParserCompletionContext CCC = Sema::PCC_Namespace;
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001881 if (DS.hasTypeSpecifier()) {
1882 bool AllowNonIdentifiers
1883 = (getCurScope()->getFlags() & (Scope::ControlScope |
1884 Scope::BlockScope |
1885 Scope::TemplateParamScope |
1886 Scope::FunctionPrototypeScope |
1887 Scope::AtCatchScope)) == 0;
1888 bool AllowNestedNameSpecifiers
1889 = DSContext == DSC_top_level ||
1890 (DSContext == DSC_class && DS.isFriendSpecified());
1891
Douglas Gregorc7b6d882010-09-16 15:14:18 +00001892 Actions.CodeCompleteDeclSpec(getCurScope(), DS,
1893 AllowNonIdentifiers,
1894 AllowNestedNameSpecifiers);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001895 return cutOffParsing();
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001896 }
1897
Douglas Gregor68e3c2e2011-02-15 20:33:25 +00001898 if (getCurScope()->getFnParent() || getCurScope()->getBlockParent())
1899 CCC = Sema::PCC_LocalDeclarationSpecifiers;
1900 else if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate)
John McCallf312b1e2010-08-26 23:41:50 +00001901 CCC = DSContext == DSC_class? Sema::PCC_MemberTemplate
1902 : Sema::PCC_Template;
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001903 else if (DSContext == DSC_class)
John McCallf312b1e2010-08-26 23:41:50 +00001904 CCC = Sema::PCC_Class;
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +00001905 else if (CurParsedObjCImpl)
John McCallf312b1e2010-08-26 23:41:50 +00001906 CCC = Sema::PCC_ObjCImplementation;
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001907
1908 Actions.CodeCompleteOrdinaryName(getCurScope(), CCC);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001909 return cutOffParsing();
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001910 }
1911
Chris Lattner5e02c472009-01-05 00:07:25 +00001912 case tok::coloncolon: // ::foo::bar
John McCall9ba61662010-02-26 08:45:28 +00001913 // C++ scope specifier. Annotate and loop, or bail out on error.
1914 if (TryAnnotateCXXScopeToken(true)) {
1915 if (!DS.hasTypeSpecifier())
1916 DS.SetTypeSpecError();
1917 goto DoneWithDeclSpec;
1918 }
John McCall2e0a7152010-03-01 18:20:46 +00001919 if (Tok.is(tok::coloncolon)) // ::new or ::delete
1920 goto DoneWithDeclSpec;
John McCall9ba61662010-02-26 08:45:28 +00001921 continue;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001922
1923 case tok::annot_cxxscope: {
Richard Smithf63eee72012-05-09 18:56:43 +00001924 if (DS.hasTypeSpecifier() || DS.isTypeAltiVecVector())
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001925 goto DoneWithDeclSpec;
1926
John McCallaa87d332009-12-12 11:40:51 +00001927 CXXScopeSpec SS;
Douglas Gregorc34348a2011-02-24 17:54:50 +00001928 Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(),
1929 Tok.getAnnotationRange(),
1930 SS);
John McCallaa87d332009-12-12 11:40:51 +00001931
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001932 // We are looking for a qualified typename.
Douglas Gregor9135c722009-03-25 15:40:00 +00001933 Token Next = NextToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001934 if (Next.is(tok::annot_template_id) &&
Douglas Gregor9135c722009-03-25 15:40:00 +00001935 static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue())
Douglas Gregorc45c2322009-03-31 00:43:58 +00001936 ->Kind == TNK_Type_template) {
Douglas Gregor9135c722009-03-25 15:40:00 +00001937 // We have a qualified template-id, e.g., N::A<int>
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001938
1939 // C++ [class.qual]p2:
1940 // In a lookup in which the constructor is an acceptable lookup
1941 // result and the nested-name-specifier nominates a class C:
1942 //
1943 // - if the name specified after the
1944 // nested-name-specifier, when looked up in C, is the
1945 // injected-class-name of C (Clause 9), or
1946 //
1947 // - if the name specified after the nested-name-specifier
1948 // is the same as the identifier or the
1949 // simple-template-id's template-name in the last
1950 // component of the nested-name-specifier,
1951 //
1952 // the name is instead considered to name the constructor of
1953 // class C.
1954 //
1955 // Thus, if the template-name is actually the constructor
1956 // name, then the code is ill-formed; this interpretation is
1957 // reinforced by the NAD status of core issue 635.
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +00001958 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Next);
John McCallba9d8532010-04-13 06:39:49 +00001959 if ((DSContext == DSC_top_level ||
1960 (DSContext == DSC_class && DS.isFriendSpecified())) &&
1961 TemplateId->Name &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00001962 Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) {
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001963 if (isConstructorDeclarator()) {
1964 // The user meant this to be an out-of-line constructor
1965 // definition, but template arguments are not allowed
1966 // there. Just allow this as a constructor; we'll
1967 // complain about it later.
1968 goto DoneWithDeclSpec;
1969 }
1970
1971 // The user meant this to name a type, but it actually names
1972 // a constructor with some extraneous template
1973 // arguments. Complain, then parse it as a type as the user
1974 // intended.
1975 Diag(TemplateId->TemplateNameLoc,
1976 diag::err_out_of_line_template_id_names_constructor)
1977 << TemplateId->Name;
1978 }
1979
John McCallaa87d332009-12-12 11:40:51 +00001980 DS.getTypeSpecScope() = SS;
1981 ConsumeToken(); // The C++ scope.
Mike Stump1eb44332009-09-09 15:08:12 +00001982 assert(Tok.is(tok::annot_template_id) &&
Douglas Gregor9135c722009-03-25 15:40:00 +00001983 "ParseOptionalCXXScopeSpecifier not working");
Douglas Gregor059101f2011-03-02 00:47:37 +00001984 AnnotateTemplateIdTokenAsType();
Douglas Gregor9135c722009-03-25 15:40:00 +00001985 continue;
1986 }
1987
Douglas Gregor9d7b3532009-09-28 07:26:33 +00001988 if (Next.is(tok::annot_typename)) {
John McCallaa87d332009-12-12 11:40:51 +00001989 DS.getTypeSpecScope() = SS;
1990 ConsumeToken(); // The C++ scope.
John McCallb3d87482010-08-24 05:47:05 +00001991 if (Tok.getAnnotationValue()) {
1992 ParsedType T = getTypeAnnotation(Tok);
Nico Weber253e80b2010-11-22 10:30:56 +00001993 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename,
1994 Tok.getAnnotationEndLoc(),
John McCallb3d87482010-08-24 05:47:05 +00001995 PrevSpec, DiagID, T);
1996 }
Douglas Gregor9d7b3532009-09-28 07:26:33 +00001997 else
1998 DS.SetTypeSpecError();
1999 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
2000 ConsumeToken(); // The typename
2001 }
2002
Douglas Gregor9135c722009-03-25 15:40:00 +00002003 if (Next.isNot(tok::identifier))
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002004 goto DoneWithDeclSpec;
2005
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002006 // If we're in a context where the identifier could be a class name,
2007 // check whether this is a constructor declaration.
John McCallba9d8532010-04-13 06:39:49 +00002008 if ((DSContext == DSC_top_level ||
2009 (DSContext == DSC_class && DS.isFriendSpecified())) &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00002010 Actions.isCurrentClassName(*Next.getIdentifierInfo(), getCurScope(),
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002011 &SS)) {
2012 if (isConstructorDeclarator())
2013 goto DoneWithDeclSpec;
2014
2015 // As noted in C++ [class.qual]p2 (cited above), when the name
2016 // of the class is qualified in a context where it could name
2017 // a constructor, its a constructor name. However, we've
2018 // looked at the declarator, and the user probably meant this
2019 // to be a type. Complain that it isn't supposed to be treated
2020 // as a type, then proceed to parse it as a type.
2021 Diag(Next.getLocation(), diag::err_out_of_line_type_names_constructor)
2022 << Next.getIdentifierInfo();
2023 }
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002024
John McCallb3d87482010-08-24 05:47:05 +00002025 ParsedType TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(),
2026 Next.getLocation(),
Douglas Gregor9e876872011-03-01 18:12:44 +00002027 getCurScope(), &SS,
2028 false, false, ParsedType(),
Abramo Bagnarafad03b72012-01-27 08:46:19 +00002029 /*IsCtorOrDtorName=*/false,
Douglas Gregor9e876872011-03-01 18:12:44 +00002030 /*NonTrivialSourceInfo=*/true);
Douglas Gregor55f6b142009-02-09 18:46:07 +00002031
Chris Lattnerf4382f52009-04-14 22:17:06 +00002032 // If the referenced identifier is not a type, then this declspec is
2033 // erroneous: We already checked about that it has no type specifier, and
2034 // C++ doesn't have implicit int. Diagnose it as a typo w.r.t. to the
Mike Stump1eb44332009-09-09 15:08:12 +00002035 // typename.
Chris Lattnerf4382f52009-04-14 22:17:06 +00002036 if (TypeRep == 0) {
2037 ConsumeToken(); // Eat the scope spec so the identifier is current.
Richard Smith69730c12012-03-12 07:56:15 +00002038 if (ParseImplicitInt(DS, &SS, TemplateInfo, AS, DSContext)) continue;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002039 goto DoneWithDeclSpec;
Chris Lattnerf4382f52009-04-14 22:17:06 +00002040 }
Mike Stump1eb44332009-09-09 15:08:12 +00002041
John McCallaa87d332009-12-12 11:40:51 +00002042 DS.getTypeSpecScope() = SS;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002043 ConsumeToken(); // The C++ scope.
2044
Douglas Gregor1a51b4a2009-02-09 15:09:02 +00002045 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00002046 DiagID, TypeRep);
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002047 if (isInvalid)
2048 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002049
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002050 DS.SetRangeEnd(Tok.getLocation());
2051 ConsumeToken(); // The typename.
2052
2053 continue;
2054 }
Mike Stump1eb44332009-09-09 15:08:12 +00002055
Chris Lattner80d0c892009-01-21 19:48:37 +00002056 case tok::annot_typename: {
John McCallb3d87482010-08-24 05:47:05 +00002057 if (Tok.getAnnotationValue()) {
2058 ParsedType T = getTypeAnnotation(Tok);
Nico Weberc43271e2010-11-22 12:50:03 +00002059 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallb3d87482010-08-24 05:47:05 +00002060 DiagID, T);
2061 } else
Douglas Gregor31a19b62009-04-01 21:51:26 +00002062 DS.SetTypeSpecError();
Chris Lattner5c5db552010-04-05 18:18:31 +00002063
2064 if (isInvalid)
2065 break;
2066
Chris Lattner80d0c892009-01-21 19:48:37 +00002067 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
2068 ConsumeToken(); // The typename
Mike Stump1eb44332009-09-09 15:08:12 +00002069
Chris Lattner80d0c892009-01-21 19:48:37 +00002070 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
2071 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00002072 // Objective-C interface.
David Blaikie4e4d0842012-03-11 07:00:24 +00002073 if (Tok.is(tok::less) && getLangOpts().ObjC1)
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00002074 ParseObjCProtocolQualifiers(DS);
2075
Chris Lattner80d0c892009-01-21 19:48:37 +00002076 continue;
2077 }
Mike Stump1eb44332009-09-09 15:08:12 +00002078
Douglas Gregorbfad9152011-04-28 15:48:45 +00002079 case tok::kw___is_signed:
2080 // GNU libstdc++ 4.4 uses __is_signed as an identifier, but Clang
2081 // typically treats it as a trait. If we see __is_signed as it appears
2082 // in libstdc++, e.g.,
2083 //
2084 // static const bool __is_signed;
2085 //
2086 // then treat __is_signed as an identifier rather than as a keyword.
2087 if (DS.getTypeSpecType() == TST_bool &&
2088 DS.getTypeQualifiers() == DeclSpec::TQ_const &&
2089 DS.getStorageClassSpec() == DeclSpec::SCS_static) {
2090 Tok.getIdentifierInfo()->RevertTokenIDToIdentifier();
2091 Tok.setKind(tok::identifier);
2092 }
2093
2094 // We're done with the declaration-specifiers.
2095 goto DoneWithDeclSpec;
2096
Chris Lattner3bd934a2008-07-26 01:18:38 +00002097 // typedef-name
David Blaikie42d6d0c2011-12-04 05:04:18 +00002098 case tok::kw_decltype:
Chris Lattner3bd934a2008-07-26 01:18:38 +00002099 case tok::identifier: {
Chris Lattner5e02c472009-01-05 00:07:25 +00002100 // In C++, check to see if this is a scope specifier like foo::bar::, if
2101 // so handle it as such. This is important for ctor parsing.
David Blaikie4e4d0842012-03-11 07:00:24 +00002102 if (getLangOpts().CPlusPlus) {
John McCall9ba61662010-02-26 08:45:28 +00002103 if (TryAnnotateCXXScopeToken(true)) {
2104 if (!DS.hasTypeSpecifier())
2105 DS.SetTypeSpecError();
2106 goto DoneWithDeclSpec;
2107 }
2108 if (!Tok.is(tok::identifier))
2109 continue;
2110 }
Mike Stump1eb44332009-09-09 15:08:12 +00002111
Chris Lattner3bd934a2008-07-26 01:18:38 +00002112 // This identifier can only be a typedef name if we haven't already seen
2113 // a type-specifier. Without this check we misparse:
2114 // typedef int X; struct Y { short X; }; as 'short int'.
2115 if (DS.hasTypeSpecifier())
2116 goto DoneWithDeclSpec;
Mike Stump1eb44332009-09-09 15:08:12 +00002117
John Thompson82287d12010-02-05 00:12:22 +00002118 // Check for need to substitute AltiVec keyword tokens.
2119 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
2120 break;
2121
Richard Smithf63eee72012-05-09 18:56:43 +00002122 // [AltiVec] 2.2: [If the 'vector' specifier is used] The syntax does not
2123 // allow the use of a typedef name as a type specifier.
2124 if (DS.isTypeAltiVecVector())
2125 goto DoneWithDeclSpec;
2126
John McCallb3d87482010-08-24 05:47:05 +00002127 ParsedType TypeRep =
2128 Actions.getTypeName(*Tok.getIdentifierInfo(),
2129 Tok.getLocation(), getCurScope());
Douglas Gregor55f6b142009-02-09 18:46:07 +00002130
Chris Lattnerc199ab32009-04-12 20:42:31 +00002131 // If this is not a typedef name, don't parse it as part of the declspec,
2132 // it must be an implicit int or an error.
John McCallb3d87482010-08-24 05:47:05 +00002133 if (!TypeRep) {
Richard Smith69730c12012-03-12 07:56:15 +00002134 if (ParseImplicitInt(DS, 0, TemplateInfo, AS, DSContext)) continue;
Chris Lattner3bd934a2008-07-26 01:18:38 +00002135 goto DoneWithDeclSpec;
Chris Lattnerc199ab32009-04-12 20:42:31 +00002136 }
Douglas Gregor55f6b142009-02-09 18:46:07 +00002137
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002138 // If we're in a context where the identifier could be a class name,
2139 // check whether this is a constructor declaration.
David Blaikie4e4d0842012-03-11 07:00:24 +00002140 if (getLangOpts().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00002141 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), getCurScope()) &&
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002142 isConstructorDeclarator())
Douglas Gregorb48fe382008-10-31 09:07:45 +00002143 goto DoneWithDeclSpec;
2144
Douglas Gregor1a51b4a2009-02-09 15:09:02 +00002145 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00002146 DiagID, TypeRep);
Chris Lattner3bd934a2008-07-26 01:18:38 +00002147 if (isInvalid)
2148 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002149
Chris Lattner3bd934a2008-07-26 01:18:38 +00002150 DS.SetRangeEnd(Tok.getLocation());
2151 ConsumeToken(); // The identifier
2152
2153 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
2154 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00002155 // Objective-C interface.
David Blaikie4e4d0842012-03-11 07:00:24 +00002156 if (Tok.is(tok::less) && getLangOpts().ObjC1)
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00002157 ParseObjCProtocolQualifiers(DS);
2158
Steve Naroff4f9b9f12008-09-22 10:28:57 +00002159 // Need to support trailing type qualifiers (e.g. "id<p> const").
2160 // If a type specifier follows, it will be diagnosed elsewhere.
2161 continue;
Chris Lattner3bd934a2008-07-26 01:18:38 +00002162 }
Douglas Gregor39a8de12009-02-25 19:37:18 +00002163
2164 // type-name
2165 case tok::annot_template_id: {
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +00002166 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregorc45c2322009-03-31 00:43:58 +00002167 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor39a8de12009-02-25 19:37:18 +00002168 // This template-id does not refer to a type name, so we're
2169 // done with the type-specifiers.
2170 goto DoneWithDeclSpec;
2171 }
2172
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002173 // If we're in a context where the template-id could be a
2174 // constructor name or specialization, check whether this is a
2175 // constructor declaration.
David Blaikie4e4d0842012-03-11 07:00:24 +00002176 if (getLangOpts().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00002177 Actions.isCurrentClassName(*TemplateId->Name, getCurScope()) &&
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002178 isConstructorDeclarator())
2179 goto DoneWithDeclSpec;
2180
Douglas Gregor39a8de12009-02-25 19:37:18 +00002181 // Turn the template-id annotation token into a type annotation
2182 // token, then try again to parse it as a type-specifier.
Douglas Gregor31a19b62009-04-01 21:51:26 +00002183 AnnotateTemplateIdTokenAsType();
Douglas Gregor39a8de12009-02-25 19:37:18 +00002184 continue;
2185 }
2186
Reid Spencer5f016e22007-07-11 17:01:13 +00002187 // GNU attributes support.
2188 case tok::kw___attribute:
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +00002189 ParseGNUAttributes(DS.getAttributes(), 0, LateAttrs);
Reid Spencer5f016e22007-07-11 17:01:13 +00002190 continue;
Steve Narofff59e17e2008-12-24 20:59:21 +00002191
2192 // Microsoft declspec support.
2193 case tok::kw___declspec:
John McCall7f040a92010-12-24 02:08:15 +00002194 ParseMicrosoftDeclSpec(DS.getAttributes());
Steve Narofff59e17e2008-12-24 20:59:21 +00002195 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00002196
Steve Naroff239f0732008-12-25 14:16:32 +00002197 // Microsoft single token adornments.
Steve Naroff86bc6cf2008-12-25 14:41:26 +00002198 case tok::kw___forceinline:
Eli Friedman290eeb02009-06-08 23:27:34 +00002199 // FIXME: Add handling here!
2200 break;
2201
2202 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00002203 case tok::kw___ptr32:
Steve Naroff86bc6cf2008-12-25 14:41:26 +00002204 case tok::kw___w64:
Steve Naroff239f0732008-12-25 14:16:32 +00002205 case tok::kw___cdecl:
2206 case tok::kw___stdcall:
2207 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00002208 case tok::kw___thiscall:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00002209 case tok::kw___unaligned:
John McCall7f040a92010-12-24 02:08:15 +00002210 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman290eeb02009-06-08 23:27:34 +00002211 continue;
2212
Dawn Perchik52fc3142010-09-03 01:29:35 +00002213 // Borland single token adornments.
2214 case tok::kw___pascal:
John McCall7f040a92010-12-24 02:08:15 +00002215 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik52fc3142010-09-03 01:29:35 +00002216 continue;
2217
Peter Collingbournef315fa82011-02-14 01:42:53 +00002218 // OpenCL single token adornments.
2219 case tok::kw___kernel:
2220 ParseOpenCLAttributes(DS.getAttributes());
2221 continue;
2222
Reid Spencer5f016e22007-07-11 17:01:13 +00002223 // storage-class-specifier
2224 case tok::kw_typedef:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002225 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_typedef, Loc,
2226 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002227 break;
2228 case tok::kw_extern:
2229 if (DS.isThreadSpecified())
Chris Lattner1ab3b962008-11-18 07:48:38 +00002230 Diag(Tok, diag::ext_thread_before) << "extern";
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002231 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_extern, Loc,
2232 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002233 break;
Steve Naroff8d54bf22007-12-18 00:16:02 +00002234 case tok::kw___private_extern__:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002235 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_private_extern,
2236 Loc, PrevSpec, DiagID);
Steve Naroff8d54bf22007-12-18 00:16:02 +00002237 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00002238 case tok::kw_static:
2239 if (DS.isThreadSpecified())
Chris Lattner1ab3b962008-11-18 07:48:38 +00002240 Diag(Tok, diag::ext_thread_before) << "static";
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002241 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_static, Loc,
2242 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002243 break;
2244 case tok::kw_auto:
David Blaikie4e4d0842012-03-11 07:00:24 +00002245 if (getLangOpts().CPlusPlus0x) {
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002246 if (isKnownToBeTypeSpecifier(GetLookAheadToken(1))) {
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002247 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc,
2248 PrevSpec, DiagID);
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002249 if (!isInvalid)
Richard Smith8f4fb192011-09-04 19:54:14 +00002250 Diag(Tok, diag::ext_auto_storage_class)
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002251 << FixItHint::CreateRemoval(DS.getStorageClassSpecLoc());
Richard Smith8f4fb192011-09-04 19:54:14 +00002252 } else
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002253 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec,
2254 DiagID);
Richard Smith8f4fb192011-09-04 19:54:14 +00002255 } else
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002256 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc,
2257 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002258 break;
2259 case tok::kw_register:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002260 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_register, Loc,
2261 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002262 break;
Sebastian Redl669d5d72008-11-14 23:42:31 +00002263 case tok::kw_mutable:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002264 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_mutable, Loc,
2265 PrevSpec, DiagID);
Sebastian Redl669d5d72008-11-14 23:42:31 +00002266 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00002267 case tok::kw___thread:
John McCallfec54012009-08-03 20:12:06 +00002268 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002269 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002270
Reid Spencer5f016e22007-07-11 17:01:13 +00002271 // function-specifier
2272 case tok::kw_inline:
John McCallfec54012009-08-03 20:12:06 +00002273 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002274 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +00002275 case tok::kw_virtual:
John McCallfec54012009-08-03 20:12:06 +00002276 isInvalid = DS.SetFunctionSpecVirtual(Loc, PrevSpec, DiagID);
Douglas Gregorb48fe382008-10-31 09:07:45 +00002277 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +00002278 case tok::kw_explicit:
John McCallfec54012009-08-03 20:12:06 +00002279 isInvalid = DS.SetFunctionSpecExplicit(Loc, PrevSpec, DiagID);
Douglas Gregorb48fe382008-10-31 09:07:45 +00002280 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002281
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002282 // alignment-specifier
2283 case tok::kw__Alignas:
David Blaikie4e4d0842012-03-11 07:00:24 +00002284 if (!getLangOpts().C11)
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00002285 Diag(Tok, diag::ext_c11_alignas);
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002286 ParseAlignmentSpecifier(DS.getAttributes());
2287 continue;
2288
Anders Carlssonf47f7a12009-05-06 04:46:28 +00002289 // friend
2290 case tok::kw_friend:
John McCall67d1a672009-08-06 02:15:43 +00002291 if (DSContext == DSC_class)
2292 isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID);
2293 else {
2294 PrevSpec = ""; // not actually used by the diagnostic
2295 DiagID = diag::err_friend_invalid_in_context;
2296 isInvalid = true;
2297 }
Anders Carlssonf47f7a12009-05-06 04:46:28 +00002298 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002299
Douglas Gregor8d267c52011-09-09 02:06:17 +00002300 // Modules
2301 case tok::kw___module_private__:
2302 isInvalid = DS.setModulePrivateSpec(Loc, PrevSpec, DiagID);
2303 break;
2304
Sebastian Redl2ac67232009-11-05 15:47:02 +00002305 // constexpr
2306 case tok::kw_constexpr:
2307 isInvalid = DS.SetConstexprSpec(Loc, PrevSpec, DiagID);
2308 break;
2309
Chris Lattner80d0c892009-01-21 19:48:37 +00002310 // type-specifier
2311 case tok::kw_short:
John McCallfec54012009-08-03 20:12:06 +00002312 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec,
2313 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002314 break;
2315 case tok::kw_long:
2316 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCallfec54012009-08-03 20:12:06 +00002317 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
2318 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002319 else
John McCallfec54012009-08-03 20:12:06 +00002320 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2321 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002322 break;
Francois Pichet338d7f72011-04-28 01:59:37 +00002323 case tok::kw___int64:
2324 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2325 DiagID);
2326 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002327 case tok::kw_signed:
John McCallfec54012009-08-03 20:12:06 +00002328 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec,
2329 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002330 break;
2331 case tok::kw_unsigned:
John McCallfec54012009-08-03 20:12:06 +00002332 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
2333 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002334 break;
2335 case tok::kw__Complex:
John McCallfec54012009-08-03 20:12:06 +00002336 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
2337 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002338 break;
2339 case tok::kw__Imaginary:
John McCallfec54012009-08-03 20:12:06 +00002340 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
2341 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002342 break;
2343 case tok::kw_void:
John McCallfec54012009-08-03 20:12:06 +00002344 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec,
2345 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002346 break;
2347 case tok::kw_char:
John McCallfec54012009-08-03 20:12:06 +00002348 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec,
2349 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002350 break;
2351 case tok::kw_int:
John McCallfec54012009-08-03 20:12:06 +00002352 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec,
2353 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002354 break;
Richard Smith5a5a9712012-04-04 06:24:32 +00002355 case tok::kw___int128:
2356 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int128, Loc, PrevSpec,
2357 DiagID);
2358 break;
2359 case tok::kw_half:
2360 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_half, Loc, PrevSpec,
2361 DiagID);
2362 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002363 case tok::kw_float:
John McCallfec54012009-08-03 20:12:06 +00002364 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec,
2365 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002366 break;
2367 case tok::kw_double:
John McCallfec54012009-08-03 20:12:06 +00002368 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec,
2369 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002370 break;
2371 case tok::kw_wchar_t:
John McCallfec54012009-08-03 20:12:06 +00002372 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec,
2373 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002374 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002375 case tok::kw_char16_t:
John McCallfec54012009-08-03 20:12:06 +00002376 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec,
2377 DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002378 break;
2379 case tok::kw_char32_t:
John McCallfec54012009-08-03 20:12:06 +00002380 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec,
2381 DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002382 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002383 case tok::kw_bool:
2384 case tok::kw__Bool:
Argyrios Kyrtzidis4383e182010-11-16 18:18:13 +00002385 if (Tok.is(tok::kw_bool) &&
2386 DS.getTypeSpecType() != DeclSpec::TST_unspecified &&
2387 DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
2388 PrevSpec = ""; // Not used by the diagnostic.
2389 DiagID = diag::err_bool_redeclaration;
Fariborz Jahaniane106a0b2011-04-19 21:42:37 +00002390 // For better error recovery.
2391 Tok.setKind(tok::identifier);
Argyrios Kyrtzidis4383e182010-11-16 18:18:13 +00002392 isInvalid = true;
2393 } else {
2394 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec,
2395 DiagID);
2396 }
Chris Lattner80d0c892009-01-21 19:48:37 +00002397 break;
2398 case tok::kw__Decimal32:
John McCallfec54012009-08-03 20:12:06 +00002399 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
2400 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002401 break;
2402 case tok::kw__Decimal64:
John McCallfec54012009-08-03 20:12:06 +00002403 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
2404 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002405 break;
2406 case tok::kw__Decimal128:
John McCallfec54012009-08-03 20:12:06 +00002407 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
2408 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002409 break;
John Thompson82287d12010-02-05 00:12:22 +00002410 case tok::kw___vector:
2411 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
2412 break;
2413 case tok::kw___pixel:
2414 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
2415 break;
John McCalla5fc4722011-04-09 22:50:59 +00002416 case tok::kw___unknown_anytype:
2417 isInvalid = DS.SetTypeSpecType(TST_unknown_anytype, Loc,
2418 PrevSpec, DiagID);
2419 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002420
2421 // class-specifier:
2422 case tok::kw_class:
2423 case tok::kw_struct:
Chris Lattner4c97d762009-04-12 21:49:30 +00002424 case tok::kw_union: {
2425 tok::TokenKind Kind = Tok.getKind();
2426 ConsumeToken();
Richard Smith69730c12012-03-12 07:56:15 +00002427 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS,
2428 EnteringContext, DSContext);
Chris Lattner80d0c892009-01-21 19:48:37 +00002429 continue;
Chris Lattner4c97d762009-04-12 21:49:30 +00002430 }
Chris Lattner80d0c892009-01-21 19:48:37 +00002431
2432 // enum-specifier:
2433 case tok::kw_enum:
Chris Lattner4c97d762009-04-12 21:49:30 +00002434 ConsumeToken();
Richard Smith69730c12012-03-12 07:56:15 +00002435 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS, DSContext);
Chris Lattner80d0c892009-01-21 19:48:37 +00002436 continue;
2437
2438 // cv-qualifier:
2439 case tok::kw_const:
John McCallfec54012009-08-03 20:12:06 +00002440 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec, DiagID,
David Blaikie4e4d0842012-03-11 07:00:24 +00002441 getLangOpts());
Chris Lattner80d0c892009-01-21 19:48:37 +00002442 break;
2443 case tok::kw_volatile:
John McCallfec54012009-08-03 20:12:06 +00002444 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
David Blaikie4e4d0842012-03-11 07:00:24 +00002445 getLangOpts());
Chris Lattner80d0c892009-01-21 19:48:37 +00002446 break;
2447 case tok::kw_restrict:
John McCallfec54012009-08-03 20:12:06 +00002448 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
David Blaikie4e4d0842012-03-11 07:00:24 +00002449 getLangOpts());
Chris Lattner80d0c892009-01-21 19:48:37 +00002450 break;
2451
Douglas Gregord57959a2009-03-27 23:10:48 +00002452 // C++ typename-specifier:
2453 case tok::kw_typename:
John McCall9ba61662010-02-26 08:45:28 +00002454 if (TryAnnotateTypeOrScopeToken()) {
2455 DS.SetTypeSpecError();
2456 goto DoneWithDeclSpec;
2457 }
2458 if (!Tok.is(tok::kw_typename))
Douglas Gregord57959a2009-03-27 23:10:48 +00002459 continue;
2460 break;
2461
Chris Lattner80d0c892009-01-21 19:48:37 +00002462 // GNU typeof support.
2463 case tok::kw_typeof:
2464 ParseTypeofSpecifier(DS);
2465 continue;
2466
David Blaikie42d6d0c2011-12-04 05:04:18 +00002467 case tok::annot_decltype:
Anders Carlsson6fd634f2009-06-24 17:47:40 +00002468 ParseDecltypeSpecifier(DS);
2469 continue;
2470
Sean Huntdb5d44b2011-05-19 05:37:45 +00002471 case tok::kw___underlying_type:
2472 ParseUnderlyingTypeSpecifier(DS);
Eli Friedmanb001de72011-10-06 23:00:33 +00002473 continue;
2474
2475 case tok::kw__Atomic:
2476 ParseAtomicSpecifier(DS);
2477 continue;
Sean Huntdb5d44b2011-05-19 05:37:45 +00002478
Peter Collingbourne207f4d82011-03-18 22:38:29 +00002479 // OpenCL qualifiers:
2480 case tok::kw_private:
David Blaikie4e4d0842012-03-11 07:00:24 +00002481 if (!getLangOpts().OpenCL)
Peter Collingbourne207f4d82011-03-18 22:38:29 +00002482 goto DoneWithDeclSpec;
2483 case tok::kw___private:
2484 case tok::kw___global:
2485 case tok::kw___local:
2486 case tok::kw___constant:
2487 case tok::kw___read_only:
2488 case tok::kw___write_only:
2489 case tok::kw___read_write:
2490 ParseOpenCLQualifiers(DS);
2491 break;
2492
Steve Naroffd3ded1f2008-06-05 00:02:44 +00002493 case tok::less:
Chris Lattner3bd934a2008-07-26 01:18:38 +00002494 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattnerbce61352008-07-26 00:20:22 +00002495 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
2496 // but we support it.
David Blaikie4e4d0842012-03-11 07:00:24 +00002497 if (DS.hasTypeSpecifier() || !getLangOpts().ObjC1)
Chris Lattnerbce61352008-07-26 00:20:22 +00002498 goto DoneWithDeclSpec;
Mike Stump1eb44332009-09-09 15:08:12 +00002499
Douglas Gregor46f936e2010-11-19 17:10:50 +00002500 if (!ParseObjCProtocolQualifiers(DS))
2501 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
2502 << FixItHint::CreateInsertion(Loc, "id")
2503 << SourceRange(Loc, DS.getSourceRange().getEnd());
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00002504
2505 // Need to support trailing type qualifiers (e.g. "id<p> const").
2506 // If a type specifier follows, it will be diagnosed elsewhere.
2507 continue;
Reid Spencer5f016e22007-07-11 17:01:13 +00002508 }
John McCallfec54012009-08-03 20:12:06 +00002509 // If the specifier wasn't legal, issue a diagnostic.
Reid Spencer5f016e22007-07-11 17:01:13 +00002510 if (isInvalid) {
2511 assert(PrevSpec && "Method did not return previous specifier!");
John McCallfec54012009-08-03 20:12:06 +00002512 assert(DiagID);
Douglas Gregorae2fb142010-08-23 14:34:43 +00002513
2514 if (DiagID == diag::ext_duplicate_declspec)
2515 Diag(Tok, DiagID)
2516 << PrevSpec << FixItHint::CreateRemoval(Tok.getLocation());
2517 else
2518 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00002519 }
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002520
Chris Lattner81c018d2008-03-13 06:29:04 +00002521 DS.SetRangeEnd(Tok.getLocation());
Fariborz Jahaniane106a0b2011-04-19 21:42:37 +00002522 if (DiagID != diag::err_bool_redeclaration)
2523 ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00002524 }
2525}
Douglas Gregoradcac882008-12-01 23:54:00 +00002526
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002527/// ParseStructDeclaration - Parse a struct declaration without the terminating
2528/// semicolon.
2529///
Reid Spencer5f016e22007-07-11 17:01:13 +00002530/// struct-declaration:
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002531/// specifier-qualifier-list struct-declarator-list
Reid Spencer5f016e22007-07-11 17:01:13 +00002532/// [GNU] __extension__ struct-declaration
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002533/// [GNU] specifier-qualifier-list
Reid Spencer5f016e22007-07-11 17:01:13 +00002534/// struct-declarator-list:
2535/// struct-declarator
2536/// struct-declarator-list ',' struct-declarator
2537/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
2538/// struct-declarator:
2539/// declarator
2540/// [GNU] declarator attributes[opt]
2541/// declarator[opt] ':' constant-expression
2542/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
2543///
Chris Lattnere1359422008-04-10 06:46:29 +00002544void Parser::
John McCallbdd563e2009-11-03 02:38:08 +00002545ParseStructDeclaration(DeclSpec &DS, FieldCallback &Fields) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002546
Chris Lattnerc46d1a12008-10-20 06:45:43 +00002547 if (Tok.is(tok::kw___extension__)) {
2548 // __extension__ silences extension warnings in the subexpression.
2549 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroff28a7ca82007-08-20 22:28:22 +00002550 ConsumeToken();
Chris Lattnerc46d1a12008-10-20 06:45:43 +00002551 return ParseStructDeclaration(DS, Fields);
2552 }
Mike Stump1eb44332009-09-09 15:08:12 +00002553
Steve Naroff28a7ca82007-08-20 22:28:22 +00002554 // Parse the common specifier-qualifiers-list piece.
Steve Naroff28a7ca82007-08-20 22:28:22 +00002555 ParseSpecifierQualifierList(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00002556
Douglas Gregor4920f1f2009-01-12 22:49:06 +00002557 // If there are no declarators, this is a free-standing declaration
2558 // specifier. Let the actions module cope with it.
Chris Lattner04d66662007-10-09 17:33:22 +00002559 if (Tok.is(tok::semi)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00002560 Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none, DS);
Steve Naroff28a7ca82007-08-20 22:28:22 +00002561 return;
2562 }
2563
2564 // Read struct-declarators until we find the semicolon.
John McCallbdd563e2009-11-03 02:38:08 +00002565 bool FirstDeclarator = true;
Richard Smith7984de32012-01-12 23:53:29 +00002566 SourceLocation CommaLoc;
Steve Naroff28a7ca82007-08-20 22:28:22 +00002567 while (1) {
John McCall92576642012-05-07 06:16:41 +00002568 ParsingDeclRAIIObject PD(*this, ParsingDeclRAIIObject::NoParent);
John McCallbdd563e2009-11-03 02:38:08 +00002569 FieldDeclarator DeclaratorInfo(DS);
Richard Smith7984de32012-01-12 23:53:29 +00002570 DeclaratorInfo.D.setCommaLoc(CommaLoc);
John McCallbdd563e2009-11-03 02:38:08 +00002571
2572 // Attributes are only allowed here on successive declarators.
John McCall7f040a92010-12-24 02:08:15 +00002573 if (!FirstDeclarator)
2574 MaybeParseGNUAttributes(DeclaratorInfo.D);
Mike Stump1eb44332009-09-09 15:08:12 +00002575
Steve Naroff28a7ca82007-08-20 22:28:22 +00002576 /// struct-declarator: declarator
2577 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattnera1efc8c2009-12-10 01:59:24 +00002578 if (Tok.isNot(tok::colon)) {
2579 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
2580 ColonProtectionRAIIObject X(*this);
Chris Lattnere1359422008-04-10 06:46:29 +00002581 ParseDeclarator(DeclaratorInfo.D);
Chris Lattnera1efc8c2009-12-10 01:59:24 +00002582 }
Mike Stump1eb44332009-09-09 15:08:12 +00002583
Chris Lattner04d66662007-10-09 17:33:22 +00002584 if (Tok.is(tok::colon)) {
Steve Naroff28a7ca82007-08-20 22:28:22 +00002585 ConsumeToken();
John McCall60d7b3a2010-08-24 06:29:42 +00002586 ExprResult Res(ParseConstantExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002587 if (Res.isInvalid())
Steve Naroff28a7ca82007-08-20 22:28:22 +00002588 SkipUntil(tok::semi, true, true);
Chris Lattner60b1e3e2008-04-10 06:15:14 +00002589 else
Sebastian Redleffa8d12008-12-10 00:02:53 +00002590 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroff28a7ca82007-08-20 22:28:22 +00002591 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00002592
Steve Naroff28a7ca82007-08-20 22:28:22 +00002593 // If attributes exist after the declarator, parse them.
John McCall7f040a92010-12-24 02:08:15 +00002594 MaybeParseGNUAttributes(DeclaratorInfo.D);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002595
John McCallbdd563e2009-11-03 02:38:08 +00002596 // We're done with this declarator; invoke the callback.
John McCalld226f652010-08-21 09:40:31 +00002597 Decl *D = Fields.invoke(DeclaratorInfo);
John McCall54abf7d2009-11-04 02:18:39 +00002598 PD.complete(D);
John McCallbdd563e2009-11-03 02:38:08 +00002599
Steve Naroff28a7ca82007-08-20 22:28:22 +00002600 // If we don't have a comma, it is either the end of the list (a ';')
2601 // or an error, bail out.
Chris Lattner04d66662007-10-09 17:33:22 +00002602 if (Tok.isNot(tok::comma))
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002603 return;
Sebastian Redlab197ba2009-02-09 18:23:29 +00002604
Steve Naroff28a7ca82007-08-20 22:28:22 +00002605 // Consume the comma.
Richard Smith7984de32012-01-12 23:53:29 +00002606 CommaLoc = ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00002607
John McCallbdd563e2009-11-03 02:38:08 +00002608 FirstDeclarator = false;
Steve Naroff28a7ca82007-08-20 22:28:22 +00002609 }
Steve Naroff28a7ca82007-08-20 22:28:22 +00002610}
2611
2612/// ParseStructUnionBody
2613/// struct-contents:
2614/// struct-declaration-list
2615/// [EXT] empty
2616/// [GNU] "struct-declaration-list" without terminatoring ';'
2617/// struct-declaration-list:
2618/// struct-declaration
2619/// struct-declaration-list struct-declaration
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002620/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroff28a7ca82007-08-20 22:28:22 +00002621///
Reid Spencer5f016e22007-07-11 17:01:13 +00002622void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
John McCalld226f652010-08-21 09:40:31 +00002623 unsigned TagType, Decl *TagDecl) {
John McCallf312b1e2010-08-26 23:41:50 +00002624 PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc,
2625 "parsing struct/union body");
Mike Stump1eb44332009-09-09 15:08:12 +00002626
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002627 BalancedDelimiterTracker T(*this, tok::l_brace);
2628 if (T.consumeOpen())
2629 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002630
Douglas Gregor3218c4b2009-01-09 22:42:13 +00002631 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
Douglas Gregor23c94db2010-07-02 17:43:08 +00002632 Actions.ActOnTagStartDefinition(getCurScope(), TagDecl);
Douglas Gregor72de6672009-01-08 20:45:30 +00002633
Reid Spencer5f016e22007-07-11 17:01:13 +00002634 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
2635 // C++.
David Blaikie4e4d0842012-03-11 07:00:24 +00002636 if (Tok.is(tok::r_brace) && !getLangOpts().CPlusPlus) {
Richard Smithd7c56e12011-12-29 21:57:33 +00002637 Diag(Tok, diag::ext_empty_struct_union) << (TagType == TST_union);
2638 Diag(Tok, diag::warn_empty_struct_union_compat) << (TagType == TST_union);
2639 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002640
Chris Lattner5f9e2722011-07-23 10:55:15 +00002641 SmallVector<Decl *, 32> FieldDecls;
Chris Lattnere1359422008-04-10 06:46:29 +00002642
Reid Spencer5f016e22007-07-11 17:01:13 +00002643 // While we still have something to read, read the declarations in the struct.
Chris Lattner04d66662007-10-09 17:33:22 +00002644 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002645 // Each iteration of this loop reads one struct-declaration.
Mike Stump1eb44332009-09-09 15:08:12 +00002646
Reid Spencer5f016e22007-07-11 17:01:13 +00002647 // Check for extraneous top-level semicolon.
Chris Lattner04d66662007-10-09 17:33:22 +00002648 if (Tok.is(tok::semi)) {
Douglas Gregor9b3064b2009-04-01 22:41:11 +00002649 Diag(Tok, diag::ext_extra_struct_semi)
Douglas Gregorf13ca062010-06-16 23:08:59 +00002650 << DeclSpec::getSpecifierName((DeclSpec::TST)TagType)
Douglas Gregor849b2432010-03-31 17:46:05 +00002651 << FixItHint::CreateRemoval(Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00002652 ConsumeToken();
2653 continue;
2654 }
Chris Lattnere1359422008-04-10 06:46:29 +00002655
2656 // Parse all the comma separated declarators.
John McCall0b7e6782011-03-24 11:26:52 +00002657 DeclSpec DS(AttrFactory);
Mike Stump1eb44332009-09-09 15:08:12 +00002658
John McCallbdd563e2009-11-03 02:38:08 +00002659 if (!Tok.is(tok::at)) {
2660 struct CFieldCallback : FieldCallback {
2661 Parser &P;
John McCalld226f652010-08-21 09:40:31 +00002662 Decl *TagDecl;
Chris Lattner5f9e2722011-07-23 10:55:15 +00002663 SmallVectorImpl<Decl *> &FieldDecls;
John McCallbdd563e2009-11-03 02:38:08 +00002664
John McCalld226f652010-08-21 09:40:31 +00002665 CFieldCallback(Parser &P, Decl *TagDecl,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002666 SmallVectorImpl<Decl *> &FieldDecls) :
John McCallbdd563e2009-11-03 02:38:08 +00002667 P(P), TagDecl(TagDecl), FieldDecls(FieldDecls) {}
2668
John McCalld226f652010-08-21 09:40:31 +00002669 virtual Decl *invoke(FieldDeclarator &FD) {
John McCallbdd563e2009-11-03 02:38:08 +00002670 // Install the declarator into the current TagDecl.
John McCalld226f652010-08-21 09:40:31 +00002671 Decl *Field = P.Actions.ActOnField(P.getCurScope(), TagDecl,
John McCall4ba39712009-11-03 21:13:47 +00002672 FD.D.getDeclSpec().getSourceRange().getBegin(),
2673 FD.D, FD.BitfieldSize);
John McCallbdd563e2009-11-03 02:38:08 +00002674 FieldDecls.push_back(Field);
2675 return Field;
Douglas Gregor91a28862009-08-26 14:27:30 +00002676 }
John McCallbdd563e2009-11-03 02:38:08 +00002677 } Callback(*this, TagDecl, FieldDecls);
2678
2679 ParseStructDeclaration(DS, Callback);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002680 } else { // Handle @defs
2681 ConsumeToken();
2682 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
2683 Diag(Tok, diag::err_unexpected_at);
Chris Lattner3e156ad2010-02-02 00:37:27 +00002684 SkipUntil(tok::semi, true);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002685 continue;
2686 }
2687 ConsumeToken();
2688 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
2689 if (!Tok.is(tok::identifier)) {
2690 Diag(Tok, diag::err_expected_ident);
Chris Lattner3e156ad2010-02-02 00:37:27 +00002691 SkipUntil(tok::semi, true);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002692 continue;
2693 }
Chris Lattner5f9e2722011-07-23 10:55:15 +00002694 SmallVector<Decl *, 16> Fields;
Douglas Gregor23c94db2010-07-02 17:43:08 +00002695 Actions.ActOnDefs(getCurScope(), TagDecl, Tok.getLocation(),
Douglas Gregor44b43212008-12-11 16:49:14 +00002696 Tok.getIdentifierInfo(), Fields);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002697 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
2698 ConsumeToken();
2699 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
Mike Stump1eb44332009-09-09 15:08:12 +00002700 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002701
Chris Lattner04d66662007-10-09 17:33:22 +00002702 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002703 ConsumeToken();
Chris Lattner04d66662007-10-09 17:33:22 +00002704 } else if (Tok.is(tok::r_brace)) {
Chris Lattner3e156ad2010-02-02 00:37:27 +00002705 ExpectAndConsume(tok::semi, diag::ext_expected_semi_decl_list);
Reid Spencer5f016e22007-07-11 17:01:13 +00002706 break;
2707 } else {
Chris Lattner3e156ad2010-02-02 00:37:27 +00002708 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
2709 // Skip to end of block or statement to avoid ext-warning on extra ';'.
Reid Spencer5f016e22007-07-11 17:01:13 +00002710 SkipUntil(tok::r_brace, true, true);
Chris Lattner3e156ad2010-02-02 00:37:27 +00002711 // If we stopped at a ';', eat it.
2712 if (Tok.is(tok::semi)) ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00002713 }
2714 }
Mike Stump1eb44332009-09-09 15:08:12 +00002715
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002716 T.consumeClose();
Mike Stump1eb44332009-09-09 15:08:12 +00002717
John McCall0b7e6782011-03-24 11:26:52 +00002718 ParsedAttributes attrs(AttrFactory);
Reid Spencer5f016e22007-07-11 17:01:13 +00002719 // If attributes exist after struct contents, parse them.
John McCall7f040a92010-12-24 02:08:15 +00002720 MaybeParseGNUAttributes(attrs);
Daniel Dunbar1bfe1c22008-10-03 02:03:53 +00002721
Douglas Gregor23c94db2010-07-02 17:43:08 +00002722 Actions.ActOnFields(getCurScope(),
David Blaikie77b6de02011-09-22 02:58:26 +00002723 RecordLoc, TagDecl, FieldDecls,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002724 T.getOpenLocation(), T.getCloseLocation(),
John McCall7f040a92010-12-24 02:08:15 +00002725 attrs.getList());
Douglas Gregor72de6672009-01-08 20:45:30 +00002726 StructScope.Exit();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002727 Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl,
2728 T.getCloseLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00002729}
2730
Reid Spencer5f016e22007-07-11 17:01:13 +00002731/// ParseEnumSpecifier
2732/// enum-specifier: [C99 6.7.2.2]
2733/// 'enum' identifier[opt] '{' enumerator-list '}'
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002734///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Reid Spencer5f016e22007-07-11 17:01:13 +00002735/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
2736/// '}' attributes[opt]
Aaron Ballman6454a022012-03-01 04:09:28 +00002737/// [MS] 'enum' __declspec[opt] identifier[opt] '{' enumerator-list ',' [opt]
2738/// '}'
Reid Spencer5f016e22007-07-11 17:01:13 +00002739/// 'enum' identifier
2740/// [GNU] 'enum' attributes[opt] identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002741///
Richard Smith1af83c42012-03-23 03:33:32 +00002742/// [C++11] enum-head '{' enumerator-list[opt] '}'
2743/// [C++11] enum-head '{' enumerator-list ',' '}'
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002744///
Richard Smith1af83c42012-03-23 03:33:32 +00002745/// enum-head: [C++11]
2746/// enum-key attribute-specifier-seq[opt] identifier[opt] enum-base[opt]
2747/// enum-key attribute-specifier-seq[opt] nested-name-specifier
2748/// identifier enum-base[opt]
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002749///
Richard Smith1af83c42012-03-23 03:33:32 +00002750/// enum-key: [C++11]
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002751/// 'enum'
2752/// 'enum' 'class'
2753/// 'enum' 'struct'
2754///
Richard Smith1af83c42012-03-23 03:33:32 +00002755/// enum-base: [C++11]
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002756/// ':' type-specifier-seq
2757///
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002758/// [C++] elaborated-type-specifier:
2759/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
2760///
Chris Lattner4c97d762009-04-12 21:49:30 +00002761void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregor9b9edd62010-03-02 17:53:14 +00002762 const ParsedTemplateInfo &TemplateInfo,
Richard Smith69730c12012-03-12 07:56:15 +00002763 AccessSpecifier AS, DeclSpecContext DSC) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002764 // Parse the tag portion of this.
Douglas Gregor374929f2009-09-18 15:37:17 +00002765 if (Tok.is(tok::code_completion)) {
2766 // Code completion for an enum name.
Douglas Gregor23c94db2010-07-02 17:43:08 +00002767 Actions.CodeCompleteTag(getCurScope(), DeclSpec::TST_enum);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002768 return cutOffParsing();
Douglas Gregor374929f2009-09-18 15:37:17 +00002769 }
John McCall57c13002011-07-06 05:58:41 +00002770
Richard Smithbdad7a22012-01-10 01:33:14 +00002771 SourceLocation ScopedEnumKWLoc;
John McCall57c13002011-07-06 05:58:41 +00002772 bool IsScopedUsingClassTag = false;
2773
David Blaikie4e4d0842012-03-11 07:00:24 +00002774 if (getLangOpts().CPlusPlus0x &&
John McCall57c13002011-07-06 05:58:41 +00002775 (Tok.is(tok::kw_class) || Tok.is(tok::kw_struct))) {
Richard Smith7fe62082011-10-15 05:09:34 +00002776 Diag(Tok, diag::warn_cxx98_compat_scoped_enum);
John McCall57c13002011-07-06 05:58:41 +00002777 IsScopedUsingClassTag = Tok.is(tok::kw_class);
Richard Smithbdad7a22012-01-10 01:33:14 +00002778 ScopedEnumKWLoc = ConsumeToken();
John McCall57c13002011-07-06 05:58:41 +00002779 }
Richard Smith1af83c42012-03-23 03:33:32 +00002780
John McCall13489672012-05-07 06:16:58 +00002781 // C++11 [temp.explicit]p12:
2782 // The usual access controls do not apply to names used to specify
2783 // explicit instantiations.
2784 // We extend this to also cover explicit specializations. Note that
2785 // we don't suppress if this turns out to be an elaborated type
2786 // specifier.
2787 bool shouldDelayDiagsInTag =
2788 (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation ||
2789 TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization);
2790 SuppressAccessChecks diagsFromTag(*this, shouldDelayDiagsInTag);
Richard Smith1af83c42012-03-23 03:33:32 +00002791
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002792 // If attributes exist after tag, parse them.
John McCall0b7e6782011-03-24 11:26:52 +00002793 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00002794 MaybeParseGNUAttributes(attrs);
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002795
Aaron Ballman6454a022012-03-01 04:09:28 +00002796 // If declspecs exist after tag, parse them.
2797 while (Tok.is(tok::kw___declspec))
2798 ParseMicrosoftDeclSpec(attrs);
2799
Richard Smith7796eb52012-03-12 08:56:40 +00002800 // Enum definitions should not be parsed in a trailing-return-type.
2801 bool AllowDeclaration = DSC != DSC_trailing;
2802
2803 bool AllowFixedUnderlyingType = AllowDeclaration &&
2804 (getLangOpts().CPlusPlus0x || getLangOpts().MicrosoftExt ||
2805 getLangOpts().ObjC2);
John McCall57c13002011-07-06 05:58:41 +00002806
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00002807 CXXScopeSpec &SS = DS.getTypeSpecScope();
David Blaikie4e4d0842012-03-11 07:00:24 +00002808 if (getLangOpts().CPlusPlus) {
John McCall57c13002011-07-06 05:58:41 +00002809 // "enum foo : bar;" is not a potential typo for "enum foo::bar;"
2810 // if a fixed underlying type is allowed.
2811 ColonProtectionRAIIObject X(*this, AllowFixedUnderlyingType);
2812
Douglas Gregorefaa93a2011-11-07 17:33:42 +00002813 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
2814 /*EnteringContext=*/false))
John McCall9ba61662010-02-26 08:45:28 +00002815 return;
2816
2817 if (SS.isSet() && Tok.isNot(tok::identifier)) {
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002818 Diag(Tok, diag::err_expected_ident);
2819 if (Tok.isNot(tok::l_brace)) {
2820 // Has no name and is not a definition.
2821 // Skip the rest of this declarator, up until the comma or semicolon.
2822 SkipUntil(tok::comma, true);
2823 return;
2824 }
2825 }
2826 }
Mike Stump1eb44332009-09-09 15:08:12 +00002827
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002828 // Must have either 'enum name' or 'enum {...}'.
Douglas Gregorb9075602011-02-22 02:55:24 +00002829 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace) &&
Richard Smith7796eb52012-03-12 08:56:40 +00002830 !(AllowFixedUnderlyingType && Tok.is(tok::colon))) {
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002831 Diag(Tok, diag::err_expected_ident_lbrace);
Mike Stump1eb44332009-09-09 15:08:12 +00002832
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002833 // Skip the rest of this declarator, up until the comma or semicolon.
2834 SkipUntil(tok::comma, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00002835 return;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002836 }
Mike Stump1eb44332009-09-09 15:08:12 +00002837
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002838 // If an identifier is present, consume and remember it.
2839 IdentifierInfo *Name = 0;
2840 SourceLocation NameLoc;
2841 if (Tok.is(tok::identifier)) {
2842 Name = Tok.getIdentifierInfo();
2843 NameLoc = ConsumeToken();
2844 }
Mike Stump1eb44332009-09-09 15:08:12 +00002845
Richard Smithbdad7a22012-01-10 01:33:14 +00002846 if (!Name && ScopedEnumKWLoc.isValid()) {
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002847 // C++0x 7.2p2: The optional identifier shall not be omitted in the
2848 // declaration of a scoped enumeration.
2849 Diag(Tok, diag::err_scoped_enum_missing_identifier);
Richard Smithbdad7a22012-01-10 01:33:14 +00002850 ScopedEnumKWLoc = SourceLocation();
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00002851 IsScopedUsingClassTag = false;
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002852 }
2853
John McCall13489672012-05-07 06:16:58 +00002854 // Okay, end the suppression area. We'll decide whether to emit the
2855 // diagnostics in a second.
2856 if (shouldDelayDiagsInTag)
2857 diagsFromTag.done();
Richard Smith1af83c42012-03-23 03:33:32 +00002858
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002859 TypeResult BaseType;
2860
Douglas Gregora61b3e72010-12-01 17:42:47 +00002861 // Parse the fixed underlying type.
Douglas Gregorb9075602011-02-22 02:55:24 +00002862 if (AllowFixedUnderlyingType && Tok.is(tok::colon)) {
Douglas Gregora61b3e72010-12-01 17:42:47 +00002863 bool PossibleBitfield = false;
2864 if (getCurScope()->getFlags() & Scope::ClassScope) {
2865 // If we're in class scope, this can either be an enum declaration with
2866 // an underlying type, or a declaration of a bitfield member. We try to
2867 // use a simple disambiguation scheme first to catch the common cases
2868 // (integer literal, sizeof); if it's still ambiguous, we then consider
2869 // anything that's a simple-type-specifier followed by '(' as an
2870 // expression. This suffices because function types are not valid
2871 // underlying types anyway.
2872 TPResult TPR = isExpressionOrTypeSpecifierSimple(NextToken().getKind());
2873 // If the next token starts an expression, we know we're parsing a
2874 // bit-field. This is the common case.
2875 if (TPR == TPResult::True())
2876 PossibleBitfield = true;
2877 // If the next token starts a type-specifier-seq, it may be either a
2878 // a fixed underlying type or the start of a function-style cast in C++;
2879 // lookahead one more token to see if it's obvious that we have a
2880 // fixed underlying type.
2881 else if (TPR == TPResult::False() &&
2882 GetLookAheadToken(2).getKind() == tok::semi) {
2883 // Consume the ':'.
2884 ConsumeToken();
2885 } else {
2886 // We have the start of a type-specifier-seq, so we have to perform
2887 // tentative parsing to determine whether we have an expression or a
2888 // type.
2889 TentativeParsingAction TPA(*this);
2890
2891 // Consume the ':'.
2892 ConsumeToken();
Richard Smithd81e9612012-02-23 01:36:12 +00002893
2894 // If we see a type specifier followed by an open-brace, we have an
2895 // ambiguity between an underlying type and a C++11 braced
2896 // function-style cast. Resolve this by always treating it as an
2897 // underlying type.
2898 // FIXME: The standard is not entirely clear on how to disambiguate in
2899 // this case.
David Blaikie4e4d0842012-03-11 07:00:24 +00002900 if ((getLangOpts().CPlusPlus &&
Richard Smithd81e9612012-02-23 01:36:12 +00002901 isCXXDeclarationSpecifier(TPResult::True()) != TPResult::True()) ||
David Blaikie4e4d0842012-03-11 07:00:24 +00002902 (!getLangOpts().CPlusPlus && !isDeclarationSpecifier(true))) {
Douglas Gregora61b3e72010-12-01 17:42:47 +00002903 // We'll parse this as a bitfield later.
2904 PossibleBitfield = true;
2905 TPA.Revert();
2906 } else {
2907 // We have a type-specifier-seq.
2908 TPA.Commit();
2909 }
2910 }
2911 } else {
2912 // Consume the ':'.
2913 ConsumeToken();
2914 }
2915
2916 if (!PossibleBitfield) {
2917 SourceRange Range;
2918 BaseType = ParseTypeName(&Range);
Douglas Gregor86f208c2011-02-22 20:32:04 +00002919
David Blaikie4e4d0842012-03-11 07:00:24 +00002920 if (!getLangOpts().CPlusPlus0x && !getLangOpts().ObjC2)
Douglas Gregor86f208c2011-02-22 20:32:04 +00002921 Diag(StartLoc, diag::ext_ms_enum_fixed_underlying_type)
2922 << Range;
David Blaikie4e4d0842012-03-11 07:00:24 +00002923 if (getLangOpts().CPlusPlus0x)
Richard Smith7fe62082011-10-15 05:09:34 +00002924 Diag(StartLoc, diag::warn_cxx98_compat_enum_fixed_underlying_type);
Douglas Gregora61b3e72010-12-01 17:42:47 +00002925 }
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002926 }
2927
Richard Smithbdad7a22012-01-10 01:33:14 +00002928 // There are four options here. If we have 'friend enum foo;' then this is a
2929 // friend declaration, and cannot have an accompanying definition. If we have
2930 // 'enum foo;', then this is a forward declaration. If we have
2931 // 'enum foo {...' then this is a definition. Otherwise we have something
2932 // like 'enum foo xyz', a reference.
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002933 //
2934 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
2935 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
2936 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
2937 //
John McCallf312b1e2010-08-26 23:41:50 +00002938 Sema::TagUseKind TUK;
John McCall13489672012-05-07 06:16:58 +00002939 if (!AllowDeclaration) {
Richard Smith7796eb52012-03-12 08:56:40 +00002940 TUK = Sema::TUK_Reference;
John McCall13489672012-05-07 06:16:58 +00002941 } else if (Tok.is(tok::l_brace)) {
2942 if (DS.isFriendSpecified()) {
2943 Diag(Tok.getLocation(), diag::err_friend_decl_defines_type)
2944 << SourceRange(DS.getFriendSpecLoc());
2945 ConsumeBrace();
2946 SkipUntil(tok::r_brace);
2947 TUK = Sema::TUK_Friend;
2948 } else {
2949 TUK = Sema::TUK_Definition;
2950 }
2951 } else if (Tok.is(tok::semi) && DSC != DSC_type_specifier) {
2952 TUK = (DS.isFriendSpecified() ? Sema::TUK_Friend : Sema::TUK_Declaration);
2953 } else {
John McCallf312b1e2010-08-26 23:41:50 +00002954 TUK = Sema::TUK_Reference;
John McCall13489672012-05-07 06:16:58 +00002955 }
2956
2957 // If this is an elaborated type specifier, and we delayed
2958 // diagnostics before, just merge them into the current pool.
2959 if (TUK == Sema::TUK_Reference && shouldDelayDiagsInTag) {
2960 diagsFromTag.redelay();
2961 }
Richard Smith1af83c42012-03-23 03:33:32 +00002962
2963 MultiTemplateParamsArg TParams;
Douglas Gregor8fc6d232010-05-03 17:48:54 +00002964 if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate &&
John McCallf312b1e2010-08-26 23:41:50 +00002965 TUK != Sema::TUK_Reference) {
Richard Smith1af83c42012-03-23 03:33:32 +00002966 if (!getLangOpts().CPlusPlus0x || !SS.isSet()) {
2967 // Skip the rest of this declarator, up until the comma or semicolon.
2968 Diag(Tok, diag::err_enum_template);
2969 SkipUntil(tok::comma, true);
2970 return;
2971 }
2972
2973 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) {
2974 // Enumerations can't be explicitly instantiated.
2975 DS.SetTypeSpecError();
2976 Diag(StartLoc, diag::err_explicit_instantiation_enum);
2977 return;
2978 }
2979
2980 assert(TemplateInfo.TemplateParams && "no template parameters");
2981 TParams = MultiTemplateParamsArg(TemplateInfo.TemplateParams->data(),
2982 TemplateInfo.TemplateParams->size());
Douglas Gregor8fc6d232010-05-03 17:48:54 +00002983 }
Richard Smith1af83c42012-03-23 03:33:32 +00002984
Douglas Gregorb9075602011-02-22 02:55:24 +00002985 if (!Name && TUK != Sema::TUK_Definition) {
2986 Diag(Tok, diag::err_enumerator_unnamed_no_def);
Richard Smith1af83c42012-03-23 03:33:32 +00002987
Douglas Gregorb9075602011-02-22 02:55:24 +00002988 // Skip the rest of this declarator, up until the comma or semicolon.
2989 SkipUntil(tok::comma, true);
2990 return;
2991 }
Richard Smith1af83c42012-03-23 03:33:32 +00002992
Douglas Gregor402abb52009-05-28 23:31:59 +00002993 bool Owned = false;
John McCallc4e70192009-09-11 04:59:25 +00002994 bool IsDependent = false;
Douglas Gregor48c89f42010-04-24 16:38:41 +00002995 const char *PrevSpec = 0;
2996 unsigned DiagID;
John McCalld226f652010-08-21 09:40:31 +00002997 Decl *TagDecl = Actions.ActOnTag(getCurScope(), DeclSpec::TST_enum, TUK,
John McCall7f040a92010-12-24 02:08:15 +00002998 StartLoc, SS, Name, NameLoc, attrs.getList(),
Richard Smith1af83c42012-03-23 03:33:32 +00002999 AS, DS.getModulePrivateSpecLoc(), TParams,
Richard Smithbdad7a22012-01-10 01:33:14 +00003000 Owned, IsDependent, ScopedEnumKWLoc,
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00003001 IsScopedUsingClassTag, BaseType);
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003002
Douglas Gregor48c89f42010-04-24 16:38:41 +00003003 if (IsDependent) {
3004 // This enum has a dependent nested-name-specifier. Handle it as a
3005 // dependent tag.
3006 if (!Name) {
3007 DS.SetTypeSpecError();
3008 Diag(Tok, diag::err_expected_type_name_after_typename);
3009 return;
3010 }
3011
Douglas Gregor23c94db2010-07-02 17:43:08 +00003012 TypeResult Type = Actions.ActOnDependentTag(getCurScope(), DeclSpec::TST_enum,
Douglas Gregor48c89f42010-04-24 16:38:41 +00003013 TUK, SS, Name, StartLoc,
3014 NameLoc);
3015 if (Type.isInvalid()) {
3016 DS.SetTypeSpecError();
3017 return;
3018 }
3019
Abramo Bagnara0daaf322011-03-16 20:16:18 +00003020 if (DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc,
3021 NameLoc.isValid() ? NameLoc : StartLoc,
3022 PrevSpec, DiagID, Type.get()))
Douglas Gregor48c89f42010-04-24 16:38:41 +00003023 Diag(StartLoc, DiagID) << PrevSpec;
3024
3025 return;
3026 }
Mike Stump1eb44332009-09-09 15:08:12 +00003027
John McCalld226f652010-08-21 09:40:31 +00003028 if (!TagDecl) {
Douglas Gregor48c89f42010-04-24 16:38:41 +00003029 // The action failed to produce an enumeration tag. If this is a
3030 // definition, consume the entire definition.
Richard Smith7796eb52012-03-12 08:56:40 +00003031 if (Tok.is(tok::l_brace) && TUK != Sema::TUK_Reference) {
Douglas Gregor48c89f42010-04-24 16:38:41 +00003032 ConsumeBrace();
3033 SkipUntil(tok::r_brace);
3034 }
3035
3036 DS.SetTypeSpecError();
3037 return;
3038 }
Richard Smithbdad7a22012-01-10 01:33:14 +00003039
Richard Smith7796eb52012-03-12 08:56:40 +00003040 if (Tok.is(tok::l_brace) && TUK != Sema::TUK_Reference) {
John McCall13489672012-05-07 06:16:58 +00003041 ParseEnumBody(StartLoc, TagDecl);
Richard Smithbdad7a22012-01-10 01:33:14 +00003042 }
Mike Stump1eb44332009-09-09 15:08:12 +00003043
Abramo Bagnara0daaf322011-03-16 20:16:18 +00003044 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc,
3045 NameLoc.isValid() ? NameLoc : StartLoc,
3046 PrevSpec, DiagID, TagDecl, Owned))
John McCallfec54012009-08-03 20:12:06 +00003047 Diag(StartLoc, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00003048}
3049
3050/// ParseEnumBody - Parse a {} enclosed enumerator-list.
3051/// enumerator-list:
3052/// enumerator
3053/// enumerator-list ',' enumerator
3054/// enumerator:
3055/// enumeration-constant
3056/// enumeration-constant '=' constant-expression
3057/// enumeration-constant:
3058/// identifier
3059///
John McCalld226f652010-08-21 09:40:31 +00003060void Parser::ParseEnumBody(SourceLocation StartLoc, Decl *EnumDecl) {
Douglas Gregor074149e2009-01-05 19:45:36 +00003061 // Enter the scope of the enum body and start the definition.
3062 ParseScope EnumScope(this, Scope::DeclScope);
Douglas Gregor23c94db2010-07-02 17:43:08 +00003063 Actions.ActOnTagStartDefinition(getCurScope(), EnumDecl);
Douglas Gregor074149e2009-01-05 19:45:36 +00003064
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003065 BalancedDelimiterTracker T(*this, tok::l_brace);
3066 T.consumeOpen();
Mike Stump1eb44332009-09-09 15:08:12 +00003067
Chris Lattner7946dd32007-08-27 17:24:30 +00003068 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
David Blaikie4e4d0842012-03-11 07:00:24 +00003069 if (Tok.is(tok::r_brace) && !getLangOpts().CPlusPlus)
Fariborz Jahanian05115522010-05-28 22:23:22 +00003070 Diag(Tok, diag::error_empty_enum);
Mike Stump1eb44332009-09-09 15:08:12 +00003071
Chris Lattner5f9e2722011-07-23 10:55:15 +00003072 SmallVector<Decl *, 32> EnumConstantDecls;
Reid Spencer5f016e22007-07-11 17:01:13 +00003073
John McCalld226f652010-08-21 09:40:31 +00003074 Decl *LastEnumConstDecl = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00003075
Reid Spencer5f016e22007-07-11 17:01:13 +00003076 // Parse the enumerator-list.
Chris Lattner04d66662007-10-09 17:33:22 +00003077 while (Tok.is(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003078 IdentifierInfo *Ident = Tok.getIdentifierInfo();
3079 SourceLocation IdentLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00003080
John McCall5b629aa2010-10-22 23:36:17 +00003081 // If attributes exist after the enumerator, parse them.
John McCall0b7e6782011-03-24 11:26:52 +00003082 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00003083 MaybeParseGNUAttributes(attrs);
John McCall5b629aa2010-10-22 23:36:17 +00003084
Reid Spencer5f016e22007-07-11 17:01:13 +00003085 SourceLocation EqualLoc;
John McCall60d7b3a2010-08-24 06:29:42 +00003086 ExprResult AssignedVal;
John McCall92576642012-05-07 06:16:41 +00003087 ParsingDeclRAIIObject PD(*this, ParsingDeclRAIIObject::NoParent);
Fariborz Jahanian5a477db2011-12-09 01:15:54 +00003088
Chris Lattner04d66662007-10-09 17:33:22 +00003089 if (Tok.is(tok::equal)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003090 EqualLoc = ConsumeToken();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00003091 AssignedVal = ParseConstantExpression();
3092 if (AssignedVal.isInvalid())
Reid Spencer5f016e22007-07-11 17:01:13 +00003093 SkipUntil(tok::comma, tok::r_brace, true, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00003094 }
Mike Stump1eb44332009-09-09 15:08:12 +00003095
Reid Spencer5f016e22007-07-11 17:01:13 +00003096 // Install the enumerator constant into EnumDecl.
John McCalld226f652010-08-21 09:40:31 +00003097 Decl *EnumConstDecl = Actions.ActOnEnumConstant(getCurScope(), EnumDecl,
3098 LastEnumConstDecl,
3099 IdentLoc, Ident,
John McCall7f040a92010-12-24 02:08:15 +00003100 attrs.getList(), EqualLoc,
John McCalld226f652010-08-21 09:40:31 +00003101 AssignedVal.release());
Fariborz Jahanian5a477db2011-12-09 01:15:54 +00003102 PD.complete(EnumConstDecl);
3103
Reid Spencer5f016e22007-07-11 17:01:13 +00003104 EnumConstantDecls.push_back(EnumConstDecl);
3105 LastEnumConstDecl = EnumConstDecl;
Mike Stump1eb44332009-09-09 15:08:12 +00003106
Douglas Gregor751f6922010-09-07 14:51:08 +00003107 if (Tok.is(tok::identifier)) {
3108 // We're missing a comma between enumerators.
3109 SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation);
3110 Diag(Loc, diag::err_enumerator_list_missing_comma)
3111 << FixItHint::CreateInsertion(Loc, ", ");
3112 continue;
3113 }
3114
Chris Lattner04d66662007-10-09 17:33:22 +00003115 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +00003116 break;
3117 SourceLocation CommaLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00003118
Richard Smith7fe62082011-10-15 05:09:34 +00003119 if (Tok.isNot(tok::identifier)) {
David Blaikie4e4d0842012-03-11 07:00:24 +00003120 if (!getLangOpts().C99 && !getLangOpts().CPlusPlus0x)
Richard Smith7fe62082011-10-15 05:09:34 +00003121 Diag(CommaLoc, diag::ext_enumerator_list_comma)
David Blaikie4e4d0842012-03-11 07:00:24 +00003122 << getLangOpts().CPlusPlus
Richard Smith7fe62082011-10-15 05:09:34 +00003123 << FixItHint::CreateRemoval(CommaLoc);
David Blaikie4e4d0842012-03-11 07:00:24 +00003124 else if (getLangOpts().CPlusPlus0x)
Richard Smith7fe62082011-10-15 05:09:34 +00003125 Diag(CommaLoc, diag::warn_cxx98_compat_enumerator_list_comma)
3126 << FixItHint::CreateRemoval(CommaLoc);
3127 }
Reid Spencer5f016e22007-07-11 17:01:13 +00003128 }
Mike Stump1eb44332009-09-09 15:08:12 +00003129
Reid Spencer5f016e22007-07-11 17:01:13 +00003130 // Eat the }.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003131 T.consumeClose();
Reid Spencer5f016e22007-07-11 17:01:13 +00003132
Reid Spencer5f016e22007-07-11 17:01:13 +00003133 // If attributes exist after the identifier list, parse them.
John McCall0b7e6782011-03-24 11:26:52 +00003134 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00003135 MaybeParseGNUAttributes(attrs);
Douglas Gregor72de6672009-01-08 20:45:30 +00003136
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003137 Actions.ActOnEnumBody(StartLoc, T.getOpenLocation(), T.getCloseLocation(),
3138 EnumDecl, EnumConstantDecls.data(),
3139 EnumConstantDecls.size(), getCurScope(),
3140 attrs.getList());
Mike Stump1eb44332009-09-09 15:08:12 +00003141
Douglas Gregor72de6672009-01-08 20:45:30 +00003142 EnumScope.Exit();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003143 Actions.ActOnTagFinishDefinition(getCurScope(), EnumDecl,
3144 T.getCloseLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00003145}
3146
3147/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff5f8aa692008-02-11 23:15:56 +00003148/// start of a type-qualifier-list.
3149bool Parser::isTypeQualifier() const {
3150 switch (Tok.getKind()) {
3151 default: return false;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003152
3153 // type-qualifier only in OpenCL
3154 case tok::kw_private:
David Blaikie4e4d0842012-03-11 07:00:24 +00003155 return getLangOpts().OpenCL;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003156
Steve Naroff5f8aa692008-02-11 23:15:56 +00003157 // type-qualifier
3158 case tok::kw_const:
3159 case tok::kw_volatile:
3160 case tok::kw_restrict:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003161 case tok::kw___private:
3162 case tok::kw___local:
3163 case tok::kw___global:
3164 case tok::kw___constant:
3165 case tok::kw___read_only:
3166 case tok::kw___read_write:
3167 case tok::kw___write_only:
Steve Naroff5f8aa692008-02-11 23:15:56 +00003168 return true;
3169 }
3170}
3171
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003172/// isKnownToBeTypeSpecifier - Return true if we know that the specified token
3173/// is definitely a type-specifier. Return false if it isn't part of a type
3174/// specifier or if we're not sure.
3175bool Parser::isKnownToBeTypeSpecifier(const Token &Tok) const {
3176 switch (Tok.getKind()) {
3177 default: return false;
3178 // type-specifiers
3179 case tok::kw_short:
3180 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00003181 case tok::kw___int64:
Richard Smith5a5a9712012-04-04 06:24:32 +00003182 case tok::kw___int128:
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003183 case tok::kw_signed:
3184 case tok::kw_unsigned:
3185 case tok::kw__Complex:
3186 case tok::kw__Imaginary:
3187 case tok::kw_void:
3188 case tok::kw_char:
3189 case tok::kw_wchar_t:
3190 case tok::kw_char16_t:
3191 case tok::kw_char32_t:
3192 case tok::kw_int:
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00003193 case tok::kw_half:
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003194 case tok::kw_float:
3195 case tok::kw_double:
3196 case tok::kw_bool:
3197 case tok::kw__Bool:
3198 case tok::kw__Decimal32:
3199 case tok::kw__Decimal64:
3200 case tok::kw__Decimal128:
3201 case tok::kw___vector:
3202
3203 // struct-or-union-specifier (C99) or class-specifier (C++)
3204 case tok::kw_class:
3205 case tok::kw_struct:
3206 case tok::kw_union:
3207 // enum-specifier
3208 case tok::kw_enum:
3209
3210 // typedef-name
3211 case tok::annot_typename:
3212 return true;
3213 }
3214}
3215
Steve Naroff5f8aa692008-02-11 23:15:56 +00003216/// isTypeSpecifierQualifier - Return true if the current token could be the
Reid Spencer5f016e22007-07-11 17:01:13 +00003217/// start of a specifier-qualifier-list.
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003218bool Parser::isTypeSpecifierQualifier() {
Reid Spencer5f016e22007-07-11 17:01:13 +00003219 switch (Tok.getKind()) {
3220 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00003221
Chris Lattner166a8fc2009-01-04 23:41:41 +00003222 case tok::identifier: // foo::bar
John Thompson82287d12010-02-05 00:12:22 +00003223 if (TryAltiVecVectorToken())
3224 return true;
3225 // Fall through.
Douglas Gregord57959a2009-03-27 23:10:48 +00003226 case tok::kw_typename: // typename T::type
Chris Lattner166a8fc2009-01-04 23:41:41 +00003227 // Annotate typenames and C++ scope specifiers. If we get one, just
3228 // recurse to handle whatever we get.
3229 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003230 return true;
3231 if (Tok.is(tok::identifier))
3232 return false;
3233 return isTypeSpecifierQualifier();
Douglas Gregord57959a2009-03-27 23:10:48 +00003234
Chris Lattner166a8fc2009-01-04 23:41:41 +00003235 case tok::coloncolon: // ::foo::bar
3236 if (NextToken().is(tok::kw_new) || // ::new
3237 NextToken().is(tok::kw_delete)) // ::delete
3238 return false;
3239
Chris Lattner166a8fc2009-01-04 23:41:41 +00003240 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003241 return true;
3242 return isTypeSpecifierQualifier();
Mike Stump1eb44332009-09-09 15:08:12 +00003243
Reid Spencer5f016e22007-07-11 17:01:13 +00003244 // GNU attributes support.
3245 case tok::kw___attribute:
Steve Naroffd1861fd2007-07-31 12:34:36 +00003246 // GNU typeof support.
3247 case tok::kw_typeof:
Mike Stump1eb44332009-09-09 15:08:12 +00003248
Reid Spencer5f016e22007-07-11 17:01:13 +00003249 // type-specifiers
3250 case tok::kw_short:
3251 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00003252 case tok::kw___int64:
Richard Smith5a5a9712012-04-04 06:24:32 +00003253 case tok::kw___int128:
Reid Spencer5f016e22007-07-11 17:01:13 +00003254 case tok::kw_signed:
3255 case tok::kw_unsigned:
3256 case tok::kw__Complex:
3257 case tok::kw__Imaginary:
3258 case tok::kw_void:
3259 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00003260 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00003261 case tok::kw_char16_t:
3262 case tok::kw_char32_t:
Reid Spencer5f016e22007-07-11 17:01:13 +00003263 case tok::kw_int:
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00003264 case tok::kw_half:
Reid Spencer5f016e22007-07-11 17:01:13 +00003265 case tok::kw_float:
3266 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00003267 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00003268 case tok::kw__Bool:
3269 case tok::kw__Decimal32:
3270 case tok::kw__Decimal64:
3271 case tok::kw__Decimal128:
John Thompson82287d12010-02-05 00:12:22 +00003272 case tok::kw___vector:
Mike Stump1eb44332009-09-09 15:08:12 +00003273
Chris Lattner99dc9142008-04-13 18:59:07 +00003274 // struct-or-union-specifier (C99) or class-specifier (C++)
3275 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00003276 case tok::kw_struct:
3277 case tok::kw_union:
3278 // enum-specifier
3279 case tok::kw_enum:
Mike Stump1eb44332009-09-09 15:08:12 +00003280
Reid Spencer5f016e22007-07-11 17:01:13 +00003281 // type-qualifier
3282 case tok::kw_const:
3283 case tok::kw_volatile:
3284 case tok::kw_restrict:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003285
3286 // typedef-name
Chris Lattnerb31757b2009-01-06 05:06:21 +00003287 case tok::annot_typename:
Reid Spencer5f016e22007-07-11 17:01:13 +00003288 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00003289
Chris Lattner7c186be2008-10-20 00:25:30 +00003290 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
3291 case tok::less:
David Blaikie4e4d0842012-03-11 07:00:24 +00003292 return getLangOpts().ObjC1;
Mike Stump1eb44332009-09-09 15:08:12 +00003293
Steve Naroff239f0732008-12-25 14:16:32 +00003294 case tok::kw___cdecl:
3295 case tok::kw___stdcall:
3296 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00003297 case tok::kw___thiscall:
Eli Friedman290eeb02009-06-08 23:27:34 +00003298 case tok::kw___w64:
3299 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00003300 case tok::kw___ptr32:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003301 case tok::kw___pascal:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00003302 case tok::kw___unaligned:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003303
3304 case tok::kw___private:
3305 case tok::kw___local:
3306 case tok::kw___global:
3307 case tok::kw___constant:
3308 case tok::kw___read_only:
3309 case tok::kw___read_write:
3310 case tok::kw___write_only:
3311
Eli Friedman290eeb02009-06-08 23:27:34 +00003312 return true;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003313
3314 case tok::kw_private:
David Blaikie4e4d0842012-03-11 07:00:24 +00003315 return getLangOpts().OpenCL;
Eli Friedmanb001de72011-10-06 23:00:33 +00003316
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00003317 // C11 _Atomic()
Eli Friedmanb001de72011-10-06 23:00:33 +00003318 case tok::kw__Atomic:
3319 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00003320 }
3321}
3322
3323/// isDeclarationSpecifier() - Return true if the current token is part of a
3324/// declaration specifier.
Douglas Gregor9497a732010-09-16 01:51:54 +00003325///
3326/// \param DisambiguatingWithExpression True to indicate that the purpose of
3327/// this check is to disambiguate between an expression and a declaration.
3328bool Parser::isDeclarationSpecifier(bool DisambiguatingWithExpression) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003329 switch (Tok.getKind()) {
3330 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00003331
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003332 case tok::kw_private:
David Blaikie4e4d0842012-03-11 07:00:24 +00003333 return getLangOpts().OpenCL;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003334
Chris Lattner166a8fc2009-01-04 23:41:41 +00003335 case tok::identifier: // foo::bar
Steve Naroff61f72cb2009-03-09 21:12:44 +00003336 // Unfortunate hack to support "Class.factoryMethod" notation.
David Blaikie4e4d0842012-03-11 07:00:24 +00003337 if (getLangOpts().ObjC1 && NextToken().is(tok::period))
Steve Naroff61f72cb2009-03-09 21:12:44 +00003338 return false;
John Thompson82287d12010-02-05 00:12:22 +00003339 if (TryAltiVecVectorToken())
3340 return true;
3341 // Fall through.
David Blaikie42d6d0c2011-12-04 05:04:18 +00003342 case tok::kw_decltype: // decltype(T())::type
Douglas Gregord57959a2009-03-27 23:10:48 +00003343 case tok::kw_typename: // typename T::type
Chris Lattner166a8fc2009-01-04 23:41:41 +00003344 // Annotate typenames and C++ scope specifiers. If we get one, just
3345 // recurse to handle whatever we get.
3346 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003347 return true;
3348 if (Tok.is(tok::identifier))
3349 return false;
Douglas Gregor9497a732010-09-16 01:51:54 +00003350
3351 // If we're in Objective-C and we have an Objective-C class type followed
3352 // by an identifier and then either ':' or ']', in a place where an
3353 // expression is permitted, then this is probably a class message send
3354 // missing the initial '['. In this case, we won't consider this to be
3355 // the start of a declaration.
3356 if (DisambiguatingWithExpression &&
3357 isStartOfObjCClassMessageMissingOpenBracket())
3358 return false;
3359
John McCall9ba61662010-02-26 08:45:28 +00003360 return isDeclarationSpecifier();
3361
Chris Lattner166a8fc2009-01-04 23:41:41 +00003362 case tok::coloncolon: // ::foo::bar
3363 if (NextToken().is(tok::kw_new) || // ::new
3364 NextToken().is(tok::kw_delete)) // ::delete
3365 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00003366
Chris Lattner166a8fc2009-01-04 23:41:41 +00003367 // Annotate typenames and C++ scope specifiers. If we get one, just
3368 // recurse to handle whatever we get.
3369 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003370 return true;
3371 return isDeclarationSpecifier();
Mike Stump1eb44332009-09-09 15:08:12 +00003372
Reid Spencer5f016e22007-07-11 17:01:13 +00003373 // storage-class-specifier
3374 case tok::kw_typedef:
3375 case tok::kw_extern:
Steve Naroff8d54bf22007-12-18 00:16:02 +00003376 case tok::kw___private_extern__:
Reid Spencer5f016e22007-07-11 17:01:13 +00003377 case tok::kw_static:
3378 case tok::kw_auto:
3379 case tok::kw_register:
3380 case tok::kw___thread:
Mike Stump1eb44332009-09-09 15:08:12 +00003381
Douglas Gregor8d267c52011-09-09 02:06:17 +00003382 // Modules
3383 case tok::kw___module_private__:
3384
Reid Spencer5f016e22007-07-11 17:01:13 +00003385 // type-specifiers
3386 case tok::kw_short:
3387 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00003388 case tok::kw___int64:
Richard Smith5a5a9712012-04-04 06:24:32 +00003389 case tok::kw___int128:
Reid Spencer5f016e22007-07-11 17:01:13 +00003390 case tok::kw_signed:
3391 case tok::kw_unsigned:
3392 case tok::kw__Complex:
3393 case tok::kw__Imaginary:
3394 case tok::kw_void:
3395 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00003396 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00003397 case tok::kw_char16_t:
3398 case tok::kw_char32_t:
3399
Reid Spencer5f016e22007-07-11 17:01:13 +00003400 case tok::kw_int:
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00003401 case tok::kw_half:
Reid Spencer5f016e22007-07-11 17:01:13 +00003402 case tok::kw_float:
3403 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00003404 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00003405 case tok::kw__Bool:
3406 case tok::kw__Decimal32:
3407 case tok::kw__Decimal64:
3408 case tok::kw__Decimal128:
John Thompson82287d12010-02-05 00:12:22 +00003409 case tok::kw___vector:
Mike Stump1eb44332009-09-09 15:08:12 +00003410
Chris Lattner99dc9142008-04-13 18:59:07 +00003411 // struct-or-union-specifier (C99) or class-specifier (C++)
3412 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00003413 case tok::kw_struct:
3414 case tok::kw_union:
3415 // enum-specifier
3416 case tok::kw_enum:
Mike Stump1eb44332009-09-09 15:08:12 +00003417
Reid Spencer5f016e22007-07-11 17:01:13 +00003418 // type-qualifier
3419 case tok::kw_const:
3420 case tok::kw_volatile:
3421 case tok::kw_restrict:
Steve Naroffd1861fd2007-07-31 12:34:36 +00003422
Reid Spencer5f016e22007-07-11 17:01:13 +00003423 // function-specifier
3424 case tok::kw_inline:
Douglas Gregorb48fe382008-10-31 09:07:45 +00003425 case tok::kw_virtual:
3426 case tok::kw_explicit:
Chris Lattnerd6c7c182007-08-09 16:40:21 +00003427
Peter Collingbournec6eb44b2011-04-15 00:35:57 +00003428 // static_assert-declaration
3429 case tok::kw__Static_assert:
3430
Chris Lattner1ef08762007-08-09 17:01:07 +00003431 // GNU typeof support.
3432 case tok::kw_typeof:
Mike Stump1eb44332009-09-09 15:08:12 +00003433
Chris Lattner1ef08762007-08-09 17:01:07 +00003434 // GNU attributes.
Chris Lattnerd6c7c182007-08-09 16:40:21 +00003435 case tok::kw___attribute:
Reid Spencer5f016e22007-07-11 17:01:13 +00003436 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00003437
Francois Pichete3d49b42011-06-19 08:02:06 +00003438 // C++0x decltype.
David Blaikie42d6d0c2011-12-04 05:04:18 +00003439 case tok::annot_decltype:
Francois Pichete3d49b42011-06-19 08:02:06 +00003440 return true;
3441
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00003442 // C11 _Atomic()
Eli Friedmanb001de72011-10-06 23:00:33 +00003443 case tok::kw__Atomic:
3444 return true;
3445
Chris Lattnerf3948c42008-07-26 03:38:44 +00003446 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
3447 case tok::less:
David Blaikie4e4d0842012-03-11 07:00:24 +00003448 return getLangOpts().ObjC1;
Mike Stump1eb44332009-09-09 15:08:12 +00003449
Douglas Gregord9d75e52011-04-27 05:41:15 +00003450 // typedef-name
3451 case tok::annot_typename:
3452 return !DisambiguatingWithExpression ||
3453 !isStartOfObjCClassMessageMissingOpenBracket();
3454
Steve Naroff47f52092009-01-06 19:34:12 +00003455 case tok::kw___declspec:
Steve Naroff239f0732008-12-25 14:16:32 +00003456 case tok::kw___cdecl:
3457 case tok::kw___stdcall:
3458 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00003459 case tok::kw___thiscall:
Eli Friedman290eeb02009-06-08 23:27:34 +00003460 case tok::kw___w64:
3461 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00003462 case tok::kw___ptr32:
Eli Friedman290eeb02009-06-08 23:27:34 +00003463 case tok::kw___forceinline:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003464 case tok::kw___pascal:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00003465 case tok::kw___unaligned:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003466
3467 case tok::kw___private:
3468 case tok::kw___local:
3469 case tok::kw___global:
3470 case tok::kw___constant:
3471 case tok::kw___read_only:
3472 case tok::kw___read_write:
3473 case tok::kw___write_only:
3474
Eli Friedman290eeb02009-06-08 23:27:34 +00003475 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00003476 }
3477}
3478
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003479bool Parser::isConstructorDeclarator() {
3480 TentativeParsingAction TPA(*this);
3481
3482 // Parse the C++ scope specifier.
3483 CXXScopeSpec SS;
Douglas Gregorefaa93a2011-11-07 17:33:42 +00003484 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
3485 /*EnteringContext=*/true)) {
John McCall9ba61662010-02-26 08:45:28 +00003486 TPA.Revert();
3487 return false;
3488 }
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003489
3490 // Parse the constructor name.
3491 if (Tok.is(tok::identifier) || Tok.is(tok::annot_template_id)) {
3492 // We already know that we have a constructor name; just consume
3493 // the token.
3494 ConsumeToken();
3495 } else {
3496 TPA.Revert();
3497 return false;
3498 }
3499
Richard Smith22592862012-03-27 23:05:05 +00003500 // Current class name must be followed by a left parenthesis.
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003501 if (Tok.isNot(tok::l_paren)) {
3502 TPA.Revert();
3503 return false;
3504 }
3505 ConsumeParen();
3506
Richard Smith22592862012-03-27 23:05:05 +00003507 // A right parenthesis, or ellipsis followed by a right parenthesis signals
3508 // that we have a constructor.
3509 if (Tok.is(tok::r_paren) ||
3510 (Tok.is(tok::ellipsis) && NextToken().is(tok::r_paren))) {
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003511 TPA.Revert();
3512 return true;
3513 }
3514
3515 // If we need to, enter the specified scope.
3516 DeclaratorScopeObj DeclScopeObj(*this, SS);
Douglas Gregor23c94db2010-07-02 17:43:08 +00003517 if (SS.isSet() && Actions.ShouldEnterDeclaratorScope(getCurScope(), SS))
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003518 DeclScopeObj.EnterDeclaratorScope();
3519
Francois Pichetdfaa5fb2011-01-31 04:54:32 +00003520 // Optionally skip Microsoft attributes.
John McCall0b7e6782011-03-24 11:26:52 +00003521 ParsedAttributes Attrs(AttrFactory);
Francois Pichetdfaa5fb2011-01-31 04:54:32 +00003522 MaybeParseMicrosoftAttributes(Attrs);
3523
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003524 // Check whether the next token(s) are part of a declaration
3525 // specifier, in which case we have the start of a parameter and,
3526 // therefore, we know that this is a constructor.
Richard Smith412e0cc2012-03-27 00:56:56 +00003527 bool IsConstructor = false;
3528 if (isDeclarationSpecifier())
3529 IsConstructor = true;
3530 else if (Tok.is(tok::identifier) ||
3531 (Tok.is(tok::annot_cxxscope) && NextToken().is(tok::identifier))) {
3532 // We've seen "C ( X" or "C ( X::Y", but "X" / "X::Y" is not a type.
3533 // This might be a parenthesized member name, but is more likely to
3534 // be a constructor declaration with an invalid argument type. Keep
3535 // looking.
3536 if (Tok.is(tok::annot_cxxscope))
3537 ConsumeToken();
3538 ConsumeToken();
3539
3540 // If this is not a constructor, we must be parsing a declarator,
Richard Smith5d8388c2012-03-27 01:42:32 +00003541 // which must have one of the following syntactic forms (see the
3542 // grammar extract at the start of ParseDirectDeclarator):
Richard Smith412e0cc2012-03-27 00:56:56 +00003543 switch (Tok.getKind()) {
3544 case tok::l_paren:
3545 // C(X ( int));
3546 case tok::l_square:
3547 // C(X [ 5]);
3548 // C(X [ [attribute]]);
3549 case tok::coloncolon:
3550 // C(X :: Y);
3551 // C(X :: *p);
3552 case tok::r_paren:
3553 // C(X )
3554 // Assume this isn't a constructor, rather than assuming it's a
3555 // constructor with an unnamed parameter of an ill-formed type.
3556 break;
3557
3558 default:
3559 IsConstructor = true;
3560 break;
3561 }
3562 }
3563
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003564 TPA.Revert();
3565 return IsConstructor;
3566}
Reid Spencer5f016e22007-07-11 17:01:13 +00003567
3568/// ParseTypeQualifierListOpt
Dawn Perchik52fc3142010-09-03 01:29:35 +00003569/// type-qualifier-list: [C99 6.7.5]
3570/// type-qualifier
3571/// [vendor] attributes
3572/// [ only if VendorAttributesAllowed=true ]
3573/// type-qualifier-list type-qualifier
3574/// [vendor] type-qualifier-list attributes
3575/// [ only if VendorAttributesAllowed=true ]
3576/// [C++0x] attribute-specifier[opt] is allowed before cv-qualifier-seq
3577/// [ only if CXX0XAttributesAllowed=true ]
3578/// Note: vendor can be GNU, MS, etc.
Reid Spencer5f016e22007-07-11 17:01:13 +00003579///
Dawn Perchik52fc3142010-09-03 01:29:35 +00003580void Parser::ParseTypeQualifierListOpt(DeclSpec &DS,
3581 bool VendorAttributesAllowed,
Richard Smithc56298d2012-04-10 03:25:07 +00003582 bool CXX11AttributesAllowed) {
3583 if (getLangOpts().CPlusPlus0x && CXX11AttributesAllowed &&
Richard Smith6ee326a2012-04-10 01:32:12 +00003584 isCXX11AttributeSpecifier()) {
John McCall0b7e6782011-03-24 11:26:52 +00003585 ParsedAttributesWithRange attrs(AttrFactory);
Richard Smithc56298d2012-04-10 03:25:07 +00003586 ParseCXX11Attributes(attrs);
Richard Smith6ee326a2012-04-10 01:32:12 +00003587 DS.takeAttributesFrom(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00003588 }
Abramo Bagnara796aa442011-03-12 11:17:06 +00003589
3590 SourceLocation EndLoc;
3591
Reid Spencer5f016e22007-07-11 17:01:13 +00003592 while (1) {
John McCallfec54012009-08-03 20:12:06 +00003593 bool isInvalid = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00003594 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00003595 unsigned DiagID = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00003596 SourceLocation Loc = Tok.getLocation();
3597
3598 switch (Tok.getKind()) {
Douglas Gregor1a480c42010-08-27 17:35:51 +00003599 case tok::code_completion:
3600 Actions.CodeCompleteTypeQualifiers(DS);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00003601 return cutOffParsing();
Douglas Gregor1a480c42010-08-27 17:35:51 +00003602
Reid Spencer5f016e22007-07-11 17:01:13 +00003603 case tok::kw_const:
John McCallfec54012009-08-03 20:12:06 +00003604 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, DiagID,
David Blaikie4e4d0842012-03-11 07:00:24 +00003605 getLangOpts());
Reid Spencer5f016e22007-07-11 17:01:13 +00003606 break;
3607 case tok::kw_volatile:
John McCallfec54012009-08-03 20:12:06 +00003608 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
David Blaikie4e4d0842012-03-11 07:00:24 +00003609 getLangOpts());
Reid Spencer5f016e22007-07-11 17:01:13 +00003610 break;
3611 case tok::kw_restrict:
John McCallfec54012009-08-03 20:12:06 +00003612 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
David Blaikie4e4d0842012-03-11 07:00:24 +00003613 getLangOpts());
Reid Spencer5f016e22007-07-11 17:01:13 +00003614 break;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003615
3616 // OpenCL qualifiers:
3617 case tok::kw_private:
David Blaikie4e4d0842012-03-11 07:00:24 +00003618 if (!getLangOpts().OpenCL)
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003619 goto DoneWithTypeQuals;
3620 case tok::kw___private:
3621 case tok::kw___global:
3622 case tok::kw___local:
3623 case tok::kw___constant:
3624 case tok::kw___read_only:
3625 case tok::kw___write_only:
3626 case tok::kw___read_write:
3627 ParseOpenCLQualifiers(DS);
3628 break;
3629
Eli Friedman290eeb02009-06-08 23:27:34 +00003630 case tok::kw___w64:
Steve Naroff86bc6cf2008-12-25 14:41:26 +00003631 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00003632 case tok::kw___ptr32:
Steve Naroff239f0732008-12-25 14:16:32 +00003633 case tok::kw___cdecl:
3634 case tok::kw___stdcall:
3635 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00003636 case tok::kw___thiscall:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00003637 case tok::kw___unaligned:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003638 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00003639 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman290eeb02009-06-08 23:27:34 +00003640 continue;
3641 }
3642 goto DoneWithTypeQuals;
Dawn Perchik52fc3142010-09-03 01:29:35 +00003643 case tok::kw___pascal:
3644 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00003645 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik52fc3142010-09-03 01:29:35 +00003646 continue;
3647 }
3648 goto DoneWithTypeQuals;
Reid Spencer5f016e22007-07-11 17:01:13 +00003649 case tok::kw___attribute:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003650 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00003651 ParseGNUAttributes(DS.getAttributes());
Chris Lattner5a69d1c2008-12-18 07:02:59 +00003652 continue; // do *not* consume the next token!
3653 }
3654 // otherwise, FALL THROUGH!
3655 default:
Steve Naroff239f0732008-12-25 14:16:32 +00003656 DoneWithTypeQuals:
Chris Lattner5a69d1c2008-12-18 07:02:59 +00003657 // If this is not a type-qualifier token, we're done reading type
3658 // qualifiers. First verify that DeclSpec's are consistent.
Douglas Gregor9b3064b2009-04-01 22:41:11 +00003659 DS.Finish(Diags, PP);
Abramo Bagnara796aa442011-03-12 11:17:06 +00003660 if (EndLoc.isValid())
3661 DS.SetRangeEnd(EndLoc);
Chris Lattner5a69d1c2008-12-18 07:02:59 +00003662 return;
Reid Spencer5f016e22007-07-11 17:01:13 +00003663 }
Chris Lattnera1fcbad2008-12-18 06:50:14 +00003664
Reid Spencer5f016e22007-07-11 17:01:13 +00003665 // If the specifier combination wasn't legal, issue a diagnostic.
3666 if (isInvalid) {
3667 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner1ab3b962008-11-18 07:48:38 +00003668 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00003669 }
Abramo Bagnara796aa442011-03-12 11:17:06 +00003670 EndLoc = ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00003671 }
3672}
3673
3674
3675/// ParseDeclarator - Parse and verify a newly-initialized declarator.
3676///
3677void Parser::ParseDeclarator(Declarator &D) {
3678 /// This implements the 'declarator' production in the C grammar, then checks
3679 /// for well-formedness and issues diagnostics.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003680 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Reid Spencer5f016e22007-07-11 17:01:13 +00003681}
3682
Richard Smith9988f282012-03-29 01:16:42 +00003683static bool isPtrOperatorToken(tok::TokenKind Kind, const LangOptions &Lang) {
3684 if (Kind == tok::star || Kind == tok::caret)
3685 return true;
3686
3687 // We parse rvalue refs in C++03, because otherwise the errors are scary.
3688 if (!Lang.CPlusPlus)
3689 return false;
3690
3691 return Kind == tok::amp || Kind == tok::ampamp;
3692}
3693
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003694/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
3695/// is parsed by the function passed to it. Pass null, and the direct-declarator
3696/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003697/// ptr-operator production.
3698///
Richard Smith0706df42011-10-19 21:33:05 +00003699/// If the grammar of this construct is extended, matching changes must also be
Richard Smith5d8388c2012-03-27 01:42:32 +00003700/// made to TryParseDeclarator and MightBeDeclarator, and possibly to
3701/// isConstructorDeclarator.
Richard Smith0706df42011-10-19 21:33:05 +00003702///
Sebastian Redlf30208a2009-01-24 21:16:55 +00003703/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
3704/// [C] pointer[opt] direct-declarator
3705/// [C++] direct-declarator
3706/// [C++] ptr-operator declarator
Reid Spencer5f016e22007-07-11 17:01:13 +00003707///
3708/// pointer: [C99 6.7.5]
3709/// '*' type-qualifier-list[opt]
3710/// '*' type-qualifier-list[opt] pointer
3711///
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003712/// ptr-operator:
3713/// '*' cv-qualifier-seq[opt]
3714/// '&'
Sebastian Redl05532f22009-03-15 22:02:01 +00003715/// [C++0x] '&&'
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003716/// [GNU] '&' restrict[opt] attributes[opt]
Sebastian Redl05532f22009-03-15 22:02:01 +00003717/// [GNU?] '&&' restrict[opt] attributes[opt]
Sebastian Redlf30208a2009-01-24 21:16:55 +00003718/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003719void Parser::ParseDeclaratorInternal(Declarator &D,
3720 DirectDeclParseFunction DirectDeclParser) {
Douglas Gregor91a28862009-08-26 14:27:30 +00003721 if (Diags.hasAllExtensionsSilenced())
3722 D.setExtension();
Douglas Gregor2ccccb32010-08-23 18:23:48 +00003723
Sebastian Redlf30208a2009-01-24 21:16:55 +00003724 // C++ member pointers start with a '::' or a nested-name.
3725 // Member pointers get special handling, since there's no place for the
3726 // scope spec in the generic path below.
David Blaikie4e4d0842012-03-11 07:00:24 +00003727 if (getLangOpts().CPlusPlus &&
Chris Lattnerf919bfe2009-03-24 17:04:48 +00003728 (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) ||
3729 Tok.is(tok::annot_cxxscope))) {
Douglas Gregorefaa93a2011-11-07 17:33:42 +00003730 bool EnteringContext = D.getContext() == Declarator::FileContext ||
3731 D.getContext() == Declarator::MemberContext;
Sebastian Redlf30208a2009-01-24 21:16:55 +00003732 CXXScopeSpec SS;
Douglas Gregorefaa93a2011-11-07 17:33:42 +00003733 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), EnteringContext);
John McCall9ba61662010-02-26 08:45:28 +00003734
Jeffrey Yasskinedc28772010-04-07 23:29:58 +00003735 if (SS.isNotEmpty()) {
Mike Stump1eb44332009-09-09 15:08:12 +00003736 if (Tok.isNot(tok::star)) {
Sebastian Redlf30208a2009-01-24 21:16:55 +00003737 // The scope spec really belongs to the direct-declarator.
3738 D.getCXXScopeSpec() = SS;
3739 if (DirectDeclParser)
3740 (this->*DirectDeclParser)(D);
3741 return;
3742 }
3743
3744 SourceLocation Loc = ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00003745 D.SetRangeEnd(Loc);
John McCall0b7e6782011-03-24 11:26:52 +00003746 DeclSpec DS(AttrFactory);
Sebastian Redlf30208a2009-01-24 21:16:55 +00003747 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00003748 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00003749
3750 // Recurse to parse whatever is left.
3751 ParseDeclaratorInternal(D, DirectDeclParser);
3752
3753 // Sema will have to catch (syntactically invalid) pointers into global
3754 // scope. It has to catch pointers into namespace scope anyway.
3755 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
John McCall0b7e6782011-03-24 11:26:52 +00003756 Loc),
3757 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003758 /* Don't replace range end. */SourceLocation());
Sebastian Redlf30208a2009-01-24 21:16:55 +00003759 return;
3760 }
3761 }
3762
3763 tok::TokenKind Kind = Tok.getKind();
Steve Naroff5618bd42008-08-27 16:04:49 +00003764 // Not a pointer, C++ reference, or block.
Richard Smith9988f282012-03-29 01:16:42 +00003765 if (!isPtrOperatorToken(Kind, getLangOpts())) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003766 if (DirectDeclParser)
3767 (this->*DirectDeclParser)(D);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003768 return;
3769 }
Sebastian Redlf30208a2009-01-24 21:16:55 +00003770
Sebastian Redl05532f22009-03-15 22:02:01 +00003771 // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference,
3772 // '&&' -> rvalue reference
Sebastian Redl743de1f2009-03-23 00:00:23 +00003773 SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&.
Sebastian Redlab197ba2009-02-09 18:23:29 +00003774 D.SetRangeEnd(Loc);
Reid Spencer5f016e22007-07-11 17:01:13 +00003775
Chris Lattner9af55002009-03-27 04:18:06 +00003776 if (Kind == tok::star || Kind == tok::caret) {
Chris Lattner76549142008-02-21 01:32:26 +00003777 // Is a pointer.
John McCall0b7e6782011-03-24 11:26:52 +00003778 DeclSpec DS(AttrFactory);
Sebastian Redlf30208a2009-01-24 21:16:55 +00003779
Richard Smith6ee326a2012-04-10 01:32:12 +00003780 // FIXME: GNU attributes are not allowed here in a new-type-id.
Reid Spencer5f016e22007-07-11 17:01:13 +00003781 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00003782 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00003783
Reid Spencer5f016e22007-07-11 17:01:13 +00003784 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003785 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroff5618bd42008-08-27 16:04:49 +00003786 if (Kind == tok::star)
3787 // Remember that we parsed a pointer type, and remember the type-quals.
3788 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
Chandler Carruthd067c072011-02-23 18:51:59 +00003789 DS.getConstSpecLoc(),
3790 DS.getVolatileSpecLoc(),
John McCall0b7e6782011-03-24 11:26:52 +00003791 DS.getRestrictSpecLoc()),
3792 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003793 SourceLocation());
Steve Naroff5618bd42008-08-27 16:04:49 +00003794 else
3795 // Remember that we parsed a Block type, and remember the type-quals.
Mike Stump1eb44332009-09-09 15:08:12 +00003796 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
John McCall0b7e6782011-03-24 11:26:52 +00003797 Loc),
3798 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003799 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00003800 } else {
3801 // Is a reference
John McCall0b7e6782011-03-24 11:26:52 +00003802 DeclSpec DS(AttrFactory);
Reid Spencer5f016e22007-07-11 17:01:13 +00003803
Sebastian Redl743de1f2009-03-23 00:00:23 +00003804 // Complain about rvalue references in C++03, but then go on and build
3805 // the declarator.
Richard Smith7fe62082011-10-15 05:09:34 +00003806 if (Kind == tok::ampamp)
David Blaikie4e4d0842012-03-11 07:00:24 +00003807 Diag(Loc, getLangOpts().CPlusPlus0x ?
Richard Smith7fe62082011-10-15 05:09:34 +00003808 diag::warn_cxx98_compat_rvalue_reference :
3809 diag::ext_rvalue_reference);
Sebastian Redl743de1f2009-03-23 00:00:23 +00003810
Richard Smith6ee326a2012-04-10 01:32:12 +00003811 // GNU-style and C++11 attributes are allowed here, as is restrict.
3812 ParseTypeQualifierListOpt(DS);
3813 D.ExtendWithDeclSpec(DS);
3814
Reid Spencer5f016e22007-07-11 17:01:13 +00003815 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
3816 // cv-qualifiers are introduced through the use of a typedef or of a
3817 // template type argument, in which case the cv-qualifiers are ignored.
Reid Spencer5f016e22007-07-11 17:01:13 +00003818 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
3819 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
3820 Diag(DS.getConstSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00003821 diag::err_invalid_reference_qualifier_application) << "const";
Reid Spencer5f016e22007-07-11 17:01:13 +00003822 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
3823 Diag(DS.getVolatileSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00003824 diag::err_invalid_reference_qualifier_application) << "volatile";
Reid Spencer5f016e22007-07-11 17:01:13 +00003825 }
3826
3827 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003828 ParseDeclaratorInternal(D, DirectDeclParser);
Reid Spencer5f016e22007-07-11 17:01:13 +00003829
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00003830 if (D.getNumTypeObjects() > 0) {
3831 // C++ [dcl.ref]p4: There shall be no references to references.
3832 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
3833 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattnerda83bac2008-11-19 07:37:42 +00003834 if (const IdentifierInfo *II = D.getIdentifier())
3835 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
3836 << II;
3837 else
3838 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
3839 << "type name";
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00003840
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003841 // Once we've complained about the reference-to-reference, we
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00003842 // can go ahead and build the (technically ill-formed)
3843 // declarator: reference collapsing will take care of it.
3844 }
3845 }
3846
Reid Spencer5f016e22007-07-11 17:01:13 +00003847 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner76549142008-02-21 01:32:26 +00003848 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
Sebastian Redl05532f22009-03-15 22:02:01 +00003849 Kind == tok::amp),
John McCall0b7e6782011-03-24 11:26:52 +00003850 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003851 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00003852 }
3853}
3854
Richard Smith9988f282012-03-29 01:16:42 +00003855static void diagnoseMisplacedEllipsis(Parser &P, Declarator &D,
3856 SourceLocation EllipsisLoc) {
3857 if (EllipsisLoc.isValid()) {
3858 FixItHint Insertion;
3859 if (!D.getEllipsisLoc().isValid()) {
3860 Insertion = FixItHint::CreateInsertion(D.getIdentifierLoc(), "...");
3861 D.setEllipsisLoc(EllipsisLoc);
3862 }
3863 P.Diag(EllipsisLoc, diag::err_misplaced_ellipsis_in_declaration)
3864 << FixItHint::CreateRemoval(EllipsisLoc) << Insertion << !D.hasName();
3865 }
3866}
3867
Reid Spencer5f016e22007-07-11 17:01:13 +00003868/// ParseDirectDeclarator
3869/// direct-declarator: [C99 6.7.5]
Douglas Gregor42a552f2008-11-05 20:51:48 +00003870/// [C99] identifier
Reid Spencer5f016e22007-07-11 17:01:13 +00003871/// '(' declarator ')'
3872/// [GNU] '(' attributes declarator ')'
3873/// [C90] direct-declarator '[' constant-expression[opt] ']'
3874/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
3875/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
3876/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
3877/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Richard Smith6ee326a2012-04-10 01:32:12 +00003878/// [C++11] direct-declarator '[' constant-expression[opt] ']'
3879/// attribute-specifier-seq[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00003880/// direct-declarator '(' parameter-type-list ')'
3881/// direct-declarator '(' identifier-list[opt] ')'
3882/// [GNU] direct-declarator '(' parameter-forward-declarations
3883/// parameter-type-list[opt] ')'
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003884/// [C++] direct-declarator '(' parameter-declaration-clause ')'
3885/// cv-qualifier-seq[opt] exception-specification[opt]
Richard Smith6ee326a2012-04-10 01:32:12 +00003886/// [C++11] direct-declarator '(' parameter-declaration-clause ')'
3887/// attribute-specifier-seq[opt] cv-qualifier-seq[opt]
3888/// ref-qualifier[opt] exception-specification[opt]
Douglas Gregorb48fe382008-10-31 09:07:45 +00003889/// [C++] declarator-id
Richard Smith6ee326a2012-04-10 01:32:12 +00003890/// [C++11] declarator-id attribute-specifier-seq[opt]
Douglas Gregor42a552f2008-11-05 20:51:48 +00003891///
3892/// declarator-id: [C++ 8]
Douglas Gregora8bc8c92010-12-23 22:44:42 +00003893/// '...'[opt] id-expression
Douglas Gregor42a552f2008-11-05 20:51:48 +00003894/// '::'[opt] nested-name-specifier[opt] type-name
3895///
3896/// id-expression: [C++ 5.1]
3897/// unqualified-id
Douglas Gregordb422df2009-09-25 21:45:23 +00003898/// qualified-id
Douglas Gregor42a552f2008-11-05 20:51:48 +00003899///
3900/// unqualified-id: [C++ 5.1]
Mike Stump1eb44332009-09-09 15:08:12 +00003901/// identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003902/// operator-function-id
Douglas Gregordb422df2009-09-25 21:45:23 +00003903/// conversion-function-id
Mike Stump1eb44332009-09-09 15:08:12 +00003904/// '~' class-name
Douglas Gregor39a8de12009-02-25 19:37:18 +00003905/// template-id
Argyrios Kyrtzidisc7ed9c62008-11-07 22:02:30 +00003906///
Richard Smith5d8388c2012-03-27 01:42:32 +00003907/// Note, any additional constructs added here may need corresponding changes
3908/// in isConstructorDeclarator.
Reid Spencer5f016e22007-07-11 17:01:13 +00003909void Parser::ParseDirectDeclarator(Declarator &D) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00003910 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003911
David Blaikie4e4d0842012-03-11 07:00:24 +00003912 if (getLangOpts().CPlusPlus && D.mayHaveIdentifier()) {
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003913 // ParseDeclaratorInternal might already have parsed the scope.
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00003914 if (D.getCXXScopeSpec().isEmpty()) {
Douglas Gregorefaa93a2011-11-07 17:33:42 +00003915 bool EnteringContext = D.getContext() == Declarator::FileContext ||
3916 D.getContext() == Declarator::MemberContext;
3917 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec(), ParsedType(),
3918 EnteringContext);
John McCall9ba61662010-02-26 08:45:28 +00003919 }
3920
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00003921 if (D.getCXXScopeSpec().isValid()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00003922 if (Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
John McCalle7e278b2009-12-11 20:04:54 +00003923 // Change the declaration context for name lookup, until this function
3924 // is exited (and the declarator has been parsed).
3925 DeclScopeObj.EnterDeclaratorScope();
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00003926 }
3927
Douglas Gregora8bc8c92010-12-23 22:44:42 +00003928 // C++0x [dcl.fct]p14:
3929 // There is a syntactic ambiguity when an ellipsis occurs at the end
3930 // of a parameter-declaration-clause without a preceding comma. In
3931 // this case, the ellipsis is parsed as part of the
3932 // abstract-declarator if the type of the parameter names a template
3933 // parameter pack that has not been expanded; otherwise, it is parsed
3934 // as part of the parameter-declaration-clause.
Richard Smith9988f282012-03-29 01:16:42 +00003935 if (Tok.is(tok::ellipsis) && D.getCXXScopeSpec().isEmpty() &&
Douglas Gregora8bc8c92010-12-23 22:44:42 +00003936 !((D.getContext() == Declarator::PrototypeContext ||
3937 D.getContext() == Declarator::BlockLiteralContext) &&
Douglas Gregora8bc8c92010-12-23 22:44:42 +00003938 NextToken().is(tok::r_paren) &&
Richard Smith9988f282012-03-29 01:16:42 +00003939 !Actions.containsUnexpandedParameterPacks(D))) {
3940 SourceLocation EllipsisLoc = ConsumeToken();
3941 if (isPtrOperatorToken(Tok.getKind(), getLangOpts())) {
3942 // The ellipsis was put in the wrong place. Recover, and explain to
3943 // the user what they should have done.
3944 ParseDeclarator(D);
3945 diagnoseMisplacedEllipsis(*this, D, EllipsisLoc);
3946 return;
3947 } else
3948 D.setEllipsisLoc(EllipsisLoc);
3949
3950 // The ellipsis can't be followed by a parenthesized declarator. We
3951 // check for that in ParseParenDeclarator, after we have disambiguated
3952 // the l_paren token.
3953 }
3954
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003955 if (Tok.is(tok::identifier) || Tok.is(tok::kw_operator) ||
3956 Tok.is(tok::annot_template_id) || Tok.is(tok::tilde)) {
3957 // We found something that indicates the start of an unqualified-id.
3958 // Parse that unqualified-id.
John McCallba9d8532010-04-13 06:39:49 +00003959 bool AllowConstructorName;
3960 if (D.getDeclSpec().hasTypeSpecifier())
3961 AllowConstructorName = false;
3962 else if (D.getCXXScopeSpec().isSet())
3963 AllowConstructorName =
3964 (D.getContext() == Declarator::FileContext ||
3965 (D.getContext() == Declarator::MemberContext &&
3966 D.getDeclSpec().isFriendSpecified()));
3967 else
3968 AllowConstructorName = (D.getContext() == Declarator::MemberContext);
3969
Abramo Bagnarae4b92762012-01-27 09:46:47 +00003970 SourceLocation TemplateKWLoc;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003971 if (ParseUnqualifiedId(D.getCXXScopeSpec(),
3972 /*EnteringContext=*/true,
3973 /*AllowDestructorName=*/true,
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003974 AllowConstructorName,
John McCallb3d87482010-08-24 05:47:05 +00003975 ParsedType(),
Abramo Bagnarae4b92762012-01-27 09:46:47 +00003976 TemplateKWLoc,
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00003977 D.getName()) ||
3978 // Once we're past the identifier, if the scope was bad, mark the
3979 // whole declarator bad.
3980 D.getCXXScopeSpec().isInvalid()) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00003981 D.SetIdentifier(0, Tok.getLocation());
3982 D.setInvalidType(true);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003983 } else {
3984 // Parsed the unqualified-id; update range information and move along.
3985 if (D.getSourceRange().getBegin().isInvalid())
3986 D.SetRangeBegin(D.getName().getSourceRange().getBegin());
3987 D.SetRangeEnd(D.getName().getSourceRange().getEnd());
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003988 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003989 goto PastIdentifier;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00003990 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003991 } else if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
David Blaikie4e4d0842012-03-11 07:00:24 +00003992 assert(!getLangOpts().CPlusPlus &&
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00003993 "There's a C++-specific check for tok::identifier above");
3994 assert(Tok.getIdentifierInfo() && "Not an identifier?");
3995 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
3996 ConsumeToken();
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003997 goto PastIdentifier;
3998 }
Richard Smith9988f282012-03-29 01:16:42 +00003999
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004000 if (Tok.is(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00004001 // direct-declarator: '(' declarator ')'
4002 // direct-declarator: '(' attributes declarator ')'
4003 // Example: 'char (*X)' or 'int (*XX)(void)'
4004 ParseParenDeclarator(D);
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004005
4006 // If the declarator was parenthesized, we entered the declarator
4007 // scope when parsing the parenthesized declarator, then exited
4008 // the scope already. Re-enter the scope, if we need to.
4009 if (D.getCXXScopeSpec().isSet()) {
Fariborz Jahanian46877cd2010-08-17 23:50:37 +00004010 // If there was an error parsing parenthesized declarator, declarator
Richard Smith9988f282012-03-29 01:16:42 +00004011 // scope may have been entered before. Don't do it again.
Fariborz Jahanian46877cd2010-08-17 23:50:37 +00004012 if (!D.isInvalidType() &&
4013 Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004014 // Change the declaration context for name lookup, until this function
4015 // is exited (and the declarator has been parsed).
Fariborz Jahanian46877cd2010-08-17 23:50:37 +00004016 DeclScopeObj.EnterDeclaratorScope();
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004017 }
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00004018 } else if (D.mayOmitIdentifier()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00004019 // This could be something simple like "int" (in which case the declarator
4020 // portion is empty), if an abstract-declarator is allowed.
4021 D.SetIdentifier(0, Tok.getLocation());
4022 } else {
Douglas Gregore950d4b2009-03-06 23:28:18 +00004023 if (D.getContext() == Declarator::MemberContext)
4024 Diag(Tok, diag::err_expected_member_name_or_semi)
4025 << D.getDeclSpec().getSourceRange();
David Blaikie4e4d0842012-03-11 07:00:24 +00004026 else if (getLangOpts().CPlusPlus)
4027 Diag(Tok, diag::err_expected_unqualified_id) << getLangOpts().CPlusPlus;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00004028 else
Chris Lattner1ab3b962008-11-18 07:48:38 +00004029 Diag(Tok, diag::err_expected_ident_lparen);
Reid Spencer5f016e22007-07-11 17:01:13 +00004030 D.SetIdentifier(0, Tok.getLocation());
Chris Lattner1f6f54b2008-11-11 06:13:16 +00004031 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00004032 }
Mike Stump1eb44332009-09-09 15:08:12 +00004033
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00004034 PastIdentifier:
Reid Spencer5f016e22007-07-11 17:01:13 +00004035 assert(D.isPastIdentifier() &&
4036 "Haven't past the location of the identifier yet?");
Mike Stump1eb44332009-09-09 15:08:12 +00004037
Richard Smith6ee326a2012-04-10 01:32:12 +00004038 // Don't parse attributes unless we have parsed an unparenthesized name.
4039 if (D.hasName() && !D.getNumTypeObjects())
John McCall7f040a92010-12-24 02:08:15 +00004040 MaybeParseCXX0XAttributes(D);
Sean Huntbbd37c62009-11-21 08:43:09 +00004041
Reid Spencer5f016e22007-07-11 17:01:13 +00004042 while (1) {
Chris Lattner04d66662007-10-09 17:33:22 +00004043 if (Tok.is(tok::l_paren)) {
David Blaikie42d6d0c2011-12-04 05:04:18 +00004044 // Enter function-declaration scope, limiting any declarators to the
4045 // function prototype scope, including parameter declarators.
4046 ParseScope PrototypeScope(this,
4047 Scope::FunctionPrototypeScope|Scope::DeclScope);
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00004048 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
4049 // In such a case, check if we actually have a function declarator; if it
4050 // is not, the declarator has been fully parsed.
David Blaikie4e4d0842012-03-11 07:00:24 +00004051 if (getLangOpts().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
Chris Lattner7399ee02008-10-20 02:05:46 +00004052 // When not in file scope, warn for ambiguous function declarators, just
4053 // in case the author intended it as a variable definition.
4054 bool warnIfAmbiguous = D.getContext() != Declarator::FileContext;
4055 if (!isCXXFunctionDeclarator(warnIfAmbiguous))
4056 break;
4057 }
John McCall0b7e6782011-03-24 11:26:52 +00004058 ParsedAttributes attrs(AttrFactory);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004059 BalancedDelimiterTracker T(*this, tok::l_paren);
4060 T.consumeOpen();
4061 ParseFunctionDeclarator(D, attrs, T);
David Blaikie42d6d0c2011-12-04 05:04:18 +00004062 PrototypeScope.Exit();
Chris Lattner04d66662007-10-09 17:33:22 +00004063 } else if (Tok.is(tok::l_square)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00004064 ParseBracketDeclarator(D);
4065 } else {
4066 break;
4067 }
4068 }
David Blaikie42d6d0c2011-12-04 05:04:18 +00004069}
Reid Spencer5f016e22007-07-11 17:01:13 +00004070
Chris Lattneref4715c2008-04-06 05:45:57 +00004071/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
4072/// only called before the identifier, so these are most likely just grouping
Mike Stump1eb44332009-09-09 15:08:12 +00004073/// parens for precedence. If we find that these are actually function
Chris Lattneref4715c2008-04-06 05:45:57 +00004074/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
4075///
4076/// direct-declarator:
4077/// '(' declarator ')'
4078/// [GNU] '(' attributes declarator ')'
Chris Lattner7399ee02008-10-20 02:05:46 +00004079/// direct-declarator '(' parameter-type-list ')'
4080/// direct-declarator '(' identifier-list[opt] ')'
4081/// [GNU] direct-declarator '(' parameter-forward-declarations
4082/// parameter-type-list[opt] ')'
Chris Lattneref4715c2008-04-06 05:45:57 +00004083///
4084void Parser::ParseParenDeclarator(Declarator &D) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004085 BalancedDelimiterTracker T(*this, tok::l_paren);
4086 T.consumeOpen();
4087
Chris Lattneref4715c2008-04-06 05:45:57 +00004088 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
Mike Stump1eb44332009-09-09 15:08:12 +00004089
Chris Lattner7399ee02008-10-20 02:05:46 +00004090 // Eat any attributes before we look at whether this is a grouping or function
4091 // declarator paren. If this is a grouping paren, the attribute applies to
4092 // the type being built up, for example:
4093 // int (__attribute__(()) *x)(long y)
4094 // If this ends up not being a grouping paren, the attribute applies to the
4095 // first argument, for example:
4096 // int (__attribute__(()) int x)
4097 // In either case, we need to eat any attributes to be able to determine what
4098 // sort of paren this is.
4099 //
John McCall0b7e6782011-03-24 11:26:52 +00004100 ParsedAttributes attrs(AttrFactory);
Chris Lattner7399ee02008-10-20 02:05:46 +00004101 bool RequiresArg = false;
4102 if (Tok.is(tok::kw___attribute)) {
John McCall7f040a92010-12-24 02:08:15 +00004103 ParseGNUAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00004104
Chris Lattner7399ee02008-10-20 02:05:46 +00004105 // We require that the argument list (if this is a non-grouping paren) be
4106 // present even if the attribute list was empty.
4107 RequiresArg = true;
4108 }
Steve Naroff239f0732008-12-25 14:16:32 +00004109 // Eat any Microsoft extensions.
Eli Friedman290eeb02009-06-08 23:27:34 +00004110 if (Tok.is(tok::kw___cdecl) || Tok.is(tok::kw___stdcall) ||
Douglas Gregorf813a2c2010-05-18 16:57:00 +00004111 Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___fastcall) ||
Francois Pichet3bd9aa42011-08-18 09:59:55 +00004112 Tok.is(tok::kw___w64) || Tok.is(tok::kw___ptr64) ||
Francois Pichet58fd97a2011-08-25 00:36:46 +00004113 Tok.is(tok::kw___ptr32) || Tok.is(tok::kw___unaligned)) {
John McCall7f040a92010-12-24 02:08:15 +00004114 ParseMicrosoftTypeAttributes(attrs);
Eli Friedman290eeb02009-06-08 23:27:34 +00004115 }
Dawn Perchik52fc3142010-09-03 01:29:35 +00004116 // Eat any Borland extensions.
Ted Kremenek8113ecf2010-11-10 05:59:39 +00004117 if (Tok.is(tok::kw___pascal))
John McCall7f040a92010-12-24 02:08:15 +00004118 ParseBorlandTypeAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00004119
Chris Lattneref4715c2008-04-06 05:45:57 +00004120 // If we haven't past the identifier yet (or where the identifier would be
4121 // stored, if this is an abstract declarator), then this is probably just
4122 // grouping parens. However, if this could be an abstract-declarator, then
4123 // this could also be the start of function arguments (consider 'void()').
4124 bool isGrouping;
Mike Stump1eb44332009-09-09 15:08:12 +00004125
Chris Lattneref4715c2008-04-06 05:45:57 +00004126 if (!D.mayOmitIdentifier()) {
4127 // If this can't be an abstract-declarator, this *must* be a grouping
4128 // paren, because we haven't seen the identifier yet.
4129 isGrouping = true;
4130 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Richard Smith22592862012-03-27 23:05:05 +00004131 (getLangOpts().CPlusPlus && Tok.is(tok::ellipsis) &&
4132 NextToken().is(tok::r_paren)) || // C++ int(...)
Richard Smith6ce48a72012-04-11 04:01:28 +00004133 isDeclarationSpecifier() || // 'int(int)' is a function.
4134 isCXX11AttributeSpecifier()) { // 'int([[]]int)' is a function.
Chris Lattneref4715c2008-04-06 05:45:57 +00004135 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
4136 // considered to be a type, not a K&R identifier-list.
4137 isGrouping = false;
4138 } else {
4139 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
4140 isGrouping = true;
4141 }
Mike Stump1eb44332009-09-09 15:08:12 +00004142
Chris Lattneref4715c2008-04-06 05:45:57 +00004143 // If this is a grouping paren, handle:
4144 // direct-declarator: '(' declarator ')'
4145 // direct-declarator: '(' attributes declarator ')'
4146 if (isGrouping) {
Richard Smith9988f282012-03-29 01:16:42 +00004147 SourceLocation EllipsisLoc = D.getEllipsisLoc();
4148 D.setEllipsisLoc(SourceLocation());
4149
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00004150 bool hadGroupingParens = D.hasGroupingParens();
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00004151 D.setGroupingParens(true);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00004152 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattneref4715c2008-04-06 05:45:57 +00004153 // Match the ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004154 T.consumeClose();
4155 D.AddTypeInfo(DeclaratorChunk::getParen(T.getOpenLocation(),
4156 T.getCloseLocation()),
4157 attrs, T.getCloseLocation());
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00004158
4159 D.setGroupingParens(hadGroupingParens);
Richard Smith9988f282012-03-29 01:16:42 +00004160
4161 // An ellipsis cannot be placed outside parentheses.
4162 if (EllipsisLoc.isValid())
4163 diagnoseMisplacedEllipsis(*this, D, EllipsisLoc);
4164
Chris Lattneref4715c2008-04-06 05:45:57 +00004165 return;
4166 }
Mike Stump1eb44332009-09-09 15:08:12 +00004167
Chris Lattneref4715c2008-04-06 05:45:57 +00004168 // Okay, if this wasn't a grouping paren, it must be the start of a function
4169 // argument list. Recognize that this declarator will never have an
Chris Lattner7399ee02008-10-20 02:05:46 +00004170 // identifier (and remember where it would have been), then call into
4171 // ParseFunctionDeclarator to handle of argument list.
Chris Lattneref4715c2008-04-06 05:45:57 +00004172 D.SetIdentifier(0, Tok.getLocation());
4173
David Blaikie42d6d0c2011-12-04 05:04:18 +00004174 // Enter function-declaration scope, limiting any declarators to the
4175 // function prototype scope, including parameter declarators.
4176 ParseScope PrototypeScope(this,
4177 Scope::FunctionPrototypeScope|Scope::DeclScope);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004178 ParseFunctionDeclarator(D, attrs, T, RequiresArg);
David Blaikie42d6d0c2011-12-04 05:04:18 +00004179 PrototypeScope.Exit();
Chris Lattneref4715c2008-04-06 05:45:57 +00004180}
4181
4182/// ParseFunctionDeclarator - We are after the identifier and have parsed the
4183/// declarator D up to a paren, which indicates that we are parsing function
4184/// arguments.
Reid Spencer5f016e22007-07-11 17:01:13 +00004185///
Richard Smith6ee326a2012-04-10 01:32:12 +00004186/// If FirstArgAttrs is non-null, then the caller parsed those arguments
4187/// immediately after the open paren - they should be considered to be the
4188/// first argument of a parameter.
Chris Lattner7399ee02008-10-20 02:05:46 +00004189///
Richard Smith6ee326a2012-04-10 01:32:12 +00004190/// If RequiresArg is true, then the first argument of the function is required
4191/// to be present and required to not be an identifier list.
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004192///
Richard Smith6ee326a2012-04-10 01:32:12 +00004193/// For C++, after the parameter-list, it also parses the cv-qualifier-seq[opt],
4194/// (C++11) ref-qualifier[opt], exception-specification[opt],
4195/// (C++11) attribute-specifier-seq[opt], and (C++11) trailing-return-type[opt].
4196///
4197/// [C++11] exception-specification:
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004198/// dynamic-exception-specification
4199/// noexcept-specification
4200///
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004201void Parser::ParseFunctionDeclarator(Declarator &D,
Richard Smith6ee326a2012-04-10 01:32:12 +00004202 ParsedAttributes &FirstArgAttrs,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004203 BalancedDelimiterTracker &Tracker,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004204 bool RequiresArg) {
David Blaikie42d6d0c2011-12-04 05:04:18 +00004205 assert(getCurScope()->isFunctionPrototypeScope() &&
4206 "Should call from a Function scope");
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004207 // lparen is already consumed!
4208 assert(D.isPastIdentifier() && "Should not call before identifier!");
4209
4210 // This should be true when the function has typed arguments.
4211 // Otherwise, it is treated as a K&R-style function.
4212 bool HasProto = false;
4213 // Build up an array of information about the parsed arguments.
Chris Lattner5f9e2722011-07-23 10:55:15 +00004214 SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004215 // Remember where we see an ellipsis, if any.
4216 SourceLocation EllipsisLoc;
4217
4218 DeclSpec DS(AttrFactory);
4219 bool RefQualifierIsLValueRef = true;
4220 SourceLocation RefQualifierLoc;
Douglas Gregor43f51032011-10-19 06:04:55 +00004221 SourceLocation ConstQualifierLoc;
4222 SourceLocation VolatileQualifierLoc;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004223 ExceptionSpecificationType ESpecType = EST_None;
4224 SourceRange ESpecRange;
Chris Lattner5f9e2722011-07-23 10:55:15 +00004225 SmallVector<ParsedType, 2> DynamicExceptions;
4226 SmallVector<SourceRange, 2> DynamicExceptionRanges;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004227 ExprResult NoexceptExpr;
Richard Smith6ee326a2012-04-10 01:32:12 +00004228 ParsedAttributes FnAttrs(AttrFactory);
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004229 ParsedType TrailingReturnType;
Richard Smith6ee326a2012-04-10 01:32:12 +00004230
James Molloy16f1f712012-02-29 10:24:19 +00004231 Actions.ActOnStartFunctionDeclarator();
4232
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004233 SourceLocation EndLoc;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004234 if (isFunctionDeclaratorIdentifierList()) {
4235 if (RequiresArg)
4236 Diag(Tok, diag::err_argument_required_after_attribute);
4237
4238 ParseFunctionDeclaratorIdentifierList(D, ParamInfo);
4239
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004240 Tracker.consumeClose();
4241 EndLoc = Tracker.getCloseLocation();
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004242 } else {
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004243 if (Tok.isNot(tok::r_paren))
Richard Smith6ee326a2012-04-10 01:32:12 +00004244 ParseParameterDeclarationClause(D, FirstArgAttrs, ParamInfo, EllipsisLoc);
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004245 else if (RequiresArg)
4246 Diag(Tok, diag::err_argument_required_after_attribute);
4247
David Blaikie4e4d0842012-03-11 07:00:24 +00004248 HasProto = ParamInfo.size() || getLangOpts().CPlusPlus;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004249
4250 // If we have the closing ')', eat it.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004251 Tracker.consumeClose();
4252 EndLoc = Tracker.getCloseLocation();
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004253
David Blaikie4e4d0842012-03-11 07:00:24 +00004254 if (getLangOpts().CPlusPlus) {
Richard Smith6ee326a2012-04-10 01:32:12 +00004255 // FIXME: Accept these components in any order, and produce fixits to
4256 // correct the order if the user gets it wrong. Ideally we should deal
4257 // with the virt-specifier-seq and pure-specifier in the same way.
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004258
4259 // Parse cv-qualifier-seq[opt].
Richard Smith6ee326a2012-04-10 01:32:12 +00004260 ParseTypeQualifierListOpt(DS, false /*no attributes*/, false);
4261 if (!DS.getSourceRange().getEnd().isInvalid()) {
4262 EndLoc = DS.getSourceRange().getEnd();
4263 ConstQualifierLoc = DS.getConstSpecLoc();
4264 VolatileQualifierLoc = DS.getVolatileSpecLoc();
4265 }
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004266
4267 // Parse ref-qualifier[opt].
4268 if (Tok.is(tok::amp) || Tok.is(tok::ampamp)) {
David Blaikie4e4d0842012-03-11 07:00:24 +00004269 Diag(Tok, getLangOpts().CPlusPlus0x ?
Richard Smith7fe62082011-10-15 05:09:34 +00004270 diag::warn_cxx98_compat_ref_qualifier :
4271 diag::ext_ref_qualifier);
Richard Smith6ee326a2012-04-10 01:32:12 +00004272
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004273 RefQualifierIsLValueRef = Tok.is(tok::amp);
4274 RefQualifierLoc = ConsumeToken();
4275 EndLoc = RefQualifierLoc;
4276 }
4277
Douglas Gregorcefc3af2012-04-16 07:05:22 +00004278 // C++11 [expr.prim.general]p3:
4279 // If a declaration declares a member function or member function
4280 // template of a class X, the expression this is a prvalue of type
4281 // "pointer to cv-qualifier-seq X" between the optional cv-qualifer-seq
4282 // and the end of the function-definition, member-declarator, or
4283 // declarator.
4284 bool IsCXX11MemberFunction =
4285 getLangOpts().CPlusPlus0x &&
4286 (D.getContext() == Declarator::MemberContext ||
4287 (D.getContext() == Declarator::FileContext &&
4288 D.getCXXScopeSpec().isValid() &&
4289 Actions.CurContext->isRecord()));
4290 Sema::CXXThisScopeRAII ThisScope(Actions,
4291 dyn_cast<CXXRecordDecl>(Actions.CurContext),
4292 DS.getTypeQualifiers(),
4293 IsCXX11MemberFunction);
Richard Smitha058fd42012-05-02 22:22:32 +00004294
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004295 // Parse exception-specification[opt].
Richard Smitha058fd42012-05-02 22:22:32 +00004296 ESpecType = tryParseExceptionSpecification(ESpecRange,
Douglas Gregor74e2fc32012-04-16 18:27:27 +00004297 DynamicExceptions,
4298 DynamicExceptionRanges,
Richard Smitha058fd42012-05-02 22:22:32 +00004299 NoexceptExpr);
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004300 if (ESpecType != EST_None)
4301 EndLoc = ESpecRange.getEnd();
4302
Richard Smith6ee326a2012-04-10 01:32:12 +00004303 // Parse attribute-specifier-seq[opt]. Per DR 979 and DR 1297, this goes
4304 // after the exception-specification.
4305 MaybeParseCXX0XAttributes(FnAttrs);
4306
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004307 // Parse trailing-return-type[opt].
David Blaikie4e4d0842012-03-11 07:00:24 +00004308 if (getLangOpts().CPlusPlus0x && Tok.is(tok::arrow)) {
Richard Smith7fe62082011-10-15 05:09:34 +00004309 Diag(Tok, diag::warn_cxx98_compat_trailing_return_type);
Douglas Gregorae7902c2011-08-04 15:30:47 +00004310 SourceRange Range;
4311 TrailingReturnType = ParseTrailingReturnType(Range).get();
4312 if (Range.getEnd().isValid())
4313 EndLoc = Range.getEnd();
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004314 }
4315 }
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004316 }
4317
4318 // Remember that we parsed a function type, and remember the attributes.
4319 D.AddTypeInfo(DeclaratorChunk::getFunction(HasProto,
4320 /*isVariadic=*/EllipsisLoc.isValid(),
4321 EllipsisLoc,
4322 ParamInfo.data(), ParamInfo.size(),
4323 DS.getTypeQualifiers(),
4324 RefQualifierIsLValueRef,
Douglas Gregor43f51032011-10-19 06:04:55 +00004325 RefQualifierLoc, ConstQualifierLoc,
4326 VolatileQualifierLoc,
Douglas Gregor90ebed02011-07-13 21:47:47 +00004327 /*MutableLoc=*/SourceLocation(),
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004328 ESpecType, ESpecRange.getBegin(),
4329 DynamicExceptions.data(),
4330 DynamicExceptionRanges.data(),
4331 DynamicExceptions.size(),
4332 NoexceptExpr.isUsable() ?
4333 NoexceptExpr.get() : 0,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004334 Tracker.getOpenLocation(),
4335 EndLoc, D,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004336 TrailingReturnType),
Richard Smith6ee326a2012-04-10 01:32:12 +00004337 FnAttrs, EndLoc);
James Molloy16f1f712012-02-29 10:24:19 +00004338
4339 Actions.ActOnEndFunctionDeclarator();
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004340}
4341
4342/// isFunctionDeclaratorIdentifierList - This parameter list may have an
4343/// identifier list form for a K&R-style function: void foo(a,b,c)
4344///
4345/// Note that identifier-lists are only allowed for normal declarators, not for
4346/// abstract-declarators.
4347bool Parser::isFunctionDeclaratorIdentifierList() {
David Blaikie4e4d0842012-03-11 07:00:24 +00004348 return !getLangOpts().CPlusPlus
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004349 && Tok.is(tok::identifier)
4350 && !TryAltiVecVectorToken()
4351 // K&R identifier lists can't have typedefs as identifiers, per C99
4352 // 6.7.5.3p11.
4353 && (TryAnnotateTypeOrScopeToken() || !Tok.is(tok::annot_typename))
4354 // Identifier lists follow a really simple grammar: the identifiers can
4355 // be followed *only* by a ", identifier" or ")". However, K&R
4356 // identifier lists are really rare in the brave new modern world, and
4357 // it is very common for someone to typo a type in a non-K&R style
4358 // list. If we are presented with something like: "void foo(intptr x,
4359 // float y)", we don't want to start parsing the function declarator as
4360 // though it is a K&R style declarator just because intptr is an
4361 // invalid type.
4362 //
4363 // To handle this, we check to see if the token after the first
4364 // identifier is a "," or ")". Only then do we parse it as an
4365 // identifier list.
4366 && (NextToken().is(tok::comma) || NextToken().is(tok::r_paren));
4367}
4368
4369/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
4370/// we found a K&R-style identifier list instead of a typed parameter list.
4371///
4372/// After returning, ParamInfo will hold the parsed parameters.
4373///
4374/// identifier-list: [C99 6.7.5]
4375/// identifier
4376/// identifier-list ',' identifier
4377///
4378void Parser::ParseFunctionDeclaratorIdentifierList(
4379 Declarator &D,
Chris Lattner5f9e2722011-07-23 10:55:15 +00004380 SmallVector<DeclaratorChunk::ParamInfo, 16> &ParamInfo) {
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004381 // If there was no identifier specified for the declarator, either we are in
4382 // an abstract-declarator, or we are in a parameter declarator which was found
4383 // to be abstract. In abstract-declarators, identifier lists are not valid:
4384 // diagnose this.
4385 if (!D.getIdentifier())
4386 Diag(Tok, diag::ext_ident_list_in_param);
4387
4388 // Maintain an efficient lookup of params we have seen so far.
4389 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
4390
4391 while (1) {
4392 // If this isn't an identifier, report the error and skip until ')'.
4393 if (Tok.isNot(tok::identifier)) {
4394 Diag(Tok, diag::err_expected_ident);
4395 SkipUntil(tok::r_paren, /*StopAtSemi=*/true, /*DontConsume=*/true);
4396 // Forget we parsed anything.
4397 ParamInfo.clear();
4398 return;
4399 }
4400
4401 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
4402
4403 // Reject 'typedef int y; int test(x, y)', but continue parsing.
4404 if (Actions.getTypeName(*ParmII, Tok.getLocation(), getCurScope()))
4405 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
4406
4407 // Verify that the argument identifier has not already been mentioned.
4408 if (!ParamsSoFar.insert(ParmII)) {
4409 Diag(Tok, diag::err_param_redefinition) << ParmII;
4410 } else {
4411 // Remember this identifier in ParamInfo.
4412 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
4413 Tok.getLocation(),
4414 0));
4415 }
4416
4417 // Eat the identifier.
4418 ConsumeToken();
4419
4420 // The list continues if we see a comma.
4421 if (Tok.isNot(tok::comma))
4422 break;
4423 ConsumeToken();
4424 }
4425}
4426
4427/// ParseParameterDeclarationClause - Parse a (possibly empty) parameter-list
4428/// after the opening parenthesis. This function will not parse a K&R-style
4429/// identifier list.
4430///
Richard Smith6ce48a72012-04-11 04:01:28 +00004431/// D is the declarator being parsed. If FirstArgAttrs is non-null, then the
4432/// caller parsed those arguments immediately after the open paren - they should
4433/// be considered to be part of the first parameter.
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004434///
4435/// After returning, ParamInfo will hold the parsed parameters. EllipsisLoc will
4436/// be the location of the ellipsis, if any was parsed.
4437///
Reid Spencer5f016e22007-07-11 17:01:13 +00004438/// parameter-type-list: [C99 6.7.5]
4439/// parameter-list
4440/// parameter-list ',' '...'
Douglas Gregored5d6512009-09-22 21:41:40 +00004441/// [C++] parameter-list '...'
Reid Spencer5f016e22007-07-11 17:01:13 +00004442///
4443/// parameter-list: [C99 6.7.5]
4444/// parameter-declaration
4445/// parameter-list ',' parameter-declaration
4446///
4447/// parameter-declaration: [C99 6.7.5]
4448/// declaration-specifiers declarator
Chris Lattner04421082008-04-08 04:40:51 +00004449/// [C++] declaration-specifiers declarator '=' assignment-expression
Sebastian Redl84407ba2012-03-14 15:54:00 +00004450/// [C++11] initializer-clause
Reid Spencer5f016e22007-07-11 17:01:13 +00004451/// [GNU] declaration-specifiers declarator attributes
Sebastian Redl50de12f2009-03-24 22:27:57 +00004452/// declaration-specifiers abstract-declarator[opt]
4453/// [C++] declaration-specifiers abstract-declarator[opt]
Chris Lattner8123a952008-04-10 02:22:51 +00004454/// '=' assignment-expression
Reid Spencer5f016e22007-07-11 17:01:13 +00004455/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
Richard Smith6ce48a72012-04-11 04:01:28 +00004456/// [C++11] attribute-specifier-seq parameter-declaration
Reid Spencer5f016e22007-07-11 17:01:13 +00004457///
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004458void Parser::ParseParameterDeclarationClause(
4459 Declarator &D,
Richard Smith6ce48a72012-04-11 04:01:28 +00004460 ParsedAttributes &FirstArgAttrs,
Chris Lattner5f9e2722011-07-23 10:55:15 +00004461 SmallVector<DeclaratorChunk::ParamInfo, 16> &ParamInfo,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004462 SourceLocation &EllipsisLoc) {
Mike Stump1eb44332009-09-09 15:08:12 +00004463
Chris Lattnerf97409f2008-04-06 06:57:35 +00004464 while (1) {
4465 if (Tok.is(tok::ellipsis)) {
Richard Smith6ce48a72012-04-11 04:01:28 +00004466 // FIXME: Issue a diagnostic if we parsed an attribute-specifier-seq
4467 // before deciding this was a parameter-declaration-clause.
Douglas Gregor965acbb2009-02-18 07:07:28 +00004468 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chris Lattnerf97409f2008-04-06 06:57:35 +00004469 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00004470 }
Mike Stump1eb44332009-09-09 15:08:12 +00004471
Chris Lattnerf97409f2008-04-06 06:57:35 +00004472 // Parse the declaration-specifiers.
John McCall54abf7d2009-11-04 02:18:39 +00004473 // Just use the ParsingDeclaration "scope" of the declarator.
John McCall0b7e6782011-03-24 11:26:52 +00004474 DeclSpec DS(AttrFactory);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004475
Richard Smith6ce48a72012-04-11 04:01:28 +00004476 // Parse any C++11 attributes.
4477 MaybeParseCXX0XAttributes(DS.getAttributes());
4478
John McCall7f040a92010-12-24 02:08:15 +00004479 // Skip any Microsoft attributes before a param.
David Blaikie4e4d0842012-03-11 07:00:24 +00004480 if (getLangOpts().MicrosoftExt && Tok.is(tok::l_square))
John McCall7f040a92010-12-24 02:08:15 +00004481 ParseMicrosoftAttributes(DS.getAttributes());
4482
4483 SourceLocation DSStart = Tok.getLocation();
Chris Lattner7399ee02008-10-20 02:05:46 +00004484
4485 // If the caller parsed attributes for the first argument, add them now.
John McCall7f040a92010-12-24 02:08:15 +00004486 // Take them so that we only apply the attributes to the first parameter.
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004487 // FIXME: If we can leave the attributes in the token stream somehow, we can
Richard Smith6ce48a72012-04-11 04:01:28 +00004488 // get rid of a parameter (FirstArgAttrs) and this statement. It might be
4489 // too much hassle.
4490 DS.takeAttributesFrom(FirstArgAttrs);
John McCall7f040a92010-12-24 02:08:15 +00004491
Chris Lattnere64c5492009-02-27 18:38:20 +00004492 ParseDeclarationSpecifiers(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00004493
Chris Lattnerf97409f2008-04-06 06:57:35 +00004494 // Parse the declarator. This is "PrototypeContext", because we must
4495 // accept either 'declarator' or 'abstract-declarator' here.
4496 Declarator ParmDecl(DS, Declarator::PrototypeContext);
4497 ParseDeclarator(ParmDecl);
4498
4499 // Parse GNU attributes, if present.
John McCall7f040a92010-12-24 02:08:15 +00004500 MaybeParseGNUAttributes(ParmDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00004501
Chris Lattnerf97409f2008-04-06 06:57:35 +00004502 // Remember this parsed parameter in ParamInfo.
4503 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
Mike Stump1eb44332009-09-09 15:08:12 +00004504
Douglas Gregor72b505b2008-12-16 21:30:33 +00004505 // DefArgToks is used when the parsing of default arguments needs
4506 // to be delayed.
4507 CachedTokens *DefArgToks = 0;
4508
Chris Lattnerf97409f2008-04-06 06:57:35 +00004509 // If no parameter was specified, verify that *something* was specified,
4510 // otherwise we have a missing type and identifier.
Chris Lattnere64c5492009-02-27 18:38:20 +00004511 if (DS.isEmpty() && ParmDecl.getIdentifier() == 0 &&
4512 ParmDecl.getNumTypeObjects() == 0) {
Chris Lattnerf97409f2008-04-06 06:57:35 +00004513 // Completely missing, emit error.
4514 Diag(DSStart, diag::err_missing_param);
4515 } else {
4516 // Otherwise, we have something. Add it and let semantic analysis try
4517 // to grok it and add the result to the ParamInfo we are building.
Mike Stump1eb44332009-09-09 15:08:12 +00004518
Chris Lattnerf97409f2008-04-06 06:57:35 +00004519 // Inform the actions module about the parameter declarator, so it gets
4520 // added to the current scope.
John McCalld226f652010-08-21 09:40:31 +00004521 Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl);
Chris Lattner04421082008-04-08 04:40:51 +00004522
4523 // Parse the default argument, if any. We parse the default
4524 // arguments in all dialects; the semantic analysis in
4525 // ActOnParamDefaultArgument will reject the default argument in
4526 // C.
4527 if (Tok.is(tok::equal)) {
Douglas Gregor61366e92008-12-24 00:01:03 +00004528 SourceLocation EqualLoc = Tok.getLocation();
4529
Chris Lattner04421082008-04-08 04:40:51 +00004530 // Parse the default argument
Douglas Gregor72b505b2008-12-16 21:30:33 +00004531 if (D.getContext() == Declarator::MemberContext) {
4532 // If we're inside a class definition, cache the tokens
4533 // corresponding to the default argument. We'll actually parse
4534 // them when we see the end of the class definition.
Douglas Gregor72b505b2008-12-16 21:30:33 +00004535 // FIXME: Can we use a smart pointer for Toks?
4536 DefArgToks = new CachedTokens;
4537
Mike Stump1eb44332009-09-09 15:08:12 +00004538 if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks,
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +00004539 /*StopAtSemi=*/true,
4540 /*ConsumeFinalToken=*/false)) {
Douglas Gregor72b505b2008-12-16 21:30:33 +00004541 delete DefArgToks;
4542 DefArgToks = 0;
Douglas Gregor61366e92008-12-24 00:01:03 +00004543 Actions.ActOnParamDefaultArgumentError(Param);
Argyrios Kyrtzidis2b602ad2010-08-06 09:47:24 +00004544 } else {
4545 // Mark the end of the default argument so that we know when to
4546 // stop when we parse it later on.
4547 Token DefArgEnd;
4548 DefArgEnd.startToken();
4549 DefArgEnd.setKind(tok::cxx_defaultarg_end);
4550 DefArgEnd.setLocation(Tok.getLocation());
4551 DefArgToks->push_back(DefArgEnd);
Mike Stump1eb44332009-09-09 15:08:12 +00004552 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc,
Anders Carlsson5e300d12009-06-12 16:51:40 +00004553 (*DefArgToks)[1].getLocation());
Argyrios Kyrtzidis2b602ad2010-08-06 09:47:24 +00004554 }
Chris Lattner04421082008-04-08 04:40:51 +00004555 } else {
Douglas Gregor72b505b2008-12-16 21:30:33 +00004556 // Consume the '='.
Douglas Gregor61366e92008-12-24 00:01:03 +00004557 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00004558
Douglas Gregorbe0f7bd2010-09-11 20:24:53 +00004559 // The argument isn't actually potentially evaluated unless it is
4560 // used.
4561 EnterExpressionEvaluationContext Eval(Actions,
Douglas Gregorccc1b5e2012-02-21 00:37:24 +00004562 Sema::PotentiallyEvaluatedIfUsed,
4563 Param);
Douglas Gregorbe0f7bd2010-09-11 20:24:53 +00004564
Sebastian Redl84407ba2012-03-14 15:54:00 +00004565 ExprResult DefArgResult;
Sebastian Redl3e280b52012-03-18 22:25:45 +00004566 if (getLangOpts().CPlusPlus0x && Tok.is(tok::l_brace)) {
4567 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
Sebastian Redl84407ba2012-03-14 15:54:00 +00004568 DefArgResult = ParseBraceInitializer();
Sebastian Redl3e280b52012-03-18 22:25:45 +00004569 } else
Sebastian Redl84407ba2012-03-14 15:54:00 +00004570 DefArgResult = ParseAssignmentExpression();
Douglas Gregor72b505b2008-12-16 21:30:33 +00004571 if (DefArgResult.isInvalid()) {
4572 Actions.ActOnParamDefaultArgumentError(Param);
4573 SkipUntil(tok::comma, tok::r_paren, true, true);
4574 } else {
4575 // Inform the actions module about the default argument
4576 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
John McCall9ae2f072010-08-23 23:25:46 +00004577 DefArgResult.take());
Douglas Gregor72b505b2008-12-16 21:30:33 +00004578 }
Chris Lattner04421082008-04-08 04:40:51 +00004579 }
4580 }
Mike Stump1eb44332009-09-09 15:08:12 +00004581
4582 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
4583 ParmDecl.getIdentifierLoc(), Param,
Douglas Gregor72b505b2008-12-16 21:30:33 +00004584 DefArgToks));
Chris Lattnerf97409f2008-04-06 06:57:35 +00004585 }
4586
4587 // If the next token is a comma, consume it and keep reading arguments.
Douglas Gregored5d6512009-09-22 21:41:40 +00004588 if (Tok.isNot(tok::comma)) {
4589 if (Tok.is(tok::ellipsis)) {
Douglas Gregored5d6512009-09-22 21:41:40 +00004590 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
4591
David Blaikie4e4d0842012-03-11 07:00:24 +00004592 if (!getLangOpts().CPlusPlus) {
Douglas Gregored5d6512009-09-22 21:41:40 +00004593 // We have ellipsis without a preceding ',', which is ill-formed
4594 // in C. Complain and provide the fix.
4595 Diag(EllipsisLoc, diag::err_missing_comma_before_ellipsis)
Douglas Gregor849b2432010-03-31 17:46:05 +00004596 << FixItHint::CreateInsertion(EllipsisLoc, ", ");
Douglas Gregored5d6512009-09-22 21:41:40 +00004597 }
4598 }
4599
4600 break;
4601 }
Mike Stump1eb44332009-09-09 15:08:12 +00004602
Chris Lattnerf97409f2008-04-06 06:57:35 +00004603 // Consume the comma.
4604 ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00004605 }
Mike Stump1eb44332009-09-09 15:08:12 +00004606
Chris Lattner66d28652008-04-06 06:34:08 +00004607}
Chris Lattneref4715c2008-04-06 05:45:57 +00004608
Reid Spencer5f016e22007-07-11 17:01:13 +00004609/// [C90] direct-declarator '[' constant-expression[opt] ']'
4610/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
4611/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
4612/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
4613/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Richard Smith6ee326a2012-04-10 01:32:12 +00004614/// [C++11] direct-declarator '[' constant-expression[opt] ']'
4615/// attribute-specifier-seq[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00004616void Parser::ParseBracketDeclarator(Declarator &D) {
Richard Smith6ee326a2012-04-10 01:32:12 +00004617 if (CheckProhibitedCXX11Attribute())
4618 return;
4619
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004620 BalancedDelimiterTracker T(*this, tok::l_square);
4621 T.consumeOpen();
Mike Stump1eb44332009-09-09 15:08:12 +00004622
Chris Lattner378c7e42008-12-18 07:27:21 +00004623 // C array syntax has many features, but by-far the most common is [] and [4].
4624 // This code does a fast path to handle some of the most obvious cases.
4625 if (Tok.getKind() == tok::r_square) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004626 T.consumeClose();
John McCall0b7e6782011-03-24 11:26:52 +00004627 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00004628 MaybeParseCXX0XAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00004629
Chris Lattner378c7e42008-12-18 07:27:21 +00004630 // Remember that we parsed the empty array type.
John McCall60d7b3a2010-08-24 06:29:42 +00004631 ExprResult NumElements;
John McCall0b7e6782011-03-24 11:26:52 +00004632 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004633 T.getOpenLocation(),
4634 T.getCloseLocation()),
4635 attrs, T.getCloseLocation());
Chris Lattner378c7e42008-12-18 07:27:21 +00004636 return;
4637 } else if (Tok.getKind() == tok::numeric_constant &&
4638 GetLookAheadToken(1).is(tok::r_square)) {
4639 // [4] is very common. Parse the numeric constant expression.
Richard Smith36f5cfe2012-03-09 08:00:36 +00004640 ExprResult ExprRes(Actions.ActOnNumericConstant(Tok, getCurScope()));
Chris Lattner378c7e42008-12-18 07:27:21 +00004641 ConsumeToken();
4642
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004643 T.consumeClose();
John McCall0b7e6782011-03-24 11:26:52 +00004644 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00004645 MaybeParseCXX0XAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00004646
Chris Lattner378c7e42008-12-18 07:27:21 +00004647 // Remember that we parsed a array type, and remember its features.
John McCall0b7e6782011-03-24 11:26:52 +00004648 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, 0,
John McCall7f040a92010-12-24 02:08:15 +00004649 ExprRes.release(),
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004650 T.getOpenLocation(),
4651 T.getCloseLocation()),
4652 attrs, T.getCloseLocation());
Chris Lattner378c7e42008-12-18 07:27:21 +00004653 return;
4654 }
Mike Stump1eb44332009-09-09 15:08:12 +00004655
Reid Spencer5f016e22007-07-11 17:01:13 +00004656 // If valid, this location is the position where we read the 'static' keyword.
4657 SourceLocation StaticLoc;
Chris Lattner04d66662007-10-09 17:33:22 +00004658 if (Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00004659 StaticLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00004660
Reid Spencer5f016e22007-07-11 17:01:13 +00004661 // If there is a type-qualifier-list, read it now.
Chris Lattnera1fcbad2008-12-18 06:50:14 +00004662 // Type qualifiers in an array subscript are a C99 feature.
John McCall0b7e6782011-03-24 11:26:52 +00004663 DeclSpec DS(AttrFactory);
Chris Lattner5a69d1c2008-12-18 07:02:59 +00004664 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Mike Stump1eb44332009-09-09 15:08:12 +00004665
Reid Spencer5f016e22007-07-11 17:01:13 +00004666 // If we haven't already read 'static', check to see if there is one after the
4667 // type-qualifier-list.
Chris Lattner04d66662007-10-09 17:33:22 +00004668 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00004669 StaticLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00004670
Reid Spencer5f016e22007-07-11 17:01:13 +00004671 // Handle "direct-declarator [ type-qual-list[opt] * ]".
4672 bool isStar = false;
John McCall60d7b3a2010-08-24 06:29:42 +00004673 ExprResult NumElements;
Mike Stump1eb44332009-09-09 15:08:12 +00004674
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00004675 // Handle the case where we have '[*]' as the array size. However, a leading
4676 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
4677 // the the token after the star is a ']'. Since stars in arrays are
4678 // infrequent, use of lookahead is not costly here.
4679 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattnera711dd02008-04-06 05:27:21 +00004680 ConsumeToken(); // Eat the '*'.
Reid Spencer5f016e22007-07-11 17:01:13 +00004681
Chris Lattnera1fcbad2008-12-18 06:50:14 +00004682 if (StaticLoc.isValid()) {
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00004683 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattnera1fcbad2008-12-18 06:50:14 +00004684 StaticLoc = SourceLocation(); // Drop the static.
4685 }
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00004686 isStar = true;
Chris Lattner04d66662007-10-09 17:33:22 +00004687 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner378c7e42008-12-18 07:27:21 +00004688 // Note, in C89, this production uses the constant-expr production instead
4689 // of assignment-expr. The only difference is that assignment-expr allows
4690 // things like '=' and '*='. Sema rejects these in C89 mode because they
4691 // are not i-c-e's, so we don't need to distinguish between the two here.
Mike Stump1eb44332009-09-09 15:08:12 +00004692
Douglas Gregore0762c92009-06-19 23:52:42 +00004693 // Parse the constant-expression or assignment-expression now (depending
4694 // on dialect).
David Blaikie4e4d0842012-03-11 07:00:24 +00004695 if (getLangOpts().CPlusPlus) {
Douglas Gregore0762c92009-06-19 23:52:42 +00004696 NumElements = ParseConstantExpression();
Eli Friedman71b8fb52012-01-21 01:01:51 +00004697 } else {
4698 EnterExpressionEvaluationContext Unevaluated(Actions,
4699 Sema::ConstantEvaluated);
Douglas Gregore0762c92009-06-19 23:52:42 +00004700 NumElements = ParseAssignmentExpression();
Eli Friedman71b8fb52012-01-21 01:01:51 +00004701 }
Reid Spencer5f016e22007-07-11 17:01:13 +00004702 }
Mike Stump1eb44332009-09-09 15:08:12 +00004703
Reid Spencer5f016e22007-07-11 17:01:13 +00004704 // If there was an error parsing the assignment-expression, recover.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00004705 if (NumElements.isInvalid()) {
Chris Lattner5cb10d32009-04-24 22:30:50 +00004706 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00004707 // If the expression was invalid, skip it.
4708 SkipUntil(tok::r_square);
4709 return;
4710 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00004711
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004712 T.consumeClose();
Sebastian Redlab197ba2009-02-09 18:23:29 +00004713
John McCall0b7e6782011-03-24 11:26:52 +00004714 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00004715 MaybeParseCXX0XAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00004716
Chris Lattner378c7e42008-12-18 07:27:21 +00004717 // Remember that we parsed a array type, and remember its features.
John McCall0b7e6782011-03-24 11:26:52 +00004718 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
Reid Spencer5f016e22007-07-11 17:01:13 +00004719 StaticLoc.isValid(), isStar,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00004720 NumElements.release(),
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004721 T.getOpenLocation(),
4722 T.getCloseLocation()),
4723 attrs, T.getCloseLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00004724}
4725
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004726/// [GNU] typeof-specifier:
4727/// typeof ( expressions )
4728/// typeof ( type-name )
4729/// [GNU/C++] typeof unary-expression
Steve Naroffd1861fd2007-07-31 12:34:36 +00004730///
4731void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner04d66662007-10-09 17:33:22 +00004732 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004733 Token OpTok = Tok;
Steve Naroffd1861fd2007-07-31 12:34:36 +00004734 SourceLocation StartLoc = ConsumeToken();
4735
John McCallcfb708c2010-01-13 20:03:27 +00004736 const bool hasParens = Tok.is(tok::l_paren);
4737
Eli Friedman71b8fb52012-01-21 01:01:51 +00004738 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated);
4739
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004740 bool isCastExpr;
John McCallb3d87482010-08-24 05:47:05 +00004741 ParsedType CastTy;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004742 SourceRange CastRange;
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00004743 ExprResult Operand = ParseExprAfterUnaryExprOrTypeTrait(OpTok, isCastExpr,
4744 CastTy, CastRange);
John McCallcfb708c2010-01-13 20:03:27 +00004745 if (hasParens)
4746 DS.setTypeofParensRange(CastRange);
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004747
4748 if (CastRange.getEnd().isInvalid())
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004749 // FIXME: Not accurate, the range gets one token more than it should.
4750 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004751 else
4752 DS.SetRangeEnd(CastRange.getEnd());
Mike Stump1eb44332009-09-09 15:08:12 +00004753
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004754 if (isCastExpr) {
4755 if (!CastTy) {
4756 DS.SetTypeSpecError();
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004757 return;
Douglas Gregor809070a2009-02-18 17:45:20 +00004758 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004759
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004760 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00004761 unsigned DiagID;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004762 // Check for duplicate type specifiers (e.g. "int typeof(int)").
4763 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00004764 DiagID, CastTy))
4765 Diag(StartLoc, DiagID) << PrevSpec;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004766 return;
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004767 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004768
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004769 // If we get here, the operand to the typeof was an expresion.
4770 if (Operand.isInvalid()) {
4771 DS.SetTypeSpecError();
Steve Naroff9dfa7b42007-08-02 02:53:48 +00004772 return;
Steve Naroffd1861fd2007-07-31 12:34:36 +00004773 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004774
Eli Friedman71b8fb52012-01-21 01:01:51 +00004775 // We might need to transform the operand if it is potentially evaluated.
4776 Operand = Actions.HandleExprEvaluationContextForTypeof(Operand.get());
4777 if (Operand.isInvalid()) {
4778 DS.SetTypeSpecError();
4779 return;
4780 }
4781
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004782 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00004783 unsigned DiagID;
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004784 // Check for duplicate type specifiers (e.g. "int typeof(int)").
4785 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
John McCallb3d87482010-08-24 05:47:05 +00004786 DiagID, Operand.get()))
John McCallfec54012009-08-03 20:12:06 +00004787 Diag(StartLoc, DiagID) << PrevSpec;
Steve Naroffd1861fd2007-07-31 12:34:36 +00004788}
Chris Lattner1b492422010-02-28 18:33:55 +00004789
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00004790/// [C11] atomic-specifier:
Eli Friedmanb001de72011-10-06 23:00:33 +00004791/// _Atomic ( type-name )
4792///
4793void Parser::ParseAtomicSpecifier(DeclSpec &DS) {
4794 assert(Tok.is(tok::kw__Atomic) && "Not an atomic specifier");
4795
4796 SourceLocation StartLoc = ConsumeToken();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004797 BalancedDelimiterTracker T(*this, tok::l_paren);
4798 if (T.expectAndConsume(diag::err_expected_lparen_after, "_Atomic")) {
Eli Friedmanb001de72011-10-06 23:00:33 +00004799 SkipUntil(tok::r_paren);
4800 return;
4801 }
4802
4803 TypeResult Result = ParseTypeName();
4804 if (Result.isInvalid()) {
4805 SkipUntil(tok::r_paren);
4806 return;
4807 }
4808
4809 // Match the ')'
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004810 T.consumeClose();
Eli Friedmanb001de72011-10-06 23:00:33 +00004811
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004812 if (T.getCloseLocation().isInvalid())
Eli Friedmanb001de72011-10-06 23:00:33 +00004813 return;
4814
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004815 DS.setTypeofParensRange(T.getRange());
4816 DS.SetRangeEnd(T.getCloseLocation());
Eli Friedmanb001de72011-10-06 23:00:33 +00004817
4818 const char *PrevSpec = 0;
4819 unsigned DiagID;
4820 if (DS.SetTypeSpecType(DeclSpec::TST_atomic, StartLoc, PrevSpec,
4821 DiagID, Result.release()))
4822 Diag(StartLoc, DiagID) << PrevSpec;
4823}
4824
Chris Lattner1b492422010-02-28 18:33:55 +00004825
4826/// TryAltiVecVectorTokenOutOfLine - Out of line body that should only be called
4827/// from TryAltiVecVectorToken.
4828bool Parser::TryAltiVecVectorTokenOutOfLine() {
4829 Token Next = NextToken();
4830 switch (Next.getKind()) {
4831 default: return false;
4832 case tok::kw_short:
4833 case tok::kw_long:
4834 case tok::kw_signed:
4835 case tok::kw_unsigned:
4836 case tok::kw_void:
4837 case tok::kw_char:
4838 case tok::kw_int:
4839 case tok::kw_float:
4840 case tok::kw_double:
4841 case tok::kw_bool:
4842 case tok::kw___pixel:
4843 Tok.setKind(tok::kw___vector);
4844 return true;
4845 case tok::identifier:
4846 if (Next.getIdentifierInfo() == Ident_pixel) {
4847 Tok.setKind(tok::kw___vector);
4848 return true;
4849 }
4850 return false;
4851 }
4852}
4853
4854bool Parser::TryAltiVecTokenOutOfLine(DeclSpec &DS, SourceLocation Loc,
4855 const char *&PrevSpec, unsigned &DiagID,
4856 bool &isInvalid) {
4857 if (Tok.getIdentifierInfo() == Ident_vector) {
4858 Token Next = NextToken();
4859 switch (Next.getKind()) {
4860 case tok::kw_short:
4861 case tok::kw_long:
4862 case tok::kw_signed:
4863 case tok::kw_unsigned:
4864 case tok::kw_void:
4865 case tok::kw_char:
4866 case tok::kw_int:
4867 case tok::kw_float:
4868 case tok::kw_double:
4869 case tok::kw_bool:
4870 case tok::kw___pixel:
4871 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
4872 return true;
4873 case tok::identifier:
4874 if (Next.getIdentifierInfo() == Ident_pixel) {
4875 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
4876 return true;
4877 }
4878 break;
4879 default:
4880 break;
4881 }
Douglas Gregora8f031f2010-06-16 15:28:57 +00004882 } else if ((Tok.getIdentifierInfo() == Ident_pixel) &&
Chris Lattner1b492422010-02-28 18:33:55 +00004883 DS.isTypeAltiVecVector()) {
4884 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
4885 return true;
4886 }
4887 return false;
4888}