blob: d91457c0d690cf5eb66c4ad91245a64876264861 [file] [log] [blame]
Chris Lattner7ad0fbe2006-11-05 07:46:30 +00001//===--- ParseDecl.cpp - Declaration Parsing ------------------------------===//
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Declaration portions of the Parser interfaces.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Parse/Parser.h"
Chris Lattner60f36222009-01-29 05:15:15 +000015#include "clang/Parse/ParseDiagnostic.h"
Peter Collingbourne599cb8e2011-03-18 22:38:29 +000016#include "clang/Basic/OpenCL.h"
Kaelyn Uhrain031643e2012-04-26 23:36:17 +000017#include "clang/Sema/Lookup.h"
John McCall8b0666c2010-08-20 18:27:03 +000018#include "clang/Sema/Scope.h"
19#include "clang/Sema/ParsedTemplate.h"
John McCallfaf5fb42010-08-26 23:41:50 +000020#include "clang/Sema/PrettyDeclStackTrace.h"
Chris Lattner8a9a97a2009-12-10 00:21:05 +000021#include "RAIIObjectsForParser.h"
Chris Lattnerad9ac942007-01-23 01:14:52 +000022#include "llvm/ADT/SmallSet.h"
Benjamin Kramer49038022012-02-04 13:45:25 +000023#include "llvm/ADT/SmallString.h"
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +000024#include "llvm/ADT/StringSwitch.h"
Chris Lattnerc0acd3d2006-07-31 05:13:43 +000025using namespace clang;
26
27//===----------------------------------------------------------------------===//
28// C99 6.7: Declarations.
29//===----------------------------------------------------------------------===//
30
Chris Lattnerf5fbd792006-08-10 23:56:11 +000031/// ParseTypeName
32/// type-name: [C99 6.7.6]
33/// specifier-qualifier-list abstract-declarator[opt]
Sebastian Redlbd150f42008-11-21 19:14:01 +000034///
35/// Called type-id in C++.
Douglas Gregor205d5e32011-01-31 16:09:46 +000036TypeResult Parser::ParseTypeName(SourceRange *Range,
John McCall31168b02011-06-15 23:02:42 +000037 Declarator::TheContext Context,
Richard Smithcd1c0552011-07-01 19:46:12 +000038 AccessSpecifier AS,
39 Decl **OwnedType) {
Richard Smith62dad822012-03-15 01:02:11 +000040 DeclSpecContext DSC = getDeclSpecContextFromDeclaratorContext(Context);
Richard Smith2f07ad52012-05-09 20:55:26 +000041 if (DSC == DSC_normal)
42 DSC = DSC_type_specifier;
Richard Smithbfdb1082012-03-12 08:56:40 +000043
Chris Lattnerf5fbd792006-08-10 23:56:11 +000044 // Parse the common declaration-specifiers piece.
John McCall084e83d2011-03-24 11:26:52 +000045 DeclSpec DS(AttrFactory);
Richard Smithbfdb1082012-03-12 08:56:40 +000046 ParseSpecifierQualifierList(DS, AS, DSC);
Richard Smithcd1c0552011-07-01 19:46:12 +000047 if (OwnedType)
48 *OwnedType = DS.isTypeSpecOwned() ? DS.getRepAsDecl() : 0;
Sebastian Redld6434562009-05-29 18:02:33 +000049
Chris Lattnerf5fbd792006-08-10 23:56:11 +000050 // Parse the abstract-declarator, if present.
Douglas Gregor205d5e32011-01-31 16:09:46 +000051 Declarator DeclaratorInfo(DS, Context);
Chris Lattnerf5fbd792006-08-10 23:56:11 +000052 ParseDeclarator(DeclaratorInfo);
Sebastian Redld6434562009-05-29 18:02:33 +000053 if (Range)
54 *Range = DeclaratorInfo.getSourceRange();
55
Chris Lattnerf6d1c9c2009-04-25 08:06:05 +000056 if (DeclaratorInfo.isInvalidType())
Douglas Gregor220cac52009-02-18 17:45:20 +000057 return true;
58
Douglas Gregor0be31a22010-07-02 17:43:08 +000059 return Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
Chris Lattnerf5fbd792006-08-10 23:56:11 +000060}
61
Caitlin Sadowski9385dd72011-09-08 17:42:22 +000062
63/// isAttributeLateParsed - Return true if the attribute has arguments that
64/// require late parsing.
65static bool isAttributeLateParsed(const IdentifierInfo &II) {
66 return llvm::StringSwitch<bool>(II.getName())
67#include "clang/Parse/AttrLateParsed.inc"
68 .Default(false);
69}
70
71
Alexis Hunt96d5c762009-11-21 08:43:09 +000072/// ParseGNUAttributes - Parse a non-empty attributes list.
Chris Lattnerb8cd5c22006-08-15 04:10:46 +000073///
74/// [GNU] attributes:
75/// attribute
76/// attributes attribute
77///
78/// [GNU] attribute:
79/// '__attribute__' '(' '(' attribute-list ')' ')'
80///
81/// [GNU] attribute-list:
82/// attrib
83/// attribute_list ',' attrib
84///
85/// [GNU] attrib:
86/// empty
Steve Naroff0f2fe172007-06-01 17:11:19 +000087/// attrib-name
88/// attrib-name '(' identifier ')'
89/// attrib-name '(' identifier ',' nonempty-expr-list ')'
90/// attrib-name '(' argument-expression-list [C99 6.5.2] ')'
Chris Lattnerb8cd5c22006-08-15 04:10:46 +000091///
Steve Naroff0f2fe172007-06-01 17:11:19 +000092/// [GNU] attrib-name:
93/// identifier
94/// typespec
95/// typequal
96/// storageclass
Mike Stump11289f42009-09-09 15:08:12 +000097///
Steve Naroff0f2fe172007-06-01 17:11:19 +000098/// FIXME: The GCC grammar/code for this construct implies we need two
Mike Stump11289f42009-09-09 15:08:12 +000099/// token lookahead. Comment from gcc: "If they start with an identifier
100/// which is followed by a comma or close parenthesis, then the arguments
Steve Naroff0f2fe172007-06-01 17:11:19 +0000101/// start with that identifier; otherwise they are an expression list."
102///
Richard Smithb12bf692011-10-17 21:20:17 +0000103/// GCC does not require the ',' between attribs in an attribute-list.
104///
Steve Naroff0f2fe172007-06-01 17:11:19 +0000105/// At the moment, I am not doing 2 token lookahead. I am also unaware of
106/// any attributes that don't work (based on my limited testing). Most
107/// attributes are very simple in practice. Until we find a bug, I don't see
108/// a pressing need to implement the 2 token lookahead.
Chris Lattnerb8cd5c22006-08-15 04:10:46 +0000109
John McCall53fa7142010-12-24 02:08:15 +0000110void Parser::ParseGNUAttributes(ParsedAttributes &attrs,
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000111 SourceLocation *endLoc,
112 LateParsedAttrList *LateAttrs) {
Alexis Hunt96d5c762009-11-21 08:43:09 +0000113 assert(Tok.is(tok::kw___attribute) && "Not a GNU attribute list!");
Mike Stump11289f42009-09-09 15:08:12 +0000114
Chris Lattner76c72282007-10-09 17:33:22 +0000115 while (Tok.is(tok::kw___attribute)) {
Steve Naroff0f2fe172007-06-01 17:11:19 +0000116 ConsumeToken();
117 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
118 "attribute")) {
119 SkipUntil(tok::r_paren, true); // skip until ) or ;
John McCall53fa7142010-12-24 02:08:15 +0000120 return;
Steve Naroff0f2fe172007-06-01 17:11:19 +0000121 }
122 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, "(")) {
123 SkipUntil(tok::r_paren, true); // skip until ) or ;
John McCall53fa7142010-12-24 02:08:15 +0000124 return;
Steve Naroff0f2fe172007-06-01 17:11:19 +0000125 }
126 // Parse the attribute-list. e.g. __attribute__(( weak, alias("__f") ))
Chris Lattner76c72282007-10-09 17:33:22 +0000127 while (Tok.is(tok::identifier) || isDeclarationSpecifier() ||
128 Tok.is(tok::comma)) {
Mike Stump11289f42009-09-09 15:08:12 +0000129 if (Tok.is(tok::comma)) {
Steve Naroff0f2fe172007-06-01 17:11:19 +0000130 // allows for empty/non-empty attributes. ((__vector_size__(16),,,,))
131 ConsumeToken();
132 continue;
133 }
134 // we have an identifier or declaration specifier (const, int, etc.)
135 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
136 SourceLocation AttrNameLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000137
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000138 if (Tok.is(tok::l_paren)) {
139 // handle "parameterized" attributes
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +0000140 if (LateAttrs && isAttributeLateParsed(*AttrName)) {
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000141 LateParsedAttribute *LA =
142 new LateParsedAttribute(this, *AttrName, AttrNameLoc);
143 LateAttrs->push_back(LA);
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +0000144
145 // Attributes in a class are parsed at the end of the class, along
146 // with other late-parsed declarations.
147 if (!ClassStack.empty())
148 getCurrentClass().LateParsedDeclarations.push_back(LA);
Mike Stump11289f42009-09-09 15:08:12 +0000149
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000150 // consume everything up to and including the matching right parens
151 ConsumeAndStoreUntil(tok::r_paren, LA->Toks, true, false);
Mike Stump11289f42009-09-09 15:08:12 +0000152
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000153 Token Eof;
154 Eof.startToken();
155 Eof.setLocation(Tok.getLocation());
156 LA->Toks.push_back(Eof);
157 } else {
158 ParseGNUAttributeArgs(AttrName, AttrNameLoc, attrs, endLoc);
Steve Naroff0f2fe172007-06-01 17:11:19 +0000159 }
160 } else {
John McCall084e83d2011-03-24 11:26:52 +0000161 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc,
Alexis Hunta0e54d42012-06-18 16:13:52 +0000162 0, SourceLocation(), 0, 0, AttributeList::AS_GNU);
Steve Naroff0f2fe172007-06-01 17:11:19 +0000163 }
164 }
Steve Naroff98d153c2007-06-06 23:19:11 +0000165 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
Steve Naroff98d153c2007-06-06 23:19:11 +0000166 SkipUntil(tok::r_paren, false);
Alexis Hunt96d5c762009-11-21 08:43:09 +0000167 SourceLocation Loc = Tok.getLocation();
Sebastian Redlf6591ca2009-02-09 18:23:29 +0000168 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) {
169 SkipUntil(tok::r_paren, false);
170 }
John McCall53fa7142010-12-24 02:08:15 +0000171 if (endLoc)
172 *endLoc = Loc;
Steve Naroff0f2fe172007-06-01 17:11:19 +0000173 }
Steve Naroff0f2fe172007-06-01 17:11:19 +0000174}
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000175
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000176
177/// Parse the arguments to a parameterized GNU attribute
178void Parser::ParseGNUAttributeArgs(IdentifierInfo *AttrName,
179 SourceLocation AttrNameLoc,
180 ParsedAttributes &Attrs,
181 SourceLocation *EndLoc) {
182
183 assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('");
184
185 // Availability attributes have their own grammar.
186 if (AttrName->isStr("availability")) {
187 ParseAvailabilityAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc);
188 return;
189 }
190 // Thread safety attributes fit into the FIXME case above, so we
191 // just parse the arguments as a list of expressions
192 if (IsThreadSafetyAttribute(AttrName->getName())) {
193 ParseThreadSafetyAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc);
194 return;
195 }
196
197 ConsumeParen(); // ignore the left paren loc for now
198
Richard Smithb12bf692011-10-17 21:20:17 +0000199 IdentifierInfo *ParmName = 0;
200 SourceLocation ParmLoc;
201 bool BuiltinType = false;
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000202
Richard Smithb12bf692011-10-17 21:20:17 +0000203 switch (Tok.getKind()) {
204 case tok::kw_char:
205 case tok::kw_wchar_t:
206 case tok::kw_char16_t:
207 case tok::kw_char32_t:
208 case tok::kw_bool:
209 case tok::kw_short:
210 case tok::kw_int:
211 case tok::kw_long:
212 case tok::kw___int64:
Richard Smithf016bbc2012-04-04 06:24:32 +0000213 case tok::kw___int128:
Richard Smithb12bf692011-10-17 21:20:17 +0000214 case tok::kw_signed:
215 case tok::kw_unsigned:
216 case tok::kw_float:
217 case tok::kw_double:
218 case tok::kw_void:
219 case tok::kw_typeof:
220 // __attribute__(( vec_type_hint(char) ))
221 // FIXME: Don't just discard the builtin type token.
222 ConsumeToken();
223 BuiltinType = true;
224 break;
225
226 case tok::identifier:
227 ParmName = Tok.getIdentifierInfo();
228 ParmLoc = ConsumeToken();
229 break;
230
231 default:
232 break;
233 }
234
235 ExprVector ArgExprs(Actions);
236
237 if (!BuiltinType &&
238 (ParmLoc.isValid() ? Tok.is(tok::comma) : Tok.isNot(tok::r_paren))) {
239 // Eat the comma.
240 if (ParmLoc.isValid())
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000241 ConsumeToken();
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000242
Richard Smithb12bf692011-10-17 21:20:17 +0000243 // Parse the non-empty comma-separated list of expressions.
244 while (1) {
245 ExprResult ArgExpr(ParseAssignmentExpression());
246 if (ArgExpr.isInvalid()) {
247 SkipUntil(tok::r_paren);
248 return;
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000249 }
Richard Smithb12bf692011-10-17 21:20:17 +0000250 ArgExprs.push_back(ArgExpr.release());
251 if (Tok.isNot(tok::comma))
252 break;
253 ConsumeToken(); // Eat the comma, move to the next argument
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000254 }
Richard Smithb12bf692011-10-17 21:20:17 +0000255 }
Fariborz Jahanian6b708652011-10-18 17:11:10 +0000256 else if (Tok.is(tok::less) && AttrName->isStr("iboutletcollection")) {
257 if (!ExpectAndConsume(tok::less, diag::err_expected_less_after, "<",
258 tok::greater)) {
Fariborz Jahanian7f733022011-10-18 23:13:50 +0000259 while (Tok.is(tok::identifier)) {
260 ConsumeToken();
261 if (Tok.is(tok::greater))
262 break;
263 if (Tok.is(tok::comma)) {
264 ConsumeToken();
265 continue;
266 }
267 }
268 if (Tok.isNot(tok::greater))
269 Diag(Tok, diag::err_iboutletcollection_with_protocol);
Fariborz Jahanian6b708652011-10-18 17:11:10 +0000270 SkipUntil(tok::r_paren, false, true); // skip until ')'
271 }
272 }
Richard Smithb12bf692011-10-17 21:20:17 +0000273
274 SourceLocation RParen = Tok.getLocation();
275 if (!ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) {
276 AttributeList *attr =
Argyrios Kyrtzidis635a9b42011-09-13 16:05:53 +0000277 Attrs.addNew(AttrName, SourceRange(AttrNameLoc, RParen), 0, AttrNameLoc,
Alexis Hunta0e54d42012-06-18 16:13:52 +0000278 ParmName, ParmLoc, ArgExprs.take(), ArgExprs.size(),
279 AttributeList::AS_GNU);
Alexis Hunt3bc72c12012-06-19 23:57:03 +0000280 if (BuiltinType && attr->getKind() == AttributeList::AT_IBOutletCollection)
Richard Smithb12bf692011-10-17 21:20:17 +0000281 Diag(Tok, diag::err_iboutletcollection_builtintype);
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000282 }
283}
284
Chad Rosierc1183952012-06-26 22:30:43 +0000285/// \brief Parses a single argument for a declspec, including the
Aaron Ballman478faed2012-06-19 22:09:27 +0000286/// surrounding parens.
Chad Rosierc1183952012-06-26 22:30:43 +0000287void Parser::ParseMicrosoftDeclSpecWithSingleArg(IdentifierInfo *AttrName,
Aaron Ballman478faed2012-06-19 22:09:27 +0000288 SourceLocation AttrNameLoc,
289 ParsedAttributes &Attrs)
290{
291 BalancedDelimiterTracker T(*this, tok::l_paren);
Chad Rosierc1183952012-06-26 22:30:43 +0000292 if (T.expectAndConsume(diag::err_expected_lparen_after,
Aaron Ballman478faed2012-06-19 22:09:27 +0000293 AttrName->getNameStart(), tok::r_paren))
294 return;
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000295
Aaron Ballman478faed2012-06-19 22:09:27 +0000296 ExprResult ArgExpr(ParseConstantExpression());
297 if (ArgExpr.isInvalid()) {
298 T.skipToEnd();
299 return;
300 }
301 Expr *ExprList = ArgExpr.take();
Chad Rosierc1183952012-06-26 22:30:43 +0000302 Attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, SourceLocation(),
Aaron Ballman478faed2012-06-19 22:09:27 +0000303 &ExprList, 1, AttributeList::AS_Declspec);
304
305 T.consumeClose();
306}
307
Chad Rosierc1183952012-06-26 22:30:43 +0000308/// \brief Determines whether a declspec is a "simple" one requiring no
Aaron Ballman478faed2012-06-19 22:09:27 +0000309/// arguments.
310bool Parser::IsSimpleMicrosoftDeclSpec(IdentifierInfo *Ident) {
311 return llvm::StringSwitch<bool>(Ident->getName())
312 .Case("dllimport", true)
313 .Case("dllexport", true)
314 .Case("noreturn", true)
315 .Case("nothrow", true)
316 .Case("noinline", true)
317 .Case("naked", true)
318 .Case("appdomain", true)
319 .Case("process", true)
320 .Case("jitintrinsic", true)
321 .Case("noalias", true)
322 .Case("restrict", true)
323 .Case("novtable", true)
324 .Case("selectany", true)
325 .Case("thread", true)
326 .Default(false);
327}
328
Chad Rosierc1183952012-06-26 22:30:43 +0000329/// \brief Attempts to parse a declspec which is not simple (one that takes
Aaron Ballman478faed2012-06-19 22:09:27 +0000330/// parameters). Will return false if we properly handled the declspec, or
331/// true if it is an unknown declspec.
Chad Rosierc1183952012-06-26 22:30:43 +0000332void Parser::ParseComplexMicrosoftDeclSpec(IdentifierInfo *Ident,
Aaron Ballman478faed2012-06-19 22:09:27 +0000333 SourceLocation Loc,
334 ParsedAttributes &Attrs) {
335 // Try to handle the easy case first -- these declspecs all take a single
336 // parameter as their argument.
337 if (llvm::StringSwitch<bool>(Ident->getName())
338 .Case("uuid", true)
339 .Case("align", true)
340 .Case("allocate", true)
341 .Default(false)) {
342 ParseMicrosoftDeclSpecWithSingleArg(Ident, Loc, Attrs);
343 } else if (Ident->getName() == "deprecated") {
Chad Rosierc1183952012-06-26 22:30:43 +0000344 // The deprecated declspec has an optional single argument, so we will
345 // check for a l-paren to decide whether we should parse an argument or
Aaron Ballman478faed2012-06-19 22:09:27 +0000346 // not.
347 if (Tok.getKind() == tok::l_paren)
348 ParseMicrosoftDeclSpecWithSingleArg(Ident, Loc, Attrs);
349 else
Chad Rosierc1183952012-06-26 22:30:43 +0000350 Attrs.addNew(Ident, Loc, 0, Loc, 0, SourceLocation(), 0, 0,
Aaron Ballman478faed2012-06-19 22:09:27 +0000351 AttributeList::AS_Declspec);
352 } else if (Ident->getName() == "property") {
353 // The property declspec is more complex in that it can take one or two
Chad Rosierc1183952012-06-26 22:30:43 +0000354 // assignment expressions as a parameter, but the lhs of the assignment
Aaron Ballman478faed2012-06-19 22:09:27 +0000355 // must be named get or put.
356 //
Chad Rosierc1183952012-06-26 22:30:43 +0000357 // For right now, we will just skip to the closing right paren of the
Aaron Ballman478faed2012-06-19 22:09:27 +0000358 // property expression.
359 //
360 // FIXME: we should deal with __declspec(property) at some point because it
361 // is used in the platform SDK headers for the Parallel Patterns Library
362 // and ATL.
363 BalancedDelimiterTracker T(*this, tok::l_paren);
Chad Rosierc1183952012-06-26 22:30:43 +0000364 if (T.expectAndConsume(diag::err_expected_lparen_after,
Aaron Ballman478faed2012-06-19 22:09:27 +0000365 Ident->getNameStart(), tok::r_paren))
366 return;
367 T.skipToEnd();
368 } else {
369 // We don't recognize this as a valid declspec, but instead of creating the
370 // attribute and allowing sema to warn about it, we will warn here instead.
371 // This is because some attributes have multiple spellings, but we need to
372 // disallow that for declspecs (such as align vs aligned). If we made the
Chad Rosierc1183952012-06-26 22:30:43 +0000373 // attribute, we'd have to split the valid declspec spelling logic into
Aaron Ballman478faed2012-06-19 22:09:27 +0000374 // both locations.
375 Diag(Loc, diag::warn_ms_declspec_unknown) << Ident;
376
377 // If there's an open paren, we should eat the open and close parens under
378 // the assumption that this unknown declspec has parameters.
379 BalancedDelimiterTracker T(*this, tok::l_paren);
380 if (!T.consumeOpen())
381 T.skipToEnd();
382 }
383}
384
Eli Friedman06de2b52009-06-08 07:21:15 +0000385/// [MS] decl-specifier:
386/// __declspec ( extended-decl-modifier-seq )
387///
388/// [MS] extended-decl-modifier-seq:
389/// extended-decl-modifier[opt]
390/// extended-decl-modifier extended-decl-modifier-seq
Aaron Ballman478faed2012-06-19 22:09:27 +0000391void Parser::ParseMicrosoftDeclSpec(ParsedAttributes &Attrs) {
Steve Naroff3a9b7e02008-12-24 20:59:21 +0000392 assert(Tok.is(tok::kw___declspec) && "Not a declspec!");
Eli Friedman06de2b52009-06-08 07:21:15 +0000393
Steve Naroff3a9b7e02008-12-24 20:59:21 +0000394 ConsumeToken();
Aaron Ballman478faed2012-06-19 22:09:27 +0000395 BalancedDelimiterTracker T(*this, tok::l_paren);
Chad Rosierc1183952012-06-26 22:30:43 +0000396 if (T.expectAndConsume(diag::err_expected_lparen_after, "__declspec",
Aaron Ballman478faed2012-06-19 22:09:27 +0000397 tok::r_paren))
John McCall53fa7142010-12-24 02:08:15 +0000398 return;
Jakob Stoklund Olesene1c0ae62012-06-19 21:48:43 +0000399
Chad Rosierc1183952012-06-26 22:30:43 +0000400 // An empty declspec is perfectly legal and should not warn. Additionally,
Aaron Ballman478faed2012-06-19 22:09:27 +0000401 // you can specify multiple attributes per declspec.
402 while (Tok.getKind() != tok::r_paren) {
403 // We expect either a well-known identifier or a generic string. Anything
404 // else is a malformed declspec.
405 bool IsString = Tok.getKind() == tok::string_literal ? true : false;
Chad Rosierc1183952012-06-26 22:30:43 +0000406 if (!IsString && Tok.getKind() != tok::identifier &&
Aaron Ballman478faed2012-06-19 22:09:27 +0000407 Tok.getKind() != tok::kw_restrict) {
408 Diag(Tok, diag::err_ms_declspec_type);
409 T.skipToEnd();
410 return;
Jakob Stoklund Olesene1c0ae62012-06-19 21:48:43 +0000411 }
Aaron Ballman478faed2012-06-19 22:09:27 +0000412
413 IdentifierInfo *AttrName;
414 SourceLocation AttrNameLoc;
415 if (IsString) {
416 SmallString<8> StrBuffer;
417 bool Invalid = false;
418 StringRef Str = PP.getSpelling(Tok, StrBuffer, &Invalid);
419 if (Invalid) {
420 T.skipToEnd();
421 return;
Jakob Stoklund Olesene1c0ae62012-06-19 21:48:43 +0000422 }
Aaron Ballman478faed2012-06-19 22:09:27 +0000423 AttrName = PP.getIdentifierInfo(Str);
424 AttrNameLoc = ConsumeStringToken();
Jakob Stoklund Olesene1c0ae62012-06-19 21:48:43 +0000425 } else {
Aaron Ballman478faed2012-06-19 22:09:27 +0000426 AttrName = Tok.getIdentifierInfo();
427 AttrNameLoc = ConsumeToken();
Jakob Stoklund Olesene1c0ae62012-06-19 21:48:43 +0000428 }
Chad Rosierc1183952012-06-26 22:30:43 +0000429
Aaron Ballman478faed2012-06-19 22:09:27 +0000430 if (IsString || IsSimpleMicrosoftDeclSpec(AttrName))
Chad Rosierc1183952012-06-26 22:30:43 +0000431 // If we have a generic string, we will allow it because there is no
432 // documented list of allowable string declspecs, but we know they exist
Aaron Ballman478faed2012-06-19 22:09:27 +0000433 // (for instance, SAL declspecs in older versions of MSVC).
434 //
Chad Rosierc1183952012-06-26 22:30:43 +0000435 // Alternatively, if the identifier is a simple one, then it requires no
Aaron Ballman478faed2012-06-19 22:09:27 +0000436 // arguments and can be turned into an attribute directly.
Chad Rosierc1183952012-06-26 22:30:43 +0000437 Attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, SourceLocation(),
Aaron Ballman478faed2012-06-19 22:09:27 +0000438 0, 0, AttributeList::AS_Declspec);
439 else
440 ParseComplexMicrosoftDeclSpec(AttrName, AttrNameLoc, Attrs);
Jakob Stoklund Olesene1c0ae62012-06-19 21:48:43 +0000441 }
Aaron Ballman478faed2012-06-19 22:09:27 +0000442 T.consumeClose();
Eli Friedman53339e02009-06-08 23:27:34 +0000443}
444
John McCall53fa7142010-12-24 02:08:15 +0000445void Parser::ParseMicrosoftTypeAttributes(ParsedAttributes &attrs) {
Eli Friedman53339e02009-06-08 23:27:34 +0000446 // Treat these like attributes
Eli Friedman53339e02009-06-08 23:27:34 +0000447 while (Tok.is(tok::kw___fastcall) || Tok.is(tok::kw___stdcall) ||
Douglas Gregora941dca2010-05-18 16:57:00 +0000448 Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___cdecl) ||
Francois Pichet17ed0202011-08-18 09:59:55 +0000449 Tok.is(tok::kw___ptr64) || Tok.is(tok::kw___w64) ||
Francois Pichetf2fb4112011-08-25 00:36:46 +0000450 Tok.is(tok::kw___ptr32) ||
Francois Pichet17ed0202011-08-18 09:59:55 +0000451 Tok.is(tok::kw___unaligned)) {
Eli Friedman53339e02009-06-08 23:27:34 +0000452 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
453 SourceLocation AttrNameLoc = ConsumeToken();
John McCall084e83d2011-03-24 11:26:52 +0000454 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
Aaron Ballman478faed2012-06-19 22:09:27 +0000455 SourceLocation(), 0, 0, AttributeList::AS_MSTypespec);
Eli Friedman53339e02009-06-08 23:27:34 +0000456 }
Steve Naroff3a9b7e02008-12-24 20:59:21 +0000457}
458
John McCall53fa7142010-12-24 02:08:15 +0000459void Parser::ParseBorlandTypeAttributes(ParsedAttributes &attrs) {
Dawn Perchik335e16b2010-09-03 01:29:35 +0000460 // Treat these like attributes
461 while (Tok.is(tok::kw___pascal)) {
462 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
463 SourceLocation AttrNameLoc = ConsumeToken();
John McCall084e83d2011-03-24 11:26:52 +0000464 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
Aaron Ballman478faed2012-06-19 22:09:27 +0000465 SourceLocation(), 0, 0, AttributeList::AS_MSTypespec);
Dawn Perchik335e16b2010-09-03 01:29:35 +0000466 }
John McCall53fa7142010-12-24 02:08:15 +0000467}
468
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +0000469void Parser::ParseOpenCLAttributes(ParsedAttributes &attrs) {
470 // Treat these like attributes
471 while (Tok.is(tok::kw___kernel)) {
472 SourceLocation AttrNameLoc = ConsumeToken();
John McCall084e83d2011-03-24 11:26:52 +0000473 attrs.addNew(PP.getIdentifierInfo("opencl_kernel_function"),
474 AttrNameLoc, 0, AttrNameLoc, 0,
Alexis Hunta0e54d42012-06-18 16:13:52 +0000475 SourceLocation(), 0, 0, AttributeList::AS_GNU);
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +0000476 }
477}
478
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000479void Parser::ParseOpenCLQualifiers(DeclSpec &DS) {
480 SourceLocation Loc = Tok.getLocation();
481 switch(Tok.getKind()) {
482 // OpenCL qualifiers:
483 case tok::kw___private:
Chad Rosierc1183952012-06-26 22:30:43 +0000484 case tok::kw_private:
John McCall084e83d2011-03-24 11:26:52 +0000485 DS.getAttributes().addNewInteger(
Chad Rosierc1183952012-06-26 22:30:43 +0000486 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000487 PP.getIdentifierInfo("address_space"), Loc, 0);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000488 break;
Chad Rosierc1183952012-06-26 22:30:43 +0000489
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000490 case tok::kw___global:
John McCall084e83d2011-03-24 11:26:52 +0000491 DS.getAttributes().addNewInteger(
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000492 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000493 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_global);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000494 break;
Chad Rosierc1183952012-06-26 22:30:43 +0000495
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000496 case tok::kw___local:
John McCall084e83d2011-03-24 11:26:52 +0000497 DS.getAttributes().addNewInteger(
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000498 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000499 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_local);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000500 break;
Chad Rosierc1183952012-06-26 22:30:43 +0000501
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000502 case tok::kw___constant:
John McCall084e83d2011-03-24 11:26:52 +0000503 DS.getAttributes().addNewInteger(
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000504 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000505 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_constant);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000506 break;
Chad Rosierc1183952012-06-26 22:30:43 +0000507
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000508 case tok::kw___read_only:
John McCall084e83d2011-03-24 11:26:52 +0000509 DS.getAttributes().addNewInteger(
Chad Rosierc1183952012-06-26 22:30:43 +0000510 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000511 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_read_only);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000512 break;
Chad Rosierc1183952012-06-26 22:30:43 +0000513
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000514 case tok::kw___write_only:
John McCall084e83d2011-03-24 11:26:52 +0000515 DS.getAttributes().addNewInteger(
Chad Rosierc1183952012-06-26 22:30:43 +0000516 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000517 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_write_only);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000518 break;
Chad Rosierc1183952012-06-26 22:30:43 +0000519
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000520 case tok::kw___read_write:
John McCall084e83d2011-03-24 11:26:52 +0000521 DS.getAttributes().addNewInteger(
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000522 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000523 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_read_write);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000524 break;
525 default: break;
526 }
527}
528
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000529/// \brief Parse a version number.
530///
531/// version:
532/// simple-integer
533/// simple-integer ',' simple-integer
534/// simple-integer ',' simple-integer ',' simple-integer
535VersionTuple Parser::ParseVersionTuple(SourceRange &Range) {
536 Range = Tok.getLocation();
537
538 if (!Tok.is(tok::numeric_constant)) {
539 Diag(Tok, diag::err_expected_version);
540 SkipUntil(tok::comma, tok::r_paren, true, true, true);
541 return VersionTuple();
542 }
543
544 // Parse the major (and possibly minor and subminor) versions, which
545 // are stored in the numeric constant. We utilize a quirk of the
546 // lexer, which is that it handles something like 1.2.3 as a single
547 // numeric constant, rather than two separate tokens.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000548 SmallString<512> Buffer;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000549 Buffer.resize(Tok.getLength()+1);
550 const char *ThisTokBegin = &Buffer[0];
551
552 // Get the spelling of the token, which eliminates trigraphs, etc.
553 bool Invalid = false;
554 unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin, &Invalid);
555 if (Invalid)
556 return VersionTuple();
557
558 // Parse the major version.
559 unsigned AfterMajor = 0;
560 unsigned Major = 0;
561 while (AfterMajor < ActualLength && isdigit(ThisTokBegin[AfterMajor])) {
562 Major = Major * 10 + ThisTokBegin[AfterMajor] - '0';
563 ++AfterMajor;
564 }
565
566 if (AfterMajor == 0) {
567 Diag(Tok, diag::err_expected_version);
568 SkipUntil(tok::comma, tok::r_paren, true, true, true);
569 return VersionTuple();
570 }
571
572 if (AfterMajor == ActualLength) {
573 ConsumeToken();
574
575 // We only had a single version component.
576 if (Major == 0) {
577 Diag(Tok, diag::err_zero_version);
578 return VersionTuple();
579 }
580
581 return VersionTuple(Major);
582 }
583
584 if (ThisTokBegin[AfterMajor] != '.' || (AfterMajor + 1 == ActualLength)) {
585 Diag(Tok, diag::err_expected_version);
586 SkipUntil(tok::comma, tok::r_paren, true, true, true);
587 return VersionTuple();
588 }
589
590 // Parse the minor version.
591 unsigned AfterMinor = AfterMajor + 1;
592 unsigned Minor = 0;
593 while (AfterMinor < ActualLength && isdigit(ThisTokBegin[AfterMinor])) {
594 Minor = Minor * 10 + ThisTokBegin[AfterMinor] - '0';
595 ++AfterMinor;
596 }
597
598 if (AfterMinor == ActualLength) {
599 ConsumeToken();
Chad Rosierc1183952012-06-26 22:30:43 +0000600
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000601 // We had major.minor.
602 if (Major == 0 && Minor == 0) {
603 Diag(Tok, diag::err_zero_version);
604 return VersionTuple();
605 }
606
Chad Rosierc1183952012-06-26 22:30:43 +0000607 return VersionTuple(Major, Minor);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000608 }
609
610 // If what follows is not a '.', we have a problem.
611 if (ThisTokBegin[AfterMinor] != '.') {
612 Diag(Tok, diag::err_expected_version);
613 SkipUntil(tok::comma, tok::r_paren, true, true, true);
Chad Rosierc1183952012-06-26 22:30:43 +0000614 return VersionTuple();
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000615 }
616
617 // Parse the subminor version.
618 unsigned AfterSubminor = AfterMinor + 1;
619 unsigned Subminor = 0;
620 while (AfterSubminor < ActualLength && isdigit(ThisTokBegin[AfterSubminor])) {
621 Subminor = Subminor * 10 + ThisTokBegin[AfterSubminor] - '0';
622 ++AfterSubminor;
623 }
624
625 if (AfterSubminor != ActualLength) {
626 Diag(Tok, diag::err_expected_version);
627 SkipUntil(tok::comma, tok::r_paren, true, true, true);
628 return VersionTuple();
629 }
630 ConsumeToken();
631 return VersionTuple(Major, Minor, Subminor);
632}
633
634/// \brief Parse the contents of the "availability" attribute.
635///
636/// availability-attribute:
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000637/// 'availability' '(' platform ',' version-arg-list, opt-message')'
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000638///
639/// platform:
640/// identifier
641///
642/// version-arg-list:
643/// version-arg
644/// version-arg ',' version-arg-list
645///
646/// version-arg:
647/// 'introduced' '=' version
648/// 'deprecated' '=' version
Douglas Gregorfdd417f2012-03-11 04:53:21 +0000649/// 'obsoleted' = version
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000650/// 'unavailable'
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000651/// opt-message:
652/// 'message' '=' <string>
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000653void Parser::ParseAvailabilityAttribute(IdentifierInfo &Availability,
654 SourceLocation AvailabilityLoc,
655 ParsedAttributes &attrs,
656 SourceLocation *endLoc) {
657 SourceLocation PlatformLoc;
658 IdentifierInfo *Platform = 0;
659
660 enum { Introduced, Deprecated, Obsoleted, Unknown };
661 AvailabilityChange Changes[Unknown];
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000662 ExprResult MessageExpr;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000663
664 // Opening '('.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000665 BalancedDelimiterTracker T(*this, tok::l_paren);
666 if (T.consumeOpen()) {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000667 Diag(Tok, diag::err_expected_lparen);
668 return;
669 }
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000670
671 // Parse the platform name,
672 if (Tok.isNot(tok::identifier)) {
673 Diag(Tok, diag::err_availability_expected_platform);
674 SkipUntil(tok::r_paren);
675 return;
676 }
677 Platform = Tok.getIdentifierInfo();
678 PlatformLoc = ConsumeToken();
679
680 // Parse the ',' following the platform name.
681 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "", tok::r_paren))
682 return;
683
684 // If we haven't grabbed the pointers for the identifiers
685 // "introduced", "deprecated", and "obsoleted", do so now.
686 if (!Ident_introduced) {
687 Ident_introduced = PP.getIdentifierInfo("introduced");
688 Ident_deprecated = PP.getIdentifierInfo("deprecated");
689 Ident_obsoleted = PP.getIdentifierInfo("obsoleted");
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000690 Ident_unavailable = PP.getIdentifierInfo("unavailable");
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000691 Ident_message = PP.getIdentifierInfo("message");
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000692 }
693
694 // Parse the set of introductions/deprecations/removals.
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000695 SourceLocation UnavailableLoc;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000696 do {
697 if (Tok.isNot(tok::identifier)) {
698 Diag(Tok, diag::err_availability_expected_change);
699 SkipUntil(tok::r_paren);
700 return;
701 }
702 IdentifierInfo *Keyword = Tok.getIdentifierInfo();
703 SourceLocation KeywordLoc = ConsumeToken();
704
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000705 if (Keyword == Ident_unavailable) {
706 if (UnavailableLoc.isValid()) {
707 Diag(KeywordLoc, diag::err_availability_redundant)
708 << Keyword << SourceRange(UnavailableLoc);
Chad Rosierc1183952012-06-26 22:30:43 +0000709 }
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000710 UnavailableLoc = KeywordLoc;
711
712 if (Tok.isNot(tok::comma))
713 break;
714
715 ConsumeToken();
716 continue;
Chad Rosierc1183952012-06-26 22:30:43 +0000717 }
718
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000719 if (Tok.isNot(tok::equal)) {
720 Diag(Tok, diag::err_expected_equal_after)
721 << Keyword;
722 SkipUntil(tok::r_paren);
723 return;
724 }
725 ConsumeToken();
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000726 if (Keyword == Ident_message) {
727 if (!isTokenStringLiteral()) {
728 Diag(Tok, diag::err_expected_string_literal);
729 SkipUntil(tok::r_paren);
730 return;
731 }
732 MessageExpr = ParseStringLiteralExpression();
733 break;
734 }
Chad Rosierc1183952012-06-26 22:30:43 +0000735
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000736 SourceRange VersionRange;
737 VersionTuple Version = ParseVersionTuple(VersionRange);
Chad Rosierc1183952012-06-26 22:30:43 +0000738
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000739 if (Version.empty()) {
740 SkipUntil(tok::r_paren);
741 return;
742 }
743
744 unsigned Index;
745 if (Keyword == Ident_introduced)
746 Index = Introduced;
747 else if (Keyword == Ident_deprecated)
748 Index = Deprecated;
749 else if (Keyword == Ident_obsoleted)
750 Index = Obsoleted;
Chad Rosierc1183952012-06-26 22:30:43 +0000751 else
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000752 Index = Unknown;
753
754 if (Index < Unknown) {
755 if (!Changes[Index].KeywordLoc.isInvalid()) {
756 Diag(KeywordLoc, diag::err_availability_redundant)
Chad Rosierc1183952012-06-26 22:30:43 +0000757 << Keyword
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000758 << SourceRange(Changes[Index].KeywordLoc,
759 Changes[Index].VersionRange.getEnd());
760 }
761
762 Changes[Index].KeywordLoc = KeywordLoc;
763 Changes[Index].Version = Version;
764 Changes[Index].VersionRange = VersionRange;
765 } else {
766 Diag(KeywordLoc, diag::err_availability_unknown_change)
767 << Keyword << VersionRange;
768 }
769
770 if (Tok.isNot(tok::comma))
771 break;
772
773 ConsumeToken();
774 } while (true);
775
776 // Closing ')'.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000777 if (T.consumeClose())
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000778 return;
779
780 if (endLoc)
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000781 *endLoc = T.getCloseLocation();
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000782
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000783 // The 'unavailable' availability cannot be combined with any other
784 // availability changes. Make sure that hasn't happened.
785 if (UnavailableLoc.isValid()) {
786 bool Complained = false;
787 for (unsigned Index = Introduced; Index != Unknown; ++Index) {
788 if (Changes[Index].KeywordLoc.isValid()) {
789 if (!Complained) {
790 Diag(UnavailableLoc, diag::warn_availability_and_unavailable)
791 << SourceRange(Changes[Index].KeywordLoc,
792 Changes[Index].VersionRange.getEnd());
793 Complained = true;
794 }
795
796 // Clear out the availability.
797 Changes[Index] = AvailabilityChange();
798 }
799 }
800 }
801
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000802 // Record this attribute
Chad Rosierc1183952012-06-26 22:30:43 +0000803 attrs.addNew(&Availability,
804 SourceRange(AvailabilityLoc, T.getCloseLocation()),
Fariborz Jahanian586be882012-01-23 23:38:32 +0000805 0, AvailabilityLoc,
John McCall084e83d2011-03-24 11:26:52 +0000806 Platform, PlatformLoc,
807 Changes[Introduced],
808 Changes[Deprecated],
Chad Rosierc1183952012-06-26 22:30:43 +0000809 Changes[Obsoleted],
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000810 UnavailableLoc, MessageExpr.take(),
Alexis Hunta0e54d42012-06-18 16:13:52 +0000811 AttributeList::AS_GNU);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000812}
813
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000814
815// Late Parsed Attributes:
816// See other examples of late parsing in lib/Parse/ParseCXXInlineMethods
817
818void Parser::LateParsedDeclaration::ParseLexedAttributes() {}
819
820void Parser::LateParsedClass::ParseLexedAttributes() {
821 Self->ParseLexedAttributes(*Class);
822}
823
824void Parser::LateParsedAttribute::ParseLexedAttributes() {
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +0000825 Self->ParseLexedAttribute(*this, true, false);
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000826}
827
828/// Wrapper class which calls ParseLexedAttribute, after setting up the
829/// scope appropriately.
830void Parser::ParseLexedAttributes(ParsingClass &Class) {
831 // Deal with templates
832 // FIXME: Test cases to make sure this does the right thing for templates.
833 bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope;
834 ParseScope ClassTemplateScope(this, Scope::TemplateParamScope,
835 HasTemplateScope);
836 if (HasTemplateScope)
837 Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate);
838
Douglas Gregor3024f072012-04-16 07:05:22 +0000839 // Set or update the scope flags.
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000840 bool AlreadyHasClassScope = Class.TopLevelClass;
Douglas Gregor3024f072012-04-16 07:05:22 +0000841 unsigned ScopeFlags = Scope::ClassScope|Scope::DeclScope;
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000842 ParseScope ClassScope(this, ScopeFlags, !AlreadyHasClassScope);
843 ParseScopeFlags ClassScopeFlags(this, ScopeFlags, AlreadyHasClassScope);
844
DeLesley Hutchins6f860042012-04-06 15:10:17 +0000845 // Enter the scope of nested classes
846 if (!AlreadyHasClassScope)
847 Actions.ActOnStartDelayedMemberDeclarations(getCurScope(),
848 Class.TagOrTemplate);
Benjamin Kramer1d373c62012-05-17 12:01:52 +0000849 if (!Class.LateParsedDeclarations.empty()) {
Douglas Gregor3024f072012-04-16 07:05:22 +0000850 // Allow 'this' within late-parsed attributes.
Chad Rosierc1183952012-06-26 22:30:43 +0000851 Sema::CXXThisScopeRAII ThisScope(Actions, Class.TagOrTemplate,
Douglas Gregor3024f072012-04-16 07:05:22 +0000852 /*TypeQuals=*/0);
Chad Rosierc1183952012-06-26 22:30:43 +0000853
Douglas Gregor3024f072012-04-16 07:05:22 +0000854 for (unsigned i = 0, ni = Class.LateParsedDeclarations.size(); i < ni; ++i){
855 Class.LateParsedDeclarations[i]->ParseLexedAttributes();
856 }
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000857 }
Chad Rosierc1183952012-06-26 22:30:43 +0000858
DeLesley Hutchins6f860042012-04-06 15:10:17 +0000859 if (!AlreadyHasClassScope)
860 Actions.ActOnFinishDelayedMemberDeclarations(getCurScope(),
861 Class.TagOrTemplate);
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000862}
863
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +0000864
865/// \brief Parse all attributes in LAs, and attach them to Decl D.
866void Parser::ParseLexedAttributeList(LateParsedAttrList &LAs, Decl *D,
867 bool EnterScope, bool OnDefinition) {
868 for (unsigned i = 0, ni = LAs.size(); i < ni; ++i) {
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +0000869 LAs[i]->addDecl(D);
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +0000870 ParseLexedAttribute(*LAs[i], EnterScope, OnDefinition);
Benjamin Kramerbafc49a2012-04-14 12:44:47 +0000871 delete LAs[i];
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +0000872 }
873 LAs.clear();
874}
875
876
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000877/// \brief Finish parsing an attribute for which parsing was delayed.
878/// This will be called at the end of parsing a class declaration
879/// for each LateParsedAttribute. We consume the saved tokens and
Chad Rosierc1183952012-06-26 22:30:43 +0000880/// create an attribute with the arguments filled in. We add this
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000881/// to the Attribute list for the decl.
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +0000882void Parser::ParseLexedAttribute(LateParsedAttribute &LA,
883 bool EnterScope, bool OnDefinition) {
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000884 // Save the current token position.
885 SourceLocation OrigLoc = Tok.getLocation();
886
887 // Append the current token at the end of the new token stream so that it
888 // doesn't get lost.
889 LA.Toks.push_back(Tok);
890 PP.EnterTokenStream(LA.Toks.data(), LA.Toks.size(), true, false);
891 // Consume the previously pushed token.
892 ConsumeAnyToken();
893
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +0000894 if (OnDefinition && !IsThreadSafetyAttribute(LA.AttrName.getName())) {
895 Diag(Tok, diag::warn_attribute_on_function_definition)
896 << LA.AttrName.getName();
897 }
898
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000899 ParsedAttributes Attrs(AttrFactory);
900 SourceLocation endLoc;
901
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +0000902 if (LA.Decls.size() == 1) {
903 Decl *D = LA.Decls[0];
Caitlin Sadowski990d5712011-09-08 17:42:31 +0000904
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +0000905 // If the Decl is templatized, add template parameters to scope.
906 bool HasTemplateScope = EnterScope && D->isTemplateDecl();
907 ParseScope TempScope(this, Scope::TemplateParamScope, HasTemplateScope);
908 if (HasTemplateScope)
909 Actions.ActOnReenterTemplateScope(Actions.CurScope, D);
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000910
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +0000911 // If the Decl is on a function, add function parameters to the scope.
912 bool HasFunctionScope = EnterScope && D->isFunctionOrFunctionTemplate();
913 ParseScope FnScope(this, Scope::FnScope|Scope::DeclScope, HasFunctionScope);
914 if (HasFunctionScope)
915 Actions.ActOnReenterFunctionContext(Actions.CurScope, D);
916
917 ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, &endLoc);
918
919 if (HasFunctionScope) {
920 Actions.ActOnExitFunctionContext();
921 FnScope.Exit(); // Pop scope, and remove Decls from IdResolver
922 }
923 if (HasTemplateScope) {
924 TempScope.Exit();
925 }
DeLesley Hutchins71d61032012-03-02 22:29:50 +0000926 } else if (LA.Decls.size() > 0) {
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +0000927 // If there are multiple decls, then the decl cannot be within the
928 // function scope.
929 ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, &endLoc);
DeLesley Hutchins71d61032012-03-02 22:29:50 +0000930 } else {
931 Diag(Tok, diag::warn_attribute_no_decl) << LA.AttrName.getName();
Caitlin Sadowski990d5712011-09-08 17:42:31 +0000932 }
933
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +0000934 for (unsigned i = 0, ni = LA.Decls.size(); i < ni; ++i) {
935 Actions.ActOnFinishDelayedAttribute(getCurScope(), LA.Decls[i], Attrs);
936 }
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000937
938 if (Tok.getLocation() != OrigLoc) {
939 // Due to a parsing error, we either went over the cached tokens or
940 // there are still cached tokens left, so we skip the leftover tokens.
941 // Since this is an uncommon situation that should be avoided, use the
942 // expensive isBeforeInTranslationUnit call.
943 if (PP.getSourceManager().isBeforeInTranslationUnit(Tok.getLocation(),
944 OrigLoc))
945 while (Tok.getLocation() != OrigLoc && Tok.isNot(tok::eof))
Douglas Gregor0cf55e92012-03-08 01:00:17 +0000946 ConsumeAnyToken();
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000947 }
948}
949
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000950/// \brief Wrapper around a case statement checking if AttrName is
951/// one of the thread safety attributes
952bool Parser::IsThreadSafetyAttribute(llvm::StringRef AttrName){
953 return llvm::StringSwitch<bool>(AttrName)
954 .Case("guarded_by", true)
955 .Case("guarded_var", true)
956 .Case("pt_guarded_by", true)
957 .Case("pt_guarded_var", true)
958 .Case("lockable", true)
959 .Case("scoped_lockable", true)
960 .Case("no_thread_safety_analysis", true)
961 .Case("acquired_after", true)
962 .Case("acquired_before", true)
963 .Case("exclusive_lock_function", true)
964 .Case("shared_lock_function", true)
965 .Case("exclusive_trylock_function", true)
966 .Case("shared_trylock_function", true)
967 .Case("unlock_function", true)
968 .Case("lock_returned", true)
969 .Case("locks_excluded", true)
970 .Case("exclusive_locks_required", true)
971 .Case("shared_locks_required", true)
972 .Default(false);
973}
974
975/// \brief Parse the contents of thread safety attributes. These
976/// should always be parsed as an expression list.
977///
978/// We need to special case the parsing due to the fact that if the first token
979/// of the first argument is an identifier, the main parse loop will store
980/// that token as a "parameter" and the rest of
981/// the arguments will be added to a list of "arguments". However,
982/// subsequent tokens in the first argument are lost. We instead parse each
983/// argument as an expression and add all arguments to the list of "arguments".
984/// In future, we will take advantage of this special case to also
985/// deal with some argument scoping issues here (for example, referring to a
986/// function parameter in the attribute on that function).
987void Parser::ParseThreadSafetyAttribute(IdentifierInfo &AttrName,
988 SourceLocation AttrNameLoc,
989 ParsedAttributes &Attrs,
990 SourceLocation *EndLoc) {
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000991 assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('");
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000992
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000993 BalancedDelimiterTracker T(*this, tok::l_paren);
994 T.consumeOpen();
Chad Rosierc1183952012-06-26 22:30:43 +0000995
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000996 ExprVector ArgExprs(Actions);
997 bool ArgExprsOk = true;
Chad Rosierc1183952012-06-26 22:30:43 +0000998
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000999 // now parse the list of expressions
DeLesley Hutchins36f5d852011-12-14 19:36:06 +00001000 while (Tok.isNot(tok::r_paren)) {
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001001 ExprResult ArgExpr(ParseAssignmentExpression());
1002 if (ArgExpr.isInvalid()) {
1003 ArgExprsOk = false;
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001004 T.consumeClose();
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001005 break;
1006 } else {
1007 ArgExprs.push_back(ArgExpr.release());
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +00001008 }
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001009 if (Tok.isNot(tok::comma))
1010 break;
1011 ConsumeToken(); // Eat the comma, move to the next argument
1012 }
1013 // Match the ')'.
DeLesley Hutchins30398dd2012-01-20 22:50:54 +00001014 if (ArgExprsOk && !T.consumeClose()) {
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001015 Attrs.addNew(&AttrName, AttrNameLoc, 0, AttrNameLoc, 0, SourceLocation(),
Alexis Hunta0e54d42012-06-18 16:13:52 +00001016 ArgExprs.take(), ArgExprs.size(), AttributeList::AS_GNU);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +00001017 }
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001018 if (EndLoc)
1019 *EndLoc = T.getCloseLocation();
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +00001020}
1021
Richard Smith7bdcc4a2012-04-10 01:32:12 +00001022/// DiagnoseProhibitedCXX11Attribute - We have found the opening square brackets
1023/// of a C++11 attribute-specifier in a location where an attribute is not
1024/// permitted. By C++11 [dcl.attr.grammar]p6, this is ill-formed. Diagnose this
1025/// situation.
1026///
1027/// \return \c true if we skipped an attribute-like chunk of tokens, \c false if
1028/// this doesn't appear to actually be an attribute-specifier, and the caller
1029/// should try to parse it.
1030bool Parser::DiagnoseProhibitedCXX11Attribute() {
1031 assert(Tok.is(tok::l_square) && NextToken().is(tok::l_square));
1032
1033 switch (isCXX11AttributeSpecifier(/*Disambiguate*/true)) {
1034 case CAK_NotAttributeSpecifier:
1035 // No diagnostic: we're in Obj-C++11 and this is not actually an attribute.
1036 return false;
1037
1038 case CAK_InvalidAttributeSpecifier:
1039 Diag(Tok.getLocation(), diag::err_l_square_l_square_not_attribute);
1040 return false;
1041
1042 case CAK_AttributeSpecifier:
1043 // Parse and discard the attributes.
1044 SourceLocation BeginLoc = ConsumeBracket();
1045 ConsumeBracket();
1046 SkipUntil(tok::r_square, /*StopAtSemi*/ false);
1047 assert(Tok.is(tok::r_square) && "isCXX11AttributeSpecifier lied");
1048 SourceLocation EndLoc = ConsumeBracket();
1049 Diag(BeginLoc, diag::err_attributes_not_allowed)
1050 << SourceRange(BeginLoc, EndLoc);
1051 return true;
1052 }
Chandler Carruthd8f7d382012-04-10 16:03:08 +00001053 llvm_unreachable("All cases handled above.");
Richard Smith7bdcc4a2012-04-10 01:32:12 +00001054}
1055
John McCall53fa7142010-12-24 02:08:15 +00001056void Parser::DiagnoseProhibitedAttributes(ParsedAttributesWithRange &attrs) {
1057 Diag(attrs.Range.getBegin(), diag::err_attributes_not_allowed)
1058 << attrs.Range;
Dawn Perchik335e16b2010-09-03 01:29:35 +00001059}
1060
Chris Lattner53361ac2006-08-10 05:19:57 +00001061/// ParseDeclaration - Parse a full 'declaration', which consists of
1062/// declaration-specifiers, some number of declarators, and a semicolon.
Chris Lattner49836b42009-04-02 04:16:50 +00001063/// 'Context' should be a Declarator::TheContext value. This returns the
1064/// location of the semicolon in DeclEnd.
Chris Lattnera5235172007-08-25 06:57:03 +00001065///
1066/// declaration: [C99 6.7]
1067/// block-declaration ->
1068/// simple-declaration
1069/// others [FIXME]
Douglas Gregoreb31f392008-12-01 23:54:00 +00001070/// [C++] template-declaration
Chris Lattnera5235172007-08-25 06:57:03 +00001071/// [C++] namespace-definition
Douglas Gregord7c4d982008-12-30 03:27:21 +00001072/// [C++] using-directive
Douglas Gregor77b50e12009-06-22 23:06:13 +00001073/// [C++] using-declaration
Richard Smithc202b282012-04-14 00:33:13 +00001074/// [C++11/C11] static_assert-declaration
Chris Lattnera5235172007-08-25 06:57:03 +00001075/// others... [FIXME]
1076///
Fariborz Jahanian1db5c942010-09-28 20:42:35 +00001077Parser::DeclGroupPtrTy Parser::ParseDeclaration(StmtVector &Stmts,
1078 unsigned Context,
Alexis Hunt96d5c762009-11-21 08:43:09 +00001079 SourceLocation &DeclEnd,
John McCall53fa7142010-12-24 02:08:15 +00001080 ParsedAttributesWithRange &attrs) {
Argyrios Kyrtzidis355094e2010-06-17 10:52:18 +00001081 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Fariborz Jahanian59b75282011-08-30 17:10:52 +00001082 // Must temporarily exit the objective-c container scope for
1083 // parsing c none objective-c decls.
1084 ObjCDeclContextSwitch ObjCDC(*this);
Chad Rosierc1183952012-06-26 22:30:43 +00001085
John McCall48871652010-08-21 09:40:31 +00001086 Decl *SingleDecl = 0;
Richard Smithcd1c0552011-07-01 19:46:12 +00001087 Decl *OwnedType = 0;
Chris Lattnera5235172007-08-25 06:57:03 +00001088 switch (Tok.getKind()) {
Douglas Gregoreb31f392008-12-01 23:54:00 +00001089 case tok::kw_template:
Douglas Gregor23996282009-05-12 21:31:51 +00001090 case tok::kw_export:
John McCall53fa7142010-12-24 02:08:15 +00001091 ProhibitAttributes(attrs);
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001092 SingleDecl = ParseDeclarationStartingWithTemplate(Context, DeclEnd);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001093 break;
Sebastian Redl67667942010-08-27 23:12:46 +00001094 case tok::kw_inline:
Sebastian Redl5a5f2c72010-08-31 00:36:45 +00001095 // Could be the start of an inline namespace. Allowed as an ext in C++03.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001096 if (getLangOpts().CPlusPlus && NextToken().is(tok::kw_namespace)) {
John McCall53fa7142010-12-24 02:08:15 +00001097 ProhibitAttributes(attrs);
Sebastian Redl67667942010-08-27 23:12:46 +00001098 SourceLocation InlineLoc = ConsumeToken();
1099 SingleDecl = ParseNamespace(Context, DeclEnd, InlineLoc);
1100 break;
1101 }
Chad Rosierc1183952012-06-26 22:30:43 +00001102 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs,
Fariborz Jahanian1db5c942010-09-28 20:42:35 +00001103 true);
Chris Lattnera5235172007-08-25 06:57:03 +00001104 case tok::kw_namespace:
John McCall53fa7142010-12-24 02:08:15 +00001105 ProhibitAttributes(attrs);
Chris Lattner49836b42009-04-02 04:16:50 +00001106 SingleDecl = ParseNamespace(Context, DeclEnd);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001107 break;
Douglas Gregord7c4d982008-12-30 03:27:21 +00001108 case tok::kw_using:
John McCall9b72f892010-11-10 02:40:36 +00001109 SingleDecl = ParseUsingDirectiveOrDeclaration(Context, ParsedTemplateInfo(),
Richard Smithcd1c0552011-07-01 19:46:12 +00001110 DeclEnd, attrs, &OwnedType);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001111 break;
Anders Carlssonf24fcff62009-03-11 16:27:10 +00001112 case tok::kw_static_assert:
Peter Collingbourne3d9cbdc2011-04-15 00:35:57 +00001113 case tok::kw__Static_assert:
John McCall53fa7142010-12-24 02:08:15 +00001114 ProhibitAttributes(attrs);
Chris Lattner49836b42009-04-02 04:16:50 +00001115 SingleDecl = ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001116 break;
Chris Lattnera5235172007-08-25 06:57:03 +00001117 default:
John McCall53fa7142010-12-24 02:08:15 +00001118 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs, true);
Chris Lattnera5235172007-08-25 06:57:03 +00001119 }
Chad Rosierc1183952012-06-26 22:30:43 +00001120
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001121 // This routine returns a DeclGroup, if the thing we parsed only contains a
Richard Smithcd1c0552011-07-01 19:46:12 +00001122 // single decl, convert it now. Alias declarations can also declare a type;
1123 // include that too if it is present.
1124 return Actions.ConvertDeclToDeclGroup(SingleDecl, OwnedType);
Chris Lattnera5235172007-08-25 06:57:03 +00001125}
1126
1127/// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl]
1128/// declaration-specifiers init-declarator-list[opt] ';'
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001129/// [C++11] attribute-specifier-seq decl-specifier-seq[opt]
1130/// init-declarator-list ';'
Chris Lattnera5235172007-08-25 06:57:03 +00001131///[C90/C++]init-declarator-list ';' [TODO]
1132/// [OMP] threadprivate-directive [TODO]
Chris Lattner32dc41c2009-03-29 17:27:48 +00001133///
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001134/// for-range-declaration: [C++11 6.5p1: stmt.ranged]
Richard Smith02e85f32011-04-14 22:09:26 +00001135/// attribute-specifier-seq[opt] type-specifier-seq declarator
1136///
Chris Lattner32dc41c2009-03-29 17:27:48 +00001137/// If RequireSemi is false, this does not check for a ';' at the end of the
Chris Lattner005fc1b2010-04-05 18:18:31 +00001138/// declaration. If it is true, it checks for and eats it.
Richard Smith02e85f32011-04-14 22:09:26 +00001139///
1140/// If FRI is non-null, we might be parsing a for-range-declaration instead
1141/// of a simple-declaration. If we find that we are, we also parse the
1142/// for-range-initializer, and place it here.
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001143Parser::DeclGroupPtrTy
1144Parser::ParseSimpleDeclaration(StmtVector &Stmts, unsigned Context,
1145 SourceLocation &DeclEnd,
1146 ParsedAttributesWithRange &attrs,
1147 bool RequireSemi, ForRangeInit *FRI) {
Chris Lattner53361ac2006-08-10 05:19:57 +00001148 // Parse the common declaration-specifiers piece.
John McCall28a6aea2009-11-04 02:18:39 +00001149 ParsingDeclSpec DS(*this);
John McCall53fa7142010-12-24 02:08:15 +00001150 DS.takeAttributesFrom(attrs);
Douglas Gregor0e7dde52011-04-24 05:37:28 +00001151
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001152 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS_none,
Richard Smith30482bc2011-02-20 03:19:35 +00001153 getDeclSpecContextFromDeclaratorContext(Context));
Abramo Bagnara1cd83682012-01-07 10:52:36 +00001154
Chris Lattner0e894622006-08-13 19:58:17 +00001155 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
1156 // declaration-specifiers init-declarator-list[opt] ';'
Chris Lattner76c72282007-10-09 17:33:22 +00001157 if (Tok.is(tok::semi)) {
Argyrios Kyrtzidisfbb2bb52012-05-16 23:49:15 +00001158 DeclEnd = Tok.getLocation();
Chris Lattner005fc1b2010-04-05 18:18:31 +00001159 if (RequireSemi) ConsumeToken();
John McCall48871652010-08-21 09:40:31 +00001160 Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none,
Douglas Gregor0e7dde52011-04-24 05:37:28 +00001161 DS);
John McCall28a6aea2009-11-04 02:18:39 +00001162 DS.complete(TheDecl);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001163 return Actions.ConvertDeclToDeclGroup(TheDecl);
Chris Lattner0e894622006-08-13 19:58:17 +00001164 }
Chad Rosierc1183952012-06-26 22:30:43 +00001165
1166 return ParseDeclGroup(DS, Context, /*FunctionDefs=*/ false, &DeclEnd, FRI);
John McCalld5a36322009-11-03 19:26:08 +00001167}
Mike Stump11289f42009-09-09 15:08:12 +00001168
Richard Smith09f76ee2011-10-19 21:33:05 +00001169/// Returns true if this might be the start of a declarator, or a common typo
1170/// for a declarator.
1171bool Parser::MightBeDeclarator(unsigned Context) {
1172 switch (Tok.getKind()) {
1173 case tok::annot_cxxscope:
1174 case tok::annot_template_id:
1175 case tok::caret:
1176 case tok::code_completion:
1177 case tok::coloncolon:
1178 case tok::ellipsis:
1179 case tok::kw___attribute:
1180 case tok::kw_operator:
1181 case tok::l_paren:
1182 case tok::star:
1183 return true;
1184
1185 case tok::amp:
1186 case tok::ampamp:
David Blaikiebbafb8a2012-03-11 07:00:24 +00001187 return getLangOpts().CPlusPlus;
Richard Smith09f76ee2011-10-19 21:33:05 +00001188
Richard Smithc8a79032012-01-09 22:31:44 +00001189 case tok::l_square: // Might be an attribute on an unnamed bit-field.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001190 return Context == Declarator::MemberContext && getLangOpts().CPlusPlus0x &&
Richard Smithc8a79032012-01-09 22:31:44 +00001191 NextToken().is(tok::l_square);
1192
1193 case tok::colon: // Might be a typo for '::' or an unnamed bit-field.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001194 return Context == Declarator::MemberContext || getLangOpts().CPlusPlus;
Richard Smithc8a79032012-01-09 22:31:44 +00001195
Richard Smith09f76ee2011-10-19 21:33:05 +00001196 case tok::identifier:
1197 switch (NextToken().getKind()) {
1198 case tok::code_completion:
1199 case tok::coloncolon:
1200 case tok::comma:
1201 case tok::equal:
1202 case tok::equalequal: // Might be a typo for '='.
1203 case tok::kw_alignas:
1204 case tok::kw_asm:
1205 case tok::kw___attribute:
1206 case tok::l_brace:
1207 case tok::l_paren:
1208 case tok::l_square:
1209 case tok::less:
1210 case tok::r_brace:
1211 case tok::r_paren:
1212 case tok::r_square:
1213 case tok::semi:
1214 return true;
1215
1216 case tok::colon:
1217 // At namespace scope, 'identifier:' is probably a typo for 'identifier::'
Richard Smithc8a79032012-01-09 22:31:44 +00001218 // and in block scope it's probably a label. Inside a class definition,
1219 // this is a bit-field.
1220 return Context == Declarator::MemberContext ||
David Blaikiebbafb8a2012-03-11 07:00:24 +00001221 (getLangOpts().CPlusPlus && Context == Declarator::FileContext);
Richard Smithc8a79032012-01-09 22:31:44 +00001222
1223 case tok::identifier: // Possible virt-specifier.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001224 return getLangOpts().CPlusPlus0x && isCXX0XVirtSpecifier(NextToken());
Richard Smith09f76ee2011-10-19 21:33:05 +00001225
1226 default:
1227 return false;
1228 }
1229
1230 default:
1231 return false;
1232 }
1233}
1234
Richard Smithb8caac82012-04-11 20:59:20 +00001235/// Skip until we reach something which seems like a sensible place to pick
1236/// up parsing after a malformed declaration. This will sometimes stop sooner
1237/// than SkipUntil(tok::r_brace) would, but will never stop later.
1238void Parser::SkipMalformedDecl() {
1239 while (true) {
1240 switch (Tok.getKind()) {
1241 case tok::l_brace:
1242 // Skip until matching }, then stop. We've probably skipped over
1243 // a malformed class or function definition or similar.
1244 ConsumeBrace();
1245 SkipUntil(tok::r_brace, /*StopAtSemi*/false);
1246 if (Tok.is(tok::comma) || Tok.is(tok::l_brace) || Tok.is(tok::kw_try)) {
1247 // This declaration isn't over yet. Keep skipping.
1248 continue;
1249 }
1250 if (Tok.is(tok::semi))
1251 ConsumeToken();
1252 return;
1253
1254 case tok::l_square:
1255 ConsumeBracket();
1256 SkipUntil(tok::r_square, /*StopAtSemi*/false);
1257 continue;
1258
1259 case tok::l_paren:
1260 ConsumeParen();
1261 SkipUntil(tok::r_paren, /*StopAtSemi*/false);
1262 continue;
1263
1264 case tok::r_brace:
1265 return;
1266
1267 case tok::semi:
1268 ConsumeToken();
1269 return;
1270
1271 case tok::kw_inline:
1272 // 'inline namespace' at the start of a line is almost certainly
1273 // a good place to pick back up parsing.
1274 if (Tok.isAtStartOfLine() && NextToken().is(tok::kw_namespace))
1275 return;
1276 break;
1277
1278 case tok::kw_namespace:
1279 // 'namespace' at the start of a line is almost certainly a good
1280 // place to pick back up parsing.
1281 if (Tok.isAtStartOfLine())
1282 return;
1283 break;
1284
1285 case tok::eof:
1286 return;
1287
1288 default:
1289 break;
1290 }
1291
1292 ConsumeAnyToken();
1293 }
1294}
1295
John McCalld5a36322009-11-03 19:26:08 +00001296/// ParseDeclGroup - Having concluded that this is either a function
1297/// definition or a group of object declarations, actually parse the
1298/// result.
John McCall28a6aea2009-11-04 02:18:39 +00001299Parser::DeclGroupPtrTy Parser::ParseDeclGroup(ParsingDeclSpec &DS,
1300 unsigned Context,
John McCalld5a36322009-11-03 19:26:08 +00001301 bool AllowFunctionDefinitions,
Richard Smith02e85f32011-04-14 22:09:26 +00001302 SourceLocation *DeclEnd,
1303 ForRangeInit *FRI) {
John McCalld5a36322009-11-03 19:26:08 +00001304 // Parse the first declarator.
John McCall28a6aea2009-11-04 02:18:39 +00001305 ParsingDeclarator D(*this, DS, static_cast<Declarator::TheContext>(Context));
John McCalld5a36322009-11-03 19:26:08 +00001306 ParseDeclarator(D);
Chris Lattner32dc41c2009-03-29 17:27:48 +00001307
John McCalld5a36322009-11-03 19:26:08 +00001308 // Bail out if the first declarator didn't seem well-formed.
1309 if (!D.hasName() && !D.mayOmitIdentifier()) {
Richard Smithb8caac82012-04-11 20:59:20 +00001310 SkipMalformedDecl();
John McCalld5a36322009-11-03 19:26:08 +00001311 return DeclGroupPtrTy();
Chris Lattnerefb0f112009-03-29 17:18:04 +00001312 }
Mike Stump11289f42009-09-09 15:08:12 +00001313
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001314 // Save late-parsed attributes for now; they need to be parsed in the
1315 // appropriate function scope after the function Decl has been constructed.
1316 LateParsedAttrList LateParsedAttrs;
1317 if (D.isFunctionDeclarator())
1318 MaybeParseGNUAttributes(D, &LateParsedAttrs);
1319
Chris Lattnerdbb1e932010-07-11 22:24:20 +00001320 // Check to see if we have a function *definition* which must have a body.
1321 if (AllowFunctionDefinitions && D.isFunctionDeclarator() &&
1322 // Look at the next token to make sure that this isn't a function
1323 // declaration. We have to check this because __attribute__ might be the
1324 // start of a function definition in GCC-extended K&R C.
Fariborz Jahanian8de79552012-07-05 19:34:20 +00001325 !isDeclarationAfterDeclarator() &&
1326 (!CurParsedObjCImpl || Tok.isNot(tok::l_brace) ||
1327 (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()))) {
Chad Rosierc1183952012-06-26 22:30:43 +00001328
Chris Lattner13901342010-07-11 22:42:07 +00001329 if (isStartOfFunctionDefinition(D)) {
John McCalld5a36322009-11-03 19:26:08 +00001330 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
1331 Diag(Tok, diag::err_function_declared_typedef);
1332
1333 // Recover by treating the 'typedef' as spurious.
1334 DS.ClearStorageClassSpecs();
1335 }
1336
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001337 Decl *TheDecl =
1338 ParseFunctionDefinition(D, ParsedTemplateInfo(), &LateParsedAttrs);
John McCalld5a36322009-11-03 19:26:08 +00001339 return Actions.ConvertDeclToDeclGroup(TheDecl);
Chris Lattner13901342010-07-11 22:42:07 +00001340 }
Chad Rosierc1183952012-06-26 22:30:43 +00001341
Chris Lattner13901342010-07-11 22:42:07 +00001342 if (isDeclarationSpecifier()) {
1343 // If there is an invalid declaration specifier right after the function
1344 // prototype, then we must be in a missing semicolon case where this isn't
1345 // actually a body. Just fall through into the code that handles it as a
1346 // prototype, and let the top-level code handle the erroneous declspec
1347 // where it would otherwise expect a comma or semicolon.
John McCalld5a36322009-11-03 19:26:08 +00001348 } else {
1349 Diag(Tok, diag::err_expected_fn_body);
1350 SkipUntil(tok::semi);
1351 return DeclGroupPtrTy();
1352 }
1353 }
1354
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001355 if (ParseAsmAttributesAfterDeclarator(D))
Richard Smith02e85f32011-04-14 22:09:26 +00001356 return DeclGroupPtrTy();
1357
1358 // C++0x [stmt.iter]p1: Check if we have a for-range-declarator. If so, we
1359 // must parse and analyze the for-range-initializer before the declaration is
1360 // analyzed.
1361 if (FRI && Tok.is(tok::colon)) {
1362 FRI->ColonLoc = ConsumeToken();
Sebastian Redl3da34892011-06-05 12:23:16 +00001363 if (Tok.is(tok::l_brace))
1364 FRI->RangeExpr = ParseBraceInitializer();
1365 else
1366 FRI->RangeExpr = ParseExpression();
Richard Smith02e85f32011-04-14 22:09:26 +00001367 Decl *ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
1368 Actions.ActOnCXXForRangeDecl(ThisDecl);
1369 Actions.FinalizeDeclaration(ThisDecl);
John McCallcf6e0c82012-01-27 01:29:43 +00001370 D.complete(ThisDecl);
Richard Smith02e85f32011-04-14 22:09:26 +00001371 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS, &ThisDecl, 1);
1372 }
1373
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001374 SmallVector<Decl *, 8> DeclsInGroup;
Richard Smith02e85f32011-04-14 22:09:26 +00001375 Decl *FirstDecl = ParseDeclarationAfterDeclaratorAndAttributes(D);
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001376 if (LateParsedAttrs.size() > 0)
1377 ParseLexedAttributeList(LateParsedAttrs, FirstDecl, true, false);
John McCall28a6aea2009-11-04 02:18:39 +00001378 D.complete(FirstDecl);
John McCall48871652010-08-21 09:40:31 +00001379 if (FirstDecl)
John McCalld5a36322009-11-03 19:26:08 +00001380 DeclsInGroup.push_back(FirstDecl);
1381
Richard Smith09f76ee2011-10-19 21:33:05 +00001382 bool ExpectSemi = Context != Declarator::ForContext;
1383
Fariborz Jahanian577574a2012-07-02 23:37:09 +00001384 if (CurParsedObjCImpl && D.isFunctionDeclarator() &&
Fariborz Jahanian8a369a82012-07-03 22:54:28 +00001385 Tok.is(tok::l_brace)) {
Fariborz Jahanian577574a2012-07-02 23:37:09 +00001386 // Consume the tokens and store them for later parsing.
1387 StashAwayMethodOrFunctionBodyTokens(FirstDecl);
1388 CurParsedObjCImpl->HasCFunction = true;
1389 ExpectSemi = false;
1390 }
1391
John McCalld5a36322009-11-03 19:26:08 +00001392 // If we don't have a comma, it is either the end of the list (a ';') or an
1393 // error, bail out.
1394 while (Tok.is(tok::comma)) {
Richard Smith09f76ee2011-10-19 21:33:05 +00001395 SourceLocation CommaLoc = ConsumeToken();
1396
1397 if (Tok.isAtStartOfLine() && ExpectSemi && !MightBeDeclarator(Context)) {
1398 // This comma was followed by a line-break and something which can't be
1399 // the start of a declarator. The comma was probably a typo for a
1400 // semicolon.
1401 Diag(CommaLoc, diag::err_expected_semi_declaration)
1402 << FixItHint::CreateReplacement(CommaLoc, ";");
1403 ExpectSemi = false;
1404 break;
1405 }
John McCalld5a36322009-11-03 19:26:08 +00001406
1407 // Parse the next declarator.
1408 D.clear();
Richard Smith8d06f422012-01-12 23:53:29 +00001409 D.setCommaLoc(CommaLoc);
John McCalld5a36322009-11-03 19:26:08 +00001410
1411 // Accept attributes in an init-declarator. In the first declarator in a
1412 // declaration, these would be part of the declspec. In subsequent
1413 // declarators, they become part of the declarator itself, so that they
1414 // don't apply to declarators after *this* one. Examples:
1415 // short __attribute__((common)) var; -> declspec
1416 // short var __attribute__((common)); -> declarator
1417 // short x, __attribute__((common)) var; -> declarator
John McCall53fa7142010-12-24 02:08:15 +00001418 MaybeParseGNUAttributes(D);
John McCalld5a36322009-11-03 19:26:08 +00001419
1420 ParseDeclarator(D);
Fariborz Jahanian372030b2012-01-13 00:14:12 +00001421 if (!D.isInvalidType()) {
1422 Decl *ThisDecl = ParseDeclarationAfterDeclarator(D);
1423 D.complete(ThisDecl);
1424 if (ThisDecl)
Chad Rosierc1183952012-06-26 22:30:43 +00001425 DeclsInGroup.push_back(ThisDecl);
Fariborz Jahanian372030b2012-01-13 00:14:12 +00001426 }
John McCalld5a36322009-11-03 19:26:08 +00001427 }
1428
1429 if (DeclEnd)
1430 *DeclEnd = Tok.getLocation();
1431
Richard Smith09f76ee2011-10-19 21:33:05 +00001432 if (ExpectSemi &&
Chris Lattner02f1b612012-04-28 16:12:17 +00001433 ExpectAndConsumeSemi(Context == Declarator::FileContext
1434 ? diag::err_invalid_token_after_toplevel_declarator
1435 : diag::err_expected_semi_declaration)) {
Chris Lattner13901342010-07-11 22:42:07 +00001436 // Okay, there was no semicolon and one was expected. If we see a
1437 // declaration specifier, just assume it was missing and continue parsing.
1438 // Otherwise things are very confused and we skip to recover.
1439 if (!isDeclarationSpecifier()) {
1440 SkipUntil(tok::r_brace, true, true);
1441 if (Tok.is(tok::semi))
1442 ConsumeToken();
1443 }
John McCalld5a36322009-11-03 19:26:08 +00001444 }
1445
Douglas Gregor0be31a22010-07-02 17:43:08 +00001446 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS,
John McCalld5a36322009-11-03 19:26:08 +00001447 DeclsInGroup.data(),
1448 DeclsInGroup.size());
Chris Lattner53361ac2006-08-10 05:19:57 +00001449}
1450
Richard Smith02e85f32011-04-14 22:09:26 +00001451/// Parse an optional simple-asm-expr and attributes, and attach them to a
1452/// declarator. Returns true on an error.
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001453bool Parser::ParseAsmAttributesAfterDeclarator(Declarator &D) {
Richard Smith02e85f32011-04-14 22:09:26 +00001454 // If a simple-asm-expr is present, parse it.
1455 if (Tok.is(tok::kw_asm)) {
1456 SourceLocation Loc;
1457 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
1458 if (AsmLabel.isInvalid()) {
1459 SkipUntil(tok::semi, true, true);
1460 return true;
1461 }
1462
1463 D.setAsmLabel(AsmLabel.release());
1464 D.SetRangeEnd(Loc);
1465 }
1466
1467 MaybeParseGNUAttributes(D);
1468 return false;
1469}
1470
Douglas Gregor23996282009-05-12 21:31:51 +00001471/// \brief Parse 'declaration' after parsing 'declaration-specifiers
1472/// declarator'. This method parses the remainder of the declaration
1473/// (including any attributes or initializer, among other things) and
1474/// finalizes the declaration.
Chris Lattnerf0f3baa2006-08-14 00:15:20 +00001475///
Chris Lattnerf0f3baa2006-08-14 00:15:20 +00001476/// init-declarator: [C99 6.7]
1477/// declarator
1478/// declarator '=' initializer
Chris Lattner6d7e6342006-08-15 03:41:14 +00001479/// [GNU] declarator simple-asm-expr[opt] attributes[opt]
1480/// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00001481/// [C++] declarator initializer[opt]
1482///
1483/// [C++] initializer:
1484/// [C++] '=' initializer-clause
1485/// [C++] '(' expression-list ')'
Sebastian Redlf769df52009-03-24 22:27:57 +00001486/// [C++0x] '=' 'default' [TODO]
1487/// [C++0x] '=' 'delete'
Sebastian Redl3da34892011-06-05 12:23:16 +00001488/// [C++0x] braced-init-list
Sebastian Redlf769df52009-03-24 22:27:57 +00001489///
1490/// According to the standard grammar, =default and =delete are function
1491/// definitions, but that definitely doesn't fit with the parser here.
Chris Lattnerf0f3baa2006-08-14 00:15:20 +00001492///
John McCall48871652010-08-21 09:40:31 +00001493Decl *Parser::ParseDeclarationAfterDeclarator(Declarator &D,
Douglas Gregorb52fabb2009-06-23 23:11:28 +00001494 const ParsedTemplateInfo &TemplateInfo) {
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001495 if (ParseAsmAttributesAfterDeclarator(D))
Richard Smith02e85f32011-04-14 22:09:26 +00001496 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00001497
Richard Smith02e85f32011-04-14 22:09:26 +00001498 return ParseDeclarationAfterDeclaratorAndAttributes(D, TemplateInfo);
1499}
Mike Stump11289f42009-09-09 15:08:12 +00001500
Richard Smith02e85f32011-04-14 22:09:26 +00001501Decl *Parser::ParseDeclarationAfterDeclaratorAndAttributes(Declarator &D,
1502 const ParsedTemplateInfo &TemplateInfo) {
Douglas Gregor23996282009-05-12 21:31:51 +00001503 // Inform the current actions module that we just parsed this declarator.
John McCall48871652010-08-21 09:40:31 +00001504 Decl *ThisDecl = 0;
Douglas Gregor450f00842009-09-25 18:43:00 +00001505 switch (TemplateInfo.Kind) {
1506 case ParsedTemplateInfo::NonTemplate:
Douglas Gregor0be31a22010-07-02 17:43:08 +00001507 ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
Douglas Gregor450f00842009-09-25 18:43:00 +00001508 break;
Chad Rosierc1183952012-06-26 22:30:43 +00001509
Douglas Gregor450f00842009-09-25 18:43:00 +00001510 case ParsedTemplateInfo::Template:
1511 case ParsedTemplateInfo::ExplicitSpecialization:
Douglas Gregor0be31a22010-07-02 17:43:08 +00001512 ThisDecl = Actions.ActOnTemplateDeclarator(getCurScope(),
John McCallfaf5fb42010-08-26 23:41:50 +00001513 MultiTemplateParamsArg(Actions,
Douglas Gregorb52fabb2009-06-23 23:11:28 +00001514 TemplateInfo.TemplateParams->data(),
1515 TemplateInfo.TemplateParams->size()),
Douglas Gregor450f00842009-09-25 18:43:00 +00001516 D);
1517 break;
Chad Rosierc1183952012-06-26 22:30:43 +00001518
Douglas Gregor450f00842009-09-25 18:43:00 +00001519 case ParsedTemplateInfo::ExplicitInstantiation: {
Chad Rosierc1183952012-06-26 22:30:43 +00001520 DeclResult ThisRes
Douglas Gregor0be31a22010-07-02 17:43:08 +00001521 = Actions.ActOnExplicitInstantiation(getCurScope(),
Douglas Gregor450f00842009-09-25 18:43:00 +00001522 TemplateInfo.ExternLoc,
1523 TemplateInfo.TemplateLoc,
1524 D);
1525 if (ThisRes.isInvalid()) {
1526 SkipUntil(tok::semi, true, true);
John McCall48871652010-08-21 09:40:31 +00001527 return 0;
Douglas Gregor450f00842009-09-25 18:43:00 +00001528 }
Chad Rosierc1183952012-06-26 22:30:43 +00001529
Douglas Gregor450f00842009-09-25 18:43:00 +00001530 ThisDecl = ThisRes.get();
1531 break;
1532 }
1533 }
Mike Stump11289f42009-09-09 15:08:12 +00001534
Richard Smith30482bc2011-02-20 03:19:35 +00001535 bool TypeContainsAuto =
1536 D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto;
1537
Douglas Gregor23996282009-05-12 21:31:51 +00001538 // Parse declarator '=' initializer.
Richard Trieuc64d3232012-01-18 22:54:52 +00001539 // If a '==' or '+=' is found, suggest a fixit to '='.
Richard Trieu4972a6d2012-01-19 22:01:51 +00001540 if (isTokenEqualOrEqualTypo()) {
Douglas Gregor23996282009-05-12 21:31:51 +00001541 ConsumeToken();
Anders Carlsson991285e2010-09-24 21:25:25 +00001542 if (Tok.is(tok::kw_delete)) {
Alexis Hunt5a7fa252011-05-12 06:15:49 +00001543 if (D.isFunctionDeclarator())
1544 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
1545 << 1 /* delete */;
1546 else
1547 Diag(ConsumeToken(), diag::err_deleted_non_function);
Alexis Hunt5dafebc2011-05-06 01:42:00 +00001548 } else if (Tok.is(tok::kw_default)) {
Alexis Hunt5a7fa252011-05-12 06:15:49 +00001549 if (D.isFunctionDeclarator())
Sebastian Redl46afb552012-02-11 23:51:21 +00001550 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
1551 << 0 /* default */;
Alexis Hunt5a7fa252011-05-12 06:15:49 +00001552 else
1553 Diag(ConsumeToken(), diag::err_default_special_members);
Douglas Gregor23996282009-05-12 21:31:51 +00001554 } else {
David Blaikiebbafb8a2012-03-11 07:00:24 +00001555 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
John McCall1f4ee7b2009-12-19 09:28:58 +00001556 EnterScope(0);
Douglas Gregor0be31a22010-07-02 17:43:08 +00001557 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
John McCall1f4ee7b2009-12-19 09:28:58 +00001558 }
Argyrios Kyrtzidis3df19782009-06-17 22:50:06 +00001559
Douglas Gregor7aa6b222010-05-30 01:49:25 +00001560 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001561 Actions.CodeCompleteInitializer(getCurScope(), ThisDecl);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001562 cutOffParsing();
1563 return 0;
Douglas Gregor7aa6b222010-05-30 01:49:25 +00001564 }
Chad Rosierc1183952012-06-26 22:30:43 +00001565
John McCalldadc5752010-08-24 06:29:42 +00001566 ExprResult Init(ParseInitializer());
Argyrios Kyrtzidis3df19782009-06-17 22:50:06 +00001567
David Blaikiebbafb8a2012-03-11 07:00:24 +00001568 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001569 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
John McCall1f4ee7b2009-12-19 09:28:58 +00001570 ExitScope();
1571 }
Argyrios Kyrtzidis3df19782009-06-17 22:50:06 +00001572
Douglas Gregor23996282009-05-12 21:31:51 +00001573 if (Init.isInvalid()) {
Douglas Gregor604c3022010-03-01 18:27:54 +00001574 SkipUntil(tok::comma, true, true);
1575 Actions.ActOnInitializerError(ThisDecl);
1576 } else
Richard Smith30482bc2011-02-20 03:19:35 +00001577 Actions.AddInitializerToDecl(ThisDecl, Init.take(),
1578 /*DirectInit=*/false, TypeContainsAuto);
Douglas Gregor23996282009-05-12 21:31:51 +00001579 }
1580 } else if (Tok.is(tok::l_paren)) {
1581 // Parse C++ direct initializer: '(' expression-list ')'
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001582 BalancedDelimiterTracker T(*this, tok::l_paren);
1583 T.consumeOpen();
1584
Douglas Gregor23996282009-05-12 21:31:51 +00001585 ExprVector Exprs(Actions);
1586 CommaLocsTy CommaLocs;
1587
David Blaikiebbafb8a2012-03-11 07:00:24 +00001588 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor613bf102009-12-22 17:47:17 +00001589 EnterScope(0);
Douglas Gregor0be31a22010-07-02 17:43:08 +00001590 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregor613bf102009-12-22 17:47:17 +00001591 }
1592
Douglas Gregor23996282009-05-12 21:31:51 +00001593 if (ParseExpressionList(Exprs, CommaLocs)) {
1594 SkipUntil(tok::r_paren);
Douglas Gregor613bf102009-12-22 17:47:17 +00001595
David Blaikiebbafb8a2012-03-11 07:00:24 +00001596 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001597 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregor613bf102009-12-22 17:47:17 +00001598 ExitScope();
1599 }
Douglas Gregor23996282009-05-12 21:31:51 +00001600 } else {
1601 // Match the ')'.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001602 T.consumeClose();
Douglas Gregor23996282009-05-12 21:31:51 +00001603
1604 assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() &&
1605 "Unexpected number of commas!");
Douglas Gregor613bf102009-12-22 17:47:17 +00001606
David Blaikiebbafb8a2012-03-11 07:00:24 +00001607 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001608 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregor613bf102009-12-22 17:47:17 +00001609 ExitScope();
1610 }
1611
Sebastian Redla9351792012-02-11 23:51:47 +00001612 ExprResult Initializer = Actions.ActOnParenListExpr(T.getOpenLocation(),
1613 T.getCloseLocation(),
1614 move_arg(Exprs));
1615 Actions.AddInitializerToDecl(ThisDecl, Initializer.take(),
1616 /*DirectInit=*/true, TypeContainsAuto);
Douglas Gregor23996282009-05-12 21:31:51 +00001617 }
Fariborz Jahanian8a369a82012-07-03 22:54:28 +00001618 } else if (getLangOpts().CPlusPlus0x && Tok.is(tok::l_brace) &&
Fariborz Jahanian8be1ecd2012-07-03 23:22:13 +00001619 (!CurParsedObjCImpl || !D.isFunctionDeclarator())) {
Sebastian Redl3da34892011-06-05 12:23:16 +00001620 // Parse C++0x braced-init-list.
Richard Smith5d164bc2011-10-15 05:09:34 +00001621 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
1622
Sebastian Redl3da34892011-06-05 12:23:16 +00001623 if (D.getCXXScopeSpec().isSet()) {
1624 EnterScope(0);
1625 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
1626 }
1627
1628 ExprResult Init(ParseBraceInitializer());
1629
1630 if (D.getCXXScopeSpec().isSet()) {
1631 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
1632 ExitScope();
1633 }
1634
1635 if (Init.isInvalid()) {
1636 Actions.ActOnInitializerError(ThisDecl);
1637 } else
1638 Actions.AddInitializerToDecl(ThisDecl, Init.take(),
1639 /*DirectInit=*/true, TypeContainsAuto);
1640
Douglas Gregor23996282009-05-12 21:31:51 +00001641 } else {
Richard Smith30482bc2011-02-20 03:19:35 +00001642 Actions.ActOnUninitializedDecl(ThisDecl, TypeContainsAuto);
Douglas Gregor23996282009-05-12 21:31:51 +00001643 }
1644
Richard Smithb2bc2e62011-02-21 20:05:19 +00001645 Actions.FinalizeDeclaration(ThisDecl);
1646
Douglas Gregor23996282009-05-12 21:31:51 +00001647 return ThisDecl;
1648}
1649
Chris Lattner1890ac82006-08-13 01:16:23 +00001650/// ParseSpecifierQualifierList
1651/// specifier-qualifier-list:
1652/// type-specifier specifier-qualifier-list[opt]
1653/// type-qualifier specifier-qualifier-list[opt]
Chris Lattnere37e2332006-08-15 04:50:22 +00001654/// [GNU] attributes specifier-qualifier-list[opt]
Chris Lattner1890ac82006-08-13 01:16:23 +00001655///
Richard Smithc5b05522012-03-12 07:56:15 +00001656void Parser::ParseSpecifierQualifierList(DeclSpec &DS, AccessSpecifier AS,
1657 DeclSpecContext DSC) {
Chris Lattner1890ac82006-08-13 01:16:23 +00001658 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
1659 /// parse declaration-specifiers and complain about extra stuff.
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00001660 /// TODO: diagnose attribute-specifiers and alignment-specifiers.
Richard Smithc5b05522012-03-12 07:56:15 +00001661 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS, DSC);
Mike Stump11289f42009-09-09 15:08:12 +00001662
Chris Lattner1890ac82006-08-13 01:16:23 +00001663 // Validate declspec for type-name.
1664 unsigned Specs = DS.getParsedSpecifiers();
Richard Smith2f07ad52012-05-09 20:55:26 +00001665 if ((DSC == DSC_type_specifier || DSC == DSC_trailing) &&
1666 !DS.hasTypeSpecifier()) {
Richard Smithc5b05522012-03-12 07:56:15 +00001667 Diag(Tok, diag::err_expected_type);
1668 DS.SetTypeSpecError();
1669 } else if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers() &&
1670 !DS.hasAttributes()) {
Chris Lattner1890ac82006-08-13 01:16:23 +00001671 Diag(Tok, diag::err_typename_requires_specqual);
Richard Smithc5b05522012-03-12 07:56:15 +00001672 if (!DS.hasTypeSpecifier())
1673 DS.SetTypeSpecError();
1674 }
Mike Stump11289f42009-09-09 15:08:12 +00001675
Chris Lattner1b22eed2006-11-28 05:12:07 +00001676 // Issue diagnostic and remove storage class if present.
Chris Lattner1890ac82006-08-13 01:16:23 +00001677 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
Chris Lattner1b22eed2006-11-28 05:12:07 +00001678 if (DS.getStorageClassSpecLoc().isValid())
1679 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
1680 else
1681 Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass);
Chris Lattnera925dc62006-11-28 04:33:46 +00001682 DS.ClearStorageClassSpecs();
Chris Lattner1890ac82006-08-13 01:16:23 +00001683 }
Mike Stump11289f42009-09-09 15:08:12 +00001684
Chris Lattner1b22eed2006-11-28 05:12:07 +00001685 // Issue diagnostic and remove function specfier if present.
Chris Lattner1890ac82006-08-13 01:16:23 +00001686 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Douglas Gregor61956c42008-10-31 09:07:45 +00001687 if (DS.isInlineSpecified())
1688 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
1689 if (DS.isVirtualSpecified())
1690 Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec);
1691 if (DS.isExplicitSpecified())
1692 Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec);
Chris Lattnera925dc62006-11-28 04:33:46 +00001693 DS.ClearFunctionSpecs();
Chris Lattner1890ac82006-08-13 01:16:23 +00001694 }
Richard Smithc5b05522012-03-12 07:56:15 +00001695
1696 // Issue diagnostic and remove constexpr specfier if present.
1697 if (DS.isConstexprSpecified()) {
1698 Diag(DS.getConstexprSpecLoc(), diag::err_typename_invalid_constexpr);
1699 DS.ClearConstexprSpec();
1700 }
Chris Lattner1890ac82006-08-13 01:16:23 +00001701}
Chris Lattner53361ac2006-08-10 05:19:57 +00001702
Chris Lattner6cc055a2009-04-12 20:42:31 +00001703/// isValidAfterIdentifierInDeclaratorAfterDeclSpec - Return true if the
1704/// specified token is valid after the identifier in a declarator which
1705/// immediately follows the declspec. For example, these things are valid:
1706///
1707/// int x [ 4]; // direct-declarator
1708/// int x ( int y); // direct-declarator
1709/// int(int x ) // direct-declarator
1710/// int x ; // simple-declaration
1711/// int x = 17; // init-declarator-list
1712/// int x , y; // init-declarator-list
1713/// int x __asm__ ("foo"); // init-declarator-list
Chris Lattnera723ba92009-04-14 21:16:09 +00001714/// int x : 4; // struct-declarator
Chris Lattner2b988c12009-04-12 22:29:43 +00001715/// int x { 5}; // C++'0x unified initializers
Chris Lattner6cc055a2009-04-12 20:42:31 +00001716///
1717/// This is not, because 'x' does not immediately follow the declspec (though
1718/// ')' happens to be valid anyway).
1719/// int (x)
1720///
1721static bool isValidAfterIdentifierInDeclarator(const Token &T) {
1722 return T.is(tok::l_square) || T.is(tok::l_paren) || T.is(tok::r_paren) ||
1723 T.is(tok::semi) || T.is(tok::comma) || T.is(tok::equal) ||
Chris Lattnera723ba92009-04-14 21:16:09 +00001724 T.is(tok::kw_asm) || T.is(tok::l_brace) || T.is(tok::colon);
Chris Lattner6cc055a2009-04-12 20:42:31 +00001725}
1726
Chris Lattner20a0c612009-04-14 21:34:55 +00001727
1728/// ParseImplicitInt - This method is called when we have an non-typename
1729/// identifier in a declspec (which normally terminates the decl spec) when
1730/// the declspec has no type specifier. In this case, the declspec is either
1731/// malformed or is "implicit int" (in K&R and C89).
1732///
1733/// This method handles diagnosing this prettily and returns false if the
1734/// declspec is done being processed. If it recovers and thinks there may be
1735/// other pieces of declspec after it, it returns true.
1736///
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001737bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS,
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001738 const ParsedTemplateInfo &TemplateInfo,
Richard Smithc5b05522012-03-12 07:56:15 +00001739 AccessSpecifier AS, DeclSpecContext DSC) {
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001740 assert(Tok.is(tok::identifier) && "should have identifier");
Mike Stump11289f42009-09-09 15:08:12 +00001741
Chris Lattner20a0c612009-04-14 21:34:55 +00001742 SourceLocation Loc = Tok.getLocation();
1743 // If we see an identifier that is not a type name, we normally would
1744 // parse it as the identifer being declared. However, when a typename
1745 // is typo'd or the definition is not included, this will incorrectly
1746 // parse the typename as the identifier name and fall over misparsing
1747 // later parts of the diagnostic.
1748 //
1749 // As such, we try to do some look-ahead in cases where this would
1750 // otherwise be an "implicit-int" case to see if this is invalid. For
1751 // example: "static foo_t x = 4;" In this case, if we parsed foo_t as
1752 // an identifier with implicit int, we'd get a parse error because the
1753 // next token is obviously invalid for a type. Parse these as a case
1754 // with an invalid type specifier.
1755 assert(!DS.hasTypeSpecifier() && "Type specifier checked above");
Mike Stump11289f42009-09-09 15:08:12 +00001756
Chris Lattner20a0c612009-04-14 21:34:55 +00001757 // Since we know that this either implicit int (which is rare) or an
Richard Smitha952ebb2012-05-15 21:01:51 +00001758 // error, do lookahead to try to do better recovery. This never applies
1759 // within a type specifier. Outside of C++, we allow this even if the
1760 // language doesn't "officially" support implicit int -- we support
1761 // implicit int as an extension in C99 and C11. Allegedly, MS also
1762 // supports implicit int in C++ mode.
Richard Smith2f07ad52012-05-09 20:55:26 +00001763 if (DSC != DSC_type_specifier && DSC != DSC_trailing &&
Richard Smitha952ebb2012-05-15 21:01:51 +00001764 (!getLangOpts().CPlusPlus || getLangOpts().MicrosoftExt) &&
Richard Smithc5b05522012-03-12 07:56:15 +00001765 isValidAfterIdentifierInDeclarator(NextToken())) {
Chris Lattner20a0c612009-04-14 21:34:55 +00001766 // If this token is valid for implicit int, e.g. "static x = 4", then
1767 // we just avoid eating the identifier, so it will be parsed as the
1768 // identifier in the declarator.
1769 return false;
1770 }
Mike Stump11289f42009-09-09 15:08:12 +00001771
Richard Smitha952ebb2012-05-15 21:01:51 +00001772 if (getLangOpts().CPlusPlus &&
1773 DS.getStorageClassSpec() == DeclSpec::SCS_auto) {
1774 // Don't require a type specifier if we have the 'auto' storage class
1775 // specifier in C++98 -- we'll promote it to a type specifier.
1776 return false;
1777 }
1778
Chris Lattner20a0c612009-04-14 21:34:55 +00001779 // Otherwise, if we don't consume this token, we are going to emit an
1780 // error anyway. Try to recover from various common problems. Check
1781 // to see if this was a reference to a tag name without a tag specified.
1782 // This is a common problem in C (saying 'foo' instead of 'struct foo').
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001783 //
1784 // C++ doesn't need this, and isTagName doesn't take SS.
1785 if (SS == 0) {
Argyrios Kyrtzidis1f329402011-04-21 17:29:47 +00001786 const char *TagName = 0, *FixitTagName = 0;
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001787 tok::TokenKind TagKind = tok::unknown;
Mike Stump11289f42009-09-09 15:08:12 +00001788
Douglas Gregor0be31a22010-07-02 17:43:08 +00001789 switch (Actions.isTagName(*Tok.getIdentifierInfo(), getCurScope())) {
Chris Lattner20a0c612009-04-14 21:34:55 +00001790 default: break;
Argyrios Kyrtzidis1f329402011-04-21 17:29:47 +00001791 case DeclSpec::TST_enum:
1792 TagName="enum" ; FixitTagName = "enum " ; TagKind=tok::kw_enum ;break;
1793 case DeclSpec::TST_union:
1794 TagName="union" ; FixitTagName = "union " ;TagKind=tok::kw_union ;break;
1795 case DeclSpec::TST_struct:
1796 TagName="struct"; FixitTagName = "struct ";TagKind=tok::kw_struct;break;
1797 case DeclSpec::TST_class:
1798 TagName="class" ; FixitTagName = "class " ;TagKind=tok::kw_class ;break;
Chris Lattner20a0c612009-04-14 21:34:55 +00001799 }
Mike Stump11289f42009-09-09 15:08:12 +00001800
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001801 if (TagName) {
Kaelyn Uhrain031643e2012-04-26 23:36:17 +00001802 IdentifierInfo *TokenName = Tok.getIdentifierInfo();
1803 LookupResult R(Actions, TokenName, SourceLocation(),
1804 Sema::LookupOrdinaryName);
1805
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001806 Diag(Loc, diag::err_use_of_tag_name_without_tag)
Kaelyn Uhrain031643e2012-04-26 23:36:17 +00001807 << TokenName << TagName << getLangOpts().CPlusPlus
1808 << FixItHint::CreateInsertion(Tok.getLocation(), FixitTagName);
1809
1810 if (Actions.LookupParsedName(R, getCurScope(), SS)) {
1811 for (LookupResult::iterator I = R.begin(), IEnd = R.end();
1812 I != IEnd; ++I)
Kaelyn Uhrain3fe3f852012-04-27 18:26:49 +00001813 Diag((*I)->getLocation(), diag::note_decl_hiding_tag_type)
Kaelyn Uhrain031643e2012-04-26 23:36:17 +00001814 << TokenName << TagName;
1815 }
Mike Stump11289f42009-09-09 15:08:12 +00001816
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001817 // Parse this as a tag as if the missing tag were present.
1818 if (TagKind == tok::kw_enum)
Richard Smithc5b05522012-03-12 07:56:15 +00001819 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS, DSC_normal);
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001820 else
Richard Smithc5b05522012-03-12 07:56:15 +00001821 ParseClassSpecifier(TagKind, Loc, DS, TemplateInfo, AS,
1822 /*EnteringContext*/ false, DSC_normal);
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001823 return true;
1824 }
Chris Lattner20a0c612009-04-14 21:34:55 +00001825 }
Mike Stump11289f42009-09-09 15:08:12 +00001826
Richard Smithfe904f02012-05-15 21:29:55 +00001827 // Determine whether this identifier could plausibly be the name of something
Richard Smithedd124e2012-05-15 21:42:17 +00001828 // being declared (with a missing type).
Richard Smithfe904f02012-05-15 21:29:55 +00001829 if (DSC != DSC_type_specifier && DSC != DSC_trailing &&
1830 (!SS || DSC == DSC_top_level || DSC == DSC_class)) {
Richard Smitha952ebb2012-05-15 21:01:51 +00001831 // Look ahead to the next token to try to figure out what this declaration
1832 // was supposed to be.
1833 switch (NextToken().getKind()) {
1834 case tok::comma:
1835 case tok::equal:
1836 case tok::kw_asm:
1837 case tok::l_brace:
1838 case tok::l_square:
1839 case tok::semi:
1840 // This looks like a variable declaration. The type is probably missing.
1841 // We're done parsing decl-specifiers.
1842 return false;
1843
1844 case tok::l_paren: {
1845 // static x(4); // 'x' is not a type
1846 // x(int n); // 'x' is not a type
1847 // x (*p)[]; // 'x' is a type
1848 //
1849 // Since we're in an error case (or the rare 'implicit int in C++' MS
1850 // extension), we can afford to perform a tentative parse to determine
1851 // which case we're in.
1852 TentativeParsingAction PA(*this);
1853 ConsumeToken();
1854 TPResult TPR = TryParseDeclarator(/*mayBeAbstract*/false);
1855 PA.Revert();
1856 if (TPR == TPResult::False())
1857 return false;
1858 // The identifier is followed by a parenthesized declarator.
1859 // It's supposed to be a type.
1860 break;
1861 }
1862
1863 default:
1864 // This is probably supposed to be a type. This includes cases like:
1865 // int f(itn);
1866 // struct S { unsinged : 4; };
1867 break;
1868 }
1869 }
1870
Chad Rosierc1183952012-06-26 22:30:43 +00001871 // This is almost certainly an invalid type name. Let the action emit a
Douglas Gregor15e56022009-10-13 23:27:22 +00001872 // diagnostic and attempt to recover.
John McCallba7bf592010-08-24 05:47:05 +00001873 ParsedType T;
Kaelyn Uhrainb5b17fe2012-06-15 23:45:58 +00001874 IdentifierInfo *II = Tok.getIdentifierInfo();
1875 if (Actions.DiagnoseUnknownTypeName(II, Loc, getCurScope(), SS, T)) {
Douglas Gregor15e56022009-10-13 23:27:22 +00001876 // The action emitted a diagnostic, so we don't have to.
1877 if (T) {
1878 // The action has suggested that the type T could be used. Set that as
1879 // the type in the declaration specifiers, consume the would-be type
1880 // name token, and we're done.
1881 const char *PrevSpec;
1882 unsigned DiagID;
John McCallba7bf592010-08-24 05:47:05 +00001883 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID, T);
Douglas Gregor15e56022009-10-13 23:27:22 +00001884 DS.SetRangeEnd(Tok.getLocation());
1885 ConsumeToken();
Kaelyn Uhrainb5b17fe2012-06-15 23:45:58 +00001886 // There may be other declaration specifiers after this.
1887 return true;
1888 } else if (II != Tok.getIdentifierInfo()) {
1889 // If no type was suggested, the correction is to a keyword
1890 Tok.setKind(II->getTokenID());
Douglas Gregor15e56022009-10-13 23:27:22 +00001891 // There may be other declaration specifiers after this.
1892 return true;
1893 }
Chad Rosierc1183952012-06-26 22:30:43 +00001894
Douglas Gregor15e56022009-10-13 23:27:22 +00001895 // Fall through; the action had no suggestion for us.
1896 } else {
1897 // The action did not emit a diagnostic, so emit one now.
1898 SourceRange R;
1899 if (SS) R = SS->getRange();
1900 Diag(Loc, diag::err_unknown_typename) << Tok.getIdentifierInfo() << R;
1901 }
Mike Stump11289f42009-09-09 15:08:12 +00001902
Douglas Gregor15e56022009-10-13 23:27:22 +00001903 // Mark this as an error.
Richard Smithc5b05522012-03-12 07:56:15 +00001904 DS.SetTypeSpecError();
Chris Lattner20a0c612009-04-14 21:34:55 +00001905 DS.SetRangeEnd(Tok.getLocation());
1906 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001907
Chris Lattner20a0c612009-04-14 21:34:55 +00001908 // TODO: Could inject an invalid typedef decl in an enclosing scope to
1909 // avoid rippling error messages on subsequent uses of the same type,
1910 // could be useful if #include was forgotten.
1911 return false;
1912}
1913
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001914/// \brief Determine the declaration specifier context from the declarator
1915/// context.
1916///
1917/// \param Context the declarator context, which is one of the
1918/// Declarator::TheContext enumerator values.
Chad Rosierc1183952012-06-26 22:30:43 +00001919Parser::DeclSpecContext
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001920Parser::getDeclSpecContextFromDeclaratorContext(unsigned Context) {
1921 if (Context == Declarator::MemberContext)
1922 return DSC_class;
1923 if (Context == Declarator::FileContext)
1924 return DSC_top_level;
Richard Smith62dad822012-03-15 01:02:11 +00001925 if (Context == Declarator::TrailingReturnContext)
1926 return DSC_trailing;
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001927 return DSC_normal;
1928}
1929
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00001930/// ParseAlignArgument - Parse the argument to an alignment-specifier.
1931///
1932/// FIXME: Simply returns an alignof() expression if the argument is a
1933/// type. Ideally, the type should be propagated directly into Sema.
1934///
Benjamin Kramere56f3932011-12-23 17:00:35 +00001935/// [C11] type-id
1936/// [C11] constant-expression
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00001937/// [C++0x] type-id ...[opt]
1938/// [C++0x] assignment-expression ...[opt]
1939ExprResult Parser::ParseAlignArgument(SourceLocation Start,
1940 SourceLocation &EllipsisLoc) {
1941 ExprResult ER;
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00001942 if (isTypeIdInParens()) {
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00001943 SourceLocation TypeLoc = Tok.getLocation();
1944 ParsedType Ty = ParseTypeName().get();
1945 SourceRange TypeRange(Start, Tok.getLocation());
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00001946 ER = Actions.ActOnUnaryExprOrTypeTraitExpr(TypeLoc, UETT_AlignOf, true,
1947 Ty.getAsOpaquePtr(), TypeRange);
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00001948 } else
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00001949 ER = ParseConstantExpression();
1950
David Blaikiebbafb8a2012-03-11 07:00:24 +00001951 if (getLangOpts().CPlusPlus0x && Tok.is(tok::ellipsis))
Peter Collingbourneccbcce02011-10-24 17:56:00 +00001952 EllipsisLoc = ConsumeToken();
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00001953
1954 return ER;
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00001955}
1956
1957/// ParseAlignmentSpecifier - Parse an alignment-specifier, and add the
1958/// attribute to Attrs.
1959///
1960/// alignment-specifier:
Benjamin Kramere56f3932011-12-23 17:00:35 +00001961/// [C11] '_Alignas' '(' type-id ')'
1962/// [C11] '_Alignas' '(' constant-expression ')'
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00001963/// [C++0x] 'alignas' '(' type-id ...[opt] ')'
1964/// [C++0x] 'alignas' '(' assignment-expression ...[opt] ')'
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00001965void Parser::ParseAlignmentSpecifier(ParsedAttributes &Attrs,
1966 SourceLocation *endLoc) {
1967 assert((Tok.is(tok::kw_alignas) || Tok.is(tok::kw__Alignas)) &&
1968 "Not an alignment-specifier!");
1969
1970 SourceLocation KWLoc = Tok.getLocation();
1971 ConsumeToken();
1972
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001973 BalancedDelimiterTracker T(*this, tok::l_paren);
1974 if (T.expectAndConsume(diag::err_expected_lparen))
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00001975 return;
1976
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00001977 SourceLocation EllipsisLoc;
1978 ExprResult ArgExpr = ParseAlignArgument(T.getOpenLocation(), EllipsisLoc);
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00001979 if (ArgExpr.isInvalid()) {
1980 SkipUntil(tok::r_paren);
1981 return;
1982 }
1983
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001984 T.consumeClose();
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00001985 if (endLoc)
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001986 *endLoc = T.getCloseLocation();
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00001987
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00001988 // FIXME: Handle pack-expansions here.
1989 if (EllipsisLoc.isValid()) {
1990 Diag(EllipsisLoc, diag::err_alignas_pack_exp_unsupported);
1991 return;
1992 }
1993
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00001994 ExprVector ArgExprs(Actions);
1995 ArgExprs.push_back(ArgExpr.release());
Alexis Hunt3bc72c12012-06-19 23:57:03 +00001996 // FIXME: This should not be GNU, but we since the attribute used is
1997 // based on the spelling, and there is no true spelling for
1998 // C++11 attributes, this isn't accepted.
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00001999 Attrs.addNew(PP.getIdentifierInfo("aligned"), KWLoc, 0, KWLoc,
Alexis Hunta0e54d42012-06-18 16:13:52 +00002000 0, T.getOpenLocation(), ArgExprs.take(), 1,
Alexis Hunt3bc72c12012-06-19 23:57:03 +00002001 AttributeList::AS_GNU);
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002002}
2003
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002004/// ParseDeclarationSpecifiers
2005/// declaration-specifiers: [C99 6.7]
Chris Lattner3b561a32006-08-13 00:12:11 +00002006/// storage-class-specifier declaration-specifiers[opt]
2007/// type-specifier declaration-specifiers[opt]
Chris Lattner3b561a32006-08-13 00:12:11 +00002008/// [C99] function-specifier declaration-specifiers[opt]
Benjamin Kramere56f3932011-12-23 17:00:35 +00002009/// [C11] alignment-specifier declaration-specifiers[opt]
Chris Lattnere37e2332006-08-15 04:50:22 +00002010/// [GNU] attributes declaration-specifiers[opt]
Douglas Gregor26701a42011-09-09 02:06:17 +00002011/// [Clang] '__module_private__' declaration-specifiers[opt]
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002012///
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002013/// storage-class-specifier: [C99 6.7.1]
Chris Lattnerda48a8e2006-08-04 05:25:55 +00002014/// 'typedef'
2015/// 'extern'
2016/// 'static'
2017/// 'auto'
2018/// 'register'
Sebastian Redlccdfaba2008-11-14 23:42:31 +00002019/// [C++] 'mutable'
Chris Lattnerda48a8e2006-08-04 05:25:55 +00002020/// [GNU] '__thread'
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002021/// function-specifier: [C99 6.7.4]
Chris Lattner3b561a32006-08-13 00:12:11 +00002022/// [C99] 'inline'
Douglas Gregor61956c42008-10-31 09:07:45 +00002023/// [C++] 'virtual'
2024/// [C++] 'explicit'
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00002025/// [OpenCL] '__kernel'
Anders Carlssoncd8db412009-05-06 04:46:28 +00002026/// 'friend': [C++ dcl.friend]
Sebastian Redl39c2a8b2009-11-05 15:47:02 +00002027/// 'constexpr': [C++0x dcl.constexpr]
Anders Carlssoncd8db412009-05-06 04:46:28 +00002028
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002029///
Douglas Gregorb9bd8a92008-12-24 02:52:09 +00002030void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
Douglas Gregor1b57ff32009-05-12 23:25:50 +00002031 const ParsedTemplateInfo &TemplateInfo,
John McCall07e91c02009-08-06 02:15:43 +00002032 AccessSpecifier AS,
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +00002033 DeclSpecContext DSContext,
2034 LateParsedAttrList *LateAttrs) {
Douglas Gregor0e7dde52011-04-24 05:37:28 +00002035 if (DS.getSourceRange().isInvalid()) {
2036 DS.SetRangeStart(Tok.getLocation());
2037 DS.SetRangeEnd(Tok.getLocation());
2038 }
Chad Rosierc1183952012-06-26 22:30:43 +00002039
Douglas Gregordf593fb2011-11-07 17:33:42 +00002040 bool EnteringContext = (DSContext == DSC_class || DSContext == DSC_top_level);
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00002041 bool AttrsLastTime = false;
2042 ParsedAttributesWithRange attrs(AttrFactory);
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002043 while (1) {
John McCall49bfce42009-08-03 20:12:06 +00002044 bool isInvalid = false;
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002045 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00002046 unsigned DiagID = 0;
2047
Chris Lattner4d8f8732006-11-28 05:05:08 +00002048 SourceLocation Loc = Tok.getLocation();
Douglas Gregor450c75a2008-11-07 15:42:26 +00002049
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002050 switch (Tok.getKind()) {
Mike Stump11289f42009-09-09 15:08:12 +00002051 default:
Chris Lattner0974b232008-07-26 00:20:22 +00002052 DoneWithDeclSpec:
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00002053 if (!AttrsLastTime)
2054 ProhibitAttributes(attrs);
2055 else
2056 DS.takeAttributesFrom(attrs);
Peter Collingbourne70188b32011-09-29 18:03:57 +00002057
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002058 // If this is not a declaration specifier token, we're done reading decl
2059 // specifiers. First verify that DeclSpec's are consistent.
Douglas Gregore3e01a22009-04-01 22:41:11 +00002060 DS.Finish(Diags, PP);
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002061 return;
Mike Stump11289f42009-09-09 15:08:12 +00002062
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00002063 case tok::l_square:
2064 case tok::kw_alignas:
2065 if (!isCXX11AttributeSpecifier())
2066 goto DoneWithDeclSpec;
2067
2068 ProhibitAttributes(attrs);
2069 // FIXME: It would be good to recover by accepting the attributes,
2070 // but attempting to do that now would cause serious
2071 // madness in terms of diagnostics.
2072 attrs.clear();
2073 attrs.Range = SourceRange();
2074
2075 ParseCXX11Attributes(attrs);
2076 AttrsLastTime = true;
Chad Rosierc1183952012-06-26 22:30:43 +00002077 continue;
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00002078
Douglas Gregorc49f5b22010-08-23 18:23:48 +00002079 case tok::code_completion: {
John McCallfaf5fb42010-08-26 23:41:50 +00002080 Sema::ParserCompletionContext CCC = Sema::PCC_Namespace;
Douglas Gregorc49f5b22010-08-23 18:23:48 +00002081 if (DS.hasTypeSpecifier()) {
2082 bool AllowNonIdentifiers
2083 = (getCurScope()->getFlags() & (Scope::ControlScope |
2084 Scope::BlockScope |
2085 Scope::TemplateParamScope |
2086 Scope::FunctionPrototypeScope |
2087 Scope::AtCatchScope)) == 0;
2088 bool AllowNestedNameSpecifiers
Chad Rosierc1183952012-06-26 22:30:43 +00002089 = DSContext == DSC_top_level ||
Douglas Gregorc49f5b22010-08-23 18:23:48 +00002090 (DSContext == DSC_class && DS.isFriendSpecified());
2091
Douglas Gregorbfcea8b2010-09-16 15:14:18 +00002092 Actions.CodeCompleteDeclSpec(getCurScope(), DS,
Chad Rosierc1183952012-06-26 22:30:43 +00002093 AllowNonIdentifiers,
Douglas Gregorbfcea8b2010-09-16 15:14:18 +00002094 AllowNestedNameSpecifiers);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002095 return cutOffParsing();
Chad Rosierc1183952012-06-26 22:30:43 +00002096 }
2097
Douglas Gregor80039242011-02-15 20:33:25 +00002098 if (getCurScope()->getFnParent() || getCurScope()->getBlockParent())
2099 CCC = Sema::PCC_LocalDeclarationSpecifiers;
2100 else if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate)
Chad Rosierc1183952012-06-26 22:30:43 +00002101 CCC = DSContext == DSC_class? Sema::PCC_MemberTemplate
John McCallfaf5fb42010-08-26 23:41:50 +00002102 : Sema::PCC_Template;
Douglas Gregorc49f5b22010-08-23 18:23:48 +00002103 else if (DSContext == DSC_class)
John McCallfaf5fb42010-08-26 23:41:50 +00002104 CCC = Sema::PCC_Class;
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002105 else if (CurParsedObjCImpl)
John McCallfaf5fb42010-08-26 23:41:50 +00002106 CCC = Sema::PCC_ObjCImplementation;
Chad Rosierc1183952012-06-26 22:30:43 +00002107
Douglas Gregorc49f5b22010-08-23 18:23:48 +00002108 Actions.CodeCompleteOrdinaryName(getCurScope(), CCC);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002109 return cutOffParsing();
Douglas Gregorc49f5b22010-08-23 18:23:48 +00002110 }
2111
Chris Lattnerbd31aa32009-01-05 00:07:25 +00002112 case tok::coloncolon: // ::foo::bar
John McCall1f476a12010-02-26 08:45:28 +00002113 // C++ scope specifier. Annotate and loop, or bail out on error.
2114 if (TryAnnotateCXXScopeToken(true)) {
2115 if (!DS.hasTypeSpecifier())
2116 DS.SetTypeSpecError();
2117 goto DoneWithDeclSpec;
2118 }
John McCall8bc2a702010-03-01 18:20:46 +00002119 if (Tok.is(tok::coloncolon)) // ::new or ::delete
2120 goto DoneWithDeclSpec;
John McCall1f476a12010-02-26 08:45:28 +00002121 continue;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002122
2123 case tok::annot_cxxscope: {
Richard Smith3092a3b2012-05-09 18:56:43 +00002124 if (DS.hasTypeSpecifier() || DS.isTypeAltiVecVector())
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002125 goto DoneWithDeclSpec;
2126
John McCall9dab4e62009-12-12 11:40:51 +00002127 CXXScopeSpec SS;
Douglas Gregor869ad452011-02-24 17:54:50 +00002128 Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(),
2129 Tok.getAnnotationRange(),
2130 SS);
John McCall9dab4e62009-12-12 11:40:51 +00002131
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002132 // We are looking for a qualified typename.
Douglas Gregor167fa622009-03-25 15:40:00 +00002133 Token Next = NextToken();
Mike Stump11289f42009-09-09 15:08:12 +00002134 if (Next.is(tok::annot_template_id) &&
Douglas Gregor167fa622009-03-25 15:40:00 +00002135 static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue())
Douglas Gregorb67535d2009-03-31 00:43:58 +00002136 ->Kind == TNK_Type_template) {
Douglas Gregor167fa622009-03-25 15:40:00 +00002137 // We have a qualified template-id, e.g., N::A<int>
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002138
2139 // C++ [class.qual]p2:
2140 // In a lookup in which the constructor is an acceptable lookup
2141 // result and the nested-name-specifier nominates a class C:
2142 //
2143 // - if the name specified after the
2144 // nested-name-specifier, when looked up in C, is the
2145 // injected-class-name of C (Clause 9), or
2146 //
2147 // - if the name specified after the nested-name-specifier
2148 // is the same as the identifier or the
2149 // simple-template-id's template-name in the last
2150 // component of the nested-name-specifier,
2151 //
2152 // the name is instead considered to name the constructor of
2153 // class C.
Chad Rosierc1183952012-06-26 22:30:43 +00002154 //
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002155 // Thus, if the template-name is actually the constructor
2156 // name, then the code is ill-formed; this interpretation is
Chad Rosierc1183952012-06-26 22:30:43 +00002157 // reinforced by the NAD status of core issue 635.
Argyrios Kyrtzidisc0c5dd22011-06-22 06:09:49 +00002158 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Next);
John McCall84821e72010-04-13 06:39:49 +00002159 if ((DSContext == DSC_top_level ||
2160 (DSContext == DSC_class && DS.isFriendSpecified())) &&
2161 TemplateId->Name &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00002162 Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) {
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002163 if (isConstructorDeclarator()) {
2164 // The user meant this to be an out-of-line constructor
2165 // definition, but template arguments are not allowed
2166 // there. Just allow this as a constructor; we'll
2167 // complain about it later.
2168 goto DoneWithDeclSpec;
2169 }
2170
2171 // The user meant this to name a type, but it actually names
2172 // a constructor with some extraneous template
2173 // arguments. Complain, then parse it as a type as the user
2174 // intended.
2175 Diag(TemplateId->TemplateNameLoc,
2176 diag::err_out_of_line_template_id_names_constructor)
2177 << TemplateId->Name;
2178 }
2179
John McCall9dab4e62009-12-12 11:40:51 +00002180 DS.getTypeSpecScope() = SS;
2181 ConsumeToken(); // The C++ scope.
Mike Stump11289f42009-09-09 15:08:12 +00002182 assert(Tok.is(tok::annot_template_id) &&
Douglas Gregor167fa622009-03-25 15:40:00 +00002183 "ParseOptionalCXXScopeSpecifier not working");
Douglas Gregore7c20652011-03-02 00:47:37 +00002184 AnnotateTemplateIdTokenAsType();
Douglas Gregor167fa622009-03-25 15:40:00 +00002185 continue;
2186 }
2187
Douglas Gregorc5790df2009-09-28 07:26:33 +00002188 if (Next.is(tok::annot_typename)) {
John McCall9dab4e62009-12-12 11:40:51 +00002189 DS.getTypeSpecScope() = SS;
2190 ConsumeToken(); // The C++ scope.
John McCallba7bf592010-08-24 05:47:05 +00002191 if (Tok.getAnnotationValue()) {
2192 ParsedType T = getTypeAnnotation(Tok);
Nico Weber77430342010-11-22 10:30:56 +00002193 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename,
Chad Rosierc1183952012-06-26 22:30:43 +00002194 Tok.getAnnotationEndLoc(),
John McCallba7bf592010-08-24 05:47:05 +00002195 PrevSpec, DiagID, T);
2196 }
Douglas Gregorc5790df2009-09-28 07:26:33 +00002197 else
2198 DS.SetTypeSpecError();
2199 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
2200 ConsumeToken(); // The typename
2201 }
2202
Douglas Gregor167fa622009-03-25 15:40:00 +00002203 if (Next.isNot(tok::identifier))
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002204 goto DoneWithDeclSpec;
2205
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002206 // If we're in a context where the identifier could be a class name,
2207 // check whether this is a constructor declaration.
John McCall84821e72010-04-13 06:39:49 +00002208 if ((DSContext == DSC_top_level ||
2209 (DSContext == DSC_class && DS.isFriendSpecified())) &&
Chad Rosierc1183952012-06-26 22:30:43 +00002210 Actions.isCurrentClassName(*Next.getIdentifierInfo(), getCurScope(),
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002211 &SS)) {
2212 if (isConstructorDeclarator())
2213 goto DoneWithDeclSpec;
2214
2215 // As noted in C++ [class.qual]p2 (cited above), when the name
2216 // of the class is qualified in a context where it could name
2217 // a constructor, its a constructor name. However, we've
2218 // looked at the declarator, and the user probably meant this
2219 // to be a type. Complain that it isn't supposed to be treated
2220 // as a type, then proceed to parse it as a type.
2221 Diag(Next.getLocation(), diag::err_out_of_line_type_names_constructor)
2222 << Next.getIdentifierInfo();
2223 }
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002224
John McCallba7bf592010-08-24 05:47:05 +00002225 ParsedType TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(),
2226 Next.getLocation(),
Douglas Gregor844cb502011-03-01 18:12:44 +00002227 getCurScope(), &SS,
2228 false, false, ParsedType(),
Abramo Bagnara4244b432012-01-27 08:46:19 +00002229 /*IsCtorOrDtorName=*/false,
Douglas Gregor844cb502011-03-01 18:12:44 +00002230 /*NonTrivialSourceInfo=*/true);
Douglas Gregor8bf42052009-02-09 18:46:07 +00002231
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002232 // If the referenced identifier is not a type, then this declspec is
2233 // erroneous: We already checked about that it has no type specifier, and
2234 // C++ doesn't have implicit int. Diagnose it as a typo w.r.t. to the
Mike Stump11289f42009-09-09 15:08:12 +00002235 // typename.
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002236 if (TypeRep == 0) {
2237 ConsumeToken(); // Eat the scope spec so the identifier is current.
Richard Smithc5b05522012-03-12 07:56:15 +00002238 if (ParseImplicitInt(DS, &SS, TemplateInfo, AS, DSContext)) continue;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002239 goto DoneWithDeclSpec;
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002240 }
Mike Stump11289f42009-09-09 15:08:12 +00002241
John McCall9dab4e62009-12-12 11:40:51 +00002242 DS.getTypeSpecScope() = SS;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002243 ConsumeToken(); // The C++ scope.
2244
Douglas Gregor9817f4a2009-02-09 15:09:02 +00002245 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00002246 DiagID, TypeRep);
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002247 if (isInvalid)
2248 break;
Mike Stump11289f42009-09-09 15:08:12 +00002249
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002250 DS.SetRangeEnd(Tok.getLocation());
2251 ConsumeToken(); // The typename.
2252
2253 continue;
2254 }
Mike Stump11289f42009-09-09 15:08:12 +00002255
Chris Lattnere387d9e2009-01-21 19:48:37 +00002256 case tok::annot_typename: {
John McCallba7bf592010-08-24 05:47:05 +00002257 if (Tok.getAnnotationValue()) {
2258 ParsedType T = getTypeAnnotation(Tok);
Nico Weber7f8bb362010-11-22 12:50:03 +00002259 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallba7bf592010-08-24 05:47:05 +00002260 DiagID, T);
2261 } else
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00002262 DS.SetTypeSpecError();
Chad Rosierc1183952012-06-26 22:30:43 +00002263
Chris Lattner005fc1b2010-04-05 18:18:31 +00002264 if (isInvalid)
2265 break;
2266
Chris Lattnere387d9e2009-01-21 19:48:37 +00002267 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
2268 ConsumeToken(); // The typename
Mike Stump11289f42009-09-09 15:08:12 +00002269
Chris Lattnere387d9e2009-01-21 19:48:37 +00002270 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
2271 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Chad Rosierc1183952012-06-26 22:30:43 +00002272 // Objective-C interface.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002273 if (Tok.is(tok::less) && getLangOpts().ObjC1)
Douglas Gregor06e41ae2010-10-21 23:17:00 +00002274 ParseObjCProtocolQualifiers(DS);
Chad Rosierc1183952012-06-26 22:30:43 +00002275
Chris Lattnere387d9e2009-01-21 19:48:37 +00002276 continue;
2277 }
Mike Stump11289f42009-09-09 15:08:12 +00002278
Douglas Gregor06873092011-04-28 15:48:45 +00002279 case tok::kw___is_signed:
2280 // GNU libstdc++ 4.4 uses __is_signed as an identifier, but Clang
2281 // typically treats it as a trait. If we see __is_signed as it appears
2282 // in libstdc++, e.g.,
2283 //
2284 // static const bool __is_signed;
2285 //
2286 // then treat __is_signed as an identifier rather than as a keyword.
2287 if (DS.getTypeSpecType() == TST_bool &&
2288 DS.getTypeQualifiers() == DeclSpec::TQ_const &&
2289 DS.getStorageClassSpec() == DeclSpec::SCS_static) {
2290 Tok.getIdentifierInfo()->RevertTokenIDToIdentifier();
2291 Tok.setKind(tok::identifier);
2292 }
2293
2294 // We're done with the declaration-specifiers.
2295 goto DoneWithDeclSpec;
Chad Rosierc1183952012-06-26 22:30:43 +00002296
Chris Lattner16fac4f2008-07-26 01:18:38 +00002297 // typedef-name
David Blaikie15a430a2011-12-04 05:04:18 +00002298 case tok::kw_decltype:
Chris Lattner16fac4f2008-07-26 01:18:38 +00002299 case tok::identifier: {
Chris Lattnerbd31aa32009-01-05 00:07:25 +00002300 // In C++, check to see if this is a scope specifier like foo::bar::, if
2301 // so handle it as such. This is important for ctor parsing.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002302 if (getLangOpts().CPlusPlus) {
John McCall1f476a12010-02-26 08:45:28 +00002303 if (TryAnnotateCXXScopeToken(true)) {
2304 if (!DS.hasTypeSpecifier())
2305 DS.SetTypeSpecError();
2306 goto DoneWithDeclSpec;
2307 }
2308 if (!Tok.is(tok::identifier))
2309 continue;
2310 }
Mike Stump11289f42009-09-09 15:08:12 +00002311
Chris Lattner16fac4f2008-07-26 01:18:38 +00002312 // This identifier can only be a typedef name if we haven't already seen
2313 // a type-specifier. Without this check we misparse:
2314 // typedef int X; struct Y { short X; }; as 'short int'.
2315 if (DS.hasTypeSpecifier())
2316 goto DoneWithDeclSpec;
Mike Stump11289f42009-09-09 15:08:12 +00002317
John Thompson22334602010-02-05 00:12:22 +00002318 // Check for need to substitute AltiVec keyword tokens.
2319 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
2320 break;
2321
Richard Smith3092a3b2012-05-09 18:56:43 +00002322 // [AltiVec] 2.2: [If the 'vector' specifier is used] The syntax does not
2323 // allow the use of a typedef name as a type specifier.
2324 if (DS.isTypeAltiVecVector())
2325 goto DoneWithDeclSpec;
2326
John McCallba7bf592010-08-24 05:47:05 +00002327 ParsedType TypeRep =
2328 Actions.getTypeName(*Tok.getIdentifierInfo(),
2329 Tok.getLocation(), getCurScope());
Douglas Gregor8bf42052009-02-09 18:46:07 +00002330
Chris Lattner6cc055a2009-04-12 20:42:31 +00002331 // If this is not a typedef name, don't parse it as part of the declspec,
2332 // it must be an implicit int or an error.
John McCallba7bf592010-08-24 05:47:05 +00002333 if (!TypeRep) {
Richard Smithc5b05522012-03-12 07:56:15 +00002334 if (ParseImplicitInt(DS, 0, TemplateInfo, AS, DSContext)) continue;
Chris Lattner16fac4f2008-07-26 01:18:38 +00002335 goto DoneWithDeclSpec;
Chris Lattner6cc055a2009-04-12 20:42:31 +00002336 }
Douglas Gregor8bf42052009-02-09 18:46:07 +00002337
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002338 // If we're in a context where the identifier could be a class name,
2339 // check whether this is a constructor declaration.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002340 if (getLangOpts().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00002341 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), getCurScope()) &&
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002342 isConstructorDeclarator())
Douglas Gregor61956c42008-10-31 09:07:45 +00002343 goto DoneWithDeclSpec;
2344
Douglas Gregor9817f4a2009-02-09 15:09:02 +00002345 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00002346 DiagID, TypeRep);
Chris Lattner16fac4f2008-07-26 01:18:38 +00002347 if (isInvalid)
2348 break;
Mike Stump11289f42009-09-09 15:08:12 +00002349
Chris Lattner16fac4f2008-07-26 01:18:38 +00002350 DS.SetRangeEnd(Tok.getLocation());
2351 ConsumeToken(); // The identifier
2352
2353 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
2354 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Chad Rosierc1183952012-06-26 22:30:43 +00002355 // Objective-C interface.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002356 if (Tok.is(tok::less) && getLangOpts().ObjC1)
Douglas Gregor06e41ae2010-10-21 23:17:00 +00002357 ParseObjCProtocolQualifiers(DS);
Chad Rosierc1183952012-06-26 22:30:43 +00002358
Steve Naroffcd5e7822008-09-22 10:28:57 +00002359 // Need to support trailing type qualifiers (e.g. "id<p> const").
2360 // If a type specifier follows, it will be diagnosed elsewhere.
2361 continue;
Chris Lattner16fac4f2008-07-26 01:18:38 +00002362 }
Douglas Gregor7f741122009-02-25 19:37:18 +00002363
2364 // type-name
2365 case tok::annot_template_id: {
Argyrios Kyrtzidisc0c5dd22011-06-22 06:09:49 +00002366 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregorb67535d2009-03-31 00:43:58 +00002367 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor7f741122009-02-25 19:37:18 +00002368 // This template-id does not refer to a type name, so we're
2369 // done with the type-specifiers.
2370 goto DoneWithDeclSpec;
2371 }
2372
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002373 // If we're in a context where the template-id could be a
2374 // constructor name or specialization, check whether this is a
2375 // constructor declaration.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002376 if (getLangOpts().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00002377 Actions.isCurrentClassName(*TemplateId->Name, getCurScope()) &&
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002378 isConstructorDeclarator())
2379 goto DoneWithDeclSpec;
2380
Douglas Gregor7f741122009-02-25 19:37:18 +00002381 // Turn the template-id annotation token into a type annotation
2382 // token, then try again to parse it as a type-specifier.
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00002383 AnnotateTemplateIdTokenAsType();
Douglas Gregor7f741122009-02-25 19:37:18 +00002384 continue;
2385 }
2386
Chris Lattnere37e2332006-08-15 04:50:22 +00002387 // GNU attributes support.
2388 case tok::kw___attribute:
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +00002389 ParseGNUAttributes(DS.getAttributes(), 0, LateAttrs);
Chris Lattnerb95cca02006-10-17 03:01:08 +00002390 continue;
Steve Naroff3a9b7e02008-12-24 20:59:21 +00002391
2392 // Microsoft declspec support.
2393 case tok::kw___declspec:
John McCall53fa7142010-12-24 02:08:15 +00002394 ParseMicrosoftDeclSpec(DS.getAttributes());
Steve Naroff3a9b7e02008-12-24 20:59:21 +00002395 continue;
Mike Stump11289f42009-09-09 15:08:12 +00002396
Steve Naroff44ac7772008-12-25 14:16:32 +00002397 // Microsoft single token adornments.
Michael J. Spencerf97bd8c2012-06-18 07:00:48 +00002398 case tok::kw___forceinline: {
2399 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec, DiagID);
2400 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
2401 SourceLocation AttrNameLoc = ConsumeToken();
Alexis Hunta0e54d42012-06-18 16:13:52 +00002402 // FIXME: This does not work correctly if it is set to be a declspec
2403 // attribute, and a GNU attribute is simply incorrect.
Michael J. Spencerf97bd8c2012-06-18 07:00:48 +00002404 DS.getAttributes().addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
Alexis Hunta0e54d42012-06-18 16:13:52 +00002405 SourceLocation(), 0, 0, AttributeList::AS_GNU);
Michael J. Spencerf97bd8c2012-06-18 07:00:48 +00002406 continue;
2407 }
Eli Friedman53339e02009-06-08 23:27:34 +00002408
2409 case tok::kw___ptr64:
Francois Pichetf2fb4112011-08-25 00:36:46 +00002410 case tok::kw___ptr32:
Steve Narofff9c29d42008-12-25 14:41:26 +00002411 case tok::kw___w64:
Steve Naroff44ac7772008-12-25 14:16:32 +00002412 case tok::kw___cdecl:
2413 case tok::kw___stdcall:
2414 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00002415 case tok::kw___thiscall:
Francois Pichet17ed0202011-08-18 09:59:55 +00002416 case tok::kw___unaligned:
John McCall53fa7142010-12-24 02:08:15 +00002417 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman53339e02009-06-08 23:27:34 +00002418 continue;
2419
Dawn Perchik335e16b2010-09-03 01:29:35 +00002420 // Borland single token adornments.
2421 case tok::kw___pascal:
John McCall53fa7142010-12-24 02:08:15 +00002422 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik335e16b2010-09-03 01:29:35 +00002423 continue;
2424
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00002425 // OpenCL single token adornments.
2426 case tok::kw___kernel:
2427 ParseOpenCLAttributes(DS.getAttributes());
2428 continue;
2429
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002430 // storage-class-specifier
2431 case tok::kw_typedef:
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002432 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_typedef, Loc,
2433 PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002434 break;
2435 case tok::kw_extern:
Chris Lattner353f5742006-11-28 04:50:12 +00002436 if (DS.isThreadSpecified())
Chris Lattner6d29c102008-11-18 07:48:38 +00002437 Diag(Tok, diag::ext_thread_before) << "extern";
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002438 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_extern, Loc,
2439 PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002440 break;
Steve Naroff2050b0d2007-12-18 00:16:02 +00002441 case tok::kw___private_extern__:
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002442 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_private_extern,
2443 Loc, PrevSpec, DiagID);
Steve Naroff2050b0d2007-12-18 00:16:02 +00002444 break;
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002445 case tok::kw_static:
Chris Lattner353f5742006-11-28 04:50:12 +00002446 if (DS.isThreadSpecified())
Chris Lattner6d29c102008-11-18 07:48:38 +00002447 Diag(Tok, diag::ext_thread_before) << "static";
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002448 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_static, Loc,
2449 PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002450 break;
2451 case tok::kw_auto:
David Blaikiebbafb8a2012-03-11 07:00:24 +00002452 if (getLangOpts().CPlusPlus0x) {
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00002453 if (isKnownToBeTypeSpecifier(GetLookAheadToken(1))) {
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002454 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc,
2455 PrevSpec, DiagID);
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00002456 if (!isInvalid)
Richard Smith58c74332011-09-04 19:54:14 +00002457 Diag(Tok, diag::ext_auto_storage_class)
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00002458 << FixItHint::CreateRemoval(DS.getStorageClassSpecLoc());
Richard Smith58c74332011-09-04 19:54:14 +00002459 } else
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00002460 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec,
2461 DiagID);
Richard Smith58c74332011-09-04 19:54:14 +00002462 } else
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002463 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc,
2464 PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002465 break;
2466 case tok::kw_register:
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002467 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_register, Loc,
2468 PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002469 break;
Sebastian Redlccdfaba2008-11-14 23:42:31 +00002470 case tok::kw_mutable:
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002471 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_mutable, Loc,
2472 PrevSpec, DiagID);
Sebastian Redlccdfaba2008-11-14 23:42:31 +00002473 break;
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002474 case tok::kw___thread:
John McCall49bfce42009-08-03 20:12:06 +00002475 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002476 break;
Mike Stump11289f42009-09-09 15:08:12 +00002477
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002478 // function-specifier
2479 case tok::kw_inline:
John McCall49bfce42009-08-03 20:12:06 +00002480 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec, DiagID);
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002481 break;
Douglas Gregor61956c42008-10-31 09:07:45 +00002482 case tok::kw_virtual:
John McCall49bfce42009-08-03 20:12:06 +00002483 isInvalid = DS.SetFunctionSpecVirtual(Loc, PrevSpec, DiagID);
Douglas Gregor61956c42008-10-31 09:07:45 +00002484 break;
Douglas Gregor61956c42008-10-31 09:07:45 +00002485 case tok::kw_explicit:
John McCall49bfce42009-08-03 20:12:06 +00002486 isInvalid = DS.SetFunctionSpecExplicit(Loc, PrevSpec, DiagID);
Douglas Gregor61956c42008-10-31 09:07:45 +00002487 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00002488
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002489 // alignment-specifier
2490 case tok::kw__Alignas:
David Blaikiebbafb8a2012-03-11 07:00:24 +00002491 if (!getLangOpts().C11)
Jordan Rose58d54722012-06-30 21:33:57 +00002492 Diag(Tok, diag::ext_c11_alignment) << Tok.getName();
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002493 ParseAlignmentSpecifier(DS.getAttributes());
2494 continue;
2495
Anders Carlssoncd8db412009-05-06 04:46:28 +00002496 // friend
2497 case tok::kw_friend:
John McCall07e91c02009-08-06 02:15:43 +00002498 if (DSContext == DSC_class)
2499 isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID);
2500 else {
2501 PrevSpec = ""; // not actually used by the diagnostic
2502 DiagID = diag::err_friend_invalid_in_context;
2503 isInvalid = true;
2504 }
Anders Carlssoncd8db412009-05-06 04:46:28 +00002505 break;
Mike Stump11289f42009-09-09 15:08:12 +00002506
Douglas Gregor26701a42011-09-09 02:06:17 +00002507 // Modules
2508 case tok::kw___module_private__:
2509 isInvalid = DS.setModulePrivateSpec(Loc, PrevSpec, DiagID);
2510 break;
Chad Rosierc1183952012-06-26 22:30:43 +00002511
Sebastian Redl39c2a8b2009-11-05 15:47:02 +00002512 // constexpr
2513 case tok::kw_constexpr:
2514 isInvalid = DS.SetConstexprSpec(Loc, PrevSpec, DiagID);
2515 break;
2516
Chris Lattnere387d9e2009-01-21 19:48:37 +00002517 // type-specifier
2518 case tok::kw_short:
John McCall49bfce42009-08-03 20:12:06 +00002519 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec,
2520 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002521 break;
2522 case tok::kw_long:
2523 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCall49bfce42009-08-03 20:12:06 +00002524 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
2525 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002526 else
John McCall49bfce42009-08-03 20:12:06 +00002527 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2528 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002529 break;
Francois Pichet84133e42011-04-28 01:59:37 +00002530 case tok::kw___int64:
2531 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2532 DiagID);
2533 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00002534 case tok::kw_signed:
John McCall49bfce42009-08-03 20:12:06 +00002535 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec,
2536 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002537 break;
2538 case tok::kw_unsigned:
John McCall49bfce42009-08-03 20:12:06 +00002539 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
2540 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002541 break;
2542 case tok::kw__Complex:
John McCall49bfce42009-08-03 20:12:06 +00002543 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
2544 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002545 break;
2546 case tok::kw__Imaginary:
John McCall49bfce42009-08-03 20:12:06 +00002547 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
2548 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002549 break;
2550 case tok::kw_void:
John McCall49bfce42009-08-03 20:12:06 +00002551 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec,
2552 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002553 break;
2554 case tok::kw_char:
John McCall49bfce42009-08-03 20:12:06 +00002555 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec,
2556 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002557 break;
2558 case tok::kw_int:
John McCall49bfce42009-08-03 20:12:06 +00002559 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec,
2560 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002561 break;
Richard Smithf016bbc2012-04-04 06:24:32 +00002562 case tok::kw___int128:
2563 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int128, Loc, PrevSpec,
2564 DiagID);
2565 break;
2566 case tok::kw_half:
2567 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_half, Loc, PrevSpec,
2568 DiagID);
2569 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00002570 case tok::kw_float:
John McCall49bfce42009-08-03 20:12:06 +00002571 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec,
2572 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002573 break;
2574 case tok::kw_double:
John McCall49bfce42009-08-03 20:12:06 +00002575 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec,
2576 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002577 break;
2578 case tok::kw_wchar_t:
John McCall49bfce42009-08-03 20:12:06 +00002579 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec,
2580 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002581 break;
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00002582 case tok::kw_char16_t:
John McCall49bfce42009-08-03 20:12:06 +00002583 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec,
2584 DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00002585 break;
2586 case tok::kw_char32_t:
John McCall49bfce42009-08-03 20:12:06 +00002587 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec,
2588 DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00002589 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00002590 case tok::kw_bool:
2591 case tok::kw__Bool:
Argyrios Kyrtzidis20ee5ae2010-11-16 18:18:13 +00002592 if (Tok.is(tok::kw_bool) &&
2593 DS.getTypeSpecType() != DeclSpec::TST_unspecified &&
2594 DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
2595 PrevSpec = ""; // Not used by the diagnostic.
2596 DiagID = diag::err_bool_redeclaration;
Fariborz Jahanian2b059992011-04-19 21:42:37 +00002597 // For better error recovery.
2598 Tok.setKind(tok::identifier);
Argyrios Kyrtzidis20ee5ae2010-11-16 18:18:13 +00002599 isInvalid = true;
2600 } else {
2601 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec,
2602 DiagID);
2603 }
Chris Lattnere387d9e2009-01-21 19:48:37 +00002604 break;
2605 case tok::kw__Decimal32:
John McCall49bfce42009-08-03 20:12:06 +00002606 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
2607 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002608 break;
2609 case tok::kw__Decimal64:
John McCall49bfce42009-08-03 20:12:06 +00002610 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
2611 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002612 break;
2613 case tok::kw__Decimal128:
John McCall49bfce42009-08-03 20:12:06 +00002614 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
2615 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002616 break;
John Thompson22334602010-02-05 00:12:22 +00002617 case tok::kw___vector:
2618 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
2619 break;
2620 case tok::kw___pixel:
2621 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
2622 break;
John McCall39439732011-04-09 22:50:59 +00002623 case tok::kw___unknown_anytype:
2624 isInvalid = DS.SetTypeSpecType(TST_unknown_anytype, Loc,
2625 PrevSpec, DiagID);
2626 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00002627
2628 // class-specifier:
2629 case tok::kw_class:
2630 case tok::kw_struct:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00002631 case tok::kw_union: {
2632 tok::TokenKind Kind = Tok.getKind();
2633 ConsumeToken();
Richard Smithc5b05522012-03-12 07:56:15 +00002634 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS,
2635 EnteringContext, DSContext);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002636 continue;
Chris Lattnerffaa0e62009-04-12 21:49:30 +00002637 }
Chris Lattnere387d9e2009-01-21 19:48:37 +00002638
2639 // enum-specifier:
2640 case tok::kw_enum:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00002641 ConsumeToken();
Richard Smithc5b05522012-03-12 07:56:15 +00002642 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS, DSContext);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002643 continue;
2644
2645 // cv-qualifier:
2646 case tok::kw_const:
John McCall49bfce42009-08-03 20:12:06 +00002647 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec, DiagID,
David Blaikiebbafb8a2012-03-11 07:00:24 +00002648 getLangOpts());
Chris Lattnere387d9e2009-01-21 19:48:37 +00002649 break;
2650 case tok::kw_volatile:
John McCall49bfce42009-08-03 20:12:06 +00002651 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
David Blaikiebbafb8a2012-03-11 07:00:24 +00002652 getLangOpts());
Chris Lattnere387d9e2009-01-21 19:48:37 +00002653 break;
2654 case tok::kw_restrict:
John McCall49bfce42009-08-03 20:12:06 +00002655 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
David Blaikiebbafb8a2012-03-11 07:00:24 +00002656 getLangOpts());
Chris Lattnere387d9e2009-01-21 19:48:37 +00002657 break;
2658
Douglas Gregor333489b2009-03-27 23:10:48 +00002659 // C++ typename-specifier:
2660 case tok::kw_typename:
John McCall1f476a12010-02-26 08:45:28 +00002661 if (TryAnnotateTypeOrScopeToken()) {
2662 DS.SetTypeSpecError();
2663 goto DoneWithDeclSpec;
2664 }
2665 if (!Tok.is(tok::kw_typename))
Douglas Gregor333489b2009-03-27 23:10:48 +00002666 continue;
2667 break;
2668
Chris Lattnere387d9e2009-01-21 19:48:37 +00002669 // GNU typeof support.
2670 case tok::kw_typeof:
2671 ParseTypeofSpecifier(DS);
2672 continue;
2673
David Blaikie15a430a2011-12-04 05:04:18 +00002674 case tok::annot_decltype:
Anders Carlsson74948d02009-06-24 17:47:40 +00002675 ParseDecltypeSpecifier(DS);
2676 continue;
2677
Alexis Hunt4a257072011-05-19 05:37:45 +00002678 case tok::kw___underlying_type:
2679 ParseUnderlyingTypeSpecifier(DS);
Eli Friedman0dfb8892011-10-06 23:00:33 +00002680 continue;
2681
2682 case tok::kw__Atomic:
2683 ParseAtomicSpecifier(DS);
2684 continue;
Alexis Hunt4a257072011-05-19 05:37:45 +00002685
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00002686 // OpenCL qualifiers:
Chad Rosierc1183952012-06-26 22:30:43 +00002687 case tok::kw_private:
David Blaikiebbafb8a2012-03-11 07:00:24 +00002688 if (!getLangOpts().OpenCL)
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00002689 goto DoneWithDeclSpec;
2690 case tok::kw___private:
2691 case tok::kw___global:
2692 case tok::kw___local:
2693 case tok::kw___constant:
2694 case tok::kw___read_only:
2695 case tok::kw___write_only:
2696 case tok::kw___read_write:
2697 ParseOpenCLQualifiers(DS);
2698 break;
Chad Rosierc1183952012-06-26 22:30:43 +00002699
Steve Naroffcfdf6162008-06-05 00:02:44 +00002700 case tok::less:
Chris Lattner16fac4f2008-07-26 01:18:38 +00002701 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattner0974b232008-07-26 00:20:22 +00002702 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
2703 // but we support it.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002704 if (DS.hasTypeSpecifier() || !getLangOpts().ObjC1)
Chris Lattner0974b232008-07-26 00:20:22 +00002705 goto DoneWithDeclSpec;
Mike Stump11289f42009-09-09 15:08:12 +00002706
Douglas Gregor3a001f42010-11-19 17:10:50 +00002707 if (!ParseObjCProtocolQualifiers(DS))
2708 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
2709 << FixItHint::CreateInsertion(Loc, "id")
2710 << SourceRange(Loc, DS.getSourceRange().getEnd());
Chad Rosierc1183952012-06-26 22:30:43 +00002711
Douglas Gregor06e41ae2010-10-21 23:17:00 +00002712 // Need to support trailing type qualifiers (e.g. "id<p> const").
2713 // If a type specifier follows, it will be diagnosed elsewhere.
2714 continue;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002715 }
John McCall49bfce42009-08-03 20:12:06 +00002716 // If the specifier wasn't legal, issue a diagnostic.
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002717 if (isInvalid) {
2718 assert(PrevSpec && "Method did not return previous specifier!");
John McCall49bfce42009-08-03 20:12:06 +00002719 assert(DiagID);
Chad Rosierc1183952012-06-26 22:30:43 +00002720
Douglas Gregora05f5ab2010-08-23 14:34:43 +00002721 if (DiagID == diag::ext_duplicate_declspec)
2722 Diag(Tok, DiagID)
2723 << PrevSpec << FixItHint::CreateRemoval(Tok.getLocation());
2724 else
2725 Diag(Tok, DiagID) << PrevSpec;
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002726 }
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00002727
Chris Lattner2e232092008-03-13 06:29:04 +00002728 DS.SetRangeEnd(Tok.getLocation());
Fariborz Jahanian2b059992011-04-19 21:42:37 +00002729 if (DiagID != diag::err_bool_redeclaration)
2730 ConsumeToken();
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00002731
2732 AttrsLastTime = false;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002733 }
2734}
Douglas Gregoreb31f392008-12-01 23:54:00 +00002735
Chris Lattner70ae4912007-10-29 04:42:53 +00002736/// ParseStructDeclaration - Parse a struct declaration without the terminating
2737/// semicolon.
2738///
Chris Lattner90a26b02007-01-23 04:38:16 +00002739/// struct-declaration:
Chris Lattner70ae4912007-10-29 04:42:53 +00002740/// specifier-qualifier-list struct-declarator-list
Chris Lattner736ed5d2007-06-09 05:59:07 +00002741/// [GNU] __extension__ struct-declaration
Chris Lattner70ae4912007-10-29 04:42:53 +00002742/// [GNU] specifier-qualifier-list
Chris Lattner90a26b02007-01-23 04:38:16 +00002743/// struct-declarator-list:
2744/// struct-declarator
2745/// struct-declarator-list ',' struct-declarator
2746/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
2747/// struct-declarator:
2748/// declarator
2749/// [GNU] declarator attributes[opt]
2750/// declarator[opt] ':' constant-expression
2751/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
2752///
Chris Lattnera12405b2008-04-10 06:46:29 +00002753void Parser::
John McCallcfefb6d2009-11-03 02:38:08 +00002754ParseStructDeclaration(DeclSpec &DS, FieldCallback &Fields) {
Chad Rosierc1183952012-06-26 22:30:43 +00002755
Chris Lattnerf02ef3e2008-10-20 06:45:43 +00002756 if (Tok.is(tok::kw___extension__)) {
2757 // __extension__ silences extension warnings in the subexpression.
2758 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroff97170802007-08-20 22:28:22 +00002759 ConsumeToken();
Chris Lattnerf02ef3e2008-10-20 06:45:43 +00002760 return ParseStructDeclaration(DS, Fields);
2761 }
Mike Stump11289f42009-09-09 15:08:12 +00002762
Steve Naroff97170802007-08-20 22:28:22 +00002763 // Parse the common specifier-qualifiers-list piece.
Steve Naroff97170802007-08-20 22:28:22 +00002764 ParseSpecifierQualifierList(DS);
Mike Stump11289f42009-09-09 15:08:12 +00002765
Douglas Gregorc6f58fe2009-01-12 22:49:06 +00002766 // If there are no declarators, this is a free-standing declaration
2767 // specifier. Let the actions module cope with it.
Chris Lattner76c72282007-10-09 17:33:22 +00002768 if (Tok.is(tok::semi)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00002769 Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none, DS);
Steve Naroff97170802007-08-20 22:28:22 +00002770 return;
2771 }
2772
2773 // Read struct-declarators until we find the semicolon.
John McCallcfefb6d2009-11-03 02:38:08 +00002774 bool FirstDeclarator = true;
Richard Smith8d06f422012-01-12 23:53:29 +00002775 SourceLocation CommaLoc;
Steve Naroff97170802007-08-20 22:28:22 +00002776 while (1) {
John McCall2ec85372012-05-07 06:16:41 +00002777 ParsingDeclRAIIObject PD(*this, ParsingDeclRAIIObject::NoParent);
John McCallcfefb6d2009-11-03 02:38:08 +00002778 FieldDeclarator DeclaratorInfo(DS);
Richard Smith8d06f422012-01-12 23:53:29 +00002779 DeclaratorInfo.D.setCommaLoc(CommaLoc);
John McCallcfefb6d2009-11-03 02:38:08 +00002780
2781 // Attributes are only allowed here on successive declarators.
John McCall53fa7142010-12-24 02:08:15 +00002782 if (!FirstDeclarator)
2783 MaybeParseGNUAttributes(DeclaratorInfo.D);
Mike Stump11289f42009-09-09 15:08:12 +00002784
Steve Naroff97170802007-08-20 22:28:22 +00002785 /// struct-declarator: declarator
2786 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattner17c3b1f2009-12-10 01:59:24 +00002787 if (Tok.isNot(tok::colon)) {
2788 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
2789 ColonProtectionRAIIObject X(*this);
Chris Lattnera12405b2008-04-10 06:46:29 +00002790 ParseDeclarator(DeclaratorInfo.D);
Chris Lattner17c3b1f2009-12-10 01:59:24 +00002791 }
Mike Stump11289f42009-09-09 15:08:12 +00002792
Chris Lattner76c72282007-10-09 17:33:22 +00002793 if (Tok.is(tok::colon)) {
Steve Naroff97170802007-08-20 22:28:22 +00002794 ConsumeToken();
John McCalldadc5752010-08-24 06:29:42 +00002795 ExprResult Res(ParseConstantExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002796 if (Res.isInvalid())
Steve Naroff97170802007-08-20 22:28:22 +00002797 SkipUntil(tok::semi, true, true);
Chris Lattner32295d32008-04-10 06:15:14 +00002798 else
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00002799 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroff97170802007-08-20 22:28:22 +00002800 }
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002801
Steve Naroff97170802007-08-20 22:28:22 +00002802 // If attributes exist after the declarator, parse them.
John McCall53fa7142010-12-24 02:08:15 +00002803 MaybeParseGNUAttributes(DeclaratorInfo.D);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002804
John McCallcfefb6d2009-11-03 02:38:08 +00002805 // We're done with this declarator; invoke the callback.
John McCall48871652010-08-21 09:40:31 +00002806 Decl *D = Fields.invoke(DeclaratorInfo);
John McCall28a6aea2009-11-04 02:18:39 +00002807 PD.complete(D);
John McCallcfefb6d2009-11-03 02:38:08 +00002808
Steve Naroff97170802007-08-20 22:28:22 +00002809 // If we don't have a comma, it is either the end of the list (a ';')
2810 // or an error, bail out.
Chris Lattner76c72282007-10-09 17:33:22 +00002811 if (Tok.isNot(tok::comma))
Chris Lattner70ae4912007-10-29 04:42:53 +00002812 return;
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002813
Steve Naroff97170802007-08-20 22:28:22 +00002814 // Consume the comma.
Richard Smith8d06f422012-01-12 23:53:29 +00002815 CommaLoc = ConsumeToken();
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002816
John McCallcfefb6d2009-11-03 02:38:08 +00002817 FirstDeclarator = false;
Steve Naroff97170802007-08-20 22:28:22 +00002818 }
Steve Naroff97170802007-08-20 22:28:22 +00002819}
2820
2821/// ParseStructUnionBody
2822/// struct-contents:
2823/// struct-declaration-list
2824/// [EXT] empty
2825/// [GNU] "struct-declaration-list" without terminatoring ';'
2826/// struct-declaration-list:
2827/// struct-declaration
2828/// struct-declaration-list struct-declaration
Chris Lattner535b8302008-06-21 19:39:06 +00002829/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroff97170802007-08-20 22:28:22 +00002830///
Chris Lattner1300fb92007-01-23 23:42:53 +00002831void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
John McCall48871652010-08-21 09:40:31 +00002832 unsigned TagType, Decl *TagDecl) {
John McCallfaf5fb42010-08-26 23:41:50 +00002833 PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc,
2834 "parsing struct/union body");
Mike Stump11289f42009-09-09 15:08:12 +00002835
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002836 BalancedDelimiterTracker T(*this, tok::l_brace);
2837 if (T.consumeOpen())
2838 return;
Mike Stump11289f42009-09-09 15:08:12 +00002839
Douglas Gregor658b9552009-01-09 22:42:13 +00002840 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
Douglas Gregor0be31a22010-07-02 17:43:08 +00002841 Actions.ActOnTagStartDefinition(getCurScope(), TagDecl);
Douglas Gregor82ac25e2009-01-08 20:45:30 +00002842
Chris Lattner7b9ace62007-01-23 20:11:08 +00002843 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
2844 // C++.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002845 if (Tok.is(tok::r_brace) && !getLangOpts().CPlusPlus) {
Richard Smithe4345902011-12-29 21:57:33 +00002846 Diag(Tok, diag::ext_empty_struct_union) << (TagType == TST_union);
2847 Diag(Tok, diag::warn_empty_struct_union_compat) << (TagType == TST_union);
2848 }
Chris Lattner7b9ace62007-01-23 20:11:08 +00002849
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002850 SmallVector<Decl *, 32> FieldDecls;
Chris Lattnera12405b2008-04-10 06:46:29 +00002851
Chris Lattner7b9ace62007-01-23 20:11:08 +00002852 // While we still have something to read, read the declarations in the struct.
Chris Lattner76c72282007-10-09 17:33:22 +00002853 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Chris Lattner90a26b02007-01-23 04:38:16 +00002854 // Each iteration of this loop reads one struct-declaration.
Mike Stump11289f42009-09-09 15:08:12 +00002855
Chris Lattner736ed5d2007-06-09 05:59:07 +00002856 // Check for extraneous top-level semicolon.
Chris Lattner76c72282007-10-09 17:33:22 +00002857 if (Tok.is(tok::semi)) {
Richard Trieu2f7dc462012-05-16 19:04:59 +00002858 ConsumeExtraSemi(InsideStruct,
2859 DeclSpec::getSpecifierName((DeclSpec::TST)TagType));
Chris Lattner36e46a22007-06-09 05:49:55 +00002860 continue;
2861 }
Chris Lattnera12405b2008-04-10 06:46:29 +00002862
2863 // Parse all the comma separated declarators.
John McCall084e83d2011-03-24 11:26:52 +00002864 DeclSpec DS(AttrFactory);
Mike Stump11289f42009-09-09 15:08:12 +00002865
John McCallcfefb6d2009-11-03 02:38:08 +00002866 if (!Tok.is(tok::at)) {
2867 struct CFieldCallback : FieldCallback {
2868 Parser &P;
John McCall48871652010-08-21 09:40:31 +00002869 Decl *TagDecl;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002870 SmallVectorImpl<Decl *> &FieldDecls;
John McCallcfefb6d2009-11-03 02:38:08 +00002871
John McCall48871652010-08-21 09:40:31 +00002872 CFieldCallback(Parser &P, Decl *TagDecl,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002873 SmallVectorImpl<Decl *> &FieldDecls) :
John McCallcfefb6d2009-11-03 02:38:08 +00002874 P(P), TagDecl(TagDecl), FieldDecls(FieldDecls) {}
2875
John McCall48871652010-08-21 09:40:31 +00002876 virtual Decl *invoke(FieldDeclarator &FD) {
John McCallcfefb6d2009-11-03 02:38:08 +00002877 // Install the declarator into the current TagDecl.
John McCall48871652010-08-21 09:40:31 +00002878 Decl *Field = P.Actions.ActOnField(P.getCurScope(), TagDecl,
John McCall5e6253b2009-11-03 21:13:47 +00002879 FD.D.getDeclSpec().getSourceRange().getBegin(),
2880 FD.D, FD.BitfieldSize);
John McCallcfefb6d2009-11-03 02:38:08 +00002881 FieldDecls.push_back(Field);
2882 return Field;
Douglas Gregor66a985d2009-08-26 14:27:30 +00002883 }
John McCallcfefb6d2009-11-03 02:38:08 +00002884 } Callback(*this, TagDecl, FieldDecls);
2885
2886 ParseStructDeclaration(DS, Callback);
Chris Lattner535b8302008-06-21 19:39:06 +00002887 } else { // Handle @defs
2888 ConsumeToken();
2889 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
2890 Diag(Tok, diag::err_unexpected_at);
Chris Lattner245c5332010-02-02 00:37:27 +00002891 SkipUntil(tok::semi, true);
Chris Lattner535b8302008-06-21 19:39:06 +00002892 continue;
2893 }
2894 ConsumeToken();
2895 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
2896 if (!Tok.is(tok::identifier)) {
2897 Diag(Tok, diag::err_expected_ident);
Chris Lattner245c5332010-02-02 00:37:27 +00002898 SkipUntil(tok::semi, true);
Chris Lattner535b8302008-06-21 19:39:06 +00002899 continue;
2900 }
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002901 SmallVector<Decl *, 16> Fields;
Douglas Gregor0be31a22010-07-02 17:43:08 +00002902 Actions.ActOnDefs(getCurScope(), TagDecl, Tok.getLocation(),
Douglas Gregor91f84212008-12-11 16:49:14 +00002903 Tok.getIdentifierInfo(), Fields);
Chris Lattner535b8302008-06-21 19:39:06 +00002904 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
2905 ConsumeToken();
2906 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
Mike Stump11289f42009-09-09 15:08:12 +00002907 }
Chris Lattner736ed5d2007-06-09 05:59:07 +00002908
Chris Lattner76c72282007-10-09 17:33:22 +00002909 if (Tok.is(tok::semi)) {
Chris Lattner90a26b02007-01-23 04:38:16 +00002910 ConsumeToken();
Chris Lattner76c72282007-10-09 17:33:22 +00002911 } else if (Tok.is(tok::r_brace)) {
Chris Lattner245c5332010-02-02 00:37:27 +00002912 ExpectAndConsume(tok::semi, diag::ext_expected_semi_decl_list);
Chris Lattner0c7e82d2007-06-09 05:54:40 +00002913 break;
Chris Lattner90a26b02007-01-23 04:38:16 +00002914 } else {
Chris Lattner245c5332010-02-02 00:37:27 +00002915 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
2916 // Skip to end of block or statement to avoid ext-warning on extra ';'.
Chris Lattner90a26b02007-01-23 04:38:16 +00002917 SkipUntil(tok::r_brace, true, true);
Chris Lattner245c5332010-02-02 00:37:27 +00002918 // If we stopped at a ';', eat it.
2919 if (Tok.is(tok::semi)) ConsumeToken();
Chris Lattner90a26b02007-01-23 04:38:16 +00002920 }
2921 }
Mike Stump11289f42009-09-09 15:08:12 +00002922
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002923 T.consumeClose();
Mike Stump11289f42009-09-09 15:08:12 +00002924
John McCall084e83d2011-03-24 11:26:52 +00002925 ParsedAttributes attrs(AttrFactory);
Chris Lattner90a26b02007-01-23 04:38:16 +00002926 // If attributes exist after struct contents, parse them.
John McCall53fa7142010-12-24 02:08:15 +00002927 MaybeParseGNUAttributes(attrs);
Daniel Dunbar15619c72008-10-03 02:03:53 +00002928
Douglas Gregor0be31a22010-07-02 17:43:08 +00002929 Actions.ActOnFields(getCurScope(),
David Blaikie751c5582011-09-22 02:58:26 +00002930 RecordLoc, TagDecl, FieldDecls,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002931 T.getOpenLocation(), T.getCloseLocation(),
John McCall53fa7142010-12-24 02:08:15 +00002932 attrs.getList());
Douglas Gregor82ac25e2009-01-08 20:45:30 +00002933 StructScope.Exit();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002934 Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl,
2935 T.getCloseLocation());
Chris Lattner90a26b02007-01-23 04:38:16 +00002936}
2937
Chris Lattner3b561a32006-08-13 00:12:11 +00002938/// ParseEnumSpecifier
Chris Lattner1890ac82006-08-13 01:16:23 +00002939/// enum-specifier: [C99 6.7.2.2]
Chris Lattner3b561a32006-08-13 00:12:11 +00002940/// 'enum' identifier[opt] '{' enumerator-list '}'
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002941///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Chris Lattnere37e2332006-08-15 04:50:22 +00002942/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
2943/// '}' attributes[opt]
Aaron Ballman9ecff022012-03-01 04:09:28 +00002944/// [MS] 'enum' __declspec[opt] identifier[opt] '{' enumerator-list ',' [opt]
2945/// '}'
Chris Lattner3b561a32006-08-13 00:12:11 +00002946/// 'enum' identifier
Chris Lattnere37e2332006-08-15 04:50:22 +00002947/// [GNU] 'enum' attributes[opt] identifier
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002948///
Richard Smith7d137e32012-03-23 03:33:32 +00002949/// [C++11] enum-head '{' enumerator-list[opt] '}'
2950/// [C++11] enum-head '{' enumerator-list ',' '}'
Douglas Gregor0bf31402010-10-08 23:50:27 +00002951///
Richard Smith7d137e32012-03-23 03:33:32 +00002952/// enum-head: [C++11]
2953/// enum-key attribute-specifier-seq[opt] identifier[opt] enum-base[opt]
2954/// enum-key attribute-specifier-seq[opt] nested-name-specifier
2955/// identifier enum-base[opt]
Douglas Gregor0bf31402010-10-08 23:50:27 +00002956///
Richard Smith7d137e32012-03-23 03:33:32 +00002957/// enum-key: [C++11]
Douglas Gregor0bf31402010-10-08 23:50:27 +00002958/// 'enum'
2959/// 'enum' 'class'
2960/// 'enum' 'struct'
2961///
Richard Smith7d137e32012-03-23 03:33:32 +00002962/// enum-base: [C++11]
Douglas Gregor0bf31402010-10-08 23:50:27 +00002963/// ':' type-specifier-seq
2964///
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002965/// [C++] elaborated-type-specifier:
2966/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
2967///
Chris Lattnerffaa0e62009-04-12 21:49:30 +00002968void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregordc70c3a2010-03-02 17:53:14 +00002969 const ParsedTemplateInfo &TemplateInfo,
Richard Smithc5b05522012-03-12 07:56:15 +00002970 AccessSpecifier AS, DeclSpecContext DSC) {
Chris Lattnerffbc2712007-01-25 06:05:38 +00002971 // Parse the tag portion of this.
Douglas Gregorf45b0cf2009-09-18 15:37:17 +00002972 if (Tok.is(tok::code_completion)) {
2973 // Code completion for an enum name.
Douglas Gregor0be31a22010-07-02 17:43:08 +00002974 Actions.CodeCompleteTag(getCurScope(), DeclSpec::TST_enum);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002975 return cutOffParsing();
Douglas Gregorf45b0cf2009-09-18 15:37:17 +00002976 }
John McCallcb432fa2011-07-06 05:58:41 +00002977
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00002978 // If attributes exist after tag, parse them.
2979 ParsedAttributesWithRange attrs(AttrFactory);
2980 MaybeParseGNUAttributes(attrs);
2981 MaybeParseCXX0XAttributes(attrs);
2982
2983 // If declspecs exist after tag, parse them.
2984 while (Tok.is(tok::kw___declspec))
2985 ParseMicrosoftDeclSpec(attrs);
2986
Richard Smith0f8ee222012-01-10 01:33:14 +00002987 SourceLocation ScopedEnumKWLoc;
John McCallcb432fa2011-07-06 05:58:41 +00002988 bool IsScopedUsingClassTag = false;
2989
John McCallbeae29a2012-06-23 22:30:04 +00002990 // In C++11, recognize 'enum class' and 'enum struct'.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002991 if (getLangOpts().CPlusPlus0x &&
John McCallcb432fa2011-07-06 05:58:41 +00002992 (Tok.is(tok::kw_class) || Tok.is(tok::kw_struct))) {
Richard Smith5d164bc2011-10-15 05:09:34 +00002993 Diag(Tok, diag::warn_cxx98_compat_scoped_enum);
John McCallcb432fa2011-07-06 05:58:41 +00002994 IsScopedUsingClassTag = Tok.is(tok::kw_class);
Richard Smith0f8ee222012-01-10 01:33:14 +00002995 ScopedEnumKWLoc = ConsumeToken();
Chad Rosierc1183952012-06-26 22:30:43 +00002996
John McCallbeae29a2012-06-23 22:30:04 +00002997 // Attributes are not allowed between these keywords. Diagnose,
2998 // but then just treat them like they appeared in the right place.
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00002999 ProhibitAttributes(attrs);
John McCallbeae29a2012-06-23 22:30:04 +00003000
3001 // They are allowed afterwards, though.
3002 MaybeParseGNUAttributes(attrs);
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003003 MaybeParseCXX0XAttributes(attrs);
John McCallbeae29a2012-06-23 22:30:04 +00003004 while (Tok.is(tok::kw___declspec))
3005 ParseMicrosoftDeclSpec(attrs);
John McCallcb432fa2011-07-06 05:58:41 +00003006 }
Richard Smith7d137e32012-03-23 03:33:32 +00003007
John McCall6347b682012-05-07 06:16:58 +00003008 // C++11 [temp.explicit]p12:
3009 // The usual access controls do not apply to names used to specify
3010 // explicit instantiations.
3011 // We extend this to also cover explicit specializations. Note that
3012 // we don't suppress if this turns out to be an elaborated type
3013 // specifier.
3014 bool shouldDelayDiagsInTag =
3015 (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation ||
3016 TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization);
3017 SuppressAccessChecks diagsFromTag(*this, shouldDelayDiagsInTag);
Richard Smith7d137e32012-03-23 03:33:32 +00003018
Richard Smithbfdb1082012-03-12 08:56:40 +00003019 // Enum definitions should not be parsed in a trailing-return-type.
3020 bool AllowDeclaration = DSC != DSC_trailing;
3021
3022 bool AllowFixedUnderlyingType = AllowDeclaration &&
3023 (getLangOpts().CPlusPlus0x || getLangOpts().MicrosoftExt ||
3024 getLangOpts().ObjC2);
John McCallcb432fa2011-07-06 05:58:41 +00003025
Abramo Bagnarad7548482010-05-19 21:37:53 +00003026 CXXScopeSpec &SS = DS.getTypeSpecScope();
David Blaikiebbafb8a2012-03-11 07:00:24 +00003027 if (getLangOpts().CPlusPlus) {
John McCallcb432fa2011-07-06 05:58:41 +00003028 // "enum foo : bar;" is not a potential typo for "enum foo::bar;"
3029 // if a fixed underlying type is allowed.
3030 ColonProtectionRAIIObject X(*this, AllowFixedUnderlyingType);
Chad Rosierc1183952012-06-26 22:30:43 +00003031
3032 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
Douglas Gregordf593fb2011-11-07 17:33:42 +00003033 /*EnteringContext=*/false))
John McCall1f476a12010-02-26 08:45:28 +00003034 return;
3035
3036 if (SS.isSet() && Tok.isNot(tok::identifier)) {
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003037 Diag(Tok, diag::err_expected_ident);
3038 if (Tok.isNot(tok::l_brace)) {
3039 // Has no name and is not a definition.
3040 // Skip the rest of this declarator, up until the comma or semicolon.
3041 SkipUntil(tok::comma, true);
3042 return;
3043 }
3044 }
3045 }
Mike Stump11289f42009-09-09 15:08:12 +00003046
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00003047 // Must have either 'enum name' or 'enum {...}'.
Douglas Gregor6cd5ae42011-02-22 02:55:24 +00003048 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace) &&
Richard Smithbfdb1082012-03-12 08:56:40 +00003049 !(AllowFixedUnderlyingType && Tok.is(tok::colon))) {
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00003050 Diag(Tok, diag::err_expected_ident_lbrace);
Mike Stump11289f42009-09-09 15:08:12 +00003051
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00003052 // Skip the rest of this declarator, up until the comma or semicolon.
3053 SkipUntil(tok::comma, true);
Chris Lattner3b561a32006-08-13 00:12:11 +00003054 return;
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00003055 }
Mike Stump11289f42009-09-09 15:08:12 +00003056
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00003057 // If an identifier is present, consume and remember it.
3058 IdentifierInfo *Name = 0;
3059 SourceLocation NameLoc;
3060 if (Tok.is(tok::identifier)) {
3061 Name = Tok.getIdentifierInfo();
3062 NameLoc = ConsumeToken();
3063 }
Mike Stump11289f42009-09-09 15:08:12 +00003064
Richard Smith0f8ee222012-01-10 01:33:14 +00003065 if (!Name && ScopedEnumKWLoc.isValid()) {
Douglas Gregor0bf31402010-10-08 23:50:27 +00003066 // C++0x 7.2p2: The optional identifier shall not be omitted in the
3067 // declaration of a scoped enumeration.
3068 Diag(Tok, diag::err_scoped_enum_missing_identifier);
Richard Smith0f8ee222012-01-10 01:33:14 +00003069 ScopedEnumKWLoc = SourceLocation();
Abramo Bagnara0e05e242010-12-03 18:54:17 +00003070 IsScopedUsingClassTag = false;
Douglas Gregor0bf31402010-10-08 23:50:27 +00003071 }
3072
John McCall6347b682012-05-07 06:16:58 +00003073 // Okay, end the suppression area. We'll decide whether to emit the
3074 // diagnostics in a second.
3075 if (shouldDelayDiagsInTag)
3076 diagsFromTag.done();
Richard Smith7d137e32012-03-23 03:33:32 +00003077
Douglas Gregor0bf31402010-10-08 23:50:27 +00003078 TypeResult BaseType;
3079
Douglas Gregord1f69f62010-12-01 17:42:47 +00003080 // Parse the fixed underlying type.
Richard Smith200f47c2012-07-02 19:14:01 +00003081 bool CanBeBitfield = getCurScope()->getFlags() & Scope::ClassScope;
Douglas Gregor6cd5ae42011-02-22 02:55:24 +00003082 if (AllowFixedUnderlyingType && Tok.is(tok::colon)) {
Douglas Gregord1f69f62010-12-01 17:42:47 +00003083 bool PossibleBitfield = false;
Richard Smith200f47c2012-07-02 19:14:01 +00003084 if (CanBeBitfield) {
Douglas Gregord1f69f62010-12-01 17:42:47 +00003085 // If we're in class scope, this can either be an enum declaration with
3086 // an underlying type, or a declaration of a bitfield member. We try to
3087 // use a simple disambiguation scheme first to catch the common cases
Chad Rosierc1183952012-06-26 22:30:43 +00003088 // (integer literal, sizeof); if it's still ambiguous, we then consider
3089 // anything that's a simple-type-specifier followed by '(' as an
3090 // expression. This suffices because function types are not valid
Douglas Gregord1f69f62010-12-01 17:42:47 +00003091 // underlying types anyway.
3092 TPResult TPR = isExpressionOrTypeSpecifierSimple(NextToken().getKind());
Chad Rosierc1183952012-06-26 22:30:43 +00003093 // If the next token starts an expression, we know we're parsing a
Douglas Gregord1f69f62010-12-01 17:42:47 +00003094 // bit-field. This is the common case.
3095 if (TPR == TPResult::True())
3096 PossibleBitfield = true;
3097 // If the next token starts a type-specifier-seq, it may be either a
3098 // a fixed underlying type or the start of a function-style cast in C++;
Chad Rosierc1183952012-06-26 22:30:43 +00003099 // lookahead one more token to see if it's obvious that we have a
Douglas Gregord1f69f62010-12-01 17:42:47 +00003100 // fixed underlying type.
Chad Rosierc1183952012-06-26 22:30:43 +00003101 else if (TPR == TPResult::False() &&
Douglas Gregord1f69f62010-12-01 17:42:47 +00003102 GetLookAheadToken(2).getKind() == tok::semi) {
3103 // Consume the ':'.
3104 ConsumeToken();
3105 } else {
3106 // We have the start of a type-specifier-seq, so we have to perform
3107 // tentative parsing to determine whether we have an expression or a
3108 // type.
3109 TentativeParsingAction TPA(*this);
3110
3111 // Consume the ':'.
3112 ConsumeToken();
Richard Smith1e3b0f02012-02-23 01:36:12 +00003113
3114 // If we see a type specifier followed by an open-brace, we have an
3115 // ambiguity between an underlying type and a C++11 braced
3116 // function-style cast. Resolve this by always treating it as an
3117 // underlying type.
3118 // FIXME: The standard is not entirely clear on how to disambiguate in
3119 // this case.
David Blaikiebbafb8a2012-03-11 07:00:24 +00003120 if ((getLangOpts().CPlusPlus &&
Richard Smith1e3b0f02012-02-23 01:36:12 +00003121 isCXXDeclarationSpecifier(TPResult::True()) != TPResult::True()) ||
David Blaikiebbafb8a2012-03-11 07:00:24 +00003122 (!getLangOpts().CPlusPlus && !isDeclarationSpecifier(true))) {
Douglas Gregord1f69f62010-12-01 17:42:47 +00003123 // We'll parse this as a bitfield later.
3124 PossibleBitfield = true;
3125 TPA.Revert();
3126 } else {
3127 // We have a type-specifier-seq.
3128 TPA.Commit();
3129 }
3130 }
3131 } else {
3132 // Consume the ':'.
3133 ConsumeToken();
3134 }
3135
3136 if (!PossibleBitfield) {
3137 SourceRange Range;
3138 BaseType = ParseTypeName(&Range);
Chad Rosierc1183952012-06-26 22:30:43 +00003139
David Blaikiebbafb8a2012-03-11 07:00:24 +00003140 if (!getLangOpts().CPlusPlus0x && !getLangOpts().ObjC2)
Douglas Gregora1aec292011-02-22 20:32:04 +00003141 Diag(StartLoc, diag::ext_ms_enum_fixed_underlying_type)
3142 << Range;
David Blaikiebbafb8a2012-03-11 07:00:24 +00003143 if (getLangOpts().CPlusPlus0x)
Richard Smith5d164bc2011-10-15 05:09:34 +00003144 Diag(StartLoc, diag::warn_cxx98_compat_enum_fixed_underlying_type);
Douglas Gregord1f69f62010-12-01 17:42:47 +00003145 }
Douglas Gregor0bf31402010-10-08 23:50:27 +00003146 }
3147
Richard Smith0f8ee222012-01-10 01:33:14 +00003148 // There are four options here. If we have 'friend enum foo;' then this is a
3149 // friend declaration, and cannot have an accompanying definition. If we have
3150 // 'enum foo;', then this is a forward declaration. If we have
3151 // 'enum foo {...' then this is a definition. Otherwise we have something
3152 // like 'enum foo xyz', a reference.
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00003153 //
3154 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
3155 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
3156 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
3157 //
John McCallfaf5fb42010-08-26 23:41:50 +00003158 Sema::TagUseKind TUK;
John McCall6347b682012-05-07 06:16:58 +00003159 if (!AllowDeclaration) {
Richard Smithbfdb1082012-03-12 08:56:40 +00003160 TUK = Sema::TUK_Reference;
John McCall6347b682012-05-07 06:16:58 +00003161 } else if (Tok.is(tok::l_brace)) {
3162 if (DS.isFriendSpecified()) {
3163 Diag(Tok.getLocation(), diag::err_friend_decl_defines_type)
3164 << SourceRange(DS.getFriendSpecLoc());
3165 ConsumeBrace();
3166 SkipUntil(tok::r_brace);
3167 TUK = Sema::TUK_Friend;
3168 } else {
3169 TUK = Sema::TUK_Definition;
3170 }
Richard Smith369b9f92012-06-25 21:37:02 +00003171 } else if (DSC != DSC_type_specifier &&
3172 (Tok.is(tok::semi) ||
Richard Smith200f47c2012-07-02 19:14:01 +00003173 (Tok.isAtStartOfLine() &&
3174 !isValidAfterTypeSpecifier(CanBeBitfield)))) {
Richard Smith369b9f92012-06-25 21:37:02 +00003175 TUK = DS.isFriendSpecified() ? Sema::TUK_Friend : Sema::TUK_Declaration;
3176 if (Tok.isNot(tok::semi)) {
3177 // A semicolon was missing after this declaration. Diagnose and recover.
3178 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_tagdecl,
3179 "enum");
3180 PP.EnterToken(Tok);
3181 Tok.setKind(tok::semi);
3182 }
John McCall6347b682012-05-07 06:16:58 +00003183 } else {
John McCallfaf5fb42010-08-26 23:41:50 +00003184 TUK = Sema::TUK_Reference;
John McCall6347b682012-05-07 06:16:58 +00003185 }
3186
3187 // If this is an elaborated type specifier, and we delayed
3188 // diagnostics before, just merge them into the current pool.
3189 if (TUK == Sema::TUK_Reference && shouldDelayDiagsInTag) {
3190 diagsFromTag.redelay();
3191 }
Richard Smith7d137e32012-03-23 03:33:32 +00003192
3193 MultiTemplateParamsArg TParams;
Douglas Gregorcbbf3e32010-05-03 17:48:54 +00003194 if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate &&
John McCallfaf5fb42010-08-26 23:41:50 +00003195 TUK != Sema::TUK_Reference) {
Richard Smith7d137e32012-03-23 03:33:32 +00003196 if (!getLangOpts().CPlusPlus0x || !SS.isSet()) {
3197 // Skip the rest of this declarator, up until the comma or semicolon.
3198 Diag(Tok, diag::err_enum_template);
3199 SkipUntil(tok::comma, true);
3200 return;
3201 }
3202
3203 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) {
3204 // Enumerations can't be explicitly instantiated.
3205 DS.SetTypeSpecError();
3206 Diag(StartLoc, diag::err_explicit_instantiation_enum);
3207 return;
3208 }
3209
3210 assert(TemplateInfo.TemplateParams && "no template parameters");
3211 TParams = MultiTemplateParamsArg(TemplateInfo.TemplateParams->data(),
3212 TemplateInfo.TemplateParams->size());
Douglas Gregorcbbf3e32010-05-03 17:48:54 +00003213 }
Chad Rosierc1183952012-06-26 22:30:43 +00003214
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003215 if (TUK == Sema::TUK_Reference)
3216 ProhibitAttributes(attrs);
Richard Smith7d137e32012-03-23 03:33:32 +00003217
Douglas Gregor6cd5ae42011-02-22 02:55:24 +00003218 if (!Name && TUK != Sema::TUK_Definition) {
3219 Diag(Tok, diag::err_enumerator_unnamed_no_def);
Richard Smith7d137e32012-03-23 03:33:32 +00003220
Douglas Gregor6cd5ae42011-02-22 02:55:24 +00003221 // Skip the rest of this declarator, up until the comma or semicolon.
3222 SkipUntil(tok::comma, true);
3223 return;
3224 }
Richard Smith7d137e32012-03-23 03:33:32 +00003225
Douglas Gregord6ab8742009-05-28 23:31:59 +00003226 bool Owned = false;
John McCall7f41d982009-09-11 04:59:25 +00003227 bool IsDependent = false;
Douglas Gregorba41d012010-04-24 16:38:41 +00003228 const char *PrevSpec = 0;
3229 unsigned DiagID;
John McCall48871652010-08-21 09:40:31 +00003230 Decl *TagDecl = Actions.ActOnTag(getCurScope(), DeclSpec::TST_enum, TUK,
John McCall53fa7142010-12-24 02:08:15 +00003231 StartLoc, SS, Name, NameLoc, attrs.getList(),
Richard Smith7d137e32012-03-23 03:33:32 +00003232 AS, DS.getModulePrivateSpecLoc(), TParams,
Richard Smith0f8ee222012-01-10 01:33:14 +00003233 Owned, IsDependent, ScopedEnumKWLoc,
Abramo Bagnara0e05e242010-12-03 18:54:17 +00003234 IsScopedUsingClassTag, BaseType);
Douglas Gregor0bf31402010-10-08 23:50:27 +00003235
Douglas Gregorba41d012010-04-24 16:38:41 +00003236 if (IsDependent) {
Chad Rosierc1183952012-06-26 22:30:43 +00003237 // This enum has a dependent nested-name-specifier. Handle it as a
Douglas Gregorba41d012010-04-24 16:38:41 +00003238 // dependent tag.
3239 if (!Name) {
3240 DS.SetTypeSpecError();
3241 Diag(Tok, diag::err_expected_type_name_after_typename);
3242 return;
3243 }
Chad Rosierc1183952012-06-26 22:30:43 +00003244
Douglas Gregor0be31a22010-07-02 17:43:08 +00003245 TypeResult Type = Actions.ActOnDependentTag(getCurScope(), DeclSpec::TST_enum,
Chad Rosierc1183952012-06-26 22:30:43 +00003246 TUK, SS, Name, StartLoc,
Douglas Gregorba41d012010-04-24 16:38:41 +00003247 NameLoc);
3248 if (Type.isInvalid()) {
3249 DS.SetTypeSpecError();
3250 return;
3251 }
Chad Rosierc1183952012-06-26 22:30:43 +00003252
Abramo Bagnara9875a3c2011-03-16 20:16:18 +00003253 if (DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc,
3254 NameLoc.isValid() ? NameLoc : StartLoc,
3255 PrevSpec, DiagID, Type.get()))
Douglas Gregorba41d012010-04-24 16:38:41 +00003256 Diag(StartLoc, DiagID) << PrevSpec;
Chad Rosierc1183952012-06-26 22:30:43 +00003257
Douglas Gregorba41d012010-04-24 16:38:41 +00003258 return;
3259 }
Mike Stump11289f42009-09-09 15:08:12 +00003260
John McCall48871652010-08-21 09:40:31 +00003261 if (!TagDecl) {
Chad Rosierc1183952012-06-26 22:30:43 +00003262 // The action failed to produce an enumeration tag. If this is a
Douglas Gregorba41d012010-04-24 16:38:41 +00003263 // definition, consume the entire definition.
Richard Smithbfdb1082012-03-12 08:56:40 +00003264 if (Tok.is(tok::l_brace) && TUK != Sema::TUK_Reference) {
Douglas Gregorba41d012010-04-24 16:38:41 +00003265 ConsumeBrace();
3266 SkipUntil(tok::r_brace);
3267 }
Chad Rosierc1183952012-06-26 22:30:43 +00003268
Douglas Gregorba41d012010-04-24 16:38:41 +00003269 DS.SetTypeSpecError();
3270 return;
3271 }
Richard Smith0f8ee222012-01-10 01:33:14 +00003272
Richard Smith369b9f92012-06-25 21:37:02 +00003273 if (Tok.is(tok::l_brace) && TUK != Sema::TUK_Reference)
John McCall6347b682012-05-07 06:16:58 +00003274 ParseEnumBody(StartLoc, TagDecl);
Mike Stump11289f42009-09-09 15:08:12 +00003275
Abramo Bagnara9875a3c2011-03-16 20:16:18 +00003276 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc,
3277 NameLoc.isValid() ? NameLoc : StartLoc,
3278 PrevSpec, DiagID, TagDecl, Owned))
John McCall49bfce42009-08-03 20:12:06 +00003279 Diag(StartLoc, DiagID) << PrevSpec;
Chris Lattner3b561a32006-08-13 00:12:11 +00003280}
3281
Chris Lattnerc1915e22007-01-25 07:29:02 +00003282/// ParseEnumBody - Parse a {} enclosed enumerator-list.
3283/// enumerator-list:
3284/// enumerator
3285/// enumerator-list ',' enumerator
3286/// enumerator:
3287/// enumeration-constant
3288/// enumeration-constant '=' constant-expression
3289/// enumeration-constant:
3290/// identifier
3291///
John McCall48871652010-08-21 09:40:31 +00003292void Parser::ParseEnumBody(SourceLocation StartLoc, Decl *EnumDecl) {
Douglas Gregor07665a62009-01-05 19:45:36 +00003293 // Enter the scope of the enum body and start the definition.
3294 ParseScope EnumScope(this, Scope::DeclScope);
Douglas Gregor0be31a22010-07-02 17:43:08 +00003295 Actions.ActOnTagStartDefinition(getCurScope(), EnumDecl);
Douglas Gregor07665a62009-01-05 19:45:36 +00003296
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003297 BalancedDelimiterTracker T(*this, tok::l_brace);
3298 T.consumeOpen();
Mike Stump11289f42009-09-09 15:08:12 +00003299
Chris Lattner37256fb2007-08-27 17:24:30 +00003300 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
David Blaikiebbafb8a2012-03-11 07:00:24 +00003301 if (Tok.is(tok::r_brace) && !getLangOpts().CPlusPlus)
Fariborz Jahanian6e814922010-05-28 22:23:22 +00003302 Diag(Tok, diag::error_empty_enum);
Mike Stump11289f42009-09-09 15:08:12 +00003303
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003304 SmallVector<Decl *, 32> EnumConstantDecls;
Chris Lattnerc1915e22007-01-25 07:29:02 +00003305
John McCall48871652010-08-21 09:40:31 +00003306 Decl *LastEnumConstDecl = 0;
Mike Stump11289f42009-09-09 15:08:12 +00003307
Chris Lattnerc1915e22007-01-25 07:29:02 +00003308 // Parse the enumerator-list.
Chris Lattner76c72282007-10-09 17:33:22 +00003309 while (Tok.is(tok::identifier)) {
Chris Lattnerc1915e22007-01-25 07:29:02 +00003310 IdentifierInfo *Ident = Tok.getIdentifierInfo();
3311 SourceLocation IdentLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00003312
John McCall811a0f52010-10-22 23:36:17 +00003313 // If attributes exist after the enumerator, parse them.
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003314 ParsedAttributesWithRange attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00003315 MaybeParseGNUAttributes(attrs);
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003316 MaybeParseCXX0XAttributes(attrs);
3317 ProhibitAttributes(attrs);
John McCall811a0f52010-10-22 23:36:17 +00003318
Chris Lattnerc1915e22007-01-25 07:29:02 +00003319 SourceLocation EqualLoc;
John McCalldadc5752010-08-24 06:29:42 +00003320 ExprResult AssignedVal;
John McCall2ec85372012-05-07 06:16:41 +00003321 ParsingDeclRAIIObject PD(*this, ParsingDeclRAIIObject::NoParent);
Chad Rosierc1183952012-06-26 22:30:43 +00003322
Chris Lattner76c72282007-10-09 17:33:22 +00003323 if (Tok.is(tok::equal)) {
Chris Lattnerc1915e22007-01-25 07:29:02 +00003324 EqualLoc = ConsumeToken();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00003325 AssignedVal = ParseConstantExpression();
3326 if (AssignedVal.isInvalid())
Chris Lattnerda6c2ce2007-04-27 19:13:15 +00003327 SkipUntil(tok::comma, tok::r_brace, true, true);
Chris Lattnerc1915e22007-01-25 07:29:02 +00003328 }
Mike Stump11289f42009-09-09 15:08:12 +00003329
Chris Lattnerc1915e22007-01-25 07:29:02 +00003330 // Install the enumerator constant into EnumDecl.
John McCall48871652010-08-21 09:40:31 +00003331 Decl *EnumConstDecl = Actions.ActOnEnumConstant(getCurScope(), EnumDecl,
3332 LastEnumConstDecl,
3333 IdentLoc, Ident,
John McCall53fa7142010-12-24 02:08:15 +00003334 attrs.getList(), EqualLoc,
John McCall48871652010-08-21 09:40:31 +00003335 AssignedVal.release());
Fariborz Jahanian329b3512011-12-09 01:15:54 +00003336 PD.complete(EnumConstDecl);
Chad Rosierc1183952012-06-26 22:30:43 +00003337
Chris Lattner4ef40012007-06-11 01:28:17 +00003338 EnumConstantDecls.push_back(EnumConstDecl);
3339 LastEnumConstDecl = EnumConstDecl;
Mike Stump11289f42009-09-09 15:08:12 +00003340
Douglas Gregorce66d022010-09-07 14:51:08 +00003341 if (Tok.is(tok::identifier)) {
3342 // We're missing a comma between enumerators.
3343 SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation);
Chad Rosierc1183952012-06-26 22:30:43 +00003344 Diag(Loc, diag::err_enumerator_list_missing_comma)
Douglas Gregorce66d022010-09-07 14:51:08 +00003345 << FixItHint::CreateInsertion(Loc, ", ");
3346 continue;
3347 }
Chad Rosierc1183952012-06-26 22:30:43 +00003348
Chris Lattner76c72282007-10-09 17:33:22 +00003349 if (Tok.isNot(tok::comma))
Chris Lattnerc1915e22007-01-25 07:29:02 +00003350 break;
3351 SourceLocation CommaLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00003352
Richard Smith5d164bc2011-10-15 05:09:34 +00003353 if (Tok.isNot(tok::identifier)) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00003354 if (!getLangOpts().C99 && !getLangOpts().CPlusPlus0x)
Richard Smith5d164bc2011-10-15 05:09:34 +00003355 Diag(CommaLoc, diag::ext_enumerator_list_comma)
David Blaikiebbafb8a2012-03-11 07:00:24 +00003356 << getLangOpts().CPlusPlus
Richard Smith5d164bc2011-10-15 05:09:34 +00003357 << FixItHint::CreateRemoval(CommaLoc);
David Blaikiebbafb8a2012-03-11 07:00:24 +00003358 else if (getLangOpts().CPlusPlus0x)
Richard Smith5d164bc2011-10-15 05:09:34 +00003359 Diag(CommaLoc, diag::warn_cxx98_compat_enumerator_list_comma)
3360 << FixItHint::CreateRemoval(CommaLoc);
3361 }
Chris Lattnerc1915e22007-01-25 07:29:02 +00003362 }
Mike Stump11289f42009-09-09 15:08:12 +00003363
Chris Lattnerc1915e22007-01-25 07:29:02 +00003364 // Eat the }.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003365 T.consumeClose();
Chris Lattnerc1915e22007-01-25 07:29:02 +00003366
Chris Lattnerc1915e22007-01-25 07:29:02 +00003367 // If attributes exist after the identifier list, parse them.
John McCall084e83d2011-03-24 11:26:52 +00003368 ParsedAttributes attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00003369 MaybeParseGNUAttributes(attrs);
Douglas Gregor82ac25e2009-01-08 20:45:30 +00003370
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003371 Actions.ActOnEnumBody(StartLoc, T.getOpenLocation(), T.getCloseLocation(),
3372 EnumDecl, EnumConstantDecls.data(),
3373 EnumConstantDecls.size(), getCurScope(),
3374 attrs.getList());
Mike Stump11289f42009-09-09 15:08:12 +00003375
Douglas Gregor82ac25e2009-01-08 20:45:30 +00003376 EnumScope.Exit();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003377 Actions.ActOnTagFinishDefinition(getCurScope(), EnumDecl,
3378 T.getCloseLocation());
Richard Smith369b9f92012-06-25 21:37:02 +00003379
3380 // The next token must be valid after an enum definition. If not, a ';'
3381 // was probably forgotten.
Richard Smith200f47c2012-07-02 19:14:01 +00003382 bool CanBeBitfield = getCurScope()->getFlags() & Scope::ClassScope;
3383 if (!isValidAfterTypeSpecifier(CanBeBitfield)) {
Richard Smith369b9f92012-06-25 21:37:02 +00003384 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_tagdecl, "enum");
3385 // Push this token back into the preprocessor and change our current token
3386 // to ';' so that the rest of the code recovers as though there were an
3387 // ';' after the definition.
3388 PP.EnterToken(Tok);
3389 Tok.setKind(tok::semi);
3390 }
Chris Lattnerc1915e22007-01-25 07:29:02 +00003391}
Chris Lattner3b561a32006-08-13 00:12:11 +00003392
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003393/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff69e8f9e2008-02-11 23:15:56 +00003394/// start of a type-qualifier-list.
3395bool Parser::isTypeQualifier() const {
3396 switch (Tok.getKind()) {
3397 default: return false;
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003398
3399 // type-qualifier only in OpenCL
3400 case tok::kw_private:
David Blaikiebbafb8a2012-03-11 07:00:24 +00003401 return getLangOpts().OpenCL;
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003402
Steve Naroff69e8f9e2008-02-11 23:15:56 +00003403 // type-qualifier
3404 case tok::kw_const:
3405 case tok::kw_volatile:
3406 case tok::kw_restrict:
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003407 case tok::kw___private:
3408 case tok::kw___local:
3409 case tok::kw___global:
3410 case tok::kw___constant:
3411 case tok::kw___read_only:
3412 case tok::kw___read_write:
3413 case tok::kw___write_only:
Steve Naroff69e8f9e2008-02-11 23:15:56 +00003414 return true;
3415 }
3416}
3417
Chris Lattnerfd48afe2010-02-28 18:18:36 +00003418/// isKnownToBeTypeSpecifier - Return true if we know that the specified token
3419/// is definitely a type-specifier. Return false if it isn't part of a type
3420/// specifier or if we're not sure.
3421bool Parser::isKnownToBeTypeSpecifier(const Token &Tok) const {
3422 switch (Tok.getKind()) {
3423 default: return false;
3424 // type-specifiers
3425 case tok::kw_short:
3426 case tok::kw_long:
Francois Pichet84133e42011-04-28 01:59:37 +00003427 case tok::kw___int64:
Richard Smithf016bbc2012-04-04 06:24:32 +00003428 case tok::kw___int128:
Chris Lattnerfd48afe2010-02-28 18:18:36 +00003429 case tok::kw_signed:
3430 case tok::kw_unsigned:
3431 case tok::kw__Complex:
3432 case tok::kw__Imaginary:
3433 case tok::kw_void:
3434 case tok::kw_char:
3435 case tok::kw_wchar_t:
3436 case tok::kw_char16_t:
3437 case tok::kw_char32_t:
3438 case tok::kw_int:
Anton Korobeynikovf0c267e2011-10-14 23:23:15 +00003439 case tok::kw_half:
Chris Lattnerfd48afe2010-02-28 18:18:36 +00003440 case tok::kw_float:
3441 case tok::kw_double:
3442 case tok::kw_bool:
3443 case tok::kw__Bool:
3444 case tok::kw__Decimal32:
3445 case tok::kw__Decimal64:
3446 case tok::kw__Decimal128:
3447 case tok::kw___vector:
Chad Rosierc1183952012-06-26 22:30:43 +00003448
Chris Lattnerfd48afe2010-02-28 18:18:36 +00003449 // struct-or-union-specifier (C99) or class-specifier (C++)
3450 case tok::kw_class:
3451 case tok::kw_struct:
3452 case tok::kw_union:
3453 // enum-specifier
3454 case tok::kw_enum:
Chad Rosierc1183952012-06-26 22:30:43 +00003455
Chris Lattnerfd48afe2010-02-28 18:18:36 +00003456 // typedef-name
3457 case tok::annot_typename:
3458 return true;
3459 }
3460}
3461
Steve Naroff69e8f9e2008-02-11 23:15:56 +00003462/// isTypeSpecifierQualifier - Return true if the current token could be the
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003463/// start of a specifier-qualifier-list.
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003464bool Parser::isTypeSpecifierQualifier() {
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003465 switch (Tok.getKind()) {
3466 default: return false;
Mike Stump11289f42009-09-09 15:08:12 +00003467
Chris Lattner020bab92009-01-04 23:41:41 +00003468 case tok::identifier: // foo::bar
John Thompson22334602010-02-05 00:12:22 +00003469 if (TryAltiVecVectorToken())
3470 return true;
3471 // Fall through.
Douglas Gregor333489b2009-03-27 23:10:48 +00003472 case tok::kw_typename: // typename T::type
Chris Lattner020bab92009-01-04 23:41:41 +00003473 // Annotate typenames and C++ scope specifiers. If we get one, just
3474 // recurse to handle whatever we get.
3475 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00003476 return true;
3477 if (Tok.is(tok::identifier))
3478 return false;
3479 return isTypeSpecifierQualifier();
Douglas Gregor333489b2009-03-27 23:10:48 +00003480
Chris Lattner020bab92009-01-04 23:41:41 +00003481 case tok::coloncolon: // ::foo::bar
3482 if (NextToken().is(tok::kw_new) || // ::new
3483 NextToken().is(tok::kw_delete)) // ::delete
3484 return false;
3485
Chris Lattner020bab92009-01-04 23:41:41 +00003486 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00003487 return true;
3488 return isTypeSpecifierQualifier();
Mike Stump11289f42009-09-09 15:08:12 +00003489
Chris Lattnere37e2332006-08-15 04:50:22 +00003490 // GNU attributes support.
3491 case tok::kw___attribute:
Steve Naroffad373bd2007-07-31 12:34:36 +00003492 // GNU typeof support.
3493 case tok::kw_typeof:
Mike Stump11289f42009-09-09 15:08:12 +00003494
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003495 // type-specifiers
3496 case tok::kw_short:
3497 case tok::kw_long:
Francois Pichet84133e42011-04-28 01:59:37 +00003498 case tok::kw___int64:
Richard Smithf016bbc2012-04-04 06:24:32 +00003499 case tok::kw___int128:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003500 case tok::kw_signed:
3501 case tok::kw_unsigned:
3502 case tok::kw__Complex:
3503 case tok::kw__Imaginary:
3504 case tok::kw_void:
3505 case tok::kw_char:
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +00003506 case tok::kw_wchar_t:
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00003507 case tok::kw_char16_t:
3508 case tok::kw_char32_t:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003509 case tok::kw_int:
Anton Korobeynikovf0c267e2011-10-14 23:23:15 +00003510 case tok::kw_half:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003511 case tok::kw_float:
3512 case tok::kw_double:
Chris Lattnerbb31a422007-11-15 05:25:19 +00003513 case tok::kw_bool:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003514 case tok::kw__Bool:
3515 case tok::kw__Decimal32:
3516 case tok::kw__Decimal64:
3517 case tok::kw__Decimal128:
John Thompson22334602010-02-05 00:12:22 +00003518 case tok::kw___vector:
Mike Stump11289f42009-09-09 15:08:12 +00003519
Chris Lattner861a2262008-04-13 18:59:07 +00003520 // struct-or-union-specifier (C99) or class-specifier (C++)
3521 case tok::kw_class:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003522 case tok::kw_struct:
3523 case tok::kw_union:
3524 // enum-specifier
3525 case tok::kw_enum:
Mike Stump11289f42009-09-09 15:08:12 +00003526
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003527 // type-qualifier
3528 case tok::kw_const:
3529 case tok::kw_volatile:
3530 case tok::kw_restrict:
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003531
3532 // typedef-name
Chris Lattnera8a3f732009-01-06 05:06:21 +00003533 case tok::annot_typename:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003534 return true;
Mike Stump11289f42009-09-09 15:08:12 +00003535
Chris Lattner409bf7d2008-10-20 00:25:30 +00003536 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
3537 case tok::less:
David Blaikiebbafb8a2012-03-11 07:00:24 +00003538 return getLangOpts().ObjC1;
Mike Stump11289f42009-09-09 15:08:12 +00003539
Steve Naroff44ac7772008-12-25 14:16:32 +00003540 case tok::kw___cdecl:
3541 case tok::kw___stdcall:
3542 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00003543 case tok::kw___thiscall:
Eli Friedman53339e02009-06-08 23:27:34 +00003544 case tok::kw___w64:
3545 case tok::kw___ptr64:
Francois Pichetf2fb4112011-08-25 00:36:46 +00003546 case tok::kw___ptr32:
Dawn Perchik335e16b2010-09-03 01:29:35 +00003547 case tok::kw___pascal:
Francois Pichet17ed0202011-08-18 09:59:55 +00003548 case tok::kw___unaligned:
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003549
3550 case tok::kw___private:
3551 case tok::kw___local:
3552 case tok::kw___global:
3553 case tok::kw___constant:
3554 case tok::kw___read_only:
3555 case tok::kw___read_write:
3556 case tok::kw___write_only:
3557
Eli Friedman53339e02009-06-08 23:27:34 +00003558 return true;
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003559
3560 case tok::kw_private:
David Blaikiebbafb8a2012-03-11 07:00:24 +00003561 return getLangOpts().OpenCL;
Eli Friedman0dfb8892011-10-06 23:00:33 +00003562
Benjamin Kramere56f3932011-12-23 17:00:35 +00003563 // C11 _Atomic()
Eli Friedman0dfb8892011-10-06 23:00:33 +00003564 case tok::kw__Atomic:
3565 return true;
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003566 }
3567}
3568
Chris Lattneracd58a32006-08-06 17:24:14 +00003569/// isDeclarationSpecifier() - Return true if the current token is part of a
3570/// declaration specifier.
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00003571///
3572/// \param DisambiguatingWithExpression True to indicate that the purpose of
3573/// this check is to disambiguate between an expression and a declaration.
3574bool Parser::isDeclarationSpecifier(bool DisambiguatingWithExpression) {
Chris Lattneracd58a32006-08-06 17:24:14 +00003575 switch (Tok.getKind()) {
3576 default: return false;
Mike Stump11289f42009-09-09 15:08:12 +00003577
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003578 case tok::kw_private:
David Blaikiebbafb8a2012-03-11 07:00:24 +00003579 return getLangOpts().OpenCL;
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003580
Chris Lattner020bab92009-01-04 23:41:41 +00003581 case tok::identifier: // foo::bar
Steve Naroff9527bbf2009-03-09 21:12:44 +00003582 // Unfortunate hack to support "Class.factoryMethod" notation.
David Blaikiebbafb8a2012-03-11 07:00:24 +00003583 if (getLangOpts().ObjC1 && NextToken().is(tok::period))
Steve Naroff9527bbf2009-03-09 21:12:44 +00003584 return false;
John Thompson22334602010-02-05 00:12:22 +00003585 if (TryAltiVecVectorToken())
3586 return true;
3587 // Fall through.
David Blaikie15a430a2011-12-04 05:04:18 +00003588 case tok::kw_decltype: // decltype(T())::type
Douglas Gregor333489b2009-03-27 23:10:48 +00003589 case tok::kw_typename: // typename T::type
Chris Lattner020bab92009-01-04 23:41:41 +00003590 // Annotate typenames and C++ scope specifiers. If we get one, just
3591 // recurse to handle whatever we get.
3592 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00003593 return true;
3594 if (Tok.is(tok::identifier))
3595 return false;
Chad Rosierc1183952012-06-26 22:30:43 +00003596
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00003597 // If we're in Objective-C and we have an Objective-C class type followed
Chad Rosierc1183952012-06-26 22:30:43 +00003598 // by an identifier and then either ':' or ']', in a place where an
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00003599 // expression is permitted, then this is probably a class message send
3600 // missing the initial '['. In this case, we won't consider this to be
3601 // the start of a declaration.
Chad Rosierc1183952012-06-26 22:30:43 +00003602 if (DisambiguatingWithExpression &&
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00003603 isStartOfObjCClassMessageMissingOpenBracket())
3604 return false;
Chad Rosierc1183952012-06-26 22:30:43 +00003605
John McCall1f476a12010-02-26 08:45:28 +00003606 return isDeclarationSpecifier();
3607
Chris Lattner020bab92009-01-04 23:41:41 +00003608 case tok::coloncolon: // ::foo::bar
3609 if (NextToken().is(tok::kw_new) || // ::new
3610 NextToken().is(tok::kw_delete)) // ::delete
3611 return false;
Mike Stump11289f42009-09-09 15:08:12 +00003612
Chris Lattner020bab92009-01-04 23:41:41 +00003613 // Annotate typenames and C++ scope specifiers. If we get one, just
3614 // recurse to handle whatever we get.
3615 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00003616 return true;
3617 return isDeclarationSpecifier();
Mike Stump11289f42009-09-09 15:08:12 +00003618
Chris Lattneracd58a32006-08-06 17:24:14 +00003619 // storage-class-specifier
3620 case tok::kw_typedef:
3621 case tok::kw_extern:
Steve Naroff2050b0d2007-12-18 00:16:02 +00003622 case tok::kw___private_extern__:
Chris Lattneracd58a32006-08-06 17:24:14 +00003623 case tok::kw_static:
3624 case tok::kw_auto:
3625 case tok::kw_register:
3626 case tok::kw___thread:
Mike Stump11289f42009-09-09 15:08:12 +00003627
Douglas Gregor26701a42011-09-09 02:06:17 +00003628 // Modules
3629 case tok::kw___module_private__:
Chad Rosierc1183952012-06-26 22:30:43 +00003630
Chris Lattneracd58a32006-08-06 17:24:14 +00003631 // type-specifiers
3632 case tok::kw_short:
3633 case tok::kw_long:
Francois Pichet84133e42011-04-28 01:59:37 +00003634 case tok::kw___int64:
Richard Smithf016bbc2012-04-04 06:24:32 +00003635 case tok::kw___int128:
Chris Lattneracd58a32006-08-06 17:24:14 +00003636 case tok::kw_signed:
3637 case tok::kw_unsigned:
3638 case tok::kw__Complex:
3639 case tok::kw__Imaginary:
3640 case tok::kw_void:
3641 case tok::kw_char:
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +00003642 case tok::kw_wchar_t:
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00003643 case tok::kw_char16_t:
3644 case tok::kw_char32_t:
3645
Chris Lattneracd58a32006-08-06 17:24:14 +00003646 case tok::kw_int:
Anton Korobeynikovf0c267e2011-10-14 23:23:15 +00003647 case tok::kw_half:
Chris Lattneracd58a32006-08-06 17:24:14 +00003648 case tok::kw_float:
3649 case tok::kw_double:
Chris Lattnerbb31a422007-11-15 05:25:19 +00003650 case tok::kw_bool:
Chris Lattneracd58a32006-08-06 17:24:14 +00003651 case tok::kw__Bool:
3652 case tok::kw__Decimal32:
3653 case tok::kw__Decimal64:
3654 case tok::kw__Decimal128:
John Thompson22334602010-02-05 00:12:22 +00003655 case tok::kw___vector:
Mike Stump11289f42009-09-09 15:08:12 +00003656
Chris Lattner861a2262008-04-13 18:59:07 +00003657 // struct-or-union-specifier (C99) or class-specifier (C++)
3658 case tok::kw_class:
Chris Lattneracd58a32006-08-06 17:24:14 +00003659 case tok::kw_struct:
3660 case tok::kw_union:
3661 // enum-specifier
3662 case tok::kw_enum:
Mike Stump11289f42009-09-09 15:08:12 +00003663
Chris Lattneracd58a32006-08-06 17:24:14 +00003664 // type-qualifier
3665 case tok::kw_const:
3666 case tok::kw_volatile:
3667 case tok::kw_restrict:
Steve Naroffad373bd2007-07-31 12:34:36 +00003668
Chris Lattneracd58a32006-08-06 17:24:14 +00003669 // function-specifier
3670 case tok::kw_inline:
Douglas Gregor61956c42008-10-31 09:07:45 +00003671 case tok::kw_virtual:
3672 case tok::kw_explicit:
Chris Lattner7b20dc72007-08-09 16:40:21 +00003673
Peter Collingbourne3d9cbdc2011-04-15 00:35:57 +00003674 // static_assert-declaration
3675 case tok::kw__Static_assert:
3676
Chris Lattner599e47e2007-08-09 17:01:07 +00003677 // GNU typeof support.
3678 case tok::kw_typeof:
Mike Stump11289f42009-09-09 15:08:12 +00003679
Chris Lattner599e47e2007-08-09 17:01:07 +00003680 // GNU attributes.
Chris Lattner7b20dc72007-08-09 16:40:21 +00003681 case tok::kw___attribute:
Chris Lattneracd58a32006-08-06 17:24:14 +00003682 return true;
Mike Stump11289f42009-09-09 15:08:12 +00003683
Francois Pichete878cb62011-06-19 08:02:06 +00003684 // C++0x decltype.
David Blaikie15a430a2011-12-04 05:04:18 +00003685 case tok::annot_decltype:
Francois Pichete878cb62011-06-19 08:02:06 +00003686 return true;
3687
Benjamin Kramere56f3932011-12-23 17:00:35 +00003688 // C11 _Atomic()
Eli Friedman0dfb8892011-10-06 23:00:33 +00003689 case tok::kw__Atomic:
3690 return true;
3691
Chris Lattner8b2ec162008-07-26 03:38:44 +00003692 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
3693 case tok::less:
David Blaikiebbafb8a2012-03-11 07:00:24 +00003694 return getLangOpts().ObjC1;
Mike Stump11289f42009-09-09 15:08:12 +00003695
Douglas Gregor19b7acf2011-04-27 05:41:15 +00003696 // typedef-name
3697 case tok::annot_typename:
3698 return !DisambiguatingWithExpression ||
3699 !isStartOfObjCClassMessageMissingOpenBracket();
Chad Rosierc1183952012-06-26 22:30:43 +00003700
Steve Narofff192fab2009-01-06 19:34:12 +00003701 case tok::kw___declspec:
Steve Naroff44ac7772008-12-25 14:16:32 +00003702 case tok::kw___cdecl:
3703 case tok::kw___stdcall:
3704 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00003705 case tok::kw___thiscall:
Eli Friedman53339e02009-06-08 23:27:34 +00003706 case tok::kw___w64:
3707 case tok::kw___ptr64:
Francois Pichetf2fb4112011-08-25 00:36:46 +00003708 case tok::kw___ptr32:
Eli Friedman53339e02009-06-08 23:27:34 +00003709 case tok::kw___forceinline:
Dawn Perchik335e16b2010-09-03 01:29:35 +00003710 case tok::kw___pascal:
Francois Pichet17ed0202011-08-18 09:59:55 +00003711 case tok::kw___unaligned:
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003712
3713 case tok::kw___private:
3714 case tok::kw___local:
3715 case tok::kw___global:
3716 case tok::kw___constant:
3717 case tok::kw___read_only:
3718 case tok::kw___read_write:
3719 case tok::kw___write_only:
3720
Eli Friedman53339e02009-06-08 23:27:34 +00003721 return true;
Chris Lattneracd58a32006-08-06 17:24:14 +00003722 }
3723}
3724
Douglas Gregor9de54ea2010-01-13 17:31:36 +00003725bool Parser::isConstructorDeclarator() {
3726 TentativeParsingAction TPA(*this);
3727
3728 // Parse the C++ scope specifier.
3729 CXXScopeSpec SS;
Chad Rosierc1183952012-06-26 22:30:43 +00003730 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
Douglas Gregordf593fb2011-11-07 17:33:42 +00003731 /*EnteringContext=*/true)) {
John McCall1f476a12010-02-26 08:45:28 +00003732 TPA.Revert();
3733 return false;
3734 }
Douglas Gregor9de54ea2010-01-13 17:31:36 +00003735
3736 // Parse the constructor name.
3737 if (Tok.is(tok::identifier) || Tok.is(tok::annot_template_id)) {
3738 // We already know that we have a constructor name; just consume
3739 // the token.
3740 ConsumeToken();
3741 } else {
3742 TPA.Revert();
3743 return false;
3744 }
3745
Richard Smith43f340f2012-03-27 23:05:05 +00003746 // Current class name must be followed by a left parenthesis.
Douglas Gregor9de54ea2010-01-13 17:31:36 +00003747 if (Tok.isNot(tok::l_paren)) {
3748 TPA.Revert();
3749 return false;
3750 }
3751 ConsumeParen();
3752
Richard Smith43f340f2012-03-27 23:05:05 +00003753 // A right parenthesis, or ellipsis followed by a right parenthesis signals
3754 // that we have a constructor.
3755 if (Tok.is(tok::r_paren) ||
3756 (Tok.is(tok::ellipsis) && NextToken().is(tok::r_paren))) {
Douglas Gregor9de54ea2010-01-13 17:31:36 +00003757 TPA.Revert();
3758 return true;
3759 }
3760
3761 // If we need to, enter the specified scope.
3762 DeclaratorScopeObj DeclScopeObj(*this, SS);
Douglas Gregor0be31a22010-07-02 17:43:08 +00003763 if (SS.isSet() && Actions.ShouldEnterDeclaratorScope(getCurScope(), SS))
Douglas Gregor9de54ea2010-01-13 17:31:36 +00003764 DeclScopeObj.EnterDeclaratorScope();
3765
Francois Pichet79f3a872011-01-31 04:54:32 +00003766 // Optionally skip Microsoft attributes.
John McCall084e83d2011-03-24 11:26:52 +00003767 ParsedAttributes Attrs(AttrFactory);
Francois Pichet79f3a872011-01-31 04:54:32 +00003768 MaybeParseMicrosoftAttributes(Attrs);
3769
Douglas Gregor9de54ea2010-01-13 17:31:36 +00003770 // Check whether the next token(s) are part of a declaration
3771 // specifier, in which case we have the start of a parameter and,
3772 // therefore, we know that this is a constructor.
Richard Smithefd009d2012-03-27 00:56:56 +00003773 bool IsConstructor = false;
3774 if (isDeclarationSpecifier())
3775 IsConstructor = true;
3776 else if (Tok.is(tok::identifier) ||
3777 (Tok.is(tok::annot_cxxscope) && NextToken().is(tok::identifier))) {
3778 // We've seen "C ( X" or "C ( X::Y", but "X" / "X::Y" is not a type.
3779 // This might be a parenthesized member name, but is more likely to
3780 // be a constructor declaration with an invalid argument type. Keep
3781 // looking.
3782 if (Tok.is(tok::annot_cxxscope))
3783 ConsumeToken();
3784 ConsumeToken();
3785
3786 // If this is not a constructor, we must be parsing a declarator,
Richard Smith1453e312012-03-27 01:42:32 +00003787 // which must have one of the following syntactic forms (see the
3788 // grammar extract at the start of ParseDirectDeclarator):
Richard Smithefd009d2012-03-27 00:56:56 +00003789 switch (Tok.getKind()) {
3790 case tok::l_paren:
3791 // C(X ( int));
3792 case tok::l_square:
3793 // C(X [ 5]);
3794 // C(X [ [attribute]]);
3795 case tok::coloncolon:
3796 // C(X :: Y);
3797 // C(X :: *p);
3798 case tok::r_paren:
3799 // C(X )
3800 // Assume this isn't a constructor, rather than assuming it's a
3801 // constructor with an unnamed parameter of an ill-formed type.
3802 break;
3803
3804 default:
3805 IsConstructor = true;
3806 break;
3807 }
3808 }
3809
Douglas Gregor9de54ea2010-01-13 17:31:36 +00003810 TPA.Revert();
3811 return IsConstructor;
3812}
Chris Lattnerb9093cd2006-08-04 04:39:53 +00003813
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003814/// ParseTypeQualifierListOpt
Dawn Perchik335e16b2010-09-03 01:29:35 +00003815/// type-qualifier-list: [C99 6.7.5]
3816/// type-qualifier
Chad Rosierc1183952012-06-26 22:30:43 +00003817/// [vendor] attributes
Dawn Perchik335e16b2010-09-03 01:29:35 +00003818/// [ only if VendorAttributesAllowed=true ]
3819/// type-qualifier-list type-qualifier
Chad Rosierc1183952012-06-26 22:30:43 +00003820/// [vendor] type-qualifier-list attributes
Dawn Perchik335e16b2010-09-03 01:29:35 +00003821/// [ only if VendorAttributesAllowed=true ]
3822/// [C++0x] attribute-specifier[opt] is allowed before cv-qualifier-seq
3823/// [ only if CXX0XAttributesAllowed=true ]
3824/// Note: vendor can be GNU, MS, etc.
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003825///
Dawn Perchik335e16b2010-09-03 01:29:35 +00003826void Parser::ParseTypeQualifierListOpt(DeclSpec &DS,
3827 bool VendorAttributesAllowed,
Richard Smith3dff2512012-04-10 03:25:07 +00003828 bool CXX11AttributesAllowed) {
3829 if (getLangOpts().CPlusPlus0x && CXX11AttributesAllowed &&
Richard Smith7bdcc4a2012-04-10 01:32:12 +00003830 isCXX11AttributeSpecifier()) {
John McCall084e83d2011-03-24 11:26:52 +00003831 ParsedAttributesWithRange attrs(AttrFactory);
Richard Smith3dff2512012-04-10 03:25:07 +00003832 ParseCXX11Attributes(attrs);
Richard Smith7bdcc4a2012-04-10 01:32:12 +00003833 DS.takeAttributesFrom(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +00003834 }
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +00003835
3836 SourceLocation EndLoc;
3837
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003838 while (1) {
John McCall49bfce42009-08-03 20:12:06 +00003839 bool isInvalid = false;
Chris Lattnerd9c3c592006-08-05 06:26:47 +00003840 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00003841 unsigned DiagID = 0;
Chris Lattner60809f52006-11-28 05:18:46 +00003842 SourceLocation Loc = Tok.getLocation();
Chris Lattnerd9c3c592006-08-05 06:26:47 +00003843
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003844 switch (Tok.getKind()) {
Douglas Gregor28c78432010-08-27 17:35:51 +00003845 case tok::code_completion:
3846 Actions.CodeCompleteTypeQualifiers(DS);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00003847 return cutOffParsing();
Chad Rosierc1183952012-06-26 22:30:43 +00003848
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003849 case tok::kw_const:
John McCall49bfce42009-08-03 20:12:06 +00003850 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, DiagID,
David Blaikiebbafb8a2012-03-11 07:00:24 +00003851 getLangOpts());
Chris Lattnerd9c3c592006-08-05 06:26:47 +00003852 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003853 case tok::kw_volatile:
John McCall49bfce42009-08-03 20:12:06 +00003854 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
David Blaikiebbafb8a2012-03-11 07:00:24 +00003855 getLangOpts());
Chris Lattnerd9c3c592006-08-05 06:26:47 +00003856 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003857 case tok::kw_restrict:
John McCall49bfce42009-08-03 20:12:06 +00003858 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
David Blaikiebbafb8a2012-03-11 07:00:24 +00003859 getLangOpts());
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003860 break;
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003861
3862 // OpenCL qualifiers:
Chad Rosierc1183952012-06-26 22:30:43 +00003863 case tok::kw_private:
David Blaikiebbafb8a2012-03-11 07:00:24 +00003864 if (!getLangOpts().OpenCL)
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003865 goto DoneWithTypeQuals;
3866 case tok::kw___private:
3867 case tok::kw___global:
3868 case tok::kw___local:
3869 case tok::kw___constant:
3870 case tok::kw___read_only:
3871 case tok::kw___write_only:
3872 case tok::kw___read_write:
3873 ParseOpenCLQualifiers(DS);
3874 break;
3875
Eli Friedman53339e02009-06-08 23:27:34 +00003876 case tok::kw___w64:
Steve Narofff9c29d42008-12-25 14:41:26 +00003877 case tok::kw___ptr64:
Francois Pichetf2fb4112011-08-25 00:36:46 +00003878 case tok::kw___ptr32:
Steve Naroff44ac7772008-12-25 14:16:32 +00003879 case tok::kw___cdecl:
3880 case tok::kw___stdcall:
3881 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00003882 case tok::kw___thiscall:
Francois Pichet17ed0202011-08-18 09:59:55 +00003883 case tok::kw___unaligned:
Dawn Perchik335e16b2010-09-03 01:29:35 +00003884 if (VendorAttributesAllowed) {
John McCall53fa7142010-12-24 02:08:15 +00003885 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman53339e02009-06-08 23:27:34 +00003886 continue;
3887 }
3888 goto DoneWithTypeQuals;
Dawn Perchik335e16b2010-09-03 01:29:35 +00003889 case tok::kw___pascal:
3890 if (VendorAttributesAllowed) {
John McCall53fa7142010-12-24 02:08:15 +00003891 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik335e16b2010-09-03 01:29:35 +00003892 continue;
3893 }
3894 goto DoneWithTypeQuals;
Chris Lattnere37e2332006-08-15 04:50:22 +00003895 case tok::kw___attribute:
Dawn Perchik335e16b2010-09-03 01:29:35 +00003896 if (VendorAttributesAllowed) {
John McCall53fa7142010-12-24 02:08:15 +00003897 ParseGNUAttributes(DS.getAttributes());
Chris Lattnercf0bab22008-12-18 07:02:59 +00003898 continue; // do *not* consume the next token!
3899 }
3900 // otherwise, FALL THROUGH!
3901 default:
Steve Naroff44ac7772008-12-25 14:16:32 +00003902 DoneWithTypeQuals:
Chris Lattnercf0bab22008-12-18 07:02:59 +00003903 // If this is not a type-qualifier token, we're done reading type
3904 // qualifiers. First verify that DeclSpec's are consistent.
Douglas Gregore3e01a22009-04-01 22:41:11 +00003905 DS.Finish(Diags, PP);
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +00003906 if (EndLoc.isValid())
3907 DS.SetRangeEnd(EndLoc);
Chris Lattnercf0bab22008-12-18 07:02:59 +00003908 return;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003909 }
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00003910
Chris Lattnerd9c3c592006-08-05 06:26:47 +00003911 // If the specifier combination wasn't legal, issue a diagnostic.
3912 if (isInvalid) {
3913 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner6d29c102008-11-18 07:48:38 +00003914 Diag(Tok, DiagID) << PrevSpec;
Chris Lattnerd9c3c592006-08-05 06:26:47 +00003915 }
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +00003916 EndLoc = ConsumeToken();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003917 }
3918}
3919
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00003920
3921/// ParseDeclarator - Parse and verify a newly-initialized declarator.
3922///
3923void Parser::ParseDeclarator(Declarator &D) {
3924 /// This implements the 'declarator' production in the C grammar, then checks
3925 /// for well-formedness and issues diagnostics.
Sebastian Redlbd150f42008-11-21 19:14:01 +00003926 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00003927}
3928
Richard Smith0efa75c2012-03-29 01:16:42 +00003929static bool isPtrOperatorToken(tok::TokenKind Kind, const LangOptions &Lang) {
3930 if (Kind == tok::star || Kind == tok::caret)
3931 return true;
3932
3933 // We parse rvalue refs in C++03, because otherwise the errors are scary.
3934 if (!Lang.CPlusPlus)
3935 return false;
3936
3937 return Kind == tok::amp || Kind == tok::ampamp;
3938}
3939
Sebastian Redlbd150f42008-11-21 19:14:01 +00003940/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
3941/// is parsed by the function passed to it. Pass null, and the direct-declarator
3942/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregordbc5daf2008-11-07 20:08:42 +00003943/// ptr-operator production.
3944///
Richard Smith09f76ee2011-10-19 21:33:05 +00003945/// If the grammar of this construct is extended, matching changes must also be
Richard Smith1453e312012-03-27 01:42:32 +00003946/// made to TryParseDeclarator and MightBeDeclarator, and possibly to
3947/// isConstructorDeclarator.
Richard Smith09f76ee2011-10-19 21:33:05 +00003948///
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00003949/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
3950/// [C] pointer[opt] direct-declarator
3951/// [C++] direct-declarator
3952/// [C++] ptr-operator declarator
Chris Lattner6c7416c2006-08-07 00:19:33 +00003953///
3954/// pointer: [C99 6.7.5]
3955/// '*' type-qualifier-list[opt]
3956/// '*' type-qualifier-list[opt] pointer
3957///
Douglas Gregordbc5daf2008-11-07 20:08:42 +00003958/// ptr-operator:
3959/// '*' cv-qualifier-seq[opt]
3960/// '&'
Sebastian Redled0f3b02009-03-15 22:02:01 +00003961/// [C++0x] '&&'
Douglas Gregordbc5daf2008-11-07 20:08:42 +00003962/// [GNU] '&' restrict[opt] attributes[opt]
Sebastian Redled0f3b02009-03-15 22:02:01 +00003963/// [GNU?] '&&' restrict[opt] attributes[opt]
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00003964/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Sebastian Redlbd150f42008-11-21 19:14:01 +00003965void Parser::ParseDeclaratorInternal(Declarator &D,
3966 DirectDeclParseFunction DirectDeclParser) {
Douglas Gregor66a985d2009-08-26 14:27:30 +00003967 if (Diags.hasAllExtensionsSilenced())
3968 D.setExtension();
Chad Rosierc1183952012-06-26 22:30:43 +00003969
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00003970 // C++ member pointers start with a '::' or a nested-name.
3971 // Member pointers get special handling, since there's no place for the
3972 // scope spec in the generic path below.
David Blaikiebbafb8a2012-03-11 07:00:24 +00003973 if (getLangOpts().CPlusPlus &&
Chris Lattner803802d2009-03-24 17:04:48 +00003974 (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) ||
3975 Tok.is(tok::annot_cxxscope))) {
Douglas Gregordf593fb2011-11-07 17:33:42 +00003976 bool EnteringContext = D.getContext() == Declarator::FileContext ||
3977 D.getContext() == Declarator::MemberContext;
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00003978 CXXScopeSpec SS;
Douglas Gregordf593fb2011-11-07 17:33:42 +00003979 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), EnteringContext);
John McCall1f476a12010-02-26 08:45:28 +00003980
Jeffrey Yasskin4e150f82010-04-07 23:29:58 +00003981 if (SS.isNotEmpty()) {
Mike Stump11289f42009-09-09 15:08:12 +00003982 if (Tok.isNot(tok::star)) {
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00003983 // The scope spec really belongs to the direct-declarator.
3984 D.getCXXScopeSpec() = SS;
3985 if (DirectDeclParser)
3986 (this->*DirectDeclParser)(D);
3987 return;
3988 }
3989
3990 SourceLocation Loc = ConsumeToken();
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003991 D.SetRangeEnd(Loc);
John McCall084e83d2011-03-24 11:26:52 +00003992 DeclSpec DS(AttrFactory);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00003993 ParseTypeQualifierListOpt(DS);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003994 D.ExtendWithDeclSpec(DS);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00003995
3996 // Recurse to parse whatever is left.
3997 ParseDeclaratorInternal(D, DirectDeclParser);
3998
3999 // Sema will have to catch (syntactically invalid) pointers into global
4000 // scope. It has to catch pointers into namespace scope anyway.
4001 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
John McCall084e83d2011-03-24 11:26:52 +00004002 Loc),
4003 DS.getAttributes(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004004 /* Don't replace range end. */SourceLocation());
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004005 return;
4006 }
4007 }
4008
4009 tok::TokenKind Kind = Tok.getKind();
Steve Naroffec33ed92008-08-27 16:04:49 +00004010 // Not a pointer, C++ reference, or block.
Richard Smith0efa75c2012-03-29 01:16:42 +00004011 if (!isPtrOperatorToken(Kind, getLangOpts())) {
Sebastian Redlbd150f42008-11-21 19:14:01 +00004012 if (DirectDeclParser)
4013 (this->*DirectDeclParser)(D);
Douglas Gregordbc5daf2008-11-07 20:08:42 +00004014 return;
4015 }
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004016
Sebastian Redled0f3b02009-03-15 22:02:01 +00004017 // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference,
4018 // '&&' -> rvalue reference
Sebastian Redl3b27be62009-03-23 00:00:23 +00004019 SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&.
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004020 D.SetRangeEnd(Loc);
Bill Wendling3708c182007-05-27 10:15:43 +00004021
Chris Lattner9eac9312009-03-27 04:18:06 +00004022 if (Kind == tok::star || Kind == tok::caret) {
Chris Lattner788404f2008-02-21 01:32:26 +00004023 // Is a pointer.
John McCall084e83d2011-03-24 11:26:52 +00004024 DeclSpec DS(AttrFactory);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004025
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004026 // FIXME: GNU attributes are not allowed here in a new-type-id.
Bill Wendling3708c182007-05-27 10:15:43 +00004027 ParseTypeQualifierListOpt(DS);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004028 D.ExtendWithDeclSpec(DS);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004029
Bill Wendling3708c182007-05-27 10:15:43 +00004030 // Recursively parse the declarator.
Sebastian Redlbd150f42008-11-21 19:14:01 +00004031 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroffec33ed92008-08-27 16:04:49 +00004032 if (Kind == tok::star)
4033 // Remember that we parsed a pointer type, and remember the type-quals.
4034 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
Chandler Carruthe71b378d2011-02-23 18:51:59 +00004035 DS.getConstSpecLoc(),
4036 DS.getVolatileSpecLoc(),
John McCall084e83d2011-03-24 11:26:52 +00004037 DS.getRestrictSpecLoc()),
4038 DS.getAttributes(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004039 SourceLocation());
Steve Naroffec33ed92008-08-27 16:04:49 +00004040 else
4041 // Remember that we parsed a Block type, and remember the type-quals.
Mike Stump11289f42009-09-09 15:08:12 +00004042 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
John McCall084e83d2011-03-24 11:26:52 +00004043 Loc),
4044 DS.getAttributes(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004045 SourceLocation());
Bill Wendling3708c182007-05-27 10:15:43 +00004046 } else {
4047 // Is a reference
John McCall084e83d2011-03-24 11:26:52 +00004048 DeclSpec DS(AttrFactory);
Bill Wendling93efb222007-06-02 23:28:54 +00004049
Sebastian Redl3b27be62009-03-23 00:00:23 +00004050 // Complain about rvalue references in C++03, but then go on and build
4051 // the declarator.
Richard Smith5d164bc2011-10-15 05:09:34 +00004052 if (Kind == tok::ampamp)
David Blaikiebbafb8a2012-03-11 07:00:24 +00004053 Diag(Loc, getLangOpts().CPlusPlus0x ?
Richard Smith5d164bc2011-10-15 05:09:34 +00004054 diag::warn_cxx98_compat_rvalue_reference :
4055 diag::ext_rvalue_reference);
Sebastian Redl3b27be62009-03-23 00:00:23 +00004056
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004057 // GNU-style and C++11 attributes are allowed here, as is restrict.
4058 ParseTypeQualifierListOpt(DS);
4059 D.ExtendWithDeclSpec(DS);
4060
Bill Wendling93efb222007-06-02 23:28:54 +00004061 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
4062 // cv-qualifiers are introduced through the use of a typedef or of a
4063 // template type argument, in which case the cv-qualifiers are ignored.
Bill Wendling93efb222007-06-02 23:28:54 +00004064 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
4065 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
4066 Diag(DS.getConstSpecLoc(),
Chris Lattner6d29c102008-11-18 07:48:38 +00004067 diag::err_invalid_reference_qualifier_application) << "const";
Bill Wendling93efb222007-06-02 23:28:54 +00004068 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
4069 Diag(DS.getVolatileSpecLoc(),
Chris Lattner6d29c102008-11-18 07:48:38 +00004070 diag::err_invalid_reference_qualifier_application) << "volatile";
Bill Wendling93efb222007-06-02 23:28:54 +00004071 }
Bill Wendling3708c182007-05-27 10:15:43 +00004072
4073 // Recursively parse the declarator.
Sebastian Redlbd150f42008-11-21 19:14:01 +00004074 ParseDeclaratorInternal(D, DirectDeclParser);
Bill Wendling3708c182007-05-27 10:15:43 +00004075
Douglas Gregor66583c52008-11-03 15:51:28 +00004076 if (D.getNumTypeObjects() > 0) {
4077 // C++ [dcl.ref]p4: There shall be no references to references.
4078 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
4079 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattnerebad6a22008-11-19 07:37:42 +00004080 if (const IdentifierInfo *II = D.getIdentifier())
4081 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
4082 << II;
4083 else
4084 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
4085 << "type name";
Douglas Gregor66583c52008-11-03 15:51:28 +00004086
Sebastian Redlbd150f42008-11-21 19:14:01 +00004087 // Once we've complained about the reference-to-reference, we
Douglas Gregor66583c52008-11-03 15:51:28 +00004088 // can go ahead and build the (technically ill-formed)
4089 // declarator: reference collapsing will take care of it.
4090 }
4091 }
4092
Bill Wendling3708c182007-05-27 10:15:43 +00004093 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner788404f2008-02-21 01:32:26 +00004094 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
Sebastian Redled0f3b02009-03-15 22:02:01 +00004095 Kind == tok::amp),
John McCall084e83d2011-03-24 11:26:52 +00004096 DS.getAttributes(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004097 SourceLocation());
Bill Wendling3708c182007-05-27 10:15:43 +00004098 }
Chris Lattner6c7416c2006-08-07 00:19:33 +00004099}
4100
Richard Smith0efa75c2012-03-29 01:16:42 +00004101static void diagnoseMisplacedEllipsis(Parser &P, Declarator &D,
4102 SourceLocation EllipsisLoc) {
4103 if (EllipsisLoc.isValid()) {
4104 FixItHint Insertion;
4105 if (!D.getEllipsisLoc().isValid()) {
4106 Insertion = FixItHint::CreateInsertion(D.getIdentifierLoc(), "...");
4107 D.setEllipsisLoc(EllipsisLoc);
4108 }
4109 P.Diag(EllipsisLoc, diag::err_misplaced_ellipsis_in_declaration)
4110 << FixItHint::CreateRemoval(EllipsisLoc) << Insertion << !D.hasName();
4111 }
4112}
4113
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004114/// ParseDirectDeclarator
4115/// direct-declarator: [C99 6.7.5]
Douglas Gregor831c93f2008-11-05 20:51:48 +00004116/// [C99] identifier
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004117/// '(' declarator ')'
4118/// [GNU] '(' attributes declarator ')'
Chris Lattnere8074e62006-08-06 18:30:15 +00004119/// [C90] direct-declarator '[' constant-expression[opt] ']'
4120/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
4121/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
4122/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
4123/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004124/// [C++11] direct-declarator '[' constant-expression[opt] ']'
4125/// attribute-specifier-seq[opt]
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004126/// direct-declarator '(' parameter-type-list ')'
4127/// direct-declarator '(' identifier-list[opt] ')'
4128/// [GNU] direct-declarator '(' parameter-forward-declarations
4129/// parameter-type-list[opt] ')'
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00004130/// [C++] direct-declarator '(' parameter-declaration-clause ')'
4131/// cv-qualifier-seq[opt] exception-specification[opt]
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004132/// [C++11] direct-declarator '(' parameter-declaration-clause ')'
4133/// attribute-specifier-seq[opt] cv-qualifier-seq[opt]
4134/// ref-qualifier[opt] exception-specification[opt]
Douglas Gregor61956c42008-10-31 09:07:45 +00004135/// [C++] declarator-id
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004136/// [C++11] declarator-id attribute-specifier-seq[opt]
Douglas Gregor831c93f2008-11-05 20:51:48 +00004137///
4138/// declarator-id: [C++ 8]
Douglas Gregor27b4c162010-12-23 22:44:42 +00004139/// '...'[opt] id-expression
Douglas Gregor831c93f2008-11-05 20:51:48 +00004140/// '::'[opt] nested-name-specifier[opt] type-name
4141///
4142/// id-expression: [C++ 5.1]
4143/// unqualified-id
Douglas Gregord90fd522009-09-25 21:45:23 +00004144/// qualified-id
Douglas Gregor831c93f2008-11-05 20:51:48 +00004145///
4146/// unqualified-id: [C++ 5.1]
Mike Stump11289f42009-09-09 15:08:12 +00004147/// identifier
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00004148/// operator-function-id
Douglas Gregord90fd522009-09-25 21:45:23 +00004149/// conversion-function-id
Mike Stump11289f42009-09-09 15:08:12 +00004150/// '~' class-name
Douglas Gregor7f741122009-02-25 19:37:18 +00004151/// template-id
Argyrios Kyrtzidise4426352008-11-07 22:02:30 +00004152///
Richard Smith1453e312012-03-27 01:42:32 +00004153/// Note, any additional constructs added here may need corresponding changes
4154/// in isConstructorDeclarator.
Chris Lattneracd58a32006-08-06 17:24:14 +00004155void Parser::ParseDirectDeclarator(Declarator &D) {
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00004156 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00004157
David Blaikiebbafb8a2012-03-11 07:00:24 +00004158 if (getLangOpts().CPlusPlus && D.mayHaveIdentifier()) {
Douglas Gregor7861a802009-11-03 01:35:08 +00004159 // ParseDeclaratorInternal might already have parsed the scope.
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00004160 if (D.getCXXScopeSpec().isEmpty()) {
Douglas Gregordf593fb2011-11-07 17:33:42 +00004161 bool EnteringContext = D.getContext() == Declarator::FileContext ||
4162 D.getContext() == Declarator::MemberContext;
Chad Rosierc1183952012-06-26 22:30:43 +00004163 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec(), ParsedType(),
Douglas Gregordf593fb2011-11-07 17:33:42 +00004164 EnteringContext);
John McCall1f476a12010-02-26 08:45:28 +00004165 }
4166
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00004167 if (D.getCXXScopeSpec().isValid()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00004168 if (Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
John McCall2b058ef2009-12-11 20:04:54 +00004169 // Change the declaration context for name lookup, until this function
4170 // is exited (and the declarator has been parsed).
4171 DeclScopeObj.EnterDeclaratorScope();
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00004172 }
4173
Douglas Gregor27b4c162010-12-23 22:44:42 +00004174 // C++0x [dcl.fct]p14:
4175 // There is a syntactic ambiguity when an ellipsis occurs at the end
Chad Rosierc1183952012-06-26 22:30:43 +00004176 // of a parameter-declaration-clause without a preceding comma. In
4177 // this case, the ellipsis is parsed as part of the
4178 // abstract-declarator if the type of the parameter names a template
Douglas Gregor27b4c162010-12-23 22:44:42 +00004179 // parameter pack that has not been expanded; otherwise, it is parsed
4180 // as part of the parameter-declaration-clause.
Richard Smith0efa75c2012-03-29 01:16:42 +00004181 if (Tok.is(tok::ellipsis) && D.getCXXScopeSpec().isEmpty() &&
Douglas Gregor27b4c162010-12-23 22:44:42 +00004182 !((D.getContext() == Declarator::PrototypeContext ||
4183 D.getContext() == Declarator::BlockLiteralContext) &&
Douglas Gregor27b4c162010-12-23 22:44:42 +00004184 NextToken().is(tok::r_paren) &&
Richard Smith0efa75c2012-03-29 01:16:42 +00004185 !Actions.containsUnexpandedParameterPacks(D))) {
4186 SourceLocation EllipsisLoc = ConsumeToken();
4187 if (isPtrOperatorToken(Tok.getKind(), getLangOpts())) {
4188 // The ellipsis was put in the wrong place. Recover, and explain to
4189 // the user what they should have done.
4190 ParseDeclarator(D);
4191 diagnoseMisplacedEllipsis(*this, D, EllipsisLoc);
4192 return;
4193 } else
4194 D.setEllipsisLoc(EllipsisLoc);
4195
4196 // The ellipsis can't be followed by a parenthesized declarator. We
4197 // check for that in ParseParenDeclarator, after we have disambiguated
4198 // the l_paren token.
4199 }
4200
Douglas Gregor7861a802009-11-03 01:35:08 +00004201 if (Tok.is(tok::identifier) || Tok.is(tok::kw_operator) ||
4202 Tok.is(tok::annot_template_id) || Tok.is(tok::tilde)) {
4203 // We found something that indicates the start of an unqualified-id.
4204 // Parse that unqualified-id.
John McCall84821e72010-04-13 06:39:49 +00004205 bool AllowConstructorName;
4206 if (D.getDeclSpec().hasTypeSpecifier())
4207 AllowConstructorName = false;
4208 else if (D.getCXXScopeSpec().isSet())
4209 AllowConstructorName =
4210 (D.getContext() == Declarator::FileContext ||
4211 (D.getContext() == Declarator::MemberContext &&
4212 D.getDeclSpec().isFriendSpecified()));
4213 else
4214 AllowConstructorName = (D.getContext() == Declarator::MemberContext);
4215
Abramo Bagnara7945c982012-01-27 09:46:47 +00004216 SourceLocation TemplateKWLoc;
Chad Rosierc1183952012-06-26 22:30:43 +00004217 if (ParseUnqualifiedId(D.getCXXScopeSpec(),
4218 /*EnteringContext=*/true,
4219 /*AllowDestructorName=*/true,
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004220 AllowConstructorName,
John McCallba7bf592010-08-24 05:47:05 +00004221 ParsedType(),
Abramo Bagnara7945c982012-01-27 09:46:47 +00004222 TemplateKWLoc,
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00004223 D.getName()) ||
4224 // Once we're past the identifier, if the scope was bad, mark the
4225 // whole declarator bad.
4226 D.getCXXScopeSpec().isInvalid()) {
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00004227 D.SetIdentifier(0, Tok.getLocation());
4228 D.setInvalidType(true);
Douglas Gregor7861a802009-11-03 01:35:08 +00004229 } else {
4230 // Parsed the unqualified-id; update range information and move along.
4231 if (D.getSourceRange().getBegin().isInvalid())
4232 D.SetRangeBegin(D.getName().getSourceRange().getBegin());
4233 D.SetRangeEnd(D.getName().getSourceRange().getEnd());
Douglas Gregordbc5daf2008-11-07 20:08:42 +00004234 }
Douglas Gregor7861a802009-11-03 01:35:08 +00004235 goto PastIdentifier;
Douglas Gregor11d0c4c2008-11-06 22:13:31 +00004236 }
Douglas Gregor7861a802009-11-03 01:35:08 +00004237 } else if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00004238 assert(!getLangOpts().CPlusPlus &&
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00004239 "There's a C++-specific check for tok::identifier above");
4240 assert(Tok.getIdentifierInfo() && "Not an identifier?");
4241 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
4242 ConsumeToken();
Douglas Gregor7861a802009-11-03 01:35:08 +00004243 goto PastIdentifier;
4244 }
Richard Smith0efa75c2012-03-29 01:16:42 +00004245
Douglas Gregor7861a802009-11-03 01:35:08 +00004246 if (Tok.is(tok::l_paren)) {
Chris Lattneracd58a32006-08-06 17:24:14 +00004247 // direct-declarator: '(' declarator ')'
Chris Lattnere37e2332006-08-15 04:50:22 +00004248 // direct-declarator: '(' attributes declarator ')'
Chris Lattneracd58a32006-08-06 17:24:14 +00004249 // Example: 'char (*X)' or 'int (*XX)(void)'
4250 ParseParenDeclarator(D);
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004251
4252 // If the declarator was parenthesized, we entered the declarator
4253 // scope when parsing the parenthesized declarator, then exited
4254 // the scope already. Re-enter the scope, if we need to.
4255 if (D.getCXXScopeSpec().isSet()) {
Fariborz Jahanian358acd52010-08-17 23:50:37 +00004256 // If there was an error parsing parenthesized declarator, declarator
Richard Smith0efa75c2012-03-29 01:16:42 +00004257 // scope may have been entered before. Don't do it again.
Fariborz Jahanian358acd52010-08-17 23:50:37 +00004258 if (!D.isInvalidType() &&
4259 Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004260 // Change the declaration context for name lookup, until this function
4261 // is exited (and the declarator has been parsed).
Fariborz Jahanian358acd52010-08-17 23:50:37 +00004262 DeclScopeObj.EnterDeclaratorScope();
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004263 }
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00004264 } else if (D.mayOmitIdentifier()) {
Chris Lattneracd58a32006-08-06 17:24:14 +00004265 // This could be something simple like "int" (in which case the declarator
4266 // portion is empty), if an abstract-declarator is allowed.
4267 D.SetIdentifier(0, Tok.getLocation());
4268 } else {
David Blaikie5d577a22012-06-29 22:03:56 +00004269 if (Tok.getKind() == tok::annot_pragma_parser_crash)
4270 *(volatile int*) 0x11 = 0;
Douglas Gregord9f92e22009-03-06 23:28:18 +00004271 if (D.getContext() == Declarator::MemberContext)
4272 Diag(Tok, diag::err_expected_member_name_or_semi)
4273 << D.getDeclSpec().getSourceRange();
David Blaikiebbafb8a2012-03-11 07:00:24 +00004274 else if (getLangOpts().CPlusPlus)
4275 Diag(Tok, diag::err_expected_unqualified_id) << getLangOpts().CPlusPlus;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00004276 else
Chris Lattner6d29c102008-11-18 07:48:38 +00004277 Diag(Tok, diag::err_expected_ident_lparen);
Chris Lattnereec40f92006-08-06 21:55:29 +00004278 D.SetIdentifier(0, Tok.getLocation());
Chris Lattner8c5dd732008-11-11 06:13:16 +00004279 D.setInvalidType(true);
Chris Lattneracd58a32006-08-06 17:24:14 +00004280 }
Mike Stump11289f42009-09-09 15:08:12 +00004281
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00004282 PastIdentifier:
Chris Lattneracd58a32006-08-06 17:24:14 +00004283 assert(D.isPastIdentifier() &&
4284 "Haven't past the location of the identifier yet?");
Mike Stump11289f42009-09-09 15:08:12 +00004285
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004286 // Don't parse attributes unless we have parsed an unparenthesized name.
4287 if (D.hasName() && !D.getNumTypeObjects())
John McCall53fa7142010-12-24 02:08:15 +00004288 MaybeParseCXX0XAttributes(D);
Alexis Hunt96d5c762009-11-21 08:43:09 +00004289
Chris Lattneracd58a32006-08-06 17:24:14 +00004290 while (1) {
Chris Lattner76c72282007-10-09 17:33:22 +00004291 if (Tok.is(tok::l_paren)) {
David Blaikie15a430a2011-12-04 05:04:18 +00004292 // Enter function-declaration scope, limiting any declarators to the
4293 // function prototype scope, including parameter declarators.
4294 ParseScope PrototypeScope(this,
4295 Scope::FunctionPrototypeScope|Scope::DeclScope);
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00004296 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
4297 // In such a case, check if we actually have a function declarator; if it
4298 // is not, the declarator has been fully parsed.
David Blaikiebbafb8a2012-03-11 07:00:24 +00004299 if (getLangOpts().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00004300 // When not in file scope, warn for ambiguous function declarators, just
4301 // in case the author intended it as a variable definition.
4302 bool warnIfAmbiguous = D.getContext() != Declarator::FileContext;
4303 if (!isCXXFunctionDeclarator(warnIfAmbiguous))
4304 break;
4305 }
John McCall084e83d2011-03-24 11:26:52 +00004306 ParsedAttributes attrs(AttrFactory);
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004307 BalancedDelimiterTracker T(*this, tok::l_paren);
4308 T.consumeOpen();
4309 ParseFunctionDeclarator(D, attrs, T);
David Blaikie15a430a2011-12-04 05:04:18 +00004310 PrototypeScope.Exit();
Chris Lattner76c72282007-10-09 17:33:22 +00004311 } else if (Tok.is(tok::l_square)) {
Chris Lattnere8074e62006-08-06 18:30:15 +00004312 ParseBracketDeclarator(D);
Chris Lattneracd58a32006-08-06 17:24:14 +00004313 } else {
4314 break;
4315 }
4316 }
Chad Rosierc1183952012-06-26 22:30:43 +00004317}
Chris Lattneracd58a32006-08-06 17:24:14 +00004318
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004319/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
4320/// only called before the identifier, so these are most likely just grouping
Mike Stump11289f42009-09-09 15:08:12 +00004321/// parens for precedence. If we find that these are actually function
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004322/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
4323///
4324/// direct-declarator:
4325/// '(' declarator ')'
4326/// [GNU] '(' attributes declarator ')'
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00004327/// direct-declarator '(' parameter-type-list ')'
4328/// direct-declarator '(' identifier-list[opt] ')'
4329/// [GNU] direct-declarator '(' parameter-forward-declarations
4330/// parameter-type-list[opt] ')'
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004331///
4332void Parser::ParseParenDeclarator(Declarator &D) {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004333 BalancedDelimiterTracker T(*this, tok::l_paren);
4334 T.consumeOpen();
4335
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004336 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
Mike Stump11289f42009-09-09 15:08:12 +00004337
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00004338 // Eat any attributes before we look at whether this is a grouping or function
4339 // declarator paren. If this is a grouping paren, the attribute applies to
4340 // the type being built up, for example:
4341 // int (__attribute__(()) *x)(long y)
4342 // If this ends up not being a grouping paren, the attribute applies to the
4343 // first argument, for example:
4344 // int (__attribute__(()) int x)
4345 // In either case, we need to eat any attributes to be able to determine what
4346 // sort of paren this is.
4347 //
John McCall084e83d2011-03-24 11:26:52 +00004348 ParsedAttributes attrs(AttrFactory);
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00004349 bool RequiresArg = false;
4350 if (Tok.is(tok::kw___attribute)) {
John McCall53fa7142010-12-24 02:08:15 +00004351 ParseGNUAttributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +00004352
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00004353 // We require that the argument list (if this is a non-grouping paren) be
4354 // present even if the attribute list was empty.
4355 RequiresArg = true;
4356 }
Steve Naroff44ac7772008-12-25 14:16:32 +00004357 // Eat any Microsoft extensions.
Eli Friedman53339e02009-06-08 23:27:34 +00004358 if (Tok.is(tok::kw___cdecl) || Tok.is(tok::kw___stdcall) ||
Douglas Gregora941dca2010-05-18 16:57:00 +00004359 Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___fastcall) ||
Francois Pichet17ed0202011-08-18 09:59:55 +00004360 Tok.is(tok::kw___w64) || Tok.is(tok::kw___ptr64) ||
Francois Pichetf2fb4112011-08-25 00:36:46 +00004361 Tok.is(tok::kw___ptr32) || Tok.is(tok::kw___unaligned)) {
John McCall53fa7142010-12-24 02:08:15 +00004362 ParseMicrosoftTypeAttributes(attrs);
Eli Friedman53339e02009-06-08 23:27:34 +00004363 }
Dawn Perchik335e16b2010-09-03 01:29:35 +00004364 // Eat any Borland extensions.
Ted Kremenek5eec2b02010-11-10 05:59:39 +00004365 if (Tok.is(tok::kw___pascal))
John McCall53fa7142010-12-24 02:08:15 +00004366 ParseBorlandTypeAttributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +00004367
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004368 // If we haven't past the identifier yet (or where the identifier would be
4369 // stored, if this is an abstract declarator), then this is probably just
4370 // grouping parens. However, if this could be an abstract-declarator, then
4371 // this could also be the start of function arguments (consider 'void()').
4372 bool isGrouping;
Mike Stump11289f42009-09-09 15:08:12 +00004373
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004374 if (!D.mayOmitIdentifier()) {
4375 // If this can't be an abstract-declarator, this *must* be a grouping
4376 // paren, because we haven't seen the identifier yet.
4377 isGrouping = true;
4378 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Richard Smith43f340f2012-03-27 23:05:05 +00004379 (getLangOpts().CPlusPlus && Tok.is(tok::ellipsis) &&
4380 NextToken().is(tok::r_paren)) || // C++ int(...)
Richard Smith2620cd92012-04-11 04:01:28 +00004381 isDeclarationSpecifier() || // 'int(int)' is a function.
4382 isCXX11AttributeSpecifier()) { // 'int([[]]int)' is a function.
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004383 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
4384 // considered to be a type, not a K&R identifier-list.
4385 isGrouping = false;
4386 } else {
4387 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
4388 isGrouping = true;
4389 }
Mike Stump11289f42009-09-09 15:08:12 +00004390
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004391 // If this is a grouping paren, handle:
4392 // direct-declarator: '(' declarator ')'
4393 // direct-declarator: '(' attributes declarator ')'
4394 if (isGrouping) {
Richard Smith0efa75c2012-03-29 01:16:42 +00004395 SourceLocation EllipsisLoc = D.getEllipsisLoc();
4396 D.setEllipsisLoc(SourceLocation());
4397
Argyrios Kyrtzidis8ae36842008-10-07 10:21:57 +00004398 bool hadGroupingParens = D.hasGroupingParens();
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00004399 D.setGroupingParens(true);
Sebastian Redlbd150f42008-11-21 19:14:01 +00004400 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004401 // Match the ')'.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004402 T.consumeClose();
Chad Rosierc1183952012-06-26 22:30:43 +00004403 D.AddTypeInfo(DeclaratorChunk::getParen(T.getOpenLocation(),
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004404 T.getCloseLocation()),
4405 attrs, T.getCloseLocation());
Argyrios Kyrtzidis8ae36842008-10-07 10:21:57 +00004406
4407 D.setGroupingParens(hadGroupingParens);
Richard Smith0efa75c2012-03-29 01:16:42 +00004408
4409 // An ellipsis cannot be placed outside parentheses.
4410 if (EllipsisLoc.isValid())
4411 diagnoseMisplacedEllipsis(*this, D, EllipsisLoc);
4412
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004413 return;
4414 }
Mike Stump11289f42009-09-09 15:08:12 +00004415
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004416 // Okay, if this wasn't a grouping paren, it must be the start of a function
4417 // argument list. Recognize that this declarator will never have an
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00004418 // identifier (and remember where it would have been), then call into
4419 // ParseFunctionDeclarator to handle of argument list.
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004420 D.SetIdentifier(0, Tok.getLocation());
4421
David Blaikie15a430a2011-12-04 05:04:18 +00004422 // Enter function-declaration scope, limiting any declarators to the
4423 // function prototype scope, including parameter declarators.
4424 ParseScope PrototypeScope(this,
4425 Scope::FunctionPrototypeScope|Scope::DeclScope);
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004426 ParseFunctionDeclarator(D, attrs, T, RequiresArg);
David Blaikie15a430a2011-12-04 05:04:18 +00004427 PrototypeScope.Exit();
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004428}
4429
4430/// ParseFunctionDeclarator - We are after the identifier and have parsed the
4431/// declarator D up to a paren, which indicates that we are parsing function
4432/// arguments.
Chris Lattneracd58a32006-08-06 17:24:14 +00004433///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004434/// If FirstArgAttrs is non-null, then the caller parsed those arguments
4435/// immediately after the open paren - they should be considered to be the
4436/// first argument of a parameter.
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00004437///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004438/// If RequiresArg is true, then the first argument of the function is required
4439/// to be present and required to not be an identifier list.
Douglas Gregor9e66af42011-07-05 16:44:18 +00004440///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004441/// For C++, after the parameter-list, it also parses the cv-qualifier-seq[opt],
4442/// (C++11) ref-qualifier[opt], exception-specification[opt],
4443/// (C++11) attribute-specifier-seq[opt], and (C++11) trailing-return-type[opt].
4444///
4445/// [C++11] exception-specification:
Douglas Gregor9e66af42011-07-05 16:44:18 +00004446/// dynamic-exception-specification
4447/// noexcept-specification
4448///
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004449void Parser::ParseFunctionDeclarator(Declarator &D,
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004450 ParsedAttributes &FirstArgAttrs,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004451 BalancedDelimiterTracker &Tracker,
Douglas Gregor9e66af42011-07-05 16:44:18 +00004452 bool RequiresArg) {
Chad Rosierc1183952012-06-26 22:30:43 +00004453 assert(getCurScope()->isFunctionPrototypeScope() &&
David Blaikie15a430a2011-12-04 05:04:18 +00004454 "Should call from a Function scope");
Douglas Gregor9e66af42011-07-05 16:44:18 +00004455 // lparen is already consumed!
4456 assert(D.isPastIdentifier() && "Should not call before identifier!");
4457
4458 // This should be true when the function has typed arguments.
4459 // Otherwise, it is treated as a K&R-style function.
4460 bool HasProto = false;
4461 // Build up an array of information about the parsed arguments.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004462 SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Douglas Gregor9e66af42011-07-05 16:44:18 +00004463 // Remember where we see an ellipsis, if any.
4464 SourceLocation EllipsisLoc;
4465
4466 DeclSpec DS(AttrFactory);
4467 bool RefQualifierIsLValueRef = true;
4468 SourceLocation RefQualifierLoc;
Douglas Gregore248eea2011-10-19 06:04:55 +00004469 SourceLocation ConstQualifierLoc;
4470 SourceLocation VolatileQualifierLoc;
Douglas Gregor9e66af42011-07-05 16:44:18 +00004471 ExceptionSpecificationType ESpecType = EST_None;
4472 SourceRange ESpecRange;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004473 SmallVector<ParsedType, 2> DynamicExceptions;
4474 SmallVector<SourceRange, 2> DynamicExceptionRanges;
Douglas Gregor9e66af42011-07-05 16:44:18 +00004475 ExprResult NoexceptExpr;
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004476 ParsedAttributes FnAttrs(AttrFactory);
Richard Smith700537c2012-06-12 01:51:59 +00004477 TypeResult TrailingReturnType;
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004478
James Molloy6f8780b2012-02-29 10:24:19 +00004479 Actions.ActOnStartFunctionDeclarator();
4480
Douglas Gregor9e66af42011-07-05 16:44:18 +00004481 SourceLocation EndLoc;
Douglas Gregor9e66af42011-07-05 16:44:18 +00004482 if (isFunctionDeclaratorIdentifierList()) {
4483 if (RequiresArg)
4484 Diag(Tok, diag::err_argument_required_after_attribute);
4485
4486 ParseFunctionDeclaratorIdentifierList(D, ParamInfo);
4487
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004488 Tracker.consumeClose();
4489 EndLoc = Tracker.getCloseLocation();
Douglas Gregor9e66af42011-07-05 16:44:18 +00004490 } else {
Douglas Gregor9e66af42011-07-05 16:44:18 +00004491 if (Tok.isNot(tok::r_paren))
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004492 ParseParameterDeclarationClause(D, FirstArgAttrs, ParamInfo, EllipsisLoc);
Douglas Gregor9e66af42011-07-05 16:44:18 +00004493 else if (RequiresArg)
4494 Diag(Tok, diag::err_argument_required_after_attribute);
4495
David Blaikiebbafb8a2012-03-11 07:00:24 +00004496 HasProto = ParamInfo.size() || getLangOpts().CPlusPlus;
Douglas Gregor9e66af42011-07-05 16:44:18 +00004497
4498 // If we have the closing ')', eat it.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004499 Tracker.consumeClose();
4500 EndLoc = Tracker.getCloseLocation();
Douglas Gregor9e66af42011-07-05 16:44:18 +00004501
David Blaikiebbafb8a2012-03-11 07:00:24 +00004502 if (getLangOpts().CPlusPlus) {
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004503 // FIXME: Accept these components in any order, and produce fixits to
4504 // correct the order if the user gets it wrong. Ideally we should deal
4505 // with the virt-specifier-seq and pure-specifier in the same way.
Douglas Gregor9e66af42011-07-05 16:44:18 +00004506
4507 // Parse cv-qualifier-seq[opt].
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004508 ParseTypeQualifierListOpt(DS, false /*no attributes*/, false);
4509 if (!DS.getSourceRange().getEnd().isInvalid()) {
4510 EndLoc = DS.getSourceRange().getEnd();
4511 ConstQualifierLoc = DS.getConstSpecLoc();
4512 VolatileQualifierLoc = DS.getVolatileSpecLoc();
4513 }
Douglas Gregor9e66af42011-07-05 16:44:18 +00004514
4515 // Parse ref-qualifier[opt].
4516 if (Tok.is(tok::amp) || Tok.is(tok::ampamp)) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00004517 Diag(Tok, getLangOpts().CPlusPlus0x ?
Richard Smith5d164bc2011-10-15 05:09:34 +00004518 diag::warn_cxx98_compat_ref_qualifier :
4519 diag::ext_ref_qualifier);
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004520
Douglas Gregor9e66af42011-07-05 16:44:18 +00004521 RefQualifierIsLValueRef = Tok.is(tok::amp);
4522 RefQualifierLoc = ConsumeToken();
4523 EndLoc = RefQualifierLoc;
4524 }
4525
Douglas Gregor3024f072012-04-16 07:05:22 +00004526 // C++11 [expr.prim.general]p3:
Chad Rosierc1183952012-06-26 22:30:43 +00004527 // If a declaration declares a member function or member function
4528 // template of a class X, the expression this is a prvalue of type
Douglas Gregor3024f072012-04-16 07:05:22 +00004529 // "pointer to cv-qualifier-seq X" between the optional cv-qualifer-seq
Chad Rosierc1183952012-06-26 22:30:43 +00004530 // and the end of the function-definition, member-declarator, or
Douglas Gregor3024f072012-04-16 07:05:22 +00004531 // declarator.
Chad Rosierc1183952012-06-26 22:30:43 +00004532 bool IsCXX11MemberFunction =
Douglas Gregor3024f072012-04-16 07:05:22 +00004533 getLangOpts().CPlusPlus0x &&
4534 (D.getContext() == Declarator::MemberContext ||
4535 (D.getContext() == Declarator::FileContext &&
Chad Rosierc1183952012-06-26 22:30:43 +00004536 D.getCXXScopeSpec().isValid() &&
Douglas Gregor3024f072012-04-16 07:05:22 +00004537 Actions.CurContext->isRecord()));
4538 Sema::CXXThisScopeRAII ThisScope(Actions,
4539 dyn_cast<CXXRecordDecl>(Actions.CurContext),
4540 DS.getTypeQualifiers(),
4541 IsCXX11MemberFunction);
Richard Smith2331bbf2012-05-02 22:22:32 +00004542
Douglas Gregor9e66af42011-07-05 16:44:18 +00004543 // Parse exception-specification[opt].
Richard Smith2331bbf2012-05-02 22:22:32 +00004544 ESpecType = tryParseExceptionSpecification(ESpecRange,
Douglas Gregor433e0532012-04-16 18:27:27 +00004545 DynamicExceptions,
4546 DynamicExceptionRanges,
Richard Smith2331bbf2012-05-02 22:22:32 +00004547 NoexceptExpr);
Douglas Gregor9e66af42011-07-05 16:44:18 +00004548 if (ESpecType != EST_None)
4549 EndLoc = ESpecRange.getEnd();
4550
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004551 // Parse attribute-specifier-seq[opt]. Per DR 979 and DR 1297, this goes
4552 // after the exception-specification.
4553 MaybeParseCXX0XAttributes(FnAttrs);
4554
Douglas Gregor9e66af42011-07-05 16:44:18 +00004555 // Parse trailing-return-type[opt].
David Blaikiebbafb8a2012-03-11 07:00:24 +00004556 if (getLangOpts().CPlusPlus0x && Tok.is(tok::arrow)) {
Richard Smith5d164bc2011-10-15 05:09:34 +00004557 Diag(Tok, diag::warn_cxx98_compat_trailing_return_type);
Douglas Gregordb0b9f12011-08-04 15:30:47 +00004558 SourceRange Range;
Richard Smith700537c2012-06-12 01:51:59 +00004559 TrailingReturnType = ParseTrailingReturnType(Range);
Douglas Gregordb0b9f12011-08-04 15:30:47 +00004560 if (Range.getEnd().isValid())
4561 EndLoc = Range.getEnd();
Douglas Gregor9e66af42011-07-05 16:44:18 +00004562 }
4563 }
Douglas Gregor9e66af42011-07-05 16:44:18 +00004564 }
4565
4566 // Remember that we parsed a function type, and remember the attributes.
4567 D.AddTypeInfo(DeclaratorChunk::getFunction(HasProto,
4568 /*isVariadic=*/EllipsisLoc.isValid(),
4569 EllipsisLoc,
4570 ParamInfo.data(), ParamInfo.size(),
4571 DS.getTypeQualifiers(),
4572 RefQualifierIsLValueRef,
Douglas Gregore248eea2011-10-19 06:04:55 +00004573 RefQualifierLoc, ConstQualifierLoc,
4574 VolatileQualifierLoc,
Douglas Gregorad69e652011-07-13 21:47:47 +00004575 /*MutableLoc=*/SourceLocation(),
Douglas Gregor9e66af42011-07-05 16:44:18 +00004576 ESpecType, ESpecRange.getBegin(),
4577 DynamicExceptions.data(),
4578 DynamicExceptionRanges.data(),
4579 DynamicExceptions.size(),
4580 NoexceptExpr.isUsable() ?
4581 NoexceptExpr.get() : 0,
Chad Rosierc1183952012-06-26 22:30:43 +00004582 Tracker.getOpenLocation(),
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004583 EndLoc, D,
Douglas Gregor9e66af42011-07-05 16:44:18 +00004584 TrailingReturnType),
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004585 FnAttrs, EndLoc);
James Molloy6f8780b2012-02-29 10:24:19 +00004586
4587 Actions.ActOnEndFunctionDeclarator();
Douglas Gregor9e66af42011-07-05 16:44:18 +00004588}
4589
4590/// isFunctionDeclaratorIdentifierList - This parameter list may have an
4591/// identifier list form for a K&R-style function: void foo(a,b,c)
4592///
4593/// Note that identifier-lists are only allowed for normal declarators, not for
4594/// abstract-declarators.
4595bool Parser::isFunctionDeclaratorIdentifierList() {
David Blaikiebbafb8a2012-03-11 07:00:24 +00004596 return !getLangOpts().CPlusPlus
Douglas Gregor9e66af42011-07-05 16:44:18 +00004597 && Tok.is(tok::identifier)
4598 && !TryAltiVecVectorToken()
4599 // K&R identifier lists can't have typedefs as identifiers, per C99
4600 // 6.7.5.3p11.
4601 && (TryAnnotateTypeOrScopeToken() || !Tok.is(tok::annot_typename))
4602 // Identifier lists follow a really simple grammar: the identifiers can
4603 // be followed *only* by a ", identifier" or ")". However, K&R
4604 // identifier lists are really rare in the brave new modern world, and
4605 // it is very common for someone to typo a type in a non-K&R style
4606 // list. If we are presented with something like: "void foo(intptr x,
4607 // float y)", we don't want to start parsing the function declarator as
4608 // though it is a K&R style declarator just because intptr is an
4609 // invalid type.
4610 //
4611 // To handle this, we check to see if the token after the first
4612 // identifier is a "," or ")". Only then do we parse it as an
4613 // identifier list.
4614 && (NextToken().is(tok::comma) || NextToken().is(tok::r_paren));
4615}
4616
4617/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
4618/// we found a K&R-style identifier list instead of a typed parameter list.
4619///
4620/// After returning, ParamInfo will hold the parsed parameters.
4621///
4622/// identifier-list: [C99 6.7.5]
4623/// identifier
4624/// identifier-list ',' identifier
4625///
4626void Parser::ParseFunctionDeclaratorIdentifierList(
4627 Declarator &D,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004628 SmallVector<DeclaratorChunk::ParamInfo, 16> &ParamInfo) {
Douglas Gregor9e66af42011-07-05 16:44:18 +00004629 // If there was no identifier specified for the declarator, either we are in
4630 // an abstract-declarator, or we are in a parameter declarator which was found
4631 // to be abstract. In abstract-declarators, identifier lists are not valid:
4632 // diagnose this.
4633 if (!D.getIdentifier())
4634 Diag(Tok, diag::ext_ident_list_in_param);
4635
4636 // Maintain an efficient lookup of params we have seen so far.
4637 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
4638
4639 while (1) {
4640 // If this isn't an identifier, report the error and skip until ')'.
4641 if (Tok.isNot(tok::identifier)) {
4642 Diag(Tok, diag::err_expected_ident);
4643 SkipUntil(tok::r_paren, /*StopAtSemi=*/true, /*DontConsume=*/true);
4644 // Forget we parsed anything.
4645 ParamInfo.clear();
4646 return;
4647 }
4648
4649 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
4650
4651 // Reject 'typedef int y; int test(x, y)', but continue parsing.
4652 if (Actions.getTypeName(*ParmII, Tok.getLocation(), getCurScope()))
4653 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
4654
4655 // Verify that the argument identifier has not already been mentioned.
4656 if (!ParamsSoFar.insert(ParmII)) {
4657 Diag(Tok, diag::err_param_redefinition) << ParmII;
4658 } else {
4659 // Remember this identifier in ParamInfo.
4660 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
4661 Tok.getLocation(),
4662 0));
4663 }
4664
4665 // Eat the identifier.
4666 ConsumeToken();
4667
4668 // The list continues if we see a comma.
4669 if (Tok.isNot(tok::comma))
4670 break;
4671 ConsumeToken();
4672 }
4673}
4674
4675/// ParseParameterDeclarationClause - Parse a (possibly empty) parameter-list
4676/// after the opening parenthesis. This function will not parse a K&R-style
4677/// identifier list.
4678///
Richard Smith2620cd92012-04-11 04:01:28 +00004679/// D is the declarator being parsed. If FirstArgAttrs is non-null, then the
4680/// caller parsed those arguments immediately after the open paren - they should
4681/// be considered to be part of the first parameter.
Douglas Gregor9e66af42011-07-05 16:44:18 +00004682///
4683/// After returning, ParamInfo will hold the parsed parameters. EllipsisLoc will
4684/// be the location of the ellipsis, if any was parsed.
4685///
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004686/// parameter-type-list: [C99 6.7.5]
4687/// parameter-list
4688/// parameter-list ',' '...'
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00004689/// [C++] parameter-list '...'
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004690///
4691/// parameter-list: [C99 6.7.5]
4692/// parameter-declaration
4693/// parameter-list ',' parameter-declaration
4694///
4695/// parameter-declaration: [C99 6.7.5]
4696/// declaration-specifiers declarator
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00004697/// [C++] declaration-specifiers declarator '=' assignment-expression
Sebastian Redldb63af22012-03-14 15:54:00 +00004698/// [C++11] initializer-clause
Chris Lattnere37e2332006-08-15 04:50:22 +00004699/// [GNU] declaration-specifiers declarator attributes
Sebastian Redlf769df52009-03-24 22:27:57 +00004700/// declaration-specifiers abstract-declarator[opt]
4701/// [C++] declaration-specifiers abstract-declarator[opt]
Chris Lattner58258242008-04-10 02:22:51 +00004702/// '=' assignment-expression
Chris Lattnere37e2332006-08-15 04:50:22 +00004703/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
Richard Smith2620cd92012-04-11 04:01:28 +00004704/// [C++11] attribute-specifier-seq parameter-declaration
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004705///
Douglas Gregor9e66af42011-07-05 16:44:18 +00004706void Parser::ParseParameterDeclarationClause(
4707 Declarator &D,
Richard Smith2620cd92012-04-11 04:01:28 +00004708 ParsedAttributes &FirstArgAttrs,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004709 SmallVector<DeclaratorChunk::ParamInfo, 16> &ParamInfo,
Douglas Gregor9e66af42011-07-05 16:44:18 +00004710 SourceLocation &EllipsisLoc) {
Mike Stump11289f42009-09-09 15:08:12 +00004711
Chris Lattner371ed4e2008-04-06 06:57:35 +00004712 while (1) {
4713 if (Tok.is(tok::ellipsis)) {
Richard Smith2620cd92012-04-11 04:01:28 +00004714 // FIXME: Issue a diagnostic if we parsed an attribute-specifier-seq
4715 // before deciding this was a parameter-declaration-clause.
Douglas Gregor94349fd2009-02-18 07:07:28 +00004716 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chris Lattner371ed4e2008-04-06 06:57:35 +00004717 break;
Chris Lattneracd58a32006-08-06 17:24:14 +00004718 }
Mike Stump11289f42009-09-09 15:08:12 +00004719
Chris Lattner371ed4e2008-04-06 06:57:35 +00004720 // Parse the declaration-specifiers.
John McCall28a6aea2009-11-04 02:18:39 +00004721 // Just use the ParsingDeclaration "scope" of the declarator.
John McCall084e83d2011-03-24 11:26:52 +00004722 DeclSpec DS(AttrFactory);
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004723
Richard Smith2620cd92012-04-11 04:01:28 +00004724 // Parse any C++11 attributes.
4725 MaybeParseCXX0XAttributes(DS.getAttributes());
4726
John McCall53fa7142010-12-24 02:08:15 +00004727 // Skip any Microsoft attributes before a param.
David Blaikiebbafb8a2012-03-11 07:00:24 +00004728 if (getLangOpts().MicrosoftExt && Tok.is(tok::l_square))
John McCall53fa7142010-12-24 02:08:15 +00004729 ParseMicrosoftAttributes(DS.getAttributes());
4730
4731 SourceLocation DSStart = Tok.getLocation();
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00004732
4733 // If the caller parsed attributes for the first argument, add them now.
John McCall53fa7142010-12-24 02:08:15 +00004734 // Take them so that we only apply the attributes to the first parameter.
Douglas Gregor9e66af42011-07-05 16:44:18 +00004735 // FIXME: If we can leave the attributes in the token stream somehow, we can
Richard Smith2620cd92012-04-11 04:01:28 +00004736 // get rid of a parameter (FirstArgAttrs) and this statement. It might be
4737 // too much hassle.
4738 DS.takeAttributesFrom(FirstArgAttrs);
John McCall53fa7142010-12-24 02:08:15 +00004739
Chris Lattnerde39c3e2009-02-27 18:38:20 +00004740 ParseDeclarationSpecifiers(DS);
Mike Stump11289f42009-09-09 15:08:12 +00004741
Chris Lattner371ed4e2008-04-06 06:57:35 +00004742 // Parse the declarator. This is "PrototypeContext", because we must
4743 // accept either 'declarator' or 'abstract-declarator' here.
4744 Declarator ParmDecl(DS, Declarator::PrototypeContext);
4745 ParseDeclarator(ParmDecl);
4746
4747 // Parse GNU attributes, if present.
John McCall53fa7142010-12-24 02:08:15 +00004748 MaybeParseGNUAttributes(ParmDecl);
Mike Stump11289f42009-09-09 15:08:12 +00004749
Chris Lattner371ed4e2008-04-06 06:57:35 +00004750 // Remember this parsed parameter in ParamInfo.
4751 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
Mike Stump11289f42009-09-09 15:08:12 +00004752
Douglas Gregor4d87df52008-12-16 21:30:33 +00004753 // DefArgToks is used when the parsing of default arguments needs
4754 // to be delayed.
4755 CachedTokens *DefArgToks = 0;
4756
Chris Lattner371ed4e2008-04-06 06:57:35 +00004757 // If no parameter was specified, verify that *something* was specified,
4758 // otherwise we have a missing type and identifier.
Chris Lattnerde39c3e2009-02-27 18:38:20 +00004759 if (DS.isEmpty() && ParmDecl.getIdentifier() == 0 &&
4760 ParmDecl.getNumTypeObjects() == 0) {
Chris Lattner371ed4e2008-04-06 06:57:35 +00004761 // Completely missing, emit error.
4762 Diag(DSStart, diag::err_missing_param);
4763 } else {
4764 // Otherwise, we have something. Add it and let semantic analysis try
4765 // to grok it and add the result to the ParamInfo we are building.
Mike Stump11289f42009-09-09 15:08:12 +00004766
Chris Lattner371ed4e2008-04-06 06:57:35 +00004767 // Inform the actions module about the parameter declarator, so it gets
4768 // added to the current scope.
John McCall48871652010-08-21 09:40:31 +00004769 Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl);
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00004770
4771 // Parse the default argument, if any. We parse the default
4772 // arguments in all dialects; the semantic analysis in
4773 // ActOnParamDefaultArgument will reject the default argument in
4774 // C.
4775 if (Tok.is(tok::equal)) {
Douglas Gregor58354032008-12-24 00:01:03 +00004776 SourceLocation EqualLoc = Tok.getLocation();
4777
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00004778 // Parse the default argument
Douglas Gregor4d87df52008-12-16 21:30:33 +00004779 if (D.getContext() == Declarator::MemberContext) {
4780 // If we're inside a class definition, cache the tokens
4781 // corresponding to the default argument. We'll actually parse
4782 // them when we see the end of the class definition.
Douglas Gregor4d87df52008-12-16 21:30:33 +00004783 // FIXME: Can we use a smart pointer for Toks?
4784 DefArgToks = new CachedTokens;
4785
Mike Stump11289f42009-09-09 15:08:12 +00004786 if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks,
Argyrios Kyrtzidis8d7bdba2010-04-23 21:20:12 +00004787 /*StopAtSemi=*/true,
4788 /*ConsumeFinalToken=*/false)) {
Douglas Gregor4d87df52008-12-16 21:30:33 +00004789 delete DefArgToks;
4790 DefArgToks = 0;
Douglas Gregor58354032008-12-24 00:01:03 +00004791 Actions.ActOnParamDefaultArgumentError(Param);
Argyrios Kyrtzidis249179c2010-08-06 09:47:24 +00004792 } else {
4793 // Mark the end of the default argument so that we know when to
4794 // stop when we parse it later on.
4795 Token DefArgEnd;
4796 DefArgEnd.startToken();
4797 DefArgEnd.setKind(tok::cxx_defaultarg_end);
4798 DefArgEnd.setLocation(Tok.getLocation());
4799 DefArgToks->push_back(DefArgEnd);
Mike Stump11289f42009-09-09 15:08:12 +00004800 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc,
Anders Carlsson84613c42009-06-12 16:51:40 +00004801 (*DefArgToks)[1].getLocation());
Argyrios Kyrtzidis249179c2010-08-06 09:47:24 +00004802 }
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00004803 } else {
Douglas Gregor4d87df52008-12-16 21:30:33 +00004804 // Consume the '='.
Douglas Gregor58354032008-12-24 00:01:03 +00004805 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00004806
Chad Rosierc1183952012-06-26 22:30:43 +00004807 // The argument isn't actually potentially evaluated unless it is
Douglas Gregor8a01b2a2010-09-11 20:24:53 +00004808 // used.
4809 EnterExpressionEvaluationContext Eval(Actions,
Douglas Gregor7fcbd902012-02-21 00:37:24 +00004810 Sema::PotentiallyEvaluatedIfUsed,
4811 Param);
Douglas Gregor8a01b2a2010-09-11 20:24:53 +00004812
Sebastian Redldb63af22012-03-14 15:54:00 +00004813 ExprResult DefArgResult;
Sebastian Redl1678d5f2012-03-18 22:25:45 +00004814 if (getLangOpts().CPlusPlus0x && Tok.is(tok::l_brace)) {
4815 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
Sebastian Redldb63af22012-03-14 15:54:00 +00004816 DefArgResult = ParseBraceInitializer();
Sebastian Redl1678d5f2012-03-18 22:25:45 +00004817 } else
Sebastian Redldb63af22012-03-14 15:54:00 +00004818 DefArgResult = ParseAssignmentExpression();
Douglas Gregor4d87df52008-12-16 21:30:33 +00004819 if (DefArgResult.isInvalid()) {
4820 Actions.ActOnParamDefaultArgumentError(Param);
4821 SkipUntil(tok::comma, tok::r_paren, true, true);
4822 } else {
4823 // Inform the actions module about the default argument
4824 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
John McCallb268a282010-08-23 23:25:46 +00004825 DefArgResult.take());
Douglas Gregor4d87df52008-12-16 21:30:33 +00004826 }
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00004827 }
4828 }
Mike Stump11289f42009-09-09 15:08:12 +00004829
4830 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
4831 ParmDecl.getIdentifierLoc(), Param,
Douglas Gregor4d87df52008-12-16 21:30:33 +00004832 DefArgToks));
Chris Lattner371ed4e2008-04-06 06:57:35 +00004833 }
4834
4835 // If the next token is a comma, consume it and keep reading arguments.
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00004836 if (Tok.isNot(tok::comma)) {
4837 if (Tok.is(tok::ellipsis)) {
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00004838 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chad Rosierc1183952012-06-26 22:30:43 +00004839
David Blaikiebbafb8a2012-03-11 07:00:24 +00004840 if (!getLangOpts().CPlusPlus) {
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00004841 // We have ellipsis without a preceding ',', which is ill-formed
4842 // in C. Complain and provide the fix.
4843 Diag(EllipsisLoc, diag::err_missing_comma_before_ellipsis)
Douglas Gregora771f462010-03-31 17:46:05 +00004844 << FixItHint::CreateInsertion(EllipsisLoc, ", ");
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00004845 }
4846 }
Chad Rosierc1183952012-06-26 22:30:43 +00004847
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00004848 break;
4849 }
Mike Stump11289f42009-09-09 15:08:12 +00004850
Chris Lattner371ed4e2008-04-06 06:57:35 +00004851 // Consume the comma.
4852 ConsumeToken();
Chris Lattneracd58a32006-08-06 17:24:14 +00004853 }
Mike Stump11289f42009-09-09 15:08:12 +00004854
Chris Lattner6c940e62008-04-06 06:34:08 +00004855}
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004856
Chris Lattnere8074e62006-08-06 18:30:15 +00004857/// [C90] direct-declarator '[' constant-expression[opt] ']'
4858/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
4859/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
4860/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
4861/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004862/// [C++11] direct-declarator '[' constant-expression[opt] ']'
4863/// attribute-specifier-seq[opt]
Chris Lattnere8074e62006-08-06 18:30:15 +00004864void Parser::ParseBracketDeclarator(Declarator &D) {
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004865 if (CheckProhibitedCXX11Attribute())
4866 return;
4867
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004868 BalancedDelimiterTracker T(*this, tok::l_square);
4869 T.consumeOpen();
Mike Stump11289f42009-09-09 15:08:12 +00004870
Chris Lattner84a11622008-12-18 07:27:21 +00004871 // C array syntax has many features, but by-far the most common is [] and [4].
4872 // This code does a fast path to handle some of the most obvious cases.
4873 if (Tok.getKind() == tok::r_square) {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004874 T.consumeClose();
John McCall084e83d2011-03-24 11:26:52 +00004875 ParsedAttributes attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00004876 MaybeParseCXX0XAttributes(attrs);
Chad Rosierc1183952012-06-26 22:30:43 +00004877
Chris Lattner84a11622008-12-18 07:27:21 +00004878 // Remember that we parsed the empty array type.
John McCalldadc5752010-08-24 06:29:42 +00004879 ExprResult NumElements;
John McCall084e83d2011-03-24 11:26:52 +00004880 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004881 T.getOpenLocation(),
4882 T.getCloseLocation()),
4883 attrs, T.getCloseLocation());
Chris Lattner84a11622008-12-18 07:27:21 +00004884 return;
4885 } else if (Tok.getKind() == tok::numeric_constant &&
4886 GetLookAheadToken(1).is(tok::r_square)) {
4887 // [4] is very common. Parse the numeric constant expression.
Richard Smithbcc22fc2012-03-09 08:00:36 +00004888 ExprResult ExprRes(Actions.ActOnNumericConstant(Tok, getCurScope()));
Chris Lattner84a11622008-12-18 07:27:21 +00004889 ConsumeToken();
4890
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004891 T.consumeClose();
John McCall084e83d2011-03-24 11:26:52 +00004892 ParsedAttributes attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00004893 MaybeParseCXX0XAttributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +00004894
Chris Lattner84a11622008-12-18 07:27:21 +00004895 // Remember that we parsed a array type, and remember its features.
John McCall084e83d2011-03-24 11:26:52 +00004896 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, 0,
John McCall53fa7142010-12-24 02:08:15 +00004897 ExprRes.release(),
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004898 T.getOpenLocation(),
4899 T.getCloseLocation()),
4900 attrs, T.getCloseLocation());
Chris Lattner84a11622008-12-18 07:27:21 +00004901 return;
4902 }
Mike Stump11289f42009-09-09 15:08:12 +00004903
Chris Lattnere8074e62006-08-06 18:30:15 +00004904 // If valid, this location is the position where we read the 'static' keyword.
4905 SourceLocation StaticLoc;
Chris Lattner76c72282007-10-09 17:33:22 +00004906 if (Tok.is(tok::kw_static))
Chris Lattneraf635312006-10-16 06:06:51 +00004907 StaticLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00004908
Chris Lattnere8074e62006-08-06 18:30:15 +00004909 // If there is a type-qualifier-list, read it now.
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00004910 // Type qualifiers in an array subscript are a C99 feature.
John McCall084e83d2011-03-24 11:26:52 +00004911 DeclSpec DS(AttrFactory);
Chris Lattnercf0bab22008-12-18 07:02:59 +00004912 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Mike Stump11289f42009-09-09 15:08:12 +00004913
Chris Lattnere8074e62006-08-06 18:30:15 +00004914 // If we haven't already read 'static', check to see if there is one after the
4915 // type-qualifier-list.
Chris Lattner76c72282007-10-09 17:33:22 +00004916 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Chris Lattneraf635312006-10-16 06:06:51 +00004917 StaticLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00004918
Chris Lattnere8074e62006-08-06 18:30:15 +00004919 // Handle "direct-declarator [ type-qual-list[opt] * ]".
Chris Lattnere8074e62006-08-06 18:30:15 +00004920 bool isStar = false;
John McCalldadc5752010-08-24 06:29:42 +00004921 ExprResult NumElements;
Mike Stump11289f42009-09-09 15:08:12 +00004922
Chris Lattner521ff2b2008-04-06 05:26:30 +00004923 // Handle the case where we have '[*]' as the array size. However, a leading
4924 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
4925 // the the token after the star is a ']'. Since stars in arrays are
4926 // infrequent, use of lookahead is not costly here.
4927 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattnerc439f0d2008-04-06 05:27:21 +00004928 ConsumeToken(); // Eat the '*'.
Chris Lattner1906f802006-08-06 19:14:46 +00004929
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00004930 if (StaticLoc.isValid()) {
Chris Lattner521ff2b2008-04-06 05:26:30 +00004931 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00004932 StaticLoc = SourceLocation(); // Drop the static.
4933 }
Chris Lattner521ff2b2008-04-06 05:26:30 +00004934 isStar = true;
Chris Lattner76c72282007-10-09 17:33:22 +00004935 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner84a11622008-12-18 07:27:21 +00004936 // Note, in C89, this production uses the constant-expr production instead
4937 // of assignment-expr. The only difference is that assignment-expr allows
4938 // things like '=' and '*='. Sema rejects these in C89 mode because they
4939 // are not i-c-e's, so we don't need to distinguish between the two here.
Mike Stump11289f42009-09-09 15:08:12 +00004940
Douglas Gregorc9c02ed2009-06-19 23:52:42 +00004941 // Parse the constant-expression or assignment-expression now (depending
4942 // on dialect).
David Blaikiebbafb8a2012-03-11 07:00:24 +00004943 if (getLangOpts().CPlusPlus) {
Douglas Gregorc9c02ed2009-06-19 23:52:42 +00004944 NumElements = ParseConstantExpression();
Eli Friedmane0afc982012-01-21 01:01:51 +00004945 } else {
4946 EnterExpressionEvaluationContext Unevaluated(Actions,
4947 Sema::ConstantEvaluated);
Douglas Gregorc9c02ed2009-06-19 23:52:42 +00004948 NumElements = ParseAssignmentExpression();
Eli Friedmane0afc982012-01-21 01:01:51 +00004949 }
Chris Lattner62591722006-08-12 18:40:58 +00004950 }
Mike Stump11289f42009-09-09 15:08:12 +00004951
Chris Lattner62591722006-08-12 18:40:58 +00004952 // If there was an error parsing the assignment-expression, recover.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00004953 if (NumElements.isInvalid()) {
Chris Lattnercd2a8c52009-04-24 22:30:50 +00004954 D.setInvalidType(true);
Chris Lattner62591722006-08-12 18:40:58 +00004955 // If the expression was invalid, skip it.
4956 SkipUntil(tok::r_square);
4957 return;
Chris Lattnere8074e62006-08-06 18:30:15 +00004958 }
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004959
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004960 T.consumeClose();
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004961
John McCall084e83d2011-03-24 11:26:52 +00004962 ParsedAttributes attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00004963 MaybeParseCXX0XAttributes(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +00004964
Chris Lattner84a11622008-12-18 07:27:21 +00004965 // Remember that we parsed a array type, and remember its features.
John McCall084e83d2011-03-24 11:26:52 +00004966 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
Chris Lattnercbc426d2006-12-02 06:43:02 +00004967 StaticLoc.isValid(), isStar,
Douglas Gregor04318252009-07-06 15:59:29 +00004968 NumElements.release(),
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004969 T.getOpenLocation(),
4970 T.getCloseLocation()),
4971 attrs, T.getCloseLocation());
Chris Lattnere8074e62006-08-06 18:30:15 +00004972}
4973
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00004974/// [GNU] typeof-specifier:
4975/// typeof ( expressions )
4976/// typeof ( type-name )
4977/// [GNU/C++] typeof unary-expression
Steve Naroffad373bd2007-07-31 12:34:36 +00004978///
4979void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner76c72282007-10-09 17:33:22 +00004980 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00004981 Token OpTok = Tok;
Steve Naroffad373bd2007-07-31 12:34:36 +00004982 SourceLocation StartLoc = ConsumeToken();
4983
John McCalle8595032010-01-13 20:03:27 +00004984 const bool hasParens = Tok.is(tok::l_paren);
4985
Eli Friedmane0afc982012-01-21 01:01:51 +00004986 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated);
4987
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00004988 bool isCastExpr;
John McCallba7bf592010-08-24 05:47:05 +00004989 ParsedType CastTy;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00004990 SourceRange CastRange;
Peter Collingbournee190dee2011-03-11 19:24:49 +00004991 ExprResult Operand = ParseExprAfterUnaryExprOrTypeTrait(OpTok, isCastExpr,
4992 CastTy, CastRange);
John McCalle8595032010-01-13 20:03:27 +00004993 if (hasParens)
4994 DS.setTypeofParensRange(CastRange);
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00004995
4996 if (CastRange.getEnd().isInvalid())
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00004997 // FIXME: Not accurate, the range gets one token more than it should.
4998 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00004999 else
5000 DS.SetRangeEnd(CastRange.getEnd());
Mike Stump11289f42009-09-09 15:08:12 +00005001
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005002 if (isCastExpr) {
5003 if (!CastTy) {
5004 DS.SetTypeSpecError();
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00005005 return;
Douglas Gregor220cac52009-02-18 17:45:20 +00005006 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00005007
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005008 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00005009 unsigned DiagID;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005010 // Check for duplicate type specifiers (e.g. "int typeof(int)").
5011 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00005012 DiagID, CastTy))
5013 Diag(StartLoc, DiagID) << PrevSpec;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005014 return;
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00005015 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00005016
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00005017 // If we get here, the operand to the typeof was an expresion.
5018 if (Operand.isInvalid()) {
5019 DS.SetTypeSpecError();
Steve Naroff4bd2f712007-08-02 02:53:48 +00005020 return;
Steve Naroffad373bd2007-07-31 12:34:36 +00005021 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00005022
Eli Friedmane0afc982012-01-21 01:01:51 +00005023 // We might need to transform the operand if it is potentially evaluated.
5024 Operand = Actions.HandleExprEvaluationContextForTypeof(Operand.get());
5025 if (Operand.isInvalid()) {
5026 DS.SetTypeSpecError();
5027 return;
5028 }
5029
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00005030 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00005031 unsigned DiagID;
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00005032 // Check for duplicate type specifiers (e.g. "int typeof(int)").
5033 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
John McCallba7bf592010-08-24 05:47:05 +00005034 DiagID, Operand.get()))
John McCall49bfce42009-08-03 20:12:06 +00005035 Diag(StartLoc, DiagID) << PrevSpec;
Steve Naroffad373bd2007-07-31 12:34:36 +00005036}
Chris Lattner73a9c7d2010-02-28 18:33:55 +00005037
Benjamin Kramere56f3932011-12-23 17:00:35 +00005038/// [C11] atomic-specifier:
Eli Friedman0dfb8892011-10-06 23:00:33 +00005039/// _Atomic ( type-name )
5040///
5041void Parser::ParseAtomicSpecifier(DeclSpec &DS) {
5042 assert(Tok.is(tok::kw__Atomic) && "Not an atomic specifier");
5043
5044 SourceLocation StartLoc = ConsumeToken();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005045 BalancedDelimiterTracker T(*this, tok::l_paren);
5046 if (T.expectAndConsume(diag::err_expected_lparen_after, "_Atomic")) {
Eli Friedman0dfb8892011-10-06 23:00:33 +00005047 SkipUntil(tok::r_paren);
5048 return;
5049 }
5050
5051 TypeResult Result = ParseTypeName();
5052 if (Result.isInvalid()) {
5053 SkipUntil(tok::r_paren);
5054 return;
5055 }
5056
5057 // Match the ')'
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005058 T.consumeClose();
Eli Friedman0dfb8892011-10-06 23:00:33 +00005059
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005060 if (T.getCloseLocation().isInvalid())
Eli Friedman0dfb8892011-10-06 23:00:33 +00005061 return;
5062
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005063 DS.setTypeofParensRange(T.getRange());
5064 DS.SetRangeEnd(T.getCloseLocation());
Eli Friedman0dfb8892011-10-06 23:00:33 +00005065
5066 const char *PrevSpec = 0;
5067 unsigned DiagID;
5068 if (DS.SetTypeSpecType(DeclSpec::TST_atomic, StartLoc, PrevSpec,
5069 DiagID, Result.release()))
5070 Diag(StartLoc, DiagID) << PrevSpec;
5071}
5072
Chris Lattner73a9c7d2010-02-28 18:33:55 +00005073
5074/// TryAltiVecVectorTokenOutOfLine - Out of line body that should only be called
5075/// from TryAltiVecVectorToken.
5076bool Parser::TryAltiVecVectorTokenOutOfLine() {
5077 Token Next = NextToken();
5078 switch (Next.getKind()) {
5079 default: return false;
5080 case tok::kw_short:
5081 case tok::kw_long:
5082 case tok::kw_signed:
5083 case tok::kw_unsigned:
5084 case tok::kw_void:
5085 case tok::kw_char:
5086 case tok::kw_int:
5087 case tok::kw_float:
5088 case tok::kw_double:
5089 case tok::kw_bool:
5090 case tok::kw___pixel:
5091 Tok.setKind(tok::kw___vector);
5092 return true;
5093 case tok::identifier:
5094 if (Next.getIdentifierInfo() == Ident_pixel) {
5095 Tok.setKind(tok::kw___vector);
5096 return true;
5097 }
5098 return false;
5099 }
5100}
5101
5102bool Parser::TryAltiVecTokenOutOfLine(DeclSpec &DS, SourceLocation Loc,
5103 const char *&PrevSpec, unsigned &DiagID,
5104 bool &isInvalid) {
5105 if (Tok.getIdentifierInfo() == Ident_vector) {
5106 Token Next = NextToken();
5107 switch (Next.getKind()) {
5108 case tok::kw_short:
5109 case tok::kw_long:
5110 case tok::kw_signed:
5111 case tok::kw_unsigned:
5112 case tok::kw_void:
5113 case tok::kw_char:
5114 case tok::kw_int:
5115 case tok::kw_float:
5116 case tok::kw_double:
5117 case tok::kw_bool:
5118 case tok::kw___pixel:
5119 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
5120 return true;
5121 case tok::identifier:
5122 if (Next.getIdentifierInfo() == Ident_pixel) {
5123 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
5124 return true;
5125 }
5126 break;
5127 default:
5128 break;
5129 }
Douglas Gregor9938e3b2010-06-16 15:28:57 +00005130 } else if ((Tok.getIdentifierInfo() == Ident_pixel) &&
Chris Lattner73a9c7d2010-02-28 18:33:55 +00005131 DS.isTypeAltiVecVector()) {
5132 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
5133 return true;
5134 }
5135 return false;
5136}