blob: 2fac0dca40253b4be04c792f4e78d8c8c9227de5 [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///
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002614/// [C++0x] enum-head '{' enumerator-list[opt] '}'
2615/// [C++0x] enum-head '{' enumerator-list ',' '}'
2616///
2617/// enum-head: [C++0x]
2618/// enum-key attributes[opt] identifier[opt] enum-base[opt]
2619/// enum-key attributes[opt] nested-name-specifier identifier enum-base[opt]
2620///
2621/// enum-key: [C++0x]
2622/// 'enum'
2623/// 'enum' 'class'
2624/// 'enum' 'struct'
2625///
2626/// enum-base: [C++0x]
2627/// ':' type-specifier-seq
2628///
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002629/// [C++] elaborated-type-specifier:
2630/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
2631///
Chris Lattner4c97d762009-04-12 21:49:30 +00002632void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregor9b9edd62010-03-02 17:53:14 +00002633 const ParsedTemplateInfo &TemplateInfo,
Richard Smith69730c12012-03-12 07:56:15 +00002634 AccessSpecifier AS, DeclSpecContext DSC) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002635 // Parse the tag portion of this.
Douglas Gregor374929f2009-09-18 15:37:17 +00002636 if (Tok.is(tok::code_completion)) {
2637 // Code completion for an enum name.
Douglas Gregor23c94db2010-07-02 17:43:08 +00002638 Actions.CodeCompleteTag(getCurScope(), DeclSpec::TST_enum);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002639 return cutOffParsing();
Douglas Gregor374929f2009-09-18 15:37:17 +00002640 }
John McCall57c13002011-07-06 05:58:41 +00002641
Richard Smithbdad7a22012-01-10 01:33:14 +00002642 SourceLocation ScopedEnumKWLoc;
John McCall57c13002011-07-06 05:58:41 +00002643 bool IsScopedUsingClassTag = false;
2644
David Blaikie4e4d0842012-03-11 07:00:24 +00002645 if (getLangOpts().CPlusPlus0x &&
John McCall57c13002011-07-06 05:58:41 +00002646 (Tok.is(tok::kw_class) || Tok.is(tok::kw_struct))) {
Richard Smith7fe62082011-10-15 05:09:34 +00002647 Diag(Tok, diag::warn_cxx98_compat_scoped_enum);
John McCall57c13002011-07-06 05:58:41 +00002648 IsScopedUsingClassTag = Tok.is(tok::kw_class);
Richard Smithbdad7a22012-01-10 01:33:14 +00002649 ScopedEnumKWLoc = ConsumeToken();
John McCall57c13002011-07-06 05:58:41 +00002650 }
Douglas Gregor374929f2009-09-18 15:37:17 +00002651
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002652 // If attributes exist after tag, parse them.
John McCall0b7e6782011-03-24 11:26:52 +00002653 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00002654 MaybeParseGNUAttributes(attrs);
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002655
Aaron Ballman6454a022012-03-01 04:09:28 +00002656 // If declspecs exist after tag, parse them.
2657 while (Tok.is(tok::kw___declspec))
2658 ParseMicrosoftDeclSpec(attrs);
2659
Richard Smith7796eb52012-03-12 08:56:40 +00002660 // Enum definitions should not be parsed in a trailing-return-type.
2661 bool AllowDeclaration = DSC != DSC_trailing;
2662
2663 bool AllowFixedUnderlyingType = AllowDeclaration &&
2664 (getLangOpts().CPlusPlus0x || getLangOpts().MicrosoftExt ||
2665 getLangOpts().ObjC2);
John McCall57c13002011-07-06 05:58:41 +00002666
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00002667 CXXScopeSpec &SS = DS.getTypeSpecScope();
David Blaikie4e4d0842012-03-11 07:00:24 +00002668 if (getLangOpts().CPlusPlus) {
John McCall57c13002011-07-06 05:58:41 +00002669 // "enum foo : bar;" is not a potential typo for "enum foo::bar;"
2670 // if a fixed underlying type is allowed.
2671 ColonProtectionRAIIObject X(*this, AllowFixedUnderlyingType);
2672
Douglas Gregorefaa93a2011-11-07 17:33:42 +00002673 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
2674 /*EnteringContext=*/false))
John McCall9ba61662010-02-26 08:45:28 +00002675 return;
2676
2677 if (SS.isSet() && Tok.isNot(tok::identifier)) {
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002678 Diag(Tok, diag::err_expected_ident);
2679 if (Tok.isNot(tok::l_brace)) {
2680 // Has no name and is not a definition.
2681 // Skip the rest of this declarator, up until the comma or semicolon.
2682 SkipUntil(tok::comma, true);
2683 return;
2684 }
2685 }
2686 }
Mike Stump1eb44332009-09-09 15:08:12 +00002687
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002688 // Must have either 'enum name' or 'enum {...}'.
Douglas Gregorb9075602011-02-22 02:55:24 +00002689 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace) &&
Richard Smith7796eb52012-03-12 08:56:40 +00002690 !(AllowFixedUnderlyingType && Tok.is(tok::colon))) {
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002691 Diag(Tok, diag::err_expected_ident_lbrace);
Mike Stump1eb44332009-09-09 15:08:12 +00002692
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002693 // Skip the rest of this declarator, up until the comma or semicolon.
2694 SkipUntil(tok::comma, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00002695 return;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002696 }
Mike Stump1eb44332009-09-09 15:08:12 +00002697
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002698 // If an identifier is present, consume and remember it.
2699 IdentifierInfo *Name = 0;
2700 SourceLocation NameLoc;
2701 if (Tok.is(tok::identifier)) {
2702 Name = Tok.getIdentifierInfo();
2703 NameLoc = ConsumeToken();
2704 }
Mike Stump1eb44332009-09-09 15:08:12 +00002705
Richard Smithbdad7a22012-01-10 01:33:14 +00002706 if (!Name && ScopedEnumKWLoc.isValid()) {
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002707 // C++0x 7.2p2: The optional identifier shall not be omitted in the
2708 // declaration of a scoped enumeration.
2709 Diag(Tok, diag::err_scoped_enum_missing_identifier);
Richard Smithbdad7a22012-01-10 01:33:14 +00002710 ScopedEnumKWLoc = SourceLocation();
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00002711 IsScopedUsingClassTag = false;
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002712 }
2713
2714 TypeResult BaseType;
2715
Douglas Gregora61b3e72010-12-01 17:42:47 +00002716 // Parse the fixed underlying type.
Douglas Gregorb9075602011-02-22 02:55:24 +00002717 if (AllowFixedUnderlyingType && Tok.is(tok::colon)) {
Douglas Gregora61b3e72010-12-01 17:42:47 +00002718 bool PossibleBitfield = false;
2719 if (getCurScope()->getFlags() & Scope::ClassScope) {
2720 // If we're in class scope, this can either be an enum declaration with
2721 // an underlying type, or a declaration of a bitfield member. We try to
2722 // use a simple disambiguation scheme first to catch the common cases
2723 // (integer literal, sizeof); if it's still ambiguous, we then consider
2724 // anything that's a simple-type-specifier followed by '(' as an
2725 // expression. This suffices because function types are not valid
2726 // underlying types anyway.
2727 TPResult TPR = isExpressionOrTypeSpecifierSimple(NextToken().getKind());
2728 // If the next token starts an expression, we know we're parsing a
2729 // bit-field. This is the common case.
2730 if (TPR == TPResult::True())
2731 PossibleBitfield = true;
2732 // If the next token starts a type-specifier-seq, it may be either a
2733 // a fixed underlying type or the start of a function-style cast in C++;
2734 // lookahead one more token to see if it's obvious that we have a
2735 // fixed underlying type.
2736 else if (TPR == TPResult::False() &&
2737 GetLookAheadToken(2).getKind() == tok::semi) {
2738 // Consume the ':'.
2739 ConsumeToken();
2740 } else {
2741 // We have the start of a type-specifier-seq, so we have to perform
2742 // tentative parsing to determine whether we have an expression or a
2743 // type.
2744 TentativeParsingAction TPA(*this);
2745
2746 // Consume the ':'.
2747 ConsumeToken();
Richard Smithd81e9612012-02-23 01:36:12 +00002748
2749 // If we see a type specifier followed by an open-brace, we have an
2750 // ambiguity between an underlying type and a C++11 braced
2751 // function-style cast. Resolve this by always treating it as an
2752 // underlying type.
2753 // FIXME: The standard is not entirely clear on how to disambiguate in
2754 // this case.
David Blaikie4e4d0842012-03-11 07:00:24 +00002755 if ((getLangOpts().CPlusPlus &&
Richard Smithd81e9612012-02-23 01:36:12 +00002756 isCXXDeclarationSpecifier(TPResult::True()) != TPResult::True()) ||
David Blaikie4e4d0842012-03-11 07:00:24 +00002757 (!getLangOpts().CPlusPlus && !isDeclarationSpecifier(true))) {
Douglas Gregora61b3e72010-12-01 17:42:47 +00002758 // We'll parse this as a bitfield later.
2759 PossibleBitfield = true;
2760 TPA.Revert();
2761 } else {
2762 // We have a type-specifier-seq.
2763 TPA.Commit();
2764 }
2765 }
2766 } else {
2767 // Consume the ':'.
2768 ConsumeToken();
2769 }
2770
2771 if (!PossibleBitfield) {
2772 SourceRange Range;
2773 BaseType = ParseTypeName(&Range);
Douglas Gregor86f208c2011-02-22 20:32:04 +00002774
David Blaikie4e4d0842012-03-11 07:00:24 +00002775 if (!getLangOpts().CPlusPlus0x && !getLangOpts().ObjC2)
Douglas Gregor86f208c2011-02-22 20:32:04 +00002776 Diag(StartLoc, diag::ext_ms_enum_fixed_underlying_type)
2777 << Range;
David Blaikie4e4d0842012-03-11 07:00:24 +00002778 if (getLangOpts().CPlusPlus0x)
Richard Smith7fe62082011-10-15 05:09:34 +00002779 Diag(StartLoc, diag::warn_cxx98_compat_enum_fixed_underlying_type);
Douglas Gregora61b3e72010-12-01 17:42:47 +00002780 }
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002781 }
2782
Richard Smithbdad7a22012-01-10 01:33:14 +00002783 // There are four options here. If we have 'friend enum foo;' then this is a
2784 // friend declaration, and cannot have an accompanying definition. If we have
2785 // 'enum foo;', then this is a forward declaration. If we have
2786 // 'enum foo {...' then this is a definition. Otherwise we have something
2787 // like 'enum foo xyz', a reference.
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002788 //
2789 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
2790 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
2791 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
2792 //
John McCallf312b1e2010-08-26 23:41:50 +00002793 Sema::TagUseKind TUK;
Richard Smithbdad7a22012-01-10 01:33:14 +00002794 if (DS.isFriendSpecified())
2795 TUK = Sema::TUK_Friend;
Richard Smith7796eb52012-03-12 08:56:40 +00002796 else if (!AllowDeclaration)
2797 TUK = Sema::TUK_Reference;
Richard Smithbdad7a22012-01-10 01:33:14 +00002798 else if (Tok.is(tok::l_brace))
John McCallf312b1e2010-08-26 23:41:50 +00002799 TUK = Sema::TUK_Definition;
Richard Smith69730c12012-03-12 07:56:15 +00002800 else if (Tok.is(tok::semi) && DSC != DSC_type_specifier)
John McCallf312b1e2010-08-26 23:41:50 +00002801 TUK = Sema::TUK_Declaration;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002802 else
John McCallf312b1e2010-08-26 23:41:50 +00002803 TUK = Sema::TUK_Reference;
Douglas Gregor8fc6d232010-05-03 17:48:54 +00002804
2805 // enums cannot be templates, although they can be referenced from a
2806 // template.
2807 if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate &&
John McCallf312b1e2010-08-26 23:41:50 +00002808 TUK != Sema::TUK_Reference) {
Douglas Gregor8fc6d232010-05-03 17:48:54 +00002809 Diag(Tok, diag::err_enum_template);
2810
2811 // Skip the rest of this declarator, up until the comma or semicolon.
2812 SkipUntil(tok::comma, true);
2813 return;
2814 }
2815
Douglas Gregorb9075602011-02-22 02:55:24 +00002816 if (!Name && TUK != Sema::TUK_Definition) {
2817 Diag(Tok, diag::err_enumerator_unnamed_no_def);
2818
2819 // Skip the rest of this declarator, up until the comma or semicolon.
2820 SkipUntil(tok::comma, true);
2821 return;
2822 }
2823
Douglas Gregor402abb52009-05-28 23:31:59 +00002824 bool Owned = false;
John McCallc4e70192009-09-11 04:59:25 +00002825 bool IsDependent = false;
Douglas Gregor48c89f42010-04-24 16:38:41 +00002826 const char *PrevSpec = 0;
2827 unsigned DiagID;
John McCalld226f652010-08-21 09:40:31 +00002828 Decl *TagDecl = Actions.ActOnTag(getCurScope(), DeclSpec::TST_enum, TUK,
John McCall7f040a92010-12-24 02:08:15 +00002829 StartLoc, SS, Name, NameLoc, attrs.getList(),
Douglas Gregore7612302011-09-09 19:05:14 +00002830 AS, DS.getModulePrivateSpecLoc(),
John McCallf312b1e2010-08-26 23:41:50 +00002831 MultiTemplateParamsArg(Actions),
Richard Smithbdad7a22012-01-10 01:33:14 +00002832 Owned, IsDependent, ScopedEnumKWLoc,
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00002833 IsScopedUsingClassTag, BaseType);
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002834
Douglas Gregor48c89f42010-04-24 16:38:41 +00002835 if (IsDependent) {
2836 // This enum has a dependent nested-name-specifier. Handle it as a
2837 // dependent tag.
2838 if (!Name) {
2839 DS.SetTypeSpecError();
2840 Diag(Tok, diag::err_expected_type_name_after_typename);
2841 return;
2842 }
2843
Douglas Gregor23c94db2010-07-02 17:43:08 +00002844 TypeResult Type = Actions.ActOnDependentTag(getCurScope(), DeclSpec::TST_enum,
Douglas Gregor48c89f42010-04-24 16:38:41 +00002845 TUK, SS, Name, StartLoc,
2846 NameLoc);
2847 if (Type.isInvalid()) {
2848 DS.SetTypeSpecError();
2849 return;
2850 }
2851
Abramo Bagnara0daaf322011-03-16 20:16:18 +00002852 if (DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc,
2853 NameLoc.isValid() ? NameLoc : StartLoc,
2854 PrevSpec, DiagID, Type.get()))
Douglas Gregor48c89f42010-04-24 16:38:41 +00002855 Diag(StartLoc, DiagID) << PrevSpec;
2856
2857 return;
2858 }
Mike Stump1eb44332009-09-09 15:08:12 +00002859
John McCalld226f652010-08-21 09:40:31 +00002860 if (!TagDecl) {
Douglas Gregor48c89f42010-04-24 16:38:41 +00002861 // The action failed to produce an enumeration tag. If this is a
2862 // definition, consume the entire definition.
Richard Smith7796eb52012-03-12 08:56:40 +00002863 if (Tok.is(tok::l_brace) && TUK != Sema::TUK_Reference) {
Douglas Gregor48c89f42010-04-24 16:38:41 +00002864 ConsumeBrace();
2865 SkipUntil(tok::r_brace);
2866 }
2867
2868 DS.SetTypeSpecError();
2869 return;
2870 }
Richard Smithbdad7a22012-01-10 01:33:14 +00002871
Richard Smith7796eb52012-03-12 08:56:40 +00002872 if (Tok.is(tok::l_brace) && TUK != Sema::TUK_Reference) {
Richard Smithbdad7a22012-01-10 01:33:14 +00002873 if (TUK == Sema::TUK_Friend)
2874 Diag(Tok, diag::err_friend_decl_defines_type)
2875 << SourceRange(DS.getFriendSpecLoc());
Reid Spencer5f016e22007-07-11 17:01:13 +00002876 ParseEnumBody(StartLoc, TagDecl);
Richard Smithbdad7a22012-01-10 01:33:14 +00002877 }
Mike Stump1eb44332009-09-09 15:08:12 +00002878
Abramo Bagnara0daaf322011-03-16 20:16:18 +00002879 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc,
2880 NameLoc.isValid() ? NameLoc : StartLoc,
2881 PrevSpec, DiagID, TagDecl, Owned))
John McCallfec54012009-08-03 20:12:06 +00002882 Diag(StartLoc, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00002883}
2884
2885/// ParseEnumBody - Parse a {} enclosed enumerator-list.
2886/// enumerator-list:
2887/// enumerator
2888/// enumerator-list ',' enumerator
2889/// enumerator:
2890/// enumeration-constant
2891/// enumeration-constant '=' constant-expression
2892/// enumeration-constant:
2893/// identifier
2894///
John McCalld226f652010-08-21 09:40:31 +00002895void Parser::ParseEnumBody(SourceLocation StartLoc, Decl *EnumDecl) {
Douglas Gregor074149e2009-01-05 19:45:36 +00002896 // Enter the scope of the enum body and start the definition.
2897 ParseScope EnumScope(this, Scope::DeclScope);
Douglas Gregor23c94db2010-07-02 17:43:08 +00002898 Actions.ActOnTagStartDefinition(getCurScope(), EnumDecl);
Douglas Gregor074149e2009-01-05 19:45:36 +00002899
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002900 BalancedDelimiterTracker T(*this, tok::l_brace);
2901 T.consumeOpen();
Mike Stump1eb44332009-09-09 15:08:12 +00002902
Chris Lattner7946dd32007-08-27 17:24:30 +00002903 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
David Blaikie4e4d0842012-03-11 07:00:24 +00002904 if (Tok.is(tok::r_brace) && !getLangOpts().CPlusPlus)
Fariborz Jahanian05115522010-05-28 22:23:22 +00002905 Diag(Tok, diag::error_empty_enum);
Mike Stump1eb44332009-09-09 15:08:12 +00002906
Chris Lattner5f9e2722011-07-23 10:55:15 +00002907 SmallVector<Decl *, 32> EnumConstantDecls;
Reid Spencer5f016e22007-07-11 17:01:13 +00002908
John McCalld226f652010-08-21 09:40:31 +00002909 Decl *LastEnumConstDecl = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00002910
Reid Spencer5f016e22007-07-11 17:01:13 +00002911 // Parse the enumerator-list.
Chris Lattner04d66662007-10-09 17:33:22 +00002912 while (Tok.is(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002913 IdentifierInfo *Ident = Tok.getIdentifierInfo();
2914 SourceLocation IdentLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00002915
John McCall5b629aa2010-10-22 23:36:17 +00002916 // If attributes exist after the enumerator, parse them.
John McCall0b7e6782011-03-24 11:26:52 +00002917 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00002918 MaybeParseGNUAttributes(attrs);
John McCall5b629aa2010-10-22 23:36:17 +00002919
Reid Spencer5f016e22007-07-11 17:01:13 +00002920 SourceLocation EqualLoc;
John McCall60d7b3a2010-08-24 06:29:42 +00002921 ExprResult AssignedVal;
Fariborz Jahanian5a477db2011-12-09 01:15:54 +00002922 ParsingDeclRAIIObject PD(*this);
2923
Chris Lattner04d66662007-10-09 17:33:22 +00002924 if (Tok.is(tok::equal)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002925 EqualLoc = ConsumeToken();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002926 AssignedVal = ParseConstantExpression();
2927 if (AssignedVal.isInvalid())
Reid Spencer5f016e22007-07-11 17:01:13 +00002928 SkipUntil(tok::comma, tok::r_brace, true, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00002929 }
Mike Stump1eb44332009-09-09 15:08:12 +00002930
Reid Spencer5f016e22007-07-11 17:01:13 +00002931 // Install the enumerator constant into EnumDecl.
John McCalld226f652010-08-21 09:40:31 +00002932 Decl *EnumConstDecl = Actions.ActOnEnumConstant(getCurScope(), EnumDecl,
2933 LastEnumConstDecl,
2934 IdentLoc, Ident,
John McCall7f040a92010-12-24 02:08:15 +00002935 attrs.getList(), EqualLoc,
John McCalld226f652010-08-21 09:40:31 +00002936 AssignedVal.release());
Fariborz Jahanian5a477db2011-12-09 01:15:54 +00002937 PD.complete(EnumConstDecl);
2938
Reid Spencer5f016e22007-07-11 17:01:13 +00002939 EnumConstantDecls.push_back(EnumConstDecl);
2940 LastEnumConstDecl = EnumConstDecl;
Mike Stump1eb44332009-09-09 15:08:12 +00002941
Douglas Gregor751f6922010-09-07 14:51:08 +00002942 if (Tok.is(tok::identifier)) {
2943 // We're missing a comma between enumerators.
2944 SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation);
2945 Diag(Loc, diag::err_enumerator_list_missing_comma)
2946 << FixItHint::CreateInsertion(Loc, ", ");
2947 continue;
2948 }
2949
Chris Lattner04d66662007-10-09 17:33:22 +00002950 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +00002951 break;
2952 SourceLocation CommaLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00002953
Richard Smith7fe62082011-10-15 05:09:34 +00002954 if (Tok.isNot(tok::identifier)) {
David Blaikie4e4d0842012-03-11 07:00:24 +00002955 if (!getLangOpts().C99 && !getLangOpts().CPlusPlus0x)
Richard Smith7fe62082011-10-15 05:09:34 +00002956 Diag(CommaLoc, diag::ext_enumerator_list_comma)
David Blaikie4e4d0842012-03-11 07:00:24 +00002957 << getLangOpts().CPlusPlus
Richard Smith7fe62082011-10-15 05:09:34 +00002958 << FixItHint::CreateRemoval(CommaLoc);
David Blaikie4e4d0842012-03-11 07:00:24 +00002959 else if (getLangOpts().CPlusPlus0x)
Richard Smith7fe62082011-10-15 05:09:34 +00002960 Diag(CommaLoc, diag::warn_cxx98_compat_enumerator_list_comma)
2961 << FixItHint::CreateRemoval(CommaLoc);
2962 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002963 }
Mike Stump1eb44332009-09-09 15:08:12 +00002964
Reid Spencer5f016e22007-07-11 17:01:13 +00002965 // Eat the }.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002966 T.consumeClose();
Reid Spencer5f016e22007-07-11 17:01:13 +00002967
Reid Spencer5f016e22007-07-11 17:01:13 +00002968 // If attributes exist after the identifier list, parse them.
John McCall0b7e6782011-03-24 11:26:52 +00002969 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00002970 MaybeParseGNUAttributes(attrs);
Douglas Gregor72de6672009-01-08 20:45:30 +00002971
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002972 Actions.ActOnEnumBody(StartLoc, T.getOpenLocation(), T.getCloseLocation(),
2973 EnumDecl, EnumConstantDecls.data(),
2974 EnumConstantDecls.size(), getCurScope(),
2975 attrs.getList());
Mike Stump1eb44332009-09-09 15:08:12 +00002976
Douglas Gregor72de6672009-01-08 20:45:30 +00002977 EnumScope.Exit();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002978 Actions.ActOnTagFinishDefinition(getCurScope(), EnumDecl,
2979 T.getCloseLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00002980}
2981
2982/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff5f8aa692008-02-11 23:15:56 +00002983/// start of a type-qualifier-list.
2984bool Parser::isTypeQualifier() const {
2985 switch (Tok.getKind()) {
2986 default: return false;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00002987
2988 // type-qualifier only in OpenCL
2989 case tok::kw_private:
David Blaikie4e4d0842012-03-11 07:00:24 +00002990 return getLangOpts().OpenCL;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00002991
Steve Naroff5f8aa692008-02-11 23:15:56 +00002992 // type-qualifier
2993 case tok::kw_const:
2994 case tok::kw_volatile:
2995 case tok::kw_restrict:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00002996 case tok::kw___private:
2997 case tok::kw___local:
2998 case tok::kw___global:
2999 case tok::kw___constant:
3000 case tok::kw___read_only:
3001 case tok::kw___read_write:
3002 case tok::kw___write_only:
Steve Naroff5f8aa692008-02-11 23:15:56 +00003003 return true;
3004 }
3005}
3006
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003007/// isKnownToBeTypeSpecifier - Return true if we know that the specified token
3008/// is definitely a type-specifier. Return false if it isn't part of a type
3009/// specifier or if we're not sure.
3010bool Parser::isKnownToBeTypeSpecifier(const Token &Tok) const {
3011 switch (Tok.getKind()) {
3012 default: return false;
3013 // type-specifiers
3014 case tok::kw_short:
3015 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00003016 case tok::kw___int64:
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003017 case tok::kw_signed:
3018 case tok::kw_unsigned:
3019 case tok::kw__Complex:
3020 case tok::kw__Imaginary:
3021 case tok::kw_void:
3022 case tok::kw_char:
3023 case tok::kw_wchar_t:
3024 case tok::kw_char16_t:
3025 case tok::kw_char32_t:
3026 case tok::kw_int:
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00003027 case tok::kw_half:
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003028 case tok::kw_float:
3029 case tok::kw_double:
3030 case tok::kw_bool:
3031 case tok::kw__Bool:
3032 case tok::kw__Decimal32:
3033 case tok::kw__Decimal64:
3034 case tok::kw__Decimal128:
3035 case tok::kw___vector:
3036
3037 // struct-or-union-specifier (C99) or class-specifier (C++)
3038 case tok::kw_class:
3039 case tok::kw_struct:
3040 case tok::kw_union:
3041 // enum-specifier
3042 case tok::kw_enum:
3043
3044 // typedef-name
3045 case tok::annot_typename:
3046 return true;
3047 }
3048}
3049
Steve Naroff5f8aa692008-02-11 23:15:56 +00003050/// isTypeSpecifierQualifier - Return true if the current token could be the
Reid Spencer5f016e22007-07-11 17:01:13 +00003051/// start of a specifier-qualifier-list.
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003052bool Parser::isTypeSpecifierQualifier() {
Reid Spencer5f016e22007-07-11 17:01:13 +00003053 switch (Tok.getKind()) {
3054 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00003055
Chris Lattner166a8fc2009-01-04 23:41:41 +00003056 case tok::identifier: // foo::bar
John Thompson82287d12010-02-05 00:12:22 +00003057 if (TryAltiVecVectorToken())
3058 return true;
3059 // Fall through.
Douglas Gregord57959a2009-03-27 23:10:48 +00003060 case tok::kw_typename: // typename T::type
Chris Lattner166a8fc2009-01-04 23:41:41 +00003061 // Annotate typenames and C++ scope specifiers. If we get one, just
3062 // recurse to handle whatever we get.
3063 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003064 return true;
3065 if (Tok.is(tok::identifier))
3066 return false;
3067 return isTypeSpecifierQualifier();
Douglas Gregord57959a2009-03-27 23:10:48 +00003068
Chris Lattner166a8fc2009-01-04 23:41:41 +00003069 case tok::coloncolon: // ::foo::bar
3070 if (NextToken().is(tok::kw_new) || // ::new
3071 NextToken().is(tok::kw_delete)) // ::delete
3072 return false;
3073
Chris Lattner166a8fc2009-01-04 23:41:41 +00003074 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003075 return true;
3076 return isTypeSpecifierQualifier();
Mike Stump1eb44332009-09-09 15:08:12 +00003077
Reid Spencer5f016e22007-07-11 17:01:13 +00003078 // GNU attributes support.
3079 case tok::kw___attribute:
Steve Naroffd1861fd2007-07-31 12:34:36 +00003080 // GNU typeof support.
3081 case tok::kw_typeof:
Mike Stump1eb44332009-09-09 15:08:12 +00003082
Reid Spencer5f016e22007-07-11 17:01:13 +00003083 // type-specifiers
3084 case tok::kw_short:
3085 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00003086 case tok::kw___int64:
Reid Spencer5f016e22007-07-11 17:01:13 +00003087 case tok::kw_signed:
3088 case tok::kw_unsigned:
3089 case tok::kw__Complex:
3090 case tok::kw__Imaginary:
3091 case tok::kw_void:
3092 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00003093 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00003094 case tok::kw_char16_t:
3095 case tok::kw_char32_t:
Reid Spencer5f016e22007-07-11 17:01:13 +00003096 case tok::kw_int:
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00003097 case tok::kw_half:
Reid Spencer5f016e22007-07-11 17:01:13 +00003098 case tok::kw_float:
3099 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00003100 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00003101 case tok::kw__Bool:
3102 case tok::kw__Decimal32:
3103 case tok::kw__Decimal64:
3104 case tok::kw__Decimal128:
John Thompson82287d12010-02-05 00:12:22 +00003105 case tok::kw___vector:
Mike Stump1eb44332009-09-09 15:08:12 +00003106
Chris Lattner99dc9142008-04-13 18:59:07 +00003107 // struct-or-union-specifier (C99) or class-specifier (C++)
3108 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00003109 case tok::kw_struct:
3110 case tok::kw_union:
3111 // enum-specifier
3112 case tok::kw_enum:
Mike Stump1eb44332009-09-09 15:08:12 +00003113
Reid Spencer5f016e22007-07-11 17:01:13 +00003114 // type-qualifier
3115 case tok::kw_const:
3116 case tok::kw_volatile:
3117 case tok::kw_restrict:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003118
3119 // typedef-name
Chris Lattnerb31757b2009-01-06 05:06:21 +00003120 case tok::annot_typename:
Reid Spencer5f016e22007-07-11 17:01:13 +00003121 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00003122
Chris Lattner7c186be2008-10-20 00:25:30 +00003123 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
3124 case tok::less:
David Blaikie4e4d0842012-03-11 07:00:24 +00003125 return getLangOpts().ObjC1;
Mike Stump1eb44332009-09-09 15:08:12 +00003126
Steve Naroff239f0732008-12-25 14:16:32 +00003127 case tok::kw___cdecl:
3128 case tok::kw___stdcall:
3129 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00003130 case tok::kw___thiscall:
Eli Friedman290eeb02009-06-08 23:27:34 +00003131 case tok::kw___w64:
3132 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00003133 case tok::kw___ptr32:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003134 case tok::kw___pascal:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00003135 case tok::kw___unaligned:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003136
3137 case tok::kw___private:
3138 case tok::kw___local:
3139 case tok::kw___global:
3140 case tok::kw___constant:
3141 case tok::kw___read_only:
3142 case tok::kw___read_write:
3143 case tok::kw___write_only:
3144
Eli Friedman290eeb02009-06-08 23:27:34 +00003145 return true;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003146
3147 case tok::kw_private:
David Blaikie4e4d0842012-03-11 07:00:24 +00003148 return getLangOpts().OpenCL;
Eli Friedmanb001de72011-10-06 23:00:33 +00003149
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00003150 // C11 _Atomic()
Eli Friedmanb001de72011-10-06 23:00:33 +00003151 case tok::kw__Atomic:
3152 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00003153 }
3154}
3155
3156/// isDeclarationSpecifier() - Return true if the current token is part of a
3157/// declaration specifier.
Douglas Gregor9497a732010-09-16 01:51:54 +00003158///
3159/// \param DisambiguatingWithExpression True to indicate that the purpose of
3160/// this check is to disambiguate between an expression and a declaration.
3161bool Parser::isDeclarationSpecifier(bool DisambiguatingWithExpression) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003162 switch (Tok.getKind()) {
3163 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00003164
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003165 case tok::kw_private:
David Blaikie4e4d0842012-03-11 07:00:24 +00003166 return getLangOpts().OpenCL;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003167
Chris Lattner166a8fc2009-01-04 23:41:41 +00003168 case tok::identifier: // foo::bar
Steve Naroff61f72cb2009-03-09 21:12:44 +00003169 // Unfortunate hack to support "Class.factoryMethod" notation.
David Blaikie4e4d0842012-03-11 07:00:24 +00003170 if (getLangOpts().ObjC1 && NextToken().is(tok::period))
Steve Naroff61f72cb2009-03-09 21:12:44 +00003171 return false;
John Thompson82287d12010-02-05 00:12:22 +00003172 if (TryAltiVecVectorToken())
3173 return true;
3174 // Fall through.
David Blaikie42d6d0c2011-12-04 05:04:18 +00003175 case tok::kw_decltype: // decltype(T())::type
Douglas Gregord57959a2009-03-27 23:10:48 +00003176 case tok::kw_typename: // typename T::type
Chris Lattner166a8fc2009-01-04 23:41:41 +00003177 // Annotate typenames and C++ scope specifiers. If we get one, just
3178 // recurse to handle whatever we get.
3179 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003180 return true;
3181 if (Tok.is(tok::identifier))
3182 return false;
Douglas Gregor9497a732010-09-16 01:51:54 +00003183
3184 // If we're in Objective-C and we have an Objective-C class type followed
3185 // by an identifier and then either ':' or ']', in a place where an
3186 // expression is permitted, then this is probably a class message send
3187 // missing the initial '['. In this case, we won't consider this to be
3188 // the start of a declaration.
3189 if (DisambiguatingWithExpression &&
3190 isStartOfObjCClassMessageMissingOpenBracket())
3191 return false;
3192
John McCall9ba61662010-02-26 08:45:28 +00003193 return isDeclarationSpecifier();
3194
Chris Lattner166a8fc2009-01-04 23:41:41 +00003195 case tok::coloncolon: // ::foo::bar
3196 if (NextToken().is(tok::kw_new) || // ::new
3197 NextToken().is(tok::kw_delete)) // ::delete
3198 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00003199
Chris Lattner166a8fc2009-01-04 23:41:41 +00003200 // Annotate typenames and C++ scope specifiers. If we get one, just
3201 // recurse to handle whatever we get.
3202 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003203 return true;
3204 return isDeclarationSpecifier();
Mike Stump1eb44332009-09-09 15:08:12 +00003205
Reid Spencer5f016e22007-07-11 17:01:13 +00003206 // storage-class-specifier
3207 case tok::kw_typedef:
3208 case tok::kw_extern:
Steve Naroff8d54bf22007-12-18 00:16:02 +00003209 case tok::kw___private_extern__:
Reid Spencer5f016e22007-07-11 17:01:13 +00003210 case tok::kw_static:
3211 case tok::kw_auto:
3212 case tok::kw_register:
3213 case tok::kw___thread:
Mike Stump1eb44332009-09-09 15:08:12 +00003214
Douglas Gregor8d267c52011-09-09 02:06:17 +00003215 // Modules
3216 case tok::kw___module_private__:
3217
Reid Spencer5f016e22007-07-11 17:01:13 +00003218 // type-specifiers
3219 case tok::kw_short:
3220 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00003221 case tok::kw___int64:
Reid Spencer5f016e22007-07-11 17:01:13 +00003222 case tok::kw_signed:
3223 case tok::kw_unsigned:
3224 case tok::kw__Complex:
3225 case tok::kw__Imaginary:
3226 case tok::kw_void:
3227 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00003228 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00003229 case tok::kw_char16_t:
3230 case tok::kw_char32_t:
3231
Reid Spencer5f016e22007-07-11 17:01:13 +00003232 case tok::kw_int:
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00003233 case tok::kw_half:
Reid Spencer5f016e22007-07-11 17:01:13 +00003234 case tok::kw_float:
3235 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00003236 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00003237 case tok::kw__Bool:
3238 case tok::kw__Decimal32:
3239 case tok::kw__Decimal64:
3240 case tok::kw__Decimal128:
John Thompson82287d12010-02-05 00:12:22 +00003241 case tok::kw___vector:
Mike Stump1eb44332009-09-09 15:08:12 +00003242
Chris Lattner99dc9142008-04-13 18:59:07 +00003243 // struct-or-union-specifier (C99) or class-specifier (C++)
3244 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00003245 case tok::kw_struct:
3246 case tok::kw_union:
3247 // enum-specifier
3248 case tok::kw_enum:
Mike Stump1eb44332009-09-09 15:08:12 +00003249
Reid Spencer5f016e22007-07-11 17:01:13 +00003250 // type-qualifier
3251 case tok::kw_const:
3252 case tok::kw_volatile:
3253 case tok::kw_restrict:
Steve Naroffd1861fd2007-07-31 12:34:36 +00003254
Reid Spencer5f016e22007-07-11 17:01:13 +00003255 // function-specifier
3256 case tok::kw_inline:
Douglas Gregorb48fe382008-10-31 09:07:45 +00003257 case tok::kw_virtual:
3258 case tok::kw_explicit:
Chris Lattnerd6c7c182007-08-09 16:40:21 +00003259
Peter Collingbournec6eb44b2011-04-15 00:35:57 +00003260 // static_assert-declaration
3261 case tok::kw__Static_assert:
3262
Chris Lattner1ef08762007-08-09 17:01:07 +00003263 // GNU typeof support.
3264 case tok::kw_typeof:
Mike Stump1eb44332009-09-09 15:08:12 +00003265
Chris Lattner1ef08762007-08-09 17:01:07 +00003266 // GNU attributes.
Chris Lattnerd6c7c182007-08-09 16:40:21 +00003267 case tok::kw___attribute:
Reid Spencer5f016e22007-07-11 17:01:13 +00003268 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00003269
Francois Pichete3d49b42011-06-19 08:02:06 +00003270 // C++0x decltype.
David Blaikie42d6d0c2011-12-04 05:04:18 +00003271 case tok::annot_decltype:
Francois Pichete3d49b42011-06-19 08:02:06 +00003272 return true;
3273
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00003274 // C11 _Atomic()
Eli Friedmanb001de72011-10-06 23:00:33 +00003275 case tok::kw__Atomic:
3276 return true;
3277
Chris Lattnerf3948c42008-07-26 03:38:44 +00003278 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
3279 case tok::less:
David Blaikie4e4d0842012-03-11 07:00:24 +00003280 return getLangOpts().ObjC1;
Mike Stump1eb44332009-09-09 15:08:12 +00003281
Douglas Gregord9d75e52011-04-27 05:41:15 +00003282 // typedef-name
3283 case tok::annot_typename:
3284 return !DisambiguatingWithExpression ||
3285 !isStartOfObjCClassMessageMissingOpenBracket();
3286
Steve Naroff47f52092009-01-06 19:34:12 +00003287 case tok::kw___declspec:
Steve Naroff239f0732008-12-25 14:16:32 +00003288 case tok::kw___cdecl:
3289 case tok::kw___stdcall:
3290 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00003291 case tok::kw___thiscall:
Eli Friedman290eeb02009-06-08 23:27:34 +00003292 case tok::kw___w64:
3293 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00003294 case tok::kw___ptr32:
Eli Friedman290eeb02009-06-08 23:27:34 +00003295 case tok::kw___forceinline:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003296 case tok::kw___pascal:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00003297 case tok::kw___unaligned:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003298
3299 case tok::kw___private:
3300 case tok::kw___local:
3301 case tok::kw___global:
3302 case tok::kw___constant:
3303 case tok::kw___read_only:
3304 case tok::kw___read_write:
3305 case tok::kw___write_only:
3306
Eli Friedman290eeb02009-06-08 23:27:34 +00003307 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00003308 }
3309}
3310
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003311bool Parser::isConstructorDeclarator() {
3312 TentativeParsingAction TPA(*this);
3313
3314 // Parse the C++ scope specifier.
3315 CXXScopeSpec SS;
Douglas Gregorefaa93a2011-11-07 17:33:42 +00003316 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
3317 /*EnteringContext=*/true)) {
John McCall9ba61662010-02-26 08:45:28 +00003318 TPA.Revert();
3319 return false;
3320 }
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003321
3322 // Parse the constructor name.
3323 if (Tok.is(tok::identifier) || Tok.is(tok::annot_template_id)) {
3324 // We already know that we have a constructor name; just consume
3325 // the token.
3326 ConsumeToken();
3327 } else {
3328 TPA.Revert();
3329 return false;
3330 }
3331
3332 // Current class name must be followed by a left parentheses.
3333 if (Tok.isNot(tok::l_paren)) {
3334 TPA.Revert();
3335 return false;
3336 }
3337 ConsumeParen();
3338
3339 // A right parentheses or ellipsis signals that we have a constructor.
3340 if (Tok.is(tok::r_paren) || Tok.is(tok::ellipsis)) {
3341 TPA.Revert();
3342 return true;
3343 }
3344
3345 // If we need to, enter the specified scope.
3346 DeclaratorScopeObj DeclScopeObj(*this, SS);
Douglas Gregor23c94db2010-07-02 17:43:08 +00003347 if (SS.isSet() && Actions.ShouldEnterDeclaratorScope(getCurScope(), SS))
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003348 DeclScopeObj.EnterDeclaratorScope();
3349
Francois Pichetdfaa5fb2011-01-31 04:54:32 +00003350 // Optionally skip Microsoft attributes.
John McCall0b7e6782011-03-24 11:26:52 +00003351 ParsedAttributes Attrs(AttrFactory);
Francois Pichetdfaa5fb2011-01-31 04:54:32 +00003352 MaybeParseMicrosoftAttributes(Attrs);
3353
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003354 // Check whether the next token(s) are part of a declaration
3355 // specifier, in which case we have the start of a parameter and,
3356 // therefore, we know that this is a constructor.
3357 bool IsConstructor = isDeclarationSpecifier();
3358 TPA.Revert();
3359 return IsConstructor;
3360}
Reid Spencer5f016e22007-07-11 17:01:13 +00003361
3362/// ParseTypeQualifierListOpt
Dawn Perchik52fc3142010-09-03 01:29:35 +00003363/// type-qualifier-list: [C99 6.7.5]
3364/// type-qualifier
3365/// [vendor] attributes
3366/// [ only if VendorAttributesAllowed=true ]
3367/// type-qualifier-list type-qualifier
3368/// [vendor] type-qualifier-list attributes
3369/// [ only if VendorAttributesAllowed=true ]
3370/// [C++0x] attribute-specifier[opt] is allowed before cv-qualifier-seq
3371/// [ only if CXX0XAttributesAllowed=true ]
3372/// Note: vendor can be GNU, MS, etc.
Reid Spencer5f016e22007-07-11 17:01:13 +00003373///
Dawn Perchik52fc3142010-09-03 01:29:35 +00003374void Parser::ParseTypeQualifierListOpt(DeclSpec &DS,
3375 bool VendorAttributesAllowed,
Sean Huntbbd37c62009-11-21 08:43:09 +00003376 bool CXX0XAttributesAllowed) {
David Blaikie4e4d0842012-03-11 07:00:24 +00003377 if (getLangOpts().CPlusPlus0x && isCXX0XAttributeSpecifier()) {
Sean Huntbbd37c62009-11-21 08:43:09 +00003378 SourceLocation Loc = Tok.getLocation();
John McCall0b7e6782011-03-24 11:26:52 +00003379 ParsedAttributesWithRange attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00003380 ParseCXX0XAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00003381 if (CXX0XAttributesAllowed)
John McCall7f040a92010-12-24 02:08:15 +00003382 DS.takeAttributesFrom(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00003383 else
3384 Diag(Loc, diag::err_attributes_not_allowed);
3385 }
Abramo Bagnara796aa442011-03-12 11:17:06 +00003386
3387 SourceLocation EndLoc;
3388
Reid Spencer5f016e22007-07-11 17:01:13 +00003389 while (1) {
John McCallfec54012009-08-03 20:12:06 +00003390 bool isInvalid = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00003391 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00003392 unsigned DiagID = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00003393 SourceLocation Loc = Tok.getLocation();
3394
3395 switch (Tok.getKind()) {
Douglas Gregor1a480c42010-08-27 17:35:51 +00003396 case tok::code_completion:
3397 Actions.CodeCompleteTypeQualifiers(DS);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00003398 return cutOffParsing();
Douglas Gregor1a480c42010-08-27 17:35:51 +00003399
Reid Spencer5f016e22007-07-11 17:01:13 +00003400 case tok::kw_const:
John McCallfec54012009-08-03 20:12:06 +00003401 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, DiagID,
David Blaikie4e4d0842012-03-11 07:00:24 +00003402 getLangOpts());
Reid Spencer5f016e22007-07-11 17:01:13 +00003403 break;
3404 case tok::kw_volatile:
John McCallfec54012009-08-03 20:12:06 +00003405 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
David Blaikie4e4d0842012-03-11 07:00:24 +00003406 getLangOpts());
Reid Spencer5f016e22007-07-11 17:01:13 +00003407 break;
3408 case tok::kw_restrict:
John McCallfec54012009-08-03 20:12:06 +00003409 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
David Blaikie4e4d0842012-03-11 07:00:24 +00003410 getLangOpts());
Reid Spencer5f016e22007-07-11 17:01:13 +00003411 break;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003412
3413 // OpenCL qualifiers:
3414 case tok::kw_private:
David Blaikie4e4d0842012-03-11 07:00:24 +00003415 if (!getLangOpts().OpenCL)
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003416 goto DoneWithTypeQuals;
3417 case tok::kw___private:
3418 case tok::kw___global:
3419 case tok::kw___local:
3420 case tok::kw___constant:
3421 case tok::kw___read_only:
3422 case tok::kw___write_only:
3423 case tok::kw___read_write:
3424 ParseOpenCLQualifiers(DS);
3425 break;
3426
Eli Friedman290eeb02009-06-08 23:27:34 +00003427 case tok::kw___w64:
Steve Naroff86bc6cf2008-12-25 14:41:26 +00003428 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00003429 case tok::kw___ptr32:
Steve Naroff239f0732008-12-25 14:16:32 +00003430 case tok::kw___cdecl:
3431 case tok::kw___stdcall:
3432 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00003433 case tok::kw___thiscall:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00003434 case tok::kw___unaligned:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003435 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00003436 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman290eeb02009-06-08 23:27:34 +00003437 continue;
3438 }
3439 goto DoneWithTypeQuals;
Dawn Perchik52fc3142010-09-03 01:29:35 +00003440 case tok::kw___pascal:
3441 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00003442 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik52fc3142010-09-03 01:29:35 +00003443 continue;
3444 }
3445 goto DoneWithTypeQuals;
Reid Spencer5f016e22007-07-11 17:01:13 +00003446 case tok::kw___attribute:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003447 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00003448 ParseGNUAttributes(DS.getAttributes());
Chris Lattner5a69d1c2008-12-18 07:02:59 +00003449 continue; // do *not* consume the next token!
3450 }
3451 // otherwise, FALL THROUGH!
3452 default:
Steve Naroff239f0732008-12-25 14:16:32 +00003453 DoneWithTypeQuals:
Chris Lattner5a69d1c2008-12-18 07:02:59 +00003454 // If this is not a type-qualifier token, we're done reading type
3455 // qualifiers. First verify that DeclSpec's are consistent.
Douglas Gregor9b3064b2009-04-01 22:41:11 +00003456 DS.Finish(Diags, PP);
Abramo Bagnara796aa442011-03-12 11:17:06 +00003457 if (EndLoc.isValid())
3458 DS.SetRangeEnd(EndLoc);
Chris Lattner5a69d1c2008-12-18 07:02:59 +00003459 return;
Reid Spencer5f016e22007-07-11 17:01:13 +00003460 }
Chris Lattnera1fcbad2008-12-18 06:50:14 +00003461
Reid Spencer5f016e22007-07-11 17:01:13 +00003462 // If the specifier combination wasn't legal, issue a diagnostic.
3463 if (isInvalid) {
3464 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner1ab3b962008-11-18 07:48:38 +00003465 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00003466 }
Abramo Bagnara796aa442011-03-12 11:17:06 +00003467 EndLoc = ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00003468 }
3469}
3470
3471
3472/// ParseDeclarator - Parse and verify a newly-initialized declarator.
3473///
3474void Parser::ParseDeclarator(Declarator &D) {
3475 /// This implements the 'declarator' production in the C grammar, then checks
3476 /// for well-formedness and issues diagnostics.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003477 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Reid Spencer5f016e22007-07-11 17:01:13 +00003478}
3479
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003480/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
3481/// is parsed by the function passed to it. Pass null, and the direct-declarator
3482/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003483/// ptr-operator production.
3484///
Richard Smith0706df42011-10-19 21:33:05 +00003485/// If the grammar of this construct is extended, matching changes must also be
3486/// made to TryParseDeclarator and MightBeDeclarator.
3487///
Sebastian Redlf30208a2009-01-24 21:16:55 +00003488/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
3489/// [C] pointer[opt] direct-declarator
3490/// [C++] direct-declarator
3491/// [C++] ptr-operator declarator
Reid Spencer5f016e22007-07-11 17:01:13 +00003492///
3493/// pointer: [C99 6.7.5]
3494/// '*' type-qualifier-list[opt]
3495/// '*' type-qualifier-list[opt] pointer
3496///
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003497/// ptr-operator:
3498/// '*' cv-qualifier-seq[opt]
3499/// '&'
Sebastian Redl05532f22009-03-15 22:02:01 +00003500/// [C++0x] '&&'
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003501/// [GNU] '&' restrict[opt] attributes[opt]
Sebastian Redl05532f22009-03-15 22:02:01 +00003502/// [GNU?] '&&' restrict[opt] attributes[opt]
Sebastian Redlf30208a2009-01-24 21:16:55 +00003503/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003504void Parser::ParseDeclaratorInternal(Declarator &D,
3505 DirectDeclParseFunction DirectDeclParser) {
Douglas Gregor91a28862009-08-26 14:27:30 +00003506 if (Diags.hasAllExtensionsSilenced())
3507 D.setExtension();
Douglas Gregor2ccccb32010-08-23 18:23:48 +00003508
Sebastian Redlf30208a2009-01-24 21:16:55 +00003509 // C++ member pointers start with a '::' or a nested-name.
3510 // Member pointers get special handling, since there's no place for the
3511 // scope spec in the generic path below.
David Blaikie4e4d0842012-03-11 07:00:24 +00003512 if (getLangOpts().CPlusPlus &&
Chris Lattnerf919bfe2009-03-24 17:04:48 +00003513 (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) ||
3514 Tok.is(tok::annot_cxxscope))) {
Douglas Gregorefaa93a2011-11-07 17:33:42 +00003515 bool EnteringContext = D.getContext() == Declarator::FileContext ||
3516 D.getContext() == Declarator::MemberContext;
Sebastian Redlf30208a2009-01-24 21:16:55 +00003517 CXXScopeSpec SS;
Douglas Gregorefaa93a2011-11-07 17:33:42 +00003518 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), EnteringContext);
John McCall9ba61662010-02-26 08:45:28 +00003519
Jeffrey Yasskinedc28772010-04-07 23:29:58 +00003520 if (SS.isNotEmpty()) {
Mike Stump1eb44332009-09-09 15:08:12 +00003521 if (Tok.isNot(tok::star)) {
Sebastian Redlf30208a2009-01-24 21:16:55 +00003522 // The scope spec really belongs to the direct-declarator.
3523 D.getCXXScopeSpec() = SS;
3524 if (DirectDeclParser)
3525 (this->*DirectDeclParser)(D);
3526 return;
3527 }
3528
3529 SourceLocation Loc = ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00003530 D.SetRangeEnd(Loc);
John McCall0b7e6782011-03-24 11:26:52 +00003531 DeclSpec DS(AttrFactory);
Sebastian Redlf30208a2009-01-24 21:16:55 +00003532 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00003533 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00003534
3535 // Recurse to parse whatever is left.
3536 ParseDeclaratorInternal(D, DirectDeclParser);
3537
3538 // Sema will have to catch (syntactically invalid) pointers into global
3539 // scope. It has to catch pointers into namespace scope anyway.
3540 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
John McCall0b7e6782011-03-24 11:26:52 +00003541 Loc),
3542 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003543 /* Don't replace range end. */SourceLocation());
Sebastian Redlf30208a2009-01-24 21:16:55 +00003544 return;
3545 }
3546 }
3547
3548 tok::TokenKind Kind = Tok.getKind();
Steve Naroff5618bd42008-08-27 16:04:49 +00003549 // Not a pointer, C++ reference, or block.
Chris Lattner9af55002009-03-27 04:18:06 +00003550 if (Kind != tok::star && Kind != tok::caret &&
David Blaikie4e4d0842012-03-11 07:00:24 +00003551 (Kind != tok::amp || !getLangOpts().CPlusPlus) &&
Sebastian Redl743de1f2009-03-23 00:00:23 +00003552 // We parse rvalue refs in C++03, because otherwise the errors are scary.
David Blaikie4e4d0842012-03-11 07:00:24 +00003553 (Kind != tok::ampamp || !getLangOpts().CPlusPlus)) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003554 if (DirectDeclParser)
3555 (this->*DirectDeclParser)(D);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003556 return;
3557 }
Sebastian Redlf30208a2009-01-24 21:16:55 +00003558
Sebastian Redl05532f22009-03-15 22:02:01 +00003559 // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference,
3560 // '&&' -> rvalue reference
Sebastian Redl743de1f2009-03-23 00:00:23 +00003561 SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&.
Sebastian Redlab197ba2009-02-09 18:23:29 +00003562 D.SetRangeEnd(Loc);
Reid Spencer5f016e22007-07-11 17:01:13 +00003563
Chris Lattner9af55002009-03-27 04:18:06 +00003564 if (Kind == tok::star || Kind == tok::caret) {
Chris Lattner76549142008-02-21 01:32:26 +00003565 // Is a pointer.
John McCall0b7e6782011-03-24 11:26:52 +00003566 DeclSpec DS(AttrFactory);
Sebastian Redlf30208a2009-01-24 21:16:55 +00003567
Reid Spencer5f016e22007-07-11 17:01:13 +00003568 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00003569 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00003570
Reid Spencer5f016e22007-07-11 17:01:13 +00003571 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003572 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroff5618bd42008-08-27 16:04:49 +00003573 if (Kind == tok::star)
3574 // Remember that we parsed a pointer type, and remember the type-quals.
3575 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
Chandler Carruthd067c072011-02-23 18:51:59 +00003576 DS.getConstSpecLoc(),
3577 DS.getVolatileSpecLoc(),
John McCall0b7e6782011-03-24 11:26:52 +00003578 DS.getRestrictSpecLoc()),
3579 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003580 SourceLocation());
Steve Naroff5618bd42008-08-27 16:04:49 +00003581 else
3582 // Remember that we parsed a Block type, and remember the type-quals.
Mike Stump1eb44332009-09-09 15:08:12 +00003583 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
John McCall0b7e6782011-03-24 11:26:52 +00003584 Loc),
3585 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003586 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00003587 } else {
3588 // Is a reference
John McCall0b7e6782011-03-24 11:26:52 +00003589 DeclSpec DS(AttrFactory);
Reid Spencer5f016e22007-07-11 17:01:13 +00003590
Sebastian Redl743de1f2009-03-23 00:00:23 +00003591 // Complain about rvalue references in C++03, but then go on and build
3592 // the declarator.
Richard Smith7fe62082011-10-15 05:09:34 +00003593 if (Kind == tok::ampamp)
David Blaikie4e4d0842012-03-11 07:00:24 +00003594 Diag(Loc, getLangOpts().CPlusPlus0x ?
Richard Smith7fe62082011-10-15 05:09:34 +00003595 diag::warn_cxx98_compat_rvalue_reference :
3596 diag::ext_rvalue_reference);
Sebastian Redl743de1f2009-03-23 00:00:23 +00003597
Reid Spencer5f016e22007-07-11 17:01:13 +00003598 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
3599 // cv-qualifiers are introduced through the use of a typedef or of a
3600 // template type argument, in which case the cv-qualifiers are ignored.
3601 //
3602 // [GNU] Retricted references are allowed.
3603 // [GNU] Attributes on references are allowed.
Sean Huntbbd37c62009-11-21 08:43:09 +00003604 // [C++0x] Attributes on references are not allowed.
3605 ParseTypeQualifierListOpt(DS, true, false);
Sebastian Redlab197ba2009-02-09 18:23:29 +00003606 D.ExtendWithDeclSpec(DS);
Reid Spencer5f016e22007-07-11 17:01:13 +00003607
3608 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
3609 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
3610 Diag(DS.getConstSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00003611 diag::err_invalid_reference_qualifier_application) << "const";
Reid Spencer5f016e22007-07-11 17:01:13 +00003612 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
3613 Diag(DS.getVolatileSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00003614 diag::err_invalid_reference_qualifier_application) << "volatile";
Reid Spencer5f016e22007-07-11 17:01:13 +00003615 }
3616
3617 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003618 ParseDeclaratorInternal(D, DirectDeclParser);
Reid Spencer5f016e22007-07-11 17:01:13 +00003619
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00003620 if (D.getNumTypeObjects() > 0) {
3621 // C++ [dcl.ref]p4: There shall be no references to references.
3622 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
3623 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattnerda83bac2008-11-19 07:37:42 +00003624 if (const IdentifierInfo *II = D.getIdentifier())
3625 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
3626 << II;
3627 else
3628 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
3629 << "type name";
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00003630
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003631 // Once we've complained about the reference-to-reference, we
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00003632 // can go ahead and build the (technically ill-formed)
3633 // declarator: reference collapsing will take care of it.
3634 }
3635 }
3636
Reid Spencer5f016e22007-07-11 17:01:13 +00003637 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner76549142008-02-21 01:32:26 +00003638 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
Sebastian Redl05532f22009-03-15 22:02:01 +00003639 Kind == tok::amp),
John McCall0b7e6782011-03-24 11:26:52 +00003640 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003641 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00003642 }
3643}
3644
3645/// ParseDirectDeclarator
3646/// direct-declarator: [C99 6.7.5]
Douglas Gregor42a552f2008-11-05 20:51:48 +00003647/// [C99] identifier
Reid Spencer5f016e22007-07-11 17:01:13 +00003648/// '(' declarator ')'
3649/// [GNU] '(' attributes declarator ')'
3650/// [C90] direct-declarator '[' constant-expression[opt] ']'
3651/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
3652/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
3653/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
3654/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
3655/// direct-declarator '(' parameter-type-list ')'
3656/// direct-declarator '(' identifier-list[opt] ')'
3657/// [GNU] direct-declarator '(' parameter-forward-declarations
3658/// parameter-type-list[opt] ')'
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003659/// [C++] direct-declarator '(' parameter-declaration-clause ')'
3660/// cv-qualifier-seq[opt] exception-specification[opt]
Douglas Gregorb48fe382008-10-31 09:07:45 +00003661/// [C++] declarator-id
Douglas Gregor42a552f2008-11-05 20:51:48 +00003662///
3663/// declarator-id: [C++ 8]
Douglas Gregora8bc8c92010-12-23 22:44:42 +00003664/// '...'[opt] id-expression
Douglas Gregor42a552f2008-11-05 20:51:48 +00003665/// '::'[opt] nested-name-specifier[opt] type-name
3666///
3667/// id-expression: [C++ 5.1]
3668/// unqualified-id
Douglas Gregordb422df2009-09-25 21:45:23 +00003669/// qualified-id
Douglas Gregor42a552f2008-11-05 20:51:48 +00003670///
3671/// unqualified-id: [C++ 5.1]
Mike Stump1eb44332009-09-09 15:08:12 +00003672/// identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003673/// operator-function-id
Douglas Gregordb422df2009-09-25 21:45:23 +00003674/// conversion-function-id
Mike Stump1eb44332009-09-09 15:08:12 +00003675/// '~' class-name
Douglas Gregor39a8de12009-02-25 19:37:18 +00003676/// template-id
Argyrios Kyrtzidisc7ed9c62008-11-07 22:02:30 +00003677///
Reid Spencer5f016e22007-07-11 17:01:13 +00003678void Parser::ParseDirectDeclarator(Declarator &D) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00003679 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003680
David Blaikie4e4d0842012-03-11 07:00:24 +00003681 if (getLangOpts().CPlusPlus && D.mayHaveIdentifier()) {
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003682 // ParseDeclaratorInternal might already have parsed the scope.
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00003683 if (D.getCXXScopeSpec().isEmpty()) {
Douglas Gregorefaa93a2011-11-07 17:33:42 +00003684 bool EnteringContext = D.getContext() == Declarator::FileContext ||
3685 D.getContext() == Declarator::MemberContext;
3686 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec(), ParsedType(),
3687 EnteringContext);
John McCall9ba61662010-02-26 08:45:28 +00003688 }
3689
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00003690 if (D.getCXXScopeSpec().isValid()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00003691 if (Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
John McCalle7e278b2009-12-11 20:04:54 +00003692 // Change the declaration context for name lookup, until this function
3693 // is exited (and the declarator has been parsed).
3694 DeclScopeObj.EnterDeclaratorScope();
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00003695 }
3696
Douglas Gregora8bc8c92010-12-23 22:44:42 +00003697 // C++0x [dcl.fct]p14:
3698 // There is a syntactic ambiguity when an ellipsis occurs at the end
3699 // of a parameter-declaration-clause without a preceding comma. In
3700 // this case, the ellipsis is parsed as part of the
3701 // abstract-declarator if the type of the parameter names a template
3702 // parameter pack that has not been expanded; otherwise, it is parsed
3703 // as part of the parameter-declaration-clause.
3704 if (Tok.is(tok::ellipsis) &&
3705 !((D.getContext() == Declarator::PrototypeContext ||
3706 D.getContext() == Declarator::BlockLiteralContext) &&
Douglas Gregora8bc8c92010-12-23 22:44:42 +00003707 NextToken().is(tok::r_paren) &&
3708 !Actions.containsUnexpandedParameterPacks(D)))
3709 D.setEllipsisLoc(ConsumeToken());
3710
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003711 if (Tok.is(tok::identifier) || Tok.is(tok::kw_operator) ||
3712 Tok.is(tok::annot_template_id) || Tok.is(tok::tilde)) {
3713 // We found something that indicates the start of an unqualified-id.
3714 // Parse that unqualified-id.
John McCallba9d8532010-04-13 06:39:49 +00003715 bool AllowConstructorName;
3716 if (D.getDeclSpec().hasTypeSpecifier())
3717 AllowConstructorName = false;
3718 else if (D.getCXXScopeSpec().isSet())
3719 AllowConstructorName =
3720 (D.getContext() == Declarator::FileContext ||
3721 (D.getContext() == Declarator::MemberContext &&
3722 D.getDeclSpec().isFriendSpecified()));
3723 else
3724 AllowConstructorName = (D.getContext() == Declarator::MemberContext);
3725
Abramo Bagnarae4b92762012-01-27 09:46:47 +00003726 SourceLocation TemplateKWLoc;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003727 if (ParseUnqualifiedId(D.getCXXScopeSpec(),
3728 /*EnteringContext=*/true,
3729 /*AllowDestructorName=*/true,
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003730 AllowConstructorName,
John McCallb3d87482010-08-24 05:47:05 +00003731 ParsedType(),
Abramo Bagnarae4b92762012-01-27 09:46:47 +00003732 TemplateKWLoc,
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00003733 D.getName()) ||
3734 // Once we're past the identifier, if the scope was bad, mark the
3735 // whole declarator bad.
3736 D.getCXXScopeSpec().isInvalid()) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00003737 D.SetIdentifier(0, Tok.getLocation());
3738 D.setInvalidType(true);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003739 } else {
3740 // Parsed the unqualified-id; update range information and move along.
3741 if (D.getSourceRange().getBegin().isInvalid())
3742 D.SetRangeBegin(D.getName().getSourceRange().getBegin());
3743 D.SetRangeEnd(D.getName().getSourceRange().getEnd());
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003744 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003745 goto PastIdentifier;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00003746 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003747 } else if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
David Blaikie4e4d0842012-03-11 07:00:24 +00003748 assert(!getLangOpts().CPlusPlus &&
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00003749 "There's a C++-specific check for tok::identifier above");
3750 assert(Tok.getIdentifierInfo() && "Not an identifier?");
3751 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
3752 ConsumeToken();
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003753 goto PastIdentifier;
3754 }
3755
3756 if (Tok.is(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003757 // direct-declarator: '(' declarator ')'
3758 // direct-declarator: '(' attributes declarator ')'
3759 // Example: 'char (*X)' or 'int (*XX)(void)'
3760 ParseParenDeclarator(D);
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003761
3762 // If the declarator was parenthesized, we entered the declarator
3763 // scope when parsing the parenthesized declarator, then exited
3764 // the scope already. Re-enter the scope, if we need to.
3765 if (D.getCXXScopeSpec().isSet()) {
Fariborz Jahanian46877cd2010-08-17 23:50:37 +00003766 // If there was an error parsing parenthesized declarator, declarator
3767 // scope may have been enterred before. Don't do it again.
3768 if (!D.isInvalidType() &&
3769 Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003770 // Change the declaration context for name lookup, until this function
3771 // is exited (and the declarator has been parsed).
Fariborz Jahanian46877cd2010-08-17 23:50:37 +00003772 DeclScopeObj.EnterDeclaratorScope();
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003773 }
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00003774 } else if (D.mayOmitIdentifier()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003775 // This could be something simple like "int" (in which case the declarator
3776 // portion is empty), if an abstract-declarator is allowed.
3777 D.SetIdentifier(0, Tok.getLocation());
3778 } else {
Douglas Gregore950d4b2009-03-06 23:28:18 +00003779 if (D.getContext() == Declarator::MemberContext)
3780 Diag(Tok, diag::err_expected_member_name_or_semi)
3781 << D.getDeclSpec().getSourceRange();
David Blaikie4e4d0842012-03-11 07:00:24 +00003782 else if (getLangOpts().CPlusPlus)
3783 Diag(Tok, diag::err_expected_unqualified_id) << getLangOpts().CPlusPlus;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003784 else
Chris Lattner1ab3b962008-11-18 07:48:38 +00003785 Diag(Tok, diag::err_expected_ident_lparen);
Reid Spencer5f016e22007-07-11 17:01:13 +00003786 D.SetIdentifier(0, Tok.getLocation());
Chris Lattner1f6f54b2008-11-11 06:13:16 +00003787 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00003788 }
Mike Stump1eb44332009-09-09 15:08:12 +00003789
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00003790 PastIdentifier:
Reid Spencer5f016e22007-07-11 17:01:13 +00003791 assert(D.isPastIdentifier() &&
3792 "Haven't past the location of the identifier yet?");
Mike Stump1eb44332009-09-09 15:08:12 +00003793
Sean Huntbbd37c62009-11-21 08:43:09 +00003794 // Don't parse attributes unless we have an identifier.
John McCall7f040a92010-12-24 02:08:15 +00003795 if (D.getIdentifier())
3796 MaybeParseCXX0XAttributes(D);
Sean Huntbbd37c62009-11-21 08:43:09 +00003797
Reid Spencer5f016e22007-07-11 17:01:13 +00003798 while (1) {
Chris Lattner04d66662007-10-09 17:33:22 +00003799 if (Tok.is(tok::l_paren)) {
David Blaikie42d6d0c2011-12-04 05:04:18 +00003800 // Enter function-declaration scope, limiting any declarators to the
3801 // function prototype scope, including parameter declarators.
3802 ParseScope PrototypeScope(this,
3803 Scope::FunctionPrototypeScope|Scope::DeclScope);
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00003804 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
3805 // In such a case, check if we actually have a function declarator; if it
3806 // is not, the declarator has been fully parsed.
David Blaikie4e4d0842012-03-11 07:00:24 +00003807 if (getLangOpts().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
Chris Lattner7399ee02008-10-20 02:05:46 +00003808 // When not in file scope, warn for ambiguous function declarators, just
3809 // in case the author intended it as a variable definition.
3810 bool warnIfAmbiguous = D.getContext() != Declarator::FileContext;
3811 if (!isCXXFunctionDeclarator(warnIfAmbiguous))
3812 break;
3813 }
John McCall0b7e6782011-03-24 11:26:52 +00003814 ParsedAttributes attrs(AttrFactory);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003815 BalancedDelimiterTracker T(*this, tok::l_paren);
3816 T.consumeOpen();
3817 ParseFunctionDeclarator(D, attrs, T);
David Blaikie42d6d0c2011-12-04 05:04:18 +00003818 PrototypeScope.Exit();
Chris Lattner04d66662007-10-09 17:33:22 +00003819 } else if (Tok.is(tok::l_square)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003820 ParseBracketDeclarator(D);
3821 } else {
3822 break;
3823 }
3824 }
David Blaikie42d6d0c2011-12-04 05:04:18 +00003825}
Reid Spencer5f016e22007-07-11 17:01:13 +00003826
Chris Lattneref4715c2008-04-06 05:45:57 +00003827/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
3828/// only called before the identifier, so these are most likely just grouping
Mike Stump1eb44332009-09-09 15:08:12 +00003829/// parens for precedence. If we find that these are actually function
Chris Lattneref4715c2008-04-06 05:45:57 +00003830/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
3831///
3832/// direct-declarator:
3833/// '(' declarator ')'
3834/// [GNU] '(' attributes declarator ')'
Chris Lattner7399ee02008-10-20 02:05:46 +00003835/// direct-declarator '(' parameter-type-list ')'
3836/// direct-declarator '(' identifier-list[opt] ')'
3837/// [GNU] direct-declarator '(' parameter-forward-declarations
3838/// parameter-type-list[opt] ')'
Chris Lattneref4715c2008-04-06 05:45:57 +00003839///
3840void Parser::ParseParenDeclarator(Declarator &D) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003841 BalancedDelimiterTracker T(*this, tok::l_paren);
3842 T.consumeOpen();
3843
Chris Lattneref4715c2008-04-06 05:45:57 +00003844 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
Mike Stump1eb44332009-09-09 15:08:12 +00003845
Chris Lattner7399ee02008-10-20 02:05:46 +00003846 // Eat any attributes before we look at whether this is a grouping or function
3847 // declarator paren. If this is a grouping paren, the attribute applies to
3848 // the type being built up, for example:
3849 // int (__attribute__(()) *x)(long y)
3850 // If this ends up not being a grouping paren, the attribute applies to the
3851 // first argument, for example:
3852 // int (__attribute__(()) int x)
3853 // In either case, we need to eat any attributes to be able to determine what
3854 // sort of paren this is.
3855 //
John McCall0b7e6782011-03-24 11:26:52 +00003856 ParsedAttributes attrs(AttrFactory);
Chris Lattner7399ee02008-10-20 02:05:46 +00003857 bool RequiresArg = false;
3858 if (Tok.is(tok::kw___attribute)) {
John McCall7f040a92010-12-24 02:08:15 +00003859 ParseGNUAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00003860
Chris Lattner7399ee02008-10-20 02:05:46 +00003861 // We require that the argument list (if this is a non-grouping paren) be
3862 // present even if the attribute list was empty.
3863 RequiresArg = true;
3864 }
Steve Naroff239f0732008-12-25 14:16:32 +00003865 // Eat any Microsoft extensions.
Eli Friedman290eeb02009-06-08 23:27:34 +00003866 if (Tok.is(tok::kw___cdecl) || Tok.is(tok::kw___stdcall) ||
Douglas Gregorf813a2c2010-05-18 16:57:00 +00003867 Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___fastcall) ||
Francois Pichet3bd9aa42011-08-18 09:59:55 +00003868 Tok.is(tok::kw___w64) || Tok.is(tok::kw___ptr64) ||
Francois Pichet58fd97a2011-08-25 00:36:46 +00003869 Tok.is(tok::kw___ptr32) || Tok.is(tok::kw___unaligned)) {
John McCall7f040a92010-12-24 02:08:15 +00003870 ParseMicrosoftTypeAttributes(attrs);
Eli Friedman290eeb02009-06-08 23:27:34 +00003871 }
Dawn Perchik52fc3142010-09-03 01:29:35 +00003872 // Eat any Borland extensions.
Ted Kremenek8113ecf2010-11-10 05:59:39 +00003873 if (Tok.is(tok::kw___pascal))
John McCall7f040a92010-12-24 02:08:15 +00003874 ParseBorlandTypeAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00003875
Chris Lattneref4715c2008-04-06 05:45:57 +00003876 // If we haven't past the identifier yet (or where the identifier would be
3877 // stored, if this is an abstract declarator), then this is probably just
3878 // grouping parens. However, if this could be an abstract-declarator, then
3879 // this could also be the start of function arguments (consider 'void()').
3880 bool isGrouping;
Mike Stump1eb44332009-09-09 15:08:12 +00003881
Chris Lattneref4715c2008-04-06 05:45:57 +00003882 if (!D.mayOmitIdentifier()) {
3883 // If this can't be an abstract-declarator, this *must* be a grouping
3884 // paren, because we haven't seen the identifier yet.
3885 isGrouping = true;
3886 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
David Blaikie4e4d0842012-03-11 07:00:24 +00003887 (getLangOpts().CPlusPlus && Tok.is(tok::ellipsis)) || // C++ int(...)
Chris Lattneref4715c2008-04-06 05:45:57 +00003888 isDeclarationSpecifier()) { // 'int(int)' is a function.
3889 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
3890 // considered to be a type, not a K&R identifier-list.
3891 isGrouping = false;
3892 } else {
3893 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
3894 isGrouping = true;
3895 }
Mike Stump1eb44332009-09-09 15:08:12 +00003896
Chris Lattneref4715c2008-04-06 05:45:57 +00003897 // If this is a grouping paren, handle:
3898 // direct-declarator: '(' declarator ')'
3899 // direct-declarator: '(' attributes declarator ')'
3900 if (isGrouping) {
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00003901 bool hadGroupingParens = D.hasGroupingParens();
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00003902 D.setGroupingParens(true);
3903
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003904 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattneref4715c2008-04-06 05:45:57 +00003905 // Match the ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003906 T.consumeClose();
3907 D.AddTypeInfo(DeclaratorChunk::getParen(T.getOpenLocation(),
3908 T.getCloseLocation()),
3909 attrs, T.getCloseLocation());
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00003910
3911 D.setGroupingParens(hadGroupingParens);
Chris Lattneref4715c2008-04-06 05:45:57 +00003912 return;
3913 }
Mike Stump1eb44332009-09-09 15:08:12 +00003914
Chris Lattneref4715c2008-04-06 05:45:57 +00003915 // Okay, if this wasn't a grouping paren, it must be the start of a function
3916 // argument list. Recognize that this declarator will never have an
Chris Lattner7399ee02008-10-20 02:05:46 +00003917 // identifier (and remember where it would have been), then call into
3918 // ParseFunctionDeclarator to handle of argument list.
Chris Lattneref4715c2008-04-06 05:45:57 +00003919 D.SetIdentifier(0, Tok.getLocation());
3920
David Blaikie42d6d0c2011-12-04 05:04:18 +00003921 // Enter function-declaration scope, limiting any declarators to the
3922 // function prototype scope, including parameter declarators.
3923 ParseScope PrototypeScope(this,
3924 Scope::FunctionPrototypeScope|Scope::DeclScope);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003925 ParseFunctionDeclarator(D, attrs, T, RequiresArg);
David Blaikie42d6d0c2011-12-04 05:04:18 +00003926 PrototypeScope.Exit();
Chris Lattneref4715c2008-04-06 05:45:57 +00003927}
3928
3929/// ParseFunctionDeclarator - We are after the identifier and have parsed the
3930/// declarator D up to a paren, which indicates that we are parsing function
3931/// arguments.
Reid Spencer5f016e22007-07-11 17:01:13 +00003932///
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00003933/// If attrs is non-null, then the caller parsed those arguments immediately
Chris Lattner7399ee02008-10-20 02:05:46 +00003934/// after the open paren - they should be considered to be the first argument of
3935/// a parameter. If RequiresArg is true, then the first argument of the
3936/// function is required to be present and required to not be an identifier
3937/// list.
3938///
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00003939/// For C++, after the parameter-list, it also parses cv-qualifier-seq[opt],
3940/// (C++0x) ref-qualifier[opt], exception-specification[opt], and
3941/// (C++0x) trailing-return-type[opt].
3942///
3943/// [C++0x] exception-specification:
3944/// dynamic-exception-specification
3945/// noexcept-specification
3946///
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003947void Parser::ParseFunctionDeclarator(Declarator &D,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00003948 ParsedAttributes &attrs,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003949 BalancedDelimiterTracker &Tracker,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00003950 bool RequiresArg) {
David Blaikie42d6d0c2011-12-04 05:04:18 +00003951 assert(getCurScope()->isFunctionPrototypeScope() &&
3952 "Should call from a Function scope");
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00003953 // lparen is already consumed!
3954 assert(D.isPastIdentifier() && "Should not call before identifier!");
3955
3956 // This should be true when the function has typed arguments.
3957 // Otherwise, it is treated as a K&R-style function.
3958 bool HasProto = false;
3959 // Build up an array of information about the parsed arguments.
Chris Lattner5f9e2722011-07-23 10:55:15 +00003960 SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00003961 // Remember where we see an ellipsis, if any.
3962 SourceLocation EllipsisLoc;
3963
3964 DeclSpec DS(AttrFactory);
3965 bool RefQualifierIsLValueRef = true;
3966 SourceLocation RefQualifierLoc;
Douglas Gregor43f51032011-10-19 06:04:55 +00003967 SourceLocation ConstQualifierLoc;
3968 SourceLocation VolatileQualifierLoc;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00003969 ExceptionSpecificationType ESpecType = EST_None;
3970 SourceRange ESpecRange;
Chris Lattner5f9e2722011-07-23 10:55:15 +00003971 SmallVector<ParsedType, 2> DynamicExceptions;
3972 SmallVector<SourceRange, 2> DynamicExceptionRanges;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00003973 ExprResult NoexceptExpr;
3974 ParsedType TrailingReturnType;
3975
James Molloy16f1f712012-02-29 10:24:19 +00003976 Actions.ActOnStartFunctionDeclarator();
3977
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00003978 SourceLocation EndLoc;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00003979 if (isFunctionDeclaratorIdentifierList()) {
3980 if (RequiresArg)
3981 Diag(Tok, diag::err_argument_required_after_attribute);
3982
3983 ParseFunctionDeclaratorIdentifierList(D, ParamInfo);
3984
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003985 Tracker.consumeClose();
3986 EndLoc = Tracker.getCloseLocation();
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00003987 } else {
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00003988 if (Tok.isNot(tok::r_paren))
3989 ParseParameterDeclarationClause(D, attrs, ParamInfo, EllipsisLoc);
3990 else if (RequiresArg)
3991 Diag(Tok, diag::err_argument_required_after_attribute);
3992
David Blaikie4e4d0842012-03-11 07:00:24 +00003993 HasProto = ParamInfo.size() || getLangOpts().CPlusPlus;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00003994
3995 // If we have the closing ')', eat it.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003996 Tracker.consumeClose();
3997 EndLoc = Tracker.getCloseLocation();
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00003998
David Blaikie4e4d0842012-03-11 07:00:24 +00003999 if (getLangOpts().CPlusPlus) {
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004000 MaybeParseCXX0XAttributes(attrs);
4001
4002 // Parse cv-qualifier-seq[opt].
4003 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Douglas Gregor43f51032011-10-19 06:04:55 +00004004 if (!DS.getSourceRange().getEnd().isInvalid()) {
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004005 EndLoc = DS.getSourceRange().getEnd();
Douglas Gregor43f51032011-10-19 06:04:55 +00004006 ConstQualifierLoc = DS.getConstSpecLoc();
4007 VolatileQualifierLoc = DS.getVolatileSpecLoc();
4008 }
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004009
4010 // Parse ref-qualifier[opt].
4011 if (Tok.is(tok::amp) || Tok.is(tok::ampamp)) {
David Blaikie4e4d0842012-03-11 07:00:24 +00004012 Diag(Tok, getLangOpts().CPlusPlus0x ?
Richard Smith7fe62082011-10-15 05:09:34 +00004013 diag::warn_cxx98_compat_ref_qualifier :
4014 diag::ext_ref_qualifier);
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004015
4016 RefQualifierIsLValueRef = Tok.is(tok::amp);
4017 RefQualifierLoc = ConsumeToken();
4018 EndLoc = RefQualifierLoc;
4019 }
4020
4021 // Parse exception-specification[opt].
4022 ESpecType = MaybeParseExceptionSpecification(ESpecRange,
4023 DynamicExceptions,
4024 DynamicExceptionRanges,
4025 NoexceptExpr);
4026 if (ESpecType != EST_None)
4027 EndLoc = ESpecRange.getEnd();
4028
4029 // Parse trailing-return-type[opt].
David Blaikie4e4d0842012-03-11 07:00:24 +00004030 if (getLangOpts().CPlusPlus0x && Tok.is(tok::arrow)) {
Richard Smith7fe62082011-10-15 05:09:34 +00004031 Diag(Tok, diag::warn_cxx98_compat_trailing_return_type);
Douglas Gregorae7902c2011-08-04 15:30:47 +00004032 SourceRange Range;
4033 TrailingReturnType = ParseTrailingReturnType(Range).get();
4034 if (Range.getEnd().isValid())
4035 EndLoc = Range.getEnd();
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004036 }
4037 }
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004038 }
4039
4040 // Remember that we parsed a function type, and remember the attributes.
4041 D.AddTypeInfo(DeclaratorChunk::getFunction(HasProto,
4042 /*isVariadic=*/EllipsisLoc.isValid(),
4043 EllipsisLoc,
4044 ParamInfo.data(), ParamInfo.size(),
4045 DS.getTypeQualifiers(),
4046 RefQualifierIsLValueRef,
Douglas Gregor43f51032011-10-19 06:04:55 +00004047 RefQualifierLoc, ConstQualifierLoc,
4048 VolatileQualifierLoc,
Douglas Gregor90ebed02011-07-13 21:47:47 +00004049 /*MutableLoc=*/SourceLocation(),
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004050 ESpecType, ESpecRange.getBegin(),
4051 DynamicExceptions.data(),
4052 DynamicExceptionRanges.data(),
4053 DynamicExceptions.size(),
4054 NoexceptExpr.isUsable() ?
4055 NoexceptExpr.get() : 0,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004056 Tracker.getOpenLocation(),
4057 EndLoc, D,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004058 TrailingReturnType),
4059 attrs, EndLoc);
James Molloy16f1f712012-02-29 10:24:19 +00004060
4061 Actions.ActOnEndFunctionDeclarator();
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004062}
4063
4064/// isFunctionDeclaratorIdentifierList - This parameter list may have an
4065/// identifier list form for a K&R-style function: void foo(a,b,c)
4066///
4067/// Note that identifier-lists are only allowed for normal declarators, not for
4068/// abstract-declarators.
4069bool Parser::isFunctionDeclaratorIdentifierList() {
David Blaikie4e4d0842012-03-11 07:00:24 +00004070 return !getLangOpts().CPlusPlus
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004071 && Tok.is(tok::identifier)
4072 && !TryAltiVecVectorToken()
4073 // K&R identifier lists can't have typedefs as identifiers, per C99
4074 // 6.7.5.3p11.
4075 && (TryAnnotateTypeOrScopeToken() || !Tok.is(tok::annot_typename))
4076 // Identifier lists follow a really simple grammar: the identifiers can
4077 // be followed *only* by a ", identifier" or ")". However, K&R
4078 // identifier lists are really rare in the brave new modern world, and
4079 // it is very common for someone to typo a type in a non-K&R style
4080 // list. If we are presented with something like: "void foo(intptr x,
4081 // float y)", we don't want to start parsing the function declarator as
4082 // though it is a K&R style declarator just because intptr is an
4083 // invalid type.
4084 //
4085 // To handle this, we check to see if the token after the first
4086 // identifier is a "," or ")". Only then do we parse it as an
4087 // identifier list.
4088 && (NextToken().is(tok::comma) || NextToken().is(tok::r_paren));
4089}
4090
4091/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
4092/// we found a K&R-style identifier list instead of a typed parameter list.
4093///
4094/// After returning, ParamInfo will hold the parsed parameters.
4095///
4096/// identifier-list: [C99 6.7.5]
4097/// identifier
4098/// identifier-list ',' identifier
4099///
4100void Parser::ParseFunctionDeclaratorIdentifierList(
4101 Declarator &D,
Chris Lattner5f9e2722011-07-23 10:55:15 +00004102 SmallVector<DeclaratorChunk::ParamInfo, 16> &ParamInfo) {
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004103 // If there was no identifier specified for the declarator, either we are in
4104 // an abstract-declarator, or we are in a parameter declarator which was found
4105 // to be abstract. In abstract-declarators, identifier lists are not valid:
4106 // diagnose this.
4107 if (!D.getIdentifier())
4108 Diag(Tok, diag::ext_ident_list_in_param);
4109
4110 // Maintain an efficient lookup of params we have seen so far.
4111 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
4112
4113 while (1) {
4114 // If this isn't an identifier, report the error and skip until ')'.
4115 if (Tok.isNot(tok::identifier)) {
4116 Diag(Tok, diag::err_expected_ident);
4117 SkipUntil(tok::r_paren, /*StopAtSemi=*/true, /*DontConsume=*/true);
4118 // Forget we parsed anything.
4119 ParamInfo.clear();
4120 return;
4121 }
4122
4123 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
4124
4125 // Reject 'typedef int y; int test(x, y)', but continue parsing.
4126 if (Actions.getTypeName(*ParmII, Tok.getLocation(), getCurScope()))
4127 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
4128
4129 // Verify that the argument identifier has not already been mentioned.
4130 if (!ParamsSoFar.insert(ParmII)) {
4131 Diag(Tok, diag::err_param_redefinition) << ParmII;
4132 } else {
4133 // Remember this identifier in ParamInfo.
4134 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
4135 Tok.getLocation(),
4136 0));
4137 }
4138
4139 // Eat the identifier.
4140 ConsumeToken();
4141
4142 // The list continues if we see a comma.
4143 if (Tok.isNot(tok::comma))
4144 break;
4145 ConsumeToken();
4146 }
4147}
4148
4149/// ParseParameterDeclarationClause - Parse a (possibly empty) parameter-list
4150/// after the opening parenthesis. This function will not parse a K&R-style
4151/// identifier list.
4152///
4153/// D is the declarator being parsed. If attrs is non-null, then the caller
4154/// parsed those arguments immediately after the open paren - they should be
4155/// considered to be the first argument of a parameter.
4156///
4157/// After returning, ParamInfo will hold the parsed parameters. EllipsisLoc will
4158/// be the location of the ellipsis, if any was parsed.
4159///
Reid Spencer5f016e22007-07-11 17:01:13 +00004160/// parameter-type-list: [C99 6.7.5]
4161/// parameter-list
4162/// parameter-list ',' '...'
Douglas Gregored5d6512009-09-22 21:41:40 +00004163/// [C++] parameter-list '...'
Reid Spencer5f016e22007-07-11 17:01:13 +00004164///
4165/// parameter-list: [C99 6.7.5]
4166/// parameter-declaration
4167/// parameter-list ',' parameter-declaration
4168///
4169/// parameter-declaration: [C99 6.7.5]
4170/// declaration-specifiers declarator
Chris Lattner04421082008-04-08 04:40:51 +00004171/// [C++] declaration-specifiers declarator '=' assignment-expression
Sebastian Redl84407ba2012-03-14 15:54:00 +00004172/// [C++11] initializer-clause
Reid Spencer5f016e22007-07-11 17:01:13 +00004173/// [GNU] declaration-specifiers declarator attributes
Sebastian Redl50de12f2009-03-24 22:27:57 +00004174/// declaration-specifiers abstract-declarator[opt]
4175/// [C++] declaration-specifiers abstract-declarator[opt]
Chris Lattner8123a952008-04-10 02:22:51 +00004176/// '=' assignment-expression
Reid Spencer5f016e22007-07-11 17:01:13 +00004177/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
4178///
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004179void Parser::ParseParameterDeclarationClause(
4180 Declarator &D,
4181 ParsedAttributes &attrs,
Chris Lattner5f9e2722011-07-23 10:55:15 +00004182 SmallVector<DeclaratorChunk::ParamInfo, 16> &ParamInfo,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004183 SourceLocation &EllipsisLoc) {
Mike Stump1eb44332009-09-09 15:08:12 +00004184
Chris Lattnerf97409f2008-04-06 06:57:35 +00004185 while (1) {
4186 if (Tok.is(tok::ellipsis)) {
Douglas Gregor965acbb2009-02-18 07:07:28 +00004187 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chris Lattnerf97409f2008-04-06 06:57:35 +00004188 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00004189 }
Mike Stump1eb44332009-09-09 15:08:12 +00004190
Chris Lattnerf97409f2008-04-06 06:57:35 +00004191 // Parse the declaration-specifiers.
John McCall54abf7d2009-11-04 02:18:39 +00004192 // Just use the ParsingDeclaration "scope" of the declarator.
John McCall0b7e6782011-03-24 11:26:52 +00004193 DeclSpec DS(AttrFactory);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004194
John McCall7f040a92010-12-24 02:08:15 +00004195 // Skip any Microsoft attributes before a param.
David Blaikie4e4d0842012-03-11 07:00:24 +00004196 if (getLangOpts().MicrosoftExt && Tok.is(tok::l_square))
John McCall7f040a92010-12-24 02:08:15 +00004197 ParseMicrosoftAttributes(DS.getAttributes());
4198
4199 SourceLocation DSStart = Tok.getLocation();
Chris Lattner7399ee02008-10-20 02:05:46 +00004200
4201 // If the caller parsed attributes for the first argument, add them now.
John McCall7f040a92010-12-24 02:08:15 +00004202 // Take them so that we only apply the attributes to the first parameter.
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004203 // FIXME: If we saw an ellipsis first, this code is not reached. Are the
4204 // attributes lost? Should they even be allowed?
4205 // FIXME: If we can leave the attributes in the token stream somehow, we can
4206 // get rid of a parameter (attrs) and this statement. It might be too much
4207 // hassle.
John McCall7f040a92010-12-24 02:08:15 +00004208 DS.takeAttributesFrom(attrs);
4209
Chris Lattnere64c5492009-02-27 18:38:20 +00004210 ParseDeclarationSpecifiers(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00004211
Chris Lattnerf97409f2008-04-06 06:57:35 +00004212 // Parse the declarator. This is "PrototypeContext", because we must
4213 // accept either 'declarator' or 'abstract-declarator' here.
4214 Declarator ParmDecl(DS, Declarator::PrototypeContext);
4215 ParseDeclarator(ParmDecl);
4216
4217 // Parse GNU attributes, if present.
John McCall7f040a92010-12-24 02:08:15 +00004218 MaybeParseGNUAttributes(ParmDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00004219
Chris Lattnerf97409f2008-04-06 06:57:35 +00004220 // Remember this parsed parameter in ParamInfo.
4221 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
Mike Stump1eb44332009-09-09 15:08:12 +00004222
Douglas Gregor72b505b2008-12-16 21:30:33 +00004223 // DefArgToks is used when the parsing of default arguments needs
4224 // to be delayed.
4225 CachedTokens *DefArgToks = 0;
4226
Chris Lattnerf97409f2008-04-06 06:57:35 +00004227 // If no parameter was specified, verify that *something* was specified,
4228 // otherwise we have a missing type and identifier.
Chris Lattnere64c5492009-02-27 18:38:20 +00004229 if (DS.isEmpty() && ParmDecl.getIdentifier() == 0 &&
4230 ParmDecl.getNumTypeObjects() == 0) {
Chris Lattnerf97409f2008-04-06 06:57:35 +00004231 // Completely missing, emit error.
4232 Diag(DSStart, diag::err_missing_param);
4233 } else {
4234 // Otherwise, we have something. Add it and let semantic analysis try
4235 // to grok it and add the result to the ParamInfo we are building.
Mike Stump1eb44332009-09-09 15:08:12 +00004236
Chris Lattnerf97409f2008-04-06 06:57:35 +00004237 // Inform the actions module about the parameter declarator, so it gets
4238 // added to the current scope.
John McCalld226f652010-08-21 09:40:31 +00004239 Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl);
Chris Lattner04421082008-04-08 04:40:51 +00004240
4241 // Parse the default argument, if any. We parse the default
4242 // arguments in all dialects; the semantic analysis in
4243 // ActOnParamDefaultArgument will reject the default argument in
4244 // C.
4245 if (Tok.is(tok::equal)) {
Douglas Gregor61366e92008-12-24 00:01:03 +00004246 SourceLocation EqualLoc = Tok.getLocation();
4247
Chris Lattner04421082008-04-08 04:40:51 +00004248 // Parse the default argument
Douglas Gregor72b505b2008-12-16 21:30:33 +00004249 if (D.getContext() == Declarator::MemberContext) {
4250 // If we're inside a class definition, cache the tokens
4251 // corresponding to the default argument. We'll actually parse
4252 // them when we see the end of the class definition.
4253 // FIXME: Templates will require something similar.
4254 // FIXME: Can we use a smart pointer for Toks?
4255 DefArgToks = new CachedTokens;
4256
Mike Stump1eb44332009-09-09 15:08:12 +00004257 if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks,
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +00004258 /*StopAtSemi=*/true,
4259 /*ConsumeFinalToken=*/false)) {
Douglas Gregor72b505b2008-12-16 21:30:33 +00004260 delete DefArgToks;
4261 DefArgToks = 0;
Douglas Gregor61366e92008-12-24 00:01:03 +00004262 Actions.ActOnParamDefaultArgumentError(Param);
Argyrios Kyrtzidis2b602ad2010-08-06 09:47:24 +00004263 } else {
4264 // Mark the end of the default argument so that we know when to
4265 // stop when we parse it later on.
4266 Token DefArgEnd;
4267 DefArgEnd.startToken();
4268 DefArgEnd.setKind(tok::cxx_defaultarg_end);
4269 DefArgEnd.setLocation(Tok.getLocation());
4270 DefArgToks->push_back(DefArgEnd);
Mike Stump1eb44332009-09-09 15:08:12 +00004271 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc,
Anders Carlsson5e300d12009-06-12 16:51:40 +00004272 (*DefArgToks)[1].getLocation());
Argyrios Kyrtzidis2b602ad2010-08-06 09:47:24 +00004273 }
Chris Lattner04421082008-04-08 04:40:51 +00004274 } else {
Douglas Gregor72b505b2008-12-16 21:30:33 +00004275 // Consume the '='.
Douglas Gregor61366e92008-12-24 00:01:03 +00004276 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00004277
Douglas Gregorbe0f7bd2010-09-11 20:24:53 +00004278 // The argument isn't actually potentially evaluated unless it is
4279 // used.
4280 EnterExpressionEvaluationContext Eval(Actions,
Douglas Gregorccc1b5e2012-02-21 00:37:24 +00004281 Sema::PotentiallyEvaluatedIfUsed,
4282 Param);
Douglas Gregorbe0f7bd2010-09-11 20:24:53 +00004283
Sebastian Redl84407ba2012-03-14 15:54:00 +00004284 ExprResult DefArgResult;
4285 if (Tok.is(tok::l_brace))
4286 DefArgResult = ParseBraceInitializer();
4287 else
4288 DefArgResult = ParseAssignmentExpression();
Douglas Gregor72b505b2008-12-16 21:30:33 +00004289 if (DefArgResult.isInvalid()) {
4290 Actions.ActOnParamDefaultArgumentError(Param);
4291 SkipUntil(tok::comma, tok::r_paren, true, true);
4292 } else {
4293 // Inform the actions module about the default argument
4294 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
John McCall9ae2f072010-08-23 23:25:46 +00004295 DefArgResult.take());
Douglas Gregor72b505b2008-12-16 21:30:33 +00004296 }
Chris Lattner04421082008-04-08 04:40:51 +00004297 }
4298 }
Mike Stump1eb44332009-09-09 15:08:12 +00004299
4300 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
4301 ParmDecl.getIdentifierLoc(), Param,
Douglas Gregor72b505b2008-12-16 21:30:33 +00004302 DefArgToks));
Chris Lattnerf97409f2008-04-06 06:57:35 +00004303 }
4304
4305 // If the next token is a comma, consume it and keep reading arguments.
Douglas Gregored5d6512009-09-22 21:41:40 +00004306 if (Tok.isNot(tok::comma)) {
4307 if (Tok.is(tok::ellipsis)) {
Douglas Gregored5d6512009-09-22 21:41:40 +00004308 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
4309
David Blaikie4e4d0842012-03-11 07:00:24 +00004310 if (!getLangOpts().CPlusPlus) {
Douglas Gregored5d6512009-09-22 21:41:40 +00004311 // We have ellipsis without a preceding ',', which is ill-formed
4312 // in C. Complain and provide the fix.
4313 Diag(EllipsisLoc, diag::err_missing_comma_before_ellipsis)
Douglas Gregor849b2432010-03-31 17:46:05 +00004314 << FixItHint::CreateInsertion(EllipsisLoc, ", ");
Douglas Gregored5d6512009-09-22 21:41:40 +00004315 }
4316 }
4317
4318 break;
4319 }
Mike Stump1eb44332009-09-09 15:08:12 +00004320
Chris Lattnerf97409f2008-04-06 06:57:35 +00004321 // Consume the comma.
4322 ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00004323 }
Mike Stump1eb44332009-09-09 15:08:12 +00004324
Chris Lattner66d28652008-04-06 06:34:08 +00004325}
Chris Lattneref4715c2008-04-06 05:45:57 +00004326
Reid Spencer5f016e22007-07-11 17:01:13 +00004327/// [C90] direct-declarator '[' constant-expression[opt] ']'
4328/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
4329/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
4330/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
4331/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
4332void Parser::ParseBracketDeclarator(Declarator &D) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004333 BalancedDelimiterTracker T(*this, tok::l_square);
4334 T.consumeOpen();
Mike Stump1eb44332009-09-09 15:08:12 +00004335
Chris Lattner378c7e42008-12-18 07:27:21 +00004336 // C array syntax has many features, but by-far the most common is [] and [4].
4337 // This code does a fast path to handle some of the most obvious cases.
4338 if (Tok.getKind() == tok::r_square) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004339 T.consumeClose();
John McCall0b7e6782011-03-24 11:26:52 +00004340 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00004341 MaybeParseCXX0XAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00004342
Chris Lattner378c7e42008-12-18 07:27:21 +00004343 // Remember that we parsed the empty array type.
John McCall60d7b3a2010-08-24 06:29:42 +00004344 ExprResult NumElements;
John McCall0b7e6782011-03-24 11:26:52 +00004345 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004346 T.getOpenLocation(),
4347 T.getCloseLocation()),
4348 attrs, T.getCloseLocation());
Chris Lattner378c7e42008-12-18 07:27:21 +00004349 return;
4350 } else if (Tok.getKind() == tok::numeric_constant &&
4351 GetLookAheadToken(1).is(tok::r_square)) {
4352 // [4] is very common. Parse the numeric constant expression.
Richard Smith36f5cfe2012-03-09 08:00:36 +00004353 ExprResult ExprRes(Actions.ActOnNumericConstant(Tok, getCurScope()));
Chris Lattner378c7e42008-12-18 07:27:21 +00004354 ConsumeToken();
4355
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004356 T.consumeClose();
John McCall0b7e6782011-03-24 11:26:52 +00004357 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00004358 MaybeParseCXX0XAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00004359
Chris Lattner378c7e42008-12-18 07:27:21 +00004360 // Remember that we parsed a array type, and remember its features.
John McCall0b7e6782011-03-24 11:26:52 +00004361 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, 0,
John McCall7f040a92010-12-24 02:08:15 +00004362 ExprRes.release(),
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004363 T.getOpenLocation(),
4364 T.getCloseLocation()),
4365 attrs, T.getCloseLocation());
Chris Lattner378c7e42008-12-18 07:27:21 +00004366 return;
4367 }
Mike Stump1eb44332009-09-09 15:08:12 +00004368
Reid Spencer5f016e22007-07-11 17:01:13 +00004369 // If valid, this location is the position where we read the 'static' keyword.
4370 SourceLocation StaticLoc;
Chris Lattner04d66662007-10-09 17:33:22 +00004371 if (Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00004372 StaticLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00004373
Reid Spencer5f016e22007-07-11 17:01:13 +00004374 // If there is a type-qualifier-list, read it now.
Chris Lattnera1fcbad2008-12-18 06:50:14 +00004375 // Type qualifiers in an array subscript are a C99 feature.
John McCall0b7e6782011-03-24 11:26:52 +00004376 DeclSpec DS(AttrFactory);
Chris Lattner5a69d1c2008-12-18 07:02:59 +00004377 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Mike Stump1eb44332009-09-09 15:08:12 +00004378
Reid Spencer5f016e22007-07-11 17:01:13 +00004379 // If we haven't already read 'static', check to see if there is one after the
4380 // type-qualifier-list.
Chris Lattner04d66662007-10-09 17:33:22 +00004381 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00004382 StaticLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00004383
Reid Spencer5f016e22007-07-11 17:01:13 +00004384 // Handle "direct-declarator [ type-qual-list[opt] * ]".
4385 bool isStar = false;
John McCall60d7b3a2010-08-24 06:29:42 +00004386 ExprResult NumElements;
Mike Stump1eb44332009-09-09 15:08:12 +00004387
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00004388 // Handle the case where we have '[*]' as the array size. However, a leading
4389 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
4390 // the the token after the star is a ']'. Since stars in arrays are
4391 // infrequent, use of lookahead is not costly here.
4392 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattnera711dd02008-04-06 05:27:21 +00004393 ConsumeToken(); // Eat the '*'.
Reid Spencer5f016e22007-07-11 17:01:13 +00004394
Chris Lattnera1fcbad2008-12-18 06:50:14 +00004395 if (StaticLoc.isValid()) {
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00004396 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattnera1fcbad2008-12-18 06:50:14 +00004397 StaticLoc = SourceLocation(); // Drop the static.
4398 }
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00004399 isStar = true;
Chris Lattner04d66662007-10-09 17:33:22 +00004400 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner378c7e42008-12-18 07:27:21 +00004401 // Note, in C89, this production uses the constant-expr production instead
4402 // of assignment-expr. The only difference is that assignment-expr allows
4403 // things like '=' and '*='. Sema rejects these in C89 mode because they
4404 // are not i-c-e's, so we don't need to distinguish between the two here.
Mike Stump1eb44332009-09-09 15:08:12 +00004405
Douglas Gregore0762c92009-06-19 23:52:42 +00004406 // Parse the constant-expression or assignment-expression now (depending
4407 // on dialect).
David Blaikie4e4d0842012-03-11 07:00:24 +00004408 if (getLangOpts().CPlusPlus) {
Douglas Gregore0762c92009-06-19 23:52:42 +00004409 NumElements = ParseConstantExpression();
Eli Friedman71b8fb52012-01-21 01:01:51 +00004410 } else {
4411 EnterExpressionEvaluationContext Unevaluated(Actions,
4412 Sema::ConstantEvaluated);
Douglas Gregore0762c92009-06-19 23:52:42 +00004413 NumElements = ParseAssignmentExpression();
Eli Friedman71b8fb52012-01-21 01:01:51 +00004414 }
Reid Spencer5f016e22007-07-11 17:01:13 +00004415 }
Mike Stump1eb44332009-09-09 15:08:12 +00004416
Reid Spencer5f016e22007-07-11 17:01:13 +00004417 // If there was an error parsing the assignment-expression, recover.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00004418 if (NumElements.isInvalid()) {
Chris Lattner5cb10d32009-04-24 22:30:50 +00004419 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00004420 // If the expression was invalid, skip it.
4421 SkipUntil(tok::r_square);
4422 return;
4423 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00004424
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004425 T.consumeClose();
Sebastian Redlab197ba2009-02-09 18:23:29 +00004426
John McCall0b7e6782011-03-24 11:26:52 +00004427 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00004428 MaybeParseCXX0XAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00004429
Chris Lattner378c7e42008-12-18 07:27:21 +00004430 // Remember that we parsed a array type, and remember its features.
John McCall0b7e6782011-03-24 11:26:52 +00004431 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
Reid Spencer5f016e22007-07-11 17:01:13 +00004432 StaticLoc.isValid(), isStar,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00004433 NumElements.release(),
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004434 T.getOpenLocation(),
4435 T.getCloseLocation()),
4436 attrs, T.getCloseLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00004437}
4438
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004439/// [GNU] typeof-specifier:
4440/// typeof ( expressions )
4441/// typeof ( type-name )
4442/// [GNU/C++] typeof unary-expression
Steve Naroffd1861fd2007-07-31 12:34:36 +00004443///
4444void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner04d66662007-10-09 17:33:22 +00004445 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004446 Token OpTok = Tok;
Steve Naroffd1861fd2007-07-31 12:34:36 +00004447 SourceLocation StartLoc = ConsumeToken();
4448
John McCallcfb708c2010-01-13 20:03:27 +00004449 const bool hasParens = Tok.is(tok::l_paren);
4450
Eli Friedman71b8fb52012-01-21 01:01:51 +00004451 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated);
4452
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004453 bool isCastExpr;
John McCallb3d87482010-08-24 05:47:05 +00004454 ParsedType CastTy;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004455 SourceRange CastRange;
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00004456 ExprResult Operand = ParseExprAfterUnaryExprOrTypeTrait(OpTok, isCastExpr,
4457 CastTy, CastRange);
John McCallcfb708c2010-01-13 20:03:27 +00004458 if (hasParens)
4459 DS.setTypeofParensRange(CastRange);
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004460
4461 if (CastRange.getEnd().isInvalid())
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004462 // FIXME: Not accurate, the range gets one token more than it should.
4463 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004464 else
4465 DS.SetRangeEnd(CastRange.getEnd());
Mike Stump1eb44332009-09-09 15:08:12 +00004466
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004467 if (isCastExpr) {
4468 if (!CastTy) {
4469 DS.SetTypeSpecError();
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004470 return;
Douglas Gregor809070a2009-02-18 17:45:20 +00004471 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004472
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004473 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00004474 unsigned DiagID;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004475 // Check for duplicate type specifiers (e.g. "int typeof(int)").
4476 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00004477 DiagID, CastTy))
4478 Diag(StartLoc, DiagID) << PrevSpec;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004479 return;
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004480 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004481
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004482 // If we get here, the operand to the typeof was an expresion.
4483 if (Operand.isInvalid()) {
4484 DS.SetTypeSpecError();
Steve Naroff9dfa7b42007-08-02 02:53:48 +00004485 return;
Steve Naroffd1861fd2007-07-31 12:34:36 +00004486 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004487
Eli Friedman71b8fb52012-01-21 01:01:51 +00004488 // We might need to transform the operand if it is potentially evaluated.
4489 Operand = Actions.HandleExprEvaluationContextForTypeof(Operand.get());
4490 if (Operand.isInvalid()) {
4491 DS.SetTypeSpecError();
4492 return;
4493 }
4494
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004495 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00004496 unsigned DiagID;
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004497 // Check for duplicate type specifiers (e.g. "int typeof(int)").
4498 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
John McCallb3d87482010-08-24 05:47:05 +00004499 DiagID, Operand.get()))
John McCallfec54012009-08-03 20:12:06 +00004500 Diag(StartLoc, DiagID) << PrevSpec;
Steve Naroffd1861fd2007-07-31 12:34:36 +00004501}
Chris Lattner1b492422010-02-28 18:33:55 +00004502
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00004503/// [C11] atomic-specifier:
Eli Friedmanb001de72011-10-06 23:00:33 +00004504/// _Atomic ( type-name )
4505///
4506void Parser::ParseAtomicSpecifier(DeclSpec &DS) {
4507 assert(Tok.is(tok::kw__Atomic) && "Not an atomic specifier");
4508
4509 SourceLocation StartLoc = ConsumeToken();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004510 BalancedDelimiterTracker T(*this, tok::l_paren);
4511 if (T.expectAndConsume(diag::err_expected_lparen_after, "_Atomic")) {
Eli Friedmanb001de72011-10-06 23:00:33 +00004512 SkipUntil(tok::r_paren);
4513 return;
4514 }
4515
4516 TypeResult Result = ParseTypeName();
4517 if (Result.isInvalid()) {
4518 SkipUntil(tok::r_paren);
4519 return;
4520 }
4521
4522 // Match the ')'
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004523 T.consumeClose();
Eli Friedmanb001de72011-10-06 23:00:33 +00004524
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004525 if (T.getCloseLocation().isInvalid())
Eli Friedmanb001de72011-10-06 23:00:33 +00004526 return;
4527
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004528 DS.setTypeofParensRange(T.getRange());
4529 DS.SetRangeEnd(T.getCloseLocation());
Eli Friedmanb001de72011-10-06 23:00:33 +00004530
4531 const char *PrevSpec = 0;
4532 unsigned DiagID;
4533 if (DS.SetTypeSpecType(DeclSpec::TST_atomic, StartLoc, PrevSpec,
4534 DiagID, Result.release()))
4535 Diag(StartLoc, DiagID) << PrevSpec;
4536}
4537
Chris Lattner1b492422010-02-28 18:33:55 +00004538
4539/// TryAltiVecVectorTokenOutOfLine - Out of line body that should only be called
4540/// from TryAltiVecVectorToken.
4541bool Parser::TryAltiVecVectorTokenOutOfLine() {
4542 Token Next = NextToken();
4543 switch (Next.getKind()) {
4544 default: return false;
4545 case tok::kw_short:
4546 case tok::kw_long:
4547 case tok::kw_signed:
4548 case tok::kw_unsigned:
4549 case tok::kw_void:
4550 case tok::kw_char:
4551 case tok::kw_int:
4552 case tok::kw_float:
4553 case tok::kw_double:
4554 case tok::kw_bool:
4555 case tok::kw___pixel:
4556 Tok.setKind(tok::kw___vector);
4557 return true;
4558 case tok::identifier:
4559 if (Next.getIdentifierInfo() == Ident_pixel) {
4560 Tok.setKind(tok::kw___vector);
4561 return true;
4562 }
4563 return false;
4564 }
4565}
4566
4567bool Parser::TryAltiVecTokenOutOfLine(DeclSpec &DS, SourceLocation Loc,
4568 const char *&PrevSpec, unsigned &DiagID,
4569 bool &isInvalid) {
4570 if (Tok.getIdentifierInfo() == Ident_vector) {
4571 Token Next = NextToken();
4572 switch (Next.getKind()) {
4573 case tok::kw_short:
4574 case tok::kw_long:
4575 case tok::kw_signed:
4576 case tok::kw_unsigned:
4577 case tok::kw_void:
4578 case tok::kw_char:
4579 case tok::kw_int:
4580 case tok::kw_float:
4581 case tok::kw_double:
4582 case tok::kw_bool:
4583 case tok::kw___pixel:
4584 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
4585 return true;
4586 case tok::identifier:
4587 if (Next.getIdentifierInfo() == Ident_pixel) {
4588 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
4589 return true;
4590 }
4591 break;
4592 default:
4593 break;
4594 }
Douglas Gregora8f031f2010-06-16 15:28:57 +00004595 } else if ((Tok.getIdentifierInfo() == Ident_pixel) &&
Chris Lattner1b492422010-02-28 18:33:55 +00004596 DS.isTypeAltiVecVector()) {
4597 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
4598 return true;
4599 }
4600 return false;
4601}