blob: 9f8f3cec2889aa2e07236214b8b9b6f13290cd78 [file] [log] [blame]
Chris Lattner7ad0fbe2006-11-05 07:46:30 +00001//===--- ParseDecl.cpp - Declaration Parsing ------------------------------===//
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Declaration portions of the Parser interfaces.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Parse/Parser.h"
Chris Lattner60f36222009-01-29 05:15:15 +000015#include "clang/Parse/ParseDiagnostic.h"
Peter Collingbourne599cb8e2011-03-18 22:38:29 +000016#include "clang/Basic/OpenCL.h"
Kaelyn Uhrain031643e2012-04-26 23:36:17 +000017#include "clang/Sema/Lookup.h"
John McCall8b0666c2010-08-20 18:27:03 +000018#include "clang/Sema/Scope.h"
19#include "clang/Sema/ParsedTemplate.h"
John McCallfaf5fb42010-08-26 23:41:50 +000020#include "clang/Sema/PrettyDeclStackTrace.h"
Chris Lattner8a9a97a2009-12-10 00:21:05 +000021#include "RAIIObjectsForParser.h"
Chris Lattnerad9ac942007-01-23 01:14:52 +000022#include "llvm/ADT/SmallSet.h"
Benjamin Kramer49038022012-02-04 13:45:25 +000023#include "llvm/ADT/SmallString.h"
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +000024#include "llvm/ADT/StringSwitch.h"
Chris Lattnerc0acd3d2006-07-31 05:13:43 +000025using namespace clang;
26
27//===----------------------------------------------------------------------===//
28// C99 6.7: Declarations.
29//===----------------------------------------------------------------------===//
30
Chris Lattnerf5fbd792006-08-10 23:56:11 +000031/// ParseTypeName
32/// type-name: [C99 6.7.6]
33/// specifier-qualifier-list abstract-declarator[opt]
Sebastian Redlbd150f42008-11-21 19:14:01 +000034///
35/// Called type-id in C++.
Douglas Gregor205d5e32011-01-31 16:09:46 +000036TypeResult Parser::ParseTypeName(SourceRange *Range,
John McCall31168b02011-06-15 23:02:42 +000037 Declarator::TheContext Context,
Richard Smithcd1c0552011-07-01 19:46:12 +000038 AccessSpecifier AS,
39 Decl **OwnedType) {
Richard Smith62dad822012-03-15 01:02:11 +000040 DeclSpecContext DSC = getDeclSpecContextFromDeclaratorContext(Context);
Richard Smith2f07ad52012-05-09 20:55:26 +000041 if (DSC == DSC_normal)
42 DSC = DSC_type_specifier;
Richard Smithbfdb1082012-03-12 08:56:40 +000043
Chris Lattnerf5fbd792006-08-10 23:56:11 +000044 // Parse the common declaration-specifiers piece.
John McCall084e83d2011-03-24 11:26:52 +000045 DeclSpec DS(AttrFactory);
Richard Smithbfdb1082012-03-12 08:56:40 +000046 ParseSpecifierQualifierList(DS, AS, DSC);
Richard Smithcd1c0552011-07-01 19:46:12 +000047 if (OwnedType)
48 *OwnedType = DS.isTypeSpecOwned() ? DS.getRepAsDecl() : 0;
Sebastian Redld6434562009-05-29 18:02:33 +000049
Chris Lattnerf5fbd792006-08-10 23:56:11 +000050 // Parse the abstract-declarator, if present.
Douglas Gregor205d5e32011-01-31 16:09:46 +000051 Declarator DeclaratorInfo(DS, Context);
Chris Lattnerf5fbd792006-08-10 23:56:11 +000052 ParseDeclarator(DeclaratorInfo);
Sebastian Redld6434562009-05-29 18:02:33 +000053 if (Range)
54 *Range = DeclaratorInfo.getSourceRange();
55
Chris Lattnerf6d1c9c2009-04-25 08:06:05 +000056 if (DeclaratorInfo.isInvalidType())
Douglas Gregor220cac52009-02-18 17:45:20 +000057 return true;
58
Douglas Gregor0be31a22010-07-02 17:43:08 +000059 return Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
Chris Lattnerf5fbd792006-08-10 23:56:11 +000060}
61
Caitlin Sadowski9385dd72011-09-08 17:42:22 +000062
63/// isAttributeLateParsed - Return true if the attribute has arguments that
64/// require late parsing.
65static bool isAttributeLateParsed(const IdentifierInfo &II) {
66 return llvm::StringSwitch<bool>(II.getName())
67#include "clang/Parse/AttrLateParsed.inc"
68 .Default(false);
69}
70
71
Alexis Hunt96d5c762009-11-21 08:43:09 +000072/// ParseGNUAttributes - Parse a non-empty attributes list.
Chris Lattnerb8cd5c22006-08-15 04:10:46 +000073///
74/// [GNU] attributes:
75/// attribute
76/// attributes attribute
77///
78/// [GNU] attribute:
79/// '__attribute__' '(' '(' attribute-list ')' ')'
80///
81/// [GNU] attribute-list:
82/// attrib
83/// attribute_list ',' attrib
84///
85/// [GNU] attrib:
86/// empty
Steve Naroff0f2fe172007-06-01 17:11:19 +000087/// attrib-name
88/// attrib-name '(' identifier ')'
89/// attrib-name '(' identifier ',' nonempty-expr-list ')'
90/// attrib-name '(' argument-expression-list [C99 6.5.2] ')'
Chris Lattnerb8cd5c22006-08-15 04:10:46 +000091///
Steve Naroff0f2fe172007-06-01 17:11:19 +000092/// [GNU] attrib-name:
93/// identifier
94/// typespec
95/// typequal
96/// storageclass
Mike Stump11289f42009-09-09 15:08:12 +000097///
Steve Naroff0f2fe172007-06-01 17:11:19 +000098/// FIXME: The GCC grammar/code for this construct implies we need two
Mike Stump11289f42009-09-09 15:08:12 +000099/// token lookahead. Comment from gcc: "If they start with an identifier
100/// which is followed by a comma or close parenthesis, then the arguments
Steve Naroff0f2fe172007-06-01 17:11:19 +0000101/// start with that identifier; otherwise they are an expression list."
102///
Richard Smithb12bf692011-10-17 21:20:17 +0000103/// GCC does not require the ',' between attribs in an attribute-list.
104///
Steve Naroff0f2fe172007-06-01 17:11:19 +0000105/// At the moment, I am not doing 2 token lookahead. I am also unaware of
106/// any attributes that don't work (based on my limited testing). Most
107/// attributes are very simple in practice. Until we find a bug, I don't see
108/// a pressing need to implement the 2 token lookahead.
Chris Lattnerb8cd5c22006-08-15 04:10:46 +0000109
John McCall53fa7142010-12-24 02:08:15 +0000110void Parser::ParseGNUAttributes(ParsedAttributes &attrs,
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000111 SourceLocation *endLoc,
112 LateParsedAttrList *LateAttrs) {
Alexis Hunt96d5c762009-11-21 08:43:09 +0000113 assert(Tok.is(tok::kw___attribute) && "Not a GNU attribute list!");
Mike Stump11289f42009-09-09 15:08:12 +0000114
Chris Lattner76c72282007-10-09 17:33:22 +0000115 while (Tok.is(tok::kw___attribute)) {
Steve Naroff0f2fe172007-06-01 17:11:19 +0000116 ConsumeToken();
117 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
118 "attribute")) {
119 SkipUntil(tok::r_paren, true); // skip until ) or ;
John McCall53fa7142010-12-24 02:08:15 +0000120 return;
Steve Naroff0f2fe172007-06-01 17:11:19 +0000121 }
122 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, "(")) {
123 SkipUntil(tok::r_paren, true); // skip until ) or ;
John McCall53fa7142010-12-24 02:08:15 +0000124 return;
Steve Naroff0f2fe172007-06-01 17:11:19 +0000125 }
126 // Parse the attribute-list. e.g. __attribute__(( weak, alias("__f") ))
Chris Lattner76c72282007-10-09 17:33:22 +0000127 while (Tok.is(tok::identifier) || isDeclarationSpecifier() ||
128 Tok.is(tok::comma)) {
Mike Stump11289f42009-09-09 15:08:12 +0000129 if (Tok.is(tok::comma)) {
Steve Naroff0f2fe172007-06-01 17:11:19 +0000130 // allows for empty/non-empty attributes. ((__vector_size__(16),,,,))
131 ConsumeToken();
132 continue;
133 }
134 // we have an identifier or declaration specifier (const, int, etc.)
135 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
136 SourceLocation AttrNameLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000137
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000138 if (Tok.is(tok::l_paren)) {
139 // handle "parameterized" attributes
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +0000140 if (LateAttrs && isAttributeLateParsed(*AttrName)) {
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000141 LateParsedAttribute *LA =
142 new LateParsedAttribute(this, *AttrName, AttrNameLoc);
143 LateAttrs->push_back(LA);
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +0000144
145 // Attributes in a class are parsed at the end of the class, along
146 // with other late-parsed declarations.
147 if (!ClassStack.empty())
148 getCurrentClass().LateParsedDeclarations.push_back(LA);
Mike Stump11289f42009-09-09 15:08:12 +0000149
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000150 // consume everything up to and including the matching right parens
151 ConsumeAndStoreUntil(tok::r_paren, LA->Toks, true, false);
Mike Stump11289f42009-09-09 15:08:12 +0000152
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000153 Token Eof;
154 Eof.startToken();
155 Eof.setLocation(Tok.getLocation());
156 LA->Toks.push_back(Eof);
157 } else {
158 ParseGNUAttributeArgs(AttrName, AttrNameLoc, attrs, endLoc);
Steve Naroff0f2fe172007-06-01 17:11:19 +0000159 }
160 } else {
John McCall084e83d2011-03-24 11:26:52 +0000161 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc,
Alexis Hunta0e54d42012-06-18 16:13:52 +0000162 0, SourceLocation(), 0, 0, AttributeList::AS_GNU);
Steve Naroff0f2fe172007-06-01 17:11:19 +0000163 }
164 }
Steve Naroff98d153c2007-06-06 23:19:11 +0000165 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
Steve Naroff98d153c2007-06-06 23:19:11 +0000166 SkipUntil(tok::r_paren, false);
Alexis Hunt96d5c762009-11-21 08:43:09 +0000167 SourceLocation Loc = Tok.getLocation();
Sebastian Redlf6591ca2009-02-09 18:23:29 +0000168 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) {
169 SkipUntil(tok::r_paren, false);
170 }
John McCall53fa7142010-12-24 02:08:15 +0000171 if (endLoc)
172 *endLoc = Loc;
Steve Naroff0f2fe172007-06-01 17:11:19 +0000173 }
Steve Naroff0f2fe172007-06-01 17:11:19 +0000174}
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000175
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000176
177/// Parse the arguments to a parameterized GNU attribute
178void Parser::ParseGNUAttributeArgs(IdentifierInfo *AttrName,
179 SourceLocation AttrNameLoc,
180 ParsedAttributes &Attrs,
181 SourceLocation *EndLoc) {
182
183 assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('");
184
185 // Availability attributes have their own grammar.
186 if (AttrName->isStr("availability")) {
187 ParseAvailabilityAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc);
188 return;
189 }
190 // Thread safety attributes fit into the FIXME case above, so we
191 // just parse the arguments as a list of expressions
192 if (IsThreadSafetyAttribute(AttrName->getName())) {
193 ParseThreadSafetyAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc);
194 return;
195 }
196
197 ConsumeParen(); // ignore the left paren loc for now
198
Richard Smithb12bf692011-10-17 21:20:17 +0000199 IdentifierInfo *ParmName = 0;
200 SourceLocation ParmLoc;
201 bool BuiltinType = false;
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000202
Richard Smithb12bf692011-10-17 21:20:17 +0000203 switch (Tok.getKind()) {
204 case tok::kw_char:
205 case tok::kw_wchar_t:
206 case tok::kw_char16_t:
207 case tok::kw_char32_t:
208 case tok::kw_bool:
209 case tok::kw_short:
210 case tok::kw_int:
211 case tok::kw_long:
212 case tok::kw___int64:
Richard Smithf016bbc2012-04-04 06:24:32 +0000213 case tok::kw___int128:
Richard Smithb12bf692011-10-17 21:20:17 +0000214 case tok::kw_signed:
215 case tok::kw_unsigned:
216 case tok::kw_float:
217 case tok::kw_double:
218 case tok::kw_void:
219 case tok::kw_typeof:
220 // __attribute__(( vec_type_hint(char) ))
221 // FIXME: Don't just discard the builtin type token.
222 ConsumeToken();
223 BuiltinType = true;
224 break;
225
226 case tok::identifier:
227 ParmName = Tok.getIdentifierInfo();
228 ParmLoc = ConsumeToken();
229 break;
230
231 default:
232 break;
233 }
234
235 ExprVector ArgExprs(Actions);
236
237 if (!BuiltinType &&
238 (ParmLoc.isValid() ? Tok.is(tok::comma) : Tok.isNot(tok::r_paren))) {
239 // Eat the comma.
240 if (ParmLoc.isValid())
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000241 ConsumeToken();
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000242
Richard Smithb12bf692011-10-17 21:20:17 +0000243 // Parse the non-empty comma-separated list of expressions.
244 while (1) {
245 ExprResult ArgExpr(ParseAssignmentExpression());
246 if (ArgExpr.isInvalid()) {
247 SkipUntil(tok::r_paren);
248 return;
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000249 }
Richard Smithb12bf692011-10-17 21:20:17 +0000250 ArgExprs.push_back(ArgExpr.release());
251 if (Tok.isNot(tok::comma))
252 break;
253 ConsumeToken(); // Eat the comma, move to the next argument
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000254 }
Richard Smithb12bf692011-10-17 21:20:17 +0000255 }
Fariborz Jahanian6b708652011-10-18 17:11:10 +0000256 else if (Tok.is(tok::less) && AttrName->isStr("iboutletcollection")) {
257 if (!ExpectAndConsume(tok::less, diag::err_expected_less_after, "<",
258 tok::greater)) {
Fariborz Jahanian7f733022011-10-18 23:13:50 +0000259 while (Tok.is(tok::identifier)) {
260 ConsumeToken();
261 if (Tok.is(tok::greater))
262 break;
263 if (Tok.is(tok::comma)) {
264 ConsumeToken();
265 continue;
266 }
267 }
268 if (Tok.isNot(tok::greater))
269 Diag(Tok, diag::err_iboutletcollection_with_protocol);
Fariborz Jahanian6b708652011-10-18 17:11:10 +0000270 SkipUntil(tok::r_paren, false, true); // skip until ')'
271 }
272 }
Richard Smithb12bf692011-10-17 21:20:17 +0000273
274 SourceLocation RParen = Tok.getLocation();
275 if (!ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) {
276 AttributeList *attr =
Argyrios Kyrtzidis635a9b42011-09-13 16:05:53 +0000277 Attrs.addNew(AttrName, SourceRange(AttrNameLoc, RParen), 0, AttrNameLoc,
Alexis Hunta0e54d42012-06-18 16:13:52 +0000278 ParmName, ParmLoc, ArgExprs.take(), ArgExprs.size(),
279 AttributeList::AS_GNU);
Alexis Hunt3bc72c12012-06-19 23:57:03 +0000280 if (BuiltinType && attr->getKind() == AttributeList::AT_IBOutletCollection)
Richard Smithb12bf692011-10-17 21:20:17 +0000281 Diag(Tok, diag::err_iboutletcollection_builtintype);
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000282 }
283}
284
Chad Rosierc1183952012-06-26 22:30:43 +0000285/// \brief Parses a single argument for a declspec, including the
Aaron Ballman478faed2012-06-19 22:09:27 +0000286/// surrounding parens.
Chad Rosierc1183952012-06-26 22:30:43 +0000287void Parser::ParseMicrosoftDeclSpecWithSingleArg(IdentifierInfo *AttrName,
Aaron Ballman478faed2012-06-19 22:09:27 +0000288 SourceLocation AttrNameLoc,
289 ParsedAttributes &Attrs)
290{
291 BalancedDelimiterTracker T(*this, tok::l_paren);
Chad Rosierc1183952012-06-26 22:30:43 +0000292 if (T.expectAndConsume(diag::err_expected_lparen_after,
Aaron Ballman478faed2012-06-19 22:09:27 +0000293 AttrName->getNameStart(), tok::r_paren))
294 return;
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000295
Aaron Ballman478faed2012-06-19 22:09:27 +0000296 ExprResult ArgExpr(ParseConstantExpression());
297 if (ArgExpr.isInvalid()) {
298 T.skipToEnd();
299 return;
300 }
301 Expr *ExprList = ArgExpr.take();
Chad Rosierc1183952012-06-26 22:30:43 +0000302 Attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, SourceLocation(),
Aaron Ballman478faed2012-06-19 22:09:27 +0000303 &ExprList, 1, AttributeList::AS_Declspec);
304
305 T.consumeClose();
306}
307
Chad Rosierc1183952012-06-26 22:30:43 +0000308/// \brief Determines whether a declspec is a "simple" one requiring no
Aaron Ballman478faed2012-06-19 22:09:27 +0000309/// arguments.
310bool Parser::IsSimpleMicrosoftDeclSpec(IdentifierInfo *Ident) {
311 return llvm::StringSwitch<bool>(Ident->getName())
312 .Case("dllimport", true)
313 .Case("dllexport", true)
314 .Case("noreturn", true)
315 .Case("nothrow", true)
316 .Case("noinline", true)
317 .Case("naked", true)
318 .Case("appdomain", true)
319 .Case("process", true)
320 .Case("jitintrinsic", true)
321 .Case("noalias", true)
322 .Case("restrict", true)
323 .Case("novtable", true)
324 .Case("selectany", true)
325 .Case("thread", true)
326 .Default(false);
327}
328
Chad Rosierc1183952012-06-26 22:30:43 +0000329/// \brief Attempts to parse a declspec which is not simple (one that takes
Aaron Ballman478faed2012-06-19 22:09:27 +0000330/// parameters). Will return false if we properly handled the declspec, or
331/// true if it is an unknown declspec.
Chad Rosierc1183952012-06-26 22:30:43 +0000332void Parser::ParseComplexMicrosoftDeclSpec(IdentifierInfo *Ident,
Aaron Ballman478faed2012-06-19 22:09:27 +0000333 SourceLocation Loc,
334 ParsedAttributes &Attrs) {
335 // Try to handle the easy case first -- these declspecs all take a single
336 // parameter as their argument.
337 if (llvm::StringSwitch<bool>(Ident->getName())
338 .Case("uuid", true)
339 .Case("align", true)
340 .Case("allocate", true)
341 .Default(false)) {
342 ParseMicrosoftDeclSpecWithSingleArg(Ident, Loc, Attrs);
343 } else if (Ident->getName() == "deprecated") {
Chad Rosierc1183952012-06-26 22:30:43 +0000344 // The deprecated declspec has an optional single argument, so we will
345 // check for a l-paren to decide whether we should parse an argument or
Aaron Ballman478faed2012-06-19 22:09:27 +0000346 // not.
347 if (Tok.getKind() == tok::l_paren)
348 ParseMicrosoftDeclSpecWithSingleArg(Ident, Loc, Attrs);
349 else
Chad Rosierc1183952012-06-26 22:30:43 +0000350 Attrs.addNew(Ident, Loc, 0, Loc, 0, SourceLocation(), 0, 0,
Aaron Ballman478faed2012-06-19 22:09:27 +0000351 AttributeList::AS_Declspec);
352 } else if (Ident->getName() == "property") {
353 // The property declspec is more complex in that it can take one or two
Chad Rosierc1183952012-06-26 22:30:43 +0000354 // assignment expressions as a parameter, but the lhs of the assignment
Aaron Ballman478faed2012-06-19 22:09:27 +0000355 // must be named get or put.
356 //
Chad Rosierc1183952012-06-26 22:30:43 +0000357 // For right now, we will just skip to the closing right paren of the
Aaron Ballman478faed2012-06-19 22:09:27 +0000358 // property expression.
359 //
360 // FIXME: we should deal with __declspec(property) at some point because it
361 // is used in the platform SDK headers for the Parallel Patterns Library
362 // and ATL.
363 BalancedDelimiterTracker T(*this, tok::l_paren);
Chad Rosierc1183952012-06-26 22:30:43 +0000364 if (T.expectAndConsume(diag::err_expected_lparen_after,
Aaron Ballman478faed2012-06-19 22:09:27 +0000365 Ident->getNameStart(), tok::r_paren))
366 return;
367 T.skipToEnd();
368 } else {
369 // We don't recognize this as a valid declspec, but instead of creating the
370 // attribute and allowing sema to warn about it, we will warn here instead.
371 // This is because some attributes have multiple spellings, but we need to
372 // disallow that for declspecs (such as align vs aligned). If we made the
Chad Rosierc1183952012-06-26 22:30:43 +0000373 // attribute, we'd have to split the valid declspec spelling logic into
Aaron Ballman478faed2012-06-19 22:09:27 +0000374 // both locations.
375 Diag(Loc, diag::warn_ms_declspec_unknown) << Ident;
376
377 // If there's an open paren, we should eat the open and close parens under
378 // the assumption that this unknown declspec has parameters.
379 BalancedDelimiterTracker T(*this, tok::l_paren);
380 if (!T.consumeOpen())
381 T.skipToEnd();
382 }
383}
384
Eli Friedman06de2b52009-06-08 07:21:15 +0000385/// [MS] decl-specifier:
386/// __declspec ( extended-decl-modifier-seq )
387///
388/// [MS] extended-decl-modifier-seq:
389/// extended-decl-modifier[opt]
390/// extended-decl-modifier extended-decl-modifier-seq
Aaron Ballman478faed2012-06-19 22:09:27 +0000391void Parser::ParseMicrosoftDeclSpec(ParsedAttributes &Attrs) {
Steve Naroff3a9b7e02008-12-24 20:59:21 +0000392 assert(Tok.is(tok::kw___declspec) && "Not a declspec!");
Eli Friedman06de2b52009-06-08 07:21:15 +0000393
Steve Naroff3a9b7e02008-12-24 20:59:21 +0000394 ConsumeToken();
Aaron Ballman478faed2012-06-19 22:09:27 +0000395 BalancedDelimiterTracker T(*this, tok::l_paren);
Chad Rosierc1183952012-06-26 22:30:43 +0000396 if (T.expectAndConsume(diag::err_expected_lparen_after, "__declspec",
Aaron Ballman478faed2012-06-19 22:09:27 +0000397 tok::r_paren))
John McCall53fa7142010-12-24 02:08:15 +0000398 return;
Jakob Stoklund Olesene1c0ae62012-06-19 21:48:43 +0000399
Chad Rosierc1183952012-06-26 22:30:43 +0000400 // An empty declspec is perfectly legal and should not warn. Additionally,
Aaron Ballman478faed2012-06-19 22:09:27 +0000401 // you can specify multiple attributes per declspec.
402 while (Tok.getKind() != tok::r_paren) {
403 // We expect either a well-known identifier or a generic string. Anything
404 // else is a malformed declspec.
405 bool IsString = Tok.getKind() == tok::string_literal ? true : false;
Chad Rosierc1183952012-06-26 22:30:43 +0000406 if (!IsString && Tok.getKind() != tok::identifier &&
Aaron Ballman478faed2012-06-19 22:09:27 +0000407 Tok.getKind() != tok::kw_restrict) {
408 Diag(Tok, diag::err_ms_declspec_type);
409 T.skipToEnd();
410 return;
Jakob Stoklund Olesene1c0ae62012-06-19 21:48:43 +0000411 }
Aaron Ballman478faed2012-06-19 22:09:27 +0000412
413 IdentifierInfo *AttrName;
414 SourceLocation AttrNameLoc;
415 if (IsString) {
416 SmallString<8> StrBuffer;
417 bool Invalid = false;
418 StringRef Str = PP.getSpelling(Tok, StrBuffer, &Invalid);
419 if (Invalid) {
420 T.skipToEnd();
421 return;
Jakob Stoklund Olesene1c0ae62012-06-19 21:48:43 +0000422 }
Aaron Ballman478faed2012-06-19 22:09:27 +0000423 AttrName = PP.getIdentifierInfo(Str);
424 AttrNameLoc = ConsumeStringToken();
Jakob Stoklund Olesene1c0ae62012-06-19 21:48:43 +0000425 } else {
Aaron Ballman478faed2012-06-19 22:09:27 +0000426 AttrName = Tok.getIdentifierInfo();
427 AttrNameLoc = ConsumeToken();
Jakob Stoklund Olesene1c0ae62012-06-19 21:48:43 +0000428 }
Chad Rosierc1183952012-06-26 22:30:43 +0000429
Aaron Ballman478faed2012-06-19 22:09:27 +0000430 if (IsString || IsSimpleMicrosoftDeclSpec(AttrName))
Chad Rosierc1183952012-06-26 22:30:43 +0000431 // If we have a generic string, we will allow it because there is no
432 // documented list of allowable string declspecs, but we know they exist
Aaron Ballman478faed2012-06-19 22:09:27 +0000433 // (for instance, SAL declspecs in older versions of MSVC).
434 //
Chad Rosierc1183952012-06-26 22:30:43 +0000435 // Alternatively, if the identifier is a simple one, then it requires no
Aaron Ballman478faed2012-06-19 22:09:27 +0000436 // arguments and can be turned into an attribute directly.
Chad Rosierc1183952012-06-26 22:30:43 +0000437 Attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, SourceLocation(),
Aaron Ballman478faed2012-06-19 22:09:27 +0000438 0, 0, AttributeList::AS_Declspec);
439 else
440 ParseComplexMicrosoftDeclSpec(AttrName, AttrNameLoc, Attrs);
Jakob Stoklund Olesene1c0ae62012-06-19 21:48:43 +0000441 }
Aaron Ballman478faed2012-06-19 22:09:27 +0000442 T.consumeClose();
Eli Friedman53339e02009-06-08 23:27:34 +0000443}
444
John McCall53fa7142010-12-24 02:08:15 +0000445void Parser::ParseMicrosoftTypeAttributes(ParsedAttributes &attrs) {
Eli Friedman53339e02009-06-08 23:27:34 +0000446 // Treat these like attributes
Eli Friedman53339e02009-06-08 23:27:34 +0000447 while (Tok.is(tok::kw___fastcall) || Tok.is(tok::kw___stdcall) ||
Douglas Gregora941dca2010-05-18 16:57:00 +0000448 Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___cdecl) ||
Francois Pichet17ed0202011-08-18 09:59:55 +0000449 Tok.is(tok::kw___ptr64) || Tok.is(tok::kw___w64) ||
Francois Pichetf2fb4112011-08-25 00:36:46 +0000450 Tok.is(tok::kw___ptr32) ||
Francois Pichet17ed0202011-08-18 09:59:55 +0000451 Tok.is(tok::kw___unaligned)) {
Eli Friedman53339e02009-06-08 23:27:34 +0000452 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
453 SourceLocation AttrNameLoc = ConsumeToken();
John McCall084e83d2011-03-24 11:26:52 +0000454 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
Aaron Ballman478faed2012-06-19 22:09:27 +0000455 SourceLocation(), 0, 0, AttributeList::AS_MSTypespec);
Eli Friedman53339e02009-06-08 23:27:34 +0000456 }
Steve Naroff3a9b7e02008-12-24 20:59:21 +0000457}
458
John McCall53fa7142010-12-24 02:08:15 +0000459void Parser::ParseBorlandTypeAttributes(ParsedAttributes &attrs) {
Dawn Perchik335e16b2010-09-03 01:29:35 +0000460 // Treat these like attributes
461 while (Tok.is(tok::kw___pascal)) {
462 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
463 SourceLocation AttrNameLoc = ConsumeToken();
John McCall084e83d2011-03-24 11:26:52 +0000464 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
Aaron Ballman478faed2012-06-19 22:09:27 +0000465 SourceLocation(), 0, 0, AttributeList::AS_MSTypespec);
Dawn Perchik335e16b2010-09-03 01:29:35 +0000466 }
John McCall53fa7142010-12-24 02:08:15 +0000467}
468
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +0000469void Parser::ParseOpenCLAttributes(ParsedAttributes &attrs) {
470 // Treat these like attributes
471 while (Tok.is(tok::kw___kernel)) {
472 SourceLocation AttrNameLoc = ConsumeToken();
John McCall084e83d2011-03-24 11:26:52 +0000473 attrs.addNew(PP.getIdentifierInfo("opencl_kernel_function"),
474 AttrNameLoc, 0, AttrNameLoc, 0,
Alexis Hunta0e54d42012-06-18 16:13:52 +0000475 SourceLocation(), 0, 0, AttributeList::AS_GNU);
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +0000476 }
477}
478
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000479void Parser::ParseOpenCLQualifiers(DeclSpec &DS) {
480 SourceLocation Loc = Tok.getLocation();
481 switch(Tok.getKind()) {
482 // OpenCL qualifiers:
483 case tok::kw___private:
Chad Rosierc1183952012-06-26 22:30:43 +0000484 case tok::kw_private:
John McCall084e83d2011-03-24 11:26:52 +0000485 DS.getAttributes().addNewInteger(
Chad Rosierc1183952012-06-26 22:30:43 +0000486 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000487 PP.getIdentifierInfo("address_space"), Loc, 0);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000488 break;
Chad Rosierc1183952012-06-26 22:30:43 +0000489
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000490 case tok::kw___global:
John McCall084e83d2011-03-24 11:26:52 +0000491 DS.getAttributes().addNewInteger(
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000492 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000493 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_global);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000494 break;
Chad Rosierc1183952012-06-26 22:30:43 +0000495
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000496 case tok::kw___local:
John McCall084e83d2011-03-24 11:26:52 +0000497 DS.getAttributes().addNewInteger(
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000498 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000499 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_local);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000500 break;
Chad Rosierc1183952012-06-26 22:30:43 +0000501
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000502 case tok::kw___constant:
John McCall084e83d2011-03-24 11:26:52 +0000503 DS.getAttributes().addNewInteger(
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000504 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000505 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_constant);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000506 break;
Chad Rosierc1183952012-06-26 22:30:43 +0000507
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000508 case tok::kw___read_only:
John McCall084e83d2011-03-24 11:26:52 +0000509 DS.getAttributes().addNewInteger(
Chad Rosierc1183952012-06-26 22:30:43 +0000510 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000511 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_read_only);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000512 break;
Chad Rosierc1183952012-06-26 22:30:43 +0000513
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000514 case tok::kw___write_only:
John McCall084e83d2011-03-24 11:26:52 +0000515 DS.getAttributes().addNewInteger(
Chad Rosierc1183952012-06-26 22:30:43 +0000516 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000517 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_write_only);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000518 break;
Chad Rosierc1183952012-06-26 22:30:43 +0000519
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000520 case tok::kw___read_write:
John McCall084e83d2011-03-24 11:26:52 +0000521 DS.getAttributes().addNewInteger(
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000522 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000523 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_read_write);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000524 break;
525 default: break;
526 }
527}
528
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000529/// \brief Parse a version number.
530///
531/// version:
532/// simple-integer
533/// simple-integer ',' simple-integer
534/// simple-integer ',' simple-integer ',' simple-integer
535VersionTuple Parser::ParseVersionTuple(SourceRange &Range) {
536 Range = Tok.getLocation();
537
538 if (!Tok.is(tok::numeric_constant)) {
539 Diag(Tok, diag::err_expected_version);
540 SkipUntil(tok::comma, tok::r_paren, true, true, true);
541 return VersionTuple();
542 }
543
544 // Parse the major (and possibly minor and subminor) versions, which
545 // are stored in the numeric constant. We utilize a quirk of the
546 // lexer, which is that it handles something like 1.2.3 as a single
547 // numeric constant, rather than two separate tokens.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000548 SmallString<512> Buffer;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000549 Buffer.resize(Tok.getLength()+1);
550 const char *ThisTokBegin = &Buffer[0];
551
552 // Get the spelling of the token, which eliminates trigraphs, etc.
553 bool Invalid = false;
554 unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin, &Invalid);
555 if (Invalid)
556 return VersionTuple();
557
558 // Parse the major version.
559 unsigned AfterMajor = 0;
560 unsigned Major = 0;
561 while (AfterMajor < ActualLength && isdigit(ThisTokBegin[AfterMajor])) {
562 Major = Major * 10 + ThisTokBegin[AfterMajor] - '0';
563 ++AfterMajor;
564 }
565
566 if (AfterMajor == 0) {
567 Diag(Tok, diag::err_expected_version);
568 SkipUntil(tok::comma, tok::r_paren, true, true, true);
569 return VersionTuple();
570 }
571
572 if (AfterMajor == ActualLength) {
573 ConsumeToken();
574
575 // We only had a single version component.
576 if (Major == 0) {
577 Diag(Tok, diag::err_zero_version);
578 return VersionTuple();
579 }
580
581 return VersionTuple(Major);
582 }
583
584 if (ThisTokBegin[AfterMajor] != '.' || (AfterMajor + 1 == ActualLength)) {
585 Diag(Tok, diag::err_expected_version);
586 SkipUntil(tok::comma, tok::r_paren, true, true, true);
587 return VersionTuple();
588 }
589
590 // Parse the minor version.
591 unsigned AfterMinor = AfterMajor + 1;
592 unsigned Minor = 0;
593 while (AfterMinor < ActualLength && isdigit(ThisTokBegin[AfterMinor])) {
594 Minor = Minor * 10 + ThisTokBegin[AfterMinor] - '0';
595 ++AfterMinor;
596 }
597
598 if (AfterMinor == ActualLength) {
599 ConsumeToken();
Chad Rosierc1183952012-06-26 22:30:43 +0000600
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000601 // We had major.minor.
602 if (Major == 0 && Minor == 0) {
603 Diag(Tok, diag::err_zero_version);
604 return VersionTuple();
605 }
606
Chad Rosierc1183952012-06-26 22:30:43 +0000607 return VersionTuple(Major, Minor);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000608 }
609
610 // If what follows is not a '.', we have a problem.
611 if (ThisTokBegin[AfterMinor] != '.') {
612 Diag(Tok, diag::err_expected_version);
613 SkipUntil(tok::comma, tok::r_paren, true, true, true);
Chad Rosierc1183952012-06-26 22:30:43 +0000614 return VersionTuple();
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000615 }
616
617 // Parse the subminor version.
618 unsigned AfterSubminor = AfterMinor + 1;
619 unsigned Subminor = 0;
620 while (AfterSubminor < ActualLength && isdigit(ThisTokBegin[AfterSubminor])) {
621 Subminor = Subminor * 10 + ThisTokBegin[AfterSubminor] - '0';
622 ++AfterSubminor;
623 }
624
625 if (AfterSubminor != ActualLength) {
626 Diag(Tok, diag::err_expected_version);
627 SkipUntil(tok::comma, tok::r_paren, true, true, true);
628 return VersionTuple();
629 }
630 ConsumeToken();
631 return VersionTuple(Major, Minor, Subminor);
632}
633
634/// \brief Parse the contents of the "availability" attribute.
635///
636/// availability-attribute:
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000637/// 'availability' '(' platform ',' version-arg-list, opt-message')'
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000638///
639/// platform:
640/// identifier
641///
642/// version-arg-list:
643/// version-arg
644/// version-arg ',' version-arg-list
645///
646/// version-arg:
647/// 'introduced' '=' version
648/// 'deprecated' '=' version
Douglas Gregorfdd417f2012-03-11 04:53:21 +0000649/// 'obsoleted' = version
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000650/// 'unavailable'
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000651/// opt-message:
652/// 'message' '=' <string>
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000653void Parser::ParseAvailabilityAttribute(IdentifierInfo &Availability,
654 SourceLocation AvailabilityLoc,
655 ParsedAttributes &attrs,
656 SourceLocation *endLoc) {
657 SourceLocation PlatformLoc;
658 IdentifierInfo *Platform = 0;
659
660 enum { Introduced, Deprecated, Obsoleted, Unknown };
661 AvailabilityChange Changes[Unknown];
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000662 ExprResult MessageExpr;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000663
664 // Opening '('.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000665 BalancedDelimiterTracker T(*this, tok::l_paren);
666 if (T.consumeOpen()) {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000667 Diag(Tok, diag::err_expected_lparen);
668 return;
669 }
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000670
671 // Parse the platform name,
672 if (Tok.isNot(tok::identifier)) {
673 Diag(Tok, diag::err_availability_expected_platform);
674 SkipUntil(tok::r_paren);
675 return;
676 }
677 Platform = Tok.getIdentifierInfo();
678 PlatformLoc = ConsumeToken();
679
680 // Parse the ',' following the platform name.
681 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "", tok::r_paren))
682 return;
683
684 // If we haven't grabbed the pointers for the identifiers
685 // "introduced", "deprecated", and "obsoleted", do so now.
686 if (!Ident_introduced) {
687 Ident_introduced = PP.getIdentifierInfo("introduced");
688 Ident_deprecated = PP.getIdentifierInfo("deprecated");
689 Ident_obsoleted = PP.getIdentifierInfo("obsoleted");
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000690 Ident_unavailable = PP.getIdentifierInfo("unavailable");
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000691 Ident_message = PP.getIdentifierInfo("message");
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000692 }
693
694 // Parse the set of introductions/deprecations/removals.
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000695 SourceLocation UnavailableLoc;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000696 do {
697 if (Tok.isNot(tok::identifier)) {
698 Diag(Tok, diag::err_availability_expected_change);
699 SkipUntil(tok::r_paren);
700 return;
701 }
702 IdentifierInfo *Keyword = Tok.getIdentifierInfo();
703 SourceLocation KeywordLoc = ConsumeToken();
704
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000705 if (Keyword == Ident_unavailable) {
706 if (UnavailableLoc.isValid()) {
707 Diag(KeywordLoc, diag::err_availability_redundant)
708 << Keyword << SourceRange(UnavailableLoc);
Chad Rosierc1183952012-06-26 22:30:43 +0000709 }
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000710 UnavailableLoc = KeywordLoc;
711
712 if (Tok.isNot(tok::comma))
713 break;
714
715 ConsumeToken();
716 continue;
Chad Rosierc1183952012-06-26 22:30:43 +0000717 }
718
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000719 if (Tok.isNot(tok::equal)) {
720 Diag(Tok, diag::err_expected_equal_after)
721 << Keyword;
722 SkipUntil(tok::r_paren);
723 return;
724 }
725 ConsumeToken();
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000726 if (Keyword == Ident_message) {
727 if (!isTokenStringLiteral()) {
728 Diag(Tok, diag::err_expected_string_literal);
729 SkipUntil(tok::r_paren);
730 return;
731 }
732 MessageExpr = ParseStringLiteralExpression();
733 break;
734 }
Chad Rosierc1183952012-06-26 22:30:43 +0000735
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000736 SourceRange VersionRange;
737 VersionTuple Version = ParseVersionTuple(VersionRange);
Chad Rosierc1183952012-06-26 22:30:43 +0000738
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000739 if (Version.empty()) {
740 SkipUntil(tok::r_paren);
741 return;
742 }
743
744 unsigned Index;
745 if (Keyword == Ident_introduced)
746 Index = Introduced;
747 else if (Keyword == Ident_deprecated)
748 Index = Deprecated;
749 else if (Keyword == Ident_obsoleted)
750 Index = Obsoleted;
Chad Rosierc1183952012-06-26 22:30:43 +0000751 else
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000752 Index = Unknown;
753
754 if (Index < Unknown) {
755 if (!Changes[Index].KeywordLoc.isInvalid()) {
756 Diag(KeywordLoc, diag::err_availability_redundant)
Chad Rosierc1183952012-06-26 22:30:43 +0000757 << Keyword
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000758 << SourceRange(Changes[Index].KeywordLoc,
759 Changes[Index].VersionRange.getEnd());
760 }
761
762 Changes[Index].KeywordLoc = KeywordLoc;
763 Changes[Index].Version = Version;
764 Changes[Index].VersionRange = VersionRange;
765 } else {
766 Diag(KeywordLoc, diag::err_availability_unknown_change)
767 << Keyword << VersionRange;
768 }
769
770 if (Tok.isNot(tok::comma))
771 break;
772
773 ConsumeToken();
774 } while (true);
775
776 // Closing ')'.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000777 if (T.consumeClose())
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000778 return;
779
780 if (endLoc)
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000781 *endLoc = T.getCloseLocation();
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000782
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000783 // The 'unavailable' availability cannot be combined with any other
784 // availability changes. Make sure that hasn't happened.
785 if (UnavailableLoc.isValid()) {
786 bool Complained = false;
787 for (unsigned Index = Introduced; Index != Unknown; ++Index) {
788 if (Changes[Index].KeywordLoc.isValid()) {
789 if (!Complained) {
790 Diag(UnavailableLoc, diag::warn_availability_and_unavailable)
791 << SourceRange(Changes[Index].KeywordLoc,
792 Changes[Index].VersionRange.getEnd());
793 Complained = true;
794 }
795
796 // Clear out the availability.
797 Changes[Index] = AvailabilityChange();
798 }
799 }
800 }
801
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000802 // Record this attribute
Chad Rosierc1183952012-06-26 22:30:43 +0000803 attrs.addNew(&Availability,
804 SourceRange(AvailabilityLoc, T.getCloseLocation()),
Fariborz Jahanian586be882012-01-23 23:38:32 +0000805 0, AvailabilityLoc,
John McCall084e83d2011-03-24 11:26:52 +0000806 Platform, PlatformLoc,
807 Changes[Introduced],
808 Changes[Deprecated],
Chad Rosierc1183952012-06-26 22:30:43 +0000809 Changes[Obsoleted],
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000810 UnavailableLoc, MessageExpr.take(),
Alexis Hunta0e54d42012-06-18 16:13:52 +0000811 AttributeList::AS_GNU);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000812}
813
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000814
815// Late Parsed Attributes:
816// See other examples of late parsing in lib/Parse/ParseCXXInlineMethods
817
818void Parser::LateParsedDeclaration::ParseLexedAttributes() {}
819
820void Parser::LateParsedClass::ParseLexedAttributes() {
821 Self->ParseLexedAttributes(*Class);
822}
823
824void Parser::LateParsedAttribute::ParseLexedAttributes() {
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +0000825 Self->ParseLexedAttribute(*this, true, false);
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000826}
827
828/// Wrapper class which calls ParseLexedAttribute, after setting up the
829/// scope appropriately.
830void Parser::ParseLexedAttributes(ParsingClass &Class) {
831 // Deal with templates
832 // FIXME: Test cases to make sure this does the right thing for templates.
833 bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope;
834 ParseScope ClassTemplateScope(this, Scope::TemplateParamScope,
835 HasTemplateScope);
836 if (HasTemplateScope)
837 Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate);
838
Douglas Gregor3024f072012-04-16 07:05:22 +0000839 // Set or update the scope flags.
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000840 bool AlreadyHasClassScope = Class.TopLevelClass;
Douglas Gregor3024f072012-04-16 07:05:22 +0000841 unsigned ScopeFlags = Scope::ClassScope|Scope::DeclScope;
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000842 ParseScope ClassScope(this, ScopeFlags, !AlreadyHasClassScope);
843 ParseScopeFlags ClassScopeFlags(this, ScopeFlags, AlreadyHasClassScope);
844
DeLesley Hutchins6f860042012-04-06 15:10:17 +0000845 // Enter the scope of nested classes
846 if (!AlreadyHasClassScope)
847 Actions.ActOnStartDelayedMemberDeclarations(getCurScope(),
848 Class.TagOrTemplate);
Benjamin Kramer1d373c62012-05-17 12:01:52 +0000849 if (!Class.LateParsedDeclarations.empty()) {
Douglas Gregor3024f072012-04-16 07:05:22 +0000850 // Allow 'this' within late-parsed attributes.
Chad Rosierc1183952012-06-26 22:30:43 +0000851 Sema::CXXThisScopeRAII ThisScope(Actions, Class.TagOrTemplate,
Douglas Gregor3024f072012-04-16 07:05:22 +0000852 /*TypeQuals=*/0);
Chad Rosierc1183952012-06-26 22:30:43 +0000853
Douglas Gregor3024f072012-04-16 07:05:22 +0000854 for (unsigned i = 0, ni = Class.LateParsedDeclarations.size(); i < ni; ++i){
855 Class.LateParsedDeclarations[i]->ParseLexedAttributes();
856 }
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000857 }
Chad Rosierc1183952012-06-26 22:30:43 +0000858
DeLesley Hutchins6f860042012-04-06 15:10:17 +0000859 if (!AlreadyHasClassScope)
860 Actions.ActOnFinishDelayedMemberDeclarations(getCurScope(),
861 Class.TagOrTemplate);
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000862}
863
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +0000864
865/// \brief Parse all attributes in LAs, and attach them to Decl D.
866void Parser::ParseLexedAttributeList(LateParsedAttrList &LAs, Decl *D,
867 bool EnterScope, bool OnDefinition) {
868 for (unsigned i = 0, ni = LAs.size(); i < ni; ++i) {
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +0000869 LAs[i]->addDecl(D);
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +0000870 ParseLexedAttribute(*LAs[i], EnterScope, OnDefinition);
Benjamin Kramerbafc49a2012-04-14 12:44:47 +0000871 delete LAs[i];
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +0000872 }
873 LAs.clear();
874}
875
876
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000877/// \brief Finish parsing an attribute for which parsing was delayed.
878/// This will be called at the end of parsing a class declaration
879/// for each LateParsedAttribute. We consume the saved tokens and
Chad Rosierc1183952012-06-26 22:30:43 +0000880/// create an attribute with the arguments filled in. We add this
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000881/// to the Attribute list for the decl.
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +0000882void Parser::ParseLexedAttribute(LateParsedAttribute &LA,
883 bool EnterScope, bool OnDefinition) {
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000884 // Save the current token position.
885 SourceLocation OrigLoc = Tok.getLocation();
886
887 // Append the current token at the end of the new token stream so that it
888 // doesn't get lost.
889 LA.Toks.push_back(Tok);
890 PP.EnterTokenStream(LA.Toks.data(), LA.Toks.size(), true, false);
891 // Consume the previously pushed token.
892 ConsumeAnyToken();
893
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +0000894 if (OnDefinition && !IsThreadSafetyAttribute(LA.AttrName.getName())) {
895 Diag(Tok, diag::warn_attribute_on_function_definition)
896 << LA.AttrName.getName();
897 }
898
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000899 ParsedAttributes Attrs(AttrFactory);
900 SourceLocation endLoc;
901
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +0000902 if (LA.Decls.size() == 1) {
903 Decl *D = LA.Decls[0];
Caitlin Sadowski990d5712011-09-08 17:42:31 +0000904
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +0000905 // If the Decl is templatized, add template parameters to scope.
906 bool HasTemplateScope = EnterScope && D->isTemplateDecl();
907 ParseScope TempScope(this, Scope::TemplateParamScope, HasTemplateScope);
908 if (HasTemplateScope)
909 Actions.ActOnReenterTemplateScope(Actions.CurScope, D);
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000910
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +0000911 // If the Decl is on a function, add function parameters to the scope.
912 bool HasFunctionScope = EnterScope && D->isFunctionOrFunctionTemplate();
913 ParseScope FnScope(this, Scope::FnScope|Scope::DeclScope, HasFunctionScope);
914 if (HasFunctionScope)
915 Actions.ActOnReenterFunctionContext(Actions.CurScope, D);
916
917 ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, &endLoc);
918
919 if (HasFunctionScope) {
920 Actions.ActOnExitFunctionContext();
921 FnScope.Exit(); // Pop scope, and remove Decls from IdResolver
922 }
923 if (HasTemplateScope) {
924 TempScope.Exit();
925 }
DeLesley Hutchins71d61032012-03-02 22:29:50 +0000926 } else if (LA.Decls.size() > 0) {
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +0000927 // If there are multiple decls, then the decl cannot be within the
928 // function scope.
929 ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, &endLoc);
DeLesley Hutchins71d61032012-03-02 22:29:50 +0000930 } else {
931 Diag(Tok, diag::warn_attribute_no_decl) << LA.AttrName.getName();
Caitlin Sadowski990d5712011-09-08 17:42:31 +0000932 }
933
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +0000934 for (unsigned i = 0, ni = LA.Decls.size(); i < ni; ++i) {
935 Actions.ActOnFinishDelayedAttribute(getCurScope(), LA.Decls[i], Attrs);
936 }
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000937
938 if (Tok.getLocation() != OrigLoc) {
939 // Due to a parsing error, we either went over the cached tokens or
940 // there are still cached tokens left, so we skip the leftover tokens.
941 // Since this is an uncommon situation that should be avoided, use the
942 // expensive isBeforeInTranslationUnit call.
943 if (PP.getSourceManager().isBeforeInTranslationUnit(Tok.getLocation(),
944 OrigLoc))
945 while (Tok.getLocation() != OrigLoc && Tok.isNot(tok::eof))
Douglas Gregor0cf55e92012-03-08 01:00:17 +0000946 ConsumeAnyToken();
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000947 }
948}
949
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000950/// \brief Wrapper around a case statement checking if AttrName is
951/// one of the thread safety attributes
952bool Parser::IsThreadSafetyAttribute(llvm::StringRef AttrName){
953 return llvm::StringSwitch<bool>(AttrName)
954 .Case("guarded_by", true)
955 .Case("guarded_var", true)
956 .Case("pt_guarded_by", true)
957 .Case("pt_guarded_var", true)
958 .Case("lockable", true)
959 .Case("scoped_lockable", true)
960 .Case("no_thread_safety_analysis", true)
961 .Case("acquired_after", true)
962 .Case("acquired_before", true)
963 .Case("exclusive_lock_function", true)
964 .Case("shared_lock_function", true)
965 .Case("exclusive_trylock_function", true)
966 .Case("shared_trylock_function", true)
967 .Case("unlock_function", true)
968 .Case("lock_returned", true)
969 .Case("locks_excluded", true)
970 .Case("exclusive_locks_required", true)
971 .Case("shared_locks_required", true)
972 .Default(false);
973}
974
975/// \brief Parse the contents of thread safety attributes. These
976/// should always be parsed as an expression list.
977///
978/// We need to special case the parsing due to the fact that if the first token
979/// of the first argument is an identifier, the main parse loop will store
980/// that token as a "parameter" and the rest of
981/// the arguments will be added to a list of "arguments". However,
982/// subsequent tokens in the first argument are lost. We instead parse each
983/// argument as an expression and add all arguments to the list of "arguments".
984/// In future, we will take advantage of this special case to also
985/// deal with some argument scoping issues here (for example, referring to a
986/// function parameter in the attribute on that function).
987void Parser::ParseThreadSafetyAttribute(IdentifierInfo &AttrName,
988 SourceLocation AttrNameLoc,
989 ParsedAttributes &Attrs,
990 SourceLocation *EndLoc) {
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000991 assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('");
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000992
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000993 BalancedDelimiterTracker T(*this, tok::l_paren);
994 T.consumeOpen();
Chad Rosierc1183952012-06-26 22:30:43 +0000995
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000996 ExprVector ArgExprs(Actions);
997 bool ArgExprsOk = true;
Chad Rosierc1183952012-06-26 22:30:43 +0000998
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000999 // now parse the list of expressions
DeLesley Hutchins36f5d852011-12-14 19:36:06 +00001000 while (Tok.isNot(tok::r_paren)) {
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001001 ExprResult ArgExpr(ParseAssignmentExpression());
1002 if (ArgExpr.isInvalid()) {
1003 ArgExprsOk = false;
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001004 T.consumeClose();
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001005 break;
1006 } else {
1007 ArgExprs.push_back(ArgExpr.release());
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +00001008 }
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001009 if (Tok.isNot(tok::comma))
1010 break;
1011 ConsumeToken(); // Eat the comma, move to the next argument
1012 }
1013 // Match the ')'.
DeLesley Hutchins30398dd2012-01-20 22:50:54 +00001014 if (ArgExprsOk && !T.consumeClose()) {
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001015 Attrs.addNew(&AttrName, AttrNameLoc, 0, AttrNameLoc, 0, SourceLocation(),
Alexis Hunta0e54d42012-06-18 16:13:52 +00001016 ArgExprs.take(), ArgExprs.size(), AttributeList::AS_GNU);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +00001017 }
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001018 if (EndLoc)
1019 *EndLoc = T.getCloseLocation();
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +00001020}
1021
Richard Smith7bdcc4a2012-04-10 01:32:12 +00001022/// DiagnoseProhibitedCXX11Attribute - We have found the opening square brackets
1023/// of a C++11 attribute-specifier in a location where an attribute is not
1024/// permitted. By C++11 [dcl.attr.grammar]p6, this is ill-formed. Diagnose this
1025/// situation.
1026///
1027/// \return \c true if we skipped an attribute-like chunk of tokens, \c false if
1028/// this doesn't appear to actually be an attribute-specifier, and the caller
1029/// should try to parse it.
1030bool Parser::DiagnoseProhibitedCXX11Attribute() {
1031 assert(Tok.is(tok::l_square) && NextToken().is(tok::l_square));
1032
1033 switch (isCXX11AttributeSpecifier(/*Disambiguate*/true)) {
1034 case CAK_NotAttributeSpecifier:
1035 // No diagnostic: we're in Obj-C++11 and this is not actually an attribute.
1036 return false;
1037
1038 case CAK_InvalidAttributeSpecifier:
1039 Diag(Tok.getLocation(), diag::err_l_square_l_square_not_attribute);
1040 return false;
1041
1042 case CAK_AttributeSpecifier:
1043 // Parse and discard the attributes.
1044 SourceLocation BeginLoc = ConsumeBracket();
1045 ConsumeBracket();
1046 SkipUntil(tok::r_square, /*StopAtSemi*/ false);
1047 assert(Tok.is(tok::r_square) && "isCXX11AttributeSpecifier lied");
1048 SourceLocation EndLoc = ConsumeBracket();
1049 Diag(BeginLoc, diag::err_attributes_not_allowed)
1050 << SourceRange(BeginLoc, EndLoc);
1051 return true;
1052 }
Chandler Carruthd8f7d382012-04-10 16:03:08 +00001053 llvm_unreachable("All cases handled above.");
Richard Smith7bdcc4a2012-04-10 01:32:12 +00001054}
1055
John McCall53fa7142010-12-24 02:08:15 +00001056void Parser::DiagnoseProhibitedAttributes(ParsedAttributesWithRange &attrs) {
1057 Diag(attrs.Range.getBegin(), diag::err_attributes_not_allowed)
1058 << attrs.Range;
Dawn Perchik335e16b2010-09-03 01:29:35 +00001059}
1060
Chris Lattner53361ac2006-08-10 05:19:57 +00001061/// ParseDeclaration - Parse a full 'declaration', which consists of
1062/// declaration-specifiers, some number of declarators, and a semicolon.
Chris Lattner49836b42009-04-02 04:16:50 +00001063/// 'Context' should be a Declarator::TheContext value. This returns the
1064/// location of the semicolon in DeclEnd.
Chris Lattnera5235172007-08-25 06:57:03 +00001065///
1066/// declaration: [C99 6.7]
1067/// block-declaration ->
1068/// simple-declaration
1069/// others [FIXME]
Douglas Gregoreb31f392008-12-01 23:54:00 +00001070/// [C++] template-declaration
Chris Lattnera5235172007-08-25 06:57:03 +00001071/// [C++] namespace-definition
Douglas Gregord7c4d982008-12-30 03:27:21 +00001072/// [C++] using-directive
Douglas Gregor77b50e12009-06-22 23:06:13 +00001073/// [C++] using-declaration
Richard Smithc202b282012-04-14 00:33:13 +00001074/// [C++11/C11] static_assert-declaration
Chris Lattnera5235172007-08-25 06:57:03 +00001075/// others... [FIXME]
1076///
Fariborz Jahanian1db5c942010-09-28 20:42:35 +00001077Parser::DeclGroupPtrTy Parser::ParseDeclaration(StmtVector &Stmts,
1078 unsigned Context,
Alexis Hunt96d5c762009-11-21 08:43:09 +00001079 SourceLocation &DeclEnd,
John McCall53fa7142010-12-24 02:08:15 +00001080 ParsedAttributesWithRange &attrs) {
Argyrios Kyrtzidis355094e2010-06-17 10:52:18 +00001081 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Fariborz Jahanian59b75282011-08-30 17:10:52 +00001082 // Must temporarily exit the objective-c container scope for
1083 // parsing c none objective-c decls.
1084 ObjCDeclContextSwitch ObjCDC(*this);
Chad Rosierc1183952012-06-26 22:30:43 +00001085
John McCall48871652010-08-21 09:40:31 +00001086 Decl *SingleDecl = 0;
Richard Smithcd1c0552011-07-01 19:46:12 +00001087 Decl *OwnedType = 0;
Chris Lattnera5235172007-08-25 06:57:03 +00001088 switch (Tok.getKind()) {
Douglas Gregoreb31f392008-12-01 23:54:00 +00001089 case tok::kw_template:
Douglas Gregor23996282009-05-12 21:31:51 +00001090 case tok::kw_export:
John McCall53fa7142010-12-24 02:08:15 +00001091 ProhibitAttributes(attrs);
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001092 SingleDecl = ParseDeclarationStartingWithTemplate(Context, DeclEnd);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001093 break;
Sebastian Redl67667942010-08-27 23:12:46 +00001094 case tok::kw_inline:
Sebastian Redl5a5f2c72010-08-31 00:36:45 +00001095 // Could be the start of an inline namespace. Allowed as an ext in C++03.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001096 if (getLangOpts().CPlusPlus && NextToken().is(tok::kw_namespace)) {
John McCall53fa7142010-12-24 02:08:15 +00001097 ProhibitAttributes(attrs);
Sebastian Redl67667942010-08-27 23:12:46 +00001098 SourceLocation InlineLoc = ConsumeToken();
1099 SingleDecl = ParseNamespace(Context, DeclEnd, InlineLoc);
1100 break;
1101 }
Chad Rosierc1183952012-06-26 22:30:43 +00001102 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs,
Fariborz Jahanian1db5c942010-09-28 20:42:35 +00001103 true);
Chris Lattnera5235172007-08-25 06:57:03 +00001104 case tok::kw_namespace:
John McCall53fa7142010-12-24 02:08:15 +00001105 ProhibitAttributes(attrs);
Chris Lattner49836b42009-04-02 04:16:50 +00001106 SingleDecl = ParseNamespace(Context, DeclEnd);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001107 break;
Douglas Gregord7c4d982008-12-30 03:27:21 +00001108 case tok::kw_using:
John McCall9b72f892010-11-10 02:40:36 +00001109 SingleDecl = ParseUsingDirectiveOrDeclaration(Context, ParsedTemplateInfo(),
Richard Smithcd1c0552011-07-01 19:46:12 +00001110 DeclEnd, attrs, &OwnedType);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001111 break;
Anders Carlssonf24fcff62009-03-11 16:27:10 +00001112 case tok::kw_static_assert:
Peter Collingbourne3d9cbdc2011-04-15 00:35:57 +00001113 case tok::kw__Static_assert:
John McCall53fa7142010-12-24 02:08:15 +00001114 ProhibitAttributes(attrs);
Chris Lattner49836b42009-04-02 04:16:50 +00001115 SingleDecl = ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001116 break;
Chris Lattnera5235172007-08-25 06:57:03 +00001117 default:
John McCall53fa7142010-12-24 02:08:15 +00001118 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs, true);
Chris Lattnera5235172007-08-25 06:57:03 +00001119 }
Chad Rosierc1183952012-06-26 22:30:43 +00001120
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001121 // This routine returns a DeclGroup, if the thing we parsed only contains a
Richard Smithcd1c0552011-07-01 19:46:12 +00001122 // single decl, convert it now. Alias declarations can also declare a type;
1123 // include that too if it is present.
1124 return Actions.ConvertDeclToDeclGroup(SingleDecl, OwnedType);
Chris Lattnera5235172007-08-25 06:57:03 +00001125}
1126
1127/// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl]
1128/// declaration-specifiers init-declarator-list[opt] ';'
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001129/// [C++11] attribute-specifier-seq decl-specifier-seq[opt]
1130/// init-declarator-list ';'
Chris Lattnera5235172007-08-25 06:57:03 +00001131///[C90/C++]init-declarator-list ';' [TODO]
1132/// [OMP] threadprivate-directive [TODO]
Chris Lattner32dc41c2009-03-29 17:27:48 +00001133///
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001134/// for-range-declaration: [C++11 6.5p1: stmt.ranged]
Richard Smith02e85f32011-04-14 22:09:26 +00001135/// attribute-specifier-seq[opt] type-specifier-seq declarator
1136///
Chris Lattner32dc41c2009-03-29 17:27:48 +00001137/// If RequireSemi is false, this does not check for a ';' at the end of the
Chris Lattner005fc1b2010-04-05 18:18:31 +00001138/// declaration. If it is true, it checks for and eats it.
Richard Smith02e85f32011-04-14 22:09:26 +00001139///
1140/// If FRI is non-null, we might be parsing a for-range-declaration instead
1141/// of a simple-declaration. If we find that we are, we also parse the
1142/// for-range-initializer, and place it here.
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001143Parser::DeclGroupPtrTy
1144Parser::ParseSimpleDeclaration(StmtVector &Stmts, unsigned Context,
1145 SourceLocation &DeclEnd,
1146 ParsedAttributesWithRange &attrs,
1147 bool RequireSemi, ForRangeInit *FRI) {
Chris Lattner53361ac2006-08-10 05:19:57 +00001148 // Parse the common declaration-specifiers piece.
John McCall28a6aea2009-11-04 02:18:39 +00001149 ParsingDeclSpec DS(*this);
John McCall53fa7142010-12-24 02:08:15 +00001150 DS.takeAttributesFrom(attrs);
Douglas Gregor0e7dde52011-04-24 05:37:28 +00001151
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001152 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS_none,
Richard Smith30482bc2011-02-20 03:19:35 +00001153 getDeclSpecContextFromDeclaratorContext(Context));
Abramo Bagnara1cd83682012-01-07 10:52:36 +00001154
Chris Lattner0e894622006-08-13 19:58:17 +00001155 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
1156 // declaration-specifiers init-declarator-list[opt] ';'
Chris Lattner76c72282007-10-09 17:33:22 +00001157 if (Tok.is(tok::semi)) {
Argyrios Kyrtzidisfbb2bb52012-05-16 23:49:15 +00001158 DeclEnd = Tok.getLocation();
Chris Lattner005fc1b2010-04-05 18:18:31 +00001159 if (RequireSemi) ConsumeToken();
John McCall48871652010-08-21 09:40:31 +00001160 Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none,
Douglas Gregor0e7dde52011-04-24 05:37:28 +00001161 DS);
John McCall28a6aea2009-11-04 02:18:39 +00001162 DS.complete(TheDecl);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001163 return Actions.ConvertDeclToDeclGroup(TheDecl);
Chris Lattner0e894622006-08-13 19:58:17 +00001164 }
Chad Rosierc1183952012-06-26 22:30:43 +00001165
1166 return ParseDeclGroup(DS, Context, /*FunctionDefs=*/ false, &DeclEnd, FRI);
John McCalld5a36322009-11-03 19:26:08 +00001167}
Mike Stump11289f42009-09-09 15:08:12 +00001168
Richard Smith09f76ee2011-10-19 21:33:05 +00001169/// Returns true if this might be the start of a declarator, or a common typo
1170/// for a declarator.
1171bool Parser::MightBeDeclarator(unsigned Context) {
1172 switch (Tok.getKind()) {
1173 case tok::annot_cxxscope:
1174 case tok::annot_template_id:
1175 case tok::caret:
1176 case tok::code_completion:
1177 case tok::coloncolon:
1178 case tok::ellipsis:
1179 case tok::kw___attribute:
1180 case tok::kw_operator:
1181 case tok::l_paren:
1182 case tok::star:
1183 return true;
1184
1185 case tok::amp:
1186 case tok::ampamp:
David Blaikiebbafb8a2012-03-11 07:00:24 +00001187 return getLangOpts().CPlusPlus;
Richard Smith09f76ee2011-10-19 21:33:05 +00001188
Richard Smithc8a79032012-01-09 22:31:44 +00001189 case tok::l_square: // Might be an attribute on an unnamed bit-field.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001190 return Context == Declarator::MemberContext && getLangOpts().CPlusPlus0x &&
Richard Smithc8a79032012-01-09 22:31:44 +00001191 NextToken().is(tok::l_square);
1192
1193 case tok::colon: // Might be a typo for '::' or an unnamed bit-field.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001194 return Context == Declarator::MemberContext || getLangOpts().CPlusPlus;
Richard Smithc8a79032012-01-09 22:31:44 +00001195
Richard Smith09f76ee2011-10-19 21:33:05 +00001196 case tok::identifier:
1197 switch (NextToken().getKind()) {
1198 case tok::code_completion:
1199 case tok::coloncolon:
1200 case tok::comma:
1201 case tok::equal:
1202 case tok::equalequal: // Might be a typo for '='.
1203 case tok::kw_alignas:
1204 case tok::kw_asm:
1205 case tok::kw___attribute:
1206 case tok::l_brace:
1207 case tok::l_paren:
1208 case tok::l_square:
1209 case tok::less:
1210 case tok::r_brace:
1211 case tok::r_paren:
1212 case tok::r_square:
1213 case tok::semi:
1214 return true;
1215
1216 case tok::colon:
1217 // At namespace scope, 'identifier:' is probably a typo for 'identifier::'
Richard Smithc8a79032012-01-09 22:31:44 +00001218 // and in block scope it's probably a label. Inside a class definition,
1219 // this is a bit-field.
1220 return Context == Declarator::MemberContext ||
David Blaikiebbafb8a2012-03-11 07:00:24 +00001221 (getLangOpts().CPlusPlus && Context == Declarator::FileContext);
Richard Smithc8a79032012-01-09 22:31:44 +00001222
1223 case tok::identifier: // Possible virt-specifier.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001224 return getLangOpts().CPlusPlus0x && isCXX0XVirtSpecifier(NextToken());
Richard Smith09f76ee2011-10-19 21:33:05 +00001225
1226 default:
1227 return false;
1228 }
1229
1230 default:
1231 return false;
1232 }
1233}
1234
Richard Smithb8caac82012-04-11 20:59:20 +00001235/// Skip until we reach something which seems like a sensible place to pick
1236/// up parsing after a malformed declaration. This will sometimes stop sooner
1237/// than SkipUntil(tok::r_brace) would, but will never stop later.
1238void Parser::SkipMalformedDecl() {
1239 while (true) {
1240 switch (Tok.getKind()) {
1241 case tok::l_brace:
1242 // Skip until matching }, then stop. We've probably skipped over
1243 // a malformed class or function definition or similar.
1244 ConsumeBrace();
1245 SkipUntil(tok::r_brace, /*StopAtSemi*/false);
1246 if (Tok.is(tok::comma) || Tok.is(tok::l_brace) || Tok.is(tok::kw_try)) {
1247 // This declaration isn't over yet. Keep skipping.
1248 continue;
1249 }
1250 if (Tok.is(tok::semi))
1251 ConsumeToken();
1252 return;
1253
1254 case tok::l_square:
1255 ConsumeBracket();
1256 SkipUntil(tok::r_square, /*StopAtSemi*/false);
1257 continue;
1258
1259 case tok::l_paren:
1260 ConsumeParen();
1261 SkipUntil(tok::r_paren, /*StopAtSemi*/false);
1262 continue;
1263
1264 case tok::r_brace:
1265 return;
1266
1267 case tok::semi:
1268 ConsumeToken();
1269 return;
1270
1271 case tok::kw_inline:
1272 // 'inline namespace' at the start of a line is almost certainly
Jordan Rose12e730c2012-07-09 16:54:53 +00001273 // a good place to pick back up parsing, except in an Objective-C
1274 // @interface context.
1275 if (Tok.isAtStartOfLine() && NextToken().is(tok::kw_namespace) &&
1276 (!ParsingInObjCContainer || CurParsedObjCImpl))
Richard Smithb8caac82012-04-11 20:59:20 +00001277 return;
1278 break;
1279
1280 case tok::kw_namespace:
1281 // 'namespace' at the start of a line is almost certainly a good
Jordan Rose12e730c2012-07-09 16:54:53 +00001282 // place to pick back up parsing, except in an Objective-C
1283 // @interface context.
1284 if (Tok.isAtStartOfLine() &&
1285 (!ParsingInObjCContainer || CurParsedObjCImpl))
1286 return;
1287 break;
1288
1289 case tok::at:
1290 // @end is very much like } in Objective-C contexts.
1291 if (NextToken().isObjCAtKeyword(tok::objc_end) &&
1292 ParsingInObjCContainer)
1293 return;
1294 break;
1295
1296 case tok::minus:
1297 case tok::plus:
1298 // - and + probably start new method declarations in Objective-C contexts.
1299 if (Tok.isAtStartOfLine() && ParsingInObjCContainer)
Richard Smithb8caac82012-04-11 20:59:20 +00001300 return;
1301 break;
1302
1303 case tok::eof:
1304 return;
1305
1306 default:
1307 break;
1308 }
1309
1310 ConsumeAnyToken();
1311 }
1312}
1313
John McCalld5a36322009-11-03 19:26:08 +00001314/// ParseDeclGroup - Having concluded that this is either a function
1315/// definition or a group of object declarations, actually parse the
1316/// result.
John McCall28a6aea2009-11-04 02:18:39 +00001317Parser::DeclGroupPtrTy Parser::ParseDeclGroup(ParsingDeclSpec &DS,
1318 unsigned Context,
John McCalld5a36322009-11-03 19:26:08 +00001319 bool AllowFunctionDefinitions,
Richard Smith02e85f32011-04-14 22:09:26 +00001320 SourceLocation *DeclEnd,
1321 ForRangeInit *FRI) {
John McCalld5a36322009-11-03 19:26:08 +00001322 // Parse the first declarator.
John McCall28a6aea2009-11-04 02:18:39 +00001323 ParsingDeclarator D(*this, DS, static_cast<Declarator::TheContext>(Context));
John McCalld5a36322009-11-03 19:26:08 +00001324 ParseDeclarator(D);
Chris Lattner32dc41c2009-03-29 17:27:48 +00001325
John McCalld5a36322009-11-03 19:26:08 +00001326 // Bail out if the first declarator didn't seem well-formed.
1327 if (!D.hasName() && !D.mayOmitIdentifier()) {
Richard Smithb8caac82012-04-11 20:59:20 +00001328 SkipMalformedDecl();
John McCalld5a36322009-11-03 19:26:08 +00001329 return DeclGroupPtrTy();
Chris Lattnerefb0f112009-03-29 17:18:04 +00001330 }
Mike Stump11289f42009-09-09 15:08:12 +00001331
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001332 // Save late-parsed attributes for now; they need to be parsed in the
1333 // appropriate function scope after the function Decl has been constructed.
1334 LateParsedAttrList LateParsedAttrs;
1335 if (D.isFunctionDeclarator())
1336 MaybeParseGNUAttributes(D, &LateParsedAttrs);
1337
Chris Lattnerdbb1e932010-07-11 22:24:20 +00001338 // Check to see if we have a function *definition* which must have a body.
1339 if (AllowFunctionDefinitions && D.isFunctionDeclarator() &&
1340 // Look at the next token to make sure that this isn't a function
1341 // declaration. We have to check this because __attribute__ might be the
1342 // start of a function definition in GCC-extended K&R C.
Fariborz Jahaniandeb144a2012-07-20 17:19:54 +00001343 // FIXME. Delayed parsing not done for c/c++ functions nested in namespace
Fariborz Jahanian8de79552012-07-05 19:34:20 +00001344 !isDeclarationAfterDeclarator() &&
1345 (!CurParsedObjCImpl || Tok.isNot(tok::l_brace) ||
Fariborz Jahaniandeb144a2012-07-20 17:19:54 +00001346 (getLangOpts().CPlusPlus &&
1347 (D.getCXXScopeSpec().isSet() ||
1348 !Actions.CurContext->isTranslationUnit())))) {
Chad Rosierc1183952012-06-26 22:30:43 +00001349
Chris Lattner13901342010-07-11 22:42:07 +00001350 if (isStartOfFunctionDefinition(D)) {
John McCalld5a36322009-11-03 19:26:08 +00001351 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
1352 Diag(Tok, diag::err_function_declared_typedef);
1353
1354 // Recover by treating the 'typedef' as spurious.
1355 DS.ClearStorageClassSpecs();
1356 }
1357
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001358 Decl *TheDecl =
1359 ParseFunctionDefinition(D, ParsedTemplateInfo(), &LateParsedAttrs);
John McCalld5a36322009-11-03 19:26:08 +00001360 return Actions.ConvertDeclToDeclGroup(TheDecl);
Chris Lattner13901342010-07-11 22:42:07 +00001361 }
Chad Rosierc1183952012-06-26 22:30:43 +00001362
Chris Lattner13901342010-07-11 22:42:07 +00001363 if (isDeclarationSpecifier()) {
1364 // If there is an invalid declaration specifier right after the function
1365 // prototype, then we must be in a missing semicolon case where this isn't
1366 // actually a body. Just fall through into the code that handles it as a
1367 // prototype, and let the top-level code handle the erroneous declspec
1368 // where it would otherwise expect a comma or semicolon.
John McCalld5a36322009-11-03 19:26:08 +00001369 } else {
1370 Diag(Tok, diag::err_expected_fn_body);
1371 SkipUntil(tok::semi);
1372 return DeclGroupPtrTy();
1373 }
1374 }
1375
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001376 if (ParseAsmAttributesAfterDeclarator(D))
Richard Smith02e85f32011-04-14 22:09:26 +00001377 return DeclGroupPtrTy();
1378
1379 // C++0x [stmt.iter]p1: Check if we have a for-range-declarator. If so, we
1380 // must parse and analyze the for-range-initializer before the declaration is
1381 // analyzed.
1382 if (FRI && Tok.is(tok::colon)) {
1383 FRI->ColonLoc = ConsumeToken();
Sebastian Redl3da34892011-06-05 12:23:16 +00001384 if (Tok.is(tok::l_brace))
1385 FRI->RangeExpr = ParseBraceInitializer();
1386 else
1387 FRI->RangeExpr = ParseExpression();
Richard Smith02e85f32011-04-14 22:09:26 +00001388 Decl *ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
1389 Actions.ActOnCXXForRangeDecl(ThisDecl);
1390 Actions.FinalizeDeclaration(ThisDecl);
John McCallcf6e0c82012-01-27 01:29:43 +00001391 D.complete(ThisDecl);
Richard Smith02e85f32011-04-14 22:09:26 +00001392 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS, &ThisDecl, 1);
1393 }
1394
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001395 SmallVector<Decl *, 8> DeclsInGroup;
Richard Smith02e85f32011-04-14 22:09:26 +00001396 Decl *FirstDecl = ParseDeclarationAfterDeclaratorAndAttributes(D);
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001397 if (LateParsedAttrs.size() > 0)
1398 ParseLexedAttributeList(LateParsedAttrs, FirstDecl, true, false);
John McCall28a6aea2009-11-04 02:18:39 +00001399 D.complete(FirstDecl);
John McCall48871652010-08-21 09:40:31 +00001400 if (FirstDecl)
John McCalld5a36322009-11-03 19:26:08 +00001401 DeclsInGroup.push_back(FirstDecl);
1402
Richard Smith09f76ee2011-10-19 21:33:05 +00001403 bool ExpectSemi = Context != Declarator::ForContext;
1404
Fariborz Jahanian577574a2012-07-02 23:37:09 +00001405 if (CurParsedObjCImpl && D.isFunctionDeclarator() &&
Fariborz Jahanian8a369a82012-07-03 22:54:28 +00001406 Tok.is(tok::l_brace)) {
Fariborz Jahanian577574a2012-07-02 23:37:09 +00001407 // Consume the tokens and store them for later parsing.
1408 StashAwayMethodOrFunctionBodyTokens(FirstDecl);
1409 CurParsedObjCImpl->HasCFunction = true;
1410 ExpectSemi = false;
1411 }
1412
John McCalld5a36322009-11-03 19:26:08 +00001413 // If we don't have a comma, it is either the end of the list (a ';') or an
1414 // error, bail out.
1415 while (Tok.is(tok::comma)) {
Richard Smith09f76ee2011-10-19 21:33:05 +00001416 SourceLocation CommaLoc = ConsumeToken();
1417
1418 if (Tok.isAtStartOfLine() && ExpectSemi && !MightBeDeclarator(Context)) {
1419 // This comma was followed by a line-break and something which can't be
1420 // the start of a declarator. The comma was probably a typo for a
1421 // semicolon.
1422 Diag(CommaLoc, diag::err_expected_semi_declaration)
1423 << FixItHint::CreateReplacement(CommaLoc, ";");
1424 ExpectSemi = false;
1425 break;
1426 }
John McCalld5a36322009-11-03 19:26:08 +00001427
1428 // Parse the next declarator.
1429 D.clear();
Richard Smith8d06f422012-01-12 23:53:29 +00001430 D.setCommaLoc(CommaLoc);
John McCalld5a36322009-11-03 19:26:08 +00001431
1432 // Accept attributes in an init-declarator. In the first declarator in a
1433 // declaration, these would be part of the declspec. In subsequent
1434 // declarators, they become part of the declarator itself, so that they
1435 // don't apply to declarators after *this* one. Examples:
1436 // short __attribute__((common)) var; -> declspec
1437 // short var __attribute__((common)); -> declarator
1438 // short x, __attribute__((common)) var; -> declarator
John McCall53fa7142010-12-24 02:08:15 +00001439 MaybeParseGNUAttributes(D);
John McCalld5a36322009-11-03 19:26:08 +00001440
1441 ParseDeclarator(D);
Fariborz Jahanian372030b2012-01-13 00:14:12 +00001442 if (!D.isInvalidType()) {
1443 Decl *ThisDecl = ParseDeclarationAfterDeclarator(D);
1444 D.complete(ThisDecl);
1445 if (ThisDecl)
Chad Rosierc1183952012-06-26 22:30:43 +00001446 DeclsInGroup.push_back(ThisDecl);
Fariborz Jahanian372030b2012-01-13 00:14:12 +00001447 }
John McCalld5a36322009-11-03 19:26:08 +00001448 }
1449
1450 if (DeclEnd)
1451 *DeclEnd = Tok.getLocation();
1452
Richard Smith09f76ee2011-10-19 21:33:05 +00001453 if (ExpectSemi &&
Chris Lattner02f1b612012-04-28 16:12:17 +00001454 ExpectAndConsumeSemi(Context == Declarator::FileContext
1455 ? diag::err_invalid_token_after_toplevel_declarator
1456 : diag::err_expected_semi_declaration)) {
Chris Lattner13901342010-07-11 22:42:07 +00001457 // Okay, there was no semicolon and one was expected. If we see a
1458 // declaration specifier, just assume it was missing and continue parsing.
1459 // Otherwise things are very confused and we skip to recover.
1460 if (!isDeclarationSpecifier()) {
1461 SkipUntil(tok::r_brace, true, true);
1462 if (Tok.is(tok::semi))
1463 ConsumeToken();
1464 }
John McCalld5a36322009-11-03 19:26:08 +00001465 }
1466
Douglas Gregor0be31a22010-07-02 17:43:08 +00001467 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS,
John McCalld5a36322009-11-03 19:26:08 +00001468 DeclsInGroup.data(),
1469 DeclsInGroup.size());
Chris Lattner53361ac2006-08-10 05:19:57 +00001470}
1471
Richard Smith02e85f32011-04-14 22:09:26 +00001472/// Parse an optional simple-asm-expr and attributes, and attach them to a
1473/// declarator. Returns true on an error.
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001474bool Parser::ParseAsmAttributesAfterDeclarator(Declarator &D) {
Richard Smith02e85f32011-04-14 22:09:26 +00001475 // If a simple-asm-expr is present, parse it.
1476 if (Tok.is(tok::kw_asm)) {
1477 SourceLocation Loc;
1478 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
1479 if (AsmLabel.isInvalid()) {
1480 SkipUntil(tok::semi, true, true);
1481 return true;
1482 }
1483
1484 D.setAsmLabel(AsmLabel.release());
1485 D.SetRangeEnd(Loc);
1486 }
1487
1488 MaybeParseGNUAttributes(D);
1489 return false;
1490}
1491
Douglas Gregor23996282009-05-12 21:31:51 +00001492/// \brief Parse 'declaration' after parsing 'declaration-specifiers
1493/// declarator'. This method parses the remainder of the declaration
1494/// (including any attributes or initializer, among other things) and
1495/// finalizes the declaration.
Chris Lattnerf0f3baa2006-08-14 00:15:20 +00001496///
Chris Lattnerf0f3baa2006-08-14 00:15:20 +00001497/// init-declarator: [C99 6.7]
1498/// declarator
1499/// declarator '=' initializer
Chris Lattner6d7e6342006-08-15 03:41:14 +00001500/// [GNU] declarator simple-asm-expr[opt] attributes[opt]
1501/// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00001502/// [C++] declarator initializer[opt]
1503///
1504/// [C++] initializer:
1505/// [C++] '=' initializer-clause
1506/// [C++] '(' expression-list ')'
Sebastian Redlf769df52009-03-24 22:27:57 +00001507/// [C++0x] '=' 'default' [TODO]
1508/// [C++0x] '=' 'delete'
Sebastian Redl3da34892011-06-05 12:23:16 +00001509/// [C++0x] braced-init-list
Sebastian Redlf769df52009-03-24 22:27:57 +00001510///
1511/// According to the standard grammar, =default and =delete are function
1512/// definitions, but that definitely doesn't fit with the parser here.
Chris Lattnerf0f3baa2006-08-14 00:15:20 +00001513///
John McCall48871652010-08-21 09:40:31 +00001514Decl *Parser::ParseDeclarationAfterDeclarator(Declarator &D,
Douglas Gregorb52fabb2009-06-23 23:11:28 +00001515 const ParsedTemplateInfo &TemplateInfo) {
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001516 if (ParseAsmAttributesAfterDeclarator(D))
Richard Smith02e85f32011-04-14 22:09:26 +00001517 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00001518
Richard Smith02e85f32011-04-14 22:09:26 +00001519 return ParseDeclarationAfterDeclaratorAndAttributes(D, TemplateInfo);
1520}
Mike Stump11289f42009-09-09 15:08:12 +00001521
Richard Smith02e85f32011-04-14 22:09:26 +00001522Decl *Parser::ParseDeclarationAfterDeclaratorAndAttributes(Declarator &D,
1523 const ParsedTemplateInfo &TemplateInfo) {
Douglas Gregor23996282009-05-12 21:31:51 +00001524 // Inform the current actions module that we just parsed this declarator.
John McCall48871652010-08-21 09:40:31 +00001525 Decl *ThisDecl = 0;
Douglas Gregor450f00842009-09-25 18:43:00 +00001526 switch (TemplateInfo.Kind) {
1527 case ParsedTemplateInfo::NonTemplate:
Douglas Gregor0be31a22010-07-02 17:43:08 +00001528 ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
Douglas Gregor450f00842009-09-25 18:43:00 +00001529 break;
Chad Rosierc1183952012-06-26 22:30:43 +00001530
Douglas Gregor450f00842009-09-25 18:43:00 +00001531 case ParsedTemplateInfo::Template:
1532 case ParsedTemplateInfo::ExplicitSpecialization:
Douglas Gregor0be31a22010-07-02 17:43:08 +00001533 ThisDecl = Actions.ActOnTemplateDeclarator(getCurScope(),
John McCallfaf5fb42010-08-26 23:41:50 +00001534 MultiTemplateParamsArg(Actions,
Douglas Gregorb52fabb2009-06-23 23:11:28 +00001535 TemplateInfo.TemplateParams->data(),
1536 TemplateInfo.TemplateParams->size()),
Douglas Gregor450f00842009-09-25 18:43:00 +00001537 D);
1538 break;
Chad Rosierc1183952012-06-26 22:30:43 +00001539
Douglas Gregor450f00842009-09-25 18:43:00 +00001540 case ParsedTemplateInfo::ExplicitInstantiation: {
Chad Rosierc1183952012-06-26 22:30:43 +00001541 DeclResult ThisRes
Douglas Gregor0be31a22010-07-02 17:43:08 +00001542 = Actions.ActOnExplicitInstantiation(getCurScope(),
Douglas Gregor450f00842009-09-25 18:43:00 +00001543 TemplateInfo.ExternLoc,
1544 TemplateInfo.TemplateLoc,
1545 D);
1546 if (ThisRes.isInvalid()) {
1547 SkipUntil(tok::semi, true, true);
John McCall48871652010-08-21 09:40:31 +00001548 return 0;
Douglas Gregor450f00842009-09-25 18:43:00 +00001549 }
Chad Rosierc1183952012-06-26 22:30:43 +00001550
Douglas Gregor450f00842009-09-25 18:43:00 +00001551 ThisDecl = ThisRes.get();
1552 break;
1553 }
1554 }
Mike Stump11289f42009-09-09 15:08:12 +00001555
Richard Smith30482bc2011-02-20 03:19:35 +00001556 bool TypeContainsAuto =
1557 D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto;
1558
Douglas Gregor23996282009-05-12 21:31:51 +00001559 // Parse declarator '=' initializer.
Richard Trieuc64d3232012-01-18 22:54:52 +00001560 // If a '==' or '+=' is found, suggest a fixit to '='.
Richard Trieu4972a6d2012-01-19 22:01:51 +00001561 if (isTokenEqualOrEqualTypo()) {
Douglas Gregor23996282009-05-12 21:31:51 +00001562 ConsumeToken();
Anders Carlsson991285e2010-09-24 21:25:25 +00001563 if (Tok.is(tok::kw_delete)) {
Alexis Hunt5a7fa252011-05-12 06:15:49 +00001564 if (D.isFunctionDeclarator())
1565 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
1566 << 1 /* delete */;
1567 else
1568 Diag(ConsumeToken(), diag::err_deleted_non_function);
Alexis Hunt5dafebc2011-05-06 01:42:00 +00001569 } else if (Tok.is(tok::kw_default)) {
Alexis Hunt5a7fa252011-05-12 06:15:49 +00001570 if (D.isFunctionDeclarator())
Sebastian Redl46afb552012-02-11 23:51:21 +00001571 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
1572 << 0 /* default */;
Alexis Hunt5a7fa252011-05-12 06:15:49 +00001573 else
1574 Diag(ConsumeToken(), diag::err_default_special_members);
Douglas Gregor23996282009-05-12 21:31:51 +00001575 } else {
David Blaikiebbafb8a2012-03-11 07:00:24 +00001576 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
John McCall1f4ee7b2009-12-19 09:28:58 +00001577 EnterScope(0);
Douglas Gregor0be31a22010-07-02 17:43:08 +00001578 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
John McCall1f4ee7b2009-12-19 09:28:58 +00001579 }
Argyrios Kyrtzidis3df19782009-06-17 22:50:06 +00001580
Douglas Gregor7aa6b222010-05-30 01:49:25 +00001581 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001582 Actions.CodeCompleteInitializer(getCurScope(), ThisDecl);
Peter Collingbourne6b4fdc22012-07-27 12:56:09 +00001583 Actions.FinalizeDeclaration(ThisDecl);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001584 cutOffParsing();
1585 return 0;
Douglas Gregor7aa6b222010-05-30 01:49:25 +00001586 }
Chad Rosierc1183952012-06-26 22:30:43 +00001587
John McCalldadc5752010-08-24 06:29:42 +00001588 ExprResult Init(ParseInitializer());
Argyrios Kyrtzidis3df19782009-06-17 22:50:06 +00001589
David Blaikiebbafb8a2012-03-11 07:00:24 +00001590 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001591 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
John McCall1f4ee7b2009-12-19 09:28:58 +00001592 ExitScope();
1593 }
Argyrios Kyrtzidis3df19782009-06-17 22:50:06 +00001594
Douglas Gregor23996282009-05-12 21:31:51 +00001595 if (Init.isInvalid()) {
Douglas Gregor604c3022010-03-01 18:27:54 +00001596 SkipUntil(tok::comma, true, true);
1597 Actions.ActOnInitializerError(ThisDecl);
1598 } else
Richard Smith30482bc2011-02-20 03:19:35 +00001599 Actions.AddInitializerToDecl(ThisDecl, Init.take(),
1600 /*DirectInit=*/false, TypeContainsAuto);
Douglas Gregor23996282009-05-12 21:31:51 +00001601 }
1602 } else if (Tok.is(tok::l_paren)) {
1603 // Parse C++ direct initializer: '(' expression-list ')'
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001604 BalancedDelimiterTracker T(*this, tok::l_paren);
1605 T.consumeOpen();
1606
Douglas Gregor23996282009-05-12 21:31:51 +00001607 ExprVector Exprs(Actions);
1608 CommaLocsTy CommaLocs;
1609
David Blaikiebbafb8a2012-03-11 07:00:24 +00001610 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor613bf102009-12-22 17:47:17 +00001611 EnterScope(0);
Douglas Gregor0be31a22010-07-02 17:43:08 +00001612 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregor613bf102009-12-22 17:47:17 +00001613 }
1614
Douglas Gregor23996282009-05-12 21:31:51 +00001615 if (ParseExpressionList(Exprs, CommaLocs)) {
1616 SkipUntil(tok::r_paren);
Douglas Gregor613bf102009-12-22 17:47:17 +00001617
David Blaikiebbafb8a2012-03-11 07:00:24 +00001618 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001619 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregor613bf102009-12-22 17:47:17 +00001620 ExitScope();
1621 }
Douglas Gregor23996282009-05-12 21:31:51 +00001622 } else {
1623 // Match the ')'.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001624 T.consumeClose();
Douglas Gregor23996282009-05-12 21:31:51 +00001625
1626 assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() &&
1627 "Unexpected number of commas!");
Douglas Gregor613bf102009-12-22 17:47:17 +00001628
David Blaikiebbafb8a2012-03-11 07:00:24 +00001629 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001630 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregor613bf102009-12-22 17:47:17 +00001631 ExitScope();
1632 }
1633
Sebastian Redla9351792012-02-11 23:51:47 +00001634 ExprResult Initializer = Actions.ActOnParenListExpr(T.getOpenLocation(),
1635 T.getCloseLocation(),
1636 move_arg(Exprs));
1637 Actions.AddInitializerToDecl(ThisDecl, Initializer.take(),
1638 /*DirectInit=*/true, TypeContainsAuto);
Douglas Gregor23996282009-05-12 21:31:51 +00001639 }
Fariborz Jahanian8a369a82012-07-03 22:54:28 +00001640 } else if (getLangOpts().CPlusPlus0x && Tok.is(tok::l_brace) &&
Fariborz Jahanian8be1ecd2012-07-03 23:22:13 +00001641 (!CurParsedObjCImpl || !D.isFunctionDeclarator())) {
Sebastian Redl3da34892011-06-05 12:23:16 +00001642 // Parse C++0x braced-init-list.
Richard Smith5d164bc2011-10-15 05:09:34 +00001643 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
1644
Sebastian Redl3da34892011-06-05 12:23:16 +00001645 if (D.getCXXScopeSpec().isSet()) {
1646 EnterScope(0);
1647 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
1648 }
1649
1650 ExprResult Init(ParseBraceInitializer());
1651
1652 if (D.getCXXScopeSpec().isSet()) {
1653 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
1654 ExitScope();
1655 }
1656
1657 if (Init.isInvalid()) {
1658 Actions.ActOnInitializerError(ThisDecl);
1659 } else
1660 Actions.AddInitializerToDecl(ThisDecl, Init.take(),
1661 /*DirectInit=*/true, TypeContainsAuto);
1662
Douglas Gregor23996282009-05-12 21:31:51 +00001663 } else {
Richard Smith30482bc2011-02-20 03:19:35 +00001664 Actions.ActOnUninitializedDecl(ThisDecl, TypeContainsAuto);
Douglas Gregor23996282009-05-12 21:31:51 +00001665 }
1666
Richard Smithb2bc2e62011-02-21 20:05:19 +00001667 Actions.FinalizeDeclaration(ThisDecl);
1668
Douglas Gregor23996282009-05-12 21:31:51 +00001669 return ThisDecl;
1670}
1671
Chris Lattner1890ac82006-08-13 01:16:23 +00001672/// ParseSpecifierQualifierList
1673/// specifier-qualifier-list:
1674/// type-specifier specifier-qualifier-list[opt]
1675/// type-qualifier specifier-qualifier-list[opt]
Chris Lattnere37e2332006-08-15 04:50:22 +00001676/// [GNU] attributes specifier-qualifier-list[opt]
Chris Lattner1890ac82006-08-13 01:16:23 +00001677///
Richard Smithc5b05522012-03-12 07:56:15 +00001678void Parser::ParseSpecifierQualifierList(DeclSpec &DS, AccessSpecifier AS,
1679 DeclSpecContext DSC) {
Chris Lattner1890ac82006-08-13 01:16:23 +00001680 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
1681 /// parse declaration-specifiers and complain about extra stuff.
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00001682 /// TODO: diagnose attribute-specifiers and alignment-specifiers.
Richard Smithc5b05522012-03-12 07:56:15 +00001683 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS, DSC);
Mike Stump11289f42009-09-09 15:08:12 +00001684
Chris Lattner1890ac82006-08-13 01:16:23 +00001685 // Validate declspec for type-name.
1686 unsigned Specs = DS.getParsedSpecifiers();
Richard Smith2f07ad52012-05-09 20:55:26 +00001687 if ((DSC == DSC_type_specifier || DSC == DSC_trailing) &&
1688 !DS.hasTypeSpecifier()) {
Richard Smithc5b05522012-03-12 07:56:15 +00001689 Diag(Tok, diag::err_expected_type);
1690 DS.SetTypeSpecError();
1691 } else if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers() &&
1692 !DS.hasAttributes()) {
Chris Lattner1890ac82006-08-13 01:16:23 +00001693 Diag(Tok, diag::err_typename_requires_specqual);
Richard Smithc5b05522012-03-12 07:56:15 +00001694 if (!DS.hasTypeSpecifier())
1695 DS.SetTypeSpecError();
1696 }
Mike Stump11289f42009-09-09 15:08:12 +00001697
Chris Lattner1b22eed2006-11-28 05:12:07 +00001698 // Issue diagnostic and remove storage class if present.
Chris Lattner1890ac82006-08-13 01:16:23 +00001699 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
Chris Lattner1b22eed2006-11-28 05:12:07 +00001700 if (DS.getStorageClassSpecLoc().isValid())
1701 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
1702 else
1703 Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass);
Chris Lattnera925dc62006-11-28 04:33:46 +00001704 DS.ClearStorageClassSpecs();
Chris Lattner1890ac82006-08-13 01:16:23 +00001705 }
Mike Stump11289f42009-09-09 15:08:12 +00001706
Chris Lattner1b22eed2006-11-28 05:12:07 +00001707 // Issue diagnostic and remove function specfier if present.
Chris Lattner1890ac82006-08-13 01:16:23 +00001708 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Douglas Gregor61956c42008-10-31 09:07:45 +00001709 if (DS.isInlineSpecified())
1710 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
1711 if (DS.isVirtualSpecified())
1712 Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec);
1713 if (DS.isExplicitSpecified())
1714 Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec);
Chris Lattnera925dc62006-11-28 04:33:46 +00001715 DS.ClearFunctionSpecs();
Chris Lattner1890ac82006-08-13 01:16:23 +00001716 }
Richard Smithc5b05522012-03-12 07:56:15 +00001717
1718 // Issue diagnostic and remove constexpr specfier if present.
1719 if (DS.isConstexprSpecified()) {
1720 Diag(DS.getConstexprSpecLoc(), diag::err_typename_invalid_constexpr);
1721 DS.ClearConstexprSpec();
1722 }
Chris Lattner1890ac82006-08-13 01:16:23 +00001723}
Chris Lattner53361ac2006-08-10 05:19:57 +00001724
Chris Lattner6cc055a2009-04-12 20:42:31 +00001725/// isValidAfterIdentifierInDeclaratorAfterDeclSpec - Return true if the
1726/// specified token is valid after the identifier in a declarator which
1727/// immediately follows the declspec. For example, these things are valid:
1728///
1729/// int x [ 4]; // direct-declarator
1730/// int x ( int y); // direct-declarator
1731/// int(int x ) // direct-declarator
1732/// int x ; // simple-declaration
1733/// int x = 17; // init-declarator-list
1734/// int x , y; // init-declarator-list
1735/// int x __asm__ ("foo"); // init-declarator-list
Chris Lattnera723ba92009-04-14 21:16:09 +00001736/// int x : 4; // struct-declarator
Chris Lattner2b988c12009-04-12 22:29:43 +00001737/// int x { 5}; // C++'0x unified initializers
Chris Lattner6cc055a2009-04-12 20:42:31 +00001738///
1739/// This is not, because 'x' does not immediately follow the declspec (though
1740/// ')' happens to be valid anyway).
1741/// int (x)
1742///
1743static bool isValidAfterIdentifierInDeclarator(const Token &T) {
1744 return T.is(tok::l_square) || T.is(tok::l_paren) || T.is(tok::r_paren) ||
1745 T.is(tok::semi) || T.is(tok::comma) || T.is(tok::equal) ||
Chris Lattnera723ba92009-04-14 21:16:09 +00001746 T.is(tok::kw_asm) || T.is(tok::l_brace) || T.is(tok::colon);
Chris Lattner6cc055a2009-04-12 20:42:31 +00001747}
1748
Chris Lattner20a0c612009-04-14 21:34:55 +00001749
1750/// ParseImplicitInt - This method is called when we have an non-typename
1751/// identifier in a declspec (which normally terminates the decl spec) when
1752/// the declspec has no type specifier. In this case, the declspec is either
1753/// malformed or is "implicit int" (in K&R and C89).
1754///
1755/// This method handles diagnosing this prettily and returns false if the
1756/// declspec is done being processed. If it recovers and thinks there may be
1757/// other pieces of declspec after it, it returns true.
1758///
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001759bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS,
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001760 const ParsedTemplateInfo &TemplateInfo,
Richard Smithc5b05522012-03-12 07:56:15 +00001761 AccessSpecifier AS, DeclSpecContext DSC) {
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001762 assert(Tok.is(tok::identifier) && "should have identifier");
Mike Stump11289f42009-09-09 15:08:12 +00001763
Chris Lattner20a0c612009-04-14 21:34:55 +00001764 SourceLocation Loc = Tok.getLocation();
1765 // If we see an identifier that is not a type name, we normally would
1766 // parse it as the identifer being declared. However, when a typename
1767 // is typo'd or the definition is not included, this will incorrectly
1768 // parse the typename as the identifier name and fall over misparsing
1769 // later parts of the diagnostic.
1770 //
1771 // As such, we try to do some look-ahead in cases where this would
1772 // otherwise be an "implicit-int" case to see if this is invalid. For
1773 // example: "static foo_t x = 4;" In this case, if we parsed foo_t as
1774 // an identifier with implicit int, we'd get a parse error because the
1775 // next token is obviously invalid for a type. Parse these as a case
1776 // with an invalid type specifier.
1777 assert(!DS.hasTypeSpecifier() && "Type specifier checked above");
Mike Stump11289f42009-09-09 15:08:12 +00001778
Chris Lattner20a0c612009-04-14 21:34:55 +00001779 // Since we know that this either implicit int (which is rare) or an
Richard Smitha952ebb2012-05-15 21:01:51 +00001780 // error, do lookahead to try to do better recovery. This never applies
1781 // within a type specifier. Outside of C++, we allow this even if the
1782 // language doesn't "officially" support implicit int -- we support
1783 // implicit int as an extension in C99 and C11. Allegedly, MS also
1784 // supports implicit int in C++ mode.
Richard Smith2f07ad52012-05-09 20:55:26 +00001785 if (DSC != DSC_type_specifier && DSC != DSC_trailing &&
Richard Smitha952ebb2012-05-15 21:01:51 +00001786 (!getLangOpts().CPlusPlus || getLangOpts().MicrosoftExt) &&
Richard Smithc5b05522012-03-12 07:56:15 +00001787 isValidAfterIdentifierInDeclarator(NextToken())) {
Chris Lattner20a0c612009-04-14 21:34:55 +00001788 // If this token is valid for implicit int, e.g. "static x = 4", then
1789 // we just avoid eating the identifier, so it will be parsed as the
1790 // identifier in the declarator.
1791 return false;
1792 }
Mike Stump11289f42009-09-09 15:08:12 +00001793
Richard Smitha952ebb2012-05-15 21:01:51 +00001794 if (getLangOpts().CPlusPlus &&
1795 DS.getStorageClassSpec() == DeclSpec::SCS_auto) {
1796 // Don't require a type specifier if we have the 'auto' storage class
1797 // specifier in C++98 -- we'll promote it to a type specifier.
1798 return false;
1799 }
1800
Chris Lattner20a0c612009-04-14 21:34:55 +00001801 // Otherwise, if we don't consume this token, we are going to emit an
1802 // error anyway. Try to recover from various common problems. Check
1803 // to see if this was a reference to a tag name without a tag specified.
1804 // This is a common problem in C (saying 'foo' instead of 'struct foo').
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001805 //
1806 // C++ doesn't need this, and isTagName doesn't take SS.
1807 if (SS == 0) {
Argyrios Kyrtzidis1f329402011-04-21 17:29:47 +00001808 const char *TagName = 0, *FixitTagName = 0;
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001809 tok::TokenKind TagKind = tok::unknown;
Mike Stump11289f42009-09-09 15:08:12 +00001810
Douglas Gregor0be31a22010-07-02 17:43:08 +00001811 switch (Actions.isTagName(*Tok.getIdentifierInfo(), getCurScope())) {
Chris Lattner20a0c612009-04-14 21:34:55 +00001812 default: break;
Argyrios Kyrtzidis1f329402011-04-21 17:29:47 +00001813 case DeclSpec::TST_enum:
1814 TagName="enum" ; FixitTagName = "enum " ; TagKind=tok::kw_enum ;break;
1815 case DeclSpec::TST_union:
1816 TagName="union" ; FixitTagName = "union " ;TagKind=tok::kw_union ;break;
1817 case DeclSpec::TST_struct:
1818 TagName="struct"; FixitTagName = "struct ";TagKind=tok::kw_struct;break;
1819 case DeclSpec::TST_class:
1820 TagName="class" ; FixitTagName = "class " ;TagKind=tok::kw_class ;break;
Chris Lattner20a0c612009-04-14 21:34:55 +00001821 }
Mike Stump11289f42009-09-09 15:08:12 +00001822
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001823 if (TagName) {
Kaelyn Uhrain031643e2012-04-26 23:36:17 +00001824 IdentifierInfo *TokenName = Tok.getIdentifierInfo();
1825 LookupResult R(Actions, TokenName, SourceLocation(),
1826 Sema::LookupOrdinaryName);
1827
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001828 Diag(Loc, diag::err_use_of_tag_name_without_tag)
Kaelyn Uhrain031643e2012-04-26 23:36:17 +00001829 << TokenName << TagName << getLangOpts().CPlusPlus
1830 << FixItHint::CreateInsertion(Tok.getLocation(), FixitTagName);
1831
1832 if (Actions.LookupParsedName(R, getCurScope(), SS)) {
1833 for (LookupResult::iterator I = R.begin(), IEnd = R.end();
1834 I != IEnd; ++I)
Kaelyn Uhrain3fe3f852012-04-27 18:26:49 +00001835 Diag((*I)->getLocation(), diag::note_decl_hiding_tag_type)
Kaelyn Uhrain031643e2012-04-26 23:36:17 +00001836 << TokenName << TagName;
1837 }
Mike Stump11289f42009-09-09 15:08:12 +00001838
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001839 // Parse this as a tag as if the missing tag were present.
1840 if (TagKind == tok::kw_enum)
Richard Smithc5b05522012-03-12 07:56:15 +00001841 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS, DSC_normal);
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001842 else
Richard Smithc5b05522012-03-12 07:56:15 +00001843 ParseClassSpecifier(TagKind, Loc, DS, TemplateInfo, AS,
1844 /*EnteringContext*/ false, DSC_normal);
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001845 return true;
1846 }
Chris Lattner20a0c612009-04-14 21:34:55 +00001847 }
Mike Stump11289f42009-09-09 15:08:12 +00001848
Richard Smithfe904f02012-05-15 21:29:55 +00001849 // Determine whether this identifier could plausibly be the name of something
Richard Smithedd124e2012-05-15 21:42:17 +00001850 // being declared (with a missing type).
Richard Smithfe904f02012-05-15 21:29:55 +00001851 if (DSC != DSC_type_specifier && DSC != DSC_trailing &&
1852 (!SS || DSC == DSC_top_level || DSC == DSC_class)) {
Richard Smitha952ebb2012-05-15 21:01:51 +00001853 // Look ahead to the next token to try to figure out what this declaration
1854 // was supposed to be.
1855 switch (NextToken().getKind()) {
1856 case tok::comma:
1857 case tok::equal:
1858 case tok::kw_asm:
1859 case tok::l_brace:
1860 case tok::l_square:
1861 case tok::semi:
1862 // This looks like a variable declaration. The type is probably missing.
1863 // We're done parsing decl-specifiers.
1864 return false;
1865
1866 case tok::l_paren: {
1867 // static x(4); // 'x' is not a type
1868 // x(int n); // 'x' is not a type
1869 // x (*p)[]; // 'x' is a type
1870 //
1871 // Since we're in an error case (or the rare 'implicit int in C++' MS
1872 // extension), we can afford to perform a tentative parse to determine
1873 // which case we're in.
1874 TentativeParsingAction PA(*this);
1875 ConsumeToken();
1876 TPResult TPR = TryParseDeclarator(/*mayBeAbstract*/false);
1877 PA.Revert();
1878 if (TPR == TPResult::False())
1879 return false;
1880 // The identifier is followed by a parenthesized declarator.
1881 // It's supposed to be a type.
1882 break;
1883 }
1884
1885 default:
1886 // This is probably supposed to be a type. This includes cases like:
1887 // int f(itn);
1888 // struct S { unsinged : 4; };
1889 break;
1890 }
1891 }
1892
Chad Rosierc1183952012-06-26 22:30:43 +00001893 // This is almost certainly an invalid type name. Let the action emit a
Douglas Gregor15e56022009-10-13 23:27:22 +00001894 // diagnostic and attempt to recover.
John McCallba7bf592010-08-24 05:47:05 +00001895 ParsedType T;
Kaelyn Uhrainb5b17fe2012-06-15 23:45:58 +00001896 IdentifierInfo *II = Tok.getIdentifierInfo();
1897 if (Actions.DiagnoseUnknownTypeName(II, Loc, getCurScope(), SS, T)) {
Douglas Gregor15e56022009-10-13 23:27:22 +00001898 // The action emitted a diagnostic, so we don't have to.
1899 if (T) {
1900 // The action has suggested that the type T could be used. Set that as
1901 // the type in the declaration specifiers, consume the would-be type
1902 // name token, and we're done.
1903 const char *PrevSpec;
1904 unsigned DiagID;
John McCallba7bf592010-08-24 05:47:05 +00001905 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID, T);
Douglas Gregor15e56022009-10-13 23:27:22 +00001906 DS.SetRangeEnd(Tok.getLocation());
1907 ConsumeToken();
Kaelyn Uhrainb5b17fe2012-06-15 23:45:58 +00001908 // There may be other declaration specifiers after this.
1909 return true;
1910 } else if (II != Tok.getIdentifierInfo()) {
1911 // If no type was suggested, the correction is to a keyword
1912 Tok.setKind(II->getTokenID());
Douglas Gregor15e56022009-10-13 23:27:22 +00001913 // There may be other declaration specifiers after this.
1914 return true;
1915 }
Chad Rosierc1183952012-06-26 22:30:43 +00001916
Douglas Gregor15e56022009-10-13 23:27:22 +00001917 // Fall through; the action had no suggestion for us.
1918 } else {
1919 // The action did not emit a diagnostic, so emit one now.
1920 SourceRange R;
1921 if (SS) R = SS->getRange();
1922 Diag(Loc, diag::err_unknown_typename) << Tok.getIdentifierInfo() << R;
1923 }
Mike Stump11289f42009-09-09 15:08:12 +00001924
Douglas Gregor15e56022009-10-13 23:27:22 +00001925 // Mark this as an error.
Richard Smithc5b05522012-03-12 07:56:15 +00001926 DS.SetTypeSpecError();
Chris Lattner20a0c612009-04-14 21:34:55 +00001927 DS.SetRangeEnd(Tok.getLocation());
1928 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001929
Chris Lattner20a0c612009-04-14 21:34:55 +00001930 // TODO: Could inject an invalid typedef decl in an enclosing scope to
1931 // avoid rippling error messages on subsequent uses of the same type,
1932 // could be useful if #include was forgotten.
1933 return false;
1934}
1935
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001936/// \brief Determine the declaration specifier context from the declarator
1937/// context.
1938///
1939/// \param Context the declarator context, which is one of the
1940/// Declarator::TheContext enumerator values.
Chad Rosierc1183952012-06-26 22:30:43 +00001941Parser::DeclSpecContext
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001942Parser::getDeclSpecContextFromDeclaratorContext(unsigned Context) {
1943 if (Context == Declarator::MemberContext)
1944 return DSC_class;
1945 if (Context == Declarator::FileContext)
1946 return DSC_top_level;
Richard Smith62dad822012-03-15 01:02:11 +00001947 if (Context == Declarator::TrailingReturnContext)
1948 return DSC_trailing;
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001949 return DSC_normal;
1950}
1951
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00001952/// ParseAlignArgument - Parse the argument to an alignment-specifier.
1953///
1954/// FIXME: Simply returns an alignof() expression if the argument is a
1955/// type. Ideally, the type should be propagated directly into Sema.
1956///
Benjamin Kramere56f3932011-12-23 17:00:35 +00001957/// [C11] type-id
1958/// [C11] constant-expression
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00001959/// [C++0x] type-id ...[opt]
1960/// [C++0x] assignment-expression ...[opt]
1961ExprResult Parser::ParseAlignArgument(SourceLocation Start,
1962 SourceLocation &EllipsisLoc) {
1963 ExprResult ER;
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00001964 if (isTypeIdInParens()) {
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00001965 SourceLocation TypeLoc = Tok.getLocation();
1966 ParsedType Ty = ParseTypeName().get();
1967 SourceRange TypeRange(Start, Tok.getLocation());
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00001968 ER = Actions.ActOnUnaryExprOrTypeTraitExpr(TypeLoc, UETT_AlignOf, true,
1969 Ty.getAsOpaquePtr(), TypeRange);
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00001970 } else
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00001971 ER = ParseConstantExpression();
1972
David Blaikiebbafb8a2012-03-11 07:00:24 +00001973 if (getLangOpts().CPlusPlus0x && Tok.is(tok::ellipsis))
Peter Collingbourneccbcce02011-10-24 17:56:00 +00001974 EllipsisLoc = ConsumeToken();
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00001975
1976 return ER;
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00001977}
1978
1979/// ParseAlignmentSpecifier - Parse an alignment-specifier, and add the
1980/// attribute to Attrs.
1981///
1982/// alignment-specifier:
Benjamin Kramere56f3932011-12-23 17:00:35 +00001983/// [C11] '_Alignas' '(' type-id ')'
1984/// [C11] '_Alignas' '(' constant-expression ')'
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00001985/// [C++0x] 'alignas' '(' type-id ...[opt] ')'
1986/// [C++0x] 'alignas' '(' assignment-expression ...[opt] ')'
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00001987void Parser::ParseAlignmentSpecifier(ParsedAttributes &Attrs,
1988 SourceLocation *endLoc) {
1989 assert((Tok.is(tok::kw_alignas) || Tok.is(tok::kw__Alignas)) &&
1990 "Not an alignment-specifier!");
1991
1992 SourceLocation KWLoc = Tok.getLocation();
1993 ConsumeToken();
1994
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001995 BalancedDelimiterTracker T(*this, tok::l_paren);
1996 if (T.expectAndConsume(diag::err_expected_lparen))
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00001997 return;
1998
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00001999 SourceLocation EllipsisLoc;
2000 ExprResult ArgExpr = ParseAlignArgument(T.getOpenLocation(), EllipsisLoc);
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002001 if (ArgExpr.isInvalid()) {
2002 SkipUntil(tok::r_paren);
2003 return;
2004 }
2005
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002006 T.consumeClose();
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002007 if (endLoc)
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002008 *endLoc = T.getCloseLocation();
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002009
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002010 // FIXME: Handle pack-expansions here.
2011 if (EllipsisLoc.isValid()) {
2012 Diag(EllipsisLoc, diag::err_alignas_pack_exp_unsupported);
2013 return;
2014 }
2015
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002016 ExprVector ArgExprs(Actions);
2017 ArgExprs.push_back(ArgExpr.release());
Alexis Hunt3bc72c12012-06-19 23:57:03 +00002018 // FIXME: This should not be GNU, but we since the attribute used is
2019 // based on the spelling, and there is no true spelling for
2020 // C++11 attributes, this isn't accepted.
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002021 Attrs.addNew(PP.getIdentifierInfo("aligned"), KWLoc, 0, KWLoc,
Alexis Hunta0e54d42012-06-18 16:13:52 +00002022 0, T.getOpenLocation(), ArgExprs.take(), 1,
Alexis Hunt3bc72c12012-06-19 23:57:03 +00002023 AttributeList::AS_GNU);
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002024}
2025
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002026/// ParseDeclarationSpecifiers
2027/// declaration-specifiers: [C99 6.7]
Chris Lattner3b561a32006-08-13 00:12:11 +00002028/// storage-class-specifier declaration-specifiers[opt]
2029/// type-specifier declaration-specifiers[opt]
Chris Lattner3b561a32006-08-13 00:12:11 +00002030/// [C99] function-specifier declaration-specifiers[opt]
Benjamin Kramere56f3932011-12-23 17:00:35 +00002031/// [C11] alignment-specifier declaration-specifiers[opt]
Chris Lattnere37e2332006-08-15 04:50:22 +00002032/// [GNU] attributes declaration-specifiers[opt]
Douglas Gregor26701a42011-09-09 02:06:17 +00002033/// [Clang] '__module_private__' declaration-specifiers[opt]
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002034///
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002035/// storage-class-specifier: [C99 6.7.1]
Chris Lattnerda48a8e2006-08-04 05:25:55 +00002036/// 'typedef'
2037/// 'extern'
2038/// 'static'
2039/// 'auto'
2040/// 'register'
Sebastian Redlccdfaba2008-11-14 23:42:31 +00002041/// [C++] 'mutable'
Chris Lattnerda48a8e2006-08-04 05:25:55 +00002042/// [GNU] '__thread'
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002043/// function-specifier: [C99 6.7.4]
Chris Lattner3b561a32006-08-13 00:12:11 +00002044/// [C99] 'inline'
Douglas Gregor61956c42008-10-31 09:07:45 +00002045/// [C++] 'virtual'
2046/// [C++] 'explicit'
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00002047/// [OpenCL] '__kernel'
Anders Carlssoncd8db412009-05-06 04:46:28 +00002048/// 'friend': [C++ dcl.friend]
Sebastian Redl39c2a8b2009-11-05 15:47:02 +00002049/// 'constexpr': [C++0x dcl.constexpr]
Anders Carlssoncd8db412009-05-06 04:46:28 +00002050
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002051///
Douglas Gregorb9bd8a92008-12-24 02:52:09 +00002052void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
Douglas Gregor1b57ff32009-05-12 23:25:50 +00002053 const ParsedTemplateInfo &TemplateInfo,
John McCall07e91c02009-08-06 02:15:43 +00002054 AccessSpecifier AS,
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +00002055 DeclSpecContext DSContext,
2056 LateParsedAttrList *LateAttrs) {
Douglas Gregor0e7dde52011-04-24 05:37:28 +00002057 if (DS.getSourceRange().isInvalid()) {
2058 DS.SetRangeStart(Tok.getLocation());
2059 DS.SetRangeEnd(Tok.getLocation());
2060 }
Chad Rosierc1183952012-06-26 22:30:43 +00002061
Douglas Gregordf593fb2011-11-07 17:33:42 +00002062 bool EnteringContext = (DSContext == DSC_class || DSContext == DSC_top_level);
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00002063 bool AttrsLastTime = false;
2064 ParsedAttributesWithRange attrs(AttrFactory);
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002065 while (1) {
John McCall49bfce42009-08-03 20:12:06 +00002066 bool isInvalid = false;
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002067 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00002068 unsigned DiagID = 0;
2069
Chris Lattner4d8f8732006-11-28 05:05:08 +00002070 SourceLocation Loc = Tok.getLocation();
Douglas Gregor450c75a2008-11-07 15:42:26 +00002071
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002072 switch (Tok.getKind()) {
Mike Stump11289f42009-09-09 15:08:12 +00002073 default:
Chris Lattner0974b232008-07-26 00:20:22 +00002074 DoneWithDeclSpec:
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00002075 if (!AttrsLastTime)
2076 ProhibitAttributes(attrs);
2077 else
2078 DS.takeAttributesFrom(attrs);
Peter Collingbourne70188b32011-09-29 18:03:57 +00002079
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002080 // If this is not a declaration specifier token, we're done reading decl
2081 // specifiers. First verify that DeclSpec's are consistent.
Douglas Gregore3e01a22009-04-01 22:41:11 +00002082 DS.Finish(Diags, PP);
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002083 return;
Mike Stump11289f42009-09-09 15:08:12 +00002084
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00002085 case tok::l_square:
2086 case tok::kw_alignas:
2087 if (!isCXX11AttributeSpecifier())
2088 goto DoneWithDeclSpec;
2089
2090 ProhibitAttributes(attrs);
2091 // FIXME: It would be good to recover by accepting the attributes,
2092 // but attempting to do that now would cause serious
2093 // madness in terms of diagnostics.
2094 attrs.clear();
2095 attrs.Range = SourceRange();
2096
2097 ParseCXX11Attributes(attrs);
2098 AttrsLastTime = true;
Chad Rosierc1183952012-06-26 22:30:43 +00002099 continue;
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00002100
Douglas Gregorc49f5b22010-08-23 18:23:48 +00002101 case tok::code_completion: {
John McCallfaf5fb42010-08-26 23:41:50 +00002102 Sema::ParserCompletionContext CCC = Sema::PCC_Namespace;
Douglas Gregorc49f5b22010-08-23 18:23:48 +00002103 if (DS.hasTypeSpecifier()) {
2104 bool AllowNonIdentifiers
2105 = (getCurScope()->getFlags() & (Scope::ControlScope |
2106 Scope::BlockScope |
2107 Scope::TemplateParamScope |
2108 Scope::FunctionPrototypeScope |
2109 Scope::AtCatchScope)) == 0;
2110 bool AllowNestedNameSpecifiers
Chad Rosierc1183952012-06-26 22:30:43 +00002111 = DSContext == DSC_top_level ||
Douglas Gregorc49f5b22010-08-23 18:23:48 +00002112 (DSContext == DSC_class && DS.isFriendSpecified());
2113
Douglas Gregorbfcea8b2010-09-16 15:14:18 +00002114 Actions.CodeCompleteDeclSpec(getCurScope(), DS,
Chad Rosierc1183952012-06-26 22:30:43 +00002115 AllowNonIdentifiers,
Douglas Gregorbfcea8b2010-09-16 15:14:18 +00002116 AllowNestedNameSpecifiers);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002117 return cutOffParsing();
Chad Rosierc1183952012-06-26 22:30:43 +00002118 }
2119
Douglas Gregor80039242011-02-15 20:33:25 +00002120 if (getCurScope()->getFnParent() || getCurScope()->getBlockParent())
2121 CCC = Sema::PCC_LocalDeclarationSpecifiers;
2122 else if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate)
Chad Rosierc1183952012-06-26 22:30:43 +00002123 CCC = DSContext == DSC_class? Sema::PCC_MemberTemplate
John McCallfaf5fb42010-08-26 23:41:50 +00002124 : Sema::PCC_Template;
Douglas Gregorc49f5b22010-08-23 18:23:48 +00002125 else if (DSContext == DSC_class)
John McCallfaf5fb42010-08-26 23:41:50 +00002126 CCC = Sema::PCC_Class;
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002127 else if (CurParsedObjCImpl)
John McCallfaf5fb42010-08-26 23:41:50 +00002128 CCC = Sema::PCC_ObjCImplementation;
Chad Rosierc1183952012-06-26 22:30:43 +00002129
Douglas Gregorc49f5b22010-08-23 18:23:48 +00002130 Actions.CodeCompleteOrdinaryName(getCurScope(), CCC);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002131 return cutOffParsing();
Douglas Gregorc49f5b22010-08-23 18:23:48 +00002132 }
2133
Chris Lattnerbd31aa32009-01-05 00:07:25 +00002134 case tok::coloncolon: // ::foo::bar
John McCall1f476a12010-02-26 08:45:28 +00002135 // C++ scope specifier. Annotate and loop, or bail out on error.
2136 if (TryAnnotateCXXScopeToken(true)) {
2137 if (!DS.hasTypeSpecifier())
2138 DS.SetTypeSpecError();
2139 goto DoneWithDeclSpec;
2140 }
John McCall8bc2a702010-03-01 18:20:46 +00002141 if (Tok.is(tok::coloncolon)) // ::new or ::delete
2142 goto DoneWithDeclSpec;
John McCall1f476a12010-02-26 08:45:28 +00002143 continue;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002144
2145 case tok::annot_cxxscope: {
Richard Smith3092a3b2012-05-09 18:56:43 +00002146 if (DS.hasTypeSpecifier() || DS.isTypeAltiVecVector())
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002147 goto DoneWithDeclSpec;
2148
John McCall9dab4e62009-12-12 11:40:51 +00002149 CXXScopeSpec SS;
Douglas Gregor869ad452011-02-24 17:54:50 +00002150 Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(),
2151 Tok.getAnnotationRange(),
2152 SS);
John McCall9dab4e62009-12-12 11:40:51 +00002153
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002154 // We are looking for a qualified typename.
Douglas Gregor167fa622009-03-25 15:40:00 +00002155 Token Next = NextToken();
Mike Stump11289f42009-09-09 15:08:12 +00002156 if (Next.is(tok::annot_template_id) &&
Douglas Gregor167fa622009-03-25 15:40:00 +00002157 static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue())
Douglas Gregorb67535d2009-03-31 00:43:58 +00002158 ->Kind == TNK_Type_template) {
Douglas Gregor167fa622009-03-25 15:40:00 +00002159 // We have a qualified template-id, e.g., N::A<int>
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002160
2161 // C++ [class.qual]p2:
2162 // In a lookup in which the constructor is an acceptable lookup
2163 // result and the nested-name-specifier nominates a class C:
2164 //
2165 // - if the name specified after the
2166 // nested-name-specifier, when looked up in C, is the
2167 // injected-class-name of C (Clause 9), or
2168 //
2169 // - if the name specified after the nested-name-specifier
2170 // is the same as the identifier or the
2171 // simple-template-id's template-name in the last
2172 // component of the nested-name-specifier,
2173 //
2174 // the name is instead considered to name the constructor of
2175 // class C.
Chad Rosierc1183952012-06-26 22:30:43 +00002176 //
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002177 // Thus, if the template-name is actually the constructor
2178 // name, then the code is ill-formed; this interpretation is
Chad Rosierc1183952012-06-26 22:30:43 +00002179 // reinforced by the NAD status of core issue 635.
Argyrios Kyrtzidisc0c5dd22011-06-22 06:09:49 +00002180 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Next);
John McCall84821e72010-04-13 06:39:49 +00002181 if ((DSContext == DSC_top_level ||
2182 (DSContext == DSC_class && DS.isFriendSpecified())) &&
2183 TemplateId->Name &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00002184 Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) {
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002185 if (isConstructorDeclarator()) {
2186 // The user meant this to be an out-of-line constructor
2187 // definition, but template arguments are not allowed
2188 // there. Just allow this as a constructor; we'll
2189 // complain about it later.
2190 goto DoneWithDeclSpec;
2191 }
2192
2193 // The user meant this to name a type, but it actually names
2194 // a constructor with some extraneous template
2195 // arguments. Complain, then parse it as a type as the user
2196 // intended.
2197 Diag(TemplateId->TemplateNameLoc,
2198 diag::err_out_of_line_template_id_names_constructor)
2199 << TemplateId->Name;
2200 }
2201
John McCall9dab4e62009-12-12 11:40:51 +00002202 DS.getTypeSpecScope() = SS;
2203 ConsumeToken(); // The C++ scope.
Mike Stump11289f42009-09-09 15:08:12 +00002204 assert(Tok.is(tok::annot_template_id) &&
Douglas Gregor167fa622009-03-25 15:40:00 +00002205 "ParseOptionalCXXScopeSpecifier not working");
Douglas Gregore7c20652011-03-02 00:47:37 +00002206 AnnotateTemplateIdTokenAsType();
Douglas Gregor167fa622009-03-25 15:40:00 +00002207 continue;
2208 }
2209
Douglas Gregorc5790df2009-09-28 07:26:33 +00002210 if (Next.is(tok::annot_typename)) {
John McCall9dab4e62009-12-12 11:40:51 +00002211 DS.getTypeSpecScope() = SS;
2212 ConsumeToken(); // The C++ scope.
John McCallba7bf592010-08-24 05:47:05 +00002213 if (Tok.getAnnotationValue()) {
2214 ParsedType T = getTypeAnnotation(Tok);
Nico Weber77430342010-11-22 10:30:56 +00002215 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename,
Chad Rosierc1183952012-06-26 22:30:43 +00002216 Tok.getAnnotationEndLoc(),
John McCallba7bf592010-08-24 05:47:05 +00002217 PrevSpec, DiagID, T);
2218 }
Douglas Gregorc5790df2009-09-28 07:26:33 +00002219 else
2220 DS.SetTypeSpecError();
2221 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
2222 ConsumeToken(); // The typename
2223 }
2224
Douglas Gregor167fa622009-03-25 15:40:00 +00002225 if (Next.isNot(tok::identifier))
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002226 goto DoneWithDeclSpec;
2227
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002228 // If we're in a context where the identifier could be a class name,
2229 // check whether this is a constructor declaration.
John McCall84821e72010-04-13 06:39:49 +00002230 if ((DSContext == DSC_top_level ||
2231 (DSContext == DSC_class && DS.isFriendSpecified())) &&
Chad Rosierc1183952012-06-26 22:30:43 +00002232 Actions.isCurrentClassName(*Next.getIdentifierInfo(), getCurScope(),
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002233 &SS)) {
2234 if (isConstructorDeclarator())
2235 goto DoneWithDeclSpec;
2236
2237 // As noted in C++ [class.qual]p2 (cited above), when the name
2238 // of the class is qualified in a context where it could name
2239 // a constructor, its a constructor name. However, we've
2240 // looked at the declarator, and the user probably meant this
2241 // to be a type. Complain that it isn't supposed to be treated
2242 // as a type, then proceed to parse it as a type.
2243 Diag(Next.getLocation(), diag::err_out_of_line_type_names_constructor)
2244 << Next.getIdentifierInfo();
2245 }
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002246
John McCallba7bf592010-08-24 05:47:05 +00002247 ParsedType TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(),
2248 Next.getLocation(),
Douglas Gregor844cb502011-03-01 18:12:44 +00002249 getCurScope(), &SS,
2250 false, false, ParsedType(),
Abramo Bagnara4244b432012-01-27 08:46:19 +00002251 /*IsCtorOrDtorName=*/false,
Douglas Gregor844cb502011-03-01 18:12:44 +00002252 /*NonTrivialSourceInfo=*/true);
Douglas Gregor8bf42052009-02-09 18:46:07 +00002253
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002254 // If the referenced identifier is not a type, then this declspec is
2255 // erroneous: We already checked about that it has no type specifier, and
2256 // C++ doesn't have implicit int. Diagnose it as a typo w.r.t. to the
Mike Stump11289f42009-09-09 15:08:12 +00002257 // typename.
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002258 if (TypeRep == 0) {
2259 ConsumeToken(); // Eat the scope spec so the identifier is current.
Richard Smithc5b05522012-03-12 07:56:15 +00002260 if (ParseImplicitInt(DS, &SS, TemplateInfo, AS, DSContext)) continue;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002261 goto DoneWithDeclSpec;
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002262 }
Mike Stump11289f42009-09-09 15:08:12 +00002263
John McCall9dab4e62009-12-12 11:40:51 +00002264 DS.getTypeSpecScope() = SS;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002265 ConsumeToken(); // The C++ scope.
2266
Douglas Gregor9817f4a2009-02-09 15:09:02 +00002267 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00002268 DiagID, TypeRep);
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002269 if (isInvalid)
2270 break;
Mike Stump11289f42009-09-09 15:08:12 +00002271
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002272 DS.SetRangeEnd(Tok.getLocation());
2273 ConsumeToken(); // The typename.
2274
2275 continue;
2276 }
Mike Stump11289f42009-09-09 15:08:12 +00002277
Chris Lattnere387d9e2009-01-21 19:48:37 +00002278 case tok::annot_typename: {
John McCallba7bf592010-08-24 05:47:05 +00002279 if (Tok.getAnnotationValue()) {
2280 ParsedType T = getTypeAnnotation(Tok);
Nico Weber7f8bb362010-11-22 12:50:03 +00002281 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallba7bf592010-08-24 05:47:05 +00002282 DiagID, T);
2283 } else
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00002284 DS.SetTypeSpecError();
Chad Rosierc1183952012-06-26 22:30:43 +00002285
Chris Lattner005fc1b2010-04-05 18:18:31 +00002286 if (isInvalid)
2287 break;
2288
Chris Lattnere387d9e2009-01-21 19:48:37 +00002289 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
2290 ConsumeToken(); // The typename
Mike Stump11289f42009-09-09 15:08:12 +00002291
Chris Lattnere387d9e2009-01-21 19:48:37 +00002292 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
2293 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Chad Rosierc1183952012-06-26 22:30:43 +00002294 // Objective-C interface.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002295 if (Tok.is(tok::less) && getLangOpts().ObjC1)
Douglas Gregor06e41ae2010-10-21 23:17:00 +00002296 ParseObjCProtocolQualifiers(DS);
Chad Rosierc1183952012-06-26 22:30:43 +00002297
Chris Lattnere387d9e2009-01-21 19:48:37 +00002298 continue;
2299 }
Mike Stump11289f42009-09-09 15:08:12 +00002300
Douglas Gregor06873092011-04-28 15:48:45 +00002301 case tok::kw___is_signed:
2302 // GNU libstdc++ 4.4 uses __is_signed as an identifier, but Clang
2303 // typically treats it as a trait. If we see __is_signed as it appears
2304 // in libstdc++, e.g.,
2305 //
2306 // static const bool __is_signed;
2307 //
2308 // then treat __is_signed as an identifier rather than as a keyword.
2309 if (DS.getTypeSpecType() == TST_bool &&
2310 DS.getTypeQualifiers() == DeclSpec::TQ_const &&
2311 DS.getStorageClassSpec() == DeclSpec::SCS_static) {
2312 Tok.getIdentifierInfo()->RevertTokenIDToIdentifier();
2313 Tok.setKind(tok::identifier);
2314 }
2315
2316 // We're done with the declaration-specifiers.
2317 goto DoneWithDeclSpec;
Chad Rosierc1183952012-06-26 22:30:43 +00002318
Chris Lattner16fac4f2008-07-26 01:18:38 +00002319 // typedef-name
David Blaikie15a430a2011-12-04 05:04:18 +00002320 case tok::kw_decltype:
Chris Lattner16fac4f2008-07-26 01:18:38 +00002321 case tok::identifier: {
Chris Lattnerbd31aa32009-01-05 00:07:25 +00002322 // In C++, check to see if this is a scope specifier like foo::bar::, if
2323 // so handle it as such. This is important for ctor parsing.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002324 if (getLangOpts().CPlusPlus) {
John McCall1f476a12010-02-26 08:45:28 +00002325 if (TryAnnotateCXXScopeToken(true)) {
2326 if (!DS.hasTypeSpecifier())
2327 DS.SetTypeSpecError();
2328 goto DoneWithDeclSpec;
2329 }
2330 if (!Tok.is(tok::identifier))
2331 continue;
2332 }
Mike Stump11289f42009-09-09 15:08:12 +00002333
Chris Lattner16fac4f2008-07-26 01:18:38 +00002334 // This identifier can only be a typedef name if we haven't already seen
2335 // a type-specifier. Without this check we misparse:
2336 // typedef int X; struct Y { short X; }; as 'short int'.
2337 if (DS.hasTypeSpecifier())
2338 goto DoneWithDeclSpec;
Mike Stump11289f42009-09-09 15:08:12 +00002339
John Thompson22334602010-02-05 00:12:22 +00002340 // Check for need to substitute AltiVec keyword tokens.
2341 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
2342 break;
2343
Richard Smith3092a3b2012-05-09 18:56:43 +00002344 // [AltiVec] 2.2: [If the 'vector' specifier is used] The syntax does not
2345 // allow the use of a typedef name as a type specifier.
2346 if (DS.isTypeAltiVecVector())
2347 goto DoneWithDeclSpec;
2348
John McCallba7bf592010-08-24 05:47:05 +00002349 ParsedType TypeRep =
2350 Actions.getTypeName(*Tok.getIdentifierInfo(),
2351 Tok.getLocation(), getCurScope());
Douglas Gregor8bf42052009-02-09 18:46:07 +00002352
Chris Lattner6cc055a2009-04-12 20:42:31 +00002353 // If this is not a typedef name, don't parse it as part of the declspec,
2354 // it must be an implicit int or an error.
John McCallba7bf592010-08-24 05:47:05 +00002355 if (!TypeRep) {
Richard Smithc5b05522012-03-12 07:56:15 +00002356 if (ParseImplicitInt(DS, 0, TemplateInfo, AS, DSContext)) continue;
Chris Lattner16fac4f2008-07-26 01:18:38 +00002357 goto DoneWithDeclSpec;
Chris Lattner6cc055a2009-04-12 20:42:31 +00002358 }
Douglas Gregor8bf42052009-02-09 18:46:07 +00002359
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002360 // If we're in a context where the identifier could be a class name,
2361 // check whether this is a constructor declaration.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002362 if (getLangOpts().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00002363 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), getCurScope()) &&
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002364 isConstructorDeclarator())
Douglas Gregor61956c42008-10-31 09:07:45 +00002365 goto DoneWithDeclSpec;
2366
Douglas Gregor9817f4a2009-02-09 15:09:02 +00002367 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00002368 DiagID, TypeRep);
Chris Lattner16fac4f2008-07-26 01:18:38 +00002369 if (isInvalid)
2370 break;
Mike Stump11289f42009-09-09 15:08:12 +00002371
Chris Lattner16fac4f2008-07-26 01:18:38 +00002372 DS.SetRangeEnd(Tok.getLocation());
2373 ConsumeToken(); // The identifier
2374
2375 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
2376 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Chad Rosierc1183952012-06-26 22:30:43 +00002377 // Objective-C interface.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002378 if (Tok.is(tok::less) && getLangOpts().ObjC1)
Douglas Gregor06e41ae2010-10-21 23:17:00 +00002379 ParseObjCProtocolQualifiers(DS);
Chad Rosierc1183952012-06-26 22:30:43 +00002380
Steve Naroffcd5e7822008-09-22 10:28:57 +00002381 // Need to support trailing type qualifiers (e.g. "id<p> const").
2382 // If a type specifier follows, it will be diagnosed elsewhere.
2383 continue;
Chris Lattner16fac4f2008-07-26 01:18:38 +00002384 }
Douglas Gregor7f741122009-02-25 19:37:18 +00002385
2386 // type-name
2387 case tok::annot_template_id: {
Argyrios Kyrtzidisc0c5dd22011-06-22 06:09:49 +00002388 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregorb67535d2009-03-31 00:43:58 +00002389 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor7f741122009-02-25 19:37:18 +00002390 // This template-id does not refer to a type name, so we're
2391 // done with the type-specifiers.
2392 goto DoneWithDeclSpec;
2393 }
2394
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002395 // If we're in a context where the template-id could be a
2396 // constructor name or specialization, check whether this is a
2397 // constructor declaration.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002398 if (getLangOpts().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00002399 Actions.isCurrentClassName(*TemplateId->Name, getCurScope()) &&
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002400 isConstructorDeclarator())
2401 goto DoneWithDeclSpec;
2402
Douglas Gregor7f741122009-02-25 19:37:18 +00002403 // Turn the template-id annotation token into a type annotation
2404 // token, then try again to parse it as a type-specifier.
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00002405 AnnotateTemplateIdTokenAsType();
Douglas Gregor7f741122009-02-25 19:37:18 +00002406 continue;
2407 }
2408
Chris Lattnere37e2332006-08-15 04:50:22 +00002409 // GNU attributes support.
2410 case tok::kw___attribute:
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +00002411 ParseGNUAttributes(DS.getAttributes(), 0, LateAttrs);
Chris Lattnerb95cca02006-10-17 03:01:08 +00002412 continue;
Steve Naroff3a9b7e02008-12-24 20:59:21 +00002413
2414 // Microsoft declspec support.
2415 case tok::kw___declspec:
John McCall53fa7142010-12-24 02:08:15 +00002416 ParseMicrosoftDeclSpec(DS.getAttributes());
Steve Naroff3a9b7e02008-12-24 20:59:21 +00002417 continue;
Mike Stump11289f42009-09-09 15:08:12 +00002418
Steve Naroff44ac7772008-12-25 14:16:32 +00002419 // Microsoft single token adornments.
Michael J. Spencerf97bd8c2012-06-18 07:00:48 +00002420 case tok::kw___forceinline: {
2421 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec, DiagID);
2422 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
2423 SourceLocation AttrNameLoc = ConsumeToken();
Alexis Hunta0e54d42012-06-18 16:13:52 +00002424 // FIXME: This does not work correctly if it is set to be a declspec
2425 // attribute, and a GNU attribute is simply incorrect.
Michael J. Spencerf97bd8c2012-06-18 07:00:48 +00002426 DS.getAttributes().addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
Alexis Hunta0e54d42012-06-18 16:13:52 +00002427 SourceLocation(), 0, 0, AttributeList::AS_GNU);
Michael J. Spencerf97bd8c2012-06-18 07:00:48 +00002428 continue;
2429 }
Eli Friedman53339e02009-06-08 23:27:34 +00002430
2431 case tok::kw___ptr64:
Francois Pichetf2fb4112011-08-25 00:36:46 +00002432 case tok::kw___ptr32:
Steve Narofff9c29d42008-12-25 14:41:26 +00002433 case tok::kw___w64:
Steve Naroff44ac7772008-12-25 14:16:32 +00002434 case tok::kw___cdecl:
2435 case tok::kw___stdcall:
2436 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00002437 case tok::kw___thiscall:
Francois Pichet17ed0202011-08-18 09:59:55 +00002438 case tok::kw___unaligned:
John McCall53fa7142010-12-24 02:08:15 +00002439 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman53339e02009-06-08 23:27:34 +00002440 continue;
2441
Dawn Perchik335e16b2010-09-03 01:29:35 +00002442 // Borland single token adornments.
2443 case tok::kw___pascal:
John McCall53fa7142010-12-24 02:08:15 +00002444 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik335e16b2010-09-03 01:29:35 +00002445 continue;
2446
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00002447 // OpenCL single token adornments.
2448 case tok::kw___kernel:
2449 ParseOpenCLAttributes(DS.getAttributes());
2450 continue;
2451
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002452 // storage-class-specifier
2453 case tok::kw_typedef:
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002454 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_typedef, Loc,
2455 PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002456 break;
2457 case tok::kw_extern:
Chris Lattner353f5742006-11-28 04:50:12 +00002458 if (DS.isThreadSpecified())
Chris Lattner6d29c102008-11-18 07:48:38 +00002459 Diag(Tok, diag::ext_thread_before) << "extern";
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002460 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_extern, Loc,
2461 PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002462 break;
Steve Naroff2050b0d2007-12-18 00:16:02 +00002463 case tok::kw___private_extern__:
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002464 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_private_extern,
2465 Loc, PrevSpec, DiagID);
Steve Naroff2050b0d2007-12-18 00:16:02 +00002466 break;
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002467 case tok::kw_static:
Chris Lattner353f5742006-11-28 04:50:12 +00002468 if (DS.isThreadSpecified())
Chris Lattner6d29c102008-11-18 07:48:38 +00002469 Diag(Tok, diag::ext_thread_before) << "static";
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002470 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_static, Loc,
2471 PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002472 break;
2473 case tok::kw_auto:
David Blaikiebbafb8a2012-03-11 07:00:24 +00002474 if (getLangOpts().CPlusPlus0x) {
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00002475 if (isKnownToBeTypeSpecifier(GetLookAheadToken(1))) {
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002476 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc,
2477 PrevSpec, DiagID);
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00002478 if (!isInvalid)
Richard Smith58c74332011-09-04 19:54:14 +00002479 Diag(Tok, diag::ext_auto_storage_class)
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00002480 << FixItHint::CreateRemoval(DS.getStorageClassSpecLoc());
Richard Smith58c74332011-09-04 19:54:14 +00002481 } else
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00002482 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec,
2483 DiagID);
Richard Smith58c74332011-09-04 19:54:14 +00002484 } else
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002485 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc,
2486 PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002487 break;
2488 case tok::kw_register:
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002489 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_register, Loc,
2490 PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002491 break;
Sebastian Redlccdfaba2008-11-14 23:42:31 +00002492 case tok::kw_mutable:
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002493 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_mutable, Loc,
2494 PrevSpec, DiagID);
Sebastian Redlccdfaba2008-11-14 23:42:31 +00002495 break;
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002496 case tok::kw___thread:
John McCall49bfce42009-08-03 20:12:06 +00002497 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002498 break;
Mike Stump11289f42009-09-09 15:08:12 +00002499
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002500 // function-specifier
2501 case tok::kw_inline:
John McCall49bfce42009-08-03 20:12:06 +00002502 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec, DiagID);
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002503 break;
Douglas Gregor61956c42008-10-31 09:07:45 +00002504 case tok::kw_virtual:
John McCall49bfce42009-08-03 20:12:06 +00002505 isInvalid = DS.SetFunctionSpecVirtual(Loc, PrevSpec, DiagID);
Douglas Gregor61956c42008-10-31 09:07:45 +00002506 break;
Douglas Gregor61956c42008-10-31 09:07:45 +00002507 case tok::kw_explicit:
John McCall49bfce42009-08-03 20:12:06 +00002508 isInvalid = DS.SetFunctionSpecExplicit(Loc, PrevSpec, DiagID);
Douglas Gregor61956c42008-10-31 09:07:45 +00002509 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00002510
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002511 // alignment-specifier
2512 case tok::kw__Alignas:
David Blaikiebbafb8a2012-03-11 07:00:24 +00002513 if (!getLangOpts().C11)
Jordan Rose58d54722012-06-30 21:33:57 +00002514 Diag(Tok, diag::ext_c11_alignment) << Tok.getName();
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002515 ParseAlignmentSpecifier(DS.getAttributes());
2516 continue;
2517
Anders Carlssoncd8db412009-05-06 04:46:28 +00002518 // friend
2519 case tok::kw_friend:
John McCall07e91c02009-08-06 02:15:43 +00002520 if (DSContext == DSC_class)
2521 isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID);
2522 else {
2523 PrevSpec = ""; // not actually used by the diagnostic
2524 DiagID = diag::err_friend_invalid_in_context;
2525 isInvalid = true;
2526 }
Anders Carlssoncd8db412009-05-06 04:46:28 +00002527 break;
Mike Stump11289f42009-09-09 15:08:12 +00002528
Douglas Gregor26701a42011-09-09 02:06:17 +00002529 // Modules
2530 case tok::kw___module_private__:
2531 isInvalid = DS.setModulePrivateSpec(Loc, PrevSpec, DiagID);
2532 break;
Chad Rosierc1183952012-06-26 22:30:43 +00002533
Sebastian Redl39c2a8b2009-11-05 15:47:02 +00002534 // constexpr
2535 case tok::kw_constexpr:
2536 isInvalid = DS.SetConstexprSpec(Loc, PrevSpec, DiagID);
2537 break;
2538
Chris Lattnere387d9e2009-01-21 19:48:37 +00002539 // type-specifier
2540 case tok::kw_short:
John McCall49bfce42009-08-03 20:12:06 +00002541 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec,
2542 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002543 break;
2544 case tok::kw_long:
2545 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCall49bfce42009-08-03 20:12:06 +00002546 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
2547 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002548 else
John McCall49bfce42009-08-03 20:12:06 +00002549 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2550 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002551 break;
Francois Pichet84133e42011-04-28 01:59:37 +00002552 case tok::kw___int64:
2553 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2554 DiagID);
2555 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00002556 case tok::kw_signed:
John McCall49bfce42009-08-03 20:12:06 +00002557 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec,
2558 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002559 break;
2560 case tok::kw_unsigned:
John McCall49bfce42009-08-03 20:12:06 +00002561 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
2562 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002563 break;
2564 case tok::kw__Complex:
John McCall49bfce42009-08-03 20:12:06 +00002565 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
2566 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002567 break;
2568 case tok::kw__Imaginary:
John McCall49bfce42009-08-03 20:12:06 +00002569 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
2570 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002571 break;
2572 case tok::kw_void:
John McCall49bfce42009-08-03 20:12:06 +00002573 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec,
2574 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002575 break;
2576 case tok::kw_char:
John McCall49bfce42009-08-03 20:12:06 +00002577 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec,
2578 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002579 break;
2580 case tok::kw_int:
John McCall49bfce42009-08-03 20:12:06 +00002581 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec,
2582 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002583 break;
Richard Smithf016bbc2012-04-04 06:24:32 +00002584 case tok::kw___int128:
2585 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int128, Loc, PrevSpec,
2586 DiagID);
2587 break;
2588 case tok::kw_half:
2589 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_half, Loc, PrevSpec,
2590 DiagID);
2591 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00002592 case tok::kw_float:
John McCall49bfce42009-08-03 20:12:06 +00002593 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec,
2594 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002595 break;
2596 case tok::kw_double:
John McCall49bfce42009-08-03 20:12:06 +00002597 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec,
2598 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002599 break;
2600 case tok::kw_wchar_t:
John McCall49bfce42009-08-03 20:12:06 +00002601 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec,
2602 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002603 break;
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00002604 case tok::kw_char16_t:
John McCall49bfce42009-08-03 20:12:06 +00002605 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec,
2606 DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00002607 break;
2608 case tok::kw_char32_t:
John McCall49bfce42009-08-03 20:12:06 +00002609 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec,
2610 DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00002611 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00002612 case tok::kw_bool:
2613 case tok::kw__Bool:
Argyrios Kyrtzidis20ee5ae2010-11-16 18:18:13 +00002614 if (Tok.is(tok::kw_bool) &&
2615 DS.getTypeSpecType() != DeclSpec::TST_unspecified &&
2616 DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
2617 PrevSpec = ""; // Not used by the diagnostic.
2618 DiagID = diag::err_bool_redeclaration;
Fariborz Jahanian2b059992011-04-19 21:42:37 +00002619 // For better error recovery.
2620 Tok.setKind(tok::identifier);
Argyrios Kyrtzidis20ee5ae2010-11-16 18:18:13 +00002621 isInvalid = true;
2622 } else {
2623 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec,
2624 DiagID);
2625 }
Chris Lattnere387d9e2009-01-21 19:48:37 +00002626 break;
2627 case tok::kw__Decimal32:
John McCall49bfce42009-08-03 20:12:06 +00002628 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
2629 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002630 break;
2631 case tok::kw__Decimal64:
John McCall49bfce42009-08-03 20:12:06 +00002632 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
2633 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002634 break;
2635 case tok::kw__Decimal128:
John McCall49bfce42009-08-03 20:12:06 +00002636 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
2637 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002638 break;
John Thompson22334602010-02-05 00:12:22 +00002639 case tok::kw___vector:
2640 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
2641 break;
2642 case tok::kw___pixel:
2643 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
2644 break;
John McCall39439732011-04-09 22:50:59 +00002645 case tok::kw___unknown_anytype:
2646 isInvalid = DS.SetTypeSpecType(TST_unknown_anytype, Loc,
2647 PrevSpec, DiagID);
2648 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00002649
2650 // class-specifier:
2651 case tok::kw_class:
2652 case tok::kw_struct:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00002653 case tok::kw_union: {
2654 tok::TokenKind Kind = Tok.getKind();
2655 ConsumeToken();
Richard Smithc5b05522012-03-12 07:56:15 +00002656 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS,
2657 EnteringContext, DSContext);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002658 continue;
Chris Lattnerffaa0e62009-04-12 21:49:30 +00002659 }
Chris Lattnere387d9e2009-01-21 19:48:37 +00002660
2661 // enum-specifier:
2662 case tok::kw_enum:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00002663 ConsumeToken();
Richard Smithc5b05522012-03-12 07:56:15 +00002664 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS, DSContext);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002665 continue;
2666
2667 // cv-qualifier:
2668 case tok::kw_const:
John McCall49bfce42009-08-03 20:12:06 +00002669 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec, DiagID,
Richard Smith7ac3c6a2012-07-24 20:24:58 +00002670 getLangOpts(), /*IsTypeSpec*/true);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002671 break;
2672 case tok::kw_volatile:
John McCall49bfce42009-08-03 20:12:06 +00002673 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
Richard Smith7ac3c6a2012-07-24 20:24:58 +00002674 getLangOpts(), /*IsTypeSpec*/true);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002675 break;
2676 case tok::kw_restrict:
John McCall49bfce42009-08-03 20:12:06 +00002677 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
Richard Smith7ac3c6a2012-07-24 20:24:58 +00002678 getLangOpts(), /*IsTypeSpec*/true);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002679 break;
2680
Douglas Gregor333489b2009-03-27 23:10:48 +00002681 // C++ typename-specifier:
2682 case tok::kw_typename:
John McCall1f476a12010-02-26 08:45:28 +00002683 if (TryAnnotateTypeOrScopeToken()) {
2684 DS.SetTypeSpecError();
2685 goto DoneWithDeclSpec;
2686 }
2687 if (!Tok.is(tok::kw_typename))
Douglas Gregor333489b2009-03-27 23:10:48 +00002688 continue;
2689 break;
2690
Chris Lattnere387d9e2009-01-21 19:48:37 +00002691 // GNU typeof support.
2692 case tok::kw_typeof:
2693 ParseTypeofSpecifier(DS);
2694 continue;
2695
David Blaikie15a430a2011-12-04 05:04:18 +00002696 case tok::annot_decltype:
Anders Carlsson74948d02009-06-24 17:47:40 +00002697 ParseDecltypeSpecifier(DS);
2698 continue;
2699
Alexis Hunt4a257072011-05-19 05:37:45 +00002700 case tok::kw___underlying_type:
2701 ParseUnderlyingTypeSpecifier(DS);
Eli Friedman0dfb8892011-10-06 23:00:33 +00002702 continue;
2703
2704 case tok::kw__Atomic:
2705 ParseAtomicSpecifier(DS);
2706 continue;
Alexis Hunt4a257072011-05-19 05:37:45 +00002707
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00002708 // OpenCL qualifiers:
Chad Rosierc1183952012-06-26 22:30:43 +00002709 case tok::kw_private:
David Blaikiebbafb8a2012-03-11 07:00:24 +00002710 if (!getLangOpts().OpenCL)
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00002711 goto DoneWithDeclSpec;
2712 case tok::kw___private:
2713 case tok::kw___global:
2714 case tok::kw___local:
2715 case tok::kw___constant:
2716 case tok::kw___read_only:
2717 case tok::kw___write_only:
2718 case tok::kw___read_write:
2719 ParseOpenCLQualifiers(DS);
2720 break;
Chad Rosierc1183952012-06-26 22:30:43 +00002721
Steve Naroffcfdf6162008-06-05 00:02:44 +00002722 case tok::less:
Chris Lattner16fac4f2008-07-26 01:18:38 +00002723 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattner0974b232008-07-26 00:20:22 +00002724 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
2725 // but we support it.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002726 if (DS.hasTypeSpecifier() || !getLangOpts().ObjC1)
Chris Lattner0974b232008-07-26 00:20:22 +00002727 goto DoneWithDeclSpec;
Mike Stump11289f42009-09-09 15:08:12 +00002728
Douglas Gregor3a001f42010-11-19 17:10:50 +00002729 if (!ParseObjCProtocolQualifiers(DS))
2730 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
2731 << FixItHint::CreateInsertion(Loc, "id")
2732 << SourceRange(Loc, DS.getSourceRange().getEnd());
Chad Rosierc1183952012-06-26 22:30:43 +00002733
Douglas Gregor06e41ae2010-10-21 23:17:00 +00002734 // Need to support trailing type qualifiers (e.g. "id<p> const").
2735 // If a type specifier follows, it will be diagnosed elsewhere.
2736 continue;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002737 }
John McCall49bfce42009-08-03 20:12:06 +00002738 // If the specifier wasn't legal, issue a diagnostic.
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002739 if (isInvalid) {
2740 assert(PrevSpec && "Method did not return previous specifier!");
John McCall49bfce42009-08-03 20:12:06 +00002741 assert(DiagID);
Chad Rosierc1183952012-06-26 22:30:43 +00002742
Douglas Gregora05f5ab2010-08-23 14:34:43 +00002743 if (DiagID == diag::ext_duplicate_declspec)
2744 Diag(Tok, DiagID)
2745 << PrevSpec << FixItHint::CreateRemoval(Tok.getLocation());
2746 else
2747 Diag(Tok, DiagID) << PrevSpec;
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002748 }
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00002749
Chris Lattner2e232092008-03-13 06:29:04 +00002750 DS.SetRangeEnd(Tok.getLocation());
Fariborz Jahanian2b059992011-04-19 21:42:37 +00002751 if (DiagID != diag::err_bool_redeclaration)
2752 ConsumeToken();
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00002753
2754 AttrsLastTime = false;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002755 }
2756}
Douglas Gregoreb31f392008-12-01 23:54:00 +00002757
Chris Lattner70ae4912007-10-29 04:42:53 +00002758/// ParseStructDeclaration - Parse a struct declaration without the terminating
2759/// semicolon.
2760///
Chris Lattner90a26b02007-01-23 04:38:16 +00002761/// struct-declaration:
Chris Lattner70ae4912007-10-29 04:42:53 +00002762/// specifier-qualifier-list struct-declarator-list
Chris Lattner736ed5d2007-06-09 05:59:07 +00002763/// [GNU] __extension__ struct-declaration
Chris Lattner70ae4912007-10-29 04:42:53 +00002764/// [GNU] specifier-qualifier-list
Chris Lattner90a26b02007-01-23 04:38:16 +00002765/// struct-declarator-list:
2766/// struct-declarator
2767/// struct-declarator-list ',' struct-declarator
2768/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
2769/// struct-declarator:
2770/// declarator
2771/// [GNU] declarator attributes[opt]
2772/// declarator[opt] ':' constant-expression
2773/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
2774///
Chris Lattnera12405b2008-04-10 06:46:29 +00002775void Parser::
Eli Friedman89b1f2c2012-08-08 23:04:35 +00002776ParseStructDeclaration(ParsingDeclSpec &DS, FieldCallback &Fields) {
Chad Rosierc1183952012-06-26 22:30:43 +00002777
Chris Lattnerf02ef3e2008-10-20 06:45:43 +00002778 if (Tok.is(tok::kw___extension__)) {
2779 // __extension__ silences extension warnings in the subexpression.
2780 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroff97170802007-08-20 22:28:22 +00002781 ConsumeToken();
Chris Lattnerf02ef3e2008-10-20 06:45:43 +00002782 return ParseStructDeclaration(DS, Fields);
2783 }
Mike Stump11289f42009-09-09 15:08:12 +00002784
Steve Naroff97170802007-08-20 22:28:22 +00002785 // Parse the common specifier-qualifiers-list piece.
Steve Naroff97170802007-08-20 22:28:22 +00002786 ParseSpecifierQualifierList(DS);
Mike Stump11289f42009-09-09 15:08:12 +00002787
Douglas Gregorc6f58fe2009-01-12 22:49:06 +00002788 // If there are no declarators, this is a free-standing declaration
2789 // specifier. Let the actions module cope with it.
Chris Lattner76c72282007-10-09 17:33:22 +00002790 if (Tok.is(tok::semi)) {
Eli Friedman89b1f2c2012-08-08 23:04:35 +00002791 Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none,
2792 DS);
2793 DS.complete(TheDecl);
Steve Naroff97170802007-08-20 22:28:22 +00002794 return;
2795 }
2796
2797 // Read struct-declarators until we find the semicolon.
John McCallcfefb6d2009-11-03 02:38:08 +00002798 bool FirstDeclarator = true;
Richard Smith8d06f422012-01-12 23:53:29 +00002799 SourceLocation CommaLoc;
Steve Naroff97170802007-08-20 22:28:22 +00002800 while (1) {
Eli Friedman89b1f2c2012-08-08 23:04:35 +00002801 ParsingFieldDeclarator DeclaratorInfo(*this, DS);
Richard Smith8d06f422012-01-12 23:53:29 +00002802 DeclaratorInfo.D.setCommaLoc(CommaLoc);
John McCallcfefb6d2009-11-03 02:38:08 +00002803
2804 // Attributes are only allowed here on successive declarators.
John McCall53fa7142010-12-24 02:08:15 +00002805 if (!FirstDeclarator)
2806 MaybeParseGNUAttributes(DeclaratorInfo.D);
Mike Stump11289f42009-09-09 15:08:12 +00002807
Steve Naroff97170802007-08-20 22:28:22 +00002808 /// struct-declarator: declarator
2809 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattner17c3b1f2009-12-10 01:59:24 +00002810 if (Tok.isNot(tok::colon)) {
2811 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
2812 ColonProtectionRAIIObject X(*this);
Chris Lattnera12405b2008-04-10 06:46:29 +00002813 ParseDeclarator(DeclaratorInfo.D);
Chris Lattner17c3b1f2009-12-10 01:59:24 +00002814 }
Mike Stump11289f42009-09-09 15:08:12 +00002815
Chris Lattner76c72282007-10-09 17:33:22 +00002816 if (Tok.is(tok::colon)) {
Steve Naroff97170802007-08-20 22:28:22 +00002817 ConsumeToken();
John McCalldadc5752010-08-24 06:29:42 +00002818 ExprResult Res(ParseConstantExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002819 if (Res.isInvalid())
Steve Naroff97170802007-08-20 22:28:22 +00002820 SkipUntil(tok::semi, true, true);
Chris Lattner32295d32008-04-10 06:15:14 +00002821 else
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00002822 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroff97170802007-08-20 22:28:22 +00002823 }
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002824
Steve Naroff97170802007-08-20 22:28:22 +00002825 // If attributes exist after the declarator, parse them.
John McCall53fa7142010-12-24 02:08:15 +00002826 MaybeParseGNUAttributes(DeclaratorInfo.D);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002827
John McCallcfefb6d2009-11-03 02:38:08 +00002828 // We're done with this declarator; invoke the callback.
Eli Friedmanba01f2b2012-08-08 23:35:12 +00002829 Fields.invoke(DeclaratorInfo);
John McCallcfefb6d2009-11-03 02:38:08 +00002830
Steve Naroff97170802007-08-20 22:28:22 +00002831 // If we don't have a comma, it is either the end of the list (a ';')
2832 // or an error, bail out.
Chris Lattner76c72282007-10-09 17:33:22 +00002833 if (Tok.isNot(tok::comma))
Chris Lattner70ae4912007-10-29 04:42:53 +00002834 return;
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002835
Steve Naroff97170802007-08-20 22:28:22 +00002836 // Consume the comma.
Richard Smith8d06f422012-01-12 23:53:29 +00002837 CommaLoc = ConsumeToken();
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002838
John McCallcfefb6d2009-11-03 02:38:08 +00002839 FirstDeclarator = false;
Steve Naroff97170802007-08-20 22:28:22 +00002840 }
Steve Naroff97170802007-08-20 22:28:22 +00002841}
2842
2843/// ParseStructUnionBody
2844/// struct-contents:
2845/// struct-declaration-list
2846/// [EXT] empty
2847/// [GNU] "struct-declaration-list" without terminatoring ';'
2848/// struct-declaration-list:
2849/// struct-declaration
2850/// struct-declaration-list struct-declaration
Chris Lattner535b8302008-06-21 19:39:06 +00002851/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroff97170802007-08-20 22:28:22 +00002852///
Chris Lattner1300fb92007-01-23 23:42:53 +00002853void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
John McCall48871652010-08-21 09:40:31 +00002854 unsigned TagType, Decl *TagDecl) {
John McCallfaf5fb42010-08-26 23:41:50 +00002855 PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc,
2856 "parsing struct/union body");
Mike Stump11289f42009-09-09 15:08:12 +00002857
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002858 BalancedDelimiterTracker T(*this, tok::l_brace);
2859 if (T.consumeOpen())
2860 return;
Mike Stump11289f42009-09-09 15:08:12 +00002861
Douglas Gregor658b9552009-01-09 22:42:13 +00002862 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
Douglas Gregor0be31a22010-07-02 17:43:08 +00002863 Actions.ActOnTagStartDefinition(getCurScope(), TagDecl);
Douglas Gregor82ac25e2009-01-08 20:45:30 +00002864
Chris Lattner7b9ace62007-01-23 20:11:08 +00002865 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
2866 // C++.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002867 if (Tok.is(tok::r_brace) && !getLangOpts().CPlusPlus) {
Richard Smithe4345902011-12-29 21:57:33 +00002868 Diag(Tok, diag::ext_empty_struct_union) << (TagType == TST_union);
2869 Diag(Tok, diag::warn_empty_struct_union_compat) << (TagType == TST_union);
2870 }
Chris Lattner7b9ace62007-01-23 20:11:08 +00002871
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002872 SmallVector<Decl *, 32> FieldDecls;
Chris Lattnera12405b2008-04-10 06:46:29 +00002873
Chris Lattner7b9ace62007-01-23 20:11:08 +00002874 // While we still have something to read, read the declarations in the struct.
Chris Lattner76c72282007-10-09 17:33:22 +00002875 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Chris Lattner90a26b02007-01-23 04:38:16 +00002876 // Each iteration of this loop reads one struct-declaration.
Mike Stump11289f42009-09-09 15:08:12 +00002877
Chris Lattner736ed5d2007-06-09 05:59:07 +00002878 // Check for extraneous top-level semicolon.
Chris Lattner76c72282007-10-09 17:33:22 +00002879 if (Tok.is(tok::semi)) {
Richard Smith87f5dc52012-07-23 05:45:25 +00002880 ConsumeExtraSemi(InsideStruct, TagType);
Chris Lattner36e46a22007-06-09 05:49:55 +00002881 continue;
2882 }
Chris Lattnera12405b2008-04-10 06:46:29 +00002883
John McCallcfefb6d2009-11-03 02:38:08 +00002884 if (!Tok.is(tok::at)) {
2885 struct CFieldCallback : FieldCallback {
2886 Parser &P;
John McCall48871652010-08-21 09:40:31 +00002887 Decl *TagDecl;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002888 SmallVectorImpl<Decl *> &FieldDecls;
John McCallcfefb6d2009-11-03 02:38:08 +00002889
John McCall48871652010-08-21 09:40:31 +00002890 CFieldCallback(Parser &P, Decl *TagDecl,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002891 SmallVectorImpl<Decl *> &FieldDecls) :
John McCallcfefb6d2009-11-03 02:38:08 +00002892 P(P), TagDecl(TagDecl), FieldDecls(FieldDecls) {}
2893
Eli Friedman934dbbf2012-08-08 23:53:27 +00002894 void invoke(ParsingFieldDeclarator &FD) {
John McCallcfefb6d2009-11-03 02:38:08 +00002895 // Install the declarator into the current TagDecl.
John McCall48871652010-08-21 09:40:31 +00002896 Decl *Field = P.Actions.ActOnField(P.getCurScope(), TagDecl,
John McCall5e6253b2009-11-03 21:13:47 +00002897 FD.D.getDeclSpec().getSourceRange().getBegin(),
2898 FD.D, FD.BitfieldSize);
John McCallcfefb6d2009-11-03 02:38:08 +00002899 FieldDecls.push_back(Field);
Eli Friedman89b1f2c2012-08-08 23:04:35 +00002900 FD.complete(Field);
Douglas Gregor66a985d2009-08-26 14:27:30 +00002901 }
John McCallcfefb6d2009-11-03 02:38:08 +00002902 } Callback(*this, TagDecl, FieldDecls);
2903
Eli Friedman89b1f2c2012-08-08 23:04:35 +00002904 // Parse all the comma separated declarators.
2905 ParsingDeclSpec DS(*this);
John McCallcfefb6d2009-11-03 02:38:08 +00002906 ParseStructDeclaration(DS, Callback);
Chris Lattner535b8302008-06-21 19:39:06 +00002907 } else { // Handle @defs
2908 ConsumeToken();
2909 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
2910 Diag(Tok, diag::err_unexpected_at);
Chris Lattner245c5332010-02-02 00:37:27 +00002911 SkipUntil(tok::semi, true);
Chris Lattner535b8302008-06-21 19:39:06 +00002912 continue;
2913 }
2914 ConsumeToken();
2915 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
2916 if (!Tok.is(tok::identifier)) {
2917 Diag(Tok, diag::err_expected_ident);
Chris Lattner245c5332010-02-02 00:37:27 +00002918 SkipUntil(tok::semi, true);
Chris Lattner535b8302008-06-21 19:39:06 +00002919 continue;
2920 }
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002921 SmallVector<Decl *, 16> Fields;
Douglas Gregor0be31a22010-07-02 17:43:08 +00002922 Actions.ActOnDefs(getCurScope(), TagDecl, Tok.getLocation(),
Douglas Gregor91f84212008-12-11 16:49:14 +00002923 Tok.getIdentifierInfo(), Fields);
Chris Lattner535b8302008-06-21 19:39:06 +00002924 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
2925 ConsumeToken();
2926 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
Mike Stump11289f42009-09-09 15:08:12 +00002927 }
Chris Lattner736ed5d2007-06-09 05:59:07 +00002928
Chris Lattner76c72282007-10-09 17:33:22 +00002929 if (Tok.is(tok::semi)) {
Chris Lattner90a26b02007-01-23 04:38:16 +00002930 ConsumeToken();
Chris Lattner76c72282007-10-09 17:33:22 +00002931 } else if (Tok.is(tok::r_brace)) {
Chris Lattner245c5332010-02-02 00:37:27 +00002932 ExpectAndConsume(tok::semi, diag::ext_expected_semi_decl_list);
Chris Lattner0c7e82d2007-06-09 05:54:40 +00002933 break;
Chris Lattner90a26b02007-01-23 04:38:16 +00002934 } else {
Chris Lattner245c5332010-02-02 00:37:27 +00002935 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
2936 // Skip to end of block or statement to avoid ext-warning on extra ';'.
Chris Lattner90a26b02007-01-23 04:38:16 +00002937 SkipUntil(tok::r_brace, true, true);
Chris Lattner245c5332010-02-02 00:37:27 +00002938 // If we stopped at a ';', eat it.
2939 if (Tok.is(tok::semi)) ConsumeToken();
Chris Lattner90a26b02007-01-23 04:38:16 +00002940 }
2941 }
Mike Stump11289f42009-09-09 15:08:12 +00002942
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002943 T.consumeClose();
Mike Stump11289f42009-09-09 15:08:12 +00002944
John McCall084e83d2011-03-24 11:26:52 +00002945 ParsedAttributes attrs(AttrFactory);
Chris Lattner90a26b02007-01-23 04:38:16 +00002946 // If attributes exist after struct contents, parse them.
John McCall53fa7142010-12-24 02:08:15 +00002947 MaybeParseGNUAttributes(attrs);
Daniel Dunbar15619c72008-10-03 02:03:53 +00002948
Douglas Gregor0be31a22010-07-02 17:43:08 +00002949 Actions.ActOnFields(getCurScope(),
David Blaikie751c5582011-09-22 02:58:26 +00002950 RecordLoc, TagDecl, FieldDecls,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002951 T.getOpenLocation(), T.getCloseLocation(),
John McCall53fa7142010-12-24 02:08:15 +00002952 attrs.getList());
Douglas Gregor82ac25e2009-01-08 20:45:30 +00002953 StructScope.Exit();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002954 Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl,
2955 T.getCloseLocation());
Chris Lattner90a26b02007-01-23 04:38:16 +00002956}
2957
Chris Lattner3b561a32006-08-13 00:12:11 +00002958/// ParseEnumSpecifier
Chris Lattner1890ac82006-08-13 01:16:23 +00002959/// enum-specifier: [C99 6.7.2.2]
Chris Lattner3b561a32006-08-13 00:12:11 +00002960/// 'enum' identifier[opt] '{' enumerator-list '}'
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002961///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Chris Lattnere37e2332006-08-15 04:50:22 +00002962/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
2963/// '}' attributes[opt]
Aaron Ballman9ecff022012-03-01 04:09:28 +00002964/// [MS] 'enum' __declspec[opt] identifier[opt] '{' enumerator-list ',' [opt]
2965/// '}'
Chris Lattner3b561a32006-08-13 00:12:11 +00002966/// 'enum' identifier
Chris Lattnere37e2332006-08-15 04:50:22 +00002967/// [GNU] 'enum' attributes[opt] identifier
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002968///
Richard Smith7d137e32012-03-23 03:33:32 +00002969/// [C++11] enum-head '{' enumerator-list[opt] '}'
2970/// [C++11] enum-head '{' enumerator-list ',' '}'
Douglas Gregor0bf31402010-10-08 23:50:27 +00002971///
Richard Smith7d137e32012-03-23 03:33:32 +00002972/// enum-head: [C++11]
2973/// enum-key attribute-specifier-seq[opt] identifier[opt] enum-base[opt]
2974/// enum-key attribute-specifier-seq[opt] nested-name-specifier
2975/// identifier enum-base[opt]
Douglas Gregor0bf31402010-10-08 23:50:27 +00002976///
Richard Smith7d137e32012-03-23 03:33:32 +00002977/// enum-key: [C++11]
Douglas Gregor0bf31402010-10-08 23:50:27 +00002978/// 'enum'
2979/// 'enum' 'class'
2980/// 'enum' 'struct'
2981///
Richard Smith7d137e32012-03-23 03:33:32 +00002982/// enum-base: [C++11]
Douglas Gregor0bf31402010-10-08 23:50:27 +00002983/// ':' type-specifier-seq
2984///
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002985/// [C++] elaborated-type-specifier:
2986/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
2987///
Chris Lattnerffaa0e62009-04-12 21:49:30 +00002988void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregordc70c3a2010-03-02 17:53:14 +00002989 const ParsedTemplateInfo &TemplateInfo,
Richard Smithc5b05522012-03-12 07:56:15 +00002990 AccessSpecifier AS, DeclSpecContext DSC) {
Chris Lattnerffbc2712007-01-25 06:05:38 +00002991 // Parse the tag portion of this.
Douglas Gregorf45b0cf2009-09-18 15:37:17 +00002992 if (Tok.is(tok::code_completion)) {
2993 // Code completion for an enum name.
Douglas Gregor0be31a22010-07-02 17:43:08 +00002994 Actions.CodeCompleteTag(getCurScope(), DeclSpec::TST_enum);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002995 return cutOffParsing();
Douglas Gregorf45b0cf2009-09-18 15:37:17 +00002996 }
John McCallcb432fa2011-07-06 05:58:41 +00002997
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00002998 // If attributes exist after tag, parse them.
2999 ParsedAttributesWithRange attrs(AttrFactory);
3000 MaybeParseGNUAttributes(attrs);
3001 MaybeParseCXX0XAttributes(attrs);
3002
3003 // If declspecs exist after tag, parse them.
3004 while (Tok.is(tok::kw___declspec))
3005 ParseMicrosoftDeclSpec(attrs);
3006
Richard Smith0f8ee222012-01-10 01:33:14 +00003007 SourceLocation ScopedEnumKWLoc;
John McCallcb432fa2011-07-06 05:58:41 +00003008 bool IsScopedUsingClassTag = false;
3009
John McCallbeae29a2012-06-23 22:30:04 +00003010 // In C++11, recognize 'enum class' and 'enum struct'.
David Blaikiebbafb8a2012-03-11 07:00:24 +00003011 if (getLangOpts().CPlusPlus0x &&
John McCallcb432fa2011-07-06 05:58:41 +00003012 (Tok.is(tok::kw_class) || Tok.is(tok::kw_struct))) {
Richard Smith5d164bc2011-10-15 05:09:34 +00003013 Diag(Tok, diag::warn_cxx98_compat_scoped_enum);
John McCallcb432fa2011-07-06 05:58:41 +00003014 IsScopedUsingClassTag = Tok.is(tok::kw_class);
Richard Smith0f8ee222012-01-10 01:33:14 +00003015 ScopedEnumKWLoc = ConsumeToken();
Chad Rosierc1183952012-06-26 22:30:43 +00003016
John McCallbeae29a2012-06-23 22:30:04 +00003017 // Attributes are not allowed between these keywords. Diagnose,
3018 // but then just treat them like they appeared in the right place.
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003019 ProhibitAttributes(attrs);
John McCallbeae29a2012-06-23 22:30:04 +00003020
3021 // They are allowed afterwards, though.
3022 MaybeParseGNUAttributes(attrs);
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003023 MaybeParseCXX0XAttributes(attrs);
John McCallbeae29a2012-06-23 22:30:04 +00003024 while (Tok.is(tok::kw___declspec))
3025 ParseMicrosoftDeclSpec(attrs);
John McCallcb432fa2011-07-06 05:58:41 +00003026 }
Richard Smith7d137e32012-03-23 03:33:32 +00003027
John McCall6347b682012-05-07 06:16:58 +00003028 // C++11 [temp.explicit]p12:
3029 // The usual access controls do not apply to names used to specify
3030 // explicit instantiations.
3031 // We extend this to also cover explicit specializations. Note that
3032 // we don't suppress if this turns out to be an elaborated type
3033 // specifier.
3034 bool shouldDelayDiagsInTag =
3035 (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation ||
3036 TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization);
3037 SuppressAccessChecks diagsFromTag(*this, shouldDelayDiagsInTag);
Richard Smith7d137e32012-03-23 03:33:32 +00003038
Richard Smithbfdb1082012-03-12 08:56:40 +00003039 // Enum definitions should not be parsed in a trailing-return-type.
3040 bool AllowDeclaration = DSC != DSC_trailing;
3041
3042 bool AllowFixedUnderlyingType = AllowDeclaration &&
3043 (getLangOpts().CPlusPlus0x || getLangOpts().MicrosoftExt ||
3044 getLangOpts().ObjC2);
John McCallcb432fa2011-07-06 05:58:41 +00003045
Abramo Bagnarad7548482010-05-19 21:37:53 +00003046 CXXScopeSpec &SS = DS.getTypeSpecScope();
David Blaikiebbafb8a2012-03-11 07:00:24 +00003047 if (getLangOpts().CPlusPlus) {
John McCallcb432fa2011-07-06 05:58:41 +00003048 // "enum foo : bar;" is not a potential typo for "enum foo::bar;"
3049 // if a fixed underlying type is allowed.
3050 ColonProtectionRAIIObject X(*this, AllowFixedUnderlyingType);
Chad Rosierc1183952012-06-26 22:30:43 +00003051
3052 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
Douglas Gregordf593fb2011-11-07 17:33:42 +00003053 /*EnteringContext=*/false))
John McCall1f476a12010-02-26 08:45:28 +00003054 return;
3055
3056 if (SS.isSet() && Tok.isNot(tok::identifier)) {
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003057 Diag(Tok, diag::err_expected_ident);
3058 if (Tok.isNot(tok::l_brace)) {
3059 // Has no name and is not a definition.
3060 // Skip the rest of this declarator, up until the comma or semicolon.
3061 SkipUntil(tok::comma, true);
3062 return;
3063 }
3064 }
3065 }
Mike Stump11289f42009-09-09 15:08:12 +00003066
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00003067 // Must have either 'enum name' or 'enum {...}'.
Douglas Gregor6cd5ae42011-02-22 02:55:24 +00003068 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace) &&
Richard Smithbfdb1082012-03-12 08:56:40 +00003069 !(AllowFixedUnderlyingType && Tok.is(tok::colon))) {
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00003070 Diag(Tok, diag::err_expected_ident_lbrace);
Mike Stump11289f42009-09-09 15:08:12 +00003071
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00003072 // Skip the rest of this declarator, up until the comma or semicolon.
3073 SkipUntil(tok::comma, true);
Chris Lattner3b561a32006-08-13 00:12:11 +00003074 return;
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00003075 }
Mike Stump11289f42009-09-09 15:08:12 +00003076
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00003077 // If an identifier is present, consume and remember it.
3078 IdentifierInfo *Name = 0;
3079 SourceLocation NameLoc;
3080 if (Tok.is(tok::identifier)) {
3081 Name = Tok.getIdentifierInfo();
3082 NameLoc = ConsumeToken();
3083 }
Mike Stump11289f42009-09-09 15:08:12 +00003084
Richard Smith0f8ee222012-01-10 01:33:14 +00003085 if (!Name && ScopedEnumKWLoc.isValid()) {
Douglas Gregor0bf31402010-10-08 23:50:27 +00003086 // C++0x 7.2p2: The optional identifier shall not be omitted in the
3087 // declaration of a scoped enumeration.
3088 Diag(Tok, diag::err_scoped_enum_missing_identifier);
Richard Smith0f8ee222012-01-10 01:33:14 +00003089 ScopedEnumKWLoc = SourceLocation();
Abramo Bagnara0e05e242010-12-03 18:54:17 +00003090 IsScopedUsingClassTag = false;
Douglas Gregor0bf31402010-10-08 23:50:27 +00003091 }
3092
John McCall6347b682012-05-07 06:16:58 +00003093 // Okay, end the suppression area. We'll decide whether to emit the
3094 // diagnostics in a second.
3095 if (shouldDelayDiagsInTag)
3096 diagsFromTag.done();
Richard Smith7d137e32012-03-23 03:33:32 +00003097
Douglas Gregor0bf31402010-10-08 23:50:27 +00003098 TypeResult BaseType;
3099
Douglas Gregord1f69f62010-12-01 17:42:47 +00003100 // Parse the fixed underlying type.
Richard Smith200f47c2012-07-02 19:14:01 +00003101 bool CanBeBitfield = getCurScope()->getFlags() & Scope::ClassScope;
Douglas Gregor6cd5ae42011-02-22 02:55:24 +00003102 if (AllowFixedUnderlyingType && Tok.is(tok::colon)) {
Douglas Gregord1f69f62010-12-01 17:42:47 +00003103 bool PossibleBitfield = false;
Richard Smith200f47c2012-07-02 19:14:01 +00003104 if (CanBeBitfield) {
Douglas Gregord1f69f62010-12-01 17:42:47 +00003105 // If we're in class scope, this can either be an enum declaration with
3106 // an underlying type, or a declaration of a bitfield member. We try to
3107 // use a simple disambiguation scheme first to catch the common cases
Chad Rosierc1183952012-06-26 22:30:43 +00003108 // (integer literal, sizeof); if it's still ambiguous, we then consider
3109 // anything that's a simple-type-specifier followed by '(' as an
3110 // expression. This suffices because function types are not valid
Douglas Gregord1f69f62010-12-01 17:42:47 +00003111 // underlying types anyway.
3112 TPResult TPR = isExpressionOrTypeSpecifierSimple(NextToken().getKind());
Chad Rosierc1183952012-06-26 22:30:43 +00003113 // If the next token starts an expression, we know we're parsing a
Douglas Gregord1f69f62010-12-01 17:42:47 +00003114 // bit-field. This is the common case.
3115 if (TPR == TPResult::True())
3116 PossibleBitfield = true;
3117 // If the next token starts a type-specifier-seq, it may be either a
3118 // a fixed underlying type or the start of a function-style cast in C++;
Chad Rosierc1183952012-06-26 22:30:43 +00003119 // lookahead one more token to see if it's obvious that we have a
Douglas Gregord1f69f62010-12-01 17:42:47 +00003120 // fixed underlying type.
Chad Rosierc1183952012-06-26 22:30:43 +00003121 else if (TPR == TPResult::False() &&
Douglas Gregord1f69f62010-12-01 17:42:47 +00003122 GetLookAheadToken(2).getKind() == tok::semi) {
3123 // Consume the ':'.
3124 ConsumeToken();
3125 } else {
3126 // We have the start of a type-specifier-seq, so we have to perform
3127 // tentative parsing to determine whether we have an expression or a
3128 // type.
3129 TentativeParsingAction TPA(*this);
3130
3131 // Consume the ':'.
3132 ConsumeToken();
Richard Smith1e3b0f02012-02-23 01:36:12 +00003133
3134 // If we see a type specifier followed by an open-brace, we have an
3135 // ambiguity between an underlying type and a C++11 braced
3136 // function-style cast. Resolve this by always treating it as an
3137 // underlying type.
3138 // FIXME: The standard is not entirely clear on how to disambiguate in
3139 // this case.
David Blaikiebbafb8a2012-03-11 07:00:24 +00003140 if ((getLangOpts().CPlusPlus &&
Richard Smith1e3b0f02012-02-23 01:36:12 +00003141 isCXXDeclarationSpecifier(TPResult::True()) != TPResult::True()) ||
David Blaikiebbafb8a2012-03-11 07:00:24 +00003142 (!getLangOpts().CPlusPlus && !isDeclarationSpecifier(true))) {
Douglas Gregord1f69f62010-12-01 17:42:47 +00003143 // We'll parse this as a bitfield later.
3144 PossibleBitfield = true;
3145 TPA.Revert();
3146 } else {
3147 // We have a type-specifier-seq.
3148 TPA.Commit();
3149 }
3150 }
3151 } else {
3152 // Consume the ':'.
3153 ConsumeToken();
3154 }
3155
3156 if (!PossibleBitfield) {
3157 SourceRange Range;
3158 BaseType = ParseTypeName(&Range);
Chad Rosierc1183952012-06-26 22:30:43 +00003159
David Blaikiebbafb8a2012-03-11 07:00:24 +00003160 if (!getLangOpts().CPlusPlus0x && !getLangOpts().ObjC2)
Douglas Gregora1aec292011-02-22 20:32:04 +00003161 Diag(StartLoc, diag::ext_ms_enum_fixed_underlying_type)
3162 << Range;
David Blaikiebbafb8a2012-03-11 07:00:24 +00003163 if (getLangOpts().CPlusPlus0x)
Richard Smith5d164bc2011-10-15 05:09:34 +00003164 Diag(StartLoc, diag::warn_cxx98_compat_enum_fixed_underlying_type);
Douglas Gregord1f69f62010-12-01 17:42:47 +00003165 }
Douglas Gregor0bf31402010-10-08 23:50:27 +00003166 }
3167
Richard Smith0f8ee222012-01-10 01:33:14 +00003168 // There are four options here. If we have 'friend enum foo;' then this is a
3169 // friend declaration, and cannot have an accompanying definition. If we have
3170 // 'enum foo;', then this is a forward declaration. If we have
3171 // 'enum foo {...' then this is a definition. Otherwise we have something
3172 // like 'enum foo xyz', a reference.
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00003173 //
3174 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
3175 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
3176 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
3177 //
John McCallfaf5fb42010-08-26 23:41:50 +00003178 Sema::TagUseKind TUK;
John McCall6347b682012-05-07 06:16:58 +00003179 if (!AllowDeclaration) {
Richard Smithbfdb1082012-03-12 08:56:40 +00003180 TUK = Sema::TUK_Reference;
John McCall6347b682012-05-07 06:16:58 +00003181 } else if (Tok.is(tok::l_brace)) {
3182 if (DS.isFriendSpecified()) {
3183 Diag(Tok.getLocation(), diag::err_friend_decl_defines_type)
3184 << SourceRange(DS.getFriendSpecLoc());
3185 ConsumeBrace();
3186 SkipUntil(tok::r_brace);
3187 TUK = Sema::TUK_Friend;
3188 } else {
3189 TUK = Sema::TUK_Definition;
3190 }
Richard Smith369b9f92012-06-25 21:37:02 +00003191 } else if (DSC != DSC_type_specifier &&
3192 (Tok.is(tok::semi) ||
Richard Smith200f47c2012-07-02 19:14:01 +00003193 (Tok.isAtStartOfLine() &&
3194 !isValidAfterTypeSpecifier(CanBeBitfield)))) {
Richard Smith369b9f92012-06-25 21:37:02 +00003195 TUK = DS.isFriendSpecified() ? Sema::TUK_Friend : Sema::TUK_Declaration;
3196 if (Tok.isNot(tok::semi)) {
3197 // A semicolon was missing after this declaration. Diagnose and recover.
3198 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_tagdecl,
3199 "enum");
3200 PP.EnterToken(Tok);
3201 Tok.setKind(tok::semi);
3202 }
John McCall6347b682012-05-07 06:16:58 +00003203 } else {
John McCallfaf5fb42010-08-26 23:41:50 +00003204 TUK = Sema::TUK_Reference;
John McCall6347b682012-05-07 06:16:58 +00003205 }
3206
3207 // If this is an elaborated type specifier, and we delayed
3208 // diagnostics before, just merge them into the current pool.
3209 if (TUK == Sema::TUK_Reference && shouldDelayDiagsInTag) {
3210 diagsFromTag.redelay();
3211 }
Richard Smith7d137e32012-03-23 03:33:32 +00003212
3213 MultiTemplateParamsArg TParams;
Douglas Gregorcbbf3e32010-05-03 17:48:54 +00003214 if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate &&
John McCallfaf5fb42010-08-26 23:41:50 +00003215 TUK != Sema::TUK_Reference) {
Richard Smith7d137e32012-03-23 03:33:32 +00003216 if (!getLangOpts().CPlusPlus0x || !SS.isSet()) {
3217 // Skip the rest of this declarator, up until the comma or semicolon.
3218 Diag(Tok, diag::err_enum_template);
3219 SkipUntil(tok::comma, true);
3220 return;
3221 }
3222
3223 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) {
3224 // Enumerations can't be explicitly instantiated.
3225 DS.SetTypeSpecError();
3226 Diag(StartLoc, diag::err_explicit_instantiation_enum);
3227 return;
3228 }
3229
3230 assert(TemplateInfo.TemplateParams && "no template parameters");
3231 TParams = MultiTemplateParamsArg(TemplateInfo.TemplateParams->data(),
3232 TemplateInfo.TemplateParams->size());
Douglas Gregorcbbf3e32010-05-03 17:48:54 +00003233 }
Chad Rosierc1183952012-06-26 22:30:43 +00003234
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003235 if (TUK == Sema::TUK_Reference)
3236 ProhibitAttributes(attrs);
Richard Smith7d137e32012-03-23 03:33:32 +00003237
Douglas Gregor6cd5ae42011-02-22 02:55:24 +00003238 if (!Name && TUK != Sema::TUK_Definition) {
3239 Diag(Tok, diag::err_enumerator_unnamed_no_def);
Richard Smith7d137e32012-03-23 03:33:32 +00003240
Douglas Gregor6cd5ae42011-02-22 02:55:24 +00003241 // Skip the rest of this declarator, up until the comma or semicolon.
3242 SkipUntil(tok::comma, true);
3243 return;
3244 }
Richard Smith7d137e32012-03-23 03:33:32 +00003245
Douglas Gregord6ab8742009-05-28 23:31:59 +00003246 bool Owned = false;
John McCall7f41d982009-09-11 04:59:25 +00003247 bool IsDependent = false;
Douglas Gregorba41d012010-04-24 16:38:41 +00003248 const char *PrevSpec = 0;
3249 unsigned DiagID;
John McCall48871652010-08-21 09:40:31 +00003250 Decl *TagDecl = Actions.ActOnTag(getCurScope(), DeclSpec::TST_enum, TUK,
John McCall53fa7142010-12-24 02:08:15 +00003251 StartLoc, SS, Name, NameLoc, attrs.getList(),
Richard Smith7d137e32012-03-23 03:33:32 +00003252 AS, DS.getModulePrivateSpecLoc(), TParams,
Richard Smith0f8ee222012-01-10 01:33:14 +00003253 Owned, IsDependent, ScopedEnumKWLoc,
Abramo Bagnara0e05e242010-12-03 18:54:17 +00003254 IsScopedUsingClassTag, BaseType);
Douglas Gregor0bf31402010-10-08 23:50:27 +00003255
Douglas Gregorba41d012010-04-24 16:38:41 +00003256 if (IsDependent) {
Chad Rosierc1183952012-06-26 22:30:43 +00003257 // This enum has a dependent nested-name-specifier. Handle it as a
Douglas Gregorba41d012010-04-24 16:38:41 +00003258 // dependent tag.
3259 if (!Name) {
3260 DS.SetTypeSpecError();
3261 Diag(Tok, diag::err_expected_type_name_after_typename);
3262 return;
3263 }
Chad Rosierc1183952012-06-26 22:30:43 +00003264
Douglas Gregor0be31a22010-07-02 17:43:08 +00003265 TypeResult Type = Actions.ActOnDependentTag(getCurScope(), DeclSpec::TST_enum,
Chad Rosierc1183952012-06-26 22:30:43 +00003266 TUK, SS, Name, StartLoc,
Douglas Gregorba41d012010-04-24 16:38:41 +00003267 NameLoc);
3268 if (Type.isInvalid()) {
3269 DS.SetTypeSpecError();
3270 return;
3271 }
Chad Rosierc1183952012-06-26 22:30:43 +00003272
Abramo Bagnara9875a3c2011-03-16 20:16:18 +00003273 if (DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc,
3274 NameLoc.isValid() ? NameLoc : StartLoc,
3275 PrevSpec, DiagID, Type.get()))
Douglas Gregorba41d012010-04-24 16:38:41 +00003276 Diag(StartLoc, DiagID) << PrevSpec;
Chad Rosierc1183952012-06-26 22:30:43 +00003277
Douglas Gregorba41d012010-04-24 16:38:41 +00003278 return;
3279 }
Mike Stump11289f42009-09-09 15:08:12 +00003280
John McCall48871652010-08-21 09:40:31 +00003281 if (!TagDecl) {
Chad Rosierc1183952012-06-26 22:30:43 +00003282 // The action failed to produce an enumeration tag. If this is a
Douglas Gregorba41d012010-04-24 16:38:41 +00003283 // definition, consume the entire definition.
Richard Smithbfdb1082012-03-12 08:56:40 +00003284 if (Tok.is(tok::l_brace) && TUK != Sema::TUK_Reference) {
Douglas Gregorba41d012010-04-24 16:38:41 +00003285 ConsumeBrace();
3286 SkipUntil(tok::r_brace);
3287 }
Chad Rosierc1183952012-06-26 22:30:43 +00003288
Douglas Gregorba41d012010-04-24 16:38:41 +00003289 DS.SetTypeSpecError();
3290 return;
3291 }
Richard Smith0f8ee222012-01-10 01:33:14 +00003292
Richard Smith369b9f92012-06-25 21:37:02 +00003293 if (Tok.is(tok::l_brace) && TUK != Sema::TUK_Reference)
John McCall6347b682012-05-07 06:16:58 +00003294 ParseEnumBody(StartLoc, TagDecl);
Mike Stump11289f42009-09-09 15:08:12 +00003295
Abramo Bagnara9875a3c2011-03-16 20:16:18 +00003296 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc,
3297 NameLoc.isValid() ? NameLoc : StartLoc,
3298 PrevSpec, DiagID, TagDecl, Owned))
John McCall49bfce42009-08-03 20:12:06 +00003299 Diag(StartLoc, DiagID) << PrevSpec;
Chris Lattner3b561a32006-08-13 00:12:11 +00003300}
3301
Chris Lattnerc1915e22007-01-25 07:29:02 +00003302/// ParseEnumBody - Parse a {} enclosed enumerator-list.
3303/// enumerator-list:
3304/// enumerator
3305/// enumerator-list ',' enumerator
3306/// enumerator:
3307/// enumeration-constant
3308/// enumeration-constant '=' constant-expression
3309/// enumeration-constant:
3310/// identifier
3311///
John McCall48871652010-08-21 09:40:31 +00003312void Parser::ParseEnumBody(SourceLocation StartLoc, Decl *EnumDecl) {
Douglas Gregor07665a62009-01-05 19:45:36 +00003313 // Enter the scope of the enum body and start the definition.
3314 ParseScope EnumScope(this, Scope::DeclScope);
Douglas Gregor0be31a22010-07-02 17:43:08 +00003315 Actions.ActOnTagStartDefinition(getCurScope(), EnumDecl);
Douglas Gregor07665a62009-01-05 19:45:36 +00003316
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003317 BalancedDelimiterTracker T(*this, tok::l_brace);
3318 T.consumeOpen();
Mike Stump11289f42009-09-09 15:08:12 +00003319
Chris Lattner37256fb2007-08-27 17:24:30 +00003320 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
David Blaikiebbafb8a2012-03-11 07:00:24 +00003321 if (Tok.is(tok::r_brace) && !getLangOpts().CPlusPlus)
Fariborz Jahanian6e814922010-05-28 22:23:22 +00003322 Diag(Tok, diag::error_empty_enum);
Mike Stump11289f42009-09-09 15:08:12 +00003323
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003324 SmallVector<Decl *, 32> EnumConstantDecls;
Chris Lattnerc1915e22007-01-25 07:29:02 +00003325
John McCall48871652010-08-21 09:40:31 +00003326 Decl *LastEnumConstDecl = 0;
Mike Stump11289f42009-09-09 15:08:12 +00003327
Chris Lattnerc1915e22007-01-25 07:29:02 +00003328 // Parse the enumerator-list.
Chris Lattner76c72282007-10-09 17:33:22 +00003329 while (Tok.is(tok::identifier)) {
Chris Lattnerc1915e22007-01-25 07:29:02 +00003330 IdentifierInfo *Ident = Tok.getIdentifierInfo();
3331 SourceLocation IdentLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00003332
John McCall811a0f52010-10-22 23:36:17 +00003333 // If attributes exist after the enumerator, parse them.
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003334 ParsedAttributesWithRange attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00003335 MaybeParseGNUAttributes(attrs);
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003336 MaybeParseCXX0XAttributes(attrs);
3337 ProhibitAttributes(attrs);
John McCall811a0f52010-10-22 23:36:17 +00003338
Chris Lattnerc1915e22007-01-25 07:29:02 +00003339 SourceLocation EqualLoc;
John McCalldadc5752010-08-24 06:29:42 +00003340 ExprResult AssignedVal;
John McCall2ec85372012-05-07 06:16:41 +00003341 ParsingDeclRAIIObject PD(*this, ParsingDeclRAIIObject::NoParent);
Chad Rosierc1183952012-06-26 22:30:43 +00003342
Chris Lattner76c72282007-10-09 17:33:22 +00003343 if (Tok.is(tok::equal)) {
Chris Lattnerc1915e22007-01-25 07:29:02 +00003344 EqualLoc = ConsumeToken();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00003345 AssignedVal = ParseConstantExpression();
3346 if (AssignedVal.isInvalid())
Chris Lattnerda6c2ce2007-04-27 19:13:15 +00003347 SkipUntil(tok::comma, tok::r_brace, true, true);
Chris Lattnerc1915e22007-01-25 07:29:02 +00003348 }
Mike Stump11289f42009-09-09 15:08:12 +00003349
Chris Lattnerc1915e22007-01-25 07:29:02 +00003350 // Install the enumerator constant into EnumDecl.
John McCall48871652010-08-21 09:40:31 +00003351 Decl *EnumConstDecl = Actions.ActOnEnumConstant(getCurScope(), EnumDecl,
3352 LastEnumConstDecl,
3353 IdentLoc, Ident,
John McCall53fa7142010-12-24 02:08:15 +00003354 attrs.getList(), EqualLoc,
John McCall48871652010-08-21 09:40:31 +00003355 AssignedVal.release());
Fariborz Jahanian329b3512011-12-09 01:15:54 +00003356 PD.complete(EnumConstDecl);
Chad Rosierc1183952012-06-26 22:30:43 +00003357
Chris Lattner4ef40012007-06-11 01:28:17 +00003358 EnumConstantDecls.push_back(EnumConstDecl);
3359 LastEnumConstDecl = EnumConstDecl;
Mike Stump11289f42009-09-09 15:08:12 +00003360
Douglas Gregorce66d022010-09-07 14:51:08 +00003361 if (Tok.is(tok::identifier)) {
3362 // We're missing a comma between enumerators.
3363 SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation);
Chad Rosierc1183952012-06-26 22:30:43 +00003364 Diag(Loc, diag::err_enumerator_list_missing_comma)
Douglas Gregorce66d022010-09-07 14:51:08 +00003365 << FixItHint::CreateInsertion(Loc, ", ");
3366 continue;
3367 }
Chad Rosierc1183952012-06-26 22:30:43 +00003368
Chris Lattner76c72282007-10-09 17:33:22 +00003369 if (Tok.isNot(tok::comma))
Chris Lattnerc1915e22007-01-25 07:29:02 +00003370 break;
3371 SourceLocation CommaLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00003372
Richard Smith5d164bc2011-10-15 05:09:34 +00003373 if (Tok.isNot(tok::identifier)) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00003374 if (!getLangOpts().C99 && !getLangOpts().CPlusPlus0x)
Richard Smith87f5dc52012-07-23 05:45:25 +00003375 Diag(CommaLoc, getLangOpts().CPlusPlus ?
3376 diag::ext_enumerator_list_comma_cxx :
3377 diag::ext_enumerator_list_comma_c)
Richard Smith5d164bc2011-10-15 05:09:34 +00003378 << FixItHint::CreateRemoval(CommaLoc);
David Blaikiebbafb8a2012-03-11 07:00:24 +00003379 else if (getLangOpts().CPlusPlus0x)
Richard Smith5d164bc2011-10-15 05:09:34 +00003380 Diag(CommaLoc, diag::warn_cxx98_compat_enumerator_list_comma)
3381 << FixItHint::CreateRemoval(CommaLoc);
3382 }
Chris Lattnerc1915e22007-01-25 07:29:02 +00003383 }
Mike Stump11289f42009-09-09 15:08:12 +00003384
Chris Lattnerc1915e22007-01-25 07:29:02 +00003385 // Eat the }.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003386 T.consumeClose();
Chris Lattnerc1915e22007-01-25 07:29:02 +00003387
Chris Lattnerc1915e22007-01-25 07:29:02 +00003388 // If attributes exist after the identifier list, parse them.
John McCall084e83d2011-03-24 11:26:52 +00003389 ParsedAttributes attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00003390 MaybeParseGNUAttributes(attrs);
Douglas Gregor82ac25e2009-01-08 20:45:30 +00003391
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003392 Actions.ActOnEnumBody(StartLoc, T.getOpenLocation(), T.getCloseLocation(),
3393 EnumDecl, EnumConstantDecls.data(),
3394 EnumConstantDecls.size(), getCurScope(),
3395 attrs.getList());
Mike Stump11289f42009-09-09 15:08:12 +00003396
Douglas Gregor82ac25e2009-01-08 20:45:30 +00003397 EnumScope.Exit();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003398 Actions.ActOnTagFinishDefinition(getCurScope(), EnumDecl,
3399 T.getCloseLocation());
Richard Smith369b9f92012-06-25 21:37:02 +00003400
3401 // The next token must be valid after an enum definition. If not, a ';'
3402 // was probably forgotten.
Richard Smith200f47c2012-07-02 19:14:01 +00003403 bool CanBeBitfield = getCurScope()->getFlags() & Scope::ClassScope;
3404 if (!isValidAfterTypeSpecifier(CanBeBitfield)) {
Richard Smith369b9f92012-06-25 21:37:02 +00003405 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_tagdecl, "enum");
3406 // Push this token back into the preprocessor and change our current token
3407 // to ';' so that the rest of the code recovers as though there were an
3408 // ';' after the definition.
3409 PP.EnterToken(Tok);
3410 Tok.setKind(tok::semi);
3411 }
Chris Lattnerc1915e22007-01-25 07:29:02 +00003412}
Chris Lattner3b561a32006-08-13 00:12:11 +00003413
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003414/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff69e8f9e2008-02-11 23:15:56 +00003415/// start of a type-qualifier-list.
3416bool Parser::isTypeQualifier() const {
3417 switch (Tok.getKind()) {
3418 default: return false;
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003419
3420 // type-qualifier only in OpenCL
3421 case tok::kw_private:
David Blaikiebbafb8a2012-03-11 07:00:24 +00003422 return getLangOpts().OpenCL;
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003423
Steve Naroff69e8f9e2008-02-11 23:15:56 +00003424 // type-qualifier
3425 case tok::kw_const:
3426 case tok::kw_volatile:
3427 case tok::kw_restrict:
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003428 case tok::kw___private:
3429 case tok::kw___local:
3430 case tok::kw___global:
3431 case tok::kw___constant:
3432 case tok::kw___read_only:
3433 case tok::kw___read_write:
3434 case tok::kw___write_only:
Steve Naroff69e8f9e2008-02-11 23:15:56 +00003435 return true;
3436 }
3437}
3438
Chris Lattnerfd48afe2010-02-28 18:18:36 +00003439/// isKnownToBeTypeSpecifier - Return true if we know that the specified token
3440/// is definitely a type-specifier. Return false if it isn't part of a type
3441/// specifier or if we're not sure.
3442bool Parser::isKnownToBeTypeSpecifier(const Token &Tok) const {
3443 switch (Tok.getKind()) {
3444 default: return false;
3445 // type-specifiers
3446 case tok::kw_short:
3447 case tok::kw_long:
Francois Pichet84133e42011-04-28 01:59:37 +00003448 case tok::kw___int64:
Richard Smithf016bbc2012-04-04 06:24:32 +00003449 case tok::kw___int128:
Chris Lattnerfd48afe2010-02-28 18:18:36 +00003450 case tok::kw_signed:
3451 case tok::kw_unsigned:
3452 case tok::kw__Complex:
3453 case tok::kw__Imaginary:
3454 case tok::kw_void:
3455 case tok::kw_char:
3456 case tok::kw_wchar_t:
3457 case tok::kw_char16_t:
3458 case tok::kw_char32_t:
3459 case tok::kw_int:
Anton Korobeynikovf0c267e2011-10-14 23:23:15 +00003460 case tok::kw_half:
Chris Lattnerfd48afe2010-02-28 18:18:36 +00003461 case tok::kw_float:
3462 case tok::kw_double:
3463 case tok::kw_bool:
3464 case tok::kw__Bool:
3465 case tok::kw__Decimal32:
3466 case tok::kw__Decimal64:
3467 case tok::kw__Decimal128:
3468 case tok::kw___vector:
Chad Rosierc1183952012-06-26 22:30:43 +00003469
Chris Lattnerfd48afe2010-02-28 18:18:36 +00003470 // struct-or-union-specifier (C99) or class-specifier (C++)
3471 case tok::kw_class:
3472 case tok::kw_struct:
3473 case tok::kw_union:
3474 // enum-specifier
3475 case tok::kw_enum:
Chad Rosierc1183952012-06-26 22:30:43 +00003476
Chris Lattnerfd48afe2010-02-28 18:18:36 +00003477 // typedef-name
3478 case tok::annot_typename:
3479 return true;
3480 }
3481}
3482
Steve Naroff69e8f9e2008-02-11 23:15:56 +00003483/// isTypeSpecifierQualifier - Return true if the current token could be the
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003484/// start of a specifier-qualifier-list.
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003485bool Parser::isTypeSpecifierQualifier() {
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003486 switch (Tok.getKind()) {
3487 default: return false;
Mike Stump11289f42009-09-09 15:08:12 +00003488
Chris Lattner020bab92009-01-04 23:41:41 +00003489 case tok::identifier: // foo::bar
John Thompson22334602010-02-05 00:12:22 +00003490 if (TryAltiVecVectorToken())
3491 return true;
3492 // Fall through.
Douglas Gregor333489b2009-03-27 23:10:48 +00003493 case tok::kw_typename: // typename T::type
Chris Lattner020bab92009-01-04 23:41:41 +00003494 // Annotate typenames and C++ scope specifiers. If we get one, just
3495 // recurse to handle whatever we get.
3496 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00003497 return true;
3498 if (Tok.is(tok::identifier))
3499 return false;
3500 return isTypeSpecifierQualifier();
Douglas Gregor333489b2009-03-27 23:10:48 +00003501
Chris Lattner020bab92009-01-04 23:41:41 +00003502 case tok::coloncolon: // ::foo::bar
3503 if (NextToken().is(tok::kw_new) || // ::new
3504 NextToken().is(tok::kw_delete)) // ::delete
3505 return false;
3506
Chris Lattner020bab92009-01-04 23:41:41 +00003507 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00003508 return true;
3509 return isTypeSpecifierQualifier();
Mike Stump11289f42009-09-09 15:08:12 +00003510
Chris Lattnere37e2332006-08-15 04:50:22 +00003511 // GNU attributes support.
3512 case tok::kw___attribute:
Steve Naroffad373bd2007-07-31 12:34:36 +00003513 // GNU typeof support.
3514 case tok::kw_typeof:
Mike Stump11289f42009-09-09 15:08:12 +00003515
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003516 // type-specifiers
3517 case tok::kw_short:
3518 case tok::kw_long:
Francois Pichet84133e42011-04-28 01:59:37 +00003519 case tok::kw___int64:
Richard Smithf016bbc2012-04-04 06:24:32 +00003520 case tok::kw___int128:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003521 case tok::kw_signed:
3522 case tok::kw_unsigned:
3523 case tok::kw__Complex:
3524 case tok::kw__Imaginary:
3525 case tok::kw_void:
3526 case tok::kw_char:
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +00003527 case tok::kw_wchar_t:
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00003528 case tok::kw_char16_t:
3529 case tok::kw_char32_t:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003530 case tok::kw_int:
Anton Korobeynikovf0c267e2011-10-14 23:23:15 +00003531 case tok::kw_half:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003532 case tok::kw_float:
3533 case tok::kw_double:
Chris Lattnerbb31a422007-11-15 05:25:19 +00003534 case tok::kw_bool:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003535 case tok::kw__Bool:
3536 case tok::kw__Decimal32:
3537 case tok::kw__Decimal64:
3538 case tok::kw__Decimal128:
John Thompson22334602010-02-05 00:12:22 +00003539 case tok::kw___vector:
Mike Stump11289f42009-09-09 15:08:12 +00003540
Chris Lattner861a2262008-04-13 18:59:07 +00003541 // struct-or-union-specifier (C99) or class-specifier (C++)
3542 case tok::kw_class:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003543 case tok::kw_struct:
3544 case tok::kw_union:
3545 // enum-specifier
3546 case tok::kw_enum:
Mike Stump11289f42009-09-09 15:08:12 +00003547
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003548 // type-qualifier
3549 case tok::kw_const:
3550 case tok::kw_volatile:
3551 case tok::kw_restrict:
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003552
3553 // typedef-name
Chris Lattnera8a3f732009-01-06 05:06:21 +00003554 case tok::annot_typename:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003555 return true;
Mike Stump11289f42009-09-09 15:08:12 +00003556
Chris Lattner409bf7d2008-10-20 00:25:30 +00003557 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
3558 case tok::less:
David Blaikiebbafb8a2012-03-11 07:00:24 +00003559 return getLangOpts().ObjC1;
Mike Stump11289f42009-09-09 15:08:12 +00003560
Steve Naroff44ac7772008-12-25 14:16:32 +00003561 case tok::kw___cdecl:
3562 case tok::kw___stdcall:
3563 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00003564 case tok::kw___thiscall:
Eli Friedman53339e02009-06-08 23:27:34 +00003565 case tok::kw___w64:
3566 case tok::kw___ptr64:
Francois Pichetf2fb4112011-08-25 00:36:46 +00003567 case tok::kw___ptr32:
Dawn Perchik335e16b2010-09-03 01:29:35 +00003568 case tok::kw___pascal:
Francois Pichet17ed0202011-08-18 09:59:55 +00003569 case tok::kw___unaligned:
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003570
3571 case tok::kw___private:
3572 case tok::kw___local:
3573 case tok::kw___global:
3574 case tok::kw___constant:
3575 case tok::kw___read_only:
3576 case tok::kw___read_write:
3577 case tok::kw___write_only:
3578
Eli Friedman53339e02009-06-08 23:27:34 +00003579 return true;
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003580
3581 case tok::kw_private:
David Blaikiebbafb8a2012-03-11 07:00:24 +00003582 return getLangOpts().OpenCL;
Eli Friedman0dfb8892011-10-06 23:00:33 +00003583
Benjamin Kramere56f3932011-12-23 17:00:35 +00003584 // C11 _Atomic()
Eli Friedman0dfb8892011-10-06 23:00:33 +00003585 case tok::kw__Atomic:
3586 return true;
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003587 }
3588}
3589
Chris Lattneracd58a32006-08-06 17:24:14 +00003590/// isDeclarationSpecifier() - Return true if the current token is part of a
3591/// declaration specifier.
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00003592///
3593/// \param DisambiguatingWithExpression True to indicate that the purpose of
3594/// this check is to disambiguate between an expression and a declaration.
3595bool Parser::isDeclarationSpecifier(bool DisambiguatingWithExpression) {
Chris Lattneracd58a32006-08-06 17:24:14 +00003596 switch (Tok.getKind()) {
3597 default: return false;
Mike Stump11289f42009-09-09 15:08:12 +00003598
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003599 case tok::kw_private:
David Blaikiebbafb8a2012-03-11 07:00:24 +00003600 return getLangOpts().OpenCL;
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003601
Chris Lattner020bab92009-01-04 23:41:41 +00003602 case tok::identifier: // foo::bar
Steve Naroff9527bbf2009-03-09 21:12:44 +00003603 // Unfortunate hack to support "Class.factoryMethod" notation.
David Blaikiebbafb8a2012-03-11 07:00:24 +00003604 if (getLangOpts().ObjC1 && NextToken().is(tok::period))
Steve Naroff9527bbf2009-03-09 21:12:44 +00003605 return false;
John Thompson22334602010-02-05 00:12:22 +00003606 if (TryAltiVecVectorToken())
3607 return true;
3608 // Fall through.
David Blaikie15a430a2011-12-04 05:04:18 +00003609 case tok::kw_decltype: // decltype(T())::type
Douglas Gregor333489b2009-03-27 23:10:48 +00003610 case tok::kw_typename: // typename T::type
Chris Lattner020bab92009-01-04 23:41:41 +00003611 // Annotate typenames and C++ scope specifiers. If we get one, just
3612 // recurse to handle whatever we get.
3613 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00003614 return true;
3615 if (Tok.is(tok::identifier))
3616 return false;
Chad Rosierc1183952012-06-26 22:30:43 +00003617
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00003618 // If we're in Objective-C and we have an Objective-C class type followed
Chad Rosierc1183952012-06-26 22:30:43 +00003619 // by an identifier and then either ':' or ']', in a place where an
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00003620 // expression is permitted, then this is probably a class message send
3621 // missing the initial '['. In this case, we won't consider this to be
3622 // the start of a declaration.
Chad Rosierc1183952012-06-26 22:30:43 +00003623 if (DisambiguatingWithExpression &&
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00003624 isStartOfObjCClassMessageMissingOpenBracket())
3625 return false;
Chad Rosierc1183952012-06-26 22:30:43 +00003626
John McCall1f476a12010-02-26 08:45:28 +00003627 return isDeclarationSpecifier();
3628
Chris Lattner020bab92009-01-04 23:41:41 +00003629 case tok::coloncolon: // ::foo::bar
3630 if (NextToken().is(tok::kw_new) || // ::new
3631 NextToken().is(tok::kw_delete)) // ::delete
3632 return false;
Mike Stump11289f42009-09-09 15:08:12 +00003633
Chris Lattner020bab92009-01-04 23:41:41 +00003634 // Annotate typenames and C++ scope specifiers. If we get one, just
3635 // recurse to handle whatever we get.
3636 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00003637 return true;
3638 return isDeclarationSpecifier();
Mike Stump11289f42009-09-09 15:08:12 +00003639
Chris Lattneracd58a32006-08-06 17:24:14 +00003640 // storage-class-specifier
3641 case tok::kw_typedef:
3642 case tok::kw_extern:
Steve Naroff2050b0d2007-12-18 00:16:02 +00003643 case tok::kw___private_extern__:
Chris Lattneracd58a32006-08-06 17:24:14 +00003644 case tok::kw_static:
3645 case tok::kw_auto:
3646 case tok::kw_register:
3647 case tok::kw___thread:
Mike Stump11289f42009-09-09 15:08:12 +00003648
Douglas Gregor26701a42011-09-09 02:06:17 +00003649 // Modules
3650 case tok::kw___module_private__:
Chad Rosierc1183952012-06-26 22:30:43 +00003651
Chris Lattneracd58a32006-08-06 17:24:14 +00003652 // type-specifiers
3653 case tok::kw_short:
3654 case tok::kw_long:
Francois Pichet84133e42011-04-28 01:59:37 +00003655 case tok::kw___int64:
Richard Smithf016bbc2012-04-04 06:24:32 +00003656 case tok::kw___int128:
Chris Lattneracd58a32006-08-06 17:24:14 +00003657 case tok::kw_signed:
3658 case tok::kw_unsigned:
3659 case tok::kw__Complex:
3660 case tok::kw__Imaginary:
3661 case tok::kw_void:
3662 case tok::kw_char:
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +00003663 case tok::kw_wchar_t:
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00003664 case tok::kw_char16_t:
3665 case tok::kw_char32_t:
3666
Chris Lattneracd58a32006-08-06 17:24:14 +00003667 case tok::kw_int:
Anton Korobeynikovf0c267e2011-10-14 23:23:15 +00003668 case tok::kw_half:
Chris Lattneracd58a32006-08-06 17:24:14 +00003669 case tok::kw_float:
3670 case tok::kw_double:
Chris Lattnerbb31a422007-11-15 05:25:19 +00003671 case tok::kw_bool:
Chris Lattneracd58a32006-08-06 17:24:14 +00003672 case tok::kw__Bool:
3673 case tok::kw__Decimal32:
3674 case tok::kw__Decimal64:
3675 case tok::kw__Decimal128:
John Thompson22334602010-02-05 00:12:22 +00003676 case tok::kw___vector:
Mike Stump11289f42009-09-09 15:08:12 +00003677
Chris Lattner861a2262008-04-13 18:59:07 +00003678 // struct-or-union-specifier (C99) or class-specifier (C++)
3679 case tok::kw_class:
Chris Lattneracd58a32006-08-06 17:24:14 +00003680 case tok::kw_struct:
3681 case tok::kw_union:
3682 // enum-specifier
3683 case tok::kw_enum:
Mike Stump11289f42009-09-09 15:08:12 +00003684
Chris Lattneracd58a32006-08-06 17:24:14 +00003685 // type-qualifier
3686 case tok::kw_const:
3687 case tok::kw_volatile:
3688 case tok::kw_restrict:
Steve Naroffad373bd2007-07-31 12:34:36 +00003689
Chris Lattneracd58a32006-08-06 17:24:14 +00003690 // function-specifier
3691 case tok::kw_inline:
Douglas Gregor61956c42008-10-31 09:07:45 +00003692 case tok::kw_virtual:
3693 case tok::kw_explicit:
Chris Lattner7b20dc72007-08-09 16:40:21 +00003694
Peter Collingbourne3d9cbdc2011-04-15 00:35:57 +00003695 // static_assert-declaration
3696 case tok::kw__Static_assert:
3697
Chris Lattner599e47e2007-08-09 17:01:07 +00003698 // GNU typeof support.
3699 case tok::kw_typeof:
Mike Stump11289f42009-09-09 15:08:12 +00003700
Chris Lattner599e47e2007-08-09 17:01:07 +00003701 // GNU attributes.
Chris Lattner7b20dc72007-08-09 16:40:21 +00003702 case tok::kw___attribute:
Chris Lattneracd58a32006-08-06 17:24:14 +00003703 return true;
Mike Stump11289f42009-09-09 15:08:12 +00003704
Francois Pichete878cb62011-06-19 08:02:06 +00003705 // C++0x decltype.
David Blaikie15a430a2011-12-04 05:04:18 +00003706 case tok::annot_decltype:
Francois Pichete878cb62011-06-19 08:02:06 +00003707 return true;
3708
Benjamin Kramere56f3932011-12-23 17:00:35 +00003709 // C11 _Atomic()
Eli Friedman0dfb8892011-10-06 23:00:33 +00003710 case tok::kw__Atomic:
3711 return true;
3712
Chris Lattner8b2ec162008-07-26 03:38:44 +00003713 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
3714 case tok::less:
David Blaikiebbafb8a2012-03-11 07:00:24 +00003715 return getLangOpts().ObjC1;
Mike Stump11289f42009-09-09 15:08:12 +00003716
Douglas Gregor19b7acf2011-04-27 05:41:15 +00003717 // typedef-name
3718 case tok::annot_typename:
3719 return !DisambiguatingWithExpression ||
3720 !isStartOfObjCClassMessageMissingOpenBracket();
Chad Rosierc1183952012-06-26 22:30:43 +00003721
Steve Narofff192fab2009-01-06 19:34:12 +00003722 case tok::kw___declspec:
Steve Naroff44ac7772008-12-25 14:16:32 +00003723 case tok::kw___cdecl:
3724 case tok::kw___stdcall:
3725 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00003726 case tok::kw___thiscall:
Eli Friedman53339e02009-06-08 23:27:34 +00003727 case tok::kw___w64:
3728 case tok::kw___ptr64:
Francois Pichetf2fb4112011-08-25 00:36:46 +00003729 case tok::kw___ptr32:
Eli Friedman53339e02009-06-08 23:27:34 +00003730 case tok::kw___forceinline:
Dawn Perchik335e16b2010-09-03 01:29:35 +00003731 case tok::kw___pascal:
Francois Pichet17ed0202011-08-18 09:59:55 +00003732 case tok::kw___unaligned:
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003733
3734 case tok::kw___private:
3735 case tok::kw___local:
3736 case tok::kw___global:
3737 case tok::kw___constant:
3738 case tok::kw___read_only:
3739 case tok::kw___read_write:
3740 case tok::kw___write_only:
3741
Eli Friedman53339e02009-06-08 23:27:34 +00003742 return true;
Chris Lattneracd58a32006-08-06 17:24:14 +00003743 }
3744}
3745
Douglas Gregor9de54ea2010-01-13 17:31:36 +00003746bool Parser::isConstructorDeclarator() {
3747 TentativeParsingAction TPA(*this);
3748
3749 // Parse the C++ scope specifier.
3750 CXXScopeSpec SS;
Chad Rosierc1183952012-06-26 22:30:43 +00003751 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
Douglas Gregordf593fb2011-11-07 17:33:42 +00003752 /*EnteringContext=*/true)) {
John McCall1f476a12010-02-26 08:45:28 +00003753 TPA.Revert();
3754 return false;
3755 }
Douglas Gregor9de54ea2010-01-13 17:31:36 +00003756
3757 // Parse the constructor name.
3758 if (Tok.is(tok::identifier) || Tok.is(tok::annot_template_id)) {
3759 // We already know that we have a constructor name; just consume
3760 // the token.
3761 ConsumeToken();
3762 } else {
3763 TPA.Revert();
3764 return false;
3765 }
3766
Richard Smith43f340f2012-03-27 23:05:05 +00003767 // Current class name must be followed by a left parenthesis.
Douglas Gregor9de54ea2010-01-13 17:31:36 +00003768 if (Tok.isNot(tok::l_paren)) {
3769 TPA.Revert();
3770 return false;
3771 }
3772 ConsumeParen();
3773
Richard Smith43f340f2012-03-27 23:05:05 +00003774 // A right parenthesis, or ellipsis followed by a right parenthesis signals
3775 // that we have a constructor.
3776 if (Tok.is(tok::r_paren) ||
3777 (Tok.is(tok::ellipsis) && NextToken().is(tok::r_paren))) {
Douglas Gregor9de54ea2010-01-13 17:31:36 +00003778 TPA.Revert();
3779 return true;
3780 }
3781
3782 // If we need to, enter the specified scope.
3783 DeclaratorScopeObj DeclScopeObj(*this, SS);
Douglas Gregor0be31a22010-07-02 17:43:08 +00003784 if (SS.isSet() && Actions.ShouldEnterDeclaratorScope(getCurScope(), SS))
Douglas Gregor9de54ea2010-01-13 17:31:36 +00003785 DeclScopeObj.EnterDeclaratorScope();
3786
Francois Pichet79f3a872011-01-31 04:54:32 +00003787 // Optionally skip Microsoft attributes.
John McCall084e83d2011-03-24 11:26:52 +00003788 ParsedAttributes Attrs(AttrFactory);
Francois Pichet79f3a872011-01-31 04:54:32 +00003789 MaybeParseMicrosoftAttributes(Attrs);
3790
Douglas Gregor9de54ea2010-01-13 17:31:36 +00003791 // Check whether the next token(s) are part of a declaration
3792 // specifier, in which case we have the start of a parameter and,
3793 // therefore, we know that this is a constructor.
Richard Smithefd009d2012-03-27 00:56:56 +00003794 bool IsConstructor = false;
3795 if (isDeclarationSpecifier())
3796 IsConstructor = true;
3797 else if (Tok.is(tok::identifier) ||
3798 (Tok.is(tok::annot_cxxscope) && NextToken().is(tok::identifier))) {
3799 // We've seen "C ( X" or "C ( X::Y", but "X" / "X::Y" is not a type.
3800 // This might be a parenthesized member name, but is more likely to
3801 // be a constructor declaration with an invalid argument type. Keep
3802 // looking.
3803 if (Tok.is(tok::annot_cxxscope))
3804 ConsumeToken();
3805 ConsumeToken();
3806
3807 // If this is not a constructor, we must be parsing a declarator,
Richard Smith1453e312012-03-27 01:42:32 +00003808 // which must have one of the following syntactic forms (see the
3809 // grammar extract at the start of ParseDirectDeclarator):
Richard Smithefd009d2012-03-27 00:56:56 +00003810 switch (Tok.getKind()) {
3811 case tok::l_paren:
3812 // C(X ( int));
3813 case tok::l_square:
3814 // C(X [ 5]);
3815 // C(X [ [attribute]]);
3816 case tok::coloncolon:
3817 // C(X :: Y);
3818 // C(X :: *p);
3819 case tok::r_paren:
3820 // C(X )
3821 // Assume this isn't a constructor, rather than assuming it's a
3822 // constructor with an unnamed parameter of an ill-formed type.
3823 break;
3824
3825 default:
3826 IsConstructor = true;
3827 break;
3828 }
3829 }
3830
Douglas Gregor9de54ea2010-01-13 17:31:36 +00003831 TPA.Revert();
3832 return IsConstructor;
3833}
Chris Lattnerb9093cd2006-08-04 04:39:53 +00003834
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003835/// ParseTypeQualifierListOpt
Dawn Perchik335e16b2010-09-03 01:29:35 +00003836/// type-qualifier-list: [C99 6.7.5]
3837/// type-qualifier
Chad Rosierc1183952012-06-26 22:30:43 +00003838/// [vendor] attributes
Dawn Perchik335e16b2010-09-03 01:29:35 +00003839/// [ only if VendorAttributesAllowed=true ]
3840/// type-qualifier-list type-qualifier
Chad Rosierc1183952012-06-26 22:30:43 +00003841/// [vendor] type-qualifier-list attributes
Dawn Perchik335e16b2010-09-03 01:29:35 +00003842/// [ only if VendorAttributesAllowed=true ]
3843/// [C++0x] attribute-specifier[opt] is allowed before cv-qualifier-seq
3844/// [ only if CXX0XAttributesAllowed=true ]
3845/// Note: vendor can be GNU, MS, etc.
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003846///
Dawn Perchik335e16b2010-09-03 01:29:35 +00003847void Parser::ParseTypeQualifierListOpt(DeclSpec &DS,
3848 bool VendorAttributesAllowed,
Richard Smith3dff2512012-04-10 03:25:07 +00003849 bool CXX11AttributesAllowed) {
3850 if (getLangOpts().CPlusPlus0x && CXX11AttributesAllowed &&
Richard Smith7bdcc4a2012-04-10 01:32:12 +00003851 isCXX11AttributeSpecifier()) {
John McCall084e83d2011-03-24 11:26:52 +00003852 ParsedAttributesWithRange attrs(AttrFactory);
Richard Smith3dff2512012-04-10 03:25:07 +00003853 ParseCXX11Attributes(attrs);
Richard Smith7bdcc4a2012-04-10 01:32:12 +00003854 DS.takeAttributesFrom(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +00003855 }
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +00003856
3857 SourceLocation EndLoc;
3858
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003859 while (1) {
John McCall49bfce42009-08-03 20:12:06 +00003860 bool isInvalid = false;
Chris Lattnerd9c3c592006-08-05 06:26:47 +00003861 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00003862 unsigned DiagID = 0;
Chris Lattner60809f52006-11-28 05:18:46 +00003863 SourceLocation Loc = Tok.getLocation();
Chris Lattnerd9c3c592006-08-05 06:26:47 +00003864
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003865 switch (Tok.getKind()) {
Douglas Gregor28c78432010-08-27 17:35:51 +00003866 case tok::code_completion:
3867 Actions.CodeCompleteTypeQualifiers(DS);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00003868 return cutOffParsing();
Chad Rosierc1183952012-06-26 22:30:43 +00003869
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003870 case tok::kw_const:
John McCall49bfce42009-08-03 20:12:06 +00003871 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, DiagID,
Richard Smith7ac3c6a2012-07-24 20:24:58 +00003872 getLangOpts(), /*IsTypeSpec*/false);
Chris Lattnerd9c3c592006-08-05 06:26:47 +00003873 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003874 case tok::kw_volatile:
John McCall49bfce42009-08-03 20:12:06 +00003875 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
Richard Smith7ac3c6a2012-07-24 20:24:58 +00003876 getLangOpts(), /*IsTypeSpec*/false);
Chris Lattnerd9c3c592006-08-05 06:26:47 +00003877 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003878 case tok::kw_restrict:
John McCall49bfce42009-08-03 20:12:06 +00003879 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
Richard Smith7ac3c6a2012-07-24 20:24:58 +00003880 getLangOpts(), /*IsTypeSpec*/false);
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003881 break;
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003882
3883 // OpenCL qualifiers:
Chad Rosierc1183952012-06-26 22:30:43 +00003884 case tok::kw_private:
David Blaikiebbafb8a2012-03-11 07:00:24 +00003885 if (!getLangOpts().OpenCL)
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003886 goto DoneWithTypeQuals;
3887 case tok::kw___private:
3888 case tok::kw___global:
3889 case tok::kw___local:
3890 case tok::kw___constant:
3891 case tok::kw___read_only:
3892 case tok::kw___write_only:
3893 case tok::kw___read_write:
3894 ParseOpenCLQualifiers(DS);
3895 break;
3896
Eli Friedman53339e02009-06-08 23:27:34 +00003897 case tok::kw___w64:
Steve Narofff9c29d42008-12-25 14:41:26 +00003898 case tok::kw___ptr64:
Francois Pichetf2fb4112011-08-25 00:36:46 +00003899 case tok::kw___ptr32:
Steve Naroff44ac7772008-12-25 14:16:32 +00003900 case tok::kw___cdecl:
3901 case tok::kw___stdcall:
3902 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00003903 case tok::kw___thiscall:
Francois Pichet17ed0202011-08-18 09:59:55 +00003904 case tok::kw___unaligned:
Dawn Perchik335e16b2010-09-03 01:29:35 +00003905 if (VendorAttributesAllowed) {
John McCall53fa7142010-12-24 02:08:15 +00003906 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman53339e02009-06-08 23:27:34 +00003907 continue;
3908 }
3909 goto DoneWithTypeQuals;
Dawn Perchik335e16b2010-09-03 01:29:35 +00003910 case tok::kw___pascal:
3911 if (VendorAttributesAllowed) {
John McCall53fa7142010-12-24 02:08:15 +00003912 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik335e16b2010-09-03 01:29:35 +00003913 continue;
3914 }
3915 goto DoneWithTypeQuals;
Chris Lattnere37e2332006-08-15 04:50:22 +00003916 case tok::kw___attribute:
Dawn Perchik335e16b2010-09-03 01:29:35 +00003917 if (VendorAttributesAllowed) {
John McCall53fa7142010-12-24 02:08:15 +00003918 ParseGNUAttributes(DS.getAttributes());
Chris Lattnercf0bab22008-12-18 07:02:59 +00003919 continue; // do *not* consume the next token!
3920 }
3921 // otherwise, FALL THROUGH!
3922 default:
Steve Naroff44ac7772008-12-25 14:16:32 +00003923 DoneWithTypeQuals:
Chris Lattnercf0bab22008-12-18 07:02:59 +00003924 // If this is not a type-qualifier token, we're done reading type
3925 // qualifiers. First verify that DeclSpec's are consistent.
Douglas Gregore3e01a22009-04-01 22:41:11 +00003926 DS.Finish(Diags, PP);
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +00003927 if (EndLoc.isValid())
3928 DS.SetRangeEnd(EndLoc);
Chris Lattnercf0bab22008-12-18 07:02:59 +00003929 return;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003930 }
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00003931
Chris Lattnerd9c3c592006-08-05 06:26:47 +00003932 // If the specifier combination wasn't legal, issue a diagnostic.
3933 if (isInvalid) {
3934 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner6d29c102008-11-18 07:48:38 +00003935 Diag(Tok, DiagID) << PrevSpec;
Chris Lattnerd9c3c592006-08-05 06:26:47 +00003936 }
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +00003937 EndLoc = ConsumeToken();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003938 }
3939}
3940
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00003941
3942/// ParseDeclarator - Parse and verify a newly-initialized declarator.
3943///
3944void Parser::ParseDeclarator(Declarator &D) {
3945 /// This implements the 'declarator' production in the C grammar, then checks
3946 /// for well-formedness and issues diagnostics.
Sebastian Redlbd150f42008-11-21 19:14:01 +00003947 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00003948}
3949
Richard Smith0efa75c2012-03-29 01:16:42 +00003950static bool isPtrOperatorToken(tok::TokenKind Kind, const LangOptions &Lang) {
3951 if (Kind == tok::star || Kind == tok::caret)
3952 return true;
3953
3954 // We parse rvalue refs in C++03, because otherwise the errors are scary.
3955 if (!Lang.CPlusPlus)
3956 return false;
3957
3958 return Kind == tok::amp || Kind == tok::ampamp;
3959}
3960
Sebastian Redlbd150f42008-11-21 19:14:01 +00003961/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
3962/// is parsed by the function passed to it. Pass null, and the direct-declarator
3963/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregordbc5daf2008-11-07 20:08:42 +00003964/// ptr-operator production.
3965///
Richard Smith09f76ee2011-10-19 21:33:05 +00003966/// If the grammar of this construct is extended, matching changes must also be
Richard Smith1453e312012-03-27 01:42:32 +00003967/// made to TryParseDeclarator and MightBeDeclarator, and possibly to
3968/// isConstructorDeclarator.
Richard Smith09f76ee2011-10-19 21:33:05 +00003969///
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00003970/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
3971/// [C] pointer[opt] direct-declarator
3972/// [C++] direct-declarator
3973/// [C++] ptr-operator declarator
Chris Lattner6c7416c2006-08-07 00:19:33 +00003974///
3975/// pointer: [C99 6.7.5]
3976/// '*' type-qualifier-list[opt]
3977/// '*' type-qualifier-list[opt] pointer
3978///
Douglas Gregordbc5daf2008-11-07 20:08:42 +00003979/// ptr-operator:
3980/// '*' cv-qualifier-seq[opt]
3981/// '&'
Sebastian Redled0f3b02009-03-15 22:02:01 +00003982/// [C++0x] '&&'
Douglas Gregordbc5daf2008-11-07 20:08:42 +00003983/// [GNU] '&' restrict[opt] attributes[opt]
Sebastian Redled0f3b02009-03-15 22:02:01 +00003984/// [GNU?] '&&' restrict[opt] attributes[opt]
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00003985/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Sebastian Redlbd150f42008-11-21 19:14:01 +00003986void Parser::ParseDeclaratorInternal(Declarator &D,
3987 DirectDeclParseFunction DirectDeclParser) {
Douglas Gregor66a985d2009-08-26 14:27:30 +00003988 if (Diags.hasAllExtensionsSilenced())
3989 D.setExtension();
Chad Rosierc1183952012-06-26 22:30:43 +00003990
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00003991 // C++ member pointers start with a '::' or a nested-name.
3992 // Member pointers get special handling, since there's no place for the
3993 // scope spec in the generic path below.
David Blaikiebbafb8a2012-03-11 07:00:24 +00003994 if (getLangOpts().CPlusPlus &&
Chris Lattner803802d2009-03-24 17:04:48 +00003995 (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) ||
3996 Tok.is(tok::annot_cxxscope))) {
Douglas Gregordf593fb2011-11-07 17:33:42 +00003997 bool EnteringContext = D.getContext() == Declarator::FileContext ||
3998 D.getContext() == Declarator::MemberContext;
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00003999 CXXScopeSpec SS;
Douglas Gregordf593fb2011-11-07 17:33:42 +00004000 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), EnteringContext);
John McCall1f476a12010-02-26 08:45:28 +00004001
Jeffrey Yasskin4e150f82010-04-07 23:29:58 +00004002 if (SS.isNotEmpty()) {
Mike Stump11289f42009-09-09 15:08:12 +00004003 if (Tok.isNot(tok::star)) {
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004004 // The scope spec really belongs to the direct-declarator.
4005 D.getCXXScopeSpec() = SS;
4006 if (DirectDeclParser)
4007 (this->*DirectDeclParser)(D);
4008 return;
4009 }
4010
4011 SourceLocation Loc = ConsumeToken();
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004012 D.SetRangeEnd(Loc);
John McCall084e83d2011-03-24 11:26:52 +00004013 DeclSpec DS(AttrFactory);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004014 ParseTypeQualifierListOpt(DS);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004015 D.ExtendWithDeclSpec(DS);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004016
4017 // Recurse to parse whatever is left.
4018 ParseDeclaratorInternal(D, DirectDeclParser);
4019
4020 // Sema will have to catch (syntactically invalid) pointers into global
4021 // scope. It has to catch pointers into namespace scope anyway.
4022 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
John McCall084e83d2011-03-24 11:26:52 +00004023 Loc),
4024 DS.getAttributes(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004025 /* Don't replace range end. */SourceLocation());
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004026 return;
4027 }
4028 }
4029
4030 tok::TokenKind Kind = Tok.getKind();
Steve Naroffec33ed92008-08-27 16:04:49 +00004031 // Not a pointer, C++ reference, or block.
Richard Smith0efa75c2012-03-29 01:16:42 +00004032 if (!isPtrOperatorToken(Kind, getLangOpts())) {
Sebastian Redlbd150f42008-11-21 19:14:01 +00004033 if (DirectDeclParser)
4034 (this->*DirectDeclParser)(D);
Douglas Gregordbc5daf2008-11-07 20:08:42 +00004035 return;
4036 }
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004037
Sebastian Redled0f3b02009-03-15 22:02:01 +00004038 // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference,
4039 // '&&' -> rvalue reference
Sebastian Redl3b27be62009-03-23 00:00:23 +00004040 SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&.
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004041 D.SetRangeEnd(Loc);
Bill Wendling3708c182007-05-27 10:15:43 +00004042
Chris Lattner9eac9312009-03-27 04:18:06 +00004043 if (Kind == tok::star || Kind == tok::caret) {
Chris Lattner788404f2008-02-21 01:32:26 +00004044 // Is a pointer.
John McCall084e83d2011-03-24 11:26:52 +00004045 DeclSpec DS(AttrFactory);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004046
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004047 // FIXME: GNU attributes are not allowed here in a new-type-id.
Bill Wendling3708c182007-05-27 10:15:43 +00004048 ParseTypeQualifierListOpt(DS);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004049 D.ExtendWithDeclSpec(DS);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004050
Bill Wendling3708c182007-05-27 10:15:43 +00004051 // Recursively parse the declarator.
Sebastian Redlbd150f42008-11-21 19:14:01 +00004052 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroffec33ed92008-08-27 16:04:49 +00004053 if (Kind == tok::star)
4054 // Remember that we parsed a pointer type, and remember the type-quals.
4055 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
Chandler Carruthe71b378d2011-02-23 18:51:59 +00004056 DS.getConstSpecLoc(),
4057 DS.getVolatileSpecLoc(),
John McCall084e83d2011-03-24 11:26:52 +00004058 DS.getRestrictSpecLoc()),
4059 DS.getAttributes(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004060 SourceLocation());
Steve Naroffec33ed92008-08-27 16:04:49 +00004061 else
4062 // Remember that we parsed a Block type, and remember the type-quals.
Mike Stump11289f42009-09-09 15:08:12 +00004063 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
John McCall084e83d2011-03-24 11:26:52 +00004064 Loc),
4065 DS.getAttributes(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004066 SourceLocation());
Bill Wendling3708c182007-05-27 10:15:43 +00004067 } else {
4068 // Is a reference
John McCall084e83d2011-03-24 11:26:52 +00004069 DeclSpec DS(AttrFactory);
Bill Wendling93efb222007-06-02 23:28:54 +00004070
Sebastian Redl3b27be62009-03-23 00:00:23 +00004071 // Complain about rvalue references in C++03, but then go on and build
4072 // the declarator.
Richard Smith5d164bc2011-10-15 05:09:34 +00004073 if (Kind == tok::ampamp)
David Blaikiebbafb8a2012-03-11 07:00:24 +00004074 Diag(Loc, getLangOpts().CPlusPlus0x ?
Richard Smith5d164bc2011-10-15 05:09:34 +00004075 diag::warn_cxx98_compat_rvalue_reference :
4076 diag::ext_rvalue_reference);
Sebastian Redl3b27be62009-03-23 00:00:23 +00004077
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004078 // GNU-style and C++11 attributes are allowed here, as is restrict.
4079 ParseTypeQualifierListOpt(DS);
4080 D.ExtendWithDeclSpec(DS);
4081
Bill Wendling93efb222007-06-02 23:28:54 +00004082 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
4083 // cv-qualifiers are introduced through the use of a typedef or of a
4084 // template type argument, in which case the cv-qualifiers are ignored.
Bill Wendling93efb222007-06-02 23:28:54 +00004085 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
4086 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
4087 Diag(DS.getConstSpecLoc(),
Chris Lattner6d29c102008-11-18 07:48:38 +00004088 diag::err_invalid_reference_qualifier_application) << "const";
Bill Wendling93efb222007-06-02 23:28:54 +00004089 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
4090 Diag(DS.getVolatileSpecLoc(),
Chris Lattner6d29c102008-11-18 07:48:38 +00004091 diag::err_invalid_reference_qualifier_application) << "volatile";
Bill Wendling93efb222007-06-02 23:28:54 +00004092 }
Bill Wendling3708c182007-05-27 10:15:43 +00004093
4094 // Recursively parse the declarator.
Sebastian Redlbd150f42008-11-21 19:14:01 +00004095 ParseDeclaratorInternal(D, DirectDeclParser);
Bill Wendling3708c182007-05-27 10:15:43 +00004096
Douglas Gregor66583c52008-11-03 15:51:28 +00004097 if (D.getNumTypeObjects() > 0) {
4098 // C++ [dcl.ref]p4: There shall be no references to references.
4099 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
4100 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattnerebad6a22008-11-19 07:37:42 +00004101 if (const IdentifierInfo *II = D.getIdentifier())
4102 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
4103 << II;
4104 else
4105 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
4106 << "type name";
Douglas Gregor66583c52008-11-03 15:51:28 +00004107
Sebastian Redlbd150f42008-11-21 19:14:01 +00004108 // Once we've complained about the reference-to-reference, we
Douglas Gregor66583c52008-11-03 15:51:28 +00004109 // can go ahead and build the (technically ill-formed)
4110 // declarator: reference collapsing will take care of it.
4111 }
4112 }
4113
Bill Wendling3708c182007-05-27 10:15:43 +00004114 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner788404f2008-02-21 01:32:26 +00004115 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
Sebastian Redled0f3b02009-03-15 22:02:01 +00004116 Kind == tok::amp),
John McCall084e83d2011-03-24 11:26:52 +00004117 DS.getAttributes(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004118 SourceLocation());
Bill Wendling3708c182007-05-27 10:15:43 +00004119 }
Chris Lattner6c7416c2006-08-07 00:19:33 +00004120}
4121
Richard Smith0efa75c2012-03-29 01:16:42 +00004122static void diagnoseMisplacedEllipsis(Parser &P, Declarator &D,
4123 SourceLocation EllipsisLoc) {
4124 if (EllipsisLoc.isValid()) {
4125 FixItHint Insertion;
4126 if (!D.getEllipsisLoc().isValid()) {
4127 Insertion = FixItHint::CreateInsertion(D.getIdentifierLoc(), "...");
4128 D.setEllipsisLoc(EllipsisLoc);
4129 }
4130 P.Diag(EllipsisLoc, diag::err_misplaced_ellipsis_in_declaration)
4131 << FixItHint::CreateRemoval(EllipsisLoc) << Insertion << !D.hasName();
4132 }
4133}
4134
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004135/// ParseDirectDeclarator
4136/// direct-declarator: [C99 6.7.5]
Douglas Gregor831c93f2008-11-05 20:51:48 +00004137/// [C99] identifier
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004138/// '(' declarator ')'
4139/// [GNU] '(' attributes declarator ')'
Chris Lattnere8074e62006-08-06 18:30:15 +00004140/// [C90] direct-declarator '[' constant-expression[opt] ']'
4141/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
4142/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
4143/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
4144/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004145/// [C++11] direct-declarator '[' constant-expression[opt] ']'
4146/// attribute-specifier-seq[opt]
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004147/// direct-declarator '(' parameter-type-list ')'
4148/// direct-declarator '(' identifier-list[opt] ')'
4149/// [GNU] direct-declarator '(' parameter-forward-declarations
4150/// parameter-type-list[opt] ')'
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00004151/// [C++] direct-declarator '(' parameter-declaration-clause ')'
4152/// cv-qualifier-seq[opt] exception-specification[opt]
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004153/// [C++11] direct-declarator '(' parameter-declaration-clause ')'
4154/// attribute-specifier-seq[opt] cv-qualifier-seq[opt]
4155/// ref-qualifier[opt] exception-specification[opt]
Douglas Gregor61956c42008-10-31 09:07:45 +00004156/// [C++] declarator-id
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004157/// [C++11] declarator-id attribute-specifier-seq[opt]
Douglas Gregor831c93f2008-11-05 20:51:48 +00004158///
4159/// declarator-id: [C++ 8]
Douglas Gregor27b4c162010-12-23 22:44:42 +00004160/// '...'[opt] id-expression
Douglas Gregor831c93f2008-11-05 20:51:48 +00004161/// '::'[opt] nested-name-specifier[opt] type-name
4162///
4163/// id-expression: [C++ 5.1]
4164/// unqualified-id
Douglas Gregord90fd522009-09-25 21:45:23 +00004165/// qualified-id
Douglas Gregor831c93f2008-11-05 20:51:48 +00004166///
4167/// unqualified-id: [C++ 5.1]
Mike Stump11289f42009-09-09 15:08:12 +00004168/// identifier
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00004169/// operator-function-id
Douglas Gregord90fd522009-09-25 21:45:23 +00004170/// conversion-function-id
Mike Stump11289f42009-09-09 15:08:12 +00004171/// '~' class-name
Douglas Gregor7f741122009-02-25 19:37:18 +00004172/// template-id
Argyrios Kyrtzidise4426352008-11-07 22:02:30 +00004173///
Richard Smith1453e312012-03-27 01:42:32 +00004174/// Note, any additional constructs added here may need corresponding changes
4175/// in isConstructorDeclarator.
Chris Lattneracd58a32006-08-06 17:24:14 +00004176void Parser::ParseDirectDeclarator(Declarator &D) {
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00004177 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00004178
David Blaikiebbafb8a2012-03-11 07:00:24 +00004179 if (getLangOpts().CPlusPlus && D.mayHaveIdentifier()) {
Douglas Gregor7861a802009-11-03 01:35:08 +00004180 // ParseDeclaratorInternal might already have parsed the scope.
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00004181 if (D.getCXXScopeSpec().isEmpty()) {
Douglas Gregordf593fb2011-11-07 17:33:42 +00004182 bool EnteringContext = D.getContext() == Declarator::FileContext ||
4183 D.getContext() == Declarator::MemberContext;
Chad Rosierc1183952012-06-26 22:30:43 +00004184 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec(), ParsedType(),
Douglas Gregordf593fb2011-11-07 17:33:42 +00004185 EnteringContext);
John McCall1f476a12010-02-26 08:45:28 +00004186 }
4187
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00004188 if (D.getCXXScopeSpec().isValid()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00004189 if (Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
John McCall2b058ef2009-12-11 20:04:54 +00004190 // Change the declaration context for name lookup, until this function
4191 // is exited (and the declarator has been parsed).
4192 DeclScopeObj.EnterDeclaratorScope();
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00004193 }
4194
Douglas Gregor27b4c162010-12-23 22:44:42 +00004195 // C++0x [dcl.fct]p14:
4196 // There is a syntactic ambiguity when an ellipsis occurs at the end
Chad Rosierc1183952012-06-26 22:30:43 +00004197 // of a parameter-declaration-clause without a preceding comma. In
4198 // this case, the ellipsis is parsed as part of the
4199 // abstract-declarator if the type of the parameter names a template
Douglas Gregor27b4c162010-12-23 22:44:42 +00004200 // parameter pack that has not been expanded; otherwise, it is parsed
4201 // as part of the parameter-declaration-clause.
Richard Smith0efa75c2012-03-29 01:16:42 +00004202 if (Tok.is(tok::ellipsis) && D.getCXXScopeSpec().isEmpty() &&
Douglas Gregor27b4c162010-12-23 22:44:42 +00004203 !((D.getContext() == Declarator::PrototypeContext ||
4204 D.getContext() == Declarator::BlockLiteralContext) &&
Douglas Gregor27b4c162010-12-23 22:44:42 +00004205 NextToken().is(tok::r_paren) &&
Richard Smith0efa75c2012-03-29 01:16:42 +00004206 !Actions.containsUnexpandedParameterPacks(D))) {
4207 SourceLocation EllipsisLoc = ConsumeToken();
4208 if (isPtrOperatorToken(Tok.getKind(), getLangOpts())) {
4209 // The ellipsis was put in the wrong place. Recover, and explain to
4210 // the user what they should have done.
4211 ParseDeclarator(D);
4212 diagnoseMisplacedEllipsis(*this, D, EllipsisLoc);
4213 return;
4214 } else
4215 D.setEllipsisLoc(EllipsisLoc);
4216
4217 // The ellipsis can't be followed by a parenthesized declarator. We
4218 // check for that in ParseParenDeclarator, after we have disambiguated
4219 // the l_paren token.
4220 }
4221
Douglas Gregor7861a802009-11-03 01:35:08 +00004222 if (Tok.is(tok::identifier) || Tok.is(tok::kw_operator) ||
4223 Tok.is(tok::annot_template_id) || Tok.is(tok::tilde)) {
4224 // We found something that indicates the start of an unqualified-id.
4225 // Parse that unqualified-id.
John McCall84821e72010-04-13 06:39:49 +00004226 bool AllowConstructorName;
4227 if (D.getDeclSpec().hasTypeSpecifier())
4228 AllowConstructorName = false;
4229 else if (D.getCXXScopeSpec().isSet())
4230 AllowConstructorName =
4231 (D.getContext() == Declarator::FileContext ||
4232 (D.getContext() == Declarator::MemberContext &&
4233 D.getDeclSpec().isFriendSpecified()));
4234 else
4235 AllowConstructorName = (D.getContext() == Declarator::MemberContext);
4236
Abramo Bagnara7945c982012-01-27 09:46:47 +00004237 SourceLocation TemplateKWLoc;
Chad Rosierc1183952012-06-26 22:30:43 +00004238 if (ParseUnqualifiedId(D.getCXXScopeSpec(),
4239 /*EnteringContext=*/true,
4240 /*AllowDestructorName=*/true,
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004241 AllowConstructorName,
John McCallba7bf592010-08-24 05:47:05 +00004242 ParsedType(),
Abramo Bagnara7945c982012-01-27 09:46:47 +00004243 TemplateKWLoc,
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00004244 D.getName()) ||
4245 // Once we're past the identifier, if the scope was bad, mark the
4246 // whole declarator bad.
4247 D.getCXXScopeSpec().isInvalid()) {
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00004248 D.SetIdentifier(0, Tok.getLocation());
4249 D.setInvalidType(true);
Douglas Gregor7861a802009-11-03 01:35:08 +00004250 } else {
4251 // Parsed the unqualified-id; update range information and move along.
4252 if (D.getSourceRange().getBegin().isInvalid())
4253 D.SetRangeBegin(D.getName().getSourceRange().getBegin());
4254 D.SetRangeEnd(D.getName().getSourceRange().getEnd());
Douglas Gregordbc5daf2008-11-07 20:08:42 +00004255 }
Douglas Gregor7861a802009-11-03 01:35:08 +00004256 goto PastIdentifier;
Douglas Gregor11d0c4c2008-11-06 22:13:31 +00004257 }
Douglas Gregor7861a802009-11-03 01:35:08 +00004258 } else if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00004259 assert(!getLangOpts().CPlusPlus &&
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00004260 "There's a C++-specific check for tok::identifier above");
4261 assert(Tok.getIdentifierInfo() && "Not an identifier?");
4262 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
4263 ConsumeToken();
Douglas Gregor7861a802009-11-03 01:35:08 +00004264 goto PastIdentifier;
4265 }
Richard Smith0efa75c2012-03-29 01:16:42 +00004266
Douglas Gregor7861a802009-11-03 01:35:08 +00004267 if (Tok.is(tok::l_paren)) {
Chris Lattneracd58a32006-08-06 17:24:14 +00004268 // direct-declarator: '(' declarator ')'
Chris Lattnere37e2332006-08-15 04:50:22 +00004269 // direct-declarator: '(' attributes declarator ')'
Chris Lattneracd58a32006-08-06 17:24:14 +00004270 // Example: 'char (*X)' or 'int (*XX)(void)'
4271 ParseParenDeclarator(D);
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004272
4273 // If the declarator was parenthesized, we entered the declarator
4274 // scope when parsing the parenthesized declarator, then exited
4275 // the scope already. Re-enter the scope, if we need to.
4276 if (D.getCXXScopeSpec().isSet()) {
Fariborz Jahanian358acd52010-08-17 23:50:37 +00004277 // If there was an error parsing parenthesized declarator, declarator
Richard Smith0efa75c2012-03-29 01:16:42 +00004278 // scope may have been entered before. Don't do it again.
Fariborz Jahanian358acd52010-08-17 23:50:37 +00004279 if (!D.isInvalidType() &&
4280 Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004281 // Change the declaration context for name lookup, until this function
4282 // is exited (and the declarator has been parsed).
Fariborz Jahanian358acd52010-08-17 23:50:37 +00004283 DeclScopeObj.EnterDeclaratorScope();
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004284 }
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00004285 } else if (D.mayOmitIdentifier()) {
Chris Lattneracd58a32006-08-06 17:24:14 +00004286 // This could be something simple like "int" (in which case the declarator
4287 // portion is empty), if an abstract-declarator is allowed.
4288 D.SetIdentifier(0, Tok.getLocation());
4289 } else {
David Blaikie5d577a22012-06-29 22:03:56 +00004290 if (Tok.getKind() == tok::annot_pragma_parser_crash)
4291 *(volatile int*) 0x11 = 0;
Douglas Gregord9f92e22009-03-06 23:28:18 +00004292 if (D.getContext() == Declarator::MemberContext)
4293 Diag(Tok, diag::err_expected_member_name_or_semi)
4294 << D.getDeclSpec().getSourceRange();
David Blaikiebbafb8a2012-03-11 07:00:24 +00004295 else if (getLangOpts().CPlusPlus)
4296 Diag(Tok, diag::err_expected_unqualified_id) << getLangOpts().CPlusPlus;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00004297 else
Chris Lattner6d29c102008-11-18 07:48:38 +00004298 Diag(Tok, diag::err_expected_ident_lparen);
Chris Lattnereec40f92006-08-06 21:55:29 +00004299 D.SetIdentifier(0, Tok.getLocation());
Chris Lattner8c5dd732008-11-11 06:13:16 +00004300 D.setInvalidType(true);
Chris Lattneracd58a32006-08-06 17:24:14 +00004301 }
Mike Stump11289f42009-09-09 15:08:12 +00004302
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00004303 PastIdentifier:
Chris Lattneracd58a32006-08-06 17:24:14 +00004304 assert(D.isPastIdentifier() &&
4305 "Haven't past the location of the identifier yet?");
Mike Stump11289f42009-09-09 15:08:12 +00004306
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004307 // Don't parse attributes unless we have parsed an unparenthesized name.
4308 if (D.hasName() && !D.getNumTypeObjects())
John McCall53fa7142010-12-24 02:08:15 +00004309 MaybeParseCXX0XAttributes(D);
Alexis Hunt96d5c762009-11-21 08:43:09 +00004310
Chris Lattneracd58a32006-08-06 17:24:14 +00004311 while (1) {
Chris Lattner76c72282007-10-09 17:33:22 +00004312 if (Tok.is(tok::l_paren)) {
David Blaikie15a430a2011-12-04 05:04:18 +00004313 // Enter function-declaration scope, limiting any declarators to the
4314 // function prototype scope, including parameter declarators.
4315 ParseScope PrototypeScope(this,
4316 Scope::FunctionPrototypeScope|Scope::DeclScope);
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00004317 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
4318 // In such a case, check if we actually have a function declarator; if it
4319 // is not, the declarator has been fully parsed.
Richard Smith943c4402012-07-30 21:30:52 +00004320 bool IsAmbiguous = false;
4321 if (getLangOpts().CPlusPlus && D.mayBeFollowedByCXXDirectInit() &&
4322 !isCXXFunctionDeclarator(&IsAmbiguous))
4323 break;
John McCall084e83d2011-03-24 11:26:52 +00004324 ParsedAttributes attrs(AttrFactory);
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004325 BalancedDelimiterTracker T(*this, tok::l_paren);
4326 T.consumeOpen();
Richard Smith943c4402012-07-30 21:30:52 +00004327 ParseFunctionDeclarator(D, attrs, T, IsAmbiguous);
David Blaikie15a430a2011-12-04 05:04:18 +00004328 PrototypeScope.Exit();
Chris Lattner76c72282007-10-09 17:33:22 +00004329 } else if (Tok.is(tok::l_square)) {
Chris Lattnere8074e62006-08-06 18:30:15 +00004330 ParseBracketDeclarator(D);
Chris Lattneracd58a32006-08-06 17:24:14 +00004331 } else {
4332 break;
4333 }
4334 }
Chad Rosierc1183952012-06-26 22:30:43 +00004335}
Chris Lattneracd58a32006-08-06 17:24:14 +00004336
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004337/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
4338/// only called before the identifier, so these are most likely just grouping
Mike Stump11289f42009-09-09 15:08:12 +00004339/// parens for precedence. If we find that these are actually function
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004340/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
4341///
4342/// direct-declarator:
4343/// '(' declarator ')'
4344/// [GNU] '(' attributes declarator ')'
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00004345/// direct-declarator '(' parameter-type-list ')'
4346/// direct-declarator '(' identifier-list[opt] ')'
4347/// [GNU] direct-declarator '(' parameter-forward-declarations
4348/// parameter-type-list[opt] ')'
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004349///
4350void Parser::ParseParenDeclarator(Declarator &D) {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004351 BalancedDelimiterTracker T(*this, tok::l_paren);
4352 T.consumeOpen();
4353
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004354 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
Mike Stump11289f42009-09-09 15:08:12 +00004355
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00004356 // Eat any attributes before we look at whether this is a grouping or function
4357 // declarator paren. If this is a grouping paren, the attribute applies to
4358 // the type being built up, for example:
4359 // int (__attribute__(()) *x)(long y)
4360 // If this ends up not being a grouping paren, the attribute applies to the
4361 // first argument, for example:
4362 // int (__attribute__(()) int x)
4363 // In either case, we need to eat any attributes to be able to determine what
4364 // sort of paren this is.
4365 //
John McCall084e83d2011-03-24 11:26:52 +00004366 ParsedAttributes attrs(AttrFactory);
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00004367 bool RequiresArg = false;
4368 if (Tok.is(tok::kw___attribute)) {
John McCall53fa7142010-12-24 02:08:15 +00004369 ParseGNUAttributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +00004370
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00004371 // We require that the argument list (if this is a non-grouping paren) be
4372 // present even if the attribute list was empty.
4373 RequiresArg = true;
4374 }
Steve Naroff44ac7772008-12-25 14:16:32 +00004375 // Eat any Microsoft extensions.
Eli Friedman53339e02009-06-08 23:27:34 +00004376 if (Tok.is(tok::kw___cdecl) || Tok.is(tok::kw___stdcall) ||
Douglas Gregora941dca2010-05-18 16:57:00 +00004377 Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___fastcall) ||
Francois Pichet17ed0202011-08-18 09:59:55 +00004378 Tok.is(tok::kw___w64) || Tok.is(tok::kw___ptr64) ||
Francois Pichetf2fb4112011-08-25 00:36:46 +00004379 Tok.is(tok::kw___ptr32) || Tok.is(tok::kw___unaligned)) {
John McCall53fa7142010-12-24 02:08:15 +00004380 ParseMicrosoftTypeAttributes(attrs);
Eli Friedman53339e02009-06-08 23:27:34 +00004381 }
Dawn Perchik335e16b2010-09-03 01:29:35 +00004382 // Eat any Borland extensions.
Ted Kremenek5eec2b02010-11-10 05:59:39 +00004383 if (Tok.is(tok::kw___pascal))
John McCall53fa7142010-12-24 02:08:15 +00004384 ParseBorlandTypeAttributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +00004385
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004386 // If we haven't past the identifier yet (or where the identifier would be
4387 // stored, if this is an abstract declarator), then this is probably just
4388 // grouping parens. However, if this could be an abstract-declarator, then
4389 // this could also be the start of function arguments (consider 'void()').
4390 bool isGrouping;
Mike Stump11289f42009-09-09 15:08:12 +00004391
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004392 if (!D.mayOmitIdentifier()) {
4393 // If this can't be an abstract-declarator, this *must* be a grouping
4394 // paren, because we haven't seen the identifier yet.
4395 isGrouping = true;
4396 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Richard Smith43f340f2012-03-27 23:05:05 +00004397 (getLangOpts().CPlusPlus && Tok.is(tok::ellipsis) &&
4398 NextToken().is(tok::r_paren)) || // C++ int(...)
Richard Smith2620cd92012-04-11 04:01:28 +00004399 isDeclarationSpecifier() || // 'int(int)' is a function.
4400 isCXX11AttributeSpecifier()) { // 'int([[]]int)' is a function.
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004401 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
4402 // considered to be a type, not a K&R identifier-list.
4403 isGrouping = false;
4404 } else {
4405 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
4406 isGrouping = true;
4407 }
Mike Stump11289f42009-09-09 15:08:12 +00004408
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004409 // If this is a grouping paren, handle:
4410 // direct-declarator: '(' declarator ')'
4411 // direct-declarator: '(' attributes declarator ')'
4412 if (isGrouping) {
Richard Smith0efa75c2012-03-29 01:16:42 +00004413 SourceLocation EllipsisLoc = D.getEllipsisLoc();
4414 D.setEllipsisLoc(SourceLocation());
4415
Argyrios Kyrtzidis8ae36842008-10-07 10:21:57 +00004416 bool hadGroupingParens = D.hasGroupingParens();
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00004417 D.setGroupingParens(true);
Sebastian Redlbd150f42008-11-21 19:14:01 +00004418 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004419 // Match the ')'.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004420 T.consumeClose();
Chad Rosierc1183952012-06-26 22:30:43 +00004421 D.AddTypeInfo(DeclaratorChunk::getParen(T.getOpenLocation(),
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004422 T.getCloseLocation()),
4423 attrs, T.getCloseLocation());
Argyrios Kyrtzidis8ae36842008-10-07 10:21:57 +00004424
4425 D.setGroupingParens(hadGroupingParens);
Richard Smith0efa75c2012-03-29 01:16:42 +00004426
4427 // An ellipsis cannot be placed outside parentheses.
4428 if (EllipsisLoc.isValid())
4429 diagnoseMisplacedEllipsis(*this, D, EllipsisLoc);
4430
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004431 return;
4432 }
Mike Stump11289f42009-09-09 15:08:12 +00004433
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004434 // Okay, if this wasn't a grouping paren, it must be the start of a function
4435 // argument list. Recognize that this declarator will never have an
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00004436 // identifier (and remember where it would have been), then call into
4437 // ParseFunctionDeclarator to handle of argument list.
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004438 D.SetIdentifier(0, Tok.getLocation());
4439
David Blaikie15a430a2011-12-04 05:04:18 +00004440 // Enter function-declaration scope, limiting any declarators to the
4441 // function prototype scope, including parameter declarators.
4442 ParseScope PrototypeScope(this,
4443 Scope::FunctionPrototypeScope|Scope::DeclScope);
Richard Smith943c4402012-07-30 21:30:52 +00004444 ParseFunctionDeclarator(D, attrs, T, false, RequiresArg);
David Blaikie15a430a2011-12-04 05:04:18 +00004445 PrototypeScope.Exit();
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004446}
4447
4448/// ParseFunctionDeclarator - We are after the identifier and have parsed the
4449/// declarator D up to a paren, which indicates that we are parsing function
4450/// arguments.
Chris Lattneracd58a32006-08-06 17:24:14 +00004451///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004452/// If FirstArgAttrs is non-null, then the caller parsed those arguments
4453/// immediately after the open paren - they should be considered to be the
4454/// first argument of a parameter.
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00004455///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004456/// If RequiresArg is true, then the first argument of the function is required
4457/// to be present and required to not be an identifier list.
Douglas Gregor9e66af42011-07-05 16:44:18 +00004458///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004459/// For C++, after the parameter-list, it also parses the cv-qualifier-seq[opt],
4460/// (C++11) ref-qualifier[opt], exception-specification[opt],
4461/// (C++11) attribute-specifier-seq[opt], and (C++11) trailing-return-type[opt].
4462///
4463/// [C++11] exception-specification:
Douglas Gregor9e66af42011-07-05 16:44:18 +00004464/// dynamic-exception-specification
4465/// noexcept-specification
4466///
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004467void Parser::ParseFunctionDeclarator(Declarator &D,
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004468 ParsedAttributes &FirstArgAttrs,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004469 BalancedDelimiterTracker &Tracker,
Richard Smith943c4402012-07-30 21:30:52 +00004470 bool IsAmbiguous,
Douglas Gregor9e66af42011-07-05 16:44:18 +00004471 bool RequiresArg) {
Chad Rosierc1183952012-06-26 22:30:43 +00004472 assert(getCurScope()->isFunctionPrototypeScope() &&
David Blaikie15a430a2011-12-04 05:04:18 +00004473 "Should call from a Function scope");
Douglas Gregor9e66af42011-07-05 16:44:18 +00004474 // lparen is already consumed!
4475 assert(D.isPastIdentifier() && "Should not call before identifier!");
4476
4477 // This should be true when the function has typed arguments.
4478 // Otherwise, it is treated as a K&R-style function.
4479 bool HasProto = false;
4480 // Build up an array of information about the parsed arguments.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004481 SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Douglas Gregor9e66af42011-07-05 16:44:18 +00004482 // Remember where we see an ellipsis, if any.
4483 SourceLocation EllipsisLoc;
4484
4485 DeclSpec DS(AttrFactory);
4486 bool RefQualifierIsLValueRef = true;
4487 SourceLocation RefQualifierLoc;
Douglas Gregore248eea2011-10-19 06:04:55 +00004488 SourceLocation ConstQualifierLoc;
4489 SourceLocation VolatileQualifierLoc;
Douglas Gregor9e66af42011-07-05 16:44:18 +00004490 ExceptionSpecificationType ESpecType = EST_None;
4491 SourceRange ESpecRange;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004492 SmallVector<ParsedType, 2> DynamicExceptions;
4493 SmallVector<SourceRange, 2> DynamicExceptionRanges;
Douglas Gregor9e66af42011-07-05 16:44:18 +00004494 ExprResult NoexceptExpr;
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004495 ParsedAttributes FnAttrs(AttrFactory);
Richard Smith700537c2012-06-12 01:51:59 +00004496 TypeResult TrailingReturnType;
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004497
James Molloy6f8780b2012-02-29 10:24:19 +00004498 Actions.ActOnStartFunctionDeclarator();
4499
Douglas Gregor9e66af42011-07-05 16:44:18 +00004500 SourceLocation EndLoc;
Douglas Gregor9e66af42011-07-05 16:44:18 +00004501 if (isFunctionDeclaratorIdentifierList()) {
4502 if (RequiresArg)
4503 Diag(Tok, diag::err_argument_required_after_attribute);
4504
4505 ParseFunctionDeclaratorIdentifierList(D, ParamInfo);
4506
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004507 Tracker.consumeClose();
4508 EndLoc = Tracker.getCloseLocation();
Douglas Gregor9e66af42011-07-05 16:44:18 +00004509 } else {
Douglas Gregor9e66af42011-07-05 16:44:18 +00004510 if (Tok.isNot(tok::r_paren))
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004511 ParseParameterDeclarationClause(D, FirstArgAttrs, ParamInfo, EllipsisLoc);
Douglas Gregor9e66af42011-07-05 16:44:18 +00004512 else if (RequiresArg)
4513 Diag(Tok, diag::err_argument_required_after_attribute);
4514
David Blaikiebbafb8a2012-03-11 07:00:24 +00004515 HasProto = ParamInfo.size() || getLangOpts().CPlusPlus;
Douglas Gregor9e66af42011-07-05 16:44:18 +00004516
4517 // If we have the closing ')', eat it.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004518 Tracker.consumeClose();
4519 EndLoc = Tracker.getCloseLocation();
Douglas Gregor9e66af42011-07-05 16:44:18 +00004520
David Blaikiebbafb8a2012-03-11 07:00:24 +00004521 if (getLangOpts().CPlusPlus) {
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004522 // FIXME: Accept these components in any order, and produce fixits to
4523 // correct the order if the user gets it wrong. Ideally we should deal
4524 // with the virt-specifier-seq and pure-specifier in the same way.
Douglas Gregor9e66af42011-07-05 16:44:18 +00004525
4526 // Parse cv-qualifier-seq[opt].
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004527 ParseTypeQualifierListOpt(DS, false /*no attributes*/, false);
4528 if (!DS.getSourceRange().getEnd().isInvalid()) {
4529 EndLoc = DS.getSourceRange().getEnd();
4530 ConstQualifierLoc = DS.getConstSpecLoc();
4531 VolatileQualifierLoc = DS.getVolatileSpecLoc();
4532 }
Douglas Gregor9e66af42011-07-05 16:44:18 +00004533
4534 // Parse ref-qualifier[opt].
4535 if (Tok.is(tok::amp) || Tok.is(tok::ampamp)) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00004536 Diag(Tok, getLangOpts().CPlusPlus0x ?
Richard Smith5d164bc2011-10-15 05:09:34 +00004537 diag::warn_cxx98_compat_ref_qualifier :
4538 diag::ext_ref_qualifier);
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004539
Douglas Gregor9e66af42011-07-05 16:44:18 +00004540 RefQualifierIsLValueRef = Tok.is(tok::amp);
4541 RefQualifierLoc = ConsumeToken();
4542 EndLoc = RefQualifierLoc;
4543 }
4544
Douglas Gregor3024f072012-04-16 07:05:22 +00004545 // C++11 [expr.prim.general]p3:
Chad Rosierc1183952012-06-26 22:30:43 +00004546 // If a declaration declares a member function or member function
4547 // template of a class X, the expression this is a prvalue of type
Douglas Gregor3024f072012-04-16 07:05:22 +00004548 // "pointer to cv-qualifier-seq X" between the optional cv-qualifer-seq
Chad Rosierc1183952012-06-26 22:30:43 +00004549 // and the end of the function-definition, member-declarator, or
Douglas Gregor3024f072012-04-16 07:05:22 +00004550 // declarator.
Chad Rosierc1183952012-06-26 22:30:43 +00004551 bool IsCXX11MemberFunction =
Douglas Gregor3024f072012-04-16 07:05:22 +00004552 getLangOpts().CPlusPlus0x &&
4553 (D.getContext() == Declarator::MemberContext ||
4554 (D.getContext() == Declarator::FileContext &&
Chad Rosierc1183952012-06-26 22:30:43 +00004555 D.getCXXScopeSpec().isValid() &&
Douglas Gregor3024f072012-04-16 07:05:22 +00004556 Actions.CurContext->isRecord()));
4557 Sema::CXXThisScopeRAII ThisScope(Actions,
4558 dyn_cast<CXXRecordDecl>(Actions.CurContext),
4559 DS.getTypeQualifiers(),
4560 IsCXX11MemberFunction);
Richard Smith2331bbf2012-05-02 22:22:32 +00004561
Douglas Gregor9e66af42011-07-05 16:44:18 +00004562 // Parse exception-specification[opt].
Richard Smith2331bbf2012-05-02 22:22:32 +00004563 ESpecType = tryParseExceptionSpecification(ESpecRange,
Douglas Gregor433e0532012-04-16 18:27:27 +00004564 DynamicExceptions,
4565 DynamicExceptionRanges,
Richard Smith2331bbf2012-05-02 22:22:32 +00004566 NoexceptExpr);
Douglas Gregor9e66af42011-07-05 16:44:18 +00004567 if (ESpecType != EST_None)
4568 EndLoc = ESpecRange.getEnd();
4569
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004570 // Parse attribute-specifier-seq[opt]. Per DR 979 and DR 1297, this goes
4571 // after the exception-specification.
4572 MaybeParseCXX0XAttributes(FnAttrs);
4573
Douglas Gregor9e66af42011-07-05 16:44:18 +00004574 // Parse trailing-return-type[opt].
David Blaikiebbafb8a2012-03-11 07:00:24 +00004575 if (getLangOpts().CPlusPlus0x && Tok.is(tok::arrow)) {
Richard Smith5d164bc2011-10-15 05:09:34 +00004576 Diag(Tok, diag::warn_cxx98_compat_trailing_return_type);
Douglas Gregordb0b9f12011-08-04 15:30:47 +00004577 SourceRange Range;
Richard Smith700537c2012-06-12 01:51:59 +00004578 TrailingReturnType = ParseTrailingReturnType(Range);
Douglas Gregordb0b9f12011-08-04 15:30:47 +00004579 if (Range.getEnd().isValid())
4580 EndLoc = Range.getEnd();
Douglas Gregor9e66af42011-07-05 16:44:18 +00004581 }
4582 }
Douglas Gregor9e66af42011-07-05 16:44:18 +00004583 }
4584
4585 // Remember that we parsed a function type, and remember the attributes.
4586 D.AddTypeInfo(DeclaratorChunk::getFunction(HasProto,
4587 /*isVariadic=*/EllipsisLoc.isValid(),
Richard Smith943c4402012-07-30 21:30:52 +00004588 IsAmbiguous, EllipsisLoc,
Douglas Gregor9e66af42011-07-05 16:44:18 +00004589 ParamInfo.data(), ParamInfo.size(),
4590 DS.getTypeQualifiers(),
4591 RefQualifierIsLValueRef,
Douglas Gregore248eea2011-10-19 06:04:55 +00004592 RefQualifierLoc, ConstQualifierLoc,
4593 VolatileQualifierLoc,
Douglas Gregorad69e652011-07-13 21:47:47 +00004594 /*MutableLoc=*/SourceLocation(),
Douglas Gregor9e66af42011-07-05 16:44:18 +00004595 ESpecType, ESpecRange.getBegin(),
4596 DynamicExceptions.data(),
4597 DynamicExceptionRanges.data(),
4598 DynamicExceptions.size(),
4599 NoexceptExpr.isUsable() ?
4600 NoexceptExpr.get() : 0,
Chad Rosierc1183952012-06-26 22:30:43 +00004601 Tracker.getOpenLocation(),
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004602 EndLoc, D,
Douglas Gregor9e66af42011-07-05 16:44:18 +00004603 TrailingReturnType),
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004604 FnAttrs, EndLoc);
James Molloy6f8780b2012-02-29 10:24:19 +00004605
4606 Actions.ActOnEndFunctionDeclarator();
Douglas Gregor9e66af42011-07-05 16:44:18 +00004607}
4608
4609/// isFunctionDeclaratorIdentifierList - This parameter list may have an
4610/// identifier list form for a K&R-style function: void foo(a,b,c)
4611///
4612/// Note that identifier-lists are only allowed for normal declarators, not for
4613/// abstract-declarators.
4614bool Parser::isFunctionDeclaratorIdentifierList() {
David Blaikiebbafb8a2012-03-11 07:00:24 +00004615 return !getLangOpts().CPlusPlus
Douglas Gregor9e66af42011-07-05 16:44:18 +00004616 && Tok.is(tok::identifier)
4617 && !TryAltiVecVectorToken()
4618 // K&R identifier lists can't have typedefs as identifiers, per C99
4619 // 6.7.5.3p11.
4620 && (TryAnnotateTypeOrScopeToken() || !Tok.is(tok::annot_typename))
4621 // Identifier lists follow a really simple grammar: the identifiers can
4622 // be followed *only* by a ", identifier" or ")". However, K&R
4623 // identifier lists are really rare in the brave new modern world, and
4624 // it is very common for someone to typo a type in a non-K&R style
4625 // list. If we are presented with something like: "void foo(intptr x,
4626 // float y)", we don't want to start parsing the function declarator as
4627 // though it is a K&R style declarator just because intptr is an
4628 // invalid type.
4629 //
4630 // To handle this, we check to see if the token after the first
4631 // identifier is a "," or ")". Only then do we parse it as an
4632 // identifier list.
4633 && (NextToken().is(tok::comma) || NextToken().is(tok::r_paren));
4634}
4635
4636/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
4637/// we found a K&R-style identifier list instead of a typed parameter list.
4638///
4639/// After returning, ParamInfo will hold the parsed parameters.
4640///
4641/// identifier-list: [C99 6.7.5]
4642/// identifier
4643/// identifier-list ',' identifier
4644///
4645void Parser::ParseFunctionDeclaratorIdentifierList(
4646 Declarator &D,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004647 SmallVector<DeclaratorChunk::ParamInfo, 16> &ParamInfo) {
Douglas Gregor9e66af42011-07-05 16:44:18 +00004648 // If there was no identifier specified for the declarator, either we are in
4649 // an abstract-declarator, or we are in a parameter declarator which was found
4650 // to be abstract. In abstract-declarators, identifier lists are not valid:
4651 // diagnose this.
4652 if (!D.getIdentifier())
4653 Diag(Tok, diag::ext_ident_list_in_param);
4654
4655 // Maintain an efficient lookup of params we have seen so far.
4656 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
4657
4658 while (1) {
4659 // If this isn't an identifier, report the error and skip until ')'.
4660 if (Tok.isNot(tok::identifier)) {
4661 Diag(Tok, diag::err_expected_ident);
4662 SkipUntil(tok::r_paren, /*StopAtSemi=*/true, /*DontConsume=*/true);
4663 // Forget we parsed anything.
4664 ParamInfo.clear();
4665 return;
4666 }
4667
4668 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
4669
4670 // Reject 'typedef int y; int test(x, y)', but continue parsing.
4671 if (Actions.getTypeName(*ParmII, Tok.getLocation(), getCurScope()))
4672 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
4673
4674 // Verify that the argument identifier has not already been mentioned.
4675 if (!ParamsSoFar.insert(ParmII)) {
4676 Diag(Tok, diag::err_param_redefinition) << ParmII;
4677 } else {
4678 // Remember this identifier in ParamInfo.
4679 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
4680 Tok.getLocation(),
4681 0));
4682 }
4683
4684 // Eat the identifier.
4685 ConsumeToken();
4686
4687 // The list continues if we see a comma.
4688 if (Tok.isNot(tok::comma))
4689 break;
4690 ConsumeToken();
4691 }
4692}
4693
4694/// ParseParameterDeclarationClause - Parse a (possibly empty) parameter-list
4695/// after the opening parenthesis. This function will not parse a K&R-style
4696/// identifier list.
4697///
Richard Smith2620cd92012-04-11 04:01:28 +00004698/// D is the declarator being parsed. If FirstArgAttrs is non-null, then the
4699/// caller parsed those arguments immediately after the open paren - they should
4700/// be considered to be part of the first parameter.
Douglas Gregor9e66af42011-07-05 16:44:18 +00004701///
4702/// After returning, ParamInfo will hold the parsed parameters. EllipsisLoc will
4703/// be the location of the ellipsis, if any was parsed.
4704///
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004705/// parameter-type-list: [C99 6.7.5]
4706/// parameter-list
4707/// parameter-list ',' '...'
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00004708/// [C++] parameter-list '...'
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004709///
4710/// parameter-list: [C99 6.7.5]
4711/// parameter-declaration
4712/// parameter-list ',' parameter-declaration
4713///
4714/// parameter-declaration: [C99 6.7.5]
4715/// declaration-specifiers declarator
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00004716/// [C++] declaration-specifiers declarator '=' assignment-expression
Sebastian Redldb63af22012-03-14 15:54:00 +00004717/// [C++11] initializer-clause
Chris Lattnere37e2332006-08-15 04:50:22 +00004718/// [GNU] declaration-specifiers declarator attributes
Sebastian Redlf769df52009-03-24 22:27:57 +00004719/// declaration-specifiers abstract-declarator[opt]
4720/// [C++] declaration-specifiers abstract-declarator[opt]
Chris Lattner58258242008-04-10 02:22:51 +00004721/// '=' assignment-expression
Chris Lattnere37e2332006-08-15 04:50:22 +00004722/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
Richard Smith2620cd92012-04-11 04:01:28 +00004723/// [C++11] attribute-specifier-seq parameter-declaration
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004724///
Douglas Gregor9e66af42011-07-05 16:44:18 +00004725void Parser::ParseParameterDeclarationClause(
4726 Declarator &D,
Richard Smith2620cd92012-04-11 04:01:28 +00004727 ParsedAttributes &FirstArgAttrs,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004728 SmallVector<DeclaratorChunk::ParamInfo, 16> &ParamInfo,
Douglas Gregor9e66af42011-07-05 16:44:18 +00004729 SourceLocation &EllipsisLoc) {
Mike Stump11289f42009-09-09 15:08:12 +00004730
Chris Lattner371ed4e2008-04-06 06:57:35 +00004731 while (1) {
4732 if (Tok.is(tok::ellipsis)) {
Richard Smith2620cd92012-04-11 04:01:28 +00004733 // FIXME: Issue a diagnostic if we parsed an attribute-specifier-seq
4734 // before deciding this was a parameter-declaration-clause.
Douglas Gregor94349fd2009-02-18 07:07:28 +00004735 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chris Lattner371ed4e2008-04-06 06:57:35 +00004736 break;
Chris Lattneracd58a32006-08-06 17:24:14 +00004737 }
Mike Stump11289f42009-09-09 15:08:12 +00004738
Chris Lattner371ed4e2008-04-06 06:57:35 +00004739 // Parse the declaration-specifiers.
John McCall28a6aea2009-11-04 02:18:39 +00004740 // Just use the ParsingDeclaration "scope" of the declarator.
John McCall084e83d2011-03-24 11:26:52 +00004741 DeclSpec DS(AttrFactory);
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004742
Richard Smith2620cd92012-04-11 04:01:28 +00004743 // Parse any C++11 attributes.
4744 MaybeParseCXX0XAttributes(DS.getAttributes());
4745
John McCall53fa7142010-12-24 02:08:15 +00004746 // Skip any Microsoft attributes before a param.
David Blaikiebbafb8a2012-03-11 07:00:24 +00004747 if (getLangOpts().MicrosoftExt && Tok.is(tok::l_square))
John McCall53fa7142010-12-24 02:08:15 +00004748 ParseMicrosoftAttributes(DS.getAttributes());
4749
4750 SourceLocation DSStart = Tok.getLocation();
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00004751
4752 // If the caller parsed attributes for the first argument, add them now.
John McCall53fa7142010-12-24 02:08:15 +00004753 // Take them so that we only apply the attributes to the first parameter.
Douglas Gregor9e66af42011-07-05 16:44:18 +00004754 // FIXME: If we can leave the attributes in the token stream somehow, we can
Richard Smith2620cd92012-04-11 04:01:28 +00004755 // get rid of a parameter (FirstArgAttrs) and this statement. It might be
4756 // too much hassle.
4757 DS.takeAttributesFrom(FirstArgAttrs);
John McCall53fa7142010-12-24 02:08:15 +00004758
Chris Lattnerde39c3e2009-02-27 18:38:20 +00004759 ParseDeclarationSpecifiers(DS);
Mike Stump11289f42009-09-09 15:08:12 +00004760
Chris Lattner371ed4e2008-04-06 06:57:35 +00004761 // Parse the declarator. This is "PrototypeContext", because we must
4762 // accept either 'declarator' or 'abstract-declarator' here.
4763 Declarator ParmDecl(DS, Declarator::PrototypeContext);
4764 ParseDeclarator(ParmDecl);
4765
4766 // Parse GNU attributes, if present.
John McCall53fa7142010-12-24 02:08:15 +00004767 MaybeParseGNUAttributes(ParmDecl);
Mike Stump11289f42009-09-09 15:08:12 +00004768
Chris Lattner371ed4e2008-04-06 06:57:35 +00004769 // Remember this parsed parameter in ParamInfo.
4770 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
Mike Stump11289f42009-09-09 15:08:12 +00004771
Douglas Gregor4d87df52008-12-16 21:30:33 +00004772 // DefArgToks is used when the parsing of default arguments needs
4773 // to be delayed.
4774 CachedTokens *DefArgToks = 0;
4775
Chris Lattner371ed4e2008-04-06 06:57:35 +00004776 // If no parameter was specified, verify that *something* was specified,
4777 // otherwise we have a missing type and identifier.
Chris Lattnerde39c3e2009-02-27 18:38:20 +00004778 if (DS.isEmpty() && ParmDecl.getIdentifier() == 0 &&
4779 ParmDecl.getNumTypeObjects() == 0) {
Chris Lattner371ed4e2008-04-06 06:57:35 +00004780 // Completely missing, emit error.
4781 Diag(DSStart, diag::err_missing_param);
4782 } else {
4783 // Otherwise, we have something. Add it and let semantic analysis try
4784 // to grok it and add the result to the ParamInfo we are building.
Mike Stump11289f42009-09-09 15:08:12 +00004785
Chris Lattner371ed4e2008-04-06 06:57:35 +00004786 // Inform the actions module about the parameter declarator, so it gets
4787 // added to the current scope.
John McCall48871652010-08-21 09:40:31 +00004788 Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl);
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00004789
4790 // Parse the default argument, if any. We parse the default
4791 // arguments in all dialects; the semantic analysis in
4792 // ActOnParamDefaultArgument will reject the default argument in
4793 // C.
4794 if (Tok.is(tok::equal)) {
Douglas Gregor58354032008-12-24 00:01:03 +00004795 SourceLocation EqualLoc = Tok.getLocation();
4796
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00004797 // Parse the default argument
Douglas Gregor4d87df52008-12-16 21:30:33 +00004798 if (D.getContext() == Declarator::MemberContext) {
4799 // If we're inside a class definition, cache the tokens
4800 // corresponding to the default argument. We'll actually parse
4801 // them when we see the end of the class definition.
Douglas Gregor4d87df52008-12-16 21:30:33 +00004802 // FIXME: Can we use a smart pointer for Toks?
4803 DefArgToks = new CachedTokens;
4804
Mike Stump11289f42009-09-09 15:08:12 +00004805 if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks,
Argyrios Kyrtzidis8d7bdba2010-04-23 21:20:12 +00004806 /*StopAtSemi=*/true,
4807 /*ConsumeFinalToken=*/false)) {
Douglas Gregor4d87df52008-12-16 21:30:33 +00004808 delete DefArgToks;
4809 DefArgToks = 0;
Douglas Gregor58354032008-12-24 00:01:03 +00004810 Actions.ActOnParamDefaultArgumentError(Param);
Argyrios Kyrtzidis249179c2010-08-06 09:47:24 +00004811 } else {
4812 // Mark the end of the default argument so that we know when to
4813 // stop when we parse it later on.
4814 Token DefArgEnd;
4815 DefArgEnd.startToken();
4816 DefArgEnd.setKind(tok::cxx_defaultarg_end);
4817 DefArgEnd.setLocation(Tok.getLocation());
4818 DefArgToks->push_back(DefArgEnd);
Mike Stump11289f42009-09-09 15:08:12 +00004819 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc,
Anders Carlsson84613c42009-06-12 16:51:40 +00004820 (*DefArgToks)[1].getLocation());
Argyrios Kyrtzidis249179c2010-08-06 09:47:24 +00004821 }
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00004822 } else {
Douglas Gregor4d87df52008-12-16 21:30:33 +00004823 // Consume the '='.
Douglas Gregor58354032008-12-24 00:01:03 +00004824 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00004825
Chad Rosierc1183952012-06-26 22:30:43 +00004826 // The argument isn't actually potentially evaluated unless it is
Douglas Gregor8a01b2a2010-09-11 20:24:53 +00004827 // used.
4828 EnterExpressionEvaluationContext Eval(Actions,
Douglas Gregor7fcbd902012-02-21 00:37:24 +00004829 Sema::PotentiallyEvaluatedIfUsed,
4830 Param);
Douglas Gregor8a01b2a2010-09-11 20:24:53 +00004831
Sebastian Redldb63af22012-03-14 15:54:00 +00004832 ExprResult DefArgResult;
Sebastian Redl1678d5f2012-03-18 22:25:45 +00004833 if (getLangOpts().CPlusPlus0x && Tok.is(tok::l_brace)) {
4834 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
Sebastian Redldb63af22012-03-14 15:54:00 +00004835 DefArgResult = ParseBraceInitializer();
Sebastian Redl1678d5f2012-03-18 22:25:45 +00004836 } else
Sebastian Redldb63af22012-03-14 15:54:00 +00004837 DefArgResult = ParseAssignmentExpression();
Douglas Gregor4d87df52008-12-16 21:30:33 +00004838 if (DefArgResult.isInvalid()) {
4839 Actions.ActOnParamDefaultArgumentError(Param);
4840 SkipUntil(tok::comma, tok::r_paren, true, true);
4841 } else {
4842 // Inform the actions module about the default argument
4843 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
John McCallb268a282010-08-23 23:25:46 +00004844 DefArgResult.take());
Douglas Gregor4d87df52008-12-16 21:30:33 +00004845 }
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00004846 }
4847 }
Mike Stump11289f42009-09-09 15:08:12 +00004848
4849 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
4850 ParmDecl.getIdentifierLoc(), Param,
Douglas Gregor4d87df52008-12-16 21:30:33 +00004851 DefArgToks));
Chris Lattner371ed4e2008-04-06 06:57:35 +00004852 }
4853
4854 // If the next token is a comma, consume it and keep reading arguments.
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00004855 if (Tok.isNot(tok::comma)) {
4856 if (Tok.is(tok::ellipsis)) {
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00004857 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chad Rosierc1183952012-06-26 22:30:43 +00004858
David Blaikiebbafb8a2012-03-11 07:00:24 +00004859 if (!getLangOpts().CPlusPlus) {
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00004860 // We have ellipsis without a preceding ',', which is ill-formed
4861 // in C. Complain and provide the fix.
4862 Diag(EllipsisLoc, diag::err_missing_comma_before_ellipsis)
Douglas Gregora771f462010-03-31 17:46:05 +00004863 << FixItHint::CreateInsertion(EllipsisLoc, ", ");
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00004864 }
4865 }
Chad Rosierc1183952012-06-26 22:30:43 +00004866
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00004867 break;
4868 }
Mike Stump11289f42009-09-09 15:08:12 +00004869
Chris Lattner371ed4e2008-04-06 06:57:35 +00004870 // Consume the comma.
4871 ConsumeToken();
Chris Lattneracd58a32006-08-06 17:24:14 +00004872 }
Mike Stump11289f42009-09-09 15:08:12 +00004873
Chris Lattner6c940e62008-04-06 06:34:08 +00004874}
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004875
Chris Lattnere8074e62006-08-06 18:30:15 +00004876/// [C90] direct-declarator '[' constant-expression[opt] ']'
4877/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
4878/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
4879/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
4880/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004881/// [C++11] direct-declarator '[' constant-expression[opt] ']'
4882/// attribute-specifier-seq[opt]
Chris Lattnere8074e62006-08-06 18:30:15 +00004883void Parser::ParseBracketDeclarator(Declarator &D) {
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004884 if (CheckProhibitedCXX11Attribute())
4885 return;
4886
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004887 BalancedDelimiterTracker T(*this, tok::l_square);
4888 T.consumeOpen();
Mike Stump11289f42009-09-09 15:08:12 +00004889
Chris Lattner84a11622008-12-18 07:27:21 +00004890 // C array syntax has many features, but by-far the most common is [] and [4].
4891 // This code does a fast path to handle some of the most obvious cases.
4892 if (Tok.getKind() == tok::r_square) {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004893 T.consumeClose();
John McCall084e83d2011-03-24 11:26:52 +00004894 ParsedAttributes attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00004895 MaybeParseCXX0XAttributes(attrs);
Chad Rosierc1183952012-06-26 22:30:43 +00004896
Chris Lattner84a11622008-12-18 07:27:21 +00004897 // Remember that we parsed the empty array type.
John McCalldadc5752010-08-24 06:29:42 +00004898 ExprResult NumElements;
John McCall084e83d2011-03-24 11:26:52 +00004899 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004900 T.getOpenLocation(),
4901 T.getCloseLocation()),
4902 attrs, T.getCloseLocation());
Chris Lattner84a11622008-12-18 07:27:21 +00004903 return;
4904 } else if (Tok.getKind() == tok::numeric_constant &&
4905 GetLookAheadToken(1).is(tok::r_square)) {
4906 // [4] is very common. Parse the numeric constant expression.
Richard Smithbcc22fc2012-03-09 08:00:36 +00004907 ExprResult ExprRes(Actions.ActOnNumericConstant(Tok, getCurScope()));
Chris Lattner84a11622008-12-18 07:27:21 +00004908 ConsumeToken();
4909
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004910 T.consumeClose();
John McCall084e83d2011-03-24 11:26:52 +00004911 ParsedAttributes attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00004912 MaybeParseCXX0XAttributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +00004913
Chris Lattner84a11622008-12-18 07:27:21 +00004914 // Remember that we parsed a array type, and remember its features.
John McCall084e83d2011-03-24 11:26:52 +00004915 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, 0,
John McCall53fa7142010-12-24 02:08:15 +00004916 ExprRes.release(),
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004917 T.getOpenLocation(),
4918 T.getCloseLocation()),
4919 attrs, T.getCloseLocation());
Chris Lattner84a11622008-12-18 07:27:21 +00004920 return;
4921 }
Mike Stump11289f42009-09-09 15:08:12 +00004922
Chris Lattnere8074e62006-08-06 18:30:15 +00004923 // If valid, this location is the position where we read the 'static' keyword.
4924 SourceLocation StaticLoc;
Chris Lattner76c72282007-10-09 17:33:22 +00004925 if (Tok.is(tok::kw_static))
Chris Lattneraf635312006-10-16 06:06:51 +00004926 StaticLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00004927
Chris Lattnere8074e62006-08-06 18:30:15 +00004928 // If there is a type-qualifier-list, read it now.
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00004929 // Type qualifiers in an array subscript are a C99 feature.
John McCall084e83d2011-03-24 11:26:52 +00004930 DeclSpec DS(AttrFactory);
Chris Lattnercf0bab22008-12-18 07:02:59 +00004931 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Mike Stump11289f42009-09-09 15:08:12 +00004932
Chris Lattnere8074e62006-08-06 18:30:15 +00004933 // If we haven't already read 'static', check to see if there is one after the
4934 // type-qualifier-list.
Chris Lattner76c72282007-10-09 17:33:22 +00004935 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Chris Lattneraf635312006-10-16 06:06:51 +00004936 StaticLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00004937
Chris Lattnere8074e62006-08-06 18:30:15 +00004938 // Handle "direct-declarator [ type-qual-list[opt] * ]".
Chris Lattnere8074e62006-08-06 18:30:15 +00004939 bool isStar = false;
John McCalldadc5752010-08-24 06:29:42 +00004940 ExprResult NumElements;
Mike Stump11289f42009-09-09 15:08:12 +00004941
Chris Lattner521ff2b2008-04-06 05:26:30 +00004942 // Handle the case where we have '[*]' as the array size. However, a leading
4943 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
Sylvestre Ledru830885c2012-07-23 08:59:39 +00004944 // the token after the star is a ']'. Since stars in arrays are
Chris Lattner521ff2b2008-04-06 05:26:30 +00004945 // infrequent, use of lookahead is not costly here.
4946 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattnerc439f0d2008-04-06 05:27:21 +00004947 ConsumeToken(); // Eat the '*'.
Chris Lattner1906f802006-08-06 19:14:46 +00004948
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00004949 if (StaticLoc.isValid()) {
Chris Lattner521ff2b2008-04-06 05:26:30 +00004950 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00004951 StaticLoc = SourceLocation(); // Drop the static.
4952 }
Chris Lattner521ff2b2008-04-06 05:26:30 +00004953 isStar = true;
Chris Lattner76c72282007-10-09 17:33:22 +00004954 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner84a11622008-12-18 07:27:21 +00004955 // Note, in C89, this production uses the constant-expr production instead
4956 // of assignment-expr. The only difference is that assignment-expr allows
4957 // things like '=' and '*='. Sema rejects these in C89 mode because they
4958 // are not i-c-e's, so we don't need to distinguish between the two here.
Mike Stump11289f42009-09-09 15:08:12 +00004959
Douglas Gregorc9c02ed2009-06-19 23:52:42 +00004960 // Parse the constant-expression or assignment-expression now (depending
4961 // on dialect).
David Blaikiebbafb8a2012-03-11 07:00:24 +00004962 if (getLangOpts().CPlusPlus) {
Douglas Gregorc9c02ed2009-06-19 23:52:42 +00004963 NumElements = ParseConstantExpression();
Eli Friedmane0afc982012-01-21 01:01:51 +00004964 } else {
4965 EnterExpressionEvaluationContext Unevaluated(Actions,
4966 Sema::ConstantEvaluated);
Douglas Gregorc9c02ed2009-06-19 23:52:42 +00004967 NumElements = ParseAssignmentExpression();
Eli Friedmane0afc982012-01-21 01:01:51 +00004968 }
Chris Lattner62591722006-08-12 18:40:58 +00004969 }
Mike Stump11289f42009-09-09 15:08:12 +00004970
Chris Lattner62591722006-08-12 18:40:58 +00004971 // If there was an error parsing the assignment-expression, recover.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00004972 if (NumElements.isInvalid()) {
Chris Lattnercd2a8c52009-04-24 22:30:50 +00004973 D.setInvalidType(true);
Chris Lattner62591722006-08-12 18:40:58 +00004974 // If the expression was invalid, skip it.
4975 SkipUntil(tok::r_square);
4976 return;
Chris Lattnere8074e62006-08-06 18:30:15 +00004977 }
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004978
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004979 T.consumeClose();
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004980
John McCall084e83d2011-03-24 11:26:52 +00004981 ParsedAttributes attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00004982 MaybeParseCXX0XAttributes(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +00004983
Chris Lattner84a11622008-12-18 07:27:21 +00004984 // Remember that we parsed a array type, and remember its features.
John McCall084e83d2011-03-24 11:26:52 +00004985 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
Chris Lattnercbc426d2006-12-02 06:43:02 +00004986 StaticLoc.isValid(), isStar,
Douglas Gregor04318252009-07-06 15:59:29 +00004987 NumElements.release(),
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004988 T.getOpenLocation(),
4989 T.getCloseLocation()),
4990 attrs, T.getCloseLocation());
Chris Lattnere8074e62006-08-06 18:30:15 +00004991}
4992
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00004993/// [GNU] typeof-specifier:
4994/// typeof ( expressions )
4995/// typeof ( type-name )
4996/// [GNU/C++] typeof unary-expression
Steve Naroffad373bd2007-07-31 12:34:36 +00004997///
4998void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner76c72282007-10-09 17:33:22 +00004999 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005000 Token OpTok = Tok;
Steve Naroffad373bd2007-07-31 12:34:36 +00005001 SourceLocation StartLoc = ConsumeToken();
5002
John McCalle8595032010-01-13 20:03:27 +00005003 const bool hasParens = Tok.is(tok::l_paren);
5004
Eli Friedmane0afc982012-01-21 01:01:51 +00005005 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated);
5006
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005007 bool isCastExpr;
John McCallba7bf592010-08-24 05:47:05 +00005008 ParsedType CastTy;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005009 SourceRange CastRange;
Peter Collingbournee190dee2011-03-11 19:24:49 +00005010 ExprResult Operand = ParseExprAfterUnaryExprOrTypeTrait(OpTok, isCastExpr,
5011 CastTy, CastRange);
John McCalle8595032010-01-13 20:03:27 +00005012 if (hasParens)
5013 DS.setTypeofParensRange(CastRange);
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005014
5015 if (CastRange.getEnd().isInvalid())
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00005016 // FIXME: Not accurate, the range gets one token more than it should.
5017 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005018 else
5019 DS.SetRangeEnd(CastRange.getEnd());
Mike Stump11289f42009-09-09 15:08:12 +00005020
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005021 if (isCastExpr) {
5022 if (!CastTy) {
5023 DS.SetTypeSpecError();
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00005024 return;
Douglas Gregor220cac52009-02-18 17:45:20 +00005025 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00005026
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005027 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00005028 unsigned DiagID;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005029 // Check for duplicate type specifiers (e.g. "int typeof(int)").
5030 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00005031 DiagID, CastTy))
5032 Diag(StartLoc, DiagID) << PrevSpec;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005033 return;
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00005034 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00005035
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00005036 // If we get here, the operand to the typeof was an expresion.
5037 if (Operand.isInvalid()) {
5038 DS.SetTypeSpecError();
Steve Naroff4bd2f712007-08-02 02:53:48 +00005039 return;
Steve Naroffad373bd2007-07-31 12:34:36 +00005040 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00005041
Eli Friedmane0afc982012-01-21 01:01:51 +00005042 // We might need to transform the operand if it is potentially evaluated.
5043 Operand = Actions.HandleExprEvaluationContextForTypeof(Operand.get());
5044 if (Operand.isInvalid()) {
5045 DS.SetTypeSpecError();
5046 return;
5047 }
5048
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00005049 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00005050 unsigned DiagID;
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00005051 // Check for duplicate type specifiers (e.g. "int typeof(int)").
5052 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
John McCallba7bf592010-08-24 05:47:05 +00005053 DiagID, Operand.get()))
John McCall49bfce42009-08-03 20:12:06 +00005054 Diag(StartLoc, DiagID) << PrevSpec;
Steve Naroffad373bd2007-07-31 12:34:36 +00005055}
Chris Lattner73a9c7d2010-02-28 18:33:55 +00005056
Benjamin Kramere56f3932011-12-23 17:00:35 +00005057/// [C11] atomic-specifier:
Eli Friedman0dfb8892011-10-06 23:00:33 +00005058/// _Atomic ( type-name )
5059///
5060void Parser::ParseAtomicSpecifier(DeclSpec &DS) {
5061 assert(Tok.is(tok::kw__Atomic) && "Not an atomic specifier");
5062
5063 SourceLocation StartLoc = ConsumeToken();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005064 BalancedDelimiterTracker T(*this, tok::l_paren);
5065 if (T.expectAndConsume(diag::err_expected_lparen_after, "_Atomic")) {
Eli Friedman0dfb8892011-10-06 23:00:33 +00005066 SkipUntil(tok::r_paren);
5067 return;
5068 }
5069
5070 TypeResult Result = ParseTypeName();
5071 if (Result.isInvalid()) {
5072 SkipUntil(tok::r_paren);
5073 return;
5074 }
5075
5076 // Match the ')'
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005077 T.consumeClose();
Eli Friedman0dfb8892011-10-06 23:00:33 +00005078
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005079 if (T.getCloseLocation().isInvalid())
Eli Friedman0dfb8892011-10-06 23:00:33 +00005080 return;
5081
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005082 DS.setTypeofParensRange(T.getRange());
5083 DS.SetRangeEnd(T.getCloseLocation());
Eli Friedman0dfb8892011-10-06 23:00:33 +00005084
5085 const char *PrevSpec = 0;
5086 unsigned DiagID;
5087 if (DS.SetTypeSpecType(DeclSpec::TST_atomic, StartLoc, PrevSpec,
5088 DiagID, Result.release()))
5089 Diag(StartLoc, DiagID) << PrevSpec;
5090}
5091
Chris Lattner73a9c7d2010-02-28 18:33:55 +00005092
5093/// TryAltiVecVectorTokenOutOfLine - Out of line body that should only be called
5094/// from TryAltiVecVectorToken.
5095bool Parser::TryAltiVecVectorTokenOutOfLine() {
5096 Token Next = NextToken();
5097 switch (Next.getKind()) {
5098 default: return false;
5099 case tok::kw_short:
5100 case tok::kw_long:
5101 case tok::kw_signed:
5102 case tok::kw_unsigned:
5103 case tok::kw_void:
5104 case tok::kw_char:
5105 case tok::kw_int:
5106 case tok::kw_float:
5107 case tok::kw_double:
5108 case tok::kw_bool:
5109 case tok::kw___pixel:
5110 Tok.setKind(tok::kw___vector);
5111 return true;
5112 case tok::identifier:
5113 if (Next.getIdentifierInfo() == Ident_pixel) {
5114 Tok.setKind(tok::kw___vector);
5115 return true;
5116 }
5117 return false;
5118 }
5119}
5120
5121bool Parser::TryAltiVecTokenOutOfLine(DeclSpec &DS, SourceLocation Loc,
5122 const char *&PrevSpec, unsigned &DiagID,
5123 bool &isInvalid) {
5124 if (Tok.getIdentifierInfo() == Ident_vector) {
5125 Token Next = NextToken();
5126 switch (Next.getKind()) {
5127 case tok::kw_short:
5128 case tok::kw_long:
5129 case tok::kw_signed:
5130 case tok::kw_unsigned:
5131 case tok::kw_void:
5132 case tok::kw_char:
5133 case tok::kw_int:
5134 case tok::kw_float:
5135 case tok::kw_double:
5136 case tok::kw_bool:
5137 case tok::kw___pixel:
5138 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
5139 return true;
5140 case tok::identifier:
5141 if (Next.getIdentifierInfo() == Ident_pixel) {
5142 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
5143 return true;
5144 }
5145 break;
5146 default:
5147 break;
5148 }
Douglas Gregor9938e3b2010-06-16 15:28:57 +00005149 } else if ((Tok.getIdentifierInfo() == Ident_pixel) &&
Chris Lattner73a9c7d2010-02-28 18:33:55 +00005150 DS.isTypeAltiVecVector()) {
5151 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
5152 return true;
5153 }
5154 return false;
5155}