blob: dc8e120f57d3334a37f2e9bca114d59e930f7f66 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- ParseDecl.cpp - Declaration Parsing ------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Declaration portions of the Parser interfaces.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Parse/Parser.h"
Chris Lattner500d3292009-01-29 05:15:15 +000015#include "clang/Parse/ParseDiagnostic.h"
Peter Collingbourne207f4d82011-03-18 22:38:29 +000016#include "clang/Basic/OpenCL.h"
Kaelyn Uhrainaec2ac62012-04-26 23:36:17 +000017#include "clang/Sema/Lookup.h"
John McCall19510852010-08-20 18:27:03 +000018#include "clang/Sema/Scope.h"
19#include "clang/Sema/ParsedTemplate.h"
John McCallf312b1e2010-08-26 23:41:50 +000020#include "clang/Sema/PrettyDeclStackTrace.h"
Chris Lattnerd167ca02009-12-10 00:21:05 +000021#include "RAIIObjectsForParser.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000022#include "llvm/ADT/SmallSet.h"
Benjamin Kramer8fe83e12012-02-04 13:45:25 +000023#include "llvm/ADT/SmallString.h"
Caitlin Sadowskib51e0312011-08-09 17:59:31 +000024#include "llvm/ADT/StringSwitch.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000025using namespace clang;
26
27//===----------------------------------------------------------------------===//
28// C99 6.7: Declarations.
29//===----------------------------------------------------------------------===//
30
31/// ParseTypeName
32/// type-name: [C99 6.7.6]
33/// specifier-qualifier-list abstract-declarator[opt]
Sebastian Redl4c5d3202008-11-21 19:14:01 +000034///
35/// Called type-id in C++.
Douglas Gregor683a81f2011-01-31 16:09:46 +000036TypeResult Parser::ParseTypeName(SourceRange *Range,
John McCallf85e1932011-06-15 23:02:42 +000037 Declarator::TheContext Context,
Richard Smithc89edf52011-07-01 19:46:12 +000038 AccessSpecifier AS,
39 Decl **OwnedType) {
Richard Smith6d96d3a2012-03-15 01:02:11 +000040 DeclSpecContext DSC = getDeclSpecContextFromDeclaratorContext(Context);
Richard Smitha971d242012-05-09 20:55:26 +000041 if (DSC == DSC_normal)
42 DSC = DSC_type_specifier;
Richard Smith7796eb52012-03-12 08:56:40 +000043
Reid Spencer5f016e22007-07-11 17:01:13 +000044 // Parse the common declaration-specifiers piece.
John McCall0b7e6782011-03-24 11:26:52 +000045 DeclSpec DS(AttrFactory);
Richard Smith7796eb52012-03-12 08:56:40 +000046 ParseSpecifierQualifierList(DS, AS, DSC);
Richard Smithc89edf52011-07-01 19:46:12 +000047 if (OwnedType)
48 *OwnedType = DS.isTypeSpecOwned() ? DS.getRepAsDecl() : 0;
Sebastian Redlef65f062009-05-29 18:02:33 +000049
Reid Spencer5f016e22007-07-11 17:01:13 +000050 // Parse the abstract-declarator, if present.
Douglas Gregor683a81f2011-01-31 16:09:46 +000051 Declarator DeclaratorInfo(DS, Context);
Reid Spencer5f016e22007-07-11 17:01:13 +000052 ParseDeclarator(DeclaratorInfo);
Sebastian Redlef65f062009-05-29 18:02:33 +000053 if (Range)
54 *Range = DeclaratorInfo.getSourceRange();
55
Chris Lattnereaaebc72009-04-25 08:06:05 +000056 if (DeclaratorInfo.isInvalidType())
Douglas Gregor809070a2009-02-18 17:45:20 +000057 return true;
58
Douglas Gregor23c94db2010-07-02 17:43:08 +000059 return Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
Reid Spencer5f016e22007-07-11 17:01:13 +000060}
61
Caitlin Sadowskieff98fc2011-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
Sean Huntbbd37c62009-11-21 08:43:09 +000072/// ParseGNUAttributes - Parse a non-empty attributes list.
Reid Spencer5f016e22007-07-11 17:01:13 +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
87/// attrib-name
88/// attrib-name '(' identifier ')'
89/// attrib-name '(' identifier ',' nonempty-expr-list ')'
90/// attrib-name '(' argument-expression-list [C99 6.5.2] ')'
91///
92/// [GNU] attrib-name:
93/// identifier
94/// typespec
95/// typequal
96/// storageclass
Mike Stump1eb44332009-09-09 15:08:12 +000097///
Reid Spencer5f016e22007-07-11 17:01:13 +000098/// FIXME: The GCC grammar/code for this construct implies we need two
Mike Stump1eb44332009-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
Reid Spencer5f016e22007-07-11 17:01:13 +0000101/// start with that identifier; otherwise they are an expression list."
102///
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000103/// GCC does not require the ',' between attribs in an attribute-list.
104///
Reid Spencer5f016e22007-07-11 17:01:13 +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.
109
John McCall7f040a92010-12-24 02:08:15 +0000110void Parser::ParseGNUAttributes(ParsedAttributes &attrs,
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000111 SourceLocation *endLoc,
112 LateParsedAttrList *LateAttrs) {
Sean Huntbbd37c62009-11-21 08:43:09 +0000113 assert(Tok.is(tok::kw___attribute) && "Not a GNU attribute list!");
Mike Stump1eb44332009-09-09 15:08:12 +0000114
Chris Lattner04d66662007-10-09 17:33:22 +0000115 while (Tok.is(tok::kw___attribute)) {
Reid Spencer5f016e22007-07-11 17:01:13 +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 McCall7f040a92010-12-24 02:08:15 +0000120 return;
Reid Spencer5f016e22007-07-11 17:01:13 +0000121 }
122 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, "(")) {
123 SkipUntil(tok::r_paren, true); // skip until ) or ;
John McCall7f040a92010-12-24 02:08:15 +0000124 return;
Reid Spencer5f016e22007-07-11 17:01:13 +0000125 }
126 // Parse the attribute-list. e.g. __attribute__(( weak, alias("__f") ))
Chris Lattner04d66662007-10-09 17:33:22 +0000127 while (Tok.is(tok::identifier) || isDeclarationSpecifier() ||
128 Tok.is(tok::comma)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000129 if (Tok.is(tok::comma)) {
Reid Spencer5f016e22007-07-11 17:01:13 +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 Stump1eb44332009-09-09 15:08:12 +0000137
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000138 if (Tok.is(tok::l_paren)) {
139 // handle "parameterized" attributes
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000140 if (LateAttrs && isAttributeLateParsed(*AttrName)) {
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000141 LateParsedAttribute *LA =
142 new LateParsedAttribute(this, *AttrName, AttrNameLoc);
143 LateAttrs->push_back(LA);
DeLesley Hutchinsc24a2332012-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 Stump1eb44332009-09-09 15:08:12 +0000149
Caitlin Sadowskieff98fc2011-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 Stump1eb44332009-09-09 15:08:12 +0000152
Caitlin Sadowskieff98fc2011-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);
Reid Spencer5f016e22007-07-11 17:01:13 +0000159 }
160 } else {
John McCall0b7e6782011-03-24 11:26:52 +0000161 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc,
Sean Hunt93f95f22012-06-18 16:13:52 +0000162 0, SourceLocation(), 0, 0, AttributeList::AS_GNU);
Reid Spencer5f016e22007-07-11 17:01:13 +0000163 }
164 }
165 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
Reid Spencer5f016e22007-07-11 17:01:13 +0000166 SkipUntil(tok::r_paren, false);
Sean Huntbbd37c62009-11-21 08:43:09 +0000167 SourceLocation Loc = Tok.getLocation();
Sebastian Redlab197ba2009-02-09 18:23:29 +0000168 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) {
169 SkipUntil(tok::r_paren, false);
170 }
John McCall7f040a92010-12-24 02:08:15 +0000171 if (endLoc)
172 *endLoc = Loc;
Reid Spencer5f016e22007-07-11 17:01:13 +0000173 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000174}
175
Caitlin Sadowskieff98fc2011-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 Smithfe0a0fb2011-10-17 21:20:17 +0000199 IdentifierInfo *ParmName = 0;
200 SourceLocation ParmLoc;
201 bool BuiltinType = false;
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000202
Richard Smithfe0a0fb2011-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 Smith5a5a9712012-04-04 06:24:32 +0000213 case tok::kw___int128:
Richard Smithfe0a0fb2011-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 Sadowskieff98fc2011-09-08 17:42:22 +0000241 ConsumeToken();
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000242
Richard Smithfe0a0fb2011-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 Sadowskieff98fc2011-09-08 17:42:22 +0000249 }
Richard Smithfe0a0fb2011-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 Sadowskieff98fc2011-09-08 17:42:22 +0000254 }
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000255 }
Fariborz Jahanian7a81e412011-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 Jahanianb2243432011-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 Jahanian7a81e412011-10-18 17:11:10 +0000270 SkipUntil(tok::r_paren, false, true); // skip until ')'
271 }
272 }
Richard Smithfe0a0fb2011-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 Kyrtzidisffcc3102011-09-13 16:05:53 +0000277 Attrs.addNew(AttrName, SourceRange(AttrNameLoc, RParen), 0, AttrNameLoc,
Sean Hunt93f95f22012-06-18 16:13:52 +0000278 ParmName, ParmLoc, ArgExprs.take(), ArgExprs.size(),
279 AttributeList::AS_GNU);
Sean Huntbfcb0372012-06-19 03:39:03 +0000280 if (BuiltinType && attr->getKind() == AttributeList::AT_IBOutletCollection)
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000281 Diag(Tok, diag::err_iboutletcollection_builtintype);
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000282 }
283}
284
285
Eli Friedmana23b4852009-06-08 07:21:15 +0000286/// ParseMicrosoftDeclSpec - Parse an __declspec construct
287///
288/// [MS] decl-specifier:
289/// __declspec ( extended-decl-modifier-seq )
290///
291/// [MS] extended-decl-modifier-seq:
292/// extended-decl-modifier[opt]
293/// extended-decl-modifier extended-decl-modifier-seq
294
John McCall7f040a92010-12-24 02:08:15 +0000295void Parser::ParseMicrosoftDeclSpec(ParsedAttributes &attrs) {
Steve Narofff59e17e2008-12-24 20:59:21 +0000296 assert(Tok.is(tok::kw___declspec) && "Not a declspec!");
Eli Friedmana23b4852009-06-08 07:21:15 +0000297
Steve Narofff59e17e2008-12-24 20:59:21 +0000298 ConsumeToken();
Eli Friedmana23b4852009-06-08 07:21:15 +0000299 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
300 "declspec")) {
301 SkipUntil(tok::r_paren, true); // skip until ) or ;
John McCall7f040a92010-12-24 02:08:15 +0000302 return;
Eli Friedmana23b4852009-06-08 07:21:15 +0000303 }
Francois Pichet373197b2011-05-07 19:04:49 +0000304
Eli Friedman290eeb02009-06-08 23:27:34 +0000305 while (Tok.getIdentifierInfo()) {
Eli Friedmana23b4852009-06-08 07:21:15 +0000306 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
307 SourceLocation AttrNameLoc = ConsumeToken();
Francois Pichet373197b2011-05-07 19:04:49 +0000308
309 // FIXME: Remove this when we have proper __declspec(property()) support.
310 // Just skip everything inside property().
311 if (AttrName->getName() == "property") {
312 ConsumeParen();
313 SkipUntil(tok::r_paren);
314 }
Eli Friedmana23b4852009-06-08 07:21:15 +0000315 if (Tok.is(tok::l_paren)) {
316 ConsumeParen();
317 // FIXME: This doesn't parse __declspec(property(get=get_func_name))
318 // correctly.
John McCall60d7b3a2010-08-24 06:29:42 +0000319 ExprResult ArgExpr(ParseAssignmentExpression());
Eli Friedmana23b4852009-06-08 07:21:15 +0000320 if (!ArgExpr.isInvalid()) {
John McCallca0408f2010-08-23 06:44:23 +0000321 Expr *ExprList = ArgExpr.take();
John McCall0b7e6782011-03-24 11:26:52 +0000322 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
Sean Hunt93f95f22012-06-18 16:13:52 +0000323 SourceLocation(), &ExprList, 1,
324 AttributeList::AS_Declspec);
Eli Friedmana23b4852009-06-08 07:21:15 +0000325 }
326 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
327 SkipUntil(tok::r_paren, false);
328 } else {
John McCall0b7e6782011-03-24 11:26:52 +0000329 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc,
Sean Hunt93f95f22012-06-18 16:13:52 +0000330 0, SourceLocation(), 0, 0, AttributeList::AS_Declspec);
Eli Friedmana23b4852009-06-08 07:21:15 +0000331 }
332 }
333 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
334 SkipUntil(tok::r_paren, false);
John McCall7f040a92010-12-24 02:08:15 +0000335 return;
Eli Friedman290eeb02009-06-08 23:27:34 +0000336}
337
John McCall7f040a92010-12-24 02:08:15 +0000338void Parser::ParseMicrosoftTypeAttributes(ParsedAttributes &attrs) {
Eli Friedman290eeb02009-06-08 23:27:34 +0000339 // Treat these like attributes
340 // FIXME: Allow Sema to distinguish between these and real attributes!
341 while (Tok.is(tok::kw___fastcall) || Tok.is(tok::kw___stdcall) ||
Douglas Gregorf813a2c2010-05-18 16:57:00 +0000342 Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___cdecl) ||
Francois Pichet3bd9aa42011-08-18 09:59:55 +0000343 Tok.is(tok::kw___ptr64) || Tok.is(tok::kw___w64) ||
Francois Pichet58fd97a2011-08-25 00:36:46 +0000344 Tok.is(tok::kw___ptr32) ||
Francois Pichet3bd9aa42011-08-18 09:59:55 +0000345 Tok.is(tok::kw___unaligned)) {
Eli Friedman290eeb02009-06-08 23:27:34 +0000346 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
347 SourceLocation AttrNameLoc = ConsumeToken();
John McCall0b7e6782011-03-24 11:26:52 +0000348 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
Sean Hunt93f95f22012-06-18 16:13:52 +0000349 SourceLocation(), 0, 0, AttributeList::AS_Declspec);
Eli Friedman290eeb02009-06-08 23:27:34 +0000350 }
Steve Narofff59e17e2008-12-24 20:59:21 +0000351}
352
John McCall7f040a92010-12-24 02:08:15 +0000353void Parser::ParseBorlandTypeAttributes(ParsedAttributes &attrs) {
Dawn Perchik52fc3142010-09-03 01:29:35 +0000354 // Treat these like attributes
355 while (Tok.is(tok::kw___pascal)) {
356 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
357 SourceLocation AttrNameLoc = ConsumeToken();
John McCall0b7e6782011-03-24 11:26:52 +0000358 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
Sean Hunt93f95f22012-06-18 16:13:52 +0000359 SourceLocation(), 0, 0, AttributeList::AS_Declspec);
Dawn Perchik52fc3142010-09-03 01:29:35 +0000360 }
John McCall7f040a92010-12-24 02:08:15 +0000361}
362
Peter Collingbournef315fa82011-02-14 01:42:53 +0000363void Parser::ParseOpenCLAttributes(ParsedAttributes &attrs) {
364 // Treat these like attributes
365 while (Tok.is(tok::kw___kernel)) {
366 SourceLocation AttrNameLoc = ConsumeToken();
John McCall0b7e6782011-03-24 11:26:52 +0000367 attrs.addNew(PP.getIdentifierInfo("opencl_kernel_function"),
368 AttrNameLoc, 0, AttrNameLoc, 0,
Sean Hunt93f95f22012-06-18 16:13:52 +0000369 SourceLocation(), 0, 0, AttributeList::AS_GNU);
Peter Collingbournef315fa82011-02-14 01:42:53 +0000370 }
371}
372
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000373void Parser::ParseOpenCLQualifiers(DeclSpec &DS) {
374 SourceLocation Loc = Tok.getLocation();
375 switch(Tok.getKind()) {
376 // OpenCL qualifiers:
377 case tok::kw___private:
378 case tok::kw_private:
John McCall0b7e6782011-03-24 11:26:52 +0000379 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000380 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000381 PP.getIdentifierInfo("address_space"), Loc, 0);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000382 break;
383
384 case tok::kw___global:
John McCall0b7e6782011-03-24 11:26:52 +0000385 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000386 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000387 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_global);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000388 break;
389
390 case tok::kw___local:
John McCall0b7e6782011-03-24 11:26:52 +0000391 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000392 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000393 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_local);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000394 break;
395
396 case tok::kw___constant:
John McCall0b7e6782011-03-24 11:26:52 +0000397 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000398 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000399 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_constant);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000400 break;
401
402 case tok::kw___read_only:
John McCall0b7e6782011-03-24 11:26:52 +0000403 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000404 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000405 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_read_only);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000406 break;
407
408 case tok::kw___write_only:
John McCall0b7e6782011-03-24 11:26:52 +0000409 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000410 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000411 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_write_only);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000412 break;
413
414 case tok::kw___read_write:
John McCall0b7e6782011-03-24 11:26:52 +0000415 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000416 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000417 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_read_write);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000418 break;
419 default: break;
420 }
421}
422
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000423/// \brief Parse a version number.
424///
425/// version:
426/// simple-integer
427/// simple-integer ',' simple-integer
428/// simple-integer ',' simple-integer ',' simple-integer
429VersionTuple Parser::ParseVersionTuple(SourceRange &Range) {
430 Range = Tok.getLocation();
431
432 if (!Tok.is(tok::numeric_constant)) {
433 Diag(Tok, diag::err_expected_version);
434 SkipUntil(tok::comma, tok::r_paren, true, true, true);
435 return VersionTuple();
436 }
437
438 // Parse the major (and possibly minor and subminor) versions, which
439 // are stored in the numeric constant. We utilize a quirk of the
440 // lexer, which is that it handles something like 1.2.3 as a single
441 // numeric constant, rather than two separate tokens.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000442 SmallString<512> Buffer;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000443 Buffer.resize(Tok.getLength()+1);
444 const char *ThisTokBegin = &Buffer[0];
445
446 // Get the spelling of the token, which eliminates trigraphs, etc.
447 bool Invalid = false;
448 unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin, &Invalid);
449 if (Invalid)
450 return VersionTuple();
451
452 // Parse the major version.
453 unsigned AfterMajor = 0;
454 unsigned Major = 0;
455 while (AfterMajor < ActualLength && isdigit(ThisTokBegin[AfterMajor])) {
456 Major = Major * 10 + ThisTokBegin[AfterMajor] - '0';
457 ++AfterMajor;
458 }
459
460 if (AfterMajor == 0) {
461 Diag(Tok, diag::err_expected_version);
462 SkipUntil(tok::comma, tok::r_paren, true, true, true);
463 return VersionTuple();
464 }
465
466 if (AfterMajor == ActualLength) {
467 ConsumeToken();
468
469 // We only had a single version component.
470 if (Major == 0) {
471 Diag(Tok, diag::err_zero_version);
472 return VersionTuple();
473 }
474
475 return VersionTuple(Major);
476 }
477
478 if (ThisTokBegin[AfterMajor] != '.' || (AfterMajor + 1 == ActualLength)) {
479 Diag(Tok, diag::err_expected_version);
480 SkipUntil(tok::comma, tok::r_paren, true, true, true);
481 return VersionTuple();
482 }
483
484 // Parse the minor version.
485 unsigned AfterMinor = AfterMajor + 1;
486 unsigned Minor = 0;
487 while (AfterMinor < ActualLength && isdigit(ThisTokBegin[AfterMinor])) {
488 Minor = Minor * 10 + ThisTokBegin[AfterMinor] - '0';
489 ++AfterMinor;
490 }
491
492 if (AfterMinor == ActualLength) {
493 ConsumeToken();
494
495 // We had major.minor.
496 if (Major == 0 && Minor == 0) {
497 Diag(Tok, diag::err_zero_version);
498 return VersionTuple();
499 }
500
501 return VersionTuple(Major, Minor);
502 }
503
504 // If what follows is not a '.', we have a problem.
505 if (ThisTokBegin[AfterMinor] != '.') {
506 Diag(Tok, diag::err_expected_version);
507 SkipUntil(tok::comma, tok::r_paren, true, true, true);
508 return VersionTuple();
509 }
510
511 // Parse the subminor version.
512 unsigned AfterSubminor = AfterMinor + 1;
513 unsigned Subminor = 0;
514 while (AfterSubminor < ActualLength && isdigit(ThisTokBegin[AfterSubminor])) {
515 Subminor = Subminor * 10 + ThisTokBegin[AfterSubminor] - '0';
516 ++AfterSubminor;
517 }
518
519 if (AfterSubminor != ActualLength) {
520 Diag(Tok, diag::err_expected_version);
521 SkipUntil(tok::comma, tok::r_paren, true, true, true);
522 return VersionTuple();
523 }
524 ConsumeToken();
525 return VersionTuple(Major, Minor, Subminor);
526}
527
528/// \brief Parse the contents of the "availability" attribute.
529///
530/// availability-attribute:
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000531/// 'availability' '(' platform ',' version-arg-list, opt-message')'
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000532///
533/// platform:
534/// identifier
535///
536/// version-arg-list:
537/// version-arg
538/// version-arg ',' version-arg-list
539///
540/// version-arg:
541/// 'introduced' '=' version
542/// 'deprecated' '=' version
Douglas Gregor93a70672012-03-11 04:53:21 +0000543/// 'obsoleted' = version
Douglas Gregorb53e4172011-03-26 03:35:55 +0000544/// 'unavailable'
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000545/// opt-message:
546/// 'message' '=' <string>
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000547void Parser::ParseAvailabilityAttribute(IdentifierInfo &Availability,
548 SourceLocation AvailabilityLoc,
549 ParsedAttributes &attrs,
550 SourceLocation *endLoc) {
551 SourceLocation PlatformLoc;
552 IdentifierInfo *Platform = 0;
553
554 enum { Introduced, Deprecated, Obsoleted, Unknown };
555 AvailabilityChange Changes[Unknown];
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000556 ExprResult MessageExpr;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000557
558 // Opening '('.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000559 BalancedDelimiterTracker T(*this, tok::l_paren);
560 if (T.consumeOpen()) {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000561 Diag(Tok, diag::err_expected_lparen);
562 return;
563 }
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000564
565 // Parse the platform name,
566 if (Tok.isNot(tok::identifier)) {
567 Diag(Tok, diag::err_availability_expected_platform);
568 SkipUntil(tok::r_paren);
569 return;
570 }
571 Platform = Tok.getIdentifierInfo();
572 PlatformLoc = ConsumeToken();
573
574 // Parse the ',' following the platform name.
575 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "", tok::r_paren))
576 return;
577
578 // If we haven't grabbed the pointers for the identifiers
579 // "introduced", "deprecated", and "obsoleted", do so now.
580 if (!Ident_introduced) {
581 Ident_introduced = PP.getIdentifierInfo("introduced");
582 Ident_deprecated = PP.getIdentifierInfo("deprecated");
583 Ident_obsoleted = PP.getIdentifierInfo("obsoleted");
Douglas Gregorb53e4172011-03-26 03:35:55 +0000584 Ident_unavailable = PP.getIdentifierInfo("unavailable");
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000585 Ident_message = PP.getIdentifierInfo("message");
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000586 }
587
588 // Parse the set of introductions/deprecations/removals.
Douglas Gregorb53e4172011-03-26 03:35:55 +0000589 SourceLocation UnavailableLoc;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000590 do {
591 if (Tok.isNot(tok::identifier)) {
592 Diag(Tok, diag::err_availability_expected_change);
593 SkipUntil(tok::r_paren);
594 return;
595 }
596 IdentifierInfo *Keyword = Tok.getIdentifierInfo();
597 SourceLocation KeywordLoc = ConsumeToken();
598
Douglas Gregorb53e4172011-03-26 03:35:55 +0000599 if (Keyword == Ident_unavailable) {
600 if (UnavailableLoc.isValid()) {
601 Diag(KeywordLoc, diag::err_availability_redundant)
602 << Keyword << SourceRange(UnavailableLoc);
603 }
604 UnavailableLoc = KeywordLoc;
605
606 if (Tok.isNot(tok::comma))
607 break;
608
609 ConsumeToken();
610 continue;
611 }
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000612
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000613 if (Tok.isNot(tok::equal)) {
614 Diag(Tok, diag::err_expected_equal_after)
615 << Keyword;
616 SkipUntil(tok::r_paren);
617 return;
618 }
619 ConsumeToken();
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000620 if (Keyword == Ident_message) {
621 if (!isTokenStringLiteral()) {
622 Diag(Tok, diag::err_expected_string_literal);
623 SkipUntil(tok::r_paren);
624 return;
625 }
626 MessageExpr = ParseStringLiteralExpression();
627 break;
628 }
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000629
630 SourceRange VersionRange;
631 VersionTuple Version = ParseVersionTuple(VersionRange);
632
633 if (Version.empty()) {
634 SkipUntil(tok::r_paren);
635 return;
636 }
637
638 unsigned Index;
639 if (Keyword == Ident_introduced)
640 Index = Introduced;
641 else if (Keyword == Ident_deprecated)
642 Index = Deprecated;
643 else if (Keyword == Ident_obsoleted)
644 Index = Obsoleted;
645 else
646 Index = Unknown;
647
648 if (Index < Unknown) {
649 if (!Changes[Index].KeywordLoc.isInvalid()) {
650 Diag(KeywordLoc, diag::err_availability_redundant)
651 << Keyword
652 << SourceRange(Changes[Index].KeywordLoc,
653 Changes[Index].VersionRange.getEnd());
654 }
655
656 Changes[Index].KeywordLoc = KeywordLoc;
657 Changes[Index].Version = Version;
658 Changes[Index].VersionRange = VersionRange;
659 } else {
660 Diag(KeywordLoc, diag::err_availability_unknown_change)
661 << Keyword << VersionRange;
662 }
663
664 if (Tok.isNot(tok::comma))
665 break;
666
667 ConsumeToken();
668 } while (true);
669
670 // Closing ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000671 if (T.consumeClose())
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000672 return;
673
674 if (endLoc)
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000675 *endLoc = T.getCloseLocation();
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000676
Douglas Gregorb53e4172011-03-26 03:35:55 +0000677 // The 'unavailable' availability cannot be combined with any other
678 // availability changes. Make sure that hasn't happened.
679 if (UnavailableLoc.isValid()) {
680 bool Complained = false;
681 for (unsigned Index = Introduced; Index != Unknown; ++Index) {
682 if (Changes[Index].KeywordLoc.isValid()) {
683 if (!Complained) {
684 Diag(UnavailableLoc, diag::warn_availability_and_unavailable)
685 << SourceRange(Changes[Index].KeywordLoc,
686 Changes[Index].VersionRange.getEnd());
687 Complained = true;
688 }
689
690 // Clear out the availability.
691 Changes[Index] = AvailabilityChange();
692 }
693 }
694 }
695
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000696 // Record this attribute
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000697 attrs.addNew(&Availability,
698 SourceRange(AvailabilityLoc, T.getCloseLocation()),
Fariborz Jahanianf96708d2012-01-23 23:38:32 +0000699 0, AvailabilityLoc,
John McCall0b7e6782011-03-24 11:26:52 +0000700 Platform, PlatformLoc,
701 Changes[Introduced],
702 Changes[Deprecated],
Douglas Gregorb53e4172011-03-26 03:35:55 +0000703 Changes[Obsoleted],
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000704 UnavailableLoc, MessageExpr.take(),
Sean Hunt93f95f22012-06-18 16:13:52 +0000705 AttributeList::AS_GNU);
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000706}
707
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000708
709// Late Parsed Attributes:
710// See other examples of late parsing in lib/Parse/ParseCXXInlineMethods
711
712void Parser::LateParsedDeclaration::ParseLexedAttributes() {}
713
714void Parser::LateParsedClass::ParseLexedAttributes() {
715 Self->ParseLexedAttributes(*Class);
716}
717
718void Parser::LateParsedAttribute::ParseLexedAttributes() {
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000719 Self->ParseLexedAttribute(*this, true, false);
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000720}
721
722/// Wrapper class which calls ParseLexedAttribute, after setting up the
723/// scope appropriately.
724void Parser::ParseLexedAttributes(ParsingClass &Class) {
725 // Deal with templates
726 // FIXME: Test cases to make sure this does the right thing for templates.
727 bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope;
728 ParseScope ClassTemplateScope(this, Scope::TemplateParamScope,
729 HasTemplateScope);
730 if (HasTemplateScope)
731 Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate);
732
Douglas Gregorcefc3af2012-04-16 07:05:22 +0000733 // Set or update the scope flags.
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000734 bool AlreadyHasClassScope = Class.TopLevelClass;
Douglas Gregorcefc3af2012-04-16 07:05:22 +0000735 unsigned ScopeFlags = Scope::ClassScope|Scope::DeclScope;
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000736 ParseScope ClassScope(this, ScopeFlags, !AlreadyHasClassScope);
737 ParseScopeFlags ClassScopeFlags(this, ScopeFlags, AlreadyHasClassScope);
738
DeLesley Hutchinscf2fa2f2012-04-06 15:10:17 +0000739 // Enter the scope of nested classes
740 if (!AlreadyHasClassScope)
741 Actions.ActOnStartDelayedMemberDeclarations(getCurScope(),
742 Class.TagOrTemplate);
Benjamin Kramer268efba2012-05-17 12:01:52 +0000743 if (!Class.LateParsedDeclarations.empty()) {
Douglas Gregorcefc3af2012-04-16 07:05:22 +0000744 // Allow 'this' within late-parsed attributes.
745 Sema::CXXThisScopeRAII ThisScope(Actions, Class.TagOrTemplate,
746 /*TypeQuals=*/0);
747
748 for (unsigned i = 0, ni = Class.LateParsedDeclarations.size(); i < ni; ++i){
749 Class.LateParsedDeclarations[i]->ParseLexedAttributes();
750 }
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000751 }
Douglas Gregorcefc3af2012-04-16 07:05:22 +0000752
DeLesley Hutchinscf2fa2f2012-04-06 15:10:17 +0000753 if (!AlreadyHasClassScope)
754 Actions.ActOnFinishDelayedMemberDeclarations(getCurScope(),
755 Class.TagOrTemplate);
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000756}
757
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000758
759/// \brief Parse all attributes in LAs, and attach them to Decl D.
760void Parser::ParseLexedAttributeList(LateParsedAttrList &LAs, Decl *D,
761 bool EnterScope, bool OnDefinition) {
762 for (unsigned i = 0, ni = LAs.size(); i < ni; ++i) {
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +0000763 LAs[i]->addDecl(D);
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000764 ParseLexedAttribute(*LAs[i], EnterScope, OnDefinition);
Benjamin Kramerd306cf72012-04-14 12:44:47 +0000765 delete LAs[i];
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000766 }
767 LAs.clear();
768}
769
770
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000771/// \brief Finish parsing an attribute for which parsing was delayed.
772/// This will be called at the end of parsing a class declaration
773/// for each LateParsedAttribute. We consume the saved tokens and
774/// create an attribute with the arguments filled in. We add this
775/// to the Attribute list for the decl.
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000776void Parser::ParseLexedAttribute(LateParsedAttribute &LA,
777 bool EnterScope, bool OnDefinition) {
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000778 // Save the current token position.
779 SourceLocation OrigLoc = Tok.getLocation();
780
781 // Append the current token at the end of the new token stream so that it
782 // doesn't get lost.
783 LA.Toks.push_back(Tok);
784 PP.EnterTokenStream(LA.Toks.data(), LA.Toks.size(), true, false);
785 // Consume the previously pushed token.
786 ConsumeAnyToken();
787
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000788 if (OnDefinition && !IsThreadSafetyAttribute(LA.AttrName.getName())) {
789 Diag(Tok, diag::warn_attribute_on_function_definition)
790 << LA.AttrName.getName();
791 }
792
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000793 ParsedAttributes Attrs(AttrFactory);
794 SourceLocation endLoc;
795
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +0000796 if (LA.Decls.size() == 1) {
797 Decl *D = LA.Decls[0];
Caitlin Sadowskied9d84a2011-09-08 17:42:31 +0000798
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +0000799 // If the Decl is templatized, add template parameters to scope.
800 bool HasTemplateScope = EnterScope && D->isTemplateDecl();
801 ParseScope TempScope(this, Scope::TemplateParamScope, HasTemplateScope);
802 if (HasTemplateScope)
803 Actions.ActOnReenterTemplateScope(Actions.CurScope, D);
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000804
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +0000805 // If the Decl is on a function, add function parameters to the scope.
806 bool HasFunctionScope = EnterScope && D->isFunctionOrFunctionTemplate();
807 ParseScope FnScope(this, Scope::FnScope|Scope::DeclScope, HasFunctionScope);
808 if (HasFunctionScope)
809 Actions.ActOnReenterFunctionContext(Actions.CurScope, D);
810
811 ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, &endLoc);
812
813 if (HasFunctionScope) {
814 Actions.ActOnExitFunctionContext();
815 FnScope.Exit(); // Pop scope, and remove Decls from IdResolver
816 }
817 if (HasTemplateScope) {
818 TempScope.Exit();
819 }
DeLesley Hutchins7ec419a2012-03-02 22:29:50 +0000820 } else if (LA.Decls.size() > 0) {
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +0000821 // If there are multiple decls, then the decl cannot be within the
822 // function scope.
823 ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, &endLoc);
DeLesley Hutchins7ec419a2012-03-02 22:29:50 +0000824 } else {
825 Diag(Tok, diag::warn_attribute_no_decl) << LA.AttrName.getName();
Caitlin Sadowskied9d84a2011-09-08 17:42:31 +0000826 }
827
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +0000828 for (unsigned i = 0, ni = LA.Decls.size(); i < ni; ++i) {
829 Actions.ActOnFinishDelayedAttribute(getCurScope(), LA.Decls[i], Attrs);
830 }
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000831
832 if (Tok.getLocation() != OrigLoc) {
833 // Due to a parsing error, we either went over the cached tokens or
834 // there are still cached tokens left, so we skip the leftover tokens.
835 // Since this is an uncommon situation that should be avoided, use the
836 // expensive isBeforeInTranslationUnit call.
837 if (PP.getSourceManager().isBeforeInTranslationUnit(Tok.getLocation(),
838 OrigLoc))
839 while (Tok.getLocation() != OrigLoc && Tok.isNot(tok::eof))
Douglas Gregord78ef5b2012-03-08 01:00:17 +0000840 ConsumeAnyToken();
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000841 }
842}
843
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000844/// \brief Wrapper around a case statement checking if AttrName is
845/// one of the thread safety attributes
846bool Parser::IsThreadSafetyAttribute(llvm::StringRef AttrName){
847 return llvm::StringSwitch<bool>(AttrName)
848 .Case("guarded_by", true)
849 .Case("guarded_var", true)
850 .Case("pt_guarded_by", true)
851 .Case("pt_guarded_var", true)
852 .Case("lockable", true)
853 .Case("scoped_lockable", true)
854 .Case("no_thread_safety_analysis", true)
855 .Case("acquired_after", true)
856 .Case("acquired_before", true)
857 .Case("exclusive_lock_function", true)
858 .Case("shared_lock_function", true)
859 .Case("exclusive_trylock_function", true)
860 .Case("shared_trylock_function", true)
861 .Case("unlock_function", true)
862 .Case("lock_returned", true)
863 .Case("locks_excluded", true)
864 .Case("exclusive_locks_required", true)
865 .Case("shared_locks_required", true)
866 .Default(false);
867}
868
869/// \brief Parse the contents of thread safety attributes. These
870/// should always be parsed as an expression list.
871///
872/// We need to special case the parsing due to the fact that if the first token
873/// of the first argument is an identifier, the main parse loop will store
874/// that token as a "parameter" and the rest of
875/// the arguments will be added to a list of "arguments". However,
876/// subsequent tokens in the first argument are lost. We instead parse each
877/// argument as an expression and add all arguments to the list of "arguments".
878/// In future, we will take advantage of this special case to also
879/// deal with some argument scoping issues here (for example, referring to a
880/// function parameter in the attribute on that function).
881void Parser::ParseThreadSafetyAttribute(IdentifierInfo &AttrName,
882 SourceLocation AttrNameLoc,
883 ParsedAttributes &Attrs,
884 SourceLocation *EndLoc) {
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000885 assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('");
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000886
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000887 BalancedDelimiterTracker T(*this, tok::l_paren);
888 T.consumeOpen();
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000889
890 ExprVector ArgExprs(Actions);
891 bool ArgExprsOk = true;
892
893 // now parse the list of expressions
DeLesley Hutchins4805f152011-12-14 19:36:06 +0000894 while (Tok.isNot(tok::r_paren)) {
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000895 ExprResult ArgExpr(ParseAssignmentExpression());
896 if (ArgExpr.isInvalid()) {
897 ArgExprsOk = false;
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000898 T.consumeClose();
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000899 break;
900 } else {
901 ArgExprs.push_back(ArgExpr.release());
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000902 }
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000903 if (Tok.isNot(tok::comma))
904 break;
905 ConsumeToken(); // Eat the comma, move to the next argument
906 }
907 // Match the ')'.
DeLesley Hutchins23323e02012-01-20 22:50:54 +0000908 if (ArgExprsOk && !T.consumeClose()) {
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000909 Attrs.addNew(&AttrName, AttrNameLoc, 0, AttrNameLoc, 0, SourceLocation(),
Sean Hunt93f95f22012-06-18 16:13:52 +0000910 ArgExprs.take(), ArgExprs.size(), AttributeList::AS_GNU);
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000911 }
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000912 if (EndLoc)
913 *EndLoc = T.getCloseLocation();
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000914}
915
Richard Smith6ee326a2012-04-10 01:32:12 +0000916/// DiagnoseProhibitedCXX11Attribute - We have found the opening square brackets
917/// of a C++11 attribute-specifier in a location where an attribute is not
918/// permitted. By C++11 [dcl.attr.grammar]p6, this is ill-formed. Diagnose this
919/// situation.
920///
921/// \return \c true if we skipped an attribute-like chunk of tokens, \c false if
922/// this doesn't appear to actually be an attribute-specifier, and the caller
923/// should try to parse it.
924bool Parser::DiagnoseProhibitedCXX11Attribute() {
925 assert(Tok.is(tok::l_square) && NextToken().is(tok::l_square));
926
927 switch (isCXX11AttributeSpecifier(/*Disambiguate*/true)) {
928 case CAK_NotAttributeSpecifier:
929 // No diagnostic: we're in Obj-C++11 and this is not actually an attribute.
930 return false;
931
932 case CAK_InvalidAttributeSpecifier:
933 Diag(Tok.getLocation(), diag::err_l_square_l_square_not_attribute);
934 return false;
935
936 case CAK_AttributeSpecifier:
937 // Parse and discard the attributes.
938 SourceLocation BeginLoc = ConsumeBracket();
939 ConsumeBracket();
940 SkipUntil(tok::r_square, /*StopAtSemi*/ false);
941 assert(Tok.is(tok::r_square) && "isCXX11AttributeSpecifier lied");
942 SourceLocation EndLoc = ConsumeBracket();
943 Diag(BeginLoc, diag::err_attributes_not_allowed)
944 << SourceRange(BeginLoc, EndLoc);
945 return true;
946 }
Chandler Carruth2c6dbd72012-04-10 16:03:08 +0000947 llvm_unreachable("All cases handled above.");
Richard Smith6ee326a2012-04-10 01:32:12 +0000948}
949
John McCall7f040a92010-12-24 02:08:15 +0000950void Parser::DiagnoseProhibitedAttributes(ParsedAttributesWithRange &attrs) {
951 Diag(attrs.Range.getBegin(), diag::err_attributes_not_allowed)
952 << attrs.Range;
Dawn Perchik52fc3142010-09-03 01:29:35 +0000953}
954
Reid Spencer5f016e22007-07-11 17:01:13 +0000955/// ParseDeclaration - Parse a full 'declaration', which consists of
956/// declaration-specifiers, some number of declarators, and a semicolon.
Chris Lattner97144fc2009-04-02 04:16:50 +0000957/// 'Context' should be a Declarator::TheContext value. This returns the
958/// location of the semicolon in DeclEnd.
Chris Lattner8f08cb72007-08-25 06:57:03 +0000959///
960/// declaration: [C99 6.7]
961/// block-declaration ->
962/// simple-declaration
963/// others [FIXME]
Douglas Gregoradcac882008-12-01 23:54:00 +0000964/// [C++] template-declaration
Chris Lattner8f08cb72007-08-25 06:57:03 +0000965/// [C++] namespace-definition
Douglas Gregorf780abc2008-12-30 03:27:21 +0000966/// [C++] using-directive
Douglas Gregord7f37bf2009-06-22 23:06:13 +0000967/// [C++] using-declaration
Richard Smith534986f2012-04-14 00:33:13 +0000968/// [C++11/C11] static_assert-declaration
Chris Lattner8f08cb72007-08-25 06:57:03 +0000969/// others... [FIXME]
970///
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +0000971Parser::DeclGroupPtrTy Parser::ParseDeclaration(StmtVector &Stmts,
972 unsigned Context,
Sean Huntbbd37c62009-11-21 08:43:09 +0000973 SourceLocation &DeclEnd,
John McCall7f040a92010-12-24 02:08:15 +0000974 ParsedAttributesWithRange &attrs) {
Argyrios Kyrtzidis36d36802010-06-17 10:52:18 +0000975 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Fariborz Jahaniane8cff362011-08-30 17:10:52 +0000976 // Must temporarily exit the objective-c container scope for
977 // parsing c none objective-c decls.
978 ObjCDeclContextSwitch ObjCDC(*this);
Argyrios Kyrtzidis36d36802010-06-17 10:52:18 +0000979
John McCalld226f652010-08-21 09:40:31 +0000980 Decl *SingleDecl = 0;
Richard Smithc89edf52011-07-01 19:46:12 +0000981 Decl *OwnedType = 0;
Chris Lattner8f08cb72007-08-25 06:57:03 +0000982 switch (Tok.getKind()) {
Douglas Gregoradcac882008-12-01 23:54:00 +0000983 case tok::kw_template:
Douglas Gregor1426e532009-05-12 21:31:51 +0000984 case tok::kw_export:
John McCall7f040a92010-12-24 02:08:15 +0000985 ProhibitAttributes(attrs);
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000986 SingleDecl = ParseDeclarationStartingWithTemplate(Context, DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +0000987 break;
Sebastian Redld078e642010-08-27 23:12:46 +0000988 case tok::kw_inline:
Sebastian Redl88e64ca2010-08-31 00:36:45 +0000989 // Could be the start of an inline namespace. Allowed as an ext in C++03.
David Blaikie4e4d0842012-03-11 07:00:24 +0000990 if (getLangOpts().CPlusPlus && NextToken().is(tok::kw_namespace)) {
John McCall7f040a92010-12-24 02:08:15 +0000991 ProhibitAttributes(attrs);
Sebastian Redld078e642010-08-27 23:12:46 +0000992 SourceLocation InlineLoc = ConsumeToken();
993 SingleDecl = ParseNamespace(Context, DeclEnd, InlineLoc);
994 break;
995 }
John McCall7f040a92010-12-24 02:08:15 +0000996 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs,
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +0000997 true);
Chris Lattner8f08cb72007-08-25 06:57:03 +0000998 case tok::kw_namespace:
John McCall7f040a92010-12-24 02:08:15 +0000999 ProhibitAttributes(attrs);
Chris Lattner97144fc2009-04-02 04:16:50 +00001000 SingleDecl = ParseNamespace(Context, DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +00001001 break;
Douglas Gregorf780abc2008-12-30 03:27:21 +00001002 case tok::kw_using:
John McCall78b81052010-11-10 02:40:36 +00001003 SingleDecl = ParseUsingDirectiveOrDeclaration(Context, ParsedTemplateInfo(),
Richard Smithc89edf52011-07-01 19:46:12 +00001004 DeclEnd, attrs, &OwnedType);
Chris Lattner682bf922009-03-29 16:50:03 +00001005 break;
Anders Carlsson511d7ab2009-03-11 16:27:10 +00001006 case tok::kw_static_assert:
Peter Collingbournec6eb44b2011-04-15 00:35:57 +00001007 case tok::kw__Static_assert:
John McCall7f040a92010-12-24 02:08:15 +00001008 ProhibitAttributes(attrs);
Chris Lattner97144fc2009-04-02 04:16:50 +00001009 SingleDecl = ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +00001010 break;
Chris Lattner8f08cb72007-08-25 06:57:03 +00001011 default:
John McCall7f040a92010-12-24 02:08:15 +00001012 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs, true);
Chris Lattner8f08cb72007-08-25 06:57:03 +00001013 }
Sean Huntbbd37c62009-11-21 08:43:09 +00001014
Chris Lattner682bf922009-03-29 16:50:03 +00001015 // This routine returns a DeclGroup, if the thing we parsed only contains a
Richard Smithc89edf52011-07-01 19:46:12 +00001016 // single decl, convert it now. Alias declarations can also declare a type;
1017 // include that too if it is present.
1018 return Actions.ConvertDeclToDeclGroup(SingleDecl, OwnedType);
Chris Lattner8f08cb72007-08-25 06:57:03 +00001019}
1020
1021/// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl]
1022/// declaration-specifiers init-declarator-list[opt] ';'
1023///[C90/C++]init-declarator-list ';' [TODO]
1024/// [OMP] threadprivate-directive [TODO]
Chris Lattnercd147752009-03-29 17:27:48 +00001025///
Richard Smithad762fc2011-04-14 22:09:26 +00001026/// for-range-declaration: [C++0x 6.5p1: stmt.ranged]
1027/// attribute-specifier-seq[opt] type-specifier-seq declarator
1028///
Chris Lattnercd147752009-03-29 17:27:48 +00001029/// If RequireSemi is false, this does not check for a ';' at the end of the
Chris Lattner5c5db552010-04-05 18:18:31 +00001030/// declaration. If it is true, it checks for and eats it.
Richard Smithad762fc2011-04-14 22:09:26 +00001031///
1032/// If FRI is non-null, we might be parsing a for-range-declaration instead
1033/// of a simple-declaration. If we find that we are, we also parse the
1034/// for-range-initializer, and place it here.
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +00001035Parser::DeclGroupPtrTy Parser::ParseSimpleDeclaration(StmtVector &Stmts,
1036 unsigned Context,
Sean Huntbbd37c62009-11-21 08:43:09 +00001037 SourceLocation &DeclEnd,
John McCall7f040a92010-12-24 02:08:15 +00001038 ParsedAttributes &attrs,
Richard Smithad762fc2011-04-14 22:09:26 +00001039 bool RequireSemi,
1040 ForRangeInit *FRI) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001041 // Parse the common declaration-specifiers piece.
John McCall54abf7d2009-11-04 02:18:39 +00001042 ParsingDeclSpec DS(*this);
John McCall7f040a92010-12-24 02:08:15 +00001043 DS.takeAttributesFrom(attrs);
Douglas Gregor312eadb2011-04-24 05:37:28 +00001044
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001045 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS_none,
Richard Smith34b41d92011-02-20 03:19:35 +00001046 getDeclSpecContextFromDeclaratorContext(Context));
Abramo Bagnara06284c12012-01-07 10:52:36 +00001047
Reid Spencer5f016e22007-07-11 17:01:13 +00001048 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
1049 // declaration-specifiers init-declarator-list[opt] ';'
Chris Lattner04d66662007-10-09 17:33:22 +00001050 if (Tok.is(tok::semi)) {
Argyrios Kyrtzidis5641b0d2012-05-16 23:49:15 +00001051 DeclEnd = Tok.getLocation();
Chris Lattner5c5db552010-04-05 18:18:31 +00001052 if (RequireSemi) ConsumeToken();
John McCalld226f652010-08-21 09:40:31 +00001053 Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none,
Douglas Gregor312eadb2011-04-24 05:37:28 +00001054 DS);
John McCall54abf7d2009-11-04 02:18:39 +00001055 DS.complete(TheDecl);
Chris Lattner682bf922009-03-29 16:50:03 +00001056 return Actions.ConvertDeclToDeclGroup(TheDecl);
Reid Spencer5f016e22007-07-11 17:01:13 +00001057 }
Douglas Gregor312eadb2011-04-24 05:37:28 +00001058
1059 return ParseDeclGroup(DS, Context, /*FunctionDefs=*/ false, &DeclEnd, FRI);
John McCalld8ac0572009-11-03 19:26:08 +00001060}
Mike Stump1eb44332009-09-09 15:08:12 +00001061
Richard Smith0706df42011-10-19 21:33:05 +00001062/// Returns true if this might be the start of a declarator, or a common typo
1063/// for a declarator.
1064bool Parser::MightBeDeclarator(unsigned Context) {
1065 switch (Tok.getKind()) {
1066 case tok::annot_cxxscope:
1067 case tok::annot_template_id:
1068 case tok::caret:
1069 case tok::code_completion:
1070 case tok::coloncolon:
1071 case tok::ellipsis:
1072 case tok::kw___attribute:
1073 case tok::kw_operator:
1074 case tok::l_paren:
1075 case tok::star:
1076 return true;
1077
1078 case tok::amp:
1079 case tok::ampamp:
David Blaikie4e4d0842012-03-11 07:00:24 +00001080 return getLangOpts().CPlusPlus;
Richard Smith0706df42011-10-19 21:33:05 +00001081
Richard Smith1c94c162012-01-09 22:31:44 +00001082 case tok::l_square: // Might be an attribute on an unnamed bit-field.
David Blaikie4e4d0842012-03-11 07:00:24 +00001083 return Context == Declarator::MemberContext && getLangOpts().CPlusPlus0x &&
Richard Smith1c94c162012-01-09 22:31:44 +00001084 NextToken().is(tok::l_square);
1085
1086 case tok::colon: // Might be a typo for '::' or an unnamed bit-field.
David Blaikie4e4d0842012-03-11 07:00:24 +00001087 return Context == Declarator::MemberContext || getLangOpts().CPlusPlus;
Richard Smith1c94c162012-01-09 22:31:44 +00001088
Richard Smith0706df42011-10-19 21:33:05 +00001089 case tok::identifier:
1090 switch (NextToken().getKind()) {
1091 case tok::code_completion:
1092 case tok::coloncolon:
1093 case tok::comma:
1094 case tok::equal:
1095 case tok::equalequal: // Might be a typo for '='.
1096 case tok::kw_alignas:
1097 case tok::kw_asm:
1098 case tok::kw___attribute:
1099 case tok::l_brace:
1100 case tok::l_paren:
1101 case tok::l_square:
1102 case tok::less:
1103 case tok::r_brace:
1104 case tok::r_paren:
1105 case tok::r_square:
1106 case tok::semi:
1107 return true;
1108
1109 case tok::colon:
1110 // At namespace scope, 'identifier:' is probably a typo for 'identifier::'
Richard Smith1c94c162012-01-09 22:31:44 +00001111 // and in block scope it's probably a label. Inside a class definition,
1112 // this is a bit-field.
1113 return Context == Declarator::MemberContext ||
David Blaikie4e4d0842012-03-11 07:00:24 +00001114 (getLangOpts().CPlusPlus && Context == Declarator::FileContext);
Richard Smith1c94c162012-01-09 22:31:44 +00001115
1116 case tok::identifier: // Possible virt-specifier.
David Blaikie4e4d0842012-03-11 07:00:24 +00001117 return getLangOpts().CPlusPlus0x && isCXX0XVirtSpecifier(NextToken());
Richard Smith0706df42011-10-19 21:33:05 +00001118
1119 default:
1120 return false;
1121 }
1122
1123 default:
1124 return false;
1125 }
1126}
1127
Richard Smith994d73f2012-04-11 20:59:20 +00001128/// Skip until we reach something which seems like a sensible place to pick
1129/// up parsing after a malformed declaration. This will sometimes stop sooner
1130/// than SkipUntil(tok::r_brace) would, but will never stop later.
1131void Parser::SkipMalformedDecl() {
1132 while (true) {
1133 switch (Tok.getKind()) {
1134 case tok::l_brace:
1135 // Skip until matching }, then stop. We've probably skipped over
1136 // a malformed class or function definition or similar.
1137 ConsumeBrace();
1138 SkipUntil(tok::r_brace, /*StopAtSemi*/false);
1139 if (Tok.is(tok::comma) || Tok.is(tok::l_brace) || Tok.is(tok::kw_try)) {
1140 // This declaration isn't over yet. Keep skipping.
1141 continue;
1142 }
1143 if (Tok.is(tok::semi))
1144 ConsumeToken();
1145 return;
1146
1147 case tok::l_square:
1148 ConsumeBracket();
1149 SkipUntil(tok::r_square, /*StopAtSemi*/false);
1150 continue;
1151
1152 case tok::l_paren:
1153 ConsumeParen();
1154 SkipUntil(tok::r_paren, /*StopAtSemi*/false);
1155 continue;
1156
1157 case tok::r_brace:
1158 return;
1159
1160 case tok::semi:
1161 ConsumeToken();
1162 return;
1163
1164 case tok::kw_inline:
1165 // 'inline namespace' at the start of a line is almost certainly
1166 // a good place to pick back up parsing.
1167 if (Tok.isAtStartOfLine() && NextToken().is(tok::kw_namespace))
1168 return;
1169 break;
1170
1171 case tok::kw_namespace:
1172 // 'namespace' at the start of a line is almost certainly a good
1173 // place to pick back up parsing.
1174 if (Tok.isAtStartOfLine())
1175 return;
1176 break;
1177
1178 case tok::eof:
1179 return;
1180
1181 default:
1182 break;
1183 }
1184
1185 ConsumeAnyToken();
1186 }
1187}
1188
John McCalld8ac0572009-11-03 19:26:08 +00001189/// ParseDeclGroup - Having concluded that this is either a function
1190/// definition or a group of object declarations, actually parse the
1191/// result.
John McCall54abf7d2009-11-04 02:18:39 +00001192Parser::DeclGroupPtrTy Parser::ParseDeclGroup(ParsingDeclSpec &DS,
1193 unsigned Context,
John McCalld8ac0572009-11-03 19:26:08 +00001194 bool AllowFunctionDefinitions,
Richard Smithad762fc2011-04-14 22:09:26 +00001195 SourceLocation *DeclEnd,
1196 ForRangeInit *FRI) {
John McCalld8ac0572009-11-03 19:26:08 +00001197 // Parse the first declarator.
John McCall54abf7d2009-11-04 02:18:39 +00001198 ParsingDeclarator D(*this, DS, static_cast<Declarator::TheContext>(Context));
John McCalld8ac0572009-11-03 19:26:08 +00001199 ParseDeclarator(D);
Chris Lattnercd147752009-03-29 17:27:48 +00001200
John McCalld8ac0572009-11-03 19:26:08 +00001201 // Bail out if the first declarator didn't seem well-formed.
1202 if (!D.hasName() && !D.mayOmitIdentifier()) {
Richard Smith994d73f2012-04-11 20:59:20 +00001203 SkipMalformedDecl();
John McCalld8ac0572009-11-03 19:26:08 +00001204 return DeclGroupPtrTy();
Chris Lattner23c4b182009-03-29 17:18:04 +00001205 }
Mike Stump1eb44332009-09-09 15:08:12 +00001206
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001207 // Save late-parsed attributes for now; they need to be parsed in the
1208 // appropriate function scope after the function Decl has been constructed.
1209 LateParsedAttrList LateParsedAttrs;
1210 if (D.isFunctionDeclarator())
1211 MaybeParseGNUAttributes(D, &LateParsedAttrs);
1212
Chris Lattnerc82daef2010-07-11 22:24:20 +00001213 // Check to see if we have a function *definition* which must have a body.
1214 if (AllowFunctionDefinitions && D.isFunctionDeclarator() &&
1215 // Look at the next token to make sure that this isn't a function
1216 // declaration. We have to check this because __attribute__ might be the
1217 // start of a function definition in GCC-extended K&R C.
1218 !isDeclarationAfterDeclarator()) {
Richard Smith58196dc2011-11-30 23:45:35 +00001219
Chris Lattner004659a2010-07-11 22:42:07 +00001220 if (isStartOfFunctionDefinition(D)) {
John McCalld8ac0572009-11-03 19:26:08 +00001221 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
1222 Diag(Tok, diag::err_function_declared_typedef);
1223
1224 // Recover by treating the 'typedef' as spurious.
1225 DS.ClearStorageClassSpecs();
1226 }
1227
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001228 Decl *TheDecl =
1229 ParseFunctionDefinition(D, ParsedTemplateInfo(), &LateParsedAttrs);
John McCalld8ac0572009-11-03 19:26:08 +00001230 return Actions.ConvertDeclToDeclGroup(TheDecl);
Chris Lattner004659a2010-07-11 22:42:07 +00001231 }
1232
1233 if (isDeclarationSpecifier()) {
1234 // If there is an invalid declaration specifier right after the function
1235 // prototype, then we must be in a missing semicolon case where this isn't
1236 // actually a body. Just fall through into the code that handles it as a
1237 // prototype, and let the top-level code handle the erroneous declspec
1238 // where it would otherwise expect a comma or semicolon.
John McCalld8ac0572009-11-03 19:26:08 +00001239 } else {
1240 Diag(Tok, diag::err_expected_fn_body);
1241 SkipUntil(tok::semi);
1242 return DeclGroupPtrTy();
1243 }
1244 }
1245
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001246 if (ParseAsmAttributesAfterDeclarator(D))
Richard Smithad762fc2011-04-14 22:09:26 +00001247 return DeclGroupPtrTy();
1248
1249 // C++0x [stmt.iter]p1: Check if we have a for-range-declarator. If so, we
1250 // must parse and analyze the for-range-initializer before the declaration is
1251 // analyzed.
1252 if (FRI && Tok.is(tok::colon)) {
1253 FRI->ColonLoc = ConsumeToken();
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001254 if (Tok.is(tok::l_brace))
1255 FRI->RangeExpr = ParseBraceInitializer();
1256 else
1257 FRI->RangeExpr = ParseExpression();
Richard Smithad762fc2011-04-14 22:09:26 +00001258 Decl *ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
1259 Actions.ActOnCXXForRangeDecl(ThisDecl);
1260 Actions.FinalizeDeclaration(ThisDecl);
John McCall6895a642012-01-27 01:29:43 +00001261 D.complete(ThisDecl);
Richard Smithad762fc2011-04-14 22:09:26 +00001262 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS, &ThisDecl, 1);
1263 }
1264
Chris Lattner5f9e2722011-07-23 10:55:15 +00001265 SmallVector<Decl *, 8> DeclsInGroup;
Richard Smithad762fc2011-04-14 22:09:26 +00001266 Decl *FirstDecl = ParseDeclarationAfterDeclaratorAndAttributes(D);
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001267 if (LateParsedAttrs.size() > 0)
1268 ParseLexedAttributeList(LateParsedAttrs, FirstDecl, true, false);
John McCall54abf7d2009-11-04 02:18:39 +00001269 D.complete(FirstDecl);
John McCalld226f652010-08-21 09:40:31 +00001270 if (FirstDecl)
John McCalld8ac0572009-11-03 19:26:08 +00001271 DeclsInGroup.push_back(FirstDecl);
1272
Richard Smith0706df42011-10-19 21:33:05 +00001273 bool ExpectSemi = Context != Declarator::ForContext;
1274
John McCalld8ac0572009-11-03 19:26:08 +00001275 // If we don't have a comma, it is either the end of the list (a ';') or an
1276 // error, bail out.
1277 while (Tok.is(tok::comma)) {
Richard Smith0706df42011-10-19 21:33:05 +00001278 SourceLocation CommaLoc = ConsumeToken();
1279
1280 if (Tok.isAtStartOfLine() && ExpectSemi && !MightBeDeclarator(Context)) {
1281 // This comma was followed by a line-break and something which can't be
1282 // the start of a declarator. The comma was probably a typo for a
1283 // semicolon.
1284 Diag(CommaLoc, diag::err_expected_semi_declaration)
1285 << FixItHint::CreateReplacement(CommaLoc, ";");
1286 ExpectSemi = false;
1287 break;
1288 }
John McCalld8ac0572009-11-03 19:26:08 +00001289
1290 // Parse the next declarator.
1291 D.clear();
Richard Smith7984de32012-01-12 23:53:29 +00001292 D.setCommaLoc(CommaLoc);
John McCalld8ac0572009-11-03 19:26:08 +00001293
1294 // Accept attributes in an init-declarator. In the first declarator in a
1295 // declaration, these would be part of the declspec. In subsequent
1296 // declarators, they become part of the declarator itself, so that they
1297 // don't apply to declarators after *this* one. Examples:
1298 // short __attribute__((common)) var; -> declspec
1299 // short var __attribute__((common)); -> declarator
1300 // short x, __attribute__((common)) var; -> declarator
John McCall7f040a92010-12-24 02:08:15 +00001301 MaybeParseGNUAttributes(D);
John McCalld8ac0572009-11-03 19:26:08 +00001302
1303 ParseDeclarator(D);
Fariborz Jahanian9baf39d2012-01-13 00:14:12 +00001304 if (!D.isInvalidType()) {
1305 Decl *ThisDecl = ParseDeclarationAfterDeclarator(D);
1306 D.complete(ThisDecl);
1307 if (ThisDecl)
1308 DeclsInGroup.push_back(ThisDecl);
1309 }
John McCalld8ac0572009-11-03 19:26:08 +00001310 }
1311
1312 if (DeclEnd)
1313 *DeclEnd = Tok.getLocation();
1314
Richard Smith0706df42011-10-19 21:33:05 +00001315 if (ExpectSemi &&
Chris Lattner8bb21d32012-04-28 16:12:17 +00001316 ExpectAndConsumeSemi(Context == Declarator::FileContext
1317 ? diag::err_invalid_token_after_toplevel_declarator
1318 : diag::err_expected_semi_declaration)) {
Chris Lattner004659a2010-07-11 22:42:07 +00001319 // Okay, there was no semicolon and one was expected. If we see a
1320 // declaration specifier, just assume it was missing and continue parsing.
1321 // Otherwise things are very confused and we skip to recover.
1322 if (!isDeclarationSpecifier()) {
1323 SkipUntil(tok::r_brace, true, true);
1324 if (Tok.is(tok::semi))
1325 ConsumeToken();
1326 }
John McCalld8ac0572009-11-03 19:26:08 +00001327 }
1328
Douglas Gregor23c94db2010-07-02 17:43:08 +00001329 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS,
John McCalld8ac0572009-11-03 19:26:08 +00001330 DeclsInGroup.data(),
1331 DeclsInGroup.size());
Reid Spencer5f016e22007-07-11 17:01:13 +00001332}
1333
Richard Smithad762fc2011-04-14 22:09:26 +00001334/// Parse an optional simple-asm-expr and attributes, and attach them to a
1335/// declarator. Returns true on an error.
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001336bool Parser::ParseAsmAttributesAfterDeclarator(Declarator &D) {
Richard Smithad762fc2011-04-14 22:09:26 +00001337 // If a simple-asm-expr is present, parse it.
1338 if (Tok.is(tok::kw_asm)) {
1339 SourceLocation Loc;
1340 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
1341 if (AsmLabel.isInvalid()) {
1342 SkipUntil(tok::semi, true, true);
1343 return true;
1344 }
1345
1346 D.setAsmLabel(AsmLabel.release());
1347 D.SetRangeEnd(Loc);
1348 }
1349
1350 MaybeParseGNUAttributes(D);
1351 return false;
1352}
1353
Douglas Gregor1426e532009-05-12 21:31:51 +00001354/// \brief Parse 'declaration' after parsing 'declaration-specifiers
1355/// declarator'. This method parses the remainder of the declaration
1356/// (including any attributes or initializer, among other things) and
1357/// finalizes the declaration.
Reid Spencer5f016e22007-07-11 17:01:13 +00001358///
Reid Spencer5f016e22007-07-11 17:01:13 +00001359/// init-declarator: [C99 6.7]
1360/// declarator
1361/// declarator '=' initializer
1362/// [GNU] declarator simple-asm-expr[opt] attributes[opt]
1363/// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00001364/// [C++] declarator initializer[opt]
1365///
1366/// [C++] initializer:
1367/// [C++] '=' initializer-clause
1368/// [C++] '(' expression-list ')'
Sebastian Redl50de12f2009-03-24 22:27:57 +00001369/// [C++0x] '=' 'default' [TODO]
1370/// [C++0x] '=' 'delete'
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001371/// [C++0x] braced-init-list
Sebastian Redl50de12f2009-03-24 22:27:57 +00001372///
1373/// According to the standard grammar, =default and =delete are function
1374/// definitions, but that definitely doesn't fit with the parser here.
Reid Spencer5f016e22007-07-11 17:01:13 +00001375///
John McCalld226f652010-08-21 09:40:31 +00001376Decl *Parser::ParseDeclarationAfterDeclarator(Declarator &D,
Douglas Gregore542c862009-06-23 23:11:28 +00001377 const ParsedTemplateInfo &TemplateInfo) {
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001378 if (ParseAsmAttributesAfterDeclarator(D))
Richard Smithad762fc2011-04-14 22:09:26 +00001379 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001380
Richard Smithad762fc2011-04-14 22:09:26 +00001381 return ParseDeclarationAfterDeclaratorAndAttributes(D, TemplateInfo);
1382}
Mike Stump1eb44332009-09-09 15:08:12 +00001383
Richard Smithad762fc2011-04-14 22:09:26 +00001384Decl *Parser::ParseDeclarationAfterDeclaratorAndAttributes(Declarator &D,
1385 const ParsedTemplateInfo &TemplateInfo) {
Douglas Gregor1426e532009-05-12 21:31:51 +00001386 // Inform the current actions module that we just parsed this declarator.
John McCalld226f652010-08-21 09:40:31 +00001387 Decl *ThisDecl = 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +00001388 switch (TemplateInfo.Kind) {
1389 case ParsedTemplateInfo::NonTemplate:
Douglas Gregor23c94db2010-07-02 17:43:08 +00001390 ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
Douglas Gregord5a423b2009-09-25 18:43:00 +00001391 break;
1392
1393 case ParsedTemplateInfo::Template:
1394 case ParsedTemplateInfo::ExplicitSpecialization:
Douglas Gregor23c94db2010-07-02 17:43:08 +00001395 ThisDecl = Actions.ActOnTemplateDeclarator(getCurScope(),
John McCallf312b1e2010-08-26 23:41:50 +00001396 MultiTemplateParamsArg(Actions,
Douglas Gregore542c862009-06-23 23:11:28 +00001397 TemplateInfo.TemplateParams->data(),
1398 TemplateInfo.TemplateParams->size()),
Douglas Gregord5a423b2009-09-25 18:43:00 +00001399 D);
1400 break;
1401
1402 case ParsedTemplateInfo::ExplicitInstantiation: {
John McCalld226f652010-08-21 09:40:31 +00001403 DeclResult ThisRes
Douglas Gregor23c94db2010-07-02 17:43:08 +00001404 = Actions.ActOnExplicitInstantiation(getCurScope(),
Douglas Gregord5a423b2009-09-25 18:43:00 +00001405 TemplateInfo.ExternLoc,
1406 TemplateInfo.TemplateLoc,
1407 D);
1408 if (ThisRes.isInvalid()) {
1409 SkipUntil(tok::semi, true, true);
John McCalld226f652010-08-21 09:40:31 +00001410 return 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +00001411 }
1412
1413 ThisDecl = ThisRes.get();
1414 break;
1415 }
1416 }
Mike Stump1eb44332009-09-09 15:08:12 +00001417
Richard Smith34b41d92011-02-20 03:19:35 +00001418 bool TypeContainsAuto =
1419 D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto;
1420
Douglas Gregor1426e532009-05-12 21:31:51 +00001421 // Parse declarator '=' initializer.
Richard Trieud6c7c672012-01-18 22:54:52 +00001422 // If a '==' or '+=' is found, suggest a fixit to '='.
Richard Trieufcaf27e2012-01-19 22:01:51 +00001423 if (isTokenEqualOrEqualTypo()) {
Douglas Gregor1426e532009-05-12 21:31:51 +00001424 ConsumeToken();
Anders Carlsson37bf9d22010-09-24 21:25:25 +00001425 if (Tok.is(tok::kw_delete)) {
Sean Hunte4246a62011-05-12 06:15:49 +00001426 if (D.isFunctionDeclarator())
1427 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
1428 << 1 /* delete */;
1429 else
1430 Diag(ConsumeToken(), diag::err_deleted_non_function);
Sean Huntfe2695e2011-05-06 01:42:00 +00001431 } else if (Tok.is(tok::kw_default)) {
Sean Hunte4246a62011-05-12 06:15:49 +00001432 if (D.isFunctionDeclarator())
Sebastian Redlecfcd562012-02-11 23:51:21 +00001433 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
1434 << 0 /* default */;
Sean Hunte4246a62011-05-12 06:15:49 +00001435 else
1436 Diag(ConsumeToken(), diag::err_default_special_members);
Douglas Gregor1426e532009-05-12 21:31:51 +00001437 } else {
David Blaikie4e4d0842012-03-11 07:00:24 +00001438 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
John McCall731ad842009-12-19 09:28:58 +00001439 EnterScope(0);
Douglas Gregor23c94db2010-07-02 17:43:08 +00001440 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
John McCall731ad842009-12-19 09:28:58 +00001441 }
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +00001442
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +00001443 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001444 Actions.CodeCompleteInitializer(getCurScope(), ThisDecl);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001445 cutOffParsing();
1446 return 0;
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +00001447 }
1448
John McCall60d7b3a2010-08-24 06:29:42 +00001449 ExprResult Init(ParseInitializer());
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +00001450
David Blaikie4e4d0842012-03-11 07:00:24 +00001451 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001452 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
John McCall731ad842009-12-19 09:28:58 +00001453 ExitScope();
1454 }
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +00001455
Douglas Gregor1426e532009-05-12 21:31:51 +00001456 if (Init.isInvalid()) {
Douglas Gregor00225542010-03-01 18:27:54 +00001457 SkipUntil(tok::comma, true, true);
1458 Actions.ActOnInitializerError(ThisDecl);
1459 } else
Richard Smith34b41d92011-02-20 03:19:35 +00001460 Actions.AddInitializerToDecl(ThisDecl, Init.take(),
1461 /*DirectInit=*/false, TypeContainsAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +00001462 }
1463 } else if (Tok.is(tok::l_paren)) {
1464 // Parse C++ direct initializer: '(' expression-list ')'
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001465 BalancedDelimiterTracker T(*this, tok::l_paren);
1466 T.consumeOpen();
1467
Douglas Gregor1426e532009-05-12 21:31:51 +00001468 ExprVector Exprs(Actions);
1469 CommaLocsTy CommaLocs;
1470
David Blaikie4e4d0842012-03-11 07:00:24 +00001471 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregorb4debae2009-12-22 17:47:17 +00001472 EnterScope(0);
Douglas Gregor23c94db2010-07-02 17:43:08 +00001473 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001474 }
1475
Douglas Gregor1426e532009-05-12 21:31:51 +00001476 if (ParseExpressionList(Exprs, CommaLocs)) {
1477 SkipUntil(tok::r_paren);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001478
David Blaikie4e4d0842012-03-11 07:00:24 +00001479 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001480 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001481 ExitScope();
1482 }
Douglas Gregor1426e532009-05-12 21:31:51 +00001483 } else {
1484 // Match the ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001485 T.consumeClose();
Douglas Gregor1426e532009-05-12 21:31:51 +00001486
1487 assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() &&
1488 "Unexpected number of commas!");
Douglas Gregorb4debae2009-12-22 17:47:17 +00001489
David Blaikie4e4d0842012-03-11 07:00:24 +00001490 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001491 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001492 ExitScope();
1493 }
1494
Sebastian Redl5b9cc5d2012-02-11 23:51:47 +00001495 ExprResult Initializer = Actions.ActOnParenListExpr(T.getOpenLocation(),
1496 T.getCloseLocation(),
1497 move_arg(Exprs));
1498 Actions.AddInitializerToDecl(ThisDecl, Initializer.take(),
1499 /*DirectInit=*/true, TypeContainsAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +00001500 }
David Blaikie4e4d0842012-03-11 07:00:24 +00001501 } else if (getLangOpts().CPlusPlus0x && Tok.is(tok::l_brace)) {
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001502 // Parse C++0x braced-init-list.
Richard Smith7fe62082011-10-15 05:09:34 +00001503 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
1504
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001505 if (D.getCXXScopeSpec().isSet()) {
1506 EnterScope(0);
1507 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
1508 }
1509
1510 ExprResult Init(ParseBraceInitializer());
1511
1512 if (D.getCXXScopeSpec().isSet()) {
1513 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
1514 ExitScope();
1515 }
1516
1517 if (Init.isInvalid()) {
1518 Actions.ActOnInitializerError(ThisDecl);
1519 } else
1520 Actions.AddInitializerToDecl(ThisDecl, Init.take(),
1521 /*DirectInit=*/true, TypeContainsAuto);
1522
Douglas Gregor1426e532009-05-12 21:31:51 +00001523 } else {
Richard Smith34b41d92011-02-20 03:19:35 +00001524 Actions.ActOnUninitializedDecl(ThisDecl, TypeContainsAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +00001525 }
1526
Richard Smith483b9f32011-02-21 20:05:19 +00001527 Actions.FinalizeDeclaration(ThisDecl);
1528
Douglas Gregor1426e532009-05-12 21:31:51 +00001529 return ThisDecl;
1530}
1531
Reid Spencer5f016e22007-07-11 17:01:13 +00001532/// ParseSpecifierQualifierList
1533/// specifier-qualifier-list:
1534/// type-specifier specifier-qualifier-list[opt]
1535/// type-qualifier specifier-qualifier-list[opt]
1536/// [GNU] attributes specifier-qualifier-list[opt]
1537///
Richard Smith69730c12012-03-12 07:56:15 +00001538void Parser::ParseSpecifierQualifierList(DeclSpec &DS, AccessSpecifier AS,
1539 DeclSpecContext DSC) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001540 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
1541 /// parse declaration-specifiers and complain about extra stuff.
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001542 /// TODO: diagnose attribute-specifiers and alignment-specifiers.
Richard Smith69730c12012-03-12 07:56:15 +00001543 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS, DSC);
Mike Stump1eb44332009-09-09 15:08:12 +00001544
Reid Spencer5f016e22007-07-11 17:01:13 +00001545 // Validate declspec for type-name.
1546 unsigned Specs = DS.getParsedSpecifiers();
Richard Smitha971d242012-05-09 20:55:26 +00001547 if ((DSC == DSC_type_specifier || DSC == DSC_trailing) &&
1548 !DS.hasTypeSpecifier()) {
Richard Smith69730c12012-03-12 07:56:15 +00001549 Diag(Tok, diag::err_expected_type);
1550 DS.SetTypeSpecError();
1551 } else if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers() &&
1552 !DS.hasAttributes()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001553 Diag(Tok, diag::err_typename_requires_specqual);
Richard Smith69730c12012-03-12 07:56:15 +00001554 if (!DS.hasTypeSpecifier())
1555 DS.SetTypeSpecError();
1556 }
Mike Stump1eb44332009-09-09 15:08:12 +00001557
Reid Spencer5f016e22007-07-11 17:01:13 +00001558 // Issue diagnostic and remove storage class if present.
1559 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
1560 if (DS.getStorageClassSpecLoc().isValid())
1561 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
1562 else
1563 Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass);
1564 DS.ClearStorageClassSpecs();
1565 }
Mike Stump1eb44332009-09-09 15:08:12 +00001566
Reid Spencer5f016e22007-07-11 17:01:13 +00001567 // Issue diagnostic and remove function specfier if present.
1568 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Douglas Gregorb48fe382008-10-31 09:07:45 +00001569 if (DS.isInlineSpecified())
1570 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
1571 if (DS.isVirtualSpecified())
1572 Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec);
1573 if (DS.isExplicitSpecified())
1574 Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec);
Reid Spencer5f016e22007-07-11 17:01:13 +00001575 DS.ClearFunctionSpecs();
1576 }
Richard Smith69730c12012-03-12 07:56:15 +00001577
1578 // Issue diagnostic and remove constexpr specfier if present.
1579 if (DS.isConstexprSpecified()) {
1580 Diag(DS.getConstexprSpecLoc(), diag::err_typename_invalid_constexpr);
1581 DS.ClearConstexprSpec();
1582 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001583}
1584
Chris Lattnerc199ab32009-04-12 20:42:31 +00001585/// isValidAfterIdentifierInDeclaratorAfterDeclSpec - Return true if the
1586/// specified token is valid after the identifier in a declarator which
1587/// immediately follows the declspec. For example, these things are valid:
1588///
1589/// int x [ 4]; // direct-declarator
1590/// int x ( int y); // direct-declarator
1591/// int(int x ) // direct-declarator
1592/// int x ; // simple-declaration
1593/// int x = 17; // init-declarator-list
1594/// int x , y; // init-declarator-list
1595/// int x __asm__ ("foo"); // init-declarator-list
Chris Lattnerb6645dd2009-04-14 21:16:09 +00001596/// int x : 4; // struct-declarator
Chris Lattnerc83c27a2009-04-12 22:29:43 +00001597/// int x { 5}; // C++'0x unified initializers
Chris Lattnerc199ab32009-04-12 20:42:31 +00001598///
1599/// This is not, because 'x' does not immediately follow the declspec (though
1600/// ')' happens to be valid anyway).
1601/// int (x)
1602///
1603static bool isValidAfterIdentifierInDeclarator(const Token &T) {
1604 return T.is(tok::l_square) || T.is(tok::l_paren) || T.is(tok::r_paren) ||
1605 T.is(tok::semi) || T.is(tok::comma) || T.is(tok::equal) ||
Chris Lattnerb6645dd2009-04-14 21:16:09 +00001606 T.is(tok::kw_asm) || T.is(tok::l_brace) || T.is(tok::colon);
Chris Lattnerc199ab32009-04-12 20:42:31 +00001607}
1608
Chris Lattnere40c2952009-04-14 21:34:55 +00001609
1610/// ParseImplicitInt - This method is called when we have an non-typename
1611/// identifier in a declspec (which normally terminates the decl spec) when
1612/// the declspec has no type specifier. In this case, the declspec is either
1613/// malformed or is "implicit int" (in K&R and C89).
1614///
1615/// This method handles diagnosing this prettily and returns false if the
1616/// declspec is done being processed. If it recovers and thinks there may be
1617/// other pieces of declspec after it, it returns true.
1618///
Chris Lattnerf4382f52009-04-14 22:17:06 +00001619bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001620 const ParsedTemplateInfo &TemplateInfo,
Richard Smith69730c12012-03-12 07:56:15 +00001621 AccessSpecifier AS, DeclSpecContext DSC) {
Chris Lattnerf4382f52009-04-14 22:17:06 +00001622 assert(Tok.is(tok::identifier) && "should have identifier");
Mike Stump1eb44332009-09-09 15:08:12 +00001623
Chris Lattnere40c2952009-04-14 21:34:55 +00001624 SourceLocation Loc = Tok.getLocation();
1625 // If we see an identifier that is not a type name, we normally would
1626 // parse it as the identifer being declared. However, when a typename
1627 // is typo'd or the definition is not included, this will incorrectly
1628 // parse the typename as the identifier name and fall over misparsing
1629 // later parts of the diagnostic.
1630 //
1631 // As such, we try to do some look-ahead in cases where this would
1632 // otherwise be an "implicit-int" case to see if this is invalid. For
1633 // example: "static foo_t x = 4;" In this case, if we parsed foo_t as
1634 // an identifier with implicit int, we'd get a parse error because the
1635 // next token is obviously invalid for a type. Parse these as a case
1636 // with an invalid type specifier.
1637 assert(!DS.hasTypeSpecifier() && "Type specifier checked above");
Mike Stump1eb44332009-09-09 15:08:12 +00001638
Chris Lattnere40c2952009-04-14 21:34:55 +00001639 // Since we know that this either implicit int (which is rare) or an
Richard Smith827adaf2012-05-15 21:01:51 +00001640 // error, do lookahead to try to do better recovery. This never applies
1641 // within a type specifier. Outside of C++, we allow this even if the
1642 // language doesn't "officially" support implicit int -- we support
1643 // implicit int as an extension in C99 and C11. Allegedly, MS also
1644 // supports implicit int in C++ mode.
Richard Smitha971d242012-05-09 20:55:26 +00001645 if (DSC != DSC_type_specifier && DSC != DSC_trailing &&
Richard Smith827adaf2012-05-15 21:01:51 +00001646 (!getLangOpts().CPlusPlus || getLangOpts().MicrosoftExt) &&
Richard Smith69730c12012-03-12 07:56:15 +00001647 isValidAfterIdentifierInDeclarator(NextToken())) {
Chris Lattnere40c2952009-04-14 21:34:55 +00001648 // If this token is valid for implicit int, e.g. "static x = 4", then
1649 // we just avoid eating the identifier, so it will be parsed as the
1650 // identifier in the declarator.
1651 return false;
1652 }
Mike Stump1eb44332009-09-09 15:08:12 +00001653
Richard Smith827adaf2012-05-15 21:01:51 +00001654 if (getLangOpts().CPlusPlus &&
1655 DS.getStorageClassSpec() == DeclSpec::SCS_auto) {
1656 // Don't require a type specifier if we have the 'auto' storage class
1657 // specifier in C++98 -- we'll promote it to a type specifier.
1658 return false;
1659 }
1660
Chris Lattnere40c2952009-04-14 21:34:55 +00001661 // Otherwise, if we don't consume this token, we are going to emit an
1662 // error anyway. Try to recover from various common problems. Check
1663 // to see if this was a reference to a tag name without a tag specified.
1664 // This is a common problem in C (saying 'foo' instead of 'struct foo').
Chris Lattnerf4382f52009-04-14 22:17:06 +00001665 //
1666 // C++ doesn't need this, and isTagName doesn't take SS.
1667 if (SS == 0) {
Argyrios Kyrtzidisb8a9d3b2011-04-21 17:29:47 +00001668 const char *TagName = 0, *FixitTagName = 0;
Chris Lattnerf4382f52009-04-14 22:17:06 +00001669 tok::TokenKind TagKind = tok::unknown;
Mike Stump1eb44332009-09-09 15:08:12 +00001670
Douglas Gregor23c94db2010-07-02 17:43:08 +00001671 switch (Actions.isTagName(*Tok.getIdentifierInfo(), getCurScope())) {
Chris Lattnere40c2952009-04-14 21:34:55 +00001672 default: break;
Argyrios Kyrtzidisb8a9d3b2011-04-21 17:29:47 +00001673 case DeclSpec::TST_enum:
1674 TagName="enum" ; FixitTagName = "enum " ; TagKind=tok::kw_enum ;break;
1675 case DeclSpec::TST_union:
1676 TagName="union" ; FixitTagName = "union " ;TagKind=tok::kw_union ;break;
1677 case DeclSpec::TST_struct:
1678 TagName="struct"; FixitTagName = "struct ";TagKind=tok::kw_struct;break;
1679 case DeclSpec::TST_class:
1680 TagName="class" ; FixitTagName = "class " ;TagKind=tok::kw_class ;break;
Chris Lattnere40c2952009-04-14 21:34:55 +00001681 }
Mike Stump1eb44332009-09-09 15:08:12 +00001682
Chris Lattnerf4382f52009-04-14 22:17:06 +00001683 if (TagName) {
Kaelyn Uhrainaec2ac62012-04-26 23:36:17 +00001684 IdentifierInfo *TokenName = Tok.getIdentifierInfo();
1685 LookupResult R(Actions, TokenName, SourceLocation(),
1686 Sema::LookupOrdinaryName);
1687
Chris Lattnerf4382f52009-04-14 22:17:06 +00001688 Diag(Loc, diag::err_use_of_tag_name_without_tag)
Kaelyn Uhrainaec2ac62012-04-26 23:36:17 +00001689 << TokenName << TagName << getLangOpts().CPlusPlus
1690 << FixItHint::CreateInsertion(Tok.getLocation(), FixitTagName);
1691
1692 if (Actions.LookupParsedName(R, getCurScope(), SS)) {
1693 for (LookupResult::iterator I = R.begin(), IEnd = R.end();
1694 I != IEnd; ++I)
Kaelyn Uhrain392b3f52012-04-27 18:26:49 +00001695 Diag((*I)->getLocation(), diag::note_decl_hiding_tag_type)
Kaelyn Uhrainaec2ac62012-04-26 23:36:17 +00001696 << TokenName << TagName;
1697 }
Mike Stump1eb44332009-09-09 15:08:12 +00001698
Chris Lattnerf4382f52009-04-14 22:17:06 +00001699 // Parse this as a tag as if the missing tag were present.
1700 if (TagKind == tok::kw_enum)
Richard Smith69730c12012-03-12 07:56:15 +00001701 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS, DSC_normal);
Chris Lattnerf4382f52009-04-14 22:17:06 +00001702 else
Richard Smith69730c12012-03-12 07:56:15 +00001703 ParseClassSpecifier(TagKind, Loc, DS, TemplateInfo, AS,
1704 /*EnteringContext*/ false, DSC_normal);
Chris Lattnerf4382f52009-04-14 22:17:06 +00001705 return true;
1706 }
Chris Lattnere40c2952009-04-14 21:34:55 +00001707 }
Mike Stump1eb44332009-09-09 15:08:12 +00001708
Richard Smith8f0a7e72012-05-15 21:29:55 +00001709 // Determine whether this identifier could plausibly be the name of something
Richard Smith7514db22012-05-15 21:42:17 +00001710 // being declared (with a missing type).
Richard Smith8f0a7e72012-05-15 21:29:55 +00001711 if (DSC != DSC_type_specifier && DSC != DSC_trailing &&
1712 (!SS || DSC == DSC_top_level || DSC == DSC_class)) {
Richard Smith827adaf2012-05-15 21:01:51 +00001713 // Look ahead to the next token to try to figure out what this declaration
1714 // was supposed to be.
1715 switch (NextToken().getKind()) {
1716 case tok::comma:
1717 case tok::equal:
1718 case tok::kw_asm:
1719 case tok::l_brace:
1720 case tok::l_square:
1721 case tok::semi:
1722 // This looks like a variable declaration. The type is probably missing.
1723 // We're done parsing decl-specifiers.
1724 return false;
1725
1726 case tok::l_paren: {
1727 // static x(4); // 'x' is not a type
1728 // x(int n); // 'x' is not a type
1729 // x (*p)[]; // 'x' is a type
1730 //
1731 // Since we're in an error case (or the rare 'implicit int in C++' MS
1732 // extension), we can afford to perform a tentative parse to determine
1733 // which case we're in.
1734 TentativeParsingAction PA(*this);
1735 ConsumeToken();
1736 TPResult TPR = TryParseDeclarator(/*mayBeAbstract*/false);
1737 PA.Revert();
1738 if (TPR == TPResult::False())
1739 return false;
1740 // The identifier is followed by a parenthesized declarator.
1741 // It's supposed to be a type.
1742 break;
1743 }
1744
1745 default:
1746 // This is probably supposed to be a type. This includes cases like:
1747 // int f(itn);
1748 // struct S { unsinged : 4; };
1749 break;
1750 }
1751 }
1752
Douglas Gregora786fdb2009-10-13 23:27:22 +00001753 // This is almost certainly an invalid type name. Let the action emit a
1754 // diagnostic and attempt to recover.
John McCallb3d87482010-08-24 05:47:05 +00001755 ParsedType T;
Kaelyn Uhrain50dc12a2012-06-15 23:45:58 +00001756 IdentifierInfo *II = Tok.getIdentifierInfo();
1757 if (Actions.DiagnoseUnknownTypeName(II, Loc, getCurScope(), SS, T)) {
Douglas Gregora786fdb2009-10-13 23:27:22 +00001758 // The action emitted a diagnostic, so we don't have to.
1759 if (T) {
1760 // The action has suggested that the type T could be used. Set that as
1761 // the type in the declaration specifiers, consume the would-be type
1762 // name token, and we're done.
1763 const char *PrevSpec;
1764 unsigned DiagID;
John McCallb3d87482010-08-24 05:47:05 +00001765 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID, T);
Douglas Gregora786fdb2009-10-13 23:27:22 +00001766 DS.SetRangeEnd(Tok.getLocation());
1767 ConsumeToken();
Kaelyn Uhrain50dc12a2012-06-15 23:45:58 +00001768 // There may be other declaration specifiers after this.
1769 return true;
1770 } else if (II != Tok.getIdentifierInfo()) {
1771 // If no type was suggested, the correction is to a keyword
1772 Tok.setKind(II->getTokenID());
Douglas Gregora786fdb2009-10-13 23:27:22 +00001773 // There may be other declaration specifiers after this.
1774 return true;
1775 }
1776
1777 // Fall through; the action had no suggestion for us.
1778 } else {
1779 // The action did not emit a diagnostic, so emit one now.
1780 SourceRange R;
1781 if (SS) R = SS->getRange();
1782 Diag(Loc, diag::err_unknown_typename) << Tok.getIdentifierInfo() << R;
1783 }
Mike Stump1eb44332009-09-09 15:08:12 +00001784
Douglas Gregora786fdb2009-10-13 23:27:22 +00001785 // Mark this as an error.
Richard Smith69730c12012-03-12 07:56:15 +00001786 DS.SetTypeSpecError();
Chris Lattnere40c2952009-04-14 21:34:55 +00001787 DS.SetRangeEnd(Tok.getLocation());
1788 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001789
Chris Lattnere40c2952009-04-14 21:34:55 +00001790 // TODO: Could inject an invalid typedef decl in an enclosing scope to
1791 // avoid rippling error messages on subsequent uses of the same type,
1792 // could be useful if #include was forgotten.
1793 return false;
1794}
1795
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001796/// \brief Determine the declaration specifier context from the declarator
1797/// context.
1798///
1799/// \param Context the declarator context, which is one of the
1800/// Declarator::TheContext enumerator values.
1801Parser::DeclSpecContext
1802Parser::getDeclSpecContextFromDeclaratorContext(unsigned Context) {
1803 if (Context == Declarator::MemberContext)
1804 return DSC_class;
1805 if (Context == Declarator::FileContext)
1806 return DSC_top_level;
Richard Smith6d96d3a2012-03-15 01:02:11 +00001807 if (Context == Declarator::TrailingReturnContext)
1808 return DSC_trailing;
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001809 return DSC_normal;
1810}
1811
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001812/// ParseAlignArgument - Parse the argument to an alignment-specifier.
1813///
1814/// FIXME: Simply returns an alignof() expression if the argument is a
1815/// type. Ideally, the type should be propagated directly into Sema.
1816///
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00001817/// [C11] type-id
1818/// [C11] constant-expression
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00001819/// [C++0x] type-id ...[opt]
1820/// [C++0x] assignment-expression ...[opt]
1821ExprResult Parser::ParseAlignArgument(SourceLocation Start,
1822 SourceLocation &EllipsisLoc) {
1823 ExprResult ER;
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001824 if (isTypeIdInParens()) {
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001825 SourceLocation TypeLoc = Tok.getLocation();
1826 ParsedType Ty = ParseTypeName().get();
1827 SourceRange TypeRange(Start, Tok.getLocation());
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00001828 ER = Actions.ActOnUnaryExprOrTypeTraitExpr(TypeLoc, UETT_AlignOf, true,
1829 Ty.getAsOpaquePtr(), TypeRange);
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001830 } else
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00001831 ER = ParseConstantExpression();
1832
David Blaikie4e4d0842012-03-11 07:00:24 +00001833 if (getLangOpts().CPlusPlus0x && Tok.is(tok::ellipsis))
Peter Collingbournefe9b2a82011-10-24 17:56:00 +00001834 EllipsisLoc = ConsumeToken();
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00001835
1836 return ER;
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001837}
1838
1839/// ParseAlignmentSpecifier - Parse an alignment-specifier, and add the
1840/// attribute to Attrs.
1841///
1842/// alignment-specifier:
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00001843/// [C11] '_Alignas' '(' type-id ')'
1844/// [C11] '_Alignas' '(' constant-expression ')'
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00001845/// [C++0x] 'alignas' '(' type-id ...[opt] ')'
1846/// [C++0x] 'alignas' '(' assignment-expression ...[opt] ')'
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001847void Parser::ParseAlignmentSpecifier(ParsedAttributes &Attrs,
1848 SourceLocation *endLoc) {
1849 assert((Tok.is(tok::kw_alignas) || Tok.is(tok::kw__Alignas)) &&
1850 "Not an alignment-specifier!");
1851
1852 SourceLocation KWLoc = Tok.getLocation();
1853 ConsumeToken();
1854
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001855 BalancedDelimiterTracker T(*this, tok::l_paren);
1856 if (T.expectAndConsume(diag::err_expected_lparen))
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001857 return;
1858
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00001859 SourceLocation EllipsisLoc;
1860 ExprResult ArgExpr = ParseAlignArgument(T.getOpenLocation(), EllipsisLoc);
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001861 if (ArgExpr.isInvalid()) {
1862 SkipUntil(tok::r_paren);
1863 return;
1864 }
1865
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001866 T.consumeClose();
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001867 if (endLoc)
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001868 *endLoc = T.getCloseLocation();
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001869
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00001870 // FIXME: Handle pack-expansions here.
1871 if (EllipsisLoc.isValid()) {
1872 Diag(EllipsisLoc, diag::err_alignas_pack_exp_unsupported);
1873 return;
1874 }
1875
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001876 ExprVector ArgExprs(Actions);
1877 ArgExprs.push_back(ArgExpr.release());
Sean Huntbfcb0372012-06-19 03:39:03 +00001878 // FIXME: This should not be GNU, but we since the attribute used is
1879 // based on the spelling, and there is no true spelling for
1880 // C++11 attributes, this isn't accepted.
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001881 Attrs.addNew(PP.getIdentifierInfo("aligned"), KWLoc, 0, KWLoc,
Sean Hunt93f95f22012-06-18 16:13:52 +00001882 0, T.getOpenLocation(), ArgExprs.take(), 1,
Sean Huntbfcb0372012-06-19 03:39:03 +00001883 AttributeList::AS_GNU);
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001884}
1885
Reid Spencer5f016e22007-07-11 17:01:13 +00001886/// ParseDeclarationSpecifiers
1887/// declaration-specifiers: [C99 6.7]
1888/// storage-class-specifier declaration-specifiers[opt]
1889/// type-specifier declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00001890/// [C99] function-specifier declaration-specifiers[opt]
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00001891/// [C11] alignment-specifier declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00001892/// [GNU] attributes declaration-specifiers[opt]
Douglas Gregor8d267c52011-09-09 02:06:17 +00001893/// [Clang] '__module_private__' declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00001894///
1895/// storage-class-specifier: [C99 6.7.1]
1896/// 'typedef'
1897/// 'extern'
1898/// 'static'
1899/// 'auto'
1900/// 'register'
Sebastian Redl669d5d72008-11-14 23:42:31 +00001901/// [C++] 'mutable'
Reid Spencer5f016e22007-07-11 17:01:13 +00001902/// [GNU] '__thread'
Reid Spencer5f016e22007-07-11 17:01:13 +00001903/// function-specifier: [C99 6.7.4]
1904/// [C99] 'inline'
Douglas Gregorb48fe382008-10-31 09:07:45 +00001905/// [C++] 'virtual'
1906/// [C++] 'explicit'
Peter Collingbournef315fa82011-02-14 01:42:53 +00001907/// [OpenCL] '__kernel'
Anders Carlssonf47f7a12009-05-06 04:46:28 +00001908/// 'friend': [C++ dcl.friend]
Sebastian Redl2ac67232009-11-05 15:47:02 +00001909/// 'constexpr': [C++0x dcl.constexpr]
Anders Carlssonf47f7a12009-05-06 04:46:28 +00001910
Reid Spencer5f016e22007-07-11 17:01:13 +00001911///
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +00001912void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001913 const ParsedTemplateInfo &TemplateInfo,
John McCall67d1a672009-08-06 02:15:43 +00001914 AccessSpecifier AS,
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +00001915 DeclSpecContext DSContext,
1916 LateParsedAttrList *LateAttrs) {
Douglas Gregor312eadb2011-04-24 05:37:28 +00001917 if (DS.getSourceRange().isInvalid()) {
1918 DS.SetRangeStart(Tok.getLocation());
1919 DS.SetRangeEnd(Tok.getLocation());
1920 }
1921
Douglas Gregorefaa93a2011-11-07 17:33:42 +00001922 bool EnteringContext = (DSContext == DSC_class || DSContext == DSC_top_level);
Reid Spencer5f016e22007-07-11 17:01:13 +00001923 while (1) {
John McCallfec54012009-08-03 20:12:06 +00001924 bool isInvalid = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001925 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00001926 unsigned DiagID = 0;
1927
Reid Spencer5f016e22007-07-11 17:01:13 +00001928 SourceLocation Loc = Tok.getLocation();
Douglas Gregor12e083c2008-11-07 15:42:26 +00001929
Reid Spencer5f016e22007-07-11 17:01:13 +00001930 switch (Tok.getKind()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001931 default:
Chris Lattnerbce61352008-07-26 00:20:22 +00001932 DoneWithDeclSpec:
Peter Collingbournef1907682011-09-29 18:03:57 +00001933 // [C++0x] decl-specifier-seq: decl-specifier attribute-specifier-seq[opt]
1934 MaybeParseCXX0XAttributes(DS.getAttributes());
1935
Reid Spencer5f016e22007-07-11 17:01:13 +00001936 // If this is not a declaration specifier token, we're done reading decl
1937 // specifiers. First verify that DeclSpec's are consistent.
Douglas Gregor9b3064b2009-04-01 22:41:11 +00001938 DS.Finish(Diags, PP);
Reid Spencer5f016e22007-07-11 17:01:13 +00001939 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001940
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001941 case tok::code_completion: {
John McCallf312b1e2010-08-26 23:41:50 +00001942 Sema::ParserCompletionContext CCC = Sema::PCC_Namespace;
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001943 if (DS.hasTypeSpecifier()) {
1944 bool AllowNonIdentifiers
1945 = (getCurScope()->getFlags() & (Scope::ControlScope |
1946 Scope::BlockScope |
1947 Scope::TemplateParamScope |
1948 Scope::FunctionPrototypeScope |
1949 Scope::AtCatchScope)) == 0;
1950 bool AllowNestedNameSpecifiers
1951 = DSContext == DSC_top_level ||
1952 (DSContext == DSC_class && DS.isFriendSpecified());
1953
Douglas Gregorc7b6d882010-09-16 15:14:18 +00001954 Actions.CodeCompleteDeclSpec(getCurScope(), DS,
1955 AllowNonIdentifiers,
1956 AllowNestedNameSpecifiers);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001957 return cutOffParsing();
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001958 }
1959
Douglas Gregor68e3c2e2011-02-15 20:33:25 +00001960 if (getCurScope()->getFnParent() || getCurScope()->getBlockParent())
1961 CCC = Sema::PCC_LocalDeclarationSpecifiers;
1962 else if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate)
John McCallf312b1e2010-08-26 23:41:50 +00001963 CCC = DSContext == DSC_class? Sema::PCC_MemberTemplate
1964 : Sema::PCC_Template;
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001965 else if (DSContext == DSC_class)
John McCallf312b1e2010-08-26 23:41:50 +00001966 CCC = Sema::PCC_Class;
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +00001967 else if (CurParsedObjCImpl)
John McCallf312b1e2010-08-26 23:41:50 +00001968 CCC = Sema::PCC_ObjCImplementation;
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001969
1970 Actions.CodeCompleteOrdinaryName(getCurScope(), CCC);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001971 return cutOffParsing();
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001972 }
1973
Chris Lattner5e02c472009-01-05 00:07:25 +00001974 case tok::coloncolon: // ::foo::bar
John McCall9ba61662010-02-26 08:45:28 +00001975 // C++ scope specifier. Annotate and loop, or bail out on error.
1976 if (TryAnnotateCXXScopeToken(true)) {
1977 if (!DS.hasTypeSpecifier())
1978 DS.SetTypeSpecError();
1979 goto DoneWithDeclSpec;
1980 }
John McCall2e0a7152010-03-01 18:20:46 +00001981 if (Tok.is(tok::coloncolon)) // ::new or ::delete
1982 goto DoneWithDeclSpec;
John McCall9ba61662010-02-26 08:45:28 +00001983 continue;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001984
1985 case tok::annot_cxxscope: {
Richard Smithf63eee72012-05-09 18:56:43 +00001986 if (DS.hasTypeSpecifier() || DS.isTypeAltiVecVector())
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001987 goto DoneWithDeclSpec;
1988
John McCallaa87d332009-12-12 11:40:51 +00001989 CXXScopeSpec SS;
Douglas Gregorc34348a2011-02-24 17:54:50 +00001990 Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(),
1991 Tok.getAnnotationRange(),
1992 SS);
John McCallaa87d332009-12-12 11:40:51 +00001993
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001994 // We are looking for a qualified typename.
Douglas Gregor9135c722009-03-25 15:40:00 +00001995 Token Next = NextToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001996 if (Next.is(tok::annot_template_id) &&
Douglas Gregor9135c722009-03-25 15:40:00 +00001997 static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue())
Douglas Gregorc45c2322009-03-31 00:43:58 +00001998 ->Kind == TNK_Type_template) {
Douglas Gregor9135c722009-03-25 15:40:00 +00001999 // We have a qualified template-id, e.g., N::A<int>
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002000
2001 // C++ [class.qual]p2:
2002 // In a lookup in which the constructor is an acceptable lookup
2003 // result and the nested-name-specifier nominates a class C:
2004 //
2005 // - if the name specified after the
2006 // nested-name-specifier, when looked up in C, is the
2007 // injected-class-name of C (Clause 9), or
2008 //
2009 // - if the name specified after the nested-name-specifier
2010 // is the same as the identifier or the
2011 // simple-template-id's template-name in the last
2012 // component of the nested-name-specifier,
2013 //
2014 // the name is instead considered to name the constructor of
2015 // class C.
2016 //
2017 // Thus, if the template-name is actually the constructor
2018 // name, then the code is ill-formed; this interpretation is
2019 // reinforced by the NAD status of core issue 635.
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +00002020 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Next);
John McCallba9d8532010-04-13 06:39:49 +00002021 if ((DSContext == DSC_top_level ||
2022 (DSContext == DSC_class && DS.isFriendSpecified())) &&
2023 TemplateId->Name &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00002024 Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) {
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002025 if (isConstructorDeclarator()) {
2026 // The user meant this to be an out-of-line constructor
2027 // definition, but template arguments are not allowed
2028 // there. Just allow this as a constructor; we'll
2029 // complain about it later.
2030 goto DoneWithDeclSpec;
2031 }
2032
2033 // The user meant this to name a type, but it actually names
2034 // a constructor with some extraneous template
2035 // arguments. Complain, then parse it as a type as the user
2036 // intended.
2037 Diag(TemplateId->TemplateNameLoc,
2038 diag::err_out_of_line_template_id_names_constructor)
2039 << TemplateId->Name;
2040 }
2041
John McCallaa87d332009-12-12 11:40:51 +00002042 DS.getTypeSpecScope() = SS;
2043 ConsumeToken(); // The C++ scope.
Mike Stump1eb44332009-09-09 15:08:12 +00002044 assert(Tok.is(tok::annot_template_id) &&
Douglas Gregor9135c722009-03-25 15:40:00 +00002045 "ParseOptionalCXXScopeSpecifier not working");
Douglas Gregor059101f2011-03-02 00:47:37 +00002046 AnnotateTemplateIdTokenAsType();
Douglas Gregor9135c722009-03-25 15:40:00 +00002047 continue;
2048 }
2049
Douglas Gregor9d7b3532009-09-28 07:26:33 +00002050 if (Next.is(tok::annot_typename)) {
John McCallaa87d332009-12-12 11:40:51 +00002051 DS.getTypeSpecScope() = SS;
2052 ConsumeToken(); // The C++ scope.
John McCallb3d87482010-08-24 05:47:05 +00002053 if (Tok.getAnnotationValue()) {
2054 ParsedType T = getTypeAnnotation(Tok);
Nico Weber253e80b2010-11-22 10:30:56 +00002055 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename,
2056 Tok.getAnnotationEndLoc(),
John McCallb3d87482010-08-24 05:47:05 +00002057 PrevSpec, DiagID, T);
2058 }
Douglas Gregor9d7b3532009-09-28 07:26:33 +00002059 else
2060 DS.SetTypeSpecError();
2061 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
2062 ConsumeToken(); // The typename
2063 }
2064
Douglas Gregor9135c722009-03-25 15:40:00 +00002065 if (Next.isNot(tok::identifier))
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002066 goto DoneWithDeclSpec;
2067
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002068 // If we're in a context where the identifier could be a class name,
2069 // check whether this is a constructor declaration.
John McCallba9d8532010-04-13 06:39:49 +00002070 if ((DSContext == DSC_top_level ||
2071 (DSContext == DSC_class && DS.isFriendSpecified())) &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00002072 Actions.isCurrentClassName(*Next.getIdentifierInfo(), getCurScope(),
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002073 &SS)) {
2074 if (isConstructorDeclarator())
2075 goto DoneWithDeclSpec;
2076
2077 // As noted in C++ [class.qual]p2 (cited above), when the name
2078 // of the class is qualified in a context where it could name
2079 // a constructor, its a constructor name. However, we've
2080 // looked at the declarator, and the user probably meant this
2081 // to be a type. Complain that it isn't supposed to be treated
2082 // as a type, then proceed to parse it as a type.
2083 Diag(Next.getLocation(), diag::err_out_of_line_type_names_constructor)
2084 << Next.getIdentifierInfo();
2085 }
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002086
John McCallb3d87482010-08-24 05:47:05 +00002087 ParsedType TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(),
2088 Next.getLocation(),
Douglas Gregor9e876872011-03-01 18:12:44 +00002089 getCurScope(), &SS,
2090 false, false, ParsedType(),
Abramo Bagnarafad03b72012-01-27 08:46:19 +00002091 /*IsCtorOrDtorName=*/false,
Douglas Gregor9e876872011-03-01 18:12:44 +00002092 /*NonTrivialSourceInfo=*/true);
Douglas Gregor55f6b142009-02-09 18:46:07 +00002093
Chris Lattnerf4382f52009-04-14 22:17:06 +00002094 // If the referenced identifier is not a type, then this declspec is
2095 // erroneous: We already checked about that it has no type specifier, and
2096 // C++ doesn't have implicit int. Diagnose it as a typo w.r.t. to the
Mike Stump1eb44332009-09-09 15:08:12 +00002097 // typename.
Chris Lattnerf4382f52009-04-14 22:17:06 +00002098 if (TypeRep == 0) {
2099 ConsumeToken(); // Eat the scope spec so the identifier is current.
Richard Smith69730c12012-03-12 07:56:15 +00002100 if (ParseImplicitInt(DS, &SS, TemplateInfo, AS, DSContext)) continue;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002101 goto DoneWithDeclSpec;
Chris Lattnerf4382f52009-04-14 22:17:06 +00002102 }
Mike Stump1eb44332009-09-09 15:08:12 +00002103
John McCallaa87d332009-12-12 11:40:51 +00002104 DS.getTypeSpecScope() = SS;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002105 ConsumeToken(); // The C++ scope.
2106
Douglas Gregor1a51b4a2009-02-09 15:09:02 +00002107 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00002108 DiagID, TypeRep);
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002109 if (isInvalid)
2110 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002111
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002112 DS.SetRangeEnd(Tok.getLocation());
2113 ConsumeToken(); // The typename.
2114
2115 continue;
2116 }
Mike Stump1eb44332009-09-09 15:08:12 +00002117
Chris Lattner80d0c892009-01-21 19:48:37 +00002118 case tok::annot_typename: {
John McCallb3d87482010-08-24 05:47:05 +00002119 if (Tok.getAnnotationValue()) {
2120 ParsedType T = getTypeAnnotation(Tok);
Nico Weberc43271e2010-11-22 12:50:03 +00002121 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallb3d87482010-08-24 05:47:05 +00002122 DiagID, T);
2123 } else
Douglas Gregor31a19b62009-04-01 21:51:26 +00002124 DS.SetTypeSpecError();
Chris Lattner5c5db552010-04-05 18:18:31 +00002125
2126 if (isInvalid)
2127 break;
2128
Chris Lattner80d0c892009-01-21 19:48:37 +00002129 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
2130 ConsumeToken(); // The typename
Mike Stump1eb44332009-09-09 15:08:12 +00002131
Chris Lattner80d0c892009-01-21 19:48:37 +00002132 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
2133 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00002134 // Objective-C interface.
David Blaikie4e4d0842012-03-11 07:00:24 +00002135 if (Tok.is(tok::less) && getLangOpts().ObjC1)
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00002136 ParseObjCProtocolQualifiers(DS);
2137
Chris Lattner80d0c892009-01-21 19:48:37 +00002138 continue;
2139 }
Mike Stump1eb44332009-09-09 15:08:12 +00002140
Douglas Gregorbfad9152011-04-28 15:48:45 +00002141 case tok::kw___is_signed:
2142 // GNU libstdc++ 4.4 uses __is_signed as an identifier, but Clang
2143 // typically treats it as a trait. If we see __is_signed as it appears
2144 // in libstdc++, e.g.,
2145 //
2146 // static const bool __is_signed;
2147 //
2148 // then treat __is_signed as an identifier rather than as a keyword.
2149 if (DS.getTypeSpecType() == TST_bool &&
2150 DS.getTypeQualifiers() == DeclSpec::TQ_const &&
2151 DS.getStorageClassSpec() == DeclSpec::SCS_static) {
2152 Tok.getIdentifierInfo()->RevertTokenIDToIdentifier();
2153 Tok.setKind(tok::identifier);
2154 }
2155
2156 // We're done with the declaration-specifiers.
2157 goto DoneWithDeclSpec;
2158
Chris Lattner3bd934a2008-07-26 01:18:38 +00002159 // typedef-name
David Blaikie42d6d0c2011-12-04 05:04:18 +00002160 case tok::kw_decltype:
Chris Lattner3bd934a2008-07-26 01:18:38 +00002161 case tok::identifier: {
Chris Lattner5e02c472009-01-05 00:07:25 +00002162 // In C++, check to see if this is a scope specifier like foo::bar::, if
2163 // so handle it as such. This is important for ctor parsing.
David Blaikie4e4d0842012-03-11 07:00:24 +00002164 if (getLangOpts().CPlusPlus) {
John McCall9ba61662010-02-26 08:45:28 +00002165 if (TryAnnotateCXXScopeToken(true)) {
2166 if (!DS.hasTypeSpecifier())
2167 DS.SetTypeSpecError();
2168 goto DoneWithDeclSpec;
2169 }
2170 if (!Tok.is(tok::identifier))
2171 continue;
2172 }
Mike Stump1eb44332009-09-09 15:08:12 +00002173
Chris Lattner3bd934a2008-07-26 01:18:38 +00002174 // This identifier can only be a typedef name if we haven't already seen
2175 // a type-specifier. Without this check we misparse:
2176 // typedef int X; struct Y { short X; }; as 'short int'.
2177 if (DS.hasTypeSpecifier())
2178 goto DoneWithDeclSpec;
Mike Stump1eb44332009-09-09 15:08:12 +00002179
John Thompson82287d12010-02-05 00:12:22 +00002180 // Check for need to substitute AltiVec keyword tokens.
2181 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
2182 break;
2183
Richard Smithf63eee72012-05-09 18:56:43 +00002184 // [AltiVec] 2.2: [If the 'vector' specifier is used] The syntax does not
2185 // allow the use of a typedef name as a type specifier.
2186 if (DS.isTypeAltiVecVector())
2187 goto DoneWithDeclSpec;
2188
John McCallb3d87482010-08-24 05:47:05 +00002189 ParsedType TypeRep =
2190 Actions.getTypeName(*Tok.getIdentifierInfo(),
2191 Tok.getLocation(), getCurScope());
Douglas Gregor55f6b142009-02-09 18:46:07 +00002192
Chris Lattnerc199ab32009-04-12 20:42:31 +00002193 // If this is not a typedef name, don't parse it as part of the declspec,
2194 // it must be an implicit int or an error.
John McCallb3d87482010-08-24 05:47:05 +00002195 if (!TypeRep) {
Richard Smith69730c12012-03-12 07:56:15 +00002196 if (ParseImplicitInt(DS, 0, TemplateInfo, AS, DSContext)) continue;
Chris Lattner3bd934a2008-07-26 01:18:38 +00002197 goto DoneWithDeclSpec;
Chris Lattnerc199ab32009-04-12 20:42:31 +00002198 }
Douglas Gregor55f6b142009-02-09 18:46:07 +00002199
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002200 // If we're in a context where the identifier could be a class name,
2201 // check whether this is a constructor declaration.
David Blaikie4e4d0842012-03-11 07:00:24 +00002202 if (getLangOpts().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00002203 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), getCurScope()) &&
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002204 isConstructorDeclarator())
Douglas Gregorb48fe382008-10-31 09:07:45 +00002205 goto DoneWithDeclSpec;
2206
Douglas Gregor1a51b4a2009-02-09 15:09:02 +00002207 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00002208 DiagID, TypeRep);
Chris Lattner3bd934a2008-07-26 01:18:38 +00002209 if (isInvalid)
2210 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002211
Chris Lattner3bd934a2008-07-26 01:18:38 +00002212 DS.SetRangeEnd(Tok.getLocation());
2213 ConsumeToken(); // The identifier
2214
2215 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
2216 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00002217 // Objective-C interface.
David Blaikie4e4d0842012-03-11 07:00:24 +00002218 if (Tok.is(tok::less) && getLangOpts().ObjC1)
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00002219 ParseObjCProtocolQualifiers(DS);
2220
Steve Naroff4f9b9f12008-09-22 10:28:57 +00002221 // Need to support trailing type qualifiers (e.g. "id<p> const").
2222 // If a type specifier follows, it will be diagnosed elsewhere.
2223 continue;
Chris Lattner3bd934a2008-07-26 01:18:38 +00002224 }
Douglas Gregor39a8de12009-02-25 19:37:18 +00002225
2226 // type-name
2227 case tok::annot_template_id: {
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +00002228 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregorc45c2322009-03-31 00:43:58 +00002229 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor39a8de12009-02-25 19:37:18 +00002230 // This template-id does not refer to a type name, so we're
2231 // done with the type-specifiers.
2232 goto DoneWithDeclSpec;
2233 }
2234
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002235 // If we're in a context where the template-id could be a
2236 // constructor name or specialization, check whether this is a
2237 // constructor declaration.
David Blaikie4e4d0842012-03-11 07:00:24 +00002238 if (getLangOpts().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00002239 Actions.isCurrentClassName(*TemplateId->Name, getCurScope()) &&
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002240 isConstructorDeclarator())
2241 goto DoneWithDeclSpec;
2242
Douglas Gregor39a8de12009-02-25 19:37:18 +00002243 // Turn the template-id annotation token into a type annotation
2244 // token, then try again to parse it as a type-specifier.
Douglas Gregor31a19b62009-04-01 21:51:26 +00002245 AnnotateTemplateIdTokenAsType();
Douglas Gregor39a8de12009-02-25 19:37:18 +00002246 continue;
2247 }
2248
Reid Spencer5f016e22007-07-11 17:01:13 +00002249 // GNU attributes support.
2250 case tok::kw___attribute:
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +00002251 ParseGNUAttributes(DS.getAttributes(), 0, LateAttrs);
Reid Spencer5f016e22007-07-11 17:01:13 +00002252 continue;
Steve Narofff59e17e2008-12-24 20:59:21 +00002253
2254 // Microsoft declspec support.
2255 case tok::kw___declspec:
John McCall7f040a92010-12-24 02:08:15 +00002256 ParseMicrosoftDeclSpec(DS.getAttributes());
Steve Narofff59e17e2008-12-24 20:59:21 +00002257 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00002258
Steve Naroff239f0732008-12-25 14:16:32 +00002259 // Microsoft single token adornments.
Michael J. Spenceradc6cbf2012-06-18 07:00:48 +00002260 case tok::kw___forceinline: {
2261 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec, DiagID);
2262 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
2263 SourceLocation AttrNameLoc = ConsumeToken();
Sean Hunt93f95f22012-06-18 16:13:52 +00002264 // FIXME: This does not work correctly if it is set to be a declspec
2265 // attribute, and a GNU attribute is simply incorrect.
Michael J. Spenceradc6cbf2012-06-18 07:00:48 +00002266 DS.getAttributes().addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
Sean Hunt93f95f22012-06-18 16:13:52 +00002267 SourceLocation(), 0, 0, AttributeList::AS_GNU);
Michael J. Spenceradc6cbf2012-06-18 07:00:48 +00002268 continue;
2269 }
Eli Friedman290eeb02009-06-08 23:27:34 +00002270
2271 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00002272 case tok::kw___ptr32:
Steve Naroff86bc6cf2008-12-25 14:41:26 +00002273 case tok::kw___w64:
Steve Naroff239f0732008-12-25 14:16:32 +00002274 case tok::kw___cdecl:
2275 case tok::kw___stdcall:
2276 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00002277 case tok::kw___thiscall:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00002278 case tok::kw___unaligned:
John McCall7f040a92010-12-24 02:08:15 +00002279 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman290eeb02009-06-08 23:27:34 +00002280 continue;
2281
Dawn Perchik52fc3142010-09-03 01:29:35 +00002282 // Borland single token adornments.
2283 case tok::kw___pascal:
John McCall7f040a92010-12-24 02:08:15 +00002284 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik52fc3142010-09-03 01:29:35 +00002285 continue;
2286
Peter Collingbournef315fa82011-02-14 01:42:53 +00002287 // OpenCL single token adornments.
2288 case tok::kw___kernel:
2289 ParseOpenCLAttributes(DS.getAttributes());
2290 continue;
2291
Reid Spencer5f016e22007-07-11 17:01:13 +00002292 // storage-class-specifier
2293 case tok::kw_typedef:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002294 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_typedef, Loc,
2295 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002296 break;
2297 case tok::kw_extern:
2298 if (DS.isThreadSpecified())
Chris Lattner1ab3b962008-11-18 07:48:38 +00002299 Diag(Tok, diag::ext_thread_before) << "extern";
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002300 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_extern, Loc,
2301 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002302 break;
Steve Naroff8d54bf22007-12-18 00:16:02 +00002303 case tok::kw___private_extern__:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002304 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_private_extern,
2305 Loc, PrevSpec, DiagID);
Steve Naroff8d54bf22007-12-18 00:16:02 +00002306 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00002307 case tok::kw_static:
2308 if (DS.isThreadSpecified())
Chris Lattner1ab3b962008-11-18 07:48:38 +00002309 Diag(Tok, diag::ext_thread_before) << "static";
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002310 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_static, Loc,
2311 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002312 break;
2313 case tok::kw_auto:
David Blaikie4e4d0842012-03-11 07:00:24 +00002314 if (getLangOpts().CPlusPlus0x) {
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002315 if (isKnownToBeTypeSpecifier(GetLookAheadToken(1))) {
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002316 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc,
2317 PrevSpec, DiagID);
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002318 if (!isInvalid)
Richard Smith8f4fb192011-09-04 19:54:14 +00002319 Diag(Tok, diag::ext_auto_storage_class)
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002320 << FixItHint::CreateRemoval(DS.getStorageClassSpecLoc());
Richard Smith8f4fb192011-09-04 19:54:14 +00002321 } else
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002322 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec,
2323 DiagID);
Richard Smith8f4fb192011-09-04 19:54:14 +00002324 } else
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002325 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc,
2326 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002327 break;
2328 case tok::kw_register:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002329 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_register, Loc,
2330 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002331 break;
Sebastian Redl669d5d72008-11-14 23:42:31 +00002332 case tok::kw_mutable:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002333 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_mutable, Loc,
2334 PrevSpec, DiagID);
Sebastian Redl669d5d72008-11-14 23:42:31 +00002335 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00002336 case tok::kw___thread:
John McCallfec54012009-08-03 20:12:06 +00002337 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002338 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002339
Reid Spencer5f016e22007-07-11 17:01:13 +00002340 // function-specifier
2341 case tok::kw_inline:
John McCallfec54012009-08-03 20:12:06 +00002342 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002343 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +00002344 case tok::kw_virtual:
John McCallfec54012009-08-03 20:12:06 +00002345 isInvalid = DS.SetFunctionSpecVirtual(Loc, PrevSpec, DiagID);
Douglas Gregorb48fe382008-10-31 09:07:45 +00002346 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +00002347 case tok::kw_explicit:
John McCallfec54012009-08-03 20:12:06 +00002348 isInvalid = DS.SetFunctionSpecExplicit(Loc, PrevSpec, DiagID);
Douglas Gregorb48fe382008-10-31 09:07:45 +00002349 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002350
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002351 // alignment-specifier
2352 case tok::kw__Alignas:
David Blaikie4e4d0842012-03-11 07:00:24 +00002353 if (!getLangOpts().C11)
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00002354 Diag(Tok, diag::ext_c11_alignas);
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002355 ParseAlignmentSpecifier(DS.getAttributes());
2356 continue;
2357
Anders Carlssonf47f7a12009-05-06 04:46:28 +00002358 // friend
2359 case tok::kw_friend:
John McCall67d1a672009-08-06 02:15:43 +00002360 if (DSContext == DSC_class)
2361 isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID);
2362 else {
2363 PrevSpec = ""; // not actually used by the diagnostic
2364 DiagID = diag::err_friend_invalid_in_context;
2365 isInvalid = true;
2366 }
Anders Carlssonf47f7a12009-05-06 04:46:28 +00002367 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002368
Douglas Gregor8d267c52011-09-09 02:06:17 +00002369 // Modules
2370 case tok::kw___module_private__:
2371 isInvalid = DS.setModulePrivateSpec(Loc, PrevSpec, DiagID);
2372 break;
2373
Sebastian Redl2ac67232009-11-05 15:47:02 +00002374 // constexpr
2375 case tok::kw_constexpr:
2376 isInvalid = DS.SetConstexprSpec(Loc, PrevSpec, DiagID);
2377 break;
2378
Chris Lattner80d0c892009-01-21 19:48:37 +00002379 // type-specifier
2380 case tok::kw_short:
John McCallfec54012009-08-03 20:12:06 +00002381 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec,
2382 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002383 break;
2384 case tok::kw_long:
2385 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCallfec54012009-08-03 20:12:06 +00002386 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
2387 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002388 else
John McCallfec54012009-08-03 20:12:06 +00002389 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2390 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002391 break;
Francois Pichet338d7f72011-04-28 01:59:37 +00002392 case tok::kw___int64:
2393 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2394 DiagID);
2395 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002396 case tok::kw_signed:
John McCallfec54012009-08-03 20:12:06 +00002397 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec,
2398 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002399 break;
2400 case tok::kw_unsigned:
John McCallfec54012009-08-03 20:12:06 +00002401 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
2402 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002403 break;
2404 case tok::kw__Complex:
John McCallfec54012009-08-03 20:12:06 +00002405 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
2406 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002407 break;
2408 case tok::kw__Imaginary:
John McCallfec54012009-08-03 20:12:06 +00002409 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
2410 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002411 break;
2412 case tok::kw_void:
John McCallfec54012009-08-03 20:12:06 +00002413 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec,
2414 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002415 break;
2416 case tok::kw_char:
John McCallfec54012009-08-03 20:12:06 +00002417 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec,
2418 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002419 break;
2420 case tok::kw_int:
John McCallfec54012009-08-03 20:12:06 +00002421 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec,
2422 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002423 break;
Richard Smith5a5a9712012-04-04 06:24:32 +00002424 case tok::kw___int128:
2425 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int128, Loc, PrevSpec,
2426 DiagID);
2427 break;
2428 case tok::kw_half:
2429 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_half, Loc, PrevSpec,
2430 DiagID);
2431 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002432 case tok::kw_float:
John McCallfec54012009-08-03 20:12:06 +00002433 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec,
2434 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002435 break;
2436 case tok::kw_double:
John McCallfec54012009-08-03 20:12:06 +00002437 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec,
2438 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002439 break;
2440 case tok::kw_wchar_t:
John McCallfec54012009-08-03 20:12:06 +00002441 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec,
2442 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002443 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002444 case tok::kw_char16_t:
John McCallfec54012009-08-03 20:12:06 +00002445 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec,
2446 DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002447 break;
2448 case tok::kw_char32_t:
John McCallfec54012009-08-03 20:12:06 +00002449 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec,
2450 DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002451 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002452 case tok::kw_bool:
2453 case tok::kw__Bool:
Argyrios Kyrtzidis4383e182010-11-16 18:18:13 +00002454 if (Tok.is(tok::kw_bool) &&
2455 DS.getTypeSpecType() != DeclSpec::TST_unspecified &&
2456 DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
2457 PrevSpec = ""; // Not used by the diagnostic.
2458 DiagID = diag::err_bool_redeclaration;
Fariborz Jahaniane106a0b2011-04-19 21:42:37 +00002459 // For better error recovery.
2460 Tok.setKind(tok::identifier);
Argyrios Kyrtzidis4383e182010-11-16 18:18:13 +00002461 isInvalid = true;
2462 } else {
2463 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec,
2464 DiagID);
2465 }
Chris Lattner80d0c892009-01-21 19:48:37 +00002466 break;
2467 case tok::kw__Decimal32:
John McCallfec54012009-08-03 20:12:06 +00002468 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
2469 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002470 break;
2471 case tok::kw__Decimal64:
John McCallfec54012009-08-03 20:12:06 +00002472 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
2473 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002474 break;
2475 case tok::kw__Decimal128:
John McCallfec54012009-08-03 20:12:06 +00002476 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
2477 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002478 break;
John Thompson82287d12010-02-05 00:12:22 +00002479 case tok::kw___vector:
2480 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
2481 break;
2482 case tok::kw___pixel:
2483 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
2484 break;
John McCalla5fc4722011-04-09 22:50:59 +00002485 case tok::kw___unknown_anytype:
2486 isInvalid = DS.SetTypeSpecType(TST_unknown_anytype, Loc,
2487 PrevSpec, DiagID);
2488 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002489
2490 // class-specifier:
2491 case tok::kw_class:
2492 case tok::kw_struct:
Chris Lattner4c97d762009-04-12 21:49:30 +00002493 case tok::kw_union: {
2494 tok::TokenKind Kind = Tok.getKind();
2495 ConsumeToken();
Richard Smith69730c12012-03-12 07:56:15 +00002496 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS,
2497 EnteringContext, DSContext);
Chris Lattner80d0c892009-01-21 19:48:37 +00002498 continue;
Chris Lattner4c97d762009-04-12 21:49:30 +00002499 }
Chris Lattner80d0c892009-01-21 19:48:37 +00002500
2501 // enum-specifier:
2502 case tok::kw_enum:
Chris Lattner4c97d762009-04-12 21:49:30 +00002503 ConsumeToken();
Richard Smith69730c12012-03-12 07:56:15 +00002504 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS, DSContext);
Chris Lattner80d0c892009-01-21 19:48:37 +00002505 continue;
2506
2507 // cv-qualifier:
2508 case tok::kw_const:
John McCallfec54012009-08-03 20:12:06 +00002509 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec, DiagID,
David Blaikie4e4d0842012-03-11 07:00:24 +00002510 getLangOpts());
Chris Lattner80d0c892009-01-21 19:48:37 +00002511 break;
2512 case tok::kw_volatile:
John McCallfec54012009-08-03 20:12:06 +00002513 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
David Blaikie4e4d0842012-03-11 07:00:24 +00002514 getLangOpts());
Chris Lattner80d0c892009-01-21 19:48:37 +00002515 break;
2516 case tok::kw_restrict:
John McCallfec54012009-08-03 20:12:06 +00002517 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
David Blaikie4e4d0842012-03-11 07:00:24 +00002518 getLangOpts());
Chris Lattner80d0c892009-01-21 19:48:37 +00002519 break;
2520
Douglas Gregord57959a2009-03-27 23:10:48 +00002521 // C++ typename-specifier:
2522 case tok::kw_typename:
John McCall9ba61662010-02-26 08:45:28 +00002523 if (TryAnnotateTypeOrScopeToken()) {
2524 DS.SetTypeSpecError();
2525 goto DoneWithDeclSpec;
2526 }
2527 if (!Tok.is(tok::kw_typename))
Douglas Gregord57959a2009-03-27 23:10:48 +00002528 continue;
2529 break;
2530
Chris Lattner80d0c892009-01-21 19:48:37 +00002531 // GNU typeof support.
2532 case tok::kw_typeof:
2533 ParseTypeofSpecifier(DS);
2534 continue;
2535
David Blaikie42d6d0c2011-12-04 05:04:18 +00002536 case tok::annot_decltype:
Anders Carlsson6fd634f2009-06-24 17:47:40 +00002537 ParseDecltypeSpecifier(DS);
2538 continue;
2539
Sean Huntdb5d44b2011-05-19 05:37:45 +00002540 case tok::kw___underlying_type:
2541 ParseUnderlyingTypeSpecifier(DS);
Eli Friedmanb001de72011-10-06 23:00:33 +00002542 continue;
2543
2544 case tok::kw__Atomic:
2545 ParseAtomicSpecifier(DS);
2546 continue;
Sean Huntdb5d44b2011-05-19 05:37:45 +00002547
Peter Collingbourne207f4d82011-03-18 22:38:29 +00002548 // OpenCL qualifiers:
2549 case tok::kw_private:
David Blaikie4e4d0842012-03-11 07:00:24 +00002550 if (!getLangOpts().OpenCL)
Peter Collingbourne207f4d82011-03-18 22:38:29 +00002551 goto DoneWithDeclSpec;
2552 case tok::kw___private:
2553 case tok::kw___global:
2554 case tok::kw___local:
2555 case tok::kw___constant:
2556 case tok::kw___read_only:
2557 case tok::kw___write_only:
2558 case tok::kw___read_write:
2559 ParseOpenCLQualifiers(DS);
2560 break;
2561
Steve Naroffd3ded1f2008-06-05 00:02:44 +00002562 case tok::less:
Chris Lattner3bd934a2008-07-26 01:18:38 +00002563 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattnerbce61352008-07-26 00:20:22 +00002564 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
2565 // but we support it.
David Blaikie4e4d0842012-03-11 07:00:24 +00002566 if (DS.hasTypeSpecifier() || !getLangOpts().ObjC1)
Chris Lattnerbce61352008-07-26 00:20:22 +00002567 goto DoneWithDeclSpec;
Mike Stump1eb44332009-09-09 15:08:12 +00002568
Douglas Gregor46f936e2010-11-19 17:10:50 +00002569 if (!ParseObjCProtocolQualifiers(DS))
2570 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
2571 << FixItHint::CreateInsertion(Loc, "id")
2572 << SourceRange(Loc, DS.getSourceRange().getEnd());
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00002573
2574 // Need to support trailing type qualifiers (e.g. "id<p> const").
2575 // If a type specifier follows, it will be diagnosed elsewhere.
2576 continue;
Reid Spencer5f016e22007-07-11 17:01:13 +00002577 }
John McCallfec54012009-08-03 20:12:06 +00002578 // If the specifier wasn't legal, issue a diagnostic.
Reid Spencer5f016e22007-07-11 17:01:13 +00002579 if (isInvalid) {
2580 assert(PrevSpec && "Method did not return previous specifier!");
John McCallfec54012009-08-03 20:12:06 +00002581 assert(DiagID);
Douglas Gregorae2fb142010-08-23 14:34:43 +00002582
2583 if (DiagID == diag::ext_duplicate_declspec)
2584 Diag(Tok, DiagID)
2585 << PrevSpec << FixItHint::CreateRemoval(Tok.getLocation());
2586 else
2587 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00002588 }
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002589
Chris Lattner81c018d2008-03-13 06:29:04 +00002590 DS.SetRangeEnd(Tok.getLocation());
Fariborz Jahaniane106a0b2011-04-19 21:42:37 +00002591 if (DiagID != diag::err_bool_redeclaration)
2592 ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00002593 }
2594}
Douglas Gregoradcac882008-12-01 23:54:00 +00002595
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002596/// ParseStructDeclaration - Parse a struct declaration without the terminating
2597/// semicolon.
2598///
Reid Spencer5f016e22007-07-11 17:01:13 +00002599/// struct-declaration:
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002600/// specifier-qualifier-list struct-declarator-list
Reid Spencer5f016e22007-07-11 17:01:13 +00002601/// [GNU] __extension__ struct-declaration
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002602/// [GNU] specifier-qualifier-list
Reid Spencer5f016e22007-07-11 17:01:13 +00002603/// struct-declarator-list:
2604/// struct-declarator
2605/// struct-declarator-list ',' struct-declarator
2606/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
2607/// struct-declarator:
2608/// declarator
2609/// [GNU] declarator attributes[opt]
2610/// declarator[opt] ':' constant-expression
2611/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
2612///
Chris Lattnere1359422008-04-10 06:46:29 +00002613void Parser::
John McCallbdd563e2009-11-03 02:38:08 +00002614ParseStructDeclaration(DeclSpec &DS, FieldCallback &Fields) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002615
Chris Lattnerc46d1a12008-10-20 06:45:43 +00002616 if (Tok.is(tok::kw___extension__)) {
2617 // __extension__ silences extension warnings in the subexpression.
2618 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroff28a7ca82007-08-20 22:28:22 +00002619 ConsumeToken();
Chris Lattnerc46d1a12008-10-20 06:45:43 +00002620 return ParseStructDeclaration(DS, Fields);
2621 }
Mike Stump1eb44332009-09-09 15:08:12 +00002622
Steve Naroff28a7ca82007-08-20 22:28:22 +00002623 // Parse the common specifier-qualifiers-list piece.
Steve Naroff28a7ca82007-08-20 22:28:22 +00002624 ParseSpecifierQualifierList(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00002625
Douglas Gregor4920f1f2009-01-12 22:49:06 +00002626 // If there are no declarators, this is a free-standing declaration
2627 // specifier. Let the actions module cope with it.
Chris Lattner04d66662007-10-09 17:33:22 +00002628 if (Tok.is(tok::semi)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00002629 Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none, DS);
Steve Naroff28a7ca82007-08-20 22:28:22 +00002630 return;
2631 }
2632
2633 // Read struct-declarators until we find the semicolon.
John McCallbdd563e2009-11-03 02:38:08 +00002634 bool FirstDeclarator = true;
Richard Smith7984de32012-01-12 23:53:29 +00002635 SourceLocation CommaLoc;
Steve Naroff28a7ca82007-08-20 22:28:22 +00002636 while (1) {
John McCall92576642012-05-07 06:16:41 +00002637 ParsingDeclRAIIObject PD(*this, ParsingDeclRAIIObject::NoParent);
John McCallbdd563e2009-11-03 02:38:08 +00002638 FieldDeclarator DeclaratorInfo(DS);
Richard Smith7984de32012-01-12 23:53:29 +00002639 DeclaratorInfo.D.setCommaLoc(CommaLoc);
John McCallbdd563e2009-11-03 02:38:08 +00002640
2641 // Attributes are only allowed here on successive declarators.
John McCall7f040a92010-12-24 02:08:15 +00002642 if (!FirstDeclarator)
2643 MaybeParseGNUAttributes(DeclaratorInfo.D);
Mike Stump1eb44332009-09-09 15:08:12 +00002644
Steve Naroff28a7ca82007-08-20 22:28:22 +00002645 /// struct-declarator: declarator
2646 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattnera1efc8c2009-12-10 01:59:24 +00002647 if (Tok.isNot(tok::colon)) {
2648 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
2649 ColonProtectionRAIIObject X(*this);
Chris Lattnere1359422008-04-10 06:46:29 +00002650 ParseDeclarator(DeclaratorInfo.D);
Chris Lattnera1efc8c2009-12-10 01:59:24 +00002651 }
Mike Stump1eb44332009-09-09 15:08:12 +00002652
Chris Lattner04d66662007-10-09 17:33:22 +00002653 if (Tok.is(tok::colon)) {
Steve Naroff28a7ca82007-08-20 22:28:22 +00002654 ConsumeToken();
John McCall60d7b3a2010-08-24 06:29:42 +00002655 ExprResult Res(ParseConstantExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002656 if (Res.isInvalid())
Steve Naroff28a7ca82007-08-20 22:28:22 +00002657 SkipUntil(tok::semi, true, true);
Chris Lattner60b1e3e2008-04-10 06:15:14 +00002658 else
Sebastian Redleffa8d12008-12-10 00:02:53 +00002659 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroff28a7ca82007-08-20 22:28:22 +00002660 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00002661
Steve Naroff28a7ca82007-08-20 22:28:22 +00002662 // If attributes exist after the declarator, parse them.
John McCall7f040a92010-12-24 02:08:15 +00002663 MaybeParseGNUAttributes(DeclaratorInfo.D);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002664
John McCallbdd563e2009-11-03 02:38:08 +00002665 // We're done with this declarator; invoke the callback.
John McCalld226f652010-08-21 09:40:31 +00002666 Decl *D = Fields.invoke(DeclaratorInfo);
John McCall54abf7d2009-11-04 02:18:39 +00002667 PD.complete(D);
John McCallbdd563e2009-11-03 02:38:08 +00002668
Steve Naroff28a7ca82007-08-20 22:28:22 +00002669 // If we don't have a comma, it is either the end of the list (a ';')
2670 // or an error, bail out.
Chris Lattner04d66662007-10-09 17:33:22 +00002671 if (Tok.isNot(tok::comma))
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002672 return;
Sebastian Redlab197ba2009-02-09 18:23:29 +00002673
Steve Naroff28a7ca82007-08-20 22:28:22 +00002674 // Consume the comma.
Richard Smith7984de32012-01-12 23:53:29 +00002675 CommaLoc = ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00002676
John McCallbdd563e2009-11-03 02:38:08 +00002677 FirstDeclarator = false;
Steve Naroff28a7ca82007-08-20 22:28:22 +00002678 }
Steve Naroff28a7ca82007-08-20 22:28:22 +00002679}
2680
2681/// ParseStructUnionBody
2682/// struct-contents:
2683/// struct-declaration-list
2684/// [EXT] empty
2685/// [GNU] "struct-declaration-list" without terminatoring ';'
2686/// struct-declaration-list:
2687/// struct-declaration
2688/// struct-declaration-list struct-declaration
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002689/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroff28a7ca82007-08-20 22:28:22 +00002690///
Reid Spencer5f016e22007-07-11 17:01:13 +00002691void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
John McCalld226f652010-08-21 09:40:31 +00002692 unsigned TagType, Decl *TagDecl) {
John McCallf312b1e2010-08-26 23:41:50 +00002693 PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc,
2694 "parsing struct/union body");
Mike Stump1eb44332009-09-09 15:08:12 +00002695
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002696 BalancedDelimiterTracker T(*this, tok::l_brace);
2697 if (T.consumeOpen())
2698 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002699
Douglas Gregor3218c4b2009-01-09 22:42:13 +00002700 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
Douglas Gregor23c94db2010-07-02 17:43:08 +00002701 Actions.ActOnTagStartDefinition(getCurScope(), TagDecl);
Douglas Gregor72de6672009-01-08 20:45:30 +00002702
Reid Spencer5f016e22007-07-11 17:01:13 +00002703 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
2704 // C++.
David Blaikie4e4d0842012-03-11 07:00:24 +00002705 if (Tok.is(tok::r_brace) && !getLangOpts().CPlusPlus) {
Richard Smithd7c56e12011-12-29 21:57:33 +00002706 Diag(Tok, diag::ext_empty_struct_union) << (TagType == TST_union);
2707 Diag(Tok, diag::warn_empty_struct_union_compat) << (TagType == TST_union);
2708 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002709
Chris Lattner5f9e2722011-07-23 10:55:15 +00002710 SmallVector<Decl *, 32> FieldDecls;
Chris Lattnere1359422008-04-10 06:46:29 +00002711
Reid Spencer5f016e22007-07-11 17:01:13 +00002712 // While we still have something to read, read the declarations in the struct.
Chris Lattner04d66662007-10-09 17:33:22 +00002713 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002714 // Each iteration of this loop reads one struct-declaration.
Mike Stump1eb44332009-09-09 15:08:12 +00002715
Reid Spencer5f016e22007-07-11 17:01:13 +00002716 // Check for extraneous top-level semicolon.
Chris Lattner04d66662007-10-09 17:33:22 +00002717 if (Tok.is(tok::semi)) {
Richard Trieu4b0e6f12012-05-16 19:04:59 +00002718 ConsumeExtraSemi(InsideStruct,
2719 DeclSpec::getSpecifierName((DeclSpec::TST)TagType));
Reid Spencer5f016e22007-07-11 17:01:13 +00002720 continue;
2721 }
Chris Lattnere1359422008-04-10 06:46:29 +00002722
2723 // Parse all the comma separated declarators.
John McCall0b7e6782011-03-24 11:26:52 +00002724 DeclSpec DS(AttrFactory);
Mike Stump1eb44332009-09-09 15:08:12 +00002725
John McCallbdd563e2009-11-03 02:38:08 +00002726 if (!Tok.is(tok::at)) {
2727 struct CFieldCallback : FieldCallback {
2728 Parser &P;
John McCalld226f652010-08-21 09:40:31 +00002729 Decl *TagDecl;
Chris Lattner5f9e2722011-07-23 10:55:15 +00002730 SmallVectorImpl<Decl *> &FieldDecls;
John McCallbdd563e2009-11-03 02:38:08 +00002731
John McCalld226f652010-08-21 09:40:31 +00002732 CFieldCallback(Parser &P, Decl *TagDecl,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002733 SmallVectorImpl<Decl *> &FieldDecls) :
John McCallbdd563e2009-11-03 02:38:08 +00002734 P(P), TagDecl(TagDecl), FieldDecls(FieldDecls) {}
2735
John McCalld226f652010-08-21 09:40:31 +00002736 virtual Decl *invoke(FieldDeclarator &FD) {
John McCallbdd563e2009-11-03 02:38:08 +00002737 // Install the declarator into the current TagDecl.
John McCalld226f652010-08-21 09:40:31 +00002738 Decl *Field = P.Actions.ActOnField(P.getCurScope(), TagDecl,
John McCall4ba39712009-11-03 21:13:47 +00002739 FD.D.getDeclSpec().getSourceRange().getBegin(),
2740 FD.D, FD.BitfieldSize);
John McCallbdd563e2009-11-03 02:38:08 +00002741 FieldDecls.push_back(Field);
2742 return Field;
Douglas Gregor91a28862009-08-26 14:27:30 +00002743 }
John McCallbdd563e2009-11-03 02:38:08 +00002744 } Callback(*this, TagDecl, FieldDecls);
2745
2746 ParseStructDeclaration(DS, Callback);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002747 } else { // Handle @defs
2748 ConsumeToken();
2749 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
2750 Diag(Tok, diag::err_unexpected_at);
Chris Lattner3e156ad2010-02-02 00:37:27 +00002751 SkipUntil(tok::semi, true);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002752 continue;
2753 }
2754 ConsumeToken();
2755 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
2756 if (!Tok.is(tok::identifier)) {
2757 Diag(Tok, diag::err_expected_ident);
Chris Lattner3e156ad2010-02-02 00:37:27 +00002758 SkipUntil(tok::semi, true);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002759 continue;
2760 }
Chris Lattner5f9e2722011-07-23 10:55:15 +00002761 SmallVector<Decl *, 16> Fields;
Douglas Gregor23c94db2010-07-02 17:43:08 +00002762 Actions.ActOnDefs(getCurScope(), TagDecl, Tok.getLocation(),
Douglas Gregor44b43212008-12-11 16:49:14 +00002763 Tok.getIdentifierInfo(), Fields);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002764 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
2765 ConsumeToken();
2766 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
Mike Stump1eb44332009-09-09 15:08:12 +00002767 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002768
Chris Lattner04d66662007-10-09 17:33:22 +00002769 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002770 ConsumeToken();
Chris Lattner04d66662007-10-09 17:33:22 +00002771 } else if (Tok.is(tok::r_brace)) {
Chris Lattner3e156ad2010-02-02 00:37:27 +00002772 ExpectAndConsume(tok::semi, diag::ext_expected_semi_decl_list);
Reid Spencer5f016e22007-07-11 17:01:13 +00002773 break;
2774 } else {
Chris Lattner3e156ad2010-02-02 00:37:27 +00002775 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
2776 // Skip to end of block or statement to avoid ext-warning on extra ';'.
Reid Spencer5f016e22007-07-11 17:01:13 +00002777 SkipUntil(tok::r_brace, true, true);
Chris Lattner3e156ad2010-02-02 00:37:27 +00002778 // If we stopped at a ';', eat it.
2779 if (Tok.is(tok::semi)) ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00002780 }
2781 }
Mike Stump1eb44332009-09-09 15:08:12 +00002782
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002783 T.consumeClose();
Mike Stump1eb44332009-09-09 15:08:12 +00002784
John McCall0b7e6782011-03-24 11:26:52 +00002785 ParsedAttributes attrs(AttrFactory);
Reid Spencer5f016e22007-07-11 17:01:13 +00002786 // If attributes exist after struct contents, parse them.
John McCall7f040a92010-12-24 02:08:15 +00002787 MaybeParseGNUAttributes(attrs);
Daniel Dunbar1bfe1c22008-10-03 02:03:53 +00002788
Douglas Gregor23c94db2010-07-02 17:43:08 +00002789 Actions.ActOnFields(getCurScope(),
David Blaikie77b6de02011-09-22 02:58:26 +00002790 RecordLoc, TagDecl, FieldDecls,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002791 T.getOpenLocation(), T.getCloseLocation(),
John McCall7f040a92010-12-24 02:08:15 +00002792 attrs.getList());
Douglas Gregor72de6672009-01-08 20:45:30 +00002793 StructScope.Exit();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002794 Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl,
2795 T.getCloseLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00002796}
2797
Reid Spencer5f016e22007-07-11 17:01:13 +00002798/// ParseEnumSpecifier
2799/// enum-specifier: [C99 6.7.2.2]
2800/// 'enum' identifier[opt] '{' enumerator-list '}'
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002801///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Reid Spencer5f016e22007-07-11 17:01:13 +00002802/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
2803/// '}' attributes[opt]
Aaron Ballman6454a022012-03-01 04:09:28 +00002804/// [MS] 'enum' __declspec[opt] identifier[opt] '{' enumerator-list ',' [opt]
2805/// '}'
Reid Spencer5f016e22007-07-11 17:01:13 +00002806/// 'enum' identifier
2807/// [GNU] 'enum' attributes[opt] identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002808///
Richard Smith1af83c42012-03-23 03:33:32 +00002809/// [C++11] enum-head '{' enumerator-list[opt] '}'
2810/// [C++11] enum-head '{' enumerator-list ',' '}'
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002811///
Richard Smith1af83c42012-03-23 03:33:32 +00002812/// enum-head: [C++11]
2813/// enum-key attribute-specifier-seq[opt] identifier[opt] enum-base[opt]
2814/// enum-key attribute-specifier-seq[opt] nested-name-specifier
2815/// identifier enum-base[opt]
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002816///
Richard Smith1af83c42012-03-23 03:33:32 +00002817/// enum-key: [C++11]
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002818/// 'enum'
2819/// 'enum' 'class'
2820/// 'enum' 'struct'
2821///
Richard Smith1af83c42012-03-23 03:33:32 +00002822/// enum-base: [C++11]
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002823/// ':' type-specifier-seq
2824///
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002825/// [C++] elaborated-type-specifier:
2826/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
2827///
Chris Lattner4c97d762009-04-12 21:49:30 +00002828void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregor9b9edd62010-03-02 17:53:14 +00002829 const ParsedTemplateInfo &TemplateInfo,
Richard Smith69730c12012-03-12 07:56:15 +00002830 AccessSpecifier AS, DeclSpecContext DSC) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002831 // Parse the tag portion of this.
Douglas Gregor374929f2009-09-18 15:37:17 +00002832 if (Tok.is(tok::code_completion)) {
2833 // Code completion for an enum name.
Douglas Gregor23c94db2010-07-02 17:43:08 +00002834 Actions.CodeCompleteTag(getCurScope(), DeclSpec::TST_enum);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002835 return cutOffParsing();
Douglas Gregor374929f2009-09-18 15:37:17 +00002836 }
John McCall57c13002011-07-06 05:58:41 +00002837
Richard Smithbdad7a22012-01-10 01:33:14 +00002838 SourceLocation ScopedEnumKWLoc;
John McCall57c13002011-07-06 05:58:41 +00002839 bool IsScopedUsingClassTag = false;
2840
David Blaikie4e4d0842012-03-11 07:00:24 +00002841 if (getLangOpts().CPlusPlus0x &&
John McCall57c13002011-07-06 05:58:41 +00002842 (Tok.is(tok::kw_class) || Tok.is(tok::kw_struct))) {
Richard Smith7fe62082011-10-15 05:09:34 +00002843 Diag(Tok, diag::warn_cxx98_compat_scoped_enum);
John McCall57c13002011-07-06 05:58:41 +00002844 IsScopedUsingClassTag = Tok.is(tok::kw_class);
Richard Smithbdad7a22012-01-10 01:33:14 +00002845 ScopedEnumKWLoc = ConsumeToken();
John McCall57c13002011-07-06 05:58:41 +00002846 }
Richard Smith1af83c42012-03-23 03:33:32 +00002847
John McCall13489672012-05-07 06:16:58 +00002848 // C++11 [temp.explicit]p12:
2849 // The usual access controls do not apply to names used to specify
2850 // explicit instantiations.
2851 // We extend this to also cover explicit specializations. Note that
2852 // we don't suppress if this turns out to be an elaborated type
2853 // specifier.
2854 bool shouldDelayDiagsInTag =
2855 (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation ||
2856 TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization);
2857 SuppressAccessChecks diagsFromTag(*this, shouldDelayDiagsInTag);
Richard Smith1af83c42012-03-23 03:33:32 +00002858
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002859 // If attributes exist after tag, parse them.
John McCall0b7e6782011-03-24 11:26:52 +00002860 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00002861 MaybeParseGNUAttributes(attrs);
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002862
Aaron Ballman6454a022012-03-01 04:09:28 +00002863 // If declspecs exist after tag, parse them.
2864 while (Tok.is(tok::kw___declspec))
2865 ParseMicrosoftDeclSpec(attrs);
2866
Richard Smith7796eb52012-03-12 08:56:40 +00002867 // Enum definitions should not be parsed in a trailing-return-type.
2868 bool AllowDeclaration = DSC != DSC_trailing;
2869
2870 bool AllowFixedUnderlyingType = AllowDeclaration &&
2871 (getLangOpts().CPlusPlus0x || getLangOpts().MicrosoftExt ||
2872 getLangOpts().ObjC2);
John McCall57c13002011-07-06 05:58:41 +00002873
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00002874 CXXScopeSpec &SS = DS.getTypeSpecScope();
David Blaikie4e4d0842012-03-11 07:00:24 +00002875 if (getLangOpts().CPlusPlus) {
John McCall57c13002011-07-06 05:58:41 +00002876 // "enum foo : bar;" is not a potential typo for "enum foo::bar;"
2877 // if a fixed underlying type is allowed.
2878 ColonProtectionRAIIObject X(*this, AllowFixedUnderlyingType);
2879
Douglas Gregorefaa93a2011-11-07 17:33:42 +00002880 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
2881 /*EnteringContext=*/false))
John McCall9ba61662010-02-26 08:45:28 +00002882 return;
2883
2884 if (SS.isSet() && Tok.isNot(tok::identifier)) {
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002885 Diag(Tok, diag::err_expected_ident);
2886 if (Tok.isNot(tok::l_brace)) {
2887 // Has no name and is not a definition.
2888 // Skip the rest of this declarator, up until the comma or semicolon.
2889 SkipUntil(tok::comma, true);
2890 return;
2891 }
2892 }
2893 }
Mike Stump1eb44332009-09-09 15:08:12 +00002894
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002895 // Must have either 'enum name' or 'enum {...}'.
Douglas Gregorb9075602011-02-22 02:55:24 +00002896 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace) &&
Richard Smith7796eb52012-03-12 08:56:40 +00002897 !(AllowFixedUnderlyingType && Tok.is(tok::colon))) {
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002898 Diag(Tok, diag::err_expected_ident_lbrace);
Mike Stump1eb44332009-09-09 15:08:12 +00002899
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002900 // Skip the rest of this declarator, up until the comma or semicolon.
2901 SkipUntil(tok::comma, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00002902 return;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002903 }
Mike Stump1eb44332009-09-09 15:08:12 +00002904
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002905 // If an identifier is present, consume and remember it.
2906 IdentifierInfo *Name = 0;
2907 SourceLocation NameLoc;
2908 if (Tok.is(tok::identifier)) {
2909 Name = Tok.getIdentifierInfo();
2910 NameLoc = ConsumeToken();
2911 }
Mike Stump1eb44332009-09-09 15:08:12 +00002912
Richard Smithbdad7a22012-01-10 01:33:14 +00002913 if (!Name && ScopedEnumKWLoc.isValid()) {
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002914 // C++0x 7.2p2: The optional identifier shall not be omitted in the
2915 // declaration of a scoped enumeration.
2916 Diag(Tok, diag::err_scoped_enum_missing_identifier);
Richard Smithbdad7a22012-01-10 01:33:14 +00002917 ScopedEnumKWLoc = SourceLocation();
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00002918 IsScopedUsingClassTag = false;
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002919 }
2920
John McCall13489672012-05-07 06:16:58 +00002921 // Okay, end the suppression area. We'll decide whether to emit the
2922 // diagnostics in a second.
2923 if (shouldDelayDiagsInTag)
2924 diagsFromTag.done();
Richard Smith1af83c42012-03-23 03:33:32 +00002925
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002926 TypeResult BaseType;
2927
Douglas Gregora61b3e72010-12-01 17:42:47 +00002928 // Parse the fixed underlying type.
Douglas Gregorb9075602011-02-22 02:55:24 +00002929 if (AllowFixedUnderlyingType && Tok.is(tok::colon)) {
Douglas Gregora61b3e72010-12-01 17:42:47 +00002930 bool PossibleBitfield = false;
2931 if (getCurScope()->getFlags() & Scope::ClassScope) {
2932 // If we're in class scope, this can either be an enum declaration with
2933 // an underlying type, or a declaration of a bitfield member. We try to
2934 // use a simple disambiguation scheme first to catch the common cases
2935 // (integer literal, sizeof); if it's still ambiguous, we then consider
2936 // anything that's a simple-type-specifier followed by '(' as an
2937 // expression. This suffices because function types are not valid
2938 // underlying types anyway.
2939 TPResult TPR = isExpressionOrTypeSpecifierSimple(NextToken().getKind());
2940 // If the next token starts an expression, we know we're parsing a
2941 // bit-field. This is the common case.
2942 if (TPR == TPResult::True())
2943 PossibleBitfield = true;
2944 // If the next token starts a type-specifier-seq, it may be either a
2945 // a fixed underlying type or the start of a function-style cast in C++;
2946 // lookahead one more token to see if it's obvious that we have a
2947 // fixed underlying type.
2948 else if (TPR == TPResult::False() &&
2949 GetLookAheadToken(2).getKind() == tok::semi) {
2950 // Consume the ':'.
2951 ConsumeToken();
2952 } else {
2953 // We have the start of a type-specifier-seq, so we have to perform
2954 // tentative parsing to determine whether we have an expression or a
2955 // type.
2956 TentativeParsingAction TPA(*this);
2957
2958 // Consume the ':'.
2959 ConsumeToken();
Richard Smithd81e9612012-02-23 01:36:12 +00002960
2961 // If we see a type specifier followed by an open-brace, we have an
2962 // ambiguity between an underlying type and a C++11 braced
2963 // function-style cast. Resolve this by always treating it as an
2964 // underlying type.
2965 // FIXME: The standard is not entirely clear on how to disambiguate in
2966 // this case.
David Blaikie4e4d0842012-03-11 07:00:24 +00002967 if ((getLangOpts().CPlusPlus &&
Richard Smithd81e9612012-02-23 01:36:12 +00002968 isCXXDeclarationSpecifier(TPResult::True()) != TPResult::True()) ||
David Blaikie4e4d0842012-03-11 07:00:24 +00002969 (!getLangOpts().CPlusPlus && !isDeclarationSpecifier(true))) {
Douglas Gregora61b3e72010-12-01 17:42:47 +00002970 // We'll parse this as a bitfield later.
2971 PossibleBitfield = true;
2972 TPA.Revert();
2973 } else {
2974 // We have a type-specifier-seq.
2975 TPA.Commit();
2976 }
2977 }
2978 } else {
2979 // Consume the ':'.
2980 ConsumeToken();
2981 }
2982
2983 if (!PossibleBitfield) {
2984 SourceRange Range;
2985 BaseType = ParseTypeName(&Range);
Douglas Gregor86f208c2011-02-22 20:32:04 +00002986
David Blaikie4e4d0842012-03-11 07:00:24 +00002987 if (!getLangOpts().CPlusPlus0x && !getLangOpts().ObjC2)
Douglas Gregor86f208c2011-02-22 20:32:04 +00002988 Diag(StartLoc, diag::ext_ms_enum_fixed_underlying_type)
2989 << Range;
David Blaikie4e4d0842012-03-11 07:00:24 +00002990 if (getLangOpts().CPlusPlus0x)
Richard Smith7fe62082011-10-15 05:09:34 +00002991 Diag(StartLoc, diag::warn_cxx98_compat_enum_fixed_underlying_type);
Douglas Gregora61b3e72010-12-01 17:42:47 +00002992 }
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002993 }
2994
Richard Smithbdad7a22012-01-10 01:33:14 +00002995 // There are four options here. If we have 'friend enum foo;' then this is a
2996 // friend declaration, and cannot have an accompanying definition. If we have
2997 // 'enum foo;', then this is a forward declaration. If we have
2998 // 'enum foo {...' then this is a definition. Otherwise we have something
2999 // like 'enum foo xyz', a reference.
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00003000 //
3001 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
3002 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
3003 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
3004 //
John McCallf312b1e2010-08-26 23:41:50 +00003005 Sema::TagUseKind TUK;
John McCall13489672012-05-07 06:16:58 +00003006 if (!AllowDeclaration) {
Richard Smith7796eb52012-03-12 08:56:40 +00003007 TUK = Sema::TUK_Reference;
John McCall13489672012-05-07 06:16:58 +00003008 } else if (Tok.is(tok::l_brace)) {
3009 if (DS.isFriendSpecified()) {
3010 Diag(Tok.getLocation(), diag::err_friend_decl_defines_type)
3011 << SourceRange(DS.getFriendSpecLoc());
3012 ConsumeBrace();
3013 SkipUntil(tok::r_brace);
3014 TUK = Sema::TUK_Friend;
3015 } else {
3016 TUK = Sema::TUK_Definition;
3017 }
3018 } else if (Tok.is(tok::semi) && DSC != DSC_type_specifier) {
3019 TUK = (DS.isFriendSpecified() ? Sema::TUK_Friend : Sema::TUK_Declaration);
3020 } else {
John McCallf312b1e2010-08-26 23:41:50 +00003021 TUK = Sema::TUK_Reference;
John McCall13489672012-05-07 06:16:58 +00003022 }
3023
3024 // If this is an elaborated type specifier, and we delayed
3025 // diagnostics before, just merge them into the current pool.
3026 if (TUK == Sema::TUK_Reference && shouldDelayDiagsInTag) {
3027 diagsFromTag.redelay();
3028 }
Richard Smith1af83c42012-03-23 03:33:32 +00003029
3030 MultiTemplateParamsArg TParams;
Douglas Gregor8fc6d232010-05-03 17:48:54 +00003031 if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate &&
John McCallf312b1e2010-08-26 23:41:50 +00003032 TUK != Sema::TUK_Reference) {
Richard Smith1af83c42012-03-23 03:33:32 +00003033 if (!getLangOpts().CPlusPlus0x || !SS.isSet()) {
3034 // Skip the rest of this declarator, up until the comma or semicolon.
3035 Diag(Tok, diag::err_enum_template);
3036 SkipUntil(tok::comma, true);
3037 return;
3038 }
3039
3040 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) {
3041 // Enumerations can't be explicitly instantiated.
3042 DS.SetTypeSpecError();
3043 Diag(StartLoc, diag::err_explicit_instantiation_enum);
3044 return;
3045 }
3046
3047 assert(TemplateInfo.TemplateParams && "no template parameters");
3048 TParams = MultiTemplateParamsArg(TemplateInfo.TemplateParams->data(),
3049 TemplateInfo.TemplateParams->size());
Douglas Gregor8fc6d232010-05-03 17:48:54 +00003050 }
Richard Smith1af83c42012-03-23 03:33:32 +00003051
Douglas Gregorb9075602011-02-22 02:55:24 +00003052 if (!Name && TUK != Sema::TUK_Definition) {
3053 Diag(Tok, diag::err_enumerator_unnamed_no_def);
Richard Smith1af83c42012-03-23 03:33:32 +00003054
Douglas Gregorb9075602011-02-22 02:55:24 +00003055 // Skip the rest of this declarator, up until the comma or semicolon.
3056 SkipUntil(tok::comma, true);
3057 return;
3058 }
Richard Smith1af83c42012-03-23 03:33:32 +00003059
Douglas Gregor402abb52009-05-28 23:31:59 +00003060 bool Owned = false;
John McCallc4e70192009-09-11 04:59:25 +00003061 bool IsDependent = false;
Douglas Gregor48c89f42010-04-24 16:38:41 +00003062 const char *PrevSpec = 0;
3063 unsigned DiagID;
John McCalld226f652010-08-21 09:40:31 +00003064 Decl *TagDecl = Actions.ActOnTag(getCurScope(), DeclSpec::TST_enum, TUK,
John McCall7f040a92010-12-24 02:08:15 +00003065 StartLoc, SS, Name, NameLoc, attrs.getList(),
Richard Smith1af83c42012-03-23 03:33:32 +00003066 AS, DS.getModulePrivateSpecLoc(), TParams,
Richard Smithbdad7a22012-01-10 01:33:14 +00003067 Owned, IsDependent, ScopedEnumKWLoc,
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00003068 IsScopedUsingClassTag, BaseType);
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003069
Douglas Gregor48c89f42010-04-24 16:38:41 +00003070 if (IsDependent) {
3071 // This enum has a dependent nested-name-specifier. Handle it as a
3072 // dependent tag.
3073 if (!Name) {
3074 DS.SetTypeSpecError();
3075 Diag(Tok, diag::err_expected_type_name_after_typename);
3076 return;
3077 }
3078
Douglas Gregor23c94db2010-07-02 17:43:08 +00003079 TypeResult Type = Actions.ActOnDependentTag(getCurScope(), DeclSpec::TST_enum,
Douglas Gregor48c89f42010-04-24 16:38:41 +00003080 TUK, SS, Name, StartLoc,
3081 NameLoc);
3082 if (Type.isInvalid()) {
3083 DS.SetTypeSpecError();
3084 return;
3085 }
3086
Abramo Bagnara0daaf322011-03-16 20:16:18 +00003087 if (DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc,
3088 NameLoc.isValid() ? NameLoc : StartLoc,
3089 PrevSpec, DiagID, Type.get()))
Douglas Gregor48c89f42010-04-24 16:38:41 +00003090 Diag(StartLoc, DiagID) << PrevSpec;
3091
3092 return;
3093 }
Mike Stump1eb44332009-09-09 15:08:12 +00003094
John McCalld226f652010-08-21 09:40:31 +00003095 if (!TagDecl) {
Douglas Gregor48c89f42010-04-24 16:38:41 +00003096 // The action failed to produce an enumeration tag. If this is a
3097 // definition, consume the entire definition.
Richard Smith7796eb52012-03-12 08:56:40 +00003098 if (Tok.is(tok::l_brace) && TUK != Sema::TUK_Reference) {
Douglas Gregor48c89f42010-04-24 16:38:41 +00003099 ConsumeBrace();
3100 SkipUntil(tok::r_brace);
3101 }
3102
3103 DS.SetTypeSpecError();
3104 return;
3105 }
Richard Smithbdad7a22012-01-10 01:33:14 +00003106
Richard Smith7796eb52012-03-12 08:56:40 +00003107 if (Tok.is(tok::l_brace) && TUK != Sema::TUK_Reference) {
John McCall13489672012-05-07 06:16:58 +00003108 ParseEnumBody(StartLoc, TagDecl);
Richard Smithbdad7a22012-01-10 01:33:14 +00003109 }
Mike Stump1eb44332009-09-09 15:08:12 +00003110
Abramo Bagnara0daaf322011-03-16 20:16:18 +00003111 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc,
3112 NameLoc.isValid() ? NameLoc : StartLoc,
3113 PrevSpec, DiagID, TagDecl, Owned))
John McCallfec54012009-08-03 20:12:06 +00003114 Diag(StartLoc, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00003115}
3116
3117/// ParseEnumBody - Parse a {} enclosed enumerator-list.
3118/// enumerator-list:
3119/// enumerator
3120/// enumerator-list ',' enumerator
3121/// enumerator:
3122/// enumeration-constant
3123/// enumeration-constant '=' constant-expression
3124/// enumeration-constant:
3125/// identifier
3126///
John McCalld226f652010-08-21 09:40:31 +00003127void Parser::ParseEnumBody(SourceLocation StartLoc, Decl *EnumDecl) {
Douglas Gregor074149e2009-01-05 19:45:36 +00003128 // Enter the scope of the enum body and start the definition.
3129 ParseScope EnumScope(this, Scope::DeclScope);
Douglas Gregor23c94db2010-07-02 17:43:08 +00003130 Actions.ActOnTagStartDefinition(getCurScope(), EnumDecl);
Douglas Gregor074149e2009-01-05 19:45:36 +00003131
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003132 BalancedDelimiterTracker T(*this, tok::l_brace);
3133 T.consumeOpen();
Mike Stump1eb44332009-09-09 15:08:12 +00003134
Chris Lattner7946dd32007-08-27 17:24:30 +00003135 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
David Blaikie4e4d0842012-03-11 07:00:24 +00003136 if (Tok.is(tok::r_brace) && !getLangOpts().CPlusPlus)
Fariborz Jahanian05115522010-05-28 22:23:22 +00003137 Diag(Tok, diag::error_empty_enum);
Mike Stump1eb44332009-09-09 15:08:12 +00003138
Chris Lattner5f9e2722011-07-23 10:55:15 +00003139 SmallVector<Decl *, 32> EnumConstantDecls;
Reid Spencer5f016e22007-07-11 17:01:13 +00003140
John McCalld226f652010-08-21 09:40:31 +00003141 Decl *LastEnumConstDecl = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00003142
Reid Spencer5f016e22007-07-11 17:01:13 +00003143 // Parse the enumerator-list.
Chris Lattner04d66662007-10-09 17:33:22 +00003144 while (Tok.is(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003145 IdentifierInfo *Ident = Tok.getIdentifierInfo();
3146 SourceLocation IdentLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00003147
John McCall5b629aa2010-10-22 23:36:17 +00003148 // If attributes exist after the enumerator, parse them.
John McCall0b7e6782011-03-24 11:26:52 +00003149 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00003150 MaybeParseGNUAttributes(attrs);
John McCall5b629aa2010-10-22 23:36:17 +00003151
Reid Spencer5f016e22007-07-11 17:01:13 +00003152 SourceLocation EqualLoc;
John McCall60d7b3a2010-08-24 06:29:42 +00003153 ExprResult AssignedVal;
John McCall92576642012-05-07 06:16:41 +00003154 ParsingDeclRAIIObject PD(*this, ParsingDeclRAIIObject::NoParent);
Fariborz Jahanian5a477db2011-12-09 01:15:54 +00003155
Chris Lattner04d66662007-10-09 17:33:22 +00003156 if (Tok.is(tok::equal)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003157 EqualLoc = ConsumeToken();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00003158 AssignedVal = ParseConstantExpression();
3159 if (AssignedVal.isInvalid())
Reid Spencer5f016e22007-07-11 17:01:13 +00003160 SkipUntil(tok::comma, tok::r_brace, true, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00003161 }
Mike Stump1eb44332009-09-09 15:08:12 +00003162
Reid Spencer5f016e22007-07-11 17:01:13 +00003163 // Install the enumerator constant into EnumDecl.
John McCalld226f652010-08-21 09:40:31 +00003164 Decl *EnumConstDecl = Actions.ActOnEnumConstant(getCurScope(), EnumDecl,
3165 LastEnumConstDecl,
3166 IdentLoc, Ident,
John McCall7f040a92010-12-24 02:08:15 +00003167 attrs.getList(), EqualLoc,
John McCalld226f652010-08-21 09:40:31 +00003168 AssignedVal.release());
Fariborz Jahanian5a477db2011-12-09 01:15:54 +00003169 PD.complete(EnumConstDecl);
3170
Reid Spencer5f016e22007-07-11 17:01:13 +00003171 EnumConstantDecls.push_back(EnumConstDecl);
3172 LastEnumConstDecl = EnumConstDecl;
Mike Stump1eb44332009-09-09 15:08:12 +00003173
Douglas Gregor751f6922010-09-07 14:51:08 +00003174 if (Tok.is(tok::identifier)) {
3175 // We're missing a comma between enumerators.
3176 SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation);
3177 Diag(Loc, diag::err_enumerator_list_missing_comma)
3178 << FixItHint::CreateInsertion(Loc, ", ");
3179 continue;
3180 }
3181
Chris Lattner04d66662007-10-09 17:33:22 +00003182 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +00003183 break;
3184 SourceLocation CommaLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00003185
Richard Smith7fe62082011-10-15 05:09:34 +00003186 if (Tok.isNot(tok::identifier)) {
David Blaikie4e4d0842012-03-11 07:00:24 +00003187 if (!getLangOpts().C99 && !getLangOpts().CPlusPlus0x)
Richard Smith7fe62082011-10-15 05:09:34 +00003188 Diag(CommaLoc, diag::ext_enumerator_list_comma)
David Blaikie4e4d0842012-03-11 07:00:24 +00003189 << getLangOpts().CPlusPlus
Richard Smith7fe62082011-10-15 05:09:34 +00003190 << FixItHint::CreateRemoval(CommaLoc);
David Blaikie4e4d0842012-03-11 07:00:24 +00003191 else if (getLangOpts().CPlusPlus0x)
Richard Smith7fe62082011-10-15 05:09:34 +00003192 Diag(CommaLoc, diag::warn_cxx98_compat_enumerator_list_comma)
3193 << FixItHint::CreateRemoval(CommaLoc);
3194 }
Reid Spencer5f016e22007-07-11 17:01:13 +00003195 }
Mike Stump1eb44332009-09-09 15:08:12 +00003196
Reid Spencer5f016e22007-07-11 17:01:13 +00003197 // Eat the }.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003198 T.consumeClose();
Reid Spencer5f016e22007-07-11 17:01:13 +00003199
Reid Spencer5f016e22007-07-11 17:01:13 +00003200 // If attributes exist after the identifier list, parse them.
John McCall0b7e6782011-03-24 11:26:52 +00003201 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00003202 MaybeParseGNUAttributes(attrs);
Douglas Gregor72de6672009-01-08 20:45:30 +00003203
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003204 Actions.ActOnEnumBody(StartLoc, T.getOpenLocation(), T.getCloseLocation(),
3205 EnumDecl, EnumConstantDecls.data(),
3206 EnumConstantDecls.size(), getCurScope(),
3207 attrs.getList());
Mike Stump1eb44332009-09-09 15:08:12 +00003208
Douglas Gregor72de6672009-01-08 20:45:30 +00003209 EnumScope.Exit();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003210 Actions.ActOnTagFinishDefinition(getCurScope(), EnumDecl,
3211 T.getCloseLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00003212}
3213
3214/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff5f8aa692008-02-11 23:15:56 +00003215/// start of a type-qualifier-list.
3216bool Parser::isTypeQualifier() const {
3217 switch (Tok.getKind()) {
3218 default: return false;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003219
3220 // type-qualifier only in OpenCL
3221 case tok::kw_private:
David Blaikie4e4d0842012-03-11 07:00:24 +00003222 return getLangOpts().OpenCL;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003223
Steve Naroff5f8aa692008-02-11 23:15:56 +00003224 // type-qualifier
3225 case tok::kw_const:
3226 case tok::kw_volatile:
3227 case tok::kw_restrict:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003228 case tok::kw___private:
3229 case tok::kw___local:
3230 case tok::kw___global:
3231 case tok::kw___constant:
3232 case tok::kw___read_only:
3233 case tok::kw___read_write:
3234 case tok::kw___write_only:
Steve Naroff5f8aa692008-02-11 23:15:56 +00003235 return true;
3236 }
3237}
3238
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003239/// isKnownToBeTypeSpecifier - Return true if we know that the specified token
3240/// is definitely a type-specifier. Return false if it isn't part of a type
3241/// specifier or if we're not sure.
3242bool Parser::isKnownToBeTypeSpecifier(const Token &Tok) const {
3243 switch (Tok.getKind()) {
3244 default: return false;
3245 // type-specifiers
3246 case tok::kw_short:
3247 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00003248 case tok::kw___int64:
Richard Smith5a5a9712012-04-04 06:24:32 +00003249 case tok::kw___int128:
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003250 case tok::kw_signed:
3251 case tok::kw_unsigned:
3252 case tok::kw__Complex:
3253 case tok::kw__Imaginary:
3254 case tok::kw_void:
3255 case tok::kw_char:
3256 case tok::kw_wchar_t:
3257 case tok::kw_char16_t:
3258 case tok::kw_char32_t:
3259 case tok::kw_int:
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00003260 case tok::kw_half:
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003261 case tok::kw_float:
3262 case tok::kw_double:
3263 case tok::kw_bool:
3264 case tok::kw__Bool:
3265 case tok::kw__Decimal32:
3266 case tok::kw__Decimal64:
3267 case tok::kw__Decimal128:
3268 case tok::kw___vector:
3269
3270 // struct-or-union-specifier (C99) or class-specifier (C++)
3271 case tok::kw_class:
3272 case tok::kw_struct:
3273 case tok::kw_union:
3274 // enum-specifier
3275 case tok::kw_enum:
3276
3277 // typedef-name
3278 case tok::annot_typename:
3279 return true;
3280 }
3281}
3282
Steve Naroff5f8aa692008-02-11 23:15:56 +00003283/// isTypeSpecifierQualifier - Return true if the current token could be the
Reid Spencer5f016e22007-07-11 17:01:13 +00003284/// start of a specifier-qualifier-list.
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003285bool Parser::isTypeSpecifierQualifier() {
Reid Spencer5f016e22007-07-11 17:01:13 +00003286 switch (Tok.getKind()) {
3287 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00003288
Chris Lattner166a8fc2009-01-04 23:41:41 +00003289 case tok::identifier: // foo::bar
John Thompson82287d12010-02-05 00:12:22 +00003290 if (TryAltiVecVectorToken())
3291 return true;
3292 // Fall through.
Douglas Gregord57959a2009-03-27 23:10:48 +00003293 case tok::kw_typename: // typename T::type
Chris Lattner166a8fc2009-01-04 23:41:41 +00003294 // Annotate typenames and C++ scope specifiers. If we get one, just
3295 // recurse to handle whatever we get.
3296 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003297 return true;
3298 if (Tok.is(tok::identifier))
3299 return false;
3300 return isTypeSpecifierQualifier();
Douglas Gregord57959a2009-03-27 23:10:48 +00003301
Chris Lattner166a8fc2009-01-04 23:41:41 +00003302 case tok::coloncolon: // ::foo::bar
3303 if (NextToken().is(tok::kw_new) || // ::new
3304 NextToken().is(tok::kw_delete)) // ::delete
3305 return false;
3306
Chris Lattner166a8fc2009-01-04 23:41:41 +00003307 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003308 return true;
3309 return isTypeSpecifierQualifier();
Mike Stump1eb44332009-09-09 15:08:12 +00003310
Reid Spencer5f016e22007-07-11 17:01:13 +00003311 // GNU attributes support.
3312 case tok::kw___attribute:
Steve Naroffd1861fd2007-07-31 12:34:36 +00003313 // GNU typeof support.
3314 case tok::kw_typeof:
Mike Stump1eb44332009-09-09 15:08:12 +00003315
Reid Spencer5f016e22007-07-11 17:01:13 +00003316 // type-specifiers
3317 case tok::kw_short:
3318 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00003319 case tok::kw___int64:
Richard Smith5a5a9712012-04-04 06:24:32 +00003320 case tok::kw___int128:
Reid Spencer5f016e22007-07-11 17:01:13 +00003321 case tok::kw_signed:
3322 case tok::kw_unsigned:
3323 case tok::kw__Complex:
3324 case tok::kw__Imaginary:
3325 case tok::kw_void:
3326 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00003327 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00003328 case tok::kw_char16_t:
3329 case tok::kw_char32_t:
Reid Spencer5f016e22007-07-11 17:01:13 +00003330 case tok::kw_int:
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00003331 case tok::kw_half:
Reid Spencer5f016e22007-07-11 17:01:13 +00003332 case tok::kw_float:
3333 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00003334 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00003335 case tok::kw__Bool:
3336 case tok::kw__Decimal32:
3337 case tok::kw__Decimal64:
3338 case tok::kw__Decimal128:
John Thompson82287d12010-02-05 00:12:22 +00003339 case tok::kw___vector:
Mike Stump1eb44332009-09-09 15:08:12 +00003340
Chris Lattner99dc9142008-04-13 18:59:07 +00003341 // struct-or-union-specifier (C99) or class-specifier (C++)
3342 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00003343 case tok::kw_struct:
3344 case tok::kw_union:
3345 // enum-specifier
3346 case tok::kw_enum:
Mike Stump1eb44332009-09-09 15:08:12 +00003347
Reid Spencer5f016e22007-07-11 17:01:13 +00003348 // type-qualifier
3349 case tok::kw_const:
3350 case tok::kw_volatile:
3351 case tok::kw_restrict:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003352
3353 // typedef-name
Chris Lattnerb31757b2009-01-06 05:06:21 +00003354 case tok::annot_typename:
Reid Spencer5f016e22007-07-11 17:01:13 +00003355 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00003356
Chris Lattner7c186be2008-10-20 00:25:30 +00003357 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
3358 case tok::less:
David Blaikie4e4d0842012-03-11 07:00:24 +00003359 return getLangOpts().ObjC1;
Mike Stump1eb44332009-09-09 15:08:12 +00003360
Steve Naroff239f0732008-12-25 14:16:32 +00003361 case tok::kw___cdecl:
3362 case tok::kw___stdcall:
3363 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00003364 case tok::kw___thiscall:
Eli Friedman290eeb02009-06-08 23:27:34 +00003365 case tok::kw___w64:
3366 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00003367 case tok::kw___ptr32:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003368 case tok::kw___pascal:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00003369 case tok::kw___unaligned:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003370
3371 case tok::kw___private:
3372 case tok::kw___local:
3373 case tok::kw___global:
3374 case tok::kw___constant:
3375 case tok::kw___read_only:
3376 case tok::kw___read_write:
3377 case tok::kw___write_only:
3378
Eli Friedman290eeb02009-06-08 23:27:34 +00003379 return true;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003380
3381 case tok::kw_private:
David Blaikie4e4d0842012-03-11 07:00:24 +00003382 return getLangOpts().OpenCL;
Eli Friedmanb001de72011-10-06 23:00:33 +00003383
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00003384 // C11 _Atomic()
Eli Friedmanb001de72011-10-06 23:00:33 +00003385 case tok::kw__Atomic:
3386 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00003387 }
3388}
3389
3390/// isDeclarationSpecifier() - Return true if the current token is part of a
3391/// declaration specifier.
Douglas Gregor9497a732010-09-16 01:51:54 +00003392///
3393/// \param DisambiguatingWithExpression True to indicate that the purpose of
3394/// this check is to disambiguate between an expression and a declaration.
3395bool Parser::isDeclarationSpecifier(bool DisambiguatingWithExpression) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003396 switch (Tok.getKind()) {
3397 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00003398
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003399 case tok::kw_private:
David Blaikie4e4d0842012-03-11 07:00:24 +00003400 return getLangOpts().OpenCL;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003401
Chris Lattner166a8fc2009-01-04 23:41:41 +00003402 case tok::identifier: // foo::bar
Steve Naroff61f72cb2009-03-09 21:12:44 +00003403 // Unfortunate hack to support "Class.factoryMethod" notation.
David Blaikie4e4d0842012-03-11 07:00:24 +00003404 if (getLangOpts().ObjC1 && NextToken().is(tok::period))
Steve Naroff61f72cb2009-03-09 21:12:44 +00003405 return false;
John Thompson82287d12010-02-05 00:12:22 +00003406 if (TryAltiVecVectorToken())
3407 return true;
3408 // Fall through.
David Blaikie42d6d0c2011-12-04 05:04:18 +00003409 case tok::kw_decltype: // decltype(T())::type
Douglas Gregord57959a2009-03-27 23:10:48 +00003410 case tok::kw_typename: // typename T::type
Chris Lattner166a8fc2009-01-04 23:41:41 +00003411 // Annotate typenames and C++ scope specifiers. If we get one, just
3412 // recurse to handle whatever we get.
3413 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003414 return true;
3415 if (Tok.is(tok::identifier))
3416 return false;
Douglas Gregor9497a732010-09-16 01:51:54 +00003417
3418 // If we're in Objective-C and we have an Objective-C class type followed
3419 // by an identifier and then either ':' or ']', in a place where an
3420 // expression is permitted, then this is probably a class message send
3421 // missing the initial '['. In this case, we won't consider this to be
3422 // the start of a declaration.
3423 if (DisambiguatingWithExpression &&
3424 isStartOfObjCClassMessageMissingOpenBracket())
3425 return false;
3426
John McCall9ba61662010-02-26 08:45:28 +00003427 return isDeclarationSpecifier();
3428
Chris Lattner166a8fc2009-01-04 23:41:41 +00003429 case tok::coloncolon: // ::foo::bar
3430 if (NextToken().is(tok::kw_new) || // ::new
3431 NextToken().is(tok::kw_delete)) // ::delete
3432 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00003433
Chris Lattner166a8fc2009-01-04 23:41:41 +00003434 // Annotate typenames and C++ scope specifiers. If we get one, just
3435 // recurse to handle whatever we get.
3436 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003437 return true;
3438 return isDeclarationSpecifier();
Mike Stump1eb44332009-09-09 15:08:12 +00003439
Reid Spencer5f016e22007-07-11 17:01:13 +00003440 // storage-class-specifier
3441 case tok::kw_typedef:
3442 case tok::kw_extern:
Steve Naroff8d54bf22007-12-18 00:16:02 +00003443 case tok::kw___private_extern__:
Reid Spencer5f016e22007-07-11 17:01:13 +00003444 case tok::kw_static:
3445 case tok::kw_auto:
3446 case tok::kw_register:
3447 case tok::kw___thread:
Mike Stump1eb44332009-09-09 15:08:12 +00003448
Douglas Gregor8d267c52011-09-09 02:06:17 +00003449 // Modules
3450 case tok::kw___module_private__:
3451
Reid Spencer5f016e22007-07-11 17:01:13 +00003452 // type-specifiers
3453 case tok::kw_short:
3454 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00003455 case tok::kw___int64:
Richard Smith5a5a9712012-04-04 06:24:32 +00003456 case tok::kw___int128:
Reid Spencer5f016e22007-07-11 17:01:13 +00003457 case tok::kw_signed:
3458 case tok::kw_unsigned:
3459 case tok::kw__Complex:
3460 case tok::kw__Imaginary:
3461 case tok::kw_void:
3462 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00003463 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00003464 case tok::kw_char16_t:
3465 case tok::kw_char32_t:
3466
Reid Spencer5f016e22007-07-11 17:01:13 +00003467 case tok::kw_int:
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00003468 case tok::kw_half:
Reid Spencer5f016e22007-07-11 17:01:13 +00003469 case tok::kw_float:
3470 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00003471 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00003472 case tok::kw__Bool:
3473 case tok::kw__Decimal32:
3474 case tok::kw__Decimal64:
3475 case tok::kw__Decimal128:
John Thompson82287d12010-02-05 00:12:22 +00003476 case tok::kw___vector:
Mike Stump1eb44332009-09-09 15:08:12 +00003477
Chris Lattner99dc9142008-04-13 18:59:07 +00003478 // struct-or-union-specifier (C99) or class-specifier (C++)
3479 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00003480 case tok::kw_struct:
3481 case tok::kw_union:
3482 // enum-specifier
3483 case tok::kw_enum:
Mike Stump1eb44332009-09-09 15:08:12 +00003484
Reid Spencer5f016e22007-07-11 17:01:13 +00003485 // type-qualifier
3486 case tok::kw_const:
3487 case tok::kw_volatile:
3488 case tok::kw_restrict:
Steve Naroffd1861fd2007-07-31 12:34:36 +00003489
Reid Spencer5f016e22007-07-11 17:01:13 +00003490 // function-specifier
3491 case tok::kw_inline:
Douglas Gregorb48fe382008-10-31 09:07:45 +00003492 case tok::kw_virtual:
3493 case tok::kw_explicit:
Chris Lattnerd6c7c182007-08-09 16:40:21 +00003494
Peter Collingbournec6eb44b2011-04-15 00:35:57 +00003495 // static_assert-declaration
3496 case tok::kw__Static_assert:
3497
Chris Lattner1ef08762007-08-09 17:01:07 +00003498 // GNU typeof support.
3499 case tok::kw_typeof:
Mike Stump1eb44332009-09-09 15:08:12 +00003500
Chris Lattner1ef08762007-08-09 17:01:07 +00003501 // GNU attributes.
Chris Lattnerd6c7c182007-08-09 16:40:21 +00003502 case tok::kw___attribute:
Reid Spencer5f016e22007-07-11 17:01:13 +00003503 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00003504
Francois Pichete3d49b42011-06-19 08:02:06 +00003505 // C++0x decltype.
David Blaikie42d6d0c2011-12-04 05:04:18 +00003506 case tok::annot_decltype:
Francois Pichete3d49b42011-06-19 08:02:06 +00003507 return true;
3508
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00003509 // C11 _Atomic()
Eli Friedmanb001de72011-10-06 23:00:33 +00003510 case tok::kw__Atomic:
3511 return true;
3512
Chris Lattnerf3948c42008-07-26 03:38:44 +00003513 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
3514 case tok::less:
David Blaikie4e4d0842012-03-11 07:00:24 +00003515 return getLangOpts().ObjC1;
Mike Stump1eb44332009-09-09 15:08:12 +00003516
Douglas Gregord9d75e52011-04-27 05:41:15 +00003517 // typedef-name
3518 case tok::annot_typename:
3519 return !DisambiguatingWithExpression ||
3520 !isStartOfObjCClassMessageMissingOpenBracket();
3521
Steve Naroff47f52092009-01-06 19:34:12 +00003522 case tok::kw___declspec:
Steve Naroff239f0732008-12-25 14:16:32 +00003523 case tok::kw___cdecl:
3524 case tok::kw___stdcall:
3525 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00003526 case tok::kw___thiscall:
Eli Friedman290eeb02009-06-08 23:27:34 +00003527 case tok::kw___w64:
3528 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00003529 case tok::kw___ptr32:
Eli Friedman290eeb02009-06-08 23:27:34 +00003530 case tok::kw___forceinline:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003531 case tok::kw___pascal:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00003532 case tok::kw___unaligned:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003533
3534 case tok::kw___private:
3535 case tok::kw___local:
3536 case tok::kw___global:
3537 case tok::kw___constant:
3538 case tok::kw___read_only:
3539 case tok::kw___read_write:
3540 case tok::kw___write_only:
3541
Eli Friedman290eeb02009-06-08 23:27:34 +00003542 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00003543 }
3544}
3545
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003546bool Parser::isConstructorDeclarator() {
3547 TentativeParsingAction TPA(*this);
3548
3549 // Parse the C++ scope specifier.
3550 CXXScopeSpec SS;
Douglas Gregorefaa93a2011-11-07 17:33:42 +00003551 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
3552 /*EnteringContext=*/true)) {
John McCall9ba61662010-02-26 08:45:28 +00003553 TPA.Revert();
3554 return false;
3555 }
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003556
3557 // Parse the constructor name.
3558 if (Tok.is(tok::identifier) || Tok.is(tok::annot_template_id)) {
3559 // We already know that we have a constructor name; just consume
3560 // the token.
3561 ConsumeToken();
3562 } else {
3563 TPA.Revert();
3564 return false;
3565 }
3566
Richard Smith22592862012-03-27 23:05:05 +00003567 // Current class name must be followed by a left parenthesis.
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003568 if (Tok.isNot(tok::l_paren)) {
3569 TPA.Revert();
3570 return false;
3571 }
3572 ConsumeParen();
3573
Richard Smith22592862012-03-27 23:05:05 +00003574 // A right parenthesis, or ellipsis followed by a right parenthesis signals
3575 // that we have a constructor.
3576 if (Tok.is(tok::r_paren) ||
3577 (Tok.is(tok::ellipsis) && NextToken().is(tok::r_paren))) {
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003578 TPA.Revert();
3579 return true;
3580 }
3581
3582 // If we need to, enter the specified scope.
3583 DeclaratorScopeObj DeclScopeObj(*this, SS);
Douglas Gregor23c94db2010-07-02 17:43:08 +00003584 if (SS.isSet() && Actions.ShouldEnterDeclaratorScope(getCurScope(), SS))
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003585 DeclScopeObj.EnterDeclaratorScope();
3586
Francois Pichetdfaa5fb2011-01-31 04:54:32 +00003587 // Optionally skip Microsoft attributes.
John McCall0b7e6782011-03-24 11:26:52 +00003588 ParsedAttributes Attrs(AttrFactory);
Francois Pichetdfaa5fb2011-01-31 04:54:32 +00003589 MaybeParseMicrosoftAttributes(Attrs);
3590
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003591 // Check whether the next token(s) are part of a declaration
3592 // specifier, in which case we have the start of a parameter and,
3593 // therefore, we know that this is a constructor.
Richard Smith412e0cc2012-03-27 00:56:56 +00003594 bool IsConstructor = false;
3595 if (isDeclarationSpecifier())
3596 IsConstructor = true;
3597 else if (Tok.is(tok::identifier) ||
3598 (Tok.is(tok::annot_cxxscope) && NextToken().is(tok::identifier))) {
3599 // We've seen "C ( X" or "C ( X::Y", but "X" / "X::Y" is not a type.
3600 // This might be a parenthesized member name, but is more likely to
3601 // be a constructor declaration with an invalid argument type. Keep
3602 // looking.
3603 if (Tok.is(tok::annot_cxxscope))
3604 ConsumeToken();
3605 ConsumeToken();
3606
3607 // If this is not a constructor, we must be parsing a declarator,
Richard Smith5d8388c2012-03-27 01:42:32 +00003608 // which must have one of the following syntactic forms (see the
3609 // grammar extract at the start of ParseDirectDeclarator):
Richard Smith412e0cc2012-03-27 00:56:56 +00003610 switch (Tok.getKind()) {
3611 case tok::l_paren:
3612 // C(X ( int));
3613 case tok::l_square:
3614 // C(X [ 5]);
3615 // C(X [ [attribute]]);
3616 case tok::coloncolon:
3617 // C(X :: Y);
3618 // C(X :: *p);
3619 case tok::r_paren:
3620 // C(X )
3621 // Assume this isn't a constructor, rather than assuming it's a
3622 // constructor with an unnamed parameter of an ill-formed type.
3623 break;
3624
3625 default:
3626 IsConstructor = true;
3627 break;
3628 }
3629 }
3630
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003631 TPA.Revert();
3632 return IsConstructor;
3633}
Reid Spencer5f016e22007-07-11 17:01:13 +00003634
3635/// ParseTypeQualifierListOpt
Dawn Perchik52fc3142010-09-03 01:29:35 +00003636/// type-qualifier-list: [C99 6.7.5]
3637/// type-qualifier
3638/// [vendor] attributes
3639/// [ only if VendorAttributesAllowed=true ]
3640/// type-qualifier-list type-qualifier
3641/// [vendor] type-qualifier-list attributes
3642/// [ only if VendorAttributesAllowed=true ]
3643/// [C++0x] attribute-specifier[opt] is allowed before cv-qualifier-seq
3644/// [ only if CXX0XAttributesAllowed=true ]
3645/// Note: vendor can be GNU, MS, etc.
Reid Spencer5f016e22007-07-11 17:01:13 +00003646///
Dawn Perchik52fc3142010-09-03 01:29:35 +00003647void Parser::ParseTypeQualifierListOpt(DeclSpec &DS,
3648 bool VendorAttributesAllowed,
Richard Smithc56298d2012-04-10 03:25:07 +00003649 bool CXX11AttributesAllowed) {
3650 if (getLangOpts().CPlusPlus0x && CXX11AttributesAllowed &&
Richard Smith6ee326a2012-04-10 01:32:12 +00003651 isCXX11AttributeSpecifier()) {
John McCall0b7e6782011-03-24 11:26:52 +00003652 ParsedAttributesWithRange attrs(AttrFactory);
Richard Smithc56298d2012-04-10 03:25:07 +00003653 ParseCXX11Attributes(attrs);
Richard Smith6ee326a2012-04-10 01:32:12 +00003654 DS.takeAttributesFrom(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00003655 }
Abramo Bagnara796aa442011-03-12 11:17:06 +00003656
3657 SourceLocation EndLoc;
3658
Reid Spencer5f016e22007-07-11 17:01:13 +00003659 while (1) {
John McCallfec54012009-08-03 20:12:06 +00003660 bool isInvalid = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00003661 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00003662 unsigned DiagID = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00003663 SourceLocation Loc = Tok.getLocation();
3664
3665 switch (Tok.getKind()) {
Douglas Gregor1a480c42010-08-27 17:35:51 +00003666 case tok::code_completion:
3667 Actions.CodeCompleteTypeQualifiers(DS);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00003668 return cutOffParsing();
Douglas Gregor1a480c42010-08-27 17:35:51 +00003669
Reid Spencer5f016e22007-07-11 17:01:13 +00003670 case tok::kw_const:
John McCallfec54012009-08-03 20:12:06 +00003671 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, DiagID,
David Blaikie4e4d0842012-03-11 07:00:24 +00003672 getLangOpts());
Reid Spencer5f016e22007-07-11 17:01:13 +00003673 break;
3674 case tok::kw_volatile:
John McCallfec54012009-08-03 20:12:06 +00003675 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
David Blaikie4e4d0842012-03-11 07:00:24 +00003676 getLangOpts());
Reid Spencer5f016e22007-07-11 17:01:13 +00003677 break;
3678 case tok::kw_restrict:
John McCallfec54012009-08-03 20:12:06 +00003679 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
David Blaikie4e4d0842012-03-11 07:00:24 +00003680 getLangOpts());
Reid Spencer5f016e22007-07-11 17:01:13 +00003681 break;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003682
3683 // OpenCL qualifiers:
3684 case tok::kw_private:
David Blaikie4e4d0842012-03-11 07:00:24 +00003685 if (!getLangOpts().OpenCL)
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003686 goto DoneWithTypeQuals;
3687 case tok::kw___private:
3688 case tok::kw___global:
3689 case tok::kw___local:
3690 case tok::kw___constant:
3691 case tok::kw___read_only:
3692 case tok::kw___write_only:
3693 case tok::kw___read_write:
3694 ParseOpenCLQualifiers(DS);
3695 break;
3696
Eli Friedman290eeb02009-06-08 23:27:34 +00003697 case tok::kw___w64:
Steve Naroff86bc6cf2008-12-25 14:41:26 +00003698 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00003699 case tok::kw___ptr32:
Steve Naroff239f0732008-12-25 14:16:32 +00003700 case tok::kw___cdecl:
3701 case tok::kw___stdcall:
3702 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00003703 case tok::kw___thiscall:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00003704 case tok::kw___unaligned:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003705 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00003706 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman290eeb02009-06-08 23:27:34 +00003707 continue;
3708 }
3709 goto DoneWithTypeQuals;
Dawn Perchik52fc3142010-09-03 01:29:35 +00003710 case tok::kw___pascal:
3711 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00003712 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik52fc3142010-09-03 01:29:35 +00003713 continue;
3714 }
3715 goto DoneWithTypeQuals;
Reid Spencer5f016e22007-07-11 17:01:13 +00003716 case tok::kw___attribute:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003717 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00003718 ParseGNUAttributes(DS.getAttributes());
Chris Lattner5a69d1c2008-12-18 07:02:59 +00003719 continue; // do *not* consume the next token!
3720 }
3721 // otherwise, FALL THROUGH!
3722 default:
Steve Naroff239f0732008-12-25 14:16:32 +00003723 DoneWithTypeQuals:
Chris Lattner5a69d1c2008-12-18 07:02:59 +00003724 // If this is not a type-qualifier token, we're done reading type
3725 // qualifiers. First verify that DeclSpec's are consistent.
Douglas Gregor9b3064b2009-04-01 22:41:11 +00003726 DS.Finish(Diags, PP);
Abramo Bagnara796aa442011-03-12 11:17:06 +00003727 if (EndLoc.isValid())
3728 DS.SetRangeEnd(EndLoc);
Chris Lattner5a69d1c2008-12-18 07:02:59 +00003729 return;
Reid Spencer5f016e22007-07-11 17:01:13 +00003730 }
Chris Lattnera1fcbad2008-12-18 06:50:14 +00003731
Reid Spencer5f016e22007-07-11 17:01:13 +00003732 // If the specifier combination wasn't legal, issue a diagnostic.
3733 if (isInvalid) {
3734 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner1ab3b962008-11-18 07:48:38 +00003735 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00003736 }
Abramo Bagnara796aa442011-03-12 11:17:06 +00003737 EndLoc = ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00003738 }
3739}
3740
3741
3742/// ParseDeclarator - Parse and verify a newly-initialized declarator.
3743///
3744void Parser::ParseDeclarator(Declarator &D) {
3745 /// This implements the 'declarator' production in the C grammar, then checks
3746 /// for well-formedness and issues diagnostics.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003747 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Reid Spencer5f016e22007-07-11 17:01:13 +00003748}
3749
Richard Smith9988f282012-03-29 01:16:42 +00003750static bool isPtrOperatorToken(tok::TokenKind Kind, const LangOptions &Lang) {
3751 if (Kind == tok::star || Kind == tok::caret)
3752 return true;
3753
3754 // We parse rvalue refs in C++03, because otherwise the errors are scary.
3755 if (!Lang.CPlusPlus)
3756 return false;
3757
3758 return Kind == tok::amp || Kind == tok::ampamp;
3759}
3760
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003761/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
3762/// is parsed by the function passed to it. Pass null, and the direct-declarator
3763/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003764/// ptr-operator production.
3765///
Richard Smith0706df42011-10-19 21:33:05 +00003766/// If the grammar of this construct is extended, matching changes must also be
Richard Smith5d8388c2012-03-27 01:42:32 +00003767/// made to TryParseDeclarator and MightBeDeclarator, and possibly to
3768/// isConstructorDeclarator.
Richard Smith0706df42011-10-19 21:33:05 +00003769///
Sebastian Redlf30208a2009-01-24 21:16:55 +00003770/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
3771/// [C] pointer[opt] direct-declarator
3772/// [C++] direct-declarator
3773/// [C++] ptr-operator declarator
Reid Spencer5f016e22007-07-11 17:01:13 +00003774///
3775/// pointer: [C99 6.7.5]
3776/// '*' type-qualifier-list[opt]
3777/// '*' type-qualifier-list[opt] pointer
3778///
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003779/// ptr-operator:
3780/// '*' cv-qualifier-seq[opt]
3781/// '&'
Sebastian Redl05532f22009-03-15 22:02:01 +00003782/// [C++0x] '&&'
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003783/// [GNU] '&' restrict[opt] attributes[opt]
Sebastian Redl05532f22009-03-15 22:02:01 +00003784/// [GNU?] '&&' restrict[opt] attributes[opt]
Sebastian Redlf30208a2009-01-24 21:16:55 +00003785/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003786void Parser::ParseDeclaratorInternal(Declarator &D,
3787 DirectDeclParseFunction DirectDeclParser) {
Douglas Gregor91a28862009-08-26 14:27:30 +00003788 if (Diags.hasAllExtensionsSilenced())
3789 D.setExtension();
Douglas Gregor2ccccb32010-08-23 18:23:48 +00003790
Sebastian Redlf30208a2009-01-24 21:16:55 +00003791 // C++ member pointers start with a '::' or a nested-name.
3792 // Member pointers get special handling, since there's no place for the
3793 // scope spec in the generic path below.
David Blaikie4e4d0842012-03-11 07:00:24 +00003794 if (getLangOpts().CPlusPlus &&
Chris Lattnerf919bfe2009-03-24 17:04:48 +00003795 (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) ||
3796 Tok.is(tok::annot_cxxscope))) {
Douglas Gregorefaa93a2011-11-07 17:33:42 +00003797 bool EnteringContext = D.getContext() == Declarator::FileContext ||
3798 D.getContext() == Declarator::MemberContext;
Sebastian Redlf30208a2009-01-24 21:16:55 +00003799 CXXScopeSpec SS;
Douglas Gregorefaa93a2011-11-07 17:33:42 +00003800 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), EnteringContext);
John McCall9ba61662010-02-26 08:45:28 +00003801
Jeffrey Yasskinedc28772010-04-07 23:29:58 +00003802 if (SS.isNotEmpty()) {
Mike Stump1eb44332009-09-09 15:08:12 +00003803 if (Tok.isNot(tok::star)) {
Sebastian Redlf30208a2009-01-24 21:16:55 +00003804 // The scope spec really belongs to the direct-declarator.
3805 D.getCXXScopeSpec() = SS;
3806 if (DirectDeclParser)
3807 (this->*DirectDeclParser)(D);
3808 return;
3809 }
3810
3811 SourceLocation Loc = ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00003812 D.SetRangeEnd(Loc);
John McCall0b7e6782011-03-24 11:26:52 +00003813 DeclSpec DS(AttrFactory);
Sebastian Redlf30208a2009-01-24 21:16:55 +00003814 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00003815 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00003816
3817 // Recurse to parse whatever is left.
3818 ParseDeclaratorInternal(D, DirectDeclParser);
3819
3820 // Sema will have to catch (syntactically invalid) pointers into global
3821 // scope. It has to catch pointers into namespace scope anyway.
3822 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
John McCall0b7e6782011-03-24 11:26:52 +00003823 Loc),
3824 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003825 /* Don't replace range end. */SourceLocation());
Sebastian Redlf30208a2009-01-24 21:16:55 +00003826 return;
3827 }
3828 }
3829
3830 tok::TokenKind Kind = Tok.getKind();
Steve Naroff5618bd42008-08-27 16:04:49 +00003831 // Not a pointer, C++ reference, or block.
Richard Smith9988f282012-03-29 01:16:42 +00003832 if (!isPtrOperatorToken(Kind, getLangOpts())) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003833 if (DirectDeclParser)
3834 (this->*DirectDeclParser)(D);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003835 return;
3836 }
Sebastian Redlf30208a2009-01-24 21:16:55 +00003837
Sebastian Redl05532f22009-03-15 22:02:01 +00003838 // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference,
3839 // '&&' -> rvalue reference
Sebastian Redl743de1f2009-03-23 00:00:23 +00003840 SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&.
Sebastian Redlab197ba2009-02-09 18:23:29 +00003841 D.SetRangeEnd(Loc);
Reid Spencer5f016e22007-07-11 17:01:13 +00003842
Chris Lattner9af55002009-03-27 04:18:06 +00003843 if (Kind == tok::star || Kind == tok::caret) {
Chris Lattner76549142008-02-21 01:32:26 +00003844 // Is a pointer.
John McCall0b7e6782011-03-24 11:26:52 +00003845 DeclSpec DS(AttrFactory);
Sebastian Redlf30208a2009-01-24 21:16:55 +00003846
Richard Smith6ee326a2012-04-10 01:32:12 +00003847 // FIXME: GNU attributes are not allowed here in a new-type-id.
Reid Spencer5f016e22007-07-11 17:01:13 +00003848 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00003849 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00003850
Reid Spencer5f016e22007-07-11 17:01:13 +00003851 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003852 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroff5618bd42008-08-27 16:04:49 +00003853 if (Kind == tok::star)
3854 // Remember that we parsed a pointer type, and remember the type-quals.
3855 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
Chandler Carruthd067c072011-02-23 18:51:59 +00003856 DS.getConstSpecLoc(),
3857 DS.getVolatileSpecLoc(),
John McCall0b7e6782011-03-24 11:26:52 +00003858 DS.getRestrictSpecLoc()),
3859 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003860 SourceLocation());
Steve Naroff5618bd42008-08-27 16:04:49 +00003861 else
3862 // Remember that we parsed a Block type, and remember the type-quals.
Mike Stump1eb44332009-09-09 15:08:12 +00003863 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
John McCall0b7e6782011-03-24 11:26:52 +00003864 Loc),
3865 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003866 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00003867 } else {
3868 // Is a reference
John McCall0b7e6782011-03-24 11:26:52 +00003869 DeclSpec DS(AttrFactory);
Reid Spencer5f016e22007-07-11 17:01:13 +00003870
Sebastian Redl743de1f2009-03-23 00:00:23 +00003871 // Complain about rvalue references in C++03, but then go on and build
3872 // the declarator.
Richard Smith7fe62082011-10-15 05:09:34 +00003873 if (Kind == tok::ampamp)
David Blaikie4e4d0842012-03-11 07:00:24 +00003874 Diag(Loc, getLangOpts().CPlusPlus0x ?
Richard Smith7fe62082011-10-15 05:09:34 +00003875 diag::warn_cxx98_compat_rvalue_reference :
3876 diag::ext_rvalue_reference);
Sebastian Redl743de1f2009-03-23 00:00:23 +00003877
Richard Smith6ee326a2012-04-10 01:32:12 +00003878 // GNU-style and C++11 attributes are allowed here, as is restrict.
3879 ParseTypeQualifierListOpt(DS);
3880 D.ExtendWithDeclSpec(DS);
3881
Reid Spencer5f016e22007-07-11 17:01:13 +00003882 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
3883 // cv-qualifiers are introduced through the use of a typedef or of a
3884 // template type argument, in which case the cv-qualifiers are ignored.
Reid Spencer5f016e22007-07-11 17:01:13 +00003885 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
3886 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
3887 Diag(DS.getConstSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00003888 diag::err_invalid_reference_qualifier_application) << "const";
Reid Spencer5f016e22007-07-11 17:01:13 +00003889 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
3890 Diag(DS.getVolatileSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00003891 diag::err_invalid_reference_qualifier_application) << "volatile";
Reid Spencer5f016e22007-07-11 17:01:13 +00003892 }
3893
3894 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003895 ParseDeclaratorInternal(D, DirectDeclParser);
Reid Spencer5f016e22007-07-11 17:01:13 +00003896
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00003897 if (D.getNumTypeObjects() > 0) {
3898 // C++ [dcl.ref]p4: There shall be no references to references.
3899 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
3900 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattnerda83bac2008-11-19 07:37:42 +00003901 if (const IdentifierInfo *II = D.getIdentifier())
3902 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
3903 << II;
3904 else
3905 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
3906 << "type name";
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00003907
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003908 // Once we've complained about the reference-to-reference, we
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00003909 // can go ahead and build the (technically ill-formed)
3910 // declarator: reference collapsing will take care of it.
3911 }
3912 }
3913
Reid Spencer5f016e22007-07-11 17:01:13 +00003914 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner76549142008-02-21 01:32:26 +00003915 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
Sebastian Redl05532f22009-03-15 22:02:01 +00003916 Kind == tok::amp),
John McCall0b7e6782011-03-24 11:26:52 +00003917 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003918 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00003919 }
3920}
3921
Richard Smith9988f282012-03-29 01:16:42 +00003922static void diagnoseMisplacedEllipsis(Parser &P, Declarator &D,
3923 SourceLocation EllipsisLoc) {
3924 if (EllipsisLoc.isValid()) {
3925 FixItHint Insertion;
3926 if (!D.getEllipsisLoc().isValid()) {
3927 Insertion = FixItHint::CreateInsertion(D.getIdentifierLoc(), "...");
3928 D.setEllipsisLoc(EllipsisLoc);
3929 }
3930 P.Diag(EllipsisLoc, diag::err_misplaced_ellipsis_in_declaration)
3931 << FixItHint::CreateRemoval(EllipsisLoc) << Insertion << !D.hasName();
3932 }
3933}
3934
Reid Spencer5f016e22007-07-11 17:01:13 +00003935/// ParseDirectDeclarator
3936/// direct-declarator: [C99 6.7.5]
Douglas Gregor42a552f2008-11-05 20:51:48 +00003937/// [C99] identifier
Reid Spencer5f016e22007-07-11 17:01:13 +00003938/// '(' declarator ')'
3939/// [GNU] '(' attributes declarator ')'
3940/// [C90] direct-declarator '[' constant-expression[opt] ']'
3941/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
3942/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
3943/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
3944/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Richard Smith6ee326a2012-04-10 01:32:12 +00003945/// [C++11] direct-declarator '[' constant-expression[opt] ']'
3946/// attribute-specifier-seq[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00003947/// direct-declarator '(' parameter-type-list ')'
3948/// direct-declarator '(' identifier-list[opt] ')'
3949/// [GNU] direct-declarator '(' parameter-forward-declarations
3950/// parameter-type-list[opt] ')'
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003951/// [C++] direct-declarator '(' parameter-declaration-clause ')'
3952/// cv-qualifier-seq[opt] exception-specification[opt]
Richard Smith6ee326a2012-04-10 01:32:12 +00003953/// [C++11] direct-declarator '(' parameter-declaration-clause ')'
3954/// attribute-specifier-seq[opt] cv-qualifier-seq[opt]
3955/// ref-qualifier[opt] exception-specification[opt]
Douglas Gregorb48fe382008-10-31 09:07:45 +00003956/// [C++] declarator-id
Richard Smith6ee326a2012-04-10 01:32:12 +00003957/// [C++11] declarator-id attribute-specifier-seq[opt]
Douglas Gregor42a552f2008-11-05 20:51:48 +00003958///
3959/// declarator-id: [C++ 8]
Douglas Gregora8bc8c92010-12-23 22:44:42 +00003960/// '...'[opt] id-expression
Douglas Gregor42a552f2008-11-05 20:51:48 +00003961/// '::'[opt] nested-name-specifier[opt] type-name
3962///
3963/// id-expression: [C++ 5.1]
3964/// unqualified-id
Douglas Gregordb422df2009-09-25 21:45:23 +00003965/// qualified-id
Douglas Gregor42a552f2008-11-05 20:51:48 +00003966///
3967/// unqualified-id: [C++ 5.1]
Mike Stump1eb44332009-09-09 15:08:12 +00003968/// identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003969/// operator-function-id
Douglas Gregordb422df2009-09-25 21:45:23 +00003970/// conversion-function-id
Mike Stump1eb44332009-09-09 15:08:12 +00003971/// '~' class-name
Douglas Gregor39a8de12009-02-25 19:37:18 +00003972/// template-id
Argyrios Kyrtzidisc7ed9c62008-11-07 22:02:30 +00003973///
Richard Smith5d8388c2012-03-27 01:42:32 +00003974/// Note, any additional constructs added here may need corresponding changes
3975/// in isConstructorDeclarator.
Reid Spencer5f016e22007-07-11 17:01:13 +00003976void Parser::ParseDirectDeclarator(Declarator &D) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00003977 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003978
David Blaikie4e4d0842012-03-11 07:00:24 +00003979 if (getLangOpts().CPlusPlus && D.mayHaveIdentifier()) {
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003980 // ParseDeclaratorInternal might already have parsed the scope.
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00003981 if (D.getCXXScopeSpec().isEmpty()) {
Douglas Gregorefaa93a2011-11-07 17:33:42 +00003982 bool EnteringContext = D.getContext() == Declarator::FileContext ||
3983 D.getContext() == Declarator::MemberContext;
3984 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec(), ParsedType(),
3985 EnteringContext);
John McCall9ba61662010-02-26 08:45:28 +00003986 }
3987
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00003988 if (D.getCXXScopeSpec().isValid()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00003989 if (Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
John McCalle7e278b2009-12-11 20:04:54 +00003990 // Change the declaration context for name lookup, until this function
3991 // is exited (and the declarator has been parsed).
3992 DeclScopeObj.EnterDeclaratorScope();
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00003993 }
3994
Douglas Gregora8bc8c92010-12-23 22:44:42 +00003995 // C++0x [dcl.fct]p14:
3996 // There is a syntactic ambiguity when an ellipsis occurs at the end
3997 // of a parameter-declaration-clause without a preceding comma. In
3998 // this case, the ellipsis is parsed as part of the
3999 // abstract-declarator if the type of the parameter names a template
4000 // parameter pack that has not been expanded; otherwise, it is parsed
4001 // as part of the parameter-declaration-clause.
Richard Smith9988f282012-03-29 01:16:42 +00004002 if (Tok.is(tok::ellipsis) && D.getCXXScopeSpec().isEmpty() &&
Douglas Gregora8bc8c92010-12-23 22:44:42 +00004003 !((D.getContext() == Declarator::PrototypeContext ||
4004 D.getContext() == Declarator::BlockLiteralContext) &&
Douglas Gregora8bc8c92010-12-23 22:44:42 +00004005 NextToken().is(tok::r_paren) &&
Richard Smith9988f282012-03-29 01:16:42 +00004006 !Actions.containsUnexpandedParameterPacks(D))) {
4007 SourceLocation EllipsisLoc = ConsumeToken();
4008 if (isPtrOperatorToken(Tok.getKind(), getLangOpts())) {
4009 // The ellipsis was put in the wrong place. Recover, and explain to
4010 // the user what they should have done.
4011 ParseDeclarator(D);
4012 diagnoseMisplacedEllipsis(*this, D, EllipsisLoc);
4013 return;
4014 } else
4015 D.setEllipsisLoc(EllipsisLoc);
4016
4017 // The ellipsis can't be followed by a parenthesized declarator. We
4018 // check for that in ParseParenDeclarator, after we have disambiguated
4019 // the l_paren token.
4020 }
4021
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004022 if (Tok.is(tok::identifier) || Tok.is(tok::kw_operator) ||
4023 Tok.is(tok::annot_template_id) || Tok.is(tok::tilde)) {
4024 // We found something that indicates the start of an unqualified-id.
4025 // Parse that unqualified-id.
John McCallba9d8532010-04-13 06:39:49 +00004026 bool AllowConstructorName;
4027 if (D.getDeclSpec().hasTypeSpecifier())
4028 AllowConstructorName = false;
4029 else if (D.getCXXScopeSpec().isSet())
4030 AllowConstructorName =
4031 (D.getContext() == Declarator::FileContext ||
4032 (D.getContext() == Declarator::MemberContext &&
4033 D.getDeclSpec().isFriendSpecified()));
4034 else
4035 AllowConstructorName = (D.getContext() == Declarator::MemberContext);
4036
Abramo Bagnarae4b92762012-01-27 09:46:47 +00004037 SourceLocation TemplateKWLoc;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004038 if (ParseUnqualifiedId(D.getCXXScopeSpec(),
4039 /*EnteringContext=*/true,
4040 /*AllowDestructorName=*/true,
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004041 AllowConstructorName,
John McCallb3d87482010-08-24 05:47:05 +00004042 ParsedType(),
Abramo Bagnarae4b92762012-01-27 09:46:47 +00004043 TemplateKWLoc,
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00004044 D.getName()) ||
4045 // Once we're past the identifier, if the scope was bad, mark the
4046 // whole declarator bad.
4047 D.getCXXScopeSpec().isInvalid()) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00004048 D.SetIdentifier(0, Tok.getLocation());
4049 D.setInvalidType(true);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004050 } else {
4051 // Parsed the unqualified-id; update range information and move along.
4052 if (D.getSourceRange().getBegin().isInvalid())
4053 D.SetRangeBegin(D.getName().getSourceRange().getBegin());
4054 D.SetRangeEnd(D.getName().getSourceRange().getEnd());
Douglas Gregor2f1bc522008-11-07 20:08:42 +00004055 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004056 goto PastIdentifier;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00004057 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004058 } else if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
David Blaikie4e4d0842012-03-11 07:00:24 +00004059 assert(!getLangOpts().CPlusPlus &&
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00004060 "There's a C++-specific check for tok::identifier above");
4061 assert(Tok.getIdentifierInfo() && "Not an identifier?");
4062 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
4063 ConsumeToken();
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004064 goto PastIdentifier;
4065 }
Richard Smith9988f282012-03-29 01:16:42 +00004066
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004067 if (Tok.is(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00004068 // direct-declarator: '(' declarator ')'
4069 // direct-declarator: '(' attributes declarator ')'
4070 // Example: 'char (*X)' or 'int (*XX)(void)'
4071 ParseParenDeclarator(D);
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004072
4073 // If the declarator was parenthesized, we entered the declarator
4074 // scope when parsing the parenthesized declarator, then exited
4075 // the scope already. Re-enter the scope, if we need to.
4076 if (D.getCXXScopeSpec().isSet()) {
Fariborz Jahanian46877cd2010-08-17 23:50:37 +00004077 // If there was an error parsing parenthesized declarator, declarator
Richard Smith9988f282012-03-29 01:16:42 +00004078 // scope may have been entered before. Don't do it again.
Fariborz Jahanian46877cd2010-08-17 23:50:37 +00004079 if (!D.isInvalidType() &&
4080 Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004081 // Change the declaration context for name lookup, until this function
4082 // is exited (and the declarator has been parsed).
Fariborz Jahanian46877cd2010-08-17 23:50:37 +00004083 DeclScopeObj.EnterDeclaratorScope();
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004084 }
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00004085 } else if (D.mayOmitIdentifier()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00004086 // This could be something simple like "int" (in which case the declarator
4087 // portion is empty), if an abstract-declarator is allowed.
4088 D.SetIdentifier(0, Tok.getLocation());
4089 } else {
Douglas Gregore950d4b2009-03-06 23:28:18 +00004090 if (D.getContext() == Declarator::MemberContext)
4091 Diag(Tok, diag::err_expected_member_name_or_semi)
4092 << D.getDeclSpec().getSourceRange();
David Blaikie4e4d0842012-03-11 07:00:24 +00004093 else if (getLangOpts().CPlusPlus)
4094 Diag(Tok, diag::err_expected_unqualified_id) << getLangOpts().CPlusPlus;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00004095 else
Chris Lattner1ab3b962008-11-18 07:48:38 +00004096 Diag(Tok, diag::err_expected_ident_lparen);
Reid Spencer5f016e22007-07-11 17:01:13 +00004097 D.SetIdentifier(0, Tok.getLocation());
Chris Lattner1f6f54b2008-11-11 06:13:16 +00004098 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00004099 }
Mike Stump1eb44332009-09-09 15:08:12 +00004100
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00004101 PastIdentifier:
Reid Spencer5f016e22007-07-11 17:01:13 +00004102 assert(D.isPastIdentifier() &&
4103 "Haven't past the location of the identifier yet?");
Mike Stump1eb44332009-09-09 15:08:12 +00004104
Richard Smith6ee326a2012-04-10 01:32:12 +00004105 // Don't parse attributes unless we have parsed an unparenthesized name.
4106 if (D.hasName() && !D.getNumTypeObjects())
John McCall7f040a92010-12-24 02:08:15 +00004107 MaybeParseCXX0XAttributes(D);
Sean Huntbbd37c62009-11-21 08:43:09 +00004108
Reid Spencer5f016e22007-07-11 17:01:13 +00004109 while (1) {
Chris Lattner04d66662007-10-09 17:33:22 +00004110 if (Tok.is(tok::l_paren)) {
David Blaikie42d6d0c2011-12-04 05:04:18 +00004111 // Enter function-declaration scope, limiting any declarators to the
4112 // function prototype scope, including parameter declarators.
4113 ParseScope PrototypeScope(this,
4114 Scope::FunctionPrototypeScope|Scope::DeclScope);
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00004115 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
4116 // In such a case, check if we actually have a function declarator; if it
4117 // is not, the declarator has been fully parsed.
David Blaikie4e4d0842012-03-11 07:00:24 +00004118 if (getLangOpts().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
Chris Lattner7399ee02008-10-20 02:05:46 +00004119 // When not in file scope, warn for ambiguous function declarators, just
4120 // in case the author intended it as a variable definition.
4121 bool warnIfAmbiguous = D.getContext() != Declarator::FileContext;
4122 if (!isCXXFunctionDeclarator(warnIfAmbiguous))
4123 break;
4124 }
John McCall0b7e6782011-03-24 11:26:52 +00004125 ParsedAttributes attrs(AttrFactory);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004126 BalancedDelimiterTracker T(*this, tok::l_paren);
4127 T.consumeOpen();
4128 ParseFunctionDeclarator(D, attrs, T);
David Blaikie42d6d0c2011-12-04 05:04:18 +00004129 PrototypeScope.Exit();
Chris Lattner04d66662007-10-09 17:33:22 +00004130 } else if (Tok.is(tok::l_square)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00004131 ParseBracketDeclarator(D);
4132 } else {
4133 break;
4134 }
4135 }
David Blaikie42d6d0c2011-12-04 05:04:18 +00004136}
Reid Spencer5f016e22007-07-11 17:01:13 +00004137
Chris Lattneref4715c2008-04-06 05:45:57 +00004138/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
4139/// only called before the identifier, so these are most likely just grouping
Mike Stump1eb44332009-09-09 15:08:12 +00004140/// parens for precedence. If we find that these are actually function
Chris Lattneref4715c2008-04-06 05:45:57 +00004141/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
4142///
4143/// direct-declarator:
4144/// '(' declarator ')'
4145/// [GNU] '(' attributes declarator ')'
Chris Lattner7399ee02008-10-20 02:05:46 +00004146/// direct-declarator '(' parameter-type-list ')'
4147/// direct-declarator '(' identifier-list[opt] ')'
4148/// [GNU] direct-declarator '(' parameter-forward-declarations
4149/// parameter-type-list[opt] ')'
Chris Lattneref4715c2008-04-06 05:45:57 +00004150///
4151void Parser::ParseParenDeclarator(Declarator &D) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004152 BalancedDelimiterTracker T(*this, tok::l_paren);
4153 T.consumeOpen();
4154
Chris Lattneref4715c2008-04-06 05:45:57 +00004155 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
Mike Stump1eb44332009-09-09 15:08:12 +00004156
Chris Lattner7399ee02008-10-20 02:05:46 +00004157 // Eat any attributes before we look at whether this is a grouping or function
4158 // declarator paren. If this is a grouping paren, the attribute applies to
4159 // the type being built up, for example:
4160 // int (__attribute__(()) *x)(long y)
4161 // If this ends up not being a grouping paren, the attribute applies to the
4162 // first argument, for example:
4163 // int (__attribute__(()) int x)
4164 // In either case, we need to eat any attributes to be able to determine what
4165 // sort of paren this is.
4166 //
John McCall0b7e6782011-03-24 11:26:52 +00004167 ParsedAttributes attrs(AttrFactory);
Chris Lattner7399ee02008-10-20 02:05:46 +00004168 bool RequiresArg = false;
4169 if (Tok.is(tok::kw___attribute)) {
John McCall7f040a92010-12-24 02:08:15 +00004170 ParseGNUAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00004171
Chris Lattner7399ee02008-10-20 02:05:46 +00004172 // We require that the argument list (if this is a non-grouping paren) be
4173 // present even if the attribute list was empty.
4174 RequiresArg = true;
4175 }
Steve Naroff239f0732008-12-25 14:16:32 +00004176 // Eat any Microsoft extensions.
Eli Friedman290eeb02009-06-08 23:27:34 +00004177 if (Tok.is(tok::kw___cdecl) || Tok.is(tok::kw___stdcall) ||
Douglas Gregorf813a2c2010-05-18 16:57:00 +00004178 Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___fastcall) ||
Francois Pichet3bd9aa42011-08-18 09:59:55 +00004179 Tok.is(tok::kw___w64) || Tok.is(tok::kw___ptr64) ||
Francois Pichet58fd97a2011-08-25 00:36:46 +00004180 Tok.is(tok::kw___ptr32) || Tok.is(tok::kw___unaligned)) {
John McCall7f040a92010-12-24 02:08:15 +00004181 ParseMicrosoftTypeAttributes(attrs);
Eli Friedman290eeb02009-06-08 23:27:34 +00004182 }
Dawn Perchik52fc3142010-09-03 01:29:35 +00004183 // Eat any Borland extensions.
Ted Kremenek8113ecf2010-11-10 05:59:39 +00004184 if (Tok.is(tok::kw___pascal))
John McCall7f040a92010-12-24 02:08:15 +00004185 ParseBorlandTypeAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00004186
Chris Lattneref4715c2008-04-06 05:45:57 +00004187 // If we haven't past the identifier yet (or where the identifier would be
4188 // stored, if this is an abstract declarator), then this is probably just
4189 // grouping parens. However, if this could be an abstract-declarator, then
4190 // this could also be the start of function arguments (consider 'void()').
4191 bool isGrouping;
Mike Stump1eb44332009-09-09 15:08:12 +00004192
Chris Lattneref4715c2008-04-06 05:45:57 +00004193 if (!D.mayOmitIdentifier()) {
4194 // If this can't be an abstract-declarator, this *must* be a grouping
4195 // paren, because we haven't seen the identifier yet.
4196 isGrouping = true;
4197 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Richard Smith22592862012-03-27 23:05:05 +00004198 (getLangOpts().CPlusPlus && Tok.is(tok::ellipsis) &&
4199 NextToken().is(tok::r_paren)) || // C++ int(...)
Richard Smith6ce48a72012-04-11 04:01:28 +00004200 isDeclarationSpecifier() || // 'int(int)' is a function.
4201 isCXX11AttributeSpecifier()) { // 'int([[]]int)' is a function.
Chris Lattneref4715c2008-04-06 05:45:57 +00004202 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
4203 // considered to be a type, not a K&R identifier-list.
4204 isGrouping = false;
4205 } else {
4206 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
4207 isGrouping = true;
4208 }
Mike Stump1eb44332009-09-09 15:08:12 +00004209
Chris Lattneref4715c2008-04-06 05:45:57 +00004210 // If this is a grouping paren, handle:
4211 // direct-declarator: '(' declarator ')'
4212 // direct-declarator: '(' attributes declarator ')'
4213 if (isGrouping) {
Richard Smith9988f282012-03-29 01:16:42 +00004214 SourceLocation EllipsisLoc = D.getEllipsisLoc();
4215 D.setEllipsisLoc(SourceLocation());
4216
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00004217 bool hadGroupingParens = D.hasGroupingParens();
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00004218 D.setGroupingParens(true);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00004219 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattneref4715c2008-04-06 05:45:57 +00004220 // Match the ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004221 T.consumeClose();
4222 D.AddTypeInfo(DeclaratorChunk::getParen(T.getOpenLocation(),
4223 T.getCloseLocation()),
4224 attrs, T.getCloseLocation());
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00004225
4226 D.setGroupingParens(hadGroupingParens);
Richard Smith9988f282012-03-29 01:16:42 +00004227
4228 // An ellipsis cannot be placed outside parentheses.
4229 if (EllipsisLoc.isValid())
4230 diagnoseMisplacedEllipsis(*this, D, EllipsisLoc);
4231
Chris Lattneref4715c2008-04-06 05:45:57 +00004232 return;
4233 }
Mike Stump1eb44332009-09-09 15:08:12 +00004234
Chris Lattneref4715c2008-04-06 05:45:57 +00004235 // Okay, if this wasn't a grouping paren, it must be the start of a function
4236 // argument list. Recognize that this declarator will never have an
Chris Lattner7399ee02008-10-20 02:05:46 +00004237 // identifier (and remember where it would have been), then call into
4238 // ParseFunctionDeclarator to handle of argument list.
Chris Lattneref4715c2008-04-06 05:45:57 +00004239 D.SetIdentifier(0, Tok.getLocation());
4240
David Blaikie42d6d0c2011-12-04 05:04:18 +00004241 // Enter function-declaration scope, limiting any declarators to the
4242 // function prototype scope, including parameter declarators.
4243 ParseScope PrototypeScope(this,
4244 Scope::FunctionPrototypeScope|Scope::DeclScope);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004245 ParseFunctionDeclarator(D, attrs, T, RequiresArg);
David Blaikie42d6d0c2011-12-04 05:04:18 +00004246 PrototypeScope.Exit();
Chris Lattneref4715c2008-04-06 05:45:57 +00004247}
4248
4249/// ParseFunctionDeclarator - We are after the identifier and have parsed the
4250/// declarator D up to a paren, which indicates that we are parsing function
4251/// arguments.
Reid Spencer5f016e22007-07-11 17:01:13 +00004252///
Richard Smith6ee326a2012-04-10 01:32:12 +00004253/// If FirstArgAttrs is non-null, then the caller parsed those arguments
4254/// immediately after the open paren - they should be considered to be the
4255/// first argument of a parameter.
Chris Lattner7399ee02008-10-20 02:05:46 +00004256///
Richard Smith6ee326a2012-04-10 01:32:12 +00004257/// If RequiresArg is true, then the first argument of the function is required
4258/// to be present and required to not be an identifier list.
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004259///
Richard Smith6ee326a2012-04-10 01:32:12 +00004260/// For C++, after the parameter-list, it also parses the cv-qualifier-seq[opt],
4261/// (C++11) ref-qualifier[opt], exception-specification[opt],
4262/// (C++11) attribute-specifier-seq[opt], and (C++11) trailing-return-type[opt].
4263///
4264/// [C++11] exception-specification:
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004265/// dynamic-exception-specification
4266/// noexcept-specification
4267///
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004268void Parser::ParseFunctionDeclarator(Declarator &D,
Richard Smith6ee326a2012-04-10 01:32:12 +00004269 ParsedAttributes &FirstArgAttrs,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004270 BalancedDelimiterTracker &Tracker,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004271 bool RequiresArg) {
David Blaikie42d6d0c2011-12-04 05:04:18 +00004272 assert(getCurScope()->isFunctionPrototypeScope() &&
4273 "Should call from a Function scope");
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004274 // lparen is already consumed!
4275 assert(D.isPastIdentifier() && "Should not call before identifier!");
4276
4277 // This should be true when the function has typed arguments.
4278 // Otherwise, it is treated as a K&R-style function.
4279 bool HasProto = false;
4280 // Build up an array of information about the parsed arguments.
Chris Lattner5f9e2722011-07-23 10:55:15 +00004281 SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004282 // Remember where we see an ellipsis, if any.
4283 SourceLocation EllipsisLoc;
4284
4285 DeclSpec DS(AttrFactory);
4286 bool RefQualifierIsLValueRef = true;
4287 SourceLocation RefQualifierLoc;
Douglas Gregor43f51032011-10-19 06:04:55 +00004288 SourceLocation ConstQualifierLoc;
4289 SourceLocation VolatileQualifierLoc;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004290 ExceptionSpecificationType ESpecType = EST_None;
4291 SourceRange ESpecRange;
Chris Lattner5f9e2722011-07-23 10:55:15 +00004292 SmallVector<ParsedType, 2> DynamicExceptions;
4293 SmallVector<SourceRange, 2> DynamicExceptionRanges;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004294 ExprResult NoexceptExpr;
Richard Smith6ee326a2012-04-10 01:32:12 +00004295 ParsedAttributes FnAttrs(AttrFactory);
Richard Smith54655be2012-06-12 01:51:59 +00004296 TypeResult TrailingReturnType;
Richard Smith6ee326a2012-04-10 01:32:12 +00004297
James Molloy16f1f712012-02-29 10:24:19 +00004298 Actions.ActOnStartFunctionDeclarator();
4299
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004300 SourceLocation EndLoc;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004301 if (isFunctionDeclaratorIdentifierList()) {
4302 if (RequiresArg)
4303 Diag(Tok, diag::err_argument_required_after_attribute);
4304
4305 ParseFunctionDeclaratorIdentifierList(D, ParamInfo);
4306
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004307 Tracker.consumeClose();
4308 EndLoc = Tracker.getCloseLocation();
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004309 } else {
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004310 if (Tok.isNot(tok::r_paren))
Richard Smith6ee326a2012-04-10 01:32:12 +00004311 ParseParameterDeclarationClause(D, FirstArgAttrs, ParamInfo, EllipsisLoc);
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004312 else if (RequiresArg)
4313 Diag(Tok, diag::err_argument_required_after_attribute);
4314
David Blaikie4e4d0842012-03-11 07:00:24 +00004315 HasProto = ParamInfo.size() || getLangOpts().CPlusPlus;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004316
4317 // If we have the closing ')', eat it.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004318 Tracker.consumeClose();
4319 EndLoc = Tracker.getCloseLocation();
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004320
David Blaikie4e4d0842012-03-11 07:00:24 +00004321 if (getLangOpts().CPlusPlus) {
Richard Smith6ee326a2012-04-10 01:32:12 +00004322 // FIXME: Accept these components in any order, and produce fixits to
4323 // correct the order if the user gets it wrong. Ideally we should deal
4324 // with the virt-specifier-seq and pure-specifier in the same way.
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004325
4326 // Parse cv-qualifier-seq[opt].
Richard Smith6ee326a2012-04-10 01:32:12 +00004327 ParseTypeQualifierListOpt(DS, false /*no attributes*/, false);
4328 if (!DS.getSourceRange().getEnd().isInvalid()) {
4329 EndLoc = DS.getSourceRange().getEnd();
4330 ConstQualifierLoc = DS.getConstSpecLoc();
4331 VolatileQualifierLoc = DS.getVolatileSpecLoc();
4332 }
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004333
4334 // Parse ref-qualifier[opt].
4335 if (Tok.is(tok::amp) || Tok.is(tok::ampamp)) {
David Blaikie4e4d0842012-03-11 07:00:24 +00004336 Diag(Tok, getLangOpts().CPlusPlus0x ?
Richard Smith7fe62082011-10-15 05:09:34 +00004337 diag::warn_cxx98_compat_ref_qualifier :
4338 diag::ext_ref_qualifier);
Richard Smith6ee326a2012-04-10 01:32:12 +00004339
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004340 RefQualifierIsLValueRef = Tok.is(tok::amp);
4341 RefQualifierLoc = ConsumeToken();
4342 EndLoc = RefQualifierLoc;
4343 }
4344
Douglas Gregorcefc3af2012-04-16 07:05:22 +00004345 // C++11 [expr.prim.general]p3:
4346 // If a declaration declares a member function or member function
4347 // template of a class X, the expression this is a prvalue of type
4348 // "pointer to cv-qualifier-seq X" between the optional cv-qualifer-seq
4349 // and the end of the function-definition, member-declarator, or
4350 // declarator.
4351 bool IsCXX11MemberFunction =
4352 getLangOpts().CPlusPlus0x &&
4353 (D.getContext() == Declarator::MemberContext ||
4354 (D.getContext() == Declarator::FileContext &&
4355 D.getCXXScopeSpec().isValid() &&
4356 Actions.CurContext->isRecord()));
4357 Sema::CXXThisScopeRAII ThisScope(Actions,
4358 dyn_cast<CXXRecordDecl>(Actions.CurContext),
4359 DS.getTypeQualifiers(),
4360 IsCXX11MemberFunction);
Richard Smitha058fd42012-05-02 22:22:32 +00004361
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004362 // Parse exception-specification[opt].
Richard Smitha058fd42012-05-02 22:22:32 +00004363 ESpecType = tryParseExceptionSpecification(ESpecRange,
Douglas Gregor74e2fc32012-04-16 18:27:27 +00004364 DynamicExceptions,
4365 DynamicExceptionRanges,
Richard Smitha058fd42012-05-02 22:22:32 +00004366 NoexceptExpr);
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004367 if (ESpecType != EST_None)
4368 EndLoc = ESpecRange.getEnd();
4369
Richard Smith6ee326a2012-04-10 01:32:12 +00004370 // Parse attribute-specifier-seq[opt]. Per DR 979 and DR 1297, this goes
4371 // after the exception-specification.
4372 MaybeParseCXX0XAttributes(FnAttrs);
4373
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004374 // Parse trailing-return-type[opt].
David Blaikie4e4d0842012-03-11 07:00:24 +00004375 if (getLangOpts().CPlusPlus0x && Tok.is(tok::arrow)) {
Richard Smith7fe62082011-10-15 05:09:34 +00004376 Diag(Tok, diag::warn_cxx98_compat_trailing_return_type);
Douglas Gregorae7902c2011-08-04 15:30:47 +00004377 SourceRange Range;
Richard Smith54655be2012-06-12 01:51:59 +00004378 TrailingReturnType = ParseTrailingReturnType(Range);
Douglas Gregorae7902c2011-08-04 15:30:47 +00004379 if (Range.getEnd().isValid())
4380 EndLoc = Range.getEnd();
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004381 }
4382 }
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004383 }
4384
4385 // Remember that we parsed a function type, and remember the attributes.
4386 D.AddTypeInfo(DeclaratorChunk::getFunction(HasProto,
4387 /*isVariadic=*/EllipsisLoc.isValid(),
4388 EllipsisLoc,
4389 ParamInfo.data(), ParamInfo.size(),
4390 DS.getTypeQualifiers(),
4391 RefQualifierIsLValueRef,
Douglas Gregor43f51032011-10-19 06:04:55 +00004392 RefQualifierLoc, ConstQualifierLoc,
4393 VolatileQualifierLoc,
Douglas Gregor90ebed02011-07-13 21:47:47 +00004394 /*MutableLoc=*/SourceLocation(),
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004395 ESpecType, ESpecRange.getBegin(),
4396 DynamicExceptions.data(),
4397 DynamicExceptionRanges.data(),
4398 DynamicExceptions.size(),
4399 NoexceptExpr.isUsable() ?
4400 NoexceptExpr.get() : 0,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004401 Tracker.getOpenLocation(),
4402 EndLoc, D,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004403 TrailingReturnType),
Richard Smith6ee326a2012-04-10 01:32:12 +00004404 FnAttrs, EndLoc);
James Molloy16f1f712012-02-29 10:24:19 +00004405
4406 Actions.ActOnEndFunctionDeclarator();
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004407}
4408
4409/// isFunctionDeclaratorIdentifierList - This parameter list may have an
4410/// identifier list form for a K&R-style function: void foo(a,b,c)
4411///
4412/// Note that identifier-lists are only allowed for normal declarators, not for
4413/// abstract-declarators.
4414bool Parser::isFunctionDeclaratorIdentifierList() {
David Blaikie4e4d0842012-03-11 07:00:24 +00004415 return !getLangOpts().CPlusPlus
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004416 && Tok.is(tok::identifier)
4417 && !TryAltiVecVectorToken()
4418 // K&R identifier lists can't have typedefs as identifiers, per C99
4419 // 6.7.5.3p11.
4420 && (TryAnnotateTypeOrScopeToken() || !Tok.is(tok::annot_typename))
4421 // Identifier lists follow a really simple grammar: the identifiers can
4422 // be followed *only* by a ", identifier" or ")". However, K&R
4423 // identifier lists are really rare in the brave new modern world, and
4424 // it is very common for someone to typo a type in a non-K&R style
4425 // list. If we are presented with something like: "void foo(intptr x,
4426 // float y)", we don't want to start parsing the function declarator as
4427 // though it is a K&R style declarator just because intptr is an
4428 // invalid type.
4429 //
4430 // To handle this, we check to see if the token after the first
4431 // identifier is a "," or ")". Only then do we parse it as an
4432 // identifier list.
4433 && (NextToken().is(tok::comma) || NextToken().is(tok::r_paren));
4434}
4435
4436/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
4437/// we found a K&R-style identifier list instead of a typed parameter list.
4438///
4439/// After returning, ParamInfo will hold the parsed parameters.
4440///
4441/// identifier-list: [C99 6.7.5]
4442/// identifier
4443/// identifier-list ',' identifier
4444///
4445void Parser::ParseFunctionDeclaratorIdentifierList(
4446 Declarator &D,
Chris Lattner5f9e2722011-07-23 10:55:15 +00004447 SmallVector<DeclaratorChunk::ParamInfo, 16> &ParamInfo) {
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004448 // If there was no identifier specified for the declarator, either we are in
4449 // an abstract-declarator, or we are in a parameter declarator which was found
4450 // to be abstract. In abstract-declarators, identifier lists are not valid:
4451 // diagnose this.
4452 if (!D.getIdentifier())
4453 Diag(Tok, diag::ext_ident_list_in_param);
4454
4455 // Maintain an efficient lookup of params we have seen so far.
4456 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
4457
4458 while (1) {
4459 // If this isn't an identifier, report the error and skip until ')'.
4460 if (Tok.isNot(tok::identifier)) {
4461 Diag(Tok, diag::err_expected_ident);
4462 SkipUntil(tok::r_paren, /*StopAtSemi=*/true, /*DontConsume=*/true);
4463 // Forget we parsed anything.
4464 ParamInfo.clear();
4465 return;
4466 }
4467
4468 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
4469
4470 // Reject 'typedef int y; int test(x, y)', but continue parsing.
4471 if (Actions.getTypeName(*ParmII, Tok.getLocation(), getCurScope()))
4472 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
4473
4474 // Verify that the argument identifier has not already been mentioned.
4475 if (!ParamsSoFar.insert(ParmII)) {
4476 Diag(Tok, diag::err_param_redefinition) << ParmII;
4477 } else {
4478 // Remember this identifier in ParamInfo.
4479 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
4480 Tok.getLocation(),
4481 0));
4482 }
4483
4484 // Eat the identifier.
4485 ConsumeToken();
4486
4487 // The list continues if we see a comma.
4488 if (Tok.isNot(tok::comma))
4489 break;
4490 ConsumeToken();
4491 }
4492}
4493
4494/// ParseParameterDeclarationClause - Parse a (possibly empty) parameter-list
4495/// after the opening parenthesis. This function will not parse a K&R-style
4496/// identifier list.
4497///
Richard Smith6ce48a72012-04-11 04:01:28 +00004498/// D is the declarator being parsed. If FirstArgAttrs is non-null, then the
4499/// caller parsed those arguments immediately after the open paren - they should
4500/// be considered to be part of the first parameter.
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004501///
4502/// After returning, ParamInfo will hold the parsed parameters. EllipsisLoc will
4503/// be the location of the ellipsis, if any was parsed.
4504///
Reid Spencer5f016e22007-07-11 17:01:13 +00004505/// parameter-type-list: [C99 6.7.5]
4506/// parameter-list
4507/// parameter-list ',' '...'
Douglas Gregored5d6512009-09-22 21:41:40 +00004508/// [C++] parameter-list '...'
Reid Spencer5f016e22007-07-11 17:01:13 +00004509///
4510/// parameter-list: [C99 6.7.5]
4511/// parameter-declaration
4512/// parameter-list ',' parameter-declaration
4513///
4514/// parameter-declaration: [C99 6.7.5]
4515/// declaration-specifiers declarator
Chris Lattner04421082008-04-08 04:40:51 +00004516/// [C++] declaration-specifiers declarator '=' assignment-expression
Sebastian Redl84407ba2012-03-14 15:54:00 +00004517/// [C++11] initializer-clause
Reid Spencer5f016e22007-07-11 17:01:13 +00004518/// [GNU] declaration-specifiers declarator attributes
Sebastian Redl50de12f2009-03-24 22:27:57 +00004519/// declaration-specifiers abstract-declarator[opt]
4520/// [C++] declaration-specifiers abstract-declarator[opt]
Chris Lattner8123a952008-04-10 02:22:51 +00004521/// '=' assignment-expression
Reid Spencer5f016e22007-07-11 17:01:13 +00004522/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
Richard Smith6ce48a72012-04-11 04:01:28 +00004523/// [C++11] attribute-specifier-seq parameter-declaration
Reid Spencer5f016e22007-07-11 17:01:13 +00004524///
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004525void Parser::ParseParameterDeclarationClause(
4526 Declarator &D,
Richard Smith6ce48a72012-04-11 04:01:28 +00004527 ParsedAttributes &FirstArgAttrs,
Chris Lattner5f9e2722011-07-23 10:55:15 +00004528 SmallVector<DeclaratorChunk::ParamInfo, 16> &ParamInfo,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004529 SourceLocation &EllipsisLoc) {
Mike Stump1eb44332009-09-09 15:08:12 +00004530
Chris Lattnerf97409f2008-04-06 06:57:35 +00004531 while (1) {
4532 if (Tok.is(tok::ellipsis)) {
Richard Smith6ce48a72012-04-11 04:01:28 +00004533 // FIXME: Issue a diagnostic if we parsed an attribute-specifier-seq
4534 // before deciding this was a parameter-declaration-clause.
Douglas Gregor965acbb2009-02-18 07:07:28 +00004535 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chris Lattnerf97409f2008-04-06 06:57:35 +00004536 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00004537 }
Mike Stump1eb44332009-09-09 15:08:12 +00004538
Chris Lattnerf97409f2008-04-06 06:57:35 +00004539 // Parse the declaration-specifiers.
John McCall54abf7d2009-11-04 02:18:39 +00004540 // Just use the ParsingDeclaration "scope" of the declarator.
John McCall0b7e6782011-03-24 11:26:52 +00004541 DeclSpec DS(AttrFactory);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004542
Richard Smith6ce48a72012-04-11 04:01:28 +00004543 // Parse any C++11 attributes.
4544 MaybeParseCXX0XAttributes(DS.getAttributes());
4545
John McCall7f040a92010-12-24 02:08:15 +00004546 // Skip any Microsoft attributes before a param.
David Blaikie4e4d0842012-03-11 07:00:24 +00004547 if (getLangOpts().MicrosoftExt && Tok.is(tok::l_square))
John McCall7f040a92010-12-24 02:08:15 +00004548 ParseMicrosoftAttributes(DS.getAttributes());
4549
4550 SourceLocation DSStart = Tok.getLocation();
Chris Lattner7399ee02008-10-20 02:05:46 +00004551
4552 // If the caller parsed attributes for the first argument, add them now.
John McCall7f040a92010-12-24 02:08:15 +00004553 // Take them so that we only apply the attributes to the first parameter.
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004554 // FIXME: If we can leave the attributes in the token stream somehow, we can
Richard Smith6ce48a72012-04-11 04:01:28 +00004555 // get rid of a parameter (FirstArgAttrs) and this statement. It might be
4556 // too much hassle.
4557 DS.takeAttributesFrom(FirstArgAttrs);
John McCall7f040a92010-12-24 02:08:15 +00004558
Chris Lattnere64c5492009-02-27 18:38:20 +00004559 ParseDeclarationSpecifiers(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00004560
Chris Lattnerf97409f2008-04-06 06:57:35 +00004561 // Parse the declarator. This is "PrototypeContext", because we must
4562 // accept either 'declarator' or 'abstract-declarator' here.
4563 Declarator ParmDecl(DS, Declarator::PrototypeContext);
4564 ParseDeclarator(ParmDecl);
4565
4566 // Parse GNU attributes, if present.
John McCall7f040a92010-12-24 02:08:15 +00004567 MaybeParseGNUAttributes(ParmDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00004568
Chris Lattnerf97409f2008-04-06 06:57:35 +00004569 // Remember this parsed parameter in ParamInfo.
4570 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
Mike Stump1eb44332009-09-09 15:08:12 +00004571
Douglas Gregor72b505b2008-12-16 21:30:33 +00004572 // DefArgToks is used when the parsing of default arguments needs
4573 // to be delayed.
4574 CachedTokens *DefArgToks = 0;
4575
Chris Lattnerf97409f2008-04-06 06:57:35 +00004576 // If no parameter was specified, verify that *something* was specified,
4577 // otherwise we have a missing type and identifier.
Chris Lattnere64c5492009-02-27 18:38:20 +00004578 if (DS.isEmpty() && ParmDecl.getIdentifier() == 0 &&
4579 ParmDecl.getNumTypeObjects() == 0) {
Chris Lattnerf97409f2008-04-06 06:57:35 +00004580 // Completely missing, emit error.
4581 Diag(DSStart, diag::err_missing_param);
4582 } else {
4583 // Otherwise, we have something. Add it and let semantic analysis try
4584 // to grok it and add the result to the ParamInfo we are building.
Mike Stump1eb44332009-09-09 15:08:12 +00004585
Chris Lattnerf97409f2008-04-06 06:57:35 +00004586 // Inform the actions module about the parameter declarator, so it gets
4587 // added to the current scope.
John McCalld226f652010-08-21 09:40:31 +00004588 Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl);
Chris Lattner04421082008-04-08 04:40:51 +00004589
4590 // Parse the default argument, if any. We parse the default
4591 // arguments in all dialects; the semantic analysis in
4592 // ActOnParamDefaultArgument will reject the default argument in
4593 // C.
4594 if (Tok.is(tok::equal)) {
Douglas Gregor61366e92008-12-24 00:01:03 +00004595 SourceLocation EqualLoc = Tok.getLocation();
4596
Chris Lattner04421082008-04-08 04:40:51 +00004597 // Parse the default argument
Douglas Gregor72b505b2008-12-16 21:30:33 +00004598 if (D.getContext() == Declarator::MemberContext) {
4599 // If we're inside a class definition, cache the tokens
4600 // corresponding to the default argument. We'll actually parse
4601 // them when we see the end of the class definition.
Douglas Gregor72b505b2008-12-16 21:30:33 +00004602 // FIXME: Can we use a smart pointer for Toks?
4603 DefArgToks = new CachedTokens;
4604
Mike Stump1eb44332009-09-09 15:08:12 +00004605 if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks,
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +00004606 /*StopAtSemi=*/true,
4607 /*ConsumeFinalToken=*/false)) {
Douglas Gregor72b505b2008-12-16 21:30:33 +00004608 delete DefArgToks;
4609 DefArgToks = 0;
Douglas Gregor61366e92008-12-24 00:01:03 +00004610 Actions.ActOnParamDefaultArgumentError(Param);
Argyrios Kyrtzidis2b602ad2010-08-06 09:47:24 +00004611 } else {
4612 // Mark the end of the default argument so that we know when to
4613 // stop when we parse it later on.
4614 Token DefArgEnd;
4615 DefArgEnd.startToken();
4616 DefArgEnd.setKind(tok::cxx_defaultarg_end);
4617 DefArgEnd.setLocation(Tok.getLocation());
4618 DefArgToks->push_back(DefArgEnd);
Mike Stump1eb44332009-09-09 15:08:12 +00004619 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc,
Anders Carlsson5e300d12009-06-12 16:51:40 +00004620 (*DefArgToks)[1].getLocation());
Argyrios Kyrtzidis2b602ad2010-08-06 09:47:24 +00004621 }
Chris Lattner04421082008-04-08 04:40:51 +00004622 } else {
Douglas Gregor72b505b2008-12-16 21:30:33 +00004623 // Consume the '='.
Douglas Gregor61366e92008-12-24 00:01:03 +00004624 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00004625
Douglas Gregorbe0f7bd2010-09-11 20:24:53 +00004626 // The argument isn't actually potentially evaluated unless it is
4627 // used.
4628 EnterExpressionEvaluationContext Eval(Actions,
Douglas Gregorccc1b5e2012-02-21 00:37:24 +00004629 Sema::PotentiallyEvaluatedIfUsed,
4630 Param);
Douglas Gregorbe0f7bd2010-09-11 20:24:53 +00004631
Sebastian Redl84407ba2012-03-14 15:54:00 +00004632 ExprResult DefArgResult;
Sebastian Redl3e280b52012-03-18 22:25:45 +00004633 if (getLangOpts().CPlusPlus0x && Tok.is(tok::l_brace)) {
4634 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
Sebastian Redl84407ba2012-03-14 15:54:00 +00004635 DefArgResult = ParseBraceInitializer();
Sebastian Redl3e280b52012-03-18 22:25:45 +00004636 } else
Sebastian Redl84407ba2012-03-14 15:54:00 +00004637 DefArgResult = ParseAssignmentExpression();
Douglas Gregor72b505b2008-12-16 21:30:33 +00004638 if (DefArgResult.isInvalid()) {
4639 Actions.ActOnParamDefaultArgumentError(Param);
4640 SkipUntil(tok::comma, tok::r_paren, true, true);
4641 } else {
4642 // Inform the actions module about the default argument
4643 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
John McCall9ae2f072010-08-23 23:25:46 +00004644 DefArgResult.take());
Douglas Gregor72b505b2008-12-16 21:30:33 +00004645 }
Chris Lattner04421082008-04-08 04:40:51 +00004646 }
4647 }
Mike Stump1eb44332009-09-09 15:08:12 +00004648
4649 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
4650 ParmDecl.getIdentifierLoc(), Param,
Douglas Gregor72b505b2008-12-16 21:30:33 +00004651 DefArgToks));
Chris Lattnerf97409f2008-04-06 06:57:35 +00004652 }
4653
4654 // If the next token is a comma, consume it and keep reading arguments.
Douglas Gregored5d6512009-09-22 21:41:40 +00004655 if (Tok.isNot(tok::comma)) {
4656 if (Tok.is(tok::ellipsis)) {
Douglas Gregored5d6512009-09-22 21:41:40 +00004657 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
4658
David Blaikie4e4d0842012-03-11 07:00:24 +00004659 if (!getLangOpts().CPlusPlus) {
Douglas Gregored5d6512009-09-22 21:41:40 +00004660 // We have ellipsis without a preceding ',', which is ill-formed
4661 // in C. Complain and provide the fix.
4662 Diag(EllipsisLoc, diag::err_missing_comma_before_ellipsis)
Douglas Gregor849b2432010-03-31 17:46:05 +00004663 << FixItHint::CreateInsertion(EllipsisLoc, ", ");
Douglas Gregored5d6512009-09-22 21:41:40 +00004664 }
4665 }
4666
4667 break;
4668 }
Mike Stump1eb44332009-09-09 15:08:12 +00004669
Chris Lattnerf97409f2008-04-06 06:57:35 +00004670 // Consume the comma.
4671 ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00004672 }
Mike Stump1eb44332009-09-09 15:08:12 +00004673
Chris Lattner66d28652008-04-06 06:34:08 +00004674}
Chris Lattneref4715c2008-04-06 05:45:57 +00004675
Reid Spencer5f016e22007-07-11 17:01:13 +00004676/// [C90] direct-declarator '[' constant-expression[opt] ']'
4677/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
4678/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
4679/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
4680/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Richard Smith6ee326a2012-04-10 01:32:12 +00004681/// [C++11] direct-declarator '[' constant-expression[opt] ']'
4682/// attribute-specifier-seq[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00004683void Parser::ParseBracketDeclarator(Declarator &D) {
Richard Smith6ee326a2012-04-10 01:32:12 +00004684 if (CheckProhibitedCXX11Attribute())
4685 return;
4686
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004687 BalancedDelimiterTracker T(*this, tok::l_square);
4688 T.consumeOpen();
Mike Stump1eb44332009-09-09 15:08:12 +00004689
Chris Lattner378c7e42008-12-18 07:27:21 +00004690 // C array syntax has many features, but by-far the most common is [] and [4].
4691 // This code does a fast path to handle some of the most obvious cases.
4692 if (Tok.getKind() == tok::r_square) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004693 T.consumeClose();
John McCall0b7e6782011-03-24 11:26:52 +00004694 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00004695 MaybeParseCXX0XAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00004696
Chris Lattner378c7e42008-12-18 07:27:21 +00004697 // Remember that we parsed the empty array type.
John McCall60d7b3a2010-08-24 06:29:42 +00004698 ExprResult NumElements;
John McCall0b7e6782011-03-24 11:26:52 +00004699 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004700 T.getOpenLocation(),
4701 T.getCloseLocation()),
4702 attrs, T.getCloseLocation());
Chris Lattner378c7e42008-12-18 07:27:21 +00004703 return;
4704 } else if (Tok.getKind() == tok::numeric_constant &&
4705 GetLookAheadToken(1).is(tok::r_square)) {
4706 // [4] is very common. Parse the numeric constant expression.
Richard Smith36f5cfe2012-03-09 08:00:36 +00004707 ExprResult ExprRes(Actions.ActOnNumericConstant(Tok, getCurScope()));
Chris Lattner378c7e42008-12-18 07:27:21 +00004708 ConsumeToken();
4709
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004710 T.consumeClose();
John McCall0b7e6782011-03-24 11:26:52 +00004711 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00004712 MaybeParseCXX0XAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00004713
Chris Lattner378c7e42008-12-18 07:27:21 +00004714 // Remember that we parsed a array type, and remember its features.
John McCall0b7e6782011-03-24 11:26:52 +00004715 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, 0,
John McCall7f040a92010-12-24 02:08:15 +00004716 ExprRes.release(),
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004717 T.getOpenLocation(),
4718 T.getCloseLocation()),
4719 attrs, T.getCloseLocation());
Chris Lattner378c7e42008-12-18 07:27:21 +00004720 return;
4721 }
Mike Stump1eb44332009-09-09 15:08:12 +00004722
Reid Spencer5f016e22007-07-11 17:01:13 +00004723 // If valid, this location is the position where we read the 'static' keyword.
4724 SourceLocation StaticLoc;
Chris Lattner04d66662007-10-09 17:33:22 +00004725 if (Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00004726 StaticLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00004727
Reid Spencer5f016e22007-07-11 17:01:13 +00004728 // If there is a type-qualifier-list, read it now.
Chris Lattnera1fcbad2008-12-18 06:50:14 +00004729 // Type qualifiers in an array subscript are a C99 feature.
John McCall0b7e6782011-03-24 11:26:52 +00004730 DeclSpec DS(AttrFactory);
Chris Lattner5a69d1c2008-12-18 07:02:59 +00004731 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Mike Stump1eb44332009-09-09 15:08:12 +00004732
Reid Spencer5f016e22007-07-11 17:01:13 +00004733 // If we haven't already read 'static', check to see if there is one after the
4734 // type-qualifier-list.
Chris Lattner04d66662007-10-09 17:33:22 +00004735 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00004736 StaticLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00004737
Reid Spencer5f016e22007-07-11 17:01:13 +00004738 // Handle "direct-declarator [ type-qual-list[opt] * ]".
4739 bool isStar = false;
John McCall60d7b3a2010-08-24 06:29:42 +00004740 ExprResult NumElements;
Mike Stump1eb44332009-09-09 15:08:12 +00004741
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00004742 // Handle the case where we have '[*]' as the array size. However, a leading
4743 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
4744 // the the token after the star is a ']'. Since stars in arrays are
4745 // infrequent, use of lookahead is not costly here.
4746 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattnera711dd02008-04-06 05:27:21 +00004747 ConsumeToken(); // Eat the '*'.
Reid Spencer5f016e22007-07-11 17:01:13 +00004748
Chris Lattnera1fcbad2008-12-18 06:50:14 +00004749 if (StaticLoc.isValid()) {
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00004750 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattnera1fcbad2008-12-18 06:50:14 +00004751 StaticLoc = SourceLocation(); // Drop the static.
4752 }
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00004753 isStar = true;
Chris Lattner04d66662007-10-09 17:33:22 +00004754 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner378c7e42008-12-18 07:27:21 +00004755 // Note, in C89, this production uses the constant-expr production instead
4756 // of assignment-expr. The only difference is that assignment-expr allows
4757 // things like '=' and '*='. Sema rejects these in C89 mode because they
4758 // are not i-c-e's, so we don't need to distinguish between the two here.
Mike Stump1eb44332009-09-09 15:08:12 +00004759
Douglas Gregore0762c92009-06-19 23:52:42 +00004760 // Parse the constant-expression or assignment-expression now (depending
4761 // on dialect).
David Blaikie4e4d0842012-03-11 07:00:24 +00004762 if (getLangOpts().CPlusPlus) {
Douglas Gregore0762c92009-06-19 23:52:42 +00004763 NumElements = ParseConstantExpression();
Eli Friedman71b8fb52012-01-21 01:01:51 +00004764 } else {
4765 EnterExpressionEvaluationContext Unevaluated(Actions,
4766 Sema::ConstantEvaluated);
Douglas Gregore0762c92009-06-19 23:52:42 +00004767 NumElements = ParseAssignmentExpression();
Eli Friedman71b8fb52012-01-21 01:01:51 +00004768 }
Reid Spencer5f016e22007-07-11 17:01:13 +00004769 }
Mike Stump1eb44332009-09-09 15:08:12 +00004770
Reid Spencer5f016e22007-07-11 17:01:13 +00004771 // If there was an error parsing the assignment-expression, recover.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00004772 if (NumElements.isInvalid()) {
Chris Lattner5cb10d32009-04-24 22:30:50 +00004773 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00004774 // If the expression was invalid, skip it.
4775 SkipUntil(tok::r_square);
4776 return;
4777 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00004778
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004779 T.consumeClose();
Sebastian Redlab197ba2009-02-09 18:23:29 +00004780
John McCall0b7e6782011-03-24 11:26:52 +00004781 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00004782 MaybeParseCXX0XAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00004783
Chris Lattner378c7e42008-12-18 07:27:21 +00004784 // Remember that we parsed a array type, and remember its features.
John McCall0b7e6782011-03-24 11:26:52 +00004785 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
Reid Spencer5f016e22007-07-11 17:01:13 +00004786 StaticLoc.isValid(), isStar,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00004787 NumElements.release(),
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004788 T.getOpenLocation(),
4789 T.getCloseLocation()),
4790 attrs, T.getCloseLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00004791}
4792
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004793/// [GNU] typeof-specifier:
4794/// typeof ( expressions )
4795/// typeof ( type-name )
4796/// [GNU/C++] typeof unary-expression
Steve Naroffd1861fd2007-07-31 12:34:36 +00004797///
4798void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner04d66662007-10-09 17:33:22 +00004799 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004800 Token OpTok = Tok;
Steve Naroffd1861fd2007-07-31 12:34:36 +00004801 SourceLocation StartLoc = ConsumeToken();
4802
John McCallcfb708c2010-01-13 20:03:27 +00004803 const bool hasParens = Tok.is(tok::l_paren);
4804
Eli Friedman71b8fb52012-01-21 01:01:51 +00004805 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated);
4806
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004807 bool isCastExpr;
John McCallb3d87482010-08-24 05:47:05 +00004808 ParsedType CastTy;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004809 SourceRange CastRange;
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00004810 ExprResult Operand = ParseExprAfterUnaryExprOrTypeTrait(OpTok, isCastExpr,
4811 CastTy, CastRange);
John McCallcfb708c2010-01-13 20:03:27 +00004812 if (hasParens)
4813 DS.setTypeofParensRange(CastRange);
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004814
4815 if (CastRange.getEnd().isInvalid())
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004816 // FIXME: Not accurate, the range gets one token more than it should.
4817 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004818 else
4819 DS.SetRangeEnd(CastRange.getEnd());
Mike Stump1eb44332009-09-09 15:08:12 +00004820
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004821 if (isCastExpr) {
4822 if (!CastTy) {
4823 DS.SetTypeSpecError();
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004824 return;
Douglas Gregor809070a2009-02-18 17:45:20 +00004825 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004826
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004827 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00004828 unsigned DiagID;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004829 // Check for duplicate type specifiers (e.g. "int typeof(int)").
4830 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00004831 DiagID, CastTy))
4832 Diag(StartLoc, DiagID) << PrevSpec;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004833 return;
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004834 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004835
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004836 // If we get here, the operand to the typeof was an expresion.
4837 if (Operand.isInvalid()) {
4838 DS.SetTypeSpecError();
Steve Naroff9dfa7b42007-08-02 02:53:48 +00004839 return;
Steve Naroffd1861fd2007-07-31 12:34:36 +00004840 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004841
Eli Friedman71b8fb52012-01-21 01:01:51 +00004842 // We might need to transform the operand if it is potentially evaluated.
4843 Operand = Actions.HandleExprEvaluationContextForTypeof(Operand.get());
4844 if (Operand.isInvalid()) {
4845 DS.SetTypeSpecError();
4846 return;
4847 }
4848
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004849 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00004850 unsigned DiagID;
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004851 // Check for duplicate type specifiers (e.g. "int typeof(int)").
4852 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
John McCallb3d87482010-08-24 05:47:05 +00004853 DiagID, Operand.get()))
John McCallfec54012009-08-03 20:12:06 +00004854 Diag(StartLoc, DiagID) << PrevSpec;
Steve Naroffd1861fd2007-07-31 12:34:36 +00004855}
Chris Lattner1b492422010-02-28 18:33:55 +00004856
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00004857/// [C11] atomic-specifier:
Eli Friedmanb001de72011-10-06 23:00:33 +00004858/// _Atomic ( type-name )
4859///
4860void Parser::ParseAtomicSpecifier(DeclSpec &DS) {
4861 assert(Tok.is(tok::kw__Atomic) && "Not an atomic specifier");
4862
4863 SourceLocation StartLoc = ConsumeToken();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004864 BalancedDelimiterTracker T(*this, tok::l_paren);
4865 if (T.expectAndConsume(diag::err_expected_lparen_after, "_Atomic")) {
Eli Friedmanb001de72011-10-06 23:00:33 +00004866 SkipUntil(tok::r_paren);
4867 return;
4868 }
4869
4870 TypeResult Result = ParseTypeName();
4871 if (Result.isInvalid()) {
4872 SkipUntil(tok::r_paren);
4873 return;
4874 }
4875
4876 // Match the ')'
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004877 T.consumeClose();
Eli Friedmanb001de72011-10-06 23:00:33 +00004878
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004879 if (T.getCloseLocation().isInvalid())
Eli Friedmanb001de72011-10-06 23:00:33 +00004880 return;
4881
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004882 DS.setTypeofParensRange(T.getRange());
4883 DS.SetRangeEnd(T.getCloseLocation());
Eli Friedmanb001de72011-10-06 23:00:33 +00004884
4885 const char *PrevSpec = 0;
4886 unsigned DiagID;
4887 if (DS.SetTypeSpecType(DeclSpec::TST_atomic, StartLoc, PrevSpec,
4888 DiagID, Result.release()))
4889 Diag(StartLoc, DiagID) << PrevSpec;
4890}
4891
Chris Lattner1b492422010-02-28 18:33:55 +00004892
4893/// TryAltiVecVectorTokenOutOfLine - Out of line body that should only be called
4894/// from TryAltiVecVectorToken.
4895bool Parser::TryAltiVecVectorTokenOutOfLine() {
4896 Token Next = NextToken();
4897 switch (Next.getKind()) {
4898 default: return false;
4899 case tok::kw_short:
4900 case tok::kw_long:
4901 case tok::kw_signed:
4902 case tok::kw_unsigned:
4903 case tok::kw_void:
4904 case tok::kw_char:
4905 case tok::kw_int:
4906 case tok::kw_float:
4907 case tok::kw_double:
4908 case tok::kw_bool:
4909 case tok::kw___pixel:
4910 Tok.setKind(tok::kw___vector);
4911 return true;
4912 case tok::identifier:
4913 if (Next.getIdentifierInfo() == Ident_pixel) {
4914 Tok.setKind(tok::kw___vector);
4915 return true;
4916 }
4917 return false;
4918 }
4919}
4920
4921bool Parser::TryAltiVecTokenOutOfLine(DeclSpec &DS, SourceLocation Loc,
4922 const char *&PrevSpec, unsigned &DiagID,
4923 bool &isInvalid) {
4924 if (Tok.getIdentifierInfo() == Ident_vector) {
4925 Token Next = NextToken();
4926 switch (Next.getKind()) {
4927 case tok::kw_short:
4928 case tok::kw_long:
4929 case tok::kw_signed:
4930 case tok::kw_unsigned:
4931 case tok::kw_void:
4932 case tok::kw_char:
4933 case tok::kw_int:
4934 case tok::kw_float:
4935 case tok::kw_double:
4936 case tok::kw_bool:
4937 case tok::kw___pixel:
4938 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
4939 return true;
4940 case tok::identifier:
4941 if (Next.getIdentifierInfo() == Ident_pixel) {
4942 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
4943 return true;
4944 }
4945 break;
4946 default:
4947 break;
4948 }
Douglas Gregora8f031f2010-06-16 15:28:57 +00004949 } else if ((Tok.getIdentifierInfo() == Ident_pixel) &&
Chris Lattner1b492422010-02-28 18:33:55 +00004950 DS.isTypeAltiVecVector()) {
4951 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
4952 return true;
4953 }
4954 return false;
4955}