blob: 476f476dab9af32021592c88cc531b38953db9de [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) {
Reid Spencer5f016e22007-07-11 17:01:13 +000039 // Parse the common declaration-specifiers piece.
John McCall0b7e6782011-03-24 11:26:52 +000040 DeclSpec DS(AttrFactory);
Richard Smithc89edf52011-07-01 19:46:12 +000041 ParseSpecifierQualifierList(DS, AS);
42 if (OwnedType)
43 *OwnedType = DS.isTypeSpecOwned() ? DS.getRepAsDecl() : 0;
Sebastian Redlef65f062009-05-29 18:02:33 +000044
Reid Spencer5f016e22007-07-11 17:01:13 +000045 // Parse the abstract-declarator, if present.
Douglas Gregor683a81f2011-01-31 16:09:46 +000046 Declarator DeclaratorInfo(DS, Context);
Reid Spencer5f016e22007-07-11 17:01:13 +000047 ParseDeclarator(DeclaratorInfo);
Sebastian Redlef65f062009-05-29 18:02:33 +000048 if (Range)
49 *Range = DeclaratorInfo.getSourceRange();
50
Chris Lattnereaaebc72009-04-25 08:06:05 +000051 if (DeclaratorInfo.isInvalidType())
Douglas Gregor809070a2009-02-18 17:45:20 +000052 return true;
53
Douglas Gregor23c94db2010-07-02 17:43:08 +000054 return Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
Reid Spencer5f016e22007-07-11 17:01:13 +000055}
56
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +000057
58/// isAttributeLateParsed - Return true if the attribute has arguments that
59/// require late parsing.
60static bool isAttributeLateParsed(const IdentifierInfo &II) {
61 return llvm::StringSwitch<bool>(II.getName())
62#include "clang/Parse/AttrLateParsed.inc"
63 .Default(false);
64}
65
66
Sean Huntbbd37c62009-11-21 08:43:09 +000067/// ParseGNUAttributes - Parse a non-empty attributes list.
Reid Spencer5f016e22007-07-11 17:01:13 +000068///
69/// [GNU] attributes:
70/// attribute
71/// attributes attribute
72///
73/// [GNU] attribute:
74/// '__attribute__' '(' '(' attribute-list ')' ')'
75///
76/// [GNU] attribute-list:
77/// attrib
78/// attribute_list ',' attrib
79///
80/// [GNU] attrib:
81/// empty
82/// attrib-name
83/// attrib-name '(' identifier ')'
84/// attrib-name '(' identifier ',' nonempty-expr-list ')'
85/// attrib-name '(' argument-expression-list [C99 6.5.2] ')'
86///
87/// [GNU] attrib-name:
88/// identifier
89/// typespec
90/// typequal
91/// storageclass
Mike Stump1eb44332009-09-09 15:08:12 +000092///
Reid Spencer5f016e22007-07-11 17:01:13 +000093/// FIXME: The GCC grammar/code for this construct implies we need two
Mike Stump1eb44332009-09-09 15:08:12 +000094/// token lookahead. Comment from gcc: "If they start with an identifier
95/// which is followed by a comma or close parenthesis, then the arguments
Reid Spencer5f016e22007-07-11 17:01:13 +000096/// start with that identifier; otherwise they are an expression list."
97///
Richard Smithfe0a0fb2011-10-17 21:20:17 +000098/// GCC does not require the ',' between attribs in an attribute-list.
99///
Reid Spencer5f016e22007-07-11 17:01:13 +0000100/// At the moment, I am not doing 2 token lookahead. I am also unaware of
101/// any attributes that don't work (based on my limited testing). Most
102/// attributes are very simple in practice. Until we find a bug, I don't see
103/// a pressing need to implement the 2 token lookahead.
104
John McCall7f040a92010-12-24 02:08:15 +0000105void Parser::ParseGNUAttributes(ParsedAttributes &attrs,
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000106 SourceLocation *endLoc,
107 LateParsedAttrList *LateAttrs) {
Sean Huntbbd37c62009-11-21 08:43:09 +0000108 assert(Tok.is(tok::kw___attribute) && "Not a GNU attribute list!");
Mike Stump1eb44332009-09-09 15:08:12 +0000109
Chris Lattner04d66662007-10-09 17:33:22 +0000110 while (Tok.is(tok::kw___attribute)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000111 ConsumeToken();
112 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
113 "attribute")) {
114 SkipUntil(tok::r_paren, true); // skip until ) or ;
John McCall7f040a92010-12-24 02:08:15 +0000115 return;
Reid Spencer5f016e22007-07-11 17:01:13 +0000116 }
117 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, "(")) {
118 SkipUntil(tok::r_paren, true); // skip until ) or ;
John McCall7f040a92010-12-24 02:08:15 +0000119 return;
Reid Spencer5f016e22007-07-11 17:01:13 +0000120 }
121 // Parse the attribute-list. e.g. __attribute__(( weak, alias("__f") ))
Chris Lattner04d66662007-10-09 17:33:22 +0000122 while (Tok.is(tok::identifier) || isDeclarationSpecifier() ||
123 Tok.is(tok::comma)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000124 if (Tok.is(tok::comma)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000125 // allows for empty/non-empty attributes. ((__vector_size__(16),,,,))
126 ConsumeToken();
127 continue;
128 }
129 // we have an identifier or declaration specifier (const, int, etc.)
130 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
131 SourceLocation AttrNameLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000132
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000133 if (Tok.is(tok::l_paren)) {
134 // handle "parameterized" attributes
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000135 if (LateAttrs && isAttributeLateParsed(*AttrName)) {
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000136 LateParsedAttribute *LA =
137 new LateParsedAttribute(this, *AttrName, AttrNameLoc);
138 LateAttrs->push_back(LA);
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000139
140 // Attributes in a class are parsed at the end of the class, along
141 // with other late-parsed declarations.
142 if (!ClassStack.empty())
143 getCurrentClass().LateParsedDeclarations.push_back(LA);
Mike Stump1eb44332009-09-09 15:08:12 +0000144
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000145 // consume everything up to and including the matching right parens
146 ConsumeAndStoreUntil(tok::r_paren, LA->Toks, true, false);
Mike Stump1eb44332009-09-09 15:08:12 +0000147
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000148 Token Eof;
149 Eof.startToken();
150 Eof.setLocation(Tok.getLocation());
151 LA->Toks.push_back(Eof);
152 } else {
153 ParseGNUAttributeArgs(AttrName, AttrNameLoc, attrs, endLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000154 }
155 } else {
John McCall0b7e6782011-03-24 11:26:52 +0000156 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc,
157 0, SourceLocation(), 0, 0);
Reid Spencer5f016e22007-07-11 17:01:13 +0000158 }
159 }
160 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
Reid Spencer5f016e22007-07-11 17:01:13 +0000161 SkipUntil(tok::r_paren, false);
Sean Huntbbd37c62009-11-21 08:43:09 +0000162 SourceLocation Loc = Tok.getLocation();
Sebastian Redlab197ba2009-02-09 18:23:29 +0000163 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) {
164 SkipUntil(tok::r_paren, false);
165 }
John McCall7f040a92010-12-24 02:08:15 +0000166 if (endLoc)
167 *endLoc = Loc;
Reid Spencer5f016e22007-07-11 17:01:13 +0000168 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000169}
170
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000171
172/// Parse the arguments to a parameterized GNU attribute
173void Parser::ParseGNUAttributeArgs(IdentifierInfo *AttrName,
174 SourceLocation AttrNameLoc,
175 ParsedAttributes &Attrs,
176 SourceLocation *EndLoc) {
177
178 assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('");
179
180 // Availability attributes have their own grammar.
181 if (AttrName->isStr("availability")) {
182 ParseAvailabilityAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc);
183 return;
184 }
185 // Thread safety attributes fit into the FIXME case above, so we
186 // just parse the arguments as a list of expressions
187 if (IsThreadSafetyAttribute(AttrName->getName())) {
188 ParseThreadSafetyAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc);
189 return;
190 }
191
192 ConsumeParen(); // ignore the left paren loc for now
193
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000194 IdentifierInfo *ParmName = 0;
195 SourceLocation ParmLoc;
196 bool BuiltinType = false;
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000197
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000198 switch (Tok.getKind()) {
199 case tok::kw_char:
200 case tok::kw_wchar_t:
201 case tok::kw_char16_t:
202 case tok::kw_char32_t:
203 case tok::kw_bool:
204 case tok::kw_short:
205 case tok::kw_int:
206 case tok::kw_long:
207 case tok::kw___int64:
208 case tok::kw_signed:
209 case tok::kw_unsigned:
210 case tok::kw_float:
211 case tok::kw_double:
212 case tok::kw_void:
213 case tok::kw_typeof:
214 // __attribute__(( vec_type_hint(char) ))
215 // FIXME: Don't just discard the builtin type token.
216 ConsumeToken();
217 BuiltinType = true;
218 break;
219
220 case tok::identifier:
221 ParmName = Tok.getIdentifierInfo();
222 ParmLoc = ConsumeToken();
223 break;
224
225 default:
226 break;
227 }
228
229 ExprVector ArgExprs(Actions);
230
231 if (!BuiltinType &&
232 (ParmLoc.isValid() ? Tok.is(tok::comma) : Tok.isNot(tok::r_paren))) {
233 // Eat the comma.
234 if (ParmLoc.isValid())
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000235 ConsumeToken();
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000236
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000237 // Parse the non-empty comma-separated list of expressions.
238 while (1) {
239 ExprResult ArgExpr(ParseAssignmentExpression());
240 if (ArgExpr.isInvalid()) {
241 SkipUntil(tok::r_paren);
242 return;
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000243 }
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000244 ArgExprs.push_back(ArgExpr.release());
245 if (Tok.isNot(tok::comma))
246 break;
247 ConsumeToken(); // Eat the comma, move to the next argument
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000248 }
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000249 }
Fariborz Jahanian7a81e412011-10-18 17:11:10 +0000250 else if (Tok.is(tok::less) && AttrName->isStr("iboutletcollection")) {
251 if (!ExpectAndConsume(tok::less, diag::err_expected_less_after, "<",
252 tok::greater)) {
Fariborz Jahanianb2243432011-10-18 23:13:50 +0000253 while (Tok.is(tok::identifier)) {
254 ConsumeToken();
255 if (Tok.is(tok::greater))
256 break;
257 if (Tok.is(tok::comma)) {
258 ConsumeToken();
259 continue;
260 }
261 }
262 if (Tok.isNot(tok::greater))
263 Diag(Tok, diag::err_iboutletcollection_with_protocol);
Fariborz Jahanian7a81e412011-10-18 17:11:10 +0000264 SkipUntil(tok::r_paren, false, true); // skip until ')'
265 }
266 }
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000267
268 SourceLocation RParen = Tok.getLocation();
269 if (!ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) {
270 AttributeList *attr =
Argyrios Kyrtzidisffcc3102011-09-13 16:05:53 +0000271 Attrs.addNew(AttrName, SourceRange(AttrNameLoc, RParen), 0, AttrNameLoc,
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000272 ParmName, ParmLoc, ArgExprs.take(), ArgExprs.size());
273 if (BuiltinType && attr->getKind() == AttributeList::AT_IBOutletCollection)
274 Diag(Tok, diag::err_iboutletcollection_builtintype);
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000275 }
276}
277
278
Eli Friedmana23b4852009-06-08 07:21:15 +0000279/// ParseMicrosoftDeclSpec - Parse an __declspec construct
280///
281/// [MS] decl-specifier:
282/// __declspec ( extended-decl-modifier-seq )
283///
284/// [MS] extended-decl-modifier-seq:
285/// extended-decl-modifier[opt]
286/// extended-decl-modifier extended-decl-modifier-seq
287
John McCall7f040a92010-12-24 02:08:15 +0000288void Parser::ParseMicrosoftDeclSpec(ParsedAttributes &attrs) {
Steve Narofff59e17e2008-12-24 20:59:21 +0000289 assert(Tok.is(tok::kw___declspec) && "Not a declspec!");
Eli Friedmana23b4852009-06-08 07:21:15 +0000290
Steve Narofff59e17e2008-12-24 20:59:21 +0000291 ConsumeToken();
Eli Friedmana23b4852009-06-08 07:21:15 +0000292 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
293 "declspec")) {
294 SkipUntil(tok::r_paren, true); // skip until ) or ;
John McCall7f040a92010-12-24 02:08:15 +0000295 return;
Eli Friedmana23b4852009-06-08 07:21:15 +0000296 }
Francois Pichet373197b2011-05-07 19:04:49 +0000297
Eli Friedman290eeb02009-06-08 23:27:34 +0000298 while (Tok.getIdentifierInfo()) {
Eli Friedmana23b4852009-06-08 07:21:15 +0000299 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
300 SourceLocation AttrNameLoc = ConsumeToken();
Francois Pichet373197b2011-05-07 19:04:49 +0000301
302 // FIXME: Remove this when we have proper __declspec(property()) support.
303 // Just skip everything inside property().
304 if (AttrName->getName() == "property") {
305 ConsumeParen();
306 SkipUntil(tok::r_paren);
307 }
Eli Friedmana23b4852009-06-08 07:21:15 +0000308 if (Tok.is(tok::l_paren)) {
309 ConsumeParen();
310 // FIXME: This doesn't parse __declspec(property(get=get_func_name))
311 // correctly.
John McCall60d7b3a2010-08-24 06:29:42 +0000312 ExprResult ArgExpr(ParseAssignmentExpression());
Eli Friedmana23b4852009-06-08 07:21:15 +0000313 if (!ArgExpr.isInvalid()) {
John McCallca0408f2010-08-23 06:44:23 +0000314 Expr *ExprList = ArgExpr.take();
John McCall0b7e6782011-03-24 11:26:52 +0000315 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
316 SourceLocation(), &ExprList, 1, true);
Eli Friedmana23b4852009-06-08 07:21:15 +0000317 }
318 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
319 SkipUntil(tok::r_paren, false);
320 } else {
John McCall0b7e6782011-03-24 11:26:52 +0000321 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc,
322 0, SourceLocation(), 0, 0, true);
Eli Friedmana23b4852009-06-08 07:21:15 +0000323 }
324 }
325 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
326 SkipUntil(tok::r_paren, false);
John McCall7f040a92010-12-24 02:08:15 +0000327 return;
Eli Friedman290eeb02009-06-08 23:27:34 +0000328}
329
John McCall7f040a92010-12-24 02:08:15 +0000330void Parser::ParseMicrosoftTypeAttributes(ParsedAttributes &attrs) {
Eli Friedman290eeb02009-06-08 23:27:34 +0000331 // Treat these like attributes
332 // FIXME: Allow Sema to distinguish between these and real attributes!
333 while (Tok.is(tok::kw___fastcall) || Tok.is(tok::kw___stdcall) ||
Douglas Gregorf813a2c2010-05-18 16:57:00 +0000334 Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___cdecl) ||
Francois Pichet3bd9aa42011-08-18 09:59:55 +0000335 Tok.is(tok::kw___ptr64) || Tok.is(tok::kw___w64) ||
Francois Pichet58fd97a2011-08-25 00:36:46 +0000336 Tok.is(tok::kw___ptr32) ||
Francois Pichet3bd9aa42011-08-18 09:59:55 +0000337 Tok.is(tok::kw___unaligned)) {
Eli Friedman290eeb02009-06-08 23:27:34 +0000338 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
339 SourceLocation AttrNameLoc = ConsumeToken();
Francois Pichet58fd97a2011-08-25 00:36:46 +0000340 if (Tok.is(tok::kw___ptr64) || Tok.is(tok::kw___w64) ||
341 Tok.is(tok::kw___ptr32))
Eli Friedman290eeb02009-06-08 23:27:34 +0000342 // FIXME: Support these properly!
343 continue;
John McCall0b7e6782011-03-24 11:26:52 +0000344 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
345 SourceLocation(), 0, 0, true);
Eli Friedman290eeb02009-06-08 23:27:34 +0000346 }
Steve Narofff59e17e2008-12-24 20:59:21 +0000347}
348
John McCall7f040a92010-12-24 02:08:15 +0000349void Parser::ParseBorlandTypeAttributes(ParsedAttributes &attrs) {
Dawn Perchik52fc3142010-09-03 01:29:35 +0000350 // Treat these like attributes
351 while (Tok.is(tok::kw___pascal)) {
352 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
353 SourceLocation AttrNameLoc = ConsumeToken();
John McCall0b7e6782011-03-24 11:26:52 +0000354 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
355 SourceLocation(), 0, 0, true);
Dawn Perchik52fc3142010-09-03 01:29:35 +0000356 }
John McCall7f040a92010-12-24 02:08:15 +0000357}
358
Peter Collingbournef315fa82011-02-14 01:42:53 +0000359void Parser::ParseOpenCLAttributes(ParsedAttributes &attrs) {
360 // Treat these like attributes
361 while (Tok.is(tok::kw___kernel)) {
362 SourceLocation AttrNameLoc = ConsumeToken();
John McCall0b7e6782011-03-24 11:26:52 +0000363 attrs.addNew(PP.getIdentifierInfo("opencl_kernel_function"),
364 AttrNameLoc, 0, AttrNameLoc, 0,
365 SourceLocation(), 0, 0, false);
Peter Collingbournef315fa82011-02-14 01:42:53 +0000366 }
367}
368
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000369void Parser::ParseOpenCLQualifiers(DeclSpec &DS) {
370 SourceLocation Loc = Tok.getLocation();
371 switch(Tok.getKind()) {
372 // OpenCL qualifiers:
373 case tok::kw___private:
374 case tok::kw_private:
John McCall0b7e6782011-03-24 11:26:52 +0000375 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000376 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000377 PP.getIdentifierInfo("address_space"), Loc, 0);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000378 break;
379
380 case tok::kw___global:
John McCall0b7e6782011-03-24 11:26:52 +0000381 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000382 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000383 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_global);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000384 break;
385
386 case tok::kw___local:
John McCall0b7e6782011-03-24 11:26:52 +0000387 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000388 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000389 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_local);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000390 break;
391
392 case tok::kw___constant:
John McCall0b7e6782011-03-24 11:26:52 +0000393 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000394 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000395 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_constant);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000396 break;
397
398 case tok::kw___read_only:
John McCall0b7e6782011-03-24 11:26:52 +0000399 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000400 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000401 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_read_only);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000402 break;
403
404 case tok::kw___write_only:
John McCall0b7e6782011-03-24 11:26:52 +0000405 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000406 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000407 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_write_only);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000408 break;
409
410 case tok::kw___read_write:
John McCall0b7e6782011-03-24 11:26:52 +0000411 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000412 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000413 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_read_write);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000414 break;
415 default: break;
416 }
417}
418
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000419/// \brief Parse a version number.
420///
421/// version:
422/// simple-integer
423/// simple-integer ',' simple-integer
424/// simple-integer ',' simple-integer ',' simple-integer
425VersionTuple Parser::ParseVersionTuple(SourceRange &Range) {
426 Range = Tok.getLocation();
427
428 if (!Tok.is(tok::numeric_constant)) {
429 Diag(Tok, diag::err_expected_version);
430 SkipUntil(tok::comma, tok::r_paren, true, true, true);
431 return VersionTuple();
432 }
433
434 // Parse the major (and possibly minor and subminor) versions, which
435 // are stored in the numeric constant. We utilize a quirk of the
436 // lexer, which is that it handles something like 1.2.3 as a single
437 // numeric constant, rather than two separate tokens.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000438 SmallString<512> Buffer;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000439 Buffer.resize(Tok.getLength()+1);
440 const char *ThisTokBegin = &Buffer[0];
441
442 // Get the spelling of the token, which eliminates trigraphs, etc.
443 bool Invalid = false;
444 unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin, &Invalid);
445 if (Invalid)
446 return VersionTuple();
447
448 // Parse the major version.
449 unsigned AfterMajor = 0;
450 unsigned Major = 0;
451 while (AfterMajor < ActualLength && isdigit(ThisTokBegin[AfterMajor])) {
452 Major = Major * 10 + ThisTokBegin[AfterMajor] - '0';
453 ++AfterMajor;
454 }
455
456 if (AfterMajor == 0) {
457 Diag(Tok, diag::err_expected_version);
458 SkipUntil(tok::comma, tok::r_paren, true, true, true);
459 return VersionTuple();
460 }
461
462 if (AfterMajor == ActualLength) {
463 ConsumeToken();
464
465 // We only had a single version component.
466 if (Major == 0) {
467 Diag(Tok, diag::err_zero_version);
468 return VersionTuple();
469 }
470
471 return VersionTuple(Major);
472 }
473
474 if (ThisTokBegin[AfterMajor] != '.' || (AfterMajor + 1 == ActualLength)) {
475 Diag(Tok, diag::err_expected_version);
476 SkipUntil(tok::comma, tok::r_paren, true, true, true);
477 return VersionTuple();
478 }
479
480 // Parse the minor version.
481 unsigned AfterMinor = AfterMajor + 1;
482 unsigned Minor = 0;
483 while (AfterMinor < ActualLength && isdigit(ThisTokBegin[AfterMinor])) {
484 Minor = Minor * 10 + ThisTokBegin[AfterMinor] - '0';
485 ++AfterMinor;
486 }
487
488 if (AfterMinor == ActualLength) {
489 ConsumeToken();
490
491 // We had major.minor.
492 if (Major == 0 && Minor == 0) {
493 Diag(Tok, diag::err_zero_version);
494 return VersionTuple();
495 }
496
497 return VersionTuple(Major, Minor);
498 }
499
500 // If what follows is not a '.', we have a problem.
501 if (ThisTokBegin[AfterMinor] != '.') {
502 Diag(Tok, diag::err_expected_version);
503 SkipUntil(tok::comma, tok::r_paren, true, true, true);
504 return VersionTuple();
505 }
506
507 // Parse the subminor version.
508 unsigned AfterSubminor = AfterMinor + 1;
509 unsigned Subminor = 0;
510 while (AfterSubminor < ActualLength && isdigit(ThisTokBegin[AfterSubminor])) {
511 Subminor = Subminor * 10 + ThisTokBegin[AfterSubminor] - '0';
512 ++AfterSubminor;
513 }
514
515 if (AfterSubminor != ActualLength) {
516 Diag(Tok, diag::err_expected_version);
517 SkipUntil(tok::comma, tok::r_paren, true, true, true);
518 return VersionTuple();
519 }
520 ConsumeToken();
521 return VersionTuple(Major, Minor, Subminor);
522}
523
524/// \brief Parse the contents of the "availability" attribute.
525///
526/// availability-attribute:
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000527/// 'availability' '(' platform ',' version-arg-list, opt-message')'
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000528///
529/// platform:
530/// identifier
531///
532/// version-arg-list:
533/// version-arg
534/// version-arg ',' version-arg-list
535///
536/// version-arg:
537/// 'introduced' '=' version
538/// 'deprecated' '=' version
539/// 'removed' = version
Douglas Gregorb53e4172011-03-26 03:35:55 +0000540/// 'unavailable'
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000541/// opt-message:
542/// 'message' '=' <string>
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000543void Parser::ParseAvailabilityAttribute(IdentifierInfo &Availability,
544 SourceLocation AvailabilityLoc,
545 ParsedAttributes &attrs,
546 SourceLocation *endLoc) {
547 SourceLocation PlatformLoc;
548 IdentifierInfo *Platform = 0;
549
550 enum { Introduced, Deprecated, Obsoleted, Unknown };
551 AvailabilityChange Changes[Unknown];
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000552 ExprResult MessageExpr;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000553
554 // Opening '('.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000555 BalancedDelimiterTracker T(*this, tok::l_paren);
556 if (T.consumeOpen()) {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000557 Diag(Tok, diag::err_expected_lparen);
558 return;
559 }
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000560
561 // Parse the platform name,
562 if (Tok.isNot(tok::identifier)) {
563 Diag(Tok, diag::err_availability_expected_platform);
564 SkipUntil(tok::r_paren);
565 return;
566 }
567 Platform = Tok.getIdentifierInfo();
568 PlatformLoc = ConsumeToken();
569
570 // Parse the ',' following the platform name.
571 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "", tok::r_paren))
572 return;
573
574 // If we haven't grabbed the pointers for the identifiers
575 // "introduced", "deprecated", and "obsoleted", do so now.
576 if (!Ident_introduced) {
577 Ident_introduced = PP.getIdentifierInfo("introduced");
578 Ident_deprecated = PP.getIdentifierInfo("deprecated");
579 Ident_obsoleted = PP.getIdentifierInfo("obsoleted");
Douglas Gregorb53e4172011-03-26 03:35:55 +0000580 Ident_unavailable = PP.getIdentifierInfo("unavailable");
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000581 Ident_message = PP.getIdentifierInfo("message");
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000582 }
583
584 // Parse the set of introductions/deprecations/removals.
Douglas Gregorb53e4172011-03-26 03:35:55 +0000585 SourceLocation UnavailableLoc;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000586 do {
587 if (Tok.isNot(tok::identifier)) {
588 Diag(Tok, diag::err_availability_expected_change);
589 SkipUntil(tok::r_paren);
590 return;
591 }
592 IdentifierInfo *Keyword = Tok.getIdentifierInfo();
593 SourceLocation KeywordLoc = ConsumeToken();
594
Douglas Gregorb53e4172011-03-26 03:35:55 +0000595 if (Keyword == Ident_unavailable) {
596 if (UnavailableLoc.isValid()) {
597 Diag(KeywordLoc, diag::err_availability_redundant)
598 << Keyword << SourceRange(UnavailableLoc);
599 }
600 UnavailableLoc = KeywordLoc;
601
602 if (Tok.isNot(tok::comma))
603 break;
604
605 ConsumeToken();
606 continue;
607 }
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000608
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000609 if (Tok.isNot(tok::equal)) {
610 Diag(Tok, diag::err_expected_equal_after)
611 << Keyword;
612 SkipUntil(tok::r_paren);
613 return;
614 }
615 ConsumeToken();
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000616 if (Keyword == Ident_message) {
617 if (!isTokenStringLiteral()) {
618 Diag(Tok, diag::err_expected_string_literal);
619 SkipUntil(tok::r_paren);
620 return;
621 }
622 MessageExpr = ParseStringLiteralExpression();
623 break;
624 }
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000625
626 SourceRange VersionRange;
627 VersionTuple Version = ParseVersionTuple(VersionRange);
628
629 if (Version.empty()) {
630 SkipUntil(tok::r_paren);
631 return;
632 }
633
634 unsigned Index;
635 if (Keyword == Ident_introduced)
636 Index = Introduced;
637 else if (Keyword == Ident_deprecated)
638 Index = Deprecated;
639 else if (Keyword == Ident_obsoleted)
640 Index = Obsoleted;
641 else
642 Index = Unknown;
643
644 if (Index < Unknown) {
645 if (!Changes[Index].KeywordLoc.isInvalid()) {
646 Diag(KeywordLoc, diag::err_availability_redundant)
647 << Keyword
648 << SourceRange(Changes[Index].KeywordLoc,
649 Changes[Index].VersionRange.getEnd());
650 }
651
652 Changes[Index].KeywordLoc = KeywordLoc;
653 Changes[Index].Version = Version;
654 Changes[Index].VersionRange = VersionRange;
655 } else {
656 Diag(KeywordLoc, diag::err_availability_unknown_change)
657 << Keyword << VersionRange;
658 }
659
660 if (Tok.isNot(tok::comma))
661 break;
662
663 ConsumeToken();
664 } while (true);
665
666 // Closing ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000667 if (T.consumeClose())
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000668 return;
669
670 if (endLoc)
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000671 *endLoc = T.getCloseLocation();
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000672
Douglas Gregorb53e4172011-03-26 03:35:55 +0000673 // The 'unavailable' availability cannot be combined with any other
674 // availability changes. Make sure that hasn't happened.
675 if (UnavailableLoc.isValid()) {
676 bool Complained = false;
677 for (unsigned Index = Introduced; Index != Unknown; ++Index) {
678 if (Changes[Index].KeywordLoc.isValid()) {
679 if (!Complained) {
680 Diag(UnavailableLoc, diag::warn_availability_and_unavailable)
681 << SourceRange(Changes[Index].KeywordLoc,
682 Changes[Index].VersionRange.getEnd());
683 Complained = true;
684 }
685
686 // Clear out the availability.
687 Changes[Index] = AvailabilityChange();
688 }
689 }
690 }
691
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000692 // Record this attribute
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000693 attrs.addNew(&Availability,
694 SourceRange(AvailabilityLoc, T.getCloseLocation()),
Fariborz Jahanianf96708d2012-01-23 23:38:32 +0000695 0, AvailabilityLoc,
John McCall0b7e6782011-03-24 11:26:52 +0000696 Platform, PlatformLoc,
697 Changes[Introduced],
698 Changes[Deprecated],
Douglas Gregorb53e4172011-03-26 03:35:55 +0000699 Changes[Obsoleted],
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000700 UnavailableLoc, MessageExpr.take(),
701 false, false);
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000702}
703
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000704
705// Late Parsed Attributes:
706// See other examples of late parsing in lib/Parse/ParseCXXInlineMethods
707
708void Parser::LateParsedDeclaration::ParseLexedAttributes() {}
709
710void Parser::LateParsedClass::ParseLexedAttributes() {
711 Self->ParseLexedAttributes(*Class);
712}
713
714void Parser::LateParsedAttribute::ParseLexedAttributes() {
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000715 Self->ParseLexedAttribute(*this, true, false);
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000716}
717
718/// Wrapper class which calls ParseLexedAttribute, after setting up the
719/// scope appropriately.
720void Parser::ParseLexedAttributes(ParsingClass &Class) {
721 // Deal with templates
722 // FIXME: Test cases to make sure this does the right thing for templates.
723 bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope;
724 ParseScope ClassTemplateScope(this, Scope::TemplateParamScope,
725 HasTemplateScope);
726 if (HasTemplateScope)
727 Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate);
728
729 // Set or update the scope flags to include Scope::ThisScope.
730 bool AlreadyHasClassScope = Class.TopLevelClass;
731 unsigned ScopeFlags = Scope::ClassScope|Scope::DeclScope|Scope::ThisScope;
732 ParseScope ClassScope(this, ScopeFlags, !AlreadyHasClassScope);
733 ParseScopeFlags ClassScopeFlags(this, ScopeFlags, AlreadyHasClassScope);
734
735 for (unsigned i = 0, ni = Class.LateParsedDeclarations.size(); i < ni; ++i) {
736 Class.LateParsedDeclarations[i]->ParseLexedAttributes();
737 }
738}
739
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000740
741/// \brief Parse all attributes in LAs, and attach them to Decl D.
742void Parser::ParseLexedAttributeList(LateParsedAttrList &LAs, Decl *D,
743 bool EnterScope, bool OnDefinition) {
744 for (unsigned i = 0, ni = LAs.size(); i < ni; ++i) {
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +0000745 LAs[i]->addDecl(D);
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000746 ParseLexedAttribute(*LAs[i], EnterScope, OnDefinition);
747 }
748 LAs.clear();
749}
750
751
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000752/// \brief Finish parsing an attribute for which parsing was delayed.
753/// This will be called at the end of parsing a class declaration
754/// for each LateParsedAttribute. We consume the saved tokens and
755/// create an attribute with the arguments filled in. We add this
756/// to the Attribute list for the decl.
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000757void Parser::ParseLexedAttribute(LateParsedAttribute &LA,
758 bool EnterScope, bool OnDefinition) {
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000759 // Save the current token position.
760 SourceLocation OrigLoc = Tok.getLocation();
761
762 // Append the current token at the end of the new token stream so that it
763 // doesn't get lost.
764 LA.Toks.push_back(Tok);
765 PP.EnterTokenStream(LA.Toks.data(), LA.Toks.size(), true, false);
766 // Consume the previously pushed token.
767 ConsumeAnyToken();
768
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000769 if (OnDefinition && !IsThreadSafetyAttribute(LA.AttrName.getName())) {
770 Diag(Tok, diag::warn_attribute_on_function_definition)
771 << LA.AttrName.getName();
772 }
773
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000774 ParsedAttributes Attrs(AttrFactory);
775 SourceLocation endLoc;
776
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +0000777 // Late parsed attributes must be attached to Decls by hand. If there
778 // are no Decls, then this was not done properly.
779 assert(LA.Decls.size() > 0 && "No decls attached to late parsed attribute");
Caitlin Sadowskied9d84a2011-09-08 17:42:31 +0000780
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +0000781 if (LA.Decls.size() == 1) {
782 Decl *D = LA.Decls[0];
Caitlin Sadowskied9d84a2011-09-08 17:42:31 +0000783
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +0000784 // If the Decl is templatized, add template parameters to scope.
785 bool HasTemplateScope = EnterScope && D->isTemplateDecl();
786 ParseScope TempScope(this, Scope::TemplateParamScope, HasTemplateScope);
787 if (HasTemplateScope)
788 Actions.ActOnReenterTemplateScope(Actions.CurScope, D);
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000789
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +0000790 // If the Decl is on a function, add function parameters to the scope.
791 bool HasFunctionScope = EnterScope && D->isFunctionOrFunctionTemplate();
792 ParseScope FnScope(this, Scope::FnScope|Scope::DeclScope, HasFunctionScope);
793 if (HasFunctionScope)
794 Actions.ActOnReenterFunctionContext(Actions.CurScope, D);
795
796 ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, &endLoc);
797
798 if (HasFunctionScope) {
799 Actions.ActOnExitFunctionContext();
800 FnScope.Exit(); // Pop scope, and remove Decls from IdResolver
801 }
802 if (HasTemplateScope) {
803 TempScope.Exit();
804 }
805 } else {
806 // If there are multiple decls, then the decl cannot be within the
807 // function scope.
808 ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, &endLoc);
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))
823 ConsumeAnyToken();
824 }
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.
939 if (getLang().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:
Richard Smith0706df42011-10-19 21:33:05 +00001028 return getLang().CPlusPlus;
1029
Richard Smith1c94c162012-01-09 22:31:44 +00001030 case tok::l_square: // Might be an attribute on an unnamed bit-field.
1031 return Context == Declarator::MemberContext && getLang().CPlusPlus0x &&
1032 NextToken().is(tok::l_square);
1033
1034 case tok::colon: // Might be a typo for '::' or an unnamed bit-field.
1035 return Context == Declarator::MemberContext || getLang().CPlusPlus;
1036
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 ||
1062 (getLang().CPlusPlus && Context == Declarator::FileContext);
1063
1064 case tok::identifier: // Possible virt-specifier.
1065 return getLang().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 {
John McCall731ad842009-12-19 09:28:58 +00001329 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
1330 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
John McCall731ad842009-12-19 09:28:58 +00001342 if (getLang().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
Douglas Gregorb4debae2009-12-22 17:47:17 +00001362 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
1363 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
1370 if (getLang().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
1381 if (getLang().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 }
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001392 } else if (getLang().CPlusPlus0x && Tok.is(tok::l_brace)) {
1393 // 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 Smithc89edf52011-07-01 19:46:12 +00001429void Parser::ParseSpecifierQualifierList(DeclSpec &DS, AccessSpecifier AS) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001430 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
1431 /// parse declaration-specifiers and complain about extra stuff.
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001432 /// TODO: diagnose attribute-specifiers and alignment-specifiers.
Richard Smithc89edf52011-07-01 19:46:12 +00001433 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS);
Mike Stump1eb44332009-09-09 15:08:12 +00001434
Reid Spencer5f016e22007-07-11 17:01:13 +00001435 // Validate declspec for type-name.
1436 unsigned Specs = DS.getParsedSpecifiers();
Chris Lattnerb6645dd2009-04-14 21:16:09 +00001437 if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers() &&
John McCall7f040a92010-12-24 02:08:15 +00001438 !DS.hasAttributes())
Reid Spencer5f016e22007-07-11 17:01:13 +00001439 Diag(Tok, diag::err_typename_requires_specqual);
Mike Stump1eb44332009-09-09 15:08:12 +00001440
Reid Spencer5f016e22007-07-11 17:01:13 +00001441 // Issue diagnostic and remove storage class if present.
1442 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
1443 if (DS.getStorageClassSpecLoc().isValid())
1444 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
1445 else
1446 Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass);
1447 DS.ClearStorageClassSpecs();
1448 }
Mike Stump1eb44332009-09-09 15:08:12 +00001449
Reid Spencer5f016e22007-07-11 17:01:13 +00001450 // Issue diagnostic and remove function specfier if present.
1451 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Douglas Gregorb48fe382008-10-31 09:07:45 +00001452 if (DS.isInlineSpecified())
1453 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
1454 if (DS.isVirtualSpecified())
1455 Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec);
1456 if (DS.isExplicitSpecified())
1457 Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec);
Reid Spencer5f016e22007-07-11 17:01:13 +00001458 DS.ClearFunctionSpecs();
1459 }
1460}
1461
Chris Lattnerc199ab32009-04-12 20:42:31 +00001462/// isValidAfterIdentifierInDeclaratorAfterDeclSpec - Return true if the
1463/// specified token is valid after the identifier in a declarator which
1464/// immediately follows the declspec. For example, these things are valid:
1465///
1466/// int x [ 4]; // direct-declarator
1467/// int x ( int y); // direct-declarator
1468/// int(int x ) // direct-declarator
1469/// int x ; // simple-declaration
1470/// int x = 17; // init-declarator-list
1471/// int x , y; // init-declarator-list
1472/// int x __asm__ ("foo"); // init-declarator-list
Chris Lattnerb6645dd2009-04-14 21:16:09 +00001473/// int x : 4; // struct-declarator
Chris Lattnerc83c27a2009-04-12 22:29:43 +00001474/// int x { 5}; // C++'0x unified initializers
Chris Lattnerc199ab32009-04-12 20:42:31 +00001475///
1476/// This is not, because 'x' does not immediately follow the declspec (though
1477/// ')' happens to be valid anyway).
1478/// int (x)
1479///
1480static bool isValidAfterIdentifierInDeclarator(const Token &T) {
1481 return T.is(tok::l_square) || T.is(tok::l_paren) || T.is(tok::r_paren) ||
1482 T.is(tok::semi) || T.is(tok::comma) || T.is(tok::equal) ||
Chris Lattnerb6645dd2009-04-14 21:16:09 +00001483 T.is(tok::kw_asm) || T.is(tok::l_brace) || T.is(tok::colon);
Chris Lattnerc199ab32009-04-12 20:42:31 +00001484}
1485
Chris Lattnere40c2952009-04-14 21:34:55 +00001486
1487/// ParseImplicitInt - This method is called when we have an non-typename
1488/// identifier in a declspec (which normally terminates the decl spec) when
1489/// the declspec has no type specifier. In this case, the declspec is either
1490/// malformed or is "implicit int" (in K&R and C89).
1491///
1492/// This method handles diagnosing this prettily and returns false if the
1493/// declspec is done being processed. If it recovers and thinks there may be
1494/// other pieces of declspec after it, it returns true.
1495///
Chris Lattnerf4382f52009-04-14 22:17:06 +00001496bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001497 const ParsedTemplateInfo &TemplateInfo,
Chris Lattnere40c2952009-04-14 21:34:55 +00001498 AccessSpecifier AS) {
Chris Lattnerf4382f52009-04-14 22:17:06 +00001499 assert(Tok.is(tok::identifier) && "should have identifier");
Mike Stump1eb44332009-09-09 15:08:12 +00001500
Chris Lattnere40c2952009-04-14 21:34:55 +00001501 SourceLocation Loc = Tok.getLocation();
1502 // If we see an identifier that is not a type name, we normally would
1503 // parse it as the identifer being declared. However, when a typename
1504 // is typo'd or the definition is not included, this will incorrectly
1505 // parse the typename as the identifier name and fall over misparsing
1506 // later parts of the diagnostic.
1507 //
1508 // As such, we try to do some look-ahead in cases where this would
1509 // otherwise be an "implicit-int" case to see if this is invalid. For
1510 // example: "static foo_t x = 4;" In this case, if we parsed foo_t as
1511 // an identifier with implicit int, we'd get a parse error because the
1512 // next token is obviously invalid for a type. Parse these as a case
1513 // with an invalid type specifier.
1514 assert(!DS.hasTypeSpecifier() && "Type specifier checked above");
Mike Stump1eb44332009-09-09 15:08:12 +00001515
Chris Lattnere40c2952009-04-14 21:34:55 +00001516 // Since we know that this either implicit int (which is rare) or an
1517 // error, we'd do lookahead to try to do better recovery.
1518 if (isValidAfterIdentifierInDeclarator(NextToken())) {
1519 // If this token is valid for implicit int, e.g. "static x = 4", then
1520 // we just avoid eating the identifier, so it will be parsed as the
1521 // identifier in the declarator.
1522 return false;
1523 }
Mike Stump1eb44332009-09-09 15:08:12 +00001524
Chris Lattnere40c2952009-04-14 21:34:55 +00001525 // Otherwise, if we don't consume this token, we are going to emit an
1526 // error anyway. Try to recover from various common problems. Check
1527 // to see if this was a reference to a tag name without a tag specified.
1528 // This is a common problem in C (saying 'foo' instead of 'struct foo').
Chris Lattnerf4382f52009-04-14 22:17:06 +00001529 //
1530 // C++ doesn't need this, and isTagName doesn't take SS.
1531 if (SS == 0) {
Argyrios Kyrtzidisb8a9d3b2011-04-21 17:29:47 +00001532 const char *TagName = 0, *FixitTagName = 0;
Chris Lattnerf4382f52009-04-14 22:17:06 +00001533 tok::TokenKind TagKind = tok::unknown;
Mike Stump1eb44332009-09-09 15:08:12 +00001534
Douglas Gregor23c94db2010-07-02 17:43:08 +00001535 switch (Actions.isTagName(*Tok.getIdentifierInfo(), getCurScope())) {
Chris Lattnere40c2952009-04-14 21:34:55 +00001536 default: break;
Argyrios Kyrtzidisb8a9d3b2011-04-21 17:29:47 +00001537 case DeclSpec::TST_enum:
1538 TagName="enum" ; FixitTagName = "enum " ; TagKind=tok::kw_enum ;break;
1539 case DeclSpec::TST_union:
1540 TagName="union" ; FixitTagName = "union " ;TagKind=tok::kw_union ;break;
1541 case DeclSpec::TST_struct:
1542 TagName="struct"; FixitTagName = "struct ";TagKind=tok::kw_struct;break;
1543 case DeclSpec::TST_class:
1544 TagName="class" ; FixitTagName = "class " ;TagKind=tok::kw_class ;break;
Chris Lattnere40c2952009-04-14 21:34:55 +00001545 }
Mike Stump1eb44332009-09-09 15:08:12 +00001546
Chris Lattnerf4382f52009-04-14 22:17:06 +00001547 if (TagName) {
1548 Diag(Loc, diag::err_use_of_tag_name_without_tag)
John McCall23e907a2010-02-14 01:03:10 +00001549 << Tok.getIdentifierInfo() << TagName << getLang().CPlusPlus
Argyrios Kyrtzidisb8a9d3b2011-04-21 17:29:47 +00001550 << FixItHint::CreateInsertion(Tok.getLocation(),FixitTagName);
Mike Stump1eb44332009-09-09 15:08:12 +00001551
Chris Lattnerf4382f52009-04-14 22:17:06 +00001552 // Parse this as a tag as if the missing tag were present.
1553 if (TagKind == tok::kw_enum)
Douglas Gregor9b9edd62010-03-02 17:53:14 +00001554 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS);
Chris Lattnerf4382f52009-04-14 22:17:06 +00001555 else
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001556 ParseClassSpecifier(TagKind, Loc, DS, TemplateInfo, AS);
Chris Lattnerf4382f52009-04-14 22:17:06 +00001557 return true;
1558 }
Chris Lattnere40c2952009-04-14 21:34:55 +00001559 }
Mike Stump1eb44332009-09-09 15:08:12 +00001560
Douglas Gregora786fdb2009-10-13 23:27:22 +00001561 // This is almost certainly an invalid type name. Let the action emit a
1562 // diagnostic and attempt to recover.
John McCallb3d87482010-08-24 05:47:05 +00001563 ParsedType T;
Douglas Gregora786fdb2009-10-13 23:27:22 +00001564 if (Actions.DiagnoseUnknownTypeName(*Tok.getIdentifierInfo(), Loc,
Douglas Gregor23c94db2010-07-02 17:43:08 +00001565 getCurScope(), SS, T)) {
Douglas Gregora786fdb2009-10-13 23:27:22 +00001566 // The action emitted a diagnostic, so we don't have to.
1567 if (T) {
1568 // The action has suggested that the type T could be used. Set that as
1569 // the type in the declaration specifiers, consume the would-be type
1570 // name token, and we're done.
1571 const char *PrevSpec;
1572 unsigned DiagID;
John McCallb3d87482010-08-24 05:47:05 +00001573 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID, T);
Douglas Gregora786fdb2009-10-13 23:27:22 +00001574 DS.SetRangeEnd(Tok.getLocation());
1575 ConsumeToken();
1576
1577 // There may be other declaration specifiers after this.
1578 return true;
1579 }
1580
1581 // Fall through; the action had no suggestion for us.
1582 } else {
1583 // The action did not emit a diagnostic, so emit one now.
1584 SourceRange R;
1585 if (SS) R = SS->getRange();
1586 Diag(Loc, diag::err_unknown_typename) << Tok.getIdentifierInfo() << R;
1587 }
Mike Stump1eb44332009-09-09 15:08:12 +00001588
Douglas Gregora786fdb2009-10-13 23:27:22 +00001589 // Mark this as an error.
Chris Lattnere40c2952009-04-14 21:34:55 +00001590 const char *PrevSpec;
John McCallfec54012009-08-03 20:12:06 +00001591 unsigned DiagID;
1592 DS.SetTypeSpecType(DeclSpec::TST_error, Loc, PrevSpec, DiagID);
Chris Lattnere40c2952009-04-14 21:34:55 +00001593 DS.SetRangeEnd(Tok.getLocation());
1594 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001595
Chris Lattnere40c2952009-04-14 21:34:55 +00001596 // TODO: Could inject an invalid typedef decl in an enclosing scope to
1597 // avoid rippling error messages on subsequent uses of the same type,
1598 // could be useful if #include was forgotten.
1599 return false;
1600}
1601
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001602/// \brief Determine the declaration specifier context from the declarator
1603/// context.
1604///
1605/// \param Context the declarator context, which is one of the
1606/// Declarator::TheContext enumerator values.
1607Parser::DeclSpecContext
1608Parser::getDeclSpecContextFromDeclaratorContext(unsigned Context) {
1609 if (Context == Declarator::MemberContext)
1610 return DSC_class;
1611 if (Context == Declarator::FileContext)
1612 return DSC_top_level;
1613 return DSC_normal;
1614}
1615
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001616/// ParseAlignArgument - Parse the argument to an alignment-specifier.
1617///
1618/// FIXME: Simply returns an alignof() expression if the argument is a
1619/// type. Ideally, the type should be propagated directly into Sema.
1620///
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00001621/// [C11] type-id
1622/// [C11] constant-expression
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00001623/// [C++0x] type-id ...[opt]
1624/// [C++0x] assignment-expression ...[opt]
1625ExprResult Parser::ParseAlignArgument(SourceLocation Start,
1626 SourceLocation &EllipsisLoc) {
1627 ExprResult ER;
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001628 if (isTypeIdInParens()) {
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001629 SourceLocation TypeLoc = Tok.getLocation();
1630 ParsedType Ty = ParseTypeName().get();
1631 SourceRange TypeRange(Start, Tok.getLocation());
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00001632 ER = Actions.ActOnUnaryExprOrTypeTraitExpr(TypeLoc, UETT_AlignOf, true,
1633 Ty.getAsOpaquePtr(), TypeRange);
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001634 } else
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00001635 ER = ParseConstantExpression();
1636
Peter Collingbournefe9b2a82011-10-24 17:56:00 +00001637 if (getLang().CPlusPlus0x && Tok.is(tok::ellipsis))
1638 EllipsisLoc = ConsumeToken();
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00001639
1640 return ER;
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001641}
1642
1643/// ParseAlignmentSpecifier - Parse an alignment-specifier, and add the
1644/// attribute to Attrs.
1645///
1646/// alignment-specifier:
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00001647/// [C11] '_Alignas' '(' type-id ')'
1648/// [C11] '_Alignas' '(' constant-expression ')'
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00001649/// [C++0x] 'alignas' '(' type-id ...[opt] ')'
1650/// [C++0x] 'alignas' '(' assignment-expression ...[opt] ')'
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001651void Parser::ParseAlignmentSpecifier(ParsedAttributes &Attrs,
1652 SourceLocation *endLoc) {
1653 assert((Tok.is(tok::kw_alignas) || Tok.is(tok::kw__Alignas)) &&
1654 "Not an alignment-specifier!");
1655
1656 SourceLocation KWLoc = Tok.getLocation();
1657 ConsumeToken();
1658
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001659 BalancedDelimiterTracker T(*this, tok::l_paren);
1660 if (T.expectAndConsume(diag::err_expected_lparen))
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001661 return;
1662
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00001663 SourceLocation EllipsisLoc;
1664 ExprResult ArgExpr = ParseAlignArgument(T.getOpenLocation(), EllipsisLoc);
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001665 if (ArgExpr.isInvalid()) {
1666 SkipUntil(tok::r_paren);
1667 return;
1668 }
1669
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001670 T.consumeClose();
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001671 if (endLoc)
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001672 *endLoc = T.getCloseLocation();
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001673
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00001674 // FIXME: Handle pack-expansions here.
1675 if (EllipsisLoc.isValid()) {
1676 Diag(EllipsisLoc, diag::err_alignas_pack_exp_unsupported);
1677 return;
1678 }
1679
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001680 ExprVector ArgExprs(Actions);
1681 ArgExprs.push_back(ArgExpr.release());
1682 Attrs.addNew(PP.getIdentifierInfo("aligned"), KWLoc, 0, KWLoc,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001683 0, T.getOpenLocation(), ArgExprs.take(), 1, false, true);
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001684}
1685
Reid Spencer5f016e22007-07-11 17:01:13 +00001686/// ParseDeclarationSpecifiers
1687/// declaration-specifiers: [C99 6.7]
1688/// storage-class-specifier declaration-specifiers[opt]
1689/// type-specifier declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00001690/// [C99] function-specifier declaration-specifiers[opt]
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00001691/// [C11] alignment-specifier declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00001692/// [GNU] attributes declaration-specifiers[opt]
Douglas Gregor8d267c52011-09-09 02:06:17 +00001693/// [Clang] '__module_private__' declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00001694///
1695/// storage-class-specifier: [C99 6.7.1]
1696/// 'typedef'
1697/// 'extern'
1698/// 'static'
1699/// 'auto'
1700/// 'register'
Sebastian Redl669d5d72008-11-14 23:42:31 +00001701/// [C++] 'mutable'
Reid Spencer5f016e22007-07-11 17:01:13 +00001702/// [GNU] '__thread'
Reid Spencer5f016e22007-07-11 17:01:13 +00001703/// function-specifier: [C99 6.7.4]
1704/// [C99] 'inline'
Douglas Gregorb48fe382008-10-31 09:07:45 +00001705/// [C++] 'virtual'
1706/// [C++] 'explicit'
Peter Collingbournef315fa82011-02-14 01:42:53 +00001707/// [OpenCL] '__kernel'
Anders Carlssonf47f7a12009-05-06 04:46:28 +00001708/// 'friend': [C++ dcl.friend]
Sebastian Redl2ac67232009-11-05 15:47:02 +00001709/// 'constexpr': [C++0x dcl.constexpr]
Anders Carlssonf47f7a12009-05-06 04:46:28 +00001710
Reid Spencer5f016e22007-07-11 17:01:13 +00001711///
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +00001712void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001713 const ParsedTemplateInfo &TemplateInfo,
John McCall67d1a672009-08-06 02:15:43 +00001714 AccessSpecifier AS,
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +00001715 DeclSpecContext DSContext,
1716 LateParsedAttrList *LateAttrs) {
Douglas Gregor312eadb2011-04-24 05:37:28 +00001717 if (DS.getSourceRange().isInvalid()) {
1718 DS.SetRangeStart(Tok.getLocation());
1719 DS.SetRangeEnd(Tok.getLocation());
1720 }
1721
Douglas Gregorefaa93a2011-11-07 17:33:42 +00001722 bool EnteringContext = (DSContext == DSC_class || DSContext == DSC_top_level);
Reid Spencer5f016e22007-07-11 17:01:13 +00001723 while (1) {
John McCallfec54012009-08-03 20:12:06 +00001724 bool isInvalid = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001725 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00001726 unsigned DiagID = 0;
1727
Reid Spencer5f016e22007-07-11 17:01:13 +00001728 SourceLocation Loc = Tok.getLocation();
Douglas Gregor12e083c2008-11-07 15:42:26 +00001729
Reid Spencer5f016e22007-07-11 17:01:13 +00001730 switch (Tok.getKind()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001731 default:
Chris Lattnerbce61352008-07-26 00:20:22 +00001732 DoneWithDeclSpec:
Peter Collingbournef1907682011-09-29 18:03:57 +00001733 // [C++0x] decl-specifier-seq: decl-specifier attribute-specifier-seq[opt]
1734 MaybeParseCXX0XAttributes(DS.getAttributes());
1735
Reid Spencer5f016e22007-07-11 17:01:13 +00001736 // If this is not a declaration specifier token, we're done reading decl
1737 // specifiers. First verify that DeclSpec's are consistent.
Douglas Gregor9b3064b2009-04-01 22:41:11 +00001738 DS.Finish(Diags, PP);
Reid Spencer5f016e22007-07-11 17:01:13 +00001739 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001740
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001741 case tok::code_completion: {
John McCallf312b1e2010-08-26 23:41:50 +00001742 Sema::ParserCompletionContext CCC = Sema::PCC_Namespace;
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001743 if (DS.hasTypeSpecifier()) {
1744 bool AllowNonIdentifiers
1745 = (getCurScope()->getFlags() & (Scope::ControlScope |
1746 Scope::BlockScope |
1747 Scope::TemplateParamScope |
1748 Scope::FunctionPrototypeScope |
1749 Scope::AtCatchScope)) == 0;
1750 bool AllowNestedNameSpecifiers
1751 = DSContext == DSC_top_level ||
1752 (DSContext == DSC_class && DS.isFriendSpecified());
1753
Douglas Gregorc7b6d882010-09-16 15:14:18 +00001754 Actions.CodeCompleteDeclSpec(getCurScope(), DS,
1755 AllowNonIdentifiers,
1756 AllowNestedNameSpecifiers);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001757 return cutOffParsing();
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001758 }
1759
Douglas Gregor68e3c2e2011-02-15 20:33:25 +00001760 if (getCurScope()->getFnParent() || getCurScope()->getBlockParent())
1761 CCC = Sema::PCC_LocalDeclarationSpecifiers;
1762 else if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate)
John McCallf312b1e2010-08-26 23:41:50 +00001763 CCC = DSContext == DSC_class? Sema::PCC_MemberTemplate
1764 : Sema::PCC_Template;
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001765 else if (DSContext == DSC_class)
John McCallf312b1e2010-08-26 23:41:50 +00001766 CCC = Sema::PCC_Class;
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +00001767 else if (CurParsedObjCImpl)
John McCallf312b1e2010-08-26 23:41:50 +00001768 CCC = Sema::PCC_ObjCImplementation;
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001769
1770 Actions.CodeCompleteOrdinaryName(getCurScope(), CCC);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001771 return cutOffParsing();
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001772 }
1773
Chris Lattner5e02c472009-01-05 00:07:25 +00001774 case tok::coloncolon: // ::foo::bar
John McCall9ba61662010-02-26 08:45:28 +00001775 // C++ scope specifier. Annotate and loop, or bail out on error.
1776 if (TryAnnotateCXXScopeToken(true)) {
1777 if (!DS.hasTypeSpecifier())
1778 DS.SetTypeSpecError();
1779 goto DoneWithDeclSpec;
1780 }
John McCall2e0a7152010-03-01 18:20:46 +00001781 if (Tok.is(tok::coloncolon)) // ::new or ::delete
1782 goto DoneWithDeclSpec;
John McCall9ba61662010-02-26 08:45:28 +00001783 continue;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001784
1785 case tok::annot_cxxscope: {
1786 if (DS.hasTypeSpecifier())
1787 goto DoneWithDeclSpec;
1788
John McCallaa87d332009-12-12 11:40:51 +00001789 CXXScopeSpec SS;
Douglas Gregorc34348a2011-02-24 17:54:50 +00001790 Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(),
1791 Tok.getAnnotationRange(),
1792 SS);
John McCallaa87d332009-12-12 11:40:51 +00001793
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001794 // We are looking for a qualified typename.
Douglas Gregor9135c722009-03-25 15:40:00 +00001795 Token Next = NextToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001796 if (Next.is(tok::annot_template_id) &&
Douglas Gregor9135c722009-03-25 15:40:00 +00001797 static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue())
Douglas Gregorc45c2322009-03-31 00:43:58 +00001798 ->Kind == TNK_Type_template) {
Douglas Gregor9135c722009-03-25 15:40:00 +00001799 // We have a qualified template-id, e.g., N::A<int>
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001800
1801 // C++ [class.qual]p2:
1802 // In a lookup in which the constructor is an acceptable lookup
1803 // result and the nested-name-specifier nominates a class C:
1804 //
1805 // - if the name specified after the
1806 // nested-name-specifier, when looked up in C, is the
1807 // injected-class-name of C (Clause 9), or
1808 //
1809 // - if the name specified after the nested-name-specifier
1810 // is the same as the identifier or the
1811 // simple-template-id's template-name in the last
1812 // component of the nested-name-specifier,
1813 //
1814 // the name is instead considered to name the constructor of
1815 // class C.
1816 //
1817 // Thus, if the template-name is actually the constructor
1818 // name, then the code is ill-formed; this interpretation is
1819 // reinforced by the NAD status of core issue 635.
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +00001820 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Next);
John McCallba9d8532010-04-13 06:39:49 +00001821 if ((DSContext == DSC_top_level ||
1822 (DSContext == DSC_class && DS.isFriendSpecified())) &&
1823 TemplateId->Name &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00001824 Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) {
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001825 if (isConstructorDeclarator()) {
1826 // The user meant this to be an out-of-line constructor
1827 // definition, but template arguments are not allowed
1828 // there. Just allow this as a constructor; we'll
1829 // complain about it later.
1830 goto DoneWithDeclSpec;
1831 }
1832
1833 // The user meant this to name a type, but it actually names
1834 // a constructor with some extraneous template
1835 // arguments. Complain, then parse it as a type as the user
1836 // intended.
1837 Diag(TemplateId->TemplateNameLoc,
1838 diag::err_out_of_line_template_id_names_constructor)
1839 << TemplateId->Name;
1840 }
1841
John McCallaa87d332009-12-12 11:40:51 +00001842 DS.getTypeSpecScope() = SS;
1843 ConsumeToken(); // The C++ scope.
Mike Stump1eb44332009-09-09 15:08:12 +00001844 assert(Tok.is(tok::annot_template_id) &&
Douglas Gregor9135c722009-03-25 15:40:00 +00001845 "ParseOptionalCXXScopeSpecifier not working");
Douglas Gregor059101f2011-03-02 00:47:37 +00001846 AnnotateTemplateIdTokenAsType();
Douglas Gregor9135c722009-03-25 15:40:00 +00001847 continue;
1848 }
1849
Douglas Gregor9d7b3532009-09-28 07:26:33 +00001850 if (Next.is(tok::annot_typename)) {
John McCallaa87d332009-12-12 11:40:51 +00001851 DS.getTypeSpecScope() = SS;
1852 ConsumeToken(); // The C++ scope.
John McCallb3d87482010-08-24 05:47:05 +00001853 if (Tok.getAnnotationValue()) {
1854 ParsedType T = getTypeAnnotation(Tok);
Nico Weber253e80b2010-11-22 10:30:56 +00001855 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename,
1856 Tok.getAnnotationEndLoc(),
John McCallb3d87482010-08-24 05:47:05 +00001857 PrevSpec, DiagID, T);
1858 }
Douglas Gregor9d7b3532009-09-28 07:26:33 +00001859 else
1860 DS.SetTypeSpecError();
1861 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1862 ConsumeToken(); // The typename
1863 }
1864
Douglas Gregor9135c722009-03-25 15:40:00 +00001865 if (Next.isNot(tok::identifier))
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001866 goto DoneWithDeclSpec;
1867
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001868 // If we're in a context where the identifier could be a class name,
1869 // check whether this is a constructor declaration.
John McCallba9d8532010-04-13 06:39:49 +00001870 if ((DSContext == DSC_top_level ||
1871 (DSContext == DSC_class && DS.isFriendSpecified())) &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00001872 Actions.isCurrentClassName(*Next.getIdentifierInfo(), getCurScope(),
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001873 &SS)) {
1874 if (isConstructorDeclarator())
1875 goto DoneWithDeclSpec;
1876
1877 // As noted in C++ [class.qual]p2 (cited above), when the name
1878 // of the class is qualified in a context where it could name
1879 // a constructor, its a constructor name. However, we've
1880 // looked at the declarator, and the user probably meant this
1881 // to be a type. Complain that it isn't supposed to be treated
1882 // as a type, then proceed to parse it as a type.
1883 Diag(Next.getLocation(), diag::err_out_of_line_type_names_constructor)
1884 << Next.getIdentifierInfo();
1885 }
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001886
John McCallb3d87482010-08-24 05:47:05 +00001887 ParsedType TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(),
1888 Next.getLocation(),
Douglas Gregor9e876872011-03-01 18:12:44 +00001889 getCurScope(), &SS,
1890 false, false, ParsedType(),
Abramo Bagnarafad03b72012-01-27 08:46:19 +00001891 /*IsCtorOrDtorName=*/false,
Douglas Gregor9e876872011-03-01 18:12:44 +00001892 /*NonTrivialSourceInfo=*/true);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001893
Chris Lattnerf4382f52009-04-14 22:17:06 +00001894 // If the referenced identifier is not a type, then this declspec is
1895 // erroneous: We already checked about that it has no type specifier, and
1896 // C++ doesn't have implicit int. Diagnose it as a typo w.r.t. to the
Mike Stump1eb44332009-09-09 15:08:12 +00001897 // typename.
Chris Lattnerf4382f52009-04-14 22:17:06 +00001898 if (TypeRep == 0) {
1899 ConsumeToken(); // Eat the scope spec so the identifier is current.
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001900 if (ParseImplicitInt(DS, &SS, TemplateInfo, AS)) continue;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001901 goto DoneWithDeclSpec;
Chris Lattnerf4382f52009-04-14 22:17:06 +00001902 }
Mike Stump1eb44332009-09-09 15:08:12 +00001903
John McCallaa87d332009-12-12 11:40:51 +00001904 DS.getTypeSpecScope() = SS;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001905 ConsumeToken(); // The C++ scope.
1906
Douglas Gregor1a51b4a2009-02-09 15:09:02 +00001907 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001908 DiagID, TypeRep);
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001909 if (isInvalid)
1910 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001911
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001912 DS.SetRangeEnd(Tok.getLocation());
1913 ConsumeToken(); // The typename.
1914
1915 continue;
1916 }
Mike Stump1eb44332009-09-09 15:08:12 +00001917
Chris Lattner80d0c892009-01-21 19:48:37 +00001918 case tok::annot_typename: {
John McCallb3d87482010-08-24 05:47:05 +00001919 if (Tok.getAnnotationValue()) {
1920 ParsedType T = getTypeAnnotation(Tok);
Nico Weberc43271e2010-11-22 12:50:03 +00001921 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallb3d87482010-08-24 05:47:05 +00001922 DiagID, T);
1923 } else
Douglas Gregor31a19b62009-04-01 21:51:26 +00001924 DS.SetTypeSpecError();
Chris Lattner5c5db552010-04-05 18:18:31 +00001925
1926 if (isInvalid)
1927 break;
1928
Chris Lattner80d0c892009-01-21 19:48:37 +00001929 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1930 ConsumeToken(); // The typename
Mike Stump1eb44332009-09-09 15:08:12 +00001931
Chris Lattner80d0c892009-01-21 19:48:37 +00001932 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1933 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001934 // Objective-C interface.
1935 if (Tok.is(tok::less) && getLang().ObjC1)
1936 ParseObjCProtocolQualifiers(DS);
1937
Chris Lattner80d0c892009-01-21 19:48:37 +00001938 continue;
1939 }
Mike Stump1eb44332009-09-09 15:08:12 +00001940
Douglas Gregorbfad9152011-04-28 15:48:45 +00001941 case tok::kw___is_signed:
1942 // GNU libstdc++ 4.4 uses __is_signed as an identifier, but Clang
1943 // typically treats it as a trait. If we see __is_signed as it appears
1944 // in libstdc++, e.g.,
1945 //
1946 // static const bool __is_signed;
1947 //
1948 // then treat __is_signed as an identifier rather than as a keyword.
1949 if (DS.getTypeSpecType() == TST_bool &&
1950 DS.getTypeQualifiers() == DeclSpec::TQ_const &&
1951 DS.getStorageClassSpec() == DeclSpec::SCS_static) {
1952 Tok.getIdentifierInfo()->RevertTokenIDToIdentifier();
1953 Tok.setKind(tok::identifier);
1954 }
1955
1956 // We're done with the declaration-specifiers.
1957 goto DoneWithDeclSpec;
1958
Chris Lattner3bd934a2008-07-26 01:18:38 +00001959 // typedef-name
David Blaikie42d6d0c2011-12-04 05:04:18 +00001960 case tok::kw_decltype:
Chris Lattner3bd934a2008-07-26 01:18:38 +00001961 case tok::identifier: {
Chris Lattner5e02c472009-01-05 00:07:25 +00001962 // In C++, check to see if this is a scope specifier like foo::bar::, if
1963 // so handle it as such. This is important for ctor parsing.
John McCall9ba61662010-02-26 08:45:28 +00001964 if (getLang().CPlusPlus) {
1965 if (TryAnnotateCXXScopeToken(true)) {
1966 if (!DS.hasTypeSpecifier())
1967 DS.SetTypeSpecError();
1968 goto DoneWithDeclSpec;
1969 }
1970 if (!Tok.is(tok::identifier))
1971 continue;
1972 }
Mike Stump1eb44332009-09-09 15:08:12 +00001973
Chris Lattner3bd934a2008-07-26 01:18:38 +00001974 // This identifier can only be a typedef name if we haven't already seen
1975 // a type-specifier. Without this check we misparse:
1976 // typedef int X; struct Y { short X; }; as 'short int'.
1977 if (DS.hasTypeSpecifier())
1978 goto DoneWithDeclSpec;
Mike Stump1eb44332009-09-09 15:08:12 +00001979
John Thompson82287d12010-02-05 00:12:22 +00001980 // Check for need to substitute AltiVec keyword tokens.
1981 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
1982 break;
1983
Chris Lattner3bd934a2008-07-26 01:18:38 +00001984 // It has to be available as a typedef too!
John McCallb3d87482010-08-24 05:47:05 +00001985 ParsedType TypeRep =
1986 Actions.getTypeName(*Tok.getIdentifierInfo(),
1987 Tok.getLocation(), getCurScope());
Douglas Gregor55f6b142009-02-09 18:46:07 +00001988
Chris Lattnerc199ab32009-04-12 20:42:31 +00001989 // If this is not a typedef name, don't parse it as part of the declspec,
1990 // it must be an implicit int or an error.
John McCallb3d87482010-08-24 05:47:05 +00001991 if (!TypeRep) {
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001992 if (ParseImplicitInt(DS, 0, TemplateInfo, AS)) continue;
Chris Lattner3bd934a2008-07-26 01:18:38 +00001993 goto DoneWithDeclSpec;
Chris Lattnerc199ab32009-04-12 20:42:31 +00001994 }
Douglas Gregor55f6b142009-02-09 18:46:07 +00001995
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001996 // If we're in a context where the identifier could be a class name,
1997 // check whether this is a constructor declaration.
1998 if (getLang().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00001999 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), getCurScope()) &&
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002000 isConstructorDeclarator())
Douglas Gregorb48fe382008-10-31 09:07:45 +00002001 goto DoneWithDeclSpec;
2002
Douglas Gregor1a51b4a2009-02-09 15:09:02 +00002003 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00002004 DiagID, TypeRep);
Chris Lattner3bd934a2008-07-26 01:18:38 +00002005 if (isInvalid)
2006 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002007
Chris Lattner3bd934a2008-07-26 01:18:38 +00002008 DS.SetRangeEnd(Tok.getLocation());
2009 ConsumeToken(); // The identifier
2010
2011 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
2012 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00002013 // Objective-C interface.
2014 if (Tok.is(tok::less) && getLang().ObjC1)
2015 ParseObjCProtocolQualifiers(DS);
2016
Steve Naroff4f9b9f12008-09-22 10:28:57 +00002017 // Need to support trailing type qualifiers (e.g. "id<p> const").
2018 // If a type specifier follows, it will be diagnosed elsewhere.
2019 continue;
Chris Lattner3bd934a2008-07-26 01:18:38 +00002020 }
Douglas Gregor39a8de12009-02-25 19:37:18 +00002021
2022 // type-name
2023 case tok::annot_template_id: {
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +00002024 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregorc45c2322009-03-31 00:43:58 +00002025 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor39a8de12009-02-25 19:37:18 +00002026 // This template-id does not refer to a type name, so we're
2027 // done with the type-specifiers.
2028 goto DoneWithDeclSpec;
2029 }
2030
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002031 // If we're in a context where the template-id could be a
2032 // constructor name or specialization, check whether this is a
2033 // constructor declaration.
2034 if (getLang().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00002035 Actions.isCurrentClassName(*TemplateId->Name, getCurScope()) &&
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002036 isConstructorDeclarator())
2037 goto DoneWithDeclSpec;
2038
Douglas Gregor39a8de12009-02-25 19:37:18 +00002039 // Turn the template-id annotation token into a type annotation
2040 // token, then try again to parse it as a type-specifier.
Douglas Gregor31a19b62009-04-01 21:51:26 +00002041 AnnotateTemplateIdTokenAsType();
Douglas Gregor39a8de12009-02-25 19:37:18 +00002042 continue;
2043 }
2044
Reid Spencer5f016e22007-07-11 17:01:13 +00002045 // GNU attributes support.
2046 case tok::kw___attribute:
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +00002047 ParseGNUAttributes(DS.getAttributes(), 0, LateAttrs);
Reid Spencer5f016e22007-07-11 17:01:13 +00002048 continue;
Steve Narofff59e17e2008-12-24 20:59:21 +00002049
2050 // Microsoft declspec support.
2051 case tok::kw___declspec:
John McCall7f040a92010-12-24 02:08:15 +00002052 ParseMicrosoftDeclSpec(DS.getAttributes());
Steve Narofff59e17e2008-12-24 20:59:21 +00002053 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00002054
Steve Naroff239f0732008-12-25 14:16:32 +00002055 // Microsoft single token adornments.
Steve Naroff86bc6cf2008-12-25 14:41:26 +00002056 case tok::kw___forceinline:
Eli Friedman290eeb02009-06-08 23:27:34 +00002057 // FIXME: Add handling here!
2058 break;
2059
2060 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00002061 case tok::kw___ptr32:
Steve Naroff86bc6cf2008-12-25 14:41:26 +00002062 case tok::kw___w64:
Steve Naroff239f0732008-12-25 14:16:32 +00002063 case tok::kw___cdecl:
2064 case tok::kw___stdcall:
2065 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00002066 case tok::kw___thiscall:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00002067 case tok::kw___unaligned:
John McCall7f040a92010-12-24 02:08:15 +00002068 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman290eeb02009-06-08 23:27:34 +00002069 continue;
2070
Dawn Perchik52fc3142010-09-03 01:29:35 +00002071 // Borland single token adornments.
2072 case tok::kw___pascal:
John McCall7f040a92010-12-24 02:08:15 +00002073 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik52fc3142010-09-03 01:29:35 +00002074 continue;
2075
Peter Collingbournef315fa82011-02-14 01:42:53 +00002076 // OpenCL single token adornments.
2077 case tok::kw___kernel:
2078 ParseOpenCLAttributes(DS.getAttributes());
2079 continue;
2080
Reid Spencer5f016e22007-07-11 17:01:13 +00002081 // storage-class-specifier
2082 case tok::kw_typedef:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002083 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_typedef, Loc,
2084 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002085 break;
2086 case tok::kw_extern:
2087 if (DS.isThreadSpecified())
Chris Lattner1ab3b962008-11-18 07:48:38 +00002088 Diag(Tok, diag::ext_thread_before) << "extern";
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002089 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_extern, Loc,
2090 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002091 break;
Steve Naroff8d54bf22007-12-18 00:16:02 +00002092 case tok::kw___private_extern__:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002093 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_private_extern,
2094 Loc, PrevSpec, DiagID);
Steve Naroff8d54bf22007-12-18 00:16:02 +00002095 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00002096 case tok::kw_static:
2097 if (DS.isThreadSpecified())
Chris Lattner1ab3b962008-11-18 07:48:38 +00002098 Diag(Tok, diag::ext_thread_before) << "static";
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002099 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_static, Loc,
2100 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002101 break;
2102 case tok::kw_auto:
Douglas Gregor18d8b792011-03-14 21:43:30 +00002103 if (getLang().CPlusPlus0x) {
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002104 if (isKnownToBeTypeSpecifier(GetLookAheadToken(1))) {
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002105 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc,
2106 PrevSpec, DiagID);
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002107 if (!isInvalid)
Richard Smith8f4fb192011-09-04 19:54:14 +00002108 Diag(Tok, diag::ext_auto_storage_class)
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002109 << FixItHint::CreateRemoval(DS.getStorageClassSpecLoc());
Richard Smith8f4fb192011-09-04 19:54:14 +00002110 } else
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002111 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec,
2112 DiagID);
Richard Smith8f4fb192011-09-04 19:54:14 +00002113 } else
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002114 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc,
2115 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002116 break;
2117 case tok::kw_register:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002118 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_register, Loc,
2119 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002120 break;
Sebastian Redl669d5d72008-11-14 23:42:31 +00002121 case tok::kw_mutable:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002122 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_mutable, Loc,
2123 PrevSpec, DiagID);
Sebastian Redl669d5d72008-11-14 23:42:31 +00002124 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00002125 case tok::kw___thread:
John McCallfec54012009-08-03 20:12:06 +00002126 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002127 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002128
Reid Spencer5f016e22007-07-11 17:01:13 +00002129 // function-specifier
2130 case tok::kw_inline:
John McCallfec54012009-08-03 20:12:06 +00002131 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002132 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +00002133 case tok::kw_virtual:
John McCallfec54012009-08-03 20:12:06 +00002134 isInvalid = DS.SetFunctionSpecVirtual(Loc, PrevSpec, DiagID);
Douglas Gregorb48fe382008-10-31 09:07:45 +00002135 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +00002136 case tok::kw_explicit:
John McCallfec54012009-08-03 20:12:06 +00002137 isInvalid = DS.SetFunctionSpecExplicit(Loc, PrevSpec, DiagID);
Douglas Gregorb48fe382008-10-31 09:07:45 +00002138 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002139
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002140 // alignment-specifier
2141 case tok::kw__Alignas:
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00002142 if (!getLang().C11)
2143 Diag(Tok, diag::ext_c11_alignas);
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002144 ParseAlignmentSpecifier(DS.getAttributes());
2145 continue;
2146
Anders Carlssonf47f7a12009-05-06 04:46:28 +00002147 // friend
2148 case tok::kw_friend:
John McCall67d1a672009-08-06 02:15:43 +00002149 if (DSContext == DSC_class)
2150 isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID);
2151 else {
2152 PrevSpec = ""; // not actually used by the diagnostic
2153 DiagID = diag::err_friend_invalid_in_context;
2154 isInvalid = true;
2155 }
Anders Carlssonf47f7a12009-05-06 04:46:28 +00002156 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002157
Douglas Gregor8d267c52011-09-09 02:06:17 +00002158 // Modules
2159 case tok::kw___module_private__:
2160 isInvalid = DS.setModulePrivateSpec(Loc, PrevSpec, DiagID);
2161 break;
2162
Sebastian Redl2ac67232009-11-05 15:47:02 +00002163 // constexpr
2164 case tok::kw_constexpr:
2165 isInvalid = DS.SetConstexprSpec(Loc, PrevSpec, DiagID);
2166 break;
2167
Chris Lattner80d0c892009-01-21 19:48:37 +00002168 // type-specifier
2169 case tok::kw_short:
John McCallfec54012009-08-03 20:12:06 +00002170 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec,
2171 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002172 break;
2173 case tok::kw_long:
2174 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCallfec54012009-08-03 20:12:06 +00002175 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
2176 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002177 else
John McCallfec54012009-08-03 20:12:06 +00002178 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2179 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002180 break;
Francois Pichet338d7f72011-04-28 01:59:37 +00002181 case tok::kw___int64:
2182 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2183 DiagID);
2184 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002185 case tok::kw_signed:
John McCallfec54012009-08-03 20:12:06 +00002186 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec,
2187 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002188 break;
2189 case tok::kw_unsigned:
John McCallfec54012009-08-03 20:12:06 +00002190 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
2191 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002192 break;
2193 case tok::kw__Complex:
John McCallfec54012009-08-03 20:12:06 +00002194 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
2195 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002196 break;
2197 case tok::kw__Imaginary:
John McCallfec54012009-08-03 20:12:06 +00002198 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
2199 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002200 break;
2201 case tok::kw_void:
John McCallfec54012009-08-03 20:12:06 +00002202 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec,
2203 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002204 break;
2205 case tok::kw_char:
John McCallfec54012009-08-03 20:12:06 +00002206 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec,
2207 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002208 break;
2209 case tok::kw_int:
John McCallfec54012009-08-03 20:12:06 +00002210 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec,
2211 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002212 break;
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00002213 case tok::kw_half:
2214 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_half, Loc, PrevSpec,
2215 DiagID);
2216 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002217 case tok::kw_float:
John McCallfec54012009-08-03 20:12:06 +00002218 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec,
2219 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002220 break;
2221 case tok::kw_double:
John McCallfec54012009-08-03 20:12:06 +00002222 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec,
2223 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002224 break;
2225 case tok::kw_wchar_t:
John McCallfec54012009-08-03 20:12:06 +00002226 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec,
2227 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002228 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002229 case tok::kw_char16_t:
John McCallfec54012009-08-03 20:12:06 +00002230 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec,
2231 DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002232 break;
2233 case tok::kw_char32_t:
John McCallfec54012009-08-03 20:12:06 +00002234 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec,
2235 DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002236 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002237 case tok::kw_bool:
2238 case tok::kw__Bool:
Argyrios Kyrtzidis4383e182010-11-16 18:18:13 +00002239 if (Tok.is(tok::kw_bool) &&
2240 DS.getTypeSpecType() != DeclSpec::TST_unspecified &&
2241 DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
2242 PrevSpec = ""; // Not used by the diagnostic.
2243 DiagID = diag::err_bool_redeclaration;
Fariborz Jahaniane106a0b2011-04-19 21:42:37 +00002244 // For better error recovery.
2245 Tok.setKind(tok::identifier);
Argyrios Kyrtzidis4383e182010-11-16 18:18:13 +00002246 isInvalid = true;
2247 } else {
2248 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec,
2249 DiagID);
2250 }
Chris Lattner80d0c892009-01-21 19:48:37 +00002251 break;
2252 case tok::kw__Decimal32:
John McCallfec54012009-08-03 20:12:06 +00002253 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
2254 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002255 break;
2256 case tok::kw__Decimal64:
John McCallfec54012009-08-03 20:12:06 +00002257 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
2258 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002259 break;
2260 case tok::kw__Decimal128:
John McCallfec54012009-08-03 20:12:06 +00002261 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
2262 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002263 break;
John Thompson82287d12010-02-05 00:12:22 +00002264 case tok::kw___vector:
2265 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
2266 break;
2267 case tok::kw___pixel:
2268 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
2269 break;
John McCalla5fc4722011-04-09 22:50:59 +00002270 case tok::kw___unknown_anytype:
2271 isInvalid = DS.SetTypeSpecType(TST_unknown_anytype, Loc,
2272 PrevSpec, DiagID);
2273 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002274
2275 // class-specifier:
2276 case tok::kw_class:
2277 case tok::kw_struct:
Chris Lattner4c97d762009-04-12 21:49:30 +00002278 case tok::kw_union: {
2279 tok::TokenKind Kind = Tok.getKind();
2280 ConsumeToken();
Douglas Gregorefaa93a2011-11-07 17:33:42 +00002281 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS, EnteringContext);
Chris Lattner80d0c892009-01-21 19:48:37 +00002282 continue;
Chris Lattner4c97d762009-04-12 21:49:30 +00002283 }
Chris Lattner80d0c892009-01-21 19:48:37 +00002284
2285 // enum-specifier:
2286 case tok::kw_enum:
Chris Lattner4c97d762009-04-12 21:49:30 +00002287 ConsumeToken();
Douglas Gregor9b9edd62010-03-02 17:53:14 +00002288 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS);
Chris Lattner80d0c892009-01-21 19:48:37 +00002289 continue;
2290
2291 // cv-qualifier:
2292 case tok::kw_const:
John McCallfec54012009-08-03 20:12:06 +00002293 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec, DiagID,
2294 getLang());
Chris Lattner80d0c892009-01-21 19:48:37 +00002295 break;
2296 case tok::kw_volatile:
John McCallfec54012009-08-03 20:12:06 +00002297 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
2298 getLang());
Chris Lattner80d0c892009-01-21 19:48:37 +00002299 break;
2300 case tok::kw_restrict:
John McCallfec54012009-08-03 20:12:06 +00002301 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
2302 getLang());
Chris Lattner80d0c892009-01-21 19:48:37 +00002303 break;
2304
Douglas Gregord57959a2009-03-27 23:10:48 +00002305 // C++ typename-specifier:
2306 case tok::kw_typename:
John McCall9ba61662010-02-26 08:45:28 +00002307 if (TryAnnotateTypeOrScopeToken()) {
2308 DS.SetTypeSpecError();
2309 goto DoneWithDeclSpec;
2310 }
2311 if (!Tok.is(tok::kw_typename))
Douglas Gregord57959a2009-03-27 23:10:48 +00002312 continue;
2313 break;
2314
Chris Lattner80d0c892009-01-21 19:48:37 +00002315 // GNU typeof support.
2316 case tok::kw_typeof:
2317 ParseTypeofSpecifier(DS);
2318 continue;
2319
David Blaikie42d6d0c2011-12-04 05:04:18 +00002320 case tok::annot_decltype:
Anders Carlsson6fd634f2009-06-24 17:47:40 +00002321 ParseDecltypeSpecifier(DS);
2322 continue;
2323
Sean Huntdb5d44b2011-05-19 05:37:45 +00002324 case tok::kw___underlying_type:
2325 ParseUnderlyingTypeSpecifier(DS);
Eli Friedmanb001de72011-10-06 23:00:33 +00002326 continue;
2327
2328 case tok::kw__Atomic:
2329 ParseAtomicSpecifier(DS);
2330 continue;
Sean Huntdb5d44b2011-05-19 05:37:45 +00002331
Peter Collingbourne207f4d82011-03-18 22:38:29 +00002332 // OpenCL qualifiers:
2333 case tok::kw_private:
2334 if (!getLang().OpenCL)
2335 goto DoneWithDeclSpec;
2336 case tok::kw___private:
2337 case tok::kw___global:
2338 case tok::kw___local:
2339 case tok::kw___constant:
2340 case tok::kw___read_only:
2341 case tok::kw___write_only:
2342 case tok::kw___read_write:
2343 ParseOpenCLQualifiers(DS);
2344 break;
2345
Steve Naroffd3ded1f2008-06-05 00:02:44 +00002346 case tok::less:
Chris Lattner3bd934a2008-07-26 01:18:38 +00002347 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattnerbce61352008-07-26 00:20:22 +00002348 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
2349 // but we support it.
Chris Lattner3bd934a2008-07-26 01:18:38 +00002350 if (DS.hasTypeSpecifier() || !getLang().ObjC1)
Chris Lattnerbce61352008-07-26 00:20:22 +00002351 goto DoneWithDeclSpec;
Mike Stump1eb44332009-09-09 15:08:12 +00002352
Douglas Gregor46f936e2010-11-19 17:10:50 +00002353 if (!ParseObjCProtocolQualifiers(DS))
2354 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
2355 << FixItHint::CreateInsertion(Loc, "id")
2356 << SourceRange(Loc, DS.getSourceRange().getEnd());
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00002357
2358 // Need to support trailing type qualifiers (e.g. "id<p> const").
2359 // If a type specifier follows, it will be diagnosed elsewhere.
2360 continue;
Reid Spencer5f016e22007-07-11 17:01:13 +00002361 }
John McCallfec54012009-08-03 20:12:06 +00002362 // If the specifier wasn't legal, issue a diagnostic.
Reid Spencer5f016e22007-07-11 17:01:13 +00002363 if (isInvalid) {
2364 assert(PrevSpec && "Method did not return previous specifier!");
John McCallfec54012009-08-03 20:12:06 +00002365 assert(DiagID);
Douglas Gregorae2fb142010-08-23 14:34:43 +00002366
2367 if (DiagID == diag::ext_duplicate_declspec)
2368 Diag(Tok, DiagID)
2369 << PrevSpec << FixItHint::CreateRemoval(Tok.getLocation());
2370 else
2371 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00002372 }
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002373
Chris Lattner81c018d2008-03-13 06:29:04 +00002374 DS.SetRangeEnd(Tok.getLocation());
Fariborz Jahaniane106a0b2011-04-19 21:42:37 +00002375 if (DiagID != diag::err_bool_redeclaration)
2376 ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00002377 }
2378}
Douglas Gregoradcac882008-12-01 23:54:00 +00002379
Chris Lattner7a0ab5f2009-01-06 06:59:53 +00002380/// ParseOptionalTypeSpecifier - Try to parse a single type-specifier. We
Douglas Gregor12e083c2008-11-07 15:42:26 +00002381/// primarily follow the C++ grammar with additions for C99 and GNU,
2382/// which together subsume the C grammar. Note that the C++
2383/// type-specifier also includes the C type-qualifier (for const,
2384/// volatile, and C99 restrict). Returns true if a type-specifier was
2385/// found (and parsed), false otherwise.
2386///
2387/// type-specifier: [C++ 7.1.5]
2388/// simple-type-specifier
2389/// class-specifier
2390/// enum-specifier
2391/// elaborated-type-specifier [TODO]
2392/// cv-qualifier
2393///
2394/// cv-qualifier: [C++ 7.1.5.1]
2395/// 'const'
2396/// 'volatile'
2397/// [C99] 'restrict'
2398///
2399/// simple-type-specifier: [ C++ 7.1.5.2]
2400/// '::'[opt] nested-name-specifier[opt] type-name [TODO]
2401/// '::'[opt] nested-name-specifier 'template' template-id [TODO]
2402/// 'char'
2403/// 'wchar_t'
2404/// 'bool'
2405/// 'short'
2406/// 'int'
2407/// 'long'
2408/// 'signed'
2409/// 'unsigned'
2410/// 'float'
2411/// 'double'
2412/// 'void'
2413/// [C99] '_Bool'
2414/// [C99] '_Complex'
2415/// [C99] '_Imaginary' // Removed in TC2?
2416/// [GNU] '_Decimal32'
2417/// [GNU] '_Decimal64'
2418/// [GNU] '_Decimal128'
2419/// [GNU] typeof-specifier
2420/// [OBJC] class-name objc-protocol-refs[opt] [TODO]
2421/// [OBJC] typedef-name objc-protocol-refs[opt] [TODO]
Anders Carlsson6fd634f2009-06-24 17:47:40 +00002422/// [C++0x] 'decltype' ( expression )
John Thompson82287d12010-02-05 00:12:22 +00002423/// [AltiVec] '__vector'
John McCallfec54012009-08-03 20:12:06 +00002424bool Parser::ParseOptionalTypeSpecifier(DeclSpec &DS, bool& isInvalid,
Chris Lattner7a0ab5f2009-01-06 06:59:53 +00002425 const char *&PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00002426 unsigned &DiagID,
Sebastian Redld9bafa72010-02-03 21:21:43 +00002427 const ParsedTemplateInfo &TemplateInfo,
2428 bool SuppressDeclarations) {
Douglas Gregor12e083c2008-11-07 15:42:26 +00002429 SourceLocation Loc = Tok.getLocation();
2430
2431 switch (Tok.getKind()) {
Chris Lattner166a8fc2009-01-04 23:41:41 +00002432 case tok::identifier: // foo::bar
Douglas Gregorc0b39642010-04-15 23:40:53 +00002433 // If we already have a type specifier, this identifier is not a type.
2434 if (DS.getTypeSpecType() != DeclSpec::TST_unspecified ||
2435 DS.getTypeSpecWidth() != DeclSpec::TSW_unspecified ||
2436 DS.getTypeSpecSign() != DeclSpec::TSS_unspecified)
2437 return false;
John Thompson82287d12010-02-05 00:12:22 +00002438 // Check for need to substitute AltiVec keyword tokens.
2439 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
2440 break;
2441 // Fall through.
David Blaikie42d6d0c2011-12-04 05:04:18 +00002442 case tok::kw_decltype:
Douglas Gregord57959a2009-03-27 23:10:48 +00002443 case tok::kw_typename: // typename foo::bar
Chris Lattner166a8fc2009-01-04 23:41:41 +00002444 // Annotate typenames and C++ scope specifiers. If we get one, just
2445 // recurse to handle whatever we get.
Kaelyn Uhrainfac94672011-10-11 01:02:41 +00002446 if (TryAnnotateTypeOrScopeToken(/*EnteringContext=*/false,
2447 /*NeedType=*/true))
John McCall9ba61662010-02-26 08:45:28 +00002448 return true;
2449 if (Tok.is(tok::identifier))
2450 return false;
2451 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
2452 TemplateInfo, SuppressDeclarations);
Chris Lattner166a8fc2009-01-04 23:41:41 +00002453 case tok::coloncolon: // ::foo::bar
2454 if (NextToken().is(tok::kw_new) || // ::new
2455 NextToken().is(tok::kw_delete)) // ::delete
2456 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002457
Chris Lattner166a8fc2009-01-04 23:41:41 +00002458 // Annotate typenames and C++ scope specifiers. If we get one, just
2459 // recurse to handle whatever we get.
Kaelyn Uhrainfac94672011-10-11 01:02:41 +00002460 if (TryAnnotateTypeOrScopeToken(/*EnteringContext=*/false,
2461 /*NeedType=*/true))
John McCall9ba61662010-02-26 08:45:28 +00002462 return true;
2463 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
2464 TemplateInfo, SuppressDeclarations);
Mike Stump1eb44332009-09-09 15:08:12 +00002465
Douglas Gregor12e083c2008-11-07 15:42:26 +00002466 // simple-type-specifier:
Chris Lattnerb31757b2009-01-06 05:06:21 +00002467 case tok::annot_typename: {
John McCallb3d87482010-08-24 05:47:05 +00002468 if (ParsedType T = getTypeAnnotation(Tok)) {
Nico Weber253e80b2010-11-22 10:30:56 +00002469 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename,
2470 Tok.getAnnotationEndLoc(), PrevSpec,
John McCallb3d87482010-08-24 05:47:05 +00002471 DiagID, T);
2472 } else
Douglas Gregor31a19b62009-04-01 21:51:26 +00002473 DS.SetTypeSpecError();
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002474 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
2475 ConsumeToken(); // The typename
Mike Stump1eb44332009-09-09 15:08:12 +00002476
Douglas Gregor12e083c2008-11-07 15:42:26 +00002477 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
2478 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
2479 // Objective-C interface. If we don't have Objective-C or a '<', this is
2480 // just a normal reference to a typedef name.
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00002481 if (Tok.is(tok::less) && getLang().ObjC1)
2482 ParseObjCProtocolQualifiers(DS);
2483
Douglas Gregor12e083c2008-11-07 15:42:26 +00002484 return true;
2485 }
2486
2487 case tok::kw_short:
John McCallfec54012009-08-03 20:12:06 +00002488 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002489 break;
2490 case tok::kw_long:
2491 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCallfec54012009-08-03 20:12:06 +00002492 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
2493 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002494 else
John McCallfec54012009-08-03 20:12:06 +00002495 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2496 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002497 break;
Francois Pichet338d7f72011-04-28 01:59:37 +00002498 case tok::kw___int64:
2499 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2500 DiagID);
2501 break;
Douglas Gregor12e083c2008-11-07 15:42:26 +00002502 case tok::kw_signed:
John McCallfec54012009-08-03 20:12:06 +00002503 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002504 break;
2505 case tok::kw_unsigned:
John McCallfec54012009-08-03 20:12:06 +00002506 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
2507 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002508 break;
2509 case tok::kw__Complex:
John McCallfec54012009-08-03 20:12:06 +00002510 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
2511 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002512 break;
2513 case tok::kw__Imaginary:
John McCallfec54012009-08-03 20:12:06 +00002514 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
2515 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002516 break;
2517 case tok::kw_void:
John McCallfec54012009-08-03 20:12:06 +00002518 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002519 break;
2520 case tok::kw_char:
John McCallfec54012009-08-03 20:12:06 +00002521 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002522 break;
2523 case tok::kw_int:
John McCallfec54012009-08-03 20:12:06 +00002524 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002525 break;
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00002526 case tok::kw_half:
2527 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_half, Loc, PrevSpec, DiagID);
2528 break;
Douglas Gregor12e083c2008-11-07 15:42:26 +00002529 case tok::kw_float:
John McCallfec54012009-08-03 20:12:06 +00002530 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002531 break;
2532 case tok::kw_double:
John McCallfec54012009-08-03 20:12:06 +00002533 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002534 break;
2535 case tok::kw_wchar_t:
John McCallfec54012009-08-03 20:12:06 +00002536 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002537 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002538 case tok::kw_char16_t:
John McCallfec54012009-08-03 20:12:06 +00002539 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002540 break;
2541 case tok::kw_char32_t:
John McCallfec54012009-08-03 20:12:06 +00002542 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002543 break;
Douglas Gregor12e083c2008-11-07 15:42:26 +00002544 case tok::kw_bool:
2545 case tok::kw__Bool:
John McCallfec54012009-08-03 20:12:06 +00002546 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002547 break;
2548 case tok::kw__Decimal32:
John McCallfec54012009-08-03 20:12:06 +00002549 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
2550 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002551 break;
2552 case tok::kw__Decimal64:
John McCallfec54012009-08-03 20:12:06 +00002553 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
2554 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002555 break;
2556 case tok::kw__Decimal128:
John McCallfec54012009-08-03 20:12:06 +00002557 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
2558 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002559 break;
John Thompson82287d12010-02-05 00:12:22 +00002560 case tok::kw___vector:
2561 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
2562 break;
2563 case tok::kw___pixel:
2564 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
2565 break;
2566
Douglas Gregor12e083c2008-11-07 15:42:26 +00002567 // class-specifier:
2568 case tok::kw_class:
2569 case tok::kw_struct:
Chris Lattner4c97d762009-04-12 21:49:30 +00002570 case tok::kw_union: {
2571 tok::TokenKind Kind = Tok.getKind();
2572 ConsumeToken();
Sebastian Redld9bafa72010-02-03 21:21:43 +00002573 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS_none,
Douglas Gregorefaa93a2011-11-07 17:33:42 +00002574 /*EnteringContext=*/false,
Sebastian Redld9bafa72010-02-03 21:21:43 +00002575 SuppressDeclarations);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002576 return true;
Chris Lattner4c97d762009-04-12 21:49:30 +00002577 }
Douglas Gregor12e083c2008-11-07 15:42:26 +00002578
2579 // enum-specifier:
2580 case tok::kw_enum:
Chris Lattner4c97d762009-04-12 21:49:30 +00002581 ConsumeToken();
Douglas Gregor9b9edd62010-03-02 17:53:14 +00002582 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS_none);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002583 return true;
2584
2585 // cv-qualifier:
2586 case tok::kw_const:
2587 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00002588 DiagID, getLang());
Douglas Gregor12e083c2008-11-07 15:42:26 +00002589 break;
2590 case tok::kw_volatile:
2591 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00002592 DiagID, getLang());
Douglas Gregor12e083c2008-11-07 15:42:26 +00002593 break;
2594 case tok::kw_restrict:
2595 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00002596 DiagID, getLang());
Douglas Gregor12e083c2008-11-07 15:42:26 +00002597 break;
2598
2599 // GNU typeof support.
2600 case tok::kw_typeof:
2601 ParseTypeofSpecifier(DS);
2602 return true;
2603
Anders Carlsson6fd634f2009-06-24 17:47:40 +00002604 // C++0x decltype support.
David Blaikie42d6d0c2011-12-04 05:04:18 +00002605 case tok::annot_decltype:
Anders Carlsson6fd634f2009-06-24 17:47:40 +00002606 ParseDecltypeSpecifier(DS);
2607 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00002608
Sean Huntdb5d44b2011-05-19 05:37:45 +00002609 // C++0x type traits support.
2610 case tok::kw___underlying_type:
2611 ParseUnderlyingTypeSpecifier(DS);
2612 return true;
2613
Eli Friedmanb001de72011-10-06 23:00:33 +00002614 case tok::kw__Atomic:
2615 ParseAtomicSpecifier(DS);
2616 return true;
2617
Peter Collingbourne207f4d82011-03-18 22:38:29 +00002618 // OpenCL qualifiers:
2619 case tok::kw_private:
2620 if (!getLang().OpenCL)
2621 return false;
2622 case tok::kw___private:
2623 case tok::kw___global:
2624 case tok::kw___local:
2625 case tok::kw___constant:
2626 case tok::kw___read_only:
2627 case tok::kw___write_only:
2628 case tok::kw___read_write:
2629 ParseOpenCLQualifiers(DS);
2630 break;
2631
Anders Carlsson0b7f7892009-06-26 23:44:14 +00002632 // C++0x auto support.
2633 case tok::kw_auto:
Richard Smith87e96eb2011-09-04 20:24:20 +00002634 // This is only called in situations where a storage-class specifier is
2635 // illegal, so we can assume an auto type specifier was intended even in
2636 // C++98. In C++98 mode, DeclSpec::Finish will produce an appropriate
2637 // extension diagnostic.
2638 if (!getLang().CPlusPlus)
Anders Carlsson0b7f7892009-06-26 23:44:14 +00002639 return false;
2640
John McCallfec54012009-08-03 20:12:06 +00002641 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec, DiagID);
Anders Carlsson0b7f7892009-06-26 23:44:14 +00002642 break;
Dawn Perchik52fc3142010-09-03 01:29:35 +00002643
Eli Friedman290eeb02009-06-08 23:27:34 +00002644 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00002645 case tok::kw___ptr32:
Eli Friedman290eeb02009-06-08 23:27:34 +00002646 case tok::kw___w64:
Steve Naroff239f0732008-12-25 14:16:32 +00002647 case tok::kw___cdecl:
2648 case tok::kw___stdcall:
2649 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00002650 case tok::kw___thiscall:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00002651 case tok::kw___unaligned:
John McCall7f040a92010-12-24 02:08:15 +00002652 ParseMicrosoftTypeAttributes(DS.getAttributes());
Chris Lattner837acd02009-01-21 19:19:26 +00002653 return true;
Steve Naroff239f0732008-12-25 14:16:32 +00002654
Dawn Perchik52fc3142010-09-03 01:29:35 +00002655 case tok::kw___pascal:
John McCall7f040a92010-12-24 02:08:15 +00002656 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik52fc3142010-09-03 01:29:35 +00002657 return true;
2658
Douglas Gregor12e083c2008-11-07 15:42:26 +00002659 default:
2660 // Not a type-specifier; do nothing.
2661 return false;
2662 }
2663
2664 // If the specifier combination wasn't legal, issue a diagnostic.
2665 if (isInvalid) {
2666 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner1ab3b962008-11-18 07:48:38 +00002667 // Pick between error or extwarn.
Chris Lattner1ab3b962008-11-18 07:48:38 +00002668 Diag(Tok, DiagID) << PrevSpec;
Douglas Gregor12e083c2008-11-07 15:42:26 +00002669 }
2670 DS.SetRangeEnd(Tok.getLocation());
2671 ConsumeToken(); // whatever we parsed above.
2672 return true;
2673}
Reid Spencer5f016e22007-07-11 17:01:13 +00002674
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002675/// ParseStructDeclaration - Parse a struct declaration without the terminating
2676/// semicolon.
2677///
Reid Spencer5f016e22007-07-11 17:01:13 +00002678/// struct-declaration:
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002679/// specifier-qualifier-list struct-declarator-list
Reid Spencer5f016e22007-07-11 17:01:13 +00002680/// [GNU] __extension__ struct-declaration
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002681/// [GNU] specifier-qualifier-list
Reid Spencer5f016e22007-07-11 17:01:13 +00002682/// struct-declarator-list:
2683/// struct-declarator
2684/// struct-declarator-list ',' struct-declarator
2685/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
2686/// struct-declarator:
2687/// declarator
2688/// [GNU] declarator attributes[opt]
2689/// declarator[opt] ':' constant-expression
2690/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
2691///
Chris Lattnere1359422008-04-10 06:46:29 +00002692void Parser::
John McCallbdd563e2009-11-03 02:38:08 +00002693ParseStructDeclaration(DeclSpec &DS, FieldCallback &Fields) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002694
Chris Lattnerc46d1a12008-10-20 06:45:43 +00002695 if (Tok.is(tok::kw___extension__)) {
2696 // __extension__ silences extension warnings in the subexpression.
2697 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroff28a7ca82007-08-20 22:28:22 +00002698 ConsumeToken();
Chris Lattnerc46d1a12008-10-20 06:45:43 +00002699 return ParseStructDeclaration(DS, Fields);
2700 }
Mike Stump1eb44332009-09-09 15:08:12 +00002701
Steve Naroff28a7ca82007-08-20 22:28:22 +00002702 // Parse the common specifier-qualifiers-list piece.
Steve Naroff28a7ca82007-08-20 22:28:22 +00002703 ParseSpecifierQualifierList(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00002704
Douglas Gregor4920f1f2009-01-12 22:49:06 +00002705 // If there are no declarators, this is a free-standing declaration
2706 // specifier. Let the actions module cope with it.
Chris Lattner04d66662007-10-09 17:33:22 +00002707 if (Tok.is(tok::semi)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00002708 Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none, DS);
Steve Naroff28a7ca82007-08-20 22:28:22 +00002709 return;
2710 }
2711
2712 // Read struct-declarators until we find the semicolon.
John McCallbdd563e2009-11-03 02:38:08 +00002713 bool FirstDeclarator = true;
Richard Smith7984de32012-01-12 23:53:29 +00002714 SourceLocation CommaLoc;
Steve Naroff28a7ca82007-08-20 22:28:22 +00002715 while (1) {
John McCall54abf7d2009-11-04 02:18:39 +00002716 ParsingDeclRAIIObject PD(*this);
John McCallbdd563e2009-11-03 02:38:08 +00002717 FieldDeclarator DeclaratorInfo(DS);
Richard Smith7984de32012-01-12 23:53:29 +00002718 DeclaratorInfo.D.setCommaLoc(CommaLoc);
John McCallbdd563e2009-11-03 02:38:08 +00002719
2720 // Attributes are only allowed here on successive declarators.
John McCall7f040a92010-12-24 02:08:15 +00002721 if (!FirstDeclarator)
2722 MaybeParseGNUAttributes(DeclaratorInfo.D);
Mike Stump1eb44332009-09-09 15:08:12 +00002723
Steve Naroff28a7ca82007-08-20 22:28:22 +00002724 /// struct-declarator: declarator
2725 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattnera1efc8c2009-12-10 01:59:24 +00002726 if (Tok.isNot(tok::colon)) {
2727 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
2728 ColonProtectionRAIIObject X(*this);
Chris Lattnere1359422008-04-10 06:46:29 +00002729 ParseDeclarator(DeclaratorInfo.D);
Chris Lattnera1efc8c2009-12-10 01:59:24 +00002730 }
Mike Stump1eb44332009-09-09 15:08:12 +00002731
Chris Lattner04d66662007-10-09 17:33:22 +00002732 if (Tok.is(tok::colon)) {
Steve Naroff28a7ca82007-08-20 22:28:22 +00002733 ConsumeToken();
John McCall60d7b3a2010-08-24 06:29:42 +00002734 ExprResult Res(ParseConstantExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002735 if (Res.isInvalid())
Steve Naroff28a7ca82007-08-20 22:28:22 +00002736 SkipUntil(tok::semi, true, true);
Chris Lattner60b1e3e2008-04-10 06:15:14 +00002737 else
Sebastian Redleffa8d12008-12-10 00:02:53 +00002738 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroff28a7ca82007-08-20 22:28:22 +00002739 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00002740
Steve Naroff28a7ca82007-08-20 22:28:22 +00002741 // If attributes exist after the declarator, parse them.
John McCall7f040a92010-12-24 02:08:15 +00002742 MaybeParseGNUAttributes(DeclaratorInfo.D);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002743
John McCallbdd563e2009-11-03 02:38:08 +00002744 // We're done with this declarator; invoke the callback.
John McCalld226f652010-08-21 09:40:31 +00002745 Decl *D = Fields.invoke(DeclaratorInfo);
John McCall54abf7d2009-11-04 02:18:39 +00002746 PD.complete(D);
John McCallbdd563e2009-11-03 02:38:08 +00002747
Steve Naroff28a7ca82007-08-20 22:28:22 +00002748 // If we don't have a comma, it is either the end of the list (a ';')
2749 // or an error, bail out.
Chris Lattner04d66662007-10-09 17:33:22 +00002750 if (Tok.isNot(tok::comma))
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002751 return;
Sebastian Redlab197ba2009-02-09 18:23:29 +00002752
Steve Naroff28a7ca82007-08-20 22:28:22 +00002753 // Consume the comma.
Richard Smith7984de32012-01-12 23:53:29 +00002754 CommaLoc = ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00002755
John McCallbdd563e2009-11-03 02:38:08 +00002756 FirstDeclarator = false;
Steve Naroff28a7ca82007-08-20 22:28:22 +00002757 }
Steve Naroff28a7ca82007-08-20 22:28:22 +00002758}
2759
2760/// ParseStructUnionBody
2761/// struct-contents:
2762/// struct-declaration-list
2763/// [EXT] empty
2764/// [GNU] "struct-declaration-list" without terminatoring ';'
2765/// struct-declaration-list:
2766/// struct-declaration
2767/// struct-declaration-list struct-declaration
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002768/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroff28a7ca82007-08-20 22:28:22 +00002769///
Reid Spencer5f016e22007-07-11 17:01:13 +00002770void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
John McCalld226f652010-08-21 09:40:31 +00002771 unsigned TagType, Decl *TagDecl) {
John McCallf312b1e2010-08-26 23:41:50 +00002772 PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc,
2773 "parsing struct/union body");
Mike Stump1eb44332009-09-09 15:08:12 +00002774
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002775 BalancedDelimiterTracker T(*this, tok::l_brace);
2776 if (T.consumeOpen())
2777 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002778
Douglas Gregor3218c4b2009-01-09 22:42:13 +00002779 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
Douglas Gregor23c94db2010-07-02 17:43:08 +00002780 Actions.ActOnTagStartDefinition(getCurScope(), TagDecl);
Douglas Gregor72de6672009-01-08 20:45:30 +00002781
Reid Spencer5f016e22007-07-11 17:01:13 +00002782 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
2783 // C++.
Richard Smithd7c56e12011-12-29 21:57:33 +00002784 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus) {
2785 Diag(Tok, diag::ext_empty_struct_union) << (TagType == TST_union);
2786 Diag(Tok, diag::warn_empty_struct_union_compat) << (TagType == TST_union);
2787 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002788
Chris Lattner5f9e2722011-07-23 10:55:15 +00002789 SmallVector<Decl *, 32> FieldDecls;
Chris Lattnere1359422008-04-10 06:46:29 +00002790
Reid Spencer5f016e22007-07-11 17:01:13 +00002791 // While we still have something to read, read the declarations in the struct.
Chris Lattner04d66662007-10-09 17:33:22 +00002792 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002793 // Each iteration of this loop reads one struct-declaration.
Mike Stump1eb44332009-09-09 15:08:12 +00002794
Reid Spencer5f016e22007-07-11 17:01:13 +00002795 // Check for extraneous top-level semicolon.
Chris Lattner04d66662007-10-09 17:33:22 +00002796 if (Tok.is(tok::semi)) {
Douglas Gregor9b3064b2009-04-01 22:41:11 +00002797 Diag(Tok, diag::ext_extra_struct_semi)
Douglas Gregorf13ca062010-06-16 23:08:59 +00002798 << DeclSpec::getSpecifierName((DeclSpec::TST)TagType)
Douglas Gregor849b2432010-03-31 17:46:05 +00002799 << FixItHint::CreateRemoval(Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00002800 ConsumeToken();
2801 continue;
2802 }
Chris Lattnere1359422008-04-10 06:46:29 +00002803
2804 // Parse all the comma separated declarators.
John McCall0b7e6782011-03-24 11:26:52 +00002805 DeclSpec DS(AttrFactory);
Mike Stump1eb44332009-09-09 15:08:12 +00002806
John McCallbdd563e2009-11-03 02:38:08 +00002807 if (!Tok.is(tok::at)) {
2808 struct CFieldCallback : FieldCallback {
2809 Parser &P;
John McCalld226f652010-08-21 09:40:31 +00002810 Decl *TagDecl;
Chris Lattner5f9e2722011-07-23 10:55:15 +00002811 SmallVectorImpl<Decl *> &FieldDecls;
John McCallbdd563e2009-11-03 02:38:08 +00002812
John McCalld226f652010-08-21 09:40:31 +00002813 CFieldCallback(Parser &P, Decl *TagDecl,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002814 SmallVectorImpl<Decl *> &FieldDecls) :
John McCallbdd563e2009-11-03 02:38:08 +00002815 P(P), TagDecl(TagDecl), FieldDecls(FieldDecls) {}
2816
John McCalld226f652010-08-21 09:40:31 +00002817 virtual Decl *invoke(FieldDeclarator &FD) {
John McCallbdd563e2009-11-03 02:38:08 +00002818 // Install the declarator into the current TagDecl.
John McCalld226f652010-08-21 09:40:31 +00002819 Decl *Field = P.Actions.ActOnField(P.getCurScope(), TagDecl,
John McCall4ba39712009-11-03 21:13:47 +00002820 FD.D.getDeclSpec().getSourceRange().getBegin(),
2821 FD.D, FD.BitfieldSize);
John McCallbdd563e2009-11-03 02:38:08 +00002822 FieldDecls.push_back(Field);
2823 return Field;
Douglas Gregor91a28862009-08-26 14:27:30 +00002824 }
John McCallbdd563e2009-11-03 02:38:08 +00002825 } Callback(*this, TagDecl, FieldDecls);
2826
2827 ParseStructDeclaration(DS, Callback);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002828 } else { // Handle @defs
2829 ConsumeToken();
2830 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
2831 Diag(Tok, diag::err_unexpected_at);
Chris Lattner3e156ad2010-02-02 00:37:27 +00002832 SkipUntil(tok::semi, true);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002833 continue;
2834 }
2835 ConsumeToken();
2836 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
2837 if (!Tok.is(tok::identifier)) {
2838 Diag(Tok, diag::err_expected_ident);
Chris Lattner3e156ad2010-02-02 00:37:27 +00002839 SkipUntil(tok::semi, true);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002840 continue;
2841 }
Chris Lattner5f9e2722011-07-23 10:55:15 +00002842 SmallVector<Decl *, 16> Fields;
Douglas Gregor23c94db2010-07-02 17:43:08 +00002843 Actions.ActOnDefs(getCurScope(), TagDecl, Tok.getLocation(),
Douglas Gregor44b43212008-12-11 16:49:14 +00002844 Tok.getIdentifierInfo(), Fields);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002845 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
2846 ConsumeToken();
2847 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
Mike Stump1eb44332009-09-09 15:08:12 +00002848 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002849
Chris Lattner04d66662007-10-09 17:33:22 +00002850 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002851 ConsumeToken();
Chris Lattner04d66662007-10-09 17:33:22 +00002852 } else if (Tok.is(tok::r_brace)) {
Chris Lattner3e156ad2010-02-02 00:37:27 +00002853 ExpectAndConsume(tok::semi, diag::ext_expected_semi_decl_list);
Reid Spencer5f016e22007-07-11 17:01:13 +00002854 break;
2855 } else {
Chris Lattner3e156ad2010-02-02 00:37:27 +00002856 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
2857 // Skip to end of block or statement to avoid ext-warning on extra ';'.
Reid Spencer5f016e22007-07-11 17:01:13 +00002858 SkipUntil(tok::r_brace, true, true);
Chris Lattner3e156ad2010-02-02 00:37:27 +00002859 // If we stopped at a ';', eat it.
2860 if (Tok.is(tok::semi)) ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00002861 }
2862 }
Mike Stump1eb44332009-09-09 15:08:12 +00002863
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002864 T.consumeClose();
Mike Stump1eb44332009-09-09 15:08:12 +00002865
John McCall0b7e6782011-03-24 11:26:52 +00002866 ParsedAttributes attrs(AttrFactory);
Reid Spencer5f016e22007-07-11 17:01:13 +00002867 // If attributes exist after struct contents, parse them.
John McCall7f040a92010-12-24 02:08:15 +00002868 MaybeParseGNUAttributes(attrs);
Daniel Dunbar1bfe1c22008-10-03 02:03:53 +00002869
Douglas Gregor23c94db2010-07-02 17:43:08 +00002870 Actions.ActOnFields(getCurScope(),
David Blaikie77b6de02011-09-22 02:58:26 +00002871 RecordLoc, TagDecl, FieldDecls,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002872 T.getOpenLocation(), T.getCloseLocation(),
John McCall7f040a92010-12-24 02:08:15 +00002873 attrs.getList());
Douglas Gregor72de6672009-01-08 20:45:30 +00002874 StructScope.Exit();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002875 Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl,
2876 T.getCloseLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00002877}
2878
Reid Spencer5f016e22007-07-11 17:01:13 +00002879/// ParseEnumSpecifier
2880/// enum-specifier: [C99 6.7.2.2]
2881/// 'enum' identifier[opt] '{' enumerator-list '}'
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002882///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Reid Spencer5f016e22007-07-11 17:01:13 +00002883/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
2884/// '}' attributes[opt]
Aaron Ballman6454a022012-03-01 04:09:28 +00002885/// [MS] 'enum' __declspec[opt] identifier[opt] '{' enumerator-list ',' [opt]
2886/// '}'
Reid Spencer5f016e22007-07-11 17:01:13 +00002887/// 'enum' identifier
2888/// [GNU] 'enum' attributes[opt] identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002889///
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002890/// [C++0x] enum-head '{' enumerator-list[opt] '}'
2891/// [C++0x] enum-head '{' enumerator-list ',' '}'
2892///
2893/// enum-head: [C++0x]
2894/// enum-key attributes[opt] identifier[opt] enum-base[opt]
2895/// enum-key attributes[opt] nested-name-specifier identifier enum-base[opt]
2896///
2897/// enum-key: [C++0x]
2898/// 'enum'
2899/// 'enum' 'class'
2900/// 'enum' 'struct'
2901///
2902/// enum-base: [C++0x]
2903/// ':' type-specifier-seq
2904///
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002905/// [C++] elaborated-type-specifier:
2906/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
2907///
Chris Lattner4c97d762009-04-12 21:49:30 +00002908void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregor9b9edd62010-03-02 17:53:14 +00002909 const ParsedTemplateInfo &TemplateInfo,
Chris Lattner4c97d762009-04-12 21:49:30 +00002910 AccessSpecifier AS) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002911 // Parse the tag portion of this.
Douglas Gregor374929f2009-09-18 15:37:17 +00002912 if (Tok.is(tok::code_completion)) {
2913 // Code completion for an enum name.
Douglas Gregor23c94db2010-07-02 17:43:08 +00002914 Actions.CodeCompleteTag(getCurScope(), DeclSpec::TST_enum);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002915 return cutOffParsing();
Douglas Gregor374929f2009-09-18 15:37:17 +00002916 }
John McCall57c13002011-07-06 05:58:41 +00002917
Richard Smithbdad7a22012-01-10 01:33:14 +00002918 SourceLocation ScopedEnumKWLoc;
John McCall57c13002011-07-06 05:58:41 +00002919 bool IsScopedUsingClassTag = false;
2920
2921 if (getLang().CPlusPlus0x &&
2922 (Tok.is(tok::kw_class) || Tok.is(tok::kw_struct))) {
Richard Smith7fe62082011-10-15 05:09:34 +00002923 Diag(Tok, diag::warn_cxx98_compat_scoped_enum);
John McCall57c13002011-07-06 05:58:41 +00002924 IsScopedUsingClassTag = Tok.is(tok::kw_class);
Richard Smithbdad7a22012-01-10 01:33:14 +00002925 ScopedEnumKWLoc = ConsumeToken();
John McCall57c13002011-07-06 05:58:41 +00002926 }
Douglas Gregor374929f2009-09-18 15:37:17 +00002927
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002928 // If attributes exist after tag, parse them.
John McCall0b7e6782011-03-24 11:26:52 +00002929 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00002930 MaybeParseGNUAttributes(attrs);
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002931
Aaron Ballman6454a022012-03-01 04:09:28 +00002932 // If declspecs exist after tag, parse them.
2933 while (Tok.is(tok::kw___declspec))
2934 ParseMicrosoftDeclSpec(attrs);
2935
Douglas Gregor5471bc82011-09-08 17:18:35 +00002936 bool AllowFixedUnderlyingType
Francois Pichet62ec1f22011-09-17 17:15:52 +00002937 = getLang().CPlusPlus0x || getLang().MicrosoftExt || getLang().ObjC2;
John McCall57c13002011-07-06 05:58:41 +00002938
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00002939 CXXScopeSpec &SS = DS.getTypeSpecScope();
John McCall9ba61662010-02-26 08:45:28 +00002940 if (getLang().CPlusPlus) {
John McCall57c13002011-07-06 05:58:41 +00002941 // "enum foo : bar;" is not a potential typo for "enum foo::bar;"
2942 // if a fixed underlying type is allowed.
2943 ColonProtectionRAIIObject X(*this, AllowFixedUnderlyingType);
2944
Douglas Gregorefaa93a2011-11-07 17:33:42 +00002945 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
2946 /*EnteringContext=*/false))
John McCall9ba61662010-02-26 08:45:28 +00002947 return;
2948
2949 if (SS.isSet() && Tok.isNot(tok::identifier)) {
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002950 Diag(Tok, diag::err_expected_ident);
2951 if (Tok.isNot(tok::l_brace)) {
2952 // Has no name and is not a definition.
2953 // Skip the rest of this declarator, up until the comma or semicolon.
2954 SkipUntil(tok::comma, true);
2955 return;
2956 }
2957 }
2958 }
Mike Stump1eb44332009-09-09 15:08:12 +00002959
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002960 // Must have either 'enum name' or 'enum {...}'.
Douglas Gregorb9075602011-02-22 02:55:24 +00002961 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace) &&
2962 (AllowFixedUnderlyingType && Tok.isNot(tok::colon))) {
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002963 Diag(Tok, diag::err_expected_ident_lbrace);
Mike Stump1eb44332009-09-09 15:08:12 +00002964
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002965 // Skip the rest of this declarator, up until the comma or semicolon.
2966 SkipUntil(tok::comma, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00002967 return;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002968 }
Mike Stump1eb44332009-09-09 15:08:12 +00002969
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002970 // If an identifier is present, consume and remember it.
2971 IdentifierInfo *Name = 0;
2972 SourceLocation NameLoc;
2973 if (Tok.is(tok::identifier)) {
2974 Name = Tok.getIdentifierInfo();
2975 NameLoc = ConsumeToken();
2976 }
Mike Stump1eb44332009-09-09 15:08:12 +00002977
Richard Smithbdad7a22012-01-10 01:33:14 +00002978 if (!Name && ScopedEnumKWLoc.isValid()) {
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002979 // C++0x 7.2p2: The optional identifier shall not be omitted in the
2980 // declaration of a scoped enumeration.
2981 Diag(Tok, diag::err_scoped_enum_missing_identifier);
Richard Smithbdad7a22012-01-10 01:33:14 +00002982 ScopedEnumKWLoc = SourceLocation();
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00002983 IsScopedUsingClassTag = false;
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002984 }
2985
2986 TypeResult BaseType;
2987
Douglas Gregora61b3e72010-12-01 17:42:47 +00002988 // Parse the fixed underlying type.
Douglas Gregorb9075602011-02-22 02:55:24 +00002989 if (AllowFixedUnderlyingType && Tok.is(tok::colon)) {
Douglas Gregora61b3e72010-12-01 17:42:47 +00002990 bool PossibleBitfield = false;
2991 if (getCurScope()->getFlags() & Scope::ClassScope) {
2992 // If we're in class scope, this can either be an enum declaration with
2993 // an underlying type, or a declaration of a bitfield member. We try to
2994 // use a simple disambiguation scheme first to catch the common cases
2995 // (integer literal, sizeof); if it's still ambiguous, we then consider
2996 // anything that's a simple-type-specifier followed by '(' as an
2997 // expression. This suffices because function types are not valid
2998 // underlying types anyway.
2999 TPResult TPR = isExpressionOrTypeSpecifierSimple(NextToken().getKind());
3000 // If the next token starts an expression, we know we're parsing a
3001 // bit-field. This is the common case.
3002 if (TPR == TPResult::True())
3003 PossibleBitfield = true;
3004 // If the next token starts a type-specifier-seq, it may be either a
3005 // a fixed underlying type or the start of a function-style cast in C++;
3006 // lookahead one more token to see if it's obvious that we have a
3007 // fixed underlying type.
3008 else if (TPR == TPResult::False() &&
3009 GetLookAheadToken(2).getKind() == tok::semi) {
3010 // Consume the ':'.
3011 ConsumeToken();
3012 } else {
3013 // We have the start of a type-specifier-seq, so we have to perform
3014 // tentative parsing to determine whether we have an expression or a
3015 // type.
3016 TentativeParsingAction TPA(*this);
3017
3018 // Consume the ':'.
3019 ConsumeToken();
Richard Smithd81e9612012-02-23 01:36:12 +00003020
3021 // If we see a type specifier followed by an open-brace, we have an
3022 // ambiguity between an underlying type and a C++11 braced
3023 // function-style cast. Resolve this by always treating it as an
3024 // underlying type.
3025 // FIXME: The standard is not entirely clear on how to disambiguate in
3026 // this case.
3027 if ((getLang().CPlusPlus &&
3028 isCXXDeclarationSpecifier(TPResult::True()) != TPResult::True()) ||
Douglas Gregor86f208c2011-02-22 20:32:04 +00003029 (!getLang().CPlusPlus && !isDeclarationSpecifier(true))) {
Douglas Gregora61b3e72010-12-01 17:42:47 +00003030 // We'll parse this as a bitfield later.
3031 PossibleBitfield = true;
3032 TPA.Revert();
3033 } else {
3034 // We have a type-specifier-seq.
3035 TPA.Commit();
3036 }
3037 }
3038 } else {
3039 // Consume the ':'.
3040 ConsumeToken();
3041 }
3042
3043 if (!PossibleBitfield) {
3044 SourceRange Range;
3045 BaseType = ParseTypeName(&Range);
Douglas Gregor86f208c2011-02-22 20:32:04 +00003046
Douglas Gregor5471bc82011-09-08 17:18:35 +00003047 if (!getLang().CPlusPlus0x && !getLang().ObjC2)
Douglas Gregor86f208c2011-02-22 20:32:04 +00003048 Diag(StartLoc, diag::ext_ms_enum_fixed_underlying_type)
3049 << Range;
Richard Smith7fe62082011-10-15 05:09:34 +00003050 if (getLang().CPlusPlus0x)
3051 Diag(StartLoc, diag::warn_cxx98_compat_enum_fixed_underlying_type);
Douglas Gregora61b3e72010-12-01 17:42:47 +00003052 }
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003053 }
3054
Richard Smithbdad7a22012-01-10 01:33:14 +00003055 // There are four options here. If we have 'friend enum foo;' then this is a
3056 // friend declaration, and cannot have an accompanying definition. If we have
3057 // 'enum foo;', then this is a forward declaration. If we have
3058 // 'enum foo {...' then this is a definition. Otherwise we have something
3059 // like 'enum foo xyz', a reference.
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00003060 //
3061 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
3062 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
3063 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
3064 //
John McCallf312b1e2010-08-26 23:41:50 +00003065 Sema::TagUseKind TUK;
Richard Smithbdad7a22012-01-10 01:33:14 +00003066 if (DS.isFriendSpecified())
3067 TUK = Sema::TUK_Friend;
3068 else if (Tok.is(tok::l_brace))
John McCallf312b1e2010-08-26 23:41:50 +00003069 TUK = Sema::TUK_Definition;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00003070 else if (Tok.is(tok::semi))
John McCallf312b1e2010-08-26 23:41:50 +00003071 TUK = Sema::TUK_Declaration;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00003072 else
John McCallf312b1e2010-08-26 23:41:50 +00003073 TUK = Sema::TUK_Reference;
Douglas Gregor8fc6d232010-05-03 17:48:54 +00003074
3075 // enums cannot be templates, although they can be referenced from a
3076 // template.
3077 if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate &&
John McCallf312b1e2010-08-26 23:41:50 +00003078 TUK != Sema::TUK_Reference) {
Douglas Gregor8fc6d232010-05-03 17:48:54 +00003079 Diag(Tok, diag::err_enum_template);
3080
3081 // Skip the rest of this declarator, up until the comma or semicolon.
3082 SkipUntil(tok::comma, true);
3083 return;
3084 }
3085
Douglas Gregorb9075602011-02-22 02:55:24 +00003086 if (!Name && TUK != Sema::TUK_Definition) {
3087 Diag(Tok, diag::err_enumerator_unnamed_no_def);
3088
3089 // Skip the rest of this declarator, up until the comma or semicolon.
3090 SkipUntil(tok::comma, true);
3091 return;
3092 }
3093
Douglas Gregor402abb52009-05-28 23:31:59 +00003094 bool Owned = false;
John McCallc4e70192009-09-11 04:59:25 +00003095 bool IsDependent = false;
Douglas Gregor48c89f42010-04-24 16:38:41 +00003096 const char *PrevSpec = 0;
3097 unsigned DiagID;
John McCalld226f652010-08-21 09:40:31 +00003098 Decl *TagDecl = Actions.ActOnTag(getCurScope(), DeclSpec::TST_enum, TUK,
John McCall7f040a92010-12-24 02:08:15 +00003099 StartLoc, SS, Name, NameLoc, attrs.getList(),
Douglas Gregore7612302011-09-09 19:05:14 +00003100 AS, DS.getModulePrivateSpecLoc(),
John McCallf312b1e2010-08-26 23:41:50 +00003101 MultiTemplateParamsArg(Actions),
Richard Smithbdad7a22012-01-10 01:33:14 +00003102 Owned, IsDependent, ScopedEnumKWLoc,
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00003103 IsScopedUsingClassTag, BaseType);
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003104
Douglas Gregor48c89f42010-04-24 16:38:41 +00003105 if (IsDependent) {
3106 // This enum has a dependent nested-name-specifier. Handle it as a
3107 // dependent tag.
3108 if (!Name) {
3109 DS.SetTypeSpecError();
3110 Diag(Tok, diag::err_expected_type_name_after_typename);
3111 return;
3112 }
3113
Douglas Gregor23c94db2010-07-02 17:43:08 +00003114 TypeResult Type = Actions.ActOnDependentTag(getCurScope(), DeclSpec::TST_enum,
Douglas Gregor48c89f42010-04-24 16:38:41 +00003115 TUK, SS, Name, StartLoc,
3116 NameLoc);
3117 if (Type.isInvalid()) {
3118 DS.SetTypeSpecError();
3119 return;
3120 }
3121
Abramo Bagnara0daaf322011-03-16 20:16:18 +00003122 if (DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc,
3123 NameLoc.isValid() ? NameLoc : StartLoc,
3124 PrevSpec, DiagID, Type.get()))
Douglas Gregor48c89f42010-04-24 16:38:41 +00003125 Diag(StartLoc, DiagID) << PrevSpec;
3126
3127 return;
3128 }
Mike Stump1eb44332009-09-09 15:08:12 +00003129
John McCalld226f652010-08-21 09:40:31 +00003130 if (!TagDecl) {
Douglas Gregor48c89f42010-04-24 16:38:41 +00003131 // The action failed to produce an enumeration tag. If this is a
3132 // definition, consume the entire definition.
3133 if (Tok.is(tok::l_brace)) {
3134 ConsumeBrace();
3135 SkipUntil(tok::r_brace);
3136 }
3137
3138 DS.SetTypeSpecError();
3139 return;
3140 }
Richard Smithbdad7a22012-01-10 01:33:14 +00003141
3142 if (Tok.is(tok::l_brace)) {
3143 if (TUK == Sema::TUK_Friend)
3144 Diag(Tok, diag::err_friend_decl_defines_type)
3145 << SourceRange(DS.getFriendSpecLoc());
Reid Spencer5f016e22007-07-11 17:01:13 +00003146 ParseEnumBody(StartLoc, TagDecl);
Richard Smithbdad7a22012-01-10 01:33:14 +00003147 }
Mike Stump1eb44332009-09-09 15:08:12 +00003148
Abramo Bagnara0daaf322011-03-16 20:16:18 +00003149 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc,
3150 NameLoc.isValid() ? NameLoc : StartLoc,
3151 PrevSpec, DiagID, TagDecl, Owned))
John McCallfec54012009-08-03 20:12:06 +00003152 Diag(StartLoc, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00003153}
3154
3155/// ParseEnumBody - Parse a {} enclosed enumerator-list.
3156/// enumerator-list:
3157/// enumerator
3158/// enumerator-list ',' enumerator
3159/// enumerator:
3160/// enumeration-constant
3161/// enumeration-constant '=' constant-expression
3162/// enumeration-constant:
3163/// identifier
3164///
John McCalld226f652010-08-21 09:40:31 +00003165void Parser::ParseEnumBody(SourceLocation StartLoc, Decl *EnumDecl) {
Douglas Gregor074149e2009-01-05 19:45:36 +00003166 // Enter the scope of the enum body and start the definition.
3167 ParseScope EnumScope(this, Scope::DeclScope);
Douglas Gregor23c94db2010-07-02 17:43:08 +00003168 Actions.ActOnTagStartDefinition(getCurScope(), EnumDecl);
Douglas Gregor074149e2009-01-05 19:45:36 +00003169
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003170 BalancedDelimiterTracker T(*this, tok::l_brace);
3171 T.consumeOpen();
Mike Stump1eb44332009-09-09 15:08:12 +00003172
Chris Lattner7946dd32007-08-27 17:24:30 +00003173 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
Chris Lattner04d66662007-10-09 17:33:22 +00003174 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Fariborz Jahanian05115522010-05-28 22:23:22 +00003175 Diag(Tok, diag::error_empty_enum);
Mike Stump1eb44332009-09-09 15:08:12 +00003176
Chris Lattner5f9e2722011-07-23 10:55:15 +00003177 SmallVector<Decl *, 32> EnumConstantDecls;
Reid Spencer5f016e22007-07-11 17:01:13 +00003178
John McCalld226f652010-08-21 09:40:31 +00003179 Decl *LastEnumConstDecl = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00003180
Reid Spencer5f016e22007-07-11 17:01:13 +00003181 // Parse the enumerator-list.
Chris Lattner04d66662007-10-09 17:33:22 +00003182 while (Tok.is(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003183 IdentifierInfo *Ident = Tok.getIdentifierInfo();
3184 SourceLocation IdentLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00003185
John McCall5b629aa2010-10-22 23:36:17 +00003186 // If attributes exist after the enumerator, parse them.
John McCall0b7e6782011-03-24 11:26:52 +00003187 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00003188 MaybeParseGNUAttributes(attrs);
John McCall5b629aa2010-10-22 23:36:17 +00003189
Reid Spencer5f016e22007-07-11 17:01:13 +00003190 SourceLocation EqualLoc;
John McCall60d7b3a2010-08-24 06:29:42 +00003191 ExprResult AssignedVal;
Fariborz Jahanian5a477db2011-12-09 01:15:54 +00003192 ParsingDeclRAIIObject PD(*this);
3193
Chris Lattner04d66662007-10-09 17:33:22 +00003194 if (Tok.is(tok::equal)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003195 EqualLoc = ConsumeToken();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00003196 AssignedVal = ParseConstantExpression();
3197 if (AssignedVal.isInvalid())
Reid Spencer5f016e22007-07-11 17:01:13 +00003198 SkipUntil(tok::comma, tok::r_brace, true, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00003199 }
Mike Stump1eb44332009-09-09 15:08:12 +00003200
Reid Spencer5f016e22007-07-11 17:01:13 +00003201 // Install the enumerator constant into EnumDecl.
John McCalld226f652010-08-21 09:40:31 +00003202 Decl *EnumConstDecl = Actions.ActOnEnumConstant(getCurScope(), EnumDecl,
3203 LastEnumConstDecl,
3204 IdentLoc, Ident,
John McCall7f040a92010-12-24 02:08:15 +00003205 attrs.getList(), EqualLoc,
John McCalld226f652010-08-21 09:40:31 +00003206 AssignedVal.release());
Fariborz Jahanian5a477db2011-12-09 01:15:54 +00003207 PD.complete(EnumConstDecl);
3208
Reid Spencer5f016e22007-07-11 17:01:13 +00003209 EnumConstantDecls.push_back(EnumConstDecl);
3210 LastEnumConstDecl = EnumConstDecl;
Mike Stump1eb44332009-09-09 15:08:12 +00003211
Douglas Gregor751f6922010-09-07 14:51:08 +00003212 if (Tok.is(tok::identifier)) {
3213 // We're missing a comma between enumerators.
3214 SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation);
3215 Diag(Loc, diag::err_enumerator_list_missing_comma)
3216 << FixItHint::CreateInsertion(Loc, ", ");
3217 continue;
3218 }
3219
Chris Lattner04d66662007-10-09 17:33:22 +00003220 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +00003221 break;
3222 SourceLocation CommaLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00003223
Richard Smith7fe62082011-10-15 05:09:34 +00003224 if (Tok.isNot(tok::identifier)) {
3225 if (!getLang().C99 && !getLang().CPlusPlus0x)
3226 Diag(CommaLoc, diag::ext_enumerator_list_comma)
3227 << getLang().CPlusPlus
3228 << FixItHint::CreateRemoval(CommaLoc);
3229 else if (getLang().CPlusPlus0x)
3230 Diag(CommaLoc, diag::warn_cxx98_compat_enumerator_list_comma)
3231 << FixItHint::CreateRemoval(CommaLoc);
3232 }
Reid Spencer5f016e22007-07-11 17:01:13 +00003233 }
Mike Stump1eb44332009-09-09 15:08:12 +00003234
Reid Spencer5f016e22007-07-11 17:01:13 +00003235 // Eat the }.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003236 T.consumeClose();
Reid Spencer5f016e22007-07-11 17:01:13 +00003237
Reid Spencer5f016e22007-07-11 17:01:13 +00003238 // If attributes exist after the identifier list, parse them.
John McCall0b7e6782011-03-24 11:26:52 +00003239 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00003240 MaybeParseGNUAttributes(attrs);
Douglas Gregor72de6672009-01-08 20:45:30 +00003241
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003242 Actions.ActOnEnumBody(StartLoc, T.getOpenLocation(), T.getCloseLocation(),
3243 EnumDecl, EnumConstantDecls.data(),
3244 EnumConstantDecls.size(), getCurScope(),
3245 attrs.getList());
Mike Stump1eb44332009-09-09 15:08:12 +00003246
Douglas Gregor72de6672009-01-08 20:45:30 +00003247 EnumScope.Exit();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003248 Actions.ActOnTagFinishDefinition(getCurScope(), EnumDecl,
3249 T.getCloseLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00003250}
3251
3252/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff5f8aa692008-02-11 23:15:56 +00003253/// start of a type-qualifier-list.
3254bool Parser::isTypeQualifier() const {
3255 switch (Tok.getKind()) {
3256 default: return false;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003257
3258 // type-qualifier only in OpenCL
3259 case tok::kw_private:
3260 return getLang().OpenCL;
3261
Steve Naroff5f8aa692008-02-11 23:15:56 +00003262 // type-qualifier
3263 case tok::kw_const:
3264 case tok::kw_volatile:
3265 case tok::kw_restrict:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003266 case tok::kw___private:
3267 case tok::kw___local:
3268 case tok::kw___global:
3269 case tok::kw___constant:
3270 case tok::kw___read_only:
3271 case tok::kw___read_write:
3272 case tok::kw___write_only:
Steve Naroff5f8aa692008-02-11 23:15:56 +00003273 return true;
3274 }
3275}
3276
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003277/// isKnownToBeTypeSpecifier - Return true if we know that the specified token
3278/// is definitely a type-specifier. Return false if it isn't part of a type
3279/// specifier or if we're not sure.
3280bool Parser::isKnownToBeTypeSpecifier(const Token &Tok) const {
3281 switch (Tok.getKind()) {
3282 default: return false;
3283 // type-specifiers
3284 case tok::kw_short:
3285 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00003286 case tok::kw___int64:
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003287 case tok::kw_signed:
3288 case tok::kw_unsigned:
3289 case tok::kw__Complex:
3290 case tok::kw__Imaginary:
3291 case tok::kw_void:
3292 case tok::kw_char:
3293 case tok::kw_wchar_t:
3294 case tok::kw_char16_t:
3295 case tok::kw_char32_t:
3296 case tok::kw_int:
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00003297 case tok::kw_half:
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003298 case tok::kw_float:
3299 case tok::kw_double:
3300 case tok::kw_bool:
3301 case tok::kw__Bool:
3302 case tok::kw__Decimal32:
3303 case tok::kw__Decimal64:
3304 case tok::kw__Decimal128:
3305 case tok::kw___vector:
3306
3307 // struct-or-union-specifier (C99) or class-specifier (C++)
3308 case tok::kw_class:
3309 case tok::kw_struct:
3310 case tok::kw_union:
3311 // enum-specifier
3312 case tok::kw_enum:
3313
3314 // typedef-name
3315 case tok::annot_typename:
3316 return true;
3317 }
3318}
3319
Steve Naroff5f8aa692008-02-11 23:15:56 +00003320/// isTypeSpecifierQualifier - Return true if the current token could be the
Reid Spencer5f016e22007-07-11 17:01:13 +00003321/// start of a specifier-qualifier-list.
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003322bool Parser::isTypeSpecifierQualifier() {
Reid Spencer5f016e22007-07-11 17:01:13 +00003323 switch (Tok.getKind()) {
3324 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00003325
Chris Lattner166a8fc2009-01-04 23:41:41 +00003326 case tok::identifier: // foo::bar
John Thompson82287d12010-02-05 00:12:22 +00003327 if (TryAltiVecVectorToken())
3328 return true;
3329 // Fall through.
Douglas Gregord57959a2009-03-27 23:10:48 +00003330 case tok::kw_typename: // typename T::type
Chris Lattner166a8fc2009-01-04 23:41:41 +00003331 // Annotate typenames and C++ scope specifiers. If we get one, just
3332 // recurse to handle whatever we get.
3333 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003334 return true;
3335 if (Tok.is(tok::identifier))
3336 return false;
3337 return isTypeSpecifierQualifier();
Douglas Gregord57959a2009-03-27 23:10:48 +00003338
Chris Lattner166a8fc2009-01-04 23:41:41 +00003339 case tok::coloncolon: // ::foo::bar
3340 if (NextToken().is(tok::kw_new) || // ::new
3341 NextToken().is(tok::kw_delete)) // ::delete
3342 return false;
3343
Chris Lattner166a8fc2009-01-04 23:41:41 +00003344 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003345 return true;
3346 return isTypeSpecifierQualifier();
Mike Stump1eb44332009-09-09 15:08:12 +00003347
Reid Spencer5f016e22007-07-11 17:01:13 +00003348 // GNU attributes support.
3349 case tok::kw___attribute:
Steve Naroffd1861fd2007-07-31 12:34:36 +00003350 // GNU typeof support.
3351 case tok::kw_typeof:
Mike Stump1eb44332009-09-09 15:08:12 +00003352
Reid Spencer5f016e22007-07-11 17:01:13 +00003353 // type-specifiers
3354 case tok::kw_short:
3355 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00003356 case tok::kw___int64:
Reid Spencer5f016e22007-07-11 17:01:13 +00003357 case tok::kw_signed:
3358 case tok::kw_unsigned:
3359 case tok::kw__Complex:
3360 case tok::kw__Imaginary:
3361 case tok::kw_void:
3362 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00003363 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00003364 case tok::kw_char16_t:
3365 case tok::kw_char32_t:
Reid Spencer5f016e22007-07-11 17:01:13 +00003366 case tok::kw_int:
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00003367 case tok::kw_half:
Reid Spencer5f016e22007-07-11 17:01:13 +00003368 case tok::kw_float:
3369 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00003370 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00003371 case tok::kw__Bool:
3372 case tok::kw__Decimal32:
3373 case tok::kw__Decimal64:
3374 case tok::kw__Decimal128:
John Thompson82287d12010-02-05 00:12:22 +00003375 case tok::kw___vector:
Mike Stump1eb44332009-09-09 15:08:12 +00003376
Chris Lattner99dc9142008-04-13 18:59:07 +00003377 // struct-or-union-specifier (C99) or class-specifier (C++)
3378 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00003379 case tok::kw_struct:
3380 case tok::kw_union:
3381 // enum-specifier
3382 case tok::kw_enum:
Mike Stump1eb44332009-09-09 15:08:12 +00003383
Reid Spencer5f016e22007-07-11 17:01:13 +00003384 // type-qualifier
3385 case tok::kw_const:
3386 case tok::kw_volatile:
3387 case tok::kw_restrict:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003388
3389 // typedef-name
Chris Lattnerb31757b2009-01-06 05:06:21 +00003390 case tok::annot_typename:
Reid Spencer5f016e22007-07-11 17:01:13 +00003391 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00003392
Chris Lattner7c186be2008-10-20 00:25:30 +00003393 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
3394 case tok::less:
3395 return getLang().ObjC1;
Mike Stump1eb44332009-09-09 15:08:12 +00003396
Steve Naroff239f0732008-12-25 14:16:32 +00003397 case tok::kw___cdecl:
3398 case tok::kw___stdcall:
3399 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00003400 case tok::kw___thiscall:
Eli Friedman290eeb02009-06-08 23:27:34 +00003401 case tok::kw___w64:
3402 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00003403 case tok::kw___ptr32:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003404 case tok::kw___pascal:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00003405 case tok::kw___unaligned:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003406
3407 case tok::kw___private:
3408 case tok::kw___local:
3409 case tok::kw___global:
3410 case tok::kw___constant:
3411 case tok::kw___read_only:
3412 case tok::kw___read_write:
3413 case tok::kw___write_only:
3414
Eli Friedman290eeb02009-06-08 23:27:34 +00003415 return true;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003416
3417 case tok::kw_private:
3418 return getLang().OpenCL;
Eli Friedmanb001de72011-10-06 23:00:33 +00003419
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00003420 // C11 _Atomic()
Eli Friedmanb001de72011-10-06 23:00:33 +00003421 case tok::kw__Atomic:
3422 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00003423 }
3424}
3425
3426/// isDeclarationSpecifier() - Return true if the current token is part of a
3427/// declaration specifier.
Douglas Gregor9497a732010-09-16 01:51:54 +00003428///
3429/// \param DisambiguatingWithExpression True to indicate that the purpose of
3430/// this check is to disambiguate between an expression and a declaration.
3431bool Parser::isDeclarationSpecifier(bool DisambiguatingWithExpression) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003432 switch (Tok.getKind()) {
3433 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00003434
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003435 case tok::kw_private:
3436 return getLang().OpenCL;
3437
Chris Lattner166a8fc2009-01-04 23:41:41 +00003438 case tok::identifier: // foo::bar
Steve Naroff61f72cb2009-03-09 21:12:44 +00003439 // Unfortunate hack to support "Class.factoryMethod" notation.
3440 if (getLang().ObjC1 && NextToken().is(tok::period))
3441 return false;
John Thompson82287d12010-02-05 00:12:22 +00003442 if (TryAltiVecVectorToken())
3443 return true;
3444 // Fall through.
David Blaikie42d6d0c2011-12-04 05:04:18 +00003445 case tok::kw_decltype: // decltype(T())::type
Douglas Gregord57959a2009-03-27 23:10:48 +00003446 case tok::kw_typename: // typename T::type
Chris Lattner166a8fc2009-01-04 23:41:41 +00003447 // Annotate typenames and C++ scope specifiers. If we get one, just
3448 // recurse to handle whatever we get.
3449 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003450 return true;
3451 if (Tok.is(tok::identifier))
3452 return false;
Douglas Gregor9497a732010-09-16 01:51:54 +00003453
3454 // If we're in Objective-C and we have an Objective-C class type followed
3455 // by an identifier and then either ':' or ']', in a place where an
3456 // expression is permitted, then this is probably a class message send
3457 // missing the initial '['. In this case, we won't consider this to be
3458 // the start of a declaration.
3459 if (DisambiguatingWithExpression &&
3460 isStartOfObjCClassMessageMissingOpenBracket())
3461 return false;
3462
John McCall9ba61662010-02-26 08:45:28 +00003463 return isDeclarationSpecifier();
3464
Chris Lattner166a8fc2009-01-04 23:41:41 +00003465 case tok::coloncolon: // ::foo::bar
3466 if (NextToken().is(tok::kw_new) || // ::new
3467 NextToken().is(tok::kw_delete)) // ::delete
3468 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00003469
Chris Lattner166a8fc2009-01-04 23:41:41 +00003470 // Annotate typenames and C++ scope specifiers. If we get one, just
3471 // recurse to handle whatever we get.
3472 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003473 return true;
3474 return isDeclarationSpecifier();
Mike Stump1eb44332009-09-09 15:08:12 +00003475
Reid Spencer5f016e22007-07-11 17:01:13 +00003476 // storage-class-specifier
3477 case tok::kw_typedef:
3478 case tok::kw_extern:
Steve Naroff8d54bf22007-12-18 00:16:02 +00003479 case tok::kw___private_extern__:
Reid Spencer5f016e22007-07-11 17:01:13 +00003480 case tok::kw_static:
3481 case tok::kw_auto:
3482 case tok::kw_register:
3483 case tok::kw___thread:
Mike Stump1eb44332009-09-09 15:08:12 +00003484
Douglas Gregor8d267c52011-09-09 02:06:17 +00003485 // Modules
3486 case tok::kw___module_private__:
3487
Reid Spencer5f016e22007-07-11 17:01:13 +00003488 // type-specifiers
3489 case tok::kw_short:
3490 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00003491 case tok::kw___int64:
Reid Spencer5f016e22007-07-11 17:01:13 +00003492 case tok::kw_signed:
3493 case tok::kw_unsigned:
3494 case tok::kw__Complex:
3495 case tok::kw__Imaginary:
3496 case tok::kw_void:
3497 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00003498 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00003499 case tok::kw_char16_t:
3500 case tok::kw_char32_t:
3501
Reid Spencer5f016e22007-07-11 17:01:13 +00003502 case tok::kw_int:
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00003503 case tok::kw_half:
Reid Spencer5f016e22007-07-11 17:01:13 +00003504 case tok::kw_float:
3505 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00003506 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00003507 case tok::kw__Bool:
3508 case tok::kw__Decimal32:
3509 case tok::kw__Decimal64:
3510 case tok::kw__Decimal128:
John Thompson82287d12010-02-05 00:12:22 +00003511 case tok::kw___vector:
Mike Stump1eb44332009-09-09 15:08:12 +00003512
Chris Lattner99dc9142008-04-13 18:59:07 +00003513 // struct-or-union-specifier (C99) or class-specifier (C++)
3514 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00003515 case tok::kw_struct:
3516 case tok::kw_union:
3517 // enum-specifier
3518 case tok::kw_enum:
Mike Stump1eb44332009-09-09 15:08:12 +00003519
Reid Spencer5f016e22007-07-11 17:01:13 +00003520 // type-qualifier
3521 case tok::kw_const:
3522 case tok::kw_volatile:
3523 case tok::kw_restrict:
Steve Naroffd1861fd2007-07-31 12:34:36 +00003524
Reid Spencer5f016e22007-07-11 17:01:13 +00003525 // function-specifier
3526 case tok::kw_inline:
Douglas Gregorb48fe382008-10-31 09:07:45 +00003527 case tok::kw_virtual:
3528 case tok::kw_explicit:
Chris Lattnerd6c7c182007-08-09 16:40:21 +00003529
Peter Collingbournec6eb44b2011-04-15 00:35:57 +00003530 // static_assert-declaration
3531 case tok::kw__Static_assert:
3532
Chris Lattner1ef08762007-08-09 17:01:07 +00003533 // GNU typeof support.
3534 case tok::kw_typeof:
Mike Stump1eb44332009-09-09 15:08:12 +00003535
Chris Lattner1ef08762007-08-09 17:01:07 +00003536 // GNU attributes.
Chris Lattnerd6c7c182007-08-09 16:40:21 +00003537 case tok::kw___attribute:
Reid Spencer5f016e22007-07-11 17:01:13 +00003538 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00003539
Francois Pichete3d49b42011-06-19 08:02:06 +00003540 // C++0x decltype.
David Blaikie42d6d0c2011-12-04 05:04:18 +00003541 case tok::annot_decltype:
Francois Pichete3d49b42011-06-19 08:02:06 +00003542 return true;
3543
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00003544 // C11 _Atomic()
Eli Friedmanb001de72011-10-06 23:00:33 +00003545 case tok::kw__Atomic:
3546 return true;
3547
Chris Lattnerf3948c42008-07-26 03:38:44 +00003548 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
3549 case tok::less:
3550 return getLang().ObjC1;
Mike Stump1eb44332009-09-09 15:08:12 +00003551
Douglas Gregord9d75e52011-04-27 05:41:15 +00003552 // typedef-name
3553 case tok::annot_typename:
3554 return !DisambiguatingWithExpression ||
3555 !isStartOfObjCClassMessageMissingOpenBracket();
3556
Steve Naroff47f52092009-01-06 19:34:12 +00003557 case tok::kw___declspec:
Steve Naroff239f0732008-12-25 14:16:32 +00003558 case tok::kw___cdecl:
3559 case tok::kw___stdcall:
3560 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00003561 case tok::kw___thiscall:
Eli Friedman290eeb02009-06-08 23:27:34 +00003562 case tok::kw___w64:
3563 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00003564 case tok::kw___ptr32:
Eli Friedman290eeb02009-06-08 23:27:34 +00003565 case tok::kw___forceinline:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003566 case tok::kw___pascal:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00003567 case tok::kw___unaligned:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003568
3569 case tok::kw___private:
3570 case tok::kw___local:
3571 case tok::kw___global:
3572 case tok::kw___constant:
3573 case tok::kw___read_only:
3574 case tok::kw___read_write:
3575 case tok::kw___write_only:
3576
Eli Friedman290eeb02009-06-08 23:27:34 +00003577 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00003578 }
3579}
3580
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003581bool Parser::isConstructorDeclarator() {
3582 TentativeParsingAction TPA(*this);
3583
3584 // Parse the C++ scope specifier.
3585 CXXScopeSpec SS;
Douglas Gregorefaa93a2011-11-07 17:33:42 +00003586 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
3587 /*EnteringContext=*/true)) {
John McCall9ba61662010-02-26 08:45:28 +00003588 TPA.Revert();
3589 return false;
3590 }
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003591
3592 // Parse the constructor name.
3593 if (Tok.is(tok::identifier) || Tok.is(tok::annot_template_id)) {
3594 // We already know that we have a constructor name; just consume
3595 // the token.
3596 ConsumeToken();
3597 } else {
3598 TPA.Revert();
3599 return false;
3600 }
3601
3602 // Current class name must be followed by a left parentheses.
3603 if (Tok.isNot(tok::l_paren)) {
3604 TPA.Revert();
3605 return false;
3606 }
3607 ConsumeParen();
3608
3609 // A right parentheses or ellipsis signals that we have a constructor.
3610 if (Tok.is(tok::r_paren) || Tok.is(tok::ellipsis)) {
3611 TPA.Revert();
3612 return true;
3613 }
3614
3615 // If we need to, enter the specified scope.
3616 DeclaratorScopeObj DeclScopeObj(*this, SS);
Douglas Gregor23c94db2010-07-02 17:43:08 +00003617 if (SS.isSet() && Actions.ShouldEnterDeclaratorScope(getCurScope(), SS))
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003618 DeclScopeObj.EnterDeclaratorScope();
3619
Francois Pichetdfaa5fb2011-01-31 04:54:32 +00003620 // Optionally skip Microsoft attributes.
John McCall0b7e6782011-03-24 11:26:52 +00003621 ParsedAttributes Attrs(AttrFactory);
Francois Pichetdfaa5fb2011-01-31 04:54:32 +00003622 MaybeParseMicrosoftAttributes(Attrs);
3623
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003624 // Check whether the next token(s) are part of a declaration
3625 // specifier, in which case we have the start of a parameter and,
3626 // therefore, we know that this is a constructor.
3627 bool IsConstructor = isDeclarationSpecifier();
3628 TPA.Revert();
3629 return IsConstructor;
3630}
Reid Spencer5f016e22007-07-11 17:01:13 +00003631
3632/// ParseTypeQualifierListOpt
Dawn Perchik52fc3142010-09-03 01:29:35 +00003633/// type-qualifier-list: [C99 6.7.5]
3634/// type-qualifier
3635/// [vendor] attributes
3636/// [ only if VendorAttributesAllowed=true ]
3637/// type-qualifier-list type-qualifier
3638/// [vendor] type-qualifier-list attributes
3639/// [ only if VendorAttributesAllowed=true ]
3640/// [C++0x] attribute-specifier[opt] is allowed before cv-qualifier-seq
3641/// [ only if CXX0XAttributesAllowed=true ]
3642/// Note: vendor can be GNU, MS, etc.
Reid Spencer5f016e22007-07-11 17:01:13 +00003643///
Dawn Perchik52fc3142010-09-03 01:29:35 +00003644void Parser::ParseTypeQualifierListOpt(DeclSpec &DS,
3645 bool VendorAttributesAllowed,
Sean Huntbbd37c62009-11-21 08:43:09 +00003646 bool CXX0XAttributesAllowed) {
3647 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier()) {
3648 SourceLocation Loc = Tok.getLocation();
John McCall0b7e6782011-03-24 11:26:52 +00003649 ParsedAttributesWithRange attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00003650 ParseCXX0XAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00003651 if (CXX0XAttributesAllowed)
John McCall7f040a92010-12-24 02:08:15 +00003652 DS.takeAttributesFrom(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00003653 else
3654 Diag(Loc, diag::err_attributes_not_allowed);
3655 }
Abramo Bagnara796aa442011-03-12 11:17:06 +00003656
3657 SourceLocation EndLoc;
3658
Reid Spencer5f016e22007-07-11 17:01:13 +00003659 while (1) {
John McCallfec54012009-08-03 20:12:06 +00003660 bool isInvalid = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00003661 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00003662 unsigned DiagID = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00003663 SourceLocation Loc = Tok.getLocation();
3664
3665 switch (Tok.getKind()) {
Douglas Gregor1a480c42010-08-27 17:35:51 +00003666 case tok::code_completion:
3667 Actions.CodeCompleteTypeQualifiers(DS);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00003668 return cutOffParsing();
Douglas Gregor1a480c42010-08-27 17:35:51 +00003669
Reid Spencer5f016e22007-07-11 17:01:13 +00003670 case tok::kw_const:
John McCallfec54012009-08-03 20:12:06 +00003671 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, DiagID,
3672 getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00003673 break;
3674 case tok::kw_volatile:
John McCallfec54012009-08-03 20:12:06 +00003675 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
3676 getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00003677 break;
3678 case tok::kw_restrict:
John McCallfec54012009-08-03 20:12:06 +00003679 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
3680 getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00003681 break;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003682
3683 // OpenCL qualifiers:
3684 case tok::kw_private:
3685 if (!getLang().OpenCL)
3686 goto DoneWithTypeQuals;
3687 case tok::kw___private:
3688 case tok::kw___global:
3689 case tok::kw___local:
3690 case tok::kw___constant:
3691 case tok::kw___read_only:
3692 case tok::kw___write_only:
3693 case tok::kw___read_write:
3694 ParseOpenCLQualifiers(DS);
3695 break;
3696
Eli Friedman290eeb02009-06-08 23:27:34 +00003697 case tok::kw___w64:
Steve Naroff86bc6cf2008-12-25 14:41:26 +00003698 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00003699 case tok::kw___ptr32:
Steve Naroff239f0732008-12-25 14:16:32 +00003700 case tok::kw___cdecl:
3701 case tok::kw___stdcall:
3702 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00003703 case tok::kw___thiscall:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00003704 case tok::kw___unaligned:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003705 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00003706 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman290eeb02009-06-08 23:27:34 +00003707 continue;
3708 }
3709 goto DoneWithTypeQuals;
Dawn Perchik52fc3142010-09-03 01:29:35 +00003710 case tok::kw___pascal:
3711 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00003712 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik52fc3142010-09-03 01:29:35 +00003713 continue;
3714 }
3715 goto DoneWithTypeQuals;
Reid Spencer5f016e22007-07-11 17:01:13 +00003716 case tok::kw___attribute:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003717 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00003718 ParseGNUAttributes(DS.getAttributes());
Chris Lattner5a69d1c2008-12-18 07:02:59 +00003719 continue; // do *not* consume the next token!
3720 }
3721 // otherwise, FALL THROUGH!
3722 default:
Steve Naroff239f0732008-12-25 14:16:32 +00003723 DoneWithTypeQuals:
Chris Lattner5a69d1c2008-12-18 07:02:59 +00003724 // If this is not a type-qualifier token, we're done reading type
3725 // qualifiers. First verify that DeclSpec's are consistent.
Douglas Gregor9b3064b2009-04-01 22:41:11 +00003726 DS.Finish(Diags, PP);
Abramo Bagnara796aa442011-03-12 11:17:06 +00003727 if (EndLoc.isValid())
3728 DS.SetRangeEnd(EndLoc);
Chris Lattner5a69d1c2008-12-18 07:02:59 +00003729 return;
Reid Spencer5f016e22007-07-11 17:01:13 +00003730 }
Chris Lattnera1fcbad2008-12-18 06:50:14 +00003731
Reid Spencer5f016e22007-07-11 17:01:13 +00003732 // If the specifier combination wasn't legal, issue a diagnostic.
3733 if (isInvalid) {
3734 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner1ab3b962008-11-18 07:48:38 +00003735 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00003736 }
Abramo Bagnara796aa442011-03-12 11:17:06 +00003737 EndLoc = ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00003738 }
3739}
3740
3741
3742/// ParseDeclarator - Parse and verify a newly-initialized declarator.
3743///
3744void Parser::ParseDeclarator(Declarator &D) {
3745 /// This implements the 'declarator' production in the C grammar, then checks
3746 /// for well-formedness and issues diagnostics.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003747 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Reid Spencer5f016e22007-07-11 17:01:13 +00003748}
3749
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003750/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
3751/// is parsed by the function passed to it. Pass null, and the direct-declarator
3752/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003753/// ptr-operator production.
3754///
Richard Smith0706df42011-10-19 21:33:05 +00003755/// If the grammar of this construct is extended, matching changes must also be
3756/// made to TryParseDeclarator and MightBeDeclarator.
3757///
Sebastian Redlf30208a2009-01-24 21:16:55 +00003758/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
3759/// [C] pointer[opt] direct-declarator
3760/// [C++] direct-declarator
3761/// [C++] ptr-operator declarator
Reid Spencer5f016e22007-07-11 17:01:13 +00003762///
3763/// pointer: [C99 6.7.5]
3764/// '*' type-qualifier-list[opt]
3765/// '*' type-qualifier-list[opt] pointer
3766///
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003767/// ptr-operator:
3768/// '*' cv-qualifier-seq[opt]
3769/// '&'
Sebastian Redl05532f22009-03-15 22:02:01 +00003770/// [C++0x] '&&'
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003771/// [GNU] '&' restrict[opt] attributes[opt]
Sebastian Redl05532f22009-03-15 22:02:01 +00003772/// [GNU?] '&&' restrict[opt] attributes[opt]
Sebastian Redlf30208a2009-01-24 21:16:55 +00003773/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003774void Parser::ParseDeclaratorInternal(Declarator &D,
3775 DirectDeclParseFunction DirectDeclParser) {
Douglas Gregor91a28862009-08-26 14:27:30 +00003776 if (Diags.hasAllExtensionsSilenced())
3777 D.setExtension();
Douglas Gregor2ccccb32010-08-23 18:23:48 +00003778
Sebastian Redlf30208a2009-01-24 21:16:55 +00003779 // C++ member pointers start with a '::' or a nested-name.
3780 // Member pointers get special handling, since there's no place for the
3781 // scope spec in the generic path below.
Chris Lattnerf919bfe2009-03-24 17:04:48 +00003782 if (getLang().CPlusPlus &&
3783 (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) ||
3784 Tok.is(tok::annot_cxxscope))) {
Douglas Gregorefaa93a2011-11-07 17:33:42 +00003785 bool EnteringContext = D.getContext() == Declarator::FileContext ||
3786 D.getContext() == Declarator::MemberContext;
Sebastian Redlf30208a2009-01-24 21:16:55 +00003787 CXXScopeSpec SS;
Douglas Gregorefaa93a2011-11-07 17:33:42 +00003788 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), EnteringContext);
John McCall9ba61662010-02-26 08:45:28 +00003789
Jeffrey Yasskinedc28772010-04-07 23:29:58 +00003790 if (SS.isNotEmpty()) {
Mike Stump1eb44332009-09-09 15:08:12 +00003791 if (Tok.isNot(tok::star)) {
Sebastian Redlf30208a2009-01-24 21:16:55 +00003792 // The scope spec really belongs to the direct-declarator.
3793 D.getCXXScopeSpec() = SS;
3794 if (DirectDeclParser)
3795 (this->*DirectDeclParser)(D);
3796 return;
3797 }
3798
3799 SourceLocation Loc = ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00003800 D.SetRangeEnd(Loc);
John McCall0b7e6782011-03-24 11:26:52 +00003801 DeclSpec DS(AttrFactory);
Sebastian Redlf30208a2009-01-24 21:16:55 +00003802 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00003803 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00003804
3805 // Recurse to parse whatever is left.
3806 ParseDeclaratorInternal(D, DirectDeclParser);
3807
3808 // Sema will have to catch (syntactically invalid) pointers into global
3809 // scope. It has to catch pointers into namespace scope anyway.
3810 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
John McCall0b7e6782011-03-24 11:26:52 +00003811 Loc),
3812 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003813 /* Don't replace range end. */SourceLocation());
Sebastian Redlf30208a2009-01-24 21:16:55 +00003814 return;
3815 }
3816 }
3817
3818 tok::TokenKind Kind = Tok.getKind();
Steve Naroff5618bd42008-08-27 16:04:49 +00003819 // Not a pointer, C++ reference, or block.
Chris Lattner9af55002009-03-27 04:18:06 +00003820 if (Kind != tok::star && Kind != tok::caret &&
Chris Lattnerf919bfe2009-03-24 17:04:48 +00003821 (Kind != tok::amp || !getLang().CPlusPlus) &&
Sebastian Redl743de1f2009-03-23 00:00:23 +00003822 // We parse rvalue refs in C++03, because otherwise the errors are scary.
Chris Lattner9af55002009-03-27 04:18:06 +00003823 (Kind != tok::ampamp || !getLang().CPlusPlus)) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003824 if (DirectDeclParser)
3825 (this->*DirectDeclParser)(D);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003826 return;
3827 }
Sebastian Redlf30208a2009-01-24 21:16:55 +00003828
Sebastian Redl05532f22009-03-15 22:02:01 +00003829 // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference,
3830 // '&&' -> rvalue reference
Sebastian Redl743de1f2009-03-23 00:00:23 +00003831 SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&.
Sebastian Redlab197ba2009-02-09 18:23:29 +00003832 D.SetRangeEnd(Loc);
Reid Spencer5f016e22007-07-11 17:01:13 +00003833
Chris Lattner9af55002009-03-27 04:18:06 +00003834 if (Kind == tok::star || Kind == tok::caret) {
Chris Lattner76549142008-02-21 01:32:26 +00003835 // Is a pointer.
John McCall0b7e6782011-03-24 11:26:52 +00003836 DeclSpec DS(AttrFactory);
Sebastian Redlf30208a2009-01-24 21:16:55 +00003837
Reid Spencer5f016e22007-07-11 17:01:13 +00003838 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00003839 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00003840
Reid Spencer5f016e22007-07-11 17:01:13 +00003841 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003842 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroff5618bd42008-08-27 16:04:49 +00003843 if (Kind == tok::star)
3844 // Remember that we parsed a pointer type, and remember the type-quals.
3845 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
Chandler Carruthd067c072011-02-23 18:51:59 +00003846 DS.getConstSpecLoc(),
3847 DS.getVolatileSpecLoc(),
John McCall0b7e6782011-03-24 11:26:52 +00003848 DS.getRestrictSpecLoc()),
3849 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003850 SourceLocation());
Steve Naroff5618bd42008-08-27 16:04:49 +00003851 else
3852 // Remember that we parsed a Block type, and remember the type-quals.
Mike Stump1eb44332009-09-09 15:08:12 +00003853 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
John McCall0b7e6782011-03-24 11:26:52 +00003854 Loc),
3855 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003856 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00003857 } else {
3858 // Is a reference
John McCall0b7e6782011-03-24 11:26:52 +00003859 DeclSpec DS(AttrFactory);
Reid Spencer5f016e22007-07-11 17:01:13 +00003860
Sebastian Redl743de1f2009-03-23 00:00:23 +00003861 // Complain about rvalue references in C++03, but then go on and build
3862 // the declarator.
Richard Smith7fe62082011-10-15 05:09:34 +00003863 if (Kind == tok::ampamp)
3864 Diag(Loc, getLang().CPlusPlus0x ?
3865 diag::warn_cxx98_compat_rvalue_reference :
3866 diag::ext_rvalue_reference);
Sebastian Redl743de1f2009-03-23 00:00:23 +00003867
Reid Spencer5f016e22007-07-11 17:01:13 +00003868 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
3869 // cv-qualifiers are introduced through the use of a typedef or of a
3870 // template type argument, in which case the cv-qualifiers are ignored.
3871 //
3872 // [GNU] Retricted references are allowed.
3873 // [GNU] Attributes on references are allowed.
Sean Huntbbd37c62009-11-21 08:43:09 +00003874 // [C++0x] Attributes on references are not allowed.
3875 ParseTypeQualifierListOpt(DS, true, false);
Sebastian Redlab197ba2009-02-09 18:23:29 +00003876 D.ExtendWithDeclSpec(DS);
Reid Spencer5f016e22007-07-11 17:01:13 +00003877
3878 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
3879 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
3880 Diag(DS.getConstSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00003881 diag::err_invalid_reference_qualifier_application) << "const";
Reid Spencer5f016e22007-07-11 17:01:13 +00003882 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
3883 Diag(DS.getVolatileSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00003884 diag::err_invalid_reference_qualifier_application) << "volatile";
Reid Spencer5f016e22007-07-11 17:01:13 +00003885 }
3886
3887 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003888 ParseDeclaratorInternal(D, DirectDeclParser);
Reid Spencer5f016e22007-07-11 17:01:13 +00003889
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00003890 if (D.getNumTypeObjects() > 0) {
3891 // C++ [dcl.ref]p4: There shall be no references to references.
3892 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
3893 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattnerda83bac2008-11-19 07:37:42 +00003894 if (const IdentifierInfo *II = D.getIdentifier())
3895 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
3896 << II;
3897 else
3898 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
3899 << "type name";
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00003900
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003901 // Once we've complained about the reference-to-reference, we
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00003902 // can go ahead and build the (technically ill-formed)
3903 // declarator: reference collapsing will take care of it.
3904 }
3905 }
3906
Reid Spencer5f016e22007-07-11 17:01:13 +00003907 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner76549142008-02-21 01:32:26 +00003908 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
Sebastian Redl05532f22009-03-15 22:02:01 +00003909 Kind == tok::amp),
John McCall0b7e6782011-03-24 11:26:52 +00003910 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003911 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00003912 }
3913}
3914
3915/// ParseDirectDeclarator
3916/// direct-declarator: [C99 6.7.5]
Douglas Gregor42a552f2008-11-05 20:51:48 +00003917/// [C99] identifier
Reid Spencer5f016e22007-07-11 17:01:13 +00003918/// '(' declarator ')'
3919/// [GNU] '(' attributes declarator ')'
3920/// [C90] direct-declarator '[' constant-expression[opt] ']'
3921/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
3922/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
3923/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
3924/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
3925/// direct-declarator '(' parameter-type-list ')'
3926/// direct-declarator '(' identifier-list[opt] ')'
3927/// [GNU] direct-declarator '(' parameter-forward-declarations
3928/// parameter-type-list[opt] ')'
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003929/// [C++] direct-declarator '(' parameter-declaration-clause ')'
3930/// cv-qualifier-seq[opt] exception-specification[opt]
Douglas Gregorb48fe382008-10-31 09:07:45 +00003931/// [C++] declarator-id
Douglas Gregor42a552f2008-11-05 20:51:48 +00003932///
3933/// declarator-id: [C++ 8]
Douglas Gregora8bc8c92010-12-23 22:44:42 +00003934/// '...'[opt] id-expression
Douglas Gregor42a552f2008-11-05 20:51:48 +00003935/// '::'[opt] nested-name-specifier[opt] type-name
3936///
3937/// id-expression: [C++ 5.1]
3938/// unqualified-id
Douglas Gregordb422df2009-09-25 21:45:23 +00003939/// qualified-id
Douglas Gregor42a552f2008-11-05 20:51:48 +00003940///
3941/// unqualified-id: [C++ 5.1]
Mike Stump1eb44332009-09-09 15:08:12 +00003942/// identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003943/// operator-function-id
Douglas Gregordb422df2009-09-25 21:45:23 +00003944/// conversion-function-id
Mike Stump1eb44332009-09-09 15:08:12 +00003945/// '~' class-name
Douglas Gregor39a8de12009-02-25 19:37:18 +00003946/// template-id
Argyrios Kyrtzidisc7ed9c62008-11-07 22:02:30 +00003947///
Reid Spencer5f016e22007-07-11 17:01:13 +00003948void Parser::ParseDirectDeclarator(Declarator &D) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00003949 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003950
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003951 if (getLang().CPlusPlus && D.mayHaveIdentifier()) {
3952 // ParseDeclaratorInternal might already have parsed the scope.
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00003953 if (D.getCXXScopeSpec().isEmpty()) {
Douglas Gregorefaa93a2011-11-07 17:33:42 +00003954 bool EnteringContext = D.getContext() == Declarator::FileContext ||
3955 D.getContext() == Declarator::MemberContext;
3956 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec(), ParsedType(),
3957 EnteringContext);
John McCall9ba61662010-02-26 08:45:28 +00003958 }
3959
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00003960 if (D.getCXXScopeSpec().isValid()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00003961 if (Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
John McCalle7e278b2009-12-11 20:04:54 +00003962 // Change the declaration context for name lookup, until this function
3963 // is exited (and the declarator has been parsed).
3964 DeclScopeObj.EnterDeclaratorScope();
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00003965 }
3966
Douglas Gregora8bc8c92010-12-23 22:44:42 +00003967 // C++0x [dcl.fct]p14:
3968 // There is a syntactic ambiguity when an ellipsis occurs at the end
3969 // of a parameter-declaration-clause without a preceding comma. In
3970 // this case, the ellipsis is parsed as part of the
3971 // abstract-declarator if the type of the parameter names a template
3972 // parameter pack that has not been expanded; otherwise, it is parsed
3973 // as part of the parameter-declaration-clause.
3974 if (Tok.is(tok::ellipsis) &&
3975 !((D.getContext() == Declarator::PrototypeContext ||
3976 D.getContext() == Declarator::BlockLiteralContext) &&
Douglas Gregora8bc8c92010-12-23 22:44:42 +00003977 NextToken().is(tok::r_paren) &&
3978 !Actions.containsUnexpandedParameterPacks(D)))
3979 D.setEllipsisLoc(ConsumeToken());
3980
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003981 if (Tok.is(tok::identifier) || Tok.is(tok::kw_operator) ||
3982 Tok.is(tok::annot_template_id) || Tok.is(tok::tilde)) {
3983 // We found something that indicates the start of an unqualified-id.
3984 // Parse that unqualified-id.
John McCallba9d8532010-04-13 06:39:49 +00003985 bool AllowConstructorName;
3986 if (D.getDeclSpec().hasTypeSpecifier())
3987 AllowConstructorName = false;
3988 else if (D.getCXXScopeSpec().isSet())
3989 AllowConstructorName =
3990 (D.getContext() == Declarator::FileContext ||
3991 (D.getContext() == Declarator::MemberContext &&
3992 D.getDeclSpec().isFriendSpecified()));
3993 else
3994 AllowConstructorName = (D.getContext() == Declarator::MemberContext);
3995
Abramo Bagnarae4b92762012-01-27 09:46:47 +00003996 SourceLocation TemplateKWLoc;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003997 if (ParseUnqualifiedId(D.getCXXScopeSpec(),
3998 /*EnteringContext=*/true,
3999 /*AllowDestructorName=*/true,
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004000 AllowConstructorName,
John McCallb3d87482010-08-24 05:47:05 +00004001 ParsedType(),
Abramo Bagnarae4b92762012-01-27 09:46:47 +00004002 TemplateKWLoc,
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00004003 D.getName()) ||
4004 // Once we're past the identifier, if the scope was bad, mark the
4005 // whole declarator bad.
4006 D.getCXXScopeSpec().isInvalid()) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00004007 D.SetIdentifier(0, Tok.getLocation());
4008 D.setInvalidType(true);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004009 } else {
4010 // Parsed the unqualified-id; update range information and move along.
4011 if (D.getSourceRange().getBegin().isInvalid())
4012 D.SetRangeBegin(D.getName().getSourceRange().getBegin());
4013 D.SetRangeEnd(D.getName().getSourceRange().getEnd());
Douglas Gregor2f1bc522008-11-07 20:08:42 +00004014 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004015 goto PastIdentifier;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00004016 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004017 } else if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00004018 assert(!getLang().CPlusPlus &&
4019 "There's a C++-specific check for tok::identifier above");
4020 assert(Tok.getIdentifierInfo() && "Not an identifier?");
4021 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
4022 ConsumeToken();
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004023 goto PastIdentifier;
4024 }
4025
4026 if (Tok.is(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00004027 // direct-declarator: '(' declarator ')'
4028 // direct-declarator: '(' attributes declarator ')'
4029 // Example: 'char (*X)' or 'int (*XX)(void)'
4030 ParseParenDeclarator(D);
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004031
4032 // If the declarator was parenthesized, we entered the declarator
4033 // scope when parsing the parenthesized declarator, then exited
4034 // the scope already. Re-enter the scope, if we need to.
4035 if (D.getCXXScopeSpec().isSet()) {
Fariborz Jahanian46877cd2010-08-17 23:50:37 +00004036 // If there was an error parsing parenthesized declarator, declarator
4037 // scope may have been enterred before. Don't do it again.
4038 if (!D.isInvalidType() &&
4039 Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004040 // Change the declaration context for name lookup, until this function
4041 // is exited (and the declarator has been parsed).
Fariborz Jahanian46877cd2010-08-17 23:50:37 +00004042 DeclScopeObj.EnterDeclaratorScope();
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004043 }
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00004044 } else if (D.mayOmitIdentifier()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00004045 // This could be something simple like "int" (in which case the declarator
4046 // portion is empty), if an abstract-declarator is allowed.
4047 D.SetIdentifier(0, Tok.getLocation());
4048 } else {
Douglas Gregore950d4b2009-03-06 23:28:18 +00004049 if (D.getContext() == Declarator::MemberContext)
4050 Diag(Tok, diag::err_expected_member_name_or_semi)
4051 << D.getDeclSpec().getSourceRange();
4052 else if (getLang().CPlusPlus)
Douglas Gregor2d1c2142009-11-03 19:44:04 +00004053 Diag(Tok, diag::err_expected_unqualified_id) << getLang().CPlusPlus;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00004054 else
Chris Lattner1ab3b962008-11-18 07:48:38 +00004055 Diag(Tok, diag::err_expected_ident_lparen);
Reid Spencer5f016e22007-07-11 17:01:13 +00004056 D.SetIdentifier(0, Tok.getLocation());
Chris Lattner1f6f54b2008-11-11 06:13:16 +00004057 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00004058 }
Mike Stump1eb44332009-09-09 15:08:12 +00004059
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00004060 PastIdentifier:
Reid Spencer5f016e22007-07-11 17:01:13 +00004061 assert(D.isPastIdentifier() &&
4062 "Haven't past the location of the identifier yet?");
Mike Stump1eb44332009-09-09 15:08:12 +00004063
Sean Huntbbd37c62009-11-21 08:43:09 +00004064 // Don't parse attributes unless we have an identifier.
John McCall7f040a92010-12-24 02:08:15 +00004065 if (D.getIdentifier())
4066 MaybeParseCXX0XAttributes(D);
Sean Huntbbd37c62009-11-21 08:43:09 +00004067
Reid Spencer5f016e22007-07-11 17:01:13 +00004068 while (1) {
Chris Lattner04d66662007-10-09 17:33:22 +00004069 if (Tok.is(tok::l_paren)) {
David Blaikie42d6d0c2011-12-04 05:04:18 +00004070 // Enter function-declaration scope, limiting any declarators to the
4071 // function prototype scope, including parameter declarators.
4072 ParseScope PrototypeScope(this,
4073 Scope::FunctionPrototypeScope|Scope::DeclScope);
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00004074 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
4075 // In such a case, check if we actually have a function declarator; if it
4076 // is not, the declarator has been fully parsed.
Chris Lattner7399ee02008-10-20 02:05:46 +00004077 if (getLang().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
4078 // When not in file scope, warn for ambiguous function declarators, just
4079 // in case the author intended it as a variable definition.
4080 bool warnIfAmbiguous = D.getContext() != Declarator::FileContext;
4081 if (!isCXXFunctionDeclarator(warnIfAmbiguous))
4082 break;
4083 }
John McCall0b7e6782011-03-24 11:26:52 +00004084 ParsedAttributes attrs(AttrFactory);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004085 BalancedDelimiterTracker T(*this, tok::l_paren);
4086 T.consumeOpen();
4087 ParseFunctionDeclarator(D, attrs, T);
David Blaikie42d6d0c2011-12-04 05:04:18 +00004088 PrototypeScope.Exit();
Chris Lattner04d66662007-10-09 17:33:22 +00004089 } else if (Tok.is(tok::l_square)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00004090 ParseBracketDeclarator(D);
4091 } else {
4092 break;
4093 }
4094 }
David Blaikie42d6d0c2011-12-04 05:04:18 +00004095}
Reid Spencer5f016e22007-07-11 17:01:13 +00004096
Chris Lattneref4715c2008-04-06 05:45:57 +00004097/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
4098/// only called before the identifier, so these are most likely just grouping
Mike Stump1eb44332009-09-09 15:08:12 +00004099/// parens for precedence. If we find that these are actually function
Chris Lattneref4715c2008-04-06 05:45:57 +00004100/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
4101///
4102/// direct-declarator:
4103/// '(' declarator ')'
4104/// [GNU] '(' attributes declarator ')'
Chris Lattner7399ee02008-10-20 02:05:46 +00004105/// direct-declarator '(' parameter-type-list ')'
4106/// direct-declarator '(' identifier-list[opt] ')'
4107/// [GNU] direct-declarator '(' parameter-forward-declarations
4108/// parameter-type-list[opt] ')'
Chris Lattneref4715c2008-04-06 05:45:57 +00004109///
4110void Parser::ParseParenDeclarator(Declarator &D) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004111 BalancedDelimiterTracker T(*this, tok::l_paren);
4112 T.consumeOpen();
4113
Chris Lattneref4715c2008-04-06 05:45:57 +00004114 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
Mike Stump1eb44332009-09-09 15:08:12 +00004115
Chris Lattner7399ee02008-10-20 02:05:46 +00004116 // Eat any attributes before we look at whether this is a grouping or function
4117 // declarator paren. If this is a grouping paren, the attribute applies to
4118 // the type being built up, for example:
4119 // int (__attribute__(()) *x)(long y)
4120 // If this ends up not being a grouping paren, the attribute applies to the
4121 // first argument, for example:
4122 // int (__attribute__(()) int x)
4123 // In either case, we need to eat any attributes to be able to determine what
4124 // sort of paren this is.
4125 //
John McCall0b7e6782011-03-24 11:26:52 +00004126 ParsedAttributes attrs(AttrFactory);
Chris Lattner7399ee02008-10-20 02:05:46 +00004127 bool RequiresArg = false;
4128 if (Tok.is(tok::kw___attribute)) {
John McCall7f040a92010-12-24 02:08:15 +00004129 ParseGNUAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00004130
Chris Lattner7399ee02008-10-20 02:05:46 +00004131 // We require that the argument list (if this is a non-grouping paren) be
4132 // present even if the attribute list was empty.
4133 RequiresArg = true;
4134 }
Steve Naroff239f0732008-12-25 14:16:32 +00004135 // Eat any Microsoft extensions.
Eli Friedman290eeb02009-06-08 23:27:34 +00004136 if (Tok.is(tok::kw___cdecl) || Tok.is(tok::kw___stdcall) ||
Douglas Gregorf813a2c2010-05-18 16:57:00 +00004137 Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___fastcall) ||
Francois Pichet3bd9aa42011-08-18 09:59:55 +00004138 Tok.is(tok::kw___w64) || Tok.is(tok::kw___ptr64) ||
Francois Pichet58fd97a2011-08-25 00:36:46 +00004139 Tok.is(tok::kw___ptr32) || Tok.is(tok::kw___unaligned)) {
John McCall7f040a92010-12-24 02:08:15 +00004140 ParseMicrosoftTypeAttributes(attrs);
Eli Friedman290eeb02009-06-08 23:27:34 +00004141 }
Dawn Perchik52fc3142010-09-03 01:29:35 +00004142 // Eat any Borland extensions.
Ted Kremenek8113ecf2010-11-10 05:59:39 +00004143 if (Tok.is(tok::kw___pascal))
John McCall7f040a92010-12-24 02:08:15 +00004144 ParseBorlandTypeAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00004145
Chris Lattneref4715c2008-04-06 05:45:57 +00004146 // If we haven't past the identifier yet (or where the identifier would be
4147 // stored, if this is an abstract declarator), then this is probably just
4148 // grouping parens. However, if this could be an abstract-declarator, then
4149 // this could also be the start of function arguments (consider 'void()').
4150 bool isGrouping;
Mike Stump1eb44332009-09-09 15:08:12 +00004151
Chris Lattneref4715c2008-04-06 05:45:57 +00004152 if (!D.mayOmitIdentifier()) {
4153 // If this can't be an abstract-declarator, this *must* be a grouping
4154 // paren, because we haven't seen the identifier yet.
4155 isGrouping = true;
4156 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Argyrios Kyrtzidise25d2702008-10-06 00:07:55 +00004157 (getLang().CPlusPlus && Tok.is(tok::ellipsis)) || // C++ int(...)
Chris Lattneref4715c2008-04-06 05:45:57 +00004158 isDeclarationSpecifier()) { // 'int(int)' is a function.
4159 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
4160 // considered to be a type, not a K&R identifier-list.
4161 isGrouping = false;
4162 } else {
4163 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
4164 isGrouping = true;
4165 }
Mike Stump1eb44332009-09-09 15:08:12 +00004166
Chris Lattneref4715c2008-04-06 05:45:57 +00004167 // If this is a grouping paren, handle:
4168 // direct-declarator: '(' declarator ')'
4169 // direct-declarator: '(' attributes declarator ')'
4170 if (isGrouping) {
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00004171 bool hadGroupingParens = D.hasGroupingParens();
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00004172 D.setGroupingParens(true);
4173
Sebastian Redl4c5d3202008-11-21 19:14:01 +00004174 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattneref4715c2008-04-06 05:45:57 +00004175 // Match the ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004176 T.consumeClose();
4177 D.AddTypeInfo(DeclaratorChunk::getParen(T.getOpenLocation(),
4178 T.getCloseLocation()),
4179 attrs, T.getCloseLocation());
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00004180
4181 D.setGroupingParens(hadGroupingParens);
Chris Lattneref4715c2008-04-06 05:45:57 +00004182 return;
4183 }
Mike Stump1eb44332009-09-09 15:08:12 +00004184
Chris Lattneref4715c2008-04-06 05:45:57 +00004185 // Okay, if this wasn't a grouping paren, it must be the start of a function
4186 // argument list. Recognize that this declarator will never have an
Chris Lattner7399ee02008-10-20 02:05:46 +00004187 // identifier (and remember where it would have been), then call into
4188 // ParseFunctionDeclarator to handle of argument list.
Chris Lattneref4715c2008-04-06 05:45:57 +00004189 D.SetIdentifier(0, Tok.getLocation());
4190
David Blaikie42d6d0c2011-12-04 05:04:18 +00004191 // Enter function-declaration scope, limiting any declarators to the
4192 // function prototype scope, including parameter declarators.
4193 ParseScope PrototypeScope(this,
4194 Scope::FunctionPrototypeScope|Scope::DeclScope);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004195 ParseFunctionDeclarator(D, attrs, T, RequiresArg);
David Blaikie42d6d0c2011-12-04 05:04:18 +00004196 PrototypeScope.Exit();
Chris Lattneref4715c2008-04-06 05:45:57 +00004197}
4198
4199/// ParseFunctionDeclarator - We are after the identifier and have parsed the
4200/// declarator D up to a paren, which indicates that we are parsing function
4201/// arguments.
Reid Spencer5f016e22007-07-11 17:01:13 +00004202///
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004203/// If attrs is non-null, then the caller parsed those arguments immediately
Chris Lattner7399ee02008-10-20 02:05:46 +00004204/// after the open paren - they should be considered to be the first argument of
4205/// a parameter. If RequiresArg is true, then the first argument of the
4206/// function is required to be present and required to not be an identifier
4207/// list.
4208///
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004209/// For C++, after the parameter-list, it also parses cv-qualifier-seq[opt],
4210/// (C++0x) ref-qualifier[opt], exception-specification[opt], and
4211/// (C++0x) trailing-return-type[opt].
4212///
4213/// [C++0x] exception-specification:
4214/// dynamic-exception-specification
4215/// noexcept-specification
4216///
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004217void Parser::ParseFunctionDeclarator(Declarator &D,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004218 ParsedAttributes &attrs,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004219 BalancedDelimiterTracker &Tracker,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004220 bool RequiresArg) {
David Blaikie42d6d0c2011-12-04 05:04:18 +00004221 assert(getCurScope()->isFunctionPrototypeScope() &&
4222 "Should call from a Function scope");
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004223 // lparen is already consumed!
4224 assert(D.isPastIdentifier() && "Should not call before identifier!");
4225
4226 // This should be true when the function has typed arguments.
4227 // Otherwise, it is treated as a K&R-style function.
4228 bool HasProto = false;
4229 // Build up an array of information about the parsed arguments.
Chris Lattner5f9e2722011-07-23 10:55:15 +00004230 SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004231 // Remember where we see an ellipsis, if any.
4232 SourceLocation EllipsisLoc;
4233
4234 DeclSpec DS(AttrFactory);
4235 bool RefQualifierIsLValueRef = true;
4236 SourceLocation RefQualifierLoc;
Douglas Gregor43f51032011-10-19 06:04:55 +00004237 SourceLocation ConstQualifierLoc;
4238 SourceLocation VolatileQualifierLoc;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004239 ExceptionSpecificationType ESpecType = EST_None;
4240 SourceRange ESpecRange;
Chris Lattner5f9e2722011-07-23 10:55:15 +00004241 SmallVector<ParsedType, 2> DynamicExceptions;
4242 SmallVector<SourceRange, 2> DynamicExceptionRanges;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004243 ExprResult NoexceptExpr;
4244 ParsedType TrailingReturnType;
4245
James Molloy16f1f712012-02-29 10:24:19 +00004246 Actions.ActOnStartFunctionDeclarator();
4247
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004248 SourceLocation EndLoc;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004249 if (isFunctionDeclaratorIdentifierList()) {
4250 if (RequiresArg)
4251 Diag(Tok, diag::err_argument_required_after_attribute);
4252
4253 ParseFunctionDeclaratorIdentifierList(D, ParamInfo);
4254
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004255 Tracker.consumeClose();
4256 EndLoc = Tracker.getCloseLocation();
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004257 } else {
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004258 if (Tok.isNot(tok::r_paren))
4259 ParseParameterDeclarationClause(D, attrs, ParamInfo, EllipsisLoc);
4260 else if (RequiresArg)
4261 Diag(Tok, diag::err_argument_required_after_attribute);
4262
4263 HasProto = ParamInfo.size() || getLang().CPlusPlus;
4264
4265 // If we have the closing ')', eat it.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004266 Tracker.consumeClose();
4267 EndLoc = Tracker.getCloseLocation();
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004268
4269 if (getLang().CPlusPlus) {
4270 MaybeParseCXX0XAttributes(attrs);
4271
4272 // Parse cv-qualifier-seq[opt].
4273 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Douglas Gregor43f51032011-10-19 06:04:55 +00004274 if (!DS.getSourceRange().getEnd().isInvalid()) {
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004275 EndLoc = DS.getSourceRange().getEnd();
Douglas Gregor43f51032011-10-19 06:04:55 +00004276 ConstQualifierLoc = DS.getConstSpecLoc();
4277 VolatileQualifierLoc = DS.getVolatileSpecLoc();
4278 }
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004279
4280 // Parse ref-qualifier[opt].
4281 if (Tok.is(tok::amp) || Tok.is(tok::ampamp)) {
Richard Smith7fe62082011-10-15 05:09:34 +00004282 Diag(Tok, getLang().CPlusPlus0x ?
4283 diag::warn_cxx98_compat_ref_qualifier :
4284 diag::ext_ref_qualifier);
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004285
4286 RefQualifierIsLValueRef = Tok.is(tok::amp);
4287 RefQualifierLoc = ConsumeToken();
4288 EndLoc = RefQualifierLoc;
4289 }
4290
4291 // Parse exception-specification[opt].
4292 ESpecType = MaybeParseExceptionSpecification(ESpecRange,
4293 DynamicExceptions,
4294 DynamicExceptionRanges,
4295 NoexceptExpr);
4296 if (ESpecType != EST_None)
4297 EndLoc = ESpecRange.getEnd();
4298
4299 // Parse trailing-return-type[opt].
4300 if (getLang().CPlusPlus0x && Tok.is(tok::arrow)) {
Richard Smith7fe62082011-10-15 05:09:34 +00004301 Diag(Tok, diag::warn_cxx98_compat_trailing_return_type);
Douglas Gregorae7902c2011-08-04 15:30:47 +00004302 SourceRange Range;
4303 TrailingReturnType = ParseTrailingReturnType(Range).get();
4304 if (Range.getEnd().isValid())
4305 EndLoc = Range.getEnd();
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004306 }
4307 }
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004308 }
4309
4310 // Remember that we parsed a function type, and remember the attributes.
4311 D.AddTypeInfo(DeclaratorChunk::getFunction(HasProto,
4312 /*isVariadic=*/EllipsisLoc.isValid(),
4313 EllipsisLoc,
4314 ParamInfo.data(), ParamInfo.size(),
4315 DS.getTypeQualifiers(),
4316 RefQualifierIsLValueRef,
Douglas Gregor43f51032011-10-19 06:04:55 +00004317 RefQualifierLoc, ConstQualifierLoc,
4318 VolatileQualifierLoc,
Douglas Gregor90ebed02011-07-13 21:47:47 +00004319 /*MutableLoc=*/SourceLocation(),
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004320 ESpecType, ESpecRange.getBegin(),
4321 DynamicExceptions.data(),
4322 DynamicExceptionRanges.data(),
4323 DynamicExceptions.size(),
4324 NoexceptExpr.isUsable() ?
4325 NoexceptExpr.get() : 0,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004326 Tracker.getOpenLocation(),
4327 EndLoc, D,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004328 TrailingReturnType),
4329 attrs, EndLoc);
James Molloy16f1f712012-02-29 10:24:19 +00004330
4331 Actions.ActOnEndFunctionDeclarator();
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004332}
4333
4334/// isFunctionDeclaratorIdentifierList - This parameter list may have an
4335/// identifier list form for a K&R-style function: void foo(a,b,c)
4336///
4337/// Note that identifier-lists are only allowed for normal declarators, not for
4338/// abstract-declarators.
4339bool Parser::isFunctionDeclaratorIdentifierList() {
4340 return !getLang().CPlusPlus
4341 && Tok.is(tok::identifier)
4342 && !TryAltiVecVectorToken()
4343 // K&R identifier lists can't have typedefs as identifiers, per C99
4344 // 6.7.5.3p11.
4345 && (TryAnnotateTypeOrScopeToken() || !Tok.is(tok::annot_typename))
4346 // Identifier lists follow a really simple grammar: the identifiers can
4347 // be followed *only* by a ", identifier" or ")". However, K&R
4348 // identifier lists are really rare in the brave new modern world, and
4349 // it is very common for someone to typo a type in a non-K&R style
4350 // list. If we are presented with something like: "void foo(intptr x,
4351 // float y)", we don't want to start parsing the function declarator as
4352 // though it is a K&R style declarator just because intptr is an
4353 // invalid type.
4354 //
4355 // To handle this, we check to see if the token after the first
4356 // identifier is a "," or ")". Only then do we parse it as an
4357 // identifier list.
4358 && (NextToken().is(tok::comma) || NextToken().is(tok::r_paren));
4359}
4360
4361/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
4362/// we found a K&R-style identifier list instead of a typed parameter list.
4363///
4364/// After returning, ParamInfo will hold the parsed parameters.
4365///
4366/// identifier-list: [C99 6.7.5]
4367/// identifier
4368/// identifier-list ',' identifier
4369///
4370void Parser::ParseFunctionDeclaratorIdentifierList(
4371 Declarator &D,
Chris Lattner5f9e2722011-07-23 10:55:15 +00004372 SmallVector<DeclaratorChunk::ParamInfo, 16> &ParamInfo) {
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004373 // If there was no identifier specified for the declarator, either we are in
4374 // an abstract-declarator, or we are in a parameter declarator which was found
4375 // to be abstract. In abstract-declarators, identifier lists are not valid:
4376 // diagnose this.
4377 if (!D.getIdentifier())
4378 Diag(Tok, diag::ext_ident_list_in_param);
4379
4380 // Maintain an efficient lookup of params we have seen so far.
4381 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
4382
4383 while (1) {
4384 // If this isn't an identifier, report the error and skip until ')'.
4385 if (Tok.isNot(tok::identifier)) {
4386 Diag(Tok, diag::err_expected_ident);
4387 SkipUntil(tok::r_paren, /*StopAtSemi=*/true, /*DontConsume=*/true);
4388 // Forget we parsed anything.
4389 ParamInfo.clear();
4390 return;
4391 }
4392
4393 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
4394
4395 // Reject 'typedef int y; int test(x, y)', but continue parsing.
4396 if (Actions.getTypeName(*ParmII, Tok.getLocation(), getCurScope()))
4397 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
4398
4399 // Verify that the argument identifier has not already been mentioned.
4400 if (!ParamsSoFar.insert(ParmII)) {
4401 Diag(Tok, diag::err_param_redefinition) << ParmII;
4402 } else {
4403 // Remember this identifier in ParamInfo.
4404 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
4405 Tok.getLocation(),
4406 0));
4407 }
4408
4409 // Eat the identifier.
4410 ConsumeToken();
4411
4412 // The list continues if we see a comma.
4413 if (Tok.isNot(tok::comma))
4414 break;
4415 ConsumeToken();
4416 }
4417}
4418
4419/// ParseParameterDeclarationClause - Parse a (possibly empty) parameter-list
4420/// after the opening parenthesis. This function will not parse a K&R-style
4421/// identifier list.
4422///
4423/// D is the declarator being parsed. If attrs is non-null, then the caller
4424/// parsed those arguments immediately after the open paren - they should be
4425/// considered to be the first argument of a parameter.
4426///
4427/// After returning, ParamInfo will hold the parsed parameters. EllipsisLoc will
4428/// be the location of the ellipsis, if any was parsed.
4429///
Reid Spencer5f016e22007-07-11 17:01:13 +00004430/// parameter-type-list: [C99 6.7.5]
4431/// parameter-list
4432/// parameter-list ',' '...'
Douglas Gregored5d6512009-09-22 21:41:40 +00004433/// [C++] parameter-list '...'
Reid Spencer5f016e22007-07-11 17:01:13 +00004434///
4435/// parameter-list: [C99 6.7.5]
4436/// parameter-declaration
4437/// parameter-list ',' parameter-declaration
4438///
4439/// parameter-declaration: [C99 6.7.5]
4440/// declaration-specifiers declarator
Chris Lattner04421082008-04-08 04:40:51 +00004441/// [C++] declaration-specifiers declarator '=' assignment-expression
Reid Spencer5f016e22007-07-11 17:01:13 +00004442/// [GNU] declaration-specifiers declarator attributes
Sebastian Redl50de12f2009-03-24 22:27:57 +00004443/// declaration-specifiers abstract-declarator[opt]
4444/// [C++] declaration-specifiers abstract-declarator[opt]
Chris Lattner8123a952008-04-10 02:22:51 +00004445/// '=' assignment-expression
Reid Spencer5f016e22007-07-11 17:01:13 +00004446/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
4447///
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004448void Parser::ParseParameterDeclarationClause(
4449 Declarator &D,
4450 ParsedAttributes &attrs,
Chris Lattner5f9e2722011-07-23 10:55:15 +00004451 SmallVector<DeclaratorChunk::ParamInfo, 16> &ParamInfo,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004452 SourceLocation &EllipsisLoc) {
Mike Stump1eb44332009-09-09 15:08:12 +00004453
Chris Lattnerf97409f2008-04-06 06:57:35 +00004454 while (1) {
4455 if (Tok.is(tok::ellipsis)) {
Douglas Gregor965acbb2009-02-18 07:07:28 +00004456 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chris Lattnerf97409f2008-04-06 06:57:35 +00004457 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00004458 }
Mike Stump1eb44332009-09-09 15:08:12 +00004459
Chris Lattnerf97409f2008-04-06 06:57:35 +00004460 // Parse the declaration-specifiers.
John McCall54abf7d2009-11-04 02:18:39 +00004461 // Just use the ParsingDeclaration "scope" of the declarator.
John McCall0b7e6782011-03-24 11:26:52 +00004462 DeclSpec DS(AttrFactory);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004463
John McCall7f040a92010-12-24 02:08:15 +00004464 // Skip any Microsoft attributes before a param.
Francois Pichet62ec1f22011-09-17 17:15:52 +00004465 if (getLang().MicrosoftExt && Tok.is(tok::l_square))
John McCall7f040a92010-12-24 02:08:15 +00004466 ParseMicrosoftAttributes(DS.getAttributes());
4467
4468 SourceLocation DSStart = Tok.getLocation();
Chris Lattner7399ee02008-10-20 02:05:46 +00004469
4470 // If the caller parsed attributes for the first argument, add them now.
John McCall7f040a92010-12-24 02:08:15 +00004471 // Take them so that we only apply the attributes to the first parameter.
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004472 // FIXME: If we saw an ellipsis first, this code is not reached. Are the
4473 // attributes lost? Should they even be allowed?
4474 // FIXME: If we can leave the attributes in the token stream somehow, we can
4475 // get rid of a parameter (attrs) and this statement. It might be too much
4476 // hassle.
John McCall7f040a92010-12-24 02:08:15 +00004477 DS.takeAttributesFrom(attrs);
4478
Chris Lattnere64c5492009-02-27 18:38:20 +00004479 ParseDeclarationSpecifiers(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00004480
Chris Lattnerf97409f2008-04-06 06:57:35 +00004481 // Parse the declarator. This is "PrototypeContext", because we must
4482 // accept either 'declarator' or 'abstract-declarator' here.
4483 Declarator ParmDecl(DS, Declarator::PrototypeContext);
4484 ParseDeclarator(ParmDecl);
4485
4486 // Parse GNU attributes, if present.
John McCall7f040a92010-12-24 02:08:15 +00004487 MaybeParseGNUAttributes(ParmDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00004488
Chris Lattnerf97409f2008-04-06 06:57:35 +00004489 // Remember this parsed parameter in ParamInfo.
4490 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
Mike Stump1eb44332009-09-09 15:08:12 +00004491
Douglas Gregor72b505b2008-12-16 21:30:33 +00004492 // DefArgToks is used when the parsing of default arguments needs
4493 // to be delayed.
4494 CachedTokens *DefArgToks = 0;
4495
Chris Lattnerf97409f2008-04-06 06:57:35 +00004496 // If no parameter was specified, verify that *something* was specified,
4497 // otherwise we have a missing type and identifier.
Chris Lattnere64c5492009-02-27 18:38:20 +00004498 if (DS.isEmpty() && ParmDecl.getIdentifier() == 0 &&
4499 ParmDecl.getNumTypeObjects() == 0) {
Chris Lattnerf97409f2008-04-06 06:57:35 +00004500 // Completely missing, emit error.
4501 Diag(DSStart, diag::err_missing_param);
4502 } else {
4503 // Otherwise, we have something. Add it and let semantic analysis try
4504 // to grok it and add the result to the ParamInfo we are building.
Mike Stump1eb44332009-09-09 15:08:12 +00004505
Chris Lattnerf97409f2008-04-06 06:57:35 +00004506 // Inform the actions module about the parameter declarator, so it gets
4507 // added to the current scope.
John McCalld226f652010-08-21 09:40:31 +00004508 Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl);
Chris Lattner04421082008-04-08 04:40:51 +00004509
4510 // Parse the default argument, if any. We parse the default
4511 // arguments in all dialects; the semantic analysis in
4512 // ActOnParamDefaultArgument will reject the default argument in
4513 // C.
4514 if (Tok.is(tok::equal)) {
Douglas Gregor61366e92008-12-24 00:01:03 +00004515 SourceLocation EqualLoc = Tok.getLocation();
4516
Chris Lattner04421082008-04-08 04:40:51 +00004517 // Parse the default argument
Douglas Gregor72b505b2008-12-16 21:30:33 +00004518 if (D.getContext() == Declarator::MemberContext) {
4519 // If we're inside a class definition, cache the tokens
4520 // corresponding to the default argument. We'll actually parse
4521 // them when we see the end of the class definition.
4522 // FIXME: Templates will require something similar.
4523 // FIXME: Can we use a smart pointer for Toks?
4524 DefArgToks = new CachedTokens;
4525
Mike Stump1eb44332009-09-09 15:08:12 +00004526 if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks,
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +00004527 /*StopAtSemi=*/true,
4528 /*ConsumeFinalToken=*/false)) {
Douglas Gregor72b505b2008-12-16 21:30:33 +00004529 delete DefArgToks;
4530 DefArgToks = 0;
Douglas Gregor61366e92008-12-24 00:01:03 +00004531 Actions.ActOnParamDefaultArgumentError(Param);
Argyrios Kyrtzidis2b602ad2010-08-06 09:47:24 +00004532 } else {
4533 // Mark the end of the default argument so that we know when to
4534 // stop when we parse it later on.
4535 Token DefArgEnd;
4536 DefArgEnd.startToken();
4537 DefArgEnd.setKind(tok::cxx_defaultarg_end);
4538 DefArgEnd.setLocation(Tok.getLocation());
4539 DefArgToks->push_back(DefArgEnd);
Mike Stump1eb44332009-09-09 15:08:12 +00004540 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc,
Anders Carlsson5e300d12009-06-12 16:51:40 +00004541 (*DefArgToks)[1].getLocation());
Argyrios Kyrtzidis2b602ad2010-08-06 09:47:24 +00004542 }
Chris Lattner04421082008-04-08 04:40:51 +00004543 } else {
Douglas Gregor72b505b2008-12-16 21:30:33 +00004544 // Consume the '='.
Douglas Gregor61366e92008-12-24 00:01:03 +00004545 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00004546
Douglas Gregorbe0f7bd2010-09-11 20:24:53 +00004547 // The argument isn't actually potentially evaluated unless it is
4548 // used.
4549 EnterExpressionEvaluationContext Eval(Actions,
Douglas Gregorccc1b5e2012-02-21 00:37:24 +00004550 Sema::PotentiallyEvaluatedIfUsed,
4551 Param);
Douglas Gregorbe0f7bd2010-09-11 20:24:53 +00004552
John McCall60d7b3a2010-08-24 06:29:42 +00004553 ExprResult DefArgResult(ParseAssignmentExpression());
Douglas Gregor72b505b2008-12-16 21:30:33 +00004554 if (DefArgResult.isInvalid()) {
4555 Actions.ActOnParamDefaultArgumentError(Param);
4556 SkipUntil(tok::comma, tok::r_paren, true, true);
4557 } else {
4558 // Inform the actions module about the default argument
4559 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
John McCall9ae2f072010-08-23 23:25:46 +00004560 DefArgResult.take());
Douglas Gregor72b505b2008-12-16 21:30:33 +00004561 }
Chris Lattner04421082008-04-08 04:40:51 +00004562 }
4563 }
Mike Stump1eb44332009-09-09 15:08:12 +00004564
4565 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
4566 ParmDecl.getIdentifierLoc(), Param,
Douglas Gregor72b505b2008-12-16 21:30:33 +00004567 DefArgToks));
Chris Lattnerf97409f2008-04-06 06:57:35 +00004568 }
4569
4570 // If the next token is a comma, consume it and keep reading arguments.
Douglas Gregored5d6512009-09-22 21:41:40 +00004571 if (Tok.isNot(tok::comma)) {
4572 if (Tok.is(tok::ellipsis)) {
Douglas Gregored5d6512009-09-22 21:41:40 +00004573 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
4574
4575 if (!getLang().CPlusPlus) {
4576 // We have ellipsis without a preceding ',', which is ill-formed
4577 // in C. Complain and provide the fix.
4578 Diag(EllipsisLoc, diag::err_missing_comma_before_ellipsis)
Douglas Gregor849b2432010-03-31 17:46:05 +00004579 << FixItHint::CreateInsertion(EllipsisLoc, ", ");
Douglas Gregored5d6512009-09-22 21:41:40 +00004580 }
4581 }
4582
4583 break;
4584 }
Mike Stump1eb44332009-09-09 15:08:12 +00004585
Chris Lattnerf97409f2008-04-06 06:57:35 +00004586 // Consume the comma.
4587 ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00004588 }
Mike Stump1eb44332009-09-09 15:08:12 +00004589
Chris Lattner66d28652008-04-06 06:34:08 +00004590}
Chris Lattneref4715c2008-04-06 05:45:57 +00004591
Reid Spencer5f016e22007-07-11 17:01:13 +00004592/// [C90] direct-declarator '[' constant-expression[opt] ']'
4593/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
4594/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
4595/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
4596/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
4597void Parser::ParseBracketDeclarator(Declarator &D) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004598 BalancedDelimiterTracker T(*this, tok::l_square);
4599 T.consumeOpen();
Mike Stump1eb44332009-09-09 15:08:12 +00004600
Chris Lattner378c7e42008-12-18 07:27:21 +00004601 // C array syntax has many features, but by-far the most common is [] and [4].
4602 // This code does a fast path to handle some of the most obvious cases.
4603 if (Tok.getKind() == tok::r_square) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004604 T.consumeClose();
John McCall0b7e6782011-03-24 11:26:52 +00004605 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00004606 MaybeParseCXX0XAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00004607
Chris Lattner378c7e42008-12-18 07:27:21 +00004608 // Remember that we parsed the empty array type.
John McCall60d7b3a2010-08-24 06:29:42 +00004609 ExprResult NumElements;
John McCall0b7e6782011-03-24 11:26:52 +00004610 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004611 T.getOpenLocation(),
4612 T.getCloseLocation()),
4613 attrs, T.getCloseLocation());
Chris Lattner378c7e42008-12-18 07:27:21 +00004614 return;
4615 } else if (Tok.getKind() == tok::numeric_constant &&
4616 GetLookAheadToken(1).is(tok::r_square)) {
4617 // [4] is very common. Parse the numeric constant expression.
John McCall60d7b3a2010-08-24 06:29:42 +00004618 ExprResult ExprRes(Actions.ActOnNumericConstant(Tok));
Chris Lattner378c7e42008-12-18 07:27:21 +00004619 ConsumeToken();
4620
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004621 T.consumeClose();
John McCall0b7e6782011-03-24 11:26:52 +00004622 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00004623 MaybeParseCXX0XAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00004624
Chris Lattner378c7e42008-12-18 07:27:21 +00004625 // Remember that we parsed a array type, and remember its features.
John McCall0b7e6782011-03-24 11:26:52 +00004626 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, 0,
John McCall7f040a92010-12-24 02:08:15 +00004627 ExprRes.release(),
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004628 T.getOpenLocation(),
4629 T.getCloseLocation()),
4630 attrs, T.getCloseLocation());
Chris Lattner378c7e42008-12-18 07:27:21 +00004631 return;
4632 }
Mike Stump1eb44332009-09-09 15:08:12 +00004633
Reid Spencer5f016e22007-07-11 17:01:13 +00004634 // If valid, this location is the position where we read the 'static' keyword.
4635 SourceLocation StaticLoc;
Chris Lattner04d66662007-10-09 17:33:22 +00004636 if (Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00004637 StaticLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00004638
Reid Spencer5f016e22007-07-11 17:01:13 +00004639 // If there is a type-qualifier-list, read it now.
Chris Lattnera1fcbad2008-12-18 06:50:14 +00004640 // Type qualifiers in an array subscript are a C99 feature.
John McCall0b7e6782011-03-24 11:26:52 +00004641 DeclSpec DS(AttrFactory);
Chris Lattner5a69d1c2008-12-18 07:02:59 +00004642 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Mike Stump1eb44332009-09-09 15:08:12 +00004643
Reid Spencer5f016e22007-07-11 17:01:13 +00004644 // If we haven't already read 'static', check to see if there is one after the
4645 // type-qualifier-list.
Chris Lattner04d66662007-10-09 17:33:22 +00004646 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00004647 StaticLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00004648
Reid Spencer5f016e22007-07-11 17:01:13 +00004649 // Handle "direct-declarator [ type-qual-list[opt] * ]".
4650 bool isStar = false;
John McCall60d7b3a2010-08-24 06:29:42 +00004651 ExprResult NumElements;
Mike Stump1eb44332009-09-09 15:08:12 +00004652
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00004653 // Handle the case where we have '[*]' as the array size. However, a leading
4654 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
4655 // the the token after the star is a ']'. Since stars in arrays are
4656 // infrequent, use of lookahead is not costly here.
4657 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattnera711dd02008-04-06 05:27:21 +00004658 ConsumeToken(); // Eat the '*'.
Reid Spencer5f016e22007-07-11 17:01:13 +00004659
Chris Lattnera1fcbad2008-12-18 06:50:14 +00004660 if (StaticLoc.isValid()) {
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00004661 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattnera1fcbad2008-12-18 06:50:14 +00004662 StaticLoc = SourceLocation(); // Drop the static.
4663 }
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00004664 isStar = true;
Chris Lattner04d66662007-10-09 17:33:22 +00004665 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner378c7e42008-12-18 07:27:21 +00004666 // Note, in C89, this production uses the constant-expr production instead
4667 // of assignment-expr. The only difference is that assignment-expr allows
4668 // things like '=' and '*='. Sema rejects these in C89 mode because they
4669 // are not i-c-e's, so we don't need to distinguish between the two here.
Mike Stump1eb44332009-09-09 15:08:12 +00004670
Douglas Gregore0762c92009-06-19 23:52:42 +00004671 // Parse the constant-expression or assignment-expression now (depending
4672 // on dialect).
Eli Friedman71b8fb52012-01-21 01:01:51 +00004673 if (getLang().CPlusPlus) {
Douglas Gregore0762c92009-06-19 23:52:42 +00004674 NumElements = ParseConstantExpression();
Eli Friedman71b8fb52012-01-21 01:01:51 +00004675 } else {
4676 EnterExpressionEvaluationContext Unevaluated(Actions,
4677 Sema::ConstantEvaluated);
Douglas Gregore0762c92009-06-19 23:52:42 +00004678 NumElements = ParseAssignmentExpression();
Eli Friedman71b8fb52012-01-21 01:01:51 +00004679 }
Reid Spencer5f016e22007-07-11 17:01:13 +00004680 }
Mike Stump1eb44332009-09-09 15:08:12 +00004681
Reid Spencer5f016e22007-07-11 17:01:13 +00004682 // If there was an error parsing the assignment-expression, recover.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00004683 if (NumElements.isInvalid()) {
Chris Lattner5cb10d32009-04-24 22:30:50 +00004684 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00004685 // If the expression was invalid, skip it.
4686 SkipUntil(tok::r_square);
4687 return;
4688 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00004689
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004690 T.consumeClose();
Sebastian Redlab197ba2009-02-09 18:23:29 +00004691
John McCall0b7e6782011-03-24 11:26:52 +00004692 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00004693 MaybeParseCXX0XAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00004694
Chris Lattner378c7e42008-12-18 07:27:21 +00004695 // Remember that we parsed a array type, and remember its features.
John McCall0b7e6782011-03-24 11:26:52 +00004696 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
Reid Spencer5f016e22007-07-11 17:01:13 +00004697 StaticLoc.isValid(), isStar,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00004698 NumElements.release(),
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004699 T.getOpenLocation(),
4700 T.getCloseLocation()),
4701 attrs, T.getCloseLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00004702}
4703
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004704/// [GNU] typeof-specifier:
4705/// typeof ( expressions )
4706/// typeof ( type-name )
4707/// [GNU/C++] typeof unary-expression
Steve Naroffd1861fd2007-07-31 12:34:36 +00004708///
4709void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner04d66662007-10-09 17:33:22 +00004710 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004711 Token OpTok = Tok;
Steve Naroffd1861fd2007-07-31 12:34:36 +00004712 SourceLocation StartLoc = ConsumeToken();
4713
John McCallcfb708c2010-01-13 20:03:27 +00004714 const bool hasParens = Tok.is(tok::l_paren);
4715
Eli Friedman71b8fb52012-01-21 01:01:51 +00004716 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated);
4717
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004718 bool isCastExpr;
John McCallb3d87482010-08-24 05:47:05 +00004719 ParsedType CastTy;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004720 SourceRange CastRange;
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00004721 ExprResult Operand = ParseExprAfterUnaryExprOrTypeTrait(OpTok, isCastExpr,
4722 CastTy, CastRange);
John McCallcfb708c2010-01-13 20:03:27 +00004723 if (hasParens)
4724 DS.setTypeofParensRange(CastRange);
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004725
4726 if (CastRange.getEnd().isInvalid())
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004727 // FIXME: Not accurate, the range gets one token more than it should.
4728 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004729 else
4730 DS.SetRangeEnd(CastRange.getEnd());
Mike Stump1eb44332009-09-09 15:08:12 +00004731
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004732 if (isCastExpr) {
4733 if (!CastTy) {
4734 DS.SetTypeSpecError();
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004735 return;
Douglas Gregor809070a2009-02-18 17:45:20 +00004736 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004737
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004738 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00004739 unsigned DiagID;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004740 // Check for duplicate type specifiers (e.g. "int typeof(int)").
4741 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00004742 DiagID, CastTy))
4743 Diag(StartLoc, DiagID) << PrevSpec;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004744 return;
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004745 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004746
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004747 // If we get here, the operand to the typeof was an expresion.
4748 if (Operand.isInvalid()) {
4749 DS.SetTypeSpecError();
Steve Naroff9dfa7b42007-08-02 02:53:48 +00004750 return;
Steve Naroffd1861fd2007-07-31 12:34:36 +00004751 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004752
Eli Friedman71b8fb52012-01-21 01:01:51 +00004753 // We might need to transform the operand if it is potentially evaluated.
4754 Operand = Actions.HandleExprEvaluationContextForTypeof(Operand.get());
4755 if (Operand.isInvalid()) {
4756 DS.SetTypeSpecError();
4757 return;
4758 }
4759
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004760 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00004761 unsigned DiagID;
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004762 // Check for duplicate type specifiers (e.g. "int typeof(int)").
4763 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
John McCallb3d87482010-08-24 05:47:05 +00004764 DiagID, Operand.get()))
John McCallfec54012009-08-03 20:12:06 +00004765 Diag(StartLoc, DiagID) << PrevSpec;
Steve Naroffd1861fd2007-07-31 12:34:36 +00004766}
Chris Lattner1b492422010-02-28 18:33:55 +00004767
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00004768/// [C11] atomic-specifier:
Eli Friedmanb001de72011-10-06 23:00:33 +00004769/// _Atomic ( type-name )
4770///
4771void Parser::ParseAtomicSpecifier(DeclSpec &DS) {
4772 assert(Tok.is(tok::kw__Atomic) && "Not an atomic specifier");
4773
4774 SourceLocation StartLoc = ConsumeToken();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004775 BalancedDelimiterTracker T(*this, tok::l_paren);
4776 if (T.expectAndConsume(diag::err_expected_lparen_after, "_Atomic")) {
Eli Friedmanb001de72011-10-06 23:00:33 +00004777 SkipUntil(tok::r_paren);
4778 return;
4779 }
4780
4781 TypeResult Result = ParseTypeName();
4782 if (Result.isInvalid()) {
4783 SkipUntil(tok::r_paren);
4784 return;
4785 }
4786
4787 // Match the ')'
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004788 T.consumeClose();
Eli Friedmanb001de72011-10-06 23:00:33 +00004789
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004790 if (T.getCloseLocation().isInvalid())
Eli Friedmanb001de72011-10-06 23:00:33 +00004791 return;
4792
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004793 DS.setTypeofParensRange(T.getRange());
4794 DS.SetRangeEnd(T.getCloseLocation());
Eli Friedmanb001de72011-10-06 23:00:33 +00004795
4796 const char *PrevSpec = 0;
4797 unsigned DiagID;
4798 if (DS.SetTypeSpecType(DeclSpec::TST_atomic, StartLoc, PrevSpec,
4799 DiagID, Result.release()))
4800 Diag(StartLoc, DiagID) << PrevSpec;
4801}
4802
Chris Lattner1b492422010-02-28 18:33:55 +00004803
4804/// TryAltiVecVectorTokenOutOfLine - Out of line body that should only be called
4805/// from TryAltiVecVectorToken.
4806bool Parser::TryAltiVecVectorTokenOutOfLine() {
4807 Token Next = NextToken();
4808 switch (Next.getKind()) {
4809 default: return false;
4810 case tok::kw_short:
4811 case tok::kw_long:
4812 case tok::kw_signed:
4813 case tok::kw_unsigned:
4814 case tok::kw_void:
4815 case tok::kw_char:
4816 case tok::kw_int:
4817 case tok::kw_float:
4818 case tok::kw_double:
4819 case tok::kw_bool:
4820 case tok::kw___pixel:
4821 Tok.setKind(tok::kw___vector);
4822 return true;
4823 case tok::identifier:
4824 if (Next.getIdentifierInfo() == Ident_pixel) {
4825 Tok.setKind(tok::kw___vector);
4826 return true;
4827 }
4828 return false;
4829 }
4830}
4831
4832bool Parser::TryAltiVecTokenOutOfLine(DeclSpec &DS, SourceLocation Loc,
4833 const char *&PrevSpec, unsigned &DiagID,
4834 bool &isInvalid) {
4835 if (Tok.getIdentifierInfo() == Ident_vector) {
4836 Token Next = NextToken();
4837 switch (Next.getKind()) {
4838 case tok::kw_short:
4839 case tok::kw_long:
4840 case tok::kw_signed:
4841 case tok::kw_unsigned:
4842 case tok::kw_void:
4843 case tok::kw_char:
4844 case tok::kw_int:
4845 case tok::kw_float:
4846 case tok::kw_double:
4847 case tok::kw_bool:
4848 case tok::kw___pixel:
4849 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
4850 return true;
4851 case tok::identifier:
4852 if (Next.getIdentifierInfo() == Ident_pixel) {
4853 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
4854 return true;
4855 }
4856 break;
4857 default:
4858 break;
4859 }
Douglas Gregora8f031f2010-06-16 15:28:57 +00004860 } else if ((Tok.getIdentifierInfo() == Ident_pixel) &&
Chris Lattner1b492422010-02-28 18:33:55 +00004861 DS.isTypeAltiVecVector()) {
4862 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
4863 return true;
4864 }
4865 return false;
4866}