blob: cb865cc9c2ba1f76a83afead63d5dcf0bffdf484 [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
Alexis Hunt96d5c762009-11-21 08:43:09 +000071/// ParseGNUAttributes - Parse a non-empty attributes list.
Chris Lattnerb8cd5c22006-08-15 04:10:46 +000072///
73/// [GNU] attributes:
74/// attribute
75/// attributes attribute
76///
77/// [GNU] attribute:
78/// '__attribute__' '(' '(' attribute-list ')' ')'
79///
80/// [GNU] attribute-list:
81/// attrib
82/// attribute_list ',' attrib
83///
84/// [GNU] attrib:
85/// empty
Steve Naroff0f2fe172007-06-01 17:11:19 +000086/// attrib-name
87/// attrib-name '(' identifier ')'
88/// attrib-name '(' identifier ',' nonempty-expr-list ')'
89/// attrib-name '(' argument-expression-list [C99 6.5.2] ')'
Chris Lattnerb8cd5c22006-08-15 04:10:46 +000090///
Steve Naroff0f2fe172007-06-01 17:11:19 +000091/// [GNU] attrib-name:
92/// identifier
93/// typespec
94/// typequal
95/// storageclass
Mike Stump11289f42009-09-09 15:08:12 +000096///
Steve Naroff0f2fe172007-06-01 17:11:19 +000097/// FIXME: The GCC grammar/code for this construct implies we need two
Mike Stump11289f42009-09-09 15:08:12 +000098/// token lookahead. Comment from gcc: "If they start with an identifier
99/// which is followed by a comma or close parenthesis, then the arguments
Steve Naroff0f2fe172007-06-01 17:11:19 +0000100/// start with that identifier; otherwise they are an expression list."
101///
Richard Smithb12bf692011-10-17 21:20:17 +0000102/// GCC does not require the ',' between attribs in an attribute-list.
103///
Steve Naroff0f2fe172007-06-01 17:11:19 +0000104/// At the moment, I am not doing 2 token lookahead. I am also unaware of
105/// any attributes that don't work (based on my limited testing). Most
106/// attributes are very simple in practice. Until we find a bug, I don't see
107/// a pressing need to implement the 2 token lookahead.
Chris Lattnerb8cd5c22006-08-15 04:10:46 +0000108
John McCall53fa7142010-12-24 02:08:15 +0000109void Parser::ParseGNUAttributes(ParsedAttributes &attrs,
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000110 SourceLocation *endLoc,
111 LateParsedAttrList *LateAttrs) {
Alexis Hunt96d5c762009-11-21 08:43:09 +0000112 assert(Tok.is(tok::kw___attribute) && "Not a GNU attribute list!");
Mike Stump11289f42009-09-09 15:08:12 +0000113
Chris Lattner76c72282007-10-09 17:33:22 +0000114 while (Tok.is(tok::kw___attribute)) {
Steve Naroff0f2fe172007-06-01 17:11:19 +0000115 ConsumeToken();
116 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
117 "attribute")) {
118 SkipUntil(tok::r_paren, true); // skip until ) or ;
John McCall53fa7142010-12-24 02:08:15 +0000119 return;
Steve Naroff0f2fe172007-06-01 17:11:19 +0000120 }
121 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, "(")) {
122 SkipUntil(tok::r_paren, true); // skip until ) or ;
John McCall53fa7142010-12-24 02:08:15 +0000123 return;
Steve Naroff0f2fe172007-06-01 17:11:19 +0000124 }
125 // Parse the attribute-list. e.g. __attribute__(( weak, alias("__f") ))
Chris Lattner76c72282007-10-09 17:33:22 +0000126 while (Tok.is(tok::identifier) || isDeclarationSpecifier() ||
127 Tok.is(tok::comma)) {
Mike Stump11289f42009-09-09 15:08:12 +0000128 if (Tok.is(tok::comma)) {
Steve Naroff0f2fe172007-06-01 17:11:19 +0000129 // allows for empty/non-empty attributes. ((__vector_size__(16),,,,))
130 ConsumeToken();
131 continue;
132 }
133 // we have an identifier or declaration specifier (const, int, etc.)
134 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
135 SourceLocation AttrNameLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000136
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000137 if (Tok.is(tok::l_paren)) {
138 // handle "parameterized" attributes
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +0000139 if (LateAttrs && isAttributeLateParsed(*AttrName)) {
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000140 LateParsedAttribute *LA =
141 new LateParsedAttribute(this, *AttrName, AttrNameLoc);
142 LateAttrs->push_back(LA);
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +0000143
144 // Attributes in a class are parsed at the end of the class, along
145 // with other late-parsed declarations.
146 if (!ClassStack.empty())
147 getCurrentClass().LateParsedDeclarations.push_back(LA);
Mike Stump11289f42009-09-09 15:08:12 +0000148
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000149 // consume everything up to and including the matching right parens
150 ConsumeAndStoreUntil(tok::r_paren, LA->Toks, true, false);
Mike Stump11289f42009-09-09 15:08:12 +0000151
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000152 Token Eof;
153 Eof.startToken();
154 Eof.setLocation(Tok.getLocation());
155 LA->Toks.push_back(Eof);
156 } else {
157 ParseGNUAttributeArgs(AttrName, AttrNameLoc, attrs, endLoc);
Steve Naroff0f2fe172007-06-01 17:11:19 +0000158 }
159 } else {
John McCall084e83d2011-03-24 11:26:52 +0000160 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc,
Alexis Hunta0e54d42012-06-18 16:13:52 +0000161 0, SourceLocation(), 0, 0, AttributeList::AS_GNU);
Steve Naroff0f2fe172007-06-01 17:11:19 +0000162 }
163 }
Steve Naroff98d153c2007-06-06 23:19:11 +0000164 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
Steve Naroff98d153c2007-06-06 23:19:11 +0000165 SkipUntil(tok::r_paren, false);
Alexis Hunt96d5c762009-11-21 08:43:09 +0000166 SourceLocation Loc = Tok.getLocation();
Sebastian Redlf6591ca2009-02-09 18:23:29 +0000167 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) {
168 SkipUntil(tok::r_paren, false);
169 }
John McCall53fa7142010-12-24 02:08:15 +0000170 if (endLoc)
171 *endLoc = Loc;
Steve Naroff0f2fe172007-06-01 17:11:19 +0000172 }
Steve Naroff0f2fe172007-06-01 17:11:19 +0000173}
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000174
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000175
176/// Parse the arguments to a parameterized GNU attribute
177void Parser::ParseGNUAttributeArgs(IdentifierInfo *AttrName,
178 SourceLocation AttrNameLoc,
179 ParsedAttributes &Attrs,
180 SourceLocation *EndLoc) {
181
182 assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('");
183
184 // Availability attributes have their own grammar.
185 if (AttrName->isStr("availability")) {
186 ParseAvailabilityAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc);
187 return;
188 }
189 // Thread safety attributes fit into the FIXME case above, so we
190 // just parse the arguments as a list of expressions
191 if (IsThreadSafetyAttribute(AttrName->getName())) {
192 ParseThreadSafetyAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc);
193 return;
194 }
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000195 // Type safety attributes have their own grammar.
196 if (AttrName->isStr("type_tag_for_datatype")) {
197 ParseTypeTagForDatatypeAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc);
198 return;
199 }
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000200
201 ConsumeParen(); // ignore the left paren loc for now
202
Richard Smithb12bf692011-10-17 21:20:17 +0000203 IdentifierInfo *ParmName = 0;
204 SourceLocation ParmLoc;
205 bool BuiltinType = false;
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000206
Richard Smithb12bf692011-10-17 21:20:17 +0000207 switch (Tok.getKind()) {
208 case tok::kw_char:
209 case tok::kw_wchar_t:
210 case tok::kw_char16_t:
211 case tok::kw_char32_t:
212 case tok::kw_bool:
213 case tok::kw_short:
214 case tok::kw_int:
215 case tok::kw_long:
216 case tok::kw___int64:
Richard Smithf016bbc2012-04-04 06:24:32 +0000217 case tok::kw___int128:
Richard Smithb12bf692011-10-17 21:20:17 +0000218 case tok::kw_signed:
219 case tok::kw_unsigned:
220 case tok::kw_float:
221 case tok::kw_double:
222 case tok::kw_void:
223 case tok::kw_typeof:
224 // __attribute__(( vec_type_hint(char) ))
225 // FIXME: Don't just discard the builtin type token.
226 ConsumeToken();
227 BuiltinType = true;
228 break;
229
230 case tok::identifier:
231 ParmName = Tok.getIdentifierInfo();
232 ParmLoc = ConsumeToken();
233 break;
234
235 default:
236 break;
237 }
238
239 ExprVector ArgExprs(Actions);
240
241 if (!BuiltinType &&
242 (ParmLoc.isValid() ? Tok.is(tok::comma) : Tok.isNot(tok::r_paren))) {
243 // Eat the comma.
244 if (ParmLoc.isValid())
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000245 ConsumeToken();
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000246
Richard Smithb12bf692011-10-17 21:20:17 +0000247 // Parse the non-empty comma-separated list of expressions.
248 while (1) {
249 ExprResult ArgExpr(ParseAssignmentExpression());
250 if (ArgExpr.isInvalid()) {
251 SkipUntil(tok::r_paren);
252 return;
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000253 }
Richard Smithb12bf692011-10-17 21:20:17 +0000254 ArgExprs.push_back(ArgExpr.release());
255 if (Tok.isNot(tok::comma))
256 break;
257 ConsumeToken(); // Eat the comma, move to the next argument
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000258 }
Richard Smithb12bf692011-10-17 21:20:17 +0000259 }
Fariborz Jahanian6b708652011-10-18 17:11:10 +0000260 else if (Tok.is(tok::less) && AttrName->isStr("iboutletcollection")) {
261 if (!ExpectAndConsume(tok::less, diag::err_expected_less_after, "<",
262 tok::greater)) {
Fariborz Jahanian7f733022011-10-18 23:13:50 +0000263 while (Tok.is(tok::identifier)) {
264 ConsumeToken();
265 if (Tok.is(tok::greater))
266 break;
267 if (Tok.is(tok::comma)) {
268 ConsumeToken();
269 continue;
270 }
271 }
272 if (Tok.isNot(tok::greater))
273 Diag(Tok, diag::err_iboutletcollection_with_protocol);
Fariborz Jahanian6b708652011-10-18 17:11:10 +0000274 SkipUntil(tok::r_paren, false, true); // skip until ')'
275 }
276 }
Richard Smithb12bf692011-10-17 21:20:17 +0000277
278 SourceLocation RParen = Tok.getLocation();
279 if (!ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) {
280 AttributeList *attr =
Argyrios Kyrtzidis635a9b42011-09-13 16:05:53 +0000281 Attrs.addNew(AttrName, SourceRange(AttrNameLoc, RParen), 0, AttrNameLoc,
Alexis Hunta0e54d42012-06-18 16:13:52 +0000282 ParmName, ParmLoc, ArgExprs.take(), ArgExprs.size(),
283 AttributeList::AS_GNU);
Alexis Hunt3bc72c12012-06-19 23:57:03 +0000284 if (BuiltinType && attr->getKind() == AttributeList::AT_IBOutletCollection)
Richard Smithb12bf692011-10-17 21:20:17 +0000285 Diag(Tok, diag::err_iboutletcollection_builtintype);
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000286 }
287}
288
Chad Rosierc1183952012-06-26 22:30:43 +0000289/// \brief Parses a single argument for a declspec, including the
Aaron Ballman478faed2012-06-19 22:09:27 +0000290/// surrounding parens.
Chad Rosierc1183952012-06-26 22:30:43 +0000291void Parser::ParseMicrosoftDeclSpecWithSingleArg(IdentifierInfo *AttrName,
Aaron Ballman478faed2012-06-19 22:09:27 +0000292 SourceLocation AttrNameLoc,
293 ParsedAttributes &Attrs)
294{
295 BalancedDelimiterTracker T(*this, tok::l_paren);
Chad Rosierc1183952012-06-26 22:30:43 +0000296 if (T.expectAndConsume(diag::err_expected_lparen_after,
Aaron Ballman478faed2012-06-19 22:09:27 +0000297 AttrName->getNameStart(), tok::r_paren))
298 return;
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000299
Aaron Ballman478faed2012-06-19 22:09:27 +0000300 ExprResult ArgExpr(ParseConstantExpression());
301 if (ArgExpr.isInvalid()) {
302 T.skipToEnd();
303 return;
304 }
305 Expr *ExprList = ArgExpr.take();
Chad Rosierc1183952012-06-26 22:30:43 +0000306 Attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, SourceLocation(),
Aaron Ballman478faed2012-06-19 22:09:27 +0000307 &ExprList, 1, AttributeList::AS_Declspec);
308
309 T.consumeClose();
310}
311
Chad Rosierc1183952012-06-26 22:30:43 +0000312/// \brief Determines whether a declspec is a "simple" one requiring no
Aaron Ballman478faed2012-06-19 22:09:27 +0000313/// arguments.
314bool Parser::IsSimpleMicrosoftDeclSpec(IdentifierInfo *Ident) {
315 return llvm::StringSwitch<bool>(Ident->getName())
316 .Case("dllimport", true)
317 .Case("dllexport", true)
318 .Case("noreturn", true)
319 .Case("nothrow", true)
320 .Case("noinline", true)
321 .Case("naked", true)
322 .Case("appdomain", true)
323 .Case("process", true)
324 .Case("jitintrinsic", true)
325 .Case("noalias", true)
326 .Case("restrict", true)
327 .Case("novtable", true)
328 .Case("selectany", true)
329 .Case("thread", true)
330 .Default(false);
331}
332
Chad Rosierc1183952012-06-26 22:30:43 +0000333/// \brief Attempts to parse a declspec which is not simple (one that takes
Aaron Ballman478faed2012-06-19 22:09:27 +0000334/// parameters). Will return false if we properly handled the declspec, or
335/// true if it is an unknown declspec.
Chad Rosierc1183952012-06-26 22:30:43 +0000336void Parser::ParseComplexMicrosoftDeclSpec(IdentifierInfo *Ident,
Aaron Ballman478faed2012-06-19 22:09:27 +0000337 SourceLocation Loc,
338 ParsedAttributes &Attrs) {
339 // Try to handle the easy case first -- these declspecs all take a single
340 // parameter as their argument.
341 if (llvm::StringSwitch<bool>(Ident->getName())
342 .Case("uuid", true)
343 .Case("align", true)
344 .Case("allocate", true)
345 .Default(false)) {
346 ParseMicrosoftDeclSpecWithSingleArg(Ident, Loc, Attrs);
347 } else if (Ident->getName() == "deprecated") {
Chad Rosierc1183952012-06-26 22:30:43 +0000348 // The deprecated declspec has an optional single argument, so we will
349 // check for a l-paren to decide whether we should parse an argument or
Aaron Ballman478faed2012-06-19 22:09:27 +0000350 // not.
351 if (Tok.getKind() == tok::l_paren)
352 ParseMicrosoftDeclSpecWithSingleArg(Ident, Loc, Attrs);
353 else
Chad Rosierc1183952012-06-26 22:30:43 +0000354 Attrs.addNew(Ident, Loc, 0, Loc, 0, SourceLocation(), 0, 0,
Aaron Ballman478faed2012-06-19 22:09:27 +0000355 AttributeList::AS_Declspec);
356 } else if (Ident->getName() == "property") {
357 // The property declspec is more complex in that it can take one or two
Chad Rosierc1183952012-06-26 22:30:43 +0000358 // assignment expressions as a parameter, but the lhs of the assignment
Aaron Ballman478faed2012-06-19 22:09:27 +0000359 // must be named get or put.
360 //
Chad Rosierc1183952012-06-26 22:30:43 +0000361 // For right now, we will just skip to the closing right paren of the
Aaron Ballman478faed2012-06-19 22:09:27 +0000362 // property expression.
363 //
364 // FIXME: we should deal with __declspec(property) at some point because it
365 // is used in the platform SDK headers for the Parallel Patterns Library
366 // and ATL.
367 BalancedDelimiterTracker T(*this, tok::l_paren);
Chad Rosierc1183952012-06-26 22:30:43 +0000368 if (T.expectAndConsume(diag::err_expected_lparen_after,
Aaron Ballman478faed2012-06-19 22:09:27 +0000369 Ident->getNameStart(), tok::r_paren))
370 return;
371 T.skipToEnd();
372 } else {
373 // We don't recognize this as a valid declspec, but instead of creating the
374 // attribute and allowing sema to warn about it, we will warn here instead.
375 // This is because some attributes have multiple spellings, but we need to
376 // disallow that for declspecs (such as align vs aligned). If we made the
Chad Rosierc1183952012-06-26 22:30:43 +0000377 // attribute, we'd have to split the valid declspec spelling logic into
Aaron Ballman478faed2012-06-19 22:09:27 +0000378 // both locations.
379 Diag(Loc, diag::warn_ms_declspec_unknown) << Ident;
380
381 // If there's an open paren, we should eat the open and close parens under
382 // the assumption that this unknown declspec has parameters.
383 BalancedDelimiterTracker T(*this, tok::l_paren);
384 if (!T.consumeOpen())
385 T.skipToEnd();
386 }
387}
388
Eli Friedman06de2b52009-06-08 07:21:15 +0000389/// [MS] decl-specifier:
390/// __declspec ( extended-decl-modifier-seq )
391///
392/// [MS] extended-decl-modifier-seq:
393/// extended-decl-modifier[opt]
394/// extended-decl-modifier extended-decl-modifier-seq
Aaron Ballman478faed2012-06-19 22:09:27 +0000395void Parser::ParseMicrosoftDeclSpec(ParsedAttributes &Attrs) {
Steve Naroff3a9b7e02008-12-24 20:59:21 +0000396 assert(Tok.is(tok::kw___declspec) && "Not a declspec!");
Eli Friedman06de2b52009-06-08 07:21:15 +0000397
Steve Naroff3a9b7e02008-12-24 20:59:21 +0000398 ConsumeToken();
Aaron Ballman478faed2012-06-19 22:09:27 +0000399 BalancedDelimiterTracker T(*this, tok::l_paren);
Chad Rosierc1183952012-06-26 22:30:43 +0000400 if (T.expectAndConsume(diag::err_expected_lparen_after, "__declspec",
Aaron Ballman478faed2012-06-19 22:09:27 +0000401 tok::r_paren))
John McCall53fa7142010-12-24 02:08:15 +0000402 return;
Jakob Stoklund Olesene1c0ae62012-06-19 21:48:43 +0000403
Chad Rosierc1183952012-06-26 22:30:43 +0000404 // An empty declspec is perfectly legal and should not warn. Additionally,
Aaron Ballman478faed2012-06-19 22:09:27 +0000405 // you can specify multiple attributes per declspec.
406 while (Tok.getKind() != tok::r_paren) {
407 // We expect either a well-known identifier or a generic string. Anything
408 // else is a malformed declspec.
409 bool IsString = Tok.getKind() == tok::string_literal ? true : false;
Chad Rosierc1183952012-06-26 22:30:43 +0000410 if (!IsString && Tok.getKind() != tok::identifier &&
Aaron Ballman478faed2012-06-19 22:09:27 +0000411 Tok.getKind() != tok::kw_restrict) {
412 Diag(Tok, diag::err_ms_declspec_type);
413 T.skipToEnd();
414 return;
Jakob Stoklund Olesene1c0ae62012-06-19 21:48:43 +0000415 }
Aaron Ballman478faed2012-06-19 22:09:27 +0000416
417 IdentifierInfo *AttrName;
418 SourceLocation AttrNameLoc;
419 if (IsString) {
420 SmallString<8> StrBuffer;
421 bool Invalid = false;
422 StringRef Str = PP.getSpelling(Tok, StrBuffer, &Invalid);
423 if (Invalid) {
424 T.skipToEnd();
425 return;
Jakob Stoklund Olesene1c0ae62012-06-19 21:48:43 +0000426 }
Aaron Ballman478faed2012-06-19 22:09:27 +0000427 AttrName = PP.getIdentifierInfo(Str);
428 AttrNameLoc = ConsumeStringToken();
Jakob Stoklund Olesene1c0ae62012-06-19 21:48:43 +0000429 } else {
Aaron Ballman478faed2012-06-19 22:09:27 +0000430 AttrName = Tok.getIdentifierInfo();
431 AttrNameLoc = ConsumeToken();
Jakob Stoklund Olesene1c0ae62012-06-19 21:48:43 +0000432 }
Chad Rosierc1183952012-06-26 22:30:43 +0000433
Aaron Ballman478faed2012-06-19 22:09:27 +0000434 if (IsString || IsSimpleMicrosoftDeclSpec(AttrName))
Chad Rosierc1183952012-06-26 22:30:43 +0000435 // If we have a generic string, we will allow it because there is no
436 // documented list of allowable string declspecs, but we know they exist
Aaron Ballman478faed2012-06-19 22:09:27 +0000437 // (for instance, SAL declspecs in older versions of MSVC).
438 //
Chad Rosierc1183952012-06-26 22:30:43 +0000439 // Alternatively, if the identifier is a simple one, then it requires no
Aaron Ballman478faed2012-06-19 22:09:27 +0000440 // arguments and can be turned into an attribute directly.
Chad Rosierc1183952012-06-26 22:30:43 +0000441 Attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, SourceLocation(),
Aaron Ballman478faed2012-06-19 22:09:27 +0000442 0, 0, AttributeList::AS_Declspec);
443 else
444 ParseComplexMicrosoftDeclSpec(AttrName, AttrNameLoc, Attrs);
Jakob Stoklund Olesene1c0ae62012-06-19 21:48:43 +0000445 }
Aaron Ballman478faed2012-06-19 22:09:27 +0000446 T.consumeClose();
Eli Friedman53339e02009-06-08 23:27:34 +0000447}
448
John McCall53fa7142010-12-24 02:08:15 +0000449void Parser::ParseMicrosoftTypeAttributes(ParsedAttributes &attrs) {
Eli Friedman53339e02009-06-08 23:27:34 +0000450 // Treat these like attributes
Eli Friedman53339e02009-06-08 23:27:34 +0000451 while (Tok.is(tok::kw___fastcall) || Tok.is(tok::kw___stdcall) ||
Douglas Gregora941dca2010-05-18 16:57:00 +0000452 Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___cdecl) ||
Francois Pichet17ed0202011-08-18 09:59:55 +0000453 Tok.is(tok::kw___ptr64) || Tok.is(tok::kw___w64) ||
Francois Pichetf2fb4112011-08-25 00:36:46 +0000454 Tok.is(tok::kw___ptr32) ||
Francois Pichet17ed0202011-08-18 09:59:55 +0000455 Tok.is(tok::kw___unaligned)) {
Eli Friedman53339e02009-06-08 23:27:34 +0000456 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
457 SourceLocation AttrNameLoc = ConsumeToken();
John McCall084e83d2011-03-24 11:26:52 +0000458 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
Aaron Ballman478faed2012-06-19 22:09:27 +0000459 SourceLocation(), 0, 0, AttributeList::AS_MSTypespec);
Eli Friedman53339e02009-06-08 23:27:34 +0000460 }
Steve Naroff3a9b7e02008-12-24 20:59:21 +0000461}
462
John McCall53fa7142010-12-24 02:08:15 +0000463void Parser::ParseBorlandTypeAttributes(ParsedAttributes &attrs) {
Dawn Perchik335e16b2010-09-03 01:29:35 +0000464 // Treat these like attributes
465 while (Tok.is(tok::kw___pascal)) {
466 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
467 SourceLocation AttrNameLoc = ConsumeToken();
John McCall084e83d2011-03-24 11:26:52 +0000468 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
Aaron Ballman478faed2012-06-19 22:09:27 +0000469 SourceLocation(), 0, 0, AttributeList::AS_MSTypespec);
Dawn Perchik335e16b2010-09-03 01:29:35 +0000470 }
John McCall53fa7142010-12-24 02:08:15 +0000471}
472
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +0000473void Parser::ParseOpenCLAttributes(ParsedAttributes &attrs) {
474 // Treat these like attributes
475 while (Tok.is(tok::kw___kernel)) {
476 SourceLocation AttrNameLoc = ConsumeToken();
John McCall084e83d2011-03-24 11:26:52 +0000477 attrs.addNew(PP.getIdentifierInfo("opencl_kernel_function"),
478 AttrNameLoc, 0, AttrNameLoc, 0,
Alexis Hunta0e54d42012-06-18 16:13:52 +0000479 SourceLocation(), 0, 0, AttributeList::AS_GNU);
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +0000480 }
481}
482
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000483void Parser::ParseOpenCLQualifiers(DeclSpec &DS) {
484 SourceLocation Loc = Tok.getLocation();
485 switch(Tok.getKind()) {
486 // OpenCL qualifiers:
487 case tok::kw___private:
Chad Rosierc1183952012-06-26 22:30:43 +0000488 case tok::kw_private:
John McCall084e83d2011-03-24 11:26:52 +0000489 DS.getAttributes().addNewInteger(
Chad Rosierc1183952012-06-26 22:30:43 +0000490 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000491 PP.getIdentifierInfo("address_space"), Loc, 0);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000492 break;
Chad Rosierc1183952012-06-26 22:30:43 +0000493
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000494 case tok::kw___global:
John McCall084e83d2011-03-24 11:26:52 +0000495 DS.getAttributes().addNewInteger(
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000496 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000497 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_global);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000498 break;
Chad Rosierc1183952012-06-26 22:30:43 +0000499
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000500 case tok::kw___local:
John McCall084e83d2011-03-24 11:26:52 +0000501 DS.getAttributes().addNewInteger(
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000502 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000503 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_local);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000504 break;
Chad Rosierc1183952012-06-26 22:30:43 +0000505
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000506 case tok::kw___constant:
John McCall084e83d2011-03-24 11:26:52 +0000507 DS.getAttributes().addNewInteger(
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000508 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000509 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_constant);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000510 break;
Chad Rosierc1183952012-06-26 22:30:43 +0000511
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000512 case tok::kw___read_only:
John McCall084e83d2011-03-24 11:26:52 +0000513 DS.getAttributes().addNewInteger(
Chad Rosierc1183952012-06-26 22:30:43 +0000514 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000515 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_read_only);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000516 break;
Chad Rosierc1183952012-06-26 22:30:43 +0000517
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000518 case tok::kw___write_only:
John McCall084e83d2011-03-24 11:26:52 +0000519 DS.getAttributes().addNewInteger(
Chad Rosierc1183952012-06-26 22:30:43 +0000520 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000521 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_write_only);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000522 break;
Chad Rosierc1183952012-06-26 22:30:43 +0000523
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000524 case tok::kw___read_write:
John McCall084e83d2011-03-24 11:26:52 +0000525 DS.getAttributes().addNewInteger(
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000526 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000527 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_read_write);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000528 break;
529 default: break;
530 }
531}
532
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000533/// \brief Parse a version number.
534///
535/// version:
536/// simple-integer
537/// simple-integer ',' simple-integer
538/// simple-integer ',' simple-integer ',' simple-integer
539VersionTuple Parser::ParseVersionTuple(SourceRange &Range) {
540 Range = Tok.getLocation();
541
542 if (!Tok.is(tok::numeric_constant)) {
543 Diag(Tok, diag::err_expected_version);
544 SkipUntil(tok::comma, tok::r_paren, true, true, true);
545 return VersionTuple();
546 }
547
548 // Parse the major (and possibly minor and subminor) versions, which
549 // are stored in the numeric constant. We utilize a quirk of the
550 // lexer, which is that it handles something like 1.2.3 as a single
551 // numeric constant, rather than two separate tokens.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000552 SmallString<512> Buffer;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000553 Buffer.resize(Tok.getLength()+1);
554 const char *ThisTokBegin = &Buffer[0];
555
556 // Get the spelling of the token, which eliminates trigraphs, etc.
557 bool Invalid = false;
558 unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin, &Invalid);
559 if (Invalid)
560 return VersionTuple();
561
562 // Parse the major version.
563 unsigned AfterMajor = 0;
564 unsigned Major = 0;
565 while (AfterMajor < ActualLength && isdigit(ThisTokBegin[AfterMajor])) {
566 Major = Major * 10 + ThisTokBegin[AfterMajor] - '0';
567 ++AfterMajor;
568 }
569
570 if (AfterMajor == 0) {
571 Diag(Tok, diag::err_expected_version);
572 SkipUntil(tok::comma, tok::r_paren, true, true, true);
573 return VersionTuple();
574 }
575
576 if (AfterMajor == ActualLength) {
577 ConsumeToken();
578
579 // We only had a single version component.
580 if (Major == 0) {
581 Diag(Tok, diag::err_zero_version);
582 return VersionTuple();
583 }
584
585 return VersionTuple(Major);
586 }
587
588 if (ThisTokBegin[AfterMajor] != '.' || (AfterMajor + 1 == ActualLength)) {
589 Diag(Tok, diag::err_expected_version);
590 SkipUntil(tok::comma, tok::r_paren, true, true, true);
591 return VersionTuple();
592 }
593
594 // Parse the minor version.
595 unsigned AfterMinor = AfterMajor + 1;
596 unsigned Minor = 0;
597 while (AfterMinor < ActualLength && isdigit(ThisTokBegin[AfterMinor])) {
598 Minor = Minor * 10 + ThisTokBegin[AfterMinor] - '0';
599 ++AfterMinor;
600 }
601
602 if (AfterMinor == ActualLength) {
603 ConsumeToken();
Chad Rosierc1183952012-06-26 22:30:43 +0000604
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000605 // We had major.minor.
606 if (Major == 0 && Minor == 0) {
607 Diag(Tok, diag::err_zero_version);
608 return VersionTuple();
609 }
610
Chad Rosierc1183952012-06-26 22:30:43 +0000611 return VersionTuple(Major, Minor);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000612 }
613
614 // If what follows is not a '.', we have a problem.
615 if (ThisTokBegin[AfterMinor] != '.') {
616 Diag(Tok, diag::err_expected_version);
617 SkipUntil(tok::comma, tok::r_paren, true, true, true);
Chad Rosierc1183952012-06-26 22:30:43 +0000618 return VersionTuple();
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000619 }
620
621 // Parse the subminor version.
622 unsigned AfterSubminor = AfterMinor + 1;
623 unsigned Subminor = 0;
624 while (AfterSubminor < ActualLength && isdigit(ThisTokBegin[AfterSubminor])) {
625 Subminor = Subminor * 10 + ThisTokBegin[AfterSubminor] - '0';
626 ++AfterSubminor;
627 }
628
629 if (AfterSubminor != ActualLength) {
630 Diag(Tok, diag::err_expected_version);
631 SkipUntil(tok::comma, tok::r_paren, true, true, true);
632 return VersionTuple();
633 }
634 ConsumeToken();
635 return VersionTuple(Major, Minor, Subminor);
636}
637
638/// \brief Parse the contents of the "availability" attribute.
639///
640/// availability-attribute:
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000641/// 'availability' '(' platform ',' version-arg-list, opt-message')'
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000642///
643/// platform:
644/// identifier
645///
646/// version-arg-list:
647/// version-arg
648/// version-arg ',' version-arg-list
649///
650/// version-arg:
651/// 'introduced' '=' version
652/// 'deprecated' '=' version
Douglas Gregorfdd417f2012-03-11 04:53:21 +0000653/// 'obsoleted' = version
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000654/// 'unavailable'
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000655/// opt-message:
656/// 'message' '=' <string>
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000657void Parser::ParseAvailabilityAttribute(IdentifierInfo &Availability,
658 SourceLocation AvailabilityLoc,
659 ParsedAttributes &attrs,
660 SourceLocation *endLoc) {
661 SourceLocation PlatformLoc;
662 IdentifierInfo *Platform = 0;
663
664 enum { Introduced, Deprecated, Obsoleted, Unknown };
665 AvailabilityChange Changes[Unknown];
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000666 ExprResult MessageExpr;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000667
668 // Opening '('.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000669 BalancedDelimiterTracker T(*this, tok::l_paren);
670 if (T.consumeOpen()) {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000671 Diag(Tok, diag::err_expected_lparen);
672 return;
673 }
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000674
675 // Parse the platform name,
676 if (Tok.isNot(tok::identifier)) {
677 Diag(Tok, diag::err_availability_expected_platform);
678 SkipUntil(tok::r_paren);
679 return;
680 }
681 Platform = Tok.getIdentifierInfo();
682 PlatformLoc = ConsumeToken();
683
684 // Parse the ',' following the platform name.
685 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "", tok::r_paren))
686 return;
687
688 // If we haven't grabbed the pointers for the identifiers
689 // "introduced", "deprecated", and "obsoleted", do so now.
690 if (!Ident_introduced) {
691 Ident_introduced = PP.getIdentifierInfo("introduced");
692 Ident_deprecated = PP.getIdentifierInfo("deprecated");
693 Ident_obsoleted = PP.getIdentifierInfo("obsoleted");
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000694 Ident_unavailable = PP.getIdentifierInfo("unavailable");
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000695 Ident_message = PP.getIdentifierInfo("message");
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000696 }
697
698 // Parse the set of introductions/deprecations/removals.
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000699 SourceLocation UnavailableLoc;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000700 do {
701 if (Tok.isNot(tok::identifier)) {
702 Diag(Tok, diag::err_availability_expected_change);
703 SkipUntil(tok::r_paren);
704 return;
705 }
706 IdentifierInfo *Keyword = Tok.getIdentifierInfo();
707 SourceLocation KeywordLoc = ConsumeToken();
708
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000709 if (Keyword == Ident_unavailable) {
710 if (UnavailableLoc.isValid()) {
711 Diag(KeywordLoc, diag::err_availability_redundant)
712 << Keyword << SourceRange(UnavailableLoc);
Chad Rosierc1183952012-06-26 22:30:43 +0000713 }
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000714 UnavailableLoc = KeywordLoc;
715
716 if (Tok.isNot(tok::comma))
717 break;
718
719 ConsumeToken();
720 continue;
Chad Rosierc1183952012-06-26 22:30:43 +0000721 }
722
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000723 if (Tok.isNot(tok::equal)) {
724 Diag(Tok, diag::err_expected_equal_after)
725 << Keyword;
726 SkipUntil(tok::r_paren);
727 return;
728 }
729 ConsumeToken();
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000730 if (Keyword == Ident_message) {
731 if (!isTokenStringLiteral()) {
732 Diag(Tok, diag::err_expected_string_literal);
733 SkipUntil(tok::r_paren);
734 return;
735 }
736 MessageExpr = ParseStringLiteralExpression();
737 break;
738 }
Chad Rosierc1183952012-06-26 22:30:43 +0000739
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000740 SourceRange VersionRange;
741 VersionTuple Version = ParseVersionTuple(VersionRange);
Chad Rosierc1183952012-06-26 22:30:43 +0000742
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000743 if (Version.empty()) {
744 SkipUntil(tok::r_paren);
745 return;
746 }
747
748 unsigned Index;
749 if (Keyword == Ident_introduced)
750 Index = Introduced;
751 else if (Keyword == Ident_deprecated)
752 Index = Deprecated;
753 else if (Keyword == Ident_obsoleted)
754 Index = Obsoleted;
Chad Rosierc1183952012-06-26 22:30:43 +0000755 else
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000756 Index = Unknown;
757
758 if (Index < Unknown) {
759 if (!Changes[Index].KeywordLoc.isInvalid()) {
760 Diag(KeywordLoc, diag::err_availability_redundant)
Chad Rosierc1183952012-06-26 22:30:43 +0000761 << Keyword
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000762 << SourceRange(Changes[Index].KeywordLoc,
763 Changes[Index].VersionRange.getEnd());
764 }
765
766 Changes[Index].KeywordLoc = KeywordLoc;
767 Changes[Index].Version = Version;
768 Changes[Index].VersionRange = VersionRange;
769 } else {
770 Diag(KeywordLoc, diag::err_availability_unknown_change)
771 << Keyword << VersionRange;
772 }
773
774 if (Tok.isNot(tok::comma))
775 break;
776
777 ConsumeToken();
778 } while (true);
779
780 // Closing ')'.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000781 if (T.consumeClose())
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000782 return;
783
784 if (endLoc)
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000785 *endLoc = T.getCloseLocation();
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000786
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000787 // The 'unavailable' availability cannot be combined with any other
788 // availability changes. Make sure that hasn't happened.
789 if (UnavailableLoc.isValid()) {
790 bool Complained = false;
791 for (unsigned Index = Introduced; Index != Unknown; ++Index) {
792 if (Changes[Index].KeywordLoc.isValid()) {
793 if (!Complained) {
794 Diag(UnavailableLoc, diag::warn_availability_and_unavailable)
795 << SourceRange(Changes[Index].KeywordLoc,
796 Changes[Index].VersionRange.getEnd());
797 Complained = true;
798 }
799
800 // Clear out the availability.
801 Changes[Index] = AvailabilityChange();
802 }
803 }
804 }
805
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000806 // Record this attribute
Chad Rosierc1183952012-06-26 22:30:43 +0000807 attrs.addNew(&Availability,
808 SourceRange(AvailabilityLoc, T.getCloseLocation()),
Fariborz Jahanian586be882012-01-23 23:38:32 +0000809 0, AvailabilityLoc,
John McCall084e83d2011-03-24 11:26:52 +0000810 Platform, PlatformLoc,
811 Changes[Introduced],
812 Changes[Deprecated],
Chad Rosierc1183952012-06-26 22:30:43 +0000813 Changes[Obsoleted],
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000814 UnavailableLoc, MessageExpr.take(),
Alexis Hunta0e54d42012-06-18 16:13:52 +0000815 AttributeList::AS_GNU);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000816}
817
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000818
819// Late Parsed Attributes:
820// See other examples of late parsing in lib/Parse/ParseCXXInlineMethods
821
822void Parser::LateParsedDeclaration::ParseLexedAttributes() {}
823
824void Parser::LateParsedClass::ParseLexedAttributes() {
825 Self->ParseLexedAttributes(*Class);
826}
827
828void Parser::LateParsedAttribute::ParseLexedAttributes() {
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +0000829 Self->ParseLexedAttribute(*this, true, false);
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000830}
831
832/// Wrapper class which calls ParseLexedAttribute, after setting up the
833/// scope appropriately.
834void Parser::ParseLexedAttributes(ParsingClass &Class) {
835 // Deal with templates
836 // FIXME: Test cases to make sure this does the right thing for templates.
837 bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope;
838 ParseScope ClassTemplateScope(this, Scope::TemplateParamScope,
839 HasTemplateScope);
840 if (HasTemplateScope)
841 Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate);
842
Douglas Gregor3024f072012-04-16 07:05:22 +0000843 // Set or update the scope flags.
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000844 bool AlreadyHasClassScope = Class.TopLevelClass;
Douglas Gregor3024f072012-04-16 07:05:22 +0000845 unsigned ScopeFlags = Scope::ClassScope|Scope::DeclScope;
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000846 ParseScope ClassScope(this, ScopeFlags, !AlreadyHasClassScope);
847 ParseScopeFlags ClassScopeFlags(this, ScopeFlags, AlreadyHasClassScope);
848
DeLesley Hutchins6f860042012-04-06 15:10:17 +0000849 // Enter the scope of nested classes
850 if (!AlreadyHasClassScope)
851 Actions.ActOnStartDelayedMemberDeclarations(getCurScope(),
852 Class.TagOrTemplate);
Benjamin Kramer1d373c62012-05-17 12:01:52 +0000853 if (!Class.LateParsedDeclarations.empty()) {
Douglas Gregor3024f072012-04-16 07:05:22 +0000854 // Allow 'this' within late-parsed attributes.
Chad Rosierc1183952012-06-26 22:30:43 +0000855 Sema::CXXThisScopeRAII ThisScope(Actions, Class.TagOrTemplate,
Douglas Gregor3024f072012-04-16 07:05:22 +0000856 /*TypeQuals=*/0);
Chad Rosierc1183952012-06-26 22:30:43 +0000857
Douglas Gregor3024f072012-04-16 07:05:22 +0000858 for (unsigned i = 0, ni = Class.LateParsedDeclarations.size(); i < ni; ++i){
859 Class.LateParsedDeclarations[i]->ParseLexedAttributes();
860 }
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000861 }
Chad Rosierc1183952012-06-26 22:30:43 +0000862
DeLesley Hutchins6f860042012-04-06 15:10:17 +0000863 if (!AlreadyHasClassScope)
864 Actions.ActOnFinishDelayedMemberDeclarations(getCurScope(),
865 Class.TagOrTemplate);
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000866}
867
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +0000868
869/// \brief Parse all attributes in LAs, and attach them to Decl D.
870void Parser::ParseLexedAttributeList(LateParsedAttrList &LAs, Decl *D,
871 bool EnterScope, bool OnDefinition) {
872 for (unsigned i = 0, ni = LAs.size(); i < ni; ++i) {
DeLesley Hutchins19c722d2012-08-15 22:41:04 +0000873 if (D)
874 LAs[i]->addDecl(D);
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +0000875 ParseLexedAttribute(*LAs[i], EnterScope, OnDefinition);
Benjamin Kramerbafc49a2012-04-14 12:44:47 +0000876 delete LAs[i];
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +0000877 }
878 LAs.clear();
879}
880
881
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000882/// \brief Finish parsing an attribute for which parsing was delayed.
883/// This will be called at the end of parsing a class declaration
884/// for each LateParsedAttribute. We consume the saved tokens and
Chad Rosierc1183952012-06-26 22:30:43 +0000885/// create an attribute with the arguments filled in. We add this
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000886/// to the Attribute list for the decl.
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +0000887void Parser::ParseLexedAttribute(LateParsedAttribute &LA,
888 bool EnterScope, bool OnDefinition) {
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000889 // Save the current token position.
890 SourceLocation OrigLoc = Tok.getLocation();
891
892 // Append the current token at the end of the new token stream so that it
893 // doesn't get lost.
894 LA.Toks.push_back(Tok);
895 PP.EnterTokenStream(LA.Toks.data(), LA.Toks.size(), true, false);
896 // Consume the previously pushed token.
897 ConsumeAnyToken();
898
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +0000899 if (OnDefinition && !IsThreadSafetyAttribute(LA.AttrName.getName())) {
900 Diag(Tok, diag::warn_attribute_on_function_definition)
901 << LA.AttrName.getName();
902 }
903
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000904 ParsedAttributes Attrs(AttrFactory);
905 SourceLocation endLoc;
906
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +0000907 if (LA.Decls.size() == 1) {
908 Decl *D = LA.Decls[0];
Caitlin Sadowski990d5712011-09-08 17:42:31 +0000909
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +0000910 // If the Decl is templatized, add template parameters to scope.
911 bool HasTemplateScope = EnterScope && D->isTemplateDecl();
912 ParseScope TempScope(this, Scope::TemplateParamScope, HasTemplateScope);
913 if (HasTemplateScope)
914 Actions.ActOnReenterTemplateScope(Actions.CurScope, D);
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000915
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +0000916 // If the Decl is on a function, add function parameters to the scope.
917 bool HasFunctionScope = EnterScope && D->isFunctionOrFunctionTemplate();
918 ParseScope FnScope(this, Scope::FnScope|Scope::DeclScope, HasFunctionScope);
919 if (HasFunctionScope)
920 Actions.ActOnReenterFunctionContext(Actions.CurScope, D);
921
922 ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, &endLoc);
923
924 if (HasFunctionScope) {
925 Actions.ActOnExitFunctionContext();
926 FnScope.Exit(); // Pop scope, and remove Decls from IdResolver
927 }
928 if (HasTemplateScope) {
929 TempScope.Exit();
930 }
DeLesley Hutchins71d61032012-03-02 22:29:50 +0000931 } else if (LA.Decls.size() > 0) {
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +0000932 // If there are multiple decls, then the decl cannot be within the
933 // function scope.
934 ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, &endLoc);
DeLesley Hutchins71d61032012-03-02 22:29:50 +0000935 } else {
936 Diag(Tok, diag::warn_attribute_no_decl) << LA.AttrName.getName();
Caitlin Sadowski990d5712011-09-08 17:42:31 +0000937 }
938
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +0000939 for (unsigned i = 0, ni = LA.Decls.size(); i < ni; ++i) {
940 Actions.ActOnFinishDelayedAttribute(getCurScope(), LA.Decls[i], Attrs);
941 }
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000942
943 if (Tok.getLocation() != OrigLoc) {
944 // Due to a parsing error, we either went over the cached tokens or
945 // there are still cached tokens left, so we skip the leftover tokens.
946 // Since this is an uncommon situation that should be avoided, use the
947 // expensive isBeforeInTranslationUnit call.
948 if (PP.getSourceManager().isBeforeInTranslationUnit(Tok.getLocation(),
949 OrigLoc))
950 while (Tok.getLocation() != OrigLoc && Tok.isNot(tok::eof))
Douglas Gregor0cf55e92012-03-08 01:00:17 +0000951 ConsumeAnyToken();
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000952 }
953}
954
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000955/// \brief Wrapper around a case statement checking if AttrName is
956/// one of the thread safety attributes
957bool Parser::IsThreadSafetyAttribute(llvm::StringRef AttrName){
958 return llvm::StringSwitch<bool>(AttrName)
959 .Case("guarded_by", true)
960 .Case("guarded_var", true)
961 .Case("pt_guarded_by", true)
962 .Case("pt_guarded_var", true)
963 .Case("lockable", true)
964 .Case("scoped_lockable", true)
965 .Case("no_thread_safety_analysis", true)
966 .Case("acquired_after", true)
967 .Case("acquired_before", true)
968 .Case("exclusive_lock_function", true)
969 .Case("shared_lock_function", true)
970 .Case("exclusive_trylock_function", true)
971 .Case("shared_trylock_function", true)
972 .Case("unlock_function", true)
973 .Case("lock_returned", true)
974 .Case("locks_excluded", true)
975 .Case("exclusive_locks_required", true)
976 .Case("shared_locks_required", true)
977 .Default(false);
978}
979
980/// \brief Parse the contents of thread safety attributes. These
981/// should always be parsed as an expression list.
982///
983/// We need to special case the parsing due to the fact that if the first token
984/// of the first argument is an identifier, the main parse loop will store
985/// that token as a "parameter" and the rest of
986/// the arguments will be added to a list of "arguments". However,
987/// subsequent tokens in the first argument are lost. We instead parse each
988/// argument as an expression and add all arguments to the list of "arguments".
989/// In future, we will take advantage of this special case to also
990/// deal with some argument scoping issues here (for example, referring to a
991/// function parameter in the attribute on that function).
992void Parser::ParseThreadSafetyAttribute(IdentifierInfo &AttrName,
993 SourceLocation AttrNameLoc,
994 ParsedAttributes &Attrs,
995 SourceLocation *EndLoc) {
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000996 assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('");
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000997
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000998 BalancedDelimiterTracker T(*this, tok::l_paren);
999 T.consumeOpen();
Chad Rosierc1183952012-06-26 22:30:43 +00001000
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001001 ExprVector ArgExprs(Actions);
1002 bool ArgExprsOk = true;
Chad Rosierc1183952012-06-26 22:30:43 +00001003
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001004 // now parse the list of expressions
DeLesley Hutchins36f5d852011-12-14 19:36:06 +00001005 while (Tok.isNot(tok::r_paren)) {
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001006 ExprResult ArgExpr(ParseAssignmentExpression());
1007 if (ArgExpr.isInvalid()) {
1008 ArgExprsOk = false;
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001009 T.consumeClose();
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001010 break;
1011 } else {
1012 ArgExprs.push_back(ArgExpr.release());
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +00001013 }
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001014 if (Tok.isNot(tok::comma))
1015 break;
1016 ConsumeToken(); // Eat the comma, move to the next argument
1017 }
1018 // Match the ')'.
DeLesley Hutchins30398dd2012-01-20 22:50:54 +00001019 if (ArgExprsOk && !T.consumeClose()) {
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001020 Attrs.addNew(&AttrName, AttrNameLoc, 0, AttrNameLoc, 0, SourceLocation(),
Alexis Hunta0e54d42012-06-18 16:13:52 +00001021 ArgExprs.take(), ArgExprs.size(), AttributeList::AS_GNU);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +00001022 }
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001023 if (EndLoc)
1024 *EndLoc = T.getCloseLocation();
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +00001025}
1026
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00001027void Parser::ParseTypeTagForDatatypeAttribute(IdentifierInfo &AttrName,
1028 SourceLocation AttrNameLoc,
1029 ParsedAttributes &Attrs,
1030 SourceLocation *EndLoc) {
1031 assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('");
1032
1033 BalancedDelimiterTracker T(*this, tok::l_paren);
1034 T.consumeOpen();
1035
1036 if (Tok.isNot(tok::identifier)) {
1037 Diag(Tok, diag::err_expected_ident);
1038 T.skipToEnd();
1039 return;
1040 }
1041 IdentifierInfo *ArgumentKind = Tok.getIdentifierInfo();
1042 SourceLocation ArgumentKindLoc = ConsumeToken();
1043
1044 if (Tok.isNot(tok::comma)) {
1045 Diag(Tok, diag::err_expected_comma);
1046 T.skipToEnd();
1047 return;
1048 }
1049 ConsumeToken();
1050
1051 SourceRange MatchingCTypeRange;
1052 TypeResult MatchingCType = ParseTypeName(&MatchingCTypeRange);
1053 if (MatchingCType.isInvalid()) {
1054 T.skipToEnd();
1055 return;
1056 }
1057
1058 bool LayoutCompatible = false;
1059 bool MustBeNull = false;
1060 while (Tok.is(tok::comma)) {
1061 ConsumeToken();
1062 if (Tok.isNot(tok::identifier)) {
1063 Diag(Tok, diag::err_expected_ident);
1064 T.skipToEnd();
1065 return;
1066 }
1067 IdentifierInfo *Flag = Tok.getIdentifierInfo();
1068 if (Flag->isStr("layout_compatible"))
1069 LayoutCompatible = true;
1070 else if (Flag->isStr("must_be_null"))
1071 MustBeNull = true;
1072 else {
1073 Diag(Tok, diag::err_type_safety_unknown_flag) << Flag;
1074 T.skipToEnd();
1075 return;
1076 }
1077 ConsumeToken(); // consume flag
1078 }
1079
1080 if (!T.consumeClose()) {
1081 Attrs.addNewTypeTagForDatatype(&AttrName, AttrNameLoc, 0, AttrNameLoc,
1082 ArgumentKind, ArgumentKindLoc,
1083 MatchingCType.release(), LayoutCompatible,
1084 MustBeNull, AttributeList::AS_GNU);
1085 }
1086
1087 if (EndLoc)
1088 *EndLoc = T.getCloseLocation();
1089}
1090
Richard Smith7bdcc4a2012-04-10 01:32:12 +00001091/// DiagnoseProhibitedCXX11Attribute - We have found the opening square brackets
1092/// of a C++11 attribute-specifier in a location where an attribute is not
1093/// permitted. By C++11 [dcl.attr.grammar]p6, this is ill-formed. Diagnose this
1094/// situation.
1095///
1096/// \return \c true if we skipped an attribute-like chunk of tokens, \c false if
1097/// this doesn't appear to actually be an attribute-specifier, and the caller
1098/// should try to parse it.
1099bool Parser::DiagnoseProhibitedCXX11Attribute() {
1100 assert(Tok.is(tok::l_square) && NextToken().is(tok::l_square));
1101
1102 switch (isCXX11AttributeSpecifier(/*Disambiguate*/true)) {
1103 case CAK_NotAttributeSpecifier:
1104 // No diagnostic: we're in Obj-C++11 and this is not actually an attribute.
1105 return false;
1106
1107 case CAK_InvalidAttributeSpecifier:
1108 Diag(Tok.getLocation(), diag::err_l_square_l_square_not_attribute);
1109 return false;
1110
1111 case CAK_AttributeSpecifier:
1112 // Parse and discard the attributes.
1113 SourceLocation BeginLoc = ConsumeBracket();
1114 ConsumeBracket();
1115 SkipUntil(tok::r_square, /*StopAtSemi*/ false);
1116 assert(Tok.is(tok::r_square) && "isCXX11AttributeSpecifier lied");
1117 SourceLocation EndLoc = ConsumeBracket();
1118 Diag(BeginLoc, diag::err_attributes_not_allowed)
1119 << SourceRange(BeginLoc, EndLoc);
1120 return true;
1121 }
Chandler Carruthd8f7d382012-04-10 16:03:08 +00001122 llvm_unreachable("All cases handled above.");
Richard Smith7bdcc4a2012-04-10 01:32:12 +00001123}
1124
John McCall53fa7142010-12-24 02:08:15 +00001125void Parser::DiagnoseProhibitedAttributes(ParsedAttributesWithRange &attrs) {
1126 Diag(attrs.Range.getBegin(), diag::err_attributes_not_allowed)
1127 << attrs.Range;
Dawn Perchik335e16b2010-09-03 01:29:35 +00001128}
1129
Chris Lattner53361ac2006-08-10 05:19:57 +00001130/// ParseDeclaration - Parse a full 'declaration', which consists of
1131/// declaration-specifiers, some number of declarators, and a semicolon.
Chris Lattner49836b42009-04-02 04:16:50 +00001132/// 'Context' should be a Declarator::TheContext value. This returns the
1133/// location of the semicolon in DeclEnd.
Chris Lattnera5235172007-08-25 06:57:03 +00001134///
1135/// declaration: [C99 6.7]
1136/// block-declaration ->
1137/// simple-declaration
1138/// others [FIXME]
Douglas Gregoreb31f392008-12-01 23:54:00 +00001139/// [C++] template-declaration
Chris Lattnera5235172007-08-25 06:57:03 +00001140/// [C++] namespace-definition
Douglas Gregord7c4d982008-12-30 03:27:21 +00001141/// [C++] using-directive
Douglas Gregor77b50e12009-06-22 23:06:13 +00001142/// [C++] using-declaration
Richard Smithc202b282012-04-14 00:33:13 +00001143/// [C++11/C11] static_assert-declaration
Chris Lattnera5235172007-08-25 06:57:03 +00001144/// others... [FIXME]
1145///
Fariborz Jahanian1db5c942010-09-28 20:42:35 +00001146Parser::DeclGroupPtrTy Parser::ParseDeclaration(StmtVector &Stmts,
1147 unsigned Context,
Alexis Hunt96d5c762009-11-21 08:43:09 +00001148 SourceLocation &DeclEnd,
John McCall53fa7142010-12-24 02:08:15 +00001149 ParsedAttributesWithRange &attrs) {
Argyrios Kyrtzidis355094e2010-06-17 10:52:18 +00001150 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Fariborz Jahanian59b75282011-08-30 17:10:52 +00001151 // Must temporarily exit the objective-c container scope for
1152 // parsing c none objective-c decls.
1153 ObjCDeclContextSwitch ObjCDC(*this);
Chad Rosierc1183952012-06-26 22:30:43 +00001154
John McCall48871652010-08-21 09:40:31 +00001155 Decl *SingleDecl = 0;
Richard Smithcd1c0552011-07-01 19:46:12 +00001156 Decl *OwnedType = 0;
Chris Lattnera5235172007-08-25 06:57:03 +00001157 switch (Tok.getKind()) {
Douglas Gregoreb31f392008-12-01 23:54:00 +00001158 case tok::kw_template:
Douglas Gregor23996282009-05-12 21:31:51 +00001159 case tok::kw_export:
John McCall53fa7142010-12-24 02:08:15 +00001160 ProhibitAttributes(attrs);
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001161 SingleDecl = ParseDeclarationStartingWithTemplate(Context, DeclEnd);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001162 break;
Sebastian Redl67667942010-08-27 23:12:46 +00001163 case tok::kw_inline:
Sebastian Redl5a5f2c72010-08-31 00:36:45 +00001164 // Could be the start of an inline namespace. Allowed as an ext in C++03.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001165 if (getLangOpts().CPlusPlus && NextToken().is(tok::kw_namespace)) {
John McCall53fa7142010-12-24 02:08:15 +00001166 ProhibitAttributes(attrs);
Sebastian Redl67667942010-08-27 23:12:46 +00001167 SourceLocation InlineLoc = ConsumeToken();
1168 SingleDecl = ParseNamespace(Context, DeclEnd, InlineLoc);
1169 break;
1170 }
Chad Rosierc1183952012-06-26 22:30:43 +00001171 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs,
Fariborz Jahanian1db5c942010-09-28 20:42:35 +00001172 true);
Chris Lattnera5235172007-08-25 06:57:03 +00001173 case tok::kw_namespace:
John McCall53fa7142010-12-24 02:08:15 +00001174 ProhibitAttributes(attrs);
Chris Lattner49836b42009-04-02 04:16:50 +00001175 SingleDecl = ParseNamespace(Context, DeclEnd);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001176 break;
Douglas Gregord7c4d982008-12-30 03:27:21 +00001177 case tok::kw_using:
John McCall9b72f892010-11-10 02:40:36 +00001178 SingleDecl = ParseUsingDirectiveOrDeclaration(Context, ParsedTemplateInfo(),
Richard Smithcd1c0552011-07-01 19:46:12 +00001179 DeclEnd, attrs, &OwnedType);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001180 break;
Anders Carlssonf24fcff62009-03-11 16:27:10 +00001181 case tok::kw_static_assert:
Peter Collingbourne3d9cbdc2011-04-15 00:35:57 +00001182 case tok::kw__Static_assert:
John McCall53fa7142010-12-24 02:08:15 +00001183 ProhibitAttributes(attrs);
Chris Lattner49836b42009-04-02 04:16:50 +00001184 SingleDecl = ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001185 break;
Chris Lattnera5235172007-08-25 06:57:03 +00001186 default:
John McCall53fa7142010-12-24 02:08:15 +00001187 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs, true);
Chris Lattnera5235172007-08-25 06:57:03 +00001188 }
Chad Rosierc1183952012-06-26 22:30:43 +00001189
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001190 // This routine returns a DeclGroup, if the thing we parsed only contains a
Richard Smithcd1c0552011-07-01 19:46:12 +00001191 // single decl, convert it now. Alias declarations can also declare a type;
1192 // include that too if it is present.
1193 return Actions.ConvertDeclToDeclGroup(SingleDecl, OwnedType);
Chris Lattnera5235172007-08-25 06:57:03 +00001194}
1195
1196/// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl]
1197/// declaration-specifiers init-declarator-list[opt] ';'
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001198/// [C++11] attribute-specifier-seq decl-specifier-seq[opt]
1199/// init-declarator-list ';'
Chris Lattnera5235172007-08-25 06:57:03 +00001200///[C90/C++]init-declarator-list ';' [TODO]
1201/// [OMP] threadprivate-directive [TODO]
Chris Lattner32dc41c2009-03-29 17:27:48 +00001202///
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001203/// for-range-declaration: [C++11 6.5p1: stmt.ranged]
Richard Smith02e85f32011-04-14 22:09:26 +00001204/// attribute-specifier-seq[opt] type-specifier-seq declarator
1205///
Chris Lattner32dc41c2009-03-29 17:27:48 +00001206/// If RequireSemi is false, this does not check for a ';' at the end of the
Chris Lattner005fc1b2010-04-05 18:18:31 +00001207/// declaration. If it is true, it checks for and eats it.
Richard Smith02e85f32011-04-14 22:09:26 +00001208///
1209/// If FRI is non-null, we might be parsing a for-range-declaration instead
1210/// of a simple-declaration. If we find that we are, we also parse the
1211/// for-range-initializer, and place it here.
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001212Parser::DeclGroupPtrTy
1213Parser::ParseSimpleDeclaration(StmtVector &Stmts, unsigned Context,
1214 SourceLocation &DeclEnd,
1215 ParsedAttributesWithRange &attrs,
1216 bool RequireSemi, ForRangeInit *FRI) {
Chris Lattner53361ac2006-08-10 05:19:57 +00001217 // Parse the common declaration-specifiers piece.
John McCall28a6aea2009-11-04 02:18:39 +00001218 ParsingDeclSpec DS(*this);
John McCall53fa7142010-12-24 02:08:15 +00001219 DS.takeAttributesFrom(attrs);
Douglas Gregor0e7dde52011-04-24 05:37:28 +00001220
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001221 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS_none,
Richard Smith30482bc2011-02-20 03:19:35 +00001222 getDeclSpecContextFromDeclaratorContext(Context));
Abramo Bagnara1cd83682012-01-07 10:52:36 +00001223
Chris Lattner0e894622006-08-13 19:58:17 +00001224 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
1225 // declaration-specifiers init-declarator-list[opt] ';'
Chris Lattner76c72282007-10-09 17:33:22 +00001226 if (Tok.is(tok::semi)) {
Argyrios Kyrtzidisfbb2bb52012-05-16 23:49:15 +00001227 DeclEnd = Tok.getLocation();
Chris Lattner005fc1b2010-04-05 18:18:31 +00001228 if (RequireSemi) ConsumeToken();
John McCall48871652010-08-21 09:40:31 +00001229 Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none,
Douglas Gregor0e7dde52011-04-24 05:37:28 +00001230 DS);
John McCall28a6aea2009-11-04 02:18:39 +00001231 DS.complete(TheDecl);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001232 return Actions.ConvertDeclToDeclGroup(TheDecl);
Chris Lattner0e894622006-08-13 19:58:17 +00001233 }
Chad Rosierc1183952012-06-26 22:30:43 +00001234
1235 return ParseDeclGroup(DS, Context, /*FunctionDefs=*/ false, &DeclEnd, FRI);
John McCalld5a36322009-11-03 19:26:08 +00001236}
Mike Stump11289f42009-09-09 15:08:12 +00001237
Richard Smith09f76ee2011-10-19 21:33:05 +00001238/// Returns true if this might be the start of a declarator, or a common typo
1239/// for a declarator.
1240bool Parser::MightBeDeclarator(unsigned Context) {
1241 switch (Tok.getKind()) {
1242 case tok::annot_cxxscope:
1243 case tok::annot_template_id:
1244 case tok::caret:
1245 case tok::code_completion:
1246 case tok::coloncolon:
1247 case tok::ellipsis:
1248 case tok::kw___attribute:
1249 case tok::kw_operator:
1250 case tok::l_paren:
1251 case tok::star:
1252 return true;
1253
1254 case tok::amp:
1255 case tok::ampamp:
David Blaikiebbafb8a2012-03-11 07:00:24 +00001256 return getLangOpts().CPlusPlus;
Richard Smith09f76ee2011-10-19 21:33:05 +00001257
Richard Smithc8a79032012-01-09 22:31:44 +00001258 case tok::l_square: // Might be an attribute on an unnamed bit-field.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001259 return Context == Declarator::MemberContext && getLangOpts().CPlusPlus0x &&
Richard Smithc8a79032012-01-09 22:31:44 +00001260 NextToken().is(tok::l_square);
1261
1262 case tok::colon: // Might be a typo for '::' or an unnamed bit-field.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001263 return Context == Declarator::MemberContext || getLangOpts().CPlusPlus;
Richard Smithc8a79032012-01-09 22:31:44 +00001264
Richard Smith09f76ee2011-10-19 21:33:05 +00001265 case tok::identifier:
1266 switch (NextToken().getKind()) {
1267 case tok::code_completion:
1268 case tok::coloncolon:
1269 case tok::comma:
1270 case tok::equal:
1271 case tok::equalequal: // Might be a typo for '='.
1272 case tok::kw_alignas:
1273 case tok::kw_asm:
1274 case tok::kw___attribute:
1275 case tok::l_brace:
1276 case tok::l_paren:
1277 case tok::l_square:
1278 case tok::less:
1279 case tok::r_brace:
1280 case tok::r_paren:
1281 case tok::r_square:
1282 case tok::semi:
1283 return true;
1284
1285 case tok::colon:
1286 // At namespace scope, 'identifier:' is probably a typo for 'identifier::'
Richard Smithc8a79032012-01-09 22:31:44 +00001287 // and in block scope it's probably a label. Inside a class definition,
1288 // this is a bit-field.
1289 return Context == Declarator::MemberContext ||
David Blaikiebbafb8a2012-03-11 07:00:24 +00001290 (getLangOpts().CPlusPlus && Context == Declarator::FileContext);
Richard Smithc8a79032012-01-09 22:31:44 +00001291
1292 case tok::identifier: // Possible virt-specifier.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001293 return getLangOpts().CPlusPlus0x && isCXX0XVirtSpecifier(NextToken());
Richard Smith09f76ee2011-10-19 21:33:05 +00001294
1295 default:
1296 return false;
1297 }
1298
1299 default:
1300 return false;
1301 }
1302}
1303
Richard Smithb8caac82012-04-11 20:59:20 +00001304/// Skip until we reach something which seems like a sensible place to pick
1305/// up parsing after a malformed declaration. This will sometimes stop sooner
1306/// than SkipUntil(tok::r_brace) would, but will never stop later.
1307void Parser::SkipMalformedDecl() {
1308 while (true) {
1309 switch (Tok.getKind()) {
1310 case tok::l_brace:
1311 // Skip until matching }, then stop. We've probably skipped over
1312 // a malformed class or function definition or similar.
1313 ConsumeBrace();
1314 SkipUntil(tok::r_brace, /*StopAtSemi*/false);
1315 if (Tok.is(tok::comma) || Tok.is(tok::l_brace) || Tok.is(tok::kw_try)) {
1316 // This declaration isn't over yet. Keep skipping.
1317 continue;
1318 }
1319 if (Tok.is(tok::semi))
1320 ConsumeToken();
1321 return;
1322
1323 case tok::l_square:
1324 ConsumeBracket();
1325 SkipUntil(tok::r_square, /*StopAtSemi*/false);
1326 continue;
1327
1328 case tok::l_paren:
1329 ConsumeParen();
1330 SkipUntil(tok::r_paren, /*StopAtSemi*/false);
1331 continue;
1332
1333 case tok::r_brace:
1334 return;
1335
1336 case tok::semi:
1337 ConsumeToken();
1338 return;
1339
1340 case tok::kw_inline:
1341 // 'inline namespace' at the start of a line is almost certainly
Jordan Rose12e730c2012-07-09 16:54:53 +00001342 // a good place to pick back up parsing, except in an Objective-C
1343 // @interface context.
1344 if (Tok.isAtStartOfLine() && NextToken().is(tok::kw_namespace) &&
1345 (!ParsingInObjCContainer || CurParsedObjCImpl))
Richard Smithb8caac82012-04-11 20:59:20 +00001346 return;
1347 break;
1348
1349 case tok::kw_namespace:
1350 // 'namespace' at the start of a line is almost certainly a good
Jordan Rose12e730c2012-07-09 16:54:53 +00001351 // place to pick back up parsing, except in an Objective-C
1352 // @interface context.
1353 if (Tok.isAtStartOfLine() &&
1354 (!ParsingInObjCContainer || CurParsedObjCImpl))
1355 return;
1356 break;
1357
1358 case tok::at:
1359 // @end is very much like } in Objective-C contexts.
1360 if (NextToken().isObjCAtKeyword(tok::objc_end) &&
1361 ParsingInObjCContainer)
1362 return;
1363 break;
1364
1365 case tok::minus:
1366 case tok::plus:
1367 // - and + probably start new method declarations in Objective-C contexts.
1368 if (Tok.isAtStartOfLine() && ParsingInObjCContainer)
Richard Smithb8caac82012-04-11 20:59:20 +00001369 return;
1370 break;
1371
1372 case tok::eof:
1373 return;
1374
1375 default:
1376 break;
1377 }
1378
1379 ConsumeAnyToken();
1380 }
1381}
1382
John McCalld5a36322009-11-03 19:26:08 +00001383/// ParseDeclGroup - Having concluded that this is either a function
1384/// definition or a group of object declarations, actually parse the
1385/// result.
John McCall28a6aea2009-11-04 02:18:39 +00001386Parser::DeclGroupPtrTy Parser::ParseDeclGroup(ParsingDeclSpec &DS,
1387 unsigned Context,
John McCalld5a36322009-11-03 19:26:08 +00001388 bool AllowFunctionDefinitions,
Richard Smith02e85f32011-04-14 22:09:26 +00001389 SourceLocation *DeclEnd,
1390 ForRangeInit *FRI) {
John McCalld5a36322009-11-03 19:26:08 +00001391 // Parse the first declarator.
John McCall28a6aea2009-11-04 02:18:39 +00001392 ParsingDeclarator D(*this, DS, static_cast<Declarator::TheContext>(Context));
John McCalld5a36322009-11-03 19:26:08 +00001393 ParseDeclarator(D);
Chris Lattner32dc41c2009-03-29 17:27:48 +00001394
John McCalld5a36322009-11-03 19:26:08 +00001395 // Bail out if the first declarator didn't seem well-formed.
1396 if (!D.hasName() && !D.mayOmitIdentifier()) {
Richard Smithb8caac82012-04-11 20:59:20 +00001397 SkipMalformedDecl();
John McCalld5a36322009-11-03 19:26:08 +00001398 return DeclGroupPtrTy();
Chris Lattnerefb0f112009-03-29 17:18:04 +00001399 }
Mike Stump11289f42009-09-09 15:08:12 +00001400
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001401 // Save late-parsed attributes for now; they need to be parsed in the
1402 // appropriate function scope after the function Decl has been constructed.
1403 LateParsedAttrList LateParsedAttrs;
1404 if (D.isFunctionDeclarator())
1405 MaybeParseGNUAttributes(D, &LateParsedAttrs);
1406
Chris Lattnerdbb1e932010-07-11 22:24:20 +00001407 // Check to see if we have a function *definition* which must have a body.
1408 if (AllowFunctionDefinitions && D.isFunctionDeclarator() &&
1409 // Look at the next token to make sure that this isn't a function
1410 // declaration. We have to check this because __attribute__ might be the
1411 // start of a function definition in GCC-extended K&R C.
Fariborz Jahanian712bb812012-08-10 15:54:40 +00001412 !isDeclarationAfterDeclarator()) {
Chad Rosierc1183952012-06-26 22:30:43 +00001413
Chris Lattner13901342010-07-11 22:42:07 +00001414 if (isStartOfFunctionDefinition(D)) {
John McCalld5a36322009-11-03 19:26:08 +00001415 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
1416 Diag(Tok, diag::err_function_declared_typedef);
1417
1418 // Recover by treating the 'typedef' as spurious.
1419 DS.ClearStorageClassSpecs();
1420 }
1421
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001422 Decl *TheDecl =
1423 ParseFunctionDefinition(D, ParsedTemplateInfo(), &LateParsedAttrs);
John McCalld5a36322009-11-03 19:26:08 +00001424 return Actions.ConvertDeclToDeclGroup(TheDecl);
Chris Lattner13901342010-07-11 22:42:07 +00001425 }
Chad Rosierc1183952012-06-26 22:30:43 +00001426
Chris Lattner13901342010-07-11 22:42:07 +00001427 if (isDeclarationSpecifier()) {
1428 // If there is an invalid declaration specifier right after the function
1429 // prototype, then we must be in a missing semicolon case where this isn't
1430 // actually a body. Just fall through into the code that handles it as a
1431 // prototype, and let the top-level code handle the erroneous declspec
1432 // where it would otherwise expect a comma or semicolon.
John McCalld5a36322009-11-03 19:26:08 +00001433 } else {
1434 Diag(Tok, diag::err_expected_fn_body);
1435 SkipUntil(tok::semi);
1436 return DeclGroupPtrTy();
1437 }
1438 }
1439
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001440 if (ParseAsmAttributesAfterDeclarator(D))
Richard Smith02e85f32011-04-14 22:09:26 +00001441 return DeclGroupPtrTy();
1442
1443 // C++0x [stmt.iter]p1: Check if we have a for-range-declarator. If so, we
1444 // must parse and analyze the for-range-initializer before the declaration is
1445 // analyzed.
1446 if (FRI && Tok.is(tok::colon)) {
1447 FRI->ColonLoc = ConsumeToken();
Sebastian Redl3da34892011-06-05 12:23:16 +00001448 if (Tok.is(tok::l_brace))
1449 FRI->RangeExpr = ParseBraceInitializer();
1450 else
1451 FRI->RangeExpr = ParseExpression();
Richard Smith02e85f32011-04-14 22:09:26 +00001452 Decl *ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
1453 Actions.ActOnCXXForRangeDecl(ThisDecl);
1454 Actions.FinalizeDeclaration(ThisDecl);
John McCallcf6e0c82012-01-27 01:29:43 +00001455 D.complete(ThisDecl);
Richard Smith02e85f32011-04-14 22:09:26 +00001456 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS, &ThisDecl, 1);
1457 }
1458
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001459 SmallVector<Decl *, 8> DeclsInGroup;
Richard Smith02e85f32011-04-14 22:09:26 +00001460 Decl *FirstDecl = ParseDeclarationAfterDeclaratorAndAttributes(D);
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001461 if (LateParsedAttrs.size() > 0)
1462 ParseLexedAttributeList(LateParsedAttrs, FirstDecl, true, false);
John McCall28a6aea2009-11-04 02:18:39 +00001463 D.complete(FirstDecl);
John McCall48871652010-08-21 09:40:31 +00001464 if (FirstDecl)
John McCalld5a36322009-11-03 19:26:08 +00001465 DeclsInGroup.push_back(FirstDecl);
1466
Richard Smith09f76ee2011-10-19 21:33:05 +00001467 bool ExpectSemi = Context != Declarator::ForContext;
Fariborz Jahanian577574a2012-07-02 23:37:09 +00001468
John McCalld5a36322009-11-03 19:26:08 +00001469 // If we don't have a comma, it is either the end of the list (a ';') or an
1470 // error, bail out.
1471 while (Tok.is(tok::comma)) {
Richard Smith09f76ee2011-10-19 21:33:05 +00001472 SourceLocation CommaLoc = ConsumeToken();
1473
1474 if (Tok.isAtStartOfLine() && ExpectSemi && !MightBeDeclarator(Context)) {
1475 // This comma was followed by a line-break and something which can't be
1476 // the start of a declarator. The comma was probably a typo for a
1477 // semicolon.
1478 Diag(CommaLoc, diag::err_expected_semi_declaration)
1479 << FixItHint::CreateReplacement(CommaLoc, ";");
1480 ExpectSemi = false;
1481 break;
1482 }
John McCalld5a36322009-11-03 19:26:08 +00001483
1484 // Parse the next declarator.
1485 D.clear();
Richard Smith8d06f422012-01-12 23:53:29 +00001486 D.setCommaLoc(CommaLoc);
John McCalld5a36322009-11-03 19:26:08 +00001487
1488 // Accept attributes in an init-declarator. In the first declarator in a
1489 // declaration, these would be part of the declspec. In subsequent
1490 // declarators, they become part of the declarator itself, so that they
1491 // don't apply to declarators after *this* one. Examples:
1492 // short __attribute__((common)) var; -> declspec
1493 // short var __attribute__((common)); -> declarator
1494 // short x, __attribute__((common)) var; -> declarator
John McCall53fa7142010-12-24 02:08:15 +00001495 MaybeParseGNUAttributes(D);
John McCalld5a36322009-11-03 19:26:08 +00001496
1497 ParseDeclarator(D);
Fariborz Jahanian372030b2012-01-13 00:14:12 +00001498 if (!D.isInvalidType()) {
1499 Decl *ThisDecl = ParseDeclarationAfterDeclarator(D);
1500 D.complete(ThisDecl);
1501 if (ThisDecl)
Chad Rosierc1183952012-06-26 22:30:43 +00001502 DeclsInGroup.push_back(ThisDecl);
Fariborz Jahanian372030b2012-01-13 00:14:12 +00001503 }
John McCalld5a36322009-11-03 19:26:08 +00001504 }
1505
1506 if (DeclEnd)
1507 *DeclEnd = Tok.getLocation();
1508
Richard Smith09f76ee2011-10-19 21:33:05 +00001509 if (ExpectSemi &&
Chris Lattner02f1b612012-04-28 16:12:17 +00001510 ExpectAndConsumeSemi(Context == Declarator::FileContext
1511 ? diag::err_invalid_token_after_toplevel_declarator
1512 : diag::err_expected_semi_declaration)) {
Chris Lattner13901342010-07-11 22:42:07 +00001513 // Okay, there was no semicolon and one was expected. If we see a
1514 // declaration specifier, just assume it was missing and continue parsing.
1515 // Otherwise things are very confused and we skip to recover.
1516 if (!isDeclarationSpecifier()) {
1517 SkipUntil(tok::r_brace, true, true);
1518 if (Tok.is(tok::semi))
1519 ConsumeToken();
1520 }
John McCalld5a36322009-11-03 19:26:08 +00001521 }
1522
Douglas Gregor0be31a22010-07-02 17:43:08 +00001523 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS,
John McCalld5a36322009-11-03 19:26:08 +00001524 DeclsInGroup.data(),
1525 DeclsInGroup.size());
Chris Lattner53361ac2006-08-10 05:19:57 +00001526}
1527
Richard Smith02e85f32011-04-14 22:09:26 +00001528/// Parse an optional simple-asm-expr and attributes, and attach them to a
1529/// declarator. Returns true on an error.
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001530bool Parser::ParseAsmAttributesAfterDeclarator(Declarator &D) {
Richard Smith02e85f32011-04-14 22:09:26 +00001531 // If a simple-asm-expr is present, parse it.
1532 if (Tok.is(tok::kw_asm)) {
1533 SourceLocation Loc;
1534 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
1535 if (AsmLabel.isInvalid()) {
1536 SkipUntil(tok::semi, true, true);
1537 return true;
1538 }
1539
1540 D.setAsmLabel(AsmLabel.release());
1541 D.SetRangeEnd(Loc);
1542 }
1543
1544 MaybeParseGNUAttributes(D);
1545 return false;
1546}
1547
Douglas Gregor23996282009-05-12 21:31:51 +00001548/// \brief Parse 'declaration' after parsing 'declaration-specifiers
1549/// declarator'. This method parses the remainder of the declaration
1550/// (including any attributes or initializer, among other things) and
1551/// finalizes the declaration.
Chris Lattnerf0f3baa2006-08-14 00:15:20 +00001552///
Chris Lattnerf0f3baa2006-08-14 00:15:20 +00001553/// init-declarator: [C99 6.7]
1554/// declarator
1555/// declarator '=' initializer
Chris Lattner6d7e6342006-08-15 03:41:14 +00001556/// [GNU] declarator simple-asm-expr[opt] attributes[opt]
1557/// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00001558/// [C++] declarator initializer[opt]
1559///
1560/// [C++] initializer:
1561/// [C++] '=' initializer-clause
1562/// [C++] '(' expression-list ')'
Sebastian Redlf769df52009-03-24 22:27:57 +00001563/// [C++0x] '=' 'default' [TODO]
1564/// [C++0x] '=' 'delete'
Sebastian Redl3da34892011-06-05 12:23:16 +00001565/// [C++0x] braced-init-list
Sebastian Redlf769df52009-03-24 22:27:57 +00001566///
1567/// According to the standard grammar, =default and =delete are function
1568/// definitions, but that definitely doesn't fit with the parser here.
Chris Lattnerf0f3baa2006-08-14 00:15:20 +00001569///
John McCall48871652010-08-21 09:40:31 +00001570Decl *Parser::ParseDeclarationAfterDeclarator(Declarator &D,
Douglas Gregorb52fabb2009-06-23 23:11:28 +00001571 const ParsedTemplateInfo &TemplateInfo) {
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001572 if (ParseAsmAttributesAfterDeclarator(D))
Richard Smith02e85f32011-04-14 22:09:26 +00001573 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00001574
Richard Smith02e85f32011-04-14 22:09:26 +00001575 return ParseDeclarationAfterDeclaratorAndAttributes(D, TemplateInfo);
1576}
Mike Stump11289f42009-09-09 15:08:12 +00001577
Richard Smith02e85f32011-04-14 22:09:26 +00001578Decl *Parser::ParseDeclarationAfterDeclaratorAndAttributes(Declarator &D,
1579 const ParsedTemplateInfo &TemplateInfo) {
Douglas Gregor23996282009-05-12 21:31:51 +00001580 // Inform the current actions module that we just parsed this declarator.
John McCall48871652010-08-21 09:40:31 +00001581 Decl *ThisDecl = 0;
Douglas Gregor450f00842009-09-25 18:43:00 +00001582 switch (TemplateInfo.Kind) {
1583 case ParsedTemplateInfo::NonTemplate:
Douglas Gregor0be31a22010-07-02 17:43:08 +00001584 ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
Douglas Gregor450f00842009-09-25 18:43:00 +00001585 break;
Chad Rosierc1183952012-06-26 22:30:43 +00001586
Douglas Gregor450f00842009-09-25 18:43:00 +00001587 case ParsedTemplateInfo::Template:
1588 case ParsedTemplateInfo::ExplicitSpecialization:
Douglas Gregor0be31a22010-07-02 17:43:08 +00001589 ThisDecl = Actions.ActOnTemplateDeclarator(getCurScope(),
John McCallfaf5fb42010-08-26 23:41:50 +00001590 MultiTemplateParamsArg(Actions,
Douglas Gregorb52fabb2009-06-23 23:11:28 +00001591 TemplateInfo.TemplateParams->data(),
1592 TemplateInfo.TemplateParams->size()),
Douglas Gregor450f00842009-09-25 18:43:00 +00001593 D);
1594 break;
Chad Rosierc1183952012-06-26 22:30:43 +00001595
Douglas Gregor450f00842009-09-25 18:43:00 +00001596 case ParsedTemplateInfo::ExplicitInstantiation: {
Chad Rosierc1183952012-06-26 22:30:43 +00001597 DeclResult ThisRes
Douglas Gregor0be31a22010-07-02 17:43:08 +00001598 = Actions.ActOnExplicitInstantiation(getCurScope(),
Douglas Gregor450f00842009-09-25 18:43:00 +00001599 TemplateInfo.ExternLoc,
1600 TemplateInfo.TemplateLoc,
1601 D);
1602 if (ThisRes.isInvalid()) {
1603 SkipUntil(tok::semi, true, true);
John McCall48871652010-08-21 09:40:31 +00001604 return 0;
Douglas Gregor450f00842009-09-25 18:43:00 +00001605 }
Chad Rosierc1183952012-06-26 22:30:43 +00001606
Douglas Gregor450f00842009-09-25 18:43:00 +00001607 ThisDecl = ThisRes.get();
1608 break;
1609 }
1610 }
Mike Stump11289f42009-09-09 15:08:12 +00001611
Richard Smith30482bc2011-02-20 03:19:35 +00001612 bool TypeContainsAuto =
1613 D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto;
1614
Douglas Gregor23996282009-05-12 21:31:51 +00001615 // Parse declarator '=' initializer.
Richard Trieuc64d3232012-01-18 22:54:52 +00001616 // If a '==' or '+=' is found, suggest a fixit to '='.
Richard Trieu4972a6d2012-01-19 22:01:51 +00001617 if (isTokenEqualOrEqualTypo()) {
Douglas Gregor23996282009-05-12 21:31:51 +00001618 ConsumeToken();
Anders Carlsson991285e2010-09-24 21:25:25 +00001619 if (Tok.is(tok::kw_delete)) {
Alexis Hunt5a7fa252011-05-12 06:15:49 +00001620 if (D.isFunctionDeclarator())
1621 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
1622 << 1 /* delete */;
1623 else
1624 Diag(ConsumeToken(), diag::err_deleted_non_function);
Alexis Hunt5dafebc2011-05-06 01:42:00 +00001625 } else if (Tok.is(tok::kw_default)) {
Alexis Hunt5a7fa252011-05-12 06:15:49 +00001626 if (D.isFunctionDeclarator())
Sebastian Redl46afb552012-02-11 23:51:21 +00001627 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
1628 << 0 /* default */;
Alexis Hunt5a7fa252011-05-12 06:15:49 +00001629 else
1630 Diag(ConsumeToken(), diag::err_default_special_members);
Douglas Gregor23996282009-05-12 21:31:51 +00001631 } else {
David Blaikiebbafb8a2012-03-11 07:00:24 +00001632 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
John McCall1f4ee7b2009-12-19 09:28:58 +00001633 EnterScope(0);
Douglas Gregor0be31a22010-07-02 17:43:08 +00001634 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
John McCall1f4ee7b2009-12-19 09:28:58 +00001635 }
Argyrios Kyrtzidis3df19782009-06-17 22:50:06 +00001636
Douglas Gregor7aa6b222010-05-30 01:49:25 +00001637 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001638 Actions.CodeCompleteInitializer(getCurScope(), ThisDecl);
Peter Collingbourne6b4fdc22012-07-27 12:56:09 +00001639 Actions.FinalizeDeclaration(ThisDecl);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001640 cutOffParsing();
1641 return 0;
Douglas Gregor7aa6b222010-05-30 01:49:25 +00001642 }
Chad Rosierc1183952012-06-26 22:30:43 +00001643
John McCalldadc5752010-08-24 06:29:42 +00001644 ExprResult Init(ParseInitializer());
Argyrios Kyrtzidis3df19782009-06-17 22:50:06 +00001645
David Blaikiebbafb8a2012-03-11 07:00:24 +00001646 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001647 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
John McCall1f4ee7b2009-12-19 09:28:58 +00001648 ExitScope();
1649 }
Argyrios Kyrtzidis3df19782009-06-17 22:50:06 +00001650
Douglas Gregor23996282009-05-12 21:31:51 +00001651 if (Init.isInvalid()) {
Douglas Gregor604c3022010-03-01 18:27:54 +00001652 SkipUntil(tok::comma, true, true);
1653 Actions.ActOnInitializerError(ThisDecl);
1654 } else
Richard Smith30482bc2011-02-20 03:19:35 +00001655 Actions.AddInitializerToDecl(ThisDecl, Init.take(),
1656 /*DirectInit=*/false, TypeContainsAuto);
Douglas Gregor23996282009-05-12 21:31:51 +00001657 }
1658 } else if (Tok.is(tok::l_paren)) {
1659 // Parse C++ direct initializer: '(' expression-list ')'
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001660 BalancedDelimiterTracker T(*this, tok::l_paren);
1661 T.consumeOpen();
1662
Douglas Gregor23996282009-05-12 21:31:51 +00001663 ExprVector Exprs(Actions);
1664 CommaLocsTy CommaLocs;
1665
David Blaikiebbafb8a2012-03-11 07:00:24 +00001666 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor613bf102009-12-22 17:47:17 +00001667 EnterScope(0);
Douglas Gregor0be31a22010-07-02 17:43:08 +00001668 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregor613bf102009-12-22 17:47:17 +00001669 }
1670
Douglas Gregor23996282009-05-12 21:31:51 +00001671 if (ParseExpressionList(Exprs, CommaLocs)) {
1672 SkipUntil(tok::r_paren);
Douglas Gregor613bf102009-12-22 17:47:17 +00001673
David Blaikiebbafb8a2012-03-11 07:00:24 +00001674 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001675 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregor613bf102009-12-22 17:47:17 +00001676 ExitScope();
1677 }
Douglas Gregor23996282009-05-12 21:31:51 +00001678 } else {
1679 // Match the ')'.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001680 T.consumeClose();
Douglas Gregor23996282009-05-12 21:31:51 +00001681
1682 assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() &&
1683 "Unexpected number of commas!");
Douglas Gregor613bf102009-12-22 17:47:17 +00001684
David Blaikiebbafb8a2012-03-11 07:00:24 +00001685 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001686 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregor613bf102009-12-22 17:47:17 +00001687 ExitScope();
1688 }
1689
Sebastian Redla9351792012-02-11 23:51:47 +00001690 ExprResult Initializer = Actions.ActOnParenListExpr(T.getOpenLocation(),
1691 T.getCloseLocation(),
1692 move_arg(Exprs));
1693 Actions.AddInitializerToDecl(ThisDecl, Initializer.take(),
1694 /*DirectInit=*/true, TypeContainsAuto);
Douglas Gregor23996282009-05-12 21:31:51 +00001695 }
Fariborz Jahanian8a369a82012-07-03 22:54:28 +00001696 } else if (getLangOpts().CPlusPlus0x && Tok.is(tok::l_brace) &&
Fariborz Jahanian8be1ecd2012-07-03 23:22:13 +00001697 (!CurParsedObjCImpl || !D.isFunctionDeclarator())) {
Sebastian Redl3da34892011-06-05 12:23:16 +00001698 // Parse C++0x braced-init-list.
Richard Smith5d164bc2011-10-15 05:09:34 +00001699 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
1700
Sebastian Redl3da34892011-06-05 12:23:16 +00001701 if (D.getCXXScopeSpec().isSet()) {
1702 EnterScope(0);
1703 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
1704 }
1705
1706 ExprResult Init(ParseBraceInitializer());
1707
1708 if (D.getCXXScopeSpec().isSet()) {
1709 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
1710 ExitScope();
1711 }
1712
1713 if (Init.isInvalid()) {
1714 Actions.ActOnInitializerError(ThisDecl);
1715 } else
1716 Actions.AddInitializerToDecl(ThisDecl, Init.take(),
1717 /*DirectInit=*/true, TypeContainsAuto);
1718
Douglas Gregor23996282009-05-12 21:31:51 +00001719 } else {
Richard Smith30482bc2011-02-20 03:19:35 +00001720 Actions.ActOnUninitializedDecl(ThisDecl, TypeContainsAuto);
Douglas Gregor23996282009-05-12 21:31:51 +00001721 }
1722
Richard Smithb2bc2e62011-02-21 20:05:19 +00001723 Actions.FinalizeDeclaration(ThisDecl);
1724
Douglas Gregor23996282009-05-12 21:31:51 +00001725 return ThisDecl;
1726}
1727
Chris Lattner1890ac82006-08-13 01:16:23 +00001728/// ParseSpecifierQualifierList
1729/// specifier-qualifier-list:
1730/// type-specifier specifier-qualifier-list[opt]
1731/// type-qualifier specifier-qualifier-list[opt]
Chris Lattnere37e2332006-08-15 04:50:22 +00001732/// [GNU] attributes specifier-qualifier-list[opt]
Chris Lattner1890ac82006-08-13 01:16:23 +00001733///
Richard Smithc5b05522012-03-12 07:56:15 +00001734void Parser::ParseSpecifierQualifierList(DeclSpec &DS, AccessSpecifier AS,
1735 DeclSpecContext DSC) {
Chris Lattner1890ac82006-08-13 01:16:23 +00001736 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
1737 /// parse declaration-specifiers and complain about extra stuff.
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00001738 /// TODO: diagnose attribute-specifiers and alignment-specifiers.
Richard Smithc5b05522012-03-12 07:56:15 +00001739 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS, DSC);
Mike Stump11289f42009-09-09 15:08:12 +00001740
Chris Lattner1890ac82006-08-13 01:16:23 +00001741 // Validate declspec for type-name.
1742 unsigned Specs = DS.getParsedSpecifiers();
Richard Smith2f07ad52012-05-09 20:55:26 +00001743 if ((DSC == DSC_type_specifier || DSC == DSC_trailing) &&
1744 !DS.hasTypeSpecifier()) {
Richard Smithc5b05522012-03-12 07:56:15 +00001745 Diag(Tok, diag::err_expected_type);
1746 DS.SetTypeSpecError();
1747 } else if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers() &&
1748 !DS.hasAttributes()) {
Chris Lattner1890ac82006-08-13 01:16:23 +00001749 Diag(Tok, diag::err_typename_requires_specqual);
Richard Smithc5b05522012-03-12 07:56:15 +00001750 if (!DS.hasTypeSpecifier())
1751 DS.SetTypeSpecError();
1752 }
Mike Stump11289f42009-09-09 15:08:12 +00001753
Chris Lattner1b22eed2006-11-28 05:12:07 +00001754 // Issue diagnostic and remove storage class if present.
Chris Lattner1890ac82006-08-13 01:16:23 +00001755 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
Chris Lattner1b22eed2006-11-28 05:12:07 +00001756 if (DS.getStorageClassSpecLoc().isValid())
1757 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
1758 else
1759 Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass);
Chris Lattnera925dc62006-11-28 04:33:46 +00001760 DS.ClearStorageClassSpecs();
Chris Lattner1890ac82006-08-13 01:16:23 +00001761 }
Mike Stump11289f42009-09-09 15:08:12 +00001762
Chris Lattner1b22eed2006-11-28 05:12:07 +00001763 // Issue diagnostic and remove function specfier if present.
Chris Lattner1890ac82006-08-13 01:16:23 +00001764 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Douglas Gregor61956c42008-10-31 09:07:45 +00001765 if (DS.isInlineSpecified())
1766 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
1767 if (DS.isVirtualSpecified())
1768 Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec);
1769 if (DS.isExplicitSpecified())
1770 Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec);
Chris Lattnera925dc62006-11-28 04:33:46 +00001771 DS.ClearFunctionSpecs();
Chris Lattner1890ac82006-08-13 01:16:23 +00001772 }
Richard Smithc5b05522012-03-12 07:56:15 +00001773
1774 // Issue diagnostic and remove constexpr specfier if present.
1775 if (DS.isConstexprSpecified()) {
1776 Diag(DS.getConstexprSpecLoc(), diag::err_typename_invalid_constexpr);
1777 DS.ClearConstexprSpec();
1778 }
Chris Lattner1890ac82006-08-13 01:16:23 +00001779}
Chris Lattner53361ac2006-08-10 05:19:57 +00001780
Chris Lattner6cc055a2009-04-12 20:42:31 +00001781/// isValidAfterIdentifierInDeclaratorAfterDeclSpec - Return true if the
1782/// specified token is valid after the identifier in a declarator which
1783/// immediately follows the declspec. For example, these things are valid:
1784///
1785/// int x [ 4]; // direct-declarator
1786/// int x ( int y); // direct-declarator
1787/// int(int x ) // direct-declarator
1788/// int x ; // simple-declaration
1789/// int x = 17; // init-declarator-list
1790/// int x , y; // init-declarator-list
1791/// int x __asm__ ("foo"); // init-declarator-list
Chris Lattnera723ba92009-04-14 21:16:09 +00001792/// int x : 4; // struct-declarator
Chris Lattner2b988c12009-04-12 22:29:43 +00001793/// int x { 5}; // C++'0x unified initializers
Chris Lattner6cc055a2009-04-12 20:42:31 +00001794///
1795/// This is not, because 'x' does not immediately follow the declspec (though
1796/// ')' happens to be valid anyway).
1797/// int (x)
1798///
1799static bool isValidAfterIdentifierInDeclarator(const Token &T) {
1800 return T.is(tok::l_square) || T.is(tok::l_paren) || T.is(tok::r_paren) ||
1801 T.is(tok::semi) || T.is(tok::comma) || T.is(tok::equal) ||
Chris Lattnera723ba92009-04-14 21:16:09 +00001802 T.is(tok::kw_asm) || T.is(tok::l_brace) || T.is(tok::colon);
Chris Lattner6cc055a2009-04-12 20:42:31 +00001803}
1804
Chris Lattner20a0c612009-04-14 21:34:55 +00001805
1806/// ParseImplicitInt - This method is called when we have an non-typename
1807/// identifier in a declspec (which normally terminates the decl spec) when
1808/// the declspec has no type specifier. In this case, the declspec is either
1809/// malformed or is "implicit int" (in K&R and C89).
1810///
1811/// This method handles diagnosing this prettily and returns false if the
1812/// declspec is done being processed. If it recovers and thinks there may be
1813/// other pieces of declspec after it, it returns true.
1814///
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001815bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS,
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001816 const ParsedTemplateInfo &TemplateInfo,
Richard Smithc5b05522012-03-12 07:56:15 +00001817 AccessSpecifier AS, DeclSpecContext DSC) {
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001818 assert(Tok.is(tok::identifier) && "should have identifier");
Mike Stump11289f42009-09-09 15:08:12 +00001819
Chris Lattner20a0c612009-04-14 21:34:55 +00001820 SourceLocation Loc = Tok.getLocation();
1821 // If we see an identifier that is not a type name, we normally would
1822 // parse it as the identifer being declared. However, when a typename
1823 // is typo'd or the definition is not included, this will incorrectly
1824 // parse the typename as the identifier name and fall over misparsing
1825 // later parts of the diagnostic.
1826 //
1827 // As such, we try to do some look-ahead in cases where this would
1828 // otherwise be an "implicit-int" case to see if this is invalid. For
1829 // example: "static foo_t x = 4;" In this case, if we parsed foo_t as
1830 // an identifier with implicit int, we'd get a parse error because the
1831 // next token is obviously invalid for a type. Parse these as a case
1832 // with an invalid type specifier.
1833 assert(!DS.hasTypeSpecifier() && "Type specifier checked above");
Mike Stump11289f42009-09-09 15:08:12 +00001834
Chris Lattner20a0c612009-04-14 21:34:55 +00001835 // Since we know that this either implicit int (which is rare) or an
Richard Smitha952ebb2012-05-15 21:01:51 +00001836 // error, do lookahead to try to do better recovery. This never applies
1837 // within a type specifier. Outside of C++, we allow this even if the
1838 // language doesn't "officially" support implicit int -- we support
1839 // implicit int as an extension in C99 and C11. Allegedly, MS also
1840 // supports implicit int in C++ mode.
Richard Smith2f07ad52012-05-09 20:55:26 +00001841 if (DSC != DSC_type_specifier && DSC != DSC_trailing &&
Richard Smitha952ebb2012-05-15 21:01:51 +00001842 (!getLangOpts().CPlusPlus || getLangOpts().MicrosoftExt) &&
Richard Smithc5b05522012-03-12 07:56:15 +00001843 isValidAfterIdentifierInDeclarator(NextToken())) {
Chris Lattner20a0c612009-04-14 21:34:55 +00001844 // If this token is valid for implicit int, e.g. "static x = 4", then
1845 // we just avoid eating the identifier, so it will be parsed as the
1846 // identifier in the declarator.
1847 return false;
1848 }
Mike Stump11289f42009-09-09 15:08:12 +00001849
Richard Smitha952ebb2012-05-15 21:01:51 +00001850 if (getLangOpts().CPlusPlus &&
1851 DS.getStorageClassSpec() == DeclSpec::SCS_auto) {
1852 // Don't require a type specifier if we have the 'auto' storage class
1853 // specifier in C++98 -- we'll promote it to a type specifier.
1854 return false;
1855 }
1856
Chris Lattner20a0c612009-04-14 21:34:55 +00001857 // Otherwise, if we don't consume this token, we are going to emit an
1858 // error anyway. Try to recover from various common problems. Check
1859 // to see if this was a reference to a tag name without a tag specified.
1860 // This is a common problem in C (saying 'foo' instead of 'struct foo').
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001861 //
1862 // C++ doesn't need this, and isTagName doesn't take SS.
1863 if (SS == 0) {
Argyrios Kyrtzidis1f329402011-04-21 17:29:47 +00001864 const char *TagName = 0, *FixitTagName = 0;
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001865 tok::TokenKind TagKind = tok::unknown;
Mike Stump11289f42009-09-09 15:08:12 +00001866
Douglas Gregor0be31a22010-07-02 17:43:08 +00001867 switch (Actions.isTagName(*Tok.getIdentifierInfo(), getCurScope())) {
Chris Lattner20a0c612009-04-14 21:34:55 +00001868 default: break;
Argyrios Kyrtzidis1f329402011-04-21 17:29:47 +00001869 case DeclSpec::TST_enum:
1870 TagName="enum" ; FixitTagName = "enum " ; TagKind=tok::kw_enum ;break;
1871 case DeclSpec::TST_union:
1872 TagName="union" ; FixitTagName = "union " ;TagKind=tok::kw_union ;break;
1873 case DeclSpec::TST_struct:
1874 TagName="struct"; FixitTagName = "struct ";TagKind=tok::kw_struct;break;
1875 case DeclSpec::TST_class:
1876 TagName="class" ; FixitTagName = "class " ;TagKind=tok::kw_class ;break;
Chris Lattner20a0c612009-04-14 21:34:55 +00001877 }
Mike Stump11289f42009-09-09 15:08:12 +00001878
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001879 if (TagName) {
Kaelyn Uhrain031643e2012-04-26 23:36:17 +00001880 IdentifierInfo *TokenName = Tok.getIdentifierInfo();
1881 LookupResult R(Actions, TokenName, SourceLocation(),
1882 Sema::LookupOrdinaryName);
1883
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001884 Diag(Loc, diag::err_use_of_tag_name_without_tag)
Kaelyn Uhrain031643e2012-04-26 23:36:17 +00001885 << TokenName << TagName << getLangOpts().CPlusPlus
1886 << FixItHint::CreateInsertion(Tok.getLocation(), FixitTagName);
1887
1888 if (Actions.LookupParsedName(R, getCurScope(), SS)) {
1889 for (LookupResult::iterator I = R.begin(), IEnd = R.end();
1890 I != IEnd; ++I)
Kaelyn Uhrain3fe3f852012-04-27 18:26:49 +00001891 Diag((*I)->getLocation(), diag::note_decl_hiding_tag_type)
Kaelyn Uhrain031643e2012-04-26 23:36:17 +00001892 << TokenName << TagName;
1893 }
Mike Stump11289f42009-09-09 15:08:12 +00001894
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001895 // Parse this as a tag as if the missing tag were present.
1896 if (TagKind == tok::kw_enum)
Richard Smithc5b05522012-03-12 07:56:15 +00001897 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS, DSC_normal);
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001898 else
Richard Smithc5b05522012-03-12 07:56:15 +00001899 ParseClassSpecifier(TagKind, Loc, DS, TemplateInfo, AS,
1900 /*EnteringContext*/ false, DSC_normal);
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001901 return true;
1902 }
Chris Lattner20a0c612009-04-14 21:34:55 +00001903 }
Mike Stump11289f42009-09-09 15:08:12 +00001904
Richard Smithfe904f02012-05-15 21:29:55 +00001905 // Determine whether this identifier could plausibly be the name of something
Richard Smithedd124e2012-05-15 21:42:17 +00001906 // being declared (with a missing type).
Richard Smithfe904f02012-05-15 21:29:55 +00001907 if (DSC != DSC_type_specifier && DSC != DSC_trailing &&
1908 (!SS || DSC == DSC_top_level || DSC == DSC_class)) {
Richard Smitha952ebb2012-05-15 21:01:51 +00001909 // Look ahead to the next token to try to figure out what this declaration
1910 // was supposed to be.
1911 switch (NextToken().getKind()) {
1912 case tok::comma:
1913 case tok::equal:
1914 case tok::kw_asm:
1915 case tok::l_brace:
1916 case tok::l_square:
1917 case tok::semi:
1918 // This looks like a variable declaration. The type is probably missing.
1919 // We're done parsing decl-specifiers.
1920 return false;
1921
1922 case tok::l_paren: {
1923 // static x(4); // 'x' is not a type
1924 // x(int n); // 'x' is not a type
1925 // x (*p)[]; // 'x' is a type
1926 //
1927 // Since we're in an error case (or the rare 'implicit int in C++' MS
1928 // extension), we can afford to perform a tentative parse to determine
1929 // which case we're in.
1930 TentativeParsingAction PA(*this);
1931 ConsumeToken();
1932 TPResult TPR = TryParseDeclarator(/*mayBeAbstract*/false);
1933 PA.Revert();
1934 if (TPR == TPResult::False())
1935 return false;
1936 // The identifier is followed by a parenthesized declarator.
1937 // It's supposed to be a type.
1938 break;
1939 }
1940
1941 default:
1942 // This is probably supposed to be a type. This includes cases like:
1943 // int f(itn);
1944 // struct S { unsinged : 4; };
1945 break;
1946 }
1947 }
1948
Chad Rosierc1183952012-06-26 22:30:43 +00001949 // This is almost certainly an invalid type name. Let the action emit a
Douglas Gregor15e56022009-10-13 23:27:22 +00001950 // diagnostic and attempt to recover.
John McCallba7bf592010-08-24 05:47:05 +00001951 ParsedType T;
Kaelyn Uhrainb5b17fe2012-06-15 23:45:58 +00001952 IdentifierInfo *II = Tok.getIdentifierInfo();
1953 if (Actions.DiagnoseUnknownTypeName(II, Loc, getCurScope(), SS, T)) {
Douglas Gregor15e56022009-10-13 23:27:22 +00001954 // The action emitted a diagnostic, so we don't have to.
1955 if (T) {
1956 // The action has suggested that the type T could be used. Set that as
1957 // the type in the declaration specifiers, consume the would-be type
1958 // name token, and we're done.
1959 const char *PrevSpec;
1960 unsigned DiagID;
John McCallba7bf592010-08-24 05:47:05 +00001961 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID, T);
Douglas Gregor15e56022009-10-13 23:27:22 +00001962 DS.SetRangeEnd(Tok.getLocation());
1963 ConsumeToken();
Kaelyn Uhrainb5b17fe2012-06-15 23:45:58 +00001964 // There may be other declaration specifiers after this.
1965 return true;
1966 } else if (II != Tok.getIdentifierInfo()) {
1967 // If no type was suggested, the correction is to a keyword
1968 Tok.setKind(II->getTokenID());
Douglas Gregor15e56022009-10-13 23:27:22 +00001969 // There may be other declaration specifiers after this.
1970 return true;
1971 }
Chad Rosierc1183952012-06-26 22:30:43 +00001972
Douglas Gregor15e56022009-10-13 23:27:22 +00001973 // Fall through; the action had no suggestion for us.
1974 } else {
1975 // The action did not emit a diagnostic, so emit one now.
1976 SourceRange R;
1977 if (SS) R = SS->getRange();
1978 Diag(Loc, diag::err_unknown_typename) << Tok.getIdentifierInfo() << R;
1979 }
Mike Stump11289f42009-09-09 15:08:12 +00001980
Douglas Gregor15e56022009-10-13 23:27:22 +00001981 // Mark this as an error.
Richard Smithc5b05522012-03-12 07:56:15 +00001982 DS.SetTypeSpecError();
Chris Lattner20a0c612009-04-14 21:34:55 +00001983 DS.SetRangeEnd(Tok.getLocation());
1984 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001985
Chris Lattner20a0c612009-04-14 21:34:55 +00001986 // TODO: Could inject an invalid typedef decl in an enclosing scope to
1987 // avoid rippling error messages on subsequent uses of the same type,
1988 // could be useful if #include was forgotten.
1989 return false;
1990}
1991
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001992/// \brief Determine the declaration specifier context from the declarator
1993/// context.
1994///
1995/// \param Context the declarator context, which is one of the
1996/// Declarator::TheContext enumerator values.
Chad Rosierc1183952012-06-26 22:30:43 +00001997Parser::DeclSpecContext
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001998Parser::getDeclSpecContextFromDeclaratorContext(unsigned Context) {
1999 if (Context == Declarator::MemberContext)
2000 return DSC_class;
2001 if (Context == Declarator::FileContext)
2002 return DSC_top_level;
Richard Smith62dad822012-03-15 01:02:11 +00002003 if (Context == Declarator::TrailingReturnContext)
2004 return DSC_trailing;
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002005 return DSC_normal;
2006}
2007
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002008/// ParseAlignArgument - Parse the argument to an alignment-specifier.
2009///
2010/// FIXME: Simply returns an alignof() expression if the argument is a
2011/// type. Ideally, the type should be propagated directly into Sema.
2012///
Benjamin Kramere56f3932011-12-23 17:00:35 +00002013/// [C11] type-id
2014/// [C11] constant-expression
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002015/// [C++0x] type-id ...[opt]
2016/// [C++0x] assignment-expression ...[opt]
2017ExprResult Parser::ParseAlignArgument(SourceLocation Start,
2018 SourceLocation &EllipsisLoc) {
2019 ExprResult ER;
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002020 if (isTypeIdInParens()) {
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002021 SourceLocation TypeLoc = Tok.getLocation();
2022 ParsedType Ty = ParseTypeName().get();
2023 SourceRange TypeRange(Start, Tok.getLocation());
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002024 ER = Actions.ActOnUnaryExprOrTypeTraitExpr(TypeLoc, UETT_AlignOf, true,
2025 Ty.getAsOpaquePtr(), TypeRange);
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002026 } else
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002027 ER = ParseConstantExpression();
2028
David Blaikiebbafb8a2012-03-11 07:00:24 +00002029 if (getLangOpts().CPlusPlus0x && Tok.is(tok::ellipsis))
Peter Collingbourneccbcce02011-10-24 17:56:00 +00002030 EllipsisLoc = ConsumeToken();
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002031
2032 return ER;
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002033}
2034
2035/// ParseAlignmentSpecifier - Parse an alignment-specifier, and add the
2036/// attribute to Attrs.
2037///
2038/// alignment-specifier:
Benjamin Kramere56f3932011-12-23 17:00:35 +00002039/// [C11] '_Alignas' '(' type-id ')'
2040/// [C11] '_Alignas' '(' constant-expression ')'
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002041/// [C++0x] 'alignas' '(' type-id ...[opt] ')'
2042/// [C++0x] 'alignas' '(' assignment-expression ...[opt] ')'
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002043void Parser::ParseAlignmentSpecifier(ParsedAttributes &Attrs,
2044 SourceLocation *endLoc) {
2045 assert((Tok.is(tok::kw_alignas) || Tok.is(tok::kw__Alignas)) &&
2046 "Not an alignment-specifier!");
2047
2048 SourceLocation KWLoc = Tok.getLocation();
2049 ConsumeToken();
2050
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002051 BalancedDelimiterTracker T(*this, tok::l_paren);
2052 if (T.expectAndConsume(diag::err_expected_lparen))
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002053 return;
2054
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002055 SourceLocation EllipsisLoc;
2056 ExprResult ArgExpr = ParseAlignArgument(T.getOpenLocation(), EllipsisLoc);
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002057 if (ArgExpr.isInvalid()) {
2058 SkipUntil(tok::r_paren);
2059 return;
2060 }
2061
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002062 T.consumeClose();
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002063 if (endLoc)
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002064 *endLoc = T.getCloseLocation();
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002065
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002066 // FIXME: Handle pack-expansions here.
2067 if (EllipsisLoc.isValid()) {
2068 Diag(EllipsisLoc, diag::err_alignas_pack_exp_unsupported);
2069 return;
2070 }
2071
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002072 ExprVector ArgExprs(Actions);
2073 ArgExprs.push_back(ArgExpr.release());
Alexis Hunt3bc72c12012-06-19 23:57:03 +00002074 // FIXME: This should not be GNU, but we since the attribute used is
2075 // based on the spelling, and there is no true spelling for
2076 // C++11 attributes, this isn't accepted.
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002077 Attrs.addNew(PP.getIdentifierInfo("aligned"), KWLoc, 0, KWLoc,
Alexis Hunta0e54d42012-06-18 16:13:52 +00002078 0, T.getOpenLocation(), ArgExprs.take(), 1,
Alexis Hunt3bc72c12012-06-19 23:57:03 +00002079 AttributeList::AS_GNU);
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002080}
2081
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002082/// ParseDeclarationSpecifiers
2083/// declaration-specifiers: [C99 6.7]
Chris Lattner3b561a32006-08-13 00:12:11 +00002084/// storage-class-specifier declaration-specifiers[opt]
2085/// type-specifier declaration-specifiers[opt]
Chris Lattner3b561a32006-08-13 00:12:11 +00002086/// [C99] function-specifier declaration-specifiers[opt]
Benjamin Kramere56f3932011-12-23 17:00:35 +00002087/// [C11] alignment-specifier declaration-specifiers[opt]
Chris Lattnere37e2332006-08-15 04:50:22 +00002088/// [GNU] attributes declaration-specifiers[opt]
Douglas Gregor26701a42011-09-09 02:06:17 +00002089/// [Clang] '__module_private__' declaration-specifiers[opt]
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002090///
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002091/// storage-class-specifier: [C99 6.7.1]
Chris Lattnerda48a8e2006-08-04 05:25:55 +00002092/// 'typedef'
2093/// 'extern'
2094/// 'static'
2095/// 'auto'
2096/// 'register'
Sebastian Redlccdfaba2008-11-14 23:42:31 +00002097/// [C++] 'mutable'
Chris Lattnerda48a8e2006-08-04 05:25:55 +00002098/// [GNU] '__thread'
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002099/// function-specifier: [C99 6.7.4]
Chris Lattner3b561a32006-08-13 00:12:11 +00002100/// [C99] 'inline'
Douglas Gregor61956c42008-10-31 09:07:45 +00002101/// [C++] 'virtual'
2102/// [C++] 'explicit'
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00002103/// [OpenCL] '__kernel'
Anders Carlssoncd8db412009-05-06 04:46:28 +00002104/// 'friend': [C++ dcl.friend]
Sebastian Redl39c2a8b2009-11-05 15:47:02 +00002105/// 'constexpr': [C++0x dcl.constexpr]
Anders Carlssoncd8db412009-05-06 04:46:28 +00002106
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002107///
Douglas Gregorb9bd8a92008-12-24 02:52:09 +00002108void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
Douglas Gregor1b57ff32009-05-12 23:25:50 +00002109 const ParsedTemplateInfo &TemplateInfo,
John McCall07e91c02009-08-06 02:15:43 +00002110 AccessSpecifier AS,
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +00002111 DeclSpecContext DSContext,
2112 LateParsedAttrList *LateAttrs) {
Douglas Gregor0e7dde52011-04-24 05:37:28 +00002113 if (DS.getSourceRange().isInvalid()) {
2114 DS.SetRangeStart(Tok.getLocation());
2115 DS.SetRangeEnd(Tok.getLocation());
2116 }
Chad Rosierc1183952012-06-26 22:30:43 +00002117
Douglas Gregordf593fb2011-11-07 17:33:42 +00002118 bool EnteringContext = (DSContext == DSC_class || DSContext == DSC_top_level);
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00002119 bool AttrsLastTime = false;
2120 ParsedAttributesWithRange attrs(AttrFactory);
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002121 while (1) {
John McCall49bfce42009-08-03 20:12:06 +00002122 bool isInvalid = false;
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002123 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00002124 unsigned DiagID = 0;
2125
Chris Lattner4d8f8732006-11-28 05:05:08 +00002126 SourceLocation Loc = Tok.getLocation();
Douglas Gregor450c75a2008-11-07 15:42:26 +00002127
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002128 switch (Tok.getKind()) {
Mike Stump11289f42009-09-09 15:08:12 +00002129 default:
Chris Lattner0974b232008-07-26 00:20:22 +00002130 DoneWithDeclSpec:
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00002131 if (!AttrsLastTime)
2132 ProhibitAttributes(attrs);
2133 else
2134 DS.takeAttributesFrom(attrs);
Peter Collingbourne70188b32011-09-29 18:03:57 +00002135
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002136 // If this is not a declaration specifier token, we're done reading decl
2137 // specifiers. First verify that DeclSpec's are consistent.
Douglas Gregore3e01a22009-04-01 22:41:11 +00002138 DS.Finish(Diags, PP);
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002139 return;
Mike Stump11289f42009-09-09 15:08:12 +00002140
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00002141 case tok::l_square:
2142 case tok::kw_alignas:
2143 if (!isCXX11AttributeSpecifier())
2144 goto DoneWithDeclSpec;
2145
2146 ProhibitAttributes(attrs);
2147 // FIXME: It would be good to recover by accepting the attributes,
2148 // but attempting to do that now would cause serious
2149 // madness in terms of diagnostics.
2150 attrs.clear();
2151 attrs.Range = SourceRange();
2152
2153 ParseCXX11Attributes(attrs);
2154 AttrsLastTime = true;
Chad Rosierc1183952012-06-26 22:30:43 +00002155 continue;
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00002156
Douglas Gregorc49f5b22010-08-23 18:23:48 +00002157 case tok::code_completion: {
John McCallfaf5fb42010-08-26 23:41:50 +00002158 Sema::ParserCompletionContext CCC = Sema::PCC_Namespace;
Douglas Gregorc49f5b22010-08-23 18:23:48 +00002159 if (DS.hasTypeSpecifier()) {
2160 bool AllowNonIdentifiers
2161 = (getCurScope()->getFlags() & (Scope::ControlScope |
2162 Scope::BlockScope |
2163 Scope::TemplateParamScope |
2164 Scope::FunctionPrototypeScope |
2165 Scope::AtCatchScope)) == 0;
2166 bool AllowNestedNameSpecifiers
Chad Rosierc1183952012-06-26 22:30:43 +00002167 = DSContext == DSC_top_level ||
Douglas Gregorc49f5b22010-08-23 18:23:48 +00002168 (DSContext == DSC_class && DS.isFriendSpecified());
2169
Douglas Gregorbfcea8b2010-09-16 15:14:18 +00002170 Actions.CodeCompleteDeclSpec(getCurScope(), DS,
Chad Rosierc1183952012-06-26 22:30:43 +00002171 AllowNonIdentifiers,
Douglas Gregorbfcea8b2010-09-16 15:14:18 +00002172 AllowNestedNameSpecifiers);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002173 return cutOffParsing();
Chad Rosierc1183952012-06-26 22:30:43 +00002174 }
2175
Douglas Gregor80039242011-02-15 20:33:25 +00002176 if (getCurScope()->getFnParent() || getCurScope()->getBlockParent())
2177 CCC = Sema::PCC_LocalDeclarationSpecifiers;
2178 else if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate)
Chad Rosierc1183952012-06-26 22:30:43 +00002179 CCC = DSContext == DSC_class? Sema::PCC_MemberTemplate
John McCallfaf5fb42010-08-26 23:41:50 +00002180 : Sema::PCC_Template;
Douglas Gregorc49f5b22010-08-23 18:23:48 +00002181 else if (DSContext == DSC_class)
John McCallfaf5fb42010-08-26 23:41:50 +00002182 CCC = Sema::PCC_Class;
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002183 else if (CurParsedObjCImpl)
John McCallfaf5fb42010-08-26 23:41:50 +00002184 CCC = Sema::PCC_ObjCImplementation;
Chad Rosierc1183952012-06-26 22:30:43 +00002185
Douglas Gregorc49f5b22010-08-23 18:23:48 +00002186 Actions.CodeCompleteOrdinaryName(getCurScope(), CCC);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002187 return cutOffParsing();
Douglas Gregorc49f5b22010-08-23 18:23:48 +00002188 }
2189
Chris Lattnerbd31aa32009-01-05 00:07:25 +00002190 case tok::coloncolon: // ::foo::bar
John McCall1f476a12010-02-26 08:45:28 +00002191 // C++ scope specifier. Annotate and loop, or bail out on error.
2192 if (TryAnnotateCXXScopeToken(true)) {
2193 if (!DS.hasTypeSpecifier())
2194 DS.SetTypeSpecError();
2195 goto DoneWithDeclSpec;
2196 }
John McCall8bc2a702010-03-01 18:20:46 +00002197 if (Tok.is(tok::coloncolon)) // ::new or ::delete
2198 goto DoneWithDeclSpec;
John McCall1f476a12010-02-26 08:45:28 +00002199 continue;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002200
2201 case tok::annot_cxxscope: {
Richard Smith3092a3b2012-05-09 18:56:43 +00002202 if (DS.hasTypeSpecifier() || DS.isTypeAltiVecVector())
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002203 goto DoneWithDeclSpec;
2204
John McCall9dab4e62009-12-12 11:40:51 +00002205 CXXScopeSpec SS;
Douglas Gregor869ad452011-02-24 17:54:50 +00002206 Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(),
2207 Tok.getAnnotationRange(),
2208 SS);
John McCall9dab4e62009-12-12 11:40:51 +00002209
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002210 // We are looking for a qualified typename.
Douglas Gregor167fa622009-03-25 15:40:00 +00002211 Token Next = NextToken();
Mike Stump11289f42009-09-09 15:08:12 +00002212 if (Next.is(tok::annot_template_id) &&
Douglas Gregor167fa622009-03-25 15:40:00 +00002213 static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue())
Douglas Gregorb67535d2009-03-31 00:43:58 +00002214 ->Kind == TNK_Type_template) {
Douglas Gregor167fa622009-03-25 15:40:00 +00002215 // We have a qualified template-id, e.g., N::A<int>
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002216
2217 // C++ [class.qual]p2:
2218 // In a lookup in which the constructor is an acceptable lookup
2219 // result and the nested-name-specifier nominates a class C:
2220 //
2221 // - if the name specified after the
2222 // nested-name-specifier, when looked up in C, is the
2223 // injected-class-name of C (Clause 9), or
2224 //
2225 // - if the name specified after the nested-name-specifier
2226 // is the same as the identifier or the
2227 // simple-template-id's template-name in the last
2228 // component of the nested-name-specifier,
2229 //
2230 // the name is instead considered to name the constructor of
2231 // class C.
Chad Rosierc1183952012-06-26 22:30:43 +00002232 //
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002233 // Thus, if the template-name is actually the constructor
2234 // name, then the code is ill-formed; this interpretation is
Chad Rosierc1183952012-06-26 22:30:43 +00002235 // reinforced by the NAD status of core issue 635.
Argyrios Kyrtzidisc0c5dd22011-06-22 06:09:49 +00002236 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Next);
John McCall84821e72010-04-13 06:39:49 +00002237 if ((DSContext == DSC_top_level ||
2238 (DSContext == DSC_class && DS.isFriendSpecified())) &&
2239 TemplateId->Name &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00002240 Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) {
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002241 if (isConstructorDeclarator()) {
2242 // The user meant this to be an out-of-line constructor
2243 // definition, but template arguments are not allowed
2244 // there. Just allow this as a constructor; we'll
2245 // complain about it later.
2246 goto DoneWithDeclSpec;
2247 }
2248
2249 // The user meant this to name a type, but it actually names
2250 // a constructor with some extraneous template
2251 // arguments. Complain, then parse it as a type as the user
2252 // intended.
2253 Diag(TemplateId->TemplateNameLoc,
2254 diag::err_out_of_line_template_id_names_constructor)
2255 << TemplateId->Name;
2256 }
2257
John McCall9dab4e62009-12-12 11:40:51 +00002258 DS.getTypeSpecScope() = SS;
2259 ConsumeToken(); // The C++ scope.
Mike Stump11289f42009-09-09 15:08:12 +00002260 assert(Tok.is(tok::annot_template_id) &&
Douglas Gregor167fa622009-03-25 15:40:00 +00002261 "ParseOptionalCXXScopeSpecifier not working");
Douglas Gregore7c20652011-03-02 00:47:37 +00002262 AnnotateTemplateIdTokenAsType();
Douglas Gregor167fa622009-03-25 15:40:00 +00002263 continue;
2264 }
2265
Douglas Gregorc5790df2009-09-28 07:26:33 +00002266 if (Next.is(tok::annot_typename)) {
John McCall9dab4e62009-12-12 11:40:51 +00002267 DS.getTypeSpecScope() = SS;
2268 ConsumeToken(); // The C++ scope.
John McCallba7bf592010-08-24 05:47:05 +00002269 if (Tok.getAnnotationValue()) {
2270 ParsedType T = getTypeAnnotation(Tok);
Nico Weber77430342010-11-22 10:30:56 +00002271 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename,
Chad Rosierc1183952012-06-26 22:30:43 +00002272 Tok.getAnnotationEndLoc(),
John McCallba7bf592010-08-24 05:47:05 +00002273 PrevSpec, DiagID, T);
2274 }
Douglas Gregorc5790df2009-09-28 07:26:33 +00002275 else
2276 DS.SetTypeSpecError();
2277 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
2278 ConsumeToken(); // The typename
2279 }
2280
Douglas Gregor167fa622009-03-25 15:40:00 +00002281 if (Next.isNot(tok::identifier))
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002282 goto DoneWithDeclSpec;
2283
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002284 // If we're in a context where the identifier could be a class name,
2285 // check whether this is a constructor declaration.
John McCall84821e72010-04-13 06:39:49 +00002286 if ((DSContext == DSC_top_level ||
2287 (DSContext == DSC_class && DS.isFriendSpecified())) &&
Chad Rosierc1183952012-06-26 22:30:43 +00002288 Actions.isCurrentClassName(*Next.getIdentifierInfo(), getCurScope(),
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002289 &SS)) {
2290 if (isConstructorDeclarator())
2291 goto DoneWithDeclSpec;
2292
2293 // As noted in C++ [class.qual]p2 (cited above), when the name
2294 // of the class is qualified in a context where it could name
2295 // a constructor, its a constructor name. However, we've
2296 // looked at the declarator, and the user probably meant this
2297 // to be a type. Complain that it isn't supposed to be treated
2298 // as a type, then proceed to parse it as a type.
2299 Diag(Next.getLocation(), diag::err_out_of_line_type_names_constructor)
2300 << Next.getIdentifierInfo();
2301 }
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002302
John McCallba7bf592010-08-24 05:47:05 +00002303 ParsedType TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(),
2304 Next.getLocation(),
Douglas Gregor844cb502011-03-01 18:12:44 +00002305 getCurScope(), &SS,
2306 false, false, ParsedType(),
Abramo Bagnara4244b432012-01-27 08:46:19 +00002307 /*IsCtorOrDtorName=*/false,
Douglas Gregor844cb502011-03-01 18:12:44 +00002308 /*NonTrivialSourceInfo=*/true);
Douglas Gregor8bf42052009-02-09 18:46:07 +00002309
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002310 // If the referenced identifier is not a type, then this declspec is
2311 // erroneous: We already checked about that it has no type specifier, and
2312 // C++ doesn't have implicit int. Diagnose it as a typo w.r.t. to the
Mike Stump11289f42009-09-09 15:08:12 +00002313 // typename.
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002314 if (TypeRep == 0) {
2315 ConsumeToken(); // Eat the scope spec so the identifier is current.
Richard Smithc5b05522012-03-12 07:56:15 +00002316 if (ParseImplicitInt(DS, &SS, TemplateInfo, AS, DSContext)) continue;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002317 goto DoneWithDeclSpec;
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002318 }
Mike Stump11289f42009-09-09 15:08:12 +00002319
John McCall9dab4e62009-12-12 11:40:51 +00002320 DS.getTypeSpecScope() = SS;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002321 ConsumeToken(); // The C++ scope.
2322
Douglas Gregor9817f4a2009-02-09 15:09:02 +00002323 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00002324 DiagID, TypeRep);
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002325 if (isInvalid)
2326 break;
Mike Stump11289f42009-09-09 15:08:12 +00002327
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002328 DS.SetRangeEnd(Tok.getLocation());
2329 ConsumeToken(); // The typename.
2330
2331 continue;
2332 }
Mike Stump11289f42009-09-09 15:08:12 +00002333
Chris Lattnere387d9e2009-01-21 19:48:37 +00002334 case tok::annot_typename: {
John McCallba7bf592010-08-24 05:47:05 +00002335 if (Tok.getAnnotationValue()) {
2336 ParsedType T = getTypeAnnotation(Tok);
Nico Weber7f8bb362010-11-22 12:50:03 +00002337 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallba7bf592010-08-24 05:47:05 +00002338 DiagID, T);
2339 } else
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00002340 DS.SetTypeSpecError();
Chad Rosierc1183952012-06-26 22:30:43 +00002341
Chris Lattner005fc1b2010-04-05 18:18:31 +00002342 if (isInvalid)
2343 break;
2344
Chris Lattnere387d9e2009-01-21 19:48:37 +00002345 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
2346 ConsumeToken(); // The typename
Mike Stump11289f42009-09-09 15:08:12 +00002347
Chris Lattnere387d9e2009-01-21 19:48:37 +00002348 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
2349 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Chad Rosierc1183952012-06-26 22:30:43 +00002350 // Objective-C interface.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002351 if (Tok.is(tok::less) && getLangOpts().ObjC1)
Douglas Gregor06e41ae2010-10-21 23:17:00 +00002352 ParseObjCProtocolQualifiers(DS);
Chad Rosierc1183952012-06-26 22:30:43 +00002353
Chris Lattnere387d9e2009-01-21 19:48:37 +00002354 continue;
2355 }
Mike Stump11289f42009-09-09 15:08:12 +00002356
Douglas Gregor06873092011-04-28 15:48:45 +00002357 case tok::kw___is_signed:
2358 // GNU libstdc++ 4.4 uses __is_signed as an identifier, but Clang
2359 // typically treats it as a trait. If we see __is_signed as it appears
2360 // in libstdc++, e.g.,
2361 //
2362 // static const bool __is_signed;
2363 //
2364 // then treat __is_signed as an identifier rather than as a keyword.
2365 if (DS.getTypeSpecType() == TST_bool &&
2366 DS.getTypeQualifiers() == DeclSpec::TQ_const &&
2367 DS.getStorageClassSpec() == DeclSpec::SCS_static) {
2368 Tok.getIdentifierInfo()->RevertTokenIDToIdentifier();
2369 Tok.setKind(tok::identifier);
2370 }
2371
2372 // We're done with the declaration-specifiers.
2373 goto DoneWithDeclSpec;
Chad Rosierc1183952012-06-26 22:30:43 +00002374
Chris Lattner16fac4f2008-07-26 01:18:38 +00002375 // typedef-name
David Blaikie15a430a2011-12-04 05:04:18 +00002376 case tok::kw_decltype:
Chris Lattner16fac4f2008-07-26 01:18:38 +00002377 case tok::identifier: {
Chris Lattnerbd31aa32009-01-05 00:07:25 +00002378 // In C++, check to see if this is a scope specifier like foo::bar::, if
2379 // so handle it as such. This is important for ctor parsing.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002380 if (getLangOpts().CPlusPlus) {
John McCall1f476a12010-02-26 08:45:28 +00002381 if (TryAnnotateCXXScopeToken(true)) {
2382 if (!DS.hasTypeSpecifier())
2383 DS.SetTypeSpecError();
2384 goto DoneWithDeclSpec;
2385 }
2386 if (!Tok.is(tok::identifier))
2387 continue;
2388 }
Mike Stump11289f42009-09-09 15:08:12 +00002389
Chris Lattner16fac4f2008-07-26 01:18:38 +00002390 // This identifier can only be a typedef name if we haven't already seen
2391 // a type-specifier. Without this check we misparse:
2392 // typedef int X; struct Y { short X; }; as 'short int'.
2393 if (DS.hasTypeSpecifier())
2394 goto DoneWithDeclSpec;
Mike Stump11289f42009-09-09 15:08:12 +00002395
John Thompson22334602010-02-05 00:12:22 +00002396 // Check for need to substitute AltiVec keyword tokens.
2397 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
2398 break;
2399
Richard Smith3092a3b2012-05-09 18:56:43 +00002400 // [AltiVec] 2.2: [If the 'vector' specifier is used] The syntax does not
2401 // allow the use of a typedef name as a type specifier.
2402 if (DS.isTypeAltiVecVector())
2403 goto DoneWithDeclSpec;
2404
John McCallba7bf592010-08-24 05:47:05 +00002405 ParsedType TypeRep =
2406 Actions.getTypeName(*Tok.getIdentifierInfo(),
2407 Tok.getLocation(), getCurScope());
Douglas Gregor8bf42052009-02-09 18:46:07 +00002408
Chris Lattner6cc055a2009-04-12 20:42:31 +00002409 // If this is not a typedef name, don't parse it as part of the declspec,
2410 // it must be an implicit int or an error.
John McCallba7bf592010-08-24 05:47:05 +00002411 if (!TypeRep) {
Richard Smithc5b05522012-03-12 07:56:15 +00002412 if (ParseImplicitInt(DS, 0, TemplateInfo, AS, DSContext)) continue;
Chris Lattner16fac4f2008-07-26 01:18:38 +00002413 goto DoneWithDeclSpec;
Chris Lattner6cc055a2009-04-12 20:42:31 +00002414 }
Douglas Gregor8bf42052009-02-09 18:46:07 +00002415
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002416 // If we're in a context where the identifier could be a class name,
2417 // check whether this is a constructor declaration.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002418 if (getLangOpts().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00002419 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), getCurScope()) &&
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002420 isConstructorDeclarator())
Douglas Gregor61956c42008-10-31 09:07:45 +00002421 goto DoneWithDeclSpec;
2422
Douglas Gregor9817f4a2009-02-09 15:09:02 +00002423 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00002424 DiagID, TypeRep);
Chris Lattner16fac4f2008-07-26 01:18:38 +00002425 if (isInvalid)
2426 break;
Mike Stump11289f42009-09-09 15:08:12 +00002427
Chris Lattner16fac4f2008-07-26 01:18:38 +00002428 DS.SetRangeEnd(Tok.getLocation());
2429 ConsumeToken(); // The identifier
2430
2431 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
2432 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Chad Rosierc1183952012-06-26 22:30:43 +00002433 // Objective-C interface.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002434 if (Tok.is(tok::less) && getLangOpts().ObjC1)
Douglas Gregor06e41ae2010-10-21 23:17:00 +00002435 ParseObjCProtocolQualifiers(DS);
Chad Rosierc1183952012-06-26 22:30:43 +00002436
Steve Naroffcd5e7822008-09-22 10:28:57 +00002437 // Need to support trailing type qualifiers (e.g. "id<p> const").
2438 // If a type specifier follows, it will be diagnosed elsewhere.
2439 continue;
Chris Lattner16fac4f2008-07-26 01:18:38 +00002440 }
Douglas Gregor7f741122009-02-25 19:37:18 +00002441
2442 // type-name
2443 case tok::annot_template_id: {
Argyrios Kyrtzidisc0c5dd22011-06-22 06:09:49 +00002444 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregorb67535d2009-03-31 00:43:58 +00002445 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor7f741122009-02-25 19:37:18 +00002446 // This template-id does not refer to a type name, so we're
2447 // done with the type-specifiers.
2448 goto DoneWithDeclSpec;
2449 }
2450
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002451 // If we're in a context where the template-id could be a
2452 // constructor name or specialization, check whether this is a
2453 // constructor declaration.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002454 if (getLangOpts().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00002455 Actions.isCurrentClassName(*TemplateId->Name, getCurScope()) &&
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002456 isConstructorDeclarator())
2457 goto DoneWithDeclSpec;
2458
Douglas Gregor7f741122009-02-25 19:37:18 +00002459 // Turn the template-id annotation token into a type annotation
2460 // token, then try again to parse it as a type-specifier.
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00002461 AnnotateTemplateIdTokenAsType();
Douglas Gregor7f741122009-02-25 19:37:18 +00002462 continue;
2463 }
2464
Chris Lattnere37e2332006-08-15 04:50:22 +00002465 // GNU attributes support.
2466 case tok::kw___attribute:
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +00002467 ParseGNUAttributes(DS.getAttributes(), 0, LateAttrs);
Chris Lattnerb95cca02006-10-17 03:01:08 +00002468 continue;
Steve Naroff3a9b7e02008-12-24 20:59:21 +00002469
2470 // Microsoft declspec support.
2471 case tok::kw___declspec:
John McCall53fa7142010-12-24 02:08:15 +00002472 ParseMicrosoftDeclSpec(DS.getAttributes());
Steve Naroff3a9b7e02008-12-24 20:59:21 +00002473 continue;
Mike Stump11289f42009-09-09 15:08:12 +00002474
Steve Naroff44ac7772008-12-25 14:16:32 +00002475 // Microsoft single token adornments.
Michael J. Spencerf97bd8c2012-06-18 07:00:48 +00002476 case tok::kw___forceinline: {
2477 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec, DiagID);
2478 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
2479 SourceLocation AttrNameLoc = ConsumeToken();
Alexis Hunta0e54d42012-06-18 16:13:52 +00002480 // FIXME: This does not work correctly if it is set to be a declspec
2481 // attribute, and a GNU attribute is simply incorrect.
Michael J. Spencerf97bd8c2012-06-18 07:00:48 +00002482 DS.getAttributes().addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
Alexis Hunta0e54d42012-06-18 16:13:52 +00002483 SourceLocation(), 0, 0, AttributeList::AS_GNU);
Michael J. Spencerf97bd8c2012-06-18 07:00:48 +00002484 continue;
2485 }
Eli Friedman53339e02009-06-08 23:27:34 +00002486
2487 case tok::kw___ptr64:
Francois Pichetf2fb4112011-08-25 00:36:46 +00002488 case tok::kw___ptr32:
Steve Narofff9c29d42008-12-25 14:41:26 +00002489 case tok::kw___w64:
Steve Naroff44ac7772008-12-25 14:16:32 +00002490 case tok::kw___cdecl:
2491 case tok::kw___stdcall:
2492 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00002493 case tok::kw___thiscall:
Francois Pichet17ed0202011-08-18 09:59:55 +00002494 case tok::kw___unaligned:
John McCall53fa7142010-12-24 02:08:15 +00002495 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman53339e02009-06-08 23:27:34 +00002496 continue;
2497
Dawn Perchik335e16b2010-09-03 01:29:35 +00002498 // Borland single token adornments.
2499 case tok::kw___pascal:
John McCall53fa7142010-12-24 02:08:15 +00002500 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik335e16b2010-09-03 01:29:35 +00002501 continue;
2502
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00002503 // OpenCL single token adornments.
2504 case tok::kw___kernel:
2505 ParseOpenCLAttributes(DS.getAttributes());
2506 continue;
2507
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002508 // storage-class-specifier
2509 case tok::kw_typedef:
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002510 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_typedef, Loc,
2511 PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002512 break;
2513 case tok::kw_extern:
Chris Lattner353f5742006-11-28 04:50:12 +00002514 if (DS.isThreadSpecified())
Chris Lattner6d29c102008-11-18 07:48:38 +00002515 Diag(Tok, diag::ext_thread_before) << "extern";
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002516 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_extern, Loc,
2517 PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002518 break;
Steve Naroff2050b0d2007-12-18 00:16:02 +00002519 case tok::kw___private_extern__:
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002520 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_private_extern,
2521 Loc, PrevSpec, DiagID);
Steve Naroff2050b0d2007-12-18 00:16:02 +00002522 break;
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002523 case tok::kw_static:
Chris Lattner353f5742006-11-28 04:50:12 +00002524 if (DS.isThreadSpecified())
Chris Lattner6d29c102008-11-18 07:48:38 +00002525 Diag(Tok, diag::ext_thread_before) << "static";
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002526 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_static, Loc,
2527 PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002528 break;
2529 case tok::kw_auto:
David Blaikiebbafb8a2012-03-11 07:00:24 +00002530 if (getLangOpts().CPlusPlus0x) {
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00002531 if (isKnownToBeTypeSpecifier(GetLookAheadToken(1))) {
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002532 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc,
2533 PrevSpec, DiagID);
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00002534 if (!isInvalid)
Richard Smith58c74332011-09-04 19:54:14 +00002535 Diag(Tok, diag::ext_auto_storage_class)
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00002536 << FixItHint::CreateRemoval(DS.getStorageClassSpecLoc());
Richard Smith58c74332011-09-04 19:54:14 +00002537 } else
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00002538 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec,
2539 DiagID);
Richard Smith58c74332011-09-04 19:54:14 +00002540 } else
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002541 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc,
2542 PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002543 break;
2544 case tok::kw_register:
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002545 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_register, Loc,
2546 PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002547 break;
Sebastian Redlccdfaba2008-11-14 23:42:31 +00002548 case tok::kw_mutable:
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002549 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_mutable, Loc,
2550 PrevSpec, DiagID);
Sebastian Redlccdfaba2008-11-14 23:42:31 +00002551 break;
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002552 case tok::kw___thread:
John McCall49bfce42009-08-03 20:12:06 +00002553 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002554 break;
Mike Stump11289f42009-09-09 15:08:12 +00002555
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002556 // function-specifier
2557 case tok::kw_inline:
John McCall49bfce42009-08-03 20:12:06 +00002558 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec, DiagID);
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002559 break;
Douglas Gregor61956c42008-10-31 09:07:45 +00002560 case tok::kw_virtual:
John McCall49bfce42009-08-03 20:12:06 +00002561 isInvalid = DS.SetFunctionSpecVirtual(Loc, PrevSpec, DiagID);
Douglas Gregor61956c42008-10-31 09:07:45 +00002562 break;
Douglas Gregor61956c42008-10-31 09:07:45 +00002563 case tok::kw_explicit:
John McCall49bfce42009-08-03 20:12:06 +00002564 isInvalid = DS.SetFunctionSpecExplicit(Loc, PrevSpec, DiagID);
Douglas Gregor61956c42008-10-31 09:07:45 +00002565 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00002566
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002567 // alignment-specifier
2568 case tok::kw__Alignas:
David Blaikiebbafb8a2012-03-11 07:00:24 +00002569 if (!getLangOpts().C11)
Jordan Rose58d54722012-06-30 21:33:57 +00002570 Diag(Tok, diag::ext_c11_alignment) << Tok.getName();
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002571 ParseAlignmentSpecifier(DS.getAttributes());
2572 continue;
2573
Anders Carlssoncd8db412009-05-06 04:46:28 +00002574 // friend
2575 case tok::kw_friend:
John McCall07e91c02009-08-06 02:15:43 +00002576 if (DSContext == DSC_class)
2577 isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID);
2578 else {
2579 PrevSpec = ""; // not actually used by the diagnostic
2580 DiagID = diag::err_friend_invalid_in_context;
2581 isInvalid = true;
2582 }
Anders Carlssoncd8db412009-05-06 04:46:28 +00002583 break;
Mike Stump11289f42009-09-09 15:08:12 +00002584
Douglas Gregor26701a42011-09-09 02:06:17 +00002585 // Modules
2586 case tok::kw___module_private__:
2587 isInvalid = DS.setModulePrivateSpec(Loc, PrevSpec, DiagID);
2588 break;
Chad Rosierc1183952012-06-26 22:30:43 +00002589
Sebastian Redl39c2a8b2009-11-05 15:47:02 +00002590 // constexpr
2591 case tok::kw_constexpr:
2592 isInvalid = DS.SetConstexprSpec(Loc, PrevSpec, DiagID);
2593 break;
2594
Chris Lattnere387d9e2009-01-21 19:48:37 +00002595 // type-specifier
2596 case tok::kw_short:
John McCall49bfce42009-08-03 20:12:06 +00002597 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec,
2598 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002599 break;
2600 case tok::kw_long:
2601 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCall49bfce42009-08-03 20:12:06 +00002602 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
2603 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002604 else
John McCall49bfce42009-08-03 20:12:06 +00002605 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2606 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002607 break;
Francois Pichet84133e42011-04-28 01:59:37 +00002608 case tok::kw___int64:
2609 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2610 DiagID);
2611 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00002612 case tok::kw_signed:
John McCall49bfce42009-08-03 20:12:06 +00002613 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec,
2614 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002615 break;
2616 case tok::kw_unsigned:
John McCall49bfce42009-08-03 20:12:06 +00002617 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
2618 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002619 break;
2620 case tok::kw__Complex:
John McCall49bfce42009-08-03 20:12:06 +00002621 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
2622 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002623 break;
2624 case tok::kw__Imaginary:
John McCall49bfce42009-08-03 20:12:06 +00002625 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
2626 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002627 break;
2628 case tok::kw_void:
John McCall49bfce42009-08-03 20:12:06 +00002629 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec,
2630 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002631 break;
2632 case tok::kw_char:
John McCall49bfce42009-08-03 20:12:06 +00002633 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec,
2634 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002635 break;
2636 case tok::kw_int:
John McCall49bfce42009-08-03 20:12:06 +00002637 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec,
2638 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002639 break;
Richard Smithf016bbc2012-04-04 06:24:32 +00002640 case tok::kw___int128:
2641 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int128, Loc, PrevSpec,
2642 DiagID);
2643 break;
2644 case tok::kw_half:
2645 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_half, Loc, PrevSpec,
2646 DiagID);
2647 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00002648 case tok::kw_float:
John McCall49bfce42009-08-03 20:12:06 +00002649 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec,
2650 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002651 break;
2652 case tok::kw_double:
John McCall49bfce42009-08-03 20:12:06 +00002653 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec,
2654 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002655 break;
2656 case tok::kw_wchar_t:
John McCall49bfce42009-08-03 20:12:06 +00002657 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec,
2658 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002659 break;
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00002660 case tok::kw_char16_t:
John McCall49bfce42009-08-03 20:12:06 +00002661 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec,
2662 DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00002663 break;
2664 case tok::kw_char32_t:
John McCall49bfce42009-08-03 20:12:06 +00002665 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec,
2666 DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00002667 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00002668 case tok::kw_bool:
2669 case tok::kw__Bool:
Argyrios Kyrtzidis20ee5ae2010-11-16 18:18:13 +00002670 if (Tok.is(tok::kw_bool) &&
2671 DS.getTypeSpecType() != DeclSpec::TST_unspecified &&
2672 DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
2673 PrevSpec = ""; // Not used by the diagnostic.
2674 DiagID = diag::err_bool_redeclaration;
Fariborz Jahanian2b059992011-04-19 21:42:37 +00002675 // For better error recovery.
2676 Tok.setKind(tok::identifier);
Argyrios Kyrtzidis20ee5ae2010-11-16 18:18:13 +00002677 isInvalid = true;
2678 } else {
2679 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec,
2680 DiagID);
2681 }
Chris Lattnere387d9e2009-01-21 19:48:37 +00002682 break;
2683 case tok::kw__Decimal32:
John McCall49bfce42009-08-03 20:12:06 +00002684 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
2685 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002686 break;
2687 case tok::kw__Decimal64:
John McCall49bfce42009-08-03 20:12:06 +00002688 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
2689 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002690 break;
2691 case tok::kw__Decimal128:
John McCall49bfce42009-08-03 20:12:06 +00002692 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
2693 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002694 break;
John Thompson22334602010-02-05 00:12:22 +00002695 case tok::kw___vector:
2696 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
2697 break;
2698 case tok::kw___pixel:
2699 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
2700 break;
John McCall39439732011-04-09 22:50:59 +00002701 case tok::kw___unknown_anytype:
2702 isInvalid = DS.SetTypeSpecType(TST_unknown_anytype, Loc,
2703 PrevSpec, DiagID);
2704 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00002705
2706 // class-specifier:
2707 case tok::kw_class:
2708 case tok::kw_struct:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00002709 case tok::kw_union: {
2710 tok::TokenKind Kind = Tok.getKind();
2711 ConsumeToken();
Richard Smithc5b05522012-03-12 07:56:15 +00002712 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS,
2713 EnteringContext, DSContext);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002714 continue;
Chris Lattnerffaa0e62009-04-12 21:49:30 +00002715 }
Chris Lattnere387d9e2009-01-21 19:48:37 +00002716
2717 // enum-specifier:
2718 case tok::kw_enum:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00002719 ConsumeToken();
Richard Smithc5b05522012-03-12 07:56:15 +00002720 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS, DSContext);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002721 continue;
2722
2723 // cv-qualifier:
2724 case tok::kw_const:
John McCall49bfce42009-08-03 20:12:06 +00002725 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec, DiagID,
Richard Smith7ac3c6a2012-07-24 20:24:58 +00002726 getLangOpts(), /*IsTypeSpec*/true);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002727 break;
2728 case tok::kw_volatile:
John McCall49bfce42009-08-03 20:12:06 +00002729 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
Richard Smith7ac3c6a2012-07-24 20:24:58 +00002730 getLangOpts(), /*IsTypeSpec*/true);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002731 break;
2732 case tok::kw_restrict:
John McCall49bfce42009-08-03 20:12:06 +00002733 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
Richard Smith7ac3c6a2012-07-24 20:24:58 +00002734 getLangOpts(), /*IsTypeSpec*/true);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002735 break;
2736
Douglas Gregor333489b2009-03-27 23:10:48 +00002737 // C++ typename-specifier:
2738 case tok::kw_typename:
John McCall1f476a12010-02-26 08:45:28 +00002739 if (TryAnnotateTypeOrScopeToken()) {
2740 DS.SetTypeSpecError();
2741 goto DoneWithDeclSpec;
2742 }
2743 if (!Tok.is(tok::kw_typename))
Douglas Gregor333489b2009-03-27 23:10:48 +00002744 continue;
2745 break;
2746
Chris Lattnere387d9e2009-01-21 19:48:37 +00002747 // GNU typeof support.
2748 case tok::kw_typeof:
2749 ParseTypeofSpecifier(DS);
2750 continue;
2751
David Blaikie15a430a2011-12-04 05:04:18 +00002752 case tok::annot_decltype:
Anders Carlsson74948d02009-06-24 17:47:40 +00002753 ParseDecltypeSpecifier(DS);
2754 continue;
2755
Alexis Hunt4a257072011-05-19 05:37:45 +00002756 case tok::kw___underlying_type:
2757 ParseUnderlyingTypeSpecifier(DS);
Eli Friedman0dfb8892011-10-06 23:00:33 +00002758 continue;
2759
2760 case tok::kw__Atomic:
2761 ParseAtomicSpecifier(DS);
2762 continue;
Alexis Hunt4a257072011-05-19 05:37:45 +00002763
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00002764 // OpenCL qualifiers:
Chad Rosierc1183952012-06-26 22:30:43 +00002765 case tok::kw_private:
David Blaikiebbafb8a2012-03-11 07:00:24 +00002766 if (!getLangOpts().OpenCL)
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00002767 goto DoneWithDeclSpec;
2768 case tok::kw___private:
2769 case tok::kw___global:
2770 case tok::kw___local:
2771 case tok::kw___constant:
2772 case tok::kw___read_only:
2773 case tok::kw___write_only:
2774 case tok::kw___read_write:
2775 ParseOpenCLQualifiers(DS);
2776 break;
Chad Rosierc1183952012-06-26 22:30:43 +00002777
Steve Naroffcfdf6162008-06-05 00:02:44 +00002778 case tok::less:
Chris Lattner16fac4f2008-07-26 01:18:38 +00002779 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattner0974b232008-07-26 00:20:22 +00002780 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
2781 // but we support it.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002782 if (DS.hasTypeSpecifier() || !getLangOpts().ObjC1)
Chris Lattner0974b232008-07-26 00:20:22 +00002783 goto DoneWithDeclSpec;
Mike Stump11289f42009-09-09 15:08:12 +00002784
Douglas Gregor3a001f42010-11-19 17:10:50 +00002785 if (!ParseObjCProtocolQualifiers(DS))
2786 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
2787 << FixItHint::CreateInsertion(Loc, "id")
2788 << SourceRange(Loc, DS.getSourceRange().getEnd());
Chad Rosierc1183952012-06-26 22:30:43 +00002789
Douglas Gregor06e41ae2010-10-21 23:17:00 +00002790 // Need to support trailing type qualifiers (e.g. "id<p> const").
2791 // If a type specifier follows, it will be diagnosed elsewhere.
2792 continue;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002793 }
John McCall49bfce42009-08-03 20:12:06 +00002794 // If the specifier wasn't legal, issue a diagnostic.
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002795 if (isInvalid) {
2796 assert(PrevSpec && "Method did not return previous specifier!");
John McCall49bfce42009-08-03 20:12:06 +00002797 assert(DiagID);
Chad Rosierc1183952012-06-26 22:30:43 +00002798
Douglas Gregora05f5ab2010-08-23 14:34:43 +00002799 if (DiagID == diag::ext_duplicate_declspec)
2800 Diag(Tok, DiagID)
2801 << PrevSpec << FixItHint::CreateRemoval(Tok.getLocation());
2802 else
2803 Diag(Tok, DiagID) << PrevSpec;
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002804 }
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00002805
Chris Lattner2e232092008-03-13 06:29:04 +00002806 DS.SetRangeEnd(Tok.getLocation());
Fariborz Jahanian2b059992011-04-19 21:42:37 +00002807 if (DiagID != diag::err_bool_redeclaration)
2808 ConsumeToken();
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00002809
2810 AttrsLastTime = false;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002811 }
2812}
Douglas Gregoreb31f392008-12-01 23:54:00 +00002813
Chris Lattner70ae4912007-10-29 04:42:53 +00002814/// ParseStructDeclaration - Parse a struct declaration without the terminating
2815/// semicolon.
2816///
Chris Lattner90a26b02007-01-23 04:38:16 +00002817/// struct-declaration:
Chris Lattner70ae4912007-10-29 04:42:53 +00002818/// specifier-qualifier-list struct-declarator-list
Chris Lattner736ed5d2007-06-09 05:59:07 +00002819/// [GNU] __extension__ struct-declaration
Chris Lattner70ae4912007-10-29 04:42:53 +00002820/// [GNU] specifier-qualifier-list
Chris Lattner90a26b02007-01-23 04:38:16 +00002821/// struct-declarator-list:
2822/// struct-declarator
2823/// struct-declarator-list ',' struct-declarator
2824/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
2825/// struct-declarator:
2826/// declarator
2827/// [GNU] declarator attributes[opt]
2828/// declarator[opt] ':' constant-expression
2829/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
2830///
Chris Lattnera12405b2008-04-10 06:46:29 +00002831void Parser::
Eli Friedman89b1f2c2012-08-08 23:04:35 +00002832ParseStructDeclaration(ParsingDeclSpec &DS, FieldCallback &Fields) {
Chad Rosierc1183952012-06-26 22:30:43 +00002833
Chris Lattnerf02ef3e2008-10-20 06:45:43 +00002834 if (Tok.is(tok::kw___extension__)) {
2835 // __extension__ silences extension warnings in the subexpression.
2836 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroff97170802007-08-20 22:28:22 +00002837 ConsumeToken();
Chris Lattnerf02ef3e2008-10-20 06:45:43 +00002838 return ParseStructDeclaration(DS, Fields);
2839 }
Mike Stump11289f42009-09-09 15:08:12 +00002840
Steve Naroff97170802007-08-20 22:28:22 +00002841 // Parse the common specifier-qualifiers-list piece.
Steve Naroff97170802007-08-20 22:28:22 +00002842 ParseSpecifierQualifierList(DS);
Mike Stump11289f42009-09-09 15:08:12 +00002843
Douglas Gregorc6f58fe2009-01-12 22:49:06 +00002844 // If there are no declarators, this is a free-standing declaration
2845 // specifier. Let the actions module cope with it.
Chris Lattner76c72282007-10-09 17:33:22 +00002846 if (Tok.is(tok::semi)) {
Eli Friedman89b1f2c2012-08-08 23:04:35 +00002847 Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none,
2848 DS);
2849 DS.complete(TheDecl);
Steve Naroff97170802007-08-20 22:28:22 +00002850 return;
2851 }
2852
2853 // Read struct-declarators until we find the semicolon.
John McCallcfefb6d2009-11-03 02:38:08 +00002854 bool FirstDeclarator = true;
Richard Smith8d06f422012-01-12 23:53:29 +00002855 SourceLocation CommaLoc;
Steve Naroff97170802007-08-20 22:28:22 +00002856 while (1) {
Eli Friedman89b1f2c2012-08-08 23:04:35 +00002857 ParsingFieldDeclarator DeclaratorInfo(*this, DS);
Richard Smith8d06f422012-01-12 23:53:29 +00002858 DeclaratorInfo.D.setCommaLoc(CommaLoc);
John McCallcfefb6d2009-11-03 02:38:08 +00002859
2860 // Attributes are only allowed here on successive declarators.
John McCall53fa7142010-12-24 02:08:15 +00002861 if (!FirstDeclarator)
2862 MaybeParseGNUAttributes(DeclaratorInfo.D);
Mike Stump11289f42009-09-09 15:08:12 +00002863
Steve Naroff97170802007-08-20 22:28:22 +00002864 /// struct-declarator: declarator
2865 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattner17c3b1f2009-12-10 01:59:24 +00002866 if (Tok.isNot(tok::colon)) {
2867 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
2868 ColonProtectionRAIIObject X(*this);
Chris Lattnera12405b2008-04-10 06:46:29 +00002869 ParseDeclarator(DeclaratorInfo.D);
Chris Lattner17c3b1f2009-12-10 01:59:24 +00002870 }
Mike Stump11289f42009-09-09 15:08:12 +00002871
Chris Lattner76c72282007-10-09 17:33:22 +00002872 if (Tok.is(tok::colon)) {
Steve Naroff97170802007-08-20 22:28:22 +00002873 ConsumeToken();
John McCalldadc5752010-08-24 06:29:42 +00002874 ExprResult Res(ParseConstantExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002875 if (Res.isInvalid())
Steve Naroff97170802007-08-20 22:28:22 +00002876 SkipUntil(tok::semi, true, true);
Chris Lattner32295d32008-04-10 06:15:14 +00002877 else
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00002878 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroff97170802007-08-20 22:28:22 +00002879 }
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002880
Steve Naroff97170802007-08-20 22:28:22 +00002881 // If attributes exist after the declarator, parse them.
John McCall53fa7142010-12-24 02:08:15 +00002882 MaybeParseGNUAttributes(DeclaratorInfo.D);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002883
John McCallcfefb6d2009-11-03 02:38:08 +00002884 // We're done with this declarator; invoke the callback.
Eli Friedmanba01f2b2012-08-08 23:35:12 +00002885 Fields.invoke(DeclaratorInfo);
John McCallcfefb6d2009-11-03 02:38:08 +00002886
Steve Naroff97170802007-08-20 22:28:22 +00002887 // If we don't have a comma, it is either the end of the list (a ';')
2888 // or an error, bail out.
Chris Lattner76c72282007-10-09 17:33:22 +00002889 if (Tok.isNot(tok::comma))
Chris Lattner70ae4912007-10-29 04:42:53 +00002890 return;
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002891
Steve Naroff97170802007-08-20 22:28:22 +00002892 // Consume the comma.
Richard Smith8d06f422012-01-12 23:53:29 +00002893 CommaLoc = ConsumeToken();
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002894
John McCallcfefb6d2009-11-03 02:38:08 +00002895 FirstDeclarator = false;
Steve Naroff97170802007-08-20 22:28:22 +00002896 }
Steve Naroff97170802007-08-20 22:28:22 +00002897}
2898
2899/// ParseStructUnionBody
2900/// struct-contents:
2901/// struct-declaration-list
2902/// [EXT] empty
2903/// [GNU] "struct-declaration-list" without terminatoring ';'
2904/// struct-declaration-list:
2905/// struct-declaration
2906/// struct-declaration-list struct-declaration
Chris Lattner535b8302008-06-21 19:39:06 +00002907/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroff97170802007-08-20 22:28:22 +00002908///
Chris Lattner1300fb92007-01-23 23:42:53 +00002909void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
John McCall48871652010-08-21 09:40:31 +00002910 unsigned TagType, Decl *TagDecl) {
John McCallfaf5fb42010-08-26 23:41:50 +00002911 PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc,
2912 "parsing struct/union body");
Mike Stump11289f42009-09-09 15:08:12 +00002913
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002914 BalancedDelimiterTracker T(*this, tok::l_brace);
2915 if (T.consumeOpen())
2916 return;
Mike Stump11289f42009-09-09 15:08:12 +00002917
Douglas Gregor658b9552009-01-09 22:42:13 +00002918 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
Douglas Gregor0be31a22010-07-02 17:43:08 +00002919 Actions.ActOnTagStartDefinition(getCurScope(), TagDecl);
Douglas Gregor82ac25e2009-01-08 20:45:30 +00002920
Chris Lattner7b9ace62007-01-23 20:11:08 +00002921 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
2922 // C++.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002923 if (Tok.is(tok::r_brace) && !getLangOpts().CPlusPlus) {
Richard Smithe4345902011-12-29 21:57:33 +00002924 Diag(Tok, diag::ext_empty_struct_union) << (TagType == TST_union);
2925 Diag(Tok, diag::warn_empty_struct_union_compat) << (TagType == TST_union);
2926 }
Chris Lattner7b9ace62007-01-23 20:11:08 +00002927
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002928 SmallVector<Decl *, 32> FieldDecls;
Chris Lattnera12405b2008-04-10 06:46:29 +00002929
Chris Lattner7b9ace62007-01-23 20:11:08 +00002930 // While we still have something to read, read the declarations in the struct.
Chris Lattner76c72282007-10-09 17:33:22 +00002931 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Chris Lattner90a26b02007-01-23 04:38:16 +00002932 // Each iteration of this loop reads one struct-declaration.
Mike Stump11289f42009-09-09 15:08:12 +00002933
Chris Lattner736ed5d2007-06-09 05:59:07 +00002934 // Check for extraneous top-level semicolon.
Chris Lattner76c72282007-10-09 17:33:22 +00002935 if (Tok.is(tok::semi)) {
Richard Smith87f5dc52012-07-23 05:45:25 +00002936 ConsumeExtraSemi(InsideStruct, TagType);
Chris Lattner36e46a22007-06-09 05:49:55 +00002937 continue;
2938 }
Chris Lattnera12405b2008-04-10 06:46:29 +00002939
John McCallcfefb6d2009-11-03 02:38:08 +00002940 if (!Tok.is(tok::at)) {
2941 struct CFieldCallback : FieldCallback {
2942 Parser &P;
John McCall48871652010-08-21 09:40:31 +00002943 Decl *TagDecl;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002944 SmallVectorImpl<Decl *> &FieldDecls;
John McCallcfefb6d2009-11-03 02:38:08 +00002945
John McCall48871652010-08-21 09:40:31 +00002946 CFieldCallback(Parser &P, Decl *TagDecl,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002947 SmallVectorImpl<Decl *> &FieldDecls) :
John McCallcfefb6d2009-11-03 02:38:08 +00002948 P(P), TagDecl(TagDecl), FieldDecls(FieldDecls) {}
2949
Eli Friedman934dbbf2012-08-08 23:53:27 +00002950 void invoke(ParsingFieldDeclarator &FD) {
John McCallcfefb6d2009-11-03 02:38:08 +00002951 // Install the declarator into the current TagDecl.
John McCall48871652010-08-21 09:40:31 +00002952 Decl *Field = P.Actions.ActOnField(P.getCurScope(), TagDecl,
John McCall5e6253b2009-11-03 21:13:47 +00002953 FD.D.getDeclSpec().getSourceRange().getBegin(),
2954 FD.D, FD.BitfieldSize);
John McCallcfefb6d2009-11-03 02:38:08 +00002955 FieldDecls.push_back(Field);
Eli Friedman89b1f2c2012-08-08 23:04:35 +00002956 FD.complete(Field);
Douglas Gregor66a985d2009-08-26 14:27:30 +00002957 }
John McCallcfefb6d2009-11-03 02:38:08 +00002958 } Callback(*this, TagDecl, FieldDecls);
2959
Eli Friedman89b1f2c2012-08-08 23:04:35 +00002960 // Parse all the comma separated declarators.
2961 ParsingDeclSpec DS(*this);
John McCallcfefb6d2009-11-03 02:38:08 +00002962 ParseStructDeclaration(DS, Callback);
Chris Lattner535b8302008-06-21 19:39:06 +00002963 } else { // Handle @defs
2964 ConsumeToken();
2965 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
2966 Diag(Tok, diag::err_unexpected_at);
Chris Lattner245c5332010-02-02 00:37:27 +00002967 SkipUntil(tok::semi, true);
Chris Lattner535b8302008-06-21 19:39:06 +00002968 continue;
2969 }
2970 ConsumeToken();
2971 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
2972 if (!Tok.is(tok::identifier)) {
2973 Diag(Tok, diag::err_expected_ident);
Chris Lattner245c5332010-02-02 00:37:27 +00002974 SkipUntil(tok::semi, true);
Chris Lattner535b8302008-06-21 19:39:06 +00002975 continue;
2976 }
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002977 SmallVector<Decl *, 16> Fields;
Douglas Gregor0be31a22010-07-02 17:43:08 +00002978 Actions.ActOnDefs(getCurScope(), TagDecl, Tok.getLocation(),
Douglas Gregor91f84212008-12-11 16:49:14 +00002979 Tok.getIdentifierInfo(), Fields);
Chris Lattner535b8302008-06-21 19:39:06 +00002980 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
2981 ConsumeToken();
2982 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
Mike Stump11289f42009-09-09 15:08:12 +00002983 }
Chris Lattner736ed5d2007-06-09 05:59:07 +00002984
Chris Lattner76c72282007-10-09 17:33:22 +00002985 if (Tok.is(tok::semi)) {
Chris Lattner90a26b02007-01-23 04:38:16 +00002986 ConsumeToken();
Chris Lattner76c72282007-10-09 17:33:22 +00002987 } else if (Tok.is(tok::r_brace)) {
Chris Lattner245c5332010-02-02 00:37:27 +00002988 ExpectAndConsume(tok::semi, diag::ext_expected_semi_decl_list);
Chris Lattner0c7e82d2007-06-09 05:54:40 +00002989 break;
Chris Lattner90a26b02007-01-23 04:38:16 +00002990 } else {
Chris Lattner245c5332010-02-02 00:37:27 +00002991 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
2992 // Skip to end of block or statement to avoid ext-warning on extra ';'.
Chris Lattner90a26b02007-01-23 04:38:16 +00002993 SkipUntil(tok::r_brace, true, true);
Chris Lattner245c5332010-02-02 00:37:27 +00002994 // If we stopped at a ';', eat it.
2995 if (Tok.is(tok::semi)) ConsumeToken();
Chris Lattner90a26b02007-01-23 04:38:16 +00002996 }
2997 }
Mike Stump11289f42009-09-09 15:08:12 +00002998
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002999 T.consumeClose();
Mike Stump11289f42009-09-09 15:08:12 +00003000
John McCall084e83d2011-03-24 11:26:52 +00003001 ParsedAttributes attrs(AttrFactory);
Chris Lattner90a26b02007-01-23 04:38:16 +00003002 // If attributes exist after struct contents, parse them.
John McCall53fa7142010-12-24 02:08:15 +00003003 MaybeParseGNUAttributes(attrs);
Daniel Dunbar15619c72008-10-03 02:03:53 +00003004
Douglas Gregor0be31a22010-07-02 17:43:08 +00003005 Actions.ActOnFields(getCurScope(),
David Blaikie751c5582011-09-22 02:58:26 +00003006 RecordLoc, TagDecl, FieldDecls,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003007 T.getOpenLocation(), T.getCloseLocation(),
John McCall53fa7142010-12-24 02:08:15 +00003008 attrs.getList());
Douglas Gregor82ac25e2009-01-08 20:45:30 +00003009 StructScope.Exit();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003010 Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl,
3011 T.getCloseLocation());
Chris Lattner90a26b02007-01-23 04:38:16 +00003012}
3013
Chris Lattner3b561a32006-08-13 00:12:11 +00003014/// ParseEnumSpecifier
Chris Lattner1890ac82006-08-13 01:16:23 +00003015/// enum-specifier: [C99 6.7.2.2]
Chris Lattner3b561a32006-08-13 00:12:11 +00003016/// 'enum' identifier[opt] '{' enumerator-list '}'
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003017///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Chris Lattnere37e2332006-08-15 04:50:22 +00003018/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
3019/// '}' attributes[opt]
Aaron Ballman9ecff022012-03-01 04:09:28 +00003020/// [MS] 'enum' __declspec[opt] identifier[opt] '{' enumerator-list ',' [opt]
3021/// '}'
Chris Lattner3b561a32006-08-13 00:12:11 +00003022/// 'enum' identifier
Chris Lattnere37e2332006-08-15 04:50:22 +00003023/// [GNU] 'enum' attributes[opt] identifier
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003024///
Richard Smith7d137e32012-03-23 03:33:32 +00003025/// [C++11] enum-head '{' enumerator-list[opt] '}'
3026/// [C++11] enum-head '{' enumerator-list ',' '}'
Douglas Gregor0bf31402010-10-08 23:50:27 +00003027///
Richard Smith7d137e32012-03-23 03:33:32 +00003028/// enum-head: [C++11]
3029/// enum-key attribute-specifier-seq[opt] identifier[opt] enum-base[opt]
3030/// enum-key attribute-specifier-seq[opt] nested-name-specifier
3031/// identifier enum-base[opt]
Douglas Gregor0bf31402010-10-08 23:50:27 +00003032///
Richard Smith7d137e32012-03-23 03:33:32 +00003033/// enum-key: [C++11]
Douglas Gregor0bf31402010-10-08 23:50:27 +00003034/// 'enum'
3035/// 'enum' 'class'
3036/// 'enum' 'struct'
3037///
Richard Smith7d137e32012-03-23 03:33:32 +00003038/// enum-base: [C++11]
Douglas Gregor0bf31402010-10-08 23:50:27 +00003039/// ':' type-specifier-seq
3040///
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003041/// [C++] elaborated-type-specifier:
3042/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
3043///
Chris Lattnerffaa0e62009-04-12 21:49:30 +00003044void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregordc70c3a2010-03-02 17:53:14 +00003045 const ParsedTemplateInfo &TemplateInfo,
Richard Smithc5b05522012-03-12 07:56:15 +00003046 AccessSpecifier AS, DeclSpecContext DSC) {
Chris Lattnerffbc2712007-01-25 06:05:38 +00003047 // Parse the tag portion of this.
Douglas Gregorf45b0cf2009-09-18 15:37:17 +00003048 if (Tok.is(tok::code_completion)) {
3049 // Code completion for an enum name.
Douglas Gregor0be31a22010-07-02 17:43:08 +00003050 Actions.CodeCompleteTag(getCurScope(), DeclSpec::TST_enum);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00003051 return cutOffParsing();
Douglas Gregorf45b0cf2009-09-18 15:37:17 +00003052 }
John McCallcb432fa2011-07-06 05:58:41 +00003053
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003054 // If attributes exist after tag, parse them.
3055 ParsedAttributesWithRange attrs(AttrFactory);
3056 MaybeParseGNUAttributes(attrs);
3057 MaybeParseCXX0XAttributes(attrs);
3058
3059 // If declspecs exist after tag, parse them.
3060 while (Tok.is(tok::kw___declspec))
3061 ParseMicrosoftDeclSpec(attrs);
3062
Richard Smith0f8ee222012-01-10 01:33:14 +00003063 SourceLocation ScopedEnumKWLoc;
John McCallcb432fa2011-07-06 05:58:41 +00003064 bool IsScopedUsingClassTag = false;
3065
John McCallbeae29a2012-06-23 22:30:04 +00003066 // In C++11, recognize 'enum class' and 'enum struct'.
David Blaikiebbafb8a2012-03-11 07:00:24 +00003067 if (getLangOpts().CPlusPlus0x &&
John McCallcb432fa2011-07-06 05:58:41 +00003068 (Tok.is(tok::kw_class) || Tok.is(tok::kw_struct))) {
Richard Smith5d164bc2011-10-15 05:09:34 +00003069 Diag(Tok, diag::warn_cxx98_compat_scoped_enum);
John McCallcb432fa2011-07-06 05:58:41 +00003070 IsScopedUsingClassTag = Tok.is(tok::kw_class);
Richard Smith0f8ee222012-01-10 01:33:14 +00003071 ScopedEnumKWLoc = ConsumeToken();
Chad Rosierc1183952012-06-26 22:30:43 +00003072
John McCallbeae29a2012-06-23 22:30:04 +00003073 // Attributes are not allowed between these keywords. Diagnose,
3074 // but then just treat them like they appeared in the right place.
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003075 ProhibitAttributes(attrs);
John McCallbeae29a2012-06-23 22:30:04 +00003076
3077 // They are allowed afterwards, though.
3078 MaybeParseGNUAttributes(attrs);
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003079 MaybeParseCXX0XAttributes(attrs);
John McCallbeae29a2012-06-23 22:30:04 +00003080 while (Tok.is(tok::kw___declspec))
3081 ParseMicrosoftDeclSpec(attrs);
John McCallcb432fa2011-07-06 05:58:41 +00003082 }
Richard Smith7d137e32012-03-23 03:33:32 +00003083
John McCall6347b682012-05-07 06:16:58 +00003084 // C++11 [temp.explicit]p12:
3085 // The usual access controls do not apply to names used to specify
3086 // explicit instantiations.
3087 // We extend this to also cover explicit specializations. Note that
3088 // we don't suppress if this turns out to be an elaborated type
3089 // specifier.
3090 bool shouldDelayDiagsInTag =
3091 (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation ||
3092 TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization);
3093 SuppressAccessChecks diagsFromTag(*this, shouldDelayDiagsInTag);
Richard Smith7d137e32012-03-23 03:33:32 +00003094
Richard Smithbfdb1082012-03-12 08:56:40 +00003095 // Enum definitions should not be parsed in a trailing-return-type.
3096 bool AllowDeclaration = DSC != DSC_trailing;
3097
3098 bool AllowFixedUnderlyingType = AllowDeclaration &&
3099 (getLangOpts().CPlusPlus0x || getLangOpts().MicrosoftExt ||
3100 getLangOpts().ObjC2);
John McCallcb432fa2011-07-06 05:58:41 +00003101
Abramo Bagnarad7548482010-05-19 21:37:53 +00003102 CXXScopeSpec &SS = DS.getTypeSpecScope();
David Blaikiebbafb8a2012-03-11 07:00:24 +00003103 if (getLangOpts().CPlusPlus) {
John McCallcb432fa2011-07-06 05:58:41 +00003104 // "enum foo : bar;" is not a potential typo for "enum foo::bar;"
3105 // if a fixed underlying type is allowed.
3106 ColonProtectionRAIIObject X(*this, AllowFixedUnderlyingType);
Chad Rosierc1183952012-06-26 22:30:43 +00003107
3108 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
Douglas Gregordf593fb2011-11-07 17:33:42 +00003109 /*EnteringContext=*/false))
John McCall1f476a12010-02-26 08:45:28 +00003110 return;
3111
3112 if (SS.isSet() && Tok.isNot(tok::identifier)) {
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003113 Diag(Tok, diag::err_expected_ident);
3114 if (Tok.isNot(tok::l_brace)) {
3115 // Has no name and is not a definition.
3116 // Skip the rest of this declarator, up until the comma or semicolon.
3117 SkipUntil(tok::comma, true);
3118 return;
3119 }
3120 }
3121 }
Mike Stump11289f42009-09-09 15:08:12 +00003122
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00003123 // Must have either 'enum name' or 'enum {...}'.
Douglas Gregor6cd5ae42011-02-22 02:55:24 +00003124 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace) &&
Richard Smithbfdb1082012-03-12 08:56:40 +00003125 !(AllowFixedUnderlyingType && Tok.is(tok::colon))) {
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00003126 Diag(Tok, diag::err_expected_ident_lbrace);
Mike Stump11289f42009-09-09 15:08:12 +00003127
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00003128 // Skip the rest of this declarator, up until the comma or semicolon.
3129 SkipUntil(tok::comma, true);
Chris Lattner3b561a32006-08-13 00:12:11 +00003130 return;
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00003131 }
Mike Stump11289f42009-09-09 15:08:12 +00003132
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00003133 // If an identifier is present, consume and remember it.
3134 IdentifierInfo *Name = 0;
3135 SourceLocation NameLoc;
3136 if (Tok.is(tok::identifier)) {
3137 Name = Tok.getIdentifierInfo();
3138 NameLoc = ConsumeToken();
3139 }
Mike Stump11289f42009-09-09 15:08:12 +00003140
Richard Smith0f8ee222012-01-10 01:33:14 +00003141 if (!Name && ScopedEnumKWLoc.isValid()) {
Douglas Gregor0bf31402010-10-08 23:50:27 +00003142 // C++0x 7.2p2: The optional identifier shall not be omitted in the
3143 // declaration of a scoped enumeration.
3144 Diag(Tok, diag::err_scoped_enum_missing_identifier);
Richard Smith0f8ee222012-01-10 01:33:14 +00003145 ScopedEnumKWLoc = SourceLocation();
Abramo Bagnara0e05e242010-12-03 18:54:17 +00003146 IsScopedUsingClassTag = false;
Douglas Gregor0bf31402010-10-08 23:50:27 +00003147 }
3148
John McCall6347b682012-05-07 06:16:58 +00003149 // Okay, end the suppression area. We'll decide whether to emit the
3150 // diagnostics in a second.
3151 if (shouldDelayDiagsInTag)
3152 diagsFromTag.done();
Richard Smith7d137e32012-03-23 03:33:32 +00003153
Douglas Gregor0bf31402010-10-08 23:50:27 +00003154 TypeResult BaseType;
3155
Douglas Gregord1f69f62010-12-01 17:42:47 +00003156 // Parse the fixed underlying type.
Richard Smith200f47c2012-07-02 19:14:01 +00003157 bool CanBeBitfield = getCurScope()->getFlags() & Scope::ClassScope;
Douglas Gregor6cd5ae42011-02-22 02:55:24 +00003158 if (AllowFixedUnderlyingType && Tok.is(tok::colon)) {
Douglas Gregord1f69f62010-12-01 17:42:47 +00003159 bool PossibleBitfield = false;
Richard Smith200f47c2012-07-02 19:14:01 +00003160 if (CanBeBitfield) {
Douglas Gregord1f69f62010-12-01 17:42:47 +00003161 // If we're in class scope, this can either be an enum declaration with
3162 // an underlying type, or a declaration of a bitfield member. We try to
3163 // use a simple disambiguation scheme first to catch the common cases
Chad Rosierc1183952012-06-26 22:30:43 +00003164 // (integer literal, sizeof); if it's still ambiguous, we then consider
3165 // anything that's a simple-type-specifier followed by '(' as an
3166 // expression. This suffices because function types are not valid
Douglas Gregord1f69f62010-12-01 17:42:47 +00003167 // underlying types anyway.
3168 TPResult TPR = isExpressionOrTypeSpecifierSimple(NextToken().getKind());
Chad Rosierc1183952012-06-26 22:30:43 +00003169 // If the next token starts an expression, we know we're parsing a
Douglas Gregord1f69f62010-12-01 17:42:47 +00003170 // bit-field. This is the common case.
3171 if (TPR == TPResult::True())
3172 PossibleBitfield = true;
3173 // If the next token starts a type-specifier-seq, it may be either a
3174 // a fixed underlying type or the start of a function-style cast in C++;
Chad Rosierc1183952012-06-26 22:30:43 +00003175 // lookahead one more token to see if it's obvious that we have a
Douglas Gregord1f69f62010-12-01 17:42:47 +00003176 // fixed underlying type.
Chad Rosierc1183952012-06-26 22:30:43 +00003177 else if (TPR == TPResult::False() &&
Douglas Gregord1f69f62010-12-01 17:42:47 +00003178 GetLookAheadToken(2).getKind() == tok::semi) {
3179 // Consume the ':'.
3180 ConsumeToken();
3181 } else {
3182 // We have the start of a type-specifier-seq, so we have to perform
3183 // tentative parsing to determine whether we have an expression or a
3184 // type.
3185 TentativeParsingAction TPA(*this);
3186
3187 // Consume the ':'.
3188 ConsumeToken();
Richard Smith1e3b0f02012-02-23 01:36:12 +00003189
3190 // If we see a type specifier followed by an open-brace, we have an
3191 // ambiguity between an underlying type and a C++11 braced
3192 // function-style cast. Resolve this by always treating it as an
3193 // underlying type.
3194 // FIXME: The standard is not entirely clear on how to disambiguate in
3195 // this case.
David Blaikiebbafb8a2012-03-11 07:00:24 +00003196 if ((getLangOpts().CPlusPlus &&
Richard Smith1e3b0f02012-02-23 01:36:12 +00003197 isCXXDeclarationSpecifier(TPResult::True()) != TPResult::True()) ||
David Blaikiebbafb8a2012-03-11 07:00:24 +00003198 (!getLangOpts().CPlusPlus && !isDeclarationSpecifier(true))) {
Douglas Gregord1f69f62010-12-01 17:42:47 +00003199 // We'll parse this as a bitfield later.
3200 PossibleBitfield = true;
3201 TPA.Revert();
3202 } else {
3203 // We have a type-specifier-seq.
3204 TPA.Commit();
3205 }
3206 }
3207 } else {
3208 // Consume the ':'.
3209 ConsumeToken();
3210 }
3211
3212 if (!PossibleBitfield) {
3213 SourceRange Range;
3214 BaseType = ParseTypeName(&Range);
Chad Rosierc1183952012-06-26 22:30:43 +00003215
David Blaikiebbafb8a2012-03-11 07:00:24 +00003216 if (!getLangOpts().CPlusPlus0x && !getLangOpts().ObjC2)
Douglas Gregora1aec292011-02-22 20:32:04 +00003217 Diag(StartLoc, diag::ext_ms_enum_fixed_underlying_type)
3218 << Range;
David Blaikiebbafb8a2012-03-11 07:00:24 +00003219 if (getLangOpts().CPlusPlus0x)
Richard Smith5d164bc2011-10-15 05:09:34 +00003220 Diag(StartLoc, diag::warn_cxx98_compat_enum_fixed_underlying_type);
Douglas Gregord1f69f62010-12-01 17:42:47 +00003221 }
Douglas Gregor0bf31402010-10-08 23:50:27 +00003222 }
3223
Richard Smith0f8ee222012-01-10 01:33:14 +00003224 // There are four options here. If we have 'friend enum foo;' then this is a
3225 // friend declaration, and cannot have an accompanying definition. If we have
3226 // 'enum foo;', then this is a forward declaration. If we have
3227 // 'enum foo {...' then this is a definition. Otherwise we have something
3228 // like 'enum foo xyz', a reference.
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00003229 //
3230 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
3231 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
3232 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
3233 //
John McCallfaf5fb42010-08-26 23:41:50 +00003234 Sema::TagUseKind TUK;
John McCall6347b682012-05-07 06:16:58 +00003235 if (!AllowDeclaration) {
Richard Smithbfdb1082012-03-12 08:56:40 +00003236 TUK = Sema::TUK_Reference;
John McCall6347b682012-05-07 06:16:58 +00003237 } else if (Tok.is(tok::l_brace)) {
3238 if (DS.isFriendSpecified()) {
3239 Diag(Tok.getLocation(), diag::err_friend_decl_defines_type)
3240 << SourceRange(DS.getFriendSpecLoc());
3241 ConsumeBrace();
3242 SkipUntil(tok::r_brace);
3243 TUK = Sema::TUK_Friend;
3244 } else {
3245 TUK = Sema::TUK_Definition;
3246 }
Richard Smith369b9f92012-06-25 21:37:02 +00003247 } else if (DSC != DSC_type_specifier &&
3248 (Tok.is(tok::semi) ||
Richard Smith200f47c2012-07-02 19:14:01 +00003249 (Tok.isAtStartOfLine() &&
3250 !isValidAfterTypeSpecifier(CanBeBitfield)))) {
Richard Smith369b9f92012-06-25 21:37:02 +00003251 TUK = DS.isFriendSpecified() ? Sema::TUK_Friend : Sema::TUK_Declaration;
3252 if (Tok.isNot(tok::semi)) {
3253 // A semicolon was missing after this declaration. Diagnose and recover.
3254 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_tagdecl,
3255 "enum");
3256 PP.EnterToken(Tok);
3257 Tok.setKind(tok::semi);
3258 }
John McCall6347b682012-05-07 06:16:58 +00003259 } else {
John McCallfaf5fb42010-08-26 23:41:50 +00003260 TUK = Sema::TUK_Reference;
John McCall6347b682012-05-07 06:16:58 +00003261 }
3262
3263 // If this is an elaborated type specifier, and we delayed
3264 // diagnostics before, just merge them into the current pool.
3265 if (TUK == Sema::TUK_Reference && shouldDelayDiagsInTag) {
3266 diagsFromTag.redelay();
3267 }
Richard Smith7d137e32012-03-23 03:33:32 +00003268
3269 MultiTemplateParamsArg TParams;
Douglas Gregorcbbf3e32010-05-03 17:48:54 +00003270 if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate &&
John McCallfaf5fb42010-08-26 23:41:50 +00003271 TUK != Sema::TUK_Reference) {
Richard Smith7d137e32012-03-23 03:33:32 +00003272 if (!getLangOpts().CPlusPlus0x || !SS.isSet()) {
3273 // Skip the rest of this declarator, up until the comma or semicolon.
3274 Diag(Tok, diag::err_enum_template);
3275 SkipUntil(tok::comma, true);
3276 return;
3277 }
3278
3279 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) {
3280 // Enumerations can't be explicitly instantiated.
3281 DS.SetTypeSpecError();
3282 Diag(StartLoc, diag::err_explicit_instantiation_enum);
3283 return;
3284 }
3285
3286 assert(TemplateInfo.TemplateParams && "no template parameters");
3287 TParams = MultiTemplateParamsArg(TemplateInfo.TemplateParams->data(),
3288 TemplateInfo.TemplateParams->size());
Douglas Gregorcbbf3e32010-05-03 17:48:54 +00003289 }
Chad Rosierc1183952012-06-26 22:30:43 +00003290
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003291 if (TUK == Sema::TUK_Reference)
3292 ProhibitAttributes(attrs);
Richard Smith7d137e32012-03-23 03:33:32 +00003293
Douglas Gregor6cd5ae42011-02-22 02:55:24 +00003294 if (!Name && TUK != Sema::TUK_Definition) {
3295 Diag(Tok, diag::err_enumerator_unnamed_no_def);
Richard Smith7d137e32012-03-23 03:33:32 +00003296
Douglas Gregor6cd5ae42011-02-22 02:55:24 +00003297 // Skip the rest of this declarator, up until the comma or semicolon.
3298 SkipUntil(tok::comma, true);
3299 return;
3300 }
Richard Smith7d137e32012-03-23 03:33:32 +00003301
Douglas Gregord6ab8742009-05-28 23:31:59 +00003302 bool Owned = false;
John McCall7f41d982009-09-11 04:59:25 +00003303 bool IsDependent = false;
Douglas Gregorba41d012010-04-24 16:38:41 +00003304 const char *PrevSpec = 0;
3305 unsigned DiagID;
John McCall48871652010-08-21 09:40:31 +00003306 Decl *TagDecl = Actions.ActOnTag(getCurScope(), DeclSpec::TST_enum, TUK,
John McCall53fa7142010-12-24 02:08:15 +00003307 StartLoc, SS, Name, NameLoc, attrs.getList(),
Richard Smith7d137e32012-03-23 03:33:32 +00003308 AS, DS.getModulePrivateSpecLoc(), TParams,
Richard Smith0f8ee222012-01-10 01:33:14 +00003309 Owned, IsDependent, ScopedEnumKWLoc,
Abramo Bagnara0e05e242010-12-03 18:54:17 +00003310 IsScopedUsingClassTag, BaseType);
Douglas Gregor0bf31402010-10-08 23:50:27 +00003311
Douglas Gregorba41d012010-04-24 16:38:41 +00003312 if (IsDependent) {
Chad Rosierc1183952012-06-26 22:30:43 +00003313 // This enum has a dependent nested-name-specifier. Handle it as a
Douglas Gregorba41d012010-04-24 16:38:41 +00003314 // dependent tag.
3315 if (!Name) {
3316 DS.SetTypeSpecError();
3317 Diag(Tok, diag::err_expected_type_name_after_typename);
3318 return;
3319 }
Chad Rosierc1183952012-06-26 22:30:43 +00003320
Douglas Gregor0be31a22010-07-02 17:43:08 +00003321 TypeResult Type = Actions.ActOnDependentTag(getCurScope(), DeclSpec::TST_enum,
Chad Rosierc1183952012-06-26 22:30:43 +00003322 TUK, SS, Name, StartLoc,
Douglas Gregorba41d012010-04-24 16:38:41 +00003323 NameLoc);
3324 if (Type.isInvalid()) {
3325 DS.SetTypeSpecError();
3326 return;
3327 }
Chad Rosierc1183952012-06-26 22:30:43 +00003328
Abramo Bagnara9875a3c2011-03-16 20:16:18 +00003329 if (DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc,
3330 NameLoc.isValid() ? NameLoc : StartLoc,
3331 PrevSpec, DiagID, Type.get()))
Douglas Gregorba41d012010-04-24 16:38:41 +00003332 Diag(StartLoc, DiagID) << PrevSpec;
Chad Rosierc1183952012-06-26 22:30:43 +00003333
Douglas Gregorba41d012010-04-24 16:38:41 +00003334 return;
3335 }
Mike Stump11289f42009-09-09 15:08:12 +00003336
John McCall48871652010-08-21 09:40:31 +00003337 if (!TagDecl) {
Chad Rosierc1183952012-06-26 22:30:43 +00003338 // The action failed to produce an enumeration tag. If this is a
Douglas Gregorba41d012010-04-24 16:38:41 +00003339 // definition, consume the entire definition.
Richard Smithbfdb1082012-03-12 08:56:40 +00003340 if (Tok.is(tok::l_brace) && TUK != Sema::TUK_Reference) {
Douglas Gregorba41d012010-04-24 16:38:41 +00003341 ConsumeBrace();
3342 SkipUntil(tok::r_brace);
3343 }
Chad Rosierc1183952012-06-26 22:30:43 +00003344
Douglas Gregorba41d012010-04-24 16:38:41 +00003345 DS.SetTypeSpecError();
3346 return;
3347 }
Richard Smith0f8ee222012-01-10 01:33:14 +00003348
Richard Smith369b9f92012-06-25 21:37:02 +00003349 if (Tok.is(tok::l_brace) && TUK != Sema::TUK_Reference)
John McCall6347b682012-05-07 06:16:58 +00003350 ParseEnumBody(StartLoc, TagDecl);
Mike Stump11289f42009-09-09 15:08:12 +00003351
Abramo Bagnara9875a3c2011-03-16 20:16:18 +00003352 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc,
3353 NameLoc.isValid() ? NameLoc : StartLoc,
3354 PrevSpec, DiagID, TagDecl, Owned))
John McCall49bfce42009-08-03 20:12:06 +00003355 Diag(StartLoc, DiagID) << PrevSpec;
Chris Lattner3b561a32006-08-13 00:12:11 +00003356}
3357
Chris Lattnerc1915e22007-01-25 07:29:02 +00003358/// ParseEnumBody - Parse a {} enclosed enumerator-list.
3359/// enumerator-list:
3360/// enumerator
3361/// enumerator-list ',' enumerator
3362/// enumerator:
3363/// enumeration-constant
3364/// enumeration-constant '=' constant-expression
3365/// enumeration-constant:
3366/// identifier
3367///
John McCall48871652010-08-21 09:40:31 +00003368void Parser::ParseEnumBody(SourceLocation StartLoc, Decl *EnumDecl) {
Douglas Gregor07665a62009-01-05 19:45:36 +00003369 // Enter the scope of the enum body and start the definition.
3370 ParseScope EnumScope(this, Scope::DeclScope);
Douglas Gregor0be31a22010-07-02 17:43:08 +00003371 Actions.ActOnTagStartDefinition(getCurScope(), EnumDecl);
Douglas Gregor07665a62009-01-05 19:45:36 +00003372
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003373 BalancedDelimiterTracker T(*this, tok::l_brace);
3374 T.consumeOpen();
Mike Stump11289f42009-09-09 15:08:12 +00003375
Chris Lattner37256fb2007-08-27 17:24:30 +00003376 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
David Blaikiebbafb8a2012-03-11 07:00:24 +00003377 if (Tok.is(tok::r_brace) && !getLangOpts().CPlusPlus)
Fariborz Jahanian6e814922010-05-28 22:23:22 +00003378 Diag(Tok, diag::error_empty_enum);
Mike Stump11289f42009-09-09 15:08:12 +00003379
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003380 SmallVector<Decl *, 32> EnumConstantDecls;
Chris Lattnerc1915e22007-01-25 07:29:02 +00003381
John McCall48871652010-08-21 09:40:31 +00003382 Decl *LastEnumConstDecl = 0;
Mike Stump11289f42009-09-09 15:08:12 +00003383
Chris Lattnerc1915e22007-01-25 07:29:02 +00003384 // Parse the enumerator-list.
Chris Lattner76c72282007-10-09 17:33:22 +00003385 while (Tok.is(tok::identifier)) {
Chris Lattnerc1915e22007-01-25 07:29:02 +00003386 IdentifierInfo *Ident = Tok.getIdentifierInfo();
3387 SourceLocation IdentLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00003388
John McCall811a0f52010-10-22 23:36:17 +00003389 // If attributes exist after the enumerator, parse them.
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003390 ParsedAttributesWithRange attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00003391 MaybeParseGNUAttributes(attrs);
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003392 MaybeParseCXX0XAttributes(attrs);
3393 ProhibitAttributes(attrs);
John McCall811a0f52010-10-22 23:36:17 +00003394
Chris Lattnerc1915e22007-01-25 07:29:02 +00003395 SourceLocation EqualLoc;
John McCalldadc5752010-08-24 06:29:42 +00003396 ExprResult AssignedVal;
John McCall2ec85372012-05-07 06:16:41 +00003397 ParsingDeclRAIIObject PD(*this, ParsingDeclRAIIObject::NoParent);
Chad Rosierc1183952012-06-26 22:30:43 +00003398
Chris Lattner76c72282007-10-09 17:33:22 +00003399 if (Tok.is(tok::equal)) {
Chris Lattnerc1915e22007-01-25 07:29:02 +00003400 EqualLoc = ConsumeToken();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00003401 AssignedVal = ParseConstantExpression();
3402 if (AssignedVal.isInvalid())
Chris Lattnerda6c2ce2007-04-27 19:13:15 +00003403 SkipUntil(tok::comma, tok::r_brace, true, true);
Chris Lattnerc1915e22007-01-25 07:29:02 +00003404 }
Mike Stump11289f42009-09-09 15:08:12 +00003405
Chris Lattnerc1915e22007-01-25 07:29:02 +00003406 // Install the enumerator constant into EnumDecl.
John McCall48871652010-08-21 09:40:31 +00003407 Decl *EnumConstDecl = Actions.ActOnEnumConstant(getCurScope(), EnumDecl,
3408 LastEnumConstDecl,
3409 IdentLoc, Ident,
John McCall53fa7142010-12-24 02:08:15 +00003410 attrs.getList(), EqualLoc,
John McCall48871652010-08-21 09:40:31 +00003411 AssignedVal.release());
Fariborz Jahanian329b3512011-12-09 01:15:54 +00003412 PD.complete(EnumConstDecl);
Chad Rosierc1183952012-06-26 22:30:43 +00003413
Chris Lattner4ef40012007-06-11 01:28:17 +00003414 EnumConstantDecls.push_back(EnumConstDecl);
3415 LastEnumConstDecl = EnumConstDecl;
Mike Stump11289f42009-09-09 15:08:12 +00003416
Douglas Gregorce66d022010-09-07 14:51:08 +00003417 if (Tok.is(tok::identifier)) {
3418 // We're missing a comma between enumerators.
3419 SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation);
Chad Rosierc1183952012-06-26 22:30:43 +00003420 Diag(Loc, diag::err_enumerator_list_missing_comma)
Douglas Gregorce66d022010-09-07 14:51:08 +00003421 << FixItHint::CreateInsertion(Loc, ", ");
3422 continue;
3423 }
Chad Rosierc1183952012-06-26 22:30:43 +00003424
Chris Lattner76c72282007-10-09 17:33:22 +00003425 if (Tok.isNot(tok::comma))
Chris Lattnerc1915e22007-01-25 07:29:02 +00003426 break;
3427 SourceLocation CommaLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00003428
Richard Smith5d164bc2011-10-15 05:09:34 +00003429 if (Tok.isNot(tok::identifier)) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00003430 if (!getLangOpts().C99 && !getLangOpts().CPlusPlus0x)
Richard Smith87f5dc52012-07-23 05:45:25 +00003431 Diag(CommaLoc, getLangOpts().CPlusPlus ?
3432 diag::ext_enumerator_list_comma_cxx :
3433 diag::ext_enumerator_list_comma_c)
Richard Smith5d164bc2011-10-15 05:09:34 +00003434 << FixItHint::CreateRemoval(CommaLoc);
David Blaikiebbafb8a2012-03-11 07:00:24 +00003435 else if (getLangOpts().CPlusPlus0x)
Richard Smith5d164bc2011-10-15 05:09:34 +00003436 Diag(CommaLoc, diag::warn_cxx98_compat_enumerator_list_comma)
3437 << FixItHint::CreateRemoval(CommaLoc);
3438 }
Chris Lattnerc1915e22007-01-25 07:29:02 +00003439 }
Mike Stump11289f42009-09-09 15:08:12 +00003440
Chris Lattnerc1915e22007-01-25 07:29:02 +00003441 // Eat the }.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003442 T.consumeClose();
Chris Lattnerc1915e22007-01-25 07:29:02 +00003443
Chris Lattnerc1915e22007-01-25 07:29:02 +00003444 // If attributes exist after the identifier list, parse them.
John McCall084e83d2011-03-24 11:26:52 +00003445 ParsedAttributes attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00003446 MaybeParseGNUAttributes(attrs);
Douglas Gregor82ac25e2009-01-08 20:45:30 +00003447
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003448 Actions.ActOnEnumBody(StartLoc, T.getOpenLocation(), T.getCloseLocation(),
3449 EnumDecl, EnumConstantDecls.data(),
3450 EnumConstantDecls.size(), getCurScope(),
3451 attrs.getList());
Mike Stump11289f42009-09-09 15:08:12 +00003452
Douglas Gregor82ac25e2009-01-08 20:45:30 +00003453 EnumScope.Exit();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003454 Actions.ActOnTagFinishDefinition(getCurScope(), EnumDecl,
3455 T.getCloseLocation());
Richard Smith369b9f92012-06-25 21:37:02 +00003456
3457 // The next token must be valid after an enum definition. If not, a ';'
3458 // was probably forgotten.
Richard Smith200f47c2012-07-02 19:14:01 +00003459 bool CanBeBitfield = getCurScope()->getFlags() & Scope::ClassScope;
3460 if (!isValidAfterTypeSpecifier(CanBeBitfield)) {
Richard Smith369b9f92012-06-25 21:37:02 +00003461 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_tagdecl, "enum");
3462 // Push this token back into the preprocessor and change our current token
3463 // to ';' so that the rest of the code recovers as though there were an
3464 // ';' after the definition.
3465 PP.EnterToken(Tok);
3466 Tok.setKind(tok::semi);
3467 }
Chris Lattnerc1915e22007-01-25 07:29:02 +00003468}
Chris Lattner3b561a32006-08-13 00:12:11 +00003469
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003470/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff69e8f9e2008-02-11 23:15:56 +00003471/// start of a type-qualifier-list.
3472bool Parser::isTypeQualifier() const {
3473 switch (Tok.getKind()) {
3474 default: return false;
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003475
3476 // type-qualifier only in OpenCL
3477 case tok::kw_private:
David Blaikiebbafb8a2012-03-11 07:00:24 +00003478 return getLangOpts().OpenCL;
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003479
Steve Naroff69e8f9e2008-02-11 23:15:56 +00003480 // type-qualifier
3481 case tok::kw_const:
3482 case tok::kw_volatile:
3483 case tok::kw_restrict:
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003484 case tok::kw___private:
3485 case tok::kw___local:
3486 case tok::kw___global:
3487 case tok::kw___constant:
3488 case tok::kw___read_only:
3489 case tok::kw___read_write:
3490 case tok::kw___write_only:
Steve Naroff69e8f9e2008-02-11 23:15:56 +00003491 return true;
3492 }
3493}
3494
Chris Lattnerfd48afe2010-02-28 18:18:36 +00003495/// isKnownToBeTypeSpecifier - Return true if we know that the specified token
3496/// is definitely a type-specifier. Return false if it isn't part of a type
3497/// specifier or if we're not sure.
3498bool Parser::isKnownToBeTypeSpecifier(const Token &Tok) const {
3499 switch (Tok.getKind()) {
3500 default: return false;
3501 // type-specifiers
3502 case tok::kw_short:
3503 case tok::kw_long:
Francois Pichet84133e42011-04-28 01:59:37 +00003504 case tok::kw___int64:
Richard Smithf016bbc2012-04-04 06:24:32 +00003505 case tok::kw___int128:
Chris Lattnerfd48afe2010-02-28 18:18:36 +00003506 case tok::kw_signed:
3507 case tok::kw_unsigned:
3508 case tok::kw__Complex:
3509 case tok::kw__Imaginary:
3510 case tok::kw_void:
3511 case tok::kw_char:
3512 case tok::kw_wchar_t:
3513 case tok::kw_char16_t:
3514 case tok::kw_char32_t:
3515 case tok::kw_int:
Anton Korobeynikovf0c267e2011-10-14 23:23:15 +00003516 case tok::kw_half:
Chris Lattnerfd48afe2010-02-28 18:18:36 +00003517 case tok::kw_float:
3518 case tok::kw_double:
3519 case tok::kw_bool:
3520 case tok::kw__Bool:
3521 case tok::kw__Decimal32:
3522 case tok::kw__Decimal64:
3523 case tok::kw__Decimal128:
3524 case tok::kw___vector:
Chad Rosierc1183952012-06-26 22:30:43 +00003525
Chris Lattnerfd48afe2010-02-28 18:18:36 +00003526 // struct-or-union-specifier (C99) or class-specifier (C++)
3527 case tok::kw_class:
3528 case tok::kw_struct:
3529 case tok::kw_union:
3530 // enum-specifier
3531 case tok::kw_enum:
Chad Rosierc1183952012-06-26 22:30:43 +00003532
Chris Lattnerfd48afe2010-02-28 18:18:36 +00003533 // typedef-name
3534 case tok::annot_typename:
3535 return true;
3536 }
3537}
3538
Steve Naroff69e8f9e2008-02-11 23:15:56 +00003539/// isTypeSpecifierQualifier - Return true if the current token could be the
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003540/// start of a specifier-qualifier-list.
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003541bool Parser::isTypeSpecifierQualifier() {
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003542 switch (Tok.getKind()) {
3543 default: return false;
Mike Stump11289f42009-09-09 15:08:12 +00003544
Chris Lattner020bab92009-01-04 23:41:41 +00003545 case tok::identifier: // foo::bar
John Thompson22334602010-02-05 00:12:22 +00003546 if (TryAltiVecVectorToken())
3547 return true;
3548 // Fall through.
Douglas Gregor333489b2009-03-27 23:10:48 +00003549 case tok::kw_typename: // typename T::type
Chris Lattner020bab92009-01-04 23:41:41 +00003550 // Annotate typenames and C++ scope specifiers. If we get one, just
3551 // recurse to handle whatever we get.
3552 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00003553 return true;
3554 if (Tok.is(tok::identifier))
3555 return false;
3556 return isTypeSpecifierQualifier();
Douglas Gregor333489b2009-03-27 23:10:48 +00003557
Chris Lattner020bab92009-01-04 23:41:41 +00003558 case tok::coloncolon: // ::foo::bar
3559 if (NextToken().is(tok::kw_new) || // ::new
3560 NextToken().is(tok::kw_delete)) // ::delete
3561 return false;
3562
Chris Lattner020bab92009-01-04 23:41:41 +00003563 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00003564 return true;
3565 return isTypeSpecifierQualifier();
Mike Stump11289f42009-09-09 15:08:12 +00003566
Chris Lattnere37e2332006-08-15 04:50:22 +00003567 // GNU attributes support.
3568 case tok::kw___attribute:
Steve Naroffad373bd2007-07-31 12:34:36 +00003569 // GNU typeof support.
3570 case tok::kw_typeof:
Mike Stump11289f42009-09-09 15:08:12 +00003571
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003572 // type-specifiers
3573 case tok::kw_short:
3574 case tok::kw_long:
Francois Pichet84133e42011-04-28 01:59:37 +00003575 case tok::kw___int64:
Richard Smithf016bbc2012-04-04 06:24:32 +00003576 case tok::kw___int128:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003577 case tok::kw_signed:
3578 case tok::kw_unsigned:
3579 case tok::kw__Complex:
3580 case tok::kw__Imaginary:
3581 case tok::kw_void:
3582 case tok::kw_char:
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +00003583 case tok::kw_wchar_t:
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00003584 case tok::kw_char16_t:
3585 case tok::kw_char32_t:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003586 case tok::kw_int:
Anton Korobeynikovf0c267e2011-10-14 23:23:15 +00003587 case tok::kw_half:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003588 case tok::kw_float:
3589 case tok::kw_double:
Chris Lattnerbb31a422007-11-15 05:25:19 +00003590 case tok::kw_bool:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003591 case tok::kw__Bool:
3592 case tok::kw__Decimal32:
3593 case tok::kw__Decimal64:
3594 case tok::kw__Decimal128:
John Thompson22334602010-02-05 00:12:22 +00003595 case tok::kw___vector:
Mike Stump11289f42009-09-09 15:08:12 +00003596
Chris Lattner861a2262008-04-13 18:59:07 +00003597 // struct-or-union-specifier (C99) or class-specifier (C++)
3598 case tok::kw_class:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003599 case tok::kw_struct:
3600 case tok::kw_union:
3601 // enum-specifier
3602 case tok::kw_enum:
Mike Stump11289f42009-09-09 15:08:12 +00003603
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003604 // type-qualifier
3605 case tok::kw_const:
3606 case tok::kw_volatile:
3607 case tok::kw_restrict:
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003608
3609 // typedef-name
Chris Lattnera8a3f732009-01-06 05:06:21 +00003610 case tok::annot_typename:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003611 return true;
Mike Stump11289f42009-09-09 15:08:12 +00003612
Chris Lattner409bf7d2008-10-20 00:25:30 +00003613 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
3614 case tok::less:
David Blaikiebbafb8a2012-03-11 07:00:24 +00003615 return getLangOpts().ObjC1;
Mike Stump11289f42009-09-09 15:08:12 +00003616
Steve Naroff44ac7772008-12-25 14:16:32 +00003617 case tok::kw___cdecl:
3618 case tok::kw___stdcall:
3619 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00003620 case tok::kw___thiscall:
Eli Friedman53339e02009-06-08 23:27:34 +00003621 case tok::kw___w64:
3622 case tok::kw___ptr64:
Francois Pichetf2fb4112011-08-25 00:36:46 +00003623 case tok::kw___ptr32:
Dawn Perchik335e16b2010-09-03 01:29:35 +00003624 case tok::kw___pascal:
Francois Pichet17ed0202011-08-18 09:59:55 +00003625 case tok::kw___unaligned:
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003626
3627 case tok::kw___private:
3628 case tok::kw___local:
3629 case tok::kw___global:
3630 case tok::kw___constant:
3631 case tok::kw___read_only:
3632 case tok::kw___read_write:
3633 case tok::kw___write_only:
3634
Eli Friedman53339e02009-06-08 23:27:34 +00003635 return true;
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003636
3637 case tok::kw_private:
David Blaikiebbafb8a2012-03-11 07:00:24 +00003638 return getLangOpts().OpenCL;
Eli Friedman0dfb8892011-10-06 23:00:33 +00003639
Benjamin Kramere56f3932011-12-23 17:00:35 +00003640 // C11 _Atomic()
Eli Friedman0dfb8892011-10-06 23:00:33 +00003641 case tok::kw__Atomic:
3642 return true;
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003643 }
3644}
3645
Chris Lattneracd58a32006-08-06 17:24:14 +00003646/// isDeclarationSpecifier() - Return true if the current token is part of a
3647/// declaration specifier.
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00003648///
3649/// \param DisambiguatingWithExpression True to indicate that the purpose of
3650/// this check is to disambiguate between an expression and a declaration.
3651bool Parser::isDeclarationSpecifier(bool DisambiguatingWithExpression) {
Chris Lattneracd58a32006-08-06 17:24:14 +00003652 switch (Tok.getKind()) {
3653 default: return false;
Mike Stump11289f42009-09-09 15:08:12 +00003654
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003655 case tok::kw_private:
David Blaikiebbafb8a2012-03-11 07:00:24 +00003656 return getLangOpts().OpenCL;
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003657
Chris Lattner020bab92009-01-04 23:41:41 +00003658 case tok::identifier: // foo::bar
Steve Naroff9527bbf2009-03-09 21:12:44 +00003659 // Unfortunate hack to support "Class.factoryMethod" notation.
David Blaikiebbafb8a2012-03-11 07:00:24 +00003660 if (getLangOpts().ObjC1 && NextToken().is(tok::period))
Steve Naroff9527bbf2009-03-09 21:12:44 +00003661 return false;
John Thompson22334602010-02-05 00:12:22 +00003662 if (TryAltiVecVectorToken())
3663 return true;
3664 // Fall through.
David Blaikie15a430a2011-12-04 05:04:18 +00003665 case tok::kw_decltype: // decltype(T())::type
Douglas Gregor333489b2009-03-27 23:10:48 +00003666 case tok::kw_typename: // typename T::type
Chris Lattner020bab92009-01-04 23:41:41 +00003667 // Annotate typenames and C++ scope specifiers. If we get one, just
3668 // recurse to handle whatever we get.
3669 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00003670 return true;
3671 if (Tok.is(tok::identifier))
3672 return false;
Chad Rosierc1183952012-06-26 22:30:43 +00003673
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00003674 // If we're in Objective-C and we have an Objective-C class type followed
Chad Rosierc1183952012-06-26 22:30:43 +00003675 // by an identifier and then either ':' or ']', in a place where an
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00003676 // expression is permitted, then this is probably a class message send
3677 // missing the initial '['. In this case, we won't consider this to be
3678 // the start of a declaration.
Chad Rosierc1183952012-06-26 22:30:43 +00003679 if (DisambiguatingWithExpression &&
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00003680 isStartOfObjCClassMessageMissingOpenBracket())
3681 return false;
Chad Rosierc1183952012-06-26 22:30:43 +00003682
John McCall1f476a12010-02-26 08:45:28 +00003683 return isDeclarationSpecifier();
3684
Chris Lattner020bab92009-01-04 23:41:41 +00003685 case tok::coloncolon: // ::foo::bar
3686 if (NextToken().is(tok::kw_new) || // ::new
3687 NextToken().is(tok::kw_delete)) // ::delete
3688 return false;
Mike Stump11289f42009-09-09 15:08:12 +00003689
Chris Lattner020bab92009-01-04 23:41:41 +00003690 // Annotate typenames and C++ scope specifiers. If we get one, just
3691 // recurse to handle whatever we get.
3692 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00003693 return true;
3694 return isDeclarationSpecifier();
Mike Stump11289f42009-09-09 15:08:12 +00003695
Chris Lattneracd58a32006-08-06 17:24:14 +00003696 // storage-class-specifier
3697 case tok::kw_typedef:
3698 case tok::kw_extern:
Steve Naroff2050b0d2007-12-18 00:16:02 +00003699 case tok::kw___private_extern__:
Chris Lattneracd58a32006-08-06 17:24:14 +00003700 case tok::kw_static:
3701 case tok::kw_auto:
3702 case tok::kw_register:
3703 case tok::kw___thread:
Mike Stump11289f42009-09-09 15:08:12 +00003704
Douglas Gregor26701a42011-09-09 02:06:17 +00003705 // Modules
3706 case tok::kw___module_private__:
Chad Rosierc1183952012-06-26 22:30:43 +00003707
Chris Lattneracd58a32006-08-06 17:24:14 +00003708 // type-specifiers
3709 case tok::kw_short:
3710 case tok::kw_long:
Francois Pichet84133e42011-04-28 01:59:37 +00003711 case tok::kw___int64:
Richard Smithf016bbc2012-04-04 06:24:32 +00003712 case tok::kw___int128:
Chris Lattneracd58a32006-08-06 17:24:14 +00003713 case tok::kw_signed:
3714 case tok::kw_unsigned:
3715 case tok::kw__Complex:
3716 case tok::kw__Imaginary:
3717 case tok::kw_void:
3718 case tok::kw_char:
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +00003719 case tok::kw_wchar_t:
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00003720 case tok::kw_char16_t:
3721 case tok::kw_char32_t:
3722
Chris Lattneracd58a32006-08-06 17:24:14 +00003723 case tok::kw_int:
Anton Korobeynikovf0c267e2011-10-14 23:23:15 +00003724 case tok::kw_half:
Chris Lattneracd58a32006-08-06 17:24:14 +00003725 case tok::kw_float:
3726 case tok::kw_double:
Chris Lattnerbb31a422007-11-15 05:25:19 +00003727 case tok::kw_bool:
Chris Lattneracd58a32006-08-06 17:24:14 +00003728 case tok::kw__Bool:
3729 case tok::kw__Decimal32:
3730 case tok::kw__Decimal64:
3731 case tok::kw__Decimal128:
John Thompson22334602010-02-05 00:12:22 +00003732 case tok::kw___vector:
Mike Stump11289f42009-09-09 15:08:12 +00003733
Chris Lattner861a2262008-04-13 18:59:07 +00003734 // struct-or-union-specifier (C99) or class-specifier (C++)
3735 case tok::kw_class:
Chris Lattneracd58a32006-08-06 17:24:14 +00003736 case tok::kw_struct:
3737 case tok::kw_union:
3738 // enum-specifier
3739 case tok::kw_enum:
Mike Stump11289f42009-09-09 15:08:12 +00003740
Chris Lattneracd58a32006-08-06 17:24:14 +00003741 // type-qualifier
3742 case tok::kw_const:
3743 case tok::kw_volatile:
3744 case tok::kw_restrict:
Steve Naroffad373bd2007-07-31 12:34:36 +00003745
Chris Lattneracd58a32006-08-06 17:24:14 +00003746 // function-specifier
3747 case tok::kw_inline:
Douglas Gregor61956c42008-10-31 09:07:45 +00003748 case tok::kw_virtual:
3749 case tok::kw_explicit:
Chris Lattner7b20dc72007-08-09 16:40:21 +00003750
Peter Collingbourne3d9cbdc2011-04-15 00:35:57 +00003751 // static_assert-declaration
3752 case tok::kw__Static_assert:
3753
Chris Lattner599e47e2007-08-09 17:01:07 +00003754 // GNU typeof support.
3755 case tok::kw_typeof:
Mike Stump11289f42009-09-09 15:08:12 +00003756
Chris Lattner599e47e2007-08-09 17:01:07 +00003757 // GNU attributes.
Chris Lattner7b20dc72007-08-09 16:40:21 +00003758 case tok::kw___attribute:
Chris Lattneracd58a32006-08-06 17:24:14 +00003759 return true;
Mike Stump11289f42009-09-09 15:08:12 +00003760
Francois Pichete878cb62011-06-19 08:02:06 +00003761 // C++0x decltype.
David Blaikie15a430a2011-12-04 05:04:18 +00003762 case tok::annot_decltype:
Francois Pichete878cb62011-06-19 08:02:06 +00003763 return true;
3764
Benjamin Kramere56f3932011-12-23 17:00:35 +00003765 // C11 _Atomic()
Eli Friedman0dfb8892011-10-06 23:00:33 +00003766 case tok::kw__Atomic:
3767 return true;
3768
Chris Lattner8b2ec162008-07-26 03:38:44 +00003769 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
3770 case tok::less:
David Blaikiebbafb8a2012-03-11 07:00:24 +00003771 return getLangOpts().ObjC1;
Mike Stump11289f42009-09-09 15:08:12 +00003772
Douglas Gregor19b7acf2011-04-27 05:41:15 +00003773 // typedef-name
3774 case tok::annot_typename:
3775 return !DisambiguatingWithExpression ||
3776 !isStartOfObjCClassMessageMissingOpenBracket();
Chad Rosierc1183952012-06-26 22:30:43 +00003777
Steve Narofff192fab2009-01-06 19:34:12 +00003778 case tok::kw___declspec:
Steve Naroff44ac7772008-12-25 14:16:32 +00003779 case tok::kw___cdecl:
3780 case tok::kw___stdcall:
3781 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00003782 case tok::kw___thiscall:
Eli Friedman53339e02009-06-08 23:27:34 +00003783 case tok::kw___w64:
3784 case tok::kw___ptr64:
Francois Pichetf2fb4112011-08-25 00:36:46 +00003785 case tok::kw___ptr32:
Eli Friedman53339e02009-06-08 23:27:34 +00003786 case tok::kw___forceinline:
Dawn Perchik335e16b2010-09-03 01:29:35 +00003787 case tok::kw___pascal:
Francois Pichet17ed0202011-08-18 09:59:55 +00003788 case tok::kw___unaligned:
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003789
3790 case tok::kw___private:
3791 case tok::kw___local:
3792 case tok::kw___global:
3793 case tok::kw___constant:
3794 case tok::kw___read_only:
3795 case tok::kw___read_write:
3796 case tok::kw___write_only:
3797
Eli Friedman53339e02009-06-08 23:27:34 +00003798 return true;
Chris Lattneracd58a32006-08-06 17:24:14 +00003799 }
3800}
3801
Douglas Gregor9de54ea2010-01-13 17:31:36 +00003802bool Parser::isConstructorDeclarator() {
3803 TentativeParsingAction TPA(*this);
3804
3805 // Parse the C++ scope specifier.
3806 CXXScopeSpec SS;
Chad Rosierc1183952012-06-26 22:30:43 +00003807 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
Douglas Gregordf593fb2011-11-07 17:33:42 +00003808 /*EnteringContext=*/true)) {
John McCall1f476a12010-02-26 08:45:28 +00003809 TPA.Revert();
3810 return false;
3811 }
Douglas Gregor9de54ea2010-01-13 17:31:36 +00003812
3813 // Parse the constructor name.
3814 if (Tok.is(tok::identifier) || Tok.is(tok::annot_template_id)) {
3815 // We already know that we have a constructor name; just consume
3816 // the token.
3817 ConsumeToken();
3818 } else {
3819 TPA.Revert();
3820 return false;
3821 }
3822
Richard Smith43f340f2012-03-27 23:05:05 +00003823 // Current class name must be followed by a left parenthesis.
Douglas Gregor9de54ea2010-01-13 17:31:36 +00003824 if (Tok.isNot(tok::l_paren)) {
3825 TPA.Revert();
3826 return false;
3827 }
3828 ConsumeParen();
3829
Richard Smith43f340f2012-03-27 23:05:05 +00003830 // A right parenthesis, or ellipsis followed by a right parenthesis signals
3831 // that we have a constructor.
3832 if (Tok.is(tok::r_paren) ||
3833 (Tok.is(tok::ellipsis) && NextToken().is(tok::r_paren))) {
Douglas Gregor9de54ea2010-01-13 17:31:36 +00003834 TPA.Revert();
3835 return true;
3836 }
3837
3838 // If we need to, enter the specified scope.
3839 DeclaratorScopeObj DeclScopeObj(*this, SS);
Douglas Gregor0be31a22010-07-02 17:43:08 +00003840 if (SS.isSet() && Actions.ShouldEnterDeclaratorScope(getCurScope(), SS))
Douglas Gregor9de54ea2010-01-13 17:31:36 +00003841 DeclScopeObj.EnterDeclaratorScope();
3842
Francois Pichet79f3a872011-01-31 04:54:32 +00003843 // Optionally skip Microsoft attributes.
John McCall084e83d2011-03-24 11:26:52 +00003844 ParsedAttributes Attrs(AttrFactory);
Francois Pichet79f3a872011-01-31 04:54:32 +00003845 MaybeParseMicrosoftAttributes(Attrs);
3846
Douglas Gregor9de54ea2010-01-13 17:31:36 +00003847 // Check whether the next token(s) are part of a declaration
3848 // specifier, in which case we have the start of a parameter and,
3849 // therefore, we know that this is a constructor.
Richard Smithefd009d2012-03-27 00:56:56 +00003850 bool IsConstructor = false;
3851 if (isDeclarationSpecifier())
3852 IsConstructor = true;
3853 else if (Tok.is(tok::identifier) ||
3854 (Tok.is(tok::annot_cxxscope) && NextToken().is(tok::identifier))) {
3855 // We've seen "C ( X" or "C ( X::Y", but "X" / "X::Y" is not a type.
3856 // This might be a parenthesized member name, but is more likely to
3857 // be a constructor declaration with an invalid argument type. Keep
3858 // looking.
3859 if (Tok.is(tok::annot_cxxscope))
3860 ConsumeToken();
3861 ConsumeToken();
3862
3863 // If this is not a constructor, we must be parsing a declarator,
Richard Smith1453e312012-03-27 01:42:32 +00003864 // which must have one of the following syntactic forms (see the
3865 // grammar extract at the start of ParseDirectDeclarator):
Richard Smithefd009d2012-03-27 00:56:56 +00003866 switch (Tok.getKind()) {
3867 case tok::l_paren:
3868 // C(X ( int));
3869 case tok::l_square:
3870 // C(X [ 5]);
3871 // C(X [ [attribute]]);
3872 case tok::coloncolon:
3873 // C(X :: Y);
3874 // C(X :: *p);
3875 case tok::r_paren:
3876 // C(X )
3877 // Assume this isn't a constructor, rather than assuming it's a
3878 // constructor with an unnamed parameter of an ill-formed type.
3879 break;
3880
3881 default:
3882 IsConstructor = true;
3883 break;
3884 }
3885 }
3886
Douglas Gregor9de54ea2010-01-13 17:31:36 +00003887 TPA.Revert();
3888 return IsConstructor;
3889}
Chris Lattnerb9093cd2006-08-04 04:39:53 +00003890
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003891/// ParseTypeQualifierListOpt
Dawn Perchik335e16b2010-09-03 01:29:35 +00003892/// type-qualifier-list: [C99 6.7.5]
3893/// type-qualifier
Chad Rosierc1183952012-06-26 22:30:43 +00003894/// [vendor] attributes
Dawn Perchik335e16b2010-09-03 01:29:35 +00003895/// [ only if VendorAttributesAllowed=true ]
3896/// type-qualifier-list type-qualifier
Chad Rosierc1183952012-06-26 22:30:43 +00003897/// [vendor] type-qualifier-list attributes
Dawn Perchik335e16b2010-09-03 01:29:35 +00003898/// [ only if VendorAttributesAllowed=true ]
3899/// [C++0x] attribute-specifier[opt] is allowed before cv-qualifier-seq
3900/// [ only if CXX0XAttributesAllowed=true ]
3901/// Note: vendor can be GNU, MS, etc.
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003902///
Dawn Perchik335e16b2010-09-03 01:29:35 +00003903void Parser::ParseTypeQualifierListOpt(DeclSpec &DS,
3904 bool VendorAttributesAllowed,
Richard Smith3dff2512012-04-10 03:25:07 +00003905 bool CXX11AttributesAllowed) {
3906 if (getLangOpts().CPlusPlus0x && CXX11AttributesAllowed &&
Richard Smith7bdcc4a2012-04-10 01:32:12 +00003907 isCXX11AttributeSpecifier()) {
John McCall084e83d2011-03-24 11:26:52 +00003908 ParsedAttributesWithRange attrs(AttrFactory);
Richard Smith3dff2512012-04-10 03:25:07 +00003909 ParseCXX11Attributes(attrs);
Richard Smith7bdcc4a2012-04-10 01:32:12 +00003910 DS.takeAttributesFrom(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +00003911 }
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +00003912
3913 SourceLocation EndLoc;
3914
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003915 while (1) {
John McCall49bfce42009-08-03 20:12:06 +00003916 bool isInvalid = false;
Chris Lattnerd9c3c592006-08-05 06:26:47 +00003917 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00003918 unsigned DiagID = 0;
Chris Lattner60809f52006-11-28 05:18:46 +00003919 SourceLocation Loc = Tok.getLocation();
Chris Lattnerd9c3c592006-08-05 06:26:47 +00003920
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003921 switch (Tok.getKind()) {
Douglas Gregor28c78432010-08-27 17:35:51 +00003922 case tok::code_completion:
3923 Actions.CodeCompleteTypeQualifiers(DS);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00003924 return cutOffParsing();
Chad Rosierc1183952012-06-26 22:30:43 +00003925
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003926 case tok::kw_const:
John McCall49bfce42009-08-03 20:12:06 +00003927 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, DiagID,
Richard Smith7ac3c6a2012-07-24 20:24:58 +00003928 getLangOpts(), /*IsTypeSpec*/false);
Chris Lattnerd9c3c592006-08-05 06:26:47 +00003929 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003930 case tok::kw_volatile:
John McCall49bfce42009-08-03 20:12:06 +00003931 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
Richard Smith7ac3c6a2012-07-24 20:24:58 +00003932 getLangOpts(), /*IsTypeSpec*/false);
Chris Lattnerd9c3c592006-08-05 06:26:47 +00003933 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003934 case tok::kw_restrict:
John McCall49bfce42009-08-03 20:12:06 +00003935 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
Richard Smith7ac3c6a2012-07-24 20:24:58 +00003936 getLangOpts(), /*IsTypeSpec*/false);
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003937 break;
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003938
3939 // OpenCL qualifiers:
Chad Rosierc1183952012-06-26 22:30:43 +00003940 case tok::kw_private:
David Blaikiebbafb8a2012-03-11 07:00:24 +00003941 if (!getLangOpts().OpenCL)
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003942 goto DoneWithTypeQuals;
3943 case tok::kw___private:
3944 case tok::kw___global:
3945 case tok::kw___local:
3946 case tok::kw___constant:
3947 case tok::kw___read_only:
3948 case tok::kw___write_only:
3949 case tok::kw___read_write:
3950 ParseOpenCLQualifiers(DS);
3951 break;
3952
Eli Friedman53339e02009-06-08 23:27:34 +00003953 case tok::kw___w64:
Steve Narofff9c29d42008-12-25 14:41:26 +00003954 case tok::kw___ptr64:
Francois Pichetf2fb4112011-08-25 00:36:46 +00003955 case tok::kw___ptr32:
Steve Naroff44ac7772008-12-25 14:16:32 +00003956 case tok::kw___cdecl:
3957 case tok::kw___stdcall:
3958 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00003959 case tok::kw___thiscall:
Francois Pichet17ed0202011-08-18 09:59:55 +00003960 case tok::kw___unaligned:
Dawn Perchik335e16b2010-09-03 01:29:35 +00003961 if (VendorAttributesAllowed) {
John McCall53fa7142010-12-24 02:08:15 +00003962 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman53339e02009-06-08 23:27:34 +00003963 continue;
3964 }
3965 goto DoneWithTypeQuals;
Dawn Perchik335e16b2010-09-03 01:29:35 +00003966 case tok::kw___pascal:
3967 if (VendorAttributesAllowed) {
John McCall53fa7142010-12-24 02:08:15 +00003968 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik335e16b2010-09-03 01:29:35 +00003969 continue;
3970 }
3971 goto DoneWithTypeQuals;
Chris Lattnere37e2332006-08-15 04:50:22 +00003972 case tok::kw___attribute:
Dawn Perchik335e16b2010-09-03 01:29:35 +00003973 if (VendorAttributesAllowed) {
John McCall53fa7142010-12-24 02:08:15 +00003974 ParseGNUAttributes(DS.getAttributes());
Chris Lattnercf0bab22008-12-18 07:02:59 +00003975 continue; // do *not* consume the next token!
3976 }
3977 // otherwise, FALL THROUGH!
3978 default:
Steve Naroff44ac7772008-12-25 14:16:32 +00003979 DoneWithTypeQuals:
Chris Lattnercf0bab22008-12-18 07:02:59 +00003980 // If this is not a type-qualifier token, we're done reading type
3981 // qualifiers. First verify that DeclSpec's are consistent.
Douglas Gregore3e01a22009-04-01 22:41:11 +00003982 DS.Finish(Diags, PP);
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +00003983 if (EndLoc.isValid())
3984 DS.SetRangeEnd(EndLoc);
Chris Lattnercf0bab22008-12-18 07:02:59 +00003985 return;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003986 }
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00003987
Chris Lattnerd9c3c592006-08-05 06:26:47 +00003988 // If the specifier combination wasn't legal, issue a diagnostic.
3989 if (isInvalid) {
3990 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner6d29c102008-11-18 07:48:38 +00003991 Diag(Tok, DiagID) << PrevSpec;
Chris Lattnerd9c3c592006-08-05 06:26:47 +00003992 }
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +00003993 EndLoc = ConsumeToken();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003994 }
3995}
3996
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00003997
3998/// ParseDeclarator - Parse and verify a newly-initialized declarator.
3999///
4000void Parser::ParseDeclarator(Declarator &D) {
4001 /// This implements the 'declarator' production in the C grammar, then checks
4002 /// for well-formedness and issues diagnostics.
Sebastian Redlbd150f42008-11-21 19:14:01 +00004003 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00004004}
4005
Richard Smith0efa75c2012-03-29 01:16:42 +00004006static bool isPtrOperatorToken(tok::TokenKind Kind, const LangOptions &Lang) {
4007 if (Kind == tok::star || Kind == tok::caret)
4008 return true;
4009
4010 // We parse rvalue refs in C++03, because otherwise the errors are scary.
4011 if (!Lang.CPlusPlus)
4012 return false;
4013
4014 return Kind == tok::amp || Kind == tok::ampamp;
4015}
4016
Sebastian Redlbd150f42008-11-21 19:14:01 +00004017/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
4018/// is parsed by the function passed to it. Pass null, and the direct-declarator
4019/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregordbc5daf2008-11-07 20:08:42 +00004020/// ptr-operator production.
4021///
Richard Smith09f76ee2011-10-19 21:33:05 +00004022/// If the grammar of this construct is extended, matching changes must also be
Richard Smith1453e312012-03-27 01:42:32 +00004023/// made to TryParseDeclarator and MightBeDeclarator, and possibly to
4024/// isConstructorDeclarator.
Richard Smith09f76ee2011-10-19 21:33:05 +00004025///
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004026/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
4027/// [C] pointer[opt] direct-declarator
4028/// [C++] direct-declarator
4029/// [C++] ptr-operator declarator
Chris Lattner6c7416c2006-08-07 00:19:33 +00004030///
4031/// pointer: [C99 6.7.5]
4032/// '*' type-qualifier-list[opt]
4033/// '*' type-qualifier-list[opt] pointer
4034///
Douglas Gregordbc5daf2008-11-07 20:08:42 +00004035/// ptr-operator:
4036/// '*' cv-qualifier-seq[opt]
4037/// '&'
Sebastian Redled0f3b02009-03-15 22:02:01 +00004038/// [C++0x] '&&'
Douglas Gregordbc5daf2008-11-07 20:08:42 +00004039/// [GNU] '&' restrict[opt] attributes[opt]
Sebastian Redled0f3b02009-03-15 22:02:01 +00004040/// [GNU?] '&&' restrict[opt] attributes[opt]
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004041/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Sebastian Redlbd150f42008-11-21 19:14:01 +00004042void Parser::ParseDeclaratorInternal(Declarator &D,
4043 DirectDeclParseFunction DirectDeclParser) {
Douglas Gregor66a985d2009-08-26 14:27:30 +00004044 if (Diags.hasAllExtensionsSilenced())
4045 D.setExtension();
Chad Rosierc1183952012-06-26 22:30:43 +00004046
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004047 // C++ member pointers start with a '::' or a nested-name.
4048 // Member pointers get special handling, since there's no place for the
4049 // scope spec in the generic path below.
David Blaikiebbafb8a2012-03-11 07:00:24 +00004050 if (getLangOpts().CPlusPlus &&
Chris Lattner803802d2009-03-24 17:04:48 +00004051 (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) ||
4052 Tok.is(tok::annot_cxxscope))) {
Douglas Gregordf593fb2011-11-07 17:33:42 +00004053 bool EnteringContext = D.getContext() == Declarator::FileContext ||
4054 D.getContext() == Declarator::MemberContext;
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004055 CXXScopeSpec SS;
Douglas Gregordf593fb2011-11-07 17:33:42 +00004056 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), EnteringContext);
John McCall1f476a12010-02-26 08:45:28 +00004057
Jeffrey Yasskin4e150f82010-04-07 23:29:58 +00004058 if (SS.isNotEmpty()) {
Mike Stump11289f42009-09-09 15:08:12 +00004059 if (Tok.isNot(tok::star)) {
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004060 // The scope spec really belongs to the direct-declarator.
4061 D.getCXXScopeSpec() = SS;
4062 if (DirectDeclParser)
4063 (this->*DirectDeclParser)(D);
4064 return;
4065 }
4066
4067 SourceLocation Loc = ConsumeToken();
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004068 D.SetRangeEnd(Loc);
John McCall084e83d2011-03-24 11:26:52 +00004069 DeclSpec DS(AttrFactory);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004070 ParseTypeQualifierListOpt(DS);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004071 D.ExtendWithDeclSpec(DS);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004072
4073 // Recurse to parse whatever is left.
4074 ParseDeclaratorInternal(D, DirectDeclParser);
4075
4076 // Sema will have to catch (syntactically invalid) pointers into global
4077 // scope. It has to catch pointers into namespace scope anyway.
4078 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
John McCall084e83d2011-03-24 11:26:52 +00004079 Loc),
4080 DS.getAttributes(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004081 /* Don't replace range end. */SourceLocation());
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004082 return;
4083 }
4084 }
4085
4086 tok::TokenKind Kind = Tok.getKind();
Steve Naroffec33ed92008-08-27 16:04:49 +00004087 // Not a pointer, C++ reference, or block.
Richard Smith0efa75c2012-03-29 01:16:42 +00004088 if (!isPtrOperatorToken(Kind, getLangOpts())) {
Sebastian Redlbd150f42008-11-21 19:14:01 +00004089 if (DirectDeclParser)
4090 (this->*DirectDeclParser)(D);
Douglas Gregordbc5daf2008-11-07 20:08:42 +00004091 return;
4092 }
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004093
Sebastian Redled0f3b02009-03-15 22:02:01 +00004094 // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference,
4095 // '&&' -> rvalue reference
Sebastian Redl3b27be62009-03-23 00:00:23 +00004096 SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&.
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004097 D.SetRangeEnd(Loc);
Bill Wendling3708c182007-05-27 10:15:43 +00004098
Chris Lattner9eac9312009-03-27 04:18:06 +00004099 if (Kind == tok::star || Kind == tok::caret) {
Chris Lattner788404f2008-02-21 01:32:26 +00004100 // Is a pointer.
John McCall084e83d2011-03-24 11:26:52 +00004101 DeclSpec DS(AttrFactory);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004102
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004103 // FIXME: GNU attributes are not allowed here in a new-type-id.
Bill Wendling3708c182007-05-27 10:15:43 +00004104 ParseTypeQualifierListOpt(DS);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004105 D.ExtendWithDeclSpec(DS);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004106
Bill Wendling3708c182007-05-27 10:15:43 +00004107 // Recursively parse the declarator.
Sebastian Redlbd150f42008-11-21 19:14:01 +00004108 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroffec33ed92008-08-27 16:04:49 +00004109 if (Kind == tok::star)
4110 // Remember that we parsed a pointer type, and remember the type-quals.
4111 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
Chandler Carruthe71b378d2011-02-23 18:51:59 +00004112 DS.getConstSpecLoc(),
4113 DS.getVolatileSpecLoc(),
John McCall084e83d2011-03-24 11:26:52 +00004114 DS.getRestrictSpecLoc()),
4115 DS.getAttributes(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004116 SourceLocation());
Steve Naroffec33ed92008-08-27 16:04:49 +00004117 else
4118 // Remember that we parsed a Block type, and remember the type-quals.
Mike Stump11289f42009-09-09 15:08:12 +00004119 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
John McCall084e83d2011-03-24 11:26:52 +00004120 Loc),
4121 DS.getAttributes(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004122 SourceLocation());
Bill Wendling3708c182007-05-27 10:15:43 +00004123 } else {
4124 // Is a reference
John McCall084e83d2011-03-24 11:26:52 +00004125 DeclSpec DS(AttrFactory);
Bill Wendling93efb222007-06-02 23:28:54 +00004126
Sebastian Redl3b27be62009-03-23 00:00:23 +00004127 // Complain about rvalue references in C++03, but then go on and build
4128 // the declarator.
Richard Smith5d164bc2011-10-15 05:09:34 +00004129 if (Kind == tok::ampamp)
David Blaikiebbafb8a2012-03-11 07:00:24 +00004130 Diag(Loc, getLangOpts().CPlusPlus0x ?
Richard Smith5d164bc2011-10-15 05:09:34 +00004131 diag::warn_cxx98_compat_rvalue_reference :
4132 diag::ext_rvalue_reference);
Sebastian Redl3b27be62009-03-23 00:00:23 +00004133
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004134 // GNU-style and C++11 attributes are allowed here, as is restrict.
4135 ParseTypeQualifierListOpt(DS);
4136 D.ExtendWithDeclSpec(DS);
4137
Bill Wendling93efb222007-06-02 23:28:54 +00004138 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
4139 // cv-qualifiers are introduced through the use of a typedef or of a
4140 // template type argument, in which case the cv-qualifiers are ignored.
Bill Wendling93efb222007-06-02 23:28:54 +00004141 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
4142 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
4143 Diag(DS.getConstSpecLoc(),
Chris Lattner6d29c102008-11-18 07:48:38 +00004144 diag::err_invalid_reference_qualifier_application) << "const";
Bill Wendling93efb222007-06-02 23:28:54 +00004145 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
4146 Diag(DS.getVolatileSpecLoc(),
Chris Lattner6d29c102008-11-18 07:48:38 +00004147 diag::err_invalid_reference_qualifier_application) << "volatile";
Bill Wendling93efb222007-06-02 23:28:54 +00004148 }
Bill Wendling3708c182007-05-27 10:15:43 +00004149
4150 // Recursively parse the declarator.
Sebastian Redlbd150f42008-11-21 19:14:01 +00004151 ParseDeclaratorInternal(D, DirectDeclParser);
Bill Wendling3708c182007-05-27 10:15:43 +00004152
Douglas Gregor66583c52008-11-03 15:51:28 +00004153 if (D.getNumTypeObjects() > 0) {
4154 // C++ [dcl.ref]p4: There shall be no references to references.
4155 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
4156 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattnerebad6a22008-11-19 07:37:42 +00004157 if (const IdentifierInfo *II = D.getIdentifier())
4158 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
4159 << II;
4160 else
4161 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
4162 << "type name";
Douglas Gregor66583c52008-11-03 15:51:28 +00004163
Sebastian Redlbd150f42008-11-21 19:14:01 +00004164 // Once we've complained about the reference-to-reference, we
Douglas Gregor66583c52008-11-03 15:51:28 +00004165 // can go ahead and build the (technically ill-formed)
4166 // declarator: reference collapsing will take care of it.
4167 }
4168 }
4169
Bill Wendling3708c182007-05-27 10:15:43 +00004170 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner788404f2008-02-21 01:32:26 +00004171 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
Sebastian Redled0f3b02009-03-15 22:02:01 +00004172 Kind == tok::amp),
John McCall084e83d2011-03-24 11:26:52 +00004173 DS.getAttributes(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004174 SourceLocation());
Bill Wendling3708c182007-05-27 10:15:43 +00004175 }
Chris Lattner6c7416c2006-08-07 00:19:33 +00004176}
4177
Richard Smith0efa75c2012-03-29 01:16:42 +00004178static void diagnoseMisplacedEllipsis(Parser &P, Declarator &D,
4179 SourceLocation EllipsisLoc) {
4180 if (EllipsisLoc.isValid()) {
4181 FixItHint Insertion;
4182 if (!D.getEllipsisLoc().isValid()) {
4183 Insertion = FixItHint::CreateInsertion(D.getIdentifierLoc(), "...");
4184 D.setEllipsisLoc(EllipsisLoc);
4185 }
4186 P.Diag(EllipsisLoc, diag::err_misplaced_ellipsis_in_declaration)
4187 << FixItHint::CreateRemoval(EllipsisLoc) << Insertion << !D.hasName();
4188 }
4189}
4190
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004191/// ParseDirectDeclarator
4192/// direct-declarator: [C99 6.7.5]
Douglas Gregor831c93f2008-11-05 20:51:48 +00004193/// [C99] identifier
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004194/// '(' declarator ')'
4195/// [GNU] '(' attributes declarator ')'
Chris Lattnere8074e62006-08-06 18:30:15 +00004196/// [C90] direct-declarator '[' constant-expression[opt] ']'
4197/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
4198/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
4199/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
4200/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004201/// [C++11] direct-declarator '[' constant-expression[opt] ']'
4202/// attribute-specifier-seq[opt]
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004203/// direct-declarator '(' parameter-type-list ')'
4204/// direct-declarator '(' identifier-list[opt] ')'
4205/// [GNU] direct-declarator '(' parameter-forward-declarations
4206/// parameter-type-list[opt] ')'
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00004207/// [C++] direct-declarator '(' parameter-declaration-clause ')'
4208/// cv-qualifier-seq[opt] exception-specification[opt]
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004209/// [C++11] direct-declarator '(' parameter-declaration-clause ')'
4210/// attribute-specifier-seq[opt] cv-qualifier-seq[opt]
4211/// ref-qualifier[opt] exception-specification[opt]
Douglas Gregor61956c42008-10-31 09:07:45 +00004212/// [C++] declarator-id
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004213/// [C++11] declarator-id attribute-specifier-seq[opt]
Douglas Gregor831c93f2008-11-05 20:51:48 +00004214///
4215/// declarator-id: [C++ 8]
Douglas Gregor27b4c162010-12-23 22:44:42 +00004216/// '...'[opt] id-expression
Douglas Gregor831c93f2008-11-05 20:51:48 +00004217/// '::'[opt] nested-name-specifier[opt] type-name
4218///
4219/// id-expression: [C++ 5.1]
4220/// unqualified-id
Douglas Gregord90fd522009-09-25 21:45:23 +00004221/// qualified-id
Douglas Gregor831c93f2008-11-05 20:51:48 +00004222///
4223/// unqualified-id: [C++ 5.1]
Mike Stump11289f42009-09-09 15:08:12 +00004224/// identifier
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00004225/// operator-function-id
Douglas Gregord90fd522009-09-25 21:45:23 +00004226/// conversion-function-id
Mike Stump11289f42009-09-09 15:08:12 +00004227/// '~' class-name
Douglas Gregor7f741122009-02-25 19:37:18 +00004228/// template-id
Argyrios Kyrtzidise4426352008-11-07 22:02:30 +00004229///
Richard Smith1453e312012-03-27 01:42:32 +00004230/// Note, any additional constructs added here may need corresponding changes
4231/// in isConstructorDeclarator.
Chris Lattneracd58a32006-08-06 17:24:14 +00004232void Parser::ParseDirectDeclarator(Declarator &D) {
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00004233 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00004234
David Blaikiebbafb8a2012-03-11 07:00:24 +00004235 if (getLangOpts().CPlusPlus && D.mayHaveIdentifier()) {
Douglas Gregor7861a802009-11-03 01:35:08 +00004236 // ParseDeclaratorInternal might already have parsed the scope.
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00004237 if (D.getCXXScopeSpec().isEmpty()) {
Douglas Gregordf593fb2011-11-07 17:33:42 +00004238 bool EnteringContext = D.getContext() == Declarator::FileContext ||
4239 D.getContext() == Declarator::MemberContext;
Chad Rosierc1183952012-06-26 22:30:43 +00004240 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec(), ParsedType(),
Douglas Gregordf593fb2011-11-07 17:33:42 +00004241 EnteringContext);
John McCall1f476a12010-02-26 08:45:28 +00004242 }
4243
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00004244 if (D.getCXXScopeSpec().isValid()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00004245 if (Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
John McCall2b058ef2009-12-11 20:04:54 +00004246 // Change the declaration context for name lookup, until this function
4247 // is exited (and the declarator has been parsed).
4248 DeclScopeObj.EnterDeclaratorScope();
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00004249 }
4250
Douglas Gregor27b4c162010-12-23 22:44:42 +00004251 // C++0x [dcl.fct]p14:
4252 // There is a syntactic ambiguity when an ellipsis occurs at the end
Chad Rosierc1183952012-06-26 22:30:43 +00004253 // of a parameter-declaration-clause without a preceding comma. In
4254 // this case, the ellipsis is parsed as part of the
4255 // abstract-declarator if the type of the parameter names a template
Douglas Gregor27b4c162010-12-23 22:44:42 +00004256 // parameter pack that has not been expanded; otherwise, it is parsed
4257 // as part of the parameter-declaration-clause.
Richard Smith0efa75c2012-03-29 01:16:42 +00004258 if (Tok.is(tok::ellipsis) && D.getCXXScopeSpec().isEmpty() &&
Douglas Gregor27b4c162010-12-23 22:44:42 +00004259 !((D.getContext() == Declarator::PrototypeContext ||
4260 D.getContext() == Declarator::BlockLiteralContext) &&
Douglas Gregor27b4c162010-12-23 22:44:42 +00004261 NextToken().is(tok::r_paren) &&
Richard Smith0efa75c2012-03-29 01:16:42 +00004262 !Actions.containsUnexpandedParameterPacks(D))) {
4263 SourceLocation EllipsisLoc = ConsumeToken();
4264 if (isPtrOperatorToken(Tok.getKind(), getLangOpts())) {
4265 // The ellipsis was put in the wrong place. Recover, and explain to
4266 // the user what they should have done.
4267 ParseDeclarator(D);
4268 diagnoseMisplacedEllipsis(*this, D, EllipsisLoc);
4269 return;
4270 } else
4271 D.setEllipsisLoc(EllipsisLoc);
4272
4273 // The ellipsis can't be followed by a parenthesized declarator. We
4274 // check for that in ParseParenDeclarator, after we have disambiguated
4275 // the l_paren token.
4276 }
4277
Douglas Gregor7861a802009-11-03 01:35:08 +00004278 if (Tok.is(tok::identifier) || Tok.is(tok::kw_operator) ||
4279 Tok.is(tok::annot_template_id) || Tok.is(tok::tilde)) {
4280 // We found something that indicates the start of an unqualified-id.
4281 // Parse that unqualified-id.
John McCall84821e72010-04-13 06:39:49 +00004282 bool AllowConstructorName;
4283 if (D.getDeclSpec().hasTypeSpecifier())
4284 AllowConstructorName = false;
4285 else if (D.getCXXScopeSpec().isSet())
4286 AllowConstructorName =
4287 (D.getContext() == Declarator::FileContext ||
4288 (D.getContext() == Declarator::MemberContext &&
4289 D.getDeclSpec().isFriendSpecified()));
4290 else
4291 AllowConstructorName = (D.getContext() == Declarator::MemberContext);
4292
Abramo Bagnara7945c982012-01-27 09:46:47 +00004293 SourceLocation TemplateKWLoc;
Chad Rosierc1183952012-06-26 22:30:43 +00004294 if (ParseUnqualifiedId(D.getCXXScopeSpec(),
4295 /*EnteringContext=*/true,
4296 /*AllowDestructorName=*/true,
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004297 AllowConstructorName,
John McCallba7bf592010-08-24 05:47:05 +00004298 ParsedType(),
Abramo Bagnara7945c982012-01-27 09:46:47 +00004299 TemplateKWLoc,
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00004300 D.getName()) ||
4301 // Once we're past the identifier, if the scope was bad, mark the
4302 // whole declarator bad.
4303 D.getCXXScopeSpec().isInvalid()) {
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00004304 D.SetIdentifier(0, Tok.getLocation());
4305 D.setInvalidType(true);
Douglas Gregor7861a802009-11-03 01:35:08 +00004306 } else {
4307 // Parsed the unqualified-id; update range information and move along.
4308 if (D.getSourceRange().getBegin().isInvalid())
4309 D.SetRangeBegin(D.getName().getSourceRange().getBegin());
4310 D.SetRangeEnd(D.getName().getSourceRange().getEnd());
Douglas Gregordbc5daf2008-11-07 20:08:42 +00004311 }
Douglas Gregor7861a802009-11-03 01:35:08 +00004312 goto PastIdentifier;
Douglas Gregor11d0c4c2008-11-06 22:13:31 +00004313 }
Douglas Gregor7861a802009-11-03 01:35:08 +00004314 } else if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00004315 assert(!getLangOpts().CPlusPlus &&
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00004316 "There's a C++-specific check for tok::identifier above");
4317 assert(Tok.getIdentifierInfo() && "Not an identifier?");
4318 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
4319 ConsumeToken();
Douglas Gregor7861a802009-11-03 01:35:08 +00004320 goto PastIdentifier;
4321 }
Richard Smith0efa75c2012-03-29 01:16:42 +00004322
Douglas Gregor7861a802009-11-03 01:35:08 +00004323 if (Tok.is(tok::l_paren)) {
Chris Lattneracd58a32006-08-06 17:24:14 +00004324 // direct-declarator: '(' declarator ')'
Chris Lattnere37e2332006-08-15 04:50:22 +00004325 // direct-declarator: '(' attributes declarator ')'
Chris Lattneracd58a32006-08-06 17:24:14 +00004326 // Example: 'char (*X)' or 'int (*XX)(void)'
4327 ParseParenDeclarator(D);
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004328
4329 // If the declarator was parenthesized, we entered the declarator
4330 // scope when parsing the parenthesized declarator, then exited
4331 // the scope already. Re-enter the scope, if we need to.
4332 if (D.getCXXScopeSpec().isSet()) {
Fariborz Jahanian358acd52010-08-17 23:50:37 +00004333 // If there was an error parsing parenthesized declarator, declarator
Richard Smith0efa75c2012-03-29 01:16:42 +00004334 // scope may have been entered before. Don't do it again.
Fariborz Jahanian358acd52010-08-17 23:50:37 +00004335 if (!D.isInvalidType() &&
4336 Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004337 // Change the declaration context for name lookup, until this function
4338 // is exited (and the declarator has been parsed).
Fariborz Jahanian358acd52010-08-17 23:50:37 +00004339 DeclScopeObj.EnterDeclaratorScope();
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004340 }
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00004341 } else if (D.mayOmitIdentifier()) {
Chris Lattneracd58a32006-08-06 17:24:14 +00004342 // This could be something simple like "int" (in which case the declarator
4343 // portion is empty), if an abstract-declarator is allowed.
4344 D.SetIdentifier(0, Tok.getLocation());
4345 } else {
David Blaikie5d577a22012-06-29 22:03:56 +00004346 if (Tok.getKind() == tok::annot_pragma_parser_crash)
4347 *(volatile int*) 0x11 = 0;
Douglas Gregord9f92e22009-03-06 23:28:18 +00004348 if (D.getContext() == Declarator::MemberContext)
4349 Diag(Tok, diag::err_expected_member_name_or_semi)
4350 << D.getDeclSpec().getSourceRange();
David Blaikiebbafb8a2012-03-11 07:00:24 +00004351 else if (getLangOpts().CPlusPlus)
4352 Diag(Tok, diag::err_expected_unqualified_id) << getLangOpts().CPlusPlus;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00004353 else
Chris Lattner6d29c102008-11-18 07:48:38 +00004354 Diag(Tok, diag::err_expected_ident_lparen);
Chris Lattnereec40f92006-08-06 21:55:29 +00004355 D.SetIdentifier(0, Tok.getLocation());
Chris Lattner8c5dd732008-11-11 06:13:16 +00004356 D.setInvalidType(true);
Chris Lattneracd58a32006-08-06 17:24:14 +00004357 }
Mike Stump11289f42009-09-09 15:08:12 +00004358
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00004359 PastIdentifier:
Chris Lattneracd58a32006-08-06 17:24:14 +00004360 assert(D.isPastIdentifier() &&
4361 "Haven't past the location of the identifier yet?");
Mike Stump11289f42009-09-09 15:08:12 +00004362
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004363 // Don't parse attributes unless we have parsed an unparenthesized name.
4364 if (D.hasName() && !D.getNumTypeObjects())
John McCall53fa7142010-12-24 02:08:15 +00004365 MaybeParseCXX0XAttributes(D);
Alexis Hunt96d5c762009-11-21 08:43:09 +00004366
Chris Lattneracd58a32006-08-06 17:24:14 +00004367 while (1) {
Chris Lattner76c72282007-10-09 17:33:22 +00004368 if (Tok.is(tok::l_paren)) {
David Blaikie15a430a2011-12-04 05:04:18 +00004369 // Enter function-declaration scope, limiting any declarators to the
4370 // function prototype scope, including parameter declarators.
4371 ParseScope PrototypeScope(this,
4372 Scope::FunctionPrototypeScope|Scope::DeclScope);
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00004373 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
4374 // In such a case, check if we actually have a function declarator; if it
4375 // is not, the declarator has been fully parsed.
Richard Smith943c4402012-07-30 21:30:52 +00004376 bool IsAmbiguous = false;
4377 if (getLangOpts().CPlusPlus && D.mayBeFollowedByCXXDirectInit() &&
4378 !isCXXFunctionDeclarator(&IsAmbiguous))
4379 break;
John McCall084e83d2011-03-24 11:26:52 +00004380 ParsedAttributes attrs(AttrFactory);
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004381 BalancedDelimiterTracker T(*this, tok::l_paren);
4382 T.consumeOpen();
Richard Smith943c4402012-07-30 21:30:52 +00004383 ParseFunctionDeclarator(D, attrs, T, IsAmbiguous);
David Blaikie15a430a2011-12-04 05:04:18 +00004384 PrototypeScope.Exit();
Chris Lattner76c72282007-10-09 17:33:22 +00004385 } else if (Tok.is(tok::l_square)) {
Chris Lattnere8074e62006-08-06 18:30:15 +00004386 ParseBracketDeclarator(D);
Chris Lattneracd58a32006-08-06 17:24:14 +00004387 } else {
4388 break;
4389 }
4390 }
Chad Rosierc1183952012-06-26 22:30:43 +00004391}
Chris Lattneracd58a32006-08-06 17:24:14 +00004392
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004393/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
4394/// only called before the identifier, so these are most likely just grouping
Mike Stump11289f42009-09-09 15:08:12 +00004395/// parens for precedence. If we find that these are actually function
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004396/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
4397///
4398/// direct-declarator:
4399/// '(' declarator ')'
4400/// [GNU] '(' attributes declarator ')'
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00004401/// direct-declarator '(' parameter-type-list ')'
4402/// direct-declarator '(' identifier-list[opt] ')'
4403/// [GNU] direct-declarator '(' parameter-forward-declarations
4404/// parameter-type-list[opt] ')'
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004405///
4406void Parser::ParseParenDeclarator(Declarator &D) {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004407 BalancedDelimiterTracker T(*this, tok::l_paren);
4408 T.consumeOpen();
4409
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004410 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
Mike Stump11289f42009-09-09 15:08:12 +00004411
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00004412 // Eat any attributes before we look at whether this is a grouping or function
4413 // declarator paren. If this is a grouping paren, the attribute applies to
4414 // the type being built up, for example:
4415 // int (__attribute__(()) *x)(long y)
4416 // If this ends up not being a grouping paren, the attribute applies to the
4417 // first argument, for example:
4418 // int (__attribute__(()) int x)
4419 // In either case, we need to eat any attributes to be able to determine what
4420 // sort of paren this is.
4421 //
John McCall084e83d2011-03-24 11:26:52 +00004422 ParsedAttributes attrs(AttrFactory);
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00004423 bool RequiresArg = false;
4424 if (Tok.is(tok::kw___attribute)) {
John McCall53fa7142010-12-24 02:08:15 +00004425 ParseGNUAttributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +00004426
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00004427 // We require that the argument list (if this is a non-grouping paren) be
4428 // present even if the attribute list was empty.
4429 RequiresArg = true;
4430 }
Steve Naroff44ac7772008-12-25 14:16:32 +00004431 // Eat any Microsoft extensions.
Eli Friedman53339e02009-06-08 23:27:34 +00004432 if (Tok.is(tok::kw___cdecl) || Tok.is(tok::kw___stdcall) ||
Douglas Gregora941dca2010-05-18 16:57:00 +00004433 Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___fastcall) ||
Francois Pichet17ed0202011-08-18 09:59:55 +00004434 Tok.is(tok::kw___w64) || Tok.is(tok::kw___ptr64) ||
Francois Pichetf2fb4112011-08-25 00:36:46 +00004435 Tok.is(tok::kw___ptr32) || Tok.is(tok::kw___unaligned)) {
John McCall53fa7142010-12-24 02:08:15 +00004436 ParseMicrosoftTypeAttributes(attrs);
Eli Friedman53339e02009-06-08 23:27:34 +00004437 }
Dawn Perchik335e16b2010-09-03 01:29:35 +00004438 // Eat any Borland extensions.
Ted Kremenek5eec2b02010-11-10 05:59:39 +00004439 if (Tok.is(tok::kw___pascal))
John McCall53fa7142010-12-24 02:08:15 +00004440 ParseBorlandTypeAttributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +00004441
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004442 // If we haven't past the identifier yet (or where the identifier would be
4443 // stored, if this is an abstract declarator), then this is probably just
4444 // grouping parens. However, if this could be an abstract-declarator, then
4445 // this could also be the start of function arguments (consider 'void()').
4446 bool isGrouping;
Mike Stump11289f42009-09-09 15:08:12 +00004447
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004448 if (!D.mayOmitIdentifier()) {
4449 // If this can't be an abstract-declarator, this *must* be a grouping
4450 // paren, because we haven't seen the identifier yet.
4451 isGrouping = true;
4452 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Richard Smith43f340f2012-03-27 23:05:05 +00004453 (getLangOpts().CPlusPlus && Tok.is(tok::ellipsis) &&
4454 NextToken().is(tok::r_paren)) || // C++ int(...)
Richard Smith2620cd92012-04-11 04:01:28 +00004455 isDeclarationSpecifier() || // 'int(int)' is a function.
4456 isCXX11AttributeSpecifier()) { // 'int([[]]int)' is a function.
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004457 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
4458 // considered to be a type, not a K&R identifier-list.
4459 isGrouping = false;
4460 } else {
4461 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
4462 isGrouping = true;
4463 }
Mike Stump11289f42009-09-09 15:08:12 +00004464
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004465 // If this is a grouping paren, handle:
4466 // direct-declarator: '(' declarator ')'
4467 // direct-declarator: '(' attributes declarator ')'
4468 if (isGrouping) {
Richard Smith0efa75c2012-03-29 01:16:42 +00004469 SourceLocation EllipsisLoc = D.getEllipsisLoc();
4470 D.setEllipsisLoc(SourceLocation());
4471
Argyrios Kyrtzidis8ae36842008-10-07 10:21:57 +00004472 bool hadGroupingParens = D.hasGroupingParens();
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00004473 D.setGroupingParens(true);
Sebastian Redlbd150f42008-11-21 19:14:01 +00004474 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004475 // Match the ')'.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004476 T.consumeClose();
Chad Rosierc1183952012-06-26 22:30:43 +00004477 D.AddTypeInfo(DeclaratorChunk::getParen(T.getOpenLocation(),
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004478 T.getCloseLocation()),
4479 attrs, T.getCloseLocation());
Argyrios Kyrtzidis8ae36842008-10-07 10:21:57 +00004480
4481 D.setGroupingParens(hadGroupingParens);
Richard Smith0efa75c2012-03-29 01:16:42 +00004482
4483 // An ellipsis cannot be placed outside parentheses.
4484 if (EllipsisLoc.isValid())
4485 diagnoseMisplacedEllipsis(*this, D, EllipsisLoc);
4486
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004487 return;
4488 }
Mike Stump11289f42009-09-09 15:08:12 +00004489
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004490 // Okay, if this wasn't a grouping paren, it must be the start of a function
4491 // argument list. Recognize that this declarator will never have an
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00004492 // identifier (and remember where it would have been), then call into
4493 // ParseFunctionDeclarator to handle of argument list.
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004494 D.SetIdentifier(0, Tok.getLocation());
4495
David Blaikie15a430a2011-12-04 05:04:18 +00004496 // Enter function-declaration scope, limiting any declarators to the
4497 // function prototype scope, including parameter declarators.
4498 ParseScope PrototypeScope(this,
4499 Scope::FunctionPrototypeScope|Scope::DeclScope);
Richard Smith943c4402012-07-30 21:30:52 +00004500 ParseFunctionDeclarator(D, attrs, T, false, RequiresArg);
David Blaikie15a430a2011-12-04 05:04:18 +00004501 PrototypeScope.Exit();
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004502}
4503
4504/// ParseFunctionDeclarator - We are after the identifier and have parsed the
4505/// declarator D up to a paren, which indicates that we are parsing function
4506/// arguments.
Chris Lattneracd58a32006-08-06 17:24:14 +00004507///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004508/// If FirstArgAttrs is non-null, then the caller parsed those arguments
4509/// immediately after the open paren - they should be considered to be the
4510/// first argument of a parameter.
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00004511///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004512/// If RequiresArg is true, then the first argument of the function is required
4513/// to be present and required to not be an identifier list.
Douglas Gregor9e66af42011-07-05 16:44:18 +00004514///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004515/// For C++, after the parameter-list, it also parses the cv-qualifier-seq[opt],
4516/// (C++11) ref-qualifier[opt], exception-specification[opt],
4517/// (C++11) attribute-specifier-seq[opt], and (C++11) trailing-return-type[opt].
4518///
4519/// [C++11] exception-specification:
Douglas Gregor9e66af42011-07-05 16:44:18 +00004520/// dynamic-exception-specification
4521/// noexcept-specification
4522///
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004523void Parser::ParseFunctionDeclarator(Declarator &D,
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004524 ParsedAttributes &FirstArgAttrs,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004525 BalancedDelimiterTracker &Tracker,
Richard Smith943c4402012-07-30 21:30:52 +00004526 bool IsAmbiguous,
Douglas Gregor9e66af42011-07-05 16:44:18 +00004527 bool RequiresArg) {
Chad Rosierc1183952012-06-26 22:30:43 +00004528 assert(getCurScope()->isFunctionPrototypeScope() &&
David Blaikie15a430a2011-12-04 05:04:18 +00004529 "Should call from a Function scope");
Douglas Gregor9e66af42011-07-05 16:44:18 +00004530 // lparen is already consumed!
4531 assert(D.isPastIdentifier() && "Should not call before identifier!");
4532
4533 // This should be true when the function has typed arguments.
4534 // Otherwise, it is treated as a K&R-style function.
4535 bool HasProto = false;
4536 // Build up an array of information about the parsed arguments.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004537 SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Douglas Gregor9e66af42011-07-05 16:44:18 +00004538 // Remember where we see an ellipsis, if any.
4539 SourceLocation EllipsisLoc;
4540
4541 DeclSpec DS(AttrFactory);
4542 bool RefQualifierIsLValueRef = true;
4543 SourceLocation RefQualifierLoc;
Douglas Gregore248eea2011-10-19 06:04:55 +00004544 SourceLocation ConstQualifierLoc;
4545 SourceLocation VolatileQualifierLoc;
Douglas Gregor9e66af42011-07-05 16:44:18 +00004546 ExceptionSpecificationType ESpecType = EST_None;
4547 SourceRange ESpecRange;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004548 SmallVector<ParsedType, 2> DynamicExceptions;
4549 SmallVector<SourceRange, 2> DynamicExceptionRanges;
Douglas Gregor9e66af42011-07-05 16:44:18 +00004550 ExprResult NoexceptExpr;
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004551 ParsedAttributes FnAttrs(AttrFactory);
Richard Smith700537c2012-06-12 01:51:59 +00004552 TypeResult TrailingReturnType;
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004553
James Molloy6f8780b2012-02-29 10:24:19 +00004554 Actions.ActOnStartFunctionDeclarator();
4555
Douglas Gregor9e66af42011-07-05 16:44:18 +00004556 SourceLocation EndLoc;
Douglas Gregor9e66af42011-07-05 16:44:18 +00004557 if (isFunctionDeclaratorIdentifierList()) {
4558 if (RequiresArg)
4559 Diag(Tok, diag::err_argument_required_after_attribute);
4560
4561 ParseFunctionDeclaratorIdentifierList(D, ParamInfo);
4562
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004563 Tracker.consumeClose();
4564 EndLoc = Tracker.getCloseLocation();
Douglas Gregor9e66af42011-07-05 16:44:18 +00004565 } else {
Douglas Gregor9e66af42011-07-05 16:44:18 +00004566 if (Tok.isNot(tok::r_paren))
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004567 ParseParameterDeclarationClause(D, FirstArgAttrs, ParamInfo, EllipsisLoc);
Douglas Gregor9e66af42011-07-05 16:44:18 +00004568 else if (RequiresArg)
4569 Diag(Tok, diag::err_argument_required_after_attribute);
4570
David Blaikiebbafb8a2012-03-11 07:00:24 +00004571 HasProto = ParamInfo.size() || getLangOpts().CPlusPlus;
Douglas Gregor9e66af42011-07-05 16:44:18 +00004572
4573 // If we have the closing ')', eat it.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004574 Tracker.consumeClose();
4575 EndLoc = Tracker.getCloseLocation();
Douglas Gregor9e66af42011-07-05 16:44:18 +00004576
David Blaikiebbafb8a2012-03-11 07:00:24 +00004577 if (getLangOpts().CPlusPlus) {
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004578 // FIXME: Accept these components in any order, and produce fixits to
4579 // correct the order if the user gets it wrong. Ideally we should deal
4580 // with the virt-specifier-seq and pure-specifier in the same way.
Douglas Gregor9e66af42011-07-05 16:44:18 +00004581
4582 // Parse cv-qualifier-seq[opt].
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004583 ParseTypeQualifierListOpt(DS, false /*no attributes*/, false);
4584 if (!DS.getSourceRange().getEnd().isInvalid()) {
4585 EndLoc = DS.getSourceRange().getEnd();
4586 ConstQualifierLoc = DS.getConstSpecLoc();
4587 VolatileQualifierLoc = DS.getVolatileSpecLoc();
4588 }
Douglas Gregor9e66af42011-07-05 16:44:18 +00004589
4590 // Parse ref-qualifier[opt].
4591 if (Tok.is(tok::amp) || Tok.is(tok::ampamp)) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00004592 Diag(Tok, getLangOpts().CPlusPlus0x ?
Richard Smith5d164bc2011-10-15 05:09:34 +00004593 diag::warn_cxx98_compat_ref_qualifier :
4594 diag::ext_ref_qualifier);
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004595
Douglas Gregor9e66af42011-07-05 16:44:18 +00004596 RefQualifierIsLValueRef = Tok.is(tok::amp);
4597 RefQualifierLoc = ConsumeToken();
4598 EndLoc = RefQualifierLoc;
4599 }
4600
Douglas Gregor3024f072012-04-16 07:05:22 +00004601 // C++11 [expr.prim.general]p3:
Chad Rosierc1183952012-06-26 22:30:43 +00004602 // If a declaration declares a member function or member function
4603 // template of a class X, the expression this is a prvalue of type
Douglas Gregor3024f072012-04-16 07:05:22 +00004604 // "pointer to cv-qualifier-seq X" between the optional cv-qualifer-seq
Chad Rosierc1183952012-06-26 22:30:43 +00004605 // and the end of the function-definition, member-declarator, or
Douglas Gregor3024f072012-04-16 07:05:22 +00004606 // declarator.
Chad Rosierc1183952012-06-26 22:30:43 +00004607 bool IsCXX11MemberFunction =
Douglas Gregor3024f072012-04-16 07:05:22 +00004608 getLangOpts().CPlusPlus0x &&
4609 (D.getContext() == Declarator::MemberContext ||
4610 (D.getContext() == Declarator::FileContext &&
Chad Rosierc1183952012-06-26 22:30:43 +00004611 D.getCXXScopeSpec().isValid() &&
Douglas Gregor3024f072012-04-16 07:05:22 +00004612 Actions.CurContext->isRecord()));
4613 Sema::CXXThisScopeRAII ThisScope(Actions,
4614 dyn_cast<CXXRecordDecl>(Actions.CurContext),
4615 DS.getTypeQualifiers(),
4616 IsCXX11MemberFunction);
Richard Smith2331bbf2012-05-02 22:22:32 +00004617
Douglas Gregor9e66af42011-07-05 16:44:18 +00004618 // Parse exception-specification[opt].
Richard Smith2331bbf2012-05-02 22:22:32 +00004619 ESpecType = tryParseExceptionSpecification(ESpecRange,
Douglas Gregor433e0532012-04-16 18:27:27 +00004620 DynamicExceptions,
4621 DynamicExceptionRanges,
Richard Smith2331bbf2012-05-02 22:22:32 +00004622 NoexceptExpr);
Douglas Gregor9e66af42011-07-05 16:44:18 +00004623 if (ESpecType != EST_None)
4624 EndLoc = ESpecRange.getEnd();
4625
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004626 // Parse attribute-specifier-seq[opt]. Per DR 979 and DR 1297, this goes
4627 // after the exception-specification.
4628 MaybeParseCXX0XAttributes(FnAttrs);
4629
Douglas Gregor9e66af42011-07-05 16:44:18 +00004630 // Parse trailing-return-type[opt].
David Blaikiebbafb8a2012-03-11 07:00:24 +00004631 if (getLangOpts().CPlusPlus0x && Tok.is(tok::arrow)) {
Richard Smith5d164bc2011-10-15 05:09:34 +00004632 Diag(Tok, diag::warn_cxx98_compat_trailing_return_type);
Douglas Gregordb0b9f12011-08-04 15:30:47 +00004633 SourceRange Range;
Richard Smith700537c2012-06-12 01:51:59 +00004634 TrailingReturnType = ParseTrailingReturnType(Range);
Douglas Gregordb0b9f12011-08-04 15:30:47 +00004635 if (Range.getEnd().isValid())
4636 EndLoc = Range.getEnd();
Douglas Gregor9e66af42011-07-05 16:44:18 +00004637 }
4638 }
Douglas Gregor9e66af42011-07-05 16:44:18 +00004639 }
4640
4641 // Remember that we parsed a function type, and remember the attributes.
4642 D.AddTypeInfo(DeclaratorChunk::getFunction(HasProto,
4643 /*isVariadic=*/EllipsisLoc.isValid(),
Richard Smith943c4402012-07-30 21:30:52 +00004644 IsAmbiguous, EllipsisLoc,
Douglas Gregor9e66af42011-07-05 16:44:18 +00004645 ParamInfo.data(), ParamInfo.size(),
4646 DS.getTypeQualifiers(),
4647 RefQualifierIsLValueRef,
Douglas Gregore248eea2011-10-19 06:04:55 +00004648 RefQualifierLoc, ConstQualifierLoc,
4649 VolatileQualifierLoc,
Douglas Gregorad69e652011-07-13 21:47:47 +00004650 /*MutableLoc=*/SourceLocation(),
Douglas Gregor9e66af42011-07-05 16:44:18 +00004651 ESpecType, ESpecRange.getBegin(),
4652 DynamicExceptions.data(),
4653 DynamicExceptionRanges.data(),
4654 DynamicExceptions.size(),
4655 NoexceptExpr.isUsable() ?
4656 NoexceptExpr.get() : 0,
Chad Rosierc1183952012-06-26 22:30:43 +00004657 Tracker.getOpenLocation(),
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004658 EndLoc, D,
Douglas Gregor9e66af42011-07-05 16:44:18 +00004659 TrailingReturnType),
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004660 FnAttrs, EndLoc);
James Molloy6f8780b2012-02-29 10:24:19 +00004661
4662 Actions.ActOnEndFunctionDeclarator();
Douglas Gregor9e66af42011-07-05 16:44:18 +00004663}
4664
4665/// isFunctionDeclaratorIdentifierList - This parameter list may have an
4666/// identifier list form for a K&R-style function: void foo(a,b,c)
4667///
4668/// Note that identifier-lists are only allowed for normal declarators, not for
4669/// abstract-declarators.
4670bool Parser::isFunctionDeclaratorIdentifierList() {
David Blaikiebbafb8a2012-03-11 07:00:24 +00004671 return !getLangOpts().CPlusPlus
Douglas Gregor9e66af42011-07-05 16:44:18 +00004672 && Tok.is(tok::identifier)
4673 && !TryAltiVecVectorToken()
4674 // K&R identifier lists can't have typedefs as identifiers, per C99
4675 // 6.7.5.3p11.
4676 && (TryAnnotateTypeOrScopeToken() || !Tok.is(tok::annot_typename))
4677 // Identifier lists follow a really simple grammar: the identifiers can
4678 // be followed *only* by a ", identifier" or ")". However, K&R
4679 // identifier lists are really rare in the brave new modern world, and
4680 // it is very common for someone to typo a type in a non-K&R style
4681 // list. If we are presented with something like: "void foo(intptr x,
4682 // float y)", we don't want to start parsing the function declarator as
4683 // though it is a K&R style declarator just because intptr is an
4684 // invalid type.
4685 //
4686 // To handle this, we check to see if the token after the first
4687 // identifier is a "," or ")". Only then do we parse it as an
4688 // identifier list.
4689 && (NextToken().is(tok::comma) || NextToken().is(tok::r_paren));
4690}
4691
4692/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
4693/// we found a K&R-style identifier list instead of a typed parameter list.
4694///
4695/// After returning, ParamInfo will hold the parsed parameters.
4696///
4697/// identifier-list: [C99 6.7.5]
4698/// identifier
4699/// identifier-list ',' identifier
4700///
4701void Parser::ParseFunctionDeclaratorIdentifierList(
4702 Declarator &D,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004703 SmallVector<DeclaratorChunk::ParamInfo, 16> &ParamInfo) {
Douglas Gregor9e66af42011-07-05 16:44:18 +00004704 // If there was no identifier specified for the declarator, either we are in
4705 // an abstract-declarator, or we are in a parameter declarator which was found
4706 // to be abstract. In abstract-declarators, identifier lists are not valid:
4707 // diagnose this.
4708 if (!D.getIdentifier())
4709 Diag(Tok, diag::ext_ident_list_in_param);
4710
4711 // Maintain an efficient lookup of params we have seen so far.
4712 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
4713
4714 while (1) {
4715 // If this isn't an identifier, report the error and skip until ')'.
4716 if (Tok.isNot(tok::identifier)) {
4717 Diag(Tok, diag::err_expected_ident);
4718 SkipUntil(tok::r_paren, /*StopAtSemi=*/true, /*DontConsume=*/true);
4719 // Forget we parsed anything.
4720 ParamInfo.clear();
4721 return;
4722 }
4723
4724 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
4725
4726 // Reject 'typedef int y; int test(x, y)', but continue parsing.
4727 if (Actions.getTypeName(*ParmII, Tok.getLocation(), getCurScope()))
4728 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
4729
4730 // Verify that the argument identifier has not already been mentioned.
4731 if (!ParamsSoFar.insert(ParmII)) {
4732 Diag(Tok, diag::err_param_redefinition) << ParmII;
4733 } else {
4734 // Remember this identifier in ParamInfo.
4735 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
4736 Tok.getLocation(),
4737 0));
4738 }
4739
4740 // Eat the identifier.
4741 ConsumeToken();
4742
4743 // The list continues if we see a comma.
4744 if (Tok.isNot(tok::comma))
4745 break;
4746 ConsumeToken();
4747 }
4748}
4749
4750/// ParseParameterDeclarationClause - Parse a (possibly empty) parameter-list
4751/// after the opening parenthesis. This function will not parse a K&R-style
4752/// identifier list.
4753///
Richard Smith2620cd92012-04-11 04:01:28 +00004754/// D is the declarator being parsed. If FirstArgAttrs is non-null, then the
4755/// caller parsed those arguments immediately after the open paren - they should
4756/// be considered to be part of the first parameter.
Douglas Gregor9e66af42011-07-05 16:44:18 +00004757///
4758/// After returning, ParamInfo will hold the parsed parameters. EllipsisLoc will
4759/// be the location of the ellipsis, if any was parsed.
4760///
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004761/// parameter-type-list: [C99 6.7.5]
4762/// parameter-list
4763/// parameter-list ',' '...'
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00004764/// [C++] parameter-list '...'
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004765///
4766/// parameter-list: [C99 6.7.5]
4767/// parameter-declaration
4768/// parameter-list ',' parameter-declaration
4769///
4770/// parameter-declaration: [C99 6.7.5]
4771/// declaration-specifiers declarator
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00004772/// [C++] declaration-specifiers declarator '=' assignment-expression
Sebastian Redldb63af22012-03-14 15:54:00 +00004773/// [C++11] initializer-clause
Chris Lattnere37e2332006-08-15 04:50:22 +00004774/// [GNU] declaration-specifiers declarator attributes
Sebastian Redlf769df52009-03-24 22:27:57 +00004775/// declaration-specifiers abstract-declarator[opt]
4776/// [C++] declaration-specifiers abstract-declarator[opt]
Chris Lattner58258242008-04-10 02:22:51 +00004777/// '=' assignment-expression
Chris Lattnere37e2332006-08-15 04:50:22 +00004778/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
Richard Smith2620cd92012-04-11 04:01:28 +00004779/// [C++11] attribute-specifier-seq parameter-declaration
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004780///
Douglas Gregor9e66af42011-07-05 16:44:18 +00004781void Parser::ParseParameterDeclarationClause(
4782 Declarator &D,
Richard Smith2620cd92012-04-11 04:01:28 +00004783 ParsedAttributes &FirstArgAttrs,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004784 SmallVector<DeclaratorChunk::ParamInfo, 16> &ParamInfo,
Douglas Gregor9e66af42011-07-05 16:44:18 +00004785 SourceLocation &EllipsisLoc) {
Mike Stump11289f42009-09-09 15:08:12 +00004786
Chris Lattner371ed4e2008-04-06 06:57:35 +00004787 while (1) {
4788 if (Tok.is(tok::ellipsis)) {
Richard Smith2620cd92012-04-11 04:01:28 +00004789 // FIXME: Issue a diagnostic if we parsed an attribute-specifier-seq
4790 // before deciding this was a parameter-declaration-clause.
Douglas Gregor94349fd2009-02-18 07:07:28 +00004791 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chris Lattner371ed4e2008-04-06 06:57:35 +00004792 break;
Chris Lattneracd58a32006-08-06 17:24:14 +00004793 }
Mike Stump11289f42009-09-09 15:08:12 +00004794
Chris Lattner371ed4e2008-04-06 06:57:35 +00004795 // Parse the declaration-specifiers.
John McCall28a6aea2009-11-04 02:18:39 +00004796 // Just use the ParsingDeclaration "scope" of the declarator.
John McCall084e83d2011-03-24 11:26:52 +00004797 DeclSpec DS(AttrFactory);
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004798
Richard Smith2620cd92012-04-11 04:01:28 +00004799 // Parse any C++11 attributes.
4800 MaybeParseCXX0XAttributes(DS.getAttributes());
4801
John McCall53fa7142010-12-24 02:08:15 +00004802 // Skip any Microsoft attributes before a param.
David Blaikiebbafb8a2012-03-11 07:00:24 +00004803 if (getLangOpts().MicrosoftExt && Tok.is(tok::l_square))
John McCall53fa7142010-12-24 02:08:15 +00004804 ParseMicrosoftAttributes(DS.getAttributes());
4805
4806 SourceLocation DSStart = Tok.getLocation();
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00004807
4808 // If the caller parsed attributes for the first argument, add them now.
John McCall53fa7142010-12-24 02:08:15 +00004809 // Take them so that we only apply the attributes to the first parameter.
Douglas Gregor9e66af42011-07-05 16:44:18 +00004810 // FIXME: If we can leave the attributes in the token stream somehow, we can
Richard Smith2620cd92012-04-11 04:01:28 +00004811 // get rid of a parameter (FirstArgAttrs) and this statement. It might be
4812 // too much hassle.
4813 DS.takeAttributesFrom(FirstArgAttrs);
John McCall53fa7142010-12-24 02:08:15 +00004814
Chris Lattnerde39c3e2009-02-27 18:38:20 +00004815 ParseDeclarationSpecifiers(DS);
Mike Stump11289f42009-09-09 15:08:12 +00004816
Chris Lattner371ed4e2008-04-06 06:57:35 +00004817 // Parse the declarator. This is "PrototypeContext", because we must
4818 // accept either 'declarator' or 'abstract-declarator' here.
4819 Declarator ParmDecl(DS, Declarator::PrototypeContext);
4820 ParseDeclarator(ParmDecl);
4821
4822 // Parse GNU attributes, if present.
John McCall53fa7142010-12-24 02:08:15 +00004823 MaybeParseGNUAttributes(ParmDecl);
Mike Stump11289f42009-09-09 15:08:12 +00004824
Chris Lattner371ed4e2008-04-06 06:57:35 +00004825 // Remember this parsed parameter in ParamInfo.
4826 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
Mike Stump11289f42009-09-09 15:08:12 +00004827
Douglas Gregor4d87df52008-12-16 21:30:33 +00004828 // DefArgToks is used when the parsing of default arguments needs
4829 // to be delayed.
4830 CachedTokens *DefArgToks = 0;
4831
Chris Lattner371ed4e2008-04-06 06:57:35 +00004832 // If no parameter was specified, verify that *something* was specified,
4833 // otherwise we have a missing type and identifier.
Chris Lattnerde39c3e2009-02-27 18:38:20 +00004834 if (DS.isEmpty() && ParmDecl.getIdentifier() == 0 &&
4835 ParmDecl.getNumTypeObjects() == 0) {
Chris Lattner371ed4e2008-04-06 06:57:35 +00004836 // Completely missing, emit error.
4837 Diag(DSStart, diag::err_missing_param);
4838 } else {
4839 // Otherwise, we have something. Add it and let semantic analysis try
4840 // to grok it and add the result to the ParamInfo we are building.
Mike Stump11289f42009-09-09 15:08:12 +00004841
Chris Lattner371ed4e2008-04-06 06:57:35 +00004842 // Inform the actions module about the parameter declarator, so it gets
4843 // added to the current scope.
John McCall48871652010-08-21 09:40:31 +00004844 Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl);
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00004845
4846 // Parse the default argument, if any. We parse the default
4847 // arguments in all dialects; the semantic analysis in
4848 // ActOnParamDefaultArgument will reject the default argument in
4849 // C.
4850 if (Tok.is(tok::equal)) {
Douglas Gregor58354032008-12-24 00:01:03 +00004851 SourceLocation EqualLoc = Tok.getLocation();
4852
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00004853 // Parse the default argument
Douglas Gregor4d87df52008-12-16 21:30:33 +00004854 if (D.getContext() == Declarator::MemberContext) {
4855 // If we're inside a class definition, cache the tokens
4856 // corresponding to the default argument. We'll actually parse
4857 // them when we see the end of the class definition.
Douglas Gregor4d87df52008-12-16 21:30:33 +00004858 // FIXME: Can we use a smart pointer for Toks?
4859 DefArgToks = new CachedTokens;
4860
Mike Stump11289f42009-09-09 15:08:12 +00004861 if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks,
Argyrios Kyrtzidis8d7bdba2010-04-23 21:20:12 +00004862 /*StopAtSemi=*/true,
4863 /*ConsumeFinalToken=*/false)) {
Douglas Gregor4d87df52008-12-16 21:30:33 +00004864 delete DefArgToks;
4865 DefArgToks = 0;
Douglas Gregor58354032008-12-24 00:01:03 +00004866 Actions.ActOnParamDefaultArgumentError(Param);
Argyrios Kyrtzidis249179c2010-08-06 09:47:24 +00004867 } else {
4868 // Mark the end of the default argument so that we know when to
4869 // stop when we parse it later on.
4870 Token DefArgEnd;
4871 DefArgEnd.startToken();
4872 DefArgEnd.setKind(tok::cxx_defaultarg_end);
4873 DefArgEnd.setLocation(Tok.getLocation());
4874 DefArgToks->push_back(DefArgEnd);
Mike Stump11289f42009-09-09 15:08:12 +00004875 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc,
Anders Carlsson84613c42009-06-12 16:51:40 +00004876 (*DefArgToks)[1].getLocation());
Argyrios Kyrtzidis249179c2010-08-06 09:47:24 +00004877 }
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00004878 } else {
Douglas Gregor4d87df52008-12-16 21:30:33 +00004879 // Consume the '='.
Douglas Gregor58354032008-12-24 00:01:03 +00004880 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00004881
Chad Rosierc1183952012-06-26 22:30:43 +00004882 // The argument isn't actually potentially evaluated unless it is
Douglas Gregor8a01b2a2010-09-11 20:24:53 +00004883 // used.
4884 EnterExpressionEvaluationContext Eval(Actions,
Douglas Gregor7fcbd902012-02-21 00:37:24 +00004885 Sema::PotentiallyEvaluatedIfUsed,
4886 Param);
Douglas Gregor8a01b2a2010-09-11 20:24:53 +00004887
Sebastian Redldb63af22012-03-14 15:54:00 +00004888 ExprResult DefArgResult;
Sebastian Redl1678d5f2012-03-18 22:25:45 +00004889 if (getLangOpts().CPlusPlus0x && Tok.is(tok::l_brace)) {
4890 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
Sebastian Redldb63af22012-03-14 15:54:00 +00004891 DefArgResult = ParseBraceInitializer();
Sebastian Redl1678d5f2012-03-18 22:25:45 +00004892 } else
Sebastian Redldb63af22012-03-14 15:54:00 +00004893 DefArgResult = ParseAssignmentExpression();
Douglas Gregor4d87df52008-12-16 21:30:33 +00004894 if (DefArgResult.isInvalid()) {
4895 Actions.ActOnParamDefaultArgumentError(Param);
4896 SkipUntil(tok::comma, tok::r_paren, true, true);
4897 } else {
4898 // Inform the actions module about the default argument
4899 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
John McCallb268a282010-08-23 23:25:46 +00004900 DefArgResult.take());
Douglas Gregor4d87df52008-12-16 21:30:33 +00004901 }
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00004902 }
4903 }
Mike Stump11289f42009-09-09 15:08:12 +00004904
4905 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
4906 ParmDecl.getIdentifierLoc(), Param,
Douglas Gregor4d87df52008-12-16 21:30:33 +00004907 DefArgToks));
Chris Lattner371ed4e2008-04-06 06:57:35 +00004908 }
4909
4910 // If the next token is a comma, consume it and keep reading arguments.
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00004911 if (Tok.isNot(tok::comma)) {
4912 if (Tok.is(tok::ellipsis)) {
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00004913 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chad Rosierc1183952012-06-26 22:30:43 +00004914
David Blaikiebbafb8a2012-03-11 07:00:24 +00004915 if (!getLangOpts().CPlusPlus) {
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00004916 // We have ellipsis without a preceding ',', which is ill-formed
4917 // in C. Complain and provide the fix.
4918 Diag(EllipsisLoc, diag::err_missing_comma_before_ellipsis)
Douglas Gregora771f462010-03-31 17:46:05 +00004919 << FixItHint::CreateInsertion(EllipsisLoc, ", ");
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00004920 }
4921 }
Chad Rosierc1183952012-06-26 22:30:43 +00004922
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00004923 break;
4924 }
Mike Stump11289f42009-09-09 15:08:12 +00004925
Chris Lattner371ed4e2008-04-06 06:57:35 +00004926 // Consume the comma.
4927 ConsumeToken();
Chris Lattneracd58a32006-08-06 17:24:14 +00004928 }
Mike Stump11289f42009-09-09 15:08:12 +00004929
Chris Lattner6c940e62008-04-06 06:34:08 +00004930}
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004931
Chris Lattnere8074e62006-08-06 18:30:15 +00004932/// [C90] direct-declarator '[' constant-expression[opt] ']'
4933/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
4934/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
4935/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
4936/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004937/// [C++11] direct-declarator '[' constant-expression[opt] ']'
4938/// attribute-specifier-seq[opt]
Chris Lattnere8074e62006-08-06 18:30:15 +00004939void Parser::ParseBracketDeclarator(Declarator &D) {
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004940 if (CheckProhibitedCXX11Attribute())
4941 return;
4942
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004943 BalancedDelimiterTracker T(*this, tok::l_square);
4944 T.consumeOpen();
Mike Stump11289f42009-09-09 15:08:12 +00004945
Chris Lattner84a11622008-12-18 07:27:21 +00004946 // C array syntax has many features, but by-far the most common is [] and [4].
4947 // This code does a fast path to handle some of the most obvious cases.
4948 if (Tok.getKind() == tok::r_square) {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004949 T.consumeClose();
John McCall084e83d2011-03-24 11:26:52 +00004950 ParsedAttributes attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00004951 MaybeParseCXX0XAttributes(attrs);
Chad Rosierc1183952012-06-26 22:30:43 +00004952
Chris Lattner84a11622008-12-18 07:27:21 +00004953 // Remember that we parsed the empty array type.
John McCalldadc5752010-08-24 06:29:42 +00004954 ExprResult NumElements;
John McCall084e83d2011-03-24 11:26:52 +00004955 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004956 T.getOpenLocation(),
4957 T.getCloseLocation()),
4958 attrs, T.getCloseLocation());
Chris Lattner84a11622008-12-18 07:27:21 +00004959 return;
4960 } else if (Tok.getKind() == tok::numeric_constant &&
4961 GetLookAheadToken(1).is(tok::r_square)) {
4962 // [4] is very common. Parse the numeric constant expression.
Richard Smithbcc22fc2012-03-09 08:00:36 +00004963 ExprResult ExprRes(Actions.ActOnNumericConstant(Tok, getCurScope()));
Chris Lattner84a11622008-12-18 07:27:21 +00004964 ConsumeToken();
4965
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004966 T.consumeClose();
John McCall084e83d2011-03-24 11:26:52 +00004967 ParsedAttributes attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00004968 MaybeParseCXX0XAttributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +00004969
Chris Lattner84a11622008-12-18 07:27:21 +00004970 // Remember that we parsed a array type, and remember its features.
John McCall084e83d2011-03-24 11:26:52 +00004971 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, 0,
John McCall53fa7142010-12-24 02:08:15 +00004972 ExprRes.release(),
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004973 T.getOpenLocation(),
4974 T.getCloseLocation()),
4975 attrs, T.getCloseLocation());
Chris Lattner84a11622008-12-18 07:27:21 +00004976 return;
4977 }
Mike Stump11289f42009-09-09 15:08:12 +00004978
Chris Lattnere8074e62006-08-06 18:30:15 +00004979 // If valid, this location is the position where we read the 'static' keyword.
4980 SourceLocation StaticLoc;
Chris Lattner76c72282007-10-09 17:33:22 +00004981 if (Tok.is(tok::kw_static))
Chris Lattneraf635312006-10-16 06:06:51 +00004982 StaticLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00004983
Chris Lattnere8074e62006-08-06 18:30:15 +00004984 // If there is a type-qualifier-list, read it now.
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00004985 // Type qualifiers in an array subscript are a C99 feature.
John McCall084e83d2011-03-24 11:26:52 +00004986 DeclSpec DS(AttrFactory);
Chris Lattnercf0bab22008-12-18 07:02:59 +00004987 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Mike Stump11289f42009-09-09 15:08:12 +00004988
Chris Lattnere8074e62006-08-06 18:30:15 +00004989 // If we haven't already read 'static', check to see if there is one after the
4990 // type-qualifier-list.
Chris Lattner76c72282007-10-09 17:33:22 +00004991 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Chris Lattneraf635312006-10-16 06:06:51 +00004992 StaticLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00004993
Chris Lattnere8074e62006-08-06 18:30:15 +00004994 // Handle "direct-declarator [ type-qual-list[opt] * ]".
Chris Lattnere8074e62006-08-06 18:30:15 +00004995 bool isStar = false;
John McCalldadc5752010-08-24 06:29:42 +00004996 ExprResult NumElements;
Mike Stump11289f42009-09-09 15:08:12 +00004997
Chris Lattner521ff2b2008-04-06 05:26:30 +00004998 // Handle the case where we have '[*]' as the array size. However, a leading
4999 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
Sylvestre Ledru830885c2012-07-23 08:59:39 +00005000 // the token after the star is a ']'. Since stars in arrays are
Chris Lattner521ff2b2008-04-06 05:26:30 +00005001 // infrequent, use of lookahead is not costly here.
5002 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattnerc439f0d2008-04-06 05:27:21 +00005003 ConsumeToken(); // Eat the '*'.
Chris Lattner1906f802006-08-06 19:14:46 +00005004
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00005005 if (StaticLoc.isValid()) {
Chris Lattner521ff2b2008-04-06 05:26:30 +00005006 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00005007 StaticLoc = SourceLocation(); // Drop the static.
5008 }
Chris Lattner521ff2b2008-04-06 05:26:30 +00005009 isStar = true;
Chris Lattner76c72282007-10-09 17:33:22 +00005010 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner84a11622008-12-18 07:27:21 +00005011 // Note, in C89, this production uses the constant-expr production instead
5012 // of assignment-expr. The only difference is that assignment-expr allows
5013 // things like '=' and '*='. Sema rejects these in C89 mode because they
5014 // are not i-c-e's, so we don't need to distinguish between the two here.
Mike Stump11289f42009-09-09 15:08:12 +00005015
Douglas Gregorc9c02ed2009-06-19 23:52:42 +00005016 // Parse the constant-expression or assignment-expression now (depending
5017 // on dialect).
David Blaikiebbafb8a2012-03-11 07:00:24 +00005018 if (getLangOpts().CPlusPlus) {
Douglas Gregorc9c02ed2009-06-19 23:52:42 +00005019 NumElements = ParseConstantExpression();
Eli Friedmane0afc982012-01-21 01:01:51 +00005020 } else {
5021 EnterExpressionEvaluationContext Unevaluated(Actions,
5022 Sema::ConstantEvaluated);
Douglas Gregorc9c02ed2009-06-19 23:52:42 +00005023 NumElements = ParseAssignmentExpression();
Eli Friedmane0afc982012-01-21 01:01:51 +00005024 }
Chris Lattner62591722006-08-12 18:40:58 +00005025 }
Mike Stump11289f42009-09-09 15:08:12 +00005026
Chris Lattner62591722006-08-12 18:40:58 +00005027 // If there was an error parsing the assignment-expression, recover.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00005028 if (NumElements.isInvalid()) {
Chris Lattnercd2a8c52009-04-24 22:30:50 +00005029 D.setInvalidType(true);
Chris Lattner62591722006-08-12 18:40:58 +00005030 // If the expression was invalid, skip it.
5031 SkipUntil(tok::r_square);
5032 return;
Chris Lattnere8074e62006-08-06 18:30:15 +00005033 }
Sebastian Redlf6591ca2009-02-09 18:23:29 +00005034
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005035 T.consumeClose();
Sebastian Redlf6591ca2009-02-09 18:23:29 +00005036
John McCall084e83d2011-03-24 11:26:52 +00005037 ParsedAttributes attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00005038 MaybeParseCXX0XAttributes(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +00005039
Chris Lattner84a11622008-12-18 07:27:21 +00005040 // Remember that we parsed a array type, and remember its features.
John McCall084e83d2011-03-24 11:26:52 +00005041 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
Chris Lattnercbc426d2006-12-02 06:43:02 +00005042 StaticLoc.isValid(), isStar,
Douglas Gregor04318252009-07-06 15:59:29 +00005043 NumElements.release(),
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005044 T.getOpenLocation(),
5045 T.getCloseLocation()),
5046 attrs, T.getCloseLocation());
Chris Lattnere8074e62006-08-06 18:30:15 +00005047}
5048
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00005049/// [GNU] typeof-specifier:
5050/// typeof ( expressions )
5051/// typeof ( type-name )
5052/// [GNU/C++] typeof unary-expression
Steve Naroffad373bd2007-07-31 12:34:36 +00005053///
5054void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner76c72282007-10-09 17:33:22 +00005055 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005056 Token OpTok = Tok;
Steve Naroffad373bd2007-07-31 12:34:36 +00005057 SourceLocation StartLoc = ConsumeToken();
5058
John McCalle8595032010-01-13 20:03:27 +00005059 const bool hasParens = Tok.is(tok::l_paren);
5060
Eli Friedmane0afc982012-01-21 01:01:51 +00005061 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated);
5062
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005063 bool isCastExpr;
John McCallba7bf592010-08-24 05:47:05 +00005064 ParsedType CastTy;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005065 SourceRange CastRange;
Peter Collingbournee190dee2011-03-11 19:24:49 +00005066 ExprResult Operand = ParseExprAfterUnaryExprOrTypeTrait(OpTok, isCastExpr,
5067 CastTy, CastRange);
John McCalle8595032010-01-13 20:03:27 +00005068 if (hasParens)
5069 DS.setTypeofParensRange(CastRange);
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005070
5071 if (CastRange.getEnd().isInvalid())
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00005072 // FIXME: Not accurate, the range gets one token more than it should.
5073 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005074 else
5075 DS.SetRangeEnd(CastRange.getEnd());
Mike Stump11289f42009-09-09 15:08:12 +00005076
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005077 if (isCastExpr) {
5078 if (!CastTy) {
5079 DS.SetTypeSpecError();
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00005080 return;
Douglas Gregor220cac52009-02-18 17:45:20 +00005081 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00005082
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005083 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00005084 unsigned DiagID;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005085 // Check for duplicate type specifiers (e.g. "int typeof(int)").
5086 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00005087 DiagID, CastTy))
5088 Diag(StartLoc, DiagID) << PrevSpec;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005089 return;
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00005090 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00005091
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00005092 // If we get here, the operand to the typeof was an expresion.
5093 if (Operand.isInvalid()) {
5094 DS.SetTypeSpecError();
Steve Naroff4bd2f712007-08-02 02:53:48 +00005095 return;
Steve Naroffad373bd2007-07-31 12:34:36 +00005096 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00005097
Eli Friedmane0afc982012-01-21 01:01:51 +00005098 // We might need to transform the operand if it is potentially evaluated.
5099 Operand = Actions.HandleExprEvaluationContextForTypeof(Operand.get());
5100 if (Operand.isInvalid()) {
5101 DS.SetTypeSpecError();
5102 return;
5103 }
5104
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00005105 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00005106 unsigned DiagID;
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00005107 // Check for duplicate type specifiers (e.g. "int typeof(int)").
5108 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
John McCallba7bf592010-08-24 05:47:05 +00005109 DiagID, Operand.get()))
John McCall49bfce42009-08-03 20:12:06 +00005110 Diag(StartLoc, DiagID) << PrevSpec;
Steve Naroffad373bd2007-07-31 12:34:36 +00005111}
Chris Lattner73a9c7d2010-02-28 18:33:55 +00005112
Benjamin Kramere56f3932011-12-23 17:00:35 +00005113/// [C11] atomic-specifier:
Eli Friedman0dfb8892011-10-06 23:00:33 +00005114/// _Atomic ( type-name )
5115///
5116void Parser::ParseAtomicSpecifier(DeclSpec &DS) {
5117 assert(Tok.is(tok::kw__Atomic) && "Not an atomic specifier");
5118
5119 SourceLocation StartLoc = ConsumeToken();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005120 BalancedDelimiterTracker T(*this, tok::l_paren);
5121 if (T.expectAndConsume(diag::err_expected_lparen_after, "_Atomic")) {
Eli Friedman0dfb8892011-10-06 23:00:33 +00005122 SkipUntil(tok::r_paren);
5123 return;
5124 }
5125
5126 TypeResult Result = ParseTypeName();
5127 if (Result.isInvalid()) {
5128 SkipUntil(tok::r_paren);
5129 return;
5130 }
5131
5132 // Match the ')'
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005133 T.consumeClose();
Eli Friedman0dfb8892011-10-06 23:00:33 +00005134
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005135 if (T.getCloseLocation().isInvalid())
Eli Friedman0dfb8892011-10-06 23:00:33 +00005136 return;
5137
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005138 DS.setTypeofParensRange(T.getRange());
5139 DS.SetRangeEnd(T.getCloseLocation());
Eli Friedman0dfb8892011-10-06 23:00:33 +00005140
5141 const char *PrevSpec = 0;
5142 unsigned DiagID;
5143 if (DS.SetTypeSpecType(DeclSpec::TST_atomic, StartLoc, PrevSpec,
5144 DiagID, Result.release()))
5145 Diag(StartLoc, DiagID) << PrevSpec;
5146}
5147
Chris Lattner73a9c7d2010-02-28 18:33:55 +00005148
5149/// TryAltiVecVectorTokenOutOfLine - Out of line body that should only be called
5150/// from TryAltiVecVectorToken.
5151bool Parser::TryAltiVecVectorTokenOutOfLine() {
5152 Token Next = NextToken();
5153 switch (Next.getKind()) {
5154 default: return false;
5155 case tok::kw_short:
5156 case tok::kw_long:
5157 case tok::kw_signed:
5158 case tok::kw_unsigned:
5159 case tok::kw_void:
5160 case tok::kw_char:
5161 case tok::kw_int:
5162 case tok::kw_float:
5163 case tok::kw_double:
5164 case tok::kw_bool:
5165 case tok::kw___pixel:
5166 Tok.setKind(tok::kw___vector);
5167 return true;
5168 case tok::identifier:
5169 if (Next.getIdentifierInfo() == Ident_pixel) {
5170 Tok.setKind(tok::kw___vector);
5171 return true;
5172 }
5173 return false;
5174 }
5175}
5176
5177bool Parser::TryAltiVecTokenOutOfLine(DeclSpec &DS, SourceLocation Loc,
5178 const char *&PrevSpec, unsigned &DiagID,
5179 bool &isInvalid) {
5180 if (Tok.getIdentifierInfo() == Ident_vector) {
5181 Token Next = NextToken();
5182 switch (Next.getKind()) {
5183 case tok::kw_short:
5184 case tok::kw_long:
5185 case tok::kw_signed:
5186 case tok::kw_unsigned:
5187 case tok::kw_void:
5188 case tok::kw_char:
5189 case tok::kw_int:
5190 case tok::kw_float:
5191 case tok::kw_double:
5192 case tok::kw_bool:
5193 case tok::kw___pixel:
5194 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
5195 return true;
5196 case tok::identifier:
5197 if (Next.getIdentifierInfo() == Ident_pixel) {
5198 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
5199 return true;
5200 }
5201 break;
5202 default:
5203 break;
5204 }
Douglas Gregor9938e3b2010-06-16 15:28:57 +00005205 } else if ((Tok.getIdentifierInfo() == Ident_pixel) &&
Chris Lattner73a9c7d2010-02-28 18:33:55 +00005206 DS.isTypeAltiVecVector()) {
5207 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
5208 return true;
5209 }
5210 return false;
5211}