blob: 674cd2d22f06ecc53bb9d8319fa73717b7849b3e [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
Benjamin Kramerf0623432012-08-23 22:51:59 +0000239 ExprVector ArgExprs;
Richard Smithb12bf692011-10-17 21:20:17 +0000240
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,
Benjamin Kramerf0623432012-08-23 22:51:59 +0000282 ParmName, ParmLoc, ArgExprs.data(), ArgExprs.size(),
Alexis Hunta0e54d42012-06-18 16:13:52 +0000283 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 for (unsigned i = 0, ni = Class.LateParsedDeclarations.size(); i < ni; ++i){
855 Class.LateParsedDeclarations[i]->ParseLexedAttributes();
856 }
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000857 }
Chad Rosierc1183952012-06-26 22:30:43 +0000858
DeLesley Hutchins6f860042012-04-06 15:10:17 +0000859 if (!AlreadyHasClassScope)
860 Actions.ActOnFinishDelayedMemberDeclarations(getCurScope(),
861 Class.TagOrTemplate);
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000862}
863
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +0000864
865/// \brief Parse all attributes in LAs, and attach them to Decl D.
866void Parser::ParseLexedAttributeList(LateParsedAttrList &LAs, Decl *D,
867 bool EnterScope, bool OnDefinition) {
868 for (unsigned i = 0, ni = LAs.size(); i < ni; ++i) {
DeLesley Hutchins19c722d2012-08-15 22:41:04 +0000869 if (D)
870 LAs[i]->addDecl(D);
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +0000871 ParseLexedAttribute(*LAs[i], EnterScope, OnDefinition);
Benjamin Kramerbafc49a2012-04-14 12:44:47 +0000872 delete LAs[i];
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +0000873 }
874 LAs.clear();
875}
876
877
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000878/// \brief Finish parsing an attribute for which parsing was delayed.
879/// This will be called at the end of parsing a class declaration
880/// for each LateParsedAttribute. We consume the saved tokens and
Chad Rosierc1183952012-06-26 22:30:43 +0000881/// create an attribute with the arguments filled in. We add this
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000882/// to the Attribute list for the decl.
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +0000883void Parser::ParseLexedAttribute(LateParsedAttribute &LA,
884 bool EnterScope, bool OnDefinition) {
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000885 // Save the current token position.
886 SourceLocation OrigLoc = Tok.getLocation();
887
888 // Append the current token at the end of the new token stream so that it
889 // doesn't get lost.
890 LA.Toks.push_back(Tok);
891 PP.EnterTokenStream(LA.Toks.data(), LA.Toks.size(), true, false);
892 // Consume the previously pushed token.
893 ConsumeAnyToken();
894
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +0000895 if (OnDefinition && !IsThreadSafetyAttribute(LA.AttrName.getName())) {
896 Diag(Tok, diag::warn_attribute_on_function_definition)
897 << LA.AttrName.getName();
898 }
899
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000900 ParsedAttributes Attrs(AttrFactory);
901 SourceLocation endLoc;
902
DeLesley Hutchinsf1150d32012-08-20 21:32:18 +0000903 if (LA.Decls.size() > 0) {
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +0000904 Decl *D = LA.Decls[0];
DeLesley Hutchinsf1150d32012-08-20 21:32:18 +0000905 NamedDecl *ND = dyn_cast<NamedDecl>(D);
906 RecordDecl *RD = dyn_cast_or_null<RecordDecl>(D->getDeclContext());
Caitlin Sadowski990d5712011-09-08 17:42:31 +0000907
DeLesley Hutchinsf1150d32012-08-20 21:32:18 +0000908 // Allow 'this' within late-parsed attributes.
909 Sema::CXXThisScopeRAII ThisScope(Actions, RD,
910 /*TypeQuals=*/0,
911 ND && RD && ND->isCXXInstanceMember());
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000912
DeLesley Hutchinsf1150d32012-08-20 21:32:18 +0000913 if (LA.Decls.size() == 1) {
914 // If the Decl is templatized, add template parameters to scope.
915 bool HasTemplateScope = EnterScope && D->isTemplateDecl();
916 ParseScope TempScope(this, Scope::TemplateParamScope, HasTemplateScope);
917 if (HasTemplateScope)
918 Actions.ActOnReenterTemplateScope(Actions.CurScope, D);
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +0000919
DeLesley Hutchinsf1150d32012-08-20 21:32:18 +0000920 // If the Decl is on a function, add function parameters to the scope.
921 bool HasFunScope = EnterScope && D->isFunctionOrFunctionTemplate();
922 ParseScope FnScope(this, Scope::FnScope|Scope::DeclScope, HasFunScope);
923 if (HasFunScope)
924 Actions.ActOnReenterFunctionContext(Actions.CurScope, D);
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +0000925
DeLesley Hutchinsf1150d32012-08-20 21:32:18 +0000926 ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, &endLoc);
927
928 if (HasFunScope) {
929 Actions.ActOnExitFunctionContext();
930 FnScope.Exit(); // Pop scope, and remove Decls from IdResolver
931 }
932 if (HasTemplateScope) {
933 TempScope.Exit();
934 }
935 } else {
936 // If there are multiple decls, then the decl cannot be within the
937 // function scope.
938 ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, &endLoc);
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +0000939 }
DeLesley Hutchins71d61032012-03-02 22:29:50 +0000940 } else {
941 Diag(Tok, diag::warn_attribute_no_decl) << LA.AttrName.getName();
Caitlin Sadowski990d5712011-09-08 17:42:31 +0000942 }
943
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +0000944 for (unsigned i = 0, ni = LA.Decls.size(); i < ni; ++i) {
945 Actions.ActOnFinishDelayedAttribute(getCurScope(), LA.Decls[i], Attrs);
946 }
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000947
948 if (Tok.getLocation() != OrigLoc) {
949 // Due to a parsing error, we either went over the cached tokens or
950 // there are still cached tokens left, so we skip the leftover tokens.
951 // Since this is an uncommon situation that should be avoided, use the
952 // expensive isBeforeInTranslationUnit call.
953 if (PP.getSourceManager().isBeforeInTranslationUnit(Tok.getLocation(),
954 OrigLoc))
955 while (Tok.getLocation() != OrigLoc && Tok.isNot(tok::eof))
Douglas Gregor0cf55e92012-03-08 01:00:17 +0000956 ConsumeAnyToken();
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000957 }
958}
959
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000960/// \brief Wrapper around a case statement checking if AttrName is
961/// one of the thread safety attributes
962bool Parser::IsThreadSafetyAttribute(llvm::StringRef AttrName){
963 return llvm::StringSwitch<bool>(AttrName)
964 .Case("guarded_by", true)
965 .Case("guarded_var", true)
966 .Case("pt_guarded_by", true)
967 .Case("pt_guarded_var", true)
968 .Case("lockable", true)
969 .Case("scoped_lockable", true)
970 .Case("no_thread_safety_analysis", true)
971 .Case("acquired_after", true)
972 .Case("acquired_before", true)
973 .Case("exclusive_lock_function", true)
974 .Case("shared_lock_function", true)
975 .Case("exclusive_trylock_function", true)
976 .Case("shared_trylock_function", true)
977 .Case("unlock_function", true)
978 .Case("lock_returned", true)
979 .Case("locks_excluded", true)
980 .Case("exclusive_locks_required", true)
981 .Case("shared_locks_required", true)
982 .Default(false);
983}
984
985/// \brief Parse the contents of thread safety attributes. These
986/// should always be parsed as an expression list.
987///
988/// We need to special case the parsing due to the fact that if the first token
989/// of the first argument is an identifier, the main parse loop will store
990/// that token as a "parameter" and the rest of
991/// the arguments will be added to a list of "arguments". However,
992/// subsequent tokens in the first argument are lost. We instead parse each
993/// argument as an expression and add all arguments to the list of "arguments".
994/// In future, we will take advantage of this special case to also
995/// deal with some argument scoping issues here (for example, referring to a
996/// function parameter in the attribute on that function).
997void Parser::ParseThreadSafetyAttribute(IdentifierInfo &AttrName,
998 SourceLocation AttrNameLoc,
999 ParsedAttributes &Attrs,
1000 SourceLocation *EndLoc) {
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001001 assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('");
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +00001002
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001003 BalancedDelimiterTracker T(*this, tok::l_paren);
1004 T.consumeOpen();
Chad Rosierc1183952012-06-26 22:30:43 +00001005
Benjamin Kramerf0623432012-08-23 22:51:59 +00001006 ExprVector ArgExprs;
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001007 bool ArgExprsOk = true;
Chad Rosierc1183952012-06-26 22:30:43 +00001008
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001009 // now parse the list of expressions
DeLesley Hutchins36f5d852011-12-14 19:36:06 +00001010 while (Tok.isNot(tok::r_paren)) {
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001011 ExprResult ArgExpr(ParseAssignmentExpression());
1012 if (ArgExpr.isInvalid()) {
1013 ArgExprsOk = false;
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001014 T.consumeClose();
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001015 break;
1016 } else {
1017 ArgExprs.push_back(ArgExpr.release());
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +00001018 }
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001019 if (Tok.isNot(tok::comma))
1020 break;
1021 ConsumeToken(); // Eat the comma, move to the next argument
1022 }
1023 // Match the ')'.
DeLesley Hutchins30398dd2012-01-20 22:50:54 +00001024 if (ArgExprsOk && !T.consumeClose()) {
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001025 Attrs.addNew(&AttrName, AttrNameLoc, 0, AttrNameLoc, 0, SourceLocation(),
Benjamin Kramerf0623432012-08-23 22:51:59 +00001026 ArgExprs.data(), ArgExprs.size(), AttributeList::AS_GNU);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +00001027 }
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001028 if (EndLoc)
1029 *EndLoc = T.getCloseLocation();
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +00001030}
1031
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00001032void Parser::ParseTypeTagForDatatypeAttribute(IdentifierInfo &AttrName,
1033 SourceLocation AttrNameLoc,
1034 ParsedAttributes &Attrs,
1035 SourceLocation *EndLoc) {
1036 assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('");
1037
1038 BalancedDelimiterTracker T(*this, tok::l_paren);
1039 T.consumeOpen();
1040
1041 if (Tok.isNot(tok::identifier)) {
1042 Diag(Tok, diag::err_expected_ident);
1043 T.skipToEnd();
1044 return;
1045 }
1046 IdentifierInfo *ArgumentKind = Tok.getIdentifierInfo();
1047 SourceLocation ArgumentKindLoc = ConsumeToken();
1048
1049 if (Tok.isNot(tok::comma)) {
1050 Diag(Tok, diag::err_expected_comma);
1051 T.skipToEnd();
1052 return;
1053 }
1054 ConsumeToken();
1055
1056 SourceRange MatchingCTypeRange;
1057 TypeResult MatchingCType = ParseTypeName(&MatchingCTypeRange);
1058 if (MatchingCType.isInvalid()) {
1059 T.skipToEnd();
1060 return;
1061 }
1062
1063 bool LayoutCompatible = false;
1064 bool MustBeNull = false;
1065 while (Tok.is(tok::comma)) {
1066 ConsumeToken();
1067 if (Tok.isNot(tok::identifier)) {
1068 Diag(Tok, diag::err_expected_ident);
1069 T.skipToEnd();
1070 return;
1071 }
1072 IdentifierInfo *Flag = Tok.getIdentifierInfo();
1073 if (Flag->isStr("layout_compatible"))
1074 LayoutCompatible = true;
1075 else if (Flag->isStr("must_be_null"))
1076 MustBeNull = true;
1077 else {
1078 Diag(Tok, diag::err_type_safety_unknown_flag) << Flag;
1079 T.skipToEnd();
1080 return;
1081 }
1082 ConsumeToken(); // consume flag
1083 }
1084
1085 if (!T.consumeClose()) {
1086 Attrs.addNewTypeTagForDatatype(&AttrName, AttrNameLoc, 0, AttrNameLoc,
1087 ArgumentKind, ArgumentKindLoc,
1088 MatchingCType.release(), LayoutCompatible,
1089 MustBeNull, AttributeList::AS_GNU);
1090 }
1091
1092 if (EndLoc)
1093 *EndLoc = T.getCloseLocation();
1094}
1095
Richard Smith7bdcc4a2012-04-10 01:32:12 +00001096/// DiagnoseProhibitedCXX11Attribute - We have found the opening square brackets
1097/// of a C++11 attribute-specifier in a location where an attribute is not
1098/// permitted. By C++11 [dcl.attr.grammar]p6, this is ill-formed. Diagnose this
1099/// situation.
1100///
1101/// \return \c true if we skipped an attribute-like chunk of tokens, \c false if
1102/// this doesn't appear to actually be an attribute-specifier, and the caller
1103/// should try to parse it.
1104bool Parser::DiagnoseProhibitedCXX11Attribute() {
1105 assert(Tok.is(tok::l_square) && NextToken().is(tok::l_square));
1106
1107 switch (isCXX11AttributeSpecifier(/*Disambiguate*/true)) {
1108 case CAK_NotAttributeSpecifier:
1109 // No diagnostic: we're in Obj-C++11 and this is not actually an attribute.
1110 return false;
1111
1112 case CAK_InvalidAttributeSpecifier:
1113 Diag(Tok.getLocation(), diag::err_l_square_l_square_not_attribute);
1114 return false;
1115
1116 case CAK_AttributeSpecifier:
1117 // Parse and discard the attributes.
1118 SourceLocation BeginLoc = ConsumeBracket();
1119 ConsumeBracket();
1120 SkipUntil(tok::r_square, /*StopAtSemi*/ false);
1121 assert(Tok.is(tok::r_square) && "isCXX11AttributeSpecifier lied");
1122 SourceLocation EndLoc = ConsumeBracket();
1123 Diag(BeginLoc, diag::err_attributes_not_allowed)
1124 << SourceRange(BeginLoc, EndLoc);
1125 return true;
1126 }
Chandler Carruthd8f7d382012-04-10 16:03:08 +00001127 llvm_unreachable("All cases handled above.");
Richard Smith7bdcc4a2012-04-10 01:32:12 +00001128}
1129
John McCall53fa7142010-12-24 02:08:15 +00001130void Parser::DiagnoseProhibitedAttributes(ParsedAttributesWithRange &attrs) {
1131 Diag(attrs.Range.getBegin(), diag::err_attributes_not_allowed)
1132 << attrs.Range;
Dawn Perchik335e16b2010-09-03 01:29:35 +00001133}
1134
Chris Lattner53361ac2006-08-10 05:19:57 +00001135/// ParseDeclaration - Parse a full 'declaration', which consists of
1136/// declaration-specifiers, some number of declarators, and a semicolon.
Chris Lattner49836b42009-04-02 04:16:50 +00001137/// 'Context' should be a Declarator::TheContext value. This returns the
1138/// location of the semicolon in DeclEnd.
Chris Lattnera5235172007-08-25 06:57:03 +00001139///
1140/// declaration: [C99 6.7]
1141/// block-declaration ->
1142/// simple-declaration
1143/// others [FIXME]
Douglas Gregoreb31f392008-12-01 23:54:00 +00001144/// [C++] template-declaration
Chris Lattnera5235172007-08-25 06:57:03 +00001145/// [C++] namespace-definition
Douglas Gregord7c4d982008-12-30 03:27:21 +00001146/// [C++] using-directive
Douglas Gregor77b50e12009-06-22 23:06:13 +00001147/// [C++] using-declaration
Richard Smithc202b282012-04-14 00:33:13 +00001148/// [C++11/C11] static_assert-declaration
Chris Lattnera5235172007-08-25 06:57:03 +00001149/// others... [FIXME]
1150///
Fariborz Jahanian1db5c942010-09-28 20:42:35 +00001151Parser::DeclGroupPtrTy Parser::ParseDeclaration(StmtVector &Stmts,
1152 unsigned Context,
Alexis Hunt96d5c762009-11-21 08:43:09 +00001153 SourceLocation &DeclEnd,
John McCall53fa7142010-12-24 02:08:15 +00001154 ParsedAttributesWithRange &attrs) {
Argyrios Kyrtzidis355094e2010-06-17 10:52:18 +00001155 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Fariborz Jahanian59b75282011-08-30 17:10:52 +00001156 // Must temporarily exit the objective-c container scope for
1157 // parsing c none objective-c decls.
1158 ObjCDeclContextSwitch ObjCDC(*this);
Chad Rosierc1183952012-06-26 22:30:43 +00001159
John McCall48871652010-08-21 09:40:31 +00001160 Decl *SingleDecl = 0;
Richard Smithcd1c0552011-07-01 19:46:12 +00001161 Decl *OwnedType = 0;
Chris Lattnera5235172007-08-25 06:57:03 +00001162 switch (Tok.getKind()) {
Douglas Gregoreb31f392008-12-01 23:54:00 +00001163 case tok::kw_template:
Douglas Gregor23996282009-05-12 21:31:51 +00001164 case tok::kw_export:
John McCall53fa7142010-12-24 02:08:15 +00001165 ProhibitAttributes(attrs);
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001166 SingleDecl = ParseDeclarationStartingWithTemplate(Context, DeclEnd);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001167 break;
Sebastian Redl67667942010-08-27 23:12:46 +00001168 case tok::kw_inline:
Sebastian Redl5a5f2c72010-08-31 00:36:45 +00001169 // Could be the start of an inline namespace. Allowed as an ext in C++03.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001170 if (getLangOpts().CPlusPlus && NextToken().is(tok::kw_namespace)) {
John McCall53fa7142010-12-24 02:08:15 +00001171 ProhibitAttributes(attrs);
Sebastian Redl67667942010-08-27 23:12:46 +00001172 SourceLocation InlineLoc = ConsumeToken();
1173 SingleDecl = ParseNamespace(Context, DeclEnd, InlineLoc);
1174 break;
1175 }
Chad Rosierc1183952012-06-26 22:30:43 +00001176 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs,
Fariborz Jahanian1db5c942010-09-28 20:42:35 +00001177 true);
Chris Lattnera5235172007-08-25 06:57:03 +00001178 case tok::kw_namespace:
John McCall53fa7142010-12-24 02:08:15 +00001179 ProhibitAttributes(attrs);
Chris Lattner49836b42009-04-02 04:16:50 +00001180 SingleDecl = ParseNamespace(Context, DeclEnd);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001181 break;
Douglas Gregord7c4d982008-12-30 03:27:21 +00001182 case tok::kw_using:
John McCall9b72f892010-11-10 02:40:36 +00001183 SingleDecl = ParseUsingDirectiveOrDeclaration(Context, ParsedTemplateInfo(),
Richard Smithcd1c0552011-07-01 19:46:12 +00001184 DeclEnd, attrs, &OwnedType);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001185 break;
Anders Carlssonf24fcff62009-03-11 16:27:10 +00001186 case tok::kw_static_assert:
Peter Collingbourne3d9cbdc2011-04-15 00:35:57 +00001187 case tok::kw__Static_assert:
John McCall53fa7142010-12-24 02:08:15 +00001188 ProhibitAttributes(attrs);
Chris Lattner49836b42009-04-02 04:16:50 +00001189 SingleDecl = ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001190 break;
Chris Lattnera5235172007-08-25 06:57:03 +00001191 default:
John McCall53fa7142010-12-24 02:08:15 +00001192 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs, true);
Chris Lattnera5235172007-08-25 06:57:03 +00001193 }
Chad Rosierc1183952012-06-26 22:30:43 +00001194
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001195 // This routine returns a DeclGroup, if the thing we parsed only contains a
Richard Smithcd1c0552011-07-01 19:46:12 +00001196 // single decl, convert it now. Alias declarations can also declare a type;
1197 // include that too if it is present.
1198 return Actions.ConvertDeclToDeclGroup(SingleDecl, OwnedType);
Chris Lattnera5235172007-08-25 06:57:03 +00001199}
1200
1201/// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl]
1202/// declaration-specifiers init-declarator-list[opt] ';'
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001203/// [C++11] attribute-specifier-seq decl-specifier-seq[opt]
1204/// init-declarator-list ';'
Chris Lattnera5235172007-08-25 06:57:03 +00001205///[C90/C++]init-declarator-list ';' [TODO]
1206/// [OMP] threadprivate-directive [TODO]
Chris Lattner32dc41c2009-03-29 17:27:48 +00001207///
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001208/// for-range-declaration: [C++11 6.5p1: stmt.ranged]
Richard Smith02e85f32011-04-14 22:09:26 +00001209/// attribute-specifier-seq[opt] type-specifier-seq declarator
1210///
Chris Lattner32dc41c2009-03-29 17:27:48 +00001211/// If RequireSemi is false, this does not check for a ';' at the end of the
Chris Lattner005fc1b2010-04-05 18:18:31 +00001212/// declaration. If it is true, it checks for and eats it.
Richard Smith02e85f32011-04-14 22:09:26 +00001213///
1214/// If FRI is non-null, we might be parsing a for-range-declaration instead
1215/// of a simple-declaration. If we find that we are, we also parse the
1216/// for-range-initializer, and place it here.
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001217Parser::DeclGroupPtrTy
1218Parser::ParseSimpleDeclaration(StmtVector &Stmts, unsigned Context,
1219 SourceLocation &DeclEnd,
1220 ParsedAttributesWithRange &attrs,
1221 bool RequireSemi, ForRangeInit *FRI) {
Chris Lattner53361ac2006-08-10 05:19:57 +00001222 // Parse the common declaration-specifiers piece.
John McCall28a6aea2009-11-04 02:18:39 +00001223 ParsingDeclSpec DS(*this);
John McCall53fa7142010-12-24 02:08:15 +00001224 DS.takeAttributesFrom(attrs);
Douglas Gregor0e7dde52011-04-24 05:37:28 +00001225
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001226 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS_none,
Richard Smith30482bc2011-02-20 03:19:35 +00001227 getDeclSpecContextFromDeclaratorContext(Context));
Abramo Bagnara1cd83682012-01-07 10:52:36 +00001228
Chris Lattner0e894622006-08-13 19:58:17 +00001229 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
1230 // declaration-specifiers init-declarator-list[opt] ';'
Chris Lattner76c72282007-10-09 17:33:22 +00001231 if (Tok.is(tok::semi)) {
Argyrios Kyrtzidisfbb2bb52012-05-16 23:49:15 +00001232 DeclEnd = Tok.getLocation();
Chris Lattner005fc1b2010-04-05 18:18:31 +00001233 if (RequireSemi) ConsumeToken();
John McCall48871652010-08-21 09:40:31 +00001234 Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none,
Douglas Gregor0e7dde52011-04-24 05:37:28 +00001235 DS);
John McCall28a6aea2009-11-04 02:18:39 +00001236 DS.complete(TheDecl);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001237 return Actions.ConvertDeclToDeclGroup(TheDecl);
Chris Lattner0e894622006-08-13 19:58:17 +00001238 }
Chad Rosierc1183952012-06-26 22:30:43 +00001239
1240 return ParseDeclGroup(DS, Context, /*FunctionDefs=*/ false, &DeclEnd, FRI);
John McCalld5a36322009-11-03 19:26:08 +00001241}
Mike Stump11289f42009-09-09 15:08:12 +00001242
Richard Smith09f76ee2011-10-19 21:33:05 +00001243/// Returns true if this might be the start of a declarator, or a common typo
1244/// for a declarator.
1245bool Parser::MightBeDeclarator(unsigned Context) {
1246 switch (Tok.getKind()) {
1247 case tok::annot_cxxscope:
1248 case tok::annot_template_id:
1249 case tok::caret:
1250 case tok::code_completion:
1251 case tok::coloncolon:
1252 case tok::ellipsis:
1253 case tok::kw___attribute:
1254 case tok::kw_operator:
1255 case tok::l_paren:
1256 case tok::star:
1257 return true;
1258
1259 case tok::amp:
1260 case tok::ampamp:
David Blaikiebbafb8a2012-03-11 07:00:24 +00001261 return getLangOpts().CPlusPlus;
Richard Smith09f76ee2011-10-19 21:33:05 +00001262
Richard Smithc8a79032012-01-09 22:31:44 +00001263 case tok::l_square: // Might be an attribute on an unnamed bit-field.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001264 return Context == Declarator::MemberContext && getLangOpts().CPlusPlus0x &&
Richard Smithc8a79032012-01-09 22:31:44 +00001265 NextToken().is(tok::l_square);
1266
1267 case tok::colon: // Might be a typo for '::' or an unnamed bit-field.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001268 return Context == Declarator::MemberContext || getLangOpts().CPlusPlus;
Richard Smithc8a79032012-01-09 22:31:44 +00001269
Richard Smith09f76ee2011-10-19 21:33:05 +00001270 case tok::identifier:
1271 switch (NextToken().getKind()) {
1272 case tok::code_completion:
1273 case tok::coloncolon:
1274 case tok::comma:
1275 case tok::equal:
1276 case tok::equalequal: // Might be a typo for '='.
1277 case tok::kw_alignas:
1278 case tok::kw_asm:
1279 case tok::kw___attribute:
1280 case tok::l_brace:
1281 case tok::l_paren:
1282 case tok::l_square:
1283 case tok::less:
1284 case tok::r_brace:
1285 case tok::r_paren:
1286 case tok::r_square:
1287 case tok::semi:
1288 return true;
1289
1290 case tok::colon:
1291 // At namespace scope, 'identifier:' is probably a typo for 'identifier::'
Richard Smithc8a79032012-01-09 22:31:44 +00001292 // and in block scope it's probably a label. Inside a class definition,
1293 // this is a bit-field.
1294 return Context == Declarator::MemberContext ||
David Blaikiebbafb8a2012-03-11 07:00:24 +00001295 (getLangOpts().CPlusPlus && Context == Declarator::FileContext);
Richard Smithc8a79032012-01-09 22:31:44 +00001296
1297 case tok::identifier: // Possible virt-specifier.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001298 return getLangOpts().CPlusPlus0x && isCXX0XVirtSpecifier(NextToken());
Richard Smith09f76ee2011-10-19 21:33:05 +00001299
1300 default:
1301 return false;
1302 }
1303
1304 default:
1305 return false;
1306 }
1307}
1308
Richard Smithb8caac82012-04-11 20:59:20 +00001309/// Skip until we reach something which seems like a sensible place to pick
1310/// up parsing after a malformed declaration. This will sometimes stop sooner
1311/// than SkipUntil(tok::r_brace) would, but will never stop later.
1312void Parser::SkipMalformedDecl() {
1313 while (true) {
1314 switch (Tok.getKind()) {
1315 case tok::l_brace:
1316 // Skip until matching }, then stop. We've probably skipped over
1317 // a malformed class or function definition or similar.
1318 ConsumeBrace();
1319 SkipUntil(tok::r_brace, /*StopAtSemi*/false);
1320 if (Tok.is(tok::comma) || Tok.is(tok::l_brace) || Tok.is(tok::kw_try)) {
1321 // This declaration isn't over yet. Keep skipping.
1322 continue;
1323 }
1324 if (Tok.is(tok::semi))
1325 ConsumeToken();
1326 return;
1327
1328 case tok::l_square:
1329 ConsumeBracket();
1330 SkipUntil(tok::r_square, /*StopAtSemi*/false);
1331 continue;
1332
1333 case tok::l_paren:
1334 ConsumeParen();
1335 SkipUntil(tok::r_paren, /*StopAtSemi*/false);
1336 continue;
1337
1338 case tok::r_brace:
1339 return;
1340
1341 case tok::semi:
1342 ConsumeToken();
1343 return;
1344
1345 case tok::kw_inline:
1346 // 'inline namespace' at the start of a line is almost certainly
Jordan Rose12e730c2012-07-09 16:54:53 +00001347 // a good place to pick back up parsing, except in an Objective-C
1348 // @interface context.
1349 if (Tok.isAtStartOfLine() && NextToken().is(tok::kw_namespace) &&
1350 (!ParsingInObjCContainer || CurParsedObjCImpl))
Richard Smithb8caac82012-04-11 20:59:20 +00001351 return;
1352 break;
1353
1354 case tok::kw_namespace:
1355 // 'namespace' at the start of a line is almost certainly a good
Jordan Rose12e730c2012-07-09 16:54:53 +00001356 // place to pick back up parsing, except in an Objective-C
1357 // @interface context.
1358 if (Tok.isAtStartOfLine() &&
1359 (!ParsingInObjCContainer || CurParsedObjCImpl))
1360 return;
1361 break;
1362
1363 case tok::at:
1364 // @end is very much like } in Objective-C contexts.
1365 if (NextToken().isObjCAtKeyword(tok::objc_end) &&
1366 ParsingInObjCContainer)
1367 return;
1368 break;
1369
1370 case tok::minus:
1371 case tok::plus:
1372 // - and + probably start new method declarations in Objective-C contexts.
1373 if (Tok.isAtStartOfLine() && ParsingInObjCContainer)
Richard Smithb8caac82012-04-11 20:59:20 +00001374 return;
1375 break;
1376
1377 case tok::eof:
1378 return;
1379
1380 default:
1381 break;
1382 }
1383
1384 ConsumeAnyToken();
1385 }
1386}
1387
John McCalld5a36322009-11-03 19:26:08 +00001388/// ParseDeclGroup - Having concluded that this is either a function
1389/// definition or a group of object declarations, actually parse the
1390/// result.
John McCall28a6aea2009-11-04 02:18:39 +00001391Parser::DeclGroupPtrTy Parser::ParseDeclGroup(ParsingDeclSpec &DS,
1392 unsigned Context,
John McCalld5a36322009-11-03 19:26:08 +00001393 bool AllowFunctionDefinitions,
Richard Smith02e85f32011-04-14 22:09:26 +00001394 SourceLocation *DeclEnd,
1395 ForRangeInit *FRI) {
John McCalld5a36322009-11-03 19:26:08 +00001396 // Parse the first declarator.
John McCall28a6aea2009-11-04 02:18:39 +00001397 ParsingDeclarator D(*this, DS, static_cast<Declarator::TheContext>(Context));
John McCalld5a36322009-11-03 19:26:08 +00001398 ParseDeclarator(D);
Chris Lattner32dc41c2009-03-29 17:27:48 +00001399
John McCalld5a36322009-11-03 19:26:08 +00001400 // Bail out if the first declarator didn't seem well-formed.
1401 if (!D.hasName() && !D.mayOmitIdentifier()) {
Richard Smithb8caac82012-04-11 20:59:20 +00001402 SkipMalformedDecl();
John McCalld5a36322009-11-03 19:26:08 +00001403 return DeclGroupPtrTy();
Chris Lattnerefb0f112009-03-29 17:18:04 +00001404 }
Mike Stump11289f42009-09-09 15:08:12 +00001405
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001406 // Save late-parsed attributes for now; they need to be parsed in the
1407 // appropriate function scope after the function Decl has been constructed.
1408 LateParsedAttrList LateParsedAttrs;
1409 if (D.isFunctionDeclarator())
1410 MaybeParseGNUAttributes(D, &LateParsedAttrs);
1411
Chris Lattnerdbb1e932010-07-11 22:24:20 +00001412 // Check to see if we have a function *definition* which must have a body.
1413 if (AllowFunctionDefinitions && D.isFunctionDeclarator() &&
1414 // Look at the next token to make sure that this isn't a function
1415 // declaration. We have to check this because __attribute__ might be the
1416 // start of a function definition in GCC-extended K&R C.
Fariborz Jahanian712bb812012-08-10 15:54:40 +00001417 !isDeclarationAfterDeclarator()) {
Chad Rosierc1183952012-06-26 22:30:43 +00001418
Chris Lattner13901342010-07-11 22:42:07 +00001419 if (isStartOfFunctionDefinition(D)) {
John McCalld5a36322009-11-03 19:26:08 +00001420 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
1421 Diag(Tok, diag::err_function_declared_typedef);
1422
1423 // Recover by treating the 'typedef' as spurious.
1424 DS.ClearStorageClassSpecs();
1425 }
1426
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001427 Decl *TheDecl =
1428 ParseFunctionDefinition(D, ParsedTemplateInfo(), &LateParsedAttrs);
John McCalld5a36322009-11-03 19:26:08 +00001429 return Actions.ConvertDeclToDeclGroup(TheDecl);
Chris Lattner13901342010-07-11 22:42:07 +00001430 }
Chad Rosierc1183952012-06-26 22:30:43 +00001431
Chris Lattner13901342010-07-11 22:42:07 +00001432 if (isDeclarationSpecifier()) {
1433 // If there is an invalid declaration specifier right after the function
1434 // prototype, then we must be in a missing semicolon case where this isn't
1435 // actually a body. Just fall through into the code that handles it as a
1436 // prototype, and let the top-level code handle the erroneous declspec
1437 // where it would otherwise expect a comma or semicolon.
John McCalld5a36322009-11-03 19:26:08 +00001438 } else {
1439 Diag(Tok, diag::err_expected_fn_body);
1440 SkipUntil(tok::semi);
1441 return DeclGroupPtrTy();
1442 }
1443 }
1444
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001445 if (ParseAsmAttributesAfterDeclarator(D))
Richard Smith02e85f32011-04-14 22:09:26 +00001446 return DeclGroupPtrTy();
1447
1448 // C++0x [stmt.iter]p1: Check if we have a for-range-declarator. If so, we
1449 // must parse and analyze the for-range-initializer before the declaration is
1450 // analyzed.
1451 if (FRI && Tok.is(tok::colon)) {
1452 FRI->ColonLoc = ConsumeToken();
Sebastian Redl3da34892011-06-05 12:23:16 +00001453 if (Tok.is(tok::l_brace))
1454 FRI->RangeExpr = ParseBraceInitializer();
1455 else
1456 FRI->RangeExpr = ParseExpression();
Richard Smith02e85f32011-04-14 22:09:26 +00001457 Decl *ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
1458 Actions.ActOnCXXForRangeDecl(ThisDecl);
1459 Actions.FinalizeDeclaration(ThisDecl);
John McCallcf6e0c82012-01-27 01:29:43 +00001460 D.complete(ThisDecl);
Richard Smith02e85f32011-04-14 22:09:26 +00001461 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS, &ThisDecl, 1);
1462 }
1463
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001464 SmallVector<Decl *, 8> DeclsInGroup;
Richard Smith02e85f32011-04-14 22:09:26 +00001465 Decl *FirstDecl = ParseDeclarationAfterDeclaratorAndAttributes(D);
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001466 if (LateParsedAttrs.size() > 0)
1467 ParseLexedAttributeList(LateParsedAttrs, FirstDecl, true, false);
John McCall28a6aea2009-11-04 02:18:39 +00001468 D.complete(FirstDecl);
John McCall48871652010-08-21 09:40:31 +00001469 if (FirstDecl)
John McCalld5a36322009-11-03 19:26:08 +00001470 DeclsInGroup.push_back(FirstDecl);
1471
Richard Smith09f76ee2011-10-19 21:33:05 +00001472 bool ExpectSemi = Context != Declarator::ForContext;
Fariborz Jahanian577574a2012-07-02 23:37:09 +00001473
John McCalld5a36322009-11-03 19:26:08 +00001474 // If we don't have a comma, it is either the end of the list (a ';') or an
1475 // error, bail out.
1476 while (Tok.is(tok::comma)) {
Richard Smith09f76ee2011-10-19 21:33:05 +00001477 SourceLocation CommaLoc = ConsumeToken();
1478
1479 if (Tok.isAtStartOfLine() && ExpectSemi && !MightBeDeclarator(Context)) {
1480 // This comma was followed by a line-break and something which can't be
1481 // the start of a declarator. The comma was probably a typo for a
1482 // semicolon.
1483 Diag(CommaLoc, diag::err_expected_semi_declaration)
1484 << FixItHint::CreateReplacement(CommaLoc, ";");
1485 ExpectSemi = false;
1486 break;
1487 }
John McCalld5a36322009-11-03 19:26:08 +00001488
1489 // Parse the next declarator.
1490 D.clear();
Richard Smith8d06f422012-01-12 23:53:29 +00001491 D.setCommaLoc(CommaLoc);
John McCalld5a36322009-11-03 19:26:08 +00001492
1493 // Accept attributes in an init-declarator. In the first declarator in a
1494 // declaration, these would be part of the declspec. In subsequent
1495 // declarators, they become part of the declarator itself, so that they
1496 // don't apply to declarators after *this* one. Examples:
1497 // short __attribute__((common)) var; -> declspec
1498 // short var __attribute__((common)); -> declarator
1499 // short x, __attribute__((common)) var; -> declarator
John McCall53fa7142010-12-24 02:08:15 +00001500 MaybeParseGNUAttributes(D);
John McCalld5a36322009-11-03 19:26:08 +00001501
1502 ParseDeclarator(D);
Fariborz Jahanian372030b2012-01-13 00:14:12 +00001503 if (!D.isInvalidType()) {
1504 Decl *ThisDecl = ParseDeclarationAfterDeclarator(D);
1505 D.complete(ThisDecl);
1506 if (ThisDecl)
Chad Rosierc1183952012-06-26 22:30:43 +00001507 DeclsInGroup.push_back(ThisDecl);
Fariborz Jahanian372030b2012-01-13 00:14:12 +00001508 }
John McCalld5a36322009-11-03 19:26:08 +00001509 }
1510
1511 if (DeclEnd)
1512 *DeclEnd = Tok.getLocation();
1513
Richard Smith09f76ee2011-10-19 21:33:05 +00001514 if (ExpectSemi &&
Chris Lattner02f1b612012-04-28 16:12:17 +00001515 ExpectAndConsumeSemi(Context == Declarator::FileContext
1516 ? diag::err_invalid_token_after_toplevel_declarator
1517 : diag::err_expected_semi_declaration)) {
Chris Lattner13901342010-07-11 22:42:07 +00001518 // Okay, there was no semicolon and one was expected. If we see a
1519 // declaration specifier, just assume it was missing and continue parsing.
1520 // Otherwise things are very confused and we skip to recover.
1521 if (!isDeclarationSpecifier()) {
1522 SkipUntil(tok::r_brace, true, true);
1523 if (Tok.is(tok::semi))
1524 ConsumeToken();
1525 }
John McCalld5a36322009-11-03 19:26:08 +00001526 }
1527
Douglas Gregor0be31a22010-07-02 17:43:08 +00001528 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS,
John McCalld5a36322009-11-03 19:26:08 +00001529 DeclsInGroup.data(),
1530 DeclsInGroup.size());
Chris Lattner53361ac2006-08-10 05:19:57 +00001531}
1532
Richard Smith02e85f32011-04-14 22:09:26 +00001533/// Parse an optional simple-asm-expr and attributes, and attach them to a
1534/// declarator. Returns true on an error.
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001535bool Parser::ParseAsmAttributesAfterDeclarator(Declarator &D) {
Richard Smith02e85f32011-04-14 22:09:26 +00001536 // If a simple-asm-expr is present, parse it.
1537 if (Tok.is(tok::kw_asm)) {
1538 SourceLocation Loc;
1539 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
1540 if (AsmLabel.isInvalid()) {
1541 SkipUntil(tok::semi, true, true);
1542 return true;
1543 }
1544
1545 D.setAsmLabel(AsmLabel.release());
1546 D.SetRangeEnd(Loc);
1547 }
1548
1549 MaybeParseGNUAttributes(D);
1550 return false;
1551}
1552
Douglas Gregor23996282009-05-12 21:31:51 +00001553/// \brief Parse 'declaration' after parsing 'declaration-specifiers
1554/// declarator'. This method parses the remainder of the declaration
1555/// (including any attributes or initializer, among other things) and
1556/// finalizes the declaration.
Chris Lattnerf0f3baa2006-08-14 00:15:20 +00001557///
Chris Lattnerf0f3baa2006-08-14 00:15:20 +00001558/// init-declarator: [C99 6.7]
1559/// declarator
1560/// declarator '=' initializer
Chris Lattner6d7e6342006-08-15 03:41:14 +00001561/// [GNU] declarator simple-asm-expr[opt] attributes[opt]
1562/// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00001563/// [C++] declarator initializer[opt]
1564///
1565/// [C++] initializer:
1566/// [C++] '=' initializer-clause
1567/// [C++] '(' expression-list ')'
Sebastian Redlf769df52009-03-24 22:27:57 +00001568/// [C++0x] '=' 'default' [TODO]
1569/// [C++0x] '=' 'delete'
Sebastian Redl3da34892011-06-05 12:23:16 +00001570/// [C++0x] braced-init-list
Sebastian Redlf769df52009-03-24 22:27:57 +00001571///
1572/// According to the standard grammar, =default and =delete are function
1573/// definitions, but that definitely doesn't fit with the parser here.
Chris Lattnerf0f3baa2006-08-14 00:15:20 +00001574///
John McCall48871652010-08-21 09:40:31 +00001575Decl *Parser::ParseDeclarationAfterDeclarator(Declarator &D,
Douglas Gregorb52fabb2009-06-23 23:11:28 +00001576 const ParsedTemplateInfo &TemplateInfo) {
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001577 if (ParseAsmAttributesAfterDeclarator(D))
Richard Smith02e85f32011-04-14 22:09:26 +00001578 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00001579
Richard Smith02e85f32011-04-14 22:09:26 +00001580 return ParseDeclarationAfterDeclaratorAndAttributes(D, TemplateInfo);
1581}
Mike Stump11289f42009-09-09 15:08:12 +00001582
Richard Smith02e85f32011-04-14 22:09:26 +00001583Decl *Parser::ParseDeclarationAfterDeclaratorAndAttributes(Declarator &D,
1584 const ParsedTemplateInfo &TemplateInfo) {
Douglas Gregor23996282009-05-12 21:31:51 +00001585 // Inform the current actions module that we just parsed this declarator.
John McCall48871652010-08-21 09:40:31 +00001586 Decl *ThisDecl = 0;
Douglas Gregor450f00842009-09-25 18:43:00 +00001587 switch (TemplateInfo.Kind) {
1588 case ParsedTemplateInfo::NonTemplate:
Douglas Gregor0be31a22010-07-02 17:43:08 +00001589 ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
Douglas Gregor450f00842009-09-25 18:43:00 +00001590 break;
Chad Rosierc1183952012-06-26 22:30:43 +00001591
Douglas Gregor450f00842009-09-25 18:43:00 +00001592 case ParsedTemplateInfo::Template:
1593 case ParsedTemplateInfo::ExplicitSpecialization:
Douglas Gregor0be31a22010-07-02 17:43:08 +00001594 ThisDecl = Actions.ActOnTemplateDeclarator(getCurScope(),
Benjamin Kramercc4c49d2012-08-23 23:38:35 +00001595 *TemplateInfo.TemplateParams,
Douglas Gregor450f00842009-09-25 18:43:00 +00001596 D);
1597 break;
Chad Rosierc1183952012-06-26 22:30:43 +00001598
Douglas Gregor450f00842009-09-25 18:43:00 +00001599 case ParsedTemplateInfo::ExplicitInstantiation: {
Chad Rosierc1183952012-06-26 22:30:43 +00001600 DeclResult ThisRes
Douglas Gregor0be31a22010-07-02 17:43:08 +00001601 = Actions.ActOnExplicitInstantiation(getCurScope(),
Douglas Gregor450f00842009-09-25 18:43:00 +00001602 TemplateInfo.ExternLoc,
1603 TemplateInfo.TemplateLoc,
1604 D);
1605 if (ThisRes.isInvalid()) {
1606 SkipUntil(tok::semi, true, true);
John McCall48871652010-08-21 09:40:31 +00001607 return 0;
Douglas Gregor450f00842009-09-25 18:43:00 +00001608 }
Chad Rosierc1183952012-06-26 22:30:43 +00001609
Douglas Gregor450f00842009-09-25 18:43:00 +00001610 ThisDecl = ThisRes.get();
1611 break;
1612 }
1613 }
Mike Stump11289f42009-09-09 15:08:12 +00001614
Richard Smith30482bc2011-02-20 03:19:35 +00001615 bool TypeContainsAuto =
1616 D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto;
1617
Douglas Gregor23996282009-05-12 21:31:51 +00001618 // Parse declarator '=' initializer.
Richard Trieuc64d3232012-01-18 22:54:52 +00001619 // If a '==' or '+=' is found, suggest a fixit to '='.
Richard Trieu4972a6d2012-01-19 22:01:51 +00001620 if (isTokenEqualOrEqualTypo()) {
Douglas Gregor23996282009-05-12 21:31:51 +00001621 ConsumeToken();
Anders Carlsson991285e2010-09-24 21:25:25 +00001622 if (Tok.is(tok::kw_delete)) {
Alexis Hunt5a7fa252011-05-12 06:15:49 +00001623 if (D.isFunctionDeclarator())
1624 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
1625 << 1 /* delete */;
1626 else
1627 Diag(ConsumeToken(), diag::err_deleted_non_function);
Alexis Hunt5dafebc2011-05-06 01:42:00 +00001628 } else if (Tok.is(tok::kw_default)) {
Alexis Hunt5a7fa252011-05-12 06:15:49 +00001629 if (D.isFunctionDeclarator())
Sebastian Redl46afb552012-02-11 23:51:21 +00001630 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
1631 << 0 /* default */;
Alexis Hunt5a7fa252011-05-12 06:15:49 +00001632 else
1633 Diag(ConsumeToken(), diag::err_default_special_members);
Douglas Gregor23996282009-05-12 21:31:51 +00001634 } else {
David Blaikiebbafb8a2012-03-11 07:00:24 +00001635 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
John McCall1f4ee7b2009-12-19 09:28:58 +00001636 EnterScope(0);
Douglas Gregor0be31a22010-07-02 17:43:08 +00001637 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
John McCall1f4ee7b2009-12-19 09:28:58 +00001638 }
Argyrios Kyrtzidis3df19782009-06-17 22:50:06 +00001639
Douglas Gregor7aa6b222010-05-30 01:49:25 +00001640 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001641 Actions.CodeCompleteInitializer(getCurScope(), ThisDecl);
Peter Collingbourne6b4fdc22012-07-27 12:56:09 +00001642 Actions.FinalizeDeclaration(ThisDecl);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001643 cutOffParsing();
1644 return 0;
Douglas Gregor7aa6b222010-05-30 01:49:25 +00001645 }
Chad Rosierc1183952012-06-26 22:30:43 +00001646
John McCalldadc5752010-08-24 06:29:42 +00001647 ExprResult Init(ParseInitializer());
Argyrios Kyrtzidis3df19782009-06-17 22:50:06 +00001648
David Blaikiebbafb8a2012-03-11 07:00:24 +00001649 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001650 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
John McCall1f4ee7b2009-12-19 09:28:58 +00001651 ExitScope();
1652 }
Argyrios Kyrtzidis3df19782009-06-17 22:50:06 +00001653
Douglas Gregor23996282009-05-12 21:31:51 +00001654 if (Init.isInvalid()) {
Douglas Gregor604c3022010-03-01 18:27:54 +00001655 SkipUntil(tok::comma, true, true);
1656 Actions.ActOnInitializerError(ThisDecl);
1657 } else
Richard Smith30482bc2011-02-20 03:19:35 +00001658 Actions.AddInitializerToDecl(ThisDecl, Init.take(),
1659 /*DirectInit=*/false, TypeContainsAuto);
Douglas Gregor23996282009-05-12 21:31:51 +00001660 }
1661 } else if (Tok.is(tok::l_paren)) {
1662 // Parse C++ direct initializer: '(' expression-list ')'
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001663 BalancedDelimiterTracker T(*this, tok::l_paren);
1664 T.consumeOpen();
1665
Benjamin Kramerf0623432012-08-23 22:51:59 +00001666 ExprVector Exprs;
Douglas Gregor23996282009-05-12 21:31:51 +00001667 CommaLocsTy CommaLocs;
1668
David Blaikiebbafb8a2012-03-11 07:00:24 +00001669 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor613bf102009-12-22 17:47:17 +00001670 EnterScope(0);
Douglas Gregor0be31a22010-07-02 17:43:08 +00001671 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregor613bf102009-12-22 17:47:17 +00001672 }
1673
Douglas Gregor23996282009-05-12 21:31:51 +00001674 if (ParseExpressionList(Exprs, CommaLocs)) {
1675 SkipUntil(tok::r_paren);
Douglas Gregor613bf102009-12-22 17:47:17 +00001676
David Blaikiebbafb8a2012-03-11 07:00:24 +00001677 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001678 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregor613bf102009-12-22 17:47:17 +00001679 ExitScope();
1680 }
Douglas Gregor23996282009-05-12 21:31:51 +00001681 } else {
1682 // Match the ')'.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001683 T.consumeClose();
Douglas Gregor23996282009-05-12 21:31:51 +00001684
1685 assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() &&
1686 "Unexpected number of commas!");
Douglas Gregor613bf102009-12-22 17:47:17 +00001687
David Blaikiebbafb8a2012-03-11 07:00:24 +00001688 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001689 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregor613bf102009-12-22 17:47:17 +00001690 ExitScope();
1691 }
1692
Sebastian Redla9351792012-02-11 23:51:47 +00001693 ExprResult Initializer = Actions.ActOnParenListExpr(T.getOpenLocation(),
1694 T.getCloseLocation(),
Benjamin Kramer62b95d82012-08-23 21:35:17 +00001695 Exprs);
Sebastian Redla9351792012-02-11 23:51:47 +00001696 Actions.AddInitializerToDecl(ThisDecl, Initializer.take(),
1697 /*DirectInit=*/true, TypeContainsAuto);
Douglas Gregor23996282009-05-12 21:31:51 +00001698 }
Fariborz Jahanian8a369a82012-07-03 22:54:28 +00001699 } else if (getLangOpts().CPlusPlus0x && Tok.is(tok::l_brace) &&
Fariborz Jahanian8be1ecd2012-07-03 23:22:13 +00001700 (!CurParsedObjCImpl || !D.isFunctionDeclarator())) {
Sebastian Redl3da34892011-06-05 12:23:16 +00001701 // Parse C++0x braced-init-list.
Richard Smith5d164bc2011-10-15 05:09:34 +00001702 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
1703
Sebastian Redl3da34892011-06-05 12:23:16 +00001704 if (D.getCXXScopeSpec().isSet()) {
1705 EnterScope(0);
1706 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
1707 }
1708
1709 ExprResult Init(ParseBraceInitializer());
1710
1711 if (D.getCXXScopeSpec().isSet()) {
1712 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
1713 ExitScope();
1714 }
1715
1716 if (Init.isInvalid()) {
1717 Actions.ActOnInitializerError(ThisDecl);
1718 } else
1719 Actions.AddInitializerToDecl(ThisDecl, Init.take(),
1720 /*DirectInit=*/true, TypeContainsAuto);
1721
Douglas Gregor23996282009-05-12 21:31:51 +00001722 } else {
Richard Smith30482bc2011-02-20 03:19:35 +00001723 Actions.ActOnUninitializedDecl(ThisDecl, TypeContainsAuto);
Douglas Gregor23996282009-05-12 21:31:51 +00001724 }
1725
Richard Smithb2bc2e62011-02-21 20:05:19 +00001726 Actions.FinalizeDeclaration(ThisDecl);
1727
Douglas Gregor23996282009-05-12 21:31:51 +00001728 return ThisDecl;
1729}
1730
Chris Lattner1890ac82006-08-13 01:16:23 +00001731/// ParseSpecifierQualifierList
1732/// specifier-qualifier-list:
1733/// type-specifier specifier-qualifier-list[opt]
1734/// type-qualifier specifier-qualifier-list[opt]
Chris Lattnere37e2332006-08-15 04:50:22 +00001735/// [GNU] attributes specifier-qualifier-list[opt]
Chris Lattner1890ac82006-08-13 01:16:23 +00001736///
Richard Smithc5b05522012-03-12 07:56:15 +00001737void Parser::ParseSpecifierQualifierList(DeclSpec &DS, AccessSpecifier AS,
1738 DeclSpecContext DSC) {
Chris Lattner1890ac82006-08-13 01:16:23 +00001739 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
1740 /// parse declaration-specifiers and complain about extra stuff.
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00001741 /// TODO: diagnose attribute-specifiers and alignment-specifiers.
Richard Smithc5b05522012-03-12 07:56:15 +00001742 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS, DSC);
Mike Stump11289f42009-09-09 15:08:12 +00001743
Chris Lattner1890ac82006-08-13 01:16:23 +00001744 // Validate declspec for type-name.
1745 unsigned Specs = DS.getParsedSpecifiers();
Richard Smith2f07ad52012-05-09 20:55:26 +00001746 if ((DSC == DSC_type_specifier || DSC == DSC_trailing) &&
1747 !DS.hasTypeSpecifier()) {
Richard Smithc5b05522012-03-12 07:56:15 +00001748 Diag(Tok, diag::err_expected_type);
1749 DS.SetTypeSpecError();
1750 } else if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers() &&
1751 !DS.hasAttributes()) {
Chris Lattner1890ac82006-08-13 01:16:23 +00001752 Diag(Tok, diag::err_typename_requires_specqual);
Richard Smithc5b05522012-03-12 07:56:15 +00001753 if (!DS.hasTypeSpecifier())
1754 DS.SetTypeSpecError();
1755 }
Mike Stump11289f42009-09-09 15:08:12 +00001756
Chris Lattner1b22eed2006-11-28 05:12:07 +00001757 // Issue diagnostic and remove storage class if present.
Chris Lattner1890ac82006-08-13 01:16:23 +00001758 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
Chris Lattner1b22eed2006-11-28 05:12:07 +00001759 if (DS.getStorageClassSpecLoc().isValid())
1760 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
1761 else
1762 Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass);
Chris Lattnera925dc62006-11-28 04:33:46 +00001763 DS.ClearStorageClassSpecs();
Chris Lattner1890ac82006-08-13 01:16:23 +00001764 }
Mike Stump11289f42009-09-09 15:08:12 +00001765
Chris Lattner1b22eed2006-11-28 05:12:07 +00001766 // Issue diagnostic and remove function specfier if present.
Chris Lattner1890ac82006-08-13 01:16:23 +00001767 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Douglas Gregor61956c42008-10-31 09:07:45 +00001768 if (DS.isInlineSpecified())
1769 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
1770 if (DS.isVirtualSpecified())
1771 Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec);
1772 if (DS.isExplicitSpecified())
1773 Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec);
Chris Lattnera925dc62006-11-28 04:33:46 +00001774 DS.ClearFunctionSpecs();
Chris Lattner1890ac82006-08-13 01:16:23 +00001775 }
Richard Smithc5b05522012-03-12 07:56:15 +00001776
1777 // Issue diagnostic and remove constexpr specfier if present.
1778 if (DS.isConstexprSpecified()) {
1779 Diag(DS.getConstexprSpecLoc(), diag::err_typename_invalid_constexpr);
1780 DS.ClearConstexprSpec();
1781 }
Chris Lattner1890ac82006-08-13 01:16:23 +00001782}
Chris Lattner53361ac2006-08-10 05:19:57 +00001783
Chris Lattner6cc055a2009-04-12 20:42:31 +00001784/// isValidAfterIdentifierInDeclaratorAfterDeclSpec - Return true if the
1785/// specified token is valid after the identifier in a declarator which
1786/// immediately follows the declspec. For example, these things are valid:
1787///
1788/// int x [ 4]; // direct-declarator
1789/// int x ( int y); // direct-declarator
1790/// int(int x ) // direct-declarator
1791/// int x ; // simple-declaration
1792/// int x = 17; // init-declarator-list
1793/// int x , y; // init-declarator-list
1794/// int x __asm__ ("foo"); // init-declarator-list
Chris Lattnera723ba92009-04-14 21:16:09 +00001795/// int x : 4; // struct-declarator
Chris Lattner2b988c12009-04-12 22:29:43 +00001796/// int x { 5}; // C++'0x unified initializers
Chris Lattner6cc055a2009-04-12 20:42:31 +00001797///
1798/// This is not, because 'x' does not immediately follow the declspec (though
1799/// ')' happens to be valid anyway).
1800/// int (x)
1801///
1802static bool isValidAfterIdentifierInDeclarator(const Token &T) {
1803 return T.is(tok::l_square) || T.is(tok::l_paren) || T.is(tok::r_paren) ||
1804 T.is(tok::semi) || T.is(tok::comma) || T.is(tok::equal) ||
Chris Lattnera723ba92009-04-14 21:16:09 +00001805 T.is(tok::kw_asm) || T.is(tok::l_brace) || T.is(tok::colon);
Chris Lattner6cc055a2009-04-12 20:42:31 +00001806}
1807
Chris Lattner20a0c612009-04-14 21:34:55 +00001808
1809/// ParseImplicitInt - This method is called when we have an non-typename
1810/// identifier in a declspec (which normally terminates the decl spec) when
1811/// the declspec has no type specifier. In this case, the declspec is either
1812/// malformed or is "implicit int" (in K&R and C89).
1813///
1814/// This method handles diagnosing this prettily and returns false if the
1815/// declspec is done being processed. If it recovers and thinks there may be
1816/// other pieces of declspec after it, it returns true.
1817///
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001818bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS,
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001819 const ParsedTemplateInfo &TemplateInfo,
Richard Smithc5b05522012-03-12 07:56:15 +00001820 AccessSpecifier AS, DeclSpecContext DSC) {
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001821 assert(Tok.is(tok::identifier) && "should have identifier");
Mike Stump11289f42009-09-09 15:08:12 +00001822
Chris Lattner20a0c612009-04-14 21:34:55 +00001823 SourceLocation Loc = Tok.getLocation();
1824 // If we see an identifier that is not a type name, we normally would
1825 // parse it as the identifer being declared. However, when a typename
1826 // is typo'd or the definition is not included, this will incorrectly
1827 // parse the typename as the identifier name and fall over misparsing
1828 // later parts of the diagnostic.
1829 //
1830 // As such, we try to do some look-ahead in cases where this would
1831 // otherwise be an "implicit-int" case to see if this is invalid. For
1832 // example: "static foo_t x = 4;" In this case, if we parsed foo_t as
1833 // an identifier with implicit int, we'd get a parse error because the
1834 // next token is obviously invalid for a type. Parse these as a case
1835 // with an invalid type specifier.
1836 assert(!DS.hasTypeSpecifier() && "Type specifier checked above");
Mike Stump11289f42009-09-09 15:08:12 +00001837
Chris Lattner20a0c612009-04-14 21:34:55 +00001838 // Since we know that this either implicit int (which is rare) or an
Richard Smitha952ebb2012-05-15 21:01:51 +00001839 // error, do lookahead to try to do better recovery. This never applies
1840 // within a type specifier. Outside of C++, we allow this even if the
1841 // language doesn't "officially" support implicit int -- we support
1842 // implicit int as an extension in C99 and C11. Allegedly, MS also
1843 // supports implicit int in C++ mode.
Richard Smith2f07ad52012-05-09 20:55:26 +00001844 if (DSC != DSC_type_specifier && DSC != DSC_trailing &&
Richard Smitha952ebb2012-05-15 21:01:51 +00001845 (!getLangOpts().CPlusPlus || getLangOpts().MicrosoftExt) &&
Richard Smithc5b05522012-03-12 07:56:15 +00001846 isValidAfterIdentifierInDeclarator(NextToken())) {
Chris Lattner20a0c612009-04-14 21:34:55 +00001847 // If this token is valid for implicit int, e.g. "static x = 4", then
1848 // we just avoid eating the identifier, so it will be parsed as the
1849 // identifier in the declarator.
1850 return false;
1851 }
Mike Stump11289f42009-09-09 15:08:12 +00001852
Richard Smitha952ebb2012-05-15 21:01:51 +00001853 if (getLangOpts().CPlusPlus &&
1854 DS.getStorageClassSpec() == DeclSpec::SCS_auto) {
1855 // Don't require a type specifier if we have the 'auto' storage class
1856 // specifier in C++98 -- we'll promote it to a type specifier.
1857 return false;
1858 }
1859
Chris Lattner20a0c612009-04-14 21:34:55 +00001860 // Otherwise, if we don't consume this token, we are going to emit an
1861 // error anyway. Try to recover from various common problems. Check
1862 // to see if this was a reference to a tag name without a tag specified.
1863 // This is a common problem in C (saying 'foo' instead of 'struct foo').
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001864 //
1865 // C++ doesn't need this, and isTagName doesn't take SS.
1866 if (SS == 0) {
Argyrios Kyrtzidis1f329402011-04-21 17:29:47 +00001867 const char *TagName = 0, *FixitTagName = 0;
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001868 tok::TokenKind TagKind = tok::unknown;
Mike Stump11289f42009-09-09 15:08:12 +00001869
Douglas Gregor0be31a22010-07-02 17:43:08 +00001870 switch (Actions.isTagName(*Tok.getIdentifierInfo(), getCurScope())) {
Chris Lattner20a0c612009-04-14 21:34:55 +00001871 default: break;
Argyrios Kyrtzidis1f329402011-04-21 17:29:47 +00001872 case DeclSpec::TST_enum:
1873 TagName="enum" ; FixitTagName = "enum " ; TagKind=tok::kw_enum ;break;
1874 case DeclSpec::TST_union:
1875 TagName="union" ; FixitTagName = "union " ;TagKind=tok::kw_union ;break;
1876 case DeclSpec::TST_struct:
1877 TagName="struct"; FixitTagName = "struct ";TagKind=tok::kw_struct;break;
Joao Matosdc86f942012-08-31 18:45:21 +00001878 case DeclSpec::TST_interface:
1879 TagName="__interface"; FixitTagName = "__interface ";
1880 TagKind=tok::kw___interface;break;
Argyrios Kyrtzidis1f329402011-04-21 17:29:47 +00001881 case DeclSpec::TST_class:
1882 TagName="class" ; FixitTagName = "class " ;TagKind=tok::kw_class ;break;
Chris Lattner20a0c612009-04-14 21:34:55 +00001883 }
Mike Stump11289f42009-09-09 15:08:12 +00001884
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001885 if (TagName) {
Kaelyn Uhrain031643e2012-04-26 23:36:17 +00001886 IdentifierInfo *TokenName = Tok.getIdentifierInfo();
1887 LookupResult R(Actions, TokenName, SourceLocation(),
1888 Sema::LookupOrdinaryName);
1889
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001890 Diag(Loc, diag::err_use_of_tag_name_without_tag)
Kaelyn Uhrain031643e2012-04-26 23:36:17 +00001891 << TokenName << TagName << getLangOpts().CPlusPlus
1892 << FixItHint::CreateInsertion(Tok.getLocation(), FixitTagName);
1893
1894 if (Actions.LookupParsedName(R, getCurScope(), SS)) {
1895 for (LookupResult::iterator I = R.begin(), IEnd = R.end();
1896 I != IEnd; ++I)
Kaelyn Uhrain3fe3f852012-04-27 18:26:49 +00001897 Diag((*I)->getLocation(), diag::note_decl_hiding_tag_type)
Kaelyn Uhrain031643e2012-04-26 23:36:17 +00001898 << TokenName << TagName;
1899 }
Mike Stump11289f42009-09-09 15:08:12 +00001900
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001901 // Parse this as a tag as if the missing tag were present.
1902 if (TagKind == tok::kw_enum)
Richard Smithc5b05522012-03-12 07:56:15 +00001903 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS, DSC_normal);
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001904 else
Richard Smithc5b05522012-03-12 07:56:15 +00001905 ParseClassSpecifier(TagKind, Loc, DS, TemplateInfo, AS,
1906 /*EnteringContext*/ false, DSC_normal);
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001907 return true;
1908 }
Chris Lattner20a0c612009-04-14 21:34:55 +00001909 }
Mike Stump11289f42009-09-09 15:08:12 +00001910
Richard Smithfe904f02012-05-15 21:29:55 +00001911 // Determine whether this identifier could plausibly be the name of something
Richard Smithedd124e2012-05-15 21:42:17 +00001912 // being declared (with a missing type).
Richard Smithfe904f02012-05-15 21:29:55 +00001913 if (DSC != DSC_type_specifier && DSC != DSC_trailing &&
1914 (!SS || DSC == DSC_top_level || DSC == DSC_class)) {
Richard Smitha952ebb2012-05-15 21:01:51 +00001915 // Look ahead to the next token to try to figure out what this declaration
1916 // was supposed to be.
1917 switch (NextToken().getKind()) {
1918 case tok::comma:
1919 case tok::equal:
1920 case tok::kw_asm:
1921 case tok::l_brace:
1922 case tok::l_square:
1923 case tok::semi:
1924 // This looks like a variable declaration. The type is probably missing.
1925 // We're done parsing decl-specifiers.
1926 return false;
1927
1928 case tok::l_paren: {
1929 // static x(4); // 'x' is not a type
1930 // x(int n); // 'x' is not a type
1931 // x (*p)[]; // 'x' is a type
1932 //
1933 // Since we're in an error case (or the rare 'implicit int in C++' MS
1934 // extension), we can afford to perform a tentative parse to determine
1935 // which case we're in.
1936 TentativeParsingAction PA(*this);
1937 ConsumeToken();
1938 TPResult TPR = TryParseDeclarator(/*mayBeAbstract*/false);
1939 PA.Revert();
1940 if (TPR == TPResult::False())
1941 return false;
1942 // The identifier is followed by a parenthesized declarator.
1943 // It's supposed to be a type.
1944 break;
1945 }
1946
1947 default:
1948 // This is probably supposed to be a type. This includes cases like:
1949 // int f(itn);
1950 // struct S { unsinged : 4; };
1951 break;
1952 }
1953 }
1954
Chad Rosierc1183952012-06-26 22:30:43 +00001955 // This is almost certainly an invalid type name. Let the action emit a
Douglas Gregor15e56022009-10-13 23:27:22 +00001956 // diagnostic and attempt to recover.
John McCallba7bf592010-08-24 05:47:05 +00001957 ParsedType T;
Kaelyn Uhrainb5b17fe2012-06-15 23:45:58 +00001958 IdentifierInfo *II = Tok.getIdentifierInfo();
1959 if (Actions.DiagnoseUnknownTypeName(II, Loc, getCurScope(), SS, T)) {
Douglas Gregor15e56022009-10-13 23:27:22 +00001960 // The action emitted a diagnostic, so we don't have to.
1961 if (T) {
1962 // The action has suggested that the type T could be used. Set that as
1963 // the type in the declaration specifiers, consume the would-be type
1964 // name token, and we're done.
1965 const char *PrevSpec;
1966 unsigned DiagID;
John McCallba7bf592010-08-24 05:47:05 +00001967 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID, T);
Douglas Gregor15e56022009-10-13 23:27:22 +00001968 DS.SetRangeEnd(Tok.getLocation());
1969 ConsumeToken();
Kaelyn Uhrainb5b17fe2012-06-15 23:45:58 +00001970 // There may be other declaration specifiers after this.
1971 return true;
1972 } else if (II != Tok.getIdentifierInfo()) {
1973 // If no type was suggested, the correction is to a keyword
1974 Tok.setKind(II->getTokenID());
Douglas Gregor15e56022009-10-13 23:27:22 +00001975 // There may be other declaration specifiers after this.
1976 return true;
1977 }
Chad Rosierc1183952012-06-26 22:30:43 +00001978
Douglas Gregor15e56022009-10-13 23:27:22 +00001979 // Fall through; the action had no suggestion for us.
1980 } else {
1981 // The action did not emit a diagnostic, so emit one now.
1982 SourceRange R;
1983 if (SS) R = SS->getRange();
1984 Diag(Loc, diag::err_unknown_typename) << Tok.getIdentifierInfo() << R;
1985 }
Mike Stump11289f42009-09-09 15:08:12 +00001986
Douglas Gregor15e56022009-10-13 23:27:22 +00001987 // Mark this as an error.
Richard Smithc5b05522012-03-12 07:56:15 +00001988 DS.SetTypeSpecError();
Chris Lattner20a0c612009-04-14 21:34:55 +00001989 DS.SetRangeEnd(Tok.getLocation());
1990 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001991
Chris Lattner20a0c612009-04-14 21:34:55 +00001992 // TODO: Could inject an invalid typedef decl in an enclosing scope to
1993 // avoid rippling error messages on subsequent uses of the same type,
1994 // could be useful if #include was forgotten.
1995 return false;
1996}
1997
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001998/// \brief Determine the declaration specifier context from the declarator
1999/// context.
2000///
2001/// \param Context the declarator context, which is one of the
2002/// Declarator::TheContext enumerator values.
Chad Rosierc1183952012-06-26 22:30:43 +00002003Parser::DeclSpecContext
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002004Parser::getDeclSpecContextFromDeclaratorContext(unsigned Context) {
2005 if (Context == Declarator::MemberContext)
2006 return DSC_class;
2007 if (Context == Declarator::FileContext)
2008 return DSC_top_level;
Richard Smith62dad822012-03-15 01:02:11 +00002009 if (Context == Declarator::TrailingReturnContext)
2010 return DSC_trailing;
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002011 return DSC_normal;
2012}
2013
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002014/// ParseAlignArgument - Parse the argument to an alignment-specifier.
2015///
2016/// FIXME: Simply returns an alignof() expression if the argument is a
2017/// type. Ideally, the type should be propagated directly into Sema.
2018///
Benjamin Kramere56f3932011-12-23 17:00:35 +00002019/// [C11] type-id
2020/// [C11] constant-expression
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002021/// [C++0x] type-id ...[opt]
2022/// [C++0x] assignment-expression ...[opt]
2023ExprResult Parser::ParseAlignArgument(SourceLocation Start,
2024 SourceLocation &EllipsisLoc) {
2025 ExprResult ER;
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002026 if (isTypeIdInParens()) {
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002027 SourceLocation TypeLoc = Tok.getLocation();
2028 ParsedType Ty = ParseTypeName().get();
2029 SourceRange TypeRange(Start, Tok.getLocation());
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002030 ER = Actions.ActOnUnaryExprOrTypeTraitExpr(TypeLoc, UETT_AlignOf, true,
2031 Ty.getAsOpaquePtr(), TypeRange);
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002032 } else
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002033 ER = ParseConstantExpression();
2034
David Blaikiebbafb8a2012-03-11 07:00:24 +00002035 if (getLangOpts().CPlusPlus0x && Tok.is(tok::ellipsis))
Peter Collingbourneccbcce02011-10-24 17:56:00 +00002036 EllipsisLoc = ConsumeToken();
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002037
2038 return ER;
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002039}
2040
2041/// ParseAlignmentSpecifier - Parse an alignment-specifier, and add the
2042/// attribute to Attrs.
2043///
2044/// alignment-specifier:
Benjamin Kramere56f3932011-12-23 17:00:35 +00002045/// [C11] '_Alignas' '(' type-id ')'
2046/// [C11] '_Alignas' '(' constant-expression ')'
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002047/// [C++0x] 'alignas' '(' type-id ...[opt] ')'
2048/// [C++0x] 'alignas' '(' assignment-expression ...[opt] ')'
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002049void Parser::ParseAlignmentSpecifier(ParsedAttributes &Attrs,
2050 SourceLocation *endLoc) {
2051 assert((Tok.is(tok::kw_alignas) || Tok.is(tok::kw__Alignas)) &&
2052 "Not an alignment-specifier!");
2053
2054 SourceLocation KWLoc = Tok.getLocation();
2055 ConsumeToken();
2056
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002057 BalancedDelimiterTracker T(*this, tok::l_paren);
2058 if (T.expectAndConsume(diag::err_expected_lparen))
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002059 return;
2060
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002061 SourceLocation EllipsisLoc;
2062 ExprResult ArgExpr = ParseAlignArgument(T.getOpenLocation(), EllipsisLoc);
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002063 if (ArgExpr.isInvalid()) {
2064 SkipUntil(tok::r_paren);
2065 return;
2066 }
2067
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002068 T.consumeClose();
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002069 if (endLoc)
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002070 *endLoc = T.getCloseLocation();
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002071
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002072 // FIXME: Handle pack-expansions here.
2073 if (EllipsisLoc.isValid()) {
2074 Diag(EllipsisLoc, diag::err_alignas_pack_exp_unsupported);
2075 return;
2076 }
2077
Benjamin Kramerf0623432012-08-23 22:51:59 +00002078 ExprVector ArgExprs;
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002079 ArgExprs.push_back(ArgExpr.release());
Alexis Hunt3bc72c12012-06-19 23:57:03 +00002080 // FIXME: This should not be GNU, but we since the attribute used is
2081 // based on the spelling, and there is no true spelling for
2082 // C++11 attributes, this isn't accepted.
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002083 Attrs.addNew(PP.getIdentifierInfo("aligned"), KWLoc, 0, KWLoc,
Benjamin Kramerf0623432012-08-23 22:51:59 +00002084 0, T.getOpenLocation(), ArgExprs.data(), 1,
Alexis Hunt3bc72c12012-06-19 23:57:03 +00002085 AttributeList::AS_GNU);
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002086}
2087
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002088/// ParseDeclarationSpecifiers
2089/// declaration-specifiers: [C99 6.7]
Chris Lattner3b561a32006-08-13 00:12:11 +00002090/// storage-class-specifier declaration-specifiers[opt]
2091/// type-specifier declaration-specifiers[opt]
Chris Lattner3b561a32006-08-13 00:12:11 +00002092/// [C99] function-specifier declaration-specifiers[opt]
Benjamin Kramere56f3932011-12-23 17:00:35 +00002093/// [C11] alignment-specifier declaration-specifiers[opt]
Chris Lattnere37e2332006-08-15 04:50:22 +00002094/// [GNU] attributes declaration-specifiers[opt]
Douglas Gregor26701a42011-09-09 02:06:17 +00002095/// [Clang] '__module_private__' declaration-specifiers[opt]
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002096///
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002097/// storage-class-specifier: [C99 6.7.1]
Chris Lattnerda48a8e2006-08-04 05:25:55 +00002098/// 'typedef'
2099/// 'extern'
2100/// 'static'
2101/// 'auto'
2102/// 'register'
Sebastian Redlccdfaba2008-11-14 23:42:31 +00002103/// [C++] 'mutable'
Chris Lattnerda48a8e2006-08-04 05:25:55 +00002104/// [GNU] '__thread'
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002105/// function-specifier: [C99 6.7.4]
Chris Lattner3b561a32006-08-13 00:12:11 +00002106/// [C99] 'inline'
Douglas Gregor61956c42008-10-31 09:07:45 +00002107/// [C++] 'virtual'
2108/// [C++] 'explicit'
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00002109/// [OpenCL] '__kernel'
Anders Carlssoncd8db412009-05-06 04:46:28 +00002110/// 'friend': [C++ dcl.friend]
Sebastian Redl39c2a8b2009-11-05 15:47:02 +00002111/// 'constexpr': [C++0x dcl.constexpr]
Anders Carlssoncd8db412009-05-06 04:46:28 +00002112
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002113///
Douglas Gregorb9bd8a92008-12-24 02:52:09 +00002114void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
Douglas Gregor1b57ff32009-05-12 23:25:50 +00002115 const ParsedTemplateInfo &TemplateInfo,
John McCall07e91c02009-08-06 02:15:43 +00002116 AccessSpecifier AS,
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +00002117 DeclSpecContext DSContext,
2118 LateParsedAttrList *LateAttrs) {
Douglas Gregor0e7dde52011-04-24 05:37:28 +00002119 if (DS.getSourceRange().isInvalid()) {
2120 DS.SetRangeStart(Tok.getLocation());
2121 DS.SetRangeEnd(Tok.getLocation());
2122 }
Chad Rosierc1183952012-06-26 22:30:43 +00002123
Douglas Gregordf593fb2011-11-07 17:33:42 +00002124 bool EnteringContext = (DSContext == DSC_class || DSContext == DSC_top_level);
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00002125 bool AttrsLastTime = false;
2126 ParsedAttributesWithRange attrs(AttrFactory);
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002127 while (1) {
John McCall49bfce42009-08-03 20:12:06 +00002128 bool isInvalid = false;
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002129 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00002130 unsigned DiagID = 0;
2131
Chris Lattner4d8f8732006-11-28 05:05:08 +00002132 SourceLocation Loc = Tok.getLocation();
Douglas Gregor450c75a2008-11-07 15:42:26 +00002133
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002134 switch (Tok.getKind()) {
Mike Stump11289f42009-09-09 15:08:12 +00002135 default:
Chris Lattner0974b232008-07-26 00:20:22 +00002136 DoneWithDeclSpec:
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00002137 if (!AttrsLastTime)
2138 ProhibitAttributes(attrs);
2139 else
2140 DS.takeAttributesFrom(attrs);
Peter Collingbourne70188b32011-09-29 18:03:57 +00002141
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002142 // If this is not a declaration specifier token, we're done reading decl
2143 // specifiers. First verify that DeclSpec's are consistent.
Douglas Gregore3e01a22009-04-01 22:41:11 +00002144 DS.Finish(Diags, PP);
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002145 return;
Mike Stump11289f42009-09-09 15:08:12 +00002146
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00002147 case tok::l_square:
2148 case tok::kw_alignas:
2149 if (!isCXX11AttributeSpecifier())
2150 goto DoneWithDeclSpec;
2151
2152 ProhibitAttributes(attrs);
2153 // FIXME: It would be good to recover by accepting the attributes,
2154 // but attempting to do that now would cause serious
2155 // madness in terms of diagnostics.
2156 attrs.clear();
2157 attrs.Range = SourceRange();
2158
2159 ParseCXX11Attributes(attrs);
2160 AttrsLastTime = true;
Chad Rosierc1183952012-06-26 22:30:43 +00002161 continue;
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00002162
Douglas Gregorc49f5b22010-08-23 18:23:48 +00002163 case tok::code_completion: {
John McCallfaf5fb42010-08-26 23:41:50 +00002164 Sema::ParserCompletionContext CCC = Sema::PCC_Namespace;
Douglas Gregorc49f5b22010-08-23 18:23:48 +00002165 if (DS.hasTypeSpecifier()) {
2166 bool AllowNonIdentifiers
2167 = (getCurScope()->getFlags() & (Scope::ControlScope |
2168 Scope::BlockScope |
2169 Scope::TemplateParamScope |
2170 Scope::FunctionPrototypeScope |
2171 Scope::AtCatchScope)) == 0;
2172 bool AllowNestedNameSpecifiers
Chad Rosierc1183952012-06-26 22:30:43 +00002173 = DSContext == DSC_top_level ||
Douglas Gregorc49f5b22010-08-23 18:23:48 +00002174 (DSContext == DSC_class && DS.isFriendSpecified());
2175
Douglas Gregorbfcea8b2010-09-16 15:14:18 +00002176 Actions.CodeCompleteDeclSpec(getCurScope(), DS,
Chad Rosierc1183952012-06-26 22:30:43 +00002177 AllowNonIdentifiers,
Douglas Gregorbfcea8b2010-09-16 15:14:18 +00002178 AllowNestedNameSpecifiers);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002179 return cutOffParsing();
Chad Rosierc1183952012-06-26 22:30:43 +00002180 }
2181
Douglas Gregor80039242011-02-15 20:33:25 +00002182 if (getCurScope()->getFnParent() || getCurScope()->getBlockParent())
2183 CCC = Sema::PCC_LocalDeclarationSpecifiers;
2184 else if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate)
Chad Rosierc1183952012-06-26 22:30:43 +00002185 CCC = DSContext == DSC_class? Sema::PCC_MemberTemplate
John McCallfaf5fb42010-08-26 23:41:50 +00002186 : Sema::PCC_Template;
Douglas Gregorc49f5b22010-08-23 18:23:48 +00002187 else if (DSContext == DSC_class)
John McCallfaf5fb42010-08-26 23:41:50 +00002188 CCC = Sema::PCC_Class;
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002189 else if (CurParsedObjCImpl)
John McCallfaf5fb42010-08-26 23:41:50 +00002190 CCC = Sema::PCC_ObjCImplementation;
Chad Rosierc1183952012-06-26 22:30:43 +00002191
Douglas Gregorc49f5b22010-08-23 18:23:48 +00002192 Actions.CodeCompleteOrdinaryName(getCurScope(), CCC);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002193 return cutOffParsing();
Douglas Gregorc49f5b22010-08-23 18:23:48 +00002194 }
2195
Chris Lattnerbd31aa32009-01-05 00:07:25 +00002196 case tok::coloncolon: // ::foo::bar
John McCall1f476a12010-02-26 08:45:28 +00002197 // C++ scope specifier. Annotate and loop, or bail out on error.
2198 if (TryAnnotateCXXScopeToken(true)) {
2199 if (!DS.hasTypeSpecifier())
2200 DS.SetTypeSpecError();
2201 goto DoneWithDeclSpec;
2202 }
John McCall8bc2a702010-03-01 18:20:46 +00002203 if (Tok.is(tok::coloncolon)) // ::new or ::delete
2204 goto DoneWithDeclSpec;
John McCall1f476a12010-02-26 08:45:28 +00002205 continue;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002206
2207 case tok::annot_cxxscope: {
Richard Smith3092a3b2012-05-09 18:56:43 +00002208 if (DS.hasTypeSpecifier() || DS.isTypeAltiVecVector())
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002209 goto DoneWithDeclSpec;
2210
John McCall9dab4e62009-12-12 11:40:51 +00002211 CXXScopeSpec SS;
Douglas Gregor869ad452011-02-24 17:54:50 +00002212 Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(),
2213 Tok.getAnnotationRange(),
2214 SS);
John McCall9dab4e62009-12-12 11:40:51 +00002215
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002216 // We are looking for a qualified typename.
Douglas Gregor167fa622009-03-25 15:40:00 +00002217 Token Next = NextToken();
Mike Stump11289f42009-09-09 15:08:12 +00002218 if (Next.is(tok::annot_template_id) &&
Douglas Gregor167fa622009-03-25 15:40:00 +00002219 static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue())
Douglas Gregorb67535d2009-03-31 00:43:58 +00002220 ->Kind == TNK_Type_template) {
Douglas Gregor167fa622009-03-25 15:40:00 +00002221 // We have a qualified template-id, e.g., N::A<int>
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002222
2223 // C++ [class.qual]p2:
2224 // In a lookup in which the constructor is an acceptable lookup
2225 // result and the nested-name-specifier nominates a class C:
2226 //
2227 // - if the name specified after the
2228 // nested-name-specifier, when looked up in C, is the
2229 // injected-class-name of C (Clause 9), or
2230 //
2231 // - if the name specified after the nested-name-specifier
2232 // is the same as the identifier or the
2233 // simple-template-id's template-name in the last
2234 // component of the nested-name-specifier,
2235 //
2236 // the name is instead considered to name the constructor of
2237 // class C.
Chad Rosierc1183952012-06-26 22:30:43 +00002238 //
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002239 // Thus, if the template-name is actually the constructor
2240 // name, then the code is ill-formed; this interpretation is
Chad Rosierc1183952012-06-26 22:30:43 +00002241 // reinforced by the NAD status of core issue 635.
Argyrios Kyrtzidisc0c5dd22011-06-22 06:09:49 +00002242 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Next);
John McCall84821e72010-04-13 06:39:49 +00002243 if ((DSContext == DSC_top_level ||
2244 (DSContext == DSC_class && DS.isFriendSpecified())) &&
2245 TemplateId->Name &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00002246 Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) {
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002247 if (isConstructorDeclarator()) {
2248 // The user meant this to be an out-of-line constructor
2249 // definition, but template arguments are not allowed
2250 // there. Just allow this as a constructor; we'll
2251 // complain about it later.
2252 goto DoneWithDeclSpec;
2253 }
2254
2255 // The user meant this to name a type, but it actually names
2256 // a constructor with some extraneous template
2257 // arguments. Complain, then parse it as a type as the user
2258 // intended.
2259 Diag(TemplateId->TemplateNameLoc,
2260 diag::err_out_of_line_template_id_names_constructor)
2261 << TemplateId->Name;
2262 }
2263
John McCall9dab4e62009-12-12 11:40:51 +00002264 DS.getTypeSpecScope() = SS;
2265 ConsumeToken(); // The C++ scope.
Mike Stump11289f42009-09-09 15:08:12 +00002266 assert(Tok.is(tok::annot_template_id) &&
Douglas Gregor167fa622009-03-25 15:40:00 +00002267 "ParseOptionalCXXScopeSpecifier not working");
Douglas Gregore7c20652011-03-02 00:47:37 +00002268 AnnotateTemplateIdTokenAsType();
Douglas Gregor167fa622009-03-25 15:40:00 +00002269 continue;
2270 }
2271
Douglas Gregorc5790df2009-09-28 07:26:33 +00002272 if (Next.is(tok::annot_typename)) {
John McCall9dab4e62009-12-12 11:40:51 +00002273 DS.getTypeSpecScope() = SS;
2274 ConsumeToken(); // The C++ scope.
John McCallba7bf592010-08-24 05:47:05 +00002275 if (Tok.getAnnotationValue()) {
2276 ParsedType T = getTypeAnnotation(Tok);
Nico Weber77430342010-11-22 10:30:56 +00002277 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename,
Chad Rosierc1183952012-06-26 22:30:43 +00002278 Tok.getAnnotationEndLoc(),
John McCallba7bf592010-08-24 05:47:05 +00002279 PrevSpec, DiagID, T);
Richard Smithda837032012-09-14 18:27:01 +00002280 if (isInvalid)
2281 break;
John McCallba7bf592010-08-24 05:47:05 +00002282 }
Douglas Gregorc5790df2009-09-28 07:26:33 +00002283 else
2284 DS.SetTypeSpecError();
2285 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
2286 ConsumeToken(); // The typename
2287 }
2288
Douglas Gregor167fa622009-03-25 15:40:00 +00002289 if (Next.isNot(tok::identifier))
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002290 goto DoneWithDeclSpec;
2291
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002292 // If we're in a context where the identifier could be a class name,
2293 // check whether this is a constructor declaration.
John McCall84821e72010-04-13 06:39:49 +00002294 if ((DSContext == DSC_top_level ||
2295 (DSContext == DSC_class && DS.isFriendSpecified())) &&
Chad Rosierc1183952012-06-26 22:30:43 +00002296 Actions.isCurrentClassName(*Next.getIdentifierInfo(), getCurScope(),
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002297 &SS)) {
2298 if (isConstructorDeclarator())
2299 goto DoneWithDeclSpec;
2300
2301 // As noted in C++ [class.qual]p2 (cited above), when the name
2302 // of the class is qualified in a context where it could name
2303 // a constructor, its a constructor name. However, we've
2304 // looked at the declarator, and the user probably meant this
2305 // to be a type. Complain that it isn't supposed to be treated
2306 // as a type, then proceed to parse it as a type.
2307 Diag(Next.getLocation(), diag::err_out_of_line_type_names_constructor)
2308 << Next.getIdentifierInfo();
2309 }
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002310
John McCallba7bf592010-08-24 05:47:05 +00002311 ParsedType TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(),
2312 Next.getLocation(),
Douglas Gregor844cb502011-03-01 18:12:44 +00002313 getCurScope(), &SS,
2314 false, false, ParsedType(),
Abramo Bagnara4244b432012-01-27 08:46:19 +00002315 /*IsCtorOrDtorName=*/false,
Douglas Gregor844cb502011-03-01 18:12:44 +00002316 /*NonTrivialSourceInfo=*/true);
Douglas Gregor8bf42052009-02-09 18:46:07 +00002317
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002318 // If the referenced identifier is not a type, then this declspec is
2319 // erroneous: We already checked about that it has no type specifier, and
2320 // C++ doesn't have implicit int. Diagnose it as a typo w.r.t. to the
Mike Stump11289f42009-09-09 15:08:12 +00002321 // typename.
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002322 if (TypeRep == 0) {
2323 ConsumeToken(); // Eat the scope spec so the identifier is current.
Richard Smithc5b05522012-03-12 07:56:15 +00002324 if (ParseImplicitInt(DS, &SS, TemplateInfo, AS, DSContext)) continue;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002325 goto DoneWithDeclSpec;
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002326 }
Mike Stump11289f42009-09-09 15:08:12 +00002327
John McCall9dab4e62009-12-12 11:40:51 +00002328 DS.getTypeSpecScope() = SS;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002329 ConsumeToken(); // The C++ scope.
2330
Douglas Gregor9817f4a2009-02-09 15:09:02 +00002331 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00002332 DiagID, TypeRep);
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002333 if (isInvalid)
2334 break;
Mike Stump11289f42009-09-09 15:08:12 +00002335
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002336 DS.SetRangeEnd(Tok.getLocation());
2337 ConsumeToken(); // The typename.
2338
2339 continue;
2340 }
Mike Stump11289f42009-09-09 15:08:12 +00002341
Chris Lattnere387d9e2009-01-21 19:48:37 +00002342 case tok::annot_typename: {
John McCallba7bf592010-08-24 05:47:05 +00002343 if (Tok.getAnnotationValue()) {
2344 ParsedType T = getTypeAnnotation(Tok);
Nico Weber7f8bb362010-11-22 12:50:03 +00002345 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallba7bf592010-08-24 05:47:05 +00002346 DiagID, T);
2347 } else
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00002348 DS.SetTypeSpecError();
Chad Rosierc1183952012-06-26 22:30:43 +00002349
Chris Lattner005fc1b2010-04-05 18:18:31 +00002350 if (isInvalid)
2351 break;
2352
Chris Lattnere387d9e2009-01-21 19:48:37 +00002353 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
2354 ConsumeToken(); // The typename
Mike Stump11289f42009-09-09 15:08:12 +00002355
Chris Lattnere387d9e2009-01-21 19:48:37 +00002356 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
2357 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Chad Rosierc1183952012-06-26 22:30:43 +00002358 // Objective-C interface.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002359 if (Tok.is(tok::less) && getLangOpts().ObjC1)
Douglas Gregor06e41ae2010-10-21 23:17:00 +00002360 ParseObjCProtocolQualifiers(DS);
Chad Rosierc1183952012-06-26 22:30:43 +00002361
Chris Lattnere387d9e2009-01-21 19:48:37 +00002362 continue;
2363 }
Mike Stump11289f42009-09-09 15:08:12 +00002364
Douglas Gregor06873092011-04-28 15:48:45 +00002365 case tok::kw___is_signed:
2366 // GNU libstdc++ 4.4 uses __is_signed as an identifier, but Clang
2367 // typically treats it as a trait. If we see __is_signed as it appears
2368 // in libstdc++, e.g.,
2369 //
2370 // static const bool __is_signed;
2371 //
2372 // then treat __is_signed as an identifier rather than as a keyword.
2373 if (DS.getTypeSpecType() == TST_bool &&
2374 DS.getTypeQualifiers() == DeclSpec::TQ_const &&
2375 DS.getStorageClassSpec() == DeclSpec::SCS_static) {
2376 Tok.getIdentifierInfo()->RevertTokenIDToIdentifier();
2377 Tok.setKind(tok::identifier);
2378 }
2379
2380 // We're done with the declaration-specifiers.
2381 goto DoneWithDeclSpec;
Chad Rosierc1183952012-06-26 22:30:43 +00002382
Chris Lattner16fac4f2008-07-26 01:18:38 +00002383 // typedef-name
David Blaikie15a430a2011-12-04 05:04:18 +00002384 case tok::kw_decltype:
Chris Lattner16fac4f2008-07-26 01:18:38 +00002385 case tok::identifier: {
Chris Lattnerbd31aa32009-01-05 00:07:25 +00002386 // In C++, check to see if this is a scope specifier like foo::bar::, if
2387 // so handle it as such. This is important for ctor parsing.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002388 if (getLangOpts().CPlusPlus) {
John McCall1f476a12010-02-26 08:45:28 +00002389 if (TryAnnotateCXXScopeToken(true)) {
2390 if (!DS.hasTypeSpecifier())
2391 DS.SetTypeSpecError();
2392 goto DoneWithDeclSpec;
2393 }
2394 if (!Tok.is(tok::identifier))
2395 continue;
2396 }
Mike Stump11289f42009-09-09 15:08:12 +00002397
Chris Lattner16fac4f2008-07-26 01:18:38 +00002398 // This identifier can only be a typedef name if we haven't already seen
2399 // a type-specifier. Without this check we misparse:
2400 // typedef int X; struct Y { short X; }; as 'short int'.
2401 if (DS.hasTypeSpecifier())
2402 goto DoneWithDeclSpec;
Mike Stump11289f42009-09-09 15:08:12 +00002403
John Thompson22334602010-02-05 00:12:22 +00002404 // Check for need to substitute AltiVec keyword tokens.
2405 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
2406 break;
2407
Richard Smith3092a3b2012-05-09 18:56:43 +00002408 // [AltiVec] 2.2: [If the 'vector' specifier is used] The syntax does not
2409 // allow the use of a typedef name as a type specifier.
2410 if (DS.isTypeAltiVecVector())
2411 goto DoneWithDeclSpec;
2412
John McCallba7bf592010-08-24 05:47:05 +00002413 ParsedType TypeRep =
2414 Actions.getTypeName(*Tok.getIdentifierInfo(),
2415 Tok.getLocation(), getCurScope());
Douglas Gregor8bf42052009-02-09 18:46:07 +00002416
Chris Lattner6cc055a2009-04-12 20:42:31 +00002417 // If this is not a typedef name, don't parse it as part of the declspec,
2418 // it must be an implicit int or an error.
John McCallba7bf592010-08-24 05:47:05 +00002419 if (!TypeRep) {
Richard Smithc5b05522012-03-12 07:56:15 +00002420 if (ParseImplicitInt(DS, 0, TemplateInfo, AS, DSContext)) continue;
Chris Lattner16fac4f2008-07-26 01:18:38 +00002421 goto DoneWithDeclSpec;
Chris Lattner6cc055a2009-04-12 20:42:31 +00002422 }
Douglas Gregor8bf42052009-02-09 18:46:07 +00002423
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002424 // If we're in a context where the identifier could be a class name,
2425 // check whether this is a constructor declaration.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002426 if (getLangOpts().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00002427 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), getCurScope()) &&
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002428 isConstructorDeclarator())
Douglas Gregor61956c42008-10-31 09:07:45 +00002429 goto DoneWithDeclSpec;
2430
Douglas Gregor9817f4a2009-02-09 15:09:02 +00002431 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00002432 DiagID, TypeRep);
Chris Lattner16fac4f2008-07-26 01:18:38 +00002433 if (isInvalid)
2434 break;
Mike Stump11289f42009-09-09 15:08:12 +00002435
Chris Lattner16fac4f2008-07-26 01:18:38 +00002436 DS.SetRangeEnd(Tok.getLocation());
2437 ConsumeToken(); // The identifier
2438
2439 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
2440 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Chad Rosierc1183952012-06-26 22:30:43 +00002441 // Objective-C interface.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002442 if (Tok.is(tok::less) && getLangOpts().ObjC1)
Douglas Gregor06e41ae2010-10-21 23:17:00 +00002443 ParseObjCProtocolQualifiers(DS);
Chad Rosierc1183952012-06-26 22:30:43 +00002444
Steve Naroffcd5e7822008-09-22 10:28:57 +00002445 // Need to support trailing type qualifiers (e.g. "id<p> const").
2446 // If a type specifier follows, it will be diagnosed elsewhere.
2447 continue;
Chris Lattner16fac4f2008-07-26 01:18:38 +00002448 }
Douglas Gregor7f741122009-02-25 19:37:18 +00002449
2450 // type-name
2451 case tok::annot_template_id: {
Argyrios Kyrtzidisc0c5dd22011-06-22 06:09:49 +00002452 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregorb67535d2009-03-31 00:43:58 +00002453 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor7f741122009-02-25 19:37:18 +00002454 // This template-id does not refer to a type name, so we're
2455 // done with the type-specifiers.
2456 goto DoneWithDeclSpec;
2457 }
2458
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002459 // If we're in a context where the template-id could be a
2460 // constructor name or specialization, check whether this is a
2461 // constructor declaration.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002462 if (getLangOpts().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00002463 Actions.isCurrentClassName(*TemplateId->Name, getCurScope()) &&
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002464 isConstructorDeclarator())
2465 goto DoneWithDeclSpec;
2466
Douglas Gregor7f741122009-02-25 19:37:18 +00002467 // Turn the template-id annotation token into a type annotation
2468 // token, then try again to parse it as a type-specifier.
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00002469 AnnotateTemplateIdTokenAsType();
Douglas Gregor7f741122009-02-25 19:37:18 +00002470 continue;
2471 }
2472
Chris Lattnere37e2332006-08-15 04:50:22 +00002473 // GNU attributes support.
2474 case tok::kw___attribute:
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +00002475 ParseGNUAttributes(DS.getAttributes(), 0, LateAttrs);
Chris Lattnerb95cca02006-10-17 03:01:08 +00002476 continue;
Steve Naroff3a9b7e02008-12-24 20:59:21 +00002477
2478 // Microsoft declspec support.
2479 case tok::kw___declspec:
John McCall53fa7142010-12-24 02:08:15 +00002480 ParseMicrosoftDeclSpec(DS.getAttributes());
Steve Naroff3a9b7e02008-12-24 20:59:21 +00002481 continue;
Mike Stump11289f42009-09-09 15:08:12 +00002482
Steve Naroff44ac7772008-12-25 14:16:32 +00002483 // Microsoft single token adornments.
Michael J. Spencerf97bd8c2012-06-18 07:00:48 +00002484 case tok::kw___forceinline: {
2485 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec, DiagID);
2486 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
Richard Smithda837032012-09-14 18:27:01 +00002487 SourceLocation AttrNameLoc = Tok.getLocation();
Alexis Hunta0e54d42012-06-18 16:13:52 +00002488 // FIXME: This does not work correctly if it is set to be a declspec
2489 // attribute, and a GNU attribute is simply incorrect.
Michael J. Spencerf97bd8c2012-06-18 07:00:48 +00002490 DS.getAttributes().addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
Alexis Hunta0e54d42012-06-18 16:13:52 +00002491 SourceLocation(), 0, 0, AttributeList::AS_GNU);
Richard Smithda837032012-09-14 18:27:01 +00002492 break;
Michael J. Spencerf97bd8c2012-06-18 07:00:48 +00002493 }
Eli Friedman53339e02009-06-08 23:27:34 +00002494
2495 case tok::kw___ptr64:
Francois Pichetf2fb4112011-08-25 00:36:46 +00002496 case tok::kw___ptr32:
Steve Narofff9c29d42008-12-25 14:41:26 +00002497 case tok::kw___w64:
Steve Naroff44ac7772008-12-25 14:16:32 +00002498 case tok::kw___cdecl:
2499 case tok::kw___stdcall:
2500 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00002501 case tok::kw___thiscall:
Francois Pichet17ed0202011-08-18 09:59:55 +00002502 case tok::kw___unaligned:
John McCall53fa7142010-12-24 02:08:15 +00002503 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman53339e02009-06-08 23:27:34 +00002504 continue;
2505
Dawn Perchik335e16b2010-09-03 01:29:35 +00002506 // Borland single token adornments.
2507 case tok::kw___pascal:
John McCall53fa7142010-12-24 02:08:15 +00002508 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik335e16b2010-09-03 01:29:35 +00002509 continue;
2510
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00002511 // OpenCL single token adornments.
2512 case tok::kw___kernel:
2513 ParseOpenCLAttributes(DS.getAttributes());
2514 continue;
2515
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002516 // storage-class-specifier
2517 case tok::kw_typedef:
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002518 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_typedef, Loc,
2519 PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002520 break;
2521 case tok::kw_extern:
Chris Lattner353f5742006-11-28 04:50:12 +00002522 if (DS.isThreadSpecified())
Chris Lattner6d29c102008-11-18 07:48:38 +00002523 Diag(Tok, diag::ext_thread_before) << "extern";
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002524 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_extern, Loc,
2525 PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002526 break;
Steve Naroff2050b0d2007-12-18 00:16:02 +00002527 case tok::kw___private_extern__:
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002528 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_private_extern,
2529 Loc, PrevSpec, DiagID);
Steve Naroff2050b0d2007-12-18 00:16:02 +00002530 break;
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002531 case tok::kw_static:
Chris Lattner353f5742006-11-28 04:50:12 +00002532 if (DS.isThreadSpecified())
Chris Lattner6d29c102008-11-18 07:48:38 +00002533 Diag(Tok, diag::ext_thread_before) << "static";
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002534 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_static, Loc,
2535 PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002536 break;
2537 case tok::kw_auto:
David Blaikiebbafb8a2012-03-11 07:00:24 +00002538 if (getLangOpts().CPlusPlus0x) {
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00002539 if (isKnownToBeTypeSpecifier(GetLookAheadToken(1))) {
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002540 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc,
2541 PrevSpec, DiagID);
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00002542 if (!isInvalid)
Richard Smith58c74332011-09-04 19:54:14 +00002543 Diag(Tok, diag::ext_auto_storage_class)
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00002544 << FixItHint::CreateRemoval(DS.getStorageClassSpecLoc());
Richard Smith58c74332011-09-04 19:54:14 +00002545 } else
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00002546 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec,
2547 DiagID);
Richard Smith58c74332011-09-04 19:54:14 +00002548 } else
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002549 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc,
2550 PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002551 break;
2552 case tok::kw_register:
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002553 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_register, Loc,
2554 PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002555 break;
Sebastian Redlccdfaba2008-11-14 23:42:31 +00002556 case tok::kw_mutable:
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002557 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_mutable, Loc,
2558 PrevSpec, DiagID);
Sebastian Redlccdfaba2008-11-14 23:42:31 +00002559 break;
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002560 case tok::kw___thread:
John McCall49bfce42009-08-03 20:12:06 +00002561 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002562 break;
Mike Stump11289f42009-09-09 15:08:12 +00002563
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002564 // function-specifier
2565 case tok::kw_inline:
John McCall49bfce42009-08-03 20:12:06 +00002566 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec, DiagID);
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002567 break;
Douglas Gregor61956c42008-10-31 09:07:45 +00002568 case tok::kw_virtual:
John McCall49bfce42009-08-03 20:12:06 +00002569 isInvalid = DS.SetFunctionSpecVirtual(Loc, PrevSpec, DiagID);
Douglas Gregor61956c42008-10-31 09:07:45 +00002570 break;
Douglas Gregor61956c42008-10-31 09:07:45 +00002571 case tok::kw_explicit:
John McCall49bfce42009-08-03 20:12:06 +00002572 isInvalid = DS.SetFunctionSpecExplicit(Loc, PrevSpec, DiagID);
Douglas Gregor61956c42008-10-31 09:07:45 +00002573 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00002574
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002575 // alignment-specifier
2576 case tok::kw__Alignas:
David Blaikiebbafb8a2012-03-11 07:00:24 +00002577 if (!getLangOpts().C11)
Jordan Rose58d54722012-06-30 21:33:57 +00002578 Diag(Tok, diag::ext_c11_alignment) << Tok.getName();
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002579 ParseAlignmentSpecifier(DS.getAttributes());
2580 continue;
2581
Anders Carlssoncd8db412009-05-06 04:46:28 +00002582 // friend
2583 case tok::kw_friend:
John McCall07e91c02009-08-06 02:15:43 +00002584 if (DSContext == DSC_class)
2585 isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID);
2586 else {
2587 PrevSpec = ""; // not actually used by the diagnostic
2588 DiagID = diag::err_friend_invalid_in_context;
2589 isInvalid = true;
2590 }
Anders Carlssoncd8db412009-05-06 04:46:28 +00002591 break;
Mike Stump11289f42009-09-09 15:08:12 +00002592
Douglas Gregor26701a42011-09-09 02:06:17 +00002593 // Modules
2594 case tok::kw___module_private__:
2595 isInvalid = DS.setModulePrivateSpec(Loc, PrevSpec, DiagID);
2596 break;
Chad Rosierc1183952012-06-26 22:30:43 +00002597
Sebastian Redl39c2a8b2009-11-05 15:47:02 +00002598 // constexpr
2599 case tok::kw_constexpr:
2600 isInvalid = DS.SetConstexprSpec(Loc, PrevSpec, DiagID);
2601 break;
2602
Chris Lattnere387d9e2009-01-21 19:48:37 +00002603 // type-specifier
2604 case tok::kw_short:
John McCall49bfce42009-08-03 20:12:06 +00002605 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec,
2606 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002607 break;
2608 case tok::kw_long:
2609 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCall49bfce42009-08-03 20:12:06 +00002610 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
2611 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002612 else
John McCall49bfce42009-08-03 20:12:06 +00002613 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2614 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002615 break;
Francois Pichet84133e42011-04-28 01:59:37 +00002616 case tok::kw___int64:
2617 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2618 DiagID);
2619 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00002620 case tok::kw_signed:
John McCall49bfce42009-08-03 20:12:06 +00002621 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec,
2622 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002623 break;
2624 case tok::kw_unsigned:
John McCall49bfce42009-08-03 20:12:06 +00002625 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
2626 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002627 break;
2628 case tok::kw__Complex:
John McCall49bfce42009-08-03 20:12:06 +00002629 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
2630 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002631 break;
2632 case tok::kw__Imaginary:
John McCall49bfce42009-08-03 20:12:06 +00002633 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
2634 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002635 break;
2636 case tok::kw_void:
John McCall49bfce42009-08-03 20:12:06 +00002637 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec,
2638 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002639 break;
2640 case tok::kw_char:
John McCall49bfce42009-08-03 20:12:06 +00002641 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec,
2642 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002643 break;
2644 case tok::kw_int:
John McCall49bfce42009-08-03 20:12:06 +00002645 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec,
2646 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002647 break;
Richard Smithf016bbc2012-04-04 06:24:32 +00002648 case tok::kw___int128:
2649 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int128, Loc, PrevSpec,
2650 DiagID);
2651 break;
2652 case tok::kw_half:
2653 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_half, Loc, PrevSpec,
2654 DiagID);
2655 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00002656 case tok::kw_float:
John McCall49bfce42009-08-03 20:12:06 +00002657 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec,
2658 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002659 break;
2660 case tok::kw_double:
John McCall49bfce42009-08-03 20:12:06 +00002661 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec,
2662 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002663 break;
2664 case tok::kw_wchar_t:
John McCall49bfce42009-08-03 20:12:06 +00002665 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec,
2666 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002667 break;
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00002668 case tok::kw_char16_t:
John McCall49bfce42009-08-03 20:12:06 +00002669 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec,
2670 DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00002671 break;
2672 case tok::kw_char32_t:
John McCall49bfce42009-08-03 20:12:06 +00002673 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec,
2674 DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00002675 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00002676 case tok::kw_bool:
2677 case tok::kw__Bool:
Argyrios Kyrtzidis20ee5ae2010-11-16 18:18:13 +00002678 if (Tok.is(tok::kw_bool) &&
2679 DS.getTypeSpecType() != DeclSpec::TST_unspecified &&
2680 DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
2681 PrevSpec = ""; // Not used by the diagnostic.
2682 DiagID = diag::err_bool_redeclaration;
Fariborz Jahanian2b059992011-04-19 21:42:37 +00002683 // For better error recovery.
2684 Tok.setKind(tok::identifier);
Argyrios Kyrtzidis20ee5ae2010-11-16 18:18:13 +00002685 isInvalid = true;
2686 } else {
2687 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec,
2688 DiagID);
2689 }
Chris Lattnere387d9e2009-01-21 19:48:37 +00002690 break;
2691 case tok::kw__Decimal32:
John McCall49bfce42009-08-03 20:12:06 +00002692 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
2693 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002694 break;
2695 case tok::kw__Decimal64:
John McCall49bfce42009-08-03 20:12:06 +00002696 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
2697 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002698 break;
2699 case tok::kw__Decimal128:
John McCall49bfce42009-08-03 20:12:06 +00002700 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
2701 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002702 break;
John Thompson22334602010-02-05 00:12:22 +00002703 case tok::kw___vector:
2704 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
2705 break;
2706 case tok::kw___pixel:
2707 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
2708 break;
John McCall39439732011-04-09 22:50:59 +00002709 case tok::kw___unknown_anytype:
2710 isInvalid = DS.SetTypeSpecType(TST_unknown_anytype, Loc,
2711 PrevSpec, DiagID);
2712 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00002713
2714 // class-specifier:
2715 case tok::kw_class:
2716 case tok::kw_struct:
Joao Matosdc86f942012-08-31 18:45:21 +00002717 case tok::kw___interface:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00002718 case tok::kw_union: {
2719 tok::TokenKind Kind = Tok.getKind();
2720 ConsumeToken();
Richard Smithc5b05522012-03-12 07:56:15 +00002721 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS,
2722 EnteringContext, DSContext);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002723 continue;
Chris Lattnerffaa0e62009-04-12 21:49:30 +00002724 }
Chris Lattnere387d9e2009-01-21 19:48:37 +00002725
2726 // enum-specifier:
2727 case tok::kw_enum:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00002728 ConsumeToken();
Richard Smithc5b05522012-03-12 07:56:15 +00002729 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS, DSContext);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002730 continue;
2731
2732 // cv-qualifier:
2733 case tok::kw_const:
John McCall49bfce42009-08-03 20:12:06 +00002734 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec, DiagID,
Richard Smith7ac3c6a2012-07-24 20:24:58 +00002735 getLangOpts(), /*IsTypeSpec*/true);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002736 break;
2737 case tok::kw_volatile:
John McCall49bfce42009-08-03 20:12:06 +00002738 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
Richard Smith7ac3c6a2012-07-24 20:24:58 +00002739 getLangOpts(), /*IsTypeSpec*/true);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002740 break;
2741 case tok::kw_restrict:
John McCall49bfce42009-08-03 20:12:06 +00002742 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
Richard Smith7ac3c6a2012-07-24 20:24:58 +00002743 getLangOpts(), /*IsTypeSpec*/true);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002744 break;
2745
Douglas Gregor333489b2009-03-27 23:10:48 +00002746 // C++ typename-specifier:
2747 case tok::kw_typename:
John McCall1f476a12010-02-26 08:45:28 +00002748 if (TryAnnotateTypeOrScopeToken()) {
2749 DS.SetTypeSpecError();
2750 goto DoneWithDeclSpec;
2751 }
2752 if (!Tok.is(tok::kw_typename))
Douglas Gregor333489b2009-03-27 23:10:48 +00002753 continue;
2754 break;
2755
Chris Lattnere387d9e2009-01-21 19:48:37 +00002756 // GNU typeof support.
2757 case tok::kw_typeof:
2758 ParseTypeofSpecifier(DS);
2759 continue;
2760
David Blaikie15a430a2011-12-04 05:04:18 +00002761 case tok::annot_decltype:
Anders Carlsson74948d02009-06-24 17:47:40 +00002762 ParseDecltypeSpecifier(DS);
2763 continue;
2764
Alexis Hunt4a257072011-05-19 05:37:45 +00002765 case tok::kw___underlying_type:
2766 ParseUnderlyingTypeSpecifier(DS);
Eli Friedman0dfb8892011-10-06 23:00:33 +00002767 continue;
2768
2769 case tok::kw__Atomic:
2770 ParseAtomicSpecifier(DS);
2771 continue;
Alexis Hunt4a257072011-05-19 05:37:45 +00002772
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00002773 // OpenCL qualifiers:
Chad Rosierc1183952012-06-26 22:30:43 +00002774 case tok::kw_private:
David Blaikiebbafb8a2012-03-11 07:00:24 +00002775 if (!getLangOpts().OpenCL)
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00002776 goto DoneWithDeclSpec;
2777 case tok::kw___private:
2778 case tok::kw___global:
2779 case tok::kw___local:
2780 case tok::kw___constant:
2781 case tok::kw___read_only:
2782 case tok::kw___write_only:
2783 case tok::kw___read_write:
2784 ParseOpenCLQualifiers(DS);
2785 break;
Chad Rosierc1183952012-06-26 22:30:43 +00002786
Steve Naroffcfdf6162008-06-05 00:02:44 +00002787 case tok::less:
Chris Lattner16fac4f2008-07-26 01:18:38 +00002788 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattner0974b232008-07-26 00:20:22 +00002789 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
2790 // but we support it.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002791 if (DS.hasTypeSpecifier() || !getLangOpts().ObjC1)
Chris Lattner0974b232008-07-26 00:20:22 +00002792 goto DoneWithDeclSpec;
Mike Stump11289f42009-09-09 15:08:12 +00002793
Douglas Gregor3a001f42010-11-19 17:10:50 +00002794 if (!ParseObjCProtocolQualifiers(DS))
2795 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
2796 << FixItHint::CreateInsertion(Loc, "id")
2797 << SourceRange(Loc, DS.getSourceRange().getEnd());
Chad Rosierc1183952012-06-26 22:30:43 +00002798
Douglas Gregor06e41ae2010-10-21 23:17:00 +00002799 // Need to support trailing type qualifiers (e.g. "id<p> const").
2800 // If a type specifier follows, it will be diagnosed elsewhere.
2801 continue;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002802 }
John McCall49bfce42009-08-03 20:12:06 +00002803 // If the specifier wasn't legal, issue a diagnostic.
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002804 if (isInvalid) {
2805 assert(PrevSpec && "Method did not return previous specifier!");
John McCall49bfce42009-08-03 20:12:06 +00002806 assert(DiagID);
Chad Rosierc1183952012-06-26 22:30:43 +00002807
Douglas Gregora05f5ab2010-08-23 14:34:43 +00002808 if (DiagID == diag::ext_duplicate_declspec)
2809 Diag(Tok, DiagID)
2810 << PrevSpec << FixItHint::CreateRemoval(Tok.getLocation());
2811 else
2812 Diag(Tok, DiagID) << PrevSpec;
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002813 }
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00002814
Chris Lattner2e232092008-03-13 06:29:04 +00002815 DS.SetRangeEnd(Tok.getLocation());
Fariborz Jahanian2b059992011-04-19 21:42:37 +00002816 if (DiagID != diag::err_bool_redeclaration)
2817 ConsumeToken();
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00002818
2819 AttrsLastTime = false;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002820 }
2821}
Douglas Gregoreb31f392008-12-01 23:54:00 +00002822
Chris Lattner70ae4912007-10-29 04:42:53 +00002823/// ParseStructDeclaration - Parse a struct declaration without the terminating
2824/// semicolon.
2825///
Chris Lattner90a26b02007-01-23 04:38:16 +00002826/// struct-declaration:
Chris Lattner70ae4912007-10-29 04:42:53 +00002827/// specifier-qualifier-list struct-declarator-list
Chris Lattner736ed5d2007-06-09 05:59:07 +00002828/// [GNU] __extension__ struct-declaration
Chris Lattner70ae4912007-10-29 04:42:53 +00002829/// [GNU] specifier-qualifier-list
Chris Lattner90a26b02007-01-23 04:38:16 +00002830/// struct-declarator-list:
2831/// struct-declarator
2832/// struct-declarator-list ',' struct-declarator
2833/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
2834/// struct-declarator:
2835/// declarator
2836/// [GNU] declarator attributes[opt]
2837/// declarator[opt] ':' constant-expression
2838/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
2839///
Chris Lattnera12405b2008-04-10 06:46:29 +00002840void Parser::
Eli Friedman89b1f2c2012-08-08 23:04:35 +00002841ParseStructDeclaration(ParsingDeclSpec &DS, FieldCallback &Fields) {
Chad Rosierc1183952012-06-26 22:30:43 +00002842
Chris Lattnerf02ef3e2008-10-20 06:45:43 +00002843 if (Tok.is(tok::kw___extension__)) {
2844 // __extension__ silences extension warnings in the subexpression.
2845 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroff97170802007-08-20 22:28:22 +00002846 ConsumeToken();
Chris Lattnerf02ef3e2008-10-20 06:45:43 +00002847 return ParseStructDeclaration(DS, Fields);
2848 }
Mike Stump11289f42009-09-09 15:08:12 +00002849
Steve Naroff97170802007-08-20 22:28:22 +00002850 // Parse the common specifier-qualifiers-list piece.
Steve Naroff97170802007-08-20 22:28:22 +00002851 ParseSpecifierQualifierList(DS);
Mike Stump11289f42009-09-09 15:08:12 +00002852
Douglas Gregorc6f58fe2009-01-12 22:49:06 +00002853 // If there are no declarators, this is a free-standing declaration
2854 // specifier. Let the actions module cope with it.
Chris Lattner76c72282007-10-09 17:33:22 +00002855 if (Tok.is(tok::semi)) {
Eli Friedman89b1f2c2012-08-08 23:04:35 +00002856 Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none,
2857 DS);
2858 DS.complete(TheDecl);
Steve Naroff97170802007-08-20 22:28:22 +00002859 return;
2860 }
2861
2862 // Read struct-declarators until we find the semicolon.
John McCallcfefb6d2009-11-03 02:38:08 +00002863 bool FirstDeclarator = true;
Richard Smith8d06f422012-01-12 23:53:29 +00002864 SourceLocation CommaLoc;
Steve Naroff97170802007-08-20 22:28:22 +00002865 while (1) {
Eli Friedman89b1f2c2012-08-08 23:04:35 +00002866 ParsingFieldDeclarator DeclaratorInfo(*this, DS);
Richard Smith8d06f422012-01-12 23:53:29 +00002867 DeclaratorInfo.D.setCommaLoc(CommaLoc);
John McCallcfefb6d2009-11-03 02:38:08 +00002868
2869 // Attributes are only allowed here on successive declarators.
John McCall53fa7142010-12-24 02:08:15 +00002870 if (!FirstDeclarator)
2871 MaybeParseGNUAttributes(DeclaratorInfo.D);
Mike Stump11289f42009-09-09 15:08:12 +00002872
Steve Naroff97170802007-08-20 22:28:22 +00002873 /// struct-declarator: declarator
2874 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattner17c3b1f2009-12-10 01:59:24 +00002875 if (Tok.isNot(tok::colon)) {
2876 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
2877 ColonProtectionRAIIObject X(*this);
Chris Lattnera12405b2008-04-10 06:46:29 +00002878 ParseDeclarator(DeclaratorInfo.D);
Chris Lattner17c3b1f2009-12-10 01:59:24 +00002879 }
Mike Stump11289f42009-09-09 15:08:12 +00002880
Chris Lattner76c72282007-10-09 17:33:22 +00002881 if (Tok.is(tok::colon)) {
Steve Naroff97170802007-08-20 22:28:22 +00002882 ConsumeToken();
John McCalldadc5752010-08-24 06:29:42 +00002883 ExprResult Res(ParseConstantExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002884 if (Res.isInvalid())
Steve Naroff97170802007-08-20 22:28:22 +00002885 SkipUntil(tok::semi, true, true);
Chris Lattner32295d32008-04-10 06:15:14 +00002886 else
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00002887 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroff97170802007-08-20 22:28:22 +00002888 }
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002889
Steve Naroff97170802007-08-20 22:28:22 +00002890 // If attributes exist after the declarator, parse them.
John McCall53fa7142010-12-24 02:08:15 +00002891 MaybeParseGNUAttributes(DeclaratorInfo.D);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002892
John McCallcfefb6d2009-11-03 02:38:08 +00002893 // We're done with this declarator; invoke the callback.
Eli Friedmanba01f2b2012-08-08 23:35:12 +00002894 Fields.invoke(DeclaratorInfo);
John McCallcfefb6d2009-11-03 02:38:08 +00002895
Steve Naroff97170802007-08-20 22:28:22 +00002896 // If we don't have a comma, it is either the end of the list (a ';')
2897 // or an error, bail out.
Chris Lattner76c72282007-10-09 17:33:22 +00002898 if (Tok.isNot(tok::comma))
Chris Lattner70ae4912007-10-29 04:42:53 +00002899 return;
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002900
Steve Naroff97170802007-08-20 22:28:22 +00002901 // Consume the comma.
Richard Smith8d06f422012-01-12 23:53:29 +00002902 CommaLoc = ConsumeToken();
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002903
John McCallcfefb6d2009-11-03 02:38:08 +00002904 FirstDeclarator = false;
Steve Naroff97170802007-08-20 22:28:22 +00002905 }
Steve Naroff97170802007-08-20 22:28:22 +00002906}
2907
2908/// ParseStructUnionBody
2909/// struct-contents:
2910/// struct-declaration-list
2911/// [EXT] empty
2912/// [GNU] "struct-declaration-list" without terminatoring ';'
2913/// struct-declaration-list:
2914/// struct-declaration
2915/// struct-declaration-list struct-declaration
Chris Lattner535b8302008-06-21 19:39:06 +00002916/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroff97170802007-08-20 22:28:22 +00002917///
Chris Lattner1300fb92007-01-23 23:42:53 +00002918void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
John McCall48871652010-08-21 09:40:31 +00002919 unsigned TagType, Decl *TagDecl) {
John McCallfaf5fb42010-08-26 23:41:50 +00002920 PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc,
2921 "parsing struct/union body");
Mike Stump11289f42009-09-09 15:08:12 +00002922
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002923 BalancedDelimiterTracker T(*this, tok::l_brace);
2924 if (T.consumeOpen())
2925 return;
Mike Stump11289f42009-09-09 15:08:12 +00002926
Douglas Gregor658b9552009-01-09 22:42:13 +00002927 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
Douglas Gregor0be31a22010-07-02 17:43:08 +00002928 Actions.ActOnTagStartDefinition(getCurScope(), TagDecl);
Douglas Gregor82ac25e2009-01-08 20:45:30 +00002929
Chris Lattner7b9ace62007-01-23 20:11:08 +00002930 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
2931 // C++.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002932 if (Tok.is(tok::r_brace) && !getLangOpts().CPlusPlus) {
Richard Smithe4345902011-12-29 21:57:33 +00002933 Diag(Tok, diag::ext_empty_struct_union) << (TagType == TST_union);
2934 Diag(Tok, diag::warn_empty_struct_union_compat) << (TagType == TST_union);
2935 }
Chris Lattner7b9ace62007-01-23 20:11:08 +00002936
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002937 SmallVector<Decl *, 32> FieldDecls;
Chris Lattnera12405b2008-04-10 06:46:29 +00002938
Chris Lattner7b9ace62007-01-23 20:11:08 +00002939 // While we still have something to read, read the declarations in the struct.
Chris Lattner76c72282007-10-09 17:33:22 +00002940 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Chris Lattner90a26b02007-01-23 04:38:16 +00002941 // Each iteration of this loop reads one struct-declaration.
Mike Stump11289f42009-09-09 15:08:12 +00002942
Chris Lattner736ed5d2007-06-09 05:59:07 +00002943 // Check for extraneous top-level semicolon.
Chris Lattner76c72282007-10-09 17:33:22 +00002944 if (Tok.is(tok::semi)) {
Richard Smith87f5dc52012-07-23 05:45:25 +00002945 ConsumeExtraSemi(InsideStruct, TagType);
Chris Lattner36e46a22007-06-09 05:49:55 +00002946 continue;
2947 }
Chris Lattnera12405b2008-04-10 06:46:29 +00002948
John McCallcfefb6d2009-11-03 02:38:08 +00002949 if (!Tok.is(tok::at)) {
2950 struct CFieldCallback : FieldCallback {
2951 Parser &P;
John McCall48871652010-08-21 09:40:31 +00002952 Decl *TagDecl;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002953 SmallVectorImpl<Decl *> &FieldDecls;
John McCallcfefb6d2009-11-03 02:38:08 +00002954
John McCall48871652010-08-21 09:40:31 +00002955 CFieldCallback(Parser &P, Decl *TagDecl,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002956 SmallVectorImpl<Decl *> &FieldDecls) :
John McCallcfefb6d2009-11-03 02:38:08 +00002957 P(P), TagDecl(TagDecl), FieldDecls(FieldDecls) {}
2958
Eli Friedman934dbbf2012-08-08 23:53:27 +00002959 void invoke(ParsingFieldDeclarator &FD) {
John McCallcfefb6d2009-11-03 02:38:08 +00002960 // Install the declarator into the current TagDecl.
John McCall48871652010-08-21 09:40:31 +00002961 Decl *Field = P.Actions.ActOnField(P.getCurScope(), TagDecl,
John McCall5e6253b2009-11-03 21:13:47 +00002962 FD.D.getDeclSpec().getSourceRange().getBegin(),
2963 FD.D, FD.BitfieldSize);
John McCallcfefb6d2009-11-03 02:38:08 +00002964 FieldDecls.push_back(Field);
Eli Friedman89b1f2c2012-08-08 23:04:35 +00002965 FD.complete(Field);
Douglas Gregor66a985d2009-08-26 14:27:30 +00002966 }
John McCallcfefb6d2009-11-03 02:38:08 +00002967 } Callback(*this, TagDecl, FieldDecls);
2968
Eli Friedman89b1f2c2012-08-08 23:04:35 +00002969 // Parse all the comma separated declarators.
2970 ParsingDeclSpec DS(*this);
John McCallcfefb6d2009-11-03 02:38:08 +00002971 ParseStructDeclaration(DS, Callback);
Chris Lattner535b8302008-06-21 19:39:06 +00002972 } else { // Handle @defs
2973 ConsumeToken();
2974 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
2975 Diag(Tok, diag::err_unexpected_at);
Chris Lattner245c5332010-02-02 00:37:27 +00002976 SkipUntil(tok::semi, true);
Chris Lattner535b8302008-06-21 19:39:06 +00002977 continue;
2978 }
2979 ConsumeToken();
2980 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
2981 if (!Tok.is(tok::identifier)) {
2982 Diag(Tok, diag::err_expected_ident);
Chris Lattner245c5332010-02-02 00:37:27 +00002983 SkipUntil(tok::semi, true);
Chris Lattner535b8302008-06-21 19:39:06 +00002984 continue;
2985 }
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002986 SmallVector<Decl *, 16> Fields;
Douglas Gregor0be31a22010-07-02 17:43:08 +00002987 Actions.ActOnDefs(getCurScope(), TagDecl, Tok.getLocation(),
Douglas Gregor91f84212008-12-11 16:49:14 +00002988 Tok.getIdentifierInfo(), Fields);
Chris Lattner535b8302008-06-21 19:39:06 +00002989 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
2990 ConsumeToken();
2991 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
Mike Stump11289f42009-09-09 15:08:12 +00002992 }
Chris Lattner736ed5d2007-06-09 05:59:07 +00002993
Chris Lattner76c72282007-10-09 17:33:22 +00002994 if (Tok.is(tok::semi)) {
Chris Lattner90a26b02007-01-23 04:38:16 +00002995 ConsumeToken();
Chris Lattner76c72282007-10-09 17:33:22 +00002996 } else if (Tok.is(tok::r_brace)) {
Chris Lattner245c5332010-02-02 00:37:27 +00002997 ExpectAndConsume(tok::semi, diag::ext_expected_semi_decl_list);
Chris Lattner0c7e82d2007-06-09 05:54:40 +00002998 break;
Chris Lattner90a26b02007-01-23 04:38:16 +00002999 } else {
Chris Lattner245c5332010-02-02 00:37:27 +00003000 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
3001 // Skip to end of block or statement to avoid ext-warning on extra ';'.
Chris Lattner90a26b02007-01-23 04:38:16 +00003002 SkipUntil(tok::r_brace, true, true);
Chris Lattner245c5332010-02-02 00:37:27 +00003003 // If we stopped at a ';', eat it.
3004 if (Tok.is(tok::semi)) ConsumeToken();
Chris Lattner90a26b02007-01-23 04:38:16 +00003005 }
3006 }
Mike Stump11289f42009-09-09 15:08:12 +00003007
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003008 T.consumeClose();
Mike Stump11289f42009-09-09 15:08:12 +00003009
John McCall084e83d2011-03-24 11:26:52 +00003010 ParsedAttributes attrs(AttrFactory);
Chris Lattner90a26b02007-01-23 04:38:16 +00003011 // If attributes exist after struct contents, parse them.
John McCall53fa7142010-12-24 02:08:15 +00003012 MaybeParseGNUAttributes(attrs);
Daniel Dunbar15619c72008-10-03 02:03:53 +00003013
Douglas Gregor0be31a22010-07-02 17:43:08 +00003014 Actions.ActOnFields(getCurScope(),
David Blaikie751c5582011-09-22 02:58:26 +00003015 RecordLoc, TagDecl, FieldDecls,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003016 T.getOpenLocation(), T.getCloseLocation(),
John McCall53fa7142010-12-24 02:08:15 +00003017 attrs.getList());
Douglas Gregor82ac25e2009-01-08 20:45:30 +00003018 StructScope.Exit();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003019 Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl,
3020 T.getCloseLocation());
Chris Lattner90a26b02007-01-23 04:38:16 +00003021}
3022
Chris Lattner3b561a32006-08-13 00:12:11 +00003023/// ParseEnumSpecifier
Chris Lattner1890ac82006-08-13 01:16:23 +00003024/// enum-specifier: [C99 6.7.2.2]
Chris Lattner3b561a32006-08-13 00:12:11 +00003025/// 'enum' identifier[opt] '{' enumerator-list '}'
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003026///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Chris Lattnere37e2332006-08-15 04:50:22 +00003027/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
3028/// '}' attributes[opt]
Aaron Ballman9ecff022012-03-01 04:09:28 +00003029/// [MS] 'enum' __declspec[opt] identifier[opt] '{' enumerator-list ',' [opt]
3030/// '}'
Chris Lattner3b561a32006-08-13 00:12:11 +00003031/// 'enum' identifier
Chris Lattnere37e2332006-08-15 04:50:22 +00003032/// [GNU] 'enum' attributes[opt] identifier
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003033///
Richard Smith7d137e32012-03-23 03:33:32 +00003034/// [C++11] enum-head '{' enumerator-list[opt] '}'
3035/// [C++11] enum-head '{' enumerator-list ',' '}'
Douglas Gregor0bf31402010-10-08 23:50:27 +00003036///
Richard Smith7d137e32012-03-23 03:33:32 +00003037/// enum-head: [C++11]
3038/// enum-key attribute-specifier-seq[opt] identifier[opt] enum-base[opt]
3039/// enum-key attribute-specifier-seq[opt] nested-name-specifier
3040/// identifier enum-base[opt]
Douglas Gregor0bf31402010-10-08 23:50:27 +00003041///
Richard Smith7d137e32012-03-23 03:33:32 +00003042/// enum-key: [C++11]
Douglas Gregor0bf31402010-10-08 23:50:27 +00003043/// 'enum'
3044/// 'enum' 'class'
3045/// 'enum' 'struct'
3046///
Richard Smith7d137e32012-03-23 03:33:32 +00003047/// enum-base: [C++11]
Douglas Gregor0bf31402010-10-08 23:50:27 +00003048/// ':' type-specifier-seq
3049///
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003050/// [C++] elaborated-type-specifier:
3051/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
3052///
Chris Lattnerffaa0e62009-04-12 21:49:30 +00003053void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregordc70c3a2010-03-02 17:53:14 +00003054 const ParsedTemplateInfo &TemplateInfo,
Richard Smithc5b05522012-03-12 07:56:15 +00003055 AccessSpecifier AS, DeclSpecContext DSC) {
Chris Lattnerffbc2712007-01-25 06:05:38 +00003056 // Parse the tag portion of this.
Douglas Gregorf45b0cf2009-09-18 15:37:17 +00003057 if (Tok.is(tok::code_completion)) {
3058 // Code completion for an enum name.
Douglas Gregor0be31a22010-07-02 17:43:08 +00003059 Actions.CodeCompleteTag(getCurScope(), DeclSpec::TST_enum);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00003060 return cutOffParsing();
Douglas Gregorf45b0cf2009-09-18 15:37:17 +00003061 }
John McCallcb432fa2011-07-06 05:58:41 +00003062
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003063 // If attributes exist after tag, parse them.
3064 ParsedAttributesWithRange attrs(AttrFactory);
3065 MaybeParseGNUAttributes(attrs);
3066 MaybeParseCXX0XAttributes(attrs);
3067
3068 // If declspecs exist after tag, parse them.
3069 while (Tok.is(tok::kw___declspec))
3070 ParseMicrosoftDeclSpec(attrs);
3071
Richard Smith0f8ee222012-01-10 01:33:14 +00003072 SourceLocation ScopedEnumKWLoc;
John McCallcb432fa2011-07-06 05:58:41 +00003073 bool IsScopedUsingClassTag = false;
3074
John McCallbeae29a2012-06-23 22:30:04 +00003075 // In C++11, recognize 'enum class' and 'enum struct'.
David Blaikiebbafb8a2012-03-11 07:00:24 +00003076 if (getLangOpts().CPlusPlus0x &&
John McCallcb432fa2011-07-06 05:58:41 +00003077 (Tok.is(tok::kw_class) || Tok.is(tok::kw_struct))) {
Richard Smith5d164bc2011-10-15 05:09:34 +00003078 Diag(Tok, diag::warn_cxx98_compat_scoped_enum);
John McCallcb432fa2011-07-06 05:58:41 +00003079 IsScopedUsingClassTag = Tok.is(tok::kw_class);
Richard Smith0f8ee222012-01-10 01:33:14 +00003080 ScopedEnumKWLoc = ConsumeToken();
Chad Rosierc1183952012-06-26 22:30:43 +00003081
John McCallbeae29a2012-06-23 22:30:04 +00003082 // Attributes are not allowed between these keywords. Diagnose,
3083 // but then just treat them like they appeared in the right place.
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003084 ProhibitAttributes(attrs);
John McCallbeae29a2012-06-23 22:30:04 +00003085
3086 // They are allowed afterwards, though.
3087 MaybeParseGNUAttributes(attrs);
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003088 MaybeParseCXX0XAttributes(attrs);
John McCallbeae29a2012-06-23 22:30:04 +00003089 while (Tok.is(tok::kw___declspec))
3090 ParseMicrosoftDeclSpec(attrs);
John McCallcb432fa2011-07-06 05:58:41 +00003091 }
Richard Smith7d137e32012-03-23 03:33:32 +00003092
John McCall6347b682012-05-07 06:16:58 +00003093 // C++11 [temp.explicit]p12:
3094 // The usual access controls do not apply to names used to specify
3095 // explicit instantiations.
3096 // We extend this to also cover explicit specializations. Note that
3097 // we don't suppress if this turns out to be an elaborated type
3098 // specifier.
3099 bool shouldDelayDiagsInTag =
3100 (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation ||
3101 TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization);
3102 SuppressAccessChecks diagsFromTag(*this, shouldDelayDiagsInTag);
Richard Smith7d137e32012-03-23 03:33:32 +00003103
Richard Smithbfdb1082012-03-12 08:56:40 +00003104 // Enum definitions should not be parsed in a trailing-return-type.
3105 bool AllowDeclaration = DSC != DSC_trailing;
3106
3107 bool AllowFixedUnderlyingType = AllowDeclaration &&
3108 (getLangOpts().CPlusPlus0x || getLangOpts().MicrosoftExt ||
3109 getLangOpts().ObjC2);
John McCallcb432fa2011-07-06 05:58:41 +00003110
Abramo Bagnarad7548482010-05-19 21:37:53 +00003111 CXXScopeSpec &SS = DS.getTypeSpecScope();
David Blaikiebbafb8a2012-03-11 07:00:24 +00003112 if (getLangOpts().CPlusPlus) {
John McCallcb432fa2011-07-06 05:58:41 +00003113 // "enum foo : bar;" is not a potential typo for "enum foo::bar;"
3114 // if a fixed underlying type is allowed.
3115 ColonProtectionRAIIObject X(*this, AllowFixedUnderlyingType);
Chad Rosierc1183952012-06-26 22:30:43 +00003116
3117 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
Douglas Gregordf593fb2011-11-07 17:33:42 +00003118 /*EnteringContext=*/false))
John McCall1f476a12010-02-26 08:45:28 +00003119 return;
3120
3121 if (SS.isSet() && Tok.isNot(tok::identifier)) {
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003122 Diag(Tok, diag::err_expected_ident);
3123 if (Tok.isNot(tok::l_brace)) {
3124 // Has no name and is not a definition.
3125 // Skip the rest of this declarator, up until the comma or semicolon.
3126 SkipUntil(tok::comma, true);
3127 return;
3128 }
3129 }
3130 }
Mike Stump11289f42009-09-09 15:08:12 +00003131
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00003132 // Must have either 'enum name' or 'enum {...}'.
Douglas Gregor6cd5ae42011-02-22 02:55:24 +00003133 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace) &&
Richard Smithbfdb1082012-03-12 08:56:40 +00003134 !(AllowFixedUnderlyingType && Tok.is(tok::colon))) {
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00003135 Diag(Tok, diag::err_expected_ident_lbrace);
Mike Stump11289f42009-09-09 15:08:12 +00003136
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00003137 // Skip the rest of this declarator, up until the comma or semicolon.
3138 SkipUntil(tok::comma, true);
Chris Lattner3b561a32006-08-13 00:12:11 +00003139 return;
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00003140 }
Mike Stump11289f42009-09-09 15:08:12 +00003141
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00003142 // If an identifier is present, consume and remember it.
3143 IdentifierInfo *Name = 0;
3144 SourceLocation NameLoc;
3145 if (Tok.is(tok::identifier)) {
3146 Name = Tok.getIdentifierInfo();
3147 NameLoc = ConsumeToken();
3148 }
Mike Stump11289f42009-09-09 15:08:12 +00003149
Richard Smith0f8ee222012-01-10 01:33:14 +00003150 if (!Name && ScopedEnumKWLoc.isValid()) {
Douglas Gregor0bf31402010-10-08 23:50:27 +00003151 // C++0x 7.2p2: The optional identifier shall not be omitted in the
3152 // declaration of a scoped enumeration.
3153 Diag(Tok, diag::err_scoped_enum_missing_identifier);
Richard Smith0f8ee222012-01-10 01:33:14 +00003154 ScopedEnumKWLoc = SourceLocation();
Abramo Bagnara0e05e242010-12-03 18:54:17 +00003155 IsScopedUsingClassTag = false;
Douglas Gregor0bf31402010-10-08 23:50:27 +00003156 }
3157
John McCall6347b682012-05-07 06:16:58 +00003158 // Okay, end the suppression area. We'll decide whether to emit the
3159 // diagnostics in a second.
3160 if (shouldDelayDiagsInTag)
3161 diagsFromTag.done();
Richard Smith7d137e32012-03-23 03:33:32 +00003162
Douglas Gregor0bf31402010-10-08 23:50:27 +00003163 TypeResult BaseType;
3164
Douglas Gregord1f69f62010-12-01 17:42:47 +00003165 // Parse the fixed underlying type.
Richard Smith200f47c2012-07-02 19:14:01 +00003166 bool CanBeBitfield = getCurScope()->getFlags() & Scope::ClassScope;
Douglas Gregor6cd5ae42011-02-22 02:55:24 +00003167 if (AllowFixedUnderlyingType && Tok.is(tok::colon)) {
Douglas Gregord1f69f62010-12-01 17:42:47 +00003168 bool PossibleBitfield = false;
Richard Smith200f47c2012-07-02 19:14:01 +00003169 if (CanBeBitfield) {
Douglas Gregord1f69f62010-12-01 17:42:47 +00003170 // If we're in class scope, this can either be an enum declaration with
3171 // an underlying type, or a declaration of a bitfield member. We try to
3172 // use a simple disambiguation scheme first to catch the common cases
Chad Rosierc1183952012-06-26 22:30:43 +00003173 // (integer literal, sizeof); if it's still ambiguous, we then consider
3174 // anything that's a simple-type-specifier followed by '(' as an
3175 // expression. This suffices because function types are not valid
Douglas Gregord1f69f62010-12-01 17:42:47 +00003176 // underlying types anyway.
Richard Smith4f605af2012-08-18 00:55:03 +00003177 EnterExpressionEvaluationContext Unevaluated(Actions,
3178 Sema::ConstantEvaluated);
Douglas Gregord1f69f62010-12-01 17:42:47 +00003179 TPResult TPR = isExpressionOrTypeSpecifierSimple(NextToken().getKind());
Chad Rosierc1183952012-06-26 22:30:43 +00003180 // If the next token starts an expression, we know we're parsing a
Douglas Gregord1f69f62010-12-01 17:42:47 +00003181 // bit-field. This is the common case.
3182 if (TPR == TPResult::True())
3183 PossibleBitfield = true;
3184 // If the next token starts a type-specifier-seq, it may be either a
3185 // a fixed underlying type or the start of a function-style cast in C++;
Chad Rosierc1183952012-06-26 22:30:43 +00003186 // lookahead one more token to see if it's obvious that we have a
Douglas Gregord1f69f62010-12-01 17:42:47 +00003187 // fixed underlying type.
Chad Rosierc1183952012-06-26 22:30:43 +00003188 else if (TPR == TPResult::False() &&
Douglas Gregord1f69f62010-12-01 17:42:47 +00003189 GetLookAheadToken(2).getKind() == tok::semi) {
3190 // Consume the ':'.
3191 ConsumeToken();
3192 } else {
3193 // We have the start of a type-specifier-seq, so we have to perform
3194 // tentative parsing to determine whether we have an expression or a
3195 // type.
3196 TentativeParsingAction TPA(*this);
3197
3198 // Consume the ':'.
3199 ConsumeToken();
Richard Smith1e3b0f02012-02-23 01:36:12 +00003200
3201 // If we see a type specifier followed by an open-brace, we have an
3202 // ambiguity between an underlying type and a C++11 braced
3203 // function-style cast. Resolve this by always treating it as an
3204 // underlying type.
3205 // FIXME: The standard is not entirely clear on how to disambiguate in
3206 // this case.
David Blaikiebbafb8a2012-03-11 07:00:24 +00003207 if ((getLangOpts().CPlusPlus &&
Richard Smith1e3b0f02012-02-23 01:36:12 +00003208 isCXXDeclarationSpecifier(TPResult::True()) != TPResult::True()) ||
David Blaikiebbafb8a2012-03-11 07:00:24 +00003209 (!getLangOpts().CPlusPlus && !isDeclarationSpecifier(true))) {
Douglas Gregord1f69f62010-12-01 17:42:47 +00003210 // We'll parse this as a bitfield later.
3211 PossibleBitfield = true;
3212 TPA.Revert();
3213 } else {
3214 // We have a type-specifier-seq.
3215 TPA.Commit();
3216 }
3217 }
3218 } else {
3219 // Consume the ':'.
3220 ConsumeToken();
3221 }
3222
3223 if (!PossibleBitfield) {
3224 SourceRange Range;
3225 BaseType = ParseTypeName(&Range);
Chad Rosierc1183952012-06-26 22:30:43 +00003226
David Blaikiebbafb8a2012-03-11 07:00:24 +00003227 if (!getLangOpts().CPlusPlus0x && !getLangOpts().ObjC2)
Douglas Gregora1aec292011-02-22 20:32:04 +00003228 Diag(StartLoc, diag::ext_ms_enum_fixed_underlying_type)
3229 << Range;
David Blaikiebbafb8a2012-03-11 07:00:24 +00003230 if (getLangOpts().CPlusPlus0x)
Richard Smith5d164bc2011-10-15 05:09:34 +00003231 Diag(StartLoc, diag::warn_cxx98_compat_enum_fixed_underlying_type);
Douglas Gregord1f69f62010-12-01 17:42:47 +00003232 }
Douglas Gregor0bf31402010-10-08 23:50:27 +00003233 }
3234
Richard Smith0f8ee222012-01-10 01:33:14 +00003235 // There are four options here. If we have 'friend enum foo;' then this is a
3236 // friend declaration, and cannot have an accompanying definition. If we have
3237 // 'enum foo;', then this is a forward declaration. If we have
3238 // 'enum foo {...' then this is a definition. Otherwise we have something
3239 // like 'enum foo xyz', a reference.
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00003240 //
3241 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
3242 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
3243 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
3244 //
John McCallfaf5fb42010-08-26 23:41:50 +00003245 Sema::TagUseKind TUK;
John McCall6347b682012-05-07 06:16:58 +00003246 if (!AllowDeclaration) {
Richard Smithbfdb1082012-03-12 08:56:40 +00003247 TUK = Sema::TUK_Reference;
John McCall6347b682012-05-07 06:16:58 +00003248 } else if (Tok.is(tok::l_brace)) {
3249 if (DS.isFriendSpecified()) {
3250 Diag(Tok.getLocation(), diag::err_friend_decl_defines_type)
3251 << SourceRange(DS.getFriendSpecLoc());
3252 ConsumeBrace();
3253 SkipUntil(tok::r_brace);
3254 TUK = Sema::TUK_Friend;
3255 } else {
3256 TUK = Sema::TUK_Definition;
3257 }
Richard Smith369b9f92012-06-25 21:37:02 +00003258 } else if (DSC != DSC_type_specifier &&
3259 (Tok.is(tok::semi) ||
Richard Smith200f47c2012-07-02 19:14:01 +00003260 (Tok.isAtStartOfLine() &&
3261 !isValidAfterTypeSpecifier(CanBeBitfield)))) {
Richard Smith369b9f92012-06-25 21:37:02 +00003262 TUK = DS.isFriendSpecified() ? Sema::TUK_Friend : Sema::TUK_Declaration;
3263 if (Tok.isNot(tok::semi)) {
3264 // A semicolon was missing after this declaration. Diagnose and recover.
3265 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_tagdecl,
3266 "enum");
3267 PP.EnterToken(Tok);
3268 Tok.setKind(tok::semi);
3269 }
John McCall6347b682012-05-07 06:16:58 +00003270 } else {
John McCallfaf5fb42010-08-26 23:41:50 +00003271 TUK = Sema::TUK_Reference;
John McCall6347b682012-05-07 06:16:58 +00003272 }
3273
3274 // If this is an elaborated type specifier, and we delayed
3275 // diagnostics before, just merge them into the current pool.
3276 if (TUK == Sema::TUK_Reference && shouldDelayDiagsInTag) {
3277 diagsFromTag.redelay();
3278 }
Richard Smith7d137e32012-03-23 03:33:32 +00003279
3280 MultiTemplateParamsArg TParams;
Douglas Gregorcbbf3e32010-05-03 17:48:54 +00003281 if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate &&
John McCallfaf5fb42010-08-26 23:41:50 +00003282 TUK != Sema::TUK_Reference) {
Richard Smith7d137e32012-03-23 03:33:32 +00003283 if (!getLangOpts().CPlusPlus0x || !SS.isSet()) {
3284 // Skip the rest of this declarator, up until the comma or semicolon.
3285 Diag(Tok, diag::err_enum_template);
3286 SkipUntil(tok::comma, true);
3287 return;
3288 }
3289
3290 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) {
3291 // Enumerations can't be explicitly instantiated.
3292 DS.SetTypeSpecError();
3293 Diag(StartLoc, diag::err_explicit_instantiation_enum);
3294 return;
3295 }
3296
3297 assert(TemplateInfo.TemplateParams && "no template parameters");
3298 TParams = MultiTemplateParamsArg(TemplateInfo.TemplateParams->data(),
3299 TemplateInfo.TemplateParams->size());
Douglas Gregorcbbf3e32010-05-03 17:48:54 +00003300 }
Chad Rosierc1183952012-06-26 22:30:43 +00003301
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003302 if (TUK == Sema::TUK_Reference)
3303 ProhibitAttributes(attrs);
Richard Smith7d137e32012-03-23 03:33:32 +00003304
Douglas Gregor6cd5ae42011-02-22 02:55:24 +00003305 if (!Name && TUK != Sema::TUK_Definition) {
3306 Diag(Tok, diag::err_enumerator_unnamed_no_def);
Richard Smith7d137e32012-03-23 03:33:32 +00003307
Douglas Gregor6cd5ae42011-02-22 02:55:24 +00003308 // Skip the rest of this declarator, up until the comma or semicolon.
3309 SkipUntil(tok::comma, true);
3310 return;
3311 }
Richard Smith7d137e32012-03-23 03:33:32 +00003312
Douglas Gregord6ab8742009-05-28 23:31:59 +00003313 bool Owned = false;
John McCall7f41d982009-09-11 04:59:25 +00003314 bool IsDependent = false;
Douglas Gregorba41d012010-04-24 16:38:41 +00003315 const char *PrevSpec = 0;
3316 unsigned DiagID;
John McCall48871652010-08-21 09:40:31 +00003317 Decl *TagDecl = Actions.ActOnTag(getCurScope(), DeclSpec::TST_enum, TUK,
John McCall53fa7142010-12-24 02:08:15 +00003318 StartLoc, SS, Name, NameLoc, attrs.getList(),
Richard Smith7d137e32012-03-23 03:33:32 +00003319 AS, DS.getModulePrivateSpecLoc(), TParams,
Richard Smith0f8ee222012-01-10 01:33:14 +00003320 Owned, IsDependent, ScopedEnumKWLoc,
Abramo Bagnara0e05e242010-12-03 18:54:17 +00003321 IsScopedUsingClassTag, BaseType);
Douglas Gregor0bf31402010-10-08 23:50:27 +00003322
Douglas Gregorba41d012010-04-24 16:38:41 +00003323 if (IsDependent) {
Chad Rosierc1183952012-06-26 22:30:43 +00003324 // This enum has a dependent nested-name-specifier. Handle it as a
Douglas Gregorba41d012010-04-24 16:38:41 +00003325 // dependent tag.
3326 if (!Name) {
3327 DS.SetTypeSpecError();
3328 Diag(Tok, diag::err_expected_type_name_after_typename);
3329 return;
3330 }
Chad Rosierc1183952012-06-26 22:30:43 +00003331
Douglas Gregor0be31a22010-07-02 17:43:08 +00003332 TypeResult Type = Actions.ActOnDependentTag(getCurScope(), DeclSpec::TST_enum,
Chad Rosierc1183952012-06-26 22:30:43 +00003333 TUK, SS, Name, StartLoc,
Douglas Gregorba41d012010-04-24 16:38:41 +00003334 NameLoc);
3335 if (Type.isInvalid()) {
3336 DS.SetTypeSpecError();
3337 return;
3338 }
Chad Rosierc1183952012-06-26 22:30:43 +00003339
Abramo Bagnara9875a3c2011-03-16 20:16:18 +00003340 if (DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc,
3341 NameLoc.isValid() ? NameLoc : StartLoc,
3342 PrevSpec, DiagID, Type.get()))
Douglas Gregorba41d012010-04-24 16:38:41 +00003343 Diag(StartLoc, DiagID) << PrevSpec;
Chad Rosierc1183952012-06-26 22:30:43 +00003344
Douglas Gregorba41d012010-04-24 16:38:41 +00003345 return;
3346 }
Mike Stump11289f42009-09-09 15:08:12 +00003347
John McCall48871652010-08-21 09:40:31 +00003348 if (!TagDecl) {
Chad Rosierc1183952012-06-26 22:30:43 +00003349 // The action failed to produce an enumeration tag. If this is a
Douglas Gregorba41d012010-04-24 16:38:41 +00003350 // definition, consume the entire definition.
Richard Smithbfdb1082012-03-12 08:56:40 +00003351 if (Tok.is(tok::l_brace) && TUK != Sema::TUK_Reference) {
Douglas Gregorba41d012010-04-24 16:38:41 +00003352 ConsumeBrace();
3353 SkipUntil(tok::r_brace);
3354 }
Chad Rosierc1183952012-06-26 22:30:43 +00003355
Douglas Gregorba41d012010-04-24 16:38:41 +00003356 DS.SetTypeSpecError();
3357 return;
3358 }
Richard Smith0f8ee222012-01-10 01:33:14 +00003359
Richard Smith369b9f92012-06-25 21:37:02 +00003360 if (Tok.is(tok::l_brace) && TUK != Sema::TUK_Reference)
John McCall6347b682012-05-07 06:16:58 +00003361 ParseEnumBody(StartLoc, TagDecl);
Mike Stump11289f42009-09-09 15:08:12 +00003362
Abramo Bagnara9875a3c2011-03-16 20:16:18 +00003363 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc,
3364 NameLoc.isValid() ? NameLoc : StartLoc,
3365 PrevSpec, DiagID, TagDecl, Owned))
John McCall49bfce42009-08-03 20:12:06 +00003366 Diag(StartLoc, DiagID) << PrevSpec;
Chris Lattner3b561a32006-08-13 00:12:11 +00003367}
3368
Chris Lattnerc1915e22007-01-25 07:29:02 +00003369/// ParseEnumBody - Parse a {} enclosed enumerator-list.
3370/// enumerator-list:
3371/// enumerator
3372/// enumerator-list ',' enumerator
3373/// enumerator:
3374/// enumeration-constant
3375/// enumeration-constant '=' constant-expression
3376/// enumeration-constant:
3377/// identifier
3378///
John McCall48871652010-08-21 09:40:31 +00003379void Parser::ParseEnumBody(SourceLocation StartLoc, Decl *EnumDecl) {
Douglas Gregor07665a62009-01-05 19:45:36 +00003380 // Enter the scope of the enum body and start the definition.
3381 ParseScope EnumScope(this, Scope::DeclScope);
Douglas Gregor0be31a22010-07-02 17:43:08 +00003382 Actions.ActOnTagStartDefinition(getCurScope(), EnumDecl);
Douglas Gregor07665a62009-01-05 19:45:36 +00003383
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003384 BalancedDelimiterTracker T(*this, tok::l_brace);
3385 T.consumeOpen();
Mike Stump11289f42009-09-09 15:08:12 +00003386
Chris Lattner37256fb2007-08-27 17:24:30 +00003387 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
David Blaikiebbafb8a2012-03-11 07:00:24 +00003388 if (Tok.is(tok::r_brace) && !getLangOpts().CPlusPlus)
Fariborz Jahanian6e814922010-05-28 22:23:22 +00003389 Diag(Tok, diag::error_empty_enum);
Mike Stump11289f42009-09-09 15:08:12 +00003390
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003391 SmallVector<Decl *, 32> EnumConstantDecls;
Chris Lattnerc1915e22007-01-25 07:29:02 +00003392
John McCall48871652010-08-21 09:40:31 +00003393 Decl *LastEnumConstDecl = 0;
Mike Stump11289f42009-09-09 15:08:12 +00003394
Chris Lattnerc1915e22007-01-25 07:29:02 +00003395 // Parse the enumerator-list.
Chris Lattner76c72282007-10-09 17:33:22 +00003396 while (Tok.is(tok::identifier)) {
Chris Lattnerc1915e22007-01-25 07:29:02 +00003397 IdentifierInfo *Ident = Tok.getIdentifierInfo();
3398 SourceLocation IdentLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00003399
John McCall811a0f52010-10-22 23:36:17 +00003400 // If attributes exist after the enumerator, parse them.
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003401 ParsedAttributesWithRange attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00003402 MaybeParseGNUAttributes(attrs);
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003403 MaybeParseCXX0XAttributes(attrs);
3404 ProhibitAttributes(attrs);
John McCall811a0f52010-10-22 23:36:17 +00003405
Chris Lattnerc1915e22007-01-25 07:29:02 +00003406 SourceLocation EqualLoc;
John McCalldadc5752010-08-24 06:29:42 +00003407 ExprResult AssignedVal;
John McCall2ec85372012-05-07 06:16:41 +00003408 ParsingDeclRAIIObject PD(*this, ParsingDeclRAIIObject::NoParent);
Chad Rosierc1183952012-06-26 22:30:43 +00003409
Chris Lattner76c72282007-10-09 17:33:22 +00003410 if (Tok.is(tok::equal)) {
Chris Lattnerc1915e22007-01-25 07:29:02 +00003411 EqualLoc = ConsumeToken();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00003412 AssignedVal = ParseConstantExpression();
3413 if (AssignedVal.isInvalid())
Chris Lattnerda6c2ce2007-04-27 19:13:15 +00003414 SkipUntil(tok::comma, tok::r_brace, true, true);
Chris Lattnerc1915e22007-01-25 07:29:02 +00003415 }
Mike Stump11289f42009-09-09 15:08:12 +00003416
Chris Lattnerc1915e22007-01-25 07:29:02 +00003417 // Install the enumerator constant into EnumDecl.
John McCall48871652010-08-21 09:40:31 +00003418 Decl *EnumConstDecl = Actions.ActOnEnumConstant(getCurScope(), EnumDecl,
3419 LastEnumConstDecl,
3420 IdentLoc, Ident,
John McCall53fa7142010-12-24 02:08:15 +00003421 attrs.getList(), EqualLoc,
John McCall48871652010-08-21 09:40:31 +00003422 AssignedVal.release());
Fariborz Jahanian329b3512011-12-09 01:15:54 +00003423 PD.complete(EnumConstDecl);
Chad Rosierc1183952012-06-26 22:30:43 +00003424
Chris Lattner4ef40012007-06-11 01:28:17 +00003425 EnumConstantDecls.push_back(EnumConstDecl);
3426 LastEnumConstDecl = EnumConstDecl;
Mike Stump11289f42009-09-09 15:08:12 +00003427
Douglas Gregorce66d022010-09-07 14:51:08 +00003428 if (Tok.is(tok::identifier)) {
3429 // We're missing a comma between enumerators.
3430 SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation);
Chad Rosierc1183952012-06-26 22:30:43 +00003431 Diag(Loc, diag::err_enumerator_list_missing_comma)
Douglas Gregorce66d022010-09-07 14:51:08 +00003432 << FixItHint::CreateInsertion(Loc, ", ");
3433 continue;
3434 }
Chad Rosierc1183952012-06-26 22:30:43 +00003435
Chris Lattner76c72282007-10-09 17:33:22 +00003436 if (Tok.isNot(tok::comma))
Chris Lattnerc1915e22007-01-25 07:29:02 +00003437 break;
3438 SourceLocation CommaLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00003439
Richard Smith5d164bc2011-10-15 05:09:34 +00003440 if (Tok.isNot(tok::identifier)) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00003441 if (!getLangOpts().C99 && !getLangOpts().CPlusPlus0x)
Richard Smith87f5dc52012-07-23 05:45:25 +00003442 Diag(CommaLoc, getLangOpts().CPlusPlus ?
3443 diag::ext_enumerator_list_comma_cxx :
3444 diag::ext_enumerator_list_comma_c)
Richard Smith5d164bc2011-10-15 05:09:34 +00003445 << FixItHint::CreateRemoval(CommaLoc);
David Blaikiebbafb8a2012-03-11 07:00:24 +00003446 else if (getLangOpts().CPlusPlus0x)
Richard Smith5d164bc2011-10-15 05:09:34 +00003447 Diag(CommaLoc, diag::warn_cxx98_compat_enumerator_list_comma)
3448 << FixItHint::CreateRemoval(CommaLoc);
3449 }
Chris Lattnerc1915e22007-01-25 07:29:02 +00003450 }
Mike Stump11289f42009-09-09 15:08:12 +00003451
Chris Lattnerc1915e22007-01-25 07:29:02 +00003452 // Eat the }.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003453 T.consumeClose();
Chris Lattnerc1915e22007-01-25 07:29:02 +00003454
Chris Lattnerc1915e22007-01-25 07:29:02 +00003455 // If attributes exist after the identifier list, parse them.
John McCall084e83d2011-03-24 11:26:52 +00003456 ParsedAttributes attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00003457 MaybeParseGNUAttributes(attrs);
Douglas Gregor82ac25e2009-01-08 20:45:30 +00003458
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003459 Actions.ActOnEnumBody(StartLoc, T.getOpenLocation(), T.getCloseLocation(),
3460 EnumDecl, EnumConstantDecls.data(),
3461 EnumConstantDecls.size(), getCurScope(),
3462 attrs.getList());
Mike Stump11289f42009-09-09 15:08:12 +00003463
Douglas Gregor82ac25e2009-01-08 20:45:30 +00003464 EnumScope.Exit();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003465 Actions.ActOnTagFinishDefinition(getCurScope(), EnumDecl,
3466 T.getCloseLocation());
Richard Smith369b9f92012-06-25 21:37:02 +00003467
3468 // The next token must be valid after an enum definition. If not, a ';'
3469 // was probably forgotten.
Richard Smith200f47c2012-07-02 19:14:01 +00003470 bool CanBeBitfield = getCurScope()->getFlags() & Scope::ClassScope;
3471 if (!isValidAfterTypeSpecifier(CanBeBitfield)) {
Richard Smith369b9f92012-06-25 21:37:02 +00003472 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_tagdecl, "enum");
3473 // Push this token back into the preprocessor and change our current token
3474 // to ';' so that the rest of the code recovers as though there were an
3475 // ';' after the definition.
3476 PP.EnterToken(Tok);
3477 Tok.setKind(tok::semi);
3478 }
Chris Lattnerc1915e22007-01-25 07:29:02 +00003479}
Chris Lattner3b561a32006-08-13 00:12:11 +00003480
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003481/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff69e8f9e2008-02-11 23:15:56 +00003482/// start of a type-qualifier-list.
3483bool Parser::isTypeQualifier() const {
3484 switch (Tok.getKind()) {
3485 default: return false;
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003486
3487 // type-qualifier only in OpenCL
3488 case tok::kw_private:
David Blaikiebbafb8a2012-03-11 07:00:24 +00003489 return getLangOpts().OpenCL;
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003490
Steve Naroff69e8f9e2008-02-11 23:15:56 +00003491 // type-qualifier
3492 case tok::kw_const:
3493 case tok::kw_volatile:
3494 case tok::kw_restrict:
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003495 case tok::kw___private:
3496 case tok::kw___local:
3497 case tok::kw___global:
3498 case tok::kw___constant:
3499 case tok::kw___read_only:
3500 case tok::kw___read_write:
3501 case tok::kw___write_only:
Steve Naroff69e8f9e2008-02-11 23:15:56 +00003502 return true;
3503 }
3504}
3505
Chris Lattnerfd48afe2010-02-28 18:18:36 +00003506/// isKnownToBeTypeSpecifier - Return true if we know that the specified token
3507/// is definitely a type-specifier. Return false if it isn't part of a type
3508/// specifier or if we're not sure.
3509bool Parser::isKnownToBeTypeSpecifier(const Token &Tok) const {
3510 switch (Tok.getKind()) {
3511 default: return false;
3512 // type-specifiers
3513 case tok::kw_short:
3514 case tok::kw_long:
Francois Pichet84133e42011-04-28 01:59:37 +00003515 case tok::kw___int64:
Richard Smithf016bbc2012-04-04 06:24:32 +00003516 case tok::kw___int128:
Chris Lattnerfd48afe2010-02-28 18:18:36 +00003517 case tok::kw_signed:
3518 case tok::kw_unsigned:
3519 case tok::kw__Complex:
3520 case tok::kw__Imaginary:
3521 case tok::kw_void:
3522 case tok::kw_char:
3523 case tok::kw_wchar_t:
3524 case tok::kw_char16_t:
3525 case tok::kw_char32_t:
3526 case tok::kw_int:
Anton Korobeynikovf0c267e2011-10-14 23:23:15 +00003527 case tok::kw_half:
Chris Lattnerfd48afe2010-02-28 18:18:36 +00003528 case tok::kw_float:
3529 case tok::kw_double:
3530 case tok::kw_bool:
3531 case tok::kw__Bool:
3532 case tok::kw__Decimal32:
3533 case tok::kw__Decimal64:
3534 case tok::kw__Decimal128:
3535 case tok::kw___vector:
Chad Rosierc1183952012-06-26 22:30:43 +00003536
Chris Lattnerfd48afe2010-02-28 18:18:36 +00003537 // struct-or-union-specifier (C99) or class-specifier (C++)
3538 case tok::kw_class:
3539 case tok::kw_struct:
Joao Matosdc86f942012-08-31 18:45:21 +00003540 case tok::kw___interface:
Chris Lattnerfd48afe2010-02-28 18:18:36 +00003541 case tok::kw_union:
3542 // enum-specifier
3543 case tok::kw_enum:
Chad Rosierc1183952012-06-26 22:30:43 +00003544
Chris Lattnerfd48afe2010-02-28 18:18:36 +00003545 // typedef-name
3546 case tok::annot_typename:
3547 return true;
3548 }
3549}
3550
Steve Naroff69e8f9e2008-02-11 23:15:56 +00003551/// isTypeSpecifierQualifier - Return true if the current token could be the
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003552/// start of a specifier-qualifier-list.
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003553bool Parser::isTypeSpecifierQualifier() {
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003554 switch (Tok.getKind()) {
3555 default: return false;
Mike Stump11289f42009-09-09 15:08:12 +00003556
Chris Lattner020bab92009-01-04 23:41:41 +00003557 case tok::identifier: // foo::bar
John Thompson22334602010-02-05 00:12:22 +00003558 if (TryAltiVecVectorToken())
3559 return true;
3560 // Fall through.
Douglas Gregor333489b2009-03-27 23:10:48 +00003561 case tok::kw_typename: // typename T::type
Chris Lattner020bab92009-01-04 23:41:41 +00003562 // Annotate typenames and C++ scope specifiers. If we get one, just
3563 // recurse to handle whatever we get.
3564 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00003565 return true;
3566 if (Tok.is(tok::identifier))
3567 return false;
3568 return isTypeSpecifierQualifier();
Douglas Gregor333489b2009-03-27 23:10:48 +00003569
Chris Lattner020bab92009-01-04 23:41:41 +00003570 case tok::coloncolon: // ::foo::bar
3571 if (NextToken().is(tok::kw_new) || // ::new
3572 NextToken().is(tok::kw_delete)) // ::delete
3573 return false;
3574
Chris Lattner020bab92009-01-04 23:41:41 +00003575 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00003576 return true;
3577 return isTypeSpecifierQualifier();
Mike Stump11289f42009-09-09 15:08:12 +00003578
Chris Lattnere37e2332006-08-15 04:50:22 +00003579 // GNU attributes support.
3580 case tok::kw___attribute:
Steve Naroffad373bd2007-07-31 12:34:36 +00003581 // GNU typeof support.
3582 case tok::kw_typeof:
Mike Stump11289f42009-09-09 15:08:12 +00003583
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003584 // type-specifiers
3585 case tok::kw_short:
3586 case tok::kw_long:
Francois Pichet84133e42011-04-28 01:59:37 +00003587 case tok::kw___int64:
Richard Smithf016bbc2012-04-04 06:24:32 +00003588 case tok::kw___int128:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003589 case tok::kw_signed:
3590 case tok::kw_unsigned:
3591 case tok::kw__Complex:
3592 case tok::kw__Imaginary:
3593 case tok::kw_void:
3594 case tok::kw_char:
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +00003595 case tok::kw_wchar_t:
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00003596 case tok::kw_char16_t:
3597 case tok::kw_char32_t:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003598 case tok::kw_int:
Anton Korobeynikovf0c267e2011-10-14 23:23:15 +00003599 case tok::kw_half:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003600 case tok::kw_float:
3601 case tok::kw_double:
Chris Lattnerbb31a422007-11-15 05:25:19 +00003602 case tok::kw_bool:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003603 case tok::kw__Bool:
3604 case tok::kw__Decimal32:
3605 case tok::kw__Decimal64:
3606 case tok::kw__Decimal128:
John Thompson22334602010-02-05 00:12:22 +00003607 case tok::kw___vector:
Mike Stump11289f42009-09-09 15:08:12 +00003608
Chris Lattner861a2262008-04-13 18:59:07 +00003609 // struct-or-union-specifier (C99) or class-specifier (C++)
3610 case tok::kw_class:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003611 case tok::kw_struct:
Joao Matosdc86f942012-08-31 18:45:21 +00003612 case tok::kw___interface:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003613 case tok::kw_union:
3614 // enum-specifier
3615 case tok::kw_enum:
Mike Stump11289f42009-09-09 15:08:12 +00003616
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003617 // type-qualifier
3618 case tok::kw_const:
3619 case tok::kw_volatile:
3620 case tok::kw_restrict:
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003621
3622 // typedef-name
Chris Lattnera8a3f732009-01-06 05:06:21 +00003623 case tok::annot_typename:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003624 return true;
Mike Stump11289f42009-09-09 15:08:12 +00003625
Chris Lattner409bf7d2008-10-20 00:25:30 +00003626 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
3627 case tok::less:
David Blaikiebbafb8a2012-03-11 07:00:24 +00003628 return getLangOpts().ObjC1;
Mike Stump11289f42009-09-09 15:08:12 +00003629
Steve Naroff44ac7772008-12-25 14:16:32 +00003630 case tok::kw___cdecl:
3631 case tok::kw___stdcall:
3632 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00003633 case tok::kw___thiscall:
Eli Friedman53339e02009-06-08 23:27:34 +00003634 case tok::kw___w64:
3635 case tok::kw___ptr64:
Francois Pichetf2fb4112011-08-25 00:36:46 +00003636 case tok::kw___ptr32:
Dawn Perchik335e16b2010-09-03 01:29:35 +00003637 case tok::kw___pascal:
Francois Pichet17ed0202011-08-18 09:59:55 +00003638 case tok::kw___unaligned:
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003639
3640 case tok::kw___private:
3641 case tok::kw___local:
3642 case tok::kw___global:
3643 case tok::kw___constant:
3644 case tok::kw___read_only:
3645 case tok::kw___read_write:
3646 case tok::kw___write_only:
3647
Eli Friedman53339e02009-06-08 23:27:34 +00003648 return true;
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003649
3650 case tok::kw_private:
David Blaikiebbafb8a2012-03-11 07:00:24 +00003651 return getLangOpts().OpenCL;
Eli Friedman0dfb8892011-10-06 23:00:33 +00003652
Benjamin Kramere56f3932011-12-23 17:00:35 +00003653 // C11 _Atomic()
Eli Friedman0dfb8892011-10-06 23:00:33 +00003654 case tok::kw__Atomic:
3655 return true;
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003656 }
3657}
3658
Chris Lattneracd58a32006-08-06 17:24:14 +00003659/// isDeclarationSpecifier() - Return true if the current token is part of a
3660/// declaration specifier.
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00003661///
3662/// \param DisambiguatingWithExpression True to indicate that the purpose of
3663/// this check is to disambiguate between an expression and a declaration.
3664bool Parser::isDeclarationSpecifier(bool DisambiguatingWithExpression) {
Chris Lattneracd58a32006-08-06 17:24:14 +00003665 switch (Tok.getKind()) {
3666 default: return false;
Mike Stump11289f42009-09-09 15:08:12 +00003667
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003668 case tok::kw_private:
David Blaikiebbafb8a2012-03-11 07:00:24 +00003669 return getLangOpts().OpenCL;
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003670
Chris Lattner020bab92009-01-04 23:41:41 +00003671 case tok::identifier: // foo::bar
Steve Naroff9527bbf2009-03-09 21:12:44 +00003672 // Unfortunate hack to support "Class.factoryMethod" notation.
David Blaikiebbafb8a2012-03-11 07:00:24 +00003673 if (getLangOpts().ObjC1 && NextToken().is(tok::period))
Steve Naroff9527bbf2009-03-09 21:12:44 +00003674 return false;
John Thompson22334602010-02-05 00:12:22 +00003675 if (TryAltiVecVectorToken())
3676 return true;
3677 // Fall through.
David Blaikie15a430a2011-12-04 05:04:18 +00003678 case tok::kw_decltype: // decltype(T())::type
Douglas Gregor333489b2009-03-27 23:10:48 +00003679 case tok::kw_typename: // typename T::type
Chris Lattner020bab92009-01-04 23:41:41 +00003680 // Annotate typenames and C++ scope specifiers. If we get one, just
3681 // recurse to handle whatever we get.
3682 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00003683 return true;
3684 if (Tok.is(tok::identifier))
3685 return false;
Chad Rosierc1183952012-06-26 22:30:43 +00003686
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00003687 // If we're in Objective-C and we have an Objective-C class type followed
Chad Rosierc1183952012-06-26 22:30:43 +00003688 // by an identifier and then either ':' or ']', in a place where an
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00003689 // expression is permitted, then this is probably a class message send
3690 // missing the initial '['. In this case, we won't consider this to be
3691 // the start of a declaration.
Chad Rosierc1183952012-06-26 22:30:43 +00003692 if (DisambiguatingWithExpression &&
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00003693 isStartOfObjCClassMessageMissingOpenBracket())
3694 return false;
Chad Rosierc1183952012-06-26 22:30:43 +00003695
John McCall1f476a12010-02-26 08:45:28 +00003696 return isDeclarationSpecifier();
3697
Chris Lattner020bab92009-01-04 23:41:41 +00003698 case tok::coloncolon: // ::foo::bar
3699 if (NextToken().is(tok::kw_new) || // ::new
3700 NextToken().is(tok::kw_delete)) // ::delete
3701 return false;
Mike Stump11289f42009-09-09 15:08:12 +00003702
Chris Lattner020bab92009-01-04 23:41:41 +00003703 // Annotate typenames and C++ scope specifiers. If we get one, just
3704 // recurse to handle whatever we get.
3705 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00003706 return true;
3707 return isDeclarationSpecifier();
Mike Stump11289f42009-09-09 15:08:12 +00003708
Chris Lattneracd58a32006-08-06 17:24:14 +00003709 // storage-class-specifier
3710 case tok::kw_typedef:
3711 case tok::kw_extern:
Steve Naroff2050b0d2007-12-18 00:16:02 +00003712 case tok::kw___private_extern__:
Chris Lattneracd58a32006-08-06 17:24:14 +00003713 case tok::kw_static:
3714 case tok::kw_auto:
3715 case tok::kw_register:
3716 case tok::kw___thread:
Mike Stump11289f42009-09-09 15:08:12 +00003717
Douglas Gregor26701a42011-09-09 02:06:17 +00003718 // Modules
3719 case tok::kw___module_private__:
Chad Rosierc1183952012-06-26 22:30:43 +00003720
Chris Lattneracd58a32006-08-06 17:24:14 +00003721 // type-specifiers
3722 case tok::kw_short:
3723 case tok::kw_long:
Francois Pichet84133e42011-04-28 01:59:37 +00003724 case tok::kw___int64:
Richard Smithf016bbc2012-04-04 06:24:32 +00003725 case tok::kw___int128:
Chris Lattneracd58a32006-08-06 17:24:14 +00003726 case tok::kw_signed:
3727 case tok::kw_unsigned:
3728 case tok::kw__Complex:
3729 case tok::kw__Imaginary:
3730 case tok::kw_void:
3731 case tok::kw_char:
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +00003732 case tok::kw_wchar_t:
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00003733 case tok::kw_char16_t:
3734 case tok::kw_char32_t:
3735
Chris Lattneracd58a32006-08-06 17:24:14 +00003736 case tok::kw_int:
Anton Korobeynikovf0c267e2011-10-14 23:23:15 +00003737 case tok::kw_half:
Chris Lattneracd58a32006-08-06 17:24:14 +00003738 case tok::kw_float:
3739 case tok::kw_double:
Chris Lattnerbb31a422007-11-15 05:25:19 +00003740 case tok::kw_bool:
Chris Lattneracd58a32006-08-06 17:24:14 +00003741 case tok::kw__Bool:
3742 case tok::kw__Decimal32:
3743 case tok::kw__Decimal64:
3744 case tok::kw__Decimal128:
John Thompson22334602010-02-05 00:12:22 +00003745 case tok::kw___vector:
Mike Stump11289f42009-09-09 15:08:12 +00003746
Chris Lattner861a2262008-04-13 18:59:07 +00003747 // struct-or-union-specifier (C99) or class-specifier (C++)
3748 case tok::kw_class:
Chris Lattneracd58a32006-08-06 17:24:14 +00003749 case tok::kw_struct:
3750 case tok::kw_union:
Joao Matosdc86f942012-08-31 18:45:21 +00003751 case tok::kw___interface:
Chris Lattneracd58a32006-08-06 17:24:14 +00003752 // enum-specifier
3753 case tok::kw_enum:
Mike Stump11289f42009-09-09 15:08:12 +00003754
Chris Lattneracd58a32006-08-06 17:24:14 +00003755 // type-qualifier
3756 case tok::kw_const:
3757 case tok::kw_volatile:
3758 case tok::kw_restrict:
Steve Naroffad373bd2007-07-31 12:34:36 +00003759
Chris Lattneracd58a32006-08-06 17:24:14 +00003760 // function-specifier
3761 case tok::kw_inline:
Douglas Gregor61956c42008-10-31 09:07:45 +00003762 case tok::kw_virtual:
3763 case tok::kw_explicit:
Chris Lattner7b20dc72007-08-09 16:40:21 +00003764
Peter Collingbourne3d9cbdc2011-04-15 00:35:57 +00003765 // static_assert-declaration
3766 case tok::kw__Static_assert:
3767
Chris Lattner599e47e2007-08-09 17:01:07 +00003768 // GNU typeof support.
3769 case tok::kw_typeof:
Mike Stump11289f42009-09-09 15:08:12 +00003770
Chris Lattner599e47e2007-08-09 17:01:07 +00003771 // GNU attributes.
Chris Lattner7b20dc72007-08-09 16:40:21 +00003772 case tok::kw___attribute:
Chris Lattneracd58a32006-08-06 17:24:14 +00003773 return true;
Mike Stump11289f42009-09-09 15:08:12 +00003774
Francois Pichete878cb62011-06-19 08:02:06 +00003775 // C++0x decltype.
David Blaikie15a430a2011-12-04 05:04:18 +00003776 case tok::annot_decltype:
Francois Pichete878cb62011-06-19 08:02:06 +00003777 return true;
3778
Benjamin Kramere56f3932011-12-23 17:00:35 +00003779 // C11 _Atomic()
Eli Friedman0dfb8892011-10-06 23:00:33 +00003780 case tok::kw__Atomic:
3781 return true;
3782
Chris Lattner8b2ec162008-07-26 03:38:44 +00003783 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
3784 case tok::less:
David Blaikiebbafb8a2012-03-11 07:00:24 +00003785 return getLangOpts().ObjC1;
Mike Stump11289f42009-09-09 15:08:12 +00003786
Douglas Gregor19b7acf2011-04-27 05:41:15 +00003787 // typedef-name
3788 case tok::annot_typename:
3789 return !DisambiguatingWithExpression ||
3790 !isStartOfObjCClassMessageMissingOpenBracket();
Chad Rosierc1183952012-06-26 22:30:43 +00003791
Steve Narofff192fab2009-01-06 19:34:12 +00003792 case tok::kw___declspec:
Steve Naroff44ac7772008-12-25 14:16:32 +00003793 case tok::kw___cdecl:
3794 case tok::kw___stdcall:
3795 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00003796 case tok::kw___thiscall:
Eli Friedman53339e02009-06-08 23:27:34 +00003797 case tok::kw___w64:
3798 case tok::kw___ptr64:
Francois Pichetf2fb4112011-08-25 00:36:46 +00003799 case tok::kw___ptr32:
Eli Friedman53339e02009-06-08 23:27:34 +00003800 case tok::kw___forceinline:
Dawn Perchik335e16b2010-09-03 01:29:35 +00003801 case tok::kw___pascal:
Francois Pichet17ed0202011-08-18 09:59:55 +00003802 case tok::kw___unaligned:
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003803
3804 case tok::kw___private:
3805 case tok::kw___local:
3806 case tok::kw___global:
3807 case tok::kw___constant:
3808 case tok::kw___read_only:
3809 case tok::kw___read_write:
3810 case tok::kw___write_only:
3811
Eli Friedman53339e02009-06-08 23:27:34 +00003812 return true;
Chris Lattneracd58a32006-08-06 17:24:14 +00003813 }
3814}
3815
Douglas Gregor9de54ea2010-01-13 17:31:36 +00003816bool Parser::isConstructorDeclarator() {
3817 TentativeParsingAction TPA(*this);
3818
3819 // Parse the C++ scope specifier.
3820 CXXScopeSpec SS;
Chad Rosierc1183952012-06-26 22:30:43 +00003821 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
Douglas Gregordf593fb2011-11-07 17:33:42 +00003822 /*EnteringContext=*/true)) {
John McCall1f476a12010-02-26 08:45:28 +00003823 TPA.Revert();
3824 return false;
3825 }
Douglas Gregor9de54ea2010-01-13 17:31:36 +00003826
3827 // Parse the constructor name.
3828 if (Tok.is(tok::identifier) || Tok.is(tok::annot_template_id)) {
3829 // We already know that we have a constructor name; just consume
3830 // the token.
3831 ConsumeToken();
3832 } else {
3833 TPA.Revert();
3834 return false;
3835 }
3836
Richard Smith43f340f2012-03-27 23:05:05 +00003837 // Current class name must be followed by a left parenthesis.
Douglas Gregor9de54ea2010-01-13 17:31:36 +00003838 if (Tok.isNot(tok::l_paren)) {
3839 TPA.Revert();
3840 return false;
3841 }
3842 ConsumeParen();
3843
Richard Smith43f340f2012-03-27 23:05:05 +00003844 // A right parenthesis, or ellipsis followed by a right parenthesis signals
3845 // that we have a constructor.
3846 if (Tok.is(tok::r_paren) ||
3847 (Tok.is(tok::ellipsis) && NextToken().is(tok::r_paren))) {
Douglas Gregor9de54ea2010-01-13 17:31:36 +00003848 TPA.Revert();
3849 return true;
3850 }
3851
3852 // If we need to, enter the specified scope.
3853 DeclaratorScopeObj DeclScopeObj(*this, SS);
Douglas Gregor0be31a22010-07-02 17:43:08 +00003854 if (SS.isSet() && Actions.ShouldEnterDeclaratorScope(getCurScope(), SS))
Douglas Gregor9de54ea2010-01-13 17:31:36 +00003855 DeclScopeObj.EnterDeclaratorScope();
3856
Francois Pichet79f3a872011-01-31 04:54:32 +00003857 // Optionally skip Microsoft attributes.
John McCall084e83d2011-03-24 11:26:52 +00003858 ParsedAttributes Attrs(AttrFactory);
Francois Pichet79f3a872011-01-31 04:54:32 +00003859 MaybeParseMicrosoftAttributes(Attrs);
3860
Douglas Gregor9de54ea2010-01-13 17:31:36 +00003861 // Check whether the next token(s) are part of a declaration
3862 // specifier, in which case we have the start of a parameter and,
3863 // therefore, we know that this is a constructor.
Richard Smithefd009d2012-03-27 00:56:56 +00003864 bool IsConstructor = false;
3865 if (isDeclarationSpecifier())
3866 IsConstructor = true;
3867 else if (Tok.is(tok::identifier) ||
3868 (Tok.is(tok::annot_cxxscope) && NextToken().is(tok::identifier))) {
3869 // We've seen "C ( X" or "C ( X::Y", but "X" / "X::Y" is not a type.
3870 // This might be a parenthesized member name, but is more likely to
3871 // be a constructor declaration with an invalid argument type. Keep
3872 // looking.
3873 if (Tok.is(tok::annot_cxxscope))
3874 ConsumeToken();
3875 ConsumeToken();
3876
3877 // If this is not a constructor, we must be parsing a declarator,
Richard Smith1453e312012-03-27 01:42:32 +00003878 // which must have one of the following syntactic forms (see the
3879 // grammar extract at the start of ParseDirectDeclarator):
Richard Smithefd009d2012-03-27 00:56:56 +00003880 switch (Tok.getKind()) {
3881 case tok::l_paren:
3882 // C(X ( int));
3883 case tok::l_square:
3884 // C(X [ 5]);
3885 // C(X [ [attribute]]);
3886 case tok::coloncolon:
3887 // C(X :: Y);
3888 // C(X :: *p);
3889 case tok::r_paren:
3890 // C(X )
3891 // Assume this isn't a constructor, rather than assuming it's a
3892 // constructor with an unnamed parameter of an ill-formed type.
3893 break;
3894
3895 default:
3896 IsConstructor = true;
3897 break;
3898 }
3899 }
3900
Douglas Gregor9de54ea2010-01-13 17:31:36 +00003901 TPA.Revert();
3902 return IsConstructor;
3903}
Chris Lattnerb9093cd2006-08-04 04:39:53 +00003904
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003905/// ParseTypeQualifierListOpt
Dawn Perchik335e16b2010-09-03 01:29:35 +00003906/// type-qualifier-list: [C99 6.7.5]
3907/// type-qualifier
Chad Rosierc1183952012-06-26 22:30:43 +00003908/// [vendor] attributes
Dawn Perchik335e16b2010-09-03 01:29:35 +00003909/// [ only if VendorAttributesAllowed=true ]
3910/// type-qualifier-list type-qualifier
Chad Rosierc1183952012-06-26 22:30:43 +00003911/// [vendor] type-qualifier-list attributes
Dawn Perchik335e16b2010-09-03 01:29:35 +00003912/// [ only if VendorAttributesAllowed=true ]
3913/// [C++0x] attribute-specifier[opt] is allowed before cv-qualifier-seq
3914/// [ only if CXX0XAttributesAllowed=true ]
3915/// Note: vendor can be GNU, MS, etc.
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003916///
Dawn Perchik335e16b2010-09-03 01:29:35 +00003917void Parser::ParseTypeQualifierListOpt(DeclSpec &DS,
3918 bool VendorAttributesAllowed,
Richard Smith3dff2512012-04-10 03:25:07 +00003919 bool CXX11AttributesAllowed) {
3920 if (getLangOpts().CPlusPlus0x && CXX11AttributesAllowed &&
Richard Smith7bdcc4a2012-04-10 01:32:12 +00003921 isCXX11AttributeSpecifier()) {
John McCall084e83d2011-03-24 11:26:52 +00003922 ParsedAttributesWithRange attrs(AttrFactory);
Richard Smith3dff2512012-04-10 03:25:07 +00003923 ParseCXX11Attributes(attrs);
Richard Smith7bdcc4a2012-04-10 01:32:12 +00003924 DS.takeAttributesFrom(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +00003925 }
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +00003926
3927 SourceLocation EndLoc;
3928
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003929 while (1) {
John McCall49bfce42009-08-03 20:12:06 +00003930 bool isInvalid = false;
Chris Lattnerd9c3c592006-08-05 06:26:47 +00003931 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00003932 unsigned DiagID = 0;
Chris Lattner60809f52006-11-28 05:18:46 +00003933 SourceLocation Loc = Tok.getLocation();
Chris Lattnerd9c3c592006-08-05 06:26:47 +00003934
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003935 switch (Tok.getKind()) {
Douglas Gregor28c78432010-08-27 17:35:51 +00003936 case tok::code_completion:
3937 Actions.CodeCompleteTypeQualifiers(DS);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00003938 return cutOffParsing();
Chad Rosierc1183952012-06-26 22:30:43 +00003939
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003940 case tok::kw_const:
John McCall49bfce42009-08-03 20:12:06 +00003941 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, DiagID,
Richard Smith7ac3c6a2012-07-24 20:24:58 +00003942 getLangOpts(), /*IsTypeSpec*/false);
Chris Lattnerd9c3c592006-08-05 06:26:47 +00003943 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003944 case tok::kw_volatile:
John McCall49bfce42009-08-03 20:12:06 +00003945 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
Richard Smith7ac3c6a2012-07-24 20:24:58 +00003946 getLangOpts(), /*IsTypeSpec*/false);
Chris Lattnerd9c3c592006-08-05 06:26:47 +00003947 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003948 case tok::kw_restrict:
John McCall49bfce42009-08-03 20:12:06 +00003949 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
Richard Smith7ac3c6a2012-07-24 20:24:58 +00003950 getLangOpts(), /*IsTypeSpec*/false);
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003951 break;
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003952
3953 // OpenCL qualifiers:
Chad Rosierc1183952012-06-26 22:30:43 +00003954 case tok::kw_private:
David Blaikiebbafb8a2012-03-11 07:00:24 +00003955 if (!getLangOpts().OpenCL)
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003956 goto DoneWithTypeQuals;
3957 case tok::kw___private:
3958 case tok::kw___global:
3959 case tok::kw___local:
3960 case tok::kw___constant:
3961 case tok::kw___read_only:
3962 case tok::kw___write_only:
3963 case tok::kw___read_write:
3964 ParseOpenCLQualifiers(DS);
3965 break;
3966
Eli Friedman53339e02009-06-08 23:27:34 +00003967 case tok::kw___w64:
Steve Narofff9c29d42008-12-25 14:41:26 +00003968 case tok::kw___ptr64:
Francois Pichetf2fb4112011-08-25 00:36:46 +00003969 case tok::kw___ptr32:
Steve Naroff44ac7772008-12-25 14:16:32 +00003970 case tok::kw___cdecl:
3971 case tok::kw___stdcall:
3972 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00003973 case tok::kw___thiscall:
Francois Pichet17ed0202011-08-18 09:59:55 +00003974 case tok::kw___unaligned:
Dawn Perchik335e16b2010-09-03 01:29:35 +00003975 if (VendorAttributesAllowed) {
John McCall53fa7142010-12-24 02:08:15 +00003976 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman53339e02009-06-08 23:27:34 +00003977 continue;
3978 }
3979 goto DoneWithTypeQuals;
Dawn Perchik335e16b2010-09-03 01:29:35 +00003980 case tok::kw___pascal:
3981 if (VendorAttributesAllowed) {
John McCall53fa7142010-12-24 02:08:15 +00003982 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik335e16b2010-09-03 01:29:35 +00003983 continue;
3984 }
3985 goto DoneWithTypeQuals;
Chris Lattnere37e2332006-08-15 04:50:22 +00003986 case tok::kw___attribute:
Dawn Perchik335e16b2010-09-03 01:29:35 +00003987 if (VendorAttributesAllowed) {
John McCall53fa7142010-12-24 02:08:15 +00003988 ParseGNUAttributes(DS.getAttributes());
Chris Lattnercf0bab22008-12-18 07:02:59 +00003989 continue; // do *not* consume the next token!
3990 }
3991 // otherwise, FALL THROUGH!
3992 default:
Steve Naroff44ac7772008-12-25 14:16:32 +00003993 DoneWithTypeQuals:
Chris Lattnercf0bab22008-12-18 07:02:59 +00003994 // If this is not a type-qualifier token, we're done reading type
3995 // qualifiers. First verify that DeclSpec's are consistent.
Douglas Gregore3e01a22009-04-01 22:41:11 +00003996 DS.Finish(Diags, PP);
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +00003997 if (EndLoc.isValid())
3998 DS.SetRangeEnd(EndLoc);
Chris Lattnercf0bab22008-12-18 07:02:59 +00003999 return;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004000 }
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00004001
Chris Lattnerd9c3c592006-08-05 06:26:47 +00004002 // If the specifier combination wasn't legal, issue a diagnostic.
4003 if (isInvalid) {
4004 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner6d29c102008-11-18 07:48:38 +00004005 Diag(Tok, DiagID) << PrevSpec;
Chris Lattnerd9c3c592006-08-05 06:26:47 +00004006 }
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +00004007 EndLoc = ConsumeToken();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004008 }
4009}
4010
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00004011
4012/// ParseDeclarator - Parse and verify a newly-initialized declarator.
4013///
4014void Parser::ParseDeclarator(Declarator &D) {
4015 /// This implements the 'declarator' production in the C grammar, then checks
4016 /// for well-formedness and issues diagnostics.
Sebastian Redlbd150f42008-11-21 19:14:01 +00004017 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00004018}
4019
Richard Smith0efa75c2012-03-29 01:16:42 +00004020static bool isPtrOperatorToken(tok::TokenKind Kind, const LangOptions &Lang) {
4021 if (Kind == tok::star || Kind == tok::caret)
4022 return true;
4023
4024 // We parse rvalue refs in C++03, because otherwise the errors are scary.
4025 if (!Lang.CPlusPlus)
4026 return false;
4027
4028 return Kind == tok::amp || Kind == tok::ampamp;
4029}
4030
Sebastian Redlbd150f42008-11-21 19:14:01 +00004031/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
4032/// is parsed by the function passed to it. Pass null, and the direct-declarator
4033/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregordbc5daf2008-11-07 20:08:42 +00004034/// ptr-operator production.
4035///
Richard Smith09f76ee2011-10-19 21:33:05 +00004036/// If the grammar of this construct is extended, matching changes must also be
Richard Smith1453e312012-03-27 01:42:32 +00004037/// made to TryParseDeclarator and MightBeDeclarator, and possibly to
4038/// isConstructorDeclarator.
Richard Smith09f76ee2011-10-19 21:33:05 +00004039///
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004040/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
4041/// [C] pointer[opt] direct-declarator
4042/// [C++] direct-declarator
4043/// [C++] ptr-operator declarator
Chris Lattner6c7416c2006-08-07 00:19:33 +00004044///
4045/// pointer: [C99 6.7.5]
4046/// '*' type-qualifier-list[opt]
4047/// '*' type-qualifier-list[opt] pointer
4048///
Douglas Gregordbc5daf2008-11-07 20:08:42 +00004049/// ptr-operator:
4050/// '*' cv-qualifier-seq[opt]
4051/// '&'
Sebastian Redled0f3b02009-03-15 22:02:01 +00004052/// [C++0x] '&&'
Douglas Gregordbc5daf2008-11-07 20:08:42 +00004053/// [GNU] '&' restrict[opt] attributes[opt]
Sebastian Redled0f3b02009-03-15 22:02:01 +00004054/// [GNU?] '&&' restrict[opt] attributes[opt]
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004055/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Sebastian Redlbd150f42008-11-21 19:14:01 +00004056void Parser::ParseDeclaratorInternal(Declarator &D,
4057 DirectDeclParseFunction DirectDeclParser) {
Douglas Gregor66a985d2009-08-26 14:27:30 +00004058 if (Diags.hasAllExtensionsSilenced())
4059 D.setExtension();
Chad Rosierc1183952012-06-26 22:30:43 +00004060
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004061 // C++ member pointers start with a '::' or a nested-name.
4062 // Member pointers get special handling, since there's no place for the
4063 // scope spec in the generic path below.
David Blaikiebbafb8a2012-03-11 07:00:24 +00004064 if (getLangOpts().CPlusPlus &&
Chris Lattner803802d2009-03-24 17:04:48 +00004065 (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) ||
4066 Tok.is(tok::annot_cxxscope))) {
Douglas Gregordf593fb2011-11-07 17:33:42 +00004067 bool EnteringContext = D.getContext() == Declarator::FileContext ||
4068 D.getContext() == Declarator::MemberContext;
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004069 CXXScopeSpec SS;
Douglas Gregordf593fb2011-11-07 17:33:42 +00004070 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), EnteringContext);
John McCall1f476a12010-02-26 08:45:28 +00004071
Jeffrey Yasskin4e150f82010-04-07 23:29:58 +00004072 if (SS.isNotEmpty()) {
Mike Stump11289f42009-09-09 15:08:12 +00004073 if (Tok.isNot(tok::star)) {
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004074 // The scope spec really belongs to the direct-declarator.
4075 D.getCXXScopeSpec() = SS;
4076 if (DirectDeclParser)
4077 (this->*DirectDeclParser)(D);
4078 return;
4079 }
4080
4081 SourceLocation Loc = ConsumeToken();
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004082 D.SetRangeEnd(Loc);
John McCall084e83d2011-03-24 11:26:52 +00004083 DeclSpec DS(AttrFactory);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004084 ParseTypeQualifierListOpt(DS);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004085 D.ExtendWithDeclSpec(DS);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004086
4087 // Recurse to parse whatever is left.
4088 ParseDeclaratorInternal(D, DirectDeclParser);
4089
4090 // Sema will have to catch (syntactically invalid) pointers into global
4091 // scope. It has to catch pointers into namespace scope anyway.
4092 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
John McCall084e83d2011-03-24 11:26:52 +00004093 Loc),
4094 DS.getAttributes(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004095 /* Don't replace range end. */SourceLocation());
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004096 return;
4097 }
4098 }
4099
4100 tok::TokenKind Kind = Tok.getKind();
Steve Naroffec33ed92008-08-27 16:04:49 +00004101 // Not a pointer, C++ reference, or block.
Richard Smith0efa75c2012-03-29 01:16:42 +00004102 if (!isPtrOperatorToken(Kind, getLangOpts())) {
Sebastian Redlbd150f42008-11-21 19:14:01 +00004103 if (DirectDeclParser)
4104 (this->*DirectDeclParser)(D);
Douglas Gregordbc5daf2008-11-07 20:08:42 +00004105 return;
4106 }
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004107
Sebastian Redled0f3b02009-03-15 22:02:01 +00004108 // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference,
4109 // '&&' -> rvalue reference
Sebastian Redl3b27be62009-03-23 00:00:23 +00004110 SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&.
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004111 D.SetRangeEnd(Loc);
Bill Wendling3708c182007-05-27 10:15:43 +00004112
Chris Lattner9eac9312009-03-27 04:18:06 +00004113 if (Kind == tok::star || Kind == tok::caret) {
Chris Lattner788404f2008-02-21 01:32:26 +00004114 // Is a pointer.
John McCall084e83d2011-03-24 11:26:52 +00004115 DeclSpec DS(AttrFactory);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004116
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004117 // FIXME: GNU attributes are not allowed here in a new-type-id.
Bill Wendling3708c182007-05-27 10:15:43 +00004118 ParseTypeQualifierListOpt(DS);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004119 D.ExtendWithDeclSpec(DS);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004120
Bill Wendling3708c182007-05-27 10:15:43 +00004121 // Recursively parse the declarator.
Sebastian Redlbd150f42008-11-21 19:14:01 +00004122 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroffec33ed92008-08-27 16:04:49 +00004123 if (Kind == tok::star)
4124 // Remember that we parsed a pointer type, and remember the type-quals.
4125 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
Chandler Carruthe71b378d2011-02-23 18:51:59 +00004126 DS.getConstSpecLoc(),
4127 DS.getVolatileSpecLoc(),
John McCall084e83d2011-03-24 11:26:52 +00004128 DS.getRestrictSpecLoc()),
4129 DS.getAttributes(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004130 SourceLocation());
Steve Naroffec33ed92008-08-27 16:04:49 +00004131 else
4132 // Remember that we parsed a Block type, and remember the type-quals.
Mike Stump11289f42009-09-09 15:08:12 +00004133 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
John McCall084e83d2011-03-24 11:26:52 +00004134 Loc),
4135 DS.getAttributes(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004136 SourceLocation());
Bill Wendling3708c182007-05-27 10:15:43 +00004137 } else {
4138 // Is a reference
John McCall084e83d2011-03-24 11:26:52 +00004139 DeclSpec DS(AttrFactory);
Bill Wendling93efb222007-06-02 23:28:54 +00004140
Sebastian Redl3b27be62009-03-23 00:00:23 +00004141 // Complain about rvalue references in C++03, but then go on and build
4142 // the declarator.
Richard Smith5d164bc2011-10-15 05:09:34 +00004143 if (Kind == tok::ampamp)
David Blaikiebbafb8a2012-03-11 07:00:24 +00004144 Diag(Loc, getLangOpts().CPlusPlus0x ?
Richard Smith5d164bc2011-10-15 05:09:34 +00004145 diag::warn_cxx98_compat_rvalue_reference :
4146 diag::ext_rvalue_reference);
Sebastian Redl3b27be62009-03-23 00:00:23 +00004147
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004148 // GNU-style and C++11 attributes are allowed here, as is restrict.
4149 ParseTypeQualifierListOpt(DS);
4150 D.ExtendWithDeclSpec(DS);
4151
Bill Wendling93efb222007-06-02 23:28:54 +00004152 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
4153 // cv-qualifiers are introduced through the use of a typedef or of a
4154 // template type argument, in which case the cv-qualifiers are ignored.
Bill Wendling93efb222007-06-02 23:28:54 +00004155 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
4156 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
4157 Diag(DS.getConstSpecLoc(),
Chris Lattner6d29c102008-11-18 07:48:38 +00004158 diag::err_invalid_reference_qualifier_application) << "const";
Bill Wendling93efb222007-06-02 23:28:54 +00004159 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
4160 Diag(DS.getVolatileSpecLoc(),
Chris Lattner6d29c102008-11-18 07:48:38 +00004161 diag::err_invalid_reference_qualifier_application) << "volatile";
Bill Wendling93efb222007-06-02 23:28:54 +00004162 }
Bill Wendling3708c182007-05-27 10:15:43 +00004163
4164 // Recursively parse the declarator.
Sebastian Redlbd150f42008-11-21 19:14:01 +00004165 ParseDeclaratorInternal(D, DirectDeclParser);
Bill Wendling3708c182007-05-27 10:15:43 +00004166
Douglas Gregor66583c52008-11-03 15:51:28 +00004167 if (D.getNumTypeObjects() > 0) {
4168 // C++ [dcl.ref]p4: There shall be no references to references.
4169 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
4170 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattnerebad6a22008-11-19 07:37:42 +00004171 if (const IdentifierInfo *II = D.getIdentifier())
4172 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
4173 << II;
4174 else
4175 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
4176 << "type name";
Douglas Gregor66583c52008-11-03 15:51:28 +00004177
Sebastian Redlbd150f42008-11-21 19:14:01 +00004178 // Once we've complained about the reference-to-reference, we
Douglas Gregor66583c52008-11-03 15:51:28 +00004179 // can go ahead and build the (technically ill-formed)
4180 // declarator: reference collapsing will take care of it.
4181 }
4182 }
4183
Bill Wendling3708c182007-05-27 10:15:43 +00004184 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner788404f2008-02-21 01:32:26 +00004185 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
Sebastian Redled0f3b02009-03-15 22:02:01 +00004186 Kind == tok::amp),
John McCall084e83d2011-03-24 11:26:52 +00004187 DS.getAttributes(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004188 SourceLocation());
Bill Wendling3708c182007-05-27 10:15:43 +00004189 }
Chris Lattner6c7416c2006-08-07 00:19:33 +00004190}
4191
Richard Smith0efa75c2012-03-29 01:16:42 +00004192static void diagnoseMisplacedEllipsis(Parser &P, Declarator &D,
4193 SourceLocation EllipsisLoc) {
4194 if (EllipsisLoc.isValid()) {
4195 FixItHint Insertion;
4196 if (!D.getEllipsisLoc().isValid()) {
4197 Insertion = FixItHint::CreateInsertion(D.getIdentifierLoc(), "...");
4198 D.setEllipsisLoc(EllipsisLoc);
4199 }
4200 P.Diag(EllipsisLoc, diag::err_misplaced_ellipsis_in_declaration)
4201 << FixItHint::CreateRemoval(EllipsisLoc) << Insertion << !D.hasName();
4202 }
4203}
4204
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004205/// ParseDirectDeclarator
4206/// direct-declarator: [C99 6.7.5]
Douglas Gregor831c93f2008-11-05 20:51:48 +00004207/// [C99] identifier
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004208/// '(' declarator ')'
4209/// [GNU] '(' attributes declarator ')'
Chris Lattnere8074e62006-08-06 18:30:15 +00004210/// [C90] direct-declarator '[' constant-expression[opt] ']'
4211/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
4212/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
4213/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
4214/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004215/// [C++11] direct-declarator '[' constant-expression[opt] ']'
4216/// attribute-specifier-seq[opt]
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004217/// direct-declarator '(' parameter-type-list ')'
4218/// direct-declarator '(' identifier-list[opt] ')'
4219/// [GNU] direct-declarator '(' parameter-forward-declarations
4220/// parameter-type-list[opt] ')'
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00004221/// [C++] direct-declarator '(' parameter-declaration-clause ')'
4222/// cv-qualifier-seq[opt] exception-specification[opt]
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004223/// [C++11] direct-declarator '(' parameter-declaration-clause ')'
4224/// attribute-specifier-seq[opt] cv-qualifier-seq[opt]
4225/// ref-qualifier[opt] exception-specification[opt]
Douglas Gregor61956c42008-10-31 09:07:45 +00004226/// [C++] declarator-id
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004227/// [C++11] declarator-id attribute-specifier-seq[opt]
Douglas Gregor831c93f2008-11-05 20:51:48 +00004228///
4229/// declarator-id: [C++ 8]
Douglas Gregor27b4c162010-12-23 22:44:42 +00004230/// '...'[opt] id-expression
Douglas Gregor831c93f2008-11-05 20:51:48 +00004231/// '::'[opt] nested-name-specifier[opt] type-name
4232///
4233/// id-expression: [C++ 5.1]
4234/// unqualified-id
Douglas Gregord90fd522009-09-25 21:45:23 +00004235/// qualified-id
Douglas Gregor831c93f2008-11-05 20:51:48 +00004236///
4237/// unqualified-id: [C++ 5.1]
Mike Stump11289f42009-09-09 15:08:12 +00004238/// identifier
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00004239/// operator-function-id
Douglas Gregord90fd522009-09-25 21:45:23 +00004240/// conversion-function-id
Mike Stump11289f42009-09-09 15:08:12 +00004241/// '~' class-name
Douglas Gregor7f741122009-02-25 19:37:18 +00004242/// template-id
Argyrios Kyrtzidise4426352008-11-07 22:02:30 +00004243///
Richard Smith1453e312012-03-27 01:42:32 +00004244/// Note, any additional constructs added here may need corresponding changes
4245/// in isConstructorDeclarator.
Chris Lattneracd58a32006-08-06 17:24:14 +00004246void Parser::ParseDirectDeclarator(Declarator &D) {
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00004247 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00004248
David Blaikiebbafb8a2012-03-11 07:00:24 +00004249 if (getLangOpts().CPlusPlus && D.mayHaveIdentifier()) {
Douglas Gregor7861a802009-11-03 01:35:08 +00004250 // ParseDeclaratorInternal might already have parsed the scope.
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00004251 if (D.getCXXScopeSpec().isEmpty()) {
Douglas Gregordf593fb2011-11-07 17:33:42 +00004252 bool EnteringContext = D.getContext() == Declarator::FileContext ||
4253 D.getContext() == Declarator::MemberContext;
Chad Rosierc1183952012-06-26 22:30:43 +00004254 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec(), ParsedType(),
Douglas Gregordf593fb2011-11-07 17:33:42 +00004255 EnteringContext);
John McCall1f476a12010-02-26 08:45:28 +00004256 }
4257
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00004258 if (D.getCXXScopeSpec().isValid()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00004259 if (Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
John McCall2b058ef2009-12-11 20:04:54 +00004260 // Change the declaration context for name lookup, until this function
4261 // is exited (and the declarator has been parsed).
4262 DeclScopeObj.EnterDeclaratorScope();
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00004263 }
4264
Douglas Gregor27b4c162010-12-23 22:44:42 +00004265 // C++0x [dcl.fct]p14:
4266 // There is a syntactic ambiguity when an ellipsis occurs at the end
Chad Rosierc1183952012-06-26 22:30:43 +00004267 // of a parameter-declaration-clause without a preceding comma. In
4268 // this case, the ellipsis is parsed as part of the
4269 // abstract-declarator if the type of the parameter names a template
Douglas Gregor27b4c162010-12-23 22:44:42 +00004270 // parameter pack that has not been expanded; otherwise, it is parsed
4271 // as part of the parameter-declaration-clause.
Richard Smith0efa75c2012-03-29 01:16:42 +00004272 if (Tok.is(tok::ellipsis) && D.getCXXScopeSpec().isEmpty() &&
Douglas Gregor27b4c162010-12-23 22:44:42 +00004273 !((D.getContext() == Declarator::PrototypeContext ||
4274 D.getContext() == Declarator::BlockLiteralContext) &&
Douglas Gregor27b4c162010-12-23 22:44:42 +00004275 NextToken().is(tok::r_paren) &&
Richard Smith0efa75c2012-03-29 01:16:42 +00004276 !Actions.containsUnexpandedParameterPacks(D))) {
4277 SourceLocation EllipsisLoc = ConsumeToken();
4278 if (isPtrOperatorToken(Tok.getKind(), getLangOpts())) {
4279 // The ellipsis was put in the wrong place. Recover, and explain to
4280 // the user what they should have done.
4281 ParseDeclarator(D);
4282 diagnoseMisplacedEllipsis(*this, D, EllipsisLoc);
4283 return;
4284 } else
4285 D.setEllipsisLoc(EllipsisLoc);
4286
4287 // The ellipsis can't be followed by a parenthesized declarator. We
4288 // check for that in ParseParenDeclarator, after we have disambiguated
4289 // the l_paren token.
4290 }
4291
Douglas Gregor7861a802009-11-03 01:35:08 +00004292 if (Tok.is(tok::identifier) || Tok.is(tok::kw_operator) ||
4293 Tok.is(tok::annot_template_id) || Tok.is(tok::tilde)) {
4294 // We found something that indicates the start of an unqualified-id.
4295 // Parse that unqualified-id.
John McCall84821e72010-04-13 06:39:49 +00004296 bool AllowConstructorName;
4297 if (D.getDeclSpec().hasTypeSpecifier())
4298 AllowConstructorName = false;
4299 else if (D.getCXXScopeSpec().isSet())
4300 AllowConstructorName =
4301 (D.getContext() == Declarator::FileContext ||
4302 (D.getContext() == Declarator::MemberContext &&
4303 D.getDeclSpec().isFriendSpecified()));
4304 else
4305 AllowConstructorName = (D.getContext() == Declarator::MemberContext);
4306
Abramo Bagnara7945c982012-01-27 09:46:47 +00004307 SourceLocation TemplateKWLoc;
Chad Rosierc1183952012-06-26 22:30:43 +00004308 if (ParseUnqualifiedId(D.getCXXScopeSpec(),
4309 /*EnteringContext=*/true,
4310 /*AllowDestructorName=*/true,
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004311 AllowConstructorName,
John McCallba7bf592010-08-24 05:47:05 +00004312 ParsedType(),
Abramo Bagnara7945c982012-01-27 09:46:47 +00004313 TemplateKWLoc,
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00004314 D.getName()) ||
4315 // Once we're past the identifier, if the scope was bad, mark the
4316 // whole declarator bad.
4317 D.getCXXScopeSpec().isInvalid()) {
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00004318 D.SetIdentifier(0, Tok.getLocation());
4319 D.setInvalidType(true);
Douglas Gregor7861a802009-11-03 01:35:08 +00004320 } else {
4321 // Parsed the unqualified-id; update range information and move along.
4322 if (D.getSourceRange().getBegin().isInvalid())
4323 D.SetRangeBegin(D.getName().getSourceRange().getBegin());
4324 D.SetRangeEnd(D.getName().getSourceRange().getEnd());
Douglas Gregordbc5daf2008-11-07 20:08:42 +00004325 }
Douglas Gregor7861a802009-11-03 01:35:08 +00004326 goto PastIdentifier;
Douglas Gregor11d0c4c2008-11-06 22:13:31 +00004327 }
Douglas Gregor7861a802009-11-03 01:35:08 +00004328 } else if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00004329 assert(!getLangOpts().CPlusPlus &&
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00004330 "There's a C++-specific check for tok::identifier above");
4331 assert(Tok.getIdentifierInfo() && "Not an identifier?");
4332 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
4333 ConsumeToken();
Douglas Gregor7861a802009-11-03 01:35:08 +00004334 goto PastIdentifier;
4335 }
Richard Smith0efa75c2012-03-29 01:16:42 +00004336
Douglas Gregor7861a802009-11-03 01:35:08 +00004337 if (Tok.is(tok::l_paren)) {
Chris Lattneracd58a32006-08-06 17:24:14 +00004338 // direct-declarator: '(' declarator ')'
Chris Lattnere37e2332006-08-15 04:50:22 +00004339 // direct-declarator: '(' attributes declarator ')'
Chris Lattneracd58a32006-08-06 17:24:14 +00004340 // Example: 'char (*X)' or 'int (*XX)(void)'
4341 ParseParenDeclarator(D);
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004342
4343 // If the declarator was parenthesized, we entered the declarator
4344 // scope when parsing the parenthesized declarator, then exited
4345 // the scope already. Re-enter the scope, if we need to.
4346 if (D.getCXXScopeSpec().isSet()) {
Fariborz Jahanian358acd52010-08-17 23:50:37 +00004347 // If there was an error parsing parenthesized declarator, declarator
Richard Smith0efa75c2012-03-29 01:16:42 +00004348 // scope may have been entered before. Don't do it again.
Fariborz Jahanian358acd52010-08-17 23:50:37 +00004349 if (!D.isInvalidType() &&
4350 Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004351 // Change the declaration context for name lookup, until this function
4352 // is exited (and the declarator has been parsed).
Fariborz Jahanian358acd52010-08-17 23:50:37 +00004353 DeclScopeObj.EnterDeclaratorScope();
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004354 }
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00004355 } else if (D.mayOmitIdentifier()) {
Chris Lattneracd58a32006-08-06 17:24:14 +00004356 // This could be something simple like "int" (in which case the declarator
4357 // portion is empty), if an abstract-declarator is allowed.
4358 D.SetIdentifier(0, Tok.getLocation());
4359 } else {
David Blaikie5d577a22012-06-29 22:03:56 +00004360 if (Tok.getKind() == tok::annot_pragma_parser_crash)
David Blaikie5bd4c2a2012-08-21 18:56:49 +00004361 LLVM_BUILTIN_TRAP;
Douglas Gregord9f92e22009-03-06 23:28:18 +00004362 if (D.getContext() == Declarator::MemberContext)
4363 Diag(Tok, diag::err_expected_member_name_or_semi)
4364 << D.getDeclSpec().getSourceRange();
David Blaikiebbafb8a2012-03-11 07:00:24 +00004365 else if (getLangOpts().CPlusPlus)
4366 Diag(Tok, diag::err_expected_unqualified_id) << getLangOpts().CPlusPlus;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00004367 else
Chris Lattner6d29c102008-11-18 07:48:38 +00004368 Diag(Tok, diag::err_expected_ident_lparen);
Chris Lattnereec40f92006-08-06 21:55:29 +00004369 D.SetIdentifier(0, Tok.getLocation());
Chris Lattner8c5dd732008-11-11 06:13:16 +00004370 D.setInvalidType(true);
Chris Lattneracd58a32006-08-06 17:24:14 +00004371 }
Mike Stump11289f42009-09-09 15:08:12 +00004372
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00004373 PastIdentifier:
Chris Lattneracd58a32006-08-06 17:24:14 +00004374 assert(D.isPastIdentifier() &&
4375 "Haven't past the location of the identifier yet?");
Mike Stump11289f42009-09-09 15:08:12 +00004376
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004377 // Don't parse attributes unless we have parsed an unparenthesized name.
4378 if (D.hasName() && !D.getNumTypeObjects())
John McCall53fa7142010-12-24 02:08:15 +00004379 MaybeParseCXX0XAttributes(D);
Alexis Hunt96d5c762009-11-21 08:43:09 +00004380
Chris Lattneracd58a32006-08-06 17:24:14 +00004381 while (1) {
Chris Lattner76c72282007-10-09 17:33:22 +00004382 if (Tok.is(tok::l_paren)) {
David Blaikie15a430a2011-12-04 05:04:18 +00004383 // Enter function-declaration scope, limiting any declarators to the
4384 // function prototype scope, including parameter declarators.
4385 ParseScope PrototypeScope(this,
4386 Scope::FunctionPrototypeScope|Scope::DeclScope);
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00004387 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
4388 // In such a case, check if we actually have a function declarator; if it
4389 // is not, the declarator has been fully parsed.
Richard Smith943c4402012-07-30 21:30:52 +00004390 bool IsAmbiguous = false;
Richard Smith4f605af2012-08-18 00:55:03 +00004391 if (getLangOpts().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
4392 // The name of the declarator, if any, is tentatively declared within
4393 // a possible direct initializer.
4394 TentativelyDeclaredIdentifiers.push_back(D.getIdentifier());
4395 bool IsFunctionDecl = isCXXFunctionDeclarator(&IsAmbiguous);
4396 TentativelyDeclaredIdentifiers.pop_back();
4397 if (!IsFunctionDecl)
4398 break;
4399 }
John McCall084e83d2011-03-24 11:26:52 +00004400 ParsedAttributes attrs(AttrFactory);
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004401 BalancedDelimiterTracker T(*this, tok::l_paren);
4402 T.consumeOpen();
Richard Smith943c4402012-07-30 21:30:52 +00004403 ParseFunctionDeclarator(D, attrs, T, IsAmbiguous);
David Blaikie15a430a2011-12-04 05:04:18 +00004404 PrototypeScope.Exit();
Chris Lattner76c72282007-10-09 17:33:22 +00004405 } else if (Tok.is(tok::l_square)) {
Chris Lattnere8074e62006-08-06 18:30:15 +00004406 ParseBracketDeclarator(D);
Chris Lattneracd58a32006-08-06 17:24:14 +00004407 } else {
4408 break;
4409 }
4410 }
Chad Rosierc1183952012-06-26 22:30:43 +00004411}
Chris Lattneracd58a32006-08-06 17:24:14 +00004412
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004413/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
4414/// only called before the identifier, so these are most likely just grouping
Mike Stump11289f42009-09-09 15:08:12 +00004415/// parens for precedence. If we find that these are actually function
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004416/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
4417///
4418/// direct-declarator:
4419/// '(' declarator ')'
4420/// [GNU] '(' attributes declarator ')'
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00004421/// direct-declarator '(' parameter-type-list ')'
4422/// direct-declarator '(' identifier-list[opt] ')'
4423/// [GNU] direct-declarator '(' parameter-forward-declarations
4424/// parameter-type-list[opt] ')'
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004425///
4426void Parser::ParseParenDeclarator(Declarator &D) {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004427 BalancedDelimiterTracker T(*this, tok::l_paren);
4428 T.consumeOpen();
4429
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004430 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
Mike Stump11289f42009-09-09 15:08:12 +00004431
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00004432 // Eat any attributes before we look at whether this is a grouping or function
4433 // declarator paren. If this is a grouping paren, the attribute applies to
4434 // the type being built up, for example:
4435 // int (__attribute__(()) *x)(long y)
4436 // If this ends up not being a grouping paren, the attribute applies to the
4437 // first argument, for example:
4438 // int (__attribute__(()) int x)
4439 // In either case, we need to eat any attributes to be able to determine what
4440 // sort of paren this is.
4441 //
John McCall084e83d2011-03-24 11:26:52 +00004442 ParsedAttributes attrs(AttrFactory);
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00004443 bool RequiresArg = false;
4444 if (Tok.is(tok::kw___attribute)) {
John McCall53fa7142010-12-24 02:08:15 +00004445 ParseGNUAttributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +00004446
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00004447 // We require that the argument list (if this is a non-grouping paren) be
4448 // present even if the attribute list was empty.
4449 RequiresArg = true;
4450 }
Steve Naroff44ac7772008-12-25 14:16:32 +00004451 // Eat any Microsoft extensions.
Eli Friedman53339e02009-06-08 23:27:34 +00004452 if (Tok.is(tok::kw___cdecl) || Tok.is(tok::kw___stdcall) ||
Douglas Gregora941dca2010-05-18 16:57:00 +00004453 Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___fastcall) ||
Francois Pichet17ed0202011-08-18 09:59:55 +00004454 Tok.is(tok::kw___w64) || Tok.is(tok::kw___ptr64) ||
Francois Pichetf2fb4112011-08-25 00:36:46 +00004455 Tok.is(tok::kw___ptr32) || Tok.is(tok::kw___unaligned)) {
John McCall53fa7142010-12-24 02:08:15 +00004456 ParseMicrosoftTypeAttributes(attrs);
Eli Friedman53339e02009-06-08 23:27:34 +00004457 }
Dawn Perchik335e16b2010-09-03 01:29:35 +00004458 // Eat any Borland extensions.
Ted Kremenek5eec2b02010-11-10 05:59:39 +00004459 if (Tok.is(tok::kw___pascal))
John McCall53fa7142010-12-24 02:08:15 +00004460 ParseBorlandTypeAttributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +00004461
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004462 // If we haven't past the identifier yet (or where the identifier would be
4463 // stored, if this is an abstract declarator), then this is probably just
4464 // grouping parens. However, if this could be an abstract-declarator, then
4465 // this could also be the start of function arguments (consider 'void()').
4466 bool isGrouping;
Mike Stump11289f42009-09-09 15:08:12 +00004467
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004468 if (!D.mayOmitIdentifier()) {
4469 // If this can't be an abstract-declarator, this *must* be a grouping
4470 // paren, because we haven't seen the identifier yet.
4471 isGrouping = true;
4472 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Richard Smith43f340f2012-03-27 23:05:05 +00004473 (getLangOpts().CPlusPlus && Tok.is(tok::ellipsis) &&
4474 NextToken().is(tok::r_paren)) || // C++ int(...)
Richard Smith2620cd92012-04-11 04:01:28 +00004475 isDeclarationSpecifier() || // 'int(int)' is a function.
4476 isCXX11AttributeSpecifier()) { // 'int([[]]int)' is a function.
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004477 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
4478 // considered to be a type, not a K&R identifier-list.
4479 isGrouping = false;
4480 } else {
4481 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
4482 isGrouping = true;
4483 }
Mike Stump11289f42009-09-09 15:08:12 +00004484
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004485 // If this is a grouping paren, handle:
4486 // direct-declarator: '(' declarator ')'
4487 // direct-declarator: '(' attributes declarator ')'
4488 if (isGrouping) {
Richard Smith0efa75c2012-03-29 01:16:42 +00004489 SourceLocation EllipsisLoc = D.getEllipsisLoc();
4490 D.setEllipsisLoc(SourceLocation());
4491
Argyrios Kyrtzidis8ae36842008-10-07 10:21:57 +00004492 bool hadGroupingParens = D.hasGroupingParens();
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00004493 D.setGroupingParens(true);
Sebastian Redlbd150f42008-11-21 19:14:01 +00004494 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004495 // Match the ')'.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004496 T.consumeClose();
Chad Rosierc1183952012-06-26 22:30:43 +00004497 D.AddTypeInfo(DeclaratorChunk::getParen(T.getOpenLocation(),
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004498 T.getCloseLocation()),
4499 attrs, T.getCloseLocation());
Argyrios Kyrtzidis8ae36842008-10-07 10:21:57 +00004500
4501 D.setGroupingParens(hadGroupingParens);
Richard Smith0efa75c2012-03-29 01:16:42 +00004502
4503 // An ellipsis cannot be placed outside parentheses.
4504 if (EllipsisLoc.isValid())
4505 diagnoseMisplacedEllipsis(*this, D, EllipsisLoc);
4506
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004507 return;
4508 }
Mike Stump11289f42009-09-09 15:08:12 +00004509
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004510 // Okay, if this wasn't a grouping paren, it must be the start of a function
4511 // argument list. Recognize that this declarator will never have an
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00004512 // identifier (and remember where it would have been), then call into
4513 // ParseFunctionDeclarator to handle of argument list.
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004514 D.SetIdentifier(0, Tok.getLocation());
4515
David Blaikie15a430a2011-12-04 05:04:18 +00004516 // Enter function-declaration scope, limiting any declarators to the
4517 // function prototype scope, including parameter declarators.
4518 ParseScope PrototypeScope(this,
4519 Scope::FunctionPrototypeScope|Scope::DeclScope);
Richard Smith943c4402012-07-30 21:30:52 +00004520 ParseFunctionDeclarator(D, attrs, T, false, RequiresArg);
David Blaikie15a430a2011-12-04 05:04:18 +00004521 PrototypeScope.Exit();
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004522}
4523
4524/// ParseFunctionDeclarator - We are after the identifier and have parsed the
4525/// declarator D up to a paren, which indicates that we are parsing function
4526/// arguments.
Chris Lattneracd58a32006-08-06 17:24:14 +00004527///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004528/// If FirstArgAttrs is non-null, then the caller parsed those arguments
4529/// immediately after the open paren - they should be considered to be the
4530/// first argument of a parameter.
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00004531///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004532/// If RequiresArg is true, then the first argument of the function is required
4533/// to be present and required to not be an identifier list.
Douglas Gregor9e66af42011-07-05 16:44:18 +00004534///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004535/// For C++, after the parameter-list, it also parses the cv-qualifier-seq[opt],
4536/// (C++11) ref-qualifier[opt], exception-specification[opt],
4537/// (C++11) attribute-specifier-seq[opt], and (C++11) trailing-return-type[opt].
4538///
4539/// [C++11] exception-specification:
Douglas Gregor9e66af42011-07-05 16:44:18 +00004540/// dynamic-exception-specification
4541/// noexcept-specification
4542///
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004543void Parser::ParseFunctionDeclarator(Declarator &D,
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004544 ParsedAttributes &FirstArgAttrs,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004545 BalancedDelimiterTracker &Tracker,
Richard Smith943c4402012-07-30 21:30:52 +00004546 bool IsAmbiguous,
Douglas Gregor9e66af42011-07-05 16:44:18 +00004547 bool RequiresArg) {
Chad Rosierc1183952012-06-26 22:30:43 +00004548 assert(getCurScope()->isFunctionPrototypeScope() &&
David Blaikie15a430a2011-12-04 05:04:18 +00004549 "Should call from a Function scope");
Douglas Gregor9e66af42011-07-05 16:44:18 +00004550 // lparen is already consumed!
4551 assert(D.isPastIdentifier() && "Should not call before identifier!");
4552
4553 // This should be true when the function has typed arguments.
4554 // Otherwise, it is treated as a K&R-style function.
4555 bool HasProto = false;
4556 // Build up an array of information about the parsed arguments.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004557 SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Douglas Gregor9e66af42011-07-05 16:44:18 +00004558 // Remember where we see an ellipsis, if any.
4559 SourceLocation EllipsisLoc;
4560
4561 DeclSpec DS(AttrFactory);
4562 bool RefQualifierIsLValueRef = true;
4563 SourceLocation RefQualifierLoc;
Douglas Gregore248eea2011-10-19 06:04:55 +00004564 SourceLocation ConstQualifierLoc;
4565 SourceLocation VolatileQualifierLoc;
Douglas Gregor9e66af42011-07-05 16:44:18 +00004566 ExceptionSpecificationType ESpecType = EST_None;
4567 SourceRange ESpecRange;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004568 SmallVector<ParsedType, 2> DynamicExceptions;
4569 SmallVector<SourceRange, 2> DynamicExceptionRanges;
Douglas Gregor9e66af42011-07-05 16:44:18 +00004570 ExprResult NoexceptExpr;
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004571 ParsedAttributes FnAttrs(AttrFactory);
Richard Smith700537c2012-06-12 01:51:59 +00004572 TypeResult TrailingReturnType;
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004573
James Molloy6f8780b2012-02-29 10:24:19 +00004574 Actions.ActOnStartFunctionDeclarator();
4575
Douglas Gregor9e66af42011-07-05 16:44:18 +00004576 SourceLocation EndLoc;
Douglas Gregor9e66af42011-07-05 16:44:18 +00004577 if (isFunctionDeclaratorIdentifierList()) {
4578 if (RequiresArg)
4579 Diag(Tok, diag::err_argument_required_after_attribute);
4580
4581 ParseFunctionDeclaratorIdentifierList(D, ParamInfo);
4582
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004583 Tracker.consumeClose();
4584 EndLoc = Tracker.getCloseLocation();
Douglas Gregor9e66af42011-07-05 16:44:18 +00004585 } else {
Douglas Gregor9e66af42011-07-05 16:44:18 +00004586 if (Tok.isNot(tok::r_paren))
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004587 ParseParameterDeclarationClause(D, FirstArgAttrs, ParamInfo, EllipsisLoc);
Douglas Gregor9e66af42011-07-05 16:44:18 +00004588 else if (RequiresArg)
4589 Diag(Tok, diag::err_argument_required_after_attribute);
4590
David Blaikiebbafb8a2012-03-11 07:00:24 +00004591 HasProto = ParamInfo.size() || getLangOpts().CPlusPlus;
Douglas Gregor9e66af42011-07-05 16:44:18 +00004592
4593 // If we have the closing ')', eat it.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004594 Tracker.consumeClose();
4595 EndLoc = Tracker.getCloseLocation();
Douglas Gregor9e66af42011-07-05 16:44:18 +00004596
David Blaikiebbafb8a2012-03-11 07:00:24 +00004597 if (getLangOpts().CPlusPlus) {
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004598 // FIXME: Accept these components in any order, and produce fixits to
4599 // correct the order if the user gets it wrong. Ideally we should deal
4600 // with the virt-specifier-seq and pure-specifier in the same way.
Douglas Gregor9e66af42011-07-05 16:44:18 +00004601
4602 // Parse cv-qualifier-seq[opt].
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004603 ParseTypeQualifierListOpt(DS, false /*no attributes*/, false);
4604 if (!DS.getSourceRange().getEnd().isInvalid()) {
4605 EndLoc = DS.getSourceRange().getEnd();
4606 ConstQualifierLoc = DS.getConstSpecLoc();
4607 VolatileQualifierLoc = DS.getVolatileSpecLoc();
4608 }
Douglas Gregor9e66af42011-07-05 16:44:18 +00004609
4610 // Parse ref-qualifier[opt].
4611 if (Tok.is(tok::amp) || Tok.is(tok::ampamp)) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00004612 Diag(Tok, getLangOpts().CPlusPlus0x ?
Richard Smith5d164bc2011-10-15 05:09:34 +00004613 diag::warn_cxx98_compat_ref_qualifier :
4614 diag::ext_ref_qualifier);
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004615
Douglas Gregor9e66af42011-07-05 16:44:18 +00004616 RefQualifierIsLValueRef = Tok.is(tok::amp);
4617 RefQualifierLoc = ConsumeToken();
4618 EndLoc = RefQualifierLoc;
4619 }
4620
Douglas Gregor3024f072012-04-16 07:05:22 +00004621 // C++11 [expr.prim.general]p3:
Chad Rosierc1183952012-06-26 22:30:43 +00004622 // If a declaration declares a member function or member function
4623 // template of a class X, the expression this is a prvalue of type
Douglas Gregor3024f072012-04-16 07:05:22 +00004624 // "pointer to cv-qualifier-seq X" between the optional cv-qualifer-seq
Chad Rosierc1183952012-06-26 22:30:43 +00004625 // and the end of the function-definition, member-declarator, or
Douglas Gregor3024f072012-04-16 07:05:22 +00004626 // declarator.
Chad Rosierc1183952012-06-26 22:30:43 +00004627 bool IsCXX11MemberFunction =
Douglas Gregor3024f072012-04-16 07:05:22 +00004628 getLangOpts().CPlusPlus0x &&
4629 (D.getContext() == Declarator::MemberContext ||
4630 (D.getContext() == Declarator::FileContext &&
Chad Rosierc1183952012-06-26 22:30:43 +00004631 D.getCXXScopeSpec().isValid() &&
Douglas Gregor3024f072012-04-16 07:05:22 +00004632 Actions.CurContext->isRecord()));
4633 Sema::CXXThisScopeRAII ThisScope(Actions,
4634 dyn_cast<CXXRecordDecl>(Actions.CurContext),
4635 DS.getTypeQualifiers(),
4636 IsCXX11MemberFunction);
Richard Smith2331bbf2012-05-02 22:22:32 +00004637
Douglas Gregor9e66af42011-07-05 16:44:18 +00004638 // Parse exception-specification[opt].
Richard Smith2331bbf2012-05-02 22:22:32 +00004639 ESpecType = tryParseExceptionSpecification(ESpecRange,
Douglas Gregor433e0532012-04-16 18:27:27 +00004640 DynamicExceptions,
4641 DynamicExceptionRanges,
Richard Smith2331bbf2012-05-02 22:22:32 +00004642 NoexceptExpr);
Douglas Gregor9e66af42011-07-05 16:44:18 +00004643 if (ESpecType != EST_None)
4644 EndLoc = ESpecRange.getEnd();
4645
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004646 // Parse attribute-specifier-seq[opt]. Per DR 979 and DR 1297, this goes
4647 // after the exception-specification.
4648 MaybeParseCXX0XAttributes(FnAttrs);
4649
Douglas Gregor9e66af42011-07-05 16:44:18 +00004650 // Parse trailing-return-type[opt].
David Blaikiebbafb8a2012-03-11 07:00:24 +00004651 if (getLangOpts().CPlusPlus0x && Tok.is(tok::arrow)) {
Richard Smith5d164bc2011-10-15 05:09:34 +00004652 Diag(Tok, diag::warn_cxx98_compat_trailing_return_type);
Douglas Gregordb0b9f12011-08-04 15:30:47 +00004653 SourceRange Range;
Richard Smith700537c2012-06-12 01:51:59 +00004654 TrailingReturnType = ParseTrailingReturnType(Range);
Douglas Gregordb0b9f12011-08-04 15:30:47 +00004655 if (Range.getEnd().isValid())
4656 EndLoc = Range.getEnd();
Douglas Gregor9e66af42011-07-05 16:44:18 +00004657 }
4658 }
Douglas Gregor9e66af42011-07-05 16:44:18 +00004659 }
4660
4661 // Remember that we parsed a function type, and remember the attributes.
4662 D.AddTypeInfo(DeclaratorChunk::getFunction(HasProto,
4663 /*isVariadic=*/EllipsisLoc.isValid(),
Richard Smith943c4402012-07-30 21:30:52 +00004664 IsAmbiguous, EllipsisLoc,
Douglas Gregor9e66af42011-07-05 16:44:18 +00004665 ParamInfo.data(), ParamInfo.size(),
4666 DS.getTypeQualifiers(),
4667 RefQualifierIsLValueRef,
Douglas Gregore248eea2011-10-19 06:04:55 +00004668 RefQualifierLoc, ConstQualifierLoc,
4669 VolatileQualifierLoc,
Douglas Gregorad69e652011-07-13 21:47:47 +00004670 /*MutableLoc=*/SourceLocation(),
Douglas Gregor9e66af42011-07-05 16:44:18 +00004671 ESpecType, ESpecRange.getBegin(),
4672 DynamicExceptions.data(),
4673 DynamicExceptionRanges.data(),
4674 DynamicExceptions.size(),
4675 NoexceptExpr.isUsable() ?
4676 NoexceptExpr.get() : 0,
Chad Rosierc1183952012-06-26 22:30:43 +00004677 Tracker.getOpenLocation(),
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004678 EndLoc, D,
Douglas Gregor9e66af42011-07-05 16:44:18 +00004679 TrailingReturnType),
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004680 FnAttrs, EndLoc);
James Molloy6f8780b2012-02-29 10:24:19 +00004681
4682 Actions.ActOnEndFunctionDeclarator();
Douglas Gregor9e66af42011-07-05 16:44:18 +00004683}
4684
4685/// isFunctionDeclaratorIdentifierList - This parameter list may have an
4686/// identifier list form for a K&R-style function: void foo(a,b,c)
4687///
4688/// Note that identifier-lists are only allowed for normal declarators, not for
4689/// abstract-declarators.
4690bool Parser::isFunctionDeclaratorIdentifierList() {
David Blaikiebbafb8a2012-03-11 07:00:24 +00004691 return !getLangOpts().CPlusPlus
Douglas Gregor9e66af42011-07-05 16:44:18 +00004692 && Tok.is(tok::identifier)
4693 && !TryAltiVecVectorToken()
4694 // K&R identifier lists can't have typedefs as identifiers, per C99
4695 // 6.7.5.3p11.
4696 && (TryAnnotateTypeOrScopeToken() || !Tok.is(tok::annot_typename))
4697 // Identifier lists follow a really simple grammar: the identifiers can
4698 // be followed *only* by a ", identifier" or ")". However, K&R
4699 // identifier lists are really rare in the brave new modern world, and
4700 // it is very common for someone to typo a type in a non-K&R style
4701 // list. If we are presented with something like: "void foo(intptr x,
4702 // float y)", we don't want to start parsing the function declarator as
4703 // though it is a K&R style declarator just because intptr is an
4704 // invalid type.
4705 //
4706 // To handle this, we check to see if the token after the first
4707 // identifier is a "," or ")". Only then do we parse it as an
4708 // identifier list.
4709 && (NextToken().is(tok::comma) || NextToken().is(tok::r_paren));
4710}
4711
4712/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
4713/// we found a K&R-style identifier list instead of a typed parameter list.
4714///
4715/// After returning, ParamInfo will hold the parsed parameters.
4716///
4717/// identifier-list: [C99 6.7.5]
4718/// identifier
4719/// identifier-list ',' identifier
4720///
4721void Parser::ParseFunctionDeclaratorIdentifierList(
4722 Declarator &D,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004723 SmallVector<DeclaratorChunk::ParamInfo, 16> &ParamInfo) {
Douglas Gregor9e66af42011-07-05 16:44:18 +00004724 // If there was no identifier specified for the declarator, either we are in
4725 // an abstract-declarator, or we are in a parameter declarator which was found
4726 // to be abstract. In abstract-declarators, identifier lists are not valid:
4727 // diagnose this.
4728 if (!D.getIdentifier())
4729 Diag(Tok, diag::ext_ident_list_in_param);
4730
4731 // Maintain an efficient lookup of params we have seen so far.
4732 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
4733
4734 while (1) {
4735 // If this isn't an identifier, report the error and skip until ')'.
4736 if (Tok.isNot(tok::identifier)) {
4737 Diag(Tok, diag::err_expected_ident);
4738 SkipUntil(tok::r_paren, /*StopAtSemi=*/true, /*DontConsume=*/true);
4739 // Forget we parsed anything.
4740 ParamInfo.clear();
4741 return;
4742 }
4743
4744 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
4745
4746 // Reject 'typedef int y; int test(x, y)', but continue parsing.
4747 if (Actions.getTypeName(*ParmII, Tok.getLocation(), getCurScope()))
4748 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
4749
4750 // Verify that the argument identifier has not already been mentioned.
4751 if (!ParamsSoFar.insert(ParmII)) {
4752 Diag(Tok, diag::err_param_redefinition) << ParmII;
4753 } else {
4754 // Remember this identifier in ParamInfo.
4755 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
4756 Tok.getLocation(),
4757 0));
4758 }
4759
4760 // Eat the identifier.
4761 ConsumeToken();
4762
4763 // The list continues if we see a comma.
4764 if (Tok.isNot(tok::comma))
4765 break;
4766 ConsumeToken();
4767 }
4768}
4769
4770/// ParseParameterDeclarationClause - Parse a (possibly empty) parameter-list
4771/// after the opening parenthesis. This function will not parse a K&R-style
4772/// identifier list.
4773///
Richard Smith2620cd92012-04-11 04:01:28 +00004774/// D is the declarator being parsed. If FirstArgAttrs is non-null, then the
4775/// caller parsed those arguments immediately after the open paren - they should
4776/// be considered to be part of the first parameter.
Douglas Gregor9e66af42011-07-05 16:44:18 +00004777///
4778/// After returning, ParamInfo will hold the parsed parameters. EllipsisLoc will
4779/// be the location of the ellipsis, if any was parsed.
4780///
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004781/// parameter-type-list: [C99 6.7.5]
4782/// parameter-list
4783/// parameter-list ',' '...'
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00004784/// [C++] parameter-list '...'
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004785///
4786/// parameter-list: [C99 6.7.5]
4787/// parameter-declaration
4788/// parameter-list ',' parameter-declaration
4789///
4790/// parameter-declaration: [C99 6.7.5]
4791/// declaration-specifiers declarator
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00004792/// [C++] declaration-specifiers declarator '=' assignment-expression
Sebastian Redldb63af22012-03-14 15:54:00 +00004793/// [C++11] initializer-clause
Chris Lattnere37e2332006-08-15 04:50:22 +00004794/// [GNU] declaration-specifiers declarator attributes
Sebastian Redlf769df52009-03-24 22:27:57 +00004795/// declaration-specifiers abstract-declarator[opt]
4796/// [C++] declaration-specifiers abstract-declarator[opt]
Chris Lattner58258242008-04-10 02:22:51 +00004797/// '=' assignment-expression
Chris Lattnere37e2332006-08-15 04:50:22 +00004798/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
Richard Smith2620cd92012-04-11 04:01:28 +00004799/// [C++11] attribute-specifier-seq parameter-declaration
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004800///
Douglas Gregor9e66af42011-07-05 16:44:18 +00004801void Parser::ParseParameterDeclarationClause(
4802 Declarator &D,
Richard Smith2620cd92012-04-11 04:01:28 +00004803 ParsedAttributes &FirstArgAttrs,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004804 SmallVector<DeclaratorChunk::ParamInfo, 16> &ParamInfo,
Douglas Gregor9e66af42011-07-05 16:44:18 +00004805 SourceLocation &EllipsisLoc) {
Mike Stump11289f42009-09-09 15:08:12 +00004806
Chris Lattner371ed4e2008-04-06 06:57:35 +00004807 while (1) {
4808 if (Tok.is(tok::ellipsis)) {
Richard Smith2620cd92012-04-11 04:01:28 +00004809 // FIXME: Issue a diagnostic if we parsed an attribute-specifier-seq
4810 // before deciding this was a parameter-declaration-clause.
Douglas Gregor94349fd2009-02-18 07:07:28 +00004811 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chris Lattner371ed4e2008-04-06 06:57:35 +00004812 break;
Chris Lattneracd58a32006-08-06 17:24:14 +00004813 }
Mike Stump11289f42009-09-09 15:08:12 +00004814
Chris Lattner371ed4e2008-04-06 06:57:35 +00004815 // Parse the declaration-specifiers.
John McCall28a6aea2009-11-04 02:18:39 +00004816 // Just use the ParsingDeclaration "scope" of the declarator.
John McCall084e83d2011-03-24 11:26:52 +00004817 DeclSpec DS(AttrFactory);
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004818
Richard Smith2620cd92012-04-11 04:01:28 +00004819 // Parse any C++11 attributes.
4820 MaybeParseCXX0XAttributes(DS.getAttributes());
4821
John McCall53fa7142010-12-24 02:08:15 +00004822 // Skip any Microsoft attributes before a param.
David Blaikiebbafb8a2012-03-11 07:00:24 +00004823 if (getLangOpts().MicrosoftExt && Tok.is(tok::l_square))
John McCall53fa7142010-12-24 02:08:15 +00004824 ParseMicrosoftAttributes(DS.getAttributes());
4825
4826 SourceLocation DSStart = Tok.getLocation();
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00004827
4828 // If the caller parsed attributes for the first argument, add them now.
John McCall53fa7142010-12-24 02:08:15 +00004829 // Take them so that we only apply the attributes to the first parameter.
Douglas Gregor9e66af42011-07-05 16:44:18 +00004830 // FIXME: If we can leave the attributes in the token stream somehow, we can
Richard Smith2620cd92012-04-11 04:01:28 +00004831 // get rid of a parameter (FirstArgAttrs) and this statement. It might be
4832 // too much hassle.
4833 DS.takeAttributesFrom(FirstArgAttrs);
John McCall53fa7142010-12-24 02:08:15 +00004834
Chris Lattnerde39c3e2009-02-27 18:38:20 +00004835 ParseDeclarationSpecifiers(DS);
Mike Stump11289f42009-09-09 15:08:12 +00004836
Chris Lattner371ed4e2008-04-06 06:57:35 +00004837 // Parse the declarator. This is "PrototypeContext", because we must
4838 // accept either 'declarator' or 'abstract-declarator' here.
4839 Declarator ParmDecl(DS, Declarator::PrototypeContext);
4840 ParseDeclarator(ParmDecl);
4841
4842 // Parse GNU attributes, if present.
John McCall53fa7142010-12-24 02:08:15 +00004843 MaybeParseGNUAttributes(ParmDecl);
Mike Stump11289f42009-09-09 15:08:12 +00004844
Chris Lattner371ed4e2008-04-06 06:57:35 +00004845 // Remember this parsed parameter in ParamInfo.
4846 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
Mike Stump11289f42009-09-09 15:08:12 +00004847
Douglas Gregor4d87df52008-12-16 21:30:33 +00004848 // DefArgToks is used when the parsing of default arguments needs
4849 // to be delayed.
4850 CachedTokens *DefArgToks = 0;
4851
Chris Lattner371ed4e2008-04-06 06:57:35 +00004852 // If no parameter was specified, verify that *something* was specified,
4853 // otherwise we have a missing type and identifier.
Chris Lattnerde39c3e2009-02-27 18:38:20 +00004854 if (DS.isEmpty() && ParmDecl.getIdentifier() == 0 &&
4855 ParmDecl.getNumTypeObjects() == 0) {
Chris Lattner371ed4e2008-04-06 06:57:35 +00004856 // Completely missing, emit error.
4857 Diag(DSStart, diag::err_missing_param);
4858 } else {
4859 // Otherwise, we have something. Add it and let semantic analysis try
4860 // to grok it and add the result to the ParamInfo we are building.
Mike Stump11289f42009-09-09 15:08:12 +00004861
Chris Lattner371ed4e2008-04-06 06:57:35 +00004862 // Inform the actions module about the parameter declarator, so it gets
4863 // added to the current scope.
John McCall48871652010-08-21 09:40:31 +00004864 Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl);
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00004865
4866 // Parse the default argument, if any. We parse the default
4867 // arguments in all dialects; the semantic analysis in
4868 // ActOnParamDefaultArgument will reject the default argument in
4869 // C.
4870 if (Tok.is(tok::equal)) {
Douglas Gregor58354032008-12-24 00:01:03 +00004871 SourceLocation EqualLoc = Tok.getLocation();
4872
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00004873 // Parse the default argument
Douglas Gregor4d87df52008-12-16 21:30:33 +00004874 if (D.getContext() == Declarator::MemberContext) {
4875 // If we're inside a class definition, cache the tokens
4876 // corresponding to the default argument. We'll actually parse
4877 // them when we see the end of the class definition.
Douglas Gregor4d87df52008-12-16 21:30:33 +00004878 // FIXME: Can we use a smart pointer for Toks?
4879 DefArgToks = new CachedTokens;
4880
Mike Stump11289f42009-09-09 15:08:12 +00004881 if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks,
Argyrios Kyrtzidis8d7bdba2010-04-23 21:20:12 +00004882 /*StopAtSemi=*/true,
4883 /*ConsumeFinalToken=*/false)) {
Douglas Gregor4d87df52008-12-16 21:30:33 +00004884 delete DefArgToks;
4885 DefArgToks = 0;
Douglas Gregor58354032008-12-24 00:01:03 +00004886 Actions.ActOnParamDefaultArgumentError(Param);
Argyrios Kyrtzidis249179c2010-08-06 09:47:24 +00004887 } else {
4888 // Mark the end of the default argument so that we know when to
4889 // stop when we parse it later on.
4890 Token DefArgEnd;
4891 DefArgEnd.startToken();
4892 DefArgEnd.setKind(tok::cxx_defaultarg_end);
4893 DefArgEnd.setLocation(Tok.getLocation());
4894 DefArgToks->push_back(DefArgEnd);
Mike Stump11289f42009-09-09 15:08:12 +00004895 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc,
Anders Carlsson84613c42009-06-12 16:51:40 +00004896 (*DefArgToks)[1].getLocation());
Argyrios Kyrtzidis249179c2010-08-06 09:47:24 +00004897 }
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00004898 } else {
Douglas Gregor4d87df52008-12-16 21:30:33 +00004899 // Consume the '='.
Douglas Gregor58354032008-12-24 00:01:03 +00004900 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00004901
Chad Rosierc1183952012-06-26 22:30:43 +00004902 // The argument isn't actually potentially evaluated unless it is
Douglas Gregor8a01b2a2010-09-11 20:24:53 +00004903 // used.
4904 EnterExpressionEvaluationContext Eval(Actions,
Douglas Gregor7fcbd902012-02-21 00:37:24 +00004905 Sema::PotentiallyEvaluatedIfUsed,
4906 Param);
Douglas Gregor8a01b2a2010-09-11 20:24:53 +00004907
Sebastian Redldb63af22012-03-14 15:54:00 +00004908 ExprResult DefArgResult;
Sebastian Redl1678d5f2012-03-18 22:25:45 +00004909 if (getLangOpts().CPlusPlus0x && Tok.is(tok::l_brace)) {
4910 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
Sebastian Redldb63af22012-03-14 15:54:00 +00004911 DefArgResult = ParseBraceInitializer();
Sebastian Redl1678d5f2012-03-18 22:25:45 +00004912 } else
Sebastian Redldb63af22012-03-14 15:54:00 +00004913 DefArgResult = ParseAssignmentExpression();
Douglas Gregor4d87df52008-12-16 21:30:33 +00004914 if (DefArgResult.isInvalid()) {
4915 Actions.ActOnParamDefaultArgumentError(Param);
4916 SkipUntil(tok::comma, tok::r_paren, true, true);
4917 } else {
4918 // Inform the actions module about the default argument
4919 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
John McCallb268a282010-08-23 23:25:46 +00004920 DefArgResult.take());
Douglas Gregor4d87df52008-12-16 21:30:33 +00004921 }
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00004922 }
4923 }
Mike Stump11289f42009-09-09 15:08:12 +00004924
4925 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
4926 ParmDecl.getIdentifierLoc(), Param,
Douglas Gregor4d87df52008-12-16 21:30:33 +00004927 DefArgToks));
Chris Lattner371ed4e2008-04-06 06:57:35 +00004928 }
4929
4930 // If the next token is a comma, consume it and keep reading arguments.
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00004931 if (Tok.isNot(tok::comma)) {
4932 if (Tok.is(tok::ellipsis)) {
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00004933 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chad Rosierc1183952012-06-26 22:30:43 +00004934
David Blaikiebbafb8a2012-03-11 07:00:24 +00004935 if (!getLangOpts().CPlusPlus) {
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00004936 // We have ellipsis without a preceding ',', which is ill-formed
4937 // in C. Complain and provide the fix.
4938 Diag(EllipsisLoc, diag::err_missing_comma_before_ellipsis)
Douglas Gregora771f462010-03-31 17:46:05 +00004939 << FixItHint::CreateInsertion(EllipsisLoc, ", ");
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00004940 }
4941 }
Chad Rosierc1183952012-06-26 22:30:43 +00004942
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00004943 break;
4944 }
Mike Stump11289f42009-09-09 15:08:12 +00004945
Chris Lattner371ed4e2008-04-06 06:57:35 +00004946 // Consume the comma.
4947 ConsumeToken();
Chris Lattneracd58a32006-08-06 17:24:14 +00004948 }
Mike Stump11289f42009-09-09 15:08:12 +00004949
Chris Lattner6c940e62008-04-06 06:34:08 +00004950}
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004951
Chris Lattnere8074e62006-08-06 18:30:15 +00004952/// [C90] direct-declarator '[' constant-expression[opt] ']'
4953/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
4954/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
4955/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
4956/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004957/// [C++11] direct-declarator '[' constant-expression[opt] ']'
4958/// attribute-specifier-seq[opt]
Chris Lattnere8074e62006-08-06 18:30:15 +00004959void Parser::ParseBracketDeclarator(Declarator &D) {
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004960 if (CheckProhibitedCXX11Attribute())
4961 return;
4962
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004963 BalancedDelimiterTracker T(*this, tok::l_square);
4964 T.consumeOpen();
Mike Stump11289f42009-09-09 15:08:12 +00004965
Chris Lattner84a11622008-12-18 07:27:21 +00004966 // C array syntax has many features, but by-far the most common is [] and [4].
4967 // This code does a fast path to handle some of the most obvious cases.
4968 if (Tok.getKind() == tok::r_square) {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004969 T.consumeClose();
John McCall084e83d2011-03-24 11:26:52 +00004970 ParsedAttributes attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00004971 MaybeParseCXX0XAttributes(attrs);
Chad Rosierc1183952012-06-26 22:30:43 +00004972
Chris Lattner84a11622008-12-18 07:27:21 +00004973 // Remember that we parsed the empty array type.
John McCalldadc5752010-08-24 06:29:42 +00004974 ExprResult NumElements;
John McCall084e83d2011-03-24 11:26:52 +00004975 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004976 T.getOpenLocation(),
4977 T.getCloseLocation()),
4978 attrs, T.getCloseLocation());
Chris Lattner84a11622008-12-18 07:27:21 +00004979 return;
4980 } else if (Tok.getKind() == tok::numeric_constant &&
4981 GetLookAheadToken(1).is(tok::r_square)) {
4982 // [4] is very common. Parse the numeric constant expression.
Richard Smithbcc22fc2012-03-09 08:00:36 +00004983 ExprResult ExprRes(Actions.ActOnNumericConstant(Tok, getCurScope()));
Chris Lattner84a11622008-12-18 07:27:21 +00004984 ConsumeToken();
4985
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004986 T.consumeClose();
John McCall084e83d2011-03-24 11:26:52 +00004987 ParsedAttributes attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00004988 MaybeParseCXX0XAttributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +00004989
Chris Lattner84a11622008-12-18 07:27:21 +00004990 // Remember that we parsed a array type, and remember its features.
John McCall084e83d2011-03-24 11:26:52 +00004991 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, 0,
John McCall53fa7142010-12-24 02:08:15 +00004992 ExprRes.release(),
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004993 T.getOpenLocation(),
4994 T.getCloseLocation()),
4995 attrs, T.getCloseLocation());
Chris Lattner84a11622008-12-18 07:27:21 +00004996 return;
4997 }
Mike Stump11289f42009-09-09 15:08:12 +00004998
Chris Lattnere8074e62006-08-06 18:30:15 +00004999 // If valid, this location is the position where we read the 'static' keyword.
5000 SourceLocation StaticLoc;
Chris Lattner76c72282007-10-09 17:33:22 +00005001 if (Tok.is(tok::kw_static))
Chris Lattneraf635312006-10-16 06:06:51 +00005002 StaticLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00005003
Chris Lattnere8074e62006-08-06 18:30:15 +00005004 // If there is a type-qualifier-list, read it now.
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00005005 // Type qualifiers in an array subscript are a C99 feature.
John McCall084e83d2011-03-24 11:26:52 +00005006 DeclSpec DS(AttrFactory);
Chris Lattnercf0bab22008-12-18 07:02:59 +00005007 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Mike Stump11289f42009-09-09 15:08:12 +00005008
Chris Lattnere8074e62006-08-06 18:30:15 +00005009 // If we haven't already read 'static', check to see if there is one after the
5010 // type-qualifier-list.
Chris Lattner76c72282007-10-09 17:33:22 +00005011 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Chris Lattneraf635312006-10-16 06:06:51 +00005012 StaticLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00005013
Chris Lattnere8074e62006-08-06 18:30:15 +00005014 // Handle "direct-declarator [ type-qual-list[opt] * ]".
Chris Lattnere8074e62006-08-06 18:30:15 +00005015 bool isStar = false;
John McCalldadc5752010-08-24 06:29:42 +00005016 ExprResult NumElements;
Mike Stump11289f42009-09-09 15:08:12 +00005017
Chris Lattner521ff2b2008-04-06 05:26:30 +00005018 // Handle the case where we have '[*]' as the array size. However, a leading
5019 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
Sylvestre Ledru830885c2012-07-23 08:59:39 +00005020 // the token after the star is a ']'. Since stars in arrays are
Chris Lattner521ff2b2008-04-06 05:26:30 +00005021 // infrequent, use of lookahead is not costly here.
5022 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattnerc439f0d2008-04-06 05:27:21 +00005023 ConsumeToken(); // Eat the '*'.
Chris Lattner1906f802006-08-06 19:14:46 +00005024
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00005025 if (StaticLoc.isValid()) {
Chris Lattner521ff2b2008-04-06 05:26:30 +00005026 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00005027 StaticLoc = SourceLocation(); // Drop the static.
5028 }
Chris Lattner521ff2b2008-04-06 05:26:30 +00005029 isStar = true;
Chris Lattner76c72282007-10-09 17:33:22 +00005030 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner84a11622008-12-18 07:27:21 +00005031 // Note, in C89, this production uses the constant-expr production instead
5032 // of assignment-expr. The only difference is that assignment-expr allows
5033 // things like '=' and '*='. Sema rejects these in C89 mode because they
5034 // are not i-c-e's, so we don't need to distinguish between the two here.
Mike Stump11289f42009-09-09 15:08:12 +00005035
Douglas Gregorc9c02ed2009-06-19 23:52:42 +00005036 // Parse the constant-expression or assignment-expression now (depending
5037 // on dialect).
David Blaikiebbafb8a2012-03-11 07:00:24 +00005038 if (getLangOpts().CPlusPlus) {
Douglas Gregorc9c02ed2009-06-19 23:52:42 +00005039 NumElements = ParseConstantExpression();
Eli Friedmane0afc982012-01-21 01:01:51 +00005040 } else {
5041 EnterExpressionEvaluationContext Unevaluated(Actions,
5042 Sema::ConstantEvaluated);
Douglas Gregorc9c02ed2009-06-19 23:52:42 +00005043 NumElements = ParseAssignmentExpression();
Eli Friedmane0afc982012-01-21 01:01:51 +00005044 }
Chris Lattner62591722006-08-12 18:40:58 +00005045 }
Mike Stump11289f42009-09-09 15:08:12 +00005046
Chris Lattner62591722006-08-12 18:40:58 +00005047 // If there was an error parsing the assignment-expression, recover.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00005048 if (NumElements.isInvalid()) {
Chris Lattnercd2a8c52009-04-24 22:30:50 +00005049 D.setInvalidType(true);
Chris Lattner62591722006-08-12 18:40:58 +00005050 // If the expression was invalid, skip it.
5051 SkipUntil(tok::r_square);
5052 return;
Chris Lattnere8074e62006-08-06 18:30:15 +00005053 }
Sebastian Redlf6591ca2009-02-09 18:23:29 +00005054
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005055 T.consumeClose();
Sebastian Redlf6591ca2009-02-09 18:23:29 +00005056
John McCall084e83d2011-03-24 11:26:52 +00005057 ParsedAttributes attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00005058 MaybeParseCXX0XAttributes(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +00005059
Chris Lattner84a11622008-12-18 07:27:21 +00005060 // Remember that we parsed a array type, and remember its features.
John McCall084e83d2011-03-24 11:26:52 +00005061 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
Chris Lattnercbc426d2006-12-02 06:43:02 +00005062 StaticLoc.isValid(), isStar,
Douglas Gregor04318252009-07-06 15:59:29 +00005063 NumElements.release(),
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005064 T.getOpenLocation(),
5065 T.getCloseLocation()),
5066 attrs, T.getCloseLocation());
Chris Lattnere8074e62006-08-06 18:30:15 +00005067}
5068
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00005069/// [GNU] typeof-specifier:
5070/// typeof ( expressions )
5071/// typeof ( type-name )
5072/// [GNU/C++] typeof unary-expression
Steve Naroffad373bd2007-07-31 12:34:36 +00005073///
5074void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner76c72282007-10-09 17:33:22 +00005075 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005076 Token OpTok = Tok;
Steve Naroffad373bd2007-07-31 12:34:36 +00005077 SourceLocation StartLoc = ConsumeToken();
5078
John McCalle8595032010-01-13 20:03:27 +00005079 const bool hasParens = Tok.is(tok::l_paren);
5080
Eli Friedman15681d62012-09-26 04:34:21 +00005081 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated,
5082 Sema::ReuseLambdaContextDecl);
Eli Friedmane0afc982012-01-21 01:01:51 +00005083
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005084 bool isCastExpr;
John McCallba7bf592010-08-24 05:47:05 +00005085 ParsedType CastTy;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005086 SourceRange CastRange;
Peter Collingbournee190dee2011-03-11 19:24:49 +00005087 ExprResult Operand = ParseExprAfterUnaryExprOrTypeTrait(OpTok, isCastExpr,
5088 CastTy, CastRange);
John McCalle8595032010-01-13 20:03:27 +00005089 if (hasParens)
5090 DS.setTypeofParensRange(CastRange);
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005091
5092 if (CastRange.getEnd().isInvalid())
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00005093 // FIXME: Not accurate, the range gets one token more than it should.
5094 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005095 else
5096 DS.SetRangeEnd(CastRange.getEnd());
Mike Stump11289f42009-09-09 15:08:12 +00005097
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005098 if (isCastExpr) {
5099 if (!CastTy) {
5100 DS.SetTypeSpecError();
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00005101 return;
Douglas Gregor220cac52009-02-18 17:45:20 +00005102 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00005103
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005104 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00005105 unsigned DiagID;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005106 // Check for duplicate type specifiers (e.g. "int typeof(int)").
5107 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00005108 DiagID, CastTy))
5109 Diag(StartLoc, DiagID) << PrevSpec;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005110 return;
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00005111 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00005112
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00005113 // If we get here, the operand to the typeof was an expresion.
5114 if (Operand.isInvalid()) {
5115 DS.SetTypeSpecError();
Steve Naroff4bd2f712007-08-02 02:53:48 +00005116 return;
Steve Naroffad373bd2007-07-31 12:34:36 +00005117 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00005118
Eli Friedmane0afc982012-01-21 01:01:51 +00005119 // We might need to transform the operand if it is potentially evaluated.
5120 Operand = Actions.HandleExprEvaluationContextForTypeof(Operand.get());
5121 if (Operand.isInvalid()) {
5122 DS.SetTypeSpecError();
5123 return;
5124 }
5125
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00005126 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00005127 unsigned DiagID;
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00005128 // Check for duplicate type specifiers (e.g. "int typeof(int)").
5129 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
John McCallba7bf592010-08-24 05:47:05 +00005130 DiagID, Operand.get()))
John McCall49bfce42009-08-03 20:12:06 +00005131 Diag(StartLoc, DiagID) << PrevSpec;
Steve Naroffad373bd2007-07-31 12:34:36 +00005132}
Chris Lattner73a9c7d2010-02-28 18:33:55 +00005133
Benjamin Kramere56f3932011-12-23 17:00:35 +00005134/// [C11] atomic-specifier:
Eli Friedman0dfb8892011-10-06 23:00:33 +00005135/// _Atomic ( type-name )
5136///
5137void Parser::ParseAtomicSpecifier(DeclSpec &DS) {
5138 assert(Tok.is(tok::kw__Atomic) && "Not an atomic specifier");
5139
5140 SourceLocation StartLoc = ConsumeToken();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005141 BalancedDelimiterTracker T(*this, tok::l_paren);
5142 if (T.expectAndConsume(diag::err_expected_lparen_after, "_Atomic")) {
Eli Friedman0dfb8892011-10-06 23:00:33 +00005143 SkipUntil(tok::r_paren);
5144 return;
5145 }
5146
5147 TypeResult Result = ParseTypeName();
5148 if (Result.isInvalid()) {
5149 SkipUntil(tok::r_paren);
5150 return;
5151 }
5152
5153 // Match the ')'
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005154 T.consumeClose();
Eli Friedman0dfb8892011-10-06 23:00:33 +00005155
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005156 if (T.getCloseLocation().isInvalid())
Eli Friedman0dfb8892011-10-06 23:00:33 +00005157 return;
5158
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005159 DS.setTypeofParensRange(T.getRange());
5160 DS.SetRangeEnd(T.getCloseLocation());
Eli Friedman0dfb8892011-10-06 23:00:33 +00005161
5162 const char *PrevSpec = 0;
5163 unsigned DiagID;
5164 if (DS.SetTypeSpecType(DeclSpec::TST_atomic, StartLoc, PrevSpec,
5165 DiagID, Result.release()))
5166 Diag(StartLoc, DiagID) << PrevSpec;
5167}
5168
Chris Lattner73a9c7d2010-02-28 18:33:55 +00005169
5170/// TryAltiVecVectorTokenOutOfLine - Out of line body that should only be called
5171/// from TryAltiVecVectorToken.
5172bool Parser::TryAltiVecVectorTokenOutOfLine() {
5173 Token Next = NextToken();
5174 switch (Next.getKind()) {
5175 default: return false;
5176 case tok::kw_short:
5177 case tok::kw_long:
5178 case tok::kw_signed:
5179 case tok::kw_unsigned:
5180 case tok::kw_void:
5181 case tok::kw_char:
5182 case tok::kw_int:
5183 case tok::kw_float:
5184 case tok::kw_double:
5185 case tok::kw_bool:
5186 case tok::kw___pixel:
5187 Tok.setKind(tok::kw___vector);
5188 return true;
5189 case tok::identifier:
5190 if (Next.getIdentifierInfo() == Ident_pixel) {
5191 Tok.setKind(tok::kw___vector);
5192 return true;
5193 }
5194 return false;
5195 }
5196}
5197
5198bool Parser::TryAltiVecTokenOutOfLine(DeclSpec &DS, SourceLocation Loc,
5199 const char *&PrevSpec, unsigned &DiagID,
5200 bool &isInvalid) {
5201 if (Tok.getIdentifierInfo() == Ident_vector) {
5202 Token Next = NextToken();
5203 switch (Next.getKind()) {
5204 case tok::kw_short:
5205 case tok::kw_long:
5206 case tok::kw_signed:
5207 case tok::kw_unsigned:
5208 case tok::kw_void:
5209 case tok::kw_char:
5210 case tok::kw_int:
5211 case tok::kw_float:
5212 case tok::kw_double:
5213 case tok::kw_bool:
5214 case tok::kw___pixel:
5215 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
5216 return true;
5217 case tok::identifier:
5218 if (Next.getIdentifierInfo() == Ident_pixel) {
5219 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
5220 return true;
5221 }
5222 break;
5223 default:
5224 break;
5225 }
Douglas Gregor9938e3b2010-06-16 15:28:57 +00005226 } else if ((Tok.getIdentifierInfo() == Ident_pixel) &&
Chris Lattner73a9c7d2010-02-28 18:33:55 +00005227 DS.isTypeAltiVecVector()) {
5228 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
5229 return true;
5230 }
5231 return false;
5232}