blob: 2d8271db407841c055f2a02d743633892681f56b [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) {
745 LAs[i]->setDecl(D);
746 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
Caitlin Sadowskied9d84a2011-09-08 17:42:31 +0000777 // If the Decl is templatized, add template parameters to scope.
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000778 bool HasTemplateScope = EnterScope && LA.D && LA.D->isTemplateDecl();
Caitlin Sadowskied9d84a2011-09-08 17:42:31 +0000779 ParseScope TempScope(this, Scope::TemplateParamScope, HasTemplateScope);
780 if (HasTemplateScope)
781 Actions.ActOnReenterTemplateScope(Actions.CurScope, LA.D);
782
783 // If the Decl is on a function, add function parameters to the scope.
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000784 bool HasFunctionScope = EnterScope && LA.D &&
785 LA.D->isFunctionOrFunctionTemplate();
Caitlin Sadowskied9d84a2011-09-08 17:42:31 +0000786 ParseScope FnScope(this, Scope::FnScope|Scope::DeclScope, HasFunctionScope);
787 if (HasFunctionScope)
788 Actions.ActOnReenterFunctionContext(Actions.CurScope, LA.D);
789
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000790 ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, &endLoc);
791
Caitlin Sadowskied9d84a2011-09-08 17:42:31 +0000792 if (HasFunctionScope) {
793 Actions.ActOnExitFunctionContext();
794 FnScope.Exit(); // Pop scope, and remove Decls from IdResolver
795 }
796 if (HasTemplateScope) {
797 TempScope.Exit();
798 }
799
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000800 // Late parsed attributes must be attached to Decls by hand. If the
801 // LA.D is not set, then this was not done properly.
802 assert(LA.D && "No decl attached to late parsed attribute");
803 Actions.ActOnFinishDelayedAttribute(getCurScope(), LA.D, Attrs);
804
805 if (Tok.getLocation() != OrigLoc) {
806 // Due to a parsing error, we either went over the cached tokens or
807 // there are still cached tokens left, so we skip the leftover tokens.
808 // Since this is an uncommon situation that should be avoided, use the
809 // expensive isBeforeInTranslationUnit call.
810 if (PP.getSourceManager().isBeforeInTranslationUnit(Tok.getLocation(),
811 OrigLoc))
812 while (Tok.getLocation() != OrigLoc && Tok.isNot(tok::eof))
813 ConsumeAnyToken();
814 }
815}
816
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000817/// \brief Wrapper around a case statement checking if AttrName is
818/// one of the thread safety attributes
819bool Parser::IsThreadSafetyAttribute(llvm::StringRef AttrName){
820 return llvm::StringSwitch<bool>(AttrName)
821 .Case("guarded_by", true)
822 .Case("guarded_var", true)
823 .Case("pt_guarded_by", true)
824 .Case("pt_guarded_var", true)
825 .Case("lockable", true)
826 .Case("scoped_lockable", true)
827 .Case("no_thread_safety_analysis", true)
828 .Case("acquired_after", true)
829 .Case("acquired_before", true)
830 .Case("exclusive_lock_function", true)
831 .Case("shared_lock_function", true)
832 .Case("exclusive_trylock_function", true)
833 .Case("shared_trylock_function", true)
834 .Case("unlock_function", true)
835 .Case("lock_returned", true)
836 .Case("locks_excluded", true)
837 .Case("exclusive_locks_required", true)
838 .Case("shared_locks_required", true)
839 .Default(false);
840}
841
842/// \brief Parse the contents of thread safety attributes. These
843/// should always be parsed as an expression list.
844///
845/// We need to special case the parsing due to the fact that if the first token
846/// of the first argument is an identifier, the main parse loop will store
847/// that token as a "parameter" and the rest of
848/// the arguments will be added to a list of "arguments". However,
849/// subsequent tokens in the first argument are lost. We instead parse each
850/// argument as an expression and add all arguments to the list of "arguments".
851/// In future, we will take advantage of this special case to also
852/// deal with some argument scoping issues here (for example, referring to a
853/// function parameter in the attribute on that function).
854void Parser::ParseThreadSafetyAttribute(IdentifierInfo &AttrName,
855 SourceLocation AttrNameLoc,
856 ParsedAttributes &Attrs,
857 SourceLocation *EndLoc) {
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000858 assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('");
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000859
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000860 BalancedDelimiterTracker T(*this, tok::l_paren);
861 T.consumeOpen();
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000862
863 ExprVector ArgExprs(Actions);
864 bool ArgExprsOk = true;
865
866 // now parse the list of expressions
DeLesley Hutchins4805f152011-12-14 19:36:06 +0000867 while (Tok.isNot(tok::r_paren)) {
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000868 ExprResult ArgExpr(ParseAssignmentExpression());
869 if (ArgExpr.isInvalid()) {
870 ArgExprsOk = false;
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000871 T.consumeClose();
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000872 break;
873 } else {
874 ArgExprs.push_back(ArgExpr.release());
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000875 }
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000876 if (Tok.isNot(tok::comma))
877 break;
878 ConsumeToken(); // Eat the comma, move to the next argument
879 }
880 // Match the ')'.
DeLesley Hutchins23323e02012-01-20 22:50:54 +0000881 if (ArgExprsOk && !T.consumeClose()) {
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000882 Attrs.addNew(&AttrName, AttrNameLoc, 0, AttrNameLoc, 0, SourceLocation(),
883 ArgExprs.take(), ArgExprs.size());
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000884 }
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000885 if (EndLoc)
886 *EndLoc = T.getCloseLocation();
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000887}
888
John McCall7f040a92010-12-24 02:08:15 +0000889void Parser::DiagnoseProhibitedAttributes(ParsedAttributesWithRange &attrs) {
890 Diag(attrs.Range.getBegin(), diag::err_attributes_not_allowed)
891 << attrs.Range;
Dawn Perchik52fc3142010-09-03 01:29:35 +0000892}
893
Reid Spencer5f016e22007-07-11 17:01:13 +0000894/// ParseDeclaration - Parse a full 'declaration', which consists of
895/// declaration-specifiers, some number of declarators, and a semicolon.
Chris Lattner97144fc2009-04-02 04:16:50 +0000896/// 'Context' should be a Declarator::TheContext value. This returns the
897/// location of the semicolon in DeclEnd.
Chris Lattner8f08cb72007-08-25 06:57:03 +0000898///
899/// declaration: [C99 6.7]
900/// block-declaration ->
901/// simple-declaration
902/// others [FIXME]
Douglas Gregoradcac882008-12-01 23:54:00 +0000903/// [C++] template-declaration
Chris Lattner8f08cb72007-08-25 06:57:03 +0000904/// [C++] namespace-definition
Douglas Gregorf780abc2008-12-30 03:27:21 +0000905/// [C++] using-directive
Douglas Gregord7f37bf2009-06-22 23:06:13 +0000906/// [C++] using-declaration
Benjamin Kramerffbe9b92011-12-23 17:00:35 +0000907/// [C++0x/C11] static_assert-declaration
Chris Lattner8f08cb72007-08-25 06:57:03 +0000908/// others... [FIXME]
909///
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +0000910Parser::DeclGroupPtrTy Parser::ParseDeclaration(StmtVector &Stmts,
911 unsigned Context,
Sean Huntbbd37c62009-11-21 08:43:09 +0000912 SourceLocation &DeclEnd,
John McCall7f040a92010-12-24 02:08:15 +0000913 ParsedAttributesWithRange &attrs) {
Argyrios Kyrtzidis36d36802010-06-17 10:52:18 +0000914 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Fariborz Jahaniane8cff362011-08-30 17:10:52 +0000915 // Must temporarily exit the objective-c container scope for
916 // parsing c none objective-c decls.
917 ObjCDeclContextSwitch ObjCDC(*this);
Argyrios Kyrtzidis36d36802010-06-17 10:52:18 +0000918
John McCalld226f652010-08-21 09:40:31 +0000919 Decl *SingleDecl = 0;
Richard Smithc89edf52011-07-01 19:46:12 +0000920 Decl *OwnedType = 0;
Chris Lattner8f08cb72007-08-25 06:57:03 +0000921 switch (Tok.getKind()) {
Douglas Gregoradcac882008-12-01 23:54:00 +0000922 case tok::kw_template:
Douglas Gregor1426e532009-05-12 21:31:51 +0000923 case tok::kw_export:
John McCall7f040a92010-12-24 02:08:15 +0000924 ProhibitAttributes(attrs);
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000925 SingleDecl = ParseDeclarationStartingWithTemplate(Context, DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +0000926 break;
Sebastian Redld078e642010-08-27 23:12:46 +0000927 case tok::kw_inline:
Sebastian Redl88e64ca2010-08-31 00:36:45 +0000928 // Could be the start of an inline namespace. Allowed as an ext in C++03.
929 if (getLang().CPlusPlus && NextToken().is(tok::kw_namespace)) {
John McCall7f040a92010-12-24 02:08:15 +0000930 ProhibitAttributes(attrs);
Sebastian Redld078e642010-08-27 23:12:46 +0000931 SourceLocation InlineLoc = ConsumeToken();
932 SingleDecl = ParseNamespace(Context, DeclEnd, InlineLoc);
933 break;
934 }
John McCall7f040a92010-12-24 02:08:15 +0000935 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs,
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +0000936 true);
Chris Lattner8f08cb72007-08-25 06:57:03 +0000937 case tok::kw_namespace:
John McCall7f040a92010-12-24 02:08:15 +0000938 ProhibitAttributes(attrs);
Chris Lattner97144fc2009-04-02 04:16:50 +0000939 SingleDecl = ParseNamespace(Context, DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +0000940 break;
Douglas Gregorf780abc2008-12-30 03:27:21 +0000941 case tok::kw_using:
John McCall78b81052010-11-10 02:40:36 +0000942 SingleDecl = ParseUsingDirectiveOrDeclaration(Context, ParsedTemplateInfo(),
Richard Smithc89edf52011-07-01 19:46:12 +0000943 DeclEnd, attrs, &OwnedType);
Chris Lattner682bf922009-03-29 16:50:03 +0000944 break;
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000945 case tok::kw_static_assert:
Peter Collingbournec6eb44b2011-04-15 00:35:57 +0000946 case tok::kw__Static_assert:
John McCall7f040a92010-12-24 02:08:15 +0000947 ProhibitAttributes(attrs);
Chris Lattner97144fc2009-04-02 04:16:50 +0000948 SingleDecl = ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +0000949 break;
Chris Lattner8f08cb72007-08-25 06:57:03 +0000950 default:
John McCall7f040a92010-12-24 02:08:15 +0000951 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs, true);
Chris Lattner8f08cb72007-08-25 06:57:03 +0000952 }
Sean Huntbbd37c62009-11-21 08:43:09 +0000953
Chris Lattner682bf922009-03-29 16:50:03 +0000954 // This routine returns a DeclGroup, if the thing we parsed only contains a
Richard Smithc89edf52011-07-01 19:46:12 +0000955 // single decl, convert it now. Alias declarations can also declare a type;
956 // include that too if it is present.
957 return Actions.ConvertDeclToDeclGroup(SingleDecl, OwnedType);
Chris Lattner8f08cb72007-08-25 06:57:03 +0000958}
959
960/// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl]
961/// declaration-specifiers init-declarator-list[opt] ';'
962///[C90/C++]init-declarator-list ';' [TODO]
963/// [OMP] threadprivate-directive [TODO]
Chris Lattnercd147752009-03-29 17:27:48 +0000964///
Richard Smithad762fc2011-04-14 22:09:26 +0000965/// for-range-declaration: [C++0x 6.5p1: stmt.ranged]
966/// attribute-specifier-seq[opt] type-specifier-seq declarator
967///
Chris Lattnercd147752009-03-29 17:27:48 +0000968/// If RequireSemi is false, this does not check for a ';' at the end of the
Chris Lattner5c5db552010-04-05 18:18:31 +0000969/// declaration. If it is true, it checks for and eats it.
Richard Smithad762fc2011-04-14 22:09:26 +0000970///
971/// If FRI is non-null, we might be parsing a for-range-declaration instead
972/// of a simple-declaration. If we find that we are, we also parse the
973/// for-range-initializer, and place it here.
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +0000974Parser::DeclGroupPtrTy Parser::ParseSimpleDeclaration(StmtVector &Stmts,
975 unsigned Context,
Sean Huntbbd37c62009-11-21 08:43:09 +0000976 SourceLocation &DeclEnd,
John McCall7f040a92010-12-24 02:08:15 +0000977 ParsedAttributes &attrs,
Richard Smithad762fc2011-04-14 22:09:26 +0000978 bool RequireSemi,
979 ForRangeInit *FRI) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000980 // Parse the common declaration-specifiers piece.
John McCall54abf7d2009-11-04 02:18:39 +0000981 ParsingDeclSpec DS(*this);
John McCall7f040a92010-12-24 02:08:15 +0000982 DS.takeAttributesFrom(attrs);
Douglas Gregor312eadb2011-04-24 05:37:28 +0000983
Douglas Gregor0efc2c12010-01-13 17:31:36 +0000984 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS_none,
Richard Smith34b41d92011-02-20 03:19:35 +0000985 getDeclSpecContextFromDeclaratorContext(Context));
Abramo Bagnara06284c12012-01-07 10:52:36 +0000986
Reid Spencer5f016e22007-07-11 17:01:13 +0000987 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
988 // declaration-specifiers init-declarator-list[opt] ';'
Chris Lattner04d66662007-10-09 17:33:22 +0000989 if (Tok.is(tok::semi)) {
Chris Lattner5c5db552010-04-05 18:18:31 +0000990 if (RequireSemi) ConsumeToken();
John McCalld226f652010-08-21 09:40:31 +0000991 Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none,
Douglas Gregor312eadb2011-04-24 05:37:28 +0000992 DS);
John McCall54abf7d2009-11-04 02:18:39 +0000993 DS.complete(TheDecl);
Chris Lattner682bf922009-03-29 16:50:03 +0000994 return Actions.ConvertDeclToDeclGroup(TheDecl);
Reid Spencer5f016e22007-07-11 17:01:13 +0000995 }
Douglas Gregor312eadb2011-04-24 05:37:28 +0000996
997 return ParseDeclGroup(DS, Context, /*FunctionDefs=*/ false, &DeclEnd, FRI);
John McCalld8ac0572009-11-03 19:26:08 +0000998}
Mike Stump1eb44332009-09-09 15:08:12 +0000999
Richard Smith0706df42011-10-19 21:33:05 +00001000/// Returns true if this might be the start of a declarator, or a common typo
1001/// for a declarator.
1002bool Parser::MightBeDeclarator(unsigned Context) {
1003 switch (Tok.getKind()) {
1004 case tok::annot_cxxscope:
1005 case tok::annot_template_id:
1006 case tok::caret:
1007 case tok::code_completion:
1008 case tok::coloncolon:
1009 case tok::ellipsis:
1010 case tok::kw___attribute:
1011 case tok::kw_operator:
1012 case tok::l_paren:
1013 case tok::star:
1014 return true;
1015
1016 case tok::amp:
1017 case tok::ampamp:
Richard Smith0706df42011-10-19 21:33:05 +00001018 return getLang().CPlusPlus;
1019
Richard Smith1c94c162012-01-09 22:31:44 +00001020 case tok::l_square: // Might be an attribute on an unnamed bit-field.
1021 return Context == Declarator::MemberContext && getLang().CPlusPlus0x &&
1022 NextToken().is(tok::l_square);
1023
1024 case tok::colon: // Might be a typo for '::' or an unnamed bit-field.
1025 return Context == Declarator::MemberContext || getLang().CPlusPlus;
1026
Richard Smith0706df42011-10-19 21:33:05 +00001027 case tok::identifier:
1028 switch (NextToken().getKind()) {
1029 case tok::code_completion:
1030 case tok::coloncolon:
1031 case tok::comma:
1032 case tok::equal:
1033 case tok::equalequal: // Might be a typo for '='.
1034 case tok::kw_alignas:
1035 case tok::kw_asm:
1036 case tok::kw___attribute:
1037 case tok::l_brace:
1038 case tok::l_paren:
1039 case tok::l_square:
1040 case tok::less:
1041 case tok::r_brace:
1042 case tok::r_paren:
1043 case tok::r_square:
1044 case tok::semi:
1045 return true;
1046
1047 case tok::colon:
1048 // At namespace scope, 'identifier:' is probably a typo for 'identifier::'
Richard Smith1c94c162012-01-09 22:31:44 +00001049 // and in block scope it's probably a label. Inside a class definition,
1050 // this is a bit-field.
1051 return Context == Declarator::MemberContext ||
1052 (getLang().CPlusPlus && Context == Declarator::FileContext);
1053
1054 case tok::identifier: // Possible virt-specifier.
1055 return getLang().CPlusPlus0x && isCXX0XVirtSpecifier(NextToken());
Richard Smith0706df42011-10-19 21:33:05 +00001056
1057 default:
1058 return false;
1059 }
1060
1061 default:
1062 return false;
1063 }
1064}
1065
John McCalld8ac0572009-11-03 19:26:08 +00001066/// ParseDeclGroup - Having concluded that this is either a function
1067/// definition or a group of object declarations, actually parse the
1068/// result.
John McCall54abf7d2009-11-04 02:18:39 +00001069Parser::DeclGroupPtrTy Parser::ParseDeclGroup(ParsingDeclSpec &DS,
1070 unsigned Context,
John McCalld8ac0572009-11-03 19:26:08 +00001071 bool AllowFunctionDefinitions,
Richard Smithad762fc2011-04-14 22:09:26 +00001072 SourceLocation *DeclEnd,
1073 ForRangeInit *FRI) {
John McCalld8ac0572009-11-03 19:26:08 +00001074 // Parse the first declarator.
John McCall54abf7d2009-11-04 02:18:39 +00001075 ParsingDeclarator D(*this, DS, static_cast<Declarator::TheContext>(Context));
John McCalld8ac0572009-11-03 19:26:08 +00001076 ParseDeclarator(D);
Chris Lattnercd147752009-03-29 17:27:48 +00001077
John McCalld8ac0572009-11-03 19:26:08 +00001078 // Bail out if the first declarator didn't seem well-formed.
1079 if (!D.hasName() && !D.mayOmitIdentifier()) {
1080 // Skip until ; or }.
1081 SkipUntil(tok::r_brace, true, true);
1082 if (Tok.is(tok::semi))
1083 ConsumeToken();
1084 return DeclGroupPtrTy();
Chris Lattner23c4b182009-03-29 17:18:04 +00001085 }
Mike Stump1eb44332009-09-09 15:08:12 +00001086
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001087 // Save late-parsed attributes for now; they need to be parsed in the
1088 // appropriate function scope after the function Decl has been constructed.
1089 LateParsedAttrList LateParsedAttrs;
1090 if (D.isFunctionDeclarator())
1091 MaybeParseGNUAttributes(D, &LateParsedAttrs);
1092
Chris Lattnerc82daef2010-07-11 22:24:20 +00001093 // Check to see if we have a function *definition* which must have a body.
1094 if (AllowFunctionDefinitions && D.isFunctionDeclarator() &&
1095 // Look at the next token to make sure that this isn't a function
1096 // declaration. We have to check this because __attribute__ might be the
1097 // start of a function definition in GCC-extended K&R C.
1098 !isDeclarationAfterDeclarator()) {
Richard Smith58196dc2011-11-30 23:45:35 +00001099
Chris Lattner004659a2010-07-11 22:42:07 +00001100 if (isStartOfFunctionDefinition(D)) {
John McCalld8ac0572009-11-03 19:26:08 +00001101 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
1102 Diag(Tok, diag::err_function_declared_typedef);
1103
1104 // Recover by treating the 'typedef' as spurious.
1105 DS.ClearStorageClassSpecs();
1106 }
1107
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001108 Decl *TheDecl =
1109 ParseFunctionDefinition(D, ParsedTemplateInfo(), &LateParsedAttrs);
John McCalld8ac0572009-11-03 19:26:08 +00001110 return Actions.ConvertDeclToDeclGroup(TheDecl);
Chris Lattner004659a2010-07-11 22:42:07 +00001111 }
1112
1113 if (isDeclarationSpecifier()) {
1114 // If there is an invalid declaration specifier right after the function
1115 // prototype, then we must be in a missing semicolon case where this isn't
1116 // actually a body. Just fall through into the code that handles it as a
1117 // prototype, and let the top-level code handle the erroneous declspec
1118 // where it would otherwise expect a comma or semicolon.
John McCalld8ac0572009-11-03 19:26:08 +00001119 } else {
1120 Diag(Tok, diag::err_expected_fn_body);
1121 SkipUntil(tok::semi);
1122 return DeclGroupPtrTy();
1123 }
1124 }
1125
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001126 if (ParseAsmAttributesAfterDeclarator(D))
Richard Smithad762fc2011-04-14 22:09:26 +00001127 return DeclGroupPtrTy();
1128
1129 // C++0x [stmt.iter]p1: Check if we have a for-range-declarator. If so, we
1130 // must parse and analyze the for-range-initializer before the declaration is
1131 // analyzed.
1132 if (FRI && Tok.is(tok::colon)) {
1133 FRI->ColonLoc = ConsumeToken();
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001134 if (Tok.is(tok::l_brace))
1135 FRI->RangeExpr = ParseBraceInitializer();
1136 else
1137 FRI->RangeExpr = ParseExpression();
Richard Smithad762fc2011-04-14 22:09:26 +00001138 Decl *ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
1139 Actions.ActOnCXXForRangeDecl(ThisDecl);
1140 Actions.FinalizeDeclaration(ThisDecl);
John McCall6895a642012-01-27 01:29:43 +00001141 D.complete(ThisDecl);
Richard Smithad762fc2011-04-14 22:09:26 +00001142 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS, &ThisDecl, 1);
1143 }
1144
Chris Lattner5f9e2722011-07-23 10:55:15 +00001145 SmallVector<Decl *, 8> DeclsInGroup;
Richard Smithad762fc2011-04-14 22:09:26 +00001146 Decl *FirstDecl = ParseDeclarationAfterDeclaratorAndAttributes(D);
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001147 if (LateParsedAttrs.size() > 0)
1148 ParseLexedAttributeList(LateParsedAttrs, FirstDecl, true, false);
John McCall54abf7d2009-11-04 02:18:39 +00001149 D.complete(FirstDecl);
John McCalld226f652010-08-21 09:40:31 +00001150 if (FirstDecl)
John McCalld8ac0572009-11-03 19:26:08 +00001151 DeclsInGroup.push_back(FirstDecl);
1152
Richard Smith0706df42011-10-19 21:33:05 +00001153 bool ExpectSemi = Context != Declarator::ForContext;
1154
John McCalld8ac0572009-11-03 19:26:08 +00001155 // If we don't have a comma, it is either the end of the list (a ';') or an
1156 // error, bail out.
1157 while (Tok.is(tok::comma)) {
Richard Smith0706df42011-10-19 21:33:05 +00001158 SourceLocation CommaLoc = ConsumeToken();
1159
1160 if (Tok.isAtStartOfLine() && ExpectSemi && !MightBeDeclarator(Context)) {
1161 // This comma was followed by a line-break and something which can't be
1162 // the start of a declarator. The comma was probably a typo for a
1163 // semicolon.
1164 Diag(CommaLoc, diag::err_expected_semi_declaration)
1165 << FixItHint::CreateReplacement(CommaLoc, ";");
1166 ExpectSemi = false;
1167 break;
1168 }
John McCalld8ac0572009-11-03 19:26:08 +00001169
1170 // Parse the next declarator.
1171 D.clear();
Richard Smith7984de32012-01-12 23:53:29 +00001172 D.setCommaLoc(CommaLoc);
John McCalld8ac0572009-11-03 19:26:08 +00001173
1174 // Accept attributes in an init-declarator. In the first declarator in a
1175 // declaration, these would be part of the declspec. In subsequent
1176 // declarators, they become part of the declarator itself, so that they
1177 // don't apply to declarators after *this* one. Examples:
1178 // short __attribute__((common)) var; -> declspec
1179 // short var __attribute__((common)); -> declarator
1180 // short x, __attribute__((common)) var; -> declarator
John McCall7f040a92010-12-24 02:08:15 +00001181 MaybeParseGNUAttributes(D);
John McCalld8ac0572009-11-03 19:26:08 +00001182
1183 ParseDeclarator(D);
Fariborz Jahanian9baf39d2012-01-13 00:14:12 +00001184 if (!D.isInvalidType()) {
1185 Decl *ThisDecl = ParseDeclarationAfterDeclarator(D);
1186 D.complete(ThisDecl);
1187 if (ThisDecl)
1188 DeclsInGroup.push_back(ThisDecl);
1189 }
John McCalld8ac0572009-11-03 19:26:08 +00001190 }
1191
1192 if (DeclEnd)
1193 *DeclEnd = Tok.getLocation();
1194
Richard Smith0706df42011-10-19 21:33:05 +00001195 if (ExpectSemi &&
John McCalld8ac0572009-11-03 19:26:08 +00001196 ExpectAndConsume(tok::semi,
1197 Context == Declarator::FileContext
1198 ? diag::err_invalid_token_after_toplevel_declarator
1199 : diag::err_expected_semi_declaration)) {
Chris Lattner004659a2010-07-11 22:42:07 +00001200 // Okay, there was no semicolon and one was expected. If we see a
1201 // declaration specifier, just assume it was missing and continue parsing.
1202 // Otherwise things are very confused and we skip to recover.
1203 if (!isDeclarationSpecifier()) {
1204 SkipUntil(tok::r_brace, true, true);
1205 if (Tok.is(tok::semi))
1206 ConsumeToken();
1207 }
John McCalld8ac0572009-11-03 19:26:08 +00001208 }
1209
Douglas Gregor23c94db2010-07-02 17:43:08 +00001210 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS,
John McCalld8ac0572009-11-03 19:26:08 +00001211 DeclsInGroup.data(),
1212 DeclsInGroup.size());
Reid Spencer5f016e22007-07-11 17:01:13 +00001213}
1214
Richard Smithad762fc2011-04-14 22:09:26 +00001215/// Parse an optional simple-asm-expr and attributes, and attach them to a
1216/// declarator. Returns true on an error.
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001217bool Parser::ParseAsmAttributesAfterDeclarator(Declarator &D) {
Richard Smithad762fc2011-04-14 22:09:26 +00001218 // If a simple-asm-expr is present, parse it.
1219 if (Tok.is(tok::kw_asm)) {
1220 SourceLocation Loc;
1221 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
1222 if (AsmLabel.isInvalid()) {
1223 SkipUntil(tok::semi, true, true);
1224 return true;
1225 }
1226
1227 D.setAsmLabel(AsmLabel.release());
1228 D.SetRangeEnd(Loc);
1229 }
1230
1231 MaybeParseGNUAttributes(D);
1232 return false;
1233}
1234
Douglas Gregor1426e532009-05-12 21:31:51 +00001235/// \brief Parse 'declaration' after parsing 'declaration-specifiers
1236/// declarator'. This method parses the remainder of the declaration
1237/// (including any attributes or initializer, among other things) and
1238/// finalizes the declaration.
Reid Spencer5f016e22007-07-11 17:01:13 +00001239///
Reid Spencer5f016e22007-07-11 17:01:13 +00001240/// init-declarator: [C99 6.7]
1241/// declarator
1242/// declarator '=' initializer
1243/// [GNU] declarator simple-asm-expr[opt] attributes[opt]
1244/// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00001245/// [C++] declarator initializer[opt]
1246///
1247/// [C++] initializer:
1248/// [C++] '=' initializer-clause
1249/// [C++] '(' expression-list ')'
Sebastian Redl50de12f2009-03-24 22:27:57 +00001250/// [C++0x] '=' 'default' [TODO]
1251/// [C++0x] '=' 'delete'
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001252/// [C++0x] braced-init-list
Sebastian Redl50de12f2009-03-24 22:27:57 +00001253///
1254/// According to the standard grammar, =default and =delete are function
1255/// definitions, but that definitely doesn't fit with the parser here.
Reid Spencer5f016e22007-07-11 17:01:13 +00001256///
John McCalld226f652010-08-21 09:40:31 +00001257Decl *Parser::ParseDeclarationAfterDeclarator(Declarator &D,
Douglas Gregore542c862009-06-23 23:11:28 +00001258 const ParsedTemplateInfo &TemplateInfo) {
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001259 if (ParseAsmAttributesAfterDeclarator(D))
Richard Smithad762fc2011-04-14 22:09:26 +00001260 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001261
Richard Smithad762fc2011-04-14 22:09:26 +00001262 return ParseDeclarationAfterDeclaratorAndAttributes(D, TemplateInfo);
1263}
Mike Stump1eb44332009-09-09 15:08:12 +00001264
Richard Smithad762fc2011-04-14 22:09:26 +00001265Decl *Parser::ParseDeclarationAfterDeclaratorAndAttributes(Declarator &D,
1266 const ParsedTemplateInfo &TemplateInfo) {
Douglas Gregor1426e532009-05-12 21:31:51 +00001267 // Inform the current actions module that we just parsed this declarator.
John McCalld226f652010-08-21 09:40:31 +00001268 Decl *ThisDecl = 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +00001269 switch (TemplateInfo.Kind) {
1270 case ParsedTemplateInfo::NonTemplate:
Douglas Gregor23c94db2010-07-02 17:43:08 +00001271 ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
Douglas Gregord5a423b2009-09-25 18:43:00 +00001272 break;
1273
1274 case ParsedTemplateInfo::Template:
1275 case ParsedTemplateInfo::ExplicitSpecialization:
Douglas Gregor23c94db2010-07-02 17:43:08 +00001276 ThisDecl = Actions.ActOnTemplateDeclarator(getCurScope(),
John McCallf312b1e2010-08-26 23:41:50 +00001277 MultiTemplateParamsArg(Actions,
Douglas Gregore542c862009-06-23 23:11:28 +00001278 TemplateInfo.TemplateParams->data(),
1279 TemplateInfo.TemplateParams->size()),
Douglas Gregord5a423b2009-09-25 18:43:00 +00001280 D);
1281 break;
1282
1283 case ParsedTemplateInfo::ExplicitInstantiation: {
John McCalld226f652010-08-21 09:40:31 +00001284 DeclResult ThisRes
Douglas Gregor23c94db2010-07-02 17:43:08 +00001285 = Actions.ActOnExplicitInstantiation(getCurScope(),
Douglas Gregord5a423b2009-09-25 18:43:00 +00001286 TemplateInfo.ExternLoc,
1287 TemplateInfo.TemplateLoc,
1288 D);
1289 if (ThisRes.isInvalid()) {
1290 SkipUntil(tok::semi, true, true);
John McCalld226f652010-08-21 09:40:31 +00001291 return 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +00001292 }
1293
1294 ThisDecl = ThisRes.get();
1295 break;
1296 }
1297 }
Mike Stump1eb44332009-09-09 15:08:12 +00001298
Richard Smith34b41d92011-02-20 03:19:35 +00001299 bool TypeContainsAuto =
1300 D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto;
1301
Douglas Gregor1426e532009-05-12 21:31:51 +00001302 // Parse declarator '=' initializer.
Richard Trieud6c7c672012-01-18 22:54:52 +00001303 // If a '==' or '+=' is found, suggest a fixit to '='.
Richard Trieufcaf27e2012-01-19 22:01:51 +00001304 if (isTokenEqualOrEqualTypo()) {
Douglas Gregor1426e532009-05-12 21:31:51 +00001305 ConsumeToken();
Anders Carlsson37bf9d22010-09-24 21:25:25 +00001306 if (Tok.is(tok::kw_delete)) {
Sean Hunte4246a62011-05-12 06:15:49 +00001307 if (D.isFunctionDeclarator())
1308 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
1309 << 1 /* delete */;
1310 else
1311 Diag(ConsumeToken(), diag::err_deleted_non_function);
Sean Huntfe2695e2011-05-06 01:42:00 +00001312 } else if (Tok.is(tok::kw_default)) {
Sean Hunte4246a62011-05-12 06:15:49 +00001313 if (D.isFunctionDeclarator())
Sebastian Redlecfcd562012-02-11 23:51:21 +00001314 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
1315 << 0 /* default */;
Sean Hunte4246a62011-05-12 06:15:49 +00001316 else
1317 Diag(ConsumeToken(), diag::err_default_special_members);
Douglas Gregor1426e532009-05-12 21:31:51 +00001318 } else {
John McCall731ad842009-12-19 09:28:58 +00001319 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
1320 EnterScope(0);
Douglas Gregor23c94db2010-07-02 17:43:08 +00001321 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
John McCall731ad842009-12-19 09:28:58 +00001322 }
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +00001323
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +00001324 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001325 Actions.CodeCompleteInitializer(getCurScope(), ThisDecl);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001326 cutOffParsing();
1327 return 0;
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +00001328 }
1329
John McCall60d7b3a2010-08-24 06:29:42 +00001330 ExprResult Init(ParseInitializer());
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +00001331
John McCall731ad842009-12-19 09:28:58 +00001332 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001333 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
John McCall731ad842009-12-19 09:28:58 +00001334 ExitScope();
1335 }
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +00001336
Douglas Gregor1426e532009-05-12 21:31:51 +00001337 if (Init.isInvalid()) {
Douglas Gregor00225542010-03-01 18:27:54 +00001338 SkipUntil(tok::comma, true, true);
1339 Actions.ActOnInitializerError(ThisDecl);
1340 } else
Richard Smith34b41d92011-02-20 03:19:35 +00001341 Actions.AddInitializerToDecl(ThisDecl, Init.take(),
1342 /*DirectInit=*/false, TypeContainsAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +00001343 }
1344 } else if (Tok.is(tok::l_paren)) {
1345 // Parse C++ direct initializer: '(' expression-list ')'
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001346 BalancedDelimiterTracker T(*this, tok::l_paren);
1347 T.consumeOpen();
1348
Douglas Gregor1426e532009-05-12 21:31:51 +00001349 ExprVector Exprs(Actions);
1350 CommaLocsTy CommaLocs;
1351
Douglas Gregorb4debae2009-12-22 17:47:17 +00001352 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
1353 EnterScope(0);
Douglas Gregor23c94db2010-07-02 17:43:08 +00001354 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001355 }
1356
Douglas Gregor1426e532009-05-12 21:31:51 +00001357 if (ParseExpressionList(Exprs, CommaLocs)) {
1358 SkipUntil(tok::r_paren);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001359
1360 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001361 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001362 ExitScope();
1363 }
Douglas Gregor1426e532009-05-12 21:31:51 +00001364 } else {
1365 // Match the ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001366 T.consumeClose();
Douglas Gregor1426e532009-05-12 21:31:51 +00001367
1368 assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() &&
1369 "Unexpected number of commas!");
Douglas Gregorb4debae2009-12-22 17:47:17 +00001370
1371 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001372 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001373 ExitScope();
1374 }
1375
Sebastian Redl5b9cc5d2012-02-11 23:51:47 +00001376 ExprResult Initializer = Actions.ActOnParenListExpr(T.getOpenLocation(),
1377 T.getCloseLocation(),
1378 move_arg(Exprs));
1379 Actions.AddInitializerToDecl(ThisDecl, Initializer.take(),
1380 /*DirectInit=*/true, TypeContainsAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +00001381 }
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001382 } else if (getLang().CPlusPlus0x && Tok.is(tok::l_brace)) {
1383 // Parse C++0x braced-init-list.
Richard Smith7fe62082011-10-15 05:09:34 +00001384 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
1385
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001386 if (D.getCXXScopeSpec().isSet()) {
1387 EnterScope(0);
1388 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
1389 }
1390
1391 ExprResult Init(ParseBraceInitializer());
1392
1393 if (D.getCXXScopeSpec().isSet()) {
1394 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
1395 ExitScope();
1396 }
1397
1398 if (Init.isInvalid()) {
1399 Actions.ActOnInitializerError(ThisDecl);
1400 } else
1401 Actions.AddInitializerToDecl(ThisDecl, Init.take(),
1402 /*DirectInit=*/true, TypeContainsAuto);
1403
Douglas Gregor1426e532009-05-12 21:31:51 +00001404 } else {
Richard Smith34b41d92011-02-20 03:19:35 +00001405 Actions.ActOnUninitializedDecl(ThisDecl, TypeContainsAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +00001406 }
1407
Richard Smith483b9f32011-02-21 20:05:19 +00001408 Actions.FinalizeDeclaration(ThisDecl);
1409
Douglas Gregor1426e532009-05-12 21:31:51 +00001410 return ThisDecl;
1411}
1412
Reid Spencer5f016e22007-07-11 17:01:13 +00001413/// ParseSpecifierQualifierList
1414/// specifier-qualifier-list:
1415/// type-specifier specifier-qualifier-list[opt]
1416/// type-qualifier specifier-qualifier-list[opt]
1417/// [GNU] attributes specifier-qualifier-list[opt]
1418///
Richard Smithc89edf52011-07-01 19:46:12 +00001419void Parser::ParseSpecifierQualifierList(DeclSpec &DS, AccessSpecifier AS) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001420 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
1421 /// parse declaration-specifiers and complain about extra stuff.
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001422 /// TODO: diagnose attribute-specifiers and alignment-specifiers.
Richard Smithc89edf52011-07-01 19:46:12 +00001423 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS);
Mike Stump1eb44332009-09-09 15:08:12 +00001424
Reid Spencer5f016e22007-07-11 17:01:13 +00001425 // Validate declspec for type-name.
1426 unsigned Specs = DS.getParsedSpecifiers();
Chris Lattnerb6645dd2009-04-14 21:16:09 +00001427 if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers() &&
John McCall7f040a92010-12-24 02:08:15 +00001428 !DS.hasAttributes())
Reid Spencer5f016e22007-07-11 17:01:13 +00001429 Diag(Tok, diag::err_typename_requires_specqual);
Mike Stump1eb44332009-09-09 15:08:12 +00001430
Reid Spencer5f016e22007-07-11 17:01:13 +00001431 // Issue diagnostic and remove storage class if present.
1432 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
1433 if (DS.getStorageClassSpecLoc().isValid())
1434 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
1435 else
1436 Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass);
1437 DS.ClearStorageClassSpecs();
1438 }
Mike Stump1eb44332009-09-09 15:08:12 +00001439
Reid Spencer5f016e22007-07-11 17:01:13 +00001440 // Issue diagnostic and remove function specfier if present.
1441 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Douglas Gregorb48fe382008-10-31 09:07:45 +00001442 if (DS.isInlineSpecified())
1443 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
1444 if (DS.isVirtualSpecified())
1445 Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec);
1446 if (DS.isExplicitSpecified())
1447 Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec);
Reid Spencer5f016e22007-07-11 17:01:13 +00001448 DS.ClearFunctionSpecs();
1449 }
1450}
1451
Chris Lattnerc199ab32009-04-12 20:42:31 +00001452/// isValidAfterIdentifierInDeclaratorAfterDeclSpec - Return true if the
1453/// specified token is valid after the identifier in a declarator which
1454/// immediately follows the declspec. For example, these things are valid:
1455///
1456/// int x [ 4]; // direct-declarator
1457/// int x ( int y); // direct-declarator
1458/// int(int x ) // direct-declarator
1459/// int x ; // simple-declaration
1460/// int x = 17; // init-declarator-list
1461/// int x , y; // init-declarator-list
1462/// int x __asm__ ("foo"); // init-declarator-list
Chris Lattnerb6645dd2009-04-14 21:16:09 +00001463/// int x : 4; // struct-declarator
Chris Lattnerc83c27a2009-04-12 22:29:43 +00001464/// int x { 5}; // C++'0x unified initializers
Chris Lattnerc199ab32009-04-12 20:42:31 +00001465///
1466/// This is not, because 'x' does not immediately follow the declspec (though
1467/// ')' happens to be valid anyway).
1468/// int (x)
1469///
1470static bool isValidAfterIdentifierInDeclarator(const Token &T) {
1471 return T.is(tok::l_square) || T.is(tok::l_paren) || T.is(tok::r_paren) ||
1472 T.is(tok::semi) || T.is(tok::comma) || T.is(tok::equal) ||
Chris Lattnerb6645dd2009-04-14 21:16:09 +00001473 T.is(tok::kw_asm) || T.is(tok::l_brace) || T.is(tok::colon);
Chris Lattnerc199ab32009-04-12 20:42:31 +00001474}
1475
Chris Lattnere40c2952009-04-14 21:34:55 +00001476
1477/// ParseImplicitInt - This method is called when we have an non-typename
1478/// identifier in a declspec (which normally terminates the decl spec) when
1479/// the declspec has no type specifier. In this case, the declspec is either
1480/// malformed or is "implicit int" (in K&R and C89).
1481///
1482/// This method handles diagnosing this prettily and returns false if the
1483/// declspec is done being processed. If it recovers and thinks there may be
1484/// other pieces of declspec after it, it returns true.
1485///
Chris Lattnerf4382f52009-04-14 22:17:06 +00001486bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001487 const ParsedTemplateInfo &TemplateInfo,
Chris Lattnere40c2952009-04-14 21:34:55 +00001488 AccessSpecifier AS) {
Chris Lattnerf4382f52009-04-14 22:17:06 +00001489 assert(Tok.is(tok::identifier) && "should have identifier");
Mike Stump1eb44332009-09-09 15:08:12 +00001490
Chris Lattnere40c2952009-04-14 21:34:55 +00001491 SourceLocation Loc = Tok.getLocation();
1492 // If we see an identifier that is not a type name, we normally would
1493 // parse it as the identifer being declared. However, when a typename
1494 // is typo'd or the definition is not included, this will incorrectly
1495 // parse the typename as the identifier name and fall over misparsing
1496 // later parts of the diagnostic.
1497 //
1498 // As such, we try to do some look-ahead in cases where this would
1499 // otherwise be an "implicit-int" case to see if this is invalid. For
1500 // example: "static foo_t x = 4;" In this case, if we parsed foo_t as
1501 // an identifier with implicit int, we'd get a parse error because the
1502 // next token is obviously invalid for a type. Parse these as a case
1503 // with an invalid type specifier.
1504 assert(!DS.hasTypeSpecifier() && "Type specifier checked above");
Mike Stump1eb44332009-09-09 15:08:12 +00001505
Chris Lattnere40c2952009-04-14 21:34:55 +00001506 // Since we know that this either implicit int (which is rare) or an
1507 // error, we'd do lookahead to try to do better recovery.
1508 if (isValidAfterIdentifierInDeclarator(NextToken())) {
1509 // If this token is valid for implicit int, e.g. "static x = 4", then
1510 // we just avoid eating the identifier, so it will be parsed as the
1511 // identifier in the declarator.
1512 return false;
1513 }
Mike Stump1eb44332009-09-09 15:08:12 +00001514
Chris Lattnere40c2952009-04-14 21:34:55 +00001515 // Otherwise, if we don't consume this token, we are going to emit an
1516 // error anyway. Try to recover from various common problems. Check
1517 // to see if this was a reference to a tag name without a tag specified.
1518 // This is a common problem in C (saying 'foo' instead of 'struct foo').
Chris Lattnerf4382f52009-04-14 22:17:06 +00001519 //
1520 // C++ doesn't need this, and isTagName doesn't take SS.
1521 if (SS == 0) {
Argyrios Kyrtzidisb8a9d3b2011-04-21 17:29:47 +00001522 const char *TagName = 0, *FixitTagName = 0;
Chris Lattnerf4382f52009-04-14 22:17:06 +00001523 tok::TokenKind TagKind = tok::unknown;
Mike Stump1eb44332009-09-09 15:08:12 +00001524
Douglas Gregor23c94db2010-07-02 17:43:08 +00001525 switch (Actions.isTagName(*Tok.getIdentifierInfo(), getCurScope())) {
Chris Lattnere40c2952009-04-14 21:34:55 +00001526 default: break;
Argyrios Kyrtzidisb8a9d3b2011-04-21 17:29:47 +00001527 case DeclSpec::TST_enum:
1528 TagName="enum" ; FixitTagName = "enum " ; TagKind=tok::kw_enum ;break;
1529 case DeclSpec::TST_union:
1530 TagName="union" ; FixitTagName = "union " ;TagKind=tok::kw_union ;break;
1531 case DeclSpec::TST_struct:
1532 TagName="struct"; FixitTagName = "struct ";TagKind=tok::kw_struct;break;
1533 case DeclSpec::TST_class:
1534 TagName="class" ; FixitTagName = "class " ;TagKind=tok::kw_class ;break;
Chris Lattnere40c2952009-04-14 21:34:55 +00001535 }
Mike Stump1eb44332009-09-09 15:08:12 +00001536
Chris Lattnerf4382f52009-04-14 22:17:06 +00001537 if (TagName) {
1538 Diag(Loc, diag::err_use_of_tag_name_without_tag)
John McCall23e907a2010-02-14 01:03:10 +00001539 << Tok.getIdentifierInfo() << TagName << getLang().CPlusPlus
Argyrios Kyrtzidisb8a9d3b2011-04-21 17:29:47 +00001540 << FixItHint::CreateInsertion(Tok.getLocation(),FixitTagName);
Mike Stump1eb44332009-09-09 15:08:12 +00001541
Chris Lattnerf4382f52009-04-14 22:17:06 +00001542 // Parse this as a tag as if the missing tag were present.
1543 if (TagKind == tok::kw_enum)
Douglas Gregor9b9edd62010-03-02 17:53:14 +00001544 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS);
Chris Lattnerf4382f52009-04-14 22:17:06 +00001545 else
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001546 ParseClassSpecifier(TagKind, Loc, DS, TemplateInfo, AS);
Chris Lattnerf4382f52009-04-14 22:17:06 +00001547 return true;
1548 }
Chris Lattnere40c2952009-04-14 21:34:55 +00001549 }
Mike Stump1eb44332009-09-09 15:08:12 +00001550
Douglas Gregora786fdb2009-10-13 23:27:22 +00001551 // This is almost certainly an invalid type name. Let the action emit a
1552 // diagnostic and attempt to recover.
John McCallb3d87482010-08-24 05:47:05 +00001553 ParsedType T;
Douglas Gregora786fdb2009-10-13 23:27:22 +00001554 if (Actions.DiagnoseUnknownTypeName(*Tok.getIdentifierInfo(), Loc,
Douglas Gregor23c94db2010-07-02 17:43:08 +00001555 getCurScope(), SS, T)) {
Douglas Gregora786fdb2009-10-13 23:27:22 +00001556 // The action emitted a diagnostic, so we don't have to.
1557 if (T) {
1558 // The action has suggested that the type T could be used. Set that as
1559 // the type in the declaration specifiers, consume the would-be type
1560 // name token, and we're done.
1561 const char *PrevSpec;
1562 unsigned DiagID;
John McCallb3d87482010-08-24 05:47:05 +00001563 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID, T);
Douglas Gregora786fdb2009-10-13 23:27:22 +00001564 DS.SetRangeEnd(Tok.getLocation());
1565 ConsumeToken();
1566
1567 // There may be other declaration specifiers after this.
1568 return true;
1569 }
1570
1571 // Fall through; the action had no suggestion for us.
1572 } else {
1573 // The action did not emit a diagnostic, so emit one now.
1574 SourceRange R;
1575 if (SS) R = SS->getRange();
1576 Diag(Loc, diag::err_unknown_typename) << Tok.getIdentifierInfo() << R;
1577 }
Mike Stump1eb44332009-09-09 15:08:12 +00001578
Douglas Gregora786fdb2009-10-13 23:27:22 +00001579 // Mark this as an error.
Chris Lattnere40c2952009-04-14 21:34:55 +00001580 const char *PrevSpec;
John McCallfec54012009-08-03 20:12:06 +00001581 unsigned DiagID;
1582 DS.SetTypeSpecType(DeclSpec::TST_error, Loc, PrevSpec, DiagID);
Chris Lattnere40c2952009-04-14 21:34:55 +00001583 DS.SetRangeEnd(Tok.getLocation());
1584 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001585
Chris Lattnere40c2952009-04-14 21:34:55 +00001586 // TODO: Could inject an invalid typedef decl in an enclosing scope to
1587 // avoid rippling error messages on subsequent uses of the same type,
1588 // could be useful if #include was forgotten.
1589 return false;
1590}
1591
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001592/// \brief Determine the declaration specifier context from the declarator
1593/// context.
1594///
1595/// \param Context the declarator context, which is one of the
1596/// Declarator::TheContext enumerator values.
1597Parser::DeclSpecContext
1598Parser::getDeclSpecContextFromDeclaratorContext(unsigned Context) {
1599 if (Context == Declarator::MemberContext)
1600 return DSC_class;
1601 if (Context == Declarator::FileContext)
1602 return DSC_top_level;
1603 return DSC_normal;
1604}
1605
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001606/// ParseAlignArgument - Parse the argument to an alignment-specifier.
1607///
1608/// FIXME: Simply returns an alignof() expression if the argument is a
1609/// type. Ideally, the type should be propagated directly into Sema.
1610///
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00001611/// [C11] type-id
1612/// [C11] constant-expression
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00001613/// [C++0x] type-id ...[opt]
1614/// [C++0x] assignment-expression ...[opt]
1615ExprResult Parser::ParseAlignArgument(SourceLocation Start,
1616 SourceLocation &EllipsisLoc) {
1617 ExprResult ER;
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001618 if (isTypeIdInParens()) {
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001619 SourceLocation TypeLoc = Tok.getLocation();
1620 ParsedType Ty = ParseTypeName().get();
1621 SourceRange TypeRange(Start, Tok.getLocation());
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00001622 ER = Actions.ActOnUnaryExprOrTypeTraitExpr(TypeLoc, UETT_AlignOf, true,
1623 Ty.getAsOpaquePtr(), TypeRange);
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001624 } else
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00001625 ER = ParseConstantExpression();
1626
Peter Collingbournefe9b2a82011-10-24 17:56:00 +00001627 if (getLang().CPlusPlus0x && Tok.is(tok::ellipsis))
1628 EllipsisLoc = ConsumeToken();
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00001629
1630 return ER;
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001631}
1632
1633/// ParseAlignmentSpecifier - Parse an alignment-specifier, and add the
1634/// attribute to Attrs.
1635///
1636/// alignment-specifier:
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00001637/// [C11] '_Alignas' '(' type-id ')'
1638/// [C11] '_Alignas' '(' constant-expression ')'
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00001639/// [C++0x] 'alignas' '(' type-id ...[opt] ')'
1640/// [C++0x] 'alignas' '(' assignment-expression ...[opt] ')'
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001641void Parser::ParseAlignmentSpecifier(ParsedAttributes &Attrs,
1642 SourceLocation *endLoc) {
1643 assert((Tok.is(tok::kw_alignas) || Tok.is(tok::kw__Alignas)) &&
1644 "Not an alignment-specifier!");
1645
1646 SourceLocation KWLoc = Tok.getLocation();
1647 ConsumeToken();
1648
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001649 BalancedDelimiterTracker T(*this, tok::l_paren);
1650 if (T.expectAndConsume(diag::err_expected_lparen))
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001651 return;
1652
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00001653 SourceLocation EllipsisLoc;
1654 ExprResult ArgExpr = ParseAlignArgument(T.getOpenLocation(), EllipsisLoc);
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001655 if (ArgExpr.isInvalid()) {
1656 SkipUntil(tok::r_paren);
1657 return;
1658 }
1659
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001660 T.consumeClose();
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001661 if (endLoc)
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001662 *endLoc = T.getCloseLocation();
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001663
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00001664 // FIXME: Handle pack-expansions here.
1665 if (EllipsisLoc.isValid()) {
1666 Diag(EllipsisLoc, diag::err_alignas_pack_exp_unsupported);
1667 return;
1668 }
1669
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001670 ExprVector ArgExprs(Actions);
1671 ArgExprs.push_back(ArgExpr.release());
1672 Attrs.addNew(PP.getIdentifierInfo("aligned"), KWLoc, 0, KWLoc,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001673 0, T.getOpenLocation(), ArgExprs.take(), 1, false, true);
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001674}
1675
Reid Spencer5f016e22007-07-11 17:01:13 +00001676/// ParseDeclarationSpecifiers
1677/// declaration-specifiers: [C99 6.7]
1678/// storage-class-specifier declaration-specifiers[opt]
1679/// type-specifier declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00001680/// [C99] function-specifier declaration-specifiers[opt]
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00001681/// [C11] alignment-specifier declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00001682/// [GNU] attributes declaration-specifiers[opt]
Douglas Gregor8d267c52011-09-09 02:06:17 +00001683/// [Clang] '__module_private__' declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00001684///
1685/// storage-class-specifier: [C99 6.7.1]
1686/// 'typedef'
1687/// 'extern'
1688/// 'static'
1689/// 'auto'
1690/// 'register'
Sebastian Redl669d5d72008-11-14 23:42:31 +00001691/// [C++] 'mutable'
Reid Spencer5f016e22007-07-11 17:01:13 +00001692/// [GNU] '__thread'
Reid Spencer5f016e22007-07-11 17:01:13 +00001693/// function-specifier: [C99 6.7.4]
1694/// [C99] 'inline'
Douglas Gregorb48fe382008-10-31 09:07:45 +00001695/// [C++] 'virtual'
1696/// [C++] 'explicit'
Peter Collingbournef315fa82011-02-14 01:42:53 +00001697/// [OpenCL] '__kernel'
Anders Carlssonf47f7a12009-05-06 04:46:28 +00001698/// 'friend': [C++ dcl.friend]
Sebastian Redl2ac67232009-11-05 15:47:02 +00001699/// 'constexpr': [C++0x dcl.constexpr]
Anders Carlssonf47f7a12009-05-06 04:46:28 +00001700
Reid Spencer5f016e22007-07-11 17:01:13 +00001701///
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +00001702void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001703 const ParsedTemplateInfo &TemplateInfo,
John McCall67d1a672009-08-06 02:15:43 +00001704 AccessSpecifier AS,
Douglas Gregor312eadb2011-04-24 05:37:28 +00001705 DeclSpecContext DSContext) {
1706 if (DS.getSourceRange().isInvalid()) {
1707 DS.SetRangeStart(Tok.getLocation());
1708 DS.SetRangeEnd(Tok.getLocation());
1709 }
1710
Douglas Gregorefaa93a2011-11-07 17:33:42 +00001711 bool EnteringContext = (DSContext == DSC_class || DSContext == DSC_top_level);
Reid Spencer5f016e22007-07-11 17:01:13 +00001712 while (1) {
John McCallfec54012009-08-03 20:12:06 +00001713 bool isInvalid = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001714 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00001715 unsigned DiagID = 0;
1716
Reid Spencer5f016e22007-07-11 17:01:13 +00001717 SourceLocation Loc = Tok.getLocation();
Douglas Gregor12e083c2008-11-07 15:42:26 +00001718
Reid Spencer5f016e22007-07-11 17:01:13 +00001719 switch (Tok.getKind()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001720 default:
Chris Lattnerbce61352008-07-26 00:20:22 +00001721 DoneWithDeclSpec:
Peter Collingbournef1907682011-09-29 18:03:57 +00001722 // [C++0x] decl-specifier-seq: decl-specifier attribute-specifier-seq[opt]
1723 MaybeParseCXX0XAttributes(DS.getAttributes());
1724
Reid Spencer5f016e22007-07-11 17:01:13 +00001725 // If this is not a declaration specifier token, we're done reading decl
1726 // specifiers. First verify that DeclSpec's are consistent.
Douglas Gregor9b3064b2009-04-01 22:41:11 +00001727 DS.Finish(Diags, PP);
Reid Spencer5f016e22007-07-11 17:01:13 +00001728 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001729
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001730 case tok::code_completion: {
John McCallf312b1e2010-08-26 23:41:50 +00001731 Sema::ParserCompletionContext CCC = Sema::PCC_Namespace;
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001732 if (DS.hasTypeSpecifier()) {
1733 bool AllowNonIdentifiers
1734 = (getCurScope()->getFlags() & (Scope::ControlScope |
1735 Scope::BlockScope |
1736 Scope::TemplateParamScope |
1737 Scope::FunctionPrototypeScope |
1738 Scope::AtCatchScope)) == 0;
1739 bool AllowNestedNameSpecifiers
1740 = DSContext == DSC_top_level ||
1741 (DSContext == DSC_class && DS.isFriendSpecified());
1742
Douglas Gregorc7b6d882010-09-16 15:14:18 +00001743 Actions.CodeCompleteDeclSpec(getCurScope(), DS,
1744 AllowNonIdentifiers,
1745 AllowNestedNameSpecifiers);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001746 return cutOffParsing();
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001747 }
1748
Douglas Gregor68e3c2e2011-02-15 20:33:25 +00001749 if (getCurScope()->getFnParent() || getCurScope()->getBlockParent())
1750 CCC = Sema::PCC_LocalDeclarationSpecifiers;
1751 else if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate)
John McCallf312b1e2010-08-26 23:41:50 +00001752 CCC = DSContext == DSC_class? Sema::PCC_MemberTemplate
1753 : Sema::PCC_Template;
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001754 else if (DSContext == DSC_class)
John McCallf312b1e2010-08-26 23:41:50 +00001755 CCC = Sema::PCC_Class;
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +00001756 else if (CurParsedObjCImpl)
John McCallf312b1e2010-08-26 23:41:50 +00001757 CCC = Sema::PCC_ObjCImplementation;
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001758
1759 Actions.CodeCompleteOrdinaryName(getCurScope(), CCC);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001760 return cutOffParsing();
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001761 }
1762
Chris Lattner5e02c472009-01-05 00:07:25 +00001763 case tok::coloncolon: // ::foo::bar
John McCall9ba61662010-02-26 08:45:28 +00001764 // C++ scope specifier. Annotate and loop, or bail out on error.
1765 if (TryAnnotateCXXScopeToken(true)) {
1766 if (!DS.hasTypeSpecifier())
1767 DS.SetTypeSpecError();
1768 goto DoneWithDeclSpec;
1769 }
John McCall2e0a7152010-03-01 18:20:46 +00001770 if (Tok.is(tok::coloncolon)) // ::new or ::delete
1771 goto DoneWithDeclSpec;
John McCall9ba61662010-02-26 08:45:28 +00001772 continue;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001773
1774 case tok::annot_cxxscope: {
1775 if (DS.hasTypeSpecifier())
1776 goto DoneWithDeclSpec;
1777
John McCallaa87d332009-12-12 11:40:51 +00001778 CXXScopeSpec SS;
Douglas Gregorc34348a2011-02-24 17:54:50 +00001779 Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(),
1780 Tok.getAnnotationRange(),
1781 SS);
John McCallaa87d332009-12-12 11:40:51 +00001782
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001783 // We are looking for a qualified typename.
Douglas Gregor9135c722009-03-25 15:40:00 +00001784 Token Next = NextToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001785 if (Next.is(tok::annot_template_id) &&
Douglas Gregor9135c722009-03-25 15:40:00 +00001786 static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue())
Douglas Gregorc45c2322009-03-31 00:43:58 +00001787 ->Kind == TNK_Type_template) {
Douglas Gregor9135c722009-03-25 15:40:00 +00001788 // We have a qualified template-id, e.g., N::A<int>
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001789
1790 // C++ [class.qual]p2:
1791 // In a lookup in which the constructor is an acceptable lookup
1792 // result and the nested-name-specifier nominates a class C:
1793 //
1794 // - if the name specified after the
1795 // nested-name-specifier, when looked up in C, is the
1796 // injected-class-name of C (Clause 9), or
1797 //
1798 // - if the name specified after the nested-name-specifier
1799 // is the same as the identifier or the
1800 // simple-template-id's template-name in the last
1801 // component of the nested-name-specifier,
1802 //
1803 // the name is instead considered to name the constructor of
1804 // class C.
1805 //
1806 // Thus, if the template-name is actually the constructor
1807 // name, then the code is ill-formed; this interpretation is
1808 // reinforced by the NAD status of core issue 635.
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +00001809 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Next);
John McCallba9d8532010-04-13 06:39:49 +00001810 if ((DSContext == DSC_top_level ||
1811 (DSContext == DSC_class && DS.isFriendSpecified())) &&
1812 TemplateId->Name &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00001813 Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) {
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001814 if (isConstructorDeclarator()) {
1815 // The user meant this to be an out-of-line constructor
1816 // definition, but template arguments are not allowed
1817 // there. Just allow this as a constructor; we'll
1818 // complain about it later.
1819 goto DoneWithDeclSpec;
1820 }
1821
1822 // The user meant this to name a type, but it actually names
1823 // a constructor with some extraneous template
1824 // arguments. Complain, then parse it as a type as the user
1825 // intended.
1826 Diag(TemplateId->TemplateNameLoc,
1827 diag::err_out_of_line_template_id_names_constructor)
1828 << TemplateId->Name;
1829 }
1830
John McCallaa87d332009-12-12 11:40:51 +00001831 DS.getTypeSpecScope() = SS;
1832 ConsumeToken(); // The C++ scope.
Mike Stump1eb44332009-09-09 15:08:12 +00001833 assert(Tok.is(tok::annot_template_id) &&
Douglas Gregor9135c722009-03-25 15:40:00 +00001834 "ParseOptionalCXXScopeSpecifier not working");
Douglas Gregor059101f2011-03-02 00:47:37 +00001835 AnnotateTemplateIdTokenAsType();
Douglas Gregor9135c722009-03-25 15:40:00 +00001836 continue;
1837 }
1838
Douglas Gregor9d7b3532009-09-28 07:26:33 +00001839 if (Next.is(tok::annot_typename)) {
John McCallaa87d332009-12-12 11:40:51 +00001840 DS.getTypeSpecScope() = SS;
1841 ConsumeToken(); // The C++ scope.
John McCallb3d87482010-08-24 05:47:05 +00001842 if (Tok.getAnnotationValue()) {
1843 ParsedType T = getTypeAnnotation(Tok);
Nico Weber253e80b2010-11-22 10:30:56 +00001844 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename,
1845 Tok.getAnnotationEndLoc(),
John McCallb3d87482010-08-24 05:47:05 +00001846 PrevSpec, DiagID, T);
1847 }
Douglas Gregor9d7b3532009-09-28 07:26:33 +00001848 else
1849 DS.SetTypeSpecError();
1850 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1851 ConsumeToken(); // The typename
1852 }
1853
Douglas Gregor9135c722009-03-25 15:40:00 +00001854 if (Next.isNot(tok::identifier))
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001855 goto DoneWithDeclSpec;
1856
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001857 // If we're in a context where the identifier could be a class name,
1858 // check whether this is a constructor declaration.
John McCallba9d8532010-04-13 06:39:49 +00001859 if ((DSContext == DSC_top_level ||
1860 (DSContext == DSC_class && DS.isFriendSpecified())) &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00001861 Actions.isCurrentClassName(*Next.getIdentifierInfo(), getCurScope(),
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001862 &SS)) {
1863 if (isConstructorDeclarator())
1864 goto DoneWithDeclSpec;
1865
1866 // As noted in C++ [class.qual]p2 (cited above), when the name
1867 // of the class is qualified in a context where it could name
1868 // a constructor, its a constructor name. However, we've
1869 // looked at the declarator, and the user probably meant this
1870 // to be a type. Complain that it isn't supposed to be treated
1871 // as a type, then proceed to parse it as a type.
1872 Diag(Next.getLocation(), diag::err_out_of_line_type_names_constructor)
1873 << Next.getIdentifierInfo();
1874 }
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001875
John McCallb3d87482010-08-24 05:47:05 +00001876 ParsedType TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(),
1877 Next.getLocation(),
Douglas Gregor9e876872011-03-01 18:12:44 +00001878 getCurScope(), &SS,
1879 false, false, ParsedType(),
Abramo Bagnarafad03b72012-01-27 08:46:19 +00001880 /*IsCtorOrDtorName=*/false,
Douglas Gregor9e876872011-03-01 18:12:44 +00001881 /*NonTrivialSourceInfo=*/true);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001882
Chris Lattnerf4382f52009-04-14 22:17:06 +00001883 // If the referenced identifier is not a type, then this declspec is
1884 // erroneous: We already checked about that it has no type specifier, and
1885 // C++ doesn't have implicit int. Diagnose it as a typo w.r.t. to the
Mike Stump1eb44332009-09-09 15:08:12 +00001886 // typename.
Chris Lattnerf4382f52009-04-14 22:17:06 +00001887 if (TypeRep == 0) {
1888 ConsumeToken(); // Eat the scope spec so the identifier is current.
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001889 if (ParseImplicitInt(DS, &SS, TemplateInfo, AS)) continue;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001890 goto DoneWithDeclSpec;
Chris Lattnerf4382f52009-04-14 22:17:06 +00001891 }
Mike Stump1eb44332009-09-09 15:08:12 +00001892
John McCallaa87d332009-12-12 11:40:51 +00001893 DS.getTypeSpecScope() = SS;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001894 ConsumeToken(); // The C++ scope.
1895
Douglas Gregor1a51b4a2009-02-09 15:09:02 +00001896 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001897 DiagID, TypeRep);
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001898 if (isInvalid)
1899 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001900
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001901 DS.SetRangeEnd(Tok.getLocation());
1902 ConsumeToken(); // The typename.
1903
1904 continue;
1905 }
Mike Stump1eb44332009-09-09 15:08:12 +00001906
Chris Lattner80d0c892009-01-21 19:48:37 +00001907 case tok::annot_typename: {
John McCallb3d87482010-08-24 05:47:05 +00001908 if (Tok.getAnnotationValue()) {
1909 ParsedType T = getTypeAnnotation(Tok);
Nico Weberc43271e2010-11-22 12:50:03 +00001910 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallb3d87482010-08-24 05:47:05 +00001911 DiagID, T);
1912 } else
Douglas Gregor31a19b62009-04-01 21:51:26 +00001913 DS.SetTypeSpecError();
Chris Lattner5c5db552010-04-05 18:18:31 +00001914
1915 if (isInvalid)
1916 break;
1917
Chris Lattner80d0c892009-01-21 19:48:37 +00001918 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1919 ConsumeToken(); // The typename
Mike Stump1eb44332009-09-09 15:08:12 +00001920
Chris Lattner80d0c892009-01-21 19:48:37 +00001921 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1922 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001923 // Objective-C interface.
1924 if (Tok.is(tok::less) && getLang().ObjC1)
1925 ParseObjCProtocolQualifiers(DS);
1926
Chris Lattner80d0c892009-01-21 19:48:37 +00001927 continue;
1928 }
Mike Stump1eb44332009-09-09 15:08:12 +00001929
Douglas Gregorbfad9152011-04-28 15:48:45 +00001930 case tok::kw___is_signed:
1931 // GNU libstdc++ 4.4 uses __is_signed as an identifier, but Clang
1932 // typically treats it as a trait. If we see __is_signed as it appears
1933 // in libstdc++, e.g.,
1934 //
1935 // static const bool __is_signed;
1936 //
1937 // then treat __is_signed as an identifier rather than as a keyword.
1938 if (DS.getTypeSpecType() == TST_bool &&
1939 DS.getTypeQualifiers() == DeclSpec::TQ_const &&
1940 DS.getStorageClassSpec() == DeclSpec::SCS_static) {
1941 Tok.getIdentifierInfo()->RevertTokenIDToIdentifier();
1942 Tok.setKind(tok::identifier);
1943 }
1944
1945 // We're done with the declaration-specifiers.
1946 goto DoneWithDeclSpec;
1947
Chris Lattner3bd934a2008-07-26 01:18:38 +00001948 // typedef-name
David Blaikie42d6d0c2011-12-04 05:04:18 +00001949 case tok::kw_decltype:
Chris Lattner3bd934a2008-07-26 01:18:38 +00001950 case tok::identifier: {
Chris Lattner5e02c472009-01-05 00:07:25 +00001951 // In C++, check to see if this is a scope specifier like foo::bar::, if
1952 // so handle it as such. This is important for ctor parsing.
John McCall9ba61662010-02-26 08:45:28 +00001953 if (getLang().CPlusPlus) {
1954 if (TryAnnotateCXXScopeToken(true)) {
1955 if (!DS.hasTypeSpecifier())
1956 DS.SetTypeSpecError();
1957 goto DoneWithDeclSpec;
1958 }
1959 if (!Tok.is(tok::identifier))
1960 continue;
1961 }
Mike Stump1eb44332009-09-09 15:08:12 +00001962
Chris Lattner3bd934a2008-07-26 01:18:38 +00001963 // This identifier can only be a typedef name if we haven't already seen
1964 // a type-specifier. Without this check we misparse:
1965 // typedef int X; struct Y { short X; }; as 'short int'.
1966 if (DS.hasTypeSpecifier())
1967 goto DoneWithDeclSpec;
Mike Stump1eb44332009-09-09 15:08:12 +00001968
John Thompson82287d12010-02-05 00:12:22 +00001969 // Check for need to substitute AltiVec keyword tokens.
1970 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
1971 break;
1972
Chris Lattner3bd934a2008-07-26 01:18:38 +00001973 // It has to be available as a typedef too!
John McCallb3d87482010-08-24 05:47:05 +00001974 ParsedType TypeRep =
1975 Actions.getTypeName(*Tok.getIdentifierInfo(),
1976 Tok.getLocation(), getCurScope());
Douglas Gregor55f6b142009-02-09 18:46:07 +00001977
Chris Lattnerc199ab32009-04-12 20:42:31 +00001978 // If this is not a typedef name, don't parse it as part of the declspec,
1979 // it must be an implicit int or an error.
John McCallb3d87482010-08-24 05:47:05 +00001980 if (!TypeRep) {
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001981 if (ParseImplicitInt(DS, 0, TemplateInfo, AS)) continue;
Chris Lattner3bd934a2008-07-26 01:18:38 +00001982 goto DoneWithDeclSpec;
Chris Lattnerc199ab32009-04-12 20:42:31 +00001983 }
Douglas Gregor55f6b142009-02-09 18:46:07 +00001984
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001985 // If we're in a context where the identifier could be a class name,
1986 // check whether this is a constructor declaration.
1987 if (getLang().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00001988 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), getCurScope()) &&
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001989 isConstructorDeclarator())
Douglas Gregorb48fe382008-10-31 09:07:45 +00001990 goto DoneWithDeclSpec;
1991
Douglas Gregor1a51b4a2009-02-09 15:09:02 +00001992 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001993 DiagID, TypeRep);
Chris Lattner3bd934a2008-07-26 01:18:38 +00001994 if (isInvalid)
1995 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001996
Chris Lattner3bd934a2008-07-26 01:18:38 +00001997 DS.SetRangeEnd(Tok.getLocation());
1998 ConsumeToken(); // The identifier
1999
2000 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
2001 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00002002 // Objective-C interface.
2003 if (Tok.is(tok::less) && getLang().ObjC1)
2004 ParseObjCProtocolQualifiers(DS);
2005
Steve Naroff4f9b9f12008-09-22 10:28:57 +00002006 // Need to support trailing type qualifiers (e.g. "id<p> const").
2007 // If a type specifier follows, it will be diagnosed elsewhere.
2008 continue;
Chris Lattner3bd934a2008-07-26 01:18:38 +00002009 }
Douglas Gregor39a8de12009-02-25 19:37:18 +00002010
2011 // type-name
2012 case tok::annot_template_id: {
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +00002013 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregorc45c2322009-03-31 00:43:58 +00002014 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor39a8de12009-02-25 19:37:18 +00002015 // This template-id does not refer to a type name, so we're
2016 // done with the type-specifiers.
2017 goto DoneWithDeclSpec;
2018 }
2019
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002020 // If we're in a context where the template-id could be a
2021 // constructor name or specialization, check whether this is a
2022 // constructor declaration.
2023 if (getLang().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00002024 Actions.isCurrentClassName(*TemplateId->Name, getCurScope()) &&
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002025 isConstructorDeclarator())
2026 goto DoneWithDeclSpec;
2027
Douglas Gregor39a8de12009-02-25 19:37:18 +00002028 // Turn the template-id annotation token into a type annotation
2029 // token, then try again to parse it as a type-specifier.
Douglas Gregor31a19b62009-04-01 21:51:26 +00002030 AnnotateTemplateIdTokenAsType();
Douglas Gregor39a8de12009-02-25 19:37:18 +00002031 continue;
2032 }
2033
Reid Spencer5f016e22007-07-11 17:01:13 +00002034 // GNU attributes support.
2035 case tok::kw___attribute:
John McCall7f040a92010-12-24 02:08:15 +00002036 ParseGNUAttributes(DS.getAttributes());
Reid Spencer5f016e22007-07-11 17:01:13 +00002037 continue;
Steve Narofff59e17e2008-12-24 20:59:21 +00002038
2039 // Microsoft declspec support.
2040 case tok::kw___declspec:
John McCall7f040a92010-12-24 02:08:15 +00002041 ParseMicrosoftDeclSpec(DS.getAttributes());
Steve Narofff59e17e2008-12-24 20:59:21 +00002042 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00002043
Steve Naroff239f0732008-12-25 14:16:32 +00002044 // Microsoft single token adornments.
Steve Naroff86bc6cf2008-12-25 14:41:26 +00002045 case tok::kw___forceinline:
Eli Friedman290eeb02009-06-08 23:27:34 +00002046 // FIXME: Add handling here!
2047 break;
2048
2049 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00002050 case tok::kw___ptr32:
Steve Naroff86bc6cf2008-12-25 14:41:26 +00002051 case tok::kw___w64:
Steve Naroff239f0732008-12-25 14:16:32 +00002052 case tok::kw___cdecl:
2053 case tok::kw___stdcall:
2054 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00002055 case tok::kw___thiscall:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00002056 case tok::kw___unaligned:
John McCall7f040a92010-12-24 02:08:15 +00002057 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman290eeb02009-06-08 23:27:34 +00002058 continue;
2059
Dawn Perchik52fc3142010-09-03 01:29:35 +00002060 // Borland single token adornments.
2061 case tok::kw___pascal:
John McCall7f040a92010-12-24 02:08:15 +00002062 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik52fc3142010-09-03 01:29:35 +00002063 continue;
2064
Peter Collingbournef315fa82011-02-14 01:42:53 +00002065 // OpenCL single token adornments.
2066 case tok::kw___kernel:
2067 ParseOpenCLAttributes(DS.getAttributes());
2068 continue;
2069
Reid Spencer5f016e22007-07-11 17:01:13 +00002070 // storage-class-specifier
2071 case tok::kw_typedef:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002072 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_typedef, Loc,
2073 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002074 break;
2075 case tok::kw_extern:
2076 if (DS.isThreadSpecified())
Chris Lattner1ab3b962008-11-18 07:48:38 +00002077 Diag(Tok, diag::ext_thread_before) << "extern";
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002078 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_extern, Loc,
2079 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002080 break;
Steve Naroff8d54bf22007-12-18 00:16:02 +00002081 case tok::kw___private_extern__:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002082 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_private_extern,
2083 Loc, PrevSpec, DiagID);
Steve Naroff8d54bf22007-12-18 00:16:02 +00002084 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00002085 case tok::kw_static:
2086 if (DS.isThreadSpecified())
Chris Lattner1ab3b962008-11-18 07:48:38 +00002087 Diag(Tok, diag::ext_thread_before) << "static";
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002088 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_static, Loc,
2089 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002090 break;
2091 case tok::kw_auto:
Douglas Gregor18d8b792011-03-14 21:43:30 +00002092 if (getLang().CPlusPlus0x) {
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002093 if (isKnownToBeTypeSpecifier(GetLookAheadToken(1))) {
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002094 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc,
2095 PrevSpec, DiagID);
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002096 if (!isInvalid)
Richard Smith8f4fb192011-09-04 19:54:14 +00002097 Diag(Tok, diag::ext_auto_storage_class)
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002098 << FixItHint::CreateRemoval(DS.getStorageClassSpecLoc());
Richard Smith8f4fb192011-09-04 19:54:14 +00002099 } else
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002100 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec,
2101 DiagID);
Richard Smith8f4fb192011-09-04 19:54:14 +00002102 } else
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002103 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc,
2104 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002105 break;
2106 case tok::kw_register:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002107 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_register, Loc,
2108 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002109 break;
Sebastian Redl669d5d72008-11-14 23:42:31 +00002110 case tok::kw_mutable:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002111 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_mutable, Loc,
2112 PrevSpec, DiagID);
Sebastian Redl669d5d72008-11-14 23:42:31 +00002113 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00002114 case tok::kw___thread:
John McCallfec54012009-08-03 20:12:06 +00002115 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002116 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002117
Reid Spencer5f016e22007-07-11 17:01:13 +00002118 // function-specifier
2119 case tok::kw_inline:
John McCallfec54012009-08-03 20:12:06 +00002120 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002121 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +00002122 case tok::kw_virtual:
John McCallfec54012009-08-03 20:12:06 +00002123 isInvalid = DS.SetFunctionSpecVirtual(Loc, PrevSpec, DiagID);
Douglas Gregorb48fe382008-10-31 09:07:45 +00002124 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +00002125 case tok::kw_explicit:
John McCallfec54012009-08-03 20:12:06 +00002126 isInvalid = DS.SetFunctionSpecExplicit(Loc, PrevSpec, DiagID);
Douglas Gregorb48fe382008-10-31 09:07:45 +00002127 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002128
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002129 // alignment-specifier
2130 case tok::kw__Alignas:
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00002131 if (!getLang().C11)
2132 Diag(Tok, diag::ext_c11_alignas);
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002133 ParseAlignmentSpecifier(DS.getAttributes());
2134 continue;
2135
Anders Carlssonf47f7a12009-05-06 04:46:28 +00002136 // friend
2137 case tok::kw_friend:
John McCall67d1a672009-08-06 02:15:43 +00002138 if (DSContext == DSC_class)
2139 isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID);
2140 else {
2141 PrevSpec = ""; // not actually used by the diagnostic
2142 DiagID = diag::err_friend_invalid_in_context;
2143 isInvalid = true;
2144 }
Anders Carlssonf47f7a12009-05-06 04:46:28 +00002145 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002146
Douglas Gregor8d267c52011-09-09 02:06:17 +00002147 // Modules
2148 case tok::kw___module_private__:
2149 isInvalid = DS.setModulePrivateSpec(Loc, PrevSpec, DiagID);
2150 break;
2151
Sebastian Redl2ac67232009-11-05 15:47:02 +00002152 // constexpr
2153 case tok::kw_constexpr:
2154 isInvalid = DS.SetConstexprSpec(Loc, PrevSpec, DiagID);
2155 break;
2156
Chris Lattner80d0c892009-01-21 19:48:37 +00002157 // type-specifier
2158 case tok::kw_short:
John McCallfec54012009-08-03 20:12:06 +00002159 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec,
2160 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002161 break;
2162 case tok::kw_long:
2163 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCallfec54012009-08-03 20:12:06 +00002164 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
2165 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002166 else
John McCallfec54012009-08-03 20:12:06 +00002167 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2168 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002169 break;
Francois Pichet338d7f72011-04-28 01:59:37 +00002170 case tok::kw___int64:
2171 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2172 DiagID);
2173 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002174 case tok::kw_signed:
John McCallfec54012009-08-03 20:12:06 +00002175 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec,
2176 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002177 break;
2178 case tok::kw_unsigned:
John McCallfec54012009-08-03 20:12:06 +00002179 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
2180 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002181 break;
2182 case tok::kw__Complex:
John McCallfec54012009-08-03 20:12:06 +00002183 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
2184 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002185 break;
2186 case tok::kw__Imaginary:
John McCallfec54012009-08-03 20:12:06 +00002187 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
2188 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002189 break;
2190 case tok::kw_void:
John McCallfec54012009-08-03 20:12:06 +00002191 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec,
2192 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002193 break;
2194 case tok::kw_char:
John McCallfec54012009-08-03 20:12:06 +00002195 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec,
2196 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002197 break;
2198 case tok::kw_int:
John McCallfec54012009-08-03 20:12:06 +00002199 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec,
2200 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002201 break;
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00002202 case tok::kw_half:
2203 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_half, Loc, PrevSpec,
2204 DiagID);
2205 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002206 case tok::kw_float:
John McCallfec54012009-08-03 20:12:06 +00002207 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec,
2208 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002209 break;
2210 case tok::kw_double:
John McCallfec54012009-08-03 20:12:06 +00002211 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec,
2212 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002213 break;
2214 case tok::kw_wchar_t:
John McCallfec54012009-08-03 20:12:06 +00002215 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec,
2216 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002217 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002218 case tok::kw_char16_t:
John McCallfec54012009-08-03 20:12:06 +00002219 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec,
2220 DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002221 break;
2222 case tok::kw_char32_t:
John McCallfec54012009-08-03 20:12:06 +00002223 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec,
2224 DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002225 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002226 case tok::kw_bool:
2227 case tok::kw__Bool:
Argyrios Kyrtzidis4383e182010-11-16 18:18:13 +00002228 if (Tok.is(tok::kw_bool) &&
2229 DS.getTypeSpecType() != DeclSpec::TST_unspecified &&
2230 DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
2231 PrevSpec = ""; // Not used by the diagnostic.
2232 DiagID = diag::err_bool_redeclaration;
Fariborz Jahaniane106a0b2011-04-19 21:42:37 +00002233 // For better error recovery.
2234 Tok.setKind(tok::identifier);
Argyrios Kyrtzidis4383e182010-11-16 18:18:13 +00002235 isInvalid = true;
2236 } else {
2237 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec,
2238 DiagID);
2239 }
Chris Lattner80d0c892009-01-21 19:48:37 +00002240 break;
2241 case tok::kw__Decimal32:
John McCallfec54012009-08-03 20:12:06 +00002242 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
2243 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002244 break;
2245 case tok::kw__Decimal64:
John McCallfec54012009-08-03 20:12:06 +00002246 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
2247 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002248 break;
2249 case tok::kw__Decimal128:
John McCallfec54012009-08-03 20:12:06 +00002250 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
2251 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002252 break;
John Thompson82287d12010-02-05 00:12:22 +00002253 case tok::kw___vector:
2254 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
2255 break;
2256 case tok::kw___pixel:
2257 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
2258 break;
John McCalla5fc4722011-04-09 22:50:59 +00002259 case tok::kw___unknown_anytype:
2260 isInvalid = DS.SetTypeSpecType(TST_unknown_anytype, Loc,
2261 PrevSpec, DiagID);
2262 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002263
2264 // class-specifier:
2265 case tok::kw_class:
2266 case tok::kw_struct:
Chris Lattner4c97d762009-04-12 21:49:30 +00002267 case tok::kw_union: {
2268 tok::TokenKind Kind = Tok.getKind();
2269 ConsumeToken();
Douglas Gregorefaa93a2011-11-07 17:33:42 +00002270 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS, EnteringContext);
Chris Lattner80d0c892009-01-21 19:48:37 +00002271 continue;
Chris Lattner4c97d762009-04-12 21:49:30 +00002272 }
Chris Lattner80d0c892009-01-21 19:48:37 +00002273
2274 // enum-specifier:
2275 case tok::kw_enum:
Chris Lattner4c97d762009-04-12 21:49:30 +00002276 ConsumeToken();
Douglas Gregor9b9edd62010-03-02 17:53:14 +00002277 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS);
Chris Lattner80d0c892009-01-21 19:48:37 +00002278 continue;
2279
2280 // cv-qualifier:
2281 case tok::kw_const:
John McCallfec54012009-08-03 20:12:06 +00002282 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec, DiagID,
2283 getLang());
Chris Lattner80d0c892009-01-21 19:48:37 +00002284 break;
2285 case tok::kw_volatile:
John McCallfec54012009-08-03 20:12:06 +00002286 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
2287 getLang());
Chris Lattner80d0c892009-01-21 19:48:37 +00002288 break;
2289 case tok::kw_restrict:
John McCallfec54012009-08-03 20:12:06 +00002290 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
2291 getLang());
Chris Lattner80d0c892009-01-21 19:48:37 +00002292 break;
2293
Douglas Gregord57959a2009-03-27 23:10:48 +00002294 // C++ typename-specifier:
2295 case tok::kw_typename:
John McCall9ba61662010-02-26 08:45:28 +00002296 if (TryAnnotateTypeOrScopeToken()) {
2297 DS.SetTypeSpecError();
2298 goto DoneWithDeclSpec;
2299 }
2300 if (!Tok.is(tok::kw_typename))
Douglas Gregord57959a2009-03-27 23:10:48 +00002301 continue;
2302 break;
2303
Chris Lattner80d0c892009-01-21 19:48:37 +00002304 // GNU typeof support.
2305 case tok::kw_typeof:
2306 ParseTypeofSpecifier(DS);
2307 continue;
2308
David Blaikie42d6d0c2011-12-04 05:04:18 +00002309 case tok::annot_decltype:
Anders Carlsson6fd634f2009-06-24 17:47:40 +00002310 ParseDecltypeSpecifier(DS);
2311 continue;
2312
Sean Huntdb5d44b2011-05-19 05:37:45 +00002313 case tok::kw___underlying_type:
2314 ParseUnderlyingTypeSpecifier(DS);
Eli Friedmanb001de72011-10-06 23:00:33 +00002315 continue;
2316
2317 case tok::kw__Atomic:
2318 ParseAtomicSpecifier(DS);
2319 continue;
Sean Huntdb5d44b2011-05-19 05:37:45 +00002320
Peter Collingbourne207f4d82011-03-18 22:38:29 +00002321 // OpenCL qualifiers:
2322 case tok::kw_private:
2323 if (!getLang().OpenCL)
2324 goto DoneWithDeclSpec;
2325 case tok::kw___private:
2326 case tok::kw___global:
2327 case tok::kw___local:
2328 case tok::kw___constant:
2329 case tok::kw___read_only:
2330 case tok::kw___write_only:
2331 case tok::kw___read_write:
2332 ParseOpenCLQualifiers(DS);
2333 break;
2334
Steve Naroffd3ded1f2008-06-05 00:02:44 +00002335 case tok::less:
Chris Lattner3bd934a2008-07-26 01:18:38 +00002336 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattnerbce61352008-07-26 00:20:22 +00002337 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
2338 // but we support it.
Chris Lattner3bd934a2008-07-26 01:18:38 +00002339 if (DS.hasTypeSpecifier() || !getLang().ObjC1)
Chris Lattnerbce61352008-07-26 00:20:22 +00002340 goto DoneWithDeclSpec;
Mike Stump1eb44332009-09-09 15:08:12 +00002341
Douglas Gregor46f936e2010-11-19 17:10:50 +00002342 if (!ParseObjCProtocolQualifiers(DS))
2343 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
2344 << FixItHint::CreateInsertion(Loc, "id")
2345 << SourceRange(Loc, DS.getSourceRange().getEnd());
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00002346
2347 // Need to support trailing type qualifiers (e.g. "id<p> const").
2348 // If a type specifier follows, it will be diagnosed elsewhere.
2349 continue;
Reid Spencer5f016e22007-07-11 17:01:13 +00002350 }
John McCallfec54012009-08-03 20:12:06 +00002351 // If the specifier wasn't legal, issue a diagnostic.
Reid Spencer5f016e22007-07-11 17:01:13 +00002352 if (isInvalid) {
2353 assert(PrevSpec && "Method did not return previous specifier!");
John McCallfec54012009-08-03 20:12:06 +00002354 assert(DiagID);
Douglas Gregorae2fb142010-08-23 14:34:43 +00002355
2356 if (DiagID == diag::ext_duplicate_declspec)
2357 Diag(Tok, DiagID)
2358 << PrevSpec << FixItHint::CreateRemoval(Tok.getLocation());
2359 else
2360 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00002361 }
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002362
Chris Lattner81c018d2008-03-13 06:29:04 +00002363 DS.SetRangeEnd(Tok.getLocation());
Fariborz Jahaniane106a0b2011-04-19 21:42:37 +00002364 if (DiagID != diag::err_bool_redeclaration)
2365 ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00002366 }
2367}
Douglas Gregoradcac882008-12-01 23:54:00 +00002368
Chris Lattner7a0ab5f2009-01-06 06:59:53 +00002369/// ParseOptionalTypeSpecifier - Try to parse a single type-specifier. We
Douglas Gregor12e083c2008-11-07 15:42:26 +00002370/// primarily follow the C++ grammar with additions for C99 and GNU,
2371/// which together subsume the C grammar. Note that the C++
2372/// type-specifier also includes the C type-qualifier (for const,
2373/// volatile, and C99 restrict). Returns true if a type-specifier was
2374/// found (and parsed), false otherwise.
2375///
2376/// type-specifier: [C++ 7.1.5]
2377/// simple-type-specifier
2378/// class-specifier
2379/// enum-specifier
2380/// elaborated-type-specifier [TODO]
2381/// cv-qualifier
2382///
2383/// cv-qualifier: [C++ 7.1.5.1]
2384/// 'const'
2385/// 'volatile'
2386/// [C99] 'restrict'
2387///
2388/// simple-type-specifier: [ C++ 7.1.5.2]
2389/// '::'[opt] nested-name-specifier[opt] type-name [TODO]
2390/// '::'[opt] nested-name-specifier 'template' template-id [TODO]
2391/// 'char'
2392/// 'wchar_t'
2393/// 'bool'
2394/// 'short'
2395/// 'int'
2396/// 'long'
2397/// 'signed'
2398/// 'unsigned'
2399/// 'float'
2400/// 'double'
2401/// 'void'
2402/// [C99] '_Bool'
2403/// [C99] '_Complex'
2404/// [C99] '_Imaginary' // Removed in TC2?
2405/// [GNU] '_Decimal32'
2406/// [GNU] '_Decimal64'
2407/// [GNU] '_Decimal128'
2408/// [GNU] typeof-specifier
2409/// [OBJC] class-name objc-protocol-refs[opt] [TODO]
2410/// [OBJC] typedef-name objc-protocol-refs[opt] [TODO]
Anders Carlsson6fd634f2009-06-24 17:47:40 +00002411/// [C++0x] 'decltype' ( expression )
John Thompson82287d12010-02-05 00:12:22 +00002412/// [AltiVec] '__vector'
John McCallfec54012009-08-03 20:12:06 +00002413bool Parser::ParseOptionalTypeSpecifier(DeclSpec &DS, bool& isInvalid,
Chris Lattner7a0ab5f2009-01-06 06:59:53 +00002414 const char *&PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00002415 unsigned &DiagID,
Sebastian Redld9bafa72010-02-03 21:21:43 +00002416 const ParsedTemplateInfo &TemplateInfo,
2417 bool SuppressDeclarations) {
Douglas Gregor12e083c2008-11-07 15:42:26 +00002418 SourceLocation Loc = Tok.getLocation();
2419
2420 switch (Tok.getKind()) {
Chris Lattner166a8fc2009-01-04 23:41:41 +00002421 case tok::identifier: // foo::bar
Douglas Gregorc0b39642010-04-15 23:40:53 +00002422 // If we already have a type specifier, this identifier is not a type.
2423 if (DS.getTypeSpecType() != DeclSpec::TST_unspecified ||
2424 DS.getTypeSpecWidth() != DeclSpec::TSW_unspecified ||
2425 DS.getTypeSpecSign() != DeclSpec::TSS_unspecified)
2426 return false;
John Thompson82287d12010-02-05 00:12:22 +00002427 // Check for need to substitute AltiVec keyword tokens.
2428 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
2429 break;
2430 // Fall through.
David Blaikie42d6d0c2011-12-04 05:04:18 +00002431 case tok::kw_decltype:
Douglas Gregord57959a2009-03-27 23:10:48 +00002432 case tok::kw_typename: // typename foo::bar
Chris Lattner166a8fc2009-01-04 23:41:41 +00002433 // Annotate typenames and C++ scope specifiers. If we get one, just
2434 // recurse to handle whatever we get.
Kaelyn Uhrainfac94672011-10-11 01:02:41 +00002435 if (TryAnnotateTypeOrScopeToken(/*EnteringContext=*/false,
2436 /*NeedType=*/true))
John McCall9ba61662010-02-26 08:45:28 +00002437 return true;
2438 if (Tok.is(tok::identifier))
2439 return false;
2440 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
2441 TemplateInfo, SuppressDeclarations);
Chris Lattner166a8fc2009-01-04 23:41:41 +00002442 case tok::coloncolon: // ::foo::bar
2443 if (NextToken().is(tok::kw_new) || // ::new
2444 NextToken().is(tok::kw_delete)) // ::delete
2445 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002446
Chris Lattner166a8fc2009-01-04 23:41:41 +00002447 // Annotate typenames and C++ scope specifiers. If we get one, just
2448 // recurse to handle whatever we get.
Kaelyn Uhrainfac94672011-10-11 01:02:41 +00002449 if (TryAnnotateTypeOrScopeToken(/*EnteringContext=*/false,
2450 /*NeedType=*/true))
John McCall9ba61662010-02-26 08:45:28 +00002451 return true;
2452 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
2453 TemplateInfo, SuppressDeclarations);
Mike Stump1eb44332009-09-09 15:08:12 +00002454
Douglas Gregor12e083c2008-11-07 15:42:26 +00002455 // simple-type-specifier:
Chris Lattnerb31757b2009-01-06 05:06:21 +00002456 case tok::annot_typename: {
John McCallb3d87482010-08-24 05:47:05 +00002457 if (ParsedType T = getTypeAnnotation(Tok)) {
Nico Weber253e80b2010-11-22 10:30:56 +00002458 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename,
2459 Tok.getAnnotationEndLoc(), PrevSpec,
John McCallb3d87482010-08-24 05:47:05 +00002460 DiagID, T);
2461 } else
Douglas Gregor31a19b62009-04-01 21:51:26 +00002462 DS.SetTypeSpecError();
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002463 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
2464 ConsumeToken(); // The typename
Mike Stump1eb44332009-09-09 15:08:12 +00002465
Douglas Gregor12e083c2008-11-07 15:42:26 +00002466 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
2467 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
2468 // Objective-C interface. If we don't have Objective-C or a '<', this is
2469 // just a normal reference to a typedef name.
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00002470 if (Tok.is(tok::less) && getLang().ObjC1)
2471 ParseObjCProtocolQualifiers(DS);
2472
Douglas Gregor12e083c2008-11-07 15:42:26 +00002473 return true;
2474 }
2475
2476 case tok::kw_short:
John McCallfec54012009-08-03 20:12:06 +00002477 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002478 break;
2479 case tok::kw_long:
2480 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCallfec54012009-08-03 20:12:06 +00002481 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
2482 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002483 else
John McCallfec54012009-08-03 20:12:06 +00002484 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2485 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002486 break;
Francois Pichet338d7f72011-04-28 01:59:37 +00002487 case tok::kw___int64:
2488 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2489 DiagID);
2490 break;
Douglas Gregor12e083c2008-11-07 15:42:26 +00002491 case tok::kw_signed:
John McCallfec54012009-08-03 20:12:06 +00002492 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002493 break;
2494 case tok::kw_unsigned:
John McCallfec54012009-08-03 20:12:06 +00002495 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
2496 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002497 break;
2498 case tok::kw__Complex:
John McCallfec54012009-08-03 20:12:06 +00002499 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
2500 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002501 break;
2502 case tok::kw__Imaginary:
John McCallfec54012009-08-03 20:12:06 +00002503 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
2504 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002505 break;
2506 case tok::kw_void:
John McCallfec54012009-08-03 20:12:06 +00002507 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002508 break;
2509 case tok::kw_char:
John McCallfec54012009-08-03 20:12:06 +00002510 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002511 break;
2512 case tok::kw_int:
John McCallfec54012009-08-03 20:12:06 +00002513 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002514 break;
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00002515 case tok::kw_half:
2516 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_half, Loc, PrevSpec, DiagID);
2517 break;
Douglas Gregor12e083c2008-11-07 15:42:26 +00002518 case tok::kw_float:
John McCallfec54012009-08-03 20:12:06 +00002519 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002520 break;
2521 case tok::kw_double:
John McCallfec54012009-08-03 20:12:06 +00002522 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002523 break;
2524 case tok::kw_wchar_t:
John McCallfec54012009-08-03 20:12:06 +00002525 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002526 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002527 case tok::kw_char16_t:
John McCallfec54012009-08-03 20:12:06 +00002528 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002529 break;
2530 case tok::kw_char32_t:
John McCallfec54012009-08-03 20:12:06 +00002531 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002532 break;
Douglas Gregor12e083c2008-11-07 15:42:26 +00002533 case tok::kw_bool:
2534 case tok::kw__Bool:
John McCallfec54012009-08-03 20:12:06 +00002535 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002536 break;
2537 case tok::kw__Decimal32:
John McCallfec54012009-08-03 20:12:06 +00002538 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
2539 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002540 break;
2541 case tok::kw__Decimal64:
John McCallfec54012009-08-03 20:12:06 +00002542 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
2543 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002544 break;
2545 case tok::kw__Decimal128:
John McCallfec54012009-08-03 20:12:06 +00002546 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
2547 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002548 break;
John Thompson82287d12010-02-05 00:12:22 +00002549 case tok::kw___vector:
2550 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
2551 break;
2552 case tok::kw___pixel:
2553 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
2554 break;
2555
Douglas Gregor12e083c2008-11-07 15:42:26 +00002556 // class-specifier:
2557 case tok::kw_class:
2558 case tok::kw_struct:
Chris Lattner4c97d762009-04-12 21:49:30 +00002559 case tok::kw_union: {
2560 tok::TokenKind Kind = Tok.getKind();
2561 ConsumeToken();
Sebastian Redld9bafa72010-02-03 21:21:43 +00002562 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS_none,
Douglas Gregorefaa93a2011-11-07 17:33:42 +00002563 /*EnteringContext=*/false,
Sebastian Redld9bafa72010-02-03 21:21:43 +00002564 SuppressDeclarations);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002565 return true;
Chris Lattner4c97d762009-04-12 21:49:30 +00002566 }
Douglas Gregor12e083c2008-11-07 15:42:26 +00002567
2568 // enum-specifier:
2569 case tok::kw_enum:
Chris Lattner4c97d762009-04-12 21:49:30 +00002570 ConsumeToken();
Douglas Gregor9b9edd62010-03-02 17:53:14 +00002571 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS_none);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002572 return true;
2573
2574 // cv-qualifier:
2575 case tok::kw_const:
2576 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00002577 DiagID, getLang());
Douglas Gregor12e083c2008-11-07 15:42:26 +00002578 break;
2579 case tok::kw_volatile:
2580 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00002581 DiagID, getLang());
Douglas Gregor12e083c2008-11-07 15:42:26 +00002582 break;
2583 case tok::kw_restrict:
2584 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00002585 DiagID, getLang());
Douglas Gregor12e083c2008-11-07 15:42:26 +00002586 break;
2587
2588 // GNU typeof support.
2589 case tok::kw_typeof:
2590 ParseTypeofSpecifier(DS);
2591 return true;
2592
Anders Carlsson6fd634f2009-06-24 17:47:40 +00002593 // C++0x decltype support.
David Blaikie42d6d0c2011-12-04 05:04:18 +00002594 case tok::annot_decltype:
Anders Carlsson6fd634f2009-06-24 17:47:40 +00002595 ParseDecltypeSpecifier(DS);
2596 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00002597
Sean Huntdb5d44b2011-05-19 05:37:45 +00002598 // C++0x type traits support.
2599 case tok::kw___underlying_type:
2600 ParseUnderlyingTypeSpecifier(DS);
2601 return true;
2602
Eli Friedmanb001de72011-10-06 23:00:33 +00002603 case tok::kw__Atomic:
2604 ParseAtomicSpecifier(DS);
2605 return true;
2606
Peter Collingbourne207f4d82011-03-18 22:38:29 +00002607 // OpenCL qualifiers:
2608 case tok::kw_private:
2609 if (!getLang().OpenCL)
2610 return false;
2611 case tok::kw___private:
2612 case tok::kw___global:
2613 case tok::kw___local:
2614 case tok::kw___constant:
2615 case tok::kw___read_only:
2616 case tok::kw___write_only:
2617 case tok::kw___read_write:
2618 ParseOpenCLQualifiers(DS);
2619 break;
2620
Anders Carlsson0b7f7892009-06-26 23:44:14 +00002621 // C++0x auto support.
2622 case tok::kw_auto:
Richard Smith87e96eb2011-09-04 20:24:20 +00002623 // This is only called in situations where a storage-class specifier is
2624 // illegal, so we can assume an auto type specifier was intended even in
2625 // C++98. In C++98 mode, DeclSpec::Finish will produce an appropriate
2626 // extension diagnostic.
2627 if (!getLang().CPlusPlus)
Anders Carlsson0b7f7892009-06-26 23:44:14 +00002628 return false;
2629
John McCallfec54012009-08-03 20:12:06 +00002630 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec, DiagID);
Anders Carlsson0b7f7892009-06-26 23:44:14 +00002631 break;
Dawn Perchik52fc3142010-09-03 01:29:35 +00002632
Eli Friedman290eeb02009-06-08 23:27:34 +00002633 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00002634 case tok::kw___ptr32:
Eli Friedman290eeb02009-06-08 23:27:34 +00002635 case tok::kw___w64:
Steve Naroff239f0732008-12-25 14:16:32 +00002636 case tok::kw___cdecl:
2637 case tok::kw___stdcall:
2638 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00002639 case tok::kw___thiscall:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00002640 case tok::kw___unaligned:
John McCall7f040a92010-12-24 02:08:15 +00002641 ParseMicrosoftTypeAttributes(DS.getAttributes());
Chris Lattner837acd02009-01-21 19:19:26 +00002642 return true;
Steve Naroff239f0732008-12-25 14:16:32 +00002643
Dawn Perchik52fc3142010-09-03 01:29:35 +00002644 case tok::kw___pascal:
John McCall7f040a92010-12-24 02:08:15 +00002645 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik52fc3142010-09-03 01:29:35 +00002646 return true;
2647
Douglas Gregor12e083c2008-11-07 15:42:26 +00002648 default:
2649 // Not a type-specifier; do nothing.
2650 return false;
2651 }
2652
2653 // If the specifier combination wasn't legal, issue a diagnostic.
2654 if (isInvalid) {
2655 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner1ab3b962008-11-18 07:48:38 +00002656 // Pick between error or extwarn.
Chris Lattner1ab3b962008-11-18 07:48:38 +00002657 Diag(Tok, DiagID) << PrevSpec;
Douglas Gregor12e083c2008-11-07 15:42:26 +00002658 }
2659 DS.SetRangeEnd(Tok.getLocation());
2660 ConsumeToken(); // whatever we parsed above.
2661 return true;
2662}
Reid Spencer5f016e22007-07-11 17:01:13 +00002663
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002664/// ParseStructDeclaration - Parse a struct declaration without the terminating
2665/// semicolon.
2666///
Reid Spencer5f016e22007-07-11 17:01:13 +00002667/// struct-declaration:
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002668/// specifier-qualifier-list struct-declarator-list
Reid Spencer5f016e22007-07-11 17:01:13 +00002669/// [GNU] __extension__ struct-declaration
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002670/// [GNU] specifier-qualifier-list
Reid Spencer5f016e22007-07-11 17:01:13 +00002671/// struct-declarator-list:
2672/// struct-declarator
2673/// struct-declarator-list ',' struct-declarator
2674/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
2675/// struct-declarator:
2676/// declarator
2677/// [GNU] declarator attributes[opt]
2678/// declarator[opt] ':' constant-expression
2679/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
2680///
Chris Lattnere1359422008-04-10 06:46:29 +00002681void Parser::
John McCallbdd563e2009-11-03 02:38:08 +00002682ParseStructDeclaration(DeclSpec &DS, FieldCallback &Fields) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002683
Chris Lattnerc46d1a12008-10-20 06:45:43 +00002684 if (Tok.is(tok::kw___extension__)) {
2685 // __extension__ silences extension warnings in the subexpression.
2686 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroff28a7ca82007-08-20 22:28:22 +00002687 ConsumeToken();
Chris Lattnerc46d1a12008-10-20 06:45:43 +00002688 return ParseStructDeclaration(DS, Fields);
2689 }
Mike Stump1eb44332009-09-09 15:08:12 +00002690
Steve Naroff28a7ca82007-08-20 22:28:22 +00002691 // Parse the common specifier-qualifiers-list piece.
Steve Naroff28a7ca82007-08-20 22:28:22 +00002692 ParseSpecifierQualifierList(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00002693
Douglas Gregor4920f1f2009-01-12 22:49:06 +00002694 // If there are no declarators, this is a free-standing declaration
2695 // specifier. Let the actions module cope with it.
Chris Lattner04d66662007-10-09 17:33:22 +00002696 if (Tok.is(tok::semi)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00002697 Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none, DS);
Steve Naroff28a7ca82007-08-20 22:28:22 +00002698 return;
2699 }
2700
2701 // Read struct-declarators until we find the semicolon.
John McCallbdd563e2009-11-03 02:38:08 +00002702 bool FirstDeclarator = true;
Richard Smith7984de32012-01-12 23:53:29 +00002703 SourceLocation CommaLoc;
Steve Naroff28a7ca82007-08-20 22:28:22 +00002704 while (1) {
John McCall54abf7d2009-11-04 02:18:39 +00002705 ParsingDeclRAIIObject PD(*this);
John McCallbdd563e2009-11-03 02:38:08 +00002706 FieldDeclarator DeclaratorInfo(DS);
Richard Smith7984de32012-01-12 23:53:29 +00002707 DeclaratorInfo.D.setCommaLoc(CommaLoc);
John McCallbdd563e2009-11-03 02:38:08 +00002708
2709 // Attributes are only allowed here on successive declarators.
John McCall7f040a92010-12-24 02:08:15 +00002710 if (!FirstDeclarator)
2711 MaybeParseGNUAttributes(DeclaratorInfo.D);
Mike Stump1eb44332009-09-09 15:08:12 +00002712
Steve Naroff28a7ca82007-08-20 22:28:22 +00002713 /// struct-declarator: declarator
2714 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattnera1efc8c2009-12-10 01:59:24 +00002715 if (Tok.isNot(tok::colon)) {
2716 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
2717 ColonProtectionRAIIObject X(*this);
Chris Lattnere1359422008-04-10 06:46:29 +00002718 ParseDeclarator(DeclaratorInfo.D);
Chris Lattnera1efc8c2009-12-10 01:59:24 +00002719 }
Mike Stump1eb44332009-09-09 15:08:12 +00002720
Chris Lattner04d66662007-10-09 17:33:22 +00002721 if (Tok.is(tok::colon)) {
Steve Naroff28a7ca82007-08-20 22:28:22 +00002722 ConsumeToken();
John McCall60d7b3a2010-08-24 06:29:42 +00002723 ExprResult Res(ParseConstantExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002724 if (Res.isInvalid())
Steve Naroff28a7ca82007-08-20 22:28:22 +00002725 SkipUntil(tok::semi, true, true);
Chris Lattner60b1e3e2008-04-10 06:15:14 +00002726 else
Sebastian Redleffa8d12008-12-10 00:02:53 +00002727 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroff28a7ca82007-08-20 22:28:22 +00002728 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00002729
Steve Naroff28a7ca82007-08-20 22:28:22 +00002730 // If attributes exist after the declarator, parse them.
John McCall7f040a92010-12-24 02:08:15 +00002731 MaybeParseGNUAttributes(DeclaratorInfo.D);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002732
John McCallbdd563e2009-11-03 02:38:08 +00002733 // We're done with this declarator; invoke the callback.
John McCalld226f652010-08-21 09:40:31 +00002734 Decl *D = Fields.invoke(DeclaratorInfo);
John McCall54abf7d2009-11-04 02:18:39 +00002735 PD.complete(D);
John McCallbdd563e2009-11-03 02:38:08 +00002736
Steve Naroff28a7ca82007-08-20 22:28:22 +00002737 // If we don't have a comma, it is either the end of the list (a ';')
2738 // or an error, bail out.
Chris Lattner04d66662007-10-09 17:33:22 +00002739 if (Tok.isNot(tok::comma))
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002740 return;
Sebastian Redlab197ba2009-02-09 18:23:29 +00002741
Steve Naroff28a7ca82007-08-20 22:28:22 +00002742 // Consume the comma.
Richard Smith7984de32012-01-12 23:53:29 +00002743 CommaLoc = ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00002744
John McCallbdd563e2009-11-03 02:38:08 +00002745 FirstDeclarator = false;
Steve Naroff28a7ca82007-08-20 22:28:22 +00002746 }
Steve Naroff28a7ca82007-08-20 22:28:22 +00002747}
2748
2749/// ParseStructUnionBody
2750/// struct-contents:
2751/// struct-declaration-list
2752/// [EXT] empty
2753/// [GNU] "struct-declaration-list" without terminatoring ';'
2754/// struct-declaration-list:
2755/// struct-declaration
2756/// struct-declaration-list struct-declaration
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002757/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroff28a7ca82007-08-20 22:28:22 +00002758///
Reid Spencer5f016e22007-07-11 17:01:13 +00002759void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
John McCalld226f652010-08-21 09:40:31 +00002760 unsigned TagType, Decl *TagDecl) {
John McCallf312b1e2010-08-26 23:41:50 +00002761 PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc,
2762 "parsing struct/union body");
Mike Stump1eb44332009-09-09 15:08:12 +00002763
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002764 BalancedDelimiterTracker T(*this, tok::l_brace);
2765 if (T.consumeOpen())
2766 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002767
Douglas Gregor3218c4b2009-01-09 22:42:13 +00002768 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
Douglas Gregor23c94db2010-07-02 17:43:08 +00002769 Actions.ActOnTagStartDefinition(getCurScope(), TagDecl);
Douglas Gregor72de6672009-01-08 20:45:30 +00002770
Reid Spencer5f016e22007-07-11 17:01:13 +00002771 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
2772 // C++.
Richard Smithd7c56e12011-12-29 21:57:33 +00002773 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus) {
2774 Diag(Tok, diag::ext_empty_struct_union) << (TagType == TST_union);
2775 Diag(Tok, diag::warn_empty_struct_union_compat) << (TagType == TST_union);
2776 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002777
Chris Lattner5f9e2722011-07-23 10:55:15 +00002778 SmallVector<Decl *, 32> FieldDecls;
Chris Lattnere1359422008-04-10 06:46:29 +00002779
Reid Spencer5f016e22007-07-11 17:01:13 +00002780 // While we still have something to read, read the declarations in the struct.
Chris Lattner04d66662007-10-09 17:33:22 +00002781 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002782 // Each iteration of this loop reads one struct-declaration.
Mike Stump1eb44332009-09-09 15:08:12 +00002783
Reid Spencer5f016e22007-07-11 17:01:13 +00002784 // Check for extraneous top-level semicolon.
Chris Lattner04d66662007-10-09 17:33:22 +00002785 if (Tok.is(tok::semi)) {
Douglas Gregor9b3064b2009-04-01 22:41:11 +00002786 Diag(Tok, diag::ext_extra_struct_semi)
Douglas Gregorf13ca062010-06-16 23:08:59 +00002787 << DeclSpec::getSpecifierName((DeclSpec::TST)TagType)
Douglas Gregor849b2432010-03-31 17:46:05 +00002788 << FixItHint::CreateRemoval(Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00002789 ConsumeToken();
2790 continue;
2791 }
Chris Lattnere1359422008-04-10 06:46:29 +00002792
2793 // Parse all the comma separated declarators.
John McCall0b7e6782011-03-24 11:26:52 +00002794 DeclSpec DS(AttrFactory);
Mike Stump1eb44332009-09-09 15:08:12 +00002795
John McCallbdd563e2009-11-03 02:38:08 +00002796 if (!Tok.is(tok::at)) {
2797 struct CFieldCallback : FieldCallback {
2798 Parser &P;
John McCalld226f652010-08-21 09:40:31 +00002799 Decl *TagDecl;
Chris Lattner5f9e2722011-07-23 10:55:15 +00002800 SmallVectorImpl<Decl *> &FieldDecls;
John McCallbdd563e2009-11-03 02:38:08 +00002801
John McCalld226f652010-08-21 09:40:31 +00002802 CFieldCallback(Parser &P, Decl *TagDecl,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002803 SmallVectorImpl<Decl *> &FieldDecls) :
John McCallbdd563e2009-11-03 02:38:08 +00002804 P(P), TagDecl(TagDecl), FieldDecls(FieldDecls) {}
2805
John McCalld226f652010-08-21 09:40:31 +00002806 virtual Decl *invoke(FieldDeclarator &FD) {
John McCallbdd563e2009-11-03 02:38:08 +00002807 // Install the declarator into the current TagDecl.
John McCalld226f652010-08-21 09:40:31 +00002808 Decl *Field = P.Actions.ActOnField(P.getCurScope(), TagDecl,
John McCall4ba39712009-11-03 21:13:47 +00002809 FD.D.getDeclSpec().getSourceRange().getBegin(),
2810 FD.D, FD.BitfieldSize);
John McCallbdd563e2009-11-03 02:38:08 +00002811 FieldDecls.push_back(Field);
2812 return Field;
Douglas Gregor91a28862009-08-26 14:27:30 +00002813 }
John McCallbdd563e2009-11-03 02:38:08 +00002814 } Callback(*this, TagDecl, FieldDecls);
2815
2816 ParseStructDeclaration(DS, Callback);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002817 } else { // Handle @defs
2818 ConsumeToken();
2819 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
2820 Diag(Tok, diag::err_unexpected_at);
Chris Lattner3e156ad2010-02-02 00:37:27 +00002821 SkipUntil(tok::semi, true);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002822 continue;
2823 }
2824 ConsumeToken();
2825 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
2826 if (!Tok.is(tok::identifier)) {
2827 Diag(Tok, diag::err_expected_ident);
Chris Lattner3e156ad2010-02-02 00:37:27 +00002828 SkipUntil(tok::semi, true);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002829 continue;
2830 }
Chris Lattner5f9e2722011-07-23 10:55:15 +00002831 SmallVector<Decl *, 16> Fields;
Douglas Gregor23c94db2010-07-02 17:43:08 +00002832 Actions.ActOnDefs(getCurScope(), TagDecl, Tok.getLocation(),
Douglas Gregor44b43212008-12-11 16:49:14 +00002833 Tok.getIdentifierInfo(), Fields);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002834 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
2835 ConsumeToken();
2836 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
Mike Stump1eb44332009-09-09 15:08:12 +00002837 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002838
Chris Lattner04d66662007-10-09 17:33:22 +00002839 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002840 ConsumeToken();
Chris Lattner04d66662007-10-09 17:33:22 +00002841 } else if (Tok.is(tok::r_brace)) {
Chris Lattner3e156ad2010-02-02 00:37:27 +00002842 ExpectAndConsume(tok::semi, diag::ext_expected_semi_decl_list);
Reid Spencer5f016e22007-07-11 17:01:13 +00002843 break;
2844 } else {
Chris Lattner3e156ad2010-02-02 00:37:27 +00002845 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
2846 // Skip to end of block or statement to avoid ext-warning on extra ';'.
Reid Spencer5f016e22007-07-11 17:01:13 +00002847 SkipUntil(tok::r_brace, true, true);
Chris Lattner3e156ad2010-02-02 00:37:27 +00002848 // If we stopped at a ';', eat it.
2849 if (Tok.is(tok::semi)) ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00002850 }
2851 }
Mike Stump1eb44332009-09-09 15:08:12 +00002852
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002853 T.consumeClose();
Mike Stump1eb44332009-09-09 15:08:12 +00002854
John McCall0b7e6782011-03-24 11:26:52 +00002855 ParsedAttributes attrs(AttrFactory);
Reid Spencer5f016e22007-07-11 17:01:13 +00002856 // If attributes exist after struct contents, parse them.
John McCall7f040a92010-12-24 02:08:15 +00002857 MaybeParseGNUAttributes(attrs);
Daniel Dunbar1bfe1c22008-10-03 02:03:53 +00002858
Douglas Gregor23c94db2010-07-02 17:43:08 +00002859 Actions.ActOnFields(getCurScope(),
David Blaikie77b6de02011-09-22 02:58:26 +00002860 RecordLoc, TagDecl, FieldDecls,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002861 T.getOpenLocation(), T.getCloseLocation(),
John McCall7f040a92010-12-24 02:08:15 +00002862 attrs.getList());
Douglas Gregor72de6672009-01-08 20:45:30 +00002863 StructScope.Exit();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002864 Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl,
2865 T.getCloseLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00002866}
2867
Reid Spencer5f016e22007-07-11 17:01:13 +00002868/// ParseEnumSpecifier
2869/// enum-specifier: [C99 6.7.2.2]
2870/// 'enum' identifier[opt] '{' enumerator-list '}'
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002871///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Reid Spencer5f016e22007-07-11 17:01:13 +00002872/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
2873/// '}' attributes[opt]
2874/// 'enum' identifier
2875/// [GNU] 'enum' attributes[opt] identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002876///
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002877/// [C++0x] enum-head '{' enumerator-list[opt] '}'
2878/// [C++0x] enum-head '{' enumerator-list ',' '}'
2879///
2880/// enum-head: [C++0x]
2881/// enum-key attributes[opt] identifier[opt] enum-base[opt]
2882/// enum-key attributes[opt] nested-name-specifier identifier enum-base[opt]
2883///
2884/// enum-key: [C++0x]
2885/// 'enum'
2886/// 'enum' 'class'
2887/// 'enum' 'struct'
2888///
2889/// enum-base: [C++0x]
2890/// ':' type-specifier-seq
2891///
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002892/// [C++] elaborated-type-specifier:
2893/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
2894///
Chris Lattner4c97d762009-04-12 21:49:30 +00002895void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregor9b9edd62010-03-02 17:53:14 +00002896 const ParsedTemplateInfo &TemplateInfo,
Chris Lattner4c97d762009-04-12 21:49:30 +00002897 AccessSpecifier AS) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002898 // Parse the tag portion of this.
Douglas Gregor374929f2009-09-18 15:37:17 +00002899 if (Tok.is(tok::code_completion)) {
2900 // Code completion for an enum name.
Douglas Gregor23c94db2010-07-02 17:43:08 +00002901 Actions.CodeCompleteTag(getCurScope(), DeclSpec::TST_enum);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002902 return cutOffParsing();
Douglas Gregor374929f2009-09-18 15:37:17 +00002903 }
John McCall57c13002011-07-06 05:58:41 +00002904
Richard Smithbdad7a22012-01-10 01:33:14 +00002905 SourceLocation ScopedEnumKWLoc;
John McCall57c13002011-07-06 05:58:41 +00002906 bool IsScopedUsingClassTag = false;
2907
2908 if (getLang().CPlusPlus0x &&
2909 (Tok.is(tok::kw_class) || Tok.is(tok::kw_struct))) {
Richard Smith7fe62082011-10-15 05:09:34 +00002910 Diag(Tok, diag::warn_cxx98_compat_scoped_enum);
John McCall57c13002011-07-06 05:58:41 +00002911 IsScopedUsingClassTag = Tok.is(tok::kw_class);
Richard Smithbdad7a22012-01-10 01:33:14 +00002912 ScopedEnumKWLoc = ConsumeToken();
John McCall57c13002011-07-06 05:58:41 +00002913 }
Douglas Gregor374929f2009-09-18 15:37:17 +00002914
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002915 // If attributes exist after tag, parse them.
John McCall0b7e6782011-03-24 11:26:52 +00002916 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00002917 MaybeParseGNUAttributes(attrs);
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002918
Douglas Gregor5471bc82011-09-08 17:18:35 +00002919 bool AllowFixedUnderlyingType
Francois Pichet62ec1f22011-09-17 17:15:52 +00002920 = getLang().CPlusPlus0x || getLang().MicrosoftExt || getLang().ObjC2;
John McCall57c13002011-07-06 05:58:41 +00002921
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00002922 CXXScopeSpec &SS = DS.getTypeSpecScope();
John McCall9ba61662010-02-26 08:45:28 +00002923 if (getLang().CPlusPlus) {
John McCall57c13002011-07-06 05:58:41 +00002924 // "enum foo : bar;" is not a potential typo for "enum foo::bar;"
2925 // if a fixed underlying type is allowed.
2926 ColonProtectionRAIIObject X(*this, AllowFixedUnderlyingType);
2927
Douglas Gregorefaa93a2011-11-07 17:33:42 +00002928 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
2929 /*EnteringContext=*/false))
John McCall9ba61662010-02-26 08:45:28 +00002930 return;
2931
2932 if (SS.isSet() && Tok.isNot(tok::identifier)) {
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002933 Diag(Tok, diag::err_expected_ident);
2934 if (Tok.isNot(tok::l_brace)) {
2935 // Has no name and is not a definition.
2936 // Skip the rest of this declarator, up until the comma or semicolon.
2937 SkipUntil(tok::comma, true);
2938 return;
2939 }
2940 }
2941 }
Mike Stump1eb44332009-09-09 15:08:12 +00002942
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002943 // Must have either 'enum name' or 'enum {...}'.
Douglas Gregorb9075602011-02-22 02:55:24 +00002944 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace) &&
2945 (AllowFixedUnderlyingType && Tok.isNot(tok::colon))) {
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002946 Diag(Tok, diag::err_expected_ident_lbrace);
Mike Stump1eb44332009-09-09 15:08:12 +00002947
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002948 // Skip the rest of this declarator, up until the comma or semicolon.
2949 SkipUntil(tok::comma, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00002950 return;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002951 }
Mike Stump1eb44332009-09-09 15:08:12 +00002952
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002953 // If an identifier is present, consume and remember it.
2954 IdentifierInfo *Name = 0;
2955 SourceLocation NameLoc;
2956 if (Tok.is(tok::identifier)) {
2957 Name = Tok.getIdentifierInfo();
2958 NameLoc = ConsumeToken();
2959 }
Mike Stump1eb44332009-09-09 15:08:12 +00002960
Richard Smithbdad7a22012-01-10 01:33:14 +00002961 if (!Name && ScopedEnumKWLoc.isValid()) {
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002962 // C++0x 7.2p2: The optional identifier shall not be omitted in the
2963 // declaration of a scoped enumeration.
2964 Diag(Tok, diag::err_scoped_enum_missing_identifier);
Richard Smithbdad7a22012-01-10 01:33:14 +00002965 ScopedEnumKWLoc = SourceLocation();
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00002966 IsScopedUsingClassTag = false;
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002967 }
2968
2969 TypeResult BaseType;
2970
Douglas Gregora61b3e72010-12-01 17:42:47 +00002971 // Parse the fixed underlying type.
Douglas Gregorb9075602011-02-22 02:55:24 +00002972 if (AllowFixedUnderlyingType && Tok.is(tok::colon)) {
Douglas Gregora61b3e72010-12-01 17:42:47 +00002973 bool PossibleBitfield = false;
2974 if (getCurScope()->getFlags() & Scope::ClassScope) {
2975 // If we're in class scope, this can either be an enum declaration with
2976 // an underlying type, or a declaration of a bitfield member. We try to
2977 // use a simple disambiguation scheme first to catch the common cases
2978 // (integer literal, sizeof); if it's still ambiguous, we then consider
2979 // anything that's a simple-type-specifier followed by '(' as an
2980 // expression. This suffices because function types are not valid
2981 // underlying types anyway.
2982 TPResult TPR = isExpressionOrTypeSpecifierSimple(NextToken().getKind());
2983 // If the next token starts an expression, we know we're parsing a
2984 // bit-field. This is the common case.
2985 if (TPR == TPResult::True())
2986 PossibleBitfield = true;
2987 // If the next token starts a type-specifier-seq, it may be either a
2988 // a fixed underlying type or the start of a function-style cast in C++;
2989 // lookahead one more token to see if it's obvious that we have a
2990 // fixed underlying type.
2991 else if (TPR == TPResult::False() &&
2992 GetLookAheadToken(2).getKind() == tok::semi) {
2993 // Consume the ':'.
2994 ConsumeToken();
2995 } else {
2996 // We have the start of a type-specifier-seq, so we have to perform
2997 // tentative parsing to determine whether we have an expression or a
2998 // type.
2999 TentativeParsingAction TPA(*this);
3000
3001 // Consume the ':'.
3002 ConsumeToken();
Richard Smithd81e9612012-02-23 01:36:12 +00003003
3004 // If we see a type specifier followed by an open-brace, we have an
3005 // ambiguity between an underlying type and a C++11 braced
3006 // function-style cast. Resolve this by always treating it as an
3007 // underlying type.
3008 // FIXME: The standard is not entirely clear on how to disambiguate in
3009 // this case.
3010 if ((getLang().CPlusPlus &&
3011 isCXXDeclarationSpecifier(TPResult::True()) != TPResult::True()) ||
Douglas Gregor86f208c2011-02-22 20:32:04 +00003012 (!getLang().CPlusPlus && !isDeclarationSpecifier(true))) {
Douglas Gregora61b3e72010-12-01 17:42:47 +00003013 // We'll parse this as a bitfield later.
3014 PossibleBitfield = true;
3015 TPA.Revert();
3016 } else {
3017 // We have a type-specifier-seq.
3018 TPA.Commit();
3019 }
3020 }
3021 } else {
3022 // Consume the ':'.
3023 ConsumeToken();
3024 }
3025
3026 if (!PossibleBitfield) {
3027 SourceRange Range;
3028 BaseType = ParseTypeName(&Range);
Douglas Gregor86f208c2011-02-22 20:32:04 +00003029
Douglas Gregor5471bc82011-09-08 17:18:35 +00003030 if (!getLang().CPlusPlus0x && !getLang().ObjC2)
Douglas Gregor86f208c2011-02-22 20:32:04 +00003031 Diag(StartLoc, diag::ext_ms_enum_fixed_underlying_type)
3032 << Range;
Richard Smith7fe62082011-10-15 05:09:34 +00003033 if (getLang().CPlusPlus0x)
3034 Diag(StartLoc, diag::warn_cxx98_compat_enum_fixed_underlying_type);
Douglas Gregora61b3e72010-12-01 17:42:47 +00003035 }
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003036 }
3037
Richard Smithbdad7a22012-01-10 01:33:14 +00003038 // There are four options here. If we have 'friend enum foo;' then this is a
3039 // friend declaration, and cannot have an accompanying definition. If we have
3040 // 'enum foo;', then this is a forward declaration. If we have
3041 // 'enum foo {...' then this is a definition. Otherwise we have something
3042 // like 'enum foo xyz', a reference.
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00003043 //
3044 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
3045 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
3046 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
3047 //
John McCallf312b1e2010-08-26 23:41:50 +00003048 Sema::TagUseKind TUK;
Richard Smithbdad7a22012-01-10 01:33:14 +00003049 if (DS.isFriendSpecified())
3050 TUK = Sema::TUK_Friend;
3051 else if (Tok.is(tok::l_brace))
John McCallf312b1e2010-08-26 23:41:50 +00003052 TUK = Sema::TUK_Definition;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00003053 else if (Tok.is(tok::semi))
John McCallf312b1e2010-08-26 23:41:50 +00003054 TUK = Sema::TUK_Declaration;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00003055 else
John McCallf312b1e2010-08-26 23:41:50 +00003056 TUK = Sema::TUK_Reference;
Douglas Gregor8fc6d232010-05-03 17:48:54 +00003057
3058 // enums cannot be templates, although they can be referenced from a
3059 // template.
3060 if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate &&
John McCallf312b1e2010-08-26 23:41:50 +00003061 TUK != Sema::TUK_Reference) {
Douglas Gregor8fc6d232010-05-03 17:48:54 +00003062 Diag(Tok, diag::err_enum_template);
3063
3064 // Skip the rest of this declarator, up until the comma or semicolon.
3065 SkipUntil(tok::comma, true);
3066 return;
3067 }
3068
Douglas Gregorb9075602011-02-22 02:55:24 +00003069 if (!Name && TUK != Sema::TUK_Definition) {
3070 Diag(Tok, diag::err_enumerator_unnamed_no_def);
3071
3072 // Skip the rest of this declarator, up until the comma or semicolon.
3073 SkipUntil(tok::comma, true);
3074 return;
3075 }
3076
Douglas Gregor402abb52009-05-28 23:31:59 +00003077 bool Owned = false;
John McCallc4e70192009-09-11 04:59:25 +00003078 bool IsDependent = false;
Douglas Gregor48c89f42010-04-24 16:38:41 +00003079 const char *PrevSpec = 0;
3080 unsigned DiagID;
John McCalld226f652010-08-21 09:40:31 +00003081 Decl *TagDecl = Actions.ActOnTag(getCurScope(), DeclSpec::TST_enum, TUK,
John McCall7f040a92010-12-24 02:08:15 +00003082 StartLoc, SS, Name, NameLoc, attrs.getList(),
Douglas Gregore7612302011-09-09 19:05:14 +00003083 AS, DS.getModulePrivateSpecLoc(),
John McCallf312b1e2010-08-26 23:41:50 +00003084 MultiTemplateParamsArg(Actions),
Richard Smithbdad7a22012-01-10 01:33:14 +00003085 Owned, IsDependent, ScopedEnumKWLoc,
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00003086 IsScopedUsingClassTag, BaseType);
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003087
Douglas Gregor48c89f42010-04-24 16:38:41 +00003088 if (IsDependent) {
3089 // This enum has a dependent nested-name-specifier. Handle it as a
3090 // dependent tag.
3091 if (!Name) {
3092 DS.SetTypeSpecError();
3093 Diag(Tok, diag::err_expected_type_name_after_typename);
3094 return;
3095 }
3096
Douglas Gregor23c94db2010-07-02 17:43:08 +00003097 TypeResult Type = Actions.ActOnDependentTag(getCurScope(), DeclSpec::TST_enum,
Douglas Gregor48c89f42010-04-24 16:38:41 +00003098 TUK, SS, Name, StartLoc,
3099 NameLoc);
3100 if (Type.isInvalid()) {
3101 DS.SetTypeSpecError();
3102 return;
3103 }
3104
Abramo Bagnara0daaf322011-03-16 20:16:18 +00003105 if (DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc,
3106 NameLoc.isValid() ? NameLoc : StartLoc,
3107 PrevSpec, DiagID, Type.get()))
Douglas Gregor48c89f42010-04-24 16:38:41 +00003108 Diag(StartLoc, DiagID) << PrevSpec;
3109
3110 return;
3111 }
Mike Stump1eb44332009-09-09 15:08:12 +00003112
John McCalld226f652010-08-21 09:40:31 +00003113 if (!TagDecl) {
Douglas Gregor48c89f42010-04-24 16:38:41 +00003114 // The action failed to produce an enumeration tag. If this is a
3115 // definition, consume the entire definition.
3116 if (Tok.is(tok::l_brace)) {
3117 ConsumeBrace();
3118 SkipUntil(tok::r_brace);
3119 }
3120
3121 DS.SetTypeSpecError();
3122 return;
3123 }
Richard Smithbdad7a22012-01-10 01:33:14 +00003124
3125 if (Tok.is(tok::l_brace)) {
3126 if (TUK == Sema::TUK_Friend)
3127 Diag(Tok, diag::err_friend_decl_defines_type)
3128 << SourceRange(DS.getFriendSpecLoc());
Reid Spencer5f016e22007-07-11 17:01:13 +00003129 ParseEnumBody(StartLoc, TagDecl);
Richard Smithbdad7a22012-01-10 01:33:14 +00003130 }
Mike Stump1eb44332009-09-09 15:08:12 +00003131
Abramo Bagnara0daaf322011-03-16 20:16:18 +00003132 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc,
3133 NameLoc.isValid() ? NameLoc : StartLoc,
3134 PrevSpec, DiagID, TagDecl, Owned))
John McCallfec54012009-08-03 20:12:06 +00003135 Diag(StartLoc, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00003136}
3137
3138/// ParseEnumBody - Parse a {} enclosed enumerator-list.
3139/// enumerator-list:
3140/// enumerator
3141/// enumerator-list ',' enumerator
3142/// enumerator:
3143/// enumeration-constant
3144/// enumeration-constant '=' constant-expression
3145/// enumeration-constant:
3146/// identifier
3147///
John McCalld226f652010-08-21 09:40:31 +00003148void Parser::ParseEnumBody(SourceLocation StartLoc, Decl *EnumDecl) {
Douglas Gregor074149e2009-01-05 19:45:36 +00003149 // Enter the scope of the enum body and start the definition.
3150 ParseScope EnumScope(this, Scope::DeclScope);
Douglas Gregor23c94db2010-07-02 17:43:08 +00003151 Actions.ActOnTagStartDefinition(getCurScope(), EnumDecl);
Douglas Gregor074149e2009-01-05 19:45:36 +00003152
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003153 BalancedDelimiterTracker T(*this, tok::l_brace);
3154 T.consumeOpen();
Mike Stump1eb44332009-09-09 15:08:12 +00003155
Chris Lattner7946dd32007-08-27 17:24:30 +00003156 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
Chris Lattner04d66662007-10-09 17:33:22 +00003157 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Fariborz Jahanian05115522010-05-28 22:23:22 +00003158 Diag(Tok, diag::error_empty_enum);
Mike Stump1eb44332009-09-09 15:08:12 +00003159
Chris Lattner5f9e2722011-07-23 10:55:15 +00003160 SmallVector<Decl *, 32> EnumConstantDecls;
Reid Spencer5f016e22007-07-11 17:01:13 +00003161
John McCalld226f652010-08-21 09:40:31 +00003162 Decl *LastEnumConstDecl = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00003163
Reid Spencer5f016e22007-07-11 17:01:13 +00003164 // Parse the enumerator-list.
Chris Lattner04d66662007-10-09 17:33:22 +00003165 while (Tok.is(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003166 IdentifierInfo *Ident = Tok.getIdentifierInfo();
3167 SourceLocation IdentLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00003168
John McCall5b629aa2010-10-22 23:36:17 +00003169 // If attributes exist after the enumerator, parse them.
John McCall0b7e6782011-03-24 11:26:52 +00003170 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00003171 MaybeParseGNUAttributes(attrs);
John McCall5b629aa2010-10-22 23:36:17 +00003172
Reid Spencer5f016e22007-07-11 17:01:13 +00003173 SourceLocation EqualLoc;
John McCall60d7b3a2010-08-24 06:29:42 +00003174 ExprResult AssignedVal;
Fariborz Jahanian5a477db2011-12-09 01:15:54 +00003175 ParsingDeclRAIIObject PD(*this);
3176
Chris Lattner04d66662007-10-09 17:33:22 +00003177 if (Tok.is(tok::equal)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003178 EqualLoc = ConsumeToken();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00003179 AssignedVal = ParseConstantExpression();
3180 if (AssignedVal.isInvalid())
Reid Spencer5f016e22007-07-11 17:01:13 +00003181 SkipUntil(tok::comma, tok::r_brace, true, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00003182 }
Mike Stump1eb44332009-09-09 15:08:12 +00003183
Reid Spencer5f016e22007-07-11 17:01:13 +00003184 // Install the enumerator constant into EnumDecl.
John McCalld226f652010-08-21 09:40:31 +00003185 Decl *EnumConstDecl = Actions.ActOnEnumConstant(getCurScope(), EnumDecl,
3186 LastEnumConstDecl,
3187 IdentLoc, Ident,
John McCall7f040a92010-12-24 02:08:15 +00003188 attrs.getList(), EqualLoc,
John McCalld226f652010-08-21 09:40:31 +00003189 AssignedVal.release());
Fariborz Jahanian5a477db2011-12-09 01:15:54 +00003190 PD.complete(EnumConstDecl);
3191
Reid Spencer5f016e22007-07-11 17:01:13 +00003192 EnumConstantDecls.push_back(EnumConstDecl);
3193 LastEnumConstDecl = EnumConstDecl;
Mike Stump1eb44332009-09-09 15:08:12 +00003194
Douglas Gregor751f6922010-09-07 14:51:08 +00003195 if (Tok.is(tok::identifier)) {
3196 // We're missing a comma between enumerators.
3197 SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation);
3198 Diag(Loc, diag::err_enumerator_list_missing_comma)
3199 << FixItHint::CreateInsertion(Loc, ", ");
3200 continue;
3201 }
3202
Chris Lattner04d66662007-10-09 17:33:22 +00003203 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +00003204 break;
3205 SourceLocation CommaLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00003206
Richard Smith7fe62082011-10-15 05:09:34 +00003207 if (Tok.isNot(tok::identifier)) {
3208 if (!getLang().C99 && !getLang().CPlusPlus0x)
3209 Diag(CommaLoc, diag::ext_enumerator_list_comma)
3210 << getLang().CPlusPlus
3211 << FixItHint::CreateRemoval(CommaLoc);
3212 else if (getLang().CPlusPlus0x)
3213 Diag(CommaLoc, diag::warn_cxx98_compat_enumerator_list_comma)
3214 << FixItHint::CreateRemoval(CommaLoc);
3215 }
Reid Spencer5f016e22007-07-11 17:01:13 +00003216 }
Mike Stump1eb44332009-09-09 15:08:12 +00003217
Reid Spencer5f016e22007-07-11 17:01:13 +00003218 // Eat the }.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003219 T.consumeClose();
Reid Spencer5f016e22007-07-11 17:01:13 +00003220
Reid Spencer5f016e22007-07-11 17:01:13 +00003221 // If attributes exist after the identifier list, parse them.
John McCall0b7e6782011-03-24 11:26:52 +00003222 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00003223 MaybeParseGNUAttributes(attrs);
Douglas Gregor72de6672009-01-08 20:45:30 +00003224
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003225 Actions.ActOnEnumBody(StartLoc, T.getOpenLocation(), T.getCloseLocation(),
3226 EnumDecl, EnumConstantDecls.data(),
3227 EnumConstantDecls.size(), getCurScope(),
3228 attrs.getList());
Mike Stump1eb44332009-09-09 15:08:12 +00003229
Douglas Gregor72de6672009-01-08 20:45:30 +00003230 EnumScope.Exit();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003231 Actions.ActOnTagFinishDefinition(getCurScope(), EnumDecl,
3232 T.getCloseLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00003233}
3234
3235/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff5f8aa692008-02-11 23:15:56 +00003236/// start of a type-qualifier-list.
3237bool Parser::isTypeQualifier() const {
3238 switch (Tok.getKind()) {
3239 default: return false;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003240
3241 // type-qualifier only in OpenCL
3242 case tok::kw_private:
3243 return getLang().OpenCL;
3244
Steve Naroff5f8aa692008-02-11 23:15:56 +00003245 // type-qualifier
3246 case tok::kw_const:
3247 case tok::kw_volatile:
3248 case tok::kw_restrict:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003249 case tok::kw___private:
3250 case tok::kw___local:
3251 case tok::kw___global:
3252 case tok::kw___constant:
3253 case tok::kw___read_only:
3254 case tok::kw___read_write:
3255 case tok::kw___write_only:
Steve Naroff5f8aa692008-02-11 23:15:56 +00003256 return true;
3257 }
3258}
3259
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003260/// isKnownToBeTypeSpecifier - Return true if we know that the specified token
3261/// is definitely a type-specifier. Return false if it isn't part of a type
3262/// specifier or if we're not sure.
3263bool Parser::isKnownToBeTypeSpecifier(const Token &Tok) const {
3264 switch (Tok.getKind()) {
3265 default: return false;
3266 // type-specifiers
3267 case tok::kw_short:
3268 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00003269 case tok::kw___int64:
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003270 case tok::kw_signed:
3271 case tok::kw_unsigned:
3272 case tok::kw__Complex:
3273 case tok::kw__Imaginary:
3274 case tok::kw_void:
3275 case tok::kw_char:
3276 case tok::kw_wchar_t:
3277 case tok::kw_char16_t:
3278 case tok::kw_char32_t:
3279 case tok::kw_int:
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00003280 case tok::kw_half:
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003281 case tok::kw_float:
3282 case tok::kw_double:
3283 case tok::kw_bool:
3284 case tok::kw__Bool:
3285 case tok::kw__Decimal32:
3286 case tok::kw__Decimal64:
3287 case tok::kw__Decimal128:
3288 case tok::kw___vector:
3289
3290 // struct-or-union-specifier (C99) or class-specifier (C++)
3291 case tok::kw_class:
3292 case tok::kw_struct:
3293 case tok::kw_union:
3294 // enum-specifier
3295 case tok::kw_enum:
3296
3297 // typedef-name
3298 case tok::annot_typename:
3299 return true;
3300 }
3301}
3302
Steve Naroff5f8aa692008-02-11 23:15:56 +00003303/// isTypeSpecifierQualifier - Return true if the current token could be the
Reid Spencer5f016e22007-07-11 17:01:13 +00003304/// start of a specifier-qualifier-list.
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003305bool Parser::isTypeSpecifierQualifier() {
Reid Spencer5f016e22007-07-11 17:01:13 +00003306 switch (Tok.getKind()) {
3307 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00003308
Chris Lattner166a8fc2009-01-04 23:41:41 +00003309 case tok::identifier: // foo::bar
John Thompson82287d12010-02-05 00:12:22 +00003310 if (TryAltiVecVectorToken())
3311 return true;
3312 // Fall through.
Douglas Gregord57959a2009-03-27 23:10:48 +00003313 case tok::kw_typename: // typename T::type
Chris Lattner166a8fc2009-01-04 23:41:41 +00003314 // Annotate typenames and C++ scope specifiers. If we get one, just
3315 // recurse to handle whatever we get.
3316 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003317 return true;
3318 if (Tok.is(tok::identifier))
3319 return false;
3320 return isTypeSpecifierQualifier();
Douglas Gregord57959a2009-03-27 23:10:48 +00003321
Chris Lattner166a8fc2009-01-04 23:41:41 +00003322 case tok::coloncolon: // ::foo::bar
3323 if (NextToken().is(tok::kw_new) || // ::new
3324 NextToken().is(tok::kw_delete)) // ::delete
3325 return false;
3326
Chris Lattner166a8fc2009-01-04 23:41:41 +00003327 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003328 return true;
3329 return isTypeSpecifierQualifier();
Mike Stump1eb44332009-09-09 15:08:12 +00003330
Reid Spencer5f016e22007-07-11 17:01:13 +00003331 // GNU attributes support.
3332 case tok::kw___attribute:
Steve Naroffd1861fd2007-07-31 12:34:36 +00003333 // GNU typeof support.
3334 case tok::kw_typeof:
Mike Stump1eb44332009-09-09 15:08:12 +00003335
Reid Spencer5f016e22007-07-11 17:01:13 +00003336 // type-specifiers
3337 case tok::kw_short:
3338 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00003339 case tok::kw___int64:
Reid Spencer5f016e22007-07-11 17:01:13 +00003340 case tok::kw_signed:
3341 case tok::kw_unsigned:
3342 case tok::kw__Complex:
3343 case tok::kw__Imaginary:
3344 case tok::kw_void:
3345 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00003346 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00003347 case tok::kw_char16_t:
3348 case tok::kw_char32_t:
Reid Spencer5f016e22007-07-11 17:01:13 +00003349 case tok::kw_int:
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00003350 case tok::kw_half:
Reid Spencer5f016e22007-07-11 17:01:13 +00003351 case tok::kw_float:
3352 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00003353 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00003354 case tok::kw__Bool:
3355 case tok::kw__Decimal32:
3356 case tok::kw__Decimal64:
3357 case tok::kw__Decimal128:
John Thompson82287d12010-02-05 00:12:22 +00003358 case tok::kw___vector:
Mike Stump1eb44332009-09-09 15:08:12 +00003359
Chris Lattner99dc9142008-04-13 18:59:07 +00003360 // struct-or-union-specifier (C99) or class-specifier (C++)
3361 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00003362 case tok::kw_struct:
3363 case tok::kw_union:
3364 // enum-specifier
3365 case tok::kw_enum:
Mike Stump1eb44332009-09-09 15:08:12 +00003366
Reid Spencer5f016e22007-07-11 17:01:13 +00003367 // type-qualifier
3368 case tok::kw_const:
3369 case tok::kw_volatile:
3370 case tok::kw_restrict:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003371
3372 // typedef-name
Chris Lattnerb31757b2009-01-06 05:06:21 +00003373 case tok::annot_typename:
Reid Spencer5f016e22007-07-11 17:01:13 +00003374 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00003375
Chris Lattner7c186be2008-10-20 00:25:30 +00003376 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
3377 case tok::less:
3378 return getLang().ObjC1;
Mike Stump1eb44332009-09-09 15:08:12 +00003379
Steve Naroff239f0732008-12-25 14:16:32 +00003380 case tok::kw___cdecl:
3381 case tok::kw___stdcall:
3382 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00003383 case tok::kw___thiscall:
Eli Friedman290eeb02009-06-08 23:27:34 +00003384 case tok::kw___w64:
3385 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00003386 case tok::kw___ptr32:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003387 case tok::kw___pascal:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00003388 case tok::kw___unaligned:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003389
3390 case tok::kw___private:
3391 case tok::kw___local:
3392 case tok::kw___global:
3393 case tok::kw___constant:
3394 case tok::kw___read_only:
3395 case tok::kw___read_write:
3396 case tok::kw___write_only:
3397
Eli Friedman290eeb02009-06-08 23:27:34 +00003398 return true;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003399
3400 case tok::kw_private:
3401 return getLang().OpenCL;
Eli Friedmanb001de72011-10-06 23:00:33 +00003402
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00003403 // C11 _Atomic()
Eli Friedmanb001de72011-10-06 23:00:33 +00003404 case tok::kw__Atomic:
3405 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00003406 }
3407}
3408
3409/// isDeclarationSpecifier() - Return true if the current token is part of a
3410/// declaration specifier.
Douglas Gregor9497a732010-09-16 01:51:54 +00003411///
3412/// \param DisambiguatingWithExpression True to indicate that the purpose of
3413/// this check is to disambiguate between an expression and a declaration.
3414bool Parser::isDeclarationSpecifier(bool DisambiguatingWithExpression) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003415 switch (Tok.getKind()) {
3416 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00003417
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003418 case tok::kw_private:
3419 return getLang().OpenCL;
3420
Chris Lattner166a8fc2009-01-04 23:41:41 +00003421 case tok::identifier: // foo::bar
Steve Naroff61f72cb2009-03-09 21:12:44 +00003422 // Unfortunate hack to support "Class.factoryMethod" notation.
3423 if (getLang().ObjC1 && NextToken().is(tok::period))
3424 return false;
John Thompson82287d12010-02-05 00:12:22 +00003425 if (TryAltiVecVectorToken())
3426 return true;
3427 // Fall through.
David Blaikie42d6d0c2011-12-04 05:04:18 +00003428 case tok::kw_decltype: // decltype(T())::type
Douglas Gregord57959a2009-03-27 23:10:48 +00003429 case tok::kw_typename: // typename T::type
Chris Lattner166a8fc2009-01-04 23:41:41 +00003430 // Annotate typenames and C++ scope specifiers. If we get one, just
3431 // recurse to handle whatever we get.
3432 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003433 return true;
3434 if (Tok.is(tok::identifier))
3435 return false;
Douglas Gregor9497a732010-09-16 01:51:54 +00003436
3437 // If we're in Objective-C and we have an Objective-C class type followed
3438 // by an identifier and then either ':' or ']', in a place where an
3439 // expression is permitted, then this is probably a class message send
3440 // missing the initial '['. In this case, we won't consider this to be
3441 // the start of a declaration.
3442 if (DisambiguatingWithExpression &&
3443 isStartOfObjCClassMessageMissingOpenBracket())
3444 return false;
3445
John McCall9ba61662010-02-26 08:45:28 +00003446 return isDeclarationSpecifier();
3447
Chris Lattner166a8fc2009-01-04 23:41:41 +00003448 case tok::coloncolon: // ::foo::bar
3449 if (NextToken().is(tok::kw_new) || // ::new
3450 NextToken().is(tok::kw_delete)) // ::delete
3451 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00003452
Chris Lattner166a8fc2009-01-04 23:41:41 +00003453 // Annotate typenames and C++ scope specifiers. If we get one, just
3454 // recurse to handle whatever we get.
3455 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003456 return true;
3457 return isDeclarationSpecifier();
Mike Stump1eb44332009-09-09 15:08:12 +00003458
Reid Spencer5f016e22007-07-11 17:01:13 +00003459 // storage-class-specifier
3460 case tok::kw_typedef:
3461 case tok::kw_extern:
Steve Naroff8d54bf22007-12-18 00:16:02 +00003462 case tok::kw___private_extern__:
Reid Spencer5f016e22007-07-11 17:01:13 +00003463 case tok::kw_static:
3464 case tok::kw_auto:
3465 case tok::kw_register:
3466 case tok::kw___thread:
Mike Stump1eb44332009-09-09 15:08:12 +00003467
Douglas Gregor8d267c52011-09-09 02:06:17 +00003468 // Modules
3469 case tok::kw___module_private__:
3470
Reid Spencer5f016e22007-07-11 17:01:13 +00003471 // type-specifiers
3472 case tok::kw_short:
3473 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00003474 case tok::kw___int64:
Reid Spencer5f016e22007-07-11 17:01:13 +00003475 case tok::kw_signed:
3476 case tok::kw_unsigned:
3477 case tok::kw__Complex:
3478 case tok::kw__Imaginary:
3479 case tok::kw_void:
3480 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00003481 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00003482 case tok::kw_char16_t:
3483 case tok::kw_char32_t:
3484
Reid Spencer5f016e22007-07-11 17:01:13 +00003485 case tok::kw_int:
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00003486 case tok::kw_half:
Reid Spencer5f016e22007-07-11 17:01:13 +00003487 case tok::kw_float:
3488 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00003489 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00003490 case tok::kw__Bool:
3491 case tok::kw__Decimal32:
3492 case tok::kw__Decimal64:
3493 case tok::kw__Decimal128:
John Thompson82287d12010-02-05 00:12:22 +00003494 case tok::kw___vector:
Mike Stump1eb44332009-09-09 15:08:12 +00003495
Chris Lattner99dc9142008-04-13 18:59:07 +00003496 // struct-or-union-specifier (C99) or class-specifier (C++)
3497 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00003498 case tok::kw_struct:
3499 case tok::kw_union:
3500 // enum-specifier
3501 case tok::kw_enum:
Mike Stump1eb44332009-09-09 15:08:12 +00003502
Reid Spencer5f016e22007-07-11 17:01:13 +00003503 // type-qualifier
3504 case tok::kw_const:
3505 case tok::kw_volatile:
3506 case tok::kw_restrict:
Steve Naroffd1861fd2007-07-31 12:34:36 +00003507
Reid Spencer5f016e22007-07-11 17:01:13 +00003508 // function-specifier
3509 case tok::kw_inline:
Douglas Gregorb48fe382008-10-31 09:07:45 +00003510 case tok::kw_virtual:
3511 case tok::kw_explicit:
Chris Lattnerd6c7c182007-08-09 16:40:21 +00003512
Peter Collingbournec6eb44b2011-04-15 00:35:57 +00003513 // static_assert-declaration
3514 case tok::kw__Static_assert:
3515
Chris Lattner1ef08762007-08-09 17:01:07 +00003516 // GNU typeof support.
3517 case tok::kw_typeof:
Mike Stump1eb44332009-09-09 15:08:12 +00003518
Chris Lattner1ef08762007-08-09 17:01:07 +00003519 // GNU attributes.
Chris Lattnerd6c7c182007-08-09 16:40:21 +00003520 case tok::kw___attribute:
Reid Spencer5f016e22007-07-11 17:01:13 +00003521 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00003522
Francois Pichete3d49b42011-06-19 08:02:06 +00003523 // C++0x decltype.
David Blaikie42d6d0c2011-12-04 05:04:18 +00003524 case tok::annot_decltype:
Francois Pichete3d49b42011-06-19 08:02:06 +00003525 return true;
3526
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00003527 // C11 _Atomic()
Eli Friedmanb001de72011-10-06 23:00:33 +00003528 case tok::kw__Atomic:
3529 return true;
3530
Chris Lattnerf3948c42008-07-26 03:38:44 +00003531 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
3532 case tok::less:
3533 return getLang().ObjC1;
Mike Stump1eb44332009-09-09 15:08:12 +00003534
Douglas Gregord9d75e52011-04-27 05:41:15 +00003535 // typedef-name
3536 case tok::annot_typename:
3537 return !DisambiguatingWithExpression ||
3538 !isStartOfObjCClassMessageMissingOpenBracket();
3539
Steve Naroff47f52092009-01-06 19:34:12 +00003540 case tok::kw___declspec:
Steve Naroff239f0732008-12-25 14:16:32 +00003541 case tok::kw___cdecl:
3542 case tok::kw___stdcall:
3543 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00003544 case tok::kw___thiscall:
Eli Friedman290eeb02009-06-08 23:27:34 +00003545 case tok::kw___w64:
3546 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00003547 case tok::kw___ptr32:
Eli Friedman290eeb02009-06-08 23:27:34 +00003548 case tok::kw___forceinline:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003549 case tok::kw___pascal:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00003550 case tok::kw___unaligned:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003551
3552 case tok::kw___private:
3553 case tok::kw___local:
3554 case tok::kw___global:
3555 case tok::kw___constant:
3556 case tok::kw___read_only:
3557 case tok::kw___read_write:
3558 case tok::kw___write_only:
3559
Eli Friedman290eeb02009-06-08 23:27:34 +00003560 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00003561 }
3562}
3563
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003564bool Parser::isConstructorDeclarator() {
3565 TentativeParsingAction TPA(*this);
3566
3567 // Parse the C++ scope specifier.
3568 CXXScopeSpec SS;
Douglas Gregorefaa93a2011-11-07 17:33:42 +00003569 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
3570 /*EnteringContext=*/true)) {
John McCall9ba61662010-02-26 08:45:28 +00003571 TPA.Revert();
3572 return false;
3573 }
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003574
3575 // Parse the constructor name.
3576 if (Tok.is(tok::identifier) || Tok.is(tok::annot_template_id)) {
3577 // We already know that we have a constructor name; just consume
3578 // the token.
3579 ConsumeToken();
3580 } else {
3581 TPA.Revert();
3582 return false;
3583 }
3584
3585 // Current class name must be followed by a left parentheses.
3586 if (Tok.isNot(tok::l_paren)) {
3587 TPA.Revert();
3588 return false;
3589 }
3590 ConsumeParen();
3591
3592 // A right parentheses or ellipsis signals that we have a constructor.
3593 if (Tok.is(tok::r_paren) || Tok.is(tok::ellipsis)) {
3594 TPA.Revert();
3595 return true;
3596 }
3597
3598 // If we need to, enter the specified scope.
3599 DeclaratorScopeObj DeclScopeObj(*this, SS);
Douglas Gregor23c94db2010-07-02 17:43:08 +00003600 if (SS.isSet() && Actions.ShouldEnterDeclaratorScope(getCurScope(), SS))
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003601 DeclScopeObj.EnterDeclaratorScope();
3602
Francois Pichetdfaa5fb2011-01-31 04:54:32 +00003603 // Optionally skip Microsoft attributes.
John McCall0b7e6782011-03-24 11:26:52 +00003604 ParsedAttributes Attrs(AttrFactory);
Francois Pichetdfaa5fb2011-01-31 04:54:32 +00003605 MaybeParseMicrosoftAttributes(Attrs);
3606
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003607 // Check whether the next token(s) are part of a declaration
3608 // specifier, in which case we have the start of a parameter and,
3609 // therefore, we know that this is a constructor.
3610 bool IsConstructor = isDeclarationSpecifier();
3611 TPA.Revert();
3612 return IsConstructor;
3613}
Reid Spencer5f016e22007-07-11 17:01:13 +00003614
3615/// ParseTypeQualifierListOpt
Dawn Perchik52fc3142010-09-03 01:29:35 +00003616/// type-qualifier-list: [C99 6.7.5]
3617/// type-qualifier
3618/// [vendor] attributes
3619/// [ only if VendorAttributesAllowed=true ]
3620/// type-qualifier-list type-qualifier
3621/// [vendor] type-qualifier-list attributes
3622/// [ only if VendorAttributesAllowed=true ]
3623/// [C++0x] attribute-specifier[opt] is allowed before cv-qualifier-seq
3624/// [ only if CXX0XAttributesAllowed=true ]
3625/// Note: vendor can be GNU, MS, etc.
Reid Spencer5f016e22007-07-11 17:01:13 +00003626///
Dawn Perchik52fc3142010-09-03 01:29:35 +00003627void Parser::ParseTypeQualifierListOpt(DeclSpec &DS,
3628 bool VendorAttributesAllowed,
Sean Huntbbd37c62009-11-21 08:43:09 +00003629 bool CXX0XAttributesAllowed) {
3630 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier()) {
3631 SourceLocation Loc = Tok.getLocation();
John McCall0b7e6782011-03-24 11:26:52 +00003632 ParsedAttributesWithRange attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00003633 ParseCXX0XAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00003634 if (CXX0XAttributesAllowed)
John McCall7f040a92010-12-24 02:08:15 +00003635 DS.takeAttributesFrom(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00003636 else
3637 Diag(Loc, diag::err_attributes_not_allowed);
3638 }
Abramo Bagnara796aa442011-03-12 11:17:06 +00003639
3640 SourceLocation EndLoc;
3641
Reid Spencer5f016e22007-07-11 17:01:13 +00003642 while (1) {
John McCallfec54012009-08-03 20:12:06 +00003643 bool isInvalid = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00003644 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00003645 unsigned DiagID = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00003646 SourceLocation Loc = Tok.getLocation();
3647
3648 switch (Tok.getKind()) {
Douglas Gregor1a480c42010-08-27 17:35:51 +00003649 case tok::code_completion:
3650 Actions.CodeCompleteTypeQualifiers(DS);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00003651 return cutOffParsing();
Douglas Gregor1a480c42010-08-27 17:35:51 +00003652
Reid Spencer5f016e22007-07-11 17:01:13 +00003653 case tok::kw_const:
John McCallfec54012009-08-03 20:12:06 +00003654 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, DiagID,
3655 getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00003656 break;
3657 case tok::kw_volatile:
John McCallfec54012009-08-03 20:12:06 +00003658 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
3659 getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00003660 break;
3661 case tok::kw_restrict:
John McCallfec54012009-08-03 20:12:06 +00003662 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
3663 getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00003664 break;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003665
3666 // OpenCL qualifiers:
3667 case tok::kw_private:
3668 if (!getLang().OpenCL)
3669 goto DoneWithTypeQuals;
3670 case tok::kw___private:
3671 case tok::kw___global:
3672 case tok::kw___local:
3673 case tok::kw___constant:
3674 case tok::kw___read_only:
3675 case tok::kw___write_only:
3676 case tok::kw___read_write:
3677 ParseOpenCLQualifiers(DS);
3678 break;
3679
Eli Friedman290eeb02009-06-08 23:27:34 +00003680 case tok::kw___w64:
Steve Naroff86bc6cf2008-12-25 14:41:26 +00003681 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00003682 case tok::kw___ptr32:
Steve Naroff239f0732008-12-25 14:16:32 +00003683 case tok::kw___cdecl:
3684 case tok::kw___stdcall:
3685 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00003686 case tok::kw___thiscall:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00003687 case tok::kw___unaligned:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003688 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00003689 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman290eeb02009-06-08 23:27:34 +00003690 continue;
3691 }
3692 goto DoneWithTypeQuals;
Dawn Perchik52fc3142010-09-03 01:29:35 +00003693 case tok::kw___pascal:
3694 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00003695 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik52fc3142010-09-03 01:29:35 +00003696 continue;
3697 }
3698 goto DoneWithTypeQuals;
Reid Spencer5f016e22007-07-11 17:01:13 +00003699 case tok::kw___attribute:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003700 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00003701 ParseGNUAttributes(DS.getAttributes());
Chris Lattner5a69d1c2008-12-18 07:02:59 +00003702 continue; // do *not* consume the next token!
3703 }
3704 // otherwise, FALL THROUGH!
3705 default:
Steve Naroff239f0732008-12-25 14:16:32 +00003706 DoneWithTypeQuals:
Chris Lattner5a69d1c2008-12-18 07:02:59 +00003707 // If this is not a type-qualifier token, we're done reading type
3708 // qualifiers. First verify that DeclSpec's are consistent.
Douglas Gregor9b3064b2009-04-01 22:41:11 +00003709 DS.Finish(Diags, PP);
Abramo Bagnara796aa442011-03-12 11:17:06 +00003710 if (EndLoc.isValid())
3711 DS.SetRangeEnd(EndLoc);
Chris Lattner5a69d1c2008-12-18 07:02:59 +00003712 return;
Reid Spencer5f016e22007-07-11 17:01:13 +00003713 }
Chris Lattnera1fcbad2008-12-18 06:50:14 +00003714
Reid Spencer5f016e22007-07-11 17:01:13 +00003715 // If the specifier combination wasn't legal, issue a diagnostic.
3716 if (isInvalid) {
3717 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner1ab3b962008-11-18 07:48:38 +00003718 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00003719 }
Abramo Bagnara796aa442011-03-12 11:17:06 +00003720 EndLoc = ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00003721 }
3722}
3723
3724
3725/// ParseDeclarator - Parse and verify a newly-initialized declarator.
3726///
3727void Parser::ParseDeclarator(Declarator &D) {
3728 /// This implements the 'declarator' production in the C grammar, then checks
3729 /// for well-formedness and issues diagnostics.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003730 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Reid Spencer5f016e22007-07-11 17:01:13 +00003731}
3732
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003733/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
3734/// is parsed by the function passed to it. Pass null, and the direct-declarator
3735/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003736/// ptr-operator production.
3737///
Richard Smith0706df42011-10-19 21:33:05 +00003738/// If the grammar of this construct is extended, matching changes must also be
3739/// made to TryParseDeclarator and MightBeDeclarator.
3740///
Sebastian Redlf30208a2009-01-24 21:16:55 +00003741/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
3742/// [C] pointer[opt] direct-declarator
3743/// [C++] direct-declarator
3744/// [C++] ptr-operator declarator
Reid Spencer5f016e22007-07-11 17:01:13 +00003745///
3746/// pointer: [C99 6.7.5]
3747/// '*' type-qualifier-list[opt]
3748/// '*' type-qualifier-list[opt] pointer
3749///
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003750/// ptr-operator:
3751/// '*' cv-qualifier-seq[opt]
3752/// '&'
Sebastian Redl05532f22009-03-15 22:02:01 +00003753/// [C++0x] '&&'
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003754/// [GNU] '&' restrict[opt] attributes[opt]
Sebastian Redl05532f22009-03-15 22:02:01 +00003755/// [GNU?] '&&' restrict[opt] attributes[opt]
Sebastian Redlf30208a2009-01-24 21:16:55 +00003756/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003757void Parser::ParseDeclaratorInternal(Declarator &D,
3758 DirectDeclParseFunction DirectDeclParser) {
Douglas Gregor91a28862009-08-26 14:27:30 +00003759 if (Diags.hasAllExtensionsSilenced())
3760 D.setExtension();
Douglas Gregor2ccccb32010-08-23 18:23:48 +00003761
Sebastian Redlf30208a2009-01-24 21:16:55 +00003762 // C++ member pointers start with a '::' or a nested-name.
3763 // Member pointers get special handling, since there's no place for the
3764 // scope spec in the generic path below.
Chris Lattnerf919bfe2009-03-24 17:04:48 +00003765 if (getLang().CPlusPlus &&
3766 (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) ||
3767 Tok.is(tok::annot_cxxscope))) {
Douglas Gregorefaa93a2011-11-07 17:33:42 +00003768 bool EnteringContext = D.getContext() == Declarator::FileContext ||
3769 D.getContext() == Declarator::MemberContext;
Sebastian Redlf30208a2009-01-24 21:16:55 +00003770 CXXScopeSpec SS;
Douglas Gregorefaa93a2011-11-07 17:33:42 +00003771 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), EnteringContext);
John McCall9ba61662010-02-26 08:45:28 +00003772
Jeffrey Yasskinedc28772010-04-07 23:29:58 +00003773 if (SS.isNotEmpty()) {
Mike Stump1eb44332009-09-09 15:08:12 +00003774 if (Tok.isNot(tok::star)) {
Sebastian Redlf30208a2009-01-24 21:16:55 +00003775 // The scope spec really belongs to the direct-declarator.
3776 D.getCXXScopeSpec() = SS;
3777 if (DirectDeclParser)
3778 (this->*DirectDeclParser)(D);
3779 return;
3780 }
3781
3782 SourceLocation Loc = ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00003783 D.SetRangeEnd(Loc);
John McCall0b7e6782011-03-24 11:26:52 +00003784 DeclSpec DS(AttrFactory);
Sebastian Redlf30208a2009-01-24 21:16:55 +00003785 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00003786 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00003787
3788 // Recurse to parse whatever is left.
3789 ParseDeclaratorInternal(D, DirectDeclParser);
3790
3791 // Sema will have to catch (syntactically invalid) pointers into global
3792 // scope. It has to catch pointers into namespace scope anyway.
3793 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
John McCall0b7e6782011-03-24 11:26:52 +00003794 Loc),
3795 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003796 /* Don't replace range end. */SourceLocation());
Sebastian Redlf30208a2009-01-24 21:16:55 +00003797 return;
3798 }
3799 }
3800
3801 tok::TokenKind Kind = Tok.getKind();
Steve Naroff5618bd42008-08-27 16:04:49 +00003802 // Not a pointer, C++ reference, or block.
Chris Lattner9af55002009-03-27 04:18:06 +00003803 if (Kind != tok::star && Kind != tok::caret &&
Chris Lattnerf919bfe2009-03-24 17:04:48 +00003804 (Kind != tok::amp || !getLang().CPlusPlus) &&
Sebastian Redl743de1f2009-03-23 00:00:23 +00003805 // We parse rvalue refs in C++03, because otherwise the errors are scary.
Chris Lattner9af55002009-03-27 04:18:06 +00003806 (Kind != tok::ampamp || !getLang().CPlusPlus)) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003807 if (DirectDeclParser)
3808 (this->*DirectDeclParser)(D);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003809 return;
3810 }
Sebastian Redlf30208a2009-01-24 21:16:55 +00003811
Sebastian Redl05532f22009-03-15 22:02:01 +00003812 // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference,
3813 // '&&' -> rvalue reference
Sebastian Redl743de1f2009-03-23 00:00:23 +00003814 SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&.
Sebastian Redlab197ba2009-02-09 18:23:29 +00003815 D.SetRangeEnd(Loc);
Reid Spencer5f016e22007-07-11 17:01:13 +00003816
Chris Lattner9af55002009-03-27 04:18:06 +00003817 if (Kind == tok::star || Kind == tok::caret) {
Chris Lattner76549142008-02-21 01:32:26 +00003818 // Is a pointer.
John McCall0b7e6782011-03-24 11:26:52 +00003819 DeclSpec DS(AttrFactory);
Sebastian Redlf30208a2009-01-24 21:16:55 +00003820
Reid Spencer5f016e22007-07-11 17:01:13 +00003821 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00003822 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00003823
Reid Spencer5f016e22007-07-11 17:01:13 +00003824 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003825 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroff5618bd42008-08-27 16:04:49 +00003826 if (Kind == tok::star)
3827 // Remember that we parsed a pointer type, and remember the type-quals.
3828 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
Chandler Carruthd067c072011-02-23 18:51:59 +00003829 DS.getConstSpecLoc(),
3830 DS.getVolatileSpecLoc(),
John McCall0b7e6782011-03-24 11:26:52 +00003831 DS.getRestrictSpecLoc()),
3832 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003833 SourceLocation());
Steve Naroff5618bd42008-08-27 16:04:49 +00003834 else
3835 // Remember that we parsed a Block type, and remember the type-quals.
Mike Stump1eb44332009-09-09 15:08:12 +00003836 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
John McCall0b7e6782011-03-24 11:26:52 +00003837 Loc),
3838 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003839 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00003840 } else {
3841 // Is a reference
John McCall0b7e6782011-03-24 11:26:52 +00003842 DeclSpec DS(AttrFactory);
Reid Spencer5f016e22007-07-11 17:01:13 +00003843
Sebastian Redl743de1f2009-03-23 00:00:23 +00003844 // Complain about rvalue references in C++03, but then go on and build
3845 // the declarator.
Richard Smith7fe62082011-10-15 05:09:34 +00003846 if (Kind == tok::ampamp)
3847 Diag(Loc, getLang().CPlusPlus0x ?
3848 diag::warn_cxx98_compat_rvalue_reference :
3849 diag::ext_rvalue_reference);
Sebastian Redl743de1f2009-03-23 00:00:23 +00003850
Reid Spencer5f016e22007-07-11 17:01:13 +00003851 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
3852 // cv-qualifiers are introduced through the use of a typedef or of a
3853 // template type argument, in which case the cv-qualifiers are ignored.
3854 //
3855 // [GNU] Retricted references are allowed.
3856 // [GNU] Attributes on references are allowed.
Sean Huntbbd37c62009-11-21 08:43:09 +00003857 // [C++0x] Attributes on references are not allowed.
3858 ParseTypeQualifierListOpt(DS, true, false);
Sebastian Redlab197ba2009-02-09 18:23:29 +00003859 D.ExtendWithDeclSpec(DS);
Reid Spencer5f016e22007-07-11 17:01:13 +00003860
3861 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
3862 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
3863 Diag(DS.getConstSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00003864 diag::err_invalid_reference_qualifier_application) << "const";
Reid Spencer5f016e22007-07-11 17:01:13 +00003865 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
3866 Diag(DS.getVolatileSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00003867 diag::err_invalid_reference_qualifier_application) << "volatile";
Reid Spencer5f016e22007-07-11 17:01:13 +00003868 }
3869
3870 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003871 ParseDeclaratorInternal(D, DirectDeclParser);
Reid Spencer5f016e22007-07-11 17:01:13 +00003872
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00003873 if (D.getNumTypeObjects() > 0) {
3874 // C++ [dcl.ref]p4: There shall be no references to references.
3875 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
3876 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattnerda83bac2008-11-19 07:37:42 +00003877 if (const IdentifierInfo *II = D.getIdentifier())
3878 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
3879 << II;
3880 else
3881 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
3882 << "type name";
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00003883
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003884 // Once we've complained about the reference-to-reference, we
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00003885 // can go ahead and build the (technically ill-formed)
3886 // declarator: reference collapsing will take care of it.
3887 }
3888 }
3889
Reid Spencer5f016e22007-07-11 17:01:13 +00003890 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner76549142008-02-21 01:32:26 +00003891 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
Sebastian Redl05532f22009-03-15 22:02:01 +00003892 Kind == tok::amp),
John McCall0b7e6782011-03-24 11:26:52 +00003893 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003894 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00003895 }
3896}
3897
3898/// ParseDirectDeclarator
3899/// direct-declarator: [C99 6.7.5]
Douglas Gregor42a552f2008-11-05 20:51:48 +00003900/// [C99] identifier
Reid Spencer5f016e22007-07-11 17:01:13 +00003901/// '(' declarator ')'
3902/// [GNU] '(' attributes declarator ')'
3903/// [C90] direct-declarator '[' constant-expression[opt] ']'
3904/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
3905/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
3906/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
3907/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
3908/// direct-declarator '(' parameter-type-list ')'
3909/// direct-declarator '(' identifier-list[opt] ')'
3910/// [GNU] direct-declarator '(' parameter-forward-declarations
3911/// parameter-type-list[opt] ')'
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003912/// [C++] direct-declarator '(' parameter-declaration-clause ')'
3913/// cv-qualifier-seq[opt] exception-specification[opt]
Douglas Gregorb48fe382008-10-31 09:07:45 +00003914/// [C++] declarator-id
Douglas Gregor42a552f2008-11-05 20:51:48 +00003915///
3916/// declarator-id: [C++ 8]
Douglas Gregora8bc8c92010-12-23 22:44:42 +00003917/// '...'[opt] id-expression
Douglas Gregor42a552f2008-11-05 20:51:48 +00003918/// '::'[opt] nested-name-specifier[opt] type-name
3919///
3920/// id-expression: [C++ 5.1]
3921/// unqualified-id
Douglas Gregordb422df2009-09-25 21:45:23 +00003922/// qualified-id
Douglas Gregor42a552f2008-11-05 20:51:48 +00003923///
3924/// unqualified-id: [C++ 5.1]
Mike Stump1eb44332009-09-09 15:08:12 +00003925/// identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003926/// operator-function-id
Douglas Gregordb422df2009-09-25 21:45:23 +00003927/// conversion-function-id
Mike Stump1eb44332009-09-09 15:08:12 +00003928/// '~' class-name
Douglas Gregor39a8de12009-02-25 19:37:18 +00003929/// template-id
Argyrios Kyrtzidisc7ed9c62008-11-07 22:02:30 +00003930///
Reid Spencer5f016e22007-07-11 17:01:13 +00003931void Parser::ParseDirectDeclarator(Declarator &D) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00003932 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003933
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003934 if (getLang().CPlusPlus && D.mayHaveIdentifier()) {
3935 // ParseDeclaratorInternal might already have parsed the scope.
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00003936 if (D.getCXXScopeSpec().isEmpty()) {
Douglas Gregorefaa93a2011-11-07 17:33:42 +00003937 bool EnteringContext = D.getContext() == Declarator::FileContext ||
3938 D.getContext() == Declarator::MemberContext;
3939 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec(), ParsedType(),
3940 EnteringContext);
John McCall9ba61662010-02-26 08:45:28 +00003941 }
3942
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00003943 if (D.getCXXScopeSpec().isValid()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00003944 if (Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
John McCalle7e278b2009-12-11 20:04:54 +00003945 // Change the declaration context for name lookup, until this function
3946 // is exited (and the declarator has been parsed).
3947 DeclScopeObj.EnterDeclaratorScope();
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00003948 }
3949
Douglas Gregora8bc8c92010-12-23 22:44:42 +00003950 // C++0x [dcl.fct]p14:
3951 // There is a syntactic ambiguity when an ellipsis occurs at the end
3952 // of a parameter-declaration-clause without a preceding comma. In
3953 // this case, the ellipsis is parsed as part of the
3954 // abstract-declarator if the type of the parameter names a template
3955 // parameter pack that has not been expanded; otherwise, it is parsed
3956 // as part of the parameter-declaration-clause.
3957 if (Tok.is(tok::ellipsis) &&
3958 !((D.getContext() == Declarator::PrototypeContext ||
3959 D.getContext() == Declarator::BlockLiteralContext) &&
Douglas Gregora8bc8c92010-12-23 22:44:42 +00003960 NextToken().is(tok::r_paren) &&
3961 !Actions.containsUnexpandedParameterPacks(D)))
3962 D.setEllipsisLoc(ConsumeToken());
3963
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003964 if (Tok.is(tok::identifier) || Tok.is(tok::kw_operator) ||
3965 Tok.is(tok::annot_template_id) || Tok.is(tok::tilde)) {
3966 // We found something that indicates the start of an unqualified-id.
3967 // Parse that unqualified-id.
John McCallba9d8532010-04-13 06:39:49 +00003968 bool AllowConstructorName;
3969 if (D.getDeclSpec().hasTypeSpecifier())
3970 AllowConstructorName = false;
3971 else if (D.getCXXScopeSpec().isSet())
3972 AllowConstructorName =
3973 (D.getContext() == Declarator::FileContext ||
3974 (D.getContext() == Declarator::MemberContext &&
3975 D.getDeclSpec().isFriendSpecified()));
3976 else
3977 AllowConstructorName = (D.getContext() == Declarator::MemberContext);
3978
Abramo Bagnarae4b92762012-01-27 09:46:47 +00003979 SourceLocation TemplateKWLoc;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003980 if (ParseUnqualifiedId(D.getCXXScopeSpec(),
3981 /*EnteringContext=*/true,
3982 /*AllowDestructorName=*/true,
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003983 AllowConstructorName,
John McCallb3d87482010-08-24 05:47:05 +00003984 ParsedType(),
Abramo Bagnarae4b92762012-01-27 09:46:47 +00003985 TemplateKWLoc,
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00003986 D.getName()) ||
3987 // Once we're past the identifier, if the scope was bad, mark the
3988 // whole declarator bad.
3989 D.getCXXScopeSpec().isInvalid()) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00003990 D.SetIdentifier(0, Tok.getLocation());
3991 D.setInvalidType(true);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003992 } else {
3993 // Parsed the unqualified-id; update range information and move along.
3994 if (D.getSourceRange().getBegin().isInvalid())
3995 D.SetRangeBegin(D.getName().getSourceRange().getBegin());
3996 D.SetRangeEnd(D.getName().getSourceRange().getEnd());
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003997 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003998 goto PastIdentifier;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00003999 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004000 } else if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00004001 assert(!getLang().CPlusPlus &&
4002 "There's a C++-specific check for tok::identifier above");
4003 assert(Tok.getIdentifierInfo() && "Not an identifier?");
4004 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
4005 ConsumeToken();
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004006 goto PastIdentifier;
4007 }
4008
4009 if (Tok.is(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00004010 // direct-declarator: '(' declarator ')'
4011 // direct-declarator: '(' attributes declarator ')'
4012 // Example: 'char (*X)' or 'int (*XX)(void)'
4013 ParseParenDeclarator(D);
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004014
4015 // If the declarator was parenthesized, we entered the declarator
4016 // scope when parsing the parenthesized declarator, then exited
4017 // the scope already. Re-enter the scope, if we need to.
4018 if (D.getCXXScopeSpec().isSet()) {
Fariborz Jahanian46877cd2010-08-17 23:50:37 +00004019 // If there was an error parsing parenthesized declarator, declarator
4020 // scope may have been enterred before. Don't do it again.
4021 if (!D.isInvalidType() &&
4022 Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004023 // Change the declaration context for name lookup, until this function
4024 // is exited (and the declarator has been parsed).
Fariborz Jahanian46877cd2010-08-17 23:50:37 +00004025 DeclScopeObj.EnterDeclaratorScope();
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004026 }
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00004027 } else if (D.mayOmitIdentifier()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00004028 // This could be something simple like "int" (in which case the declarator
4029 // portion is empty), if an abstract-declarator is allowed.
4030 D.SetIdentifier(0, Tok.getLocation());
4031 } else {
Douglas Gregore950d4b2009-03-06 23:28:18 +00004032 if (D.getContext() == Declarator::MemberContext)
4033 Diag(Tok, diag::err_expected_member_name_or_semi)
4034 << D.getDeclSpec().getSourceRange();
4035 else if (getLang().CPlusPlus)
Douglas Gregor2d1c2142009-11-03 19:44:04 +00004036 Diag(Tok, diag::err_expected_unqualified_id) << getLang().CPlusPlus;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00004037 else
Chris Lattner1ab3b962008-11-18 07:48:38 +00004038 Diag(Tok, diag::err_expected_ident_lparen);
Reid Spencer5f016e22007-07-11 17:01:13 +00004039 D.SetIdentifier(0, Tok.getLocation());
Chris Lattner1f6f54b2008-11-11 06:13:16 +00004040 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00004041 }
Mike Stump1eb44332009-09-09 15:08:12 +00004042
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00004043 PastIdentifier:
Reid Spencer5f016e22007-07-11 17:01:13 +00004044 assert(D.isPastIdentifier() &&
4045 "Haven't past the location of the identifier yet?");
Mike Stump1eb44332009-09-09 15:08:12 +00004046
Sean Huntbbd37c62009-11-21 08:43:09 +00004047 // Don't parse attributes unless we have an identifier.
John McCall7f040a92010-12-24 02:08:15 +00004048 if (D.getIdentifier())
4049 MaybeParseCXX0XAttributes(D);
Sean Huntbbd37c62009-11-21 08:43:09 +00004050
Reid Spencer5f016e22007-07-11 17:01:13 +00004051 while (1) {
Chris Lattner04d66662007-10-09 17:33:22 +00004052 if (Tok.is(tok::l_paren)) {
David Blaikie42d6d0c2011-12-04 05:04:18 +00004053 // Enter function-declaration scope, limiting any declarators to the
4054 // function prototype scope, including parameter declarators.
4055 ParseScope PrototypeScope(this,
4056 Scope::FunctionPrototypeScope|Scope::DeclScope);
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00004057 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
4058 // In such a case, check if we actually have a function declarator; if it
4059 // is not, the declarator has been fully parsed.
Chris Lattner7399ee02008-10-20 02:05:46 +00004060 if (getLang().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
4061 // When not in file scope, warn for ambiguous function declarators, just
4062 // in case the author intended it as a variable definition.
4063 bool warnIfAmbiguous = D.getContext() != Declarator::FileContext;
4064 if (!isCXXFunctionDeclarator(warnIfAmbiguous))
4065 break;
4066 }
John McCall0b7e6782011-03-24 11:26:52 +00004067 ParsedAttributes attrs(AttrFactory);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004068 BalancedDelimiterTracker T(*this, tok::l_paren);
4069 T.consumeOpen();
4070 ParseFunctionDeclarator(D, attrs, T);
David Blaikie42d6d0c2011-12-04 05:04:18 +00004071 PrototypeScope.Exit();
Chris Lattner04d66662007-10-09 17:33:22 +00004072 } else if (Tok.is(tok::l_square)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00004073 ParseBracketDeclarator(D);
4074 } else {
4075 break;
4076 }
4077 }
David Blaikie42d6d0c2011-12-04 05:04:18 +00004078}
Reid Spencer5f016e22007-07-11 17:01:13 +00004079
Chris Lattneref4715c2008-04-06 05:45:57 +00004080/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
4081/// only called before the identifier, so these are most likely just grouping
Mike Stump1eb44332009-09-09 15:08:12 +00004082/// parens for precedence. If we find that these are actually function
Chris Lattneref4715c2008-04-06 05:45:57 +00004083/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
4084///
4085/// direct-declarator:
4086/// '(' declarator ')'
4087/// [GNU] '(' attributes declarator ')'
Chris Lattner7399ee02008-10-20 02:05:46 +00004088/// direct-declarator '(' parameter-type-list ')'
4089/// direct-declarator '(' identifier-list[opt] ')'
4090/// [GNU] direct-declarator '(' parameter-forward-declarations
4091/// parameter-type-list[opt] ')'
Chris Lattneref4715c2008-04-06 05:45:57 +00004092///
4093void Parser::ParseParenDeclarator(Declarator &D) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004094 BalancedDelimiterTracker T(*this, tok::l_paren);
4095 T.consumeOpen();
4096
Chris Lattneref4715c2008-04-06 05:45:57 +00004097 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
Mike Stump1eb44332009-09-09 15:08:12 +00004098
Chris Lattner7399ee02008-10-20 02:05:46 +00004099 // Eat any attributes before we look at whether this is a grouping or function
4100 // declarator paren. If this is a grouping paren, the attribute applies to
4101 // the type being built up, for example:
4102 // int (__attribute__(()) *x)(long y)
4103 // If this ends up not being a grouping paren, the attribute applies to the
4104 // first argument, for example:
4105 // int (__attribute__(()) int x)
4106 // In either case, we need to eat any attributes to be able to determine what
4107 // sort of paren this is.
4108 //
John McCall0b7e6782011-03-24 11:26:52 +00004109 ParsedAttributes attrs(AttrFactory);
Chris Lattner7399ee02008-10-20 02:05:46 +00004110 bool RequiresArg = false;
4111 if (Tok.is(tok::kw___attribute)) {
John McCall7f040a92010-12-24 02:08:15 +00004112 ParseGNUAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00004113
Chris Lattner7399ee02008-10-20 02:05:46 +00004114 // We require that the argument list (if this is a non-grouping paren) be
4115 // present even if the attribute list was empty.
4116 RequiresArg = true;
4117 }
Steve Naroff239f0732008-12-25 14:16:32 +00004118 // Eat any Microsoft extensions.
Eli Friedman290eeb02009-06-08 23:27:34 +00004119 if (Tok.is(tok::kw___cdecl) || Tok.is(tok::kw___stdcall) ||
Douglas Gregorf813a2c2010-05-18 16:57:00 +00004120 Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___fastcall) ||
Francois Pichet3bd9aa42011-08-18 09:59:55 +00004121 Tok.is(tok::kw___w64) || Tok.is(tok::kw___ptr64) ||
Francois Pichet58fd97a2011-08-25 00:36:46 +00004122 Tok.is(tok::kw___ptr32) || Tok.is(tok::kw___unaligned)) {
John McCall7f040a92010-12-24 02:08:15 +00004123 ParseMicrosoftTypeAttributes(attrs);
Eli Friedman290eeb02009-06-08 23:27:34 +00004124 }
Dawn Perchik52fc3142010-09-03 01:29:35 +00004125 // Eat any Borland extensions.
Ted Kremenek8113ecf2010-11-10 05:59:39 +00004126 if (Tok.is(tok::kw___pascal))
John McCall7f040a92010-12-24 02:08:15 +00004127 ParseBorlandTypeAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00004128
Chris Lattneref4715c2008-04-06 05:45:57 +00004129 // If we haven't past the identifier yet (or where the identifier would be
4130 // stored, if this is an abstract declarator), then this is probably just
4131 // grouping parens. However, if this could be an abstract-declarator, then
4132 // this could also be the start of function arguments (consider 'void()').
4133 bool isGrouping;
Mike Stump1eb44332009-09-09 15:08:12 +00004134
Chris Lattneref4715c2008-04-06 05:45:57 +00004135 if (!D.mayOmitIdentifier()) {
4136 // If this can't be an abstract-declarator, this *must* be a grouping
4137 // paren, because we haven't seen the identifier yet.
4138 isGrouping = true;
4139 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Argyrios Kyrtzidise25d2702008-10-06 00:07:55 +00004140 (getLang().CPlusPlus && Tok.is(tok::ellipsis)) || // C++ int(...)
Chris Lattneref4715c2008-04-06 05:45:57 +00004141 isDeclarationSpecifier()) { // 'int(int)' is a function.
4142 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
4143 // considered to be a type, not a K&R identifier-list.
4144 isGrouping = false;
4145 } else {
4146 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
4147 isGrouping = true;
4148 }
Mike Stump1eb44332009-09-09 15:08:12 +00004149
Chris Lattneref4715c2008-04-06 05:45:57 +00004150 // If this is a grouping paren, handle:
4151 // direct-declarator: '(' declarator ')'
4152 // direct-declarator: '(' attributes declarator ')'
4153 if (isGrouping) {
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00004154 bool hadGroupingParens = D.hasGroupingParens();
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00004155 D.setGroupingParens(true);
4156
Sebastian Redl4c5d3202008-11-21 19:14:01 +00004157 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattneref4715c2008-04-06 05:45:57 +00004158 // Match the ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004159 T.consumeClose();
4160 D.AddTypeInfo(DeclaratorChunk::getParen(T.getOpenLocation(),
4161 T.getCloseLocation()),
4162 attrs, T.getCloseLocation());
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00004163
4164 D.setGroupingParens(hadGroupingParens);
Chris Lattneref4715c2008-04-06 05:45:57 +00004165 return;
4166 }
Mike Stump1eb44332009-09-09 15:08:12 +00004167
Chris Lattneref4715c2008-04-06 05:45:57 +00004168 // Okay, if this wasn't a grouping paren, it must be the start of a function
4169 // argument list. Recognize that this declarator will never have an
Chris Lattner7399ee02008-10-20 02:05:46 +00004170 // identifier (and remember where it would have been), then call into
4171 // ParseFunctionDeclarator to handle of argument list.
Chris Lattneref4715c2008-04-06 05:45:57 +00004172 D.SetIdentifier(0, Tok.getLocation());
4173
David Blaikie42d6d0c2011-12-04 05:04:18 +00004174 // Enter function-declaration scope, limiting any declarators to the
4175 // function prototype scope, including parameter declarators.
4176 ParseScope PrototypeScope(this,
4177 Scope::FunctionPrototypeScope|Scope::DeclScope);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004178 ParseFunctionDeclarator(D, attrs, T, RequiresArg);
David Blaikie42d6d0c2011-12-04 05:04:18 +00004179 PrototypeScope.Exit();
Chris Lattneref4715c2008-04-06 05:45:57 +00004180}
4181
4182/// ParseFunctionDeclarator - We are after the identifier and have parsed the
4183/// declarator D up to a paren, which indicates that we are parsing function
4184/// arguments.
Reid Spencer5f016e22007-07-11 17:01:13 +00004185///
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004186/// If attrs is non-null, then the caller parsed those arguments immediately
Chris Lattner7399ee02008-10-20 02:05:46 +00004187/// after the open paren - they should be considered to be the first argument of
4188/// a parameter. If RequiresArg is true, then the first argument of the
4189/// function is required to be present and required to not be an identifier
4190/// list.
4191///
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004192/// For C++, after the parameter-list, it also parses cv-qualifier-seq[opt],
4193/// (C++0x) ref-qualifier[opt], exception-specification[opt], and
4194/// (C++0x) trailing-return-type[opt].
4195///
4196/// [C++0x] exception-specification:
4197/// dynamic-exception-specification
4198/// noexcept-specification
4199///
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004200void Parser::ParseFunctionDeclarator(Declarator &D,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004201 ParsedAttributes &attrs,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004202 BalancedDelimiterTracker &Tracker,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004203 bool RequiresArg) {
David Blaikie42d6d0c2011-12-04 05:04:18 +00004204 assert(getCurScope()->isFunctionPrototypeScope() &&
4205 "Should call from a Function scope");
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004206 // lparen is already consumed!
4207 assert(D.isPastIdentifier() && "Should not call before identifier!");
4208
4209 // This should be true when the function has typed arguments.
4210 // Otherwise, it is treated as a K&R-style function.
4211 bool HasProto = false;
4212 // Build up an array of information about the parsed arguments.
Chris Lattner5f9e2722011-07-23 10:55:15 +00004213 SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004214 // Remember where we see an ellipsis, if any.
4215 SourceLocation EllipsisLoc;
4216
4217 DeclSpec DS(AttrFactory);
4218 bool RefQualifierIsLValueRef = true;
4219 SourceLocation RefQualifierLoc;
Douglas Gregor43f51032011-10-19 06:04:55 +00004220 SourceLocation ConstQualifierLoc;
4221 SourceLocation VolatileQualifierLoc;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004222 ExceptionSpecificationType ESpecType = EST_None;
4223 SourceRange ESpecRange;
Chris Lattner5f9e2722011-07-23 10:55:15 +00004224 SmallVector<ParsedType, 2> DynamicExceptions;
4225 SmallVector<SourceRange, 2> DynamicExceptionRanges;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004226 ExprResult NoexceptExpr;
4227 ParsedType TrailingReturnType;
4228
James Molloy16f1f712012-02-29 10:24:19 +00004229 Actions.ActOnStartFunctionDeclarator();
4230
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004231 SourceLocation EndLoc;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004232 if (isFunctionDeclaratorIdentifierList()) {
4233 if (RequiresArg)
4234 Diag(Tok, diag::err_argument_required_after_attribute);
4235
4236 ParseFunctionDeclaratorIdentifierList(D, ParamInfo);
4237
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004238 Tracker.consumeClose();
4239 EndLoc = Tracker.getCloseLocation();
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004240 } else {
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004241 if (Tok.isNot(tok::r_paren))
4242 ParseParameterDeclarationClause(D, attrs, ParamInfo, EllipsisLoc);
4243 else if (RequiresArg)
4244 Diag(Tok, diag::err_argument_required_after_attribute);
4245
4246 HasProto = ParamInfo.size() || getLang().CPlusPlus;
4247
4248 // If we have the closing ')', eat it.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004249 Tracker.consumeClose();
4250 EndLoc = Tracker.getCloseLocation();
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004251
4252 if (getLang().CPlusPlus) {
4253 MaybeParseCXX0XAttributes(attrs);
4254
4255 // Parse cv-qualifier-seq[opt].
4256 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Douglas Gregor43f51032011-10-19 06:04:55 +00004257 if (!DS.getSourceRange().getEnd().isInvalid()) {
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004258 EndLoc = DS.getSourceRange().getEnd();
Douglas Gregor43f51032011-10-19 06:04:55 +00004259 ConstQualifierLoc = DS.getConstSpecLoc();
4260 VolatileQualifierLoc = DS.getVolatileSpecLoc();
4261 }
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004262
4263 // Parse ref-qualifier[opt].
4264 if (Tok.is(tok::amp) || Tok.is(tok::ampamp)) {
Richard Smith7fe62082011-10-15 05:09:34 +00004265 Diag(Tok, getLang().CPlusPlus0x ?
4266 diag::warn_cxx98_compat_ref_qualifier :
4267 diag::ext_ref_qualifier);
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004268
4269 RefQualifierIsLValueRef = Tok.is(tok::amp);
4270 RefQualifierLoc = ConsumeToken();
4271 EndLoc = RefQualifierLoc;
4272 }
4273
4274 // Parse exception-specification[opt].
4275 ESpecType = MaybeParseExceptionSpecification(ESpecRange,
4276 DynamicExceptions,
4277 DynamicExceptionRanges,
4278 NoexceptExpr);
4279 if (ESpecType != EST_None)
4280 EndLoc = ESpecRange.getEnd();
4281
4282 // Parse trailing-return-type[opt].
4283 if (getLang().CPlusPlus0x && Tok.is(tok::arrow)) {
Richard Smith7fe62082011-10-15 05:09:34 +00004284 Diag(Tok, diag::warn_cxx98_compat_trailing_return_type);
Douglas Gregorae7902c2011-08-04 15:30:47 +00004285 SourceRange Range;
4286 TrailingReturnType = ParseTrailingReturnType(Range).get();
4287 if (Range.getEnd().isValid())
4288 EndLoc = Range.getEnd();
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004289 }
4290 }
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004291 }
4292
4293 // Remember that we parsed a function type, and remember the attributes.
4294 D.AddTypeInfo(DeclaratorChunk::getFunction(HasProto,
4295 /*isVariadic=*/EllipsisLoc.isValid(),
4296 EllipsisLoc,
4297 ParamInfo.data(), ParamInfo.size(),
4298 DS.getTypeQualifiers(),
4299 RefQualifierIsLValueRef,
Douglas Gregor43f51032011-10-19 06:04:55 +00004300 RefQualifierLoc, ConstQualifierLoc,
4301 VolatileQualifierLoc,
Douglas Gregor90ebed02011-07-13 21:47:47 +00004302 /*MutableLoc=*/SourceLocation(),
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004303 ESpecType, ESpecRange.getBegin(),
4304 DynamicExceptions.data(),
4305 DynamicExceptionRanges.data(),
4306 DynamicExceptions.size(),
4307 NoexceptExpr.isUsable() ?
4308 NoexceptExpr.get() : 0,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004309 Tracker.getOpenLocation(),
4310 EndLoc, D,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004311 TrailingReturnType),
4312 attrs, EndLoc);
James Molloy16f1f712012-02-29 10:24:19 +00004313
4314 Actions.ActOnEndFunctionDeclarator();
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004315}
4316
4317/// isFunctionDeclaratorIdentifierList - This parameter list may have an
4318/// identifier list form for a K&R-style function: void foo(a,b,c)
4319///
4320/// Note that identifier-lists are only allowed for normal declarators, not for
4321/// abstract-declarators.
4322bool Parser::isFunctionDeclaratorIdentifierList() {
4323 return !getLang().CPlusPlus
4324 && Tok.is(tok::identifier)
4325 && !TryAltiVecVectorToken()
4326 // K&R identifier lists can't have typedefs as identifiers, per C99
4327 // 6.7.5.3p11.
4328 && (TryAnnotateTypeOrScopeToken() || !Tok.is(tok::annot_typename))
4329 // Identifier lists follow a really simple grammar: the identifiers can
4330 // be followed *only* by a ", identifier" or ")". However, K&R
4331 // identifier lists are really rare in the brave new modern world, and
4332 // it is very common for someone to typo a type in a non-K&R style
4333 // list. If we are presented with something like: "void foo(intptr x,
4334 // float y)", we don't want to start parsing the function declarator as
4335 // though it is a K&R style declarator just because intptr is an
4336 // invalid type.
4337 //
4338 // To handle this, we check to see if the token after the first
4339 // identifier is a "," or ")". Only then do we parse it as an
4340 // identifier list.
4341 && (NextToken().is(tok::comma) || NextToken().is(tok::r_paren));
4342}
4343
4344/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
4345/// we found a K&R-style identifier list instead of a typed parameter list.
4346///
4347/// After returning, ParamInfo will hold the parsed parameters.
4348///
4349/// identifier-list: [C99 6.7.5]
4350/// identifier
4351/// identifier-list ',' identifier
4352///
4353void Parser::ParseFunctionDeclaratorIdentifierList(
4354 Declarator &D,
Chris Lattner5f9e2722011-07-23 10:55:15 +00004355 SmallVector<DeclaratorChunk::ParamInfo, 16> &ParamInfo) {
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004356 // If there was no identifier specified for the declarator, either we are in
4357 // an abstract-declarator, or we are in a parameter declarator which was found
4358 // to be abstract. In abstract-declarators, identifier lists are not valid:
4359 // diagnose this.
4360 if (!D.getIdentifier())
4361 Diag(Tok, diag::ext_ident_list_in_param);
4362
4363 // Maintain an efficient lookup of params we have seen so far.
4364 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
4365
4366 while (1) {
4367 // If this isn't an identifier, report the error and skip until ')'.
4368 if (Tok.isNot(tok::identifier)) {
4369 Diag(Tok, diag::err_expected_ident);
4370 SkipUntil(tok::r_paren, /*StopAtSemi=*/true, /*DontConsume=*/true);
4371 // Forget we parsed anything.
4372 ParamInfo.clear();
4373 return;
4374 }
4375
4376 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
4377
4378 // Reject 'typedef int y; int test(x, y)', but continue parsing.
4379 if (Actions.getTypeName(*ParmII, Tok.getLocation(), getCurScope()))
4380 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
4381
4382 // Verify that the argument identifier has not already been mentioned.
4383 if (!ParamsSoFar.insert(ParmII)) {
4384 Diag(Tok, diag::err_param_redefinition) << ParmII;
4385 } else {
4386 // Remember this identifier in ParamInfo.
4387 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
4388 Tok.getLocation(),
4389 0));
4390 }
4391
4392 // Eat the identifier.
4393 ConsumeToken();
4394
4395 // The list continues if we see a comma.
4396 if (Tok.isNot(tok::comma))
4397 break;
4398 ConsumeToken();
4399 }
4400}
4401
4402/// ParseParameterDeclarationClause - Parse a (possibly empty) parameter-list
4403/// after the opening parenthesis. This function will not parse a K&R-style
4404/// identifier list.
4405///
4406/// D is the declarator being parsed. If attrs is non-null, then the caller
4407/// parsed those arguments immediately after the open paren - they should be
4408/// considered to be the first argument of a parameter.
4409///
4410/// After returning, ParamInfo will hold the parsed parameters. EllipsisLoc will
4411/// be the location of the ellipsis, if any was parsed.
4412///
Reid Spencer5f016e22007-07-11 17:01:13 +00004413/// parameter-type-list: [C99 6.7.5]
4414/// parameter-list
4415/// parameter-list ',' '...'
Douglas Gregored5d6512009-09-22 21:41:40 +00004416/// [C++] parameter-list '...'
Reid Spencer5f016e22007-07-11 17:01:13 +00004417///
4418/// parameter-list: [C99 6.7.5]
4419/// parameter-declaration
4420/// parameter-list ',' parameter-declaration
4421///
4422/// parameter-declaration: [C99 6.7.5]
4423/// declaration-specifiers declarator
Chris Lattner04421082008-04-08 04:40:51 +00004424/// [C++] declaration-specifiers declarator '=' assignment-expression
Reid Spencer5f016e22007-07-11 17:01:13 +00004425/// [GNU] declaration-specifiers declarator attributes
Sebastian Redl50de12f2009-03-24 22:27:57 +00004426/// declaration-specifiers abstract-declarator[opt]
4427/// [C++] declaration-specifiers abstract-declarator[opt]
Chris Lattner8123a952008-04-10 02:22:51 +00004428/// '=' assignment-expression
Reid Spencer5f016e22007-07-11 17:01:13 +00004429/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
4430///
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004431void Parser::ParseParameterDeclarationClause(
4432 Declarator &D,
4433 ParsedAttributes &attrs,
Chris Lattner5f9e2722011-07-23 10:55:15 +00004434 SmallVector<DeclaratorChunk::ParamInfo, 16> &ParamInfo,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004435 SourceLocation &EllipsisLoc) {
Mike Stump1eb44332009-09-09 15:08:12 +00004436
Chris Lattnerf97409f2008-04-06 06:57:35 +00004437 while (1) {
4438 if (Tok.is(tok::ellipsis)) {
Douglas Gregor965acbb2009-02-18 07:07:28 +00004439 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chris Lattnerf97409f2008-04-06 06:57:35 +00004440 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00004441 }
Mike Stump1eb44332009-09-09 15:08:12 +00004442
Chris Lattnerf97409f2008-04-06 06:57:35 +00004443 // Parse the declaration-specifiers.
John McCall54abf7d2009-11-04 02:18:39 +00004444 // Just use the ParsingDeclaration "scope" of the declarator.
John McCall0b7e6782011-03-24 11:26:52 +00004445 DeclSpec DS(AttrFactory);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004446
John McCall7f040a92010-12-24 02:08:15 +00004447 // Skip any Microsoft attributes before a param.
Francois Pichet62ec1f22011-09-17 17:15:52 +00004448 if (getLang().MicrosoftExt && Tok.is(tok::l_square))
John McCall7f040a92010-12-24 02:08:15 +00004449 ParseMicrosoftAttributes(DS.getAttributes());
4450
4451 SourceLocation DSStart = Tok.getLocation();
Chris Lattner7399ee02008-10-20 02:05:46 +00004452
4453 // If the caller parsed attributes for the first argument, add them now.
John McCall7f040a92010-12-24 02:08:15 +00004454 // Take them so that we only apply the attributes to the first parameter.
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004455 // FIXME: If we saw an ellipsis first, this code is not reached. Are the
4456 // attributes lost? Should they even be allowed?
4457 // FIXME: If we can leave the attributes in the token stream somehow, we can
4458 // get rid of a parameter (attrs) and this statement. It might be too much
4459 // hassle.
John McCall7f040a92010-12-24 02:08:15 +00004460 DS.takeAttributesFrom(attrs);
4461
Chris Lattnere64c5492009-02-27 18:38:20 +00004462 ParseDeclarationSpecifiers(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00004463
Chris Lattnerf97409f2008-04-06 06:57:35 +00004464 // Parse the declarator. This is "PrototypeContext", because we must
4465 // accept either 'declarator' or 'abstract-declarator' here.
4466 Declarator ParmDecl(DS, Declarator::PrototypeContext);
4467 ParseDeclarator(ParmDecl);
4468
4469 // Parse GNU attributes, if present.
John McCall7f040a92010-12-24 02:08:15 +00004470 MaybeParseGNUAttributes(ParmDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00004471
Chris Lattnerf97409f2008-04-06 06:57:35 +00004472 // Remember this parsed parameter in ParamInfo.
4473 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
Mike Stump1eb44332009-09-09 15:08:12 +00004474
Douglas Gregor72b505b2008-12-16 21:30:33 +00004475 // DefArgToks is used when the parsing of default arguments needs
4476 // to be delayed.
4477 CachedTokens *DefArgToks = 0;
4478
Chris Lattnerf97409f2008-04-06 06:57:35 +00004479 // If no parameter was specified, verify that *something* was specified,
4480 // otherwise we have a missing type and identifier.
Chris Lattnere64c5492009-02-27 18:38:20 +00004481 if (DS.isEmpty() && ParmDecl.getIdentifier() == 0 &&
4482 ParmDecl.getNumTypeObjects() == 0) {
Chris Lattnerf97409f2008-04-06 06:57:35 +00004483 // Completely missing, emit error.
4484 Diag(DSStart, diag::err_missing_param);
4485 } else {
4486 // Otherwise, we have something. Add it and let semantic analysis try
4487 // to grok it and add the result to the ParamInfo we are building.
Mike Stump1eb44332009-09-09 15:08:12 +00004488
Chris Lattnerf97409f2008-04-06 06:57:35 +00004489 // Inform the actions module about the parameter declarator, so it gets
4490 // added to the current scope.
John McCalld226f652010-08-21 09:40:31 +00004491 Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl);
Chris Lattner04421082008-04-08 04:40:51 +00004492
4493 // Parse the default argument, if any. We parse the default
4494 // arguments in all dialects; the semantic analysis in
4495 // ActOnParamDefaultArgument will reject the default argument in
4496 // C.
4497 if (Tok.is(tok::equal)) {
Douglas Gregor61366e92008-12-24 00:01:03 +00004498 SourceLocation EqualLoc = Tok.getLocation();
4499
Chris Lattner04421082008-04-08 04:40:51 +00004500 // Parse the default argument
Douglas Gregor72b505b2008-12-16 21:30:33 +00004501 if (D.getContext() == Declarator::MemberContext) {
4502 // If we're inside a class definition, cache the tokens
4503 // corresponding to the default argument. We'll actually parse
4504 // them when we see the end of the class definition.
4505 // FIXME: Templates will require something similar.
4506 // FIXME: Can we use a smart pointer for Toks?
4507 DefArgToks = new CachedTokens;
4508
Mike Stump1eb44332009-09-09 15:08:12 +00004509 if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks,
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +00004510 /*StopAtSemi=*/true,
4511 /*ConsumeFinalToken=*/false)) {
Douglas Gregor72b505b2008-12-16 21:30:33 +00004512 delete DefArgToks;
4513 DefArgToks = 0;
Douglas Gregor61366e92008-12-24 00:01:03 +00004514 Actions.ActOnParamDefaultArgumentError(Param);
Argyrios Kyrtzidis2b602ad2010-08-06 09:47:24 +00004515 } else {
4516 // Mark the end of the default argument so that we know when to
4517 // stop when we parse it later on.
4518 Token DefArgEnd;
4519 DefArgEnd.startToken();
4520 DefArgEnd.setKind(tok::cxx_defaultarg_end);
4521 DefArgEnd.setLocation(Tok.getLocation());
4522 DefArgToks->push_back(DefArgEnd);
Mike Stump1eb44332009-09-09 15:08:12 +00004523 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc,
Anders Carlsson5e300d12009-06-12 16:51:40 +00004524 (*DefArgToks)[1].getLocation());
Argyrios Kyrtzidis2b602ad2010-08-06 09:47:24 +00004525 }
Chris Lattner04421082008-04-08 04:40:51 +00004526 } else {
Douglas Gregor72b505b2008-12-16 21:30:33 +00004527 // Consume the '='.
Douglas Gregor61366e92008-12-24 00:01:03 +00004528 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00004529
Douglas Gregorbe0f7bd2010-09-11 20:24:53 +00004530 // The argument isn't actually potentially evaluated unless it is
4531 // used.
4532 EnterExpressionEvaluationContext Eval(Actions,
Douglas Gregorccc1b5e2012-02-21 00:37:24 +00004533 Sema::PotentiallyEvaluatedIfUsed,
4534 Param);
Douglas Gregorbe0f7bd2010-09-11 20:24:53 +00004535
John McCall60d7b3a2010-08-24 06:29:42 +00004536 ExprResult DefArgResult(ParseAssignmentExpression());
Douglas Gregor72b505b2008-12-16 21:30:33 +00004537 if (DefArgResult.isInvalid()) {
4538 Actions.ActOnParamDefaultArgumentError(Param);
4539 SkipUntil(tok::comma, tok::r_paren, true, true);
4540 } else {
4541 // Inform the actions module about the default argument
4542 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
John McCall9ae2f072010-08-23 23:25:46 +00004543 DefArgResult.take());
Douglas Gregor72b505b2008-12-16 21:30:33 +00004544 }
Chris Lattner04421082008-04-08 04:40:51 +00004545 }
4546 }
Mike Stump1eb44332009-09-09 15:08:12 +00004547
4548 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
4549 ParmDecl.getIdentifierLoc(), Param,
Douglas Gregor72b505b2008-12-16 21:30:33 +00004550 DefArgToks));
Chris Lattnerf97409f2008-04-06 06:57:35 +00004551 }
4552
4553 // If the next token is a comma, consume it and keep reading arguments.
Douglas Gregored5d6512009-09-22 21:41:40 +00004554 if (Tok.isNot(tok::comma)) {
4555 if (Tok.is(tok::ellipsis)) {
Douglas Gregored5d6512009-09-22 21:41:40 +00004556 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
4557
4558 if (!getLang().CPlusPlus) {
4559 // We have ellipsis without a preceding ',', which is ill-formed
4560 // in C. Complain and provide the fix.
4561 Diag(EllipsisLoc, diag::err_missing_comma_before_ellipsis)
Douglas Gregor849b2432010-03-31 17:46:05 +00004562 << FixItHint::CreateInsertion(EllipsisLoc, ", ");
Douglas Gregored5d6512009-09-22 21:41:40 +00004563 }
4564 }
4565
4566 break;
4567 }
Mike Stump1eb44332009-09-09 15:08:12 +00004568
Chris Lattnerf97409f2008-04-06 06:57:35 +00004569 // Consume the comma.
4570 ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00004571 }
Mike Stump1eb44332009-09-09 15:08:12 +00004572
Chris Lattner66d28652008-04-06 06:34:08 +00004573}
Chris Lattneref4715c2008-04-06 05:45:57 +00004574
Reid Spencer5f016e22007-07-11 17:01:13 +00004575/// [C90] direct-declarator '[' constant-expression[opt] ']'
4576/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
4577/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
4578/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
4579/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
4580void Parser::ParseBracketDeclarator(Declarator &D) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004581 BalancedDelimiterTracker T(*this, tok::l_square);
4582 T.consumeOpen();
Mike Stump1eb44332009-09-09 15:08:12 +00004583
Chris Lattner378c7e42008-12-18 07:27:21 +00004584 // C array syntax has many features, but by-far the most common is [] and [4].
4585 // This code does a fast path to handle some of the most obvious cases.
4586 if (Tok.getKind() == tok::r_square) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004587 T.consumeClose();
John McCall0b7e6782011-03-24 11:26:52 +00004588 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00004589 MaybeParseCXX0XAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00004590
Chris Lattner378c7e42008-12-18 07:27:21 +00004591 // Remember that we parsed the empty array type.
John McCall60d7b3a2010-08-24 06:29:42 +00004592 ExprResult NumElements;
John McCall0b7e6782011-03-24 11:26:52 +00004593 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004594 T.getOpenLocation(),
4595 T.getCloseLocation()),
4596 attrs, T.getCloseLocation());
Chris Lattner378c7e42008-12-18 07:27:21 +00004597 return;
4598 } else if (Tok.getKind() == tok::numeric_constant &&
4599 GetLookAheadToken(1).is(tok::r_square)) {
4600 // [4] is very common. Parse the numeric constant expression.
John McCall60d7b3a2010-08-24 06:29:42 +00004601 ExprResult ExprRes(Actions.ActOnNumericConstant(Tok));
Chris Lattner378c7e42008-12-18 07:27:21 +00004602 ConsumeToken();
4603
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);
Mike Stump1eb44332009-09-09 15:08:12 +00004607
Chris Lattner378c7e42008-12-18 07:27:21 +00004608 // Remember that we parsed a array type, and remember its features.
John McCall0b7e6782011-03-24 11:26:52 +00004609 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, 0,
John McCall7f040a92010-12-24 02:08:15 +00004610 ExprRes.release(),
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 }
Mike Stump1eb44332009-09-09 15:08:12 +00004616
Reid Spencer5f016e22007-07-11 17:01:13 +00004617 // If valid, this location is the position where we read the 'static' keyword.
4618 SourceLocation StaticLoc;
Chris Lattner04d66662007-10-09 17:33:22 +00004619 if (Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00004620 StaticLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00004621
Reid Spencer5f016e22007-07-11 17:01:13 +00004622 // If there is a type-qualifier-list, read it now.
Chris Lattnera1fcbad2008-12-18 06:50:14 +00004623 // Type qualifiers in an array subscript are a C99 feature.
John McCall0b7e6782011-03-24 11:26:52 +00004624 DeclSpec DS(AttrFactory);
Chris Lattner5a69d1c2008-12-18 07:02:59 +00004625 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Mike Stump1eb44332009-09-09 15:08:12 +00004626
Reid Spencer5f016e22007-07-11 17:01:13 +00004627 // If we haven't already read 'static', check to see if there is one after the
4628 // type-qualifier-list.
Chris Lattner04d66662007-10-09 17:33:22 +00004629 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00004630 StaticLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00004631
Reid Spencer5f016e22007-07-11 17:01:13 +00004632 // Handle "direct-declarator [ type-qual-list[opt] * ]".
4633 bool isStar = false;
John McCall60d7b3a2010-08-24 06:29:42 +00004634 ExprResult NumElements;
Mike Stump1eb44332009-09-09 15:08:12 +00004635
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00004636 // Handle the case where we have '[*]' as the array size. However, a leading
4637 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
4638 // the the token after the star is a ']'. Since stars in arrays are
4639 // infrequent, use of lookahead is not costly here.
4640 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattnera711dd02008-04-06 05:27:21 +00004641 ConsumeToken(); // Eat the '*'.
Reid Spencer5f016e22007-07-11 17:01:13 +00004642
Chris Lattnera1fcbad2008-12-18 06:50:14 +00004643 if (StaticLoc.isValid()) {
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00004644 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattnera1fcbad2008-12-18 06:50:14 +00004645 StaticLoc = SourceLocation(); // Drop the static.
4646 }
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00004647 isStar = true;
Chris Lattner04d66662007-10-09 17:33:22 +00004648 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner378c7e42008-12-18 07:27:21 +00004649 // Note, in C89, this production uses the constant-expr production instead
4650 // of assignment-expr. The only difference is that assignment-expr allows
4651 // things like '=' and '*='. Sema rejects these in C89 mode because they
4652 // are not i-c-e's, so we don't need to distinguish between the two here.
Mike Stump1eb44332009-09-09 15:08:12 +00004653
Douglas Gregore0762c92009-06-19 23:52:42 +00004654 // Parse the constant-expression or assignment-expression now (depending
4655 // on dialect).
Eli Friedman71b8fb52012-01-21 01:01:51 +00004656 if (getLang().CPlusPlus) {
Douglas Gregore0762c92009-06-19 23:52:42 +00004657 NumElements = ParseConstantExpression();
Eli Friedman71b8fb52012-01-21 01:01:51 +00004658 } else {
4659 EnterExpressionEvaluationContext Unevaluated(Actions,
4660 Sema::ConstantEvaluated);
Douglas Gregore0762c92009-06-19 23:52:42 +00004661 NumElements = ParseAssignmentExpression();
Eli Friedman71b8fb52012-01-21 01:01:51 +00004662 }
Reid Spencer5f016e22007-07-11 17:01:13 +00004663 }
Mike Stump1eb44332009-09-09 15:08:12 +00004664
Reid Spencer5f016e22007-07-11 17:01:13 +00004665 // If there was an error parsing the assignment-expression, recover.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00004666 if (NumElements.isInvalid()) {
Chris Lattner5cb10d32009-04-24 22:30:50 +00004667 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00004668 // If the expression was invalid, skip it.
4669 SkipUntil(tok::r_square);
4670 return;
4671 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00004672
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004673 T.consumeClose();
Sebastian Redlab197ba2009-02-09 18:23:29 +00004674
John McCall0b7e6782011-03-24 11:26:52 +00004675 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00004676 MaybeParseCXX0XAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00004677
Chris Lattner378c7e42008-12-18 07:27:21 +00004678 // Remember that we parsed a array type, and remember its features.
John McCall0b7e6782011-03-24 11:26:52 +00004679 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
Reid Spencer5f016e22007-07-11 17:01:13 +00004680 StaticLoc.isValid(), isStar,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00004681 NumElements.release(),
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004682 T.getOpenLocation(),
4683 T.getCloseLocation()),
4684 attrs, T.getCloseLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00004685}
4686
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004687/// [GNU] typeof-specifier:
4688/// typeof ( expressions )
4689/// typeof ( type-name )
4690/// [GNU/C++] typeof unary-expression
Steve Naroffd1861fd2007-07-31 12:34:36 +00004691///
4692void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner04d66662007-10-09 17:33:22 +00004693 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004694 Token OpTok = Tok;
Steve Naroffd1861fd2007-07-31 12:34:36 +00004695 SourceLocation StartLoc = ConsumeToken();
4696
John McCallcfb708c2010-01-13 20:03:27 +00004697 const bool hasParens = Tok.is(tok::l_paren);
4698
Eli Friedman71b8fb52012-01-21 01:01:51 +00004699 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated);
4700
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004701 bool isCastExpr;
John McCallb3d87482010-08-24 05:47:05 +00004702 ParsedType CastTy;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004703 SourceRange CastRange;
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00004704 ExprResult Operand = ParseExprAfterUnaryExprOrTypeTrait(OpTok, isCastExpr,
4705 CastTy, CastRange);
John McCallcfb708c2010-01-13 20:03:27 +00004706 if (hasParens)
4707 DS.setTypeofParensRange(CastRange);
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004708
4709 if (CastRange.getEnd().isInvalid())
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004710 // FIXME: Not accurate, the range gets one token more than it should.
4711 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004712 else
4713 DS.SetRangeEnd(CastRange.getEnd());
Mike Stump1eb44332009-09-09 15:08:12 +00004714
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004715 if (isCastExpr) {
4716 if (!CastTy) {
4717 DS.SetTypeSpecError();
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004718 return;
Douglas Gregor809070a2009-02-18 17:45:20 +00004719 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004720
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004721 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00004722 unsigned DiagID;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004723 // Check for duplicate type specifiers (e.g. "int typeof(int)").
4724 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00004725 DiagID, CastTy))
4726 Diag(StartLoc, DiagID) << PrevSpec;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004727 return;
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004728 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004729
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004730 // If we get here, the operand to the typeof was an expresion.
4731 if (Operand.isInvalid()) {
4732 DS.SetTypeSpecError();
Steve Naroff9dfa7b42007-08-02 02:53:48 +00004733 return;
Steve Naroffd1861fd2007-07-31 12:34:36 +00004734 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004735
Eli Friedman71b8fb52012-01-21 01:01:51 +00004736 // We might need to transform the operand if it is potentially evaluated.
4737 Operand = Actions.HandleExprEvaluationContextForTypeof(Operand.get());
4738 if (Operand.isInvalid()) {
4739 DS.SetTypeSpecError();
4740 return;
4741 }
4742
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004743 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00004744 unsigned DiagID;
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004745 // Check for duplicate type specifiers (e.g. "int typeof(int)").
4746 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
John McCallb3d87482010-08-24 05:47:05 +00004747 DiagID, Operand.get()))
John McCallfec54012009-08-03 20:12:06 +00004748 Diag(StartLoc, DiagID) << PrevSpec;
Steve Naroffd1861fd2007-07-31 12:34:36 +00004749}
Chris Lattner1b492422010-02-28 18:33:55 +00004750
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00004751/// [C11] atomic-specifier:
Eli Friedmanb001de72011-10-06 23:00:33 +00004752/// _Atomic ( type-name )
4753///
4754void Parser::ParseAtomicSpecifier(DeclSpec &DS) {
4755 assert(Tok.is(tok::kw__Atomic) && "Not an atomic specifier");
4756
4757 SourceLocation StartLoc = ConsumeToken();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004758 BalancedDelimiterTracker T(*this, tok::l_paren);
4759 if (T.expectAndConsume(diag::err_expected_lparen_after, "_Atomic")) {
Eli Friedmanb001de72011-10-06 23:00:33 +00004760 SkipUntil(tok::r_paren);
4761 return;
4762 }
4763
4764 TypeResult Result = ParseTypeName();
4765 if (Result.isInvalid()) {
4766 SkipUntil(tok::r_paren);
4767 return;
4768 }
4769
4770 // Match the ')'
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004771 T.consumeClose();
Eli Friedmanb001de72011-10-06 23:00:33 +00004772
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004773 if (T.getCloseLocation().isInvalid())
Eli Friedmanb001de72011-10-06 23:00:33 +00004774 return;
4775
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004776 DS.setTypeofParensRange(T.getRange());
4777 DS.SetRangeEnd(T.getCloseLocation());
Eli Friedmanb001de72011-10-06 23:00:33 +00004778
4779 const char *PrevSpec = 0;
4780 unsigned DiagID;
4781 if (DS.SetTypeSpecType(DeclSpec::TST_atomic, StartLoc, PrevSpec,
4782 DiagID, Result.release()))
4783 Diag(StartLoc, DiagID) << PrevSpec;
4784}
4785
Chris Lattner1b492422010-02-28 18:33:55 +00004786
4787/// TryAltiVecVectorTokenOutOfLine - Out of line body that should only be called
4788/// from TryAltiVecVectorToken.
4789bool Parser::TryAltiVecVectorTokenOutOfLine() {
4790 Token Next = NextToken();
4791 switch (Next.getKind()) {
4792 default: return false;
4793 case tok::kw_short:
4794 case tok::kw_long:
4795 case tok::kw_signed:
4796 case tok::kw_unsigned:
4797 case tok::kw_void:
4798 case tok::kw_char:
4799 case tok::kw_int:
4800 case tok::kw_float:
4801 case tok::kw_double:
4802 case tok::kw_bool:
4803 case tok::kw___pixel:
4804 Tok.setKind(tok::kw___vector);
4805 return true;
4806 case tok::identifier:
4807 if (Next.getIdentifierInfo() == Ident_pixel) {
4808 Tok.setKind(tok::kw___vector);
4809 return true;
4810 }
4811 return false;
4812 }
4813}
4814
4815bool Parser::TryAltiVecTokenOutOfLine(DeclSpec &DS, SourceLocation Loc,
4816 const char *&PrevSpec, unsigned &DiagID,
4817 bool &isInvalid) {
4818 if (Tok.getIdentifierInfo() == Ident_vector) {
4819 Token Next = NextToken();
4820 switch (Next.getKind()) {
4821 case tok::kw_short:
4822 case tok::kw_long:
4823 case tok::kw_signed:
4824 case tok::kw_unsigned:
4825 case tok::kw_void:
4826 case tok::kw_char:
4827 case tok::kw_int:
4828 case tok::kw_float:
4829 case tok::kw_double:
4830 case tok::kw_bool:
4831 case tok::kw___pixel:
4832 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
4833 return true;
4834 case tok::identifier:
4835 if (Next.getIdentifierInfo() == Ident_pixel) {
4836 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
4837 return true;
4838 }
4839 break;
4840 default:
4841 break;
4842 }
Douglas Gregora8f031f2010-06-16 15:28:57 +00004843 } else if ((Tok.getIdentifierInfo() == Ident_pixel) &&
Chris Lattner1b492422010-02-28 18:33:55 +00004844 DS.isTypeAltiVecVector()) {
4845 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
4846 return true;
4847 }
4848 return false;
4849}