blob: 9aa5eaba00ce4a28c3a89d44381451834f46399a [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"
John McCall19510852010-08-20 18:27:03 +000017#include "clang/Sema/Scope.h"
18#include "clang/Sema/ParsedTemplate.h"
John McCallf312b1e2010-08-26 23:41:50 +000019#include "clang/Sema/PrettyDeclStackTrace.h"
Chris Lattnerd167ca02009-12-10 00:21:05 +000020#include "RAIIObjectsForParser.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000021#include "llvm/ADT/SmallSet.h"
Benjamin Kramer8fe83e12012-02-04 13:45:25 +000022#include "llvm/ADT/SmallString.h"
Caitlin Sadowskib51e0312011-08-09 17:59:31 +000023#include "llvm/ADT/StringSwitch.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000024using namespace clang;
25
26//===----------------------------------------------------------------------===//
27// C99 6.7: Declarations.
28//===----------------------------------------------------------------------===//
29
30/// ParseTypeName
31/// type-name: [C99 6.7.6]
32/// specifier-qualifier-list abstract-declarator[opt]
Sebastian Redl4c5d3202008-11-21 19:14:01 +000033///
34/// Called type-id in C++.
Douglas Gregor683a81f2011-01-31 16:09:46 +000035TypeResult Parser::ParseTypeName(SourceRange *Range,
John McCallf85e1932011-06-15 23:02:42 +000036 Declarator::TheContext Context,
Richard Smithc89edf52011-07-01 19:46:12 +000037 AccessSpecifier AS,
38 Decl **OwnedType) {
Richard Smith6d96d3a2012-03-15 01:02:11 +000039 DeclSpecContext DSC = getDeclSpecContextFromDeclaratorContext(Context);
Richard Smith7796eb52012-03-12 08:56:40 +000040
Reid Spencer5f016e22007-07-11 17:01:13 +000041 // Parse the common declaration-specifiers piece.
John McCall0b7e6782011-03-24 11:26:52 +000042 DeclSpec DS(AttrFactory);
Richard Smith7796eb52012-03-12 08:56:40 +000043 ParseSpecifierQualifierList(DS, AS, DSC);
Richard Smithc89edf52011-07-01 19:46:12 +000044 if (OwnedType)
45 *OwnedType = DS.isTypeSpecOwned() ? DS.getRepAsDecl() : 0;
Sebastian Redlef65f062009-05-29 18:02:33 +000046
Reid Spencer5f016e22007-07-11 17:01:13 +000047 // Parse the abstract-declarator, if present.
Douglas Gregor683a81f2011-01-31 16:09:46 +000048 Declarator DeclaratorInfo(DS, Context);
Reid Spencer5f016e22007-07-11 17:01:13 +000049 ParseDeclarator(DeclaratorInfo);
Sebastian Redlef65f062009-05-29 18:02:33 +000050 if (Range)
51 *Range = DeclaratorInfo.getSourceRange();
52
Chris Lattnereaaebc72009-04-25 08:06:05 +000053 if (DeclaratorInfo.isInvalidType())
Douglas Gregor809070a2009-02-18 17:45:20 +000054 return true;
55
Douglas Gregor23c94db2010-07-02 17:43:08 +000056 return Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
Reid Spencer5f016e22007-07-11 17:01:13 +000057}
58
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +000059
60/// isAttributeLateParsed - Return true if the attribute has arguments that
61/// require late parsing.
62static bool isAttributeLateParsed(const IdentifierInfo &II) {
63 return llvm::StringSwitch<bool>(II.getName())
64#include "clang/Parse/AttrLateParsed.inc"
65 .Default(false);
66}
67
68
Sean Huntbbd37c62009-11-21 08:43:09 +000069/// ParseGNUAttributes - Parse a non-empty attributes list.
Reid Spencer5f016e22007-07-11 17:01:13 +000070///
71/// [GNU] attributes:
72/// attribute
73/// attributes attribute
74///
75/// [GNU] attribute:
76/// '__attribute__' '(' '(' attribute-list ')' ')'
77///
78/// [GNU] attribute-list:
79/// attrib
80/// attribute_list ',' attrib
81///
82/// [GNU] attrib:
83/// empty
84/// attrib-name
85/// attrib-name '(' identifier ')'
86/// attrib-name '(' identifier ',' nonempty-expr-list ')'
87/// attrib-name '(' argument-expression-list [C99 6.5.2] ')'
88///
89/// [GNU] attrib-name:
90/// identifier
91/// typespec
92/// typequal
93/// storageclass
Mike Stump1eb44332009-09-09 15:08:12 +000094///
Reid Spencer5f016e22007-07-11 17:01:13 +000095/// FIXME: The GCC grammar/code for this construct implies we need two
Mike Stump1eb44332009-09-09 15:08:12 +000096/// token lookahead. Comment from gcc: "If they start with an identifier
97/// which is followed by a comma or close parenthesis, then the arguments
Reid Spencer5f016e22007-07-11 17:01:13 +000098/// start with that identifier; otherwise they are an expression list."
99///
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000100/// GCC does not require the ',' between attribs in an attribute-list.
101///
Reid Spencer5f016e22007-07-11 17:01:13 +0000102/// At the moment, I am not doing 2 token lookahead. I am also unaware of
103/// any attributes that don't work (based on my limited testing). Most
104/// attributes are very simple in practice. Until we find a bug, I don't see
105/// a pressing need to implement the 2 token lookahead.
106
John McCall7f040a92010-12-24 02:08:15 +0000107void Parser::ParseGNUAttributes(ParsedAttributes &attrs,
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000108 SourceLocation *endLoc,
109 LateParsedAttrList *LateAttrs) {
Sean Huntbbd37c62009-11-21 08:43:09 +0000110 assert(Tok.is(tok::kw___attribute) && "Not a GNU attribute list!");
Mike Stump1eb44332009-09-09 15:08:12 +0000111
Chris Lattner04d66662007-10-09 17:33:22 +0000112 while (Tok.is(tok::kw___attribute)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000113 ConsumeToken();
114 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
115 "attribute")) {
116 SkipUntil(tok::r_paren, true); // skip until ) or ;
John McCall7f040a92010-12-24 02:08:15 +0000117 return;
Reid Spencer5f016e22007-07-11 17:01:13 +0000118 }
119 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, "(")) {
120 SkipUntil(tok::r_paren, true); // skip until ) or ;
John McCall7f040a92010-12-24 02:08:15 +0000121 return;
Reid Spencer5f016e22007-07-11 17:01:13 +0000122 }
123 // Parse the attribute-list. e.g. __attribute__(( weak, alias("__f") ))
Chris Lattner04d66662007-10-09 17:33:22 +0000124 while (Tok.is(tok::identifier) || isDeclarationSpecifier() ||
125 Tok.is(tok::comma)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000126 if (Tok.is(tok::comma)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000127 // allows for empty/non-empty attributes. ((__vector_size__(16),,,,))
128 ConsumeToken();
129 continue;
130 }
131 // we have an identifier or declaration specifier (const, int, etc.)
132 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
133 SourceLocation AttrNameLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000134
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000135 if (Tok.is(tok::l_paren)) {
136 // handle "parameterized" attributes
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000137 if (LateAttrs && isAttributeLateParsed(*AttrName)) {
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000138 LateParsedAttribute *LA =
139 new LateParsedAttribute(this, *AttrName, AttrNameLoc);
140 LateAttrs->push_back(LA);
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000141
142 // Attributes in a class are parsed at the end of the class, along
143 // with other late-parsed declarations.
144 if (!ClassStack.empty())
145 getCurrentClass().LateParsedDeclarations.push_back(LA);
Mike Stump1eb44332009-09-09 15:08:12 +0000146
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000147 // consume everything up to and including the matching right parens
148 ConsumeAndStoreUntil(tok::r_paren, LA->Toks, true, false);
Mike Stump1eb44332009-09-09 15:08:12 +0000149
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000150 Token Eof;
151 Eof.startToken();
152 Eof.setLocation(Tok.getLocation());
153 LA->Toks.push_back(Eof);
154 } else {
155 ParseGNUAttributeArgs(AttrName, AttrNameLoc, attrs, endLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000156 }
157 } else {
John McCall0b7e6782011-03-24 11:26:52 +0000158 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc,
159 0, SourceLocation(), 0, 0);
Reid Spencer5f016e22007-07-11 17:01:13 +0000160 }
161 }
162 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
Reid Spencer5f016e22007-07-11 17:01:13 +0000163 SkipUntil(tok::r_paren, false);
Sean Huntbbd37c62009-11-21 08:43:09 +0000164 SourceLocation Loc = Tok.getLocation();
Sebastian Redlab197ba2009-02-09 18:23:29 +0000165 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) {
166 SkipUntil(tok::r_paren, false);
167 }
John McCall7f040a92010-12-24 02:08:15 +0000168 if (endLoc)
169 *endLoc = Loc;
Reid Spencer5f016e22007-07-11 17:01:13 +0000170 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000171}
172
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000173
174/// Parse the arguments to a parameterized GNU attribute
175void Parser::ParseGNUAttributeArgs(IdentifierInfo *AttrName,
176 SourceLocation AttrNameLoc,
177 ParsedAttributes &Attrs,
178 SourceLocation *EndLoc) {
179
180 assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('");
181
182 // Availability attributes have their own grammar.
183 if (AttrName->isStr("availability")) {
184 ParseAvailabilityAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc);
185 return;
186 }
187 // Thread safety attributes fit into the FIXME case above, so we
188 // just parse the arguments as a list of expressions
189 if (IsThreadSafetyAttribute(AttrName->getName())) {
190 ParseThreadSafetyAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc);
191 return;
192 }
193
194 ConsumeParen(); // ignore the left paren loc for now
195
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000196 IdentifierInfo *ParmName = 0;
197 SourceLocation ParmLoc;
198 bool BuiltinType = false;
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000199
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000200 switch (Tok.getKind()) {
201 case tok::kw_char:
202 case tok::kw_wchar_t:
203 case tok::kw_char16_t:
204 case tok::kw_char32_t:
205 case tok::kw_bool:
206 case tok::kw_short:
207 case tok::kw_int:
208 case tok::kw_long:
209 case tok::kw___int64:
210 case tok::kw_signed:
211 case tok::kw_unsigned:
212 case tok::kw_float:
213 case tok::kw_double:
214 case tok::kw_void:
215 case tok::kw_typeof:
216 // __attribute__(( vec_type_hint(char) ))
217 // FIXME: Don't just discard the builtin type token.
218 ConsumeToken();
219 BuiltinType = true;
220 break;
221
222 case tok::identifier:
223 ParmName = Tok.getIdentifierInfo();
224 ParmLoc = ConsumeToken();
225 break;
226
227 default:
228 break;
229 }
230
231 ExprVector ArgExprs(Actions);
232
233 if (!BuiltinType &&
234 (ParmLoc.isValid() ? Tok.is(tok::comma) : Tok.isNot(tok::r_paren))) {
235 // Eat the comma.
236 if (ParmLoc.isValid())
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000237 ConsumeToken();
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000238
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000239 // Parse the non-empty comma-separated list of expressions.
240 while (1) {
241 ExprResult ArgExpr(ParseAssignmentExpression());
242 if (ArgExpr.isInvalid()) {
243 SkipUntil(tok::r_paren);
244 return;
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000245 }
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000246 ArgExprs.push_back(ArgExpr.release());
247 if (Tok.isNot(tok::comma))
248 break;
249 ConsumeToken(); // Eat the comma, move to the next argument
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000250 }
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000251 }
Fariborz Jahanian7a81e412011-10-18 17:11:10 +0000252 else if (Tok.is(tok::less) && AttrName->isStr("iboutletcollection")) {
253 if (!ExpectAndConsume(tok::less, diag::err_expected_less_after, "<",
254 tok::greater)) {
Fariborz Jahanianb2243432011-10-18 23:13:50 +0000255 while (Tok.is(tok::identifier)) {
256 ConsumeToken();
257 if (Tok.is(tok::greater))
258 break;
259 if (Tok.is(tok::comma)) {
260 ConsumeToken();
261 continue;
262 }
263 }
264 if (Tok.isNot(tok::greater))
265 Diag(Tok, diag::err_iboutletcollection_with_protocol);
Fariborz Jahanian7a81e412011-10-18 17:11:10 +0000266 SkipUntil(tok::r_paren, false, true); // skip until ')'
267 }
268 }
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000269
270 SourceLocation RParen = Tok.getLocation();
271 if (!ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) {
272 AttributeList *attr =
Argyrios Kyrtzidisffcc3102011-09-13 16:05:53 +0000273 Attrs.addNew(AttrName, SourceRange(AttrNameLoc, RParen), 0, AttrNameLoc,
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000274 ParmName, ParmLoc, ArgExprs.take(), ArgExprs.size());
Michael Hane53ac8a2012-03-07 00:12:16 +0000275 if (BuiltinType && attr->getKind() == AttributeList::AT_iboutletcollection)
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000276 Diag(Tok, diag::err_iboutletcollection_builtintype);
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000277 }
278}
279
280
Eli Friedmana23b4852009-06-08 07:21:15 +0000281/// ParseMicrosoftDeclSpec - Parse an __declspec construct
282///
283/// [MS] decl-specifier:
284/// __declspec ( extended-decl-modifier-seq )
285///
286/// [MS] extended-decl-modifier-seq:
287/// extended-decl-modifier[opt]
288/// extended-decl-modifier extended-decl-modifier-seq
289
John McCall7f040a92010-12-24 02:08:15 +0000290void Parser::ParseMicrosoftDeclSpec(ParsedAttributes &attrs) {
Steve Narofff59e17e2008-12-24 20:59:21 +0000291 assert(Tok.is(tok::kw___declspec) && "Not a declspec!");
Eli Friedmana23b4852009-06-08 07:21:15 +0000292
Steve Narofff59e17e2008-12-24 20:59:21 +0000293 ConsumeToken();
Eli Friedmana23b4852009-06-08 07:21:15 +0000294 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
295 "declspec")) {
296 SkipUntil(tok::r_paren, true); // skip until ) or ;
John McCall7f040a92010-12-24 02:08:15 +0000297 return;
Eli Friedmana23b4852009-06-08 07:21:15 +0000298 }
Francois Pichet373197b2011-05-07 19:04:49 +0000299
Eli Friedman290eeb02009-06-08 23:27:34 +0000300 while (Tok.getIdentifierInfo()) {
Eli Friedmana23b4852009-06-08 07:21:15 +0000301 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
302 SourceLocation AttrNameLoc = ConsumeToken();
Francois Pichet373197b2011-05-07 19:04:49 +0000303
304 // FIXME: Remove this when we have proper __declspec(property()) support.
305 // Just skip everything inside property().
306 if (AttrName->getName() == "property") {
307 ConsumeParen();
308 SkipUntil(tok::r_paren);
309 }
Eli Friedmana23b4852009-06-08 07:21:15 +0000310 if (Tok.is(tok::l_paren)) {
311 ConsumeParen();
312 // FIXME: This doesn't parse __declspec(property(get=get_func_name))
313 // correctly.
John McCall60d7b3a2010-08-24 06:29:42 +0000314 ExprResult ArgExpr(ParseAssignmentExpression());
Eli Friedmana23b4852009-06-08 07:21:15 +0000315 if (!ArgExpr.isInvalid()) {
John McCallca0408f2010-08-23 06:44:23 +0000316 Expr *ExprList = ArgExpr.take();
John McCall0b7e6782011-03-24 11:26:52 +0000317 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
318 SourceLocation(), &ExprList, 1, true);
Eli Friedmana23b4852009-06-08 07:21:15 +0000319 }
320 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
321 SkipUntil(tok::r_paren, false);
322 } else {
John McCall0b7e6782011-03-24 11:26:52 +0000323 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc,
324 0, SourceLocation(), 0, 0, true);
Eli Friedmana23b4852009-06-08 07:21:15 +0000325 }
326 }
327 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
328 SkipUntil(tok::r_paren, false);
John McCall7f040a92010-12-24 02:08:15 +0000329 return;
Eli Friedman290eeb02009-06-08 23:27:34 +0000330}
331
John McCall7f040a92010-12-24 02:08:15 +0000332void Parser::ParseMicrosoftTypeAttributes(ParsedAttributes &attrs) {
Eli Friedman290eeb02009-06-08 23:27:34 +0000333 // Treat these like attributes
334 // FIXME: Allow Sema to distinguish between these and real attributes!
335 while (Tok.is(tok::kw___fastcall) || Tok.is(tok::kw___stdcall) ||
Douglas Gregorf813a2c2010-05-18 16:57:00 +0000336 Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___cdecl) ||
Francois Pichet3bd9aa42011-08-18 09:59:55 +0000337 Tok.is(tok::kw___ptr64) || Tok.is(tok::kw___w64) ||
Francois Pichet58fd97a2011-08-25 00:36:46 +0000338 Tok.is(tok::kw___ptr32) ||
Francois Pichet3bd9aa42011-08-18 09:59:55 +0000339 Tok.is(tok::kw___unaligned)) {
Eli Friedman290eeb02009-06-08 23:27:34 +0000340 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
341 SourceLocation AttrNameLoc = ConsumeToken();
Francois Pichet58fd97a2011-08-25 00:36:46 +0000342 if (Tok.is(tok::kw___ptr64) || Tok.is(tok::kw___w64) ||
343 Tok.is(tok::kw___ptr32))
Eli Friedman290eeb02009-06-08 23:27:34 +0000344 // FIXME: Support these properly!
345 continue;
John McCall0b7e6782011-03-24 11:26:52 +0000346 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
347 SourceLocation(), 0, 0, true);
Eli Friedman290eeb02009-06-08 23:27:34 +0000348 }
Steve Narofff59e17e2008-12-24 20:59:21 +0000349}
350
John McCall7f040a92010-12-24 02:08:15 +0000351void Parser::ParseBorlandTypeAttributes(ParsedAttributes &attrs) {
Dawn Perchik52fc3142010-09-03 01:29:35 +0000352 // Treat these like attributes
353 while (Tok.is(tok::kw___pascal)) {
354 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
355 SourceLocation AttrNameLoc = ConsumeToken();
John McCall0b7e6782011-03-24 11:26:52 +0000356 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
357 SourceLocation(), 0, 0, true);
Dawn Perchik52fc3142010-09-03 01:29:35 +0000358 }
John McCall7f040a92010-12-24 02:08:15 +0000359}
360
Peter Collingbournef315fa82011-02-14 01:42:53 +0000361void Parser::ParseOpenCLAttributes(ParsedAttributes &attrs) {
362 // Treat these like attributes
363 while (Tok.is(tok::kw___kernel)) {
364 SourceLocation AttrNameLoc = ConsumeToken();
John McCall0b7e6782011-03-24 11:26:52 +0000365 attrs.addNew(PP.getIdentifierInfo("opencl_kernel_function"),
366 AttrNameLoc, 0, AttrNameLoc, 0,
367 SourceLocation(), 0, 0, false);
Peter Collingbournef315fa82011-02-14 01:42:53 +0000368 }
369}
370
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000371void Parser::ParseOpenCLQualifiers(DeclSpec &DS) {
372 SourceLocation Loc = Tok.getLocation();
373 switch(Tok.getKind()) {
374 // OpenCL qualifiers:
375 case tok::kw___private:
376 case tok::kw_private:
John McCall0b7e6782011-03-24 11:26:52 +0000377 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000378 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000379 PP.getIdentifierInfo("address_space"), Loc, 0);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000380 break;
381
382 case tok::kw___global:
John McCall0b7e6782011-03-24 11:26:52 +0000383 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000384 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000385 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_global);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000386 break;
387
388 case tok::kw___local:
John McCall0b7e6782011-03-24 11:26:52 +0000389 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000390 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000391 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_local);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000392 break;
393
394 case tok::kw___constant:
John McCall0b7e6782011-03-24 11:26:52 +0000395 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000396 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000397 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_constant);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000398 break;
399
400 case tok::kw___read_only:
John McCall0b7e6782011-03-24 11:26:52 +0000401 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000402 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000403 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_read_only);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000404 break;
405
406 case tok::kw___write_only:
John McCall0b7e6782011-03-24 11:26:52 +0000407 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000408 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000409 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_write_only);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000410 break;
411
412 case tok::kw___read_write:
John McCall0b7e6782011-03-24 11:26:52 +0000413 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000414 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000415 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_read_write);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000416 break;
417 default: break;
418 }
419}
420
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000421/// \brief Parse a version number.
422///
423/// version:
424/// simple-integer
425/// simple-integer ',' simple-integer
426/// simple-integer ',' simple-integer ',' simple-integer
427VersionTuple Parser::ParseVersionTuple(SourceRange &Range) {
428 Range = Tok.getLocation();
429
430 if (!Tok.is(tok::numeric_constant)) {
431 Diag(Tok, diag::err_expected_version);
432 SkipUntil(tok::comma, tok::r_paren, true, true, true);
433 return VersionTuple();
434 }
435
436 // Parse the major (and possibly minor and subminor) versions, which
437 // are stored in the numeric constant. We utilize a quirk of the
438 // lexer, which is that it handles something like 1.2.3 as a single
439 // numeric constant, rather than two separate tokens.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000440 SmallString<512> Buffer;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000441 Buffer.resize(Tok.getLength()+1);
442 const char *ThisTokBegin = &Buffer[0];
443
444 // Get the spelling of the token, which eliminates trigraphs, etc.
445 bool Invalid = false;
446 unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin, &Invalid);
447 if (Invalid)
448 return VersionTuple();
449
450 // Parse the major version.
451 unsigned AfterMajor = 0;
452 unsigned Major = 0;
453 while (AfterMajor < ActualLength && isdigit(ThisTokBegin[AfterMajor])) {
454 Major = Major * 10 + ThisTokBegin[AfterMajor] - '0';
455 ++AfterMajor;
456 }
457
458 if (AfterMajor == 0) {
459 Diag(Tok, diag::err_expected_version);
460 SkipUntil(tok::comma, tok::r_paren, true, true, true);
461 return VersionTuple();
462 }
463
464 if (AfterMajor == ActualLength) {
465 ConsumeToken();
466
467 // We only had a single version component.
468 if (Major == 0) {
469 Diag(Tok, diag::err_zero_version);
470 return VersionTuple();
471 }
472
473 return VersionTuple(Major);
474 }
475
476 if (ThisTokBegin[AfterMajor] != '.' || (AfterMajor + 1 == ActualLength)) {
477 Diag(Tok, diag::err_expected_version);
478 SkipUntil(tok::comma, tok::r_paren, true, true, true);
479 return VersionTuple();
480 }
481
482 // Parse the minor version.
483 unsigned AfterMinor = AfterMajor + 1;
484 unsigned Minor = 0;
485 while (AfterMinor < ActualLength && isdigit(ThisTokBegin[AfterMinor])) {
486 Minor = Minor * 10 + ThisTokBegin[AfterMinor] - '0';
487 ++AfterMinor;
488 }
489
490 if (AfterMinor == ActualLength) {
491 ConsumeToken();
492
493 // We had major.minor.
494 if (Major == 0 && Minor == 0) {
495 Diag(Tok, diag::err_zero_version);
496 return VersionTuple();
497 }
498
499 return VersionTuple(Major, Minor);
500 }
501
502 // If what follows is not a '.', we have a problem.
503 if (ThisTokBegin[AfterMinor] != '.') {
504 Diag(Tok, diag::err_expected_version);
505 SkipUntil(tok::comma, tok::r_paren, true, true, true);
506 return VersionTuple();
507 }
508
509 // Parse the subminor version.
510 unsigned AfterSubminor = AfterMinor + 1;
511 unsigned Subminor = 0;
512 while (AfterSubminor < ActualLength && isdigit(ThisTokBegin[AfterSubminor])) {
513 Subminor = Subminor * 10 + ThisTokBegin[AfterSubminor] - '0';
514 ++AfterSubminor;
515 }
516
517 if (AfterSubminor != ActualLength) {
518 Diag(Tok, diag::err_expected_version);
519 SkipUntil(tok::comma, tok::r_paren, true, true, true);
520 return VersionTuple();
521 }
522 ConsumeToken();
523 return VersionTuple(Major, Minor, Subminor);
524}
525
526/// \brief Parse the contents of the "availability" attribute.
527///
528/// availability-attribute:
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000529/// 'availability' '(' platform ',' version-arg-list, opt-message')'
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000530///
531/// platform:
532/// identifier
533///
534/// version-arg-list:
535/// version-arg
536/// version-arg ',' version-arg-list
537///
538/// version-arg:
539/// 'introduced' '=' version
540/// 'deprecated' '=' version
Douglas Gregor93a70672012-03-11 04:53:21 +0000541/// 'obsoleted' = version
Douglas Gregorb53e4172011-03-26 03:35:55 +0000542/// 'unavailable'
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000543/// opt-message:
544/// 'message' '=' <string>
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000545void Parser::ParseAvailabilityAttribute(IdentifierInfo &Availability,
546 SourceLocation AvailabilityLoc,
547 ParsedAttributes &attrs,
548 SourceLocation *endLoc) {
549 SourceLocation PlatformLoc;
550 IdentifierInfo *Platform = 0;
551
552 enum { Introduced, Deprecated, Obsoleted, Unknown };
553 AvailabilityChange Changes[Unknown];
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000554 ExprResult MessageExpr;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000555
556 // Opening '('.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000557 BalancedDelimiterTracker T(*this, tok::l_paren);
558 if (T.consumeOpen()) {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000559 Diag(Tok, diag::err_expected_lparen);
560 return;
561 }
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000562
563 // Parse the platform name,
564 if (Tok.isNot(tok::identifier)) {
565 Diag(Tok, diag::err_availability_expected_platform);
566 SkipUntil(tok::r_paren);
567 return;
568 }
569 Platform = Tok.getIdentifierInfo();
570 PlatformLoc = ConsumeToken();
571
572 // Parse the ',' following the platform name.
573 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "", tok::r_paren))
574 return;
575
576 // If we haven't grabbed the pointers for the identifiers
577 // "introduced", "deprecated", and "obsoleted", do so now.
578 if (!Ident_introduced) {
579 Ident_introduced = PP.getIdentifierInfo("introduced");
580 Ident_deprecated = PP.getIdentifierInfo("deprecated");
581 Ident_obsoleted = PP.getIdentifierInfo("obsoleted");
Douglas Gregorb53e4172011-03-26 03:35:55 +0000582 Ident_unavailable = PP.getIdentifierInfo("unavailable");
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000583 Ident_message = PP.getIdentifierInfo("message");
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000584 }
585
586 // Parse the set of introductions/deprecations/removals.
Douglas Gregorb53e4172011-03-26 03:35:55 +0000587 SourceLocation UnavailableLoc;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000588 do {
589 if (Tok.isNot(tok::identifier)) {
590 Diag(Tok, diag::err_availability_expected_change);
591 SkipUntil(tok::r_paren);
592 return;
593 }
594 IdentifierInfo *Keyword = Tok.getIdentifierInfo();
595 SourceLocation KeywordLoc = ConsumeToken();
596
Douglas Gregorb53e4172011-03-26 03:35:55 +0000597 if (Keyword == Ident_unavailable) {
598 if (UnavailableLoc.isValid()) {
599 Diag(KeywordLoc, diag::err_availability_redundant)
600 << Keyword << SourceRange(UnavailableLoc);
601 }
602 UnavailableLoc = KeywordLoc;
603
604 if (Tok.isNot(tok::comma))
605 break;
606
607 ConsumeToken();
608 continue;
609 }
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000610
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000611 if (Tok.isNot(tok::equal)) {
612 Diag(Tok, diag::err_expected_equal_after)
613 << Keyword;
614 SkipUntil(tok::r_paren);
615 return;
616 }
617 ConsumeToken();
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000618 if (Keyword == Ident_message) {
619 if (!isTokenStringLiteral()) {
620 Diag(Tok, diag::err_expected_string_literal);
621 SkipUntil(tok::r_paren);
622 return;
623 }
624 MessageExpr = ParseStringLiteralExpression();
625 break;
626 }
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000627
628 SourceRange VersionRange;
629 VersionTuple Version = ParseVersionTuple(VersionRange);
630
631 if (Version.empty()) {
632 SkipUntil(tok::r_paren);
633 return;
634 }
635
636 unsigned Index;
637 if (Keyword == Ident_introduced)
638 Index = Introduced;
639 else if (Keyword == Ident_deprecated)
640 Index = Deprecated;
641 else if (Keyword == Ident_obsoleted)
642 Index = Obsoleted;
643 else
644 Index = Unknown;
645
646 if (Index < Unknown) {
647 if (!Changes[Index].KeywordLoc.isInvalid()) {
648 Diag(KeywordLoc, diag::err_availability_redundant)
649 << Keyword
650 << SourceRange(Changes[Index].KeywordLoc,
651 Changes[Index].VersionRange.getEnd());
652 }
653
654 Changes[Index].KeywordLoc = KeywordLoc;
655 Changes[Index].Version = Version;
656 Changes[Index].VersionRange = VersionRange;
657 } else {
658 Diag(KeywordLoc, diag::err_availability_unknown_change)
659 << Keyword << VersionRange;
660 }
661
662 if (Tok.isNot(tok::comma))
663 break;
664
665 ConsumeToken();
666 } while (true);
667
668 // Closing ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000669 if (T.consumeClose())
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000670 return;
671
672 if (endLoc)
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000673 *endLoc = T.getCloseLocation();
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000674
Douglas Gregorb53e4172011-03-26 03:35:55 +0000675 // The 'unavailable' availability cannot be combined with any other
676 // availability changes. Make sure that hasn't happened.
677 if (UnavailableLoc.isValid()) {
678 bool Complained = false;
679 for (unsigned Index = Introduced; Index != Unknown; ++Index) {
680 if (Changes[Index].KeywordLoc.isValid()) {
681 if (!Complained) {
682 Diag(UnavailableLoc, diag::warn_availability_and_unavailable)
683 << SourceRange(Changes[Index].KeywordLoc,
684 Changes[Index].VersionRange.getEnd());
685 Complained = true;
686 }
687
688 // Clear out the availability.
689 Changes[Index] = AvailabilityChange();
690 }
691 }
692 }
693
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000694 // Record this attribute
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000695 attrs.addNew(&Availability,
696 SourceRange(AvailabilityLoc, T.getCloseLocation()),
Fariborz Jahanianf96708d2012-01-23 23:38:32 +0000697 0, AvailabilityLoc,
John McCall0b7e6782011-03-24 11:26:52 +0000698 Platform, PlatformLoc,
699 Changes[Introduced],
700 Changes[Deprecated],
Douglas Gregorb53e4172011-03-26 03:35:55 +0000701 Changes[Obsoleted],
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000702 UnavailableLoc, MessageExpr.take(),
703 false, false);
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000704}
705
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000706
707// Late Parsed Attributes:
708// See other examples of late parsing in lib/Parse/ParseCXXInlineMethods
709
710void Parser::LateParsedDeclaration::ParseLexedAttributes() {}
711
712void Parser::LateParsedClass::ParseLexedAttributes() {
713 Self->ParseLexedAttributes(*Class);
714}
715
716void Parser::LateParsedAttribute::ParseLexedAttributes() {
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000717 Self->ParseLexedAttribute(*this, true, false);
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000718}
719
720/// Wrapper class which calls ParseLexedAttribute, after setting up the
721/// scope appropriately.
722void Parser::ParseLexedAttributes(ParsingClass &Class) {
723 // Deal with templates
724 // FIXME: Test cases to make sure this does the right thing for templates.
725 bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope;
726 ParseScope ClassTemplateScope(this, Scope::TemplateParamScope,
727 HasTemplateScope);
728 if (HasTemplateScope)
729 Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate);
730
731 // Set or update the scope flags to include Scope::ThisScope.
732 bool AlreadyHasClassScope = Class.TopLevelClass;
733 unsigned ScopeFlags = Scope::ClassScope|Scope::DeclScope|Scope::ThisScope;
734 ParseScope ClassScope(this, ScopeFlags, !AlreadyHasClassScope);
735 ParseScopeFlags ClassScopeFlags(this, ScopeFlags, AlreadyHasClassScope);
736
737 for (unsigned i = 0, ni = Class.LateParsedDeclarations.size(); i < ni; ++i) {
738 Class.LateParsedDeclarations[i]->ParseLexedAttributes();
739 }
740}
741
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000742
743/// \brief Parse all attributes in LAs, and attach them to Decl D.
744void Parser::ParseLexedAttributeList(LateParsedAttrList &LAs, Decl *D,
745 bool EnterScope, bool OnDefinition) {
746 for (unsigned i = 0, ni = LAs.size(); i < ni; ++i) {
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +0000747 LAs[i]->addDecl(D);
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000748 ParseLexedAttribute(*LAs[i], EnterScope, OnDefinition);
749 }
750 LAs.clear();
751}
752
753
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000754/// \brief Finish parsing an attribute for which parsing was delayed.
755/// This will be called at the end of parsing a class declaration
756/// for each LateParsedAttribute. We consume the saved tokens and
757/// create an attribute with the arguments filled in. We add this
758/// to the Attribute list for the decl.
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000759void Parser::ParseLexedAttribute(LateParsedAttribute &LA,
760 bool EnterScope, bool OnDefinition) {
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000761 // Save the current token position.
762 SourceLocation OrigLoc = Tok.getLocation();
763
764 // Append the current token at the end of the new token stream so that it
765 // doesn't get lost.
766 LA.Toks.push_back(Tok);
767 PP.EnterTokenStream(LA.Toks.data(), LA.Toks.size(), true, false);
768 // Consume the previously pushed token.
769 ConsumeAnyToken();
770
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000771 if (OnDefinition && !IsThreadSafetyAttribute(LA.AttrName.getName())) {
772 Diag(Tok, diag::warn_attribute_on_function_definition)
773 << LA.AttrName.getName();
774 }
775
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000776 ParsedAttributes Attrs(AttrFactory);
777 SourceLocation endLoc;
778
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +0000779 if (LA.Decls.size() == 1) {
780 Decl *D = LA.Decls[0];
Caitlin Sadowskied9d84a2011-09-08 17:42:31 +0000781
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +0000782 // If the Decl is templatized, add template parameters to scope.
783 bool HasTemplateScope = EnterScope && D->isTemplateDecl();
784 ParseScope TempScope(this, Scope::TemplateParamScope, HasTemplateScope);
785 if (HasTemplateScope)
786 Actions.ActOnReenterTemplateScope(Actions.CurScope, D);
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000787
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +0000788 // If the Decl is on a function, add function parameters to the scope.
789 bool HasFunctionScope = EnterScope && D->isFunctionOrFunctionTemplate();
790 ParseScope FnScope(this, Scope::FnScope|Scope::DeclScope, HasFunctionScope);
791 if (HasFunctionScope)
792 Actions.ActOnReenterFunctionContext(Actions.CurScope, D);
793
794 ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, &endLoc);
795
796 if (HasFunctionScope) {
797 Actions.ActOnExitFunctionContext();
798 FnScope.Exit(); // Pop scope, and remove Decls from IdResolver
799 }
800 if (HasTemplateScope) {
801 TempScope.Exit();
802 }
DeLesley Hutchins7ec419a2012-03-02 22:29:50 +0000803 } else if (LA.Decls.size() > 0) {
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +0000804 // If there are multiple decls, then the decl cannot be within the
805 // function scope.
806 ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, &endLoc);
DeLesley Hutchins7ec419a2012-03-02 22:29:50 +0000807 } else {
808 Diag(Tok, diag::warn_attribute_no_decl) << LA.AttrName.getName();
Caitlin Sadowskied9d84a2011-09-08 17:42:31 +0000809 }
810
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +0000811 for (unsigned i = 0, ni = LA.Decls.size(); i < ni; ++i) {
812 Actions.ActOnFinishDelayedAttribute(getCurScope(), LA.Decls[i], Attrs);
813 }
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000814
815 if (Tok.getLocation() != OrigLoc) {
816 // Due to a parsing error, we either went over the cached tokens or
817 // there are still cached tokens left, so we skip the leftover tokens.
818 // Since this is an uncommon situation that should be avoided, use the
819 // expensive isBeforeInTranslationUnit call.
820 if (PP.getSourceManager().isBeforeInTranslationUnit(Tok.getLocation(),
821 OrigLoc))
822 while (Tok.getLocation() != OrigLoc && Tok.isNot(tok::eof))
Douglas Gregord78ef5b2012-03-08 01:00:17 +0000823 ConsumeAnyToken();
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000824 }
825}
826
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000827/// \brief Wrapper around a case statement checking if AttrName is
828/// one of the thread safety attributes
829bool Parser::IsThreadSafetyAttribute(llvm::StringRef AttrName){
830 return llvm::StringSwitch<bool>(AttrName)
831 .Case("guarded_by", true)
832 .Case("guarded_var", true)
833 .Case("pt_guarded_by", true)
834 .Case("pt_guarded_var", true)
835 .Case("lockable", true)
836 .Case("scoped_lockable", true)
837 .Case("no_thread_safety_analysis", true)
838 .Case("acquired_after", true)
839 .Case("acquired_before", true)
840 .Case("exclusive_lock_function", true)
841 .Case("shared_lock_function", true)
842 .Case("exclusive_trylock_function", true)
843 .Case("shared_trylock_function", true)
844 .Case("unlock_function", true)
845 .Case("lock_returned", true)
846 .Case("locks_excluded", true)
847 .Case("exclusive_locks_required", true)
848 .Case("shared_locks_required", true)
849 .Default(false);
850}
851
852/// \brief Parse the contents of thread safety attributes. These
853/// should always be parsed as an expression list.
854///
855/// We need to special case the parsing due to the fact that if the first token
856/// of the first argument is an identifier, the main parse loop will store
857/// that token as a "parameter" and the rest of
858/// the arguments will be added to a list of "arguments". However,
859/// subsequent tokens in the first argument are lost. We instead parse each
860/// argument as an expression and add all arguments to the list of "arguments".
861/// In future, we will take advantage of this special case to also
862/// deal with some argument scoping issues here (for example, referring to a
863/// function parameter in the attribute on that function).
864void Parser::ParseThreadSafetyAttribute(IdentifierInfo &AttrName,
865 SourceLocation AttrNameLoc,
866 ParsedAttributes &Attrs,
867 SourceLocation *EndLoc) {
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000868 assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('");
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000869
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000870 BalancedDelimiterTracker T(*this, tok::l_paren);
871 T.consumeOpen();
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000872
873 ExprVector ArgExprs(Actions);
874 bool ArgExprsOk = true;
875
876 // now parse the list of expressions
DeLesley Hutchins4805f152011-12-14 19:36:06 +0000877 while (Tok.isNot(tok::r_paren)) {
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000878 ExprResult ArgExpr(ParseAssignmentExpression());
879 if (ArgExpr.isInvalid()) {
880 ArgExprsOk = false;
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000881 T.consumeClose();
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000882 break;
883 } else {
884 ArgExprs.push_back(ArgExpr.release());
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000885 }
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000886 if (Tok.isNot(tok::comma))
887 break;
888 ConsumeToken(); // Eat the comma, move to the next argument
889 }
890 // Match the ')'.
DeLesley Hutchins23323e02012-01-20 22:50:54 +0000891 if (ArgExprsOk && !T.consumeClose()) {
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000892 Attrs.addNew(&AttrName, AttrNameLoc, 0, AttrNameLoc, 0, SourceLocation(),
893 ArgExprs.take(), ArgExprs.size());
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000894 }
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000895 if (EndLoc)
896 *EndLoc = T.getCloseLocation();
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000897}
898
John McCall7f040a92010-12-24 02:08:15 +0000899void Parser::DiagnoseProhibitedAttributes(ParsedAttributesWithRange &attrs) {
900 Diag(attrs.Range.getBegin(), diag::err_attributes_not_allowed)
901 << attrs.Range;
Dawn Perchik52fc3142010-09-03 01:29:35 +0000902}
903
Reid Spencer5f016e22007-07-11 17:01:13 +0000904/// ParseDeclaration - Parse a full 'declaration', which consists of
905/// declaration-specifiers, some number of declarators, and a semicolon.
Chris Lattner97144fc2009-04-02 04:16:50 +0000906/// 'Context' should be a Declarator::TheContext value. This returns the
907/// location of the semicolon in DeclEnd.
Chris Lattner8f08cb72007-08-25 06:57:03 +0000908///
909/// declaration: [C99 6.7]
910/// block-declaration ->
911/// simple-declaration
912/// others [FIXME]
Douglas Gregoradcac882008-12-01 23:54:00 +0000913/// [C++] template-declaration
Chris Lattner8f08cb72007-08-25 06:57:03 +0000914/// [C++] namespace-definition
Douglas Gregorf780abc2008-12-30 03:27:21 +0000915/// [C++] using-directive
Douglas Gregord7f37bf2009-06-22 23:06:13 +0000916/// [C++] using-declaration
Benjamin Kramerffbe9b92011-12-23 17:00:35 +0000917/// [C++0x/C11] static_assert-declaration
Chris Lattner8f08cb72007-08-25 06:57:03 +0000918/// others... [FIXME]
919///
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +0000920Parser::DeclGroupPtrTy Parser::ParseDeclaration(StmtVector &Stmts,
921 unsigned Context,
Sean Huntbbd37c62009-11-21 08:43:09 +0000922 SourceLocation &DeclEnd,
John McCall7f040a92010-12-24 02:08:15 +0000923 ParsedAttributesWithRange &attrs) {
Argyrios Kyrtzidis36d36802010-06-17 10:52:18 +0000924 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Fariborz Jahaniane8cff362011-08-30 17:10:52 +0000925 // Must temporarily exit the objective-c container scope for
926 // parsing c none objective-c decls.
927 ObjCDeclContextSwitch ObjCDC(*this);
Argyrios Kyrtzidis36d36802010-06-17 10:52:18 +0000928
John McCalld226f652010-08-21 09:40:31 +0000929 Decl *SingleDecl = 0;
Richard Smithc89edf52011-07-01 19:46:12 +0000930 Decl *OwnedType = 0;
Chris Lattner8f08cb72007-08-25 06:57:03 +0000931 switch (Tok.getKind()) {
Douglas Gregoradcac882008-12-01 23:54:00 +0000932 case tok::kw_template:
Douglas Gregor1426e532009-05-12 21:31:51 +0000933 case tok::kw_export:
John McCall7f040a92010-12-24 02:08:15 +0000934 ProhibitAttributes(attrs);
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000935 SingleDecl = ParseDeclarationStartingWithTemplate(Context, DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +0000936 break;
Sebastian Redld078e642010-08-27 23:12:46 +0000937 case tok::kw_inline:
Sebastian Redl88e64ca2010-08-31 00:36:45 +0000938 // Could be the start of an inline namespace. Allowed as an ext in C++03.
David Blaikie4e4d0842012-03-11 07:00:24 +0000939 if (getLangOpts().CPlusPlus && NextToken().is(tok::kw_namespace)) {
John McCall7f040a92010-12-24 02:08:15 +0000940 ProhibitAttributes(attrs);
Sebastian Redld078e642010-08-27 23:12:46 +0000941 SourceLocation InlineLoc = ConsumeToken();
942 SingleDecl = ParseNamespace(Context, DeclEnd, InlineLoc);
943 break;
944 }
John McCall7f040a92010-12-24 02:08:15 +0000945 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs,
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +0000946 true);
Chris Lattner8f08cb72007-08-25 06:57:03 +0000947 case tok::kw_namespace:
John McCall7f040a92010-12-24 02:08:15 +0000948 ProhibitAttributes(attrs);
Chris Lattner97144fc2009-04-02 04:16:50 +0000949 SingleDecl = ParseNamespace(Context, DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +0000950 break;
Douglas Gregorf780abc2008-12-30 03:27:21 +0000951 case tok::kw_using:
John McCall78b81052010-11-10 02:40:36 +0000952 SingleDecl = ParseUsingDirectiveOrDeclaration(Context, ParsedTemplateInfo(),
Richard Smithc89edf52011-07-01 19:46:12 +0000953 DeclEnd, attrs, &OwnedType);
Chris Lattner682bf922009-03-29 16:50:03 +0000954 break;
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000955 case tok::kw_static_assert:
Peter Collingbournec6eb44b2011-04-15 00:35:57 +0000956 case tok::kw__Static_assert:
John McCall7f040a92010-12-24 02:08:15 +0000957 ProhibitAttributes(attrs);
Chris Lattner97144fc2009-04-02 04:16:50 +0000958 SingleDecl = ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +0000959 break;
Chris Lattner8f08cb72007-08-25 06:57:03 +0000960 default:
John McCall7f040a92010-12-24 02:08:15 +0000961 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs, true);
Chris Lattner8f08cb72007-08-25 06:57:03 +0000962 }
Sean Huntbbd37c62009-11-21 08:43:09 +0000963
Chris Lattner682bf922009-03-29 16:50:03 +0000964 // This routine returns a DeclGroup, if the thing we parsed only contains a
Richard Smithc89edf52011-07-01 19:46:12 +0000965 // single decl, convert it now. Alias declarations can also declare a type;
966 // include that too if it is present.
967 return Actions.ConvertDeclToDeclGroup(SingleDecl, OwnedType);
Chris Lattner8f08cb72007-08-25 06:57:03 +0000968}
969
970/// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl]
971/// declaration-specifiers init-declarator-list[opt] ';'
972///[C90/C++]init-declarator-list ';' [TODO]
973/// [OMP] threadprivate-directive [TODO]
Chris Lattnercd147752009-03-29 17:27:48 +0000974///
Richard Smithad762fc2011-04-14 22:09:26 +0000975/// for-range-declaration: [C++0x 6.5p1: stmt.ranged]
976/// attribute-specifier-seq[opt] type-specifier-seq declarator
977///
Chris Lattnercd147752009-03-29 17:27:48 +0000978/// If RequireSemi is false, this does not check for a ';' at the end of the
Chris Lattner5c5db552010-04-05 18:18:31 +0000979/// declaration. If it is true, it checks for and eats it.
Richard Smithad762fc2011-04-14 22:09:26 +0000980///
981/// If FRI is non-null, we might be parsing a for-range-declaration instead
982/// of a simple-declaration. If we find that we are, we also parse the
983/// for-range-initializer, and place it here.
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +0000984Parser::DeclGroupPtrTy Parser::ParseSimpleDeclaration(StmtVector &Stmts,
985 unsigned Context,
Sean Huntbbd37c62009-11-21 08:43:09 +0000986 SourceLocation &DeclEnd,
John McCall7f040a92010-12-24 02:08:15 +0000987 ParsedAttributes &attrs,
Richard Smithad762fc2011-04-14 22:09:26 +0000988 bool RequireSemi,
989 ForRangeInit *FRI) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000990 // Parse the common declaration-specifiers piece.
John McCall54abf7d2009-11-04 02:18:39 +0000991 ParsingDeclSpec DS(*this);
John McCall7f040a92010-12-24 02:08:15 +0000992 DS.takeAttributesFrom(attrs);
Douglas Gregor312eadb2011-04-24 05:37:28 +0000993
Douglas Gregor0efc2c12010-01-13 17:31:36 +0000994 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS_none,
Richard Smith34b41d92011-02-20 03:19:35 +0000995 getDeclSpecContextFromDeclaratorContext(Context));
Abramo Bagnara06284c12012-01-07 10:52:36 +0000996
Reid Spencer5f016e22007-07-11 17:01:13 +0000997 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
998 // declaration-specifiers init-declarator-list[opt] ';'
Chris Lattner04d66662007-10-09 17:33:22 +0000999 if (Tok.is(tok::semi)) {
Chris Lattner5c5db552010-04-05 18:18:31 +00001000 if (RequireSemi) ConsumeToken();
John McCalld226f652010-08-21 09:40:31 +00001001 Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none,
Douglas Gregor312eadb2011-04-24 05:37:28 +00001002 DS);
John McCall54abf7d2009-11-04 02:18:39 +00001003 DS.complete(TheDecl);
Chris Lattner682bf922009-03-29 16:50:03 +00001004 return Actions.ConvertDeclToDeclGroup(TheDecl);
Reid Spencer5f016e22007-07-11 17:01:13 +00001005 }
Douglas Gregor312eadb2011-04-24 05:37:28 +00001006
1007 return ParseDeclGroup(DS, Context, /*FunctionDefs=*/ false, &DeclEnd, FRI);
John McCalld8ac0572009-11-03 19:26:08 +00001008}
Mike Stump1eb44332009-09-09 15:08:12 +00001009
Richard Smith0706df42011-10-19 21:33:05 +00001010/// Returns true if this might be the start of a declarator, or a common typo
1011/// for a declarator.
1012bool Parser::MightBeDeclarator(unsigned Context) {
1013 switch (Tok.getKind()) {
1014 case tok::annot_cxxscope:
1015 case tok::annot_template_id:
1016 case tok::caret:
1017 case tok::code_completion:
1018 case tok::coloncolon:
1019 case tok::ellipsis:
1020 case tok::kw___attribute:
1021 case tok::kw_operator:
1022 case tok::l_paren:
1023 case tok::star:
1024 return true;
1025
1026 case tok::amp:
1027 case tok::ampamp:
David Blaikie4e4d0842012-03-11 07:00:24 +00001028 return getLangOpts().CPlusPlus;
Richard Smith0706df42011-10-19 21:33:05 +00001029
Richard Smith1c94c162012-01-09 22:31:44 +00001030 case tok::l_square: // Might be an attribute on an unnamed bit-field.
David Blaikie4e4d0842012-03-11 07:00:24 +00001031 return Context == Declarator::MemberContext && getLangOpts().CPlusPlus0x &&
Richard Smith1c94c162012-01-09 22:31:44 +00001032 NextToken().is(tok::l_square);
1033
1034 case tok::colon: // Might be a typo for '::' or an unnamed bit-field.
David Blaikie4e4d0842012-03-11 07:00:24 +00001035 return Context == Declarator::MemberContext || getLangOpts().CPlusPlus;
Richard Smith1c94c162012-01-09 22:31:44 +00001036
Richard Smith0706df42011-10-19 21:33:05 +00001037 case tok::identifier:
1038 switch (NextToken().getKind()) {
1039 case tok::code_completion:
1040 case tok::coloncolon:
1041 case tok::comma:
1042 case tok::equal:
1043 case tok::equalequal: // Might be a typo for '='.
1044 case tok::kw_alignas:
1045 case tok::kw_asm:
1046 case tok::kw___attribute:
1047 case tok::l_brace:
1048 case tok::l_paren:
1049 case tok::l_square:
1050 case tok::less:
1051 case tok::r_brace:
1052 case tok::r_paren:
1053 case tok::r_square:
1054 case tok::semi:
1055 return true;
1056
1057 case tok::colon:
1058 // At namespace scope, 'identifier:' is probably a typo for 'identifier::'
Richard Smith1c94c162012-01-09 22:31:44 +00001059 // and in block scope it's probably a label. Inside a class definition,
1060 // this is a bit-field.
1061 return Context == Declarator::MemberContext ||
David Blaikie4e4d0842012-03-11 07:00:24 +00001062 (getLangOpts().CPlusPlus && Context == Declarator::FileContext);
Richard Smith1c94c162012-01-09 22:31:44 +00001063
1064 case tok::identifier: // Possible virt-specifier.
David Blaikie4e4d0842012-03-11 07:00:24 +00001065 return getLangOpts().CPlusPlus0x && isCXX0XVirtSpecifier(NextToken());
Richard Smith0706df42011-10-19 21:33:05 +00001066
1067 default:
1068 return false;
1069 }
1070
1071 default:
1072 return false;
1073 }
1074}
1075
John McCalld8ac0572009-11-03 19:26:08 +00001076/// ParseDeclGroup - Having concluded that this is either a function
1077/// definition or a group of object declarations, actually parse the
1078/// result.
John McCall54abf7d2009-11-04 02:18:39 +00001079Parser::DeclGroupPtrTy Parser::ParseDeclGroup(ParsingDeclSpec &DS,
1080 unsigned Context,
John McCalld8ac0572009-11-03 19:26:08 +00001081 bool AllowFunctionDefinitions,
Richard Smithad762fc2011-04-14 22:09:26 +00001082 SourceLocation *DeclEnd,
1083 ForRangeInit *FRI) {
John McCalld8ac0572009-11-03 19:26:08 +00001084 // Parse the first declarator.
John McCall54abf7d2009-11-04 02:18:39 +00001085 ParsingDeclarator D(*this, DS, static_cast<Declarator::TheContext>(Context));
John McCalld8ac0572009-11-03 19:26:08 +00001086 ParseDeclarator(D);
Chris Lattnercd147752009-03-29 17:27:48 +00001087
John McCalld8ac0572009-11-03 19:26:08 +00001088 // Bail out if the first declarator didn't seem well-formed.
1089 if (!D.hasName() && !D.mayOmitIdentifier()) {
1090 // Skip until ; or }.
1091 SkipUntil(tok::r_brace, true, true);
1092 if (Tok.is(tok::semi))
1093 ConsumeToken();
1094 return DeclGroupPtrTy();
Chris Lattner23c4b182009-03-29 17:18:04 +00001095 }
Mike Stump1eb44332009-09-09 15:08:12 +00001096
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001097 // Save late-parsed attributes for now; they need to be parsed in the
1098 // appropriate function scope after the function Decl has been constructed.
1099 LateParsedAttrList LateParsedAttrs;
1100 if (D.isFunctionDeclarator())
1101 MaybeParseGNUAttributes(D, &LateParsedAttrs);
1102
Chris Lattnerc82daef2010-07-11 22:24:20 +00001103 // Check to see if we have a function *definition* which must have a body.
1104 if (AllowFunctionDefinitions && D.isFunctionDeclarator() &&
1105 // Look at the next token to make sure that this isn't a function
1106 // declaration. We have to check this because __attribute__ might be the
1107 // start of a function definition in GCC-extended K&R C.
1108 !isDeclarationAfterDeclarator()) {
Richard Smith58196dc2011-11-30 23:45:35 +00001109
Chris Lattner004659a2010-07-11 22:42:07 +00001110 if (isStartOfFunctionDefinition(D)) {
John McCalld8ac0572009-11-03 19:26:08 +00001111 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
1112 Diag(Tok, diag::err_function_declared_typedef);
1113
1114 // Recover by treating the 'typedef' as spurious.
1115 DS.ClearStorageClassSpecs();
1116 }
1117
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001118 Decl *TheDecl =
1119 ParseFunctionDefinition(D, ParsedTemplateInfo(), &LateParsedAttrs);
John McCalld8ac0572009-11-03 19:26:08 +00001120 return Actions.ConvertDeclToDeclGroup(TheDecl);
Chris Lattner004659a2010-07-11 22:42:07 +00001121 }
1122
1123 if (isDeclarationSpecifier()) {
1124 // If there is an invalid declaration specifier right after the function
1125 // prototype, then we must be in a missing semicolon case where this isn't
1126 // actually a body. Just fall through into the code that handles it as a
1127 // prototype, and let the top-level code handle the erroneous declspec
1128 // where it would otherwise expect a comma or semicolon.
John McCalld8ac0572009-11-03 19:26:08 +00001129 } else {
1130 Diag(Tok, diag::err_expected_fn_body);
1131 SkipUntil(tok::semi);
1132 return DeclGroupPtrTy();
1133 }
1134 }
1135
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001136 if (ParseAsmAttributesAfterDeclarator(D))
Richard Smithad762fc2011-04-14 22:09:26 +00001137 return DeclGroupPtrTy();
1138
1139 // C++0x [stmt.iter]p1: Check if we have a for-range-declarator. If so, we
1140 // must parse and analyze the for-range-initializer before the declaration is
1141 // analyzed.
1142 if (FRI && Tok.is(tok::colon)) {
1143 FRI->ColonLoc = ConsumeToken();
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001144 if (Tok.is(tok::l_brace))
1145 FRI->RangeExpr = ParseBraceInitializer();
1146 else
1147 FRI->RangeExpr = ParseExpression();
Richard Smithad762fc2011-04-14 22:09:26 +00001148 Decl *ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
1149 Actions.ActOnCXXForRangeDecl(ThisDecl);
1150 Actions.FinalizeDeclaration(ThisDecl);
John McCall6895a642012-01-27 01:29:43 +00001151 D.complete(ThisDecl);
Richard Smithad762fc2011-04-14 22:09:26 +00001152 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS, &ThisDecl, 1);
1153 }
1154
Chris Lattner5f9e2722011-07-23 10:55:15 +00001155 SmallVector<Decl *, 8> DeclsInGroup;
Richard Smithad762fc2011-04-14 22:09:26 +00001156 Decl *FirstDecl = ParseDeclarationAfterDeclaratorAndAttributes(D);
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001157 if (LateParsedAttrs.size() > 0)
1158 ParseLexedAttributeList(LateParsedAttrs, FirstDecl, true, false);
John McCall54abf7d2009-11-04 02:18:39 +00001159 D.complete(FirstDecl);
John McCalld226f652010-08-21 09:40:31 +00001160 if (FirstDecl)
John McCalld8ac0572009-11-03 19:26:08 +00001161 DeclsInGroup.push_back(FirstDecl);
1162
Richard Smith0706df42011-10-19 21:33:05 +00001163 bool ExpectSemi = Context != Declarator::ForContext;
1164
John McCalld8ac0572009-11-03 19:26:08 +00001165 // If we don't have a comma, it is either the end of the list (a ';') or an
1166 // error, bail out.
1167 while (Tok.is(tok::comma)) {
Richard Smith0706df42011-10-19 21:33:05 +00001168 SourceLocation CommaLoc = ConsumeToken();
1169
1170 if (Tok.isAtStartOfLine() && ExpectSemi && !MightBeDeclarator(Context)) {
1171 // This comma was followed by a line-break and something which can't be
1172 // the start of a declarator. The comma was probably a typo for a
1173 // semicolon.
1174 Diag(CommaLoc, diag::err_expected_semi_declaration)
1175 << FixItHint::CreateReplacement(CommaLoc, ";");
1176 ExpectSemi = false;
1177 break;
1178 }
John McCalld8ac0572009-11-03 19:26:08 +00001179
1180 // Parse the next declarator.
1181 D.clear();
Richard Smith7984de32012-01-12 23:53:29 +00001182 D.setCommaLoc(CommaLoc);
John McCalld8ac0572009-11-03 19:26:08 +00001183
1184 // Accept attributes in an init-declarator. In the first declarator in a
1185 // declaration, these would be part of the declspec. In subsequent
1186 // declarators, they become part of the declarator itself, so that they
1187 // don't apply to declarators after *this* one. Examples:
1188 // short __attribute__((common)) var; -> declspec
1189 // short var __attribute__((common)); -> declarator
1190 // short x, __attribute__((common)) var; -> declarator
John McCall7f040a92010-12-24 02:08:15 +00001191 MaybeParseGNUAttributes(D);
John McCalld8ac0572009-11-03 19:26:08 +00001192
1193 ParseDeclarator(D);
Fariborz Jahanian9baf39d2012-01-13 00:14:12 +00001194 if (!D.isInvalidType()) {
1195 Decl *ThisDecl = ParseDeclarationAfterDeclarator(D);
1196 D.complete(ThisDecl);
1197 if (ThisDecl)
1198 DeclsInGroup.push_back(ThisDecl);
1199 }
John McCalld8ac0572009-11-03 19:26:08 +00001200 }
1201
1202 if (DeclEnd)
1203 *DeclEnd = Tok.getLocation();
1204
Richard Smith0706df42011-10-19 21:33:05 +00001205 if (ExpectSemi &&
John McCalld8ac0572009-11-03 19:26:08 +00001206 ExpectAndConsume(tok::semi,
1207 Context == Declarator::FileContext
1208 ? diag::err_invalid_token_after_toplevel_declarator
1209 : diag::err_expected_semi_declaration)) {
Chris Lattner004659a2010-07-11 22:42:07 +00001210 // Okay, there was no semicolon and one was expected. If we see a
1211 // declaration specifier, just assume it was missing and continue parsing.
1212 // Otherwise things are very confused and we skip to recover.
1213 if (!isDeclarationSpecifier()) {
1214 SkipUntil(tok::r_brace, true, true);
1215 if (Tok.is(tok::semi))
1216 ConsumeToken();
1217 }
John McCalld8ac0572009-11-03 19:26:08 +00001218 }
1219
Douglas Gregor23c94db2010-07-02 17:43:08 +00001220 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS,
John McCalld8ac0572009-11-03 19:26:08 +00001221 DeclsInGroup.data(),
1222 DeclsInGroup.size());
Reid Spencer5f016e22007-07-11 17:01:13 +00001223}
1224
Richard Smithad762fc2011-04-14 22:09:26 +00001225/// Parse an optional simple-asm-expr and attributes, and attach them to a
1226/// declarator. Returns true on an error.
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001227bool Parser::ParseAsmAttributesAfterDeclarator(Declarator &D) {
Richard Smithad762fc2011-04-14 22:09:26 +00001228 // If a simple-asm-expr is present, parse it.
1229 if (Tok.is(tok::kw_asm)) {
1230 SourceLocation Loc;
1231 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
1232 if (AsmLabel.isInvalid()) {
1233 SkipUntil(tok::semi, true, true);
1234 return true;
1235 }
1236
1237 D.setAsmLabel(AsmLabel.release());
1238 D.SetRangeEnd(Loc);
1239 }
1240
1241 MaybeParseGNUAttributes(D);
1242 return false;
1243}
1244
Douglas Gregor1426e532009-05-12 21:31:51 +00001245/// \brief Parse 'declaration' after parsing 'declaration-specifiers
1246/// declarator'. This method parses the remainder of the declaration
1247/// (including any attributes or initializer, among other things) and
1248/// finalizes the declaration.
Reid Spencer5f016e22007-07-11 17:01:13 +00001249///
Reid Spencer5f016e22007-07-11 17:01:13 +00001250/// init-declarator: [C99 6.7]
1251/// declarator
1252/// declarator '=' initializer
1253/// [GNU] declarator simple-asm-expr[opt] attributes[opt]
1254/// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00001255/// [C++] declarator initializer[opt]
1256///
1257/// [C++] initializer:
1258/// [C++] '=' initializer-clause
1259/// [C++] '(' expression-list ')'
Sebastian Redl50de12f2009-03-24 22:27:57 +00001260/// [C++0x] '=' 'default' [TODO]
1261/// [C++0x] '=' 'delete'
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001262/// [C++0x] braced-init-list
Sebastian Redl50de12f2009-03-24 22:27:57 +00001263///
1264/// According to the standard grammar, =default and =delete are function
1265/// definitions, but that definitely doesn't fit with the parser here.
Reid Spencer5f016e22007-07-11 17:01:13 +00001266///
John McCalld226f652010-08-21 09:40:31 +00001267Decl *Parser::ParseDeclarationAfterDeclarator(Declarator &D,
Douglas Gregore542c862009-06-23 23:11:28 +00001268 const ParsedTemplateInfo &TemplateInfo) {
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001269 if (ParseAsmAttributesAfterDeclarator(D))
Richard Smithad762fc2011-04-14 22:09:26 +00001270 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001271
Richard Smithad762fc2011-04-14 22:09:26 +00001272 return ParseDeclarationAfterDeclaratorAndAttributes(D, TemplateInfo);
1273}
Mike Stump1eb44332009-09-09 15:08:12 +00001274
Richard Smithad762fc2011-04-14 22:09:26 +00001275Decl *Parser::ParseDeclarationAfterDeclaratorAndAttributes(Declarator &D,
1276 const ParsedTemplateInfo &TemplateInfo) {
Douglas Gregor1426e532009-05-12 21:31:51 +00001277 // Inform the current actions module that we just parsed this declarator.
John McCalld226f652010-08-21 09:40:31 +00001278 Decl *ThisDecl = 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +00001279 switch (TemplateInfo.Kind) {
1280 case ParsedTemplateInfo::NonTemplate:
Douglas Gregor23c94db2010-07-02 17:43:08 +00001281 ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
Douglas Gregord5a423b2009-09-25 18:43:00 +00001282 break;
1283
1284 case ParsedTemplateInfo::Template:
1285 case ParsedTemplateInfo::ExplicitSpecialization:
Douglas Gregor23c94db2010-07-02 17:43:08 +00001286 ThisDecl = Actions.ActOnTemplateDeclarator(getCurScope(),
John McCallf312b1e2010-08-26 23:41:50 +00001287 MultiTemplateParamsArg(Actions,
Douglas Gregore542c862009-06-23 23:11:28 +00001288 TemplateInfo.TemplateParams->data(),
1289 TemplateInfo.TemplateParams->size()),
Douglas Gregord5a423b2009-09-25 18:43:00 +00001290 D);
1291 break;
1292
1293 case ParsedTemplateInfo::ExplicitInstantiation: {
John McCalld226f652010-08-21 09:40:31 +00001294 DeclResult ThisRes
Douglas Gregor23c94db2010-07-02 17:43:08 +00001295 = Actions.ActOnExplicitInstantiation(getCurScope(),
Douglas Gregord5a423b2009-09-25 18:43:00 +00001296 TemplateInfo.ExternLoc,
1297 TemplateInfo.TemplateLoc,
1298 D);
1299 if (ThisRes.isInvalid()) {
1300 SkipUntil(tok::semi, true, true);
John McCalld226f652010-08-21 09:40:31 +00001301 return 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +00001302 }
1303
1304 ThisDecl = ThisRes.get();
1305 break;
1306 }
1307 }
Mike Stump1eb44332009-09-09 15:08:12 +00001308
Richard Smith34b41d92011-02-20 03:19:35 +00001309 bool TypeContainsAuto =
1310 D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto;
1311
Douglas Gregor1426e532009-05-12 21:31:51 +00001312 // Parse declarator '=' initializer.
Richard Trieud6c7c672012-01-18 22:54:52 +00001313 // If a '==' or '+=' is found, suggest a fixit to '='.
Richard Trieufcaf27e2012-01-19 22:01:51 +00001314 if (isTokenEqualOrEqualTypo()) {
Douglas Gregor1426e532009-05-12 21:31:51 +00001315 ConsumeToken();
Anders Carlsson37bf9d22010-09-24 21:25:25 +00001316 if (Tok.is(tok::kw_delete)) {
Sean Hunte4246a62011-05-12 06:15:49 +00001317 if (D.isFunctionDeclarator())
1318 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
1319 << 1 /* delete */;
1320 else
1321 Diag(ConsumeToken(), diag::err_deleted_non_function);
Sean Huntfe2695e2011-05-06 01:42:00 +00001322 } else if (Tok.is(tok::kw_default)) {
Sean Hunte4246a62011-05-12 06:15:49 +00001323 if (D.isFunctionDeclarator())
Sebastian Redlecfcd562012-02-11 23:51:21 +00001324 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
1325 << 0 /* default */;
Sean Hunte4246a62011-05-12 06:15:49 +00001326 else
1327 Diag(ConsumeToken(), diag::err_default_special_members);
Douglas Gregor1426e532009-05-12 21:31:51 +00001328 } else {
David Blaikie4e4d0842012-03-11 07:00:24 +00001329 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
John McCall731ad842009-12-19 09:28:58 +00001330 EnterScope(0);
Douglas Gregor23c94db2010-07-02 17:43:08 +00001331 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
John McCall731ad842009-12-19 09:28:58 +00001332 }
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +00001333
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +00001334 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001335 Actions.CodeCompleteInitializer(getCurScope(), ThisDecl);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001336 cutOffParsing();
1337 return 0;
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +00001338 }
1339
John McCall60d7b3a2010-08-24 06:29:42 +00001340 ExprResult Init(ParseInitializer());
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +00001341
David Blaikie4e4d0842012-03-11 07:00:24 +00001342 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001343 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
John McCall731ad842009-12-19 09:28:58 +00001344 ExitScope();
1345 }
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +00001346
Douglas Gregor1426e532009-05-12 21:31:51 +00001347 if (Init.isInvalid()) {
Douglas Gregor00225542010-03-01 18:27:54 +00001348 SkipUntil(tok::comma, true, true);
1349 Actions.ActOnInitializerError(ThisDecl);
1350 } else
Richard Smith34b41d92011-02-20 03:19:35 +00001351 Actions.AddInitializerToDecl(ThisDecl, Init.take(),
1352 /*DirectInit=*/false, TypeContainsAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +00001353 }
1354 } else if (Tok.is(tok::l_paren)) {
1355 // Parse C++ direct initializer: '(' expression-list ')'
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001356 BalancedDelimiterTracker T(*this, tok::l_paren);
1357 T.consumeOpen();
1358
Douglas Gregor1426e532009-05-12 21:31:51 +00001359 ExprVector Exprs(Actions);
1360 CommaLocsTy CommaLocs;
1361
David Blaikie4e4d0842012-03-11 07:00:24 +00001362 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregorb4debae2009-12-22 17:47:17 +00001363 EnterScope(0);
Douglas Gregor23c94db2010-07-02 17:43:08 +00001364 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001365 }
1366
Douglas Gregor1426e532009-05-12 21:31:51 +00001367 if (ParseExpressionList(Exprs, CommaLocs)) {
1368 SkipUntil(tok::r_paren);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001369
David Blaikie4e4d0842012-03-11 07:00:24 +00001370 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001371 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001372 ExitScope();
1373 }
Douglas Gregor1426e532009-05-12 21:31:51 +00001374 } else {
1375 // Match the ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001376 T.consumeClose();
Douglas Gregor1426e532009-05-12 21:31:51 +00001377
1378 assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() &&
1379 "Unexpected number of commas!");
Douglas Gregorb4debae2009-12-22 17:47:17 +00001380
David Blaikie4e4d0842012-03-11 07:00:24 +00001381 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001382 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001383 ExitScope();
1384 }
1385
Sebastian Redl5b9cc5d2012-02-11 23:51:47 +00001386 ExprResult Initializer = Actions.ActOnParenListExpr(T.getOpenLocation(),
1387 T.getCloseLocation(),
1388 move_arg(Exprs));
1389 Actions.AddInitializerToDecl(ThisDecl, Initializer.take(),
1390 /*DirectInit=*/true, TypeContainsAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +00001391 }
David Blaikie4e4d0842012-03-11 07:00:24 +00001392 } else if (getLangOpts().CPlusPlus0x && Tok.is(tok::l_brace)) {
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001393 // Parse C++0x braced-init-list.
Richard Smith7fe62082011-10-15 05:09:34 +00001394 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
1395
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001396 if (D.getCXXScopeSpec().isSet()) {
1397 EnterScope(0);
1398 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
1399 }
1400
1401 ExprResult Init(ParseBraceInitializer());
1402
1403 if (D.getCXXScopeSpec().isSet()) {
1404 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
1405 ExitScope();
1406 }
1407
1408 if (Init.isInvalid()) {
1409 Actions.ActOnInitializerError(ThisDecl);
1410 } else
1411 Actions.AddInitializerToDecl(ThisDecl, Init.take(),
1412 /*DirectInit=*/true, TypeContainsAuto);
1413
Douglas Gregor1426e532009-05-12 21:31:51 +00001414 } else {
Richard Smith34b41d92011-02-20 03:19:35 +00001415 Actions.ActOnUninitializedDecl(ThisDecl, TypeContainsAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +00001416 }
1417
Richard Smith483b9f32011-02-21 20:05:19 +00001418 Actions.FinalizeDeclaration(ThisDecl);
1419
Douglas Gregor1426e532009-05-12 21:31:51 +00001420 return ThisDecl;
1421}
1422
Reid Spencer5f016e22007-07-11 17:01:13 +00001423/// ParseSpecifierQualifierList
1424/// specifier-qualifier-list:
1425/// type-specifier specifier-qualifier-list[opt]
1426/// type-qualifier specifier-qualifier-list[opt]
1427/// [GNU] attributes specifier-qualifier-list[opt]
1428///
Richard Smith69730c12012-03-12 07:56:15 +00001429void Parser::ParseSpecifierQualifierList(DeclSpec &DS, AccessSpecifier AS,
1430 DeclSpecContext DSC) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001431 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
1432 /// parse declaration-specifiers and complain about extra stuff.
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001433 /// TODO: diagnose attribute-specifiers and alignment-specifiers.
Richard Smith69730c12012-03-12 07:56:15 +00001434 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS, DSC);
Mike Stump1eb44332009-09-09 15:08:12 +00001435
Reid Spencer5f016e22007-07-11 17:01:13 +00001436 // Validate declspec for type-name.
1437 unsigned Specs = DS.getParsedSpecifiers();
Richard Smith69730c12012-03-12 07:56:15 +00001438 if (DSC == DSC_type_specifier && !DS.hasTypeSpecifier()) {
1439 Diag(Tok, diag::err_expected_type);
1440 DS.SetTypeSpecError();
1441 } else if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers() &&
1442 !DS.hasAttributes()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001443 Diag(Tok, diag::err_typename_requires_specqual);
Richard Smith69730c12012-03-12 07:56:15 +00001444 if (!DS.hasTypeSpecifier())
1445 DS.SetTypeSpecError();
1446 }
Mike Stump1eb44332009-09-09 15:08:12 +00001447
Reid Spencer5f016e22007-07-11 17:01:13 +00001448 // Issue diagnostic and remove storage class if present.
1449 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
1450 if (DS.getStorageClassSpecLoc().isValid())
1451 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
1452 else
1453 Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass);
1454 DS.ClearStorageClassSpecs();
1455 }
Mike Stump1eb44332009-09-09 15:08:12 +00001456
Reid Spencer5f016e22007-07-11 17:01:13 +00001457 // Issue diagnostic and remove function specfier if present.
1458 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Douglas Gregorb48fe382008-10-31 09:07:45 +00001459 if (DS.isInlineSpecified())
1460 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
1461 if (DS.isVirtualSpecified())
1462 Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec);
1463 if (DS.isExplicitSpecified())
1464 Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec);
Reid Spencer5f016e22007-07-11 17:01:13 +00001465 DS.ClearFunctionSpecs();
1466 }
Richard Smith69730c12012-03-12 07:56:15 +00001467
1468 // Issue diagnostic and remove constexpr specfier if present.
1469 if (DS.isConstexprSpecified()) {
1470 Diag(DS.getConstexprSpecLoc(), diag::err_typename_invalid_constexpr);
1471 DS.ClearConstexprSpec();
1472 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001473}
1474
Chris Lattnerc199ab32009-04-12 20:42:31 +00001475/// isValidAfterIdentifierInDeclaratorAfterDeclSpec - Return true if the
1476/// specified token is valid after the identifier in a declarator which
1477/// immediately follows the declspec. For example, these things are valid:
1478///
1479/// int x [ 4]; // direct-declarator
1480/// int x ( int y); // direct-declarator
1481/// int(int x ) // direct-declarator
1482/// int x ; // simple-declaration
1483/// int x = 17; // init-declarator-list
1484/// int x , y; // init-declarator-list
1485/// int x __asm__ ("foo"); // init-declarator-list
Chris Lattnerb6645dd2009-04-14 21:16:09 +00001486/// int x : 4; // struct-declarator
Chris Lattnerc83c27a2009-04-12 22:29:43 +00001487/// int x { 5}; // C++'0x unified initializers
Chris Lattnerc199ab32009-04-12 20:42:31 +00001488///
1489/// This is not, because 'x' does not immediately follow the declspec (though
1490/// ')' happens to be valid anyway).
1491/// int (x)
1492///
1493static bool isValidAfterIdentifierInDeclarator(const Token &T) {
1494 return T.is(tok::l_square) || T.is(tok::l_paren) || T.is(tok::r_paren) ||
1495 T.is(tok::semi) || T.is(tok::comma) || T.is(tok::equal) ||
Chris Lattnerb6645dd2009-04-14 21:16:09 +00001496 T.is(tok::kw_asm) || T.is(tok::l_brace) || T.is(tok::colon);
Chris Lattnerc199ab32009-04-12 20:42:31 +00001497}
1498
Chris Lattnere40c2952009-04-14 21:34:55 +00001499
1500/// ParseImplicitInt - This method is called when we have an non-typename
1501/// identifier in a declspec (which normally terminates the decl spec) when
1502/// the declspec has no type specifier. In this case, the declspec is either
1503/// malformed or is "implicit int" (in K&R and C89).
1504///
1505/// This method handles diagnosing this prettily and returns false if the
1506/// declspec is done being processed. If it recovers and thinks there may be
1507/// other pieces of declspec after it, it returns true.
1508///
Chris Lattnerf4382f52009-04-14 22:17:06 +00001509bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001510 const ParsedTemplateInfo &TemplateInfo,
Richard Smith69730c12012-03-12 07:56:15 +00001511 AccessSpecifier AS, DeclSpecContext DSC) {
Chris Lattnerf4382f52009-04-14 22:17:06 +00001512 assert(Tok.is(tok::identifier) && "should have identifier");
Mike Stump1eb44332009-09-09 15:08:12 +00001513
Chris Lattnere40c2952009-04-14 21:34:55 +00001514 SourceLocation Loc = Tok.getLocation();
1515 // If we see an identifier that is not a type name, we normally would
1516 // parse it as the identifer being declared. However, when a typename
1517 // is typo'd or the definition is not included, this will incorrectly
1518 // parse the typename as the identifier name and fall over misparsing
1519 // later parts of the diagnostic.
1520 //
1521 // As such, we try to do some look-ahead in cases where this would
1522 // otherwise be an "implicit-int" case to see if this is invalid. For
1523 // example: "static foo_t x = 4;" In this case, if we parsed foo_t as
1524 // an identifier with implicit int, we'd get a parse error because the
1525 // next token is obviously invalid for a type. Parse these as a case
1526 // with an invalid type specifier.
1527 assert(!DS.hasTypeSpecifier() && "Type specifier checked above");
Mike Stump1eb44332009-09-09 15:08:12 +00001528
Chris Lattnere40c2952009-04-14 21:34:55 +00001529 // Since we know that this either implicit int (which is rare) or an
Richard Smith69730c12012-03-12 07:56:15 +00001530 // error, do lookahead to try to do better recovery. This never applies within
1531 // a type specifier.
1532 // FIXME: Don't bail out here in languages with no implicit int (like
1533 // C++ with no -fms-extensions). This is much more likely to be an undeclared
1534 // type or typo than a use of implicit int.
1535 if (DSC != DSC_type_specifier &&
1536 isValidAfterIdentifierInDeclarator(NextToken())) {
Chris Lattnere40c2952009-04-14 21:34:55 +00001537 // If this token is valid for implicit int, e.g. "static x = 4", then
1538 // we just avoid eating the identifier, so it will be parsed as the
1539 // identifier in the declarator.
1540 return false;
1541 }
Mike Stump1eb44332009-09-09 15:08:12 +00001542
Chris Lattnere40c2952009-04-14 21:34:55 +00001543 // Otherwise, if we don't consume this token, we are going to emit an
1544 // error anyway. Try to recover from various common problems. Check
1545 // to see if this was a reference to a tag name without a tag specified.
1546 // This is a common problem in C (saying 'foo' instead of 'struct foo').
Chris Lattnerf4382f52009-04-14 22:17:06 +00001547 //
1548 // C++ doesn't need this, and isTagName doesn't take SS.
1549 if (SS == 0) {
Argyrios Kyrtzidisb8a9d3b2011-04-21 17:29:47 +00001550 const char *TagName = 0, *FixitTagName = 0;
Chris Lattnerf4382f52009-04-14 22:17:06 +00001551 tok::TokenKind TagKind = tok::unknown;
Mike Stump1eb44332009-09-09 15:08:12 +00001552
Douglas Gregor23c94db2010-07-02 17:43:08 +00001553 switch (Actions.isTagName(*Tok.getIdentifierInfo(), getCurScope())) {
Chris Lattnere40c2952009-04-14 21:34:55 +00001554 default: break;
Argyrios Kyrtzidisb8a9d3b2011-04-21 17:29:47 +00001555 case DeclSpec::TST_enum:
1556 TagName="enum" ; FixitTagName = "enum " ; TagKind=tok::kw_enum ;break;
1557 case DeclSpec::TST_union:
1558 TagName="union" ; FixitTagName = "union " ;TagKind=tok::kw_union ;break;
1559 case DeclSpec::TST_struct:
1560 TagName="struct"; FixitTagName = "struct ";TagKind=tok::kw_struct;break;
1561 case DeclSpec::TST_class:
1562 TagName="class" ; FixitTagName = "class " ;TagKind=tok::kw_class ;break;
Chris Lattnere40c2952009-04-14 21:34:55 +00001563 }
Mike Stump1eb44332009-09-09 15:08:12 +00001564
Chris Lattnerf4382f52009-04-14 22:17:06 +00001565 if (TagName) {
1566 Diag(Loc, diag::err_use_of_tag_name_without_tag)
David Blaikie4e4d0842012-03-11 07:00:24 +00001567 << Tok.getIdentifierInfo() << TagName << getLangOpts().CPlusPlus
Argyrios Kyrtzidisb8a9d3b2011-04-21 17:29:47 +00001568 << FixItHint::CreateInsertion(Tok.getLocation(),FixitTagName);
Mike Stump1eb44332009-09-09 15:08:12 +00001569
Chris Lattnerf4382f52009-04-14 22:17:06 +00001570 // Parse this as a tag as if the missing tag were present.
1571 if (TagKind == tok::kw_enum)
Richard Smith69730c12012-03-12 07:56:15 +00001572 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS, DSC_normal);
Chris Lattnerf4382f52009-04-14 22:17:06 +00001573 else
Richard Smith69730c12012-03-12 07:56:15 +00001574 ParseClassSpecifier(TagKind, Loc, DS, TemplateInfo, AS,
1575 /*EnteringContext*/ false, DSC_normal);
Chris Lattnerf4382f52009-04-14 22:17:06 +00001576 return true;
1577 }
Chris Lattnere40c2952009-04-14 21:34:55 +00001578 }
Mike Stump1eb44332009-09-09 15:08:12 +00001579
Douglas Gregora786fdb2009-10-13 23:27:22 +00001580 // This is almost certainly an invalid type name. Let the action emit a
1581 // diagnostic and attempt to recover.
John McCallb3d87482010-08-24 05:47:05 +00001582 ParsedType T;
Douglas Gregora786fdb2009-10-13 23:27:22 +00001583 if (Actions.DiagnoseUnknownTypeName(*Tok.getIdentifierInfo(), Loc,
Douglas Gregor23c94db2010-07-02 17:43:08 +00001584 getCurScope(), SS, T)) {
Douglas Gregora786fdb2009-10-13 23:27:22 +00001585 // The action emitted a diagnostic, so we don't have to.
1586 if (T) {
1587 // The action has suggested that the type T could be used. Set that as
1588 // the type in the declaration specifiers, consume the would-be type
1589 // name token, and we're done.
1590 const char *PrevSpec;
1591 unsigned DiagID;
John McCallb3d87482010-08-24 05:47:05 +00001592 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID, T);
Douglas Gregora786fdb2009-10-13 23:27:22 +00001593 DS.SetRangeEnd(Tok.getLocation());
1594 ConsumeToken();
1595
1596 // There may be other declaration specifiers after this.
1597 return true;
1598 }
1599
1600 // Fall through; the action had no suggestion for us.
1601 } else {
1602 // The action did not emit a diagnostic, so emit one now.
1603 SourceRange R;
1604 if (SS) R = SS->getRange();
1605 Diag(Loc, diag::err_unknown_typename) << Tok.getIdentifierInfo() << R;
1606 }
Mike Stump1eb44332009-09-09 15:08:12 +00001607
Douglas Gregora786fdb2009-10-13 23:27:22 +00001608 // Mark this as an error.
Richard Smith69730c12012-03-12 07:56:15 +00001609 DS.SetTypeSpecError();
Chris Lattnere40c2952009-04-14 21:34:55 +00001610 DS.SetRangeEnd(Tok.getLocation());
1611 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001612
Chris Lattnere40c2952009-04-14 21:34:55 +00001613 // TODO: Could inject an invalid typedef decl in an enclosing scope to
1614 // avoid rippling error messages on subsequent uses of the same type,
1615 // could be useful if #include was forgotten.
1616 return false;
1617}
1618
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001619/// \brief Determine the declaration specifier context from the declarator
1620/// context.
1621///
1622/// \param Context the declarator context, which is one of the
1623/// Declarator::TheContext enumerator values.
1624Parser::DeclSpecContext
1625Parser::getDeclSpecContextFromDeclaratorContext(unsigned Context) {
1626 if (Context == Declarator::MemberContext)
1627 return DSC_class;
1628 if (Context == Declarator::FileContext)
1629 return DSC_top_level;
Richard Smith6d96d3a2012-03-15 01:02:11 +00001630 if (Context == Declarator::TrailingReturnContext)
1631 return DSC_trailing;
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001632 return DSC_normal;
1633}
1634
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001635/// ParseAlignArgument - Parse the argument to an alignment-specifier.
1636///
1637/// FIXME: Simply returns an alignof() expression if the argument is a
1638/// type. Ideally, the type should be propagated directly into Sema.
1639///
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00001640/// [C11] type-id
1641/// [C11] constant-expression
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00001642/// [C++0x] type-id ...[opt]
1643/// [C++0x] assignment-expression ...[opt]
1644ExprResult Parser::ParseAlignArgument(SourceLocation Start,
1645 SourceLocation &EllipsisLoc) {
1646 ExprResult ER;
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001647 if (isTypeIdInParens()) {
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001648 SourceLocation TypeLoc = Tok.getLocation();
1649 ParsedType Ty = ParseTypeName().get();
1650 SourceRange TypeRange(Start, Tok.getLocation());
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00001651 ER = Actions.ActOnUnaryExprOrTypeTraitExpr(TypeLoc, UETT_AlignOf, true,
1652 Ty.getAsOpaquePtr(), TypeRange);
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001653 } else
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00001654 ER = ParseConstantExpression();
1655
David Blaikie4e4d0842012-03-11 07:00:24 +00001656 if (getLangOpts().CPlusPlus0x && Tok.is(tok::ellipsis))
Peter Collingbournefe9b2a82011-10-24 17:56:00 +00001657 EllipsisLoc = ConsumeToken();
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00001658
1659 return ER;
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001660}
1661
1662/// ParseAlignmentSpecifier - Parse an alignment-specifier, and add the
1663/// attribute to Attrs.
1664///
1665/// alignment-specifier:
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00001666/// [C11] '_Alignas' '(' type-id ')'
1667/// [C11] '_Alignas' '(' constant-expression ')'
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00001668/// [C++0x] 'alignas' '(' type-id ...[opt] ')'
1669/// [C++0x] 'alignas' '(' assignment-expression ...[opt] ')'
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001670void Parser::ParseAlignmentSpecifier(ParsedAttributes &Attrs,
1671 SourceLocation *endLoc) {
1672 assert((Tok.is(tok::kw_alignas) || Tok.is(tok::kw__Alignas)) &&
1673 "Not an alignment-specifier!");
1674
1675 SourceLocation KWLoc = Tok.getLocation();
1676 ConsumeToken();
1677
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001678 BalancedDelimiterTracker T(*this, tok::l_paren);
1679 if (T.expectAndConsume(diag::err_expected_lparen))
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001680 return;
1681
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00001682 SourceLocation EllipsisLoc;
1683 ExprResult ArgExpr = ParseAlignArgument(T.getOpenLocation(), EllipsisLoc);
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001684 if (ArgExpr.isInvalid()) {
1685 SkipUntil(tok::r_paren);
1686 return;
1687 }
1688
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001689 T.consumeClose();
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001690 if (endLoc)
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001691 *endLoc = T.getCloseLocation();
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001692
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00001693 // FIXME: Handle pack-expansions here.
1694 if (EllipsisLoc.isValid()) {
1695 Diag(EllipsisLoc, diag::err_alignas_pack_exp_unsupported);
1696 return;
1697 }
1698
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001699 ExprVector ArgExprs(Actions);
1700 ArgExprs.push_back(ArgExpr.release());
1701 Attrs.addNew(PP.getIdentifierInfo("aligned"), KWLoc, 0, KWLoc,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001702 0, T.getOpenLocation(), ArgExprs.take(), 1, false, true);
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001703}
1704
Reid Spencer5f016e22007-07-11 17:01:13 +00001705/// ParseDeclarationSpecifiers
1706/// declaration-specifiers: [C99 6.7]
1707/// storage-class-specifier declaration-specifiers[opt]
1708/// type-specifier declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00001709/// [C99] function-specifier declaration-specifiers[opt]
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00001710/// [C11] alignment-specifier declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00001711/// [GNU] attributes declaration-specifiers[opt]
Douglas Gregor8d267c52011-09-09 02:06:17 +00001712/// [Clang] '__module_private__' declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00001713///
1714/// storage-class-specifier: [C99 6.7.1]
1715/// 'typedef'
1716/// 'extern'
1717/// 'static'
1718/// 'auto'
1719/// 'register'
Sebastian Redl669d5d72008-11-14 23:42:31 +00001720/// [C++] 'mutable'
Reid Spencer5f016e22007-07-11 17:01:13 +00001721/// [GNU] '__thread'
Reid Spencer5f016e22007-07-11 17:01:13 +00001722/// function-specifier: [C99 6.7.4]
1723/// [C99] 'inline'
Douglas Gregorb48fe382008-10-31 09:07:45 +00001724/// [C++] 'virtual'
1725/// [C++] 'explicit'
Peter Collingbournef315fa82011-02-14 01:42:53 +00001726/// [OpenCL] '__kernel'
Anders Carlssonf47f7a12009-05-06 04:46:28 +00001727/// 'friend': [C++ dcl.friend]
Sebastian Redl2ac67232009-11-05 15:47:02 +00001728/// 'constexpr': [C++0x dcl.constexpr]
Anders Carlssonf47f7a12009-05-06 04:46:28 +00001729
Reid Spencer5f016e22007-07-11 17:01:13 +00001730///
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +00001731void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001732 const ParsedTemplateInfo &TemplateInfo,
John McCall67d1a672009-08-06 02:15:43 +00001733 AccessSpecifier AS,
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +00001734 DeclSpecContext DSContext,
1735 LateParsedAttrList *LateAttrs) {
Douglas Gregor312eadb2011-04-24 05:37:28 +00001736 if (DS.getSourceRange().isInvalid()) {
1737 DS.SetRangeStart(Tok.getLocation());
1738 DS.SetRangeEnd(Tok.getLocation());
1739 }
1740
Douglas Gregorefaa93a2011-11-07 17:33:42 +00001741 bool EnteringContext = (DSContext == DSC_class || DSContext == DSC_top_level);
Reid Spencer5f016e22007-07-11 17:01:13 +00001742 while (1) {
John McCallfec54012009-08-03 20:12:06 +00001743 bool isInvalid = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001744 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00001745 unsigned DiagID = 0;
1746
Reid Spencer5f016e22007-07-11 17:01:13 +00001747 SourceLocation Loc = Tok.getLocation();
Douglas Gregor12e083c2008-11-07 15:42:26 +00001748
Reid Spencer5f016e22007-07-11 17:01:13 +00001749 switch (Tok.getKind()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001750 default:
Chris Lattnerbce61352008-07-26 00:20:22 +00001751 DoneWithDeclSpec:
Peter Collingbournef1907682011-09-29 18:03:57 +00001752 // [C++0x] decl-specifier-seq: decl-specifier attribute-specifier-seq[opt]
1753 MaybeParseCXX0XAttributes(DS.getAttributes());
1754
Reid Spencer5f016e22007-07-11 17:01:13 +00001755 // If this is not a declaration specifier token, we're done reading decl
1756 // specifiers. First verify that DeclSpec's are consistent.
Douglas Gregor9b3064b2009-04-01 22:41:11 +00001757 DS.Finish(Diags, PP);
Reid Spencer5f016e22007-07-11 17:01:13 +00001758 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001759
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001760 case tok::code_completion: {
John McCallf312b1e2010-08-26 23:41:50 +00001761 Sema::ParserCompletionContext CCC = Sema::PCC_Namespace;
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001762 if (DS.hasTypeSpecifier()) {
1763 bool AllowNonIdentifiers
1764 = (getCurScope()->getFlags() & (Scope::ControlScope |
1765 Scope::BlockScope |
1766 Scope::TemplateParamScope |
1767 Scope::FunctionPrototypeScope |
1768 Scope::AtCatchScope)) == 0;
1769 bool AllowNestedNameSpecifiers
1770 = DSContext == DSC_top_level ||
1771 (DSContext == DSC_class && DS.isFriendSpecified());
1772
Douglas Gregorc7b6d882010-09-16 15:14:18 +00001773 Actions.CodeCompleteDeclSpec(getCurScope(), DS,
1774 AllowNonIdentifiers,
1775 AllowNestedNameSpecifiers);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001776 return cutOffParsing();
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001777 }
1778
Douglas Gregor68e3c2e2011-02-15 20:33:25 +00001779 if (getCurScope()->getFnParent() || getCurScope()->getBlockParent())
1780 CCC = Sema::PCC_LocalDeclarationSpecifiers;
1781 else if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate)
John McCallf312b1e2010-08-26 23:41:50 +00001782 CCC = DSContext == DSC_class? Sema::PCC_MemberTemplate
1783 : Sema::PCC_Template;
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001784 else if (DSContext == DSC_class)
John McCallf312b1e2010-08-26 23:41:50 +00001785 CCC = Sema::PCC_Class;
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +00001786 else if (CurParsedObjCImpl)
John McCallf312b1e2010-08-26 23:41:50 +00001787 CCC = Sema::PCC_ObjCImplementation;
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001788
1789 Actions.CodeCompleteOrdinaryName(getCurScope(), CCC);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001790 return cutOffParsing();
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001791 }
1792
Chris Lattner5e02c472009-01-05 00:07:25 +00001793 case tok::coloncolon: // ::foo::bar
John McCall9ba61662010-02-26 08:45:28 +00001794 // C++ scope specifier. Annotate and loop, or bail out on error.
1795 if (TryAnnotateCXXScopeToken(true)) {
1796 if (!DS.hasTypeSpecifier())
1797 DS.SetTypeSpecError();
1798 goto DoneWithDeclSpec;
1799 }
John McCall2e0a7152010-03-01 18:20:46 +00001800 if (Tok.is(tok::coloncolon)) // ::new or ::delete
1801 goto DoneWithDeclSpec;
John McCall9ba61662010-02-26 08:45:28 +00001802 continue;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001803
1804 case tok::annot_cxxscope: {
1805 if (DS.hasTypeSpecifier())
1806 goto DoneWithDeclSpec;
1807
John McCallaa87d332009-12-12 11:40:51 +00001808 CXXScopeSpec SS;
Douglas Gregorc34348a2011-02-24 17:54:50 +00001809 Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(),
1810 Tok.getAnnotationRange(),
1811 SS);
John McCallaa87d332009-12-12 11:40:51 +00001812
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001813 // We are looking for a qualified typename.
Douglas Gregor9135c722009-03-25 15:40:00 +00001814 Token Next = NextToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001815 if (Next.is(tok::annot_template_id) &&
Douglas Gregor9135c722009-03-25 15:40:00 +00001816 static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue())
Douglas Gregorc45c2322009-03-31 00:43:58 +00001817 ->Kind == TNK_Type_template) {
Douglas Gregor9135c722009-03-25 15:40:00 +00001818 // We have a qualified template-id, e.g., N::A<int>
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001819
1820 // C++ [class.qual]p2:
1821 // In a lookup in which the constructor is an acceptable lookup
1822 // result and the nested-name-specifier nominates a class C:
1823 //
1824 // - if the name specified after the
1825 // nested-name-specifier, when looked up in C, is the
1826 // injected-class-name of C (Clause 9), or
1827 //
1828 // - if the name specified after the nested-name-specifier
1829 // is the same as the identifier or the
1830 // simple-template-id's template-name in the last
1831 // component of the nested-name-specifier,
1832 //
1833 // the name is instead considered to name the constructor of
1834 // class C.
1835 //
1836 // Thus, if the template-name is actually the constructor
1837 // name, then the code is ill-formed; this interpretation is
1838 // reinforced by the NAD status of core issue 635.
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +00001839 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Next);
John McCallba9d8532010-04-13 06:39:49 +00001840 if ((DSContext == DSC_top_level ||
1841 (DSContext == DSC_class && DS.isFriendSpecified())) &&
1842 TemplateId->Name &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00001843 Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) {
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001844 if (isConstructorDeclarator()) {
1845 // The user meant this to be an out-of-line constructor
1846 // definition, but template arguments are not allowed
1847 // there. Just allow this as a constructor; we'll
1848 // complain about it later.
1849 goto DoneWithDeclSpec;
1850 }
1851
1852 // The user meant this to name a type, but it actually names
1853 // a constructor with some extraneous template
1854 // arguments. Complain, then parse it as a type as the user
1855 // intended.
1856 Diag(TemplateId->TemplateNameLoc,
1857 diag::err_out_of_line_template_id_names_constructor)
1858 << TemplateId->Name;
1859 }
1860
John McCallaa87d332009-12-12 11:40:51 +00001861 DS.getTypeSpecScope() = SS;
1862 ConsumeToken(); // The C++ scope.
Mike Stump1eb44332009-09-09 15:08:12 +00001863 assert(Tok.is(tok::annot_template_id) &&
Douglas Gregor9135c722009-03-25 15:40:00 +00001864 "ParseOptionalCXXScopeSpecifier not working");
Douglas Gregor059101f2011-03-02 00:47:37 +00001865 AnnotateTemplateIdTokenAsType();
Douglas Gregor9135c722009-03-25 15:40:00 +00001866 continue;
1867 }
1868
Douglas Gregor9d7b3532009-09-28 07:26:33 +00001869 if (Next.is(tok::annot_typename)) {
John McCallaa87d332009-12-12 11:40:51 +00001870 DS.getTypeSpecScope() = SS;
1871 ConsumeToken(); // The C++ scope.
John McCallb3d87482010-08-24 05:47:05 +00001872 if (Tok.getAnnotationValue()) {
1873 ParsedType T = getTypeAnnotation(Tok);
Nico Weber253e80b2010-11-22 10:30:56 +00001874 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename,
1875 Tok.getAnnotationEndLoc(),
John McCallb3d87482010-08-24 05:47:05 +00001876 PrevSpec, DiagID, T);
1877 }
Douglas Gregor9d7b3532009-09-28 07:26:33 +00001878 else
1879 DS.SetTypeSpecError();
1880 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1881 ConsumeToken(); // The typename
1882 }
1883
Douglas Gregor9135c722009-03-25 15:40:00 +00001884 if (Next.isNot(tok::identifier))
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001885 goto DoneWithDeclSpec;
1886
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001887 // If we're in a context where the identifier could be a class name,
1888 // check whether this is a constructor declaration.
John McCallba9d8532010-04-13 06:39:49 +00001889 if ((DSContext == DSC_top_level ||
1890 (DSContext == DSC_class && DS.isFriendSpecified())) &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00001891 Actions.isCurrentClassName(*Next.getIdentifierInfo(), getCurScope(),
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001892 &SS)) {
1893 if (isConstructorDeclarator())
1894 goto DoneWithDeclSpec;
1895
1896 // As noted in C++ [class.qual]p2 (cited above), when the name
1897 // of the class is qualified in a context where it could name
1898 // a constructor, its a constructor name. However, we've
1899 // looked at the declarator, and the user probably meant this
1900 // to be a type. Complain that it isn't supposed to be treated
1901 // as a type, then proceed to parse it as a type.
1902 Diag(Next.getLocation(), diag::err_out_of_line_type_names_constructor)
1903 << Next.getIdentifierInfo();
1904 }
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001905
John McCallb3d87482010-08-24 05:47:05 +00001906 ParsedType TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(),
1907 Next.getLocation(),
Douglas Gregor9e876872011-03-01 18:12:44 +00001908 getCurScope(), &SS,
1909 false, false, ParsedType(),
Abramo Bagnarafad03b72012-01-27 08:46:19 +00001910 /*IsCtorOrDtorName=*/false,
Douglas Gregor9e876872011-03-01 18:12:44 +00001911 /*NonTrivialSourceInfo=*/true);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001912
Chris Lattnerf4382f52009-04-14 22:17:06 +00001913 // If the referenced identifier is not a type, then this declspec is
1914 // erroneous: We already checked about that it has no type specifier, and
1915 // C++ doesn't have implicit int. Diagnose it as a typo w.r.t. to the
Mike Stump1eb44332009-09-09 15:08:12 +00001916 // typename.
Chris Lattnerf4382f52009-04-14 22:17:06 +00001917 if (TypeRep == 0) {
1918 ConsumeToken(); // Eat the scope spec so the identifier is current.
Richard Smith69730c12012-03-12 07:56:15 +00001919 if (ParseImplicitInt(DS, &SS, TemplateInfo, AS, DSContext)) continue;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001920 goto DoneWithDeclSpec;
Chris Lattnerf4382f52009-04-14 22:17:06 +00001921 }
Mike Stump1eb44332009-09-09 15:08:12 +00001922
John McCallaa87d332009-12-12 11:40:51 +00001923 DS.getTypeSpecScope() = SS;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001924 ConsumeToken(); // The C++ scope.
1925
Douglas Gregor1a51b4a2009-02-09 15:09:02 +00001926 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001927 DiagID, TypeRep);
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001928 if (isInvalid)
1929 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001930
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001931 DS.SetRangeEnd(Tok.getLocation());
1932 ConsumeToken(); // The typename.
1933
1934 continue;
1935 }
Mike Stump1eb44332009-09-09 15:08:12 +00001936
Chris Lattner80d0c892009-01-21 19:48:37 +00001937 case tok::annot_typename: {
John McCallb3d87482010-08-24 05:47:05 +00001938 if (Tok.getAnnotationValue()) {
1939 ParsedType T = getTypeAnnotation(Tok);
Nico Weberc43271e2010-11-22 12:50:03 +00001940 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallb3d87482010-08-24 05:47:05 +00001941 DiagID, T);
1942 } else
Douglas Gregor31a19b62009-04-01 21:51:26 +00001943 DS.SetTypeSpecError();
Chris Lattner5c5db552010-04-05 18:18:31 +00001944
1945 if (isInvalid)
1946 break;
1947
Chris Lattner80d0c892009-01-21 19:48:37 +00001948 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1949 ConsumeToken(); // The typename
Mike Stump1eb44332009-09-09 15:08:12 +00001950
Chris Lattner80d0c892009-01-21 19:48:37 +00001951 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1952 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001953 // Objective-C interface.
David Blaikie4e4d0842012-03-11 07:00:24 +00001954 if (Tok.is(tok::less) && getLangOpts().ObjC1)
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001955 ParseObjCProtocolQualifiers(DS);
1956
Chris Lattner80d0c892009-01-21 19:48:37 +00001957 continue;
1958 }
Mike Stump1eb44332009-09-09 15:08:12 +00001959
Douglas Gregorbfad9152011-04-28 15:48:45 +00001960 case tok::kw___is_signed:
1961 // GNU libstdc++ 4.4 uses __is_signed as an identifier, but Clang
1962 // typically treats it as a trait. If we see __is_signed as it appears
1963 // in libstdc++, e.g.,
1964 //
1965 // static const bool __is_signed;
1966 //
1967 // then treat __is_signed as an identifier rather than as a keyword.
1968 if (DS.getTypeSpecType() == TST_bool &&
1969 DS.getTypeQualifiers() == DeclSpec::TQ_const &&
1970 DS.getStorageClassSpec() == DeclSpec::SCS_static) {
1971 Tok.getIdentifierInfo()->RevertTokenIDToIdentifier();
1972 Tok.setKind(tok::identifier);
1973 }
1974
1975 // We're done with the declaration-specifiers.
1976 goto DoneWithDeclSpec;
1977
Chris Lattner3bd934a2008-07-26 01:18:38 +00001978 // typedef-name
David Blaikie42d6d0c2011-12-04 05:04:18 +00001979 case tok::kw_decltype:
Chris Lattner3bd934a2008-07-26 01:18:38 +00001980 case tok::identifier: {
Chris Lattner5e02c472009-01-05 00:07:25 +00001981 // In C++, check to see if this is a scope specifier like foo::bar::, if
1982 // so handle it as such. This is important for ctor parsing.
David Blaikie4e4d0842012-03-11 07:00:24 +00001983 if (getLangOpts().CPlusPlus) {
John McCall9ba61662010-02-26 08:45:28 +00001984 if (TryAnnotateCXXScopeToken(true)) {
1985 if (!DS.hasTypeSpecifier())
1986 DS.SetTypeSpecError();
1987 goto DoneWithDeclSpec;
1988 }
1989 if (!Tok.is(tok::identifier))
1990 continue;
1991 }
Mike Stump1eb44332009-09-09 15:08:12 +00001992
Chris Lattner3bd934a2008-07-26 01:18:38 +00001993 // This identifier can only be a typedef name if we haven't already seen
1994 // a type-specifier. Without this check we misparse:
1995 // typedef int X; struct Y { short X; }; as 'short int'.
1996 if (DS.hasTypeSpecifier())
1997 goto DoneWithDeclSpec;
Mike Stump1eb44332009-09-09 15:08:12 +00001998
John Thompson82287d12010-02-05 00:12:22 +00001999 // Check for need to substitute AltiVec keyword tokens.
2000 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
2001 break;
2002
John McCallb3d87482010-08-24 05:47:05 +00002003 ParsedType TypeRep =
2004 Actions.getTypeName(*Tok.getIdentifierInfo(),
2005 Tok.getLocation(), getCurScope());
Douglas Gregor55f6b142009-02-09 18:46:07 +00002006
Chris Lattnerc199ab32009-04-12 20:42:31 +00002007 // If this is not a typedef name, don't parse it as part of the declspec,
2008 // it must be an implicit int or an error.
John McCallb3d87482010-08-24 05:47:05 +00002009 if (!TypeRep) {
Richard Smith69730c12012-03-12 07:56:15 +00002010 if (ParseImplicitInt(DS, 0, TemplateInfo, AS, DSContext)) continue;
Chris Lattner3bd934a2008-07-26 01:18:38 +00002011 goto DoneWithDeclSpec;
Chris Lattnerc199ab32009-04-12 20:42:31 +00002012 }
Douglas Gregor55f6b142009-02-09 18:46:07 +00002013
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002014 // If we're in a context where the identifier could be a class name,
2015 // check whether this is a constructor declaration.
David Blaikie4e4d0842012-03-11 07:00:24 +00002016 if (getLangOpts().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00002017 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), getCurScope()) &&
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002018 isConstructorDeclarator())
Douglas Gregorb48fe382008-10-31 09:07:45 +00002019 goto DoneWithDeclSpec;
2020
Douglas Gregor1a51b4a2009-02-09 15:09:02 +00002021 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00002022 DiagID, TypeRep);
Chris Lattner3bd934a2008-07-26 01:18:38 +00002023 if (isInvalid)
2024 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002025
Chris Lattner3bd934a2008-07-26 01:18:38 +00002026 DS.SetRangeEnd(Tok.getLocation());
2027 ConsumeToken(); // The identifier
2028
2029 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
2030 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00002031 // Objective-C interface.
David Blaikie4e4d0842012-03-11 07:00:24 +00002032 if (Tok.is(tok::less) && getLangOpts().ObjC1)
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00002033 ParseObjCProtocolQualifiers(DS);
2034
Steve Naroff4f9b9f12008-09-22 10:28:57 +00002035 // Need to support trailing type qualifiers (e.g. "id<p> const").
2036 // If a type specifier follows, it will be diagnosed elsewhere.
2037 continue;
Chris Lattner3bd934a2008-07-26 01:18:38 +00002038 }
Douglas Gregor39a8de12009-02-25 19:37:18 +00002039
2040 // type-name
2041 case tok::annot_template_id: {
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +00002042 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregorc45c2322009-03-31 00:43:58 +00002043 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor39a8de12009-02-25 19:37:18 +00002044 // This template-id does not refer to a type name, so we're
2045 // done with the type-specifiers.
2046 goto DoneWithDeclSpec;
2047 }
2048
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002049 // If we're in a context where the template-id could be a
2050 // constructor name or specialization, check whether this is a
2051 // constructor declaration.
David Blaikie4e4d0842012-03-11 07:00:24 +00002052 if (getLangOpts().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00002053 Actions.isCurrentClassName(*TemplateId->Name, getCurScope()) &&
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002054 isConstructorDeclarator())
2055 goto DoneWithDeclSpec;
2056
Douglas Gregor39a8de12009-02-25 19:37:18 +00002057 // Turn the template-id annotation token into a type annotation
2058 // token, then try again to parse it as a type-specifier.
Douglas Gregor31a19b62009-04-01 21:51:26 +00002059 AnnotateTemplateIdTokenAsType();
Douglas Gregor39a8de12009-02-25 19:37:18 +00002060 continue;
2061 }
2062
Reid Spencer5f016e22007-07-11 17:01:13 +00002063 // GNU attributes support.
2064 case tok::kw___attribute:
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +00002065 ParseGNUAttributes(DS.getAttributes(), 0, LateAttrs);
Reid Spencer5f016e22007-07-11 17:01:13 +00002066 continue;
Steve Narofff59e17e2008-12-24 20:59:21 +00002067
2068 // Microsoft declspec support.
2069 case tok::kw___declspec:
John McCall7f040a92010-12-24 02:08:15 +00002070 ParseMicrosoftDeclSpec(DS.getAttributes());
Steve Narofff59e17e2008-12-24 20:59:21 +00002071 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00002072
Steve Naroff239f0732008-12-25 14:16:32 +00002073 // Microsoft single token adornments.
Steve Naroff86bc6cf2008-12-25 14:41:26 +00002074 case tok::kw___forceinline:
Eli Friedman290eeb02009-06-08 23:27:34 +00002075 // FIXME: Add handling here!
2076 break;
2077
2078 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00002079 case tok::kw___ptr32:
Steve Naroff86bc6cf2008-12-25 14:41:26 +00002080 case tok::kw___w64:
Steve Naroff239f0732008-12-25 14:16:32 +00002081 case tok::kw___cdecl:
2082 case tok::kw___stdcall:
2083 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00002084 case tok::kw___thiscall:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00002085 case tok::kw___unaligned:
John McCall7f040a92010-12-24 02:08:15 +00002086 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman290eeb02009-06-08 23:27:34 +00002087 continue;
2088
Dawn Perchik52fc3142010-09-03 01:29:35 +00002089 // Borland single token adornments.
2090 case tok::kw___pascal:
John McCall7f040a92010-12-24 02:08:15 +00002091 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik52fc3142010-09-03 01:29:35 +00002092 continue;
2093
Peter Collingbournef315fa82011-02-14 01:42:53 +00002094 // OpenCL single token adornments.
2095 case tok::kw___kernel:
2096 ParseOpenCLAttributes(DS.getAttributes());
2097 continue;
2098
Reid Spencer5f016e22007-07-11 17:01:13 +00002099 // storage-class-specifier
2100 case tok::kw_typedef:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002101 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_typedef, Loc,
2102 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002103 break;
2104 case tok::kw_extern:
2105 if (DS.isThreadSpecified())
Chris Lattner1ab3b962008-11-18 07:48:38 +00002106 Diag(Tok, diag::ext_thread_before) << "extern";
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002107 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_extern, Loc,
2108 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002109 break;
Steve Naroff8d54bf22007-12-18 00:16:02 +00002110 case tok::kw___private_extern__:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002111 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_private_extern,
2112 Loc, PrevSpec, DiagID);
Steve Naroff8d54bf22007-12-18 00:16:02 +00002113 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00002114 case tok::kw_static:
2115 if (DS.isThreadSpecified())
Chris Lattner1ab3b962008-11-18 07:48:38 +00002116 Diag(Tok, diag::ext_thread_before) << "static";
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002117 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_static, Loc,
2118 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002119 break;
2120 case tok::kw_auto:
David Blaikie4e4d0842012-03-11 07:00:24 +00002121 if (getLangOpts().CPlusPlus0x) {
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002122 if (isKnownToBeTypeSpecifier(GetLookAheadToken(1))) {
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002123 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc,
2124 PrevSpec, DiagID);
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002125 if (!isInvalid)
Richard Smith8f4fb192011-09-04 19:54:14 +00002126 Diag(Tok, diag::ext_auto_storage_class)
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002127 << FixItHint::CreateRemoval(DS.getStorageClassSpecLoc());
Richard Smith8f4fb192011-09-04 19:54:14 +00002128 } else
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002129 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec,
2130 DiagID);
Richard Smith8f4fb192011-09-04 19:54:14 +00002131 } else
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002132 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc,
2133 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002134 break;
2135 case tok::kw_register:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002136 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_register, Loc,
2137 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002138 break;
Sebastian Redl669d5d72008-11-14 23:42:31 +00002139 case tok::kw_mutable:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002140 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_mutable, Loc,
2141 PrevSpec, DiagID);
Sebastian Redl669d5d72008-11-14 23:42:31 +00002142 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00002143 case tok::kw___thread:
John McCallfec54012009-08-03 20:12:06 +00002144 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002145 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002146
Reid Spencer5f016e22007-07-11 17:01:13 +00002147 // function-specifier
2148 case tok::kw_inline:
John McCallfec54012009-08-03 20:12:06 +00002149 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002150 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +00002151 case tok::kw_virtual:
John McCallfec54012009-08-03 20:12:06 +00002152 isInvalid = DS.SetFunctionSpecVirtual(Loc, PrevSpec, DiagID);
Douglas Gregorb48fe382008-10-31 09:07:45 +00002153 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +00002154 case tok::kw_explicit:
John McCallfec54012009-08-03 20:12:06 +00002155 isInvalid = DS.SetFunctionSpecExplicit(Loc, PrevSpec, DiagID);
Douglas Gregorb48fe382008-10-31 09:07:45 +00002156 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002157
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002158 // alignment-specifier
2159 case tok::kw__Alignas:
David Blaikie4e4d0842012-03-11 07:00:24 +00002160 if (!getLangOpts().C11)
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00002161 Diag(Tok, diag::ext_c11_alignas);
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002162 ParseAlignmentSpecifier(DS.getAttributes());
2163 continue;
2164
Anders Carlssonf47f7a12009-05-06 04:46:28 +00002165 // friend
2166 case tok::kw_friend:
John McCall67d1a672009-08-06 02:15:43 +00002167 if (DSContext == DSC_class)
2168 isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID);
2169 else {
2170 PrevSpec = ""; // not actually used by the diagnostic
2171 DiagID = diag::err_friend_invalid_in_context;
2172 isInvalid = true;
2173 }
Anders Carlssonf47f7a12009-05-06 04:46:28 +00002174 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002175
Douglas Gregor8d267c52011-09-09 02:06:17 +00002176 // Modules
2177 case tok::kw___module_private__:
2178 isInvalid = DS.setModulePrivateSpec(Loc, PrevSpec, DiagID);
2179 break;
2180
Sebastian Redl2ac67232009-11-05 15:47:02 +00002181 // constexpr
2182 case tok::kw_constexpr:
2183 isInvalid = DS.SetConstexprSpec(Loc, PrevSpec, DiagID);
2184 break;
2185
Chris Lattner80d0c892009-01-21 19:48:37 +00002186 // type-specifier
2187 case tok::kw_short:
John McCallfec54012009-08-03 20:12:06 +00002188 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec,
2189 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002190 break;
2191 case tok::kw_long:
2192 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCallfec54012009-08-03 20:12:06 +00002193 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
2194 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002195 else
John McCallfec54012009-08-03 20:12:06 +00002196 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2197 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002198 break;
Francois Pichet338d7f72011-04-28 01:59:37 +00002199 case tok::kw___int64:
2200 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2201 DiagID);
2202 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002203 case tok::kw_signed:
John McCallfec54012009-08-03 20:12:06 +00002204 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec,
2205 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002206 break;
2207 case tok::kw_unsigned:
John McCallfec54012009-08-03 20:12:06 +00002208 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
2209 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002210 break;
2211 case tok::kw__Complex:
John McCallfec54012009-08-03 20:12:06 +00002212 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
2213 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002214 break;
2215 case tok::kw__Imaginary:
John McCallfec54012009-08-03 20:12:06 +00002216 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
2217 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002218 break;
2219 case tok::kw_void:
John McCallfec54012009-08-03 20:12:06 +00002220 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec,
2221 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002222 break;
2223 case tok::kw_char:
John McCallfec54012009-08-03 20:12:06 +00002224 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec,
2225 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002226 break;
2227 case tok::kw_int:
John McCallfec54012009-08-03 20:12:06 +00002228 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec,
2229 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002230 break;
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00002231 case tok::kw_half:
2232 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_half, Loc, PrevSpec,
2233 DiagID);
2234 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002235 case tok::kw_float:
John McCallfec54012009-08-03 20:12:06 +00002236 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec,
2237 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002238 break;
2239 case tok::kw_double:
John McCallfec54012009-08-03 20:12:06 +00002240 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec,
2241 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002242 break;
2243 case tok::kw_wchar_t:
John McCallfec54012009-08-03 20:12:06 +00002244 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec,
2245 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002246 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002247 case tok::kw_char16_t:
John McCallfec54012009-08-03 20:12:06 +00002248 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec,
2249 DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002250 break;
2251 case tok::kw_char32_t:
John McCallfec54012009-08-03 20:12:06 +00002252 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec,
2253 DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002254 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002255 case tok::kw_bool:
2256 case tok::kw__Bool:
Argyrios Kyrtzidis4383e182010-11-16 18:18:13 +00002257 if (Tok.is(tok::kw_bool) &&
2258 DS.getTypeSpecType() != DeclSpec::TST_unspecified &&
2259 DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
2260 PrevSpec = ""; // Not used by the diagnostic.
2261 DiagID = diag::err_bool_redeclaration;
Fariborz Jahaniane106a0b2011-04-19 21:42:37 +00002262 // For better error recovery.
2263 Tok.setKind(tok::identifier);
Argyrios Kyrtzidis4383e182010-11-16 18:18:13 +00002264 isInvalid = true;
2265 } else {
2266 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec,
2267 DiagID);
2268 }
Chris Lattner80d0c892009-01-21 19:48:37 +00002269 break;
2270 case tok::kw__Decimal32:
John McCallfec54012009-08-03 20:12:06 +00002271 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
2272 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002273 break;
2274 case tok::kw__Decimal64:
John McCallfec54012009-08-03 20:12:06 +00002275 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
2276 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002277 break;
2278 case tok::kw__Decimal128:
John McCallfec54012009-08-03 20:12:06 +00002279 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
2280 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002281 break;
John Thompson82287d12010-02-05 00:12:22 +00002282 case tok::kw___vector:
2283 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
2284 break;
2285 case tok::kw___pixel:
2286 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
2287 break;
John McCalla5fc4722011-04-09 22:50:59 +00002288 case tok::kw___unknown_anytype:
2289 isInvalid = DS.SetTypeSpecType(TST_unknown_anytype, Loc,
2290 PrevSpec, DiagID);
2291 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002292
2293 // class-specifier:
2294 case tok::kw_class:
2295 case tok::kw_struct:
Chris Lattner4c97d762009-04-12 21:49:30 +00002296 case tok::kw_union: {
2297 tok::TokenKind Kind = Tok.getKind();
2298 ConsumeToken();
Richard Smith69730c12012-03-12 07:56:15 +00002299 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS,
2300 EnteringContext, DSContext);
Chris Lattner80d0c892009-01-21 19:48:37 +00002301 continue;
Chris Lattner4c97d762009-04-12 21:49:30 +00002302 }
Chris Lattner80d0c892009-01-21 19:48:37 +00002303
2304 // enum-specifier:
2305 case tok::kw_enum:
Chris Lattner4c97d762009-04-12 21:49:30 +00002306 ConsumeToken();
Richard Smith69730c12012-03-12 07:56:15 +00002307 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS, DSContext);
Chris Lattner80d0c892009-01-21 19:48:37 +00002308 continue;
2309
2310 // cv-qualifier:
2311 case tok::kw_const:
John McCallfec54012009-08-03 20:12:06 +00002312 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec, DiagID,
David Blaikie4e4d0842012-03-11 07:00:24 +00002313 getLangOpts());
Chris Lattner80d0c892009-01-21 19:48:37 +00002314 break;
2315 case tok::kw_volatile:
John McCallfec54012009-08-03 20:12:06 +00002316 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
David Blaikie4e4d0842012-03-11 07:00:24 +00002317 getLangOpts());
Chris Lattner80d0c892009-01-21 19:48:37 +00002318 break;
2319 case tok::kw_restrict:
John McCallfec54012009-08-03 20:12:06 +00002320 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
David Blaikie4e4d0842012-03-11 07:00:24 +00002321 getLangOpts());
Chris Lattner80d0c892009-01-21 19:48:37 +00002322 break;
2323
Douglas Gregord57959a2009-03-27 23:10:48 +00002324 // C++ typename-specifier:
2325 case tok::kw_typename:
John McCall9ba61662010-02-26 08:45:28 +00002326 if (TryAnnotateTypeOrScopeToken()) {
2327 DS.SetTypeSpecError();
2328 goto DoneWithDeclSpec;
2329 }
2330 if (!Tok.is(tok::kw_typename))
Douglas Gregord57959a2009-03-27 23:10:48 +00002331 continue;
2332 break;
2333
Chris Lattner80d0c892009-01-21 19:48:37 +00002334 // GNU typeof support.
2335 case tok::kw_typeof:
2336 ParseTypeofSpecifier(DS);
2337 continue;
2338
David Blaikie42d6d0c2011-12-04 05:04:18 +00002339 case tok::annot_decltype:
Anders Carlsson6fd634f2009-06-24 17:47:40 +00002340 ParseDecltypeSpecifier(DS);
2341 continue;
2342
Sean Huntdb5d44b2011-05-19 05:37:45 +00002343 case tok::kw___underlying_type:
2344 ParseUnderlyingTypeSpecifier(DS);
Eli Friedmanb001de72011-10-06 23:00:33 +00002345 continue;
2346
2347 case tok::kw__Atomic:
2348 ParseAtomicSpecifier(DS);
2349 continue;
Sean Huntdb5d44b2011-05-19 05:37:45 +00002350
Peter Collingbourne207f4d82011-03-18 22:38:29 +00002351 // OpenCL qualifiers:
2352 case tok::kw_private:
David Blaikie4e4d0842012-03-11 07:00:24 +00002353 if (!getLangOpts().OpenCL)
Peter Collingbourne207f4d82011-03-18 22:38:29 +00002354 goto DoneWithDeclSpec;
2355 case tok::kw___private:
2356 case tok::kw___global:
2357 case tok::kw___local:
2358 case tok::kw___constant:
2359 case tok::kw___read_only:
2360 case tok::kw___write_only:
2361 case tok::kw___read_write:
2362 ParseOpenCLQualifiers(DS);
2363 break;
2364
Steve Naroffd3ded1f2008-06-05 00:02:44 +00002365 case tok::less:
Chris Lattner3bd934a2008-07-26 01:18:38 +00002366 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattnerbce61352008-07-26 00:20:22 +00002367 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
2368 // but we support it.
David Blaikie4e4d0842012-03-11 07:00:24 +00002369 if (DS.hasTypeSpecifier() || !getLangOpts().ObjC1)
Chris Lattnerbce61352008-07-26 00:20:22 +00002370 goto DoneWithDeclSpec;
Mike Stump1eb44332009-09-09 15:08:12 +00002371
Douglas Gregor46f936e2010-11-19 17:10:50 +00002372 if (!ParseObjCProtocolQualifiers(DS))
2373 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
2374 << FixItHint::CreateInsertion(Loc, "id")
2375 << SourceRange(Loc, DS.getSourceRange().getEnd());
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00002376
2377 // Need to support trailing type qualifiers (e.g. "id<p> const").
2378 // If a type specifier follows, it will be diagnosed elsewhere.
2379 continue;
Reid Spencer5f016e22007-07-11 17:01:13 +00002380 }
John McCallfec54012009-08-03 20:12:06 +00002381 // If the specifier wasn't legal, issue a diagnostic.
Reid Spencer5f016e22007-07-11 17:01:13 +00002382 if (isInvalid) {
2383 assert(PrevSpec && "Method did not return previous specifier!");
John McCallfec54012009-08-03 20:12:06 +00002384 assert(DiagID);
Douglas Gregorae2fb142010-08-23 14:34:43 +00002385
2386 if (DiagID == diag::ext_duplicate_declspec)
2387 Diag(Tok, DiagID)
2388 << PrevSpec << FixItHint::CreateRemoval(Tok.getLocation());
2389 else
2390 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00002391 }
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002392
Chris Lattner81c018d2008-03-13 06:29:04 +00002393 DS.SetRangeEnd(Tok.getLocation());
Fariborz Jahaniane106a0b2011-04-19 21:42:37 +00002394 if (DiagID != diag::err_bool_redeclaration)
2395 ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00002396 }
2397}
Douglas Gregoradcac882008-12-01 23:54:00 +00002398
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002399/// ParseStructDeclaration - Parse a struct declaration without the terminating
2400/// semicolon.
2401///
Reid Spencer5f016e22007-07-11 17:01:13 +00002402/// struct-declaration:
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002403/// specifier-qualifier-list struct-declarator-list
Reid Spencer5f016e22007-07-11 17:01:13 +00002404/// [GNU] __extension__ struct-declaration
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002405/// [GNU] specifier-qualifier-list
Reid Spencer5f016e22007-07-11 17:01:13 +00002406/// struct-declarator-list:
2407/// struct-declarator
2408/// struct-declarator-list ',' struct-declarator
2409/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
2410/// struct-declarator:
2411/// declarator
2412/// [GNU] declarator attributes[opt]
2413/// declarator[opt] ':' constant-expression
2414/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
2415///
Chris Lattnere1359422008-04-10 06:46:29 +00002416void Parser::
John McCallbdd563e2009-11-03 02:38:08 +00002417ParseStructDeclaration(DeclSpec &DS, FieldCallback &Fields) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002418
Chris Lattnerc46d1a12008-10-20 06:45:43 +00002419 if (Tok.is(tok::kw___extension__)) {
2420 // __extension__ silences extension warnings in the subexpression.
2421 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroff28a7ca82007-08-20 22:28:22 +00002422 ConsumeToken();
Chris Lattnerc46d1a12008-10-20 06:45:43 +00002423 return ParseStructDeclaration(DS, Fields);
2424 }
Mike Stump1eb44332009-09-09 15:08:12 +00002425
Steve Naroff28a7ca82007-08-20 22:28:22 +00002426 // Parse the common specifier-qualifiers-list piece.
Steve Naroff28a7ca82007-08-20 22:28:22 +00002427 ParseSpecifierQualifierList(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00002428
Douglas Gregor4920f1f2009-01-12 22:49:06 +00002429 // If there are no declarators, this is a free-standing declaration
2430 // specifier. Let the actions module cope with it.
Chris Lattner04d66662007-10-09 17:33:22 +00002431 if (Tok.is(tok::semi)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00002432 Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none, DS);
Steve Naroff28a7ca82007-08-20 22:28:22 +00002433 return;
2434 }
2435
2436 // Read struct-declarators until we find the semicolon.
John McCallbdd563e2009-11-03 02:38:08 +00002437 bool FirstDeclarator = true;
Richard Smith7984de32012-01-12 23:53:29 +00002438 SourceLocation CommaLoc;
Steve Naroff28a7ca82007-08-20 22:28:22 +00002439 while (1) {
John McCall54abf7d2009-11-04 02:18:39 +00002440 ParsingDeclRAIIObject PD(*this);
John McCallbdd563e2009-11-03 02:38:08 +00002441 FieldDeclarator DeclaratorInfo(DS);
Richard Smith7984de32012-01-12 23:53:29 +00002442 DeclaratorInfo.D.setCommaLoc(CommaLoc);
John McCallbdd563e2009-11-03 02:38:08 +00002443
2444 // Attributes are only allowed here on successive declarators.
John McCall7f040a92010-12-24 02:08:15 +00002445 if (!FirstDeclarator)
2446 MaybeParseGNUAttributes(DeclaratorInfo.D);
Mike Stump1eb44332009-09-09 15:08:12 +00002447
Steve Naroff28a7ca82007-08-20 22:28:22 +00002448 /// struct-declarator: declarator
2449 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattnera1efc8c2009-12-10 01:59:24 +00002450 if (Tok.isNot(tok::colon)) {
2451 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
2452 ColonProtectionRAIIObject X(*this);
Chris Lattnere1359422008-04-10 06:46:29 +00002453 ParseDeclarator(DeclaratorInfo.D);
Chris Lattnera1efc8c2009-12-10 01:59:24 +00002454 }
Mike Stump1eb44332009-09-09 15:08:12 +00002455
Chris Lattner04d66662007-10-09 17:33:22 +00002456 if (Tok.is(tok::colon)) {
Steve Naroff28a7ca82007-08-20 22:28:22 +00002457 ConsumeToken();
John McCall60d7b3a2010-08-24 06:29:42 +00002458 ExprResult Res(ParseConstantExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002459 if (Res.isInvalid())
Steve Naroff28a7ca82007-08-20 22:28:22 +00002460 SkipUntil(tok::semi, true, true);
Chris Lattner60b1e3e2008-04-10 06:15:14 +00002461 else
Sebastian Redleffa8d12008-12-10 00:02:53 +00002462 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroff28a7ca82007-08-20 22:28:22 +00002463 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00002464
Steve Naroff28a7ca82007-08-20 22:28:22 +00002465 // If attributes exist after the declarator, parse them.
John McCall7f040a92010-12-24 02:08:15 +00002466 MaybeParseGNUAttributes(DeclaratorInfo.D);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002467
John McCallbdd563e2009-11-03 02:38:08 +00002468 // We're done with this declarator; invoke the callback.
John McCalld226f652010-08-21 09:40:31 +00002469 Decl *D = Fields.invoke(DeclaratorInfo);
John McCall54abf7d2009-11-04 02:18:39 +00002470 PD.complete(D);
John McCallbdd563e2009-11-03 02:38:08 +00002471
Steve Naroff28a7ca82007-08-20 22:28:22 +00002472 // If we don't have a comma, it is either the end of the list (a ';')
2473 // or an error, bail out.
Chris Lattner04d66662007-10-09 17:33:22 +00002474 if (Tok.isNot(tok::comma))
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002475 return;
Sebastian Redlab197ba2009-02-09 18:23:29 +00002476
Steve Naroff28a7ca82007-08-20 22:28:22 +00002477 // Consume the comma.
Richard Smith7984de32012-01-12 23:53:29 +00002478 CommaLoc = ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00002479
John McCallbdd563e2009-11-03 02:38:08 +00002480 FirstDeclarator = false;
Steve Naroff28a7ca82007-08-20 22:28:22 +00002481 }
Steve Naroff28a7ca82007-08-20 22:28:22 +00002482}
2483
2484/// ParseStructUnionBody
2485/// struct-contents:
2486/// struct-declaration-list
2487/// [EXT] empty
2488/// [GNU] "struct-declaration-list" without terminatoring ';'
2489/// struct-declaration-list:
2490/// struct-declaration
2491/// struct-declaration-list struct-declaration
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002492/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroff28a7ca82007-08-20 22:28:22 +00002493///
Reid Spencer5f016e22007-07-11 17:01:13 +00002494void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
John McCalld226f652010-08-21 09:40:31 +00002495 unsigned TagType, Decl *TagDecl) {
John McCallf312b1e2010-08-26 23:41:50 +00002496 PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc,
2497 "parsing struct/union body");
Mike Stump1eb44332009-09-09 15:08:12 +00002498
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002499 BalancedDelimiterTracker T(*this, tok::l_brace);
2500 if (T.consumeOpen())
2501 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002502
Douglas Gregor3218c4b2009-01-09 22:42:13 +00002503 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
Douglas Gregor23c94db2010-07-02 17:43:08 +00002504 Actions.ActOnTagStartDefinition(getCurScope(), TagDecl);
Douglas Gregor72de6672009-01-08 20:45:30 +00002505
Reid Spencer5f016e22007-07-11 17:01:13 +00002506 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
2507 // C++.
David Blaikie4e4d0842012-03-11 07:00:24 +00002508 if (Tok.is(tok::r_brace) && !getLangOpts().CPlusPlus) {
Richard Smithd7c56e12011-12-29 21:57:33 +00002509 Diag(Tok, diag::ext_empty_struct_union) << (TagType == TST_union);
2510 Diag(Tok, diag::warn_empty_struct_union_compat) << (TagType == TST_union);
2511 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002512
Chris Lattner5f9e2722011-07-23 10:55:15 +00002513 SmallVector<Decl *, 32> FieldDecls;
Chris Lattnere1359422008-04-10 06:46:29 +00002514
Reid Spencer5f016e22007-07-11 17:01:13 +00002515 // While we still have something to read, read the declarations in the struct.
Chris Lattner04d66662007-10-09 17:33:22 +00002516 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002517 // Each iteration of this loop reads one struct-declaration.
Mike Stump1eb44332009-09-09 15:08:12 +00002518
Reid Spencer5f016e22007-07-11 17:01:13 +00002519 // Check for extraneous top-level semicolon.
Chris Lattner04d66662007-10-09 17:33:22 +00002520 if (Tok.is(tok::semi)) {
Douglas Gregor9b3064b2009-04-01 22:41:11 +00002521 Diag(Tok, diag::ext_extra_struct_semi)
Douglas Gregorf13ca062010-06-16 23:08:59 +00002522 << DeclSpec::getSpecifierName((DeclSpec::TST)TagType)
Douglas Gregor849b2432010-03-31 17:46:05 +00002523 << FixItHint::CreateRemoval(Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00002524 ConsumeToken();
2525 continue;
2526 }
Chris Lattnere1359422008-04-10 06:46:29 +00002527
2528 // Parse all the comma separated declarators.
John McCall0b7e6782011-03-24 11:26:52 +00002529 DeclSpec DS(AttrFactory);
Mike Stump1eb44332009-09-09 15:08:12 +00002530
John McCallbdd563e2009-11-03 02:38:08 +00002531 if (!Tok.is(tok::at)) {
2532 struct CFieldCallback : FieldCallback {
2533 Parser &P;
John McCalld226f652010-08-21 09:40:31 +00002534 Decl *TagDecl;
Chris Lattner5f9e2722011-07-23 10:55:15 +00002535 SmallVectorImpl<Decl *> &FieldDecls;
John McCallbdd563e2009-11-03 02:38:08 +00002536
John McCalld226f652010-08-21 09:40:31 +00002537 CFieldCallback(Parser &P, Decl *TagDecl,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002538 SmallVectorImpl<Decl *> &FieldDecls) :
John McCallbdd563e2009-11-03 02:38:08 +00002539 P(P), TagDecl(TagDecl), FieldDecls(FieldDecls) {}
2540
John McCalld226f652010-08-21 09:40:31 +00002541 virtual Decl *invoke(FieldDeclarator &FD) {
John McCallbdd563e2009-11-03 02:38:08 +00002542 // Install the declarator into the current TagDecl.
John McCalld226f652010-08-21 09:40:31 +00002543 Decl *Field = P.Actions.ActOnField(P.getCurScope(), TagDecl,
John McCall4ba39712009-11-03 21:13:47 +00002544 FD.D.getDeclSpec().getSourceRange().getBegin(),
2545 FD.D, FD.BitfieldSize);
John McCallbdd563e2009-11-03 02:38:08 +00002546 FieldDecls.push_back(Field);
2547 return Field;
Douglas Gregor91a28862009-08-26 14:27:30 +00002548 }
John McCallbdd563e2009-11-03 02:38:08 +00002549 } Callback(*this, TagDecl, FieldDecls);
2550
2551 ParseStructDeclaration(DS, Callback);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002552 } else { // Handle @defs
2553 ConsumeToken();
2554 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
2555 Diag(Tok, diag::err_unexpected_at);
Chris Lattner3e156ad2010-02-02 00:37:27 +00002556 SkipUntil(tok::semi, true);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002557 continue;
2558 }
2559 ConsumeToken();
2560 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
2561 if (!Tok.is(tok::identifier)) {
2562 Diag(Tok, diag::err_expected_ident);
Chris Lattner3e156ad2010-02-02 00:37:27 +00002563 SkipUntil(tok::semi, true);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002564 continue;
2565 }
Chris Lattner5f9e2722011-07-23 10:55:15 +00002566 SmallVector<Decl *, 16> Fields;
Douglas Gregor23c94db2010-07-02 17:43:08 +00002567 Actions.ActOnDefs(getCurScope(), TagDecl, Tok.getLocation(),
Douglas Gregor44b43212008-12-11 16:49:14 +00002568 Tok.getIdentifierInfo(), Fields);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002569 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
2570 ConsumeToken();
2571 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
Mike Stump1eb44332009-09-09 15:08:12 +00002572 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002573
Chris Lattner04d66662007-10-09 17:33:22 +00002574 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002575 ConsumeToken();
Chris Lattner04d66662007-10-09 17:33:22 +00002576 } else if (Tok.is(tok::r_brace)) {
Chris Lattner3e156ad2010-02-02 00:37:27 +00002577 ExpectAndConsume(tok::semi, diag::ext_expected_semi_decl_list);
Reid Spencer5f016e22007-07-11 17:01:13 +00002578 break;
2579 } else {
Chris Lattner3e156ad2010-02-02 00:37:27 +00002580 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
2581 // Skip to end of block or statement to avoid ext-warning on extra ';'.
Reid Spencer5f016e22007-07-11 17:01:13 +00002582 SkipUntil(tok::r_brace, true, true);
Chris Lattner3e156ad2010-02-02 00:37:27 +00002583 // If we stopped at a ';', eat it.
2584 if (Tok.is(tok::semi)) ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00002585 }
2586 }
Mike Stump1eb44332009-09-09 15:08:12 +00002587
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002588 T.consumeClose();
Mike Stump1eb44332009-09-09 15:08:12 +00002589
John McCall0b7e6782011-03-24 11:26:52 +00002590 ParsedAttributes attrs(AttrFactory);
Reid Spencer5f016e22007-07-11 17:01:13 +00002591 // If attributes exist after struct contents, parse them.
John McCall7f040a92010-12-24 02:08:15 +00002592 MaybeParseGNUAttributes(attrs);
Daniel Dunbar1bfe1c22008-10-03 02:03:53 +00002593
Douglas Gregor23c94db2010-07-02 17:43:08 +00002594 Actions.ActOnFields(getCurScope(),
David Blaikie77b6de02011-09-22 02:58:26 +00002595 RecordLoc, TagDecl, FieldDecls,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002596 T.getOpenLocation(), T.getCloseLocation(),
John McCall7f040a92010-12-24 02:08:15 +00002597 attrs.getList());
Douglas Gregor72de6672009-01-08 20:45:30 +00002598 StructScope.Exit();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002599 Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl,
2600 T.getCloseLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00002601}
2602
Reid Spencer5f016e22007-07-11 17:01:13 +00002603/// ParseEnumSpecifier
2604/// enum-specifier: [C99 6.7.2.2]
2605/// 'enum' identifier[opt] '{' enumerator-list '}'
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002606///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Reid Spencer5f016e22007-07-11 17:01:13 +00002607/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
2608/// '}' attributes[opt]
Aaron Ballman6454a022012-03-01 04:09:28 +00002609/// [MS] 'enum' __declspec[opt] identifier[opt] '{' enumerator-list ',' [opt]
2610/// '}'
Reid Spencer5f016e22007-07-11 17:01:13 +00002611/// 'enum' identifier
2612/// [GNU] 'enum' attributes[opt] identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002613///
Richard Smith1af83c42012-03-23 03:33:32 +00002614/// [C++11] enum-head '{' enumerator-list[opt] '}'
2615/// [C++11] enum-head '{' enumerator-list ',' '}'
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002616///
Richard Smith1af83c42012-03-23 03:33:32 +00002617/// enum-head: [C++11]
2618/// enum-key attribute-specifier-seq[opt] identifier[opt] enum-base[opt]
2619/// enum-key attribute-specifier-seq[opt] nested-name-specifier
2620/// identifier enum-base[opt]
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002621///
Richard Smith1af83c42012-03-23 03:33:32 +00002622/// enum-key: [C++11]
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002623/// 'enum'
2624/// 'enum' 'class'
2625/// 'enum' 'struct'
2626///
Richard Smith1af83c42012-03-23 03:33:32 +00002627/// enum-base: [C++11]
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002628/// ':' type-specifier-seq
2629///
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002630/// [C++] elaborated-type-specifier:
2631/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
2632///
Chris Lattner4c97d762009-04-12 21:49:30 +00002633void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregor9b9edd62010-03-02 17:53:14 +00002634 const ParsedTemplateInfo &TemplateInfo,
Richard Smith69730c12012-03-12 07:56:15 +00002635 AccessSpecifier AS, DeclSpecContext DSC) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002636 // Parse the tag portion of this.
Douglas Gregor374929f2009-09-18 15:37:17 +00002637 if (Tok.is(tok::code_completion)) {
2638 // Code completion for an enum name.
Douglas Gregor23c94db2010-07-02 17:43:08 +00002639 Actions.CodeCompleteTag(getCurScope(), DeclSpec::TST_enum);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002640 return cutOffParsing();
Douglas Gregor374929f2009-09-18 15:37:17 +00002641 }
John McCall57c13002011-07-06 05:58:41 +00002642
Richard Smithbdad7a22012-01-10 01:33:14 +00002643 SourceLocation ScopedEnumKWLoc;
John McCall57c13002011-07-06 05:58:41 +00002644 bool IsScopedUsingClassTag = false;
2645
David Blaikie4e4d0842012-03-11 07:00:24 +00002646 if (getLangOpts().CPlusPlus0x &&
John McCall57c13002011-07-06 05:58:41 +00002647 (Tok.is(tok::kw_class) || Tok.is(tok::kw_struct))) {
Richard Smith7fe62082011-10-15 05:09:34 +00002648 Diag(Tok, diag::warn_cxx98_compat_scoped_enum);
John McCall57c13002011-07-06 05:58:41 +00002649 IsScopedUsingClassTag = Tok.is(tok::kw_class);
Richard Smithbdad7a22012-01-10 01:33:14 +00002650 ScopedEnumKWLoc = ConsumeToken();
John McCall57c13002011-07-06 05:58:41 +00002651 }
Richard Smith1af83c42012-03-23 03:33:32 +00002652
2653 // C++11 [temp.explicit]p12: The usual access controls do not apply to names
2654 // used to specify explicit instantiations. We extend this to also cover
2655 // explicit specializations.
2656 Sema::SuppressAccessChecksRAII SuppressAccess(Actions,
2657 TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation ||
2658 TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization);
2659
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002660 // If attributes exist after tag, parse them.
John McCall0b7e6782011-03-24 11:26:52 +00002661 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00002662 MaybeParseGNUAttributes(attrs);
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002663
Aaron Ballman6454a022012-03-01 04:09:28 +00002664 // If declspecs exist after tag, parse them.
2665 while (Tok.is(tok::kw___declspec))
2666 ParseMicrosoftDeclSpec(attrs);
2667
Richard Smith7796eb52012-03-12 08:56:40 +00002668 // Enum definitions should not be parsed in a trailing-return-type.
2669 bool AllowDeclaration = DSC != DSC_trailing;
2670
2671 bool AllowFixedUnderlyingType = AllowDeclaration &&
2672 (getLangOpts().CPlusPlus0x || getLangOpts().MicrosoftExt ||
2673 getLangOpts().ObjC2);
John McCall57c13002011-07-06 05:58:41 +00002674
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00002675 CXXScopeSpec &SS = DS.getTypeSpecScope();
David Blaikie4e4d0842012-03-11 07:00:24 +00002676 if (getLangOpts().CPlusPlus) {
John McCall57c13002011-07-06 05:58:41 +00002677 // "enum foo : bar;" is not a potential typo for "enum foo::bar;"
2678 // if a fixed underlying type is allowed.
2679 ColonProtectionRAIIObject X(*this, AllowFixedUnderlyingType);
2680
Douglas Gregorefaa93a2011-11-07 17:33:42 +00002681 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
2682 /*EnteringContext=*/false))
John McCall9ba61662010-02-26 08:45:28 +00002683 return;
2684
2685 if (SS.isSet() && Tok.isNot(tok::identifier)) {
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002686 Diag(Tok, diag::err_expected_ident);
2687 if (Tok.isNot(tok::l_brace)) {
2688 // Has no name and is not a definition.
2689 // Skip the rest of this declarator, up until the comma or semicolon.
2690 SkipUntil(tok::comma, true);
2691 return;
2692 }
2693 }
2694 }
Mike Stump1eb44332009-09-09 15:08:12 +00002695
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002696 // Must have either 'enum name' or 'enum {...}'.
Douglas Gregorb9075602011-02-22 02:55:24 +00002697 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace) &&
Richard Smith7796eb52012-03-12 08:56:40 +00002698 !(AllowFixedUnderlyingType && Tok.is(tok::colon))) {
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002699 Diag(Tok, diag::err_expected_ident_lbrace);
Mike Stump1eb44332009-09-09 15:08:12 +00002700
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002701 // Skip the rest of this declarator, up until the comma or semicolon.
2702 SkipUntil(tok::comma, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00002703 return;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002704 }
Mike Stump1eb44332009-09-09 15:08:12 +00002705
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002706 // If an identifier is present, consume and remember it.
2707 IdentifierInfo *Name = 0;
2708 SourceLocation NameLoc;
2709 if (Tok.is(tok::identifier)) {
2710 Name = Tok.getIdentifierInfo();
2711 NameLoc = ConsumeToken();
2712 }
Mike Stump1eb44332009-09-09 15:08:12 +00002713
Richard Smithbdad7a22012-01-10 01:33:14 +00002714 if (!Name && ScopedEnumKWLoc.isValid()) {
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002715 // C++0x 7.2p2: The optional identifier shall not be omitted in the
2716 // declaration of a scoped enumeration.
2717 Diag(Tok, diag::err_scoped_enum_missing_identifier);
Richard Smithbdad7a22012-01-10 01:33:14 +00002718 ScopedEnumKWLoc = SourceLocation();
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00002719 IsScopedUsingClassTag = false;
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002720 }
2721
Richard Smith1af83c42012-03-23 03:33:32 +00002722 // Stop suppressing access control now we've parsed the enum name.
2723 SuppressAccess.done();
2724
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002725 TypeResult BaseType;
2726
Douglas Gregora61b3e72010-12-01 17:42:47 +00002727 // Parse the fixed underlying type.
Douglas Gregorb9075602011-02-22 02:55:24 +00002728 if (AllowFixedUnderlyingType && Tok.is(tok::colon)) {
Douglas Gregora61b3e72010-12-01 17:42:47 +00002729 bool PossibleBitfield = false;
2730 if (getCurScope()->getFlags() & Scope::ClassScope) {
2731 // If we're in class scope, this can either be an enum declaration with
2732 // an underlying type, or a declaration of a bitfield member. We try to
2733 // use a simple disambiguation scheme first to catch the common cases
2734 // (integer literal, sizeof); if it's still ambiguous, we then consider
2735 // anything that's a simple-type-specifier followed by '(' as an
2736 // expression. This suffices because function types are not valid
2737 // underlying types anyway.
2738 TPResult TPR = isExpressionOrTypeSpecifierSimple(NextToken().getKind());
2739 // If the next token starts an expression, we know we're parsing a
2740 // bit-field. This is the common case.
2741 if (TPR == TPResult::True())
2742 PossibleBitfield = true;
2743 // If the next token starts a type-specifier-seq, it may be either a
2744 // a fixed underlying type or the start of a function-style cast in C++;
2745 // lookahead one more token to see if it's obvious that we have a
2746 // fixed underlying type.
2747 else if (TPR == TPResult::False() &&
2748 GetLookAheadToken(2).getKind() == tok::semi) {
2749 // Consume the ':'.
2750 ConsumeToken();
2751 } else {
2752 // We have the start of a type-specifier-seq, so we have to perform
2753 // tentative parsing to determine whether we have an expression or a
2754 // type.
2755 TentativeParsingAction TPA(*this);
2756
2757 // Consume the ':'.
2758 ConsumeToken();
Richard Smithd81e9612012-02-23 01:36:12 +00002759
2760 // If we see a type specifier followed by an open-brace, we have an
2761 // ambiguity between an underlying type and a C++11 braced
2762 // function-style cast. Resolve this by always treating it as an
2763 // underlying type.
2764 // FIXME: The standard is not entirely clear on how to disambiguate in
2765 // this case.
David Blaikie4e4d0842012-03-11 07:00:24 +00002766 if ((getLangOpts().CPlusPlus &&
Richard Smithd81e9612012-02-23 01:36:12 +00002767 isCXXDeclarationSpecifier(TPResult::True()) != TPResult::True()) ||
David Blaikie4e4d0842012-03-11 07:00:24 +00002768 (!getLangOpts().CPlusPlus && !isDeclarationSpecifier(true))) {
Douglas Gregora61b3e72010-12-01 17:42:47 +00002769 // We'll parse this as a bitfield later.
2770 PossibleBitfield = true;
2771 TPA.Revert();
2772 } else {
2773 // We have a type-specifier-seq.
2774 TPA.Commit();
2775 }
2776 }
2777 } else {
2778 // Consume the ':'.
2779 ConsumeToken();
2780 }
2781
2782 if (!PossibleBitfield) {
2783 SourceRange Range;
2784 BaseType = ParseTypeName(&Range);
Douglas Gregor86f208c2011-02-22 20:32:04 +00002785
David Blaikie4e4d0842012-03-11 07:00:24 +00002786 if (!getLangOpts().CPlusPlus0x && !getLangOpts().ObjC2)
Douglas Gregor86f208c2011-02-22 20:32:04 +00002787 Diag(StartLoc, diag::ext_ms_enum_fixed_underlying_type)
2788 << Range;
David Blaikie4e4d0842012-03-11 07:00:24 +00002789 if (getLangOpts().CPlusPlus0x)
Richard Smith7fe62082011-10-15 05:09:34 +00002790 Diag(StartLoc, diag::warn_cxx98_compat_enum_fixed_underlying_type);
Douglas Gregora61b3e72010-12-01 17:42:47 +00002791 }
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002792 }
2793
Richard Smithbdad7a22012-01-10 01:33:14 +00002794 // There are four options here. If we have 'friend enum foo;' then this is a
2795 // friend declaration, and cannot have an accompanying definition. If we have
2796 // 'enum foo;', then this is a forward declaration. If we have
2797 // 'enum foo {...' then this is a definition. Otherwise we have something
2798 // like 'enum foo xyz', a reference.
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002799 //
2800 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
2801 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
2802 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
2803 //
John McCallf312b1e2010-08-26 23:41:50 +00002804 Sema::TagUseKind TUK;
Richard Smithbdad7a22012-01-10 01:33:14 +00002805 if (DS.isFriendSpecified())
2806 TUK = Sema::TUK_Friend;
Richard Smith7796eb52012-03-12 08:56:40 +00002807 else if (!AllowDeclaration)
2808 TUK = Sema::TUK_Reference;
Richard Smithbdad7a22012-01-10 01:33:14 +00002809 else if (Tok.is(tok::l_brace))
John McCallf312b1e2010-08-26 23:41:50 +00002810 TUK = Sema::TUK_Definition;
Richard Smith69730c12012-03-12 07:56:15 +00002811 else if (Tok.is(tok::semi) && DSC != DSC_type_specifier)
John McCallf312b1e2010-08-26 23:41:50 +00002812 TUK = Sema::TUK_Declaration;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002813 else
John McCallf312b1e2010-08-26 23:41:50 +00002814 TUK = Sema::TUK_Reference;
Richard Smith1af83c42012-03-23 03:33:32 +00002815
2816 MultiTemplateParamsArg TParams;
Douglas Gregor8fc6d232010-05-03 17:48:54 +00002817 if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate &&
John McCallf312b1e2010-08-26 23:41:50 +00002818 TUK != Sema::TUK_Reference) {
Richard Smith1af83c42012-03-23 03:33:32 +00002819 if (!getLangOpts().CPlusPlus0x || !SS.isSet()) {
2820 // Skip the rest of this declarator, up until the comma or semicolon.
2821 Diag(Tok, diag::err_enum_template);
2822 SkipUntil(tok::comma, true);
2823 return;
2824 }
2825
2826 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) {
2827 // Enumerations can't be explicitly instantiated.
2828 DS.SetTypeSpecError();
2829 Diag(StartLoc, diag::err_explicit_instantiation_enum);
2830 return;
2831 }
2832
2833 assert(TemplateInfo.TemplateParams && "no template parameters");
2834 TParams = MultiTemplateParamsArg(TemplateInfo.TemplateParams->data(),
2835 TemplateInfo.TemplateParams->size());
Douglas Gregor8fc6d232010-05-03 17:48:54 +00002836 }
Richard Smith1af83c42012-03-23 03:33:32 +00002837
Douglas Gregorb9075602011-02-22 02:55:24 +00002838 if (!Name && TUK != Sema::TUK_Definition) {
2839 Diag(Tok, diag::err_enumerator_unnamed_no_def);
Richard Smith1af83c42012-03-23 03:33:32 +00002840
Douglas Gregorb9075602011-02-22 02:55:24 +00002841 // Skip the rest of this declarator, up until the comma or semicolon.
2842 SkipUntil(tok::comma, true);
2843 return;
2844 }
Richard Smith1af83c42012-03-23 03:33:32 +00002845
Douglas Gregor402abb52009-05-28 23:31:59 +00002846 bool Owned = false;
John McCallc4e70192009-09-11 04:59:25 +00002847 bool IsDependent = false;
Douglas Gregor48c89f42010-04-24 16:38:41 +00002848 const char *PrevSpec = 0;
2849 unsigned DiagID;
John McCalld226f652010-08-21 09:40:31 +00002850 Decl *TagDecl = Actions.ActOnTag(getCurScope(), DeclSpec::TST_enum, TUK,
John McCall7f040a92010-12-24 02:08:15 +00002851 StartLoc, SS, Name, NameLoc, attrs.getList(),
Richard Smith1af83c42012-03-23 03:33:32 +00002852 AS, DS.getModulePrivateSpecLoc(), TParams,
Richard Smithbdad7a22012-01-10 01:33:14 +00002853 Owned, IsDependent, ScopedEnumKWLoc,
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00002854 IsScopedUsingClassTag, BaseType);
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002855
Douglas Gregor48c89f42010-04-24 16:38:41 +00002856 if (IsDependent) {
2857 // This enum has a dependent nested-name-specifier. Handle it as a
2858 // dependent tag.
2859 if (!Name) {
2860 DS.SetTypeSpecError();
2861 Diag(Tok, diag::err_expected_type_name_after_typename);
2862 return;
2863 }
2864
Douglas Gregor23c94db2010-07-02 17:43:08 +00002865 TypeResult Type = Actions.ActOnDependentTag(getCurScope(), DeclSpec::TST_enum,
Douglas Gregor48c89f42010-04-24 16:38:41 +00002866 TUK, SS, Name, StartLoc,
2867 NameLoc);
2868 if (Type.isInvalid()) {
2869 DS.SetTypeSpecError();
2870 return;
2871 }
2872
Abramo Bagnara0daaf322011-03-16 20:16:18 +00002873 if (DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc,
2874 NameLoc.isValid() ? NameLoc : StartLoc,
2875 PrevSpec, DiagID, Type.get()))
Douglas Gregor48c89f42010-04-24 16:38:41 +00002876 Diag(StartLoc, DiagID) << PrevSpec;
2877
2878 return;
2879 }
Mike Stump1eb44332009-09-09 15:08:12 +00002880
John McCalld226f652010-08-21 09:40:31 +00002881 if (!TagDecl) {
Douglas Gregor48c89f42010-04-24 16:38:41 +00002882 // The action failed to produce an enumeration tag. If this is a
2883 // definition, consume the entire definition.
Richard Smith7796eb52012-03-12 08:56:40 +00002884 if (Tok.is(tok::l_brace) && TUK != Sema::TUK_Reference) {
Douglas Gregor48c89f42010-04-24 16:38:41 +00002885 ConsumeBrace();
2886 SkipUntil(tok::r_brace);
2887 }
2888
2889 DS.SetTypeSpecError();
2890 return;
2891 }
Richard Smithbdad7a22012-01-10 01:33:14 +00002892
Richard Smith7796eb52012-03-12 08:56:40 +00002893 if (Tok.is(tok::l_brace) && TUK != Sema::TUK_Reference) {
Richard Smith1af83c42012-03-23 03:33:32 +00002894 if (TUK == Sema::TUK_Friend) {
Richard Smithbdad7a22012-01-10 01:33:14 +00002895 Diag(Tok, diag::err_friend_decl_defines_type)
2896 << SourceRange(DS.getFriendSpecLoc());
Richard Smith1af83c42012-03-23 03:33:32 +00002897 ConsumeBrace();
2898 SkipUntil(tok::r_brace);
2899 } else {
2900 ParseEnumBody(StartLoc, TagDecl);
2901 }
Richard Smithbdad7a22012-01-10 01:33:14 +00002902 }
Mike Stump1eb44332009-09-09 15:08:12 +00002903
Abramo Bagnara0daaf322011-03-16 20:16:18 +00002904 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc,
2905 NameLoc.isValid() ? NameLoc : StartLoc,
2906 PrevSpec, DiagID, TagDecl, Owned))
John McCallfec54012009-08-03 20:12:06 +00002907 Diag(StartLoc, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00002908}
2909
2910/// ParseEnumBody - Parse a {} enclosed enumerator-list.
2911/// enumerator-list:
2912/// enumerator
2913/// enumerator-list ',' enumerator
2914/// enumerator:
2915/// enumeration-constant
2916/// enumeration-constant '=' constant-expression
2917/// enumeration-constant:
2918/// identifier
2919///
John McCalld226f652010-08-21 09:40:31 +00002920void Parser::ParseEnumBody(SourceLocation StartLoc, Decl *EnumDecl) {
Douglas Gregor074149e2009-01-05 19:45:36 +00002921 // Enter the scope of the enum body and start the definition.
2922 ParseScope EnumScope(this, Scope::DeclScope);
Douglas Gregor23c94db2010-07-02 17:43:08 +00002923 Actions.ActOnTagStartDefinition(getCurScope(), EnumDecl);
Douglas Gregor074149e2009-01-05 19:45:36 +00002924
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002925 BalancedDelimiterTracker T(*this, tok::l_brace);
2926 T.consumeOpen();
Mike Stump1eb44332009-09-09 15:08:12 +00002927
Chris Lattner7946dd32007-08-27 17:24:30 +00002928 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
David Blaikie4e4d0842012-03-11 07:00:24 +00002929 if (Tok.is(tok::r_brace) && !getLangOpts().CPlusPlus)
Fariborz Jahanian05115522010-05-28 22:23:22 +00002930 Diag(Tok, diag::error_empty_enum);
Mike Stump1eb44332009-09-09 15:08:12 +00002931
Chris Lattner5f9e2722011-07-23 10:55:15 +00002932 SmallVector<Decl *, 32> EnumConstantDecls;
Reid Spencer5f016e22007-07-11 17:01:13 +00002933
John McCalld226f652010-08-21 09:40:31 +00002934 Decl *LastEnumConstDecl = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00002935
Reid Spencer5f016e22007-07-11 17:01:13 +00002936 // Parse the enumerator-list.
Chris Lattner04d66662007-10-09 17:33:22 +00002937 while (Tok.is(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002938 IdentifierInfo *Ident = Tok.getIdentifierInfo();
2939 SourceLocation IdentLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00002940
John McCall5b629aa2010-10-22 23:36:17 +00002941 // If attributes exist after the enumerator, parse them.
John McCall0b7e6782011-03-24 11:26:52 +00002942 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00002943 MaybeParseGNUAttributes(attrs);
John McCall5b629aa2010-10-22 23:36:17 +00002944
Reid Spencer5f016e22007-07-11 17:01:13 +00002945 SourceLocation EqualLoc;
John McCall60d7b3a2010-08-24 06:29:42 +00002946 ExprResult AssignedVal;
Fariborz Jahanian5a477db2011-12-09 01:15:54 +00002947 ParsingDeclRAIIObject PD(*this);
2948
Chris Lattner04d66662007-10-09 17:33:22 +00002949 if (Tok.is(tok::equal)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002950 EqualLoc = ConsumeToken();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002951 AssignedVal = ParseConstantExpression();
2952 if (AssignedVal.isInvalid())
Reid Spencer5f016e22007-07-11 17:01:13 +00002953 SkipUntil(tok::comma, tok::r_brace, true, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00002954 }
Mike Stump1eb44332009-09-09 15:08:12 +00002955
Reid Spencer5f016e22007-07-11 17:01:13 +00002956 // Install the enumerator constant into EnumDecl.
John McCalld226f652010-08-21 09:40:31 +00002957 Decl *EnumConstDecl = Actions.ActOnEnumConstant(getCurScope(), EnumDecl,
2958 LastEnumConstDecl,
2959 IdentLoc, Ident,
John McCall7f040a92010-12-24 02:08:15 +00002960 attrs.getList(), EqualLoc,
John McCalld226f652010-08-21 09:40:31 +00002961 AssignedVal.release());
Fariborz Jahanian5a477db2011-12-09 01:15:54 +00002962 PD.complete(EnumConstDecl);
2963
Reid Spencer5f016e22007-07-11 17:01:13 +00002964 EnumConstantDecls.push_back(EnumConstDecl);
2965 LastEnumConstDecl = EnumConstDecl;
Mike Stump1eb44332009-09-09 15:08:12 +00002966
Douglas Gregor751f6922010-09-07 14:51:08 +00002967 if (Tok.is(tok::identifier)) {
2968 // We're missing a comma between enumerators.
2969 SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation);
2970 Diag(Loc, diag::err_enumerator_list_missing_comma)
2971 << FixItHint::CreateInsertion(Loc, ", ");
2972 continue;
2973 }
2974
Chris Lattner04d66662007-10-09 17:33:22 +00002975 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +00002976 break;
2977 SourceLocation CommaLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00002978
Richard Smith7fe62082011-10-15 05:09:34 +00002979 if (Tok.isNot(tok::identifier)) {
David Blaikie4e4d0842012-03-11 07:00:24 +00002980 if (!getLangOpts().C99 && !getLangOpts().CPlusPlus0x)
Richard Smith7fe62082011-10-15 05:09:34 +00002981 Diag(CommaLoc, diag::ext_enumerator_list_comma)
David Blaikie4e4d0842012-03-11 07:00:24 +00002982 << getLangOpts().CPlusPlus
Richard Smith7fe62082011-10-15 05:09:34 +00002983 << FixItHint::CreateRemoval(CommaLoc);
David Blaikie4e4d0842012-03-11 07:00:24 +00002984 else if (getLangOpts().CPlusPlus0x)
Richard Smith7fe62082011-10-15 05:09:34 +00002985 Diag(CommaLoc, diag::warn_cxx98_compat_enumerator_list_comma)
2986 << FixItHint::CreateRemoval(CommaLoc);
2987 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002988 }
Mike Stump1eb44332009-09-09 15:08:12 +00002989
Reid Spencer5f016e22007-07-11 17:01:13 +00002990 // Eat the }.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002991 T.consumeClose();
Reid Spencer5f016e22007-07-11 17:01:13 +00002992
Reid Spencer5f016e22007-07-11 17:01:13 +00002993 // If attributes exist after the identifier list, parse them.
John McCall0b7e6782011-03-24 11:26:52 +00002994 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00002995 MaybeParseGNUAttributes(attrs);
Douglas Gregor72de6672009-01-08 20:45:30 +00002996
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002997 Actions.ActOnEnumBody(StartLoc, T.getOpenLocation(), T.getCloseLocation(),
2998 EnumDecl, EnumConstantDecls.data(),
2999 EnumConstantDecls.size(), getCurScope(),
3000 attrs.getList());
Mike Stump1eb44332009-09-09 15:08:12 +00003001
Douglas Gregor72de6672009-01-08 20:45:30 +00003002 EnumScope.Exit();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003003 Actions.ActOnTagFinishDefinition(getCurScope(), EnumDecl,
3004 T.getCloseLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00003005}
3006
3007/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff5f8aa692008-02-11 23:15:56 +00003008/// start of a type-qualifier-list.
3009bool Parser::isTypeQualifier() const {
3010 switch (Tok.getKind()) {
3011 default: return false;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003012
3013 // type-qualifier only in OpenCL
3014 case tok::kw_private:
David Blaikie4e4d0842012-03-11 07:00:24 +00003015 return getLangOpts().OpenCL;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003016
Steve Naroff5f8aa692008-02-11 23:15:56 +00003017 // type-qualifier
3018 case tok::kw_const:
3019 case tok::kw_volatile:
3020 case tok::kw_restrict:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003021 case tok::kw___private:
3022 case tok::kw___local:
3023 case tok::kw___global:
3024 case tok::kw___constant:
3025 case tok::kw___read_only:
3026 case tok::kw___read_write:
3027 case tok::kw___write_only:
Steve Naroff5f8aa692008-02-11 23:15:56 +00003028 return true;
3029 }
3030}
3031
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003032/// isKnownToBeTypeSpecifier - Return true if we know that the specified token
3033/// is definitely a type-specifier. Return false if it isn't part of a type
3034/// specifier or if we're not sure.
3035bool Parser::isKnownToBeTypeSpecifier(const Token &Tok) const {
3036 switch (Tok.getKind()) {
3037 default: return false;
3038 // type-specifiers
3039 case tok::kw_short:
3040 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00003041 case tok::kw___int64:
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003042 case tok::kw_signed:
3043 case tok::kw_unsigned:
3044 case tok::kw__Complex:
3045 case tok::kw__Imaginary:
3046 case tok::kw_void:
3047 case tok::kw_char:
3048 case tok::kw_wchar_t:
3049 case tok::kw_char16_t:
3050 case tok::kw_char32_t:
3051 case tok::kw_int:
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00003052 case tok::kw_half:
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003053 case tok::kw_float:
3054 case tok::kw_double:
3055 case tok::kw_bool:
3056 case tok::kw__Bool:
3057 case tok::kw__Decimal32:
3058 case tok::kw__Decimal64:
3059 case tok::kw__Decimal128:
3060 case tok::kw___vector:
3061
3062 // struct-or-union-specifier (C99) or class-specifier (C++)
3063 case tok::kw_class:
3064 case tok::kw_struct:
3065 case tok::kw_union:
3066 // enum-specifier
3067 case tok::kw_enum:
3068
3069 // typedef-name
3070 case tok::annot_typename:
3071 return true;
3072 }
3073}
3074
Steve Naroff5f8aa692008-02-11 23:15:56 +00003075/// isTypeSpecifierQualifier - Return true if the current token could be the
Reid Spencer5f016e22007-07-11 17:01:13 +00003076/// start of a specifier-qualifier-list.
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003077bool Parser::isTypeSpecifierQualifier() {
Reid Spencer5f016e22007-07-11 17:01:13 +00003078 switch (Tok.getKind()) {
3079 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00003080
Chris Lattner166a8fc2009-01-04 23:41:41 +00003081 case tok::identifier: // foo::bar
John Thompson82287d12010-02-05 00:12:22 +00003082 if (TryAltiVecVectorToken())
3083 return true;
3084 // Fall through.
Douglas Gregord57959a2009-03-27 23:10:48 +00003085 case tok::kw_typename: // typename T::type
Chris Lattner166a8fc2009-01-04 23:41:41 +00003086 // Annotate typenames and C++ scope specifiers. If we get one, just
3087 // recurse to handle whatever we get.
3088 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003089 return true;
3090 if (Tok.is(tok::identifier))
3091 return false;
3092 return isTypeSpecifierQualifier();
Douglas Gregord57959a2009-03-27 23:10:48 +00003093
Chris Lattner166a8fc2009-01-04 23:41:41 +00003094 case tok::coloncolon: // ::foo::bar
3095 if (NextToken().is(tok::kw_new) || // ::new
3096 NextToken().is(tok::kw_delete)) // ::delete
3097 return false;
3098
Chris Lattner166a8fc2009-01-04 23:41:41 +00003099 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003100 return true;
3101 return isTypeSpecifierQualifier();
Mike Stump1eb44332009-09-09 15:08:12 +00003102
Reid Spencer5f016e22007-07-11 17:01:13 +00003103 // GNU attributes support.
3104 case tok::kw___attribute:
Steve Naroffd1861fd2007-07-31 12:34:36 +00003105 // GNU typeof support.
3106 case tok::kw_typeof:
Mike Stump1eb44332009-09-09 15:08:12 +00003107
Reid Spencer5f016e22007-07-11 17:01:13 +00003108 // type-specifiers
3109 case tok::kw_short:
3110 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00003111 case tok::kw___int64:
Reid Spencer5f016e22007-07-11 17:01:13 +00003112 case tok::kw_signed:
3113 case tok::kw_unsigned:
3114 case tok::kw__Complex:
3115 case tok::kw__Imaginary:
3116 case tok::kw_void:
3117 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00003118 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00003119 case tok::kw_char16_t:
3120 case tok::kw_char32_t:
Reid Spencer5f016e22007-07-11 17:01:13 +00003121 case tok::kw_int:
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00003122 case tok::kw_half:
Reid Spencer5f016e22007-07-11 17:01:13 +00003123 case tok::kw_float:
3124 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00003125 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00003126 case tok::kw__Bool:
3127 case tok::kw__Decimal32:
3128 case tok::kw__Decimal64:
3129 case tok::kw__Decimal128:
John Thompson82287d12010-02-05 00:12:22 +00003130 case tok::kw___vector:
Mike Stump1eb44332009-09-09 15:08:12 +00003131
Chris Lattner99dc9142008-04-13 18:59:07 +00003132 // struct-or-union-specifier (C99) or class-specifier (C++)
3133 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00003134 case tok::kw_struct:
3135 case tok::kw_union:
3136 // enum-specifier
3137 case tok::kw_enum:
Mike Stump1eb44332009-09-09 15:08:12 +00003138
Reid Spencer5f016e22007-07-11 17:01:13 +00003139 // type-qualifier
3140 case tok::kw_const:
3141 case tok::kw_volatile:
3142 case tok::kw_restrict:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003143
3144 // typedef-name
Chris Lattnerb31757b2009-01-06 05:06:21 +00003145 case tok::annot_typename:
Reid Spencer5f016e22007-07-11 17:01:13 +00003146 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00003147
Chris Lattner7c186be2008-10-20 00:25:30 +00003148 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
3149 case tok::less:
David Blaikie4e4d0842012-03-11 07:00:24 +00003150 return getLangOpts().ObjC1;
Mike Stump1eb44332009-09-09 15:08:12 +00003151
Steve Naroff239f0732008-12-25 14:16:32 +00003152 case tok::kw___cdecl:
3153 case tok::kw___stdcall:
3154 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00003155 case tok::kw___thiscall:
Eli Friedman290eeb02009-06-08 23:27:34 +00003156 case tok::kw___w64:
3157 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00003158 case tok::kw___ptr32:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003159 case tok::kw___pascal:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00003160 case tok::kw___unaligned:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003161
3162 case tok::kw___private:
3163 case tok::kw___local:
3164 case tok::kw___global:
3165 case tok::kw___constant:
3166 case tok::kw___read_only:
3167 case tok::kw___read_write:
3168 case tok::kw___write_only:
3169
Eli Friedman290eeb02009-06-08 23:27:34 +00003170 return true;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003171
3172 case tok::kw_private:
David Blaikie4e4d0842012-03-11 07:00:24 +00003173 return getLangOpts().OpenCL;
Eli Friedmanb001de72011-10-06 23:00:33 +00003174
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00003175 // C11 _Atomic()
Eli Friedmanb001de72011-10-06 23:00:33 +00003176 case tok::kw__Atomic:
3177 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00003178 }
3179}
3180
3181/// isDeclarationSpecifier() - Return true if the current token is part of a
3182/// declaration specifier.
Douglas Gregor9497a732010-09-16 01:51:54 +00003183///
3184/// \param DisambiguatingWithExpression True to indicate that the purpose of
3185/// this check is to disambiguate between an expression and a declaration.
3186bool Parser::isDeclarationSpecifier(bool DisambiguatingWithExpression) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003187 switch (Tok.getKind()) {
3188 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00003189
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003190 case tok::kw_private:
David Blaikie4e4d0842012-03-11 07:00:24 +00003191 return getLangOpts().OpenCL;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003192
Chris Lattner166a8fc2009-01-04 23:41:41 +00003193 case tok::identifier: // foo::bar
Steve Naroff61f72cb2009-03-09 21:12:44 +00003194 // Unfortunate hack to support "Class.factoryMethod" notation.
David Blaikie4e4d0842012-03-11 07:00:24 +00003195 if (getLangOpts().ObjC1 && NextToken().is(tok::period))
Steve Naroff61f72cb2009-03-09 21:12:44 +00003196 return false;
John Thompson82287d12010-02-05 00:12:22 +00003197 if (TryAltiVecVectorToken())
3198 return true;
3199 // Fall through.
David Blaikie42d6d0c2011-12-04 05:04:18 +00003200 case tok::kw_decltype: // decltype(T())::type
Douglas Gregord57959a2009-03-27 23:10:48 +00003201 case tok::kw_typename: // typename T::type
Chris Lattner166a8fc2009-01-04 23:41:41 +00003202 // Annotate typenames and C++ scope specifiers. If we get one, just
3203 // recurse to handle whatever we get.
3204 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003205 return true;
3206 if (Tok.is(tok::identifier))
3207 return false;
Douglas Gregor9497a732010-09-16 01:51:54 +00003208
3209 // If we're in Objective-C and we have an Objective-C class type followed
3210 // by an identifier and then either ':' or ']', in a place where an
3211 // expression is permitted, then this is probably a class message send
3212 // missing the initial '['. In this case, we won't consider this to be
3213 // the start of a declaration.
3214 if (DisambiguatingWithExpression &&
3215 isStartOfObjCClassMessageMissingOpenBracket())
3216 return false;
3217
John McCall9ba61662010-02-26 08:45:28 +00003218 return isDeclarationSpecifier();
3219
Chris Lattner166a8fc2009-01-04 23:41:41 +00003220 case tok::coloncolon: // ::foo::bar
3221 if (NextToken().is(tok::kw_new) || // ::new
3222 NextToken().is(tok::kw_delete)) // ::delete
3223 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00003224
Chris Lattner166a8fc2009-01-04 23:41:41 +00003225 // Annotate typenames and C++ scope specifiers. If we get one, just
3226 // recurse to handle whatever we get.
3227 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003228 return true;
3229 return isDeclarationSpecifier();
Mike Stump1eb44332009-09-09 15:08:12 +00003230
Reid Spencer5f016e22007-07-11 17:01:13 +00003231 // storage-class-specifier
3232 case tok::kw_typedef:
3233 case tok::kw_extern:
Steve Naroff8d54bf22007-12-18 00:16:02 +00003234 case tok::kw___private_extern__:
Reid Spencer5f016e22007-07-11 17:01:13 +00003235 case tok::kw_static:
3236 case tok::kw_auto:
3237 case tok::kw_register:
3238 case tok::kw___thread:
Mike Stump1eb44332009-09-09 15:08:12 +00003239
Douglas Gregor8d267c52011-09-09 02:06:17 +00003240 // Modules
3241 case tok::kw___module_private__:
3242
Reid Spencer5f016e22007-07-11 17:01:13 +00003243 // type-specifiers
3244 case tok::kw_short:
3245 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00003246 case tok::kw___int64:
Reid Spencer5f016e22007-07-11 17:01:13 +00003247 case tok::kw_signed:
3248 case tok::kw_unsigned:
3249 case tok::kw__Complex:
3250 case tok::kw__Imaginary:
3251 case tok::kw_void:
3252 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00003253 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00003254 case tok::kw_char16_t:
3255 case tok::kw_char32_t:
3256
Reid Spencer5f016e22007-07-11 17:01:13 +00003257 case tok::kw_int:
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00003258 case tok::kw_half:
Reid Spencer5f016e22007-07-11 17:01:13 +00003259 case tok::kw_float:
3260 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00003261 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00003262 case tok::kw__Bool:
3263 case tok::kw__Decimal32:
3264 case tok::kw__Decimal64:
3265 case tok::kw__Decimal128:
John Thompson82287d12010-02-05 00:12:22 +00003266 case tok::kw___vector:
Mike Stump1eb44332009-09-09 15:08:12 +00003267
Chris Lattner99dc9142008-04-13 18:59:07 +00003268 // struct-or-union-specifier (C99) or class-specifier (C++)
3269 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00003270 case tok::kw_struct:
3271 case tok::kw_union:
3272 // enum-specifier
3273 case tok::kw_enum:
Mike Stump1eb44332009-09-09 15:08:12 +00003274
Reid Spencer5f016e22007-07-11 17:01:13 +00003275 // type-qualifier
3276 case tok::kw_const:
3277 case tok::kw_volatile:
3278 case tok::kw_restrict:
Steve Naroffd1861fd2007-07-31 12:34:36 +00003279
Reid Spencer5f016e22007-07-11 17:01:13 +00003280 // function-specifier
3281 case tok::kw_inline:
Douglas Gregorb48fe382008-10-31 09:07:45 +00003282 case tok::kw_virtual:
3283 case tok::kw_explicit:
Chris Lattnerd6c7c182007-08-09 16:40:21 +00003284
Peter Collingbournec6eb44b2011-04-15 00:35:57 +00003285 // static_assert-declaration
3286 case tok::kw__Static_assert:
3287
Chris Lattner1ef08762007-08-09 17:01:07 +00003288 // GNU typeof support.
3289 case tok::kw_typeof:
Mike Stump1eb44332009-09-09 15:08:12 +00003290
Chris Lattner1ef08762007-08-09 17:01:07 +00003291 // GNU attributes.
Chris Lattnerd6c7c182007-08-09 16:40:21 +00003292 case tok::kw___attribute:
Reid Spencer5f016e22007-07-11 17:01:13 +00003293 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00003294
Francois Pichete3d49b42011-06-19 08:02:06 +00003295 // C++0x decltype.
David Blaikie42d6d0c2011-12-04 05:04:18 +00003296 case tok::annot_decltype:
Francois Pichete3d49b42011-06-19 08:02:06 +00003297 return true;
3298
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00003299 // C11 _Atomic()
Eli Friedmanb001de72011-10-06 23:00:33 +00003300 case tok::kw__Atomic:
3301 return true;
3302
Chris Lattnerf3948c42008-07-26 03:38:44 +00003303 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
3304 case tok::less:
David Blaikie4e4d0842012-03-11 07:00:24 +00003305 return getLangOpts().ObjC1;
Mike Stump1eb44332009-09-09 15:08:12 +00003306
Douglas Gregord9d75e52011-04-27 05:41:15 +00003307 // typedef-name
3308 case tok::annot_typename:
3309 return !DisambiguatingWithExpression ||
3310 !isStartOfObjCClassMessageMissingOpenBracket();
3311
Steve Naroff47f52092009-01-06 19:34:12 +00003312 case tok::kw___declspec:
Steve Naroff239f0732008-12-25 14:16:32 +00003313 case tok::kw___cdecl:
3314 case tok::kw___stdcall:
3315 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00003316 case tok::kw___thiscall:
Eli Friedman290eeb02009-06-08 23:27:34 +00003317 case tok::kw___w64:
3318 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00003319 case tok::kw___ptr32:
Eli Friedman290eeb02009-06-08 23:27:34 +00003320 case tok::kw___forceinline:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003321 case tok::kw___pascal:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00003322 case tok::kw___unaligned:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003323
3324 case tok::kw___private:
3325 case tok::kw___local:
3326 case tok::kw___global:
3327 case tok::kw___constant:
3328 case tok::kw___read_only:
3329 case tok::kw___read_write:
3330 case tok::kw___write_only:
3331
Eli Friedman290eeb02009-06-08 23:27:34 +00003332 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00003333 }
3334}
3335
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003336bool Parser::isConstructorDeclarator() {
3337 TentativeParsingAction TPA(*this);
3338
3339 // Parse the C++ scope specifier.
3340 CXXScopeSpec SS;
Douglas Gregorefaa93a2011-11-07 17:33:42 +00003341 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
3342 /*EnteringContext=*/true)) {
John McCall9ba61662010-02-26 08:45:28 +00003343 TPA.Revert();
3344 return false;
3345 }
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003346
3347 // Parse the constructor name.
3348 if (Tok.is(tok::identifier) || Tok.is(tok::annot_template_id)) {
3349 // We already know that we have a constructor name; just consume
3350 // the token.
3351 ConsumeToken();
3352 } else {
3353 TPA.Revert();
3354 return false;
3355 }
3356
Richard Smith22592862012-03-27 23:05:05 +00003357 // Current class name must be followed by a left parenthesis.
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003358 if (Tok.isNot(tok::l_paren)) {
3359 TPA.Revert();
3360 return false;
3361 }
3362 ConsumeParen();
3363
Richard Smith22592862012-03-27 23:05:05 +00003364 // A right parenthesis, or ellipsis followed by a right parenthesis signals
3365 // that we have a constructor.
3366 if (Tok.is(tok::r_paren) ||
3367 (Tok.is(tok::ellipsis) && NextToken().is(tok::r_paren))) {
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003368 TPA.Revert();
3369 return true;
3370 }
3371
3372 // If we need to, enter the specified scope.
3373 DeclaratorScopeObj DeclScopeObj(*this, SS);
Douglas Gregor23c94db2010-07-02 17:43:08 +00003374 if (SS.isSet() && Actions.ShouldEnterDeclaratorScope(getCurScope(), SS))
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003375 DeclScopeObj.EnterDeclaratorScope();
3376
Francois Pichetdfaa5fb2011-01-31 04:54:32 +00003377 // Optionally skip Microsoft attributes.
John McCall0b7e6782011-03-24 11:26:52 +00003378 ParsedAttributes Attrs(AttrFactory);
Francois Pichetdfaa5fb2011-01-31 04:54:32 +00003379 MaybeParseMicrosoftAttributes(Attrs);
3380
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003381 // Check whether the next token(s) are part of a declaration
3382 // specifier, in which case we have the start of a parameter and,
3383 // therefore, we know that this is a constructor.
Richard Smith412e0cc2012-03-27 00:56:56 +00003384 bool IsConstructor = false;
3385 if (isDeclarationSpecifier())
3386 IsConstructor = true;
3387 else if (Tok.is(tok::identifier) ||
3388 (Tok.is(tok::annot_cxxscope) && NextToken().is(tok::identifier))) {
3389 // We've seen "C ( X" or "C ( X::Y", but "X" / "X::Y" is not a type.
3390 // This might be a parenthesized member name, but is more likely to
3391 // be a constructor declaration with an invalid argument type. Keep
3392 // looking.
3393 if (Tok.is(tok::annot_cxxscope))
3394 ConsumeToken();
3395 ConsumeToken();
3396
3397 // If this is not a constructor, we must be parsing a declarator,
Richard Smith5d8388c2012-03-27 01:42:32 +00003398 // which must have one of the following syntactic forms (see the
3399 // grammar extract at the start of ParseDirectDeclarator):
Richard Smith412e0cc2012-03-27 00:56:56 +00003400 switch (Tok.getKind()) {
3401 case tok::l_paren:
3402 // C(X ( int));
3403 case tok::l_square:
3404 // C(X [ 5]);
3405 // C(X [ [attribute]]);
3406 case tok::coloncolon:
3407 // C(X :: Y);
3408 // C(X :: *p);
3409 case tok::r_paren:
3410 // C(X )
3411 // Assume this isn't a constructor, rather than assuming it's a
3412 // constructor with an unnamed parameter of an ill-formed type.
3413 break;
3414
3415 default:
3416 IsConstructor = true;
3417 break;
3418 }
3419 }
3420
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003421 TPA.Revert();
3422 return IsConstructor;
3423}
Reid Spencer5f016e22007-07-11 17:01:13 +00003424
3425/// ParseTypeQualifierListOpt
Dawn Perchik52fc3142010-09-03 01:29:35 +00003426/// type-qualifier-list: [C99 6.7.5]
3427/// type-qualifier
3428/// [vendor] attributes
3429/// [ only if VendorAttributesAllowed=true ]
3430/// type-qualifier-list type-qualifier
3431/// [vendor] type-qualifier-list attributes
3432/// [ only if VendorAttributesAllowed=true ]
3433/// [C++0x] attribute-specifier[opt] is allowed before cv-qualifier-seq
3434/// [ only if CXX0XAttributesAllowed=true ]
3435/// Note: vendor can be GNU, MS, etc.
Reid Spencer5f016e22007-07-11 17:01:13 +00003436///
Dawn Perchik52fc3142010-09-03 01:29:35 +00003437void Parser::ParseTypeQualifierListOpt(DeclSpec &DS,
3438 bool VendorAttributesAllowed,
Sean Huntbbd37c62009-11-21 08:43:09 +00003439 bool CXX0XAttributesAllowed) {
David Blaikie4e4d0842012-03-11 07:00:24 +00003440 if (getLangOpts().CPlusPlus0x && isCXX0XAttributeSpecifier()) {
Sean Huntbbd37c62009-11-21 08:43:09 +00003441 SourceLocation Loc = Tok.getLocation();
John McCall0b7e6782011-03-24 11:26:52 +00003442 ParsedAttributesWithRange attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00003443 ParseCXX0XAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00003444 if (CXX0XAttributesAllowed)
John McCall7f040a92010-12-24 02:08:15 +00003445 DS.takeAttributesFrom(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00003446 else
3447 Diag(Loc, diag::err_attributes_not_allowed);
3448 }
Abramo Bagnara796aa442011-03-12 11:17:06 +00003449
3450 SourceLocation EndLoc;
3451
Reid Spencer5f016e22007-07-11 17:01:13 +00003452 while (1) {
John McCallfec54012009-08-03 20:12:06 +00003453 bool isInvalid = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00003454 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00003455 unsigned DiagID = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00003456 SourceLocation Loc = Tok.getLocation();
3457
3458 switch (Tok.getKind()) {
Douglas Gregor1a480c42010-08-27 17:35:51 +00003459 case tok::code_completion:
3460 Actions.CodeCompleteTypeQualifiers(DS);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00003461 return cutOffParsing();
Douglas Gregor1a480c42010-08-27 17:35:51 +00003462
Reid Spencer5f016e22007-07-11 17:01:13 +00003463 case tok::kw_const:
John McCallfec54012009-08-03 20:12:06 +00003464 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, DiagID,
David Blaikie4e4d0842012-03-11 07:00:24 +00003465 getLangOpts());
Reid Spencer5f016e22007-07-11 17:01:13 +00003466 break;
3467 case tok::kw_volatile:
John McCallfec54012009-08-03 20:12:06 +00003468 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
David Blaikie4e4d0842012-03-11 07:00:24 +00003469 getLangOpts());
Reid Spencer5f016e22007-07-11 17:01:13 +00003470 break;
3471 case tok::kw_restrict:
John McCallfec54012009-08-03 20:12:06 +00003472 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
David Blaikie4e4d0842012-03-11 07:00:24 +00003473 getLangOpts());
Reid Spencer5f016e22007-07-11 17:01:13 +00003474 break;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003475
3476 // OpenCL qualifiers:
3477 case tok::kw_private:
David Blaikie4e4d0842012-03-11 07:00:24 +00003478 if (!getLangOpts().OpenCL)
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003479 goto DoneWithTypeQuals;
3480 case tok::kw___private:
3481 case tok::kw___global:
3482 case tok::kw___local:
3483 case tok::kw___constant:
3484 case tok::kw___read_only:
3485 case tok::kw___write_only:
3486 case tok::kw___read_write:
3487 ParseOpenCLQualifiers(DS);
3488 break;
3489
Eli Friedman290eeb02009-06-08 23:27:34 +00003490 case tok::kw___w64:
Steve Naroff86bc6cf2008-12-25 14:41:26 +00003491 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00003492 case tok::kw___ptr32:
Steve Naroff239f0732008-12-25 14:16:32 +00003493 case tok::kw___cdecl:
3494 case tok::kw___stdcall:
3495 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00003496 case tok::kw___thiscall:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00003497 case tok::kw___unaligned:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003498 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00003499 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman290eeb02009-06-08 23:27:34 +00003500 continue;
3501 }
3502 goto DoneWithTypeQuals;
Dawn Perchik52fc3142010-09-03 01:29:35 +00003503 case tok::kw___pascal:
3504 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00003505 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik52fc3142010-09-03 01:29:35 +00003506 continue;
3507 }
3508 goto DoneWithTypeQuals;
Reid Spencer5f016e22007-07-11 17:01:13 +00003509 case tok::kw___attribute:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003510 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00003511 ParseGNUAttributes(DS.getAttributes());
Chris Lattner5a69d1c2008-12-18 07:02:59 +00003512 continue; // do *not* consume the next token!
3513 }
3514 // otherwise, FALL THROUGH!
3515 default:
Steve Naroff239f0732008-12-25 14:16:32 +00003516 DoneWithTypeQuals:
Chris Lattner5a69d1c2008-12-18 07:02:59 +00003517 // If this is not a type-qualifier token, we're done reading type
3518 // qualifiers. First verify that DeclSpec's are consistent.
Douglas Gregor9b3064b2009-04-01 22:41:11 +00003519 DS.Finish(Diags, PP);
Abramo Bagnara796aa442011-03-12 11:17:06 +00003520 if (EndLoc.isValid())
3521 DS.SetRangeEnd(EndLoc);
Chris Lattner5a69d1c2008-12-18 07:02:59 +00003522 return;
Reid Spencer5f016e22007-07-11 17:01:13 +00003523 }
Chris Lattnera1fcbad2008-12-18 06:50:14 +00003524
Reid Spencer5f016e22007-07-11 17:01:13 +00003525 // If the specifier combination wasn't legal, issue a diagnostic.
3526 if (isInvalid) {
3527 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner1ab3b962008-11-18 07:48:38 +00003528 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00003529 }
Abramo Bagnara796aa442011-03-12 11:17:06 +00003530 EndLoc = ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00003531 }
3532}
3533
3534
3535/// ParseDeclarator - Parse and verify a newly-initialized declarator.
3536///
3537void Parser::ParseDeclarator(Declarator &D) {
3538 /// This implements the 'declarator' production in the C grammar, then checks
3539 /// for well-formedness and issues diagnostics.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003540 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Reid Spencer5f016e22007-07-11 17:01:13 +00003541}
3542
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003543/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
3544/// is parsed by the function passed to it. Pass null, and the direct-declarator
3545/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003546/// ptr-operator production.
3547///
Richard Smith0706df42011-10-19 21:33:05 +00003548/// If the grammar of this construct is extended, matching changes must also be
Richard Smith5d8388c2012-03-27 01:42:32 +00003549/// made to TryParseDeclarator and MightBeDeclarator, and possibly to
3550/// isConstructorDeclarator.
Richard Smith0706df42011-10-19 21:33:05 +00003551///
Sebastian Redlf30208a2009-01-24 21:16:55 +00003552/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
3553/// [C] pointer[opt] direct-declarator
3554/// [C++] direct-declarator
3555/// [C++] ptr-operator declarator
Reid Spencer5f016e22007-07-11 17:01:13 +00003556///
3557/// pointer: [C99 6.7.5]
3558/// '*' type-qualifier-list[opt]
3559/// '*' type-qualifier-list[opt] pointer
3560///
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003561/// ptr-operator:
3562/// '*' cv-qualifier-seq[opt]
3563/// '&'
Sebastian Redl05532f22009-03-15 22:02:01 +00003564/// [C++0x] '&&'
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003565/// [GNU] '&' restrict[opt] attributes[opt]
Sebastian Redl05532f22009-03-15 22:02:01 +00003566/// [GNU?] '&&' restrict[opt] attributes[opt]
Sebastian Redlf30208a2009-01-24 21:16:55 +00003567/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003568void Parser::ParseDeclaratorInternal(Declarator &D,
3569 DirectDeclParseFunction DirectDeclParser) {
Douglas Gregor91a28862009-08-26 14:27:30 +00003570 if (Diags.hasAllExtensionsSilenced())
3571 D.setExtension();
Douglas Gregor2ccccb32010-08-23 18:23:48 +00003572
Sebastian Redlf30208a2009-01-24 21:16:55 +00003573 // C++ member pointers start with a '::' or a nested-name.
3574 // Member pointers get special handling, since there's no place for the
3575 // scope spec in the generic path below.
David Blaikie4e4d0842012-03-11 07:00:24 +00003576 if (getLangOpts().CPlusPlus &&
Chris Lattnerf919bfe2009-03-24 17:04:48 +00003577 (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) ||
3578 Tok.is(tok::annot_cxxscope))) {
Douglas Gregorefaa93a2011-11-07 17:33:42 +00003579 bool EnteringContext = D.getContext() == Declarator::FileContext ||
3580 D.getContext() == Declarator::MemberContext;
Sebastian Redlf30208a2009-01-24 21:16:55 +00003581 CXXScopeSpec SS;
Douglas Gregorefaa93a2011-11-07 17:33:42 +00003582 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), EnteringContext);
John McCall9ba61662010-02-26 08:45:28 +00003583
Jeffrey Yasskinedc28772010-04-07 23:29:58 +00003584 if (SS.isNotEmpty()) {
Mike Stump1eb44332009-09-09 15:08:12 +00003585 if (Tok.isNot(tok::star)) {
Sebastian Redlf30208a2009-01-24 21:16:55 +00003586 // The scope spec really belongs to the direct-declarator.
3587 D.getCXXScopeSpec() = SS;
3588 if (DirectDeclParser)
3589 (this->*DirectDeclParser)(D);
3590 return;
3591 }
3592
3593 SourceLocation Loc = ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00003594 D.SetRangeEnd(Loc);
John McCall0b7e6782011-03-24 11:26:52 +00003595 DeclSpec DS(AttrFactory);
Sebastian Redlf30208a2009-01-24 21:16:55 +00003596 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00003597 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00003598
3599 // Recurse to parse whatever is left.
3600 ParseDeclaratorInternal(D, DirectDeclParser);
3601
3602 // Sema will have to catch (syntactically invalid) pointers into global
3603 // scope. It has to catch pointers into namespace scope anyway.
3604 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
John McCall0b7e6782011-03-24 11:26:52 +00003605 Loc),
3606 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003607 /* Don't replace range end. */SourceLocation());
Sebastian Redlf30208a2009-01-24 21:16:55 +00003608 return;
3609 }
3610 }
3611
3612 tok::TokenKind Kind = Tok.getKind();
Steve Naroff5618bd42008-08-27 16:04:49 +00003613 // Not a pointer, C++ reference, or block.
Chris Lattner9af55002009-03-27 04:18:06 +00003614 if (Kind != tok::star && Kind != tok::caret &&
David Blaikie4e4d0842012-03-11 07:00:24 +00003615 (Kind != tok::amp || !getLangOpts().CPlusPlus) &&
Sebastian Redl743de1f2009-03-23 00:00:23 +00003616 // We parse rvalue refs in C++03, because otherwise the errors are scary.
David Blaikie4e4d0842012-03-11 07:00:24 +00003617 (Kind != tok::ampamp || !getLangOpts().CPlusPlus)) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003618 if (DirectDeclParser)
3619 (this->*DirectDeclParser)(D);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003620 return;
3621 }
Sebastian Redlf30208a2009-01-24 21:16:55 +00003622
Sebastian Redl05532f22009-03-15 22:02:01 +00003623 // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference,
3624 // '&&' -> rvalue reference
Sebastian Redl743de1f2009-03-23 00:00:23 +00003625 SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&.
Sebastian Redlab197ba2009-02-09 18:23:29 +00003626 D.SetRangeEnd(Loc);
Reid Spencer5f016e22007-07-11 17:01:13 +00003627
Chris Lattner9af55002009-03-27 04:18:06 +00003628 if (Kind == tok::star || Kind == tok::caret) {
Chris Lattner76549142008-02-21 01:32:26 +00003629 // Is a pointer.
John McCall0b7e6782011-03-24 11:26:52 +00003630 DeclSpec DS(AttrFactory);
Sebastian Redlf30208a2009-01-24 21:16:55 +00003631
Reid Spencer5f016e22007-07-11 17:01:13 +00003632 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00003633 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00003634
Reid Spencer5f016e22007-07-11 17:01:13 +00003635 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003636 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroff5618bd42008-08-27 16:04:49 +00003637 if (Kind == tok::star)
3638 // Remember that we parsed a pointer type, and remember the type-quals.
3639 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
Chandler Carruthd067c072011-02-23 18:51:59 +00003640 DS.getConstSpecLoc(),
3641 DS.getVolatileSpecLoc(),
John McCall0b7e6782011-03-24 11:26:52 +00003642 DS.getRestrictSpecLoc()),
3643 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003644 SourceLocation());
Steve Naroff5618bd42008-08-27 16:04:49 +00003645 else
3646 // Remember that we parsed a Block type, and remember the type-quals.
Mike Stump1eb44332009-09-09 15:08:12 +00003647 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
John McCall0b7e6782011-03-24 11:26:52 +00003648 Loc),
3649 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003650 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00003651 } else {
3652 // Is a reference
John McCall0b7e6782011-03-24 11:26:52 +00003653 DeclSpec DS(AttrFactory);
Reid Spencer5f016e22007-07-11 17:01:13 +00003654
Sebastian Redl743de1f2009-03-23 00:00:23 +00003655 // Complain about rvalue references in C++03, but then go on and build
3656 // the declarator.
Richard Smith7fe62082011-10-15 05:09:34 +00003657 if (Kind == tok::ampamp)
David Blaikie4e4d0842012-03-11 07:00:24 +00003658 Diag(Loc, getLangOpts().CPlusPlus0x ?
Richard Smith7fe62082011-10-15 05:09:34 +00003659 diag::warn_cxx98_compat_rvalue_reference :
3660 diag::ext_rvalue_reference);
Sebastian Redl743de1f2009-03-23 00:00:23 +00003661
Reid Spencer5f016e22007-07-11 17:01:13 +00003662 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
3663 // cv-qualifiers are introduced through the use of a typedef or of a
3664 // template type argument, in which case the cv-qualifiers are ignored.
3665 //
3666 // [GNU] Retricted references are allowed.
3667 // [GNU] Attributes on references are allowed.
Sean Huntbbd37c62009-11-21 08:43:09 +00003668 // [C++0x] Attributes on references are not allowed.
3669 ParseTypeQualifierListOpt(DS, true, false);
Sebastian Redlab197ba2009-02-09 18:23:29 +00003670 D.ExtendWithDeclSpec(DS);
Reid Spencer5f016e22007-07-11 17:01:13 +00003671
3672 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
3673 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
3674 Diag(DS.getConstSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00003675 diag::err_invalid_reference_qualifier_application) << "const";
Reid Spencer5f016e22007-07-11 17:01:13 +00003676 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
3677 Diag(DS.getVolatileSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00003678 diag::err_invalid_reference_qualifier_application) << "volatile";
Reid Spencer5f016e22007-07-11 17:01:13 +00003679 }
3680
3681 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003682 ParseDeclaratorInternal(D, DirectDeclParser);
Reid Spencer5f016e22007-07-11 17:01:13 +00003683
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00003684 if (D.getNumTypeObjects() > 0) {
3685 // C++ [dcl.ref]p4: There shall be no references to references.
3686 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
3687 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattnerda83bac2008-11-19 07:37:42 +00003688 if (const IdentifierInfo *II = D.getIdentifier())
3689 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
3690 << II;
3691 else
3692 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
3693 << "type name";
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00003694
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003695 // Once we've complained about the reference-to-reference, we
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00003696 // can go ahead and build the (technically ill-formed)
3697 // declarator: reference collapsing will take care of it.
3698 }
3699 }
3700
Reid Spencer5f016e22007-07-11 17:01:13 +00003701 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner76549142008-02-21 01:32:26 +00003702 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
Sebastian Redl05532f22009-03-15 22:02:01 +00003703 Kind == tok::amp),
John McCall0b7e6782011-03-24 11:26:52 +00003704 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003705 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00003706 }
3707}
3708
3709/// ParseDirectDeclarator
3710/// direct-declarator: [C99 6.7.5]
Douglas Gregor42a552f2008-11-05 20:51:48 +00003711/// [C99] identifier
Reid Spencer5f016e22007-07-11 17:01:13 +00003712/// '(' declarator ')'
3713/// [GNU] '(' attributes declarator ')'
3714/// [C90] direct-declarator '[' constant-expression[opt] ']'
3715/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
3716/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
3717/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
3718/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
3719/// direct-declarator '(' parameter-type-list ')'
3720/// direct-declarator '(' identifier-list[opt] ')'
3721/// [GNU] direct-declarator '(' parameter-forward-declarations
3722/// parameter-type-list[opt] ')'
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003723/// [C++] direct-declarator '(' parameter-declaration-clause ')'
3724/// cv-qualifier-seq[opt] exception-specification[opt]
Douglas Gregorb48fe382008-10-31 09:07:45 +00003725/// [C++] declarator-id
Douglas Gregor42a552f2008-11-05 20:51:48 +00003726///
3727/// declarator-id: [C++ 8]
Douglas Gregora8bc8c92010-12-23 22:44:42 +00003728/// '...'[opt] id-expression
Douglas Gregor42a552f2008-11-05 20:51:48 +00003729/// '::'[opt] nested-name-specifier[opt] type-name
3730///
3731/// id-expression: [C++ 5.1]
3732/// unqualified-id
Douglas Gregordb422df2009-09-25 21:45:23 +00003733/// qualified-id
Douglas Gregor42a552f2008-11-05 20:51:48 +00003734///
3735/// unqualified-id: [C++ 5.1]
Mike Stump1eb44332009-09-09 15:08:12 +00003736/// identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003737/// operator-function-id
Douglas Gregordb422df2009-09-25 21:45:23 +00003738/// conversion-function-id
Mike Stump1eb44332009-09-09 15:08:12 +00003739/// '~' class-name
Douglas Gregor39a8de12009-02-25 19:37:18 +00003740/// template-id
Argyrios Kyrtzidisc7ed9c62008-11-07 22:02:30 +00003741///
Richard Smith5d8388c2012-03-27 01:42:32 +00003742/// Note, any additional constructs added here may need corresponding changes
3743/// in isConstructorDeclarator.
Reid Spencer5f016e22007-07-11 17:01:13 +00003744void Parser::ParseDirectDeclarator(Declarator &D) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00003745 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003746
David Blaikie4e4d0842012-03-11 07:00:24 +00003747 if (getLangOpts().CPlusPlus && D.mayHaveIdentifier()) {
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003748 // ParseDeclaratorInternal might already have parsed the scope.
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00003749 if (D.getCXXScopeSpec().isEmpty()) {
Douglas Gregorefaa93a2011-11-07 17:33:42 +00003750 bool EnteringContext = D.getContext() == Declarator::FileContext ||
3751 D.getContext() == Declarator::MemberContext;
3752 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec(), ParsedType(),
3753 EnteringContext);
John McCall9ba61662010-02-26 08:45:28 +00003754 }
3755
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00003756 if (D.getCXXScopeSpec().isValid()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00003757 if (Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
John McCalle7e278b2009-12-11 20:04:54 +00003758 // Change the declaration context for name lookup, until this function
3759 // is exited (and the declarator has been parsed).
3760 DeclScopeObj.EnterDeclaratorScope();
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00003761 }
3762
Douglas Gregora8bc8c92010-12-23 22:44:42 +00003763 // C++0x [dcl.fct]p14:
3764 // There is a syntactic ambiguity when an ellipsis occurs at the end
3765 // of a parameter-declaration-clause without a preceding comma. In
3766 // this case, the ellipsis is parsed as part of the
3767 // abstract-declarator if the type of the parameter names a template
3768 // parameter pack that has not been expanded; otherwise, it is parsed
3769 // as part of the parameter-declaration-clause.
3770 if (Tok.is(tok::ellipsis) &&
3771 !((D.getContext() == Declarator::PrototypeContext ||
3772 D.getContext() == Declarator::BlockLiteralContext) &&
Douglas Gregora8bc8c92010-12-23 22:44:42 +00003773 NextToken().is(tok::r_paren) &&
3774 !Actions.containsUnexpandedParameterPacks(D)))
3775 D.setEllipsisLoc(ConsumeToken());
3776
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003777 if (Tok.is(tok::identifier) || Tok.is(tok::kw_operator) ||
3778 Tok.is(tok::annot_template_id) || Tok.is(tok::tilde)) {
3779 // We found something that indicates the start of an unqualified-id.
3780 // Parse that unqualified-id.
John McCallba9d8532010-04-13 06:39:49 +00003781 bool AllowConstructorName;
3782 if (D.getDeclSpec().hasTypeSpecifier())
3783 AllowConstructorName = false;
3784 else if (D.getCXXScopeSpec().isSet())
3785 AllowConstructorName =
3786 (D.getContext() == Declarator::FileContext ||
3787 (D.getContext() == Declarator::MemberContext &&
3788 D.getDeclSpec().isFriendSpecified()));
3789 else
3790 AllowConstructorName = (D.getContext() == Declarator::MemberContext);
3791
Abramo Bagnarae4b92762012-01-27 09:46:47 +00003792 SourceLocation TemplateKWLoc;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003793 if (ParseUnqualifiedId(D.getCXXScopeSpec(),
3794 /*EnteringContext=*/true,
3795 /*AllowDestructorName=*/true,
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003796 AllowConstructorName,
John McCallb3d87482010-08-24 05:47:05 +00003797 ParsedType(),
Abramo Bagnarae4b92762012-01-27 09:46:47 +00003798 TemplateKWLoc,
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00003799 D.getName()) ||
3800 // Once we're past the identifier, if the scope was bad, mark the
3801 // whole declarator bad.
3802 D.getCXXScopeSpec().isInvalid()) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00003803 D.SetIdentifier(0, Tok.getLocation());
3804 D.setInvalidType(true);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003805 } else {
3806 // Parsed the unqualified-id; update range information and move along.
3807 if (D.getSourceRange().getBegin().isInvalid())
3808 D.SetRangeBegin(D.getName().getSourceRange().getBegin());
3809 D.SetRangeEnd(D.getName().getSourceRange().getEnd());
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003810 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003811 goto PastIdentifier;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00003812 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003813 } else if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
David Blaikie4e4d0842012-03-11 07:00:24 +00003814 assert(!getLangOpts().CPlusPlus &&
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00003815 "There's a C++-specific check for tok::identifier above");
3816 assert(Tok.getIdentifierInfo() && "Not an identifier?");
3817 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
3818 ConsumeToken();
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003819 goto PastIdentifier;
3820 }
3821
3822 if (Tok.is(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003823 // direct-declarator: '(' declarator ')'
3824 // direct-declarator: '(' attributes declarator ')'
3825 // Example: 'char (*X)' or 'int (*XX)(void)'
3826 ParseParenDeclarator(D);
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003827
3828 // If the declarator was parenthesized, we entered the declarator
3829 // scope when parsing the parenthesized declarator, then exited
3830 // the scope already. Re-enter the scope, if we need to.
3831 if (D.getCXXScopeSpec().isSet()) {
Fariborz Jahanian46877cd2010-08-17 23:50:37 +00003832 // If there was an error parsing parenthesized declarator, declarator
3833 // scope may have been enterred before. Don't do it again.
3834 if (!D.isInvalidType() &&
3835 Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003836 // Change the declaration context for name lookup, until this function
3837 // is exited (and the declarator has been parsed).
Fariborz Jahanian46877cd2010-08-17 23:50:37 +00003838 DeclScopeObj.EnterDeclaratorScope();
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003839 }
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00003840 } else if (D.mayOmitIdentifier()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003841 // This could be something simple like "int" (in which case the declarator
3842 // portion is empty), if an abstract-declarator is allowed.
3843 D.SetIdentifier(0, Tok.getLocation());
3844 } else {
Douglas Gregore950d4b2009-03-06 23:28:18 +00003845 if (D.getContext() == Declarator::MemberContext)
3846 Diag(Tok, diag::err_expected_member_name_or_semi)
3847 << D.getDeclSpec().getSourceRange();
David Blaikie4e4d0842012-03-11 07:00:24 +00003848 else if (getLangOpts().CPlusPlus)
3849 Diag(Tok, diag::err_expected_unqualified_id) << getLangOpts().CPlusPlus;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003850 else
Chris Lattner1ab3b962008-11-18 07:48:38 +00003851 Diag(Tok, diag::err_expected_ident_lparen);
Reid Spencer5f016e22007-07-11 17:01:13 +00003852 D.SetIdentifier(0, Tok.getLocation());
Chris Lattner1f6f54b2008-11-11 06:13:16 +00003853 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00003854 }
Mike Stump1eb44332009-09-09 15:08:12 +00003855
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00003856 PastIdentifier:
Reid Spencer5f016e22007-07-11 17:01:13 +00003857 assert(D.isPastIdentifier() &&
3858 "Haven't past the location of the identifier yet?");
Mike Stump1eb44332009-09-09 15:08:12 +00003859
Sean Huntbbd37c62009-11-21 08:43:09 +00003860 // Don't parse attributes unless we have an identifier.
John McCall7f040a92010-12-24 02:08:15 +00003861 if (D.getIdentifier())
3862 MaybeParseCXX0XAttributes(D);
Sean Huntbbd37c62009-11-21 08:43:09 +00003863
Reid Spencer5f016e22007-07-11 17:01:13 +00003864 while (1) {
Chris Lattner04d66662007-10-09 17:33:22 +00003865 if (Tok.is(tok::l_paren)) {
David Blaikie42d6d0c2011-12-04 05:04:18 +00003866 // Enter function-declaration scope, limiting any declarators to the
3867 // function prototype scope, including parameter declarators.
3868 ParseScope PrototypeScope(this,
3869 Scope::FunctionPrototypeScope|Scope::DeclScope);
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00003870 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
3871 // In such a case, check if we actually have a function declarator; if it
3872 // is not, the declarator has been fully parsed.
David Blaikie4e4d0842012-03-11 07:00:24 +00003873 if (getLangOpts().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
Chris Lattner7399ee02008-10-20 02:05:46 +00003874 // When not in file scope, warn for ambiguous function declarators, just
3875 // in case the author intended it as a variable definition.
3876 bool warnIfAmbiguous = D.getContext() != Declarator::FileContext;
3877 if (!isCXXFunctionDeclarator(warnIfAmbiguous))
3878 break;
3879 }
John McCall0b7e6782011-03-24 11:26:52 +00003880 ParsedAttributes attrs(AttrFactory);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003881 BalancedDelimiterTracker T(*this, tok::l_paren);
3882 T.consumeOpen();
3883 ParseFunctionDeclarator(D, attrs, T);
David Blaikie42d6d0c2011-12-04 05:04:18 +00003884 PrototypeScope.Exit();
Chris Lattner04d66662007-10-09 17:33:22 +00003885 } else if (Tok.is(tok::l_square)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003886 ParseBracketDeclarator(D);
3887 } else {
3888 break;
3889 }
3890 }
David Blaikie42d6d0c2011-12-04 05:04:18 +00003891}
Reid Spencer5f016e22007-07-11 17:01:13 +00003892
Chris Lattneref4715c2008-04-06 05:45:57 +00003893/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
3894/// only called before the identifier, so these are most likely just grouping
Mike Stump1eb44332009-09-09 15:08:12 +00003895/// parens for precedence. If we find that these are actually function
Chris Lattneref4715c2008-04-06 05:45:57 +00003896/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
3897///
3898/// direct-declarator:
3899/// '(' declarator ')'
3900/// [GNU] '(' attributes declarator ')'
Chris Lattner7399ee02008-10-20 02:05:46 +00003901/// direct-declarator '(' parameter-type-list ')'
3902/// direct-declarator '(' identifier-list[opt] ')'
3903/// [GNU] direct-declarator '(' parameter-forward-declarations
3904/// parameter-type-list[opt] ')'
Chris Lattneref4715c2008-04-06 05:45:57 +00003905///
3906void Parser::ParseParenDeclarator(Declarator &D) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003907 BalancedDelimiterTracker T(*this, tok::l_paren);
3908 T.consumeOpen();
3909
Chris Lattneref4715c2008-04-06 05:45:57 +00003910 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
Mike Stump1eb44332009-09-09 15:08:12 +00003911
Chris Lattner7399ee02008-10-20 02:05:46 +00003912 // Eat any attributes before we look at whether this is a grouping or function
3913 // declarator paren. If this is a grouping paren, the attribute applies to
3914 // the type being built up, for example:
3915 // int (__attribute__(()) *x)(long y)
3916 // If this ends up not being a grouping paren, the attribute applies to the
3917 // first argument, for example:
3918 // int (__attribute__(()) int x)
3919 // In either case, we need to eat any attributes to be able to determine what
3920 // sort of paren this is.
3921 //
John McCall0b7e6782011-03-24 11:26:52 +00003922 ParsedAttributes attrs(AttrFactory);
Chris Lattner7399ee02008-10-20 02:05:46 +00003923 bool RequiresArg = false;
3924 if (Tok.is(tok::kw___attribute)) {
John McCall7f040a92010-12-24 02:08:15 +00003925 ParseGNUAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00003926
Chris Lattner7399ee02008-10-20 02:05:46 +00003927 // We require that the argument list (if this is a non-grouping paren) be
3928 // present even if the attribute list was empty.
3929 RequiresArg = true;
3930 }
Steve Naroff239f0732008-12-25 14:16:32 +00003931 // Eat any Microsoft extensions.
Eli Friedman290eeb02009-06-08 23:27:34 +00003932 if (Tok.is(tok::kw___cdecl) || Tok.is(tok::kw___stdcall) ||
Douglas Gregorf813a2c2010-05-18 16:57:00 +00003933 Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___fastcall) ||
Francois Pichet3bd9aa42011-08-18 09:59:55 +00003934 Tok.is(tok::kw___w64) || Tok.is(tok::kw___ptr64) ||
Francois Pichet58fd97a2011-08-25 00:36:46 +00003935 Tok.is(tok::kw___ptr32) || Tok.is(tok::kw___unaligned)) {
John McCall7f040a92010-12-24 02:08:15 +00003936 ParseMicrosoftTypeAttributes(attrs);
Eli Friedman290eeb02009-06-08 23:27:34 +00003937 }
Dawn Perchik52fc3142010-09-03 01:29:35 +00003938 // Eat any Borland extensions.
Ted Kremenek8113ecf2010-11-10 05:59:39 +00003939 if (Tok.is(tok::kw___pascal))
John McCall7f040a92010-12-24 02:08:15 +00003940 ParseBorlandTypeAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00003941
Chris Lattneref4715c2008-04-06 05:45:57 +00003942 // If we haven't past the identifier yet (or where the identifier would be
3943 // stored, if this is an abstract declarator), then this is probably just
3944 // grouping parens. However, if this could be an abstract-declarator, then
3945 // this could also be the start of function arguments (consider 'void()').
3946 bool isGrouping;
Mike Stump1eb44332009-09-09 15:08:12 +00003947
Chris Lattneref4715c2008-04-06 05:45:57 +00003948 if (!D.mayOmitIdentifier()) {
3949 // If this can't be an abstract-declarator, this *must* be a grouping
3950 // paren, because we haven't seen the identifier yet.
3951 isGrouping = true;
3952 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Richard Smith22592862012-03-27 23:05:05 +00003953 (getLangOpts().CPlusPlus && Tok.is(tok::ellipsis) &&
3954 NextToken().is(tok::r_paren)) || // C++ int(...)
Chris Lattneref4715c2008-04-06 05:45:57 +00003955 isDeclarationSpecifier()) { // 'int(int)' is a function.
3956 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
3957 // considered to be a type, not a K&R identifier-list.
3958 isGrouping = false;
3959 } else {
3960 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
3961 isGrouping = true;
3962 }
Mike Stump1eb44332009-09-09 15:08:12 +00003963
Chris Lattneref4715c2008-04-06 05:45:57 +00003964 // If this is a grouping paren, handle:
3965 // direct-declarator: '(' declarator ')'
3966 // direct-declarator: '(' attributes declarator ')'
3967 if (isGrouping) {
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00003968 bool hadGroupingParens = D.hasGroupingParens();
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00003969 D.setGroupingParens(true);
3970
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003971 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattneref4715c2008-04-06 05:45:57 +00003972 // Match the ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003973 T.consumeClose();
3974 D.AddTypeInfo(DeclaratorChunk::getParen(T.getOpenLocation(),
3975 T.getCloseLocation()),
3976 attrs, T.getCloseLocation());
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00003977
3978 D.setGroupingParens(hadGroupingParens);
Chris Lattneref4715c2008-04-06 05:45:57 +00003979 return;
3980 }
Mike Stump1eb44332009-09-09 15:08:12 +00003981
Chris Lattneref4715c2008-04-06 05:45:57 +00003982 // Okay, if this wasn't a grouping paren, it must be the start of a function
3983 // argument list. Recognize that this declarator will never have an
Chris Lattner7399ee02008-10-20 02:05:46 +00003984 // identifier (and remember where it would have been), then call into
3985 // ParseFunctionDeclarator to handle of argument list.
Chris Lattneref4715c2008-04-06 05:45:57 +00003986 D.SetIdentifier(0, Tok.getLocation());
3987
David Blaikie42d6d0c2011-12-04 05:04:18 +00003988 // Enter function-declaration scope, limiting any declarators to the
3989 // function prototype scope, including parameter declarators.
3990 ParseScope PrototypeScope(this,
3991 Scope::FunctionPrototypeScope|Scope::DeclScope);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003992 ParseFunctionDeclarator(D, attrs, T, RequiresArg);
David Blaikie42d6d0c2011-12-04 05:04:18 +00003993 PrototypeScope.Exit();
Chris Lattneref4715c2008-04-06 05:45:57 +00003994}
3995
3996/// ParseFunctionDeclarator - We are after the identifier and have parsed the
3997/// declarator D up to a paren, which indicates that we are parsing function
3998/// arguments.
Reid Spencer5f016e22007-07-11 17:01:13 +00003999///
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004000/// If attrs is non-null, then the caller parsed those arguments immediately
Chris Lattner7399ee02008-10-20 02:05:46 +00004001/// after the open paren - they should be considered to be the first argument of
4002/// a parameter. If RequiresArg is true, then the first argument of the
4003/// function is required to be present and required to not be an identifier
4004/// list.
4005///
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004006/// For C++, after the parameter-list, it also parses cv-qualifier-seq[opt],
4007/// (C++0x) ref-qualifier[opt], exception-specification[opt], and
4008/// (C++0x) trailing-return-type[opt].
4009///
4010/// [C++0x] exception-specification:
4011/// dynamic-exception-specification
4012/// noexcept-specification
4013///
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004014void Parser::ParseFunctionDeclarator(Declarator &D,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004015 ParsedAttributes &attrs,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004016 BalancedDelimiterTracker &Tracker,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004017 bool RequiresArg) {
David Blaikie42d6d0c2011-12-04 05:04:18 +00004018 assert(getCurScope()->isFunctionPrototypeScope() &&
4019 "Should call from a Function scope");
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004020 // lparen is already consumed!
4021 assert(D.isPastIdentifier() && "Should not call before identifier!");
4022
4023 // This should be true when the function has typed arguments.
4024 // Otherwise, it is treated as a K&R-style function.
4025 bool HasProto = false;
4026 // Build up an array of information about the parsed arguments.
Chris Lattner5f9e2722011-07-23 10:55:15 +00004027 SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004028 // Remember where we see an ellipsis, if any.
4029 SourceLocation EllipsisLoc;
4030
4031 DeclSpec DS(AttrFactory);
4032 bool RefQualifierIsLValueRef = true;
4033 SourceLocation RefQualifierLoc;
Douglas Gregor43f51032011-10-19 06:04:55 +00004034 SourceLocation ConstQualifierLoc;
4035 SourceLocation VolatileQualifierLoc;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004036 ExceptionSpecificationType ESpecType = EST_None;
4037 SourceRange ESpecRange;
Chris Lattner5f9e2722011-07-23 10:55:15 +00004038 SmallVector<ParsedType, 2> DynamicExceptions;
4039 SmallVector<SourceRange, 2> DynamicExceptionRanges;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004040 ExprResult NoexceptExpr;
4041 ParsedType TrailingReturnType;
4042
James Molloy16f1f712012-02-29 10:24:19 +00004043 Actions.ActOnStartFunctionDeclarator();
4044
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004045 SourceLocation EndLoc;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004046 if (isFunctionDeclaratorIdentifierList()) {
4047 if (RequiresArg)
4048 Diag(Tok, diag::err_argument_required_after_attribute);
4049
4050 ParseFunctionDeclaratorIdentifierList(D, ParamInfo);
4051
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004052 Tracker.consumeClose();
4053 EndLoc = Tracker.getCloseLocation();
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004054 } else {
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004055 if (Tok.isNot(tok::r_paren))
4056 ParseParameterDeclarationClause(D, attrs, ParamInfo, EllipsisLoc);
4057 else if (RequiresArg)
4058 Diag(Tok, diag::err_argument_required_after_attribute);
4059
David Blaikie4e4d0842012-03-11 07:00:24 +00004060 HasProto = ParamInfo.size() || getLangOpts().CPlusPlus;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004061
4062 // If we have the closing ')', eat it.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004063 Tracker.consumeClose();
4064 EndLoc = Tracker.getCloseLocation();
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004065
David Blaikie4e4d0842012-03-11 07:00:24 +00004066 if (getLangOpts().CPlusPlus) {
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004067 MaybeParseCXX0XAttributes(attrs);
4068
4069 // Parse cv-qualifier-seq[opt].
4070 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Douglas Gregor43f51032011-10-19 06:04:55 +00004071 if (!DS.getSourceRange().getEnd().isInvalid()) {
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004072 EndLoc = DS.getSourceRange().getEnd();
Douglas Gregor43f51032011-10-19 06:04:55 +00004073 ConstQualifierLoc = DS.getConstSpecLoc();
4074 VolatileQualifierLoc = DS.getVolatileSpecLoc();
4075 }
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004076
4077 // Parse ref-qualifier[opt].
4078 if (Tok.is(tok::amp) || Tok.is(tok::ampamp)) {
David Blaikie4e4d0842012-03-11 07:00:24 +00004079 Diag(Tok, getLangOpts().CPlusPlus0x ?
Richard Smith7fe62082011-10-15 05:09:34 +00004080 diag::warn_cxx98_compat_ref_qualifier :
4081 diag::ext_ref_qualifier);
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004082
4083 RefQualifierIsLValueRef = Tok.is(tok::amp);
4084 RefQualifierLoc = ConsumeToken();
4085 EndLoc = RefQualifierLoc;
4086 }
4087
4088 // Parse exception-specification[opt].
4089 ESpecType = MaybeParseExceptionSpecification(ESpecRange,
4090 DynamicExceptions,
4091 DynamicExceptionRanges,
4092 NoexceptExpr);
4093 if (ESpecType != EST_None)
4094 EndLoc = ESpecRange.getEnd();
4095
4096 // Parse trailing-return-type[opt].
David Blaikie4e4d0842012-03-11 07:00:24 +00004097 if (getLangOpts().CPlusPlus0x && Tok.is(tok::arrow)) {
Richard Smith7fe62082011-10-15 05:09:34 +00004098 Diag(Tok, diag::warn_cxx98_compat_trailing_return_type);
Douglas Gregorae7902c2011-08-04 15:30:47 +00004099 SourceRange Range;
4100 TrailingReturnType = ParseTrailingReturnType(Range).get();
4101 if (Range.getEnd().isValid())
4102 EndLoc = Range.getEnd();
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004103 }
4104 }
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004105 }
4106
4107 // Remember that we parsed a function type, and remember the attributes.
4108 D.AddTypeInfo(DeclaratorChunk::getFunction(HasProto,
4109 /*isVariadic=*/EllipsisLoc.isValid(),
4110 EllipsisLoc,
4111 ParamInfo.data(), ParamInfo.size(),
4112 DS.getTypeQualifiers(),
4113 RefQualifierIsLValueRef,
Douglas Gregor43f51032011-10-19 06:04:55 +00004114 RefQualifierLoc, ConstQualifierLoc,
4115 VolatileQualifierLoc,
Douglas Gregor90ebed02011-07-13 21:47:47 +00004116 /*MutableLoc=*/SourceLocation(),
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004117 ESpecType, ESpecRange.getBegin(),
4118 DynamicExceptions.data(),
4119 DynamicExceptionRanges.data(),
4120 DynamicExceptions.size(),
4121 NoexceptExpr.isUsable() ?
4122 NoexceptExpr.get() : 0,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004123 Tracker.getOpenLocation(),
4124 EndLoc, D,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004125 TrailingReturnType),
4126 attrs, EndLoc);
James Molloy16f1f712012-02-29 10:24:19 +00004127
4128 Actions.ActOnEndFunctionDeclarator();
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004129}
4130
4131/// isFunctionDeclaratorIdentifierList - This parameter list may have an
4132/// identifier list form for a K&R-style function: void foo(a,b,c)
4133///
4134/// Note that identifier-lists are only allowed for normal declarators, not for
4135/// abstract-declarators.
4136bool Parser::isFunctionDeclaratorIdentifierList() {
David Blaikie4e4d0842012-03-11 07:00:24 +00004137 return !getLangOpts().CPlusPlus
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004138 && Tok.is(tok::identifier)
4139 && !TryAltiVecVectorToken()
4140 // K&R identifier lists can't have typedefs as identifiers, per C99
4141 // 6.7.5.3p11.
4142 && (TryAnnotateTypeOrScopeToken() || !Tok.is(tok::annot_typename))
4143 // Identifier lists follow a really simple grammar: the identifiers can
4144 // be followed *only* by a ", identifier" or ")". However, K&R
4145 // identifier lists are really rare in the brave new modern world, and
4146 // it is very common for someone to typo a type in a non-K&R style
4147 // list. If we are presented with something like: "void foo(intptr x,
4148 // float y)", we don't want to start parsing the function declarator as
4149 // though it is a K&R style declarator just because intptr is an
4150 // invalid type.
4151 //
4152 // To handle this, we check to see if the token after the first
4153 // identifier is a "," or ")". Only then do we parse it as an
4154 // identifier list.
4155 && (NextToken().is(tok::comma) || NextToken().is(tok::r_paren));
4156}
4157
4158/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
4159/// we found a K&R-style identifier list instead of a typed parameter list.
4160///
4161/// After returning, ParamInfo will hold the parsed parameters.
4162///
4163/// identifier-list: [C99 6.7.5]
4164/// identifier
4165/// identifier-list ',' identifier
4166///
4167void Parser::ParseFunctionDeclaratorIdentifierList(
4168 Declarator &D,
Chris Lattner5f9e2722011-07-23 10:55:15 +00004169 SmallVector<DeclaratorChunk::ParamInfo, 16> &ParamInfo) {
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004170 // If there was no identifier specified for the declarator, either we are in
4171 // an abstract-declarator, or we are in a parameter declarator which was found
4172 // to be abstract. In abstract-declarators, identifier lists are not valid:
4173 // diagnose this.
4174 if (!D.getIdentifier())
4175 Diag(Tok, diag::ext_ident_list_in_param);
4176
4177 // Maintain an efficient lookup of params we have seen so far.
4178 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
4179
4180 while (1) {
4181 // If this isn't an identifier, report the error and skip until ')'.
4182 if (Tok.isNot(tok::identifier)) {
4183 Diag(Tok, diag::err_expected_ident);
4184 SkipUntil(tok::r_paren, /*StopAtSemi=*/true, /*DontConsume=*/true);
4185 // Forget we parsed anything.
4186 ParamInfo.clear();
4187 return;
4188 }
4189
4190 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
4191
4192 // Reject 'typedef int y; int test(x, y)', but continue parsing.
4193 if (Actions.getTypeName(*ParmII, Tok.getLocation(), getCurScope()))
4194 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
4195
4196 // Verify that the argument identifier has not already been mentioned.
4197 if (!ParamsSoFar.insert(ParmII)) {
4198 Diag(Tok, diag::err_param_redefinition) << ParmII;
4199 } else {
4200 // Remember this identifier in ParamInfo.
4201 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
4202 Tok.getLocation(),
4203 0));
4204 }
4205
4206 // Eat the identifier.
4207 ConsumeToken();
4208
4209 // The list continues if we see a comma.
4210 if (Tok.isNot(tok::comma))
4211 break;
4212 ConsumeToken();
4213 }
4214}
4215
4216/// ParseParameterDeclarationClause - Parse a (possibly empty) parameter-list
4217/// after the opening parenthesis. This function will not parse a K&R-style
4218/// identifier list.
4219///
4220/// D is the declarator being parsed. If attrs is non-null, then the caller
4221/// parsed those arguments immediately after the open paren - they should be
4222/// considered to be the first argument of a parameter.
4223///
4224/// After returning, ParamInfo will hold the parsed parameters. EllipsisLoc will
4225/// be the location of the ellipsis, if any was parsed.
4226///
Reid Spencer5f016e22007-07-11 17:01:13 +00004227/// parameter-type-list: [C99 6.7.5]
4228/// parameter-list
4229/// parameter-list ',' '...'
Douglas Gregored5d6512009-09-22 21:41:40 +00004230/// [C++] parameter-list '...'
Reid Spencer5f016e22007-07-11 17:01:13 +00004231///
4232/// parameter-list: [C99 6.7.5]
4233/// parameter-declaration
4234/// parameter-list ',' parameter-declaration
4235///
4236/// parameter-declaration: [C99 6.7.5]
4237/// declaration-specifiers declarator
Chris Lattner04421082008-04-08 04:40:51 +00004238/// [C++] declaration-specifiers declarator '=' assignment-expression
Sebastian Redl84407ba2012-03-14 15:54:00 +00004239/// [C++11] initializer-clause
Reid Spencer5f016e22007-07-11 17:01:13 +00004240/// [GNU] declaration-specifiers declarator attributes
Sebastian Redl50de12f2009-03-24 22:27:57 +00004241/// declaration-specifiers abstract-declarator[opt]
4242/// [C++] declaration-specifiers abstract-declarator[opt]
Chris Lattner8123a952008-04-10 02:22:51 +00004243/// '=' assignment-expression
Reid Spencer5f016e22007-07-11 17:01:13 +00004244/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
4245///
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004246void Parser::ParseParameterDeclarationClause(
4247 Declarator &D,
4248 ParsedAttributes &attrs,
Chris Lattner5f9e2722011-07-23 10:55:15 +00004249 SmallVector<DeclaratorChunk::ParamInfo, 16> &ParamInfo,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004250 SourceLocation &EllipsisLoc) {
Mike Stump1eb44332009-09-09 15:08:12 +00004251
Chris Lattnerf97409f2008-04-06 06:57:35 +00004252 while (1) {
4253 if (Tok.is(tok::ellipsis)) {
Douglas Gregor965acbb2009-02-18 07:07:28 +00004254 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chris Lattnerf97409f2008-04-06 06:57:35 +00004255 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00004256 }
Mike Stump1eb44332009-09-09 15:08:12 +00004257
Chris Lattnerf97409f2008-04-06 06:57:35 +00004258 // Parse the declaration-specifiers.
John McCall54abf7d2009-11-04 02:18:39 +00004259 // Just use the ParsingDeclaration "scope" of the declarator.
John McCall0b7e6782011-03-24 11:26:52 +00004260 DeclSpec DS(AttrFactory);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004261
John McCall7f040a92010-12-24 02:08:15 +00004262 // Skip any Microsoft attributes before a param.
David Blaikie4e4d0842012-03-11 07:00:24 +00004263 if (getLangOpts().MicrosoftExt && Tok.is(tok::l_square))
John McCall7f040a92010-12-24 02:08:15 +00004264 ParseMicrosoftAttributes(DS.getAttributes());
4265
4266 SourceLocation DSStart = Tok.getLocation();
Chris Lattner7399ee02008-10-20 02:05:46 +00004267
4268 // If the caller parsed attributes for the first argument, add them now.
John McCall7f040a92010-12-24 02:08:15 +00004269 // Take them so that we only apply the attributes to the first parameter.
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004270 // FIXME: If we saw an ellipsis first, this code is not reached. Are the
4271 // attributes lost? Should they even be allowed?
4272 // FIXME: If we can leave the attributes in the token stream somehow, we can
4273 // get rid of a parameter (attrs) and this statement. It might be too much
4274 // hassle.
John McCall7f040a92010-12-24 02:08:15 +00004275 DS.takeAttributesFrom(attrs);
4276
Chris Lattnere64c5492009-02-27 18:38:20 +00004277 ParseDeclarationSpecifiers(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00004278
Chris Lattnerf97409f2008-04-06 06:57:35 +00004279 // Parse the declarator. This is "PrototypeContext", because we must
4280 // accept either 'declarator' or 'abstract-declarator' here.
4281 Declarator ParmDecl(DS, Declarator::PrototypeContext);
4282 ParseDeclarator(ParmDecl);
4283
4284 // Parse GNU attributes, if present.
John McCall7f040a92010-12-24 02:08:15 +00004285 MaybeParseGNUAttributes(ParmDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00004286
Chris Lattnerf97409f2008-04-06 06:57:35 +00004287 // Remember this parsed parameter in ParamInfo.
4288 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
Mike Stump1eb44332009-09-09 15:08:12 +00004289
Douglas Gregor72b505b2008-12-16 21:30:33 +00004290 // DefArgToks is used when the parsing of default arguments needs
4291 // to be delayed.
4292 CachedTokens *DefArgToks = 0;
4293
Chris Lattnerf97409f2008-04-06 06:57:35 +00004294 // If no parameter was specified, verify that *something* was specified,
4295 // otherwise we have a missing type and identifier.
Chris Lattnere64c5492009-02-27 18:38:20 +00004296 if (DS.isEmpty() && ParmDecl.getIdentifier() == 0 &&
4297 ParmDecl.getNumTypeObjects() == 0) {
Chris Lattnerf97409f2008-04-06 06:57:35 +00004298 // Completely missing, emit error.
4299 Diag(DSStart, diag::err_missing_param);
4300 } else {
4301 // Otherwise, we have something. Add it and let semantic analysis try
4302 // to grok it and add the result to the ParamInfo we are building.
Mike Stump1eb44332009-09-09 15:08:12 +00004303
Chris Lattnerf97409f2008-04-06 06:57:35 +00004304 // Inform the actions module about the parameter declarator, so it gets
4305 // added to the current scope.
John McCalld226f652010-08-21 09:40:31 +00004306 Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl);
Chris Lattner04421082008-04-08 04:40:51 +00004307
4308 // Parse the default argument, if any. We parse the default
4309 // arguments in all dialects; the semantic analysis in
4310 // ActOnParamDefaultArgument will reject the default argument in
4311 // C.
4312 if (Tok.is(tok::equal)) {
Douglas Gregor61366e92008-12-24 00:01:03 +00004313 SourceLocation EqualLoc = Tok.getLocation();
4314
Chris Lattner04421082008-04-08 04:40:51 +00004315 // Parse the default argument
Douglas Gregor72b505b2008-12-16 21:30:33 +00004316 if (D.getContext() == Declarator::MemberContext) {
4317 // If we're inside a class definition, cache the tokens
4318 // corresponding to the default argument. We'll actually parse
4319 // them when we see the end of the class definition.
4320 // FIXME: Templates will require something similar.
4321 // FIXME: Can we use a smart pointer for Toks?
4322 DefArgToks = new CachedTokens;
4323
Mike Stump1eb44332009-09-09 15:08:12 +00004324 if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks,
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +00004325 /*StopAtSemi=*/true,
4326 /*ConsumeFinalToken=*/false)) {
Douglas Gregor72b505b2008-12-16 21:30:33 +00004327 delete DefArgToks;
4328 DefArgToks = 0;
Douglas Gregor61366e92008-12-24 00:01:03 +00004329 Actions.ActOnParamDefaultArgumentError(Param);
Argyrios Kyrtzidis2b602ad2010-08-06 09:47:24 +00004330 } else {
4331 // Mark the end of the default argument so that we know when to
4332 // stop when we parse it later on.
4333 Token DefArgEnd;
4334 DefArgEnd.startToken();
4335 DefArgEnd.setKind(tok::cxx_defaultarg_end);
4336 DefArgEnd.setLocation(Tok.getLocation());
4337 DefArgToks->push_back(DefArgEnd);
Mike Stump1eb44332009-09-09 15:08:12 +00004338 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc,
Anders Carlsson5e300d12009-06-12 16:51:40 +00004339 (*DefArgToks)[1].getLocation());
Argyrios Kyrtzidis2b602ad2010-08-06 09:47:24 +00004340 }
Chris Lattner04421082008-04-08 04:40:51 +00004341 } else {
Douglas Gregor72b505b2008-12-16 21:30:33 +00004342 // Consume the '='.
Douglas Gregor61366e92008-12-24 00:01:03 +00004343 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00004344
Douglas Gregorbe0f7bd2010-09-11 20:24:53 +00004345 // The argument isn't actually potentially evaluated unless it is
4346 // used.
4347 EnterExpressionEvaluationContext Eval(Actions,
Douglas Gregorccc1b5e2012-02-21 00:37:24 +00004348 Sema::PotentiallyEvaluatedIfUsed,
4349 Param);
Douglas Gregorbe0f7bd2010-09-11 20:24:53 +00004350
Sebastian Redl84407ba2012-03-14 15:54:00 +00004351 ExprResult DefArgResult;
Sebastian Redl3e280b52012-03-18 22:25:45 +00004352 if (getLangOpts().CPlusPlus0x && Tok.is(tok::l_brace)) {
4353 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
Sebastian Redl84407ba2012-03-14 15:54:00 +00004354 DefArgResult = ParseBraceInitializer();
Sebastian Redl3e280b52012-03-18 22:25:45 +00004355 } else
Sebastian Redl84407ba2012-03-14 15:54:00 +00004356 DefArgResult = ParseAssignmentExpression();
Douglas Gregor72b505b2008-12-16 21:30:33 +00004357 if (DefArgResult.isInvalid()) {
4358 Actions.ActOnParamDefaultArgumentError(Param);
4359 SkipUntil(tok::comma, tok::r_paren, true, true);
4360 } else {
4361 // Inform the actions module about the default argument
4362 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
John McCall9ae2f072010-08-23 23:25:46 +00004363 DefArgResult.take());
Douglas Gregor72b505b2008-12-16 21:30:33 +00004364 }
Chris Lattner04421082008-04-08 04:40:51 +00004365 }
4366 }
Mike Stump1eb44332009-09-09 15:08:12 +00004367
4368 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
4369 ParmDecl.getIdentifierLoc(), Param,
Douglas Gregor72b505b2008-12-16 21:30:33 +00004370 DefArgToks));
Chris Lattnerf97409f2008-04-06 06:57:35 +00004371 }
4372
4373 // If the next token is a comma, consume it and keep reading arguments.
Douglas Gregored5d6512009-09-22 21:41:40 +00004374 if (Tok.isNot(tok::comma)) {
4375 if (Tok.is(tok::ellipsis)) {
Douglas Gregored5d6512009-09-22 21:41:40 +00004376 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
4377
David Blaikie4e4d0842012-03-11 07:00:24 +00004378 if (!getLangOpts().CPlusPlus) {
Douglas Gregored5d6512009-09-22 21:41:40 +00004379 // We have ellipsis without a preceding ',', which is ill-formed
4380 // in C. Complain and provide the fix.
4381 Diag(EllipsisLoc, diag::err_missing_comma_before_ellipsis)
Douglas Gregor849b2432010-03-31 17:46:05 +00004382 << FixItHint::CreateInsertion(EllipsisLoc, ", ");
Douglas Gregored5d6512009-09-22 21:41:40 +00004383 }
4384 }
4385
4386 break;
4387 }
Mike Stump1eb44332009-09-09 15:08:12 +00004388
Chris Lattnerf97409f2008-04-06 06:57:35 +00004389 // Consume the comma.
4390 ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00004391 }
Mike Stump1eb44332009-09-09 15:08:12 +00004392
Chris Lattner66d28652008-04-06 06:34:08 +00004393}
Chris Lattneref4715c2008-04-06 05:45:57 +00004394
Reid Spencer5f016e22007-07-11 17:01:13 +00004395/// [C90] direct-declarator '[' constant-expression[opt] ']'
4396/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
4397/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
4398/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
4399/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
4400void Parser::ParseBracketDeclarator(Declarator &D) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004401 BalancedDelimiterTracker T(*this, tok::l_square);
4402 T.consumeOpen();
Mike Stump1eb44332009-09-09 15:08:12 +00004403
Chris Lattner378c7e42008-12-18 07:27:21 +00004404 // C array syntax has many features, but by-far the most common is [] and [4].
4405 // This code does a fast path to handle some of the most obvious cases.
4406 if (Tok.getKind() == tok::r_square) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004407 T.consumeClose();
John McCall0b7e6782011-03-24 11:26:52 +00004408 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00004409 MaybeParseCXX0XAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00004410
Chris Lattner378c7e42008-12-18 07:27:21 +00004411 // Remember that we parsed the empty array type.
John McCall60d7b3a2010-08-24 06:29:42 +00004412 ExprResult NumElements;
John McCall0b7e6782011-03-24 11:26:52 +00004413 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004414 T.getOpenLocation(),
4415 T.getCloseLocation()),
4416 attrs, T.getCloseLocation());
Chris Lattner378c7e42008-12-18 07:27:21 +00004417 return;
4418 } else if (Tok.getKind() == tok::numeric_constant &&
4419 GetLookAheadToken(1).is(tok::r_square)) {
4420 // [4] is very common. Parse the numeric constant expression.
Richard Smith36f5cfe2012-03-09 08:00:36 +00004421 ExprResult ExprRes(Actions.ActOnNumericConstant(Tok, getCurScope()));
Chris Lattner378c7e42008-12-18 07:27:21 +00004422 ConsumeToken();
4423
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004424 T.consumeClose();
John McCall0b7e6782011-03-24 11:26:52 +00004425 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00004426 MaybeParseCXX0XAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00004427
Chris Lattner378c7e42008-12-18 07:27:21 +00004428 // Remember that we parsed a array type, and remember its features.
John McCall0b7e6782011-03-24 11:26:52 +00004429 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, 0,
John McCall7f040a92010-12-24 02:08:15 +00004430 ExprRes.release(),
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004431 T.getOpenLocation(),
4432 T.getCloseLocation()),
4433 attrs, T.getCloseLocation());
Chris Lattner378c7e42008-12-18 07:27:21 +00004434 return;
4435 }
Mike Stump1eb44332009-09-09 15:08:12 +00004436
Reid Spencer5f016e22007-07-11 17:01:13 +00004437 // If valid, this location is the position where we read the 'static' keyword.
4438 SourceLocation StaticLoc;
Chris Lattner04d66662007-10-09 17:33:22 +00004439 if (Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00004440 StaticLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00004441
Reid Spencer5f016e22007-07-11 17:01:13 +00004442 // If there is a type-qualifier-list, read it now.
Chris Lattnera1fcbad2008-12-18 06:50:14 +00004443 // Type qualifiers in an array subscript are a C99 feature.
John McCall0b7e6782011-03-24 11:26:52 +00004444 DeclSpec DS(AttrFactory);
Chris Lattner5a69d1c2008-12-18 07:02:59 +00004445 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Mike Stump1eb44332009-09-09 15:08:12 +00004446
Reid Spencer5f016e22007-07-11 17:01:13 +00004447 // If we haven't already read 'static', check to see if there is one after the
4448 // type-qualifier-list.
Chris Lattner04d66662007-10-09 17:33:22 +00004449 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00004450 StaticLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00004451
Reid Spencer5f016e22007-07-11 17:01:13 +00004452 // Handle "direct-declarator [ type-qual-list[opt] * ]".
4453 bool isStar = false;
John McCall60d7b3a2010-08-24 06:29:42 +00004454 ExprResult NumElements;
Mike Stump1eb44332009-09-09 15:08:12 +00004455
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00004456 // Handle the case where we have '[*]' as the array size. However, a leading
4457 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
4458 // the the token after the star is a ']'. Since stars in arrays are
4459 // infrequent, use of lookahead is not costly here.
4460 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattnera711dd02008-04-06 05:27:21 +00004461 ConsumeToken(); // Eat the '*'.
Reid Spencer5f016e22007-07-11 17:01:13 +00004462
Chris Lattnera1fcbad2008-12-18 06:50:14 +00004463 if (StaticLoc.isValid()) {
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00004464 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattnera1fcbad2008-12-18 06:50:14 +00004465 StaticLoc = SourceLocation(); // Drop the static.
4466 }
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00004467 isStar = true;
Chris Lattner04d66662007-10-09 17:33:22 +00004468 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner378c7e42008-12-18 07:27:21 +00004469 // Note, in C89, this production uses the constant-expr production instead
4470 // of assignment-expr. The only difference is that assignment-expr allows
4471 // things like '=' and '*='. Sema rejects these in C89 mode because they
4472 // are not i-c-e's, so we don't need to distinguish between the two here.
Mike Stump1eb44332009-09-09 15:08:12 +00004473
Douglas Gregore0762c92009-06-19 23:52:42 +00004474 // Parse the constant-expression or assignment-expression now (depending
4475 // on dialect).
David Blaikie4e4d0842012-03-11 07:00:24 +00004476 if (getLangOpts().CPlusPlus) {
Douglas Gregore0762c92009-06-19 23:52:42 +00004477 NumElements = ParseConstantExpression();
Eli Friedman71b8fb52012-01-21 01:01:51 +00004478 } else {
4479 EnterExpressionEvaluationContext Unevaluated(Actions,
4480 Sema::ConstantEvaluated);
Douglas Gregore0762c92009-06-19 23:52:42 +00004481 NumElements = ParseAssignmentExpression();
Eli Friedman71b8fb52012-01-21 01:01:51 +00004482 }
Reid Spencer5f016e22007-07-11 17:01:13 +00004483 }
Mike Stump1eb44332009-09-09 15:08:12 +00004484
Reid Spencer5f016e22007-07-11 17:01:13 +00004485 // If there was an error parsing the assignment-expression, recover.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00004486 if (NumElements.isInvalid()) {
Chris Lattner5cb10d32009-04-24 22:30:50 +00004487 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00004488 // If the expression was invalid, skip it.
4489 SkipUntil(tok::r_square);
4490 return;
4491 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00004492
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004493 T.consumeClose();
Sebastian Redlab197ba2009-02-09 18:23:29 +00004494
John McCall0b7e6782011-03-24 11:26:52 +00004495 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00004496 MaybeParseCXX0XAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00004497
Chris Lattner378c7e42008-12-18 07:27:21 +00004498 // Remember that we parsed a array type, and remember its features.
John McCall0b7e6782011-03-24 11:26:52 +00004499 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
Reid Spencer5f016e22007-07-11 17:01:13 +00004500 StaticLoc.isValid(), isStar,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00004501 NumElements.release(),
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004502 T.getOpenLocation(),
4503 T.getCloseLocation()),
4504 attrs, T.getCloseLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00004505}
4506
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004507/// [GNU] typeof-specifier:
4508/// typeof ( expressions )
4509/// typeof ( type-name )
4510/// [GNU/C++] typeof unary-expression
Steve Naroffd1861fd2007-07-31 12:34:36 +00004511///
4512void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner04d66662007-10-09 17:33:22 +00004513 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004514 Token OpTok = Tok;
Steve Naroffd1861fd2007-07-31 12:34:36 +00004515 SourceLocation StartLoc = ConsumeToken();
4516
John McCallcfb708c2010-01-13 20:03:27 +00004517 const bool hasParens = Tok.is(tok::l_paren);
4518
Eli Friedman71b8fb52012-01-21 01:01:51 +00004519 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated);
4520
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004521 bool isCastExpr;
John McCallb3d87482010-08-24 05:47:05 +00004522 ParsedType CastTy;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004523 SourceRange CastRange;
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00004524 ExprResult Operand = ParseExprAfterUnaryExprOrTypeTrait(OpTok, isCastExpr,
4525 CastTy, CastRange);
John McCallcfb708c2010-01-13 20:03:27 +00004526 if (hasParens)
4527 DS.setTypeofParensRange(CastRange);
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004528
4529 if (CastRange.getEnd().isInvalid())
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004530 // FIXME: Not accurate, the range gets one token more than it should.
4531 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004532 else
4533 DS.SetRangeEnd(CastRange.getEnd());
Mike Stump1eb44332009-09-09 15:08:12 +00004534
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004535 if (isCastExpr) {
4536 if (!CastTy) {
4537 DS.SetTypeSpecError();
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004538 return;
Douglas Gregor809070a2009-02-18 17:45:20 +00004539 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004540
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004541 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00004542 unsigned DiagID;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004543 // Check for duplicate type specifiers (e.g. "int typeof(int)").
4544 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00004545 DiagID, CastTy))
4546 Diag(StartLoc, DiagID) << PrevSpec;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004547 return;
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004548 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004549
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004550 // If we get here, the operand to the typeof was an expresion.
4551 if (Operand.isInvalid()) {
4552 DS.SetTypeSpecError();
Steve Naroff9dfa7b42007-08-02 02:53:48 +00004553 return;
Steve Naroffd1861fd2007-07-31 12:34:36 +00004554 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004555
Eli Friedman71b8fb52012-01-21 01:01:51 +00004556 // We might need to transform the operand if it is potentially evaluated.
4557 Operand = Actions.HandleExprEvaluationContextForTypeof(Operand.get());
4558 if (Operand.isInvalid()) {
4559 DS.SetTypeSpecError();
4560 return;
4561 }
4562
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004563 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00004564 unsigned DiagID;
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004565 // Check for duplicate type specifiers (e.g. "int typeof(int)").
4566 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
John McCallb3d87482010-08-24 05:47:05 +00004567 DiagID, Operand.get()))
John McCallfec54012009-08-03 20:12:06 +00004568 Diag(StartLoc, DiagID) << PrevSpec;
Steve Naroffd1861fd2007-07-31 12:34:36 +00004569}
Chris Lattner1b492422010-02-28 18:33:55 +00004570
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00004571/// [C11] atomic-specifier:
Eli Friedmanb001de72011-10-06 23:00:33 +00004572/// _Atomic ( type-name )
4573///
4574void Parser::ParseAtomicSpecifier(DeclSpec &DS) {
4575 assert(Tok.is(tok::kw__Atomic) && "Not an atomic specifier");
4576
4577 SourceLocation StartLoc = ConsumeToken();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004578 BalancedDelimiterTracker T(*this, tok::l_paren);
4579 if (T.expectAndConsume(diag::err_expected_lparen_after, "_Atomic")) {
Eli Friedmanb001de72011-10-06 23:00:33 +00004580 SkipUntil(tok::r_paren);
4581 return;
4582 }
4583
4584 TypeResult Result = ParseTypeName();
4585 if (Result.isInvalid()) {
4586 SkipUntil(tok::r_paren);
4587 return;
4588 }
4589
4590 // Match the ')'
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004591 T.consumeClose();
Eli Friedmanb001de72011-10-06 23:00:33 +00004592
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004593 if (T.getCloseLocation().isInvalid())
Eli Friedmanb001de72011-10-06 23:00:33 +00004594 return;
4595
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004596 DS.setTypeofParensRange(T.getRange());
4597 DS.SetRangeEnd(T.getCloseLocation());
Eli Friedmanb001de72011-10-06 23:00:33 +00004598
4599 const char *PrevSpec = 0;
4600 unsigned DiagID;
4601 if (DS.SetTypeSpecType(DeclSpec::TST_atomic, StartLoc, PrevSpec,
4602 DiagID, Result.release()))
4603 Diag(StartLoc, DiagID) << PrevSpec;
4604}
4605
Chris Lattner1b492422010-02-28 18:33:55 +00004606
4607/// TryAltiVecVectorTokenOutOfLine - Out of line body that should only be called
4608/// from TryAltiVecVectorToken.
4609bool Parser::TryAltiVecVectorTokenOutOfLine() {
4610 Token Next = NextToken();
4611 switch (Next.getKind()) {
4612 default: return false;
4613 case tok::kw_short:
4614 case tok::kw_long:
4615 case tok::kw_signed:
4616 case tok::kw_unsigned:
4617 case tok::kw_void:
4618 case tok::kw_char:
4619 case tok::kw_int:
4620 case tok::kw_float:
4621 case tok::kw_double:
4622 case tok::kw_bool:
4623 case tok::kw___pixel:
4624 Tok.setKind(tok::kw___vector);
4625 return true;
4626 case tok::identifier:
4627 if (Next.getIdentifierInfo() == Ident_pixel) {
4628 Tok.setKind(tok::kw___vector);
4629 return true;
4630 }
4631 return false;
4632 }
4633}
4634
4635bool Parser::TryAltiVecTokenOutOfLine(DeclSpec &DS, SourceLocation Loc,
4636 const char *&PrevSpec, unsigned &DiagID,
4637 bool &isInvalid) {
4638 if (Tok.getIdentifierInfo() == Ident_vector) {
4639 Token Next = NextToken();
4640 switch (Next.getKind()) {
4641 case tok::kw_short:
4642 case tok::kw_long:
4643 case tok::kw_signed:
4644 case tok::kw_unsigned:
4645 case tok::kw_void:
4646 case tok::kw_char:
4647 case tok::kw_int:
4648 case tok::kw_float:
4649 case tok::kw_double:
4650 case tok::kw_bool:
4651 case tok::kw___pixel:
4652 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
4653 return true;
4654 case tok::identifier:
4655 if (Next.getIdentifierInfo() == Ident_pixel) {
4656 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
4657 return true;
4658 }
4659 break;
4660 default:
4661 break;
4662 }
Douglas Gregora8f031f2010-06-16 15:28:57 +00004663 } else if ((Tok.getIdentifierInfo() == Ident_pixel) &&
Chris Lattner1b492422010-02-28 18:33:55 +00004664 DS.isTypeAltiVecVector()) {
4665 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
4666 return true;
4667 }
4668 return false;
4669}