blob: c1f6eb5d42393a6f6c9301baec4077909dfcc74c [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());
Michael Hane53ac8a2012-03-07 00:12:16 +0000273 if (BuiltinType && attr->getKind() == AttributeList::AT_iboutletcollection)
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000274 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
Douglas Gregor93a70672012-03-11 04:53:21 +0000539/// 'obsoleted' = version
Douglas Gregorb53e4172011-03-26 03:35:55 +0000540/// 'unavailable'
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000541/// opt-message:
542/// 'message' '=' <string>
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000543void Parser::ParseAvailabilityAttribute(IdentifierInfo &Availability,
544 SourceLocation AvailabilityLoc,
545 ParsedAttributes &attrs,
546 SourceLocation *endLoc) {
547 SourceLocation PlatformLoc;
548 IdentifierInfo *Platform = 0;
549
550 enum { Introduced, Deprecated, Obsoleted, Unknown };
551 AvailabilityChange Changes[Unknown];
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000552 ExprResult MessageExpr;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000553
554 // Opening '('.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000555 BalancedDelimiterTracker T(*this, tok::l_paren);
556 if (T.consumeOpen()) {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000557 Diag(Tok, diag::err_expected_lparen);
558 return;
559 }
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000560
561 // Parse the platform name,
562 if (Tok.isNot(tok::identifier)) {
563 Diag(Tok, diag::err_availability_expected_platform);
564 SkipUntil(tok::r_paren);
565 return;
566 }
567 Platform = Tok.getIdentifierInfo();
568 PlatformLoc = ConsumeToken();
569
570 // Parse the ',' following the platform name.
571 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "", tok::r_paren))
572 return;
573
574 // If we haven't grabbed the pointers for the identifiers
575 // "introduced", "deprecated", and "obsoleted", do so now.
576 if (!Ident_introduced) {
577 Ident_introduced = PP.getIdentifierInfo("introduced");
578 Ident_deprecated = PP.getIdentifierInfo("deprecated");
579 Ident_obsoleted = PP.getIdentifierInfo("obsoleted");
Douglas Gregorb53e4172011-03-26 03:35:55 +0000580 Ident_unavailable = PP.getIdentifierInfo("unavailable");
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000581 Ident_message = PP.getIdentifierInfo("message");
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000582 }
583
584 // Parse the set of introductions/deprecations/removals.
Douglas Gregorb53e4172011-03-26 03:35:55 +0000585 SourceLocation UnavailableLoc;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000586 do {
587 if (Tok.isNot(tok::identifier)) {
588 Diag(Tok, diag::err_availability_expected_change);
589 SkipUntil(tok::r_paren);
590 return;
591 }
592 IdentifierInfo *Keyword = Tok.getIdentifierInfo();
593 SourceLocation KeywordLoc = ConsumeToken();
594
Douglas Gregorb53e4172011-03-26 03:35:55 +0000595 if (Keyword == Ident_unavailable) {
596 if (UnavailableLoc.isValid()) {
597 Diag(KeywordLoc, diag::err_availability_redundant)
598 << Keyword << SourceRange(UnavailableLoc);
599 }
600 UnavailableLoc = KeywordLoc;
601
602 if (Tok.isNot(tok::comma))
603 break;
604
605 ConsumeToken();
606 continue;
607 }
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000608
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000609 if (Tok.isNot(tok::equal)) {
610 Diag(Tok, diag::err_expected_equal_after)
611 << Keyword;
612 SkipUntil(tok::r_paren);
613 return;
614 }
615 ConsumeToken();
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000616 if (Keyword == Ident_message) {
617 if (!isTokenStringLiteral()) {
618 Diag(Tok, diag::err_expected_string_literal);
619 SkipUntil(tok::r_paren);
620 return;
621 }
622 MessageExpr = ParseStringLiteralExpression();
623 break;
624 }
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000625
626 SourceRange VersionRange;
627 VersionTuple Version = ParseVersionTuple(VersionRange);
628
629 if (Version.empty()) {
630 SkipUntil(tok::r_paren);
631 return;
632 }
633
634 unsigned Index;
635 if (Keyword == Ident_introduced)
636 Index = Introduced;
637 else if (Keyword == Ident_deprecated)
638 Index = Deprecated;
639 else if (Keyword == Ident_obsoleted)
640 Index = Obsoleted;
641 else
642 Index = Unknown;
643
644 if (Index < Unknown) {
645 if (!Changes[Index].KeywordLoc.isInvalid()) {
646 Diag(KeywordLoc, diag::err_availability_redundant)
647 << Keyword
648 << SourceRange(Changes[Index].KeywordLoc,
649 Changes[Index].VersionRange.getEnd());
650 }
651
652 Changes[Index].KeywordLoc = KeywordLoc;
653 Changes[Index].Version = Version;
654 Changes[Index].VersionRange = VersionRange;
655 } else {
656 Diag(KeywordLoc, diag::err_availability_unknown_change)
657 << Keyword << VersionRange;
658 }
659
660 if (Tok.isNot(tok::comma))
661 break;
662
663 ConsumeToken();
664 } while (true);
665
666 // Closing ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000667 if (T.consumeClose())
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000668 return;
669
670 if (endLoc)
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000671 *endLoc = T.getCloseLocation();
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000672
Douglas Gregorb53e4172011-03-26 03:35:55 +0000673 // The 'unavailable' availability cannot be combined with any other
674 // availability changes. Make sure that hasn't happened.
675 if (UnavailableLoc.isValid()) {
676 bool Complained = false;
677 for (unsigned Index = Introduced; Index != Unknown; ++Index) {
678 if (Changes[Index].KeywordLoc.isValid()) {
679 if (!Complained) {
680 Diag(UnavailableLoc, diag::warn_availability_and_unavailable)
681 << SourceRange(Changes[Index].KeywordLoc,
682 Changes[Index].VersionRange.getEnd());
683 Complained = true;
684 }
685
686 // Clear out the availability.
687 Changes[Index] = AvailabilityChange();
688 }
689 }
690 }
691
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000692 // Record this attribute
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000693 attrs.addNew(&Availability,
694 SourceRange(AvailabilityLoc, T.getCloseLocation()),
Fariborz Jahanianf96708d2012-01-23 23:38:32 +0000695 0, AvailabilityLoc,
John McCall0b7e6782011-03-24 11:26:52 +0000696 Platform, PlatformLoc,
697 Changes[Introduced],
698 Changes[Deprecated],
Douglas Gregorb53e4172011-03-26 03:35:55 +0000699 Changes[Obsoleted],
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000700 UnavailableLoc, MessageExpr.take(),
701 false, false);
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000702}
703
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000704
705// Late Parsed Attributes:
706// See other examples of late parsing in lib/Parse/ParseCXXInlineMethods
707
708void Parser::LateParsedDeclaration::ParseLexedAttributes() {}
709
710void Parser::LateParsedClass::ParseLexedAttributes() {
711 Self->ParseLexedAttributes(*Class);
712}
713
714void Parser::LateParsedAttribute::ParseLexedAttributes() {
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000715 Self->ParseLexedAttribute(*this, true, false);
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000716}
717
718/// Wrapper class which calls ParseLexedAttribute, after setting up the
719/// scope appropriately.
720void Parser::ParseLexedAttributes(ParsingClass &Class) {
721 // Deal with templates
722 // FIXME: Test cases to make sure this does the right thing for templates.
723 bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope;
724 ParseScope ClassTemplateScope(this, Scope::TemplateParamScope,
725 HasTemplateScope);
726 if (HasTemplateScope)
727 Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate);
728
729 // Set or update the scope flags to include Scope::ThisScope.
730 bool AlreadyHasClassScope = Class.TopLevelClass;
731 unsigned ScopeFlags = Scope::ClassScope|Scope::DeclScope|Scope::ThisScope;
732 ParseScope ClassScope(this, ScopeFlags, !AlreadyHasClassScope);
733 ParseScopeFlags ClassScopeFlags(this, ScopeFlags, AlreadyHasClassScope);
734
735 for (unsigned i = 0, ni = Class.LateParsedDeclarations.size(); i < ni; ++i) {
736 Class.LateParsedDeclarations[i]->ParseLexedAttributes();
737 }
738}
739
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000740
741/// \brief Parse all attributes in LAs, and attach them to Decl D.
742void Parser::ParseLexedAttributeList(LateParsedAttrList &LAs, Decl *D,
743 bool EnterScope, bool OnDefinition) {
744 for (unsigned i = 0, ni = LAs.size(); i < ni; ++i) {
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +0000745 LAs[i]->addDecl(D);
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000746 ParseLexedAttribute(*LAs[i], EnterScope, OnDefinition);
747 }
748 LAs.clear();
749}
750
751
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000752/// \brief Finish parsing an attribute for which parsing was delayed.
753/// This will be called at the end of parsing a class declaration
754/// for each LateParsedAttribute. We consume the saved tokens and
755/// create an attribute with the arguments filled in. We add this
756/// to the Attribute list for the decl.
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000757void Parser::ParseLexedAttribute(LateParsedAttribute &LA,
758 bool EnterScope, bool OnDefinition) {
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000759 // Save the current token position.
760 SourceLocation OrigLoc = Tok.getLocation();
761
762 // Append the current token at the end of the new token stream so that it
763 // doesn't get lost.
764 LA.Toks.push_back(Tok);
765 PP.EnterTokenStream(LA.Toks.data(), LA.Toks.size(), true, false);
766 // Consume the previously pushed token.
767 ConsumeAnyToken();
768
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000769 if (OnDefinition && !IsThreadSafetyAttribute(LA.AttrName.getName())) {
770 Diag(Tok, diag::warn_attribute_on_function_definition)
771 << LA.AttrName.getName();
772 }
773
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000774 ParsedAttributes Attrs(AttrFactory);
775 SourceLocation endLoc;
776
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +0000777 if (LA.Decls.size() == 1) {
778 Decl *D = LA.Decls[0];
Caitlin Sadowskied9d84a2011-09-08 17:42:31 +0000779
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +0000780 // If the Decl is templatized, add template parameters to scope.
781 bool HasTemplateScope = EnterScope && D->isTemplateDecl();
782 ParseScope TempScope(this, Scope::TemplateParamScope, HasTemplateScope);
783 if (HasTemplateScope)
784 Actions.ActOnReenterTemplateScope(Actions.CurScope, D);
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000785
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +0000786 // If the Decl is on a function, add function parameters to the scope.
787 bool HasFunctionScope = EnterScope && D->isFunctionOrFunctionTemplate();
788 ParseScope FnScope(this, Scope::FnScope|Scope::DeclScope, HasFunctionScope);
789 if (HasFunctionScope)
790 Actions.ActOnReenterFunctionContext(Actions.CurScope, D);
791
792 ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, &endLoc);
793
794 if (HasFunctionScope) {
795 Actions.ActOnExitFunctionContext();
796 FnScope.Exit(); // Pop scope, and remove Decls from IdResolver
797 }
798 if (HasTemplateScope) {
799 TempScope.Exit();
800 }
DeLesley Hutchins7ec419a2012-03-02 22:29:50 +0000801 } else if (LA.Decls.size() > 0) {
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +0000802 // If there are multiple decls, then the decl cannot be within the
803 // function scope.
804 ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, &endLoc);
DeLesley Hutchins7ec419a2012-03-02 22:29:50 +0000805 } else {
806 Diag(Tok, diag::warn_attribute_no_decl) << LA.AttrName.getName();
Caitlin Sadowskied9d84a2011-09-08 17:42:31 +0000807 }
808
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +0000809 for (unsigned i = 0, ni = LA.Decls.size(); i < ni; ++i) {
810 Actions.ActOnFinishDelayedAttribute(getCurScope(), LA.Decls[i], Attrs);
811 }
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000812
813 if (Tok.getLocation() != OrigLoc) {
814 // Due to a parsing error, we either went over the cached tokens or
815 // there are still cached tokens left, so we skip the leftover tokens.
816 // Since this is an uncommon situation that should be avoided, use the
817 // expensive isBeforeInTranslationUnit call.
818 if (PP.getSourceManager().isBeforeInTranslationUnit(Tok.getLocation(),
819 OrigLoc))
820 while (Tok.getLocation() != OrigLoc && Tok.isNot(tok::eof))
Douglas Gregord78ef5b2012-03-08 01:00:17 +0000821 ConsumeAnyToken();
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000822 }
823}
824
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000825/// \brief Wrapper around a case statement checking if AttrName is
826/// one of the thread safety attributes
827bool Parser::IsThreadSafetyAttribute(llvm::StringRef AttrName){
828 return llvm::StringSwitch<bool>(AttrName)
829 .Case("guarded_by", true)
830 .Case("guarded_var", true)
831 .Case("pt_guarded_by", true)
832 .Case("pt_guarded_var", true)
833 .Case("lockable", true)
834 .Case("scoped_lockable", true)
835 .Case("no_thread_safety_analysis", true)
836 .Case("acquired_after", true)
837 .Case("acquired_before", true)
838 .Case("exclusive_lock_function", true)
839 .Case("shared_lock_function", true)
840 .Case("exclusive_trylock_function", true)
841 .Case("shared_trylock_function", true)
842 .Case("unlock_function", true)
843 .Case("lock_returned", true)
844 .Case("locks_excluded", true)
845 .Case("exclusive_locks_required", true)
846 .Case("shared_locks_required", true)
847 .Default(false);
848}
849
850/// \brief Parse the contents of thread safety attributes. These
851/// should always be parsed as an expression list.
852///
853/// We need to special case the parsing due to the fact that if the first token
854/// of the first argument is an identifier, the main parse loop will store
855/// that token as a "parameter" and the rest of
856/// the arguments will be added to a list of "arguments". However,
857/// subsequent tokens in the first argument are lost. We instead parse each
858/// argument as an expression and add all arguments to the list of "arguments".
859/// In future, we will take advantage of this special case to also
860/// deal with some argument scoping issues here (for example, referring to a
861/// function parameter in the attribute on that function).
862void Parser::ParseThreadSafetyAttribute(IdentifierInfo &AttrName,
863 SourceLocation AttrNameLoc,
864 ParsedAttributes &Attrs,
865 SourceLocation *EndLoc) {
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000866 assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('");
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000867
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000868 BalancedDelimiterTracker T(*this, tok::l_paren);
869 T.consumeOpen();
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000870
871 ExprVector ArgExprs(Actions);
872 bool ArgExprsOk = true;
873
874 // now parse the list of expressions
DeLesley Hutchins4805f152011-12-14 19:36:06 +0000875 while (Tok.isNot(tok::r_paren)) {
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000876 ExprResult ArgExpr(ParseAssignmentExpression());
877 if (ArgExpr.isInvalid()) {
878 ArgExprsOk = false;
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000879 T.consumeClose();
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000880 break;
881 } else {
882 ArgExprs.push_back(ArgExpr.release());
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000883 }
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000884 if (Tok.isNot(tok::comma))
885 break;
886 ConsumeToken(); // Eat the comma, move to the next argument
887 }
888 // Match the ')'.
DeLesley Hutchins23323e02012-01-20 22:50:54 +0000889 if (ArgExprsOk && !T.consumeClose()) {
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000890 Attrs.addNew(&AttrName, AttrNameLoc, 0, AttrNameLoc, 0, SourceLocation(),
891 ArgExprs.take(), ArgExprs.size());
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000892 }
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000893 if (EndLoc)
894 *EndLoc = T.getCloseLocation();
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000895}
896
John McCall7f040a92010-12-24 02:08:15 +0000897void Parser::DiagnoseProhibitedAttributes(ParsedAttributesWithRange &attrs) {
898 Diag(attrs.Range.getBegin(), diag::err_attributes_not_allowed)
899 << attrs.Range;
Dawn Perchik52fc3142010-09-03 01:29:35 +0000900}
901
Reid Spencer5f016e22007-07-11 17:01:13 +0000902/// ParseDeclaration - Parse a full 'declaration', which consists of
903/// declaration-specifiers, some number of declarators, and a semicolon.
Chris Lattner97144fc2009-04-02 04:16:50 +0000904/// 'Context' should be a Declarator::TheContext value. This returns the
905/// location of the semicolon in DeclEnd.
Chris Lattner8f08cb72007-08-25 06:57:03 +0000906///
907/// declaration: [C99 6.7]
908/// block-declaration ->
909/// simple-declaration
910/// others [FIXME]
Douglas Gregoradcac882008-12-01 23:54:00 +0000911/// [C++] template-declaration
Chris Lattner8f08cb72007-08-25 06:57:03 +0000912/// [C++] namespace-definition
Douglas Gregorf780abc2008-12-30 03:27:21 +0000913/// [C++] using-directive
Douglas Gregord7f37bf2009-06-22 23:06:13 +0000914/// [C++] using-declaration
Benjamin Kramerffbe9b92011-12-23 17:00:35 +0000915/// [C++0x/C11] static_assert-declaration
Chris Lattner8f08cb72007-08-25 06:57:03 +0000916/// others... [FIXME]
917///
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +0000918Parser::DeclGroupPtrTy Parser::ParseDeclaration(StmtVector &Stmts,
919 unsigned Context,
Sean Huntbbd37c62009-11-21 08:43:09 +0000920 SourceLocation &DeclEnd,
John McCall7f040a92010-12-24 02:08:15 +0000921 ParsedAttributesWithRange &attrs) {
Argyrios Kyrtzidis36d36802010-06-17 10:52:18 +0000922 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Fariborz Jahaniane8cff362011-08-30 17:10:52 +0000923 // Must temporarily exit the objective-c container scope for
924 // parsing c none objective-c decls.
925 ObjCDeclContextSwitch ObjCDC(*this);
Argyrios Kyrtzidis36d36802010-06-17 10:52:18 +0000926
John McCalld226f652010-08-21 09:40:31 +0000927 Decl *SingleDecl = 0;
Richard Smithc89edf52011-07-01 19:46:12 +0000928 Decl *OwnedType = 0;
Chris Lattner8f08cb72007-08-25 06:57:03 +0000929 switch (Tok.getKind()) {
Douglas Gregoradcac882008-12-01 23:54:00 +0000930 case tok::kw_template:
Douglas Gregor1426e532009-05-12 21:31:51 +0000931 case tok::kw_export:
John McCall7f040a92010-12-24 02:08:15 +0000932 ProhibitAttributes(attrs);
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000933 SingleDecl = ParseDeclarationStartingWithTemplate(Context, DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +0000934 break;
Sebastian Redld078e642010-08-27 23:12:46 +0000935 case tok::kw_inline:
Sebastian Redl88e64ca2010-08-31 00:36:45 +0000936 // Could be the start of an inline namespace. Allowed as an ext in C++03.
David Blaikie4e4d0842012-03-11 07:00:24 +0000937 if (getLangOpts().CPlusPlus && NextToken().is(tok::kw_namespace)) {
John McCall7f040a92010-12-24 02:08:15 +0000938 ProhibitAttributes(attrs);
Sebastian Redld078e642010-08-27 23:12:46 +0000939 SourceLocation InlineLoc = ConsumeToken();
940 SingleDecl = ParseNamespace(Context, DeclEnd, InlineLoc);
941 break;
942 }
John McCall7f040a92010-12-24 02:08:15 +0000943 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs,
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +0000944 true);
Chris Lattner8f08cb72007-08-25 06:57:03 +0000945 case tok::kw_namespace:
John McCall7f040a92010-12-24 02:08:15 +0000946 ProhibitAttributes(attrs);
Chris Lattner97144fc2009-04-02 04:16:50 +0000947 SingleDecl = ParseNamespace(Context, DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +0000948 break;
Douglas Gregorf780abc2008-12-30 03:27:21 +0000949 case tok::kw_using:
John McCall78b81052010-11-10 02:40:36 +0000950 SingleDecl = ParseUsingDirectiveOrDeclaration(Context, ParsedTemplateInfo(),
Richard Smithc89edf52011-07-01 19:46:12 +0000951 DeclEnd, attrs, &OwnedType);
Chris Lattner682bf922009-03-29 16:50:03 +0000952 break;
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000953 case tok::kw_static_assert:
Peter Collingbournec6eb44b2011-04-15 00:35:57 +0000954 case tok::kw__Static_assert:
John McCall7f040a92010-12-24 02:08:15 +0000955 ProhibitAttributes(attrs);
Chris Lattner97144fc2009-04-02 04:16:50 +0000956 SingleDecl = ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +0000957 break;
Chris Lattner8f08cb72007-08-25 06:57:03 +0000958 default:
John McCall7f040a92010-12-24 02:08:15 +0000959 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs, true);
Chris Lattner8f08cb72007-08-25 06:57:03 +0000960 }
Sean Huntbbd37c62009-11-21 08:43:09 +0000961
Chris Lattner682bf922009-03-29 16:50:03 +0000962 // This routine returns a DeclGroup, if the thing we parsed only contains a
Richard Smithc89edf52011-07-01 19:46:12 +0000963 // single decl, convert it now. Alias declarations can also declare a type;
964 // include that too if it is present.
965 return Actions.ConvertDeclToDeclGroup(SingleDecl, OwnedType);
Chris Lattner8f08cb72007-08-25 06:57:03 +0000966}
967
968/// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl]
969/// declaration-specifiers init-declarator-list[opt] ';'
970///[C90/C++]init-declarator-list ';' [TODO]
971/// [OMP] threadprivate-directive [TODO]
Chris Lattnercd147752009-03-29 17:27:48 +0000972///
Richard Smithad762fc2011-04-14 22:09:26 +0000973/// for-range-declaration: [C++0x 6.5p1: stmt.ranged]
974/// attribute-specifier-seq[opt] type-specifier-seq declarator
975///
Chris Lattnercd147752009-03-29 17:27:48 +0000976/// If RequireSemi is false, this does not check for a ';' at the end of the
Chris Lattner5c5db552010-04-05 18:18:31 +0000977/// declaration. If it is true, it checks for and eats it.
Richard Smithad762fc2011-04-14 22:09:26 +0000978///
979/// If FRI is non-null, we might be parsing a for-range-declaration instead
980/// of a simple-declaration. If we find that we are, we also parse the
981/// for-range-initializer, and place it here.
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +0000982Parser::DeclGroupPtrTy Parser::ParseSimpleDeclaration(StmtVector &Stmts,
983 unsigned Context,
Sean Huntbbd37c62009-11-21 08:43:09 +0000984 SourceLocation &DeclEnd,
John McCall7f040a92010-12-24 02:08:15 +0000985 ParsedAttributes &attrs,
Richard Smithad762fc2011-04-14 22:09:26 +0000986 bool RequireSemi,
987 ForRangeInit *FRI) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000988 // Parse the common declaration-specifiers piece.
John McCall54abf7d2009-11-04 02:18:39 +0000989 ParsingDeclSpec DS(*this);
John McCall7f040a92010-12-24 02:08:15 +0000990 DS.takeAttributesFrom(attrs);
Douglas Gregor312eadb2011-04-24 05:37:28 +0000991
Douglas Gregor0efc2c12010-01-13 17:31:36 +0000992 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS_none,
Richard Smith34b41d92011-02-20 03:19:35 +0000993 getDeclSpecContextFromDeclaratorContext(Context));
Abramo Bagnara06284c12012-01-07 10:52:36 +0000994
Reid Spencer5f016e22007-07-11 17:01:13 +0000995 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
996 // declaration-specifiers init-declarator-list[opt] ';'
Chris Lattner04d66662007-10-09 17:33:22 +0000997 if (Tok.is(tok::semi)) {
Chris Lattner5c5db552010-04-05 18:18:31 +0000998 if (RequireSemi) ConsumeToken();
John McCalld226f652010-08-21 09:40:31 +0000999 Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none,
Douglas Gregor312eadb2011-04-24 05:37:28 +00001000 DS);
John McCall54abf7d2009-11-04 02:18:39 +00001001 DS.complete(TheDecl);
Chris Lattner682bf922009-03-29 16:50:03 +00001002 return Actions.ConvertDeclToDeclGroup(TheDecl);
Reid Spencer5f016e22007-07-11 17:01:13 +00001003 }
Douglas Gregor312eadb2011-04-24 05:37:28 +00001004
1005 return ParseDeclGroup(DS, Context, /*FunctionDefs=*/ false, &DeclEnd, FRI);
John McCalld8ac0572009-11-03 19:26:08 +00001006}
Mike Stump1eb44332009-09-09 15:08:12 +00001007
Richard Smith0706df42011-10-19 21:33:05 +00001008/// Returns true if this might be the start of a declarator, or a common typo
1009/// for a declarator.
1010bool Parser::MightBeDeclarator(unsigned Context) {
1011 switch (Tok.getKind()) {
1012 case tok::annot_cxxscope:
1013 case tok::annot_template_id:
1014 case tok::caret:
1015 case tok::code_completion:
1016 case tok::coloncolon:
1017 case tok::ellipsis:
1018 case tok::kw___attribute:
1019 case tok::kw_operator:
1020 case tok::l_paren:
1021 case tok::star:
1022 return true;
1023
1024 case tok::amp:
1025 case tok::ampamp:
David Blaikie4e4d0842012-03-11 07:00:24 +00001026 return getLangOpts().CPlusPlus;
Richard Smith0706df42011-10-19 21:33:05 +00001027
Richard Smith1c94c162012-01-09 22:31:44 +00001028 case tok::l_square: // Might be an attribute on an unnamed bit-field.
David Blaikie4e4d0842012-03-11 07:00:24 +00001029 return Context == Declarator::MemberContext && getLangOpts().CPlusPlus0x &&
Richard Smith1c94c162012-01-09 22:31:44 +00001030 NextToken().is(tok::l_square);
1031
1032 case tok::colon: // Might be a typo for '::' or an unnamed bit-field.
David Blaikie4e4d0842012-03-11 07:00:24 +00001033 return Context == Declarator::MemberContext || getLangOpts().CPlusPlus;
Richard Smith1c94c162012-01-09 22:31:44 +00001034
Richard Smith0706df42011-10-19 21:33:05 +00001035 case tok::identifier:
1036 switch (NextToken().getKind()) {
1037 case tok::code_completion:
1038 case tok::coloncolon:
1039 case tok::comma:
1040 case tok::equal:
1041 case tok::equalequal: // Might be a typo for '='.
1042 case tok::kw_alignas:
1043 case tok::kw_asm:
1044 case tok::kw___attribute:
1045 case tok::l_brace:
1046 case tok::l_paren:
1047 case tok::l_square:
1048 case tok::less:
1049 case tok::r_brace:
1050 case tok::r_paren:
1051 case tok::r_square:
1052 case tok::semi:
1053 return true;
1054
1055 case tok::colon:
1056 // At namespace scope, 'identifier:' is probably a typo for 'identifier::'
Richard Smith1c94c162012-01-09 22:31:44 +00001057 // and in block scope it's probably a label. Inside a class definition,
1058 // this is a bit-field.
1059 return Context == Declarator::MemberContext ||
David Blaikie4e4d0842012-03-11 07:00:24 +00001060 (getLangOpts().CPlusPlus && Context == Declarator::FileContext);
Richard Smith1c94c162012-01-09 22:31:44 +00001061
1062 case tok::identifier: // Possible virt-specifier.
David Blaikie4e4d0842012-03-11 07:00:24 +00001063 return getLangOpts().CPlusPlus0x && isCXX0XVirtSpecifier(NextToken());
Richard Smith0706df42011-10-19 21:33:05 +00001064
1065 default:
1066 return false;
1067 }
1068
1069 default:
1070 return false;
1071 }
1072}
1073
John McCalld8ac0572009-11-03 19:26:08 +00001074/// ParseDeclGroup - Having concluded that this is either a function
1075/// definition or a group of object declarations, actually parse the
1076/// result.
John McCall54abf7d2009-11-04 02:18:39 +00001077Parser::DeclGroupPtrTy Parser::ParseDeclGroup(ParsingDeclSpec &DS,
1078 unsigned Context,
John McCalld8ac0572009-11-03 19:26:08 +00001079 bool AllowFunctionDefinitions,
Richard Smithad762fc2011-04-14 22:09:26 +00001080 SourceLocation *DeclEnd,
1081 ForRangeInit *FRI) {
John McCalld8ac0572009-11-03 19:26:08 +00001082 // Parse the first declarator.
John McCall54abf7d2009-11-04 02:18:39 +00001083 ParsingDeclarator D(*this, DS, static_cast<Declarator::TheContext>(Context));
John McCalld8ac0572009-11-03 19:26:08 +00001084 ParseDeclarator(D);
Chris Lattnercd147752009-03-29 17:27:48 +00001085
John McCalld8ac0572009-11-03 19:26:08 +00001086 // Bail out if the first declarator didn't seem well-formed.
1087 if (!D.hasName() && !D.mayOmitIdentifier()) {
1088 // Skip until ; or }.
1089 SkipUntil(tok::r_brace, true, true);
1090 if (Tok.is(tok::semi))
1091 ConsumeToken();
1092 return DeclGroupPtrTy();
Chris Lattner23c4b182009-03-29 17:18:04 +00001093 }
Mike Stump1eb44332009-09-09 15:08:12 +00001094
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001095 // Save late-parsed attributes for now; they need to be parsed in the
1096 // appropriate function scope after the function Decl has been constructed.
1097 LateParsedAttrList LateParsedAttrs;
1098 if (D.isFunctionDeclarator())
1099 MaybeParseGNUAttributes(D, &LateParsedAttrs);
1100
Chris Lattnerc82daef2010-07-11 22:24:20 +00001101 // Check to see if we have a function *definition* which must have a body.
1102 if (AllowFunctionDefinitions && D.isFunctionDeclarator() &&
1103 // Look at the next token to make sure that this isn't a function
1104 // declaration. We have to check this because __attribute__ might be the
1105 // start of a function definition in GCC-extended K&R C.
1106 !isDeclarationAfterDeclarator()) {
Richard Smith58196dc2011-11-30 23:45:35 +00001107
Chris Lattner004659a2010-07-11 22:42:07 +00001108 if (isStartOfFunctionDefinition(D)) {
John McCalld8ac0572009-11-03 19:26:08 +00001109 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
1110 Diag(Tok, diag::err_function_declared_typedef);
1111
1112 // Recover by treating the 'typedef' as spurious.
1113 DS.ClearStorageClassSpecs();
1114 }
1115
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001116 Decl *TheDecl =
1117 ParseFunctionDefinition(D, ParsedTemplateInfo(), &LateParsedAttrs);
John McCalld8ac0572009-11-03 19:26:08 +00001118 return Actions.ConvertDeclToDeclGroup(TheDecl);
Chris Lattner004659a2010-07-11 22:42:07 +00001119 }
1120
1121 if (isDeclarationSpecifier()) {
1122 // If there is an invalid declaration specifier right after the function
1123 // prototype, then we must be in a missing semicolon case where this isn't
1124 // actually a body. Just fall through into the code that handles it as a
1125 // prototype, and let the top-level code handle the erroneous declspec
1126 // where it would otherwise expect a comma or semicolon.
John McCalld8ac0572009-11-03 19:26:08 +00001127 } else {
1128 Diag(Tok, diag::err_expected_fn_body);
1129 SkipUntil(tok::semi);
1130 return DeclGroupPtrTy();
1131 }
1132 }
1133
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001134 if (ParseAsmAttributesAfterDeclarator(D))
Richard Smithad762fc2011-04-14 22:09:26 +00001135 return DeclGroupPtrTy();
1136
1137 // C++0x [stmt.iter]p1: Check if we have a for-range-declarator. If so, we
1138 // must parse and analyze the for-range-initializer before the declaration is
1139 // analyzed.
1140 if (FRI && Tok.is(tok::colon)) {
1141 FRI->ColonLoc = ConsumeToken();
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001142 if (Tok.is(tok::l_brace))
1143 FRI->RangeExpr = ParseBraceInitializer();
1144 else
1145 FRI->RangeExpr = ParseExpression();
Richard Smithad762fc2011-04-14 22:09:26 +00001146 Decl *ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
1147 Actions.ActOnCXXForRangeDecl(ThisDecl);
1148 Actions.FinalizeDeclaration(ThisDecl);
John McCall6895a642012-01-27 01:29:43 +00001149 D.complete(ThisDecl);
Richard Smithad762fc2011-04-14 22:09:26 +00001150 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS, &ThisDecl, 1);
1151 }
1152
Chris Lattner5f9e2722011-07-23 10:55:15 +00001153 SmallVector<Decl *, 8> DeclsInGroup;
Richard Smithad762fc2011-04-14 22:09:26 +00001154 Decl *FirstDecl = ParseDeclarationAfterDeclaratorAndAttributes(D);
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001155 if (LateParsedAttrs.size() > 0)
1156 ParseLexedAttributeList(LateParsedAttrs, FirstDecl, true, false);
John McCall54abf7d2009-11-04 02:18:39 +00001157 D.complete(FirstDecl);
John McCalld226f652010-08-21 09:40:31 +00001158 if (FirstDecl)
John McCalld8ac0572009-11-03 19:26:08 +00001159 DeclsInGroup.push_back(FirstDecl);
1160
Richard Smith0706df42011-10-19 21:33:05 +00001161 bool ExpectSemi = Context != Declarator::ForContext;
1162
John McCalld8ac0572009-11-03 19:26:08 +00001163 // If we don't have a comma, it is either the end of the list (a ';') or an
1164 // error, bail out.
1165 while (Tok.is(tok::comma)) {
Richard Smith0706df42011-10-19 21:33:05 +00001166 SourceLocation CommaLoc = ConsumeToken();
1167
1168 if (Tok.isAtStartOfLine() && ExpectSemi && !MightBeDeclarator(Context)) {
1169 // This comma was followed by a line-break and something which can't be
1170 // the start of a declarator. The comma was probably a typo for a
1171 // semicolon.
1172 Diag(CommaLoc, diag::err_expected_semi_declaration)
1173 << FixItHint::CreateReplacement(CommaLoc, ";");
1174 ExpectSemi = false;
1175 break;
1176 }
John McCalld8ac0572009-11-03 19:26:08 +00001177
1178 // Parse the next declarator.
1179 D.clear();
Richard Smith7984de32012-01-12 23:53:29 +00001180 D.setCommaLoc(CommaLoc);
John McCalld8ac0572009-11-03 19:26:08 +00001181
1182 // Accept attributes in an init-declarator. In the first declarator in a
1183 // declaration, these would be part of the declspec. In subsequent
1184 // declarators, they become part of the declarator itself, so that they
1185 // don't apply to declarators after *this* one. Examples:
1186 // short __attribute__((common)) var; -> declspec
1187 // short var __attribute__((common)); -> declarator
1188 // short x, __attribute__((common)) var; -> declarator
John McCall7f040a92010-12-24 02:08:15 +00001189 MaybeParseGNUAttributes(D);
John McCalld8ac0572009-11-03 19:26:08 +00001190
1191 ParseDeclarator(D);
Fariborz Jahanian9baf39d2012-01-13 00:14:12 +00001192 if (!D.isInvalidType()) {
1193 Decl *ThisDecl = ParseDeclarationAfterDeclarator(D);
1194 D.complete(ThisDecl);
1195 if (ThisDecl)
1196 DeclsInGroup.push_back(ThisDecl);
1197 }
John McCalld8ac0572009-11-03 19:26:08 +00001198 }
1199
1200 if (DeclEnd)
1201 *DeclEnd = Tok.getLocation();
1202
Richard Smith0706df42011-10-19 21:33:05 +00001203 if (ExpectSemi &&
John McCalld8ac0572009-11-03 19:26:08 +00001204 ExpectAndConsume(tok::semi,
1205 Context == Declarator::FileContext
1206 ? diag::err_invalid_token_after_toplevel_declarator
1207 : diag::err_expected_semi_declaration)) {
Chris Lattner004659a2010-07-11 22:42:07 +00001208 // Okay, there was no semicolon and one was expected. If we see a
1209 // declaration specifier, just assume it was missing and continue parsing.
1210 // Otherwise things are very confused and we skip to recover.
1211 if (!isDeclarationSpecifier()) {
1212 SkipUntil(tok::r_brace, true, true);
1213 if (Tok.is(tok::semi))
1214 ConsumeToken();
1215 }
John McCalld8ac0572009-11-03 19:26:08 +00001216 }
1217
Douglas Gregor23c94db2010-07-02 17:43:08 +00001218 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS,
John McCalld8ac0572009-11-03 19:26:08 +00001219 DeclsInGroup.data(),
1220 DeclsInGroup.size());
Reid Spencer5f016e22007-07-11 17:01:13 +00001221}
1222
Richard Smithad762fc2011-04-14 22:09:26 +00001223/// Parse an optional simple-asm-expr and attributes, and attach them to a
1224/// declarator. Returns true on an error.
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001225bool Parser::ParseAsmAttributesAfterDeclarator(Declarator &D) {
Richard Smithad762fc2011-04-14 22:09:26 +00001226 // If a simple-asm-expr is present, parse it.
1227 if (Tok.is(tok::kw_asm)) {
1228 SourceLocation Loc;
1229 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
1230 if (AsmLabel.isInvalid()) {
1231 SkipUntil(tok::semi, true, true);
1232 return true;
1233 }
1234
1235 D.setAsmLabel(AsmLabel.release());
1236 D.SetRangeEnd(Loc);
1237 }
1238
1239 MaybeParseGNUAttributes(D);
1240 return false;
1241}
1242
Douglas Gregor1426e532009-05-12 21:31:51 +00001243/// \brief Parse 'declaration' after parsing 'declaration-specifiers
1244/// declarator'. This method parses the remainder of the declaration
1245/// (including any attributes or initializer, among other things) and
1246/// finalizes the declaration.
Reid Spencer5f016e22007-07-11 17:01:13 +00001247///
Reid Spencer5f016e22007-07-11 17:01:13 +00001248/// init-declarator: [C99 6.7]
1249/// declarator
1250/// declarator '=' initializer
1251/// [GNU] declarator simple-asm-expr[opt] attributes[opt]
1252/// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00001253/// [C++] declarator initializer[opt]
1254///
1255/// [C++] initializer:
1256/// [C++] '=' initializer-clause
1257/// [C++] '(' expression-list ')'
Sebastian Redl50de12f2009-03-24 22:27:57 +00001258/// [C++0x] '=' 'default' [TODO]
1259/// [C++0x] '=' 'delete'
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001260/// [C++0x] braced-init-list
Sebastian Redl50de12f2009-03-24 22:27:57 +00001261///
1262/// According to the standard grammar, =default and =delete are function
1263/// definitions, but that definitely doesn't fit with the parser here.
Reid Spencer5f016e22007-07-11 17:01:13 +00001264///
John McCalld226f652010-08-21 09:40:31 +00001265Decl *Parser::ParseDeclarationAfterDeclarator(Declarator &D,
Douglas Gregore542c862009-06-23 23:11:28 +00001266 const ParsedTemplateInfo &TemplateInfo) {
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001267 if (ParseAsmAttributesAfterDeclarator(D))
Richard Smithad762fc2011-04-14 22:09:26 +00001268 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001269
Richard Smithad762fc2011-04-14 22:09:26 +00001270 return ParseDeclarationAfterDeclaratorAndAttributes(D, TemplateInfo);
1271}
Mike Stump1eb44332009-09-09 15:08:12 +00001272
Richard Smithad762fc2011-04-14 22:09:26 +00001273Decl *Parser::ParseDeclarationAfterDeclaratorAndAttributes(Declarator &D,
1274 const ParsedTemplateInfo &TemplateInfo) {
Douglas Gregor1426e532009-05-12 21:31:51 +00001275 // Inform the current actions module that we just parsed this declarator.
John McCalld226f652010-08-21 09:40:31 +00001276 Decl *ThisDecl = 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +00001277 switch (TemplateInfo.Kind) {
1278 case ParsedTemplateInfo::NonTemplate:
Douglas Gregor23c94db2010-07-02 17:43:08 +00001279 ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
Douglas Gregord5a423b2009-09-25 18:43:00 +00001280 break;
1281
1282 case ParsedTemplateInfo::Template:
1283 case ParsedTemplateInfo::ExplicitSpecialization:
Douglas Gregor23c94db2010-07-02 17:43:08 +00001284 ThisDecl = Actions.ActOnTemplateDeclarator(getCurScope(),
John McCallf312b1e2010-08-26 23:41:50 +00001285 MultiTemplateParamsArg(Actions,
Douglas Gregore542c862009-06-23 23:11:28 +00001286 TemplateInfo.TemplateParams->data(),
1287 TemplateInfo.TemplateParams->size()),
Douglas Gregord5a423b2009-09-25 18:43:00 +00001288 D);
1289 break;
1290
1291 case ParsedTemplateInfo::ExplicitInstantiation: {
John McCalld226f652010-08-21 09:40:31 +00001292 DeclResult ThisRes
Douglas Gregor23c94db2010-07-02 17:43:08 +00001293 = Actions.ActOnExplicitInstantiation(getCurScope(),
Douglas Gregord5a423b2009-09-25 18:43:00 +00001294 TemplateInfo.ExternLoc,
1295 TemplateInfo.TemplateLoc,
1296 D);
1297 if (ThisRes.isInvalid()) {
1298 SkipUntil(tok::semi, true, true);
John McCalld226f652010-08-21 09:40:31 +00001299 return 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +00001300 }
1301
1302 ThisDecl = ThisRes.get();
1303 break;
1304 }
1305 }
Mike Stump1eb44332009-09-09 15:08:12 +00001306
Richard Smith34b41d92011-02-20 03:19:35 +00001307 bool TypeContainsAuto =
1308 D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto;
1309
Douglas Gregor1426e532009-05-12 21:31:51 +00001310 // Parse declarator '=' initializer.
Richard Trieud6c7c672012-01-18 22:54:52 +00001311 // If a '==' or '+=' is found, suggest a fixit to '='.
Richard Trieufcaf27e2012-01-19 22:01:51 +00001312 if (isTokenEqualOrEqualTypo()) {
Douglas Gregor1426e532009-05-12 21:31:51 +00001313 ConsumeToken();
Anders Carlsson37bf9d22010-09-24 21:25:25 +00001314 if (Tok.is(tok::kw_delete)) {
Sean Hunte4246a62011-05-12 06:15:49 +00001315 if (D.isFunctionDeclarator())
1316 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
1317 << 1 /* delete */;
1318 else
1319 Diag(ConsumeToken(), diag::err_deleted_non_function);
Sean Huntfe2695e2011-05-06 01:42:00 +00001320 } else if (Tok.is(tok::kw_default)) {
Sean Hunte4246a62011-05-12 06:15:49 +00001321 if (D.isFunctionDeclarator())
Sebastian Redlecfcd562012-02-11 23:51:21 +00001322 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
1323 << 0 /* default */;
Sean Hunte4246a62011-05-12 06:15:49 +00001324 else
1325 Diag(ConsumeToken(), diag::err_default_special_members);
Douglas Gregor1426e532009-05-12 21:31:51 +00001326 } else {
David Blaikie4e4d0842012-03-11 07:00:24 +00001327 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
John McCall731ad842009-12-19 09:28:58 +00001328 EnterScope(0);
Douglas Gregor23c94db2010-07-02 17:43:08 +00001329 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
John McCall731ad842009-12-19 09:28:58 +00001330 }
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +00001331
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +00001332 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001333 Actions.CodeCompleteInitializer(getCurScope(), ThisDecl);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001334 cutOffParsing();
1335 return 0;
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +00001336 }
1337
John McCall60d7b3a2010-08-24 06:29:42 +00001338 ExprResult Init(ParseInitializer());
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +00001339
David Blaikie4e4d0842012-03-11 07:00:24 +00001340 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001341 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
John McCall731ad842009-12-19 09:28:58 +00001342 ExitScope();
1343 }
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +00001344
Douglas Gregor1426e532009-05-12 21:31:51 +00001345 if (Init.isInvalid()) {
Douglas Gregor00225542010-03-01 18:27:54 +00001346 SkipUntil(tok::comma, true, true);
1347 Actions.ActOnInitializerError(ThisDecl);
1348 } else
Richard Smith34b41d92011-02-20 03:19:35 +00001349 Actions.AddInitializerToDecl(ThisDecl, Init.take(),
1350 /*DirectInit=*/false, TypeContainsAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +00001351 }
1352 } else if (Tok.is(tok::l_paren)) {
1353 // Parse C++ direct initializer: '(' expression-list ')'
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001354 BalancedDelimiterTracker T(*this, tok::l_paren);
1355 T.consumeOpen();
1356
Douglas Gregor1426e532009-05-12 21:31:51 +00001357 ExprVector Exprs(Actions);
1358 CommaLocsTy CommaLocs;
1359
David Blaikie4e4d0842012-03-11 07:00:24 +00001360 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregorb4debae2009-12-22 17:47:17 +00001361 EnterScope(0);
Douglas Gregor23c94db2010-07-02 17:43:08 +00001362 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001363 }
1364
Douglas Gregor1426e532009-05-12 21:31:51 +00001365 if (ParseExpressionList(Exprs, CommaLocs)) {
1366 SkipUntil(tok::r_paren);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001367
David Blaikie4e4d0842012-03-11 07:00:24 +00001368 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001369 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001370 ExitScope();
1371 }
Douglas Gregor1426e532009-05-12 21:31:51 +00001372 } else {
1373 // Match the ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001374 T.consumeClose();
Douglas Gregor1426e532009-05-12 21:31:51 +00001375
1376 assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() &&
1377 "Unexpected number of commas!");
Douglas Gregorb4debae2009-12-22 17:47:17 +00001378
David Blaikie4e4d0842012-03-11 07:00:24 +00001379 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001380 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001381 ExitScope();
1382 }
1383
Sebastian Redl5b9cc5d2012-02-11 23:51:47 +00001384 ExprResult Initializer = Actions.ActOnParenListExpr(T.getOpenLocation(),
1385 T.getCloseLocation(),
1386 move_arg(Exprs));
1387 Actions.AddInitializerToDecl(ThisDecl, Initializer.take(),
1388 /*DirectInit=*/true, TypeContainsAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +00001389 }
David Blaikie4e4d0842012-03-11 07:00:24 +00001390 } else if (getLangOpts().CPlusPlus0x && Tok.is(tok::l_brace)) {
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001391 // Parse C++0x braced-init-list.
Richard Smith7fe62082011-10-15 05:09:34 +00001392 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
1393
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001394 if (D.getCXXScopeSpec().isSet()) {
1395 EnterScope(0);
1396 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
1397 }
1398
1399 ExprResult Init(ParseBraceInitializer());
1400
1401 if (D.getCXXScopeSpec().isSet()) {
1402 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
1403 ExitScope();
1404 }
1405
1406 if (Init.isInvalid()) {
1407 Actions.ActOnInitializerError(ThisDecl);
1408 } else
1409 Actions.AddInitializerToDecl(ThisDecl, Init.take(),
1410 /*DirectInit=*/true, TypeContainsAuto);
1411
Douglas Gregor1426e532009-05-12 21:31:51 +00001412 } else {
Richard Smith34b41d92011-02-20 03:19:35 +00001413 Actions.ActOnUninitializedDecl(ThisDecl, TypeContainsAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +00001414 }
1415
Richard Smith483b9f32011-02-21 20:05:19 +00001416 Actions.FinalizeDeclaration(ThisDecl);
1417
Douglas Gregor1426e532009-05-12 21:31:51 +00001418 return ThisDecl;
1419}
1420
Reid Spencer5f016e22007-07-11 17:01:13 +00001421/// ParseSpecifierQualifierList
1422/// specifier-qualifier-list:
1423/// type-specifier specifier-qualifier-list[opt]
1424/// type-qualifier specifier-qualifier-list[opt]
1425/// [GNU] attributes specifier-qualifier-list[opt]
1426///
Richard Smith69730c12012-03-12 07:56:15 +00001427void Parser::ParseSpecifierQualifierList(DeclSpec &DS, AccessSpecifier AS,
1428 DeclSpecContext DSC) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001429 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
1430 /// parse declaration-specifiers and complain about extra stuff.
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001431 /// TODO: diagnose attribute-specifiers and alignment-specifiers.
Richard Smith69730c12012-03-12 07:56:15 +00001432 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS, DSC);
Mike Stump1eb44332009-09-09 15:08:12 +00001433
Reid Spencer5f016e22007-07-11 17:01:13 +00001434 // Validate declspec for type-name.
1435 unsigned Specs = DS.getParsedSpecifiers();
Richard Smith69730c12012-03-12 07:56:15 +00001436 if (DSC == DSC_type_specifier && !DS.hasTypeSpecifier()) {
1437 Diag(Tok, diag::err_expected_type);
1438 DS.SetTypeSpecError();
1439 } else if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers() &&
1440 !DS.hasAttributes()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001441 Diag(Tok, diag::err_typename_requires_specqual);
Richard Smith69730c12012-03-12 07:56:15 +00001442 if (!DS.hasTypeSpecifier())
1443 DS.SetTypeSpecError();
1444 }
Mike Stump1eb44332009-09-09 15:08:12 +00001445
Reid Spencer5f016e22007-07-11 17:01:13 +00001446 // Issue diagnostic and remove storage class if present.
1447 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
1448 if (DS.getStorageClassSpecLoc().isValid())
1449 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
1450 else
1451 Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass);
1452 DS.ClearStorageClassSpecs();
1453 }
Mike Stump1eb44332009-09-09 15:08:12 +00001454
Reid Spencer5f016e22007-07-11 17:01:13 +00001455 // Issue diagnostic and remove function specfier if present.
1456 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Douglas Gregorb48fe382008-10-31 09:07:45 +00001457 if (DS.isInlineSpecified())
1458 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
1459 if (DS.isVirtualSpecified())
1460 Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec);
1461 if (DS.isExplicitSpecified())
1462 Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec);
Reid Spencer5f016e22007-07-11 17:01:13 +00001463 DS.ClearFunctionSpecs();
1464 }
Richard Smith69730c12012-03-12 07:56:15 +00001465
1466 // Issue diagnostic and remove constexpr specfier if present.
1467 if (DS.isConstexprSpecified()) {
1468 Diag(DS.getConstexprSpecLoc(), diag::err_typename_invalid_constexpr);
1469 DS.ClearConstexprSpec();
1470 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001471}
1472
Chris Lattnerc199ab32009-04-12 20:42:31 +00001473/// isValidAfterIdentifierInDeclaratorAfterDeclSpec - Return true if the
1474/// specified token is valid after the identifier in a declarator which
1475/// immediately follows the declspec. For example, these things are valid:
1476///
1477/// int x [ 4]; // direct-declarator
1478/// int x ( int y); // direct-declarator
1479/// int(int x ) // direct-declarator
1480/// int x ; // simple-declaration
1481/// int x = 17; // init-declarator-list
1482/// int x , y; // init-declarator-list
1483/// int x __asm__ ("foo"); // init-declarator-list
Chris Lattnerb6645dd2009-04-14 21:16:09 +00001484/// int x : 4; // struct-declarator
Chris Lattnerc83c27a2009-04-12 22:29:43 +00001485/// int x { 5}; // C++'0x unified initializers
Chris Lattnerc199ab32009-04-12 20:42:31 +00001486///
1487/// This is not, because 'x' does not immediately follow the declspec (though
1488/// ')' happens to be valid anyway).
1489/// int (x)
1490///
1491static bool isValidAfterIdentifierInDeclarator(const Token &T) {
1492 return T.is(tok::l_square) || T.is(tok::l_paren) || T.is(tok::r_paren) ||
1493 T.is(tok::semi) || T.is(tok::comma) || T.is(tok::equal) ||
Chris Lattnerb6645dd2009-04-14 21:16:09 +00001494 T.is(tok::kw_asm) || T.is(tok::l_brace) || T.is(tok::colon);
Chris Lattnerc199ab32009-04-12 20:42:31 +00001495}
1496
Chris Lattnere40c2952009-04-14 21:34:55 +00001497
1498/// ParseImplicitInt - This method is called when we have an non-typename
1499/// identifier in a declspec (which normally terminates the decl spec) when
1500/// the declspec has no type specifier. In this case, the declspec is either
1501/// malformed or is "implicit int" (in K&R and C89).
1502///
1503/// This method handles diagnosing this prettily and returns false if the
1504/// declspec is done being processed. If it recovers and thinks there may be
1505/// other pieces of declspec after it, it returns true.
1506///
Chris Lattnerf4382f52009-04-14 22:17:06 +00001507bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001508 const ParsedTemplateInfo &TemplateInfo,
Richard Smith69730c12012-03-12 07:56:15 +00001509 AccessSpecifier AS, DeclSpecContext DSC) {
Chris Lattnerf4382f52009-04-14 22:17:06 +00001510 assert(Tok.is(tok::identifier) && "should have identifier");
Mike Stump1eb44332009-09-09 15:08:12 +00001511
Chris Lattnere40c2952009-04-14 21:34:55 +00001512 SourceLocation Loc = Tok.getLocation();
1513 // If we see an identifier that is not a type name, we normally would
1514 // parse it as the identifer being declared. However, when a typename
1515 // is typo'd or the definition is not included, this will incorrectly
1516 // parse the typename as the identifier name and fall over misparsing
1517 // later parts of the diagnostic.
1518 //
1519 // As such, we try to do some look-ahead in cases where this would
1520 // otherwise be an "implicit-int" case to see if this is invalid. For
1521 // example: "static foo_t x = 4;" In this case, if we parsed foo_t as
1522 // an identifier with implicit int, we'd get a parse error because the
1523 // next token is obviously invalid for a type. Parse these as a case
1524 // with an invalid type specifier.
1525 assert(!DS.hasTypeSpecifier() && "Type specifier checked above");
Mike Stump1eb44332009-09-09 15:08:12 +00001526
Chris Lattnere40c2952009-04-14 21:34:55 +00001527 // Since we know that this either implicit int (which is rare) or an
Richard Smith69730c12012-03-12 07:56:15 +00001528 // error, do lookahead to try to do better recovery. This never applies within
1529 // a type specifier.
1530 // FIXME: Don't bail out here in languages with no implicit int (like
1531 // C++ with no -fms-extensions). This is much more likely to be an undeclared
1532 // type or typo than a use of implicit int.
1533 if (DSC != DSC_type_specifier &&
1534 isValidAfterIdentifierInDeclarator(NextToken())) {
Chris Lattnere40c2952009-04-14 21:34:55 +00001535 // If this token is valid for implicit int, e.g. "static x = 4", then
1536 // we just avoid eating the identifier, so it will be parsed as the
1537 // identifier in the declarator.
1538 return false;
1539 }
Mike Stump1eb44332009-09-09 15:08:12 +00001540
Chris Lattnere40c2952009-04-14 21:34:55 +00001541 // Otherwise, if we don't consume this token, we are going to emit an
1542 // error anyway. Try to recover from various common problems. Check
1543 // to see if this was a reference to a tag name without a tag specified.
1544 // This is a common problem in C (saying 'foo' instead of 'struct foo').
Chris Lattnerf4382f52009-04-14 22:17:06 +00001545 //
1546 // C++ doesn't need this, and isTagName doesn't take SS.
1547 if (SS == 0) {
Argyrios Kyrtzidisb8a9d3b2011-04-21 17:29:47 +00001548 const char *TagName = 0, *FixitTagName = 0;
Chris Lattnerf4382f52009-04-14 22:17:06 +00001549 tok::TokenKind TagKind = tok::unknown;
Mike Stump1eb44332009-09-09 15:08:12 +00001550
Douglas Gregor23c94db2010-07-02 17:43:08 +00001551 switch (Actions.isTagName(*Tok.getIdentifierInfo(), getCurScope())) {
Chris Lattnere40c2952009-04-14 21:34:55 +00001552 default: break;
Argyrios Kyrtzidisb8a9d3b2011-04-21 17:29:47 +00001553 case DeclSpec::TST_enum:
1554 TagName="enum" ; FixitTagName = "enum " ; TagKind=tok::kw_enum ;break;
1555 case DeclSpec::TST_union:
1556 TagName="union" ; FixitTagName = "union " ;TagKind=tok::kw_union ;break;
1557 case DeclSpec::TST_struct:
1558 TagName="struct"; FixitTagName = "struct ";TagKind=tok::kw_struct;break;
1559 case DeclSpec::TST_class:
1560 TagName="class" ; FixitTagName = "class " ;TagKind=tok::kw_class ;break;
Chris Lattnere40c2952009-04-14 21:34:55 +00001561 }
Mike Stump1eb44332009-09-09 15:08:12 +00001562
Chris Lattnerf4382f52009-04-14 22:17:06 +00001563 if (TagName) {
1564 Diag(Loc, diag::err_use_of_tag_name_without_tag)
David Blaikie4e4d0842012-03-11 07:00:24 +00001565 << Tok.getIdentifierInfo() << TagName << getLangOpts().CPlusPlus
Argyrios Kyrtzidisb8a9d3b2011-04-21 17:29:47 +00001566 << FixItHint::CreateInsertion(Tok.getLocation(),FixitTagName);
Mike Stump1eb44332009-09-09 15:08:12 +00001567
Chris Lattnerf4382f52009-04-14 22:17:06 +00001568 // Parse this as a tag as if the missing tag were present.
1569 if (TagKind == tok::kw_enum)
Richard Smith69730c12012-03-12 07:56:15 +00001570 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS, DSC_normal);
Chris Lattnerf4382f52009-04-14 22:17:06 +00001571 else
Richard Smith69730c12012-03-12 07:56:15 +00001572 ParseClassSpecifier(TagKind, Loc, DS, TemplateInfo, AS,
1573 /*EnteringContext*/ false, DSC_normal);
Chris Lattnerf4382f52009-04-14 22:17:06 +00001574 return true;
1575 }
Chris Lattnere40c2952009-04-14 21:34:55 +00001576 }
Mike Stump1eb44332009-09-09 15:08:12 +00001577
Douglas Gregora786fdb2009-10-13 23:27:22 +00001578 // This is almost certainly an invalid type name. Let the action emit a
1579 // diagnostic and attempt to recover.
John McCallb3d87482010-08-24 05:47:05 +00001580 ParsedType T;
Douglas Gregora786fdb2009-10-13 23:27:22 +00001581 if (Actions.DiagnoseUnknownTypeName(*Tok.getIdentifierInfo(), Loc,
Douglas Gregor23c94db2010-07-02 17:43:08 +00001582 getCurScope(), SS, T)) {
Douglas Gregora786fdb2009-10-13 23:27:22 +00001583 // The action emitted a diagnostic, so we don't have to.
1584 if (T) {
1585 // The action has suggested that the type T could be used. Set that as
1586 // the type in the declaration specifiers, consume the would-be type
1587 // name token, and we're done.
1588 const char *PrevSpec;
1589 unsigned DiagID;
John McCallb3d87482010-08-24 05:47:05 +00001590 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID, T);
Douglas Gregora786fdb2009-10-13 23:27:22 +00001591 DS.SetRangeEnd(Tok.getLocation());
1592 ConsumeToken();
1593
1594 // There may be other declaration specifiers after this.
1595 return true;
1596 }
1597
1598 // Fall through; the action had no suggestion for us.
1599 } else {
1600 // The action did not emit a diagnostic, so emit one now.
1601 SourceRange R;
1602 if (SS) R = SS->getRange();
1603 Diag(Loc, diag::err_unknown_typename) << Tok.getIdentifierInfo() << R;
1604 }
Mike Stump1eb44332009-09-09 15:08:12 +00001605
Douglas Gregora786fdb2009-10-13 23:27:22 +00001606 // Mark this as an error.
Richard Smith69730c12012-03-12 07:56:15 +00001607 DS.SetTypeSpecError();
Chris Lattnere40c2952009-04-14 21:34:55 +00001608 DS.SetRangeEnd(Tok.getLocation());
1609 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001610
Chris Lattnere40c2952009-04-14 21:34:55 +00001611 // TODO: Could inject an invalid typedef decl in an enclosing scope to
1612 // avoid rippling error messages on subsequent uses of the same type,
1613 // could be useful if #include was forgotten.
1614 return false;
1615}
1616
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001617/// \brief Determine the declaration specifier context from the declarator
1618/// context.
1619///
1620/// \param Context the declarator context, which is one of the
1621/// Declarator::TheContext enumerator values.
1622Parser::DeclSpecContext
1623Parser::getDeclSpecContextFromDeclaratorContext(unsigned Context) {
1624 if (Context == Declarator::MemberContext)
1625 return DSC_class;
1626 if (Context == Declarator::FileContext)
1627 return DSC_top_level;
1628 return DSC_normal;
1629}
1630
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001631/// ParseAlignArgument - Parse the argument to an alignment-specifier.
1632///
1633/// FIXME: Simply returns an alignof() expression if the argument is a
1634/// type. Ideally, the type should be propagated directly into Sema.
1635///
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00001636/// [C11] type-id
1637/// [C11] constant-expression
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00001638/// [C++0x] type-id ...[opt]
1639/// [C++0x] assignment-expression ...[opt]
1640ExprResult Parser::ParseAlignArgument(SourceLocation Start,
1641 SourceLocation &EllipsisLoc) {
1642 ExprResult ER;
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001643 if (isTypeIdInParens()) {
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001644 SourceLocation TypeLoc = Tok.getLocation();
1645 ParsedType Ty = ParseTypeName().get();
1646 SourceRange TypeRange(Start, Tok.getLocation());
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00001647 ER = Actions.ActOnUnaryExprOrTypeTraitExpr(TypeLoc, UETT_AlignOf, true,
1648 Ty.getAsOpaquePtr(), TypeRange);
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001649 } else
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00001650 ER = ParseConstantExpression();
1651
David Blaikie4e4d0842012-03-11 07:00:24 +00001652 if (getLangOpts().CPlusPlus0x && Tok.is(tok::ellipsis))
Peter Collingbournefe9b2a82011-10-24 17:56:00 +00001653 EllipsisLoc = ConsumeToken();
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00001654
1655 return ER;
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001656}
1657
1658/// ParseAlignmentSpecifier - Parse an alignment-specifier, and add the
1659/// attribute to Attrs.
1660///
1661/// alignment-specifier:
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00001662/// [C11] '_Alignas' '(' type-id ')'
1663/// [C11] '_Alignas' '(' constant-expression ')'
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00001664/// [C++0x] 'alignas' '(' type-id ...[opt] ')'
1665/// [C++0x] 'alignas' '(' assignment-expression ...[opt] ')'
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001666void Parser::ParseAlignmentSpecifier(ParsedAttributes &Attrs,
1667 SourceLocation *endLoc) {
1668 assert((Tok.is(tok::kw_alignas) || Tok.is(tok::kw__Alignas)) &&
1669 "Not an alignment-specifier!");
1670
1671 SourceLocation KWLoc = Tok.getLocation();
1672 ConsumeToken();
1673
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001674 BalancedDelimiterTracker T(*this, tok::l_paren);
1675 if (T.expectAndConsume(diag::err_expected_lparen))
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001676 return;
1677
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00001678 SourceLocation EllipsisLoc;
1679 ExprResult ArgExpr = ParseAlignArgument(T.getOpenLocation(), EllipsisLoc);
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001680 if (ArgExpr.isInvalid()) {
1681 SkipUntil(tok::r_paren);
1682 return;
1683 }
1684
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001685 T.consumeClose();
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001686 if (endLoc)
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001687 *endLoc = T.getCloseLocation();
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001688
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00001689 // FIXME: Handle pack-expansions here.
1690 if (EllipsisLoc.isValid()) {
1691 Diag(EllipsisLoc, diag::err_alignas_pack_exp_unsupported);
1692 return;
1693 }
1694
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001695 ExprVector ArgExprs(Actions);
1696 ArgExprs.push_back(ArgExpr.release());
1697 Attrs.addNew(PP.getIdentifierInfo("aligned"), KWLoc, 0, KWLoc,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001698 0, T.getOpenLocation(), ArgExprs.take(), 1, false, true);
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001699}
1700
Reid Spencer5f016e22007-07-11 17:01:13 +00001701/// ParseDeclarationSpecifiers
1702/// declaration-specifiers: [C99 6.7]
1703/// storage-class-specifier declaration-specifiers[opt]
1704/// type-specifier declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00001705/// [C99] function-specifier declaration-specifiers[opt]
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00001706/// [C11] alignment-specifier declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00001707/// [GNU] attributes declaration-specifiers[opt]
Douglas Gregor8d267c52011-09-09 02:06:17 +00001708/// [Clang] '__module_private__' declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00001709///
1710/// storage-class-specifier: [C99 6.7.1]
1711/// 'typedef'
1712/// 'extern'
1713/// 'static'
1714/// 'auto'
1715/// 'register'
Sebastian Redl669d5d72008-11-14 23:42:31 +00001716/// [C++] 'mutable'
Reid Spencer5f016e22007-07-11 17:01:13 +00001717/// [GNU] '__thread'
Reid Spencer5f016e22007-07-11 17:01:13 +00001718/// function-specifier: [C99 6.7.4]
1719/// [C99] 'inline'
Douglas Gregorb48fe382008-10-31 09:07:45 +00001720/// [C++] 'virtual'
1721/// [C++] 'explicit'
Peter Collingbournef315fa82011-02-14 01:42:53 +00001722/// [OpenCL] '__kernel'
Anders Carlssonf47f7a12009-05-06 04:46:28 +00001723/// 'friend': [C++ dcl.friend]
Sebastian Redl2ac67232009-11-05 15:47:02 +00001724/// 'constexpr': [C++0x dcl.constexpr]
Anders Carlssonf47f7a12009-05-06 04:46:28 +00001725
Reid Spencer5f016e22007-07-11 17:01:13 +00001726///
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +00001727void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001728 const ParsedTemplateInfo &TemplateInfo,
John McCall67d1a672009-08-06 02:15:43 +00001729 AccessSpecifier AS,
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +00001730 DeclSpecContext DSContext,
1731 LateParsedAttrList *LateAttrs) {
Douglas Gregor312eadb2011-04-24 05:37:28 +00001732 if (DS.getSourceRange().isInvalid()) {
1733 DS.SetRangeStart(Tok.getLocation());
1734 DS.SetRangeEnd(Tok.getLocation());
1735 }
1736
Douglas Gregorefaa93a2011-11-07 17:33:42 +00001737 bool EnteringContext = (DSContext == DSC_class || DSContext == DSC_top_level);
Reid Spencer5f016e22007-07-11 17:01:13 +00001738 while (1) {
John McCallfec54012009-08-03 20:12:06 +00001739 bool isInvalid = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001740 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00001741 unsigned DiagID = 0;
1742
Reid Spencer5f016e22007-07-11 17:01:13 +00001743 SourceLocation Loc = Tok.getLocation();
Douglas Gregor12e083c2008-11-07 15:42:26 +00001744
Reid Spencer5f016e22007-07-11 17:01:13 +00001745 switch (Tok.getKind()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001746 default:
Chris Lattnerbce61352008-07-26 00:20:22 +00001747 DoneWithDeclSpec:
Peter Collingbournef1907682011-09-29 18:03:57 +00001748 // [C++0x] decl-specifier-seq: decl-specifier attribute-specifier-seq[opt]
1749 MaybeParseCXX0XAttributes(DS.getAttributes());
1750
Reid Spencer5f016e22007-07-11 17:01:13 +00001751 // If this is not a declaration specifier token, we're done reading decl
1752 // specifiers. First verify that DeclSpec's are consistent.
Douglas Gregor9b3064b2009-04-01 22:41:11 +00001753 DS.Finish(Diags, PP);
Reid Spencer5f016e22007-07-11 17:01:13 +00001754 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001755
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001756 case tok::code_completion: {
John McCallf312b1e2010-08-26 23:41:50 +00001757 Sema::ParserCompletionContext CCC = Sema::PCC_Namespace;
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001758 if (DS.hasTypeSpecifier()) {
1759 bool AllowNonIdentifiers
1760 = (getCurScope()->getFlags() & (Scope::ControlScope |
1761 Scope::BlockScope |
1762 Scope::TemplateParamScope |
1763 Scope::FunctionPrototypeScope |
1764 Scope::AtCatchScope)) == 0;
1765 bool AllowNestedNameSpecifiers
1766 = DSContext == DSC_top_level ||
1767 (DSContext == DSC_class && DS.isFriendSpecified());
1768
Douglas Gregorc7b6d882010-09-16 15:14:18 +00001769 Actions.CodeCompleteDeclSpec(getCurScope(), DS,
1770 AllowNonIdentifiers,
1771 AllowNestedNameSpecifiers);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001772 return cutOffParsing();
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001773 }
1774
Douglas Gregor68e3c2e2011-02-15 20:33:25 +00001775 if (getCurScope()->getFnParent() || getCurScope()->getBlockParent())
1776 CCC = Sema::PCC_LocalDeclarationSpecifiers;
1777 else if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate)
John McCallf312b1e2010-08-26 23:41:50 +00001778 CCC = DSContext == DSC_class? Sema::PCC_MemberTemplate
1779 : Sema::PCC_Template;
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001780 else if (DSContext == DSC_class)
John McCallf312b1e2010-08-26 23:41:50 +00001781 CCC = Sema::PCC_Class;
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +00001782 else if (CurParsedObjCImpl)
John McCallf312b1e2010-08-26 23:41:50 +00001783 CCC = Sema::PCC_ObjCImplementation;
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001784
1785 Actions.CodeCompleteOrdinaryName(getCurScope(), CCC);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001786 return cutOffParsing();
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001787 }
1788
Chris Lattner5e02c472009-01-05 00:07:25 +00001789 case tok::coloncolon: // ::foo::bar
John McCall9ba61662010-02-26 08:45:28 +00001790 // C++ scope specifier. Annotate and loop, or bail out on error.
1791 if (TryAnnotateCXXScopeToken(true)) {
1792 if (!DS.hasTypeSpecifier())
1793 DS.SetTypeSpecError();
1794 goto DoneWithDeclSpec;
1795 }
John McCall2e0a7152010-03-01 18:20:46 +00001796 if (Tok.is(tok::coloncolon)) // ::new or ::delete
1797 goto DoneWithDeclSpec;
John McCall9ba61662010-02-26 08:45:28 +00001798 continue;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001799
1800 case tok::annot_cxxscope: {
1801 if (DS.hasTypeSpecifier())
1802 goto DoneWithDeclSpec;
1803
John McCallaa87d332009-12-12 11:40:51 +00001804 CXXScopeSpec SS;
Douglas Gregorc34348a2011-02-24 17:54:50 +00001805 Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(),
1806 Tok.getAnnotationRange(),
1807 SS);
John McCallaa87d332009-12-12 11:40:51 +00001808
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001809 // We are looking for a qualified typename.
Douglas Gregor9135c722009-03-25 15:40:00 +00001810 Token Next = NextToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001811 if (Next.is(tok::annot_template_id) &&
Douglas Gregor9135c722009-03-25 15:40:00 +00001812 static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue())
Douglas Gregorc45c2322009-03-31 00:43:58 +00001813 ->Kind == TNK_Type_template) {
Douglas Gregor9135c722009-03-25 15:40:00 +00001814 // We have a qualified template-id, e.g., N::A<int>
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001815
1816 // C++ [class.qual]p2:
1817 // In a lookup in which the constructor is an acceptable lookup
1818 // result and the nested-name-specifier nominates a class C:
1819 //
1820 // - if the name specified after the
1821 // nested-name-specifier, when looked up in C, is the
1822 // injected-class-name of C (Clause 9), or
1823 //
1824 // - if the name specified after the nested-name-specifier
1825 // is the same as the identifier or the
1826 // simple-template-id's template-name in the last
1827 // component of the nested-name-specifier,
1828 //
1829 // the name is instead considered to name the constructor of
1830 // class C.
1831 //
1832 // Thus, if the template-name is actually the constructor
1833 // name, then the code is ill-formed; this interpretation is
1834 // reinforced by the NAD status of core issue 635.
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +00001835 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Next);
John McCallba9d8532010-04-13 06:39:49 +00001836 if ((DSContext == DSC_top_level ||
1837 (DSContext == DSC_class && DS.isFriendSpecified())) &&
1838 TemplateId->Name &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00001839 Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) {
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001840 if (isConstructorDeclarator()) {
1841 // The user meant this to be an out-of-line constructor
1842 // definition, but template arguments are not allowed
1843 // there. Just allow this as a constructor; we'll
1844 // complain about it later.
1845 goto DoneWithDeclSpec;
1846 }
1847
1848 // The user meant this to name a type, but it actually names
1849 // a constructor with some extraneous template
1850 // arguments. Complain, then parse it as a type as the user
1851 // intended.
1852 Diag(TemplateId->TemplateNameLoc,
1853 diag::err_out_of_line_template_id_names_constructor)
1854 << TemplateId->Name;
1855 }
1856
John McCallaa87d332009-12-12 11:40:51 +00001857 DS.getTypeSpecScope() = SS;
1858 ConsumeToken(); // The C++ scope.
Mike Stump1eb44332009-09-09 15:08:12 +00001859 assert(Tok.is(tok::annot_template_id) &&
Douglas Gregor9135c722009-03-25 15:40:00 +00001860 "ParseOptionalCXXScopeSpecifier not working");
Douglas Gregor059101f2011-03-02 00:47:37 +00001861 AnnotateTemplateIdTokenAsType();
Douglas Gregor9135c722009-03-25 15:40:00 +00001862 continue;
1863 }
1864
Douglas Gregor9d7b3532009-09-28 07:26:33 +00001865 if (Next.is(tok::annot_typename)) {
John McCallaa87d332009-12-12 11:40:51 +00001866 DS.getTypeSpecScope() = SS;
1867 ConsumeToken(); // The C++ scope.
John McCallb3d87482010-08-24 05:47:05 +00001868 if (Tok.getAnnotationValue()) {
1869 ParsedType T = getTypeAnnotation(Tok);
Nico Weber253e80b2010-11-22 10:30:56 +00001870 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename,
1871 Tok.getAnnotationEndLoc(),
John McCallb3d87482010-08-24 05:47:05 +00001872 PrevSpec, DiagID, T);
1873 }
Douglas Gregor9d7b3532009-09-28 07:26:33 +00001874 else
1875 DS.SetTypeSpecError();
1876 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1877 ConsumeToken(); // The typename
1878 }
1879
Douglas Gregor9135c722009-03-25 15:40:00 +00001880 if (Next.isNot(tok::identifier))
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001881 goto DoneWithDeclSpec;
1882
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001883 // If we're in a context where the identifier could be a class name,
1884 // check whether this is a constructor declaration.
John McCallba9d8532010-04-13 06:39:49 +00001885 if ((DSContext == DSC_top_level ||
1886 (DSContext == DSC_class && DS.isFriendSpecified())) &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00001887 Actions.isCurrentClassName(*Next.getIdentifierInfo(), getCurScope(),
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001888 &SS)) {
1889 if (isConstructorDeclarator())
1890 goto DoneWithDeclSpec;
1891
1892 // As noted in C++ [class.qual]p2 (cited above), when the name
1893 // of the class is qualified in a context where it could name
1894 // a constructor, its a constructor name. However, we've
1895 // looked at the declarator, and the user probably meant this
1896 // to be a type. Complain that it isn't supposed to be treated
1897 // as a type, then proceed to parse it as a type.
1898 Diag(Next.getLocation(), diag::err_out_of_line_type_names_constructor)
1899 << Next.getIdentifierInfo();
1900 }
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001901
John McCallb3d87482010-08-24 05:47:05 +00001902 ParsedType TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(),
1903 Next.getLocation(),
Douglas Gregor9e876872011-03-01 18:12:44 +00001904 getCurScope(), &SS,
1905 false, false, ParsedType(),
Abramo Bagnarafad03b72012-01-27 08:46:19 +00001906 /*IsCtorOrDtorName=*/false,
Douglas Gregor9e876872011-03-01 18:12:44 +00001907 /*NonTrivialSourceInfo=*/true);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001908
Chris Lattnerf4382f52009-04-14 22:17:06 +00001909 // If the referenced identifier is not a type, then this declspec is
1910 // erroneous: We already checked about that it has no type specifier, and
1911 // C++ doesn't have implicit int. Diagnose it as a typo w.r.t. to the
Mike Stump1eb44332009-09-09 15:08:12 +00001912 // typename.
Chris Lattnerf4382f52009-04-14 22:17:06 +00001913 if (TypeRep == 0) {
1914 ConsumeToken(); // Eat the scope spec so the identifier is current.
Richard Smith69730c12012-03-12 07:56:15 +00001915 if (ParseImplicitInt(DS, &SS, TemplateInfo, AS, DSContext)) continue;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001916 goto DoneWithDeclSpec;
Chris Lattnerf4382f52009-04-14 22:17:06 +00001917 }
Mike Stump1eb44332009-09-09 15:08:12 +00001918
John McCallaa87d332009-12-12 11:40:51 +00001919 DS.getTypeSpecScope() = SS;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001920 ConsumeToken(); // The C++ scope.
1921
Douglas Gregor1a51b4a2009-02-09 15:09:02 +00001922 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001923 DiagID, TypeRep);
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001924 if (isInvalid)
1925 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001926
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001927 DS.SetRangeEnd(Tok.getLocation());
1928 ConsumeToken(); // The typename.
1929
1930 continue;
1931 }
Mike Stump1eb44332009-09-09 15:08:12 +00001932
Chris Lattner80d0c892009-01-21 19:48:37 +00001933 case tok::annot_typename: {
John McCallb3d87482010-08-24 05:47:05 +00001934 if (Tok.getAnnotationValue()) {
1935 ParsedType T = getTypeAnnotation(Tok);
Nico Weberc43271e2010-11-22 12:50:03 +00001936 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallb3d87482010-08-24 05:47:05 +00001937 DiagID, T);
1938 } else
Douglas Gregor31a19b62009-04-01 21:51:26 +00001939 DS.SetTypeSpecError();
Chris Lattner5c5db552010-04-05 18:18:31 +00001940
1941 if (isInvalid)
1942 break;
1943
Chris Lattner80d0c892009-01-21 19:48:37 +00001944 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1945 ConsumeToken(); // The typename
Mike Stump1eb44332009-09-09 15:08:12 +00001946
Chris Lattner80d0c892009-01-21 19:48:37 +00001947 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1948 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001949 // Objective-C interface.
David Blaikie4e4d0842012-03-11 07:00:24 +00001950 if (Tok.is(tok::less) && getLangOpts().ObjC1)
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001951 ParseObjCProtocolQualifiers(DS);
1952
Chris Lattner80d0c892009-01-21 19:48:37 +00001953 continue;
1954 }
Mike Stump1eb44332009-09-09 15:08:12 +00001955
Douglas Gregorbfad9152011-04-28 15:48:45 +00001956 case tok::kw___is_signed:
1957 // GNU libstdc++ 4.4 uses __is_signed as an identifier, but Clang
1958 // typically treats it as a trait. If we see __is_signed as it appears
1959 // in libstdc++, e.g.,
1960 //
1961 // static const bool __is_signed;
1962 //
1963 // then treat __is_signed as an identifier rather than as a keyword.
1964 if (DS.getTypeSpecType() == TST_bool &&
1965 DS.getTypeQualifiers() == DeclSpec::TQ_const &&
1966 DS.getStorageClassSpec() == DeclSpec::SCS_static) {
1967 Tok.getIdentifierInfo()->RevertTokenIDToIdentifier();
1968 Tok.setKind(tok::identifier);
1969 }
1970
1971 // We're done with the declaration-specifiers.
1972 goto DoneWithDeclSpec;
1973
Chris Lattner3bd934a2008-07-26 01:18:38 +00001974 // typedef-name
David Blaikie42d6d0c2011-12-04 05:04:18 +00001975 case tok::kw_decltype:
Chris Lattner3bd934a2008-07-26 01:18:38 +00001976 case tok::identifier: {
Chris Lattner5e02c472009-01-05 00:07:25 +00001977 // In C++, check to see if this is a scope specifier like foo::bar::, if
1978 // so handle it as such. This is important for ctor parsing.
David Blaikie4e4d0842012-03-11 07:00:24 +00001979 if (getLangOpts().CPlusPlus) {
John McCall9ba61662010-02-26 08:45:28 +00001980 if (TryAnnotateCXXScopeToken(true)) {
1981 if (!DS.hasTypeSpecifier())
1982 DS.SetTypeSpecError();
1983 goto DoneWithDeclSpec;
1984 }
1985 if (!Tok.is(tok::identifier))
1986 continue;
1987 }
Mike Stump1eb44332009-09-09 15:08:12 +00001988
Chris Lattner3bd934a2008-07-26 01:18:38 +00001989 // This identifier can only be a typedef name if we haven't already seen
1990 // a type-specifier. Without this check we misparse:
1991 // typedef int X; struct Y { short X; }; as 'short int'.
1992 if (DS.hasTypeSpecifier())
1993 goto DoneWithDeclSpec;
Mike Stump1eb44332009-09-09 15:08:12 +00001994
John Thompson82287d12010-02-05 00:12:22 +00001995 // Check for need to substitute AltiVec keyword tokens.
1996 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
1997 break;
1998
John McCallb3d87482010-08-24 05:47:05 +00001999 ParsedType TypeRep =
2000 Actions.getTypeName(*Tok.getIdentifierInfo(),
2001 Tok.getLocation(), getCurScope());
Douglas Gregor55f6b142009-02-09 18:46:07 +00002002
Chris Lattnerc199ab32009-04-12 20:42:31 +00002003 // If this is not a typedef name, don't parse it as part of the declspec,
2004 // it must be an implicit int or an error.
John McCallb3d87482010-08-24 05:47:05 +00002005 if (!TypeRep) {
Richard Smith69730c12012-03-12 07:56:15 +00002006 if (ParseImplicitInt(DS, 0, TemplateInfo, AS, DSContext)) continue;
Chris Lattner3bd934a2008-07-26 01:18:38 +00002007 goto DoneWithDeclSpec;
Chris Lattnerc199ab32009-04-12 20:42:31 +00002008 }
Douglas Gregor55f6b142009-02-09 18:46:07 +00002009
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002010 // If we're in a context where the identifier could be a class name,
2011 // check whether this is a constructor declaration.
David Blaikie4e4d0842012-03-11 07:00:24 +00002012 if (getLangOpts().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00002013 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), getCurScope()) &&
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002014 isConstructorDeclarator())
Douglas Gregorb48fe382008-10-31 09:07:45 +00002015 goto DoneWithDeclSpec;
2016
Douglas Gregor1a51b4a2009-02-09 15:09:02 +00002017 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00002018 DiagID, TypeRep);
Chris Lattner3bd934a2008-07-26 01:18:38 +00002019 if (isInvalid)
2020 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002021
Chris Lattner3bd934a2008-07-26 01:18:38 +00002022 DS.SetRangeEnd(Tok.getLocation());
2023 ConsumeToken(); // The identifier
2024
2025 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
2026 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00002027 // Objective-C interface.
David Blaikie4e4d0842012-03-11 07:00:24 +00002028 if (Tok.is(tok::less) && getLangOpts().ObjC1)
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00002029 ParseObjCProtocolQualifiers(DS);
2030
Steve Naroff4f9b9f12008-09-22 10:28:57 +00002031 // Need to support trailing type qualifiers (e.g. "id<p> const").
2032 // If a type specifier follows, it will be diagnosed elsewhere.
2033 continue;
Chris Lattner3bd934a2008-07-26 01:18:38 +00002034 }
Douglas Gregor39a8de12009-02-25 19:37:18 +00002035
2036 // type-name
2037 case tok::annot_template_id: {
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +00002038 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregorc45c2322009-03-31 00:43:58 +00002039 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor39a8de12009-02-25 19:37:18 +00002040 // This template-id does not refer to a type name, so we're
2041 // done with the type-specifiers.
2042 goto DoneWithDeclSpec;
2043 }
2044
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002045 // If we're in a context where the template-id could be a
2046 // constructor name or specialization, check whether this is a
2047 // constructor declaration.
David Blaikie4e4d0842012-03-11 07:00:24 +00002048 if (getLangOpts().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00002049 Actions.isCurrentClassName(*TemplateId->Name, getCurScope()) &&
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002050 isConstructorDeclarator())
2051 goto DoneWithDeclSpec;
2052
Douglas Gregor39a8de12009-02-25 19:37:18 +00002053 // Turn the template-id annotation token into a type annotation
2054 // token, then try again to parse it as a type-specifier.
Douglas Gregor31a19b62009-04-01 21:51:26 +00002055 AnnotateTemplateIdTokenAsType();
Douglas Gregor39a8de12009-02-25 19:37:18 +00002056 continue;
2057 }
2058
Reid Spencer5f016e22007-07-11 17:01:13 +00002059 // GNU attributes support.
2060 case tok::kw___attribute:
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +00002061 ParseGNUAttributes(DS.getAttributes(), 0, LateAttrs);
Reid Spencer5f016e22007-07-11 17:01:13 +00002062 continue;
Steve Narofff59e17e2008-12-24 20:59:21 +00002063
2064 // Microsoft declspec support.
2065 case tok::kw___declspec:
John McCall7f040a92010-12-24 02:08:15 +00002066 ParseMicrosoftDeclSpec(DS.getAttributes());
Steve Narofff59e17e2008-12-24 20:59:21 +00002067 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00002068
Steve Naroff239f0732008-12-25 14:16:32 +00002069 // Microsoft single token adornments.
Steve Naroff86bc6cf2008-12-25 14:41:26 +00002070 case tok::kw___forceinline:
Eli Friedman290eeb02009-06-08 23:27:34 +00002071 // FIXME: Add handling here!
2072 break;
2073
2074 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00002075 case tok::kw___ptr32:
Steve Naroff86bc6cf2008-12-25 14:41:26 +00002076 case tok::kw___w64:
Steve Naroff239f0732008-12-25 14:16:32 +00002077 case tok::kw___cdecl:
2078 case tok::kw___stdcall:
2079 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00002080 case tok::kw___thiscall:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00002081 case tok::kw___unaligned:
John McCall7f040a92010-12-24 02:08:15 +00002082 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman290eeb02009-06-08 23:27:34 +00002083 continue;
2084
Dawn Perchik52fc3142010-09-03 01:29:35 +00002085 // Borland single token adornments.
2086 case tok::kw___pascal:
John McCall7f040a92010-12-24 02:08:15 +00002087 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik52fc3142010-09-03 01:29:35 +00002088 continue;
2089
Peter Collingbournef315fa82011-02-14 01:42:53 +00002090 // OpenCL single token adornments.
2091 case tok::kw___kernel:
2092 ParseOpenCLAttributes(DS.getAttributes());
2093 continue;
2094
Reid Spencer5f016e22007-07-11 17:01:13 +00002095 // storage-class-specifier
2096 case tok::kw_typedef:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002097 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_typedef, Loc,
2098 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002099 break;
2100 case tok::kw_extern:
2101 if (DS.isThreadSpecified())
Chris Lattner1ab3b962008-11-18 07:48:38 +00002102 Diag(Tok, diag::ext_thread_before) << "extern";
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002103 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_extern, Loc,
2104 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002105 break;
Steve Naroff8d54bf22007-12-18 00:16:02 +00002106 case tok::kw___private_extern__:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002107 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_private_extern,
2108 Loc, PrevSpec, DiagID);
Steve Naroff8d54bf22007-12-18 00:16:02 +00002109 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00002110 case tok::kw_static:
2111 if (DS.isThreadSpecified())
Chris Lattner1ab3b962008-11-18 07:48:38 +00002112 Diag(Tok, diag::ext_thread_before) << "static";
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002113 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_static, Loc,
2114 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002115 break;
2116 case tok::kw_auto:
David Blaikie4e4d0842012-03-11 07:00:24 +00002117 if (getLangOpts().CPlusPlus0x) {
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002118 if (isKnownToBeTypeSpecifier(GetLookAheadToken(1))) {
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002119 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc,
2120 PrevSpec, DiagID);
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002121 if (!isInvalid)
Richard Smith8f4fb192011-09-04 19:54:14 +00002122 Diag(Tok, diag::ext_auto_storage_class)
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002123 << FixItHint::CreateRemoval(DS.getStorageClassSpecLoc());
Richard Smith8f4fb192011-09-04 19:54:14 +00002124 } else
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002125 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec,
2126 DiagID);
Richard Smith8f4fb192011-09-04 19:54:14 +00002127 } else
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002128 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc,
2129 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002130 break;
2131 case tok::kw_register:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002132 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_register, Loc,
2133 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002134 break;
Sebastian Redl669d5d72008-11-14 23:42:31 +00002135 case tok::kw_mutable:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002136 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_mutable, Loc,
2137 PrevSpec, DiagID);
Sebastian Redl669d5d72008-11-14 23:42:31 +00002138 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00002139 case tok::kw___thread:
John McCallfec54012009-08-03 20:12:06 +00002140 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002141 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002142
Reid Spencer5f016e22007-07-11 17:01:13 +00002143 // function-specifier
2144 case tok::kw_inline:
John McCallfec54012009-08-03 20:12:06 +00002145 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002146 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +00002147 case tok::kw_virtual:
John McCallfec54012009-08-03 20:12:06 +00002148 isInvalid = DS.SetFunctionSpecVirtual(Loc, PrevSpec, DiagID);
Douglas Gregorb48fe382008-10-31 09:07:45 +00002149 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +00002150 case tok::kw_explicit:
John McCallfec54012009-08-03 20:12:06 +00002151 isInvalid = DS.SetFunctionSpecExplicit(Loc, PrevSpec, DiagID);
Douglas Gregorb48fe382008-10-31 09:07:45 +00002152 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002153
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002154 // alignment-specifier
2155 case tok::kw__Alignas:
David Blaikie4e4d0842012-03-11 07:00:24 +00002156 if (!getLangOpts().C11)
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00002157 Diag(Tok, diag::ext_c11_alignas);
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002158 ParseAlignmentSpecifier(DS.getAttributes());
2159 continue;
2160
Anders Carlssonf47f7a12009-05-06 04:46:28 +00002161 // friend
2162 case tok::kw_friend:
John McCall67d1a672009-08-06 02:15:43 +00002163 if (DSContext == DSC_class)
2164 isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID);
2165 else {
2166 PrevSpec = ""; // not actually used by the diagnostic
2167 DiagID = diag::err_friend_invalid_in_context;
2168 isInvalid = true;
2169 }
Anders Carlssonf47f7a12009-05-06 04:46:28 +00002170 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002171
Douglas Gregor8d267c52011-09-09 02:06:17 +00002172 // Modules
2173 case tok::kw___module_private__:
2174 isInvalid = DS.setModulePrivateSpec(Loc, PrevSpec, DiagID);
2175 break;
2176
Sebastian Redl2ac67232009-11-05 15:47:02 +00002177 // constexpr
2178 case tok::kw_constexpr:
2179 isInvalid = DS.SetConstexprSpec(Loc, PrevSpec, DiagID);
2180 break;
2181
Chris Lattner80d0c892009-01-21 19:48:37 +00002182 // type-specifier
2183 case tok::kw_short:
John McCallfec54012009-08-03 20:12:06 +00002184 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec,
2185 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002186 break;
2187 case tok::kw_long:
2188 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCallfec54012009-08-03 20:12:06 +00002189 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
2190 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002191 else
John McCallfec54012009-08-03 20:12:06 +00002192 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2193 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002194 break;
Francois Pichet338d7f72011-04-28 01:59:37 +00002195 case tok::kw___int64:
2196 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2197 DiagID);
2198 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002199 case tok::kw_signed:
John McCallfec54012009-08-03 20:12:06 +00002200 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec,
2201 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002202 break;
2203 case tok::kw_unsigned:
John McCallfec54012009-08-03 20:12:06 +00002204 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
2205 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002206 break;
2207 case tok::kw__Complex:
John McCallfec54012009-08-03 20:12:06 +00002208 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
2209 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002210 break;
2211 case tok::kw__Imaginary:
John McCallfec54012009-08-03 20:12:06 +00002212 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
2213 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002214 break;
2215 case tok::kw_void:
John McCallfec54012009-08-03 20:12:06 +00002216 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec,
2217 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002218 break;
2219 case tok::kw_char:
John McCallfec54012009-08-03 20:12:06 +00002220 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec,
2221 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002222 break;
2223 case tok::kw_int:
John McCallfec54012009-08-03 20:12:06 +00002224 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec,
2225 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002226 break;
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00002227 case tok::kw_half:
2228 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_half, Loc, PrevSpec,
2229 DiagID);
2230 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002231 case tok::kw_float:
John McCallfec54012009-08-03 20:12:06 +00002232 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec,
2233 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002234 break;
2235 case tok::kw_double:
John McCallfec54012009-08-03 20:12:06 +00002236 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec,
2237 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002238 break;
2239 case tok::kw_wchar_t:
John McCallfec54012009-08-03 20:12:06 +00002240 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec,
2241 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002242 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002243 case tok::kw_char16_t:
John McCallfec54012009-08-03 20:12:06 +00002244 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec,
2245 DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002246 break;
2247 case tok::kw_char32_t:
John McCallfec54012009-08-03 20:12:06 +00002248 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec,
2249 DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002250 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002251 case tok::kw_bool:
2252 case tok::kw__Bool:
Argyrios Kyrtzidis4383e182010-11-16 18:18:13 +00002253 if (Tok.is(tok::kw_bool) &&
2254 DS.getTypeSpecType() != DeclSpec::TST_unspecified &&
2255 DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
2256 PrevSpec = ""; // Not used by the diagnostic.
2257 DiagID = diag::err_bool_redeclaration;
Fariborz Jahaniane106a0b2011-04-19 21:42:37 +00002258 // For better error recovery.
2259 Tok.setKind(tok::identifier);
Argyrios Kyrtzidis4383e182010-11-16 18:18:13 +00002260 isInvalid = true;
2261 } else {
2262 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec,
2263 DiagID);
2264 }
Chris Lattner80d0c892009-01-21 19:48:37 +00002265 break;
2266 case tok::kw__Decimal32:
John McCallfec54012009-08-03 20:12:06 +00002267 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
2268 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002269 break;
2270 case tok::kw__Decimal64:
John McCallfec54012009-08-03 20:12:06 +00002271 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
2272 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002273 break;
2274 case tok::kw__Decimal128:
John McCallfec54012009-08-03 20:12:06 +00002275 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
2276 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002277 break;
John Thompson82287d12010-02-05 00:12:22 +00002278 case tok::kw___vector:
2279 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
2280 break;
2281 case tok::kw___pixel:
2282 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
2283 break;
John McCalla5fc4722011-04-09 22:50:59 +00002284 case tok::kw___unknown_anytype:
2285 isInvalid = DS.SetTypeSpecType(TST_unknown_anytype, Loc,
2286 PrevSpec, DiagID);
2287 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002288
2289 // class-specifier:
2290 case tok::kw_class:
2291 case tok::kw_struct:
Chris Lattner4c97d762009-04-12 21:49:30 +00002292 case tok::kw_union: {
2293 tok::TokenKind Kind = Tok.getKind();
2294 ConsumeToken();
Richard Smith69730c12012-03-12 07:56:15 +00002295 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS,
2296 EnteringContext, DSContext);
Chris Lattner80d0c892009-01-21 19:48:37 +00002297 continue;
Chris Lattner4c97d762009-04-12 21:49:30 +00002298 }
Chris Lattner80d0c892009-01-21 19:48:37 +00002299
2300 // enum-specifier:
2301 case tok::kw_enum:
Chris Lattner4c97d762009-04-12 21:49:30 +00002302 ConsumeToken();
Richard Smith69730c12012-03-12 07:56:15 +00002303 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS, DSContext);
Chris Lattner80d0c892009-01-21 19:48:37 +00002304 continue;
2305
2306 // cv-qualifier:
2307 case tok::kw_const:
John McCallfec54012009-08-03 20:12:06 +00002308 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec, DiagID,
David Blaikie4e4d0842012-03-11 07:00:24 +00002309 getLangOpts());
Chris Lattner80d0c892009-01-21 19:48:37 +00002310 break;
2311 case tok::kw_volatile:
John McCallfec54012009-08-03 20:12:06 +00002312 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
David Blaikie4e4d0842012-03-11 07:00:24 +00002313 getLangOpts());
Chris Lattner80d0c892009-01-21 19:48:37 +00002314 break;
2315 case tok::kw_restrict:
John McCallfec54012009-08-03 20:12:06 +00002316 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
David Blaikie4e4d0842012-03-11 07:00:24 +00002317 getLangOpts());
Chris Lattner80d0c892009-01-21 19:48:37 +00002318 break;
2319
Douglas Gregord57959a2009-03-27 23:10:48 +00002320 // C++ typename-specifier:
2321 case tok::kw_typename:
John McCall9ba61662010-02-26 08:45:28 +00002322 if (TryAnnotateTypeOrScopeToken()) {
2323 DS.SetTypeSpecError();
2324 goto DoneWithDeclSpec;
2325 }
2326 if (!Tok.is(tok::kw_typename))
Douglas Gregord57959a2009-03-27 23:10:48 +00002327 continue;
2328 break;
2329
Chris Lattner80d0c892009-01-21 19:48:37 +00002330 // GNU typeof support.
2331 case tok::kw_typeof:
2332 ParseTypeofSpecifier(DS);
2333 continue;
2334
David Blaikie42d6d0c2011-12-04 05:04:18 +00002335 case tok::annot_decltype:
Anders Carlsson6fd634f2009-06-24 17:47:40 +00002336 ParseDecltypeSpecifier(DS);
2337 continue;
2338
Sean Huntdb5d44b2011-05-19 05:37:45 +00002339 case tok::kw___underlying_type:
2340 ParseUnderlyingTypeSpecifier(DS);
Eli Friedmanb001de72011-10-06 23:00:33 +00002341 continue;
2342
2343 case tok::kw__Atomic:
2344 ParseAtomicSpecifier(DS);
2345 continue;
Sean Huntdb5d44b2011-05-19 05:37:45 +00002346
Peter Collingbourne207f4d82011-03-18 22:38:29 +00002347 // OpenCL qualifiers:
2348 case tok::kw_private:
David Blaikie4e4d0842012-03-11 07:00:24 +00002349 if (!getLangOpts().OpenCL)
Peter Collingbourne207f4d82011-03-18 22:38:29 +00002350 goto DoneWithDeclSpec;
2351 case tok::kw___private:
2352 case tok::kw___global:
2353 case tok::kw___local:
2354 case tok::kw___constant:
2355 case tok::kw___read_only:
2356 case tok::kw___write_only:
2357 case tok::kw___read_write:
2358 ParseOpenCLQualifiers(DS);
2359 break;
2360
Steve Naroffd3ded1f2008-06-05 00:02:44 +00002361 case tok::less:
Chris Lattner3bd934a2008-07-26 01:18:38 +00002362 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattnerbce61352008-07-26 00:20:22 +00002363 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
2364 // but we support it.
David Blaikie4e4d0842012-03-11 07:00:24 +00002365 if (DS.hasTypeSpecifier() || !getLangOpts().ObjC1)
Chris Lattnerbce61352008-07-26 00:20:22 +00002366 goto DoneWithDeclSpec;
Mike Stump1eb44332009-09-09 15:08:12 +00002367
Douglas Gregor46f936e2010-11-19 17:10:50 +00002368 if (!ParseObjCProtocolQualifiers(DS))
2369 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
2370 << FixItHint::CreateInsertion(Loc, "id")
2371 << SourceRange(Loc, DS.getSourceRange().getEnd());
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00002372
2373 // Need to support trailing type qualifiers (e.g. "id<p> const").
2374 // If a type specifier follows, it will be diagnosed elsewhere.
2375 continue;
Reid Spencer5f016e22007-07-11 17:01:13 +00002376 }
John McCallfec54012009-08-03 20:12:06 +00002377 // If the specifier wasn't legal, issue a diagnostic.
Reid Spencer5f016e22007-07-11 17:01:13 +00002378 if (isInvalid) {
2379 assert(PrevSpec && "Method did not return previous specifier!");
John McCallfec54012009-08-03 20:12:06 +00002380 assert(DiagID);
Douglas Gregorae2fb142010-08-23 14:34:43 +00002381
2382 if (DiagID == diag::ext_duplicate_declspec)
2383 Diag(Tok, DiagID)
2384 << PrevSpec << FixItHint::CreateRemoval(Tok.getLocation());
2385 else
2386 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00002387 }
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002388
Chris Lattner81c018d2008-03-13 06:29:04 +00002389 DS.SetRangeEnd(Tok.getLocation());
Fariborz Jahaniane106a0b2011-04-19 21:42:37 +00002390 if (DiagID != diag::err_bool_redeclaration)
2391 ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00002392 }
2393}
Douglas Gregoradcac882008-12-01 23:54:00 +00002394
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002395/// ParseStructDeclaration - Parse a struct declaration without the terminating
2396/// semicolon.
2397///
Reid Spencer5f016e22007-07-11 17:01:13 +00002398/// struct-declaration:
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002399/// specifier-qualifier-list struct-declarator-list
Reid Spencer5f016e22007-07-11 17:01:13 +00002400/// [GNU] __extension__ struct-declaration
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002401/// [GNU] specifier-qualifier-list
Reid Spencer5f016e22007-07-11 17:01:13 +00002402/// struct-declarator-list:
2403/// struct-declarator
2404/// struct-declarator-list ',' struct-declarator
2405/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
2406/// struct-declarator:
2407/// declarator
2408/// [GNU] declarator attributes[opt]
2409/// declarator[opt] ':' constant-expression
2410/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
2411///
Chris Lattnere1359422008-04-10 06:46:29 +00002412void Parser::
John McCallbdd563e2009-11-03 02:38:08 +00002413ParseStructDeclaration(DeclSpec &DS, FieldCallback &Fields) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002414
Chris Lattnerc46d1a12008-10-20 06:45:43 +00002415 if (Tok.is(tok::kw___extension__)) {
2416 // __extension__ silences extension warnings in the subexpression.
2417 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroff28a7ca82007-08-20 22:28:22 +00002418 ConsumeToken();
Chris Lattnerc46d1a12008-10-20 06:45:43 +00002419 return ParseStructDeclaration(DS, Fields);
2420 }
Mike Stump1eb44332009-09-09 15:08:12 +00002421
Steve Naroff28a7ca82007-08-20 22:28:22 +00002422 // Parse the common specifier-qualifiers-list piece.
Steve Naroff28a7ca82007-08-20 22:28:22 +00002423 ParseSpecifierQualifierList(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00002424
Douglas Gregor4920f1f2009-01-12 22:49:06 +00002425 // If there are no declarators, this is a free-standing declaration
2426 // specifier. Let the actions module cope with it.
Chris Lattner04d66662007-10-09 17:33:22 +00002427 if (Tok.is(tok::semi)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00002428 Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none, DS);
Steve Naroff28a7ca82007-08-20 22:28:22 +00002429 return;
2430 }
2431
2432 // Read struct-declarators until we find the semicolon.
John McCallbdd563e2009-11-03 02:38:08 +00002433 bool FirstDeclarator = true;
Richard Smith7984de32012-01-12 23:53:29 +00002434 SourceLocation CommaLoc;
Steve Naroff28a7ca82007-08-20 22:28:22 +00002435 while (1) {
John McCall54abf7d2009-11-04 02:18:39 +00002436 ParsingDeclRAIIObject PD(*this);
John McCallbdd563e2009-11-03 02:38:08 +00002437 FieldDeclarator DeclaratorInfo(DS);
Richard Smith7984de32012-01-12 23:53:29 +00002438 DeclaratorInfo.D.setCommaLoc(CommaLoc);
John McCallbdd563e2009-11-03 02:38:08 +00002439
2440 // Attributes are only allowed here on successive declarators.
John McCall7f040a92010-12-24 02:08:15 +00002441 if (!FirstDeclarator)
2442 MaybeParseGNUAttributes(DeclaratorInfo.D);
Mike Stump1eb44332009-09-09 15:08:12 +00002443
Steve Naroff28a7ca82007-08-20 22:28:22 +00002444 /// struct-declarator: declarator
2445 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattnera1efc8c2009-12-10 01:59:24 +00002446 if (Tok.isNot(tok::colon)) {
2447 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
2448 ColonProtectionRAIIObject X(*this);
Chris Lattnere1359422008-04-10 06:46:29 +00002449 ParseDeclarator(DeclaratorInfo.D);
Chris Lattnera1efc8c2009-12-10 01:59:24 +00002450 }
Mike Stump1eb44332009-09-09 15:08:12 +00002451
Chris Lattner04d66662007-10-09 17:33:22 +00002452 if (Tok.is(tok::colon)) {
Steve Naroff28a7ca82007-08-20 22:28:22 +00002453 ConsumeToken();
John McCall60d7b3a2010-08-24 06:29:42 +00002454 ExprResult Res(ParseConstantExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002455 if (Res.isInvalid())
Steve Naroff28a7ca82007-08-20 22:28:22 +00002456 SkipUntil(tok::semi, true, true);
Chris Lattner60b1e3e2008-04-10 06:15:14 +00002457 else
Sebastian Redleffa8d12008-12-10 00:02:53 +00002458 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroff28a7ca82007-08-20 22:28:22 +00002459 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00002460
Steve Naroff28a7ca82007-08-20 22:28:22 +00002461 // If attributes exist after the declarator, parse them.
John McCall7f040a92010-12-24 02:08:15 +00002462 MaybeParseGNUAttributes(DeclaratorInfo.D);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002463
John McCallbdd563e2009-11-03 02:38:08 +00002464 // We're done with this declarator; invoke the callback.
John McCalld226f652010-08-21 09:40:31 +00002465 Decl *D = Fields.invoke(DeclaratorInfo);
John McCall54abf7d2009-11-04 02:18:39 +00002466 PD.complete(D);
John McCallbdd563e2009-11-03 02:38:08 +00002467
Steve Naroff28a7ca82007-08-20 22:28:22 +00002468 // If we don't have a comma, it is either the end of the list (a ';')
2469 // or an error, bail out.
Chris Lattner04d66662007-10-09 17:33:22 +00002470 if (Tok.isNot(tok::comma))
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002471 return;
Sebastian Redlab197ba2009-02-09 18:23:29 +00002472
Steve Naroff28a7ca82007-08-20 22:28:22 +00002473 // Consume the comma.
Richard Smith7984de32012-01-12 23:53:29 +00002474 CommaLoc = ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00002475
John McCallbdd563e2009-11-03 02:38:08 +00002476 FirstDeclarator = false;
Steve Naroff28a7ca82007-08-20 22:28:22 +00002477 }
Steve Naroff28a7ca82007-08-20 22:28:22 +00002478}
2479
2480/// ParseStructUnionBody
2481/// struct-contents:
2482/// struct-declaration-list
2483/// [EXT] empty
2484/// [GNU] "struct-declaration-list" without terminatoring ';'
2485/// struct-declaration-list:
2486/// struct-declaration
2487/// struct-declaration-list struct-declaration
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002488/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroff28a7ca82007-08-20 22:28:22 +00002489///
Reid Spencer5f016e22007-07-11 17:01:13 +00002490void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
John McCalld226f652010-08-21 09:40:31 +00002491 unsigned TagType, Decl *TagDecl) {
John McCallf312b1e2010-08-26 23:41:50 +00002492 PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc,
2493 "parsing struct/union body");
Mike Stump1eb44332009-09-09 15:08:12 +00002494
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002495 BalancedDelimiterTracker T(*this, tok::l_brace);
2496 if (T.consumeOpen())
2497 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002498
Douglas Gregor3218c4b2009-01-09 22:42:13 +00002499 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
Douglas Gregor23c94db2010-07-02 17:43:08 +00002500 Actions.ActOnTagStartDefinition(getCurScope(), TagDecl);
Douglas Gregor72de6672009-01-08 20:45:30 +00002501
Reid Spencer5f016e22007-07-11 17:01:13 +00002502 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
2503 // C++.
David Blaikie4e4d0842012-03-11 07:00:24 +00002504 if (Tok.is(tok::r_brace) && !getLangOpts().CPlusPlus) {
Richard Smithd7c56e12011-12-29 21:57:33 +00002505 Diag(Tok, diag::ext_empty_struct_union) << (TagType == TST_union);
2506 Diag(Tok, diag::warn_empty_struct_union_compat) << (TagType == TST_union);
2507 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002508
Chris Lattner5f9e2722011-07-23 10:55:15 +00002509 SmallVector<Decl *, 32> FieldDecls;
Chris Lattnere1359422008-04-10 06:46:29 +00002510
Reid Spencer5f016e22007-07-11 17:01:13 +00002511 // While we still have something to read, read the declarations in the struct.
Chris Lattner04d66662007-10-09 17:33:22 +00002512 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002513 // Each iteration of this loop reads one struct-declaration.
Mike Stump1eb44332009-09-09 15:08:12 +00002514
Reid Spencer5f016e22007-07-11 17:01:13 +00002515 // Check for extraneous top-level semicolon.
Chris Lattner04d66662007-10-09 17:33:22 +00002516 if (Tok.is(tok::semi)) {
Douglas Gregor9b3064b2009-04-01 22:41:11 +00002517 Diag(Tok, diag::ext_extra_struct_semi)
Douglas Gregorf13ca062010-06-16 23:08:59 +00002518 << DeclSpec::getSpecifierName((DeclSpec::TST)TagType)
Douglas Gregor849b2432010-03-31 17:46:05 +00002519 << FixItHint::CreateRemoval(Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00002520 ConsumeToken();
2521 continue;
2522 }
Chris Lattnere1359422008-04-10 06:46:29 +00002523
2524 // Parse all the comma separated declarators.
John McCall0b7e6782011-03-24 11:26:52 +00002525 DeclSpec DS(AttrFactory);
Mike Stump1eb44332009-09-09 15:08:12 +00002526
John McCallbdd563e2009-11-03 02:38:08 +00002527 if (!Tok.is(tok::at)) {
2528 struct CFieldCallback : FieldCallback {
2529 Parser &P;
John McCalld226f652010-08-21 09:40:31 +00002530 Decl *TagDecl;
Chris Lattner5f9e2722011-07-23 10:55:15 +00002531 SmallVectorImpl<Decl *> &FieldDecls;
John McCallbdd563e2009-11-03 02:38:08 +00002532
John McCalld226f652010-08-21 09:40:31 +00002533 CFieldCallback(Parser &P, Decl *TagDecl,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002534 SmallVectorImpl<Decl *> &FieldDecls) :
John McCallbdd563e2009-11-03 02:38:08 +00002535 P(P), TagDecl(TagDecl), FieldDecls(FieldDecls) {}
2536
John McCalld226f652010-08-21 09:40:31 +00002537 virtual Decl *invoke(FieldDeclarator &FD) {
John McCallbdd563e2009-11-03 02:38:08 +00002538 // Install the declarator into the current TagDecl.
John McCalld226f652010-08-21 09:40:31 +00002539 Decl *Field = P.Actions.ActOnField(P.getCurScope(), TagDecl,
John McCall4ba39712009-11-03 21:13:47 +00002540 FD.D.getDeclSpec().getSourceRange().getBegin(),
2541 FD.D, FD.BitfieldSize);
John McCallbdd563e2009-11-03 02:38:08 +00002542 FieldDecls.push_back(Field);
2543 return Field;
Douglas Gregor91a28862009-08-26 14:27:30 +00002544 }
John McCallbdd563e2009-11-03 02:38:08 +00002545 } Callback(*this, TagDecl, FieldDecls);
2546
2547 ParseStructDeclaration(DS, Callback);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002548 } else { // Handle @defs
2549 ConsumeToken();
2550 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
2551 Diag(Tok, diag::err_unexpected_at);
Chris Lattner3e156ad2010-02-02 00:37:27 +00002552 SkipUntil(tok::semi, true);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002553 continue;
2554 }
2555 ConsumeToken();
2556 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
2557 if (!Tok.is(tok::identifier)) {
2558 Diag(Tok, diag::err_expected_ident);
Chris Lattner3e156ad2010-02-02 00:37:27 +00002559 SkipUntil(tok::semi, true);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002560 continue;
2561 }
Chris Lattner5f9e2722011-07-23 10:55:15 +00002562 SmallVector<Decl *, 16> Fields;
Douglas Gregor23c94db2010-07-02 17:43:08 +00002563 Actions.ActOnDefs(getCurScope(), TagDecl, Tok.getLocation(),
Douglas Gregor44b43212008-12-11 16:49:14 +00002564 Tok.getIdentifierInfo(), Fields);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002565 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
2566 ConsumeToken();
2567 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
Mike Stump1eb44332009-09-09 15:08:12 +00002568 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002569
Chris Lattner04d66662007-10-09 17:33:22 +00002570 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002571 ConsumeToken();
Chris Lattner04d66662007-10-09 17:33:22 +00002572 } else if (Tok.is(tok::r_brace)) {
Chris Lattner3e156ad2010-02-02 00:37:27 +00002573 ExpectAndConsume(tok::semi, diag::ext_expected_semi_decl_list);
Reid Spencer5f016e22007-07-11 17:01:13 +00002574 break;
2575 } else {
Chris Lattner3e156ad2010-02-02 00:37:27 +00002576 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
2577 // Skip to end of block or statement to avoid ext-warning on extra ';'.
Reid Spencer5f016e22007-07-11 17:01:13 +00002578 SkipUntil(tok::r_brace, true, true);
Chris Lattner3e156ad2010-02-02 00:37:27 +00002579 // If we stopped at a ';', eat it.
2580 if (Tok.is(tok::semi)) ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00002581 }
2582 }
Mike Stump1eb44332009-09-09 15:08:12 +00002583
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002584 T.consumeClose();
Mike Stump1eb44332009-09-09 15:08:12 +00002585
John McCall0b7e6782011-03-24 11:26:52 +00002586 ParsedAttributes attrs(AttrFactory);
Reid Spencer5f016e22007-07-11 17:01:13 +00002587 // If attributes exist after struct contents, parse them.
John McCall7f040a92010-12-24 02:08:15 +00002588 MaybeParseGNUAttributes(attrs);
Daniel Dunbar1bfe1c22008-10-03 02:03:53 +00002589
Douglas Gregor23c94db2010-07-02 17:43:08 +00002590 Actions.ActOnFields(getCurScope(),
David Blaikie77b6de02011-09-22 02:58:26 +00002591 RecordLoc, TagDecl, FieldDecls,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002592 T.getOpenLocation(), T.getCloseLocation(),
John McCall7f040a92010-12-24 02:08:15 +00002593 attrs.getList());
Douglas Gregor72de6672009-01-08 20:45:30 +00002594 StructScope.Exit();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002595 Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl,
2596 T.getCloseLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00002597}
2598
Reid Spencer5f016e22007-07-11 17:01:13 +00002599/// ParseEnumSpecifier
2600/// enum-specifier: [C99 6.7.2.2]
2601/// 'enum' identifier[opt] '{' enumerator-list '}'
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002602///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Reid Spencer5f016e22007-07-11 17:01:13 +00002603/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
2604/// '}' attributes[opt]
Aaron Ballman6454a022012-03-01 04:09:28 +00002605/// [MS] 'enum' __declspec[opt] identifier[opt] '{' enumerator-list ',' [opt]
2606/// '}'
Reid Spencer5f016e22007-07-11 17:01:13 +00002607/// 'enum' identifier
2608/// [GNU] 'enum' attributes[opt] identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002609///
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002610/// [C++0x] enum-head '{' enumerator-list[opt] '}'
2611/// [C++0x] enum-head '{' enumerator-list ',' '}'
2612///
2613/// enum-head: [C++0x]
2614/// enum-key attributes[opt] identifier[opt] enum-base[opt]
2615/// enum-key attributes[opt] nested-name-specifier identifier enum-base[opt]
2616///
2617/// enum-key: [C++0x]
2618/// 'enum'
2619/// 'enum' 'class'
2620/// 'enum' 'struct'
2621///
2622/// enum-base: [C++0x]
2623/// ':' type-specifier-seq
2624///
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002625/// [C++] elaborated-type-specifier:
2626/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
2627///
Chris Lattner4c97d762009-04-12 21:49:30 +00002628void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregor9b9edd62010-03-02 17:53:14 +00002629 const ParsedTemplateInfo &TemplateInfo,
Richard Smith69730c12012-03-12 07:56:15 +00002630 AccessSpecifier AS, DeclSpecContext DSC) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002631 // Parse the tag portion of this.
Douglas Gregor374929f2009-09-18 15:37:17 +00002632 if (Tok.is(tok::code_completion)) {
2633 // Code completion for an enum name.
Douglas Gregor23c94db2010-07-02 17:43:08 +00002634 Actions.CodeCompleteTag(getCurScope(), DeclSpec::TST_enum);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002635 return cutOffParsing();
Douglas Gregor374929f2009-09-18 15:37:17 +00002636 }
John McCall57c13002011-07-06 05:58:41 +00002637
Richard Smithbdad7a22012-01-10 01:33:14 +00002638 SourceLocation ScopedEnumKWLoc;
John McCall57c13002011-07-06 05:58:41 +00002639 bool IsScopedUsingClassTag = false;
2640
David Blaikie4e4d0842012-03-11 07:00:24 +00002641 if (getLangOpts().CPlusPlus0x &&
John McCall57c13002011-07-06 05:58:41 +00002642 (Tok.is(tok::kw_class) || Tok.is(tok::kw_struct))) {
Richard Smith7fe62082011-10-15 05:09:34 +00002643 Diag(Tok, diag::warn_cxx98_compat_scoped_enum);
John McCall57c13002011-07-06 05:58:41 +00002644 IsScopedUsingClassTag = Tok.is(tok::kw_class);
Richard Smithbdad7a22012-01-10 01:33:14 +00002645 ScopedEnumKWLoc = ConsumeToken();
John McCall57c13002011-07-06 05:58:41 +00002646 }
Douglas Gregor374929f2009-09-18 15:37:17 +00002647
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002648 // If attributes exist after tag, parse them.
John McCall0b7e6782011-03-24 11:26:52 +00002649 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00002650 MaybeParseGNUAttributes(attrs);
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002651
Aaron Ballman6454a022012-03-01 04:09:28 +00002652 // If declspecs exist after tag, parse them.
2653 while (Tok.is(tok::kw___declspec))
2654 ParseMicrosoftDeclSpec(attrs);
2655
Douglas Gregor5471bc82011-09-08 17:18:35 +00002656 bool AllowFixedUnderlyingType
David Blaikie4e4d0842012-03-11 07:00:24 +00002657 = getLangOpts().CPlusPlus0x || getLangOpts().MicrosoftExt || getLangOpts().ObjC2;
John McCall57c13002011-07-06 05:58:41 +00002658
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00002659 CXXScopeSpec &SS = DS.getTypeSpecScope();
David Blaikie4e4d0842012-03-11 07:00:24 +00002660 if (getLangOpts().CPlusPlus) {
John McCall57c13002011-07-06 05:58:41 +00002661 // "enum foo : bar;" is not a potential typo for "enum foo::bar;"
2662 // if a fixed underlying type is allowed.
2663 ColonProtectionRAIIObject X(*this, AllowFixedUnderlyingType);
2664
Douglas Gregorefaa93a2011-11-07 17:33:42 +00002665 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
2666 /*EnteringContext=*/false))
John McCall9ba61662010-02-26 08:45:28 +00002667 return;
2668
2669 if (SS.isSet() && Tok.isNot(tok::identifier)) {
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002670 Diag(Tok, diag::err_expected_ident);
2671 if (Tok.isNot(tok::l_brace)) {
2672 // Has no name and is not a definition.
2673 // Skip the rest of this declarator, up until the comma or semicolon.
2674 SkipUntil(tok::comma, true);
2675 return;
2676 }
2677 }
2678 }
Mike Stump1eb44332009-09-09 15:08:12 +00002679
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002680 // Must have either 'enum name' or 'enum {...}'.
Douglas Gregorb9075602011-02-22 02:55:24 +00002681 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace) &&
2682 (AllowFixedUnderlyingType && Tok.isNot(tok::colon))) {
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002683 Diag(Tok, diag::err_expected_ident_lbrace);
Mike Stump1eb44332009-09-09 15:08:12 +00002684
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002685 // Skip the rest of this declarator, up until the comma or semicolon.
2686 SkipUntil(tok::comma, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00002687 return;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002688 }
Mike Stump1eb44332009-09-09 15:08:12 +00002689
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002690 // If an identifier is present, consume and remember it.
2691 IdentifierInfo *Name = 0;
2692 SourceLocation NameLoc;
2693 if (Tok.is(tok::identifier)) {
2694 Name = Tok.getIdentifierInfo();
2695 NameLoc = ConsumeToken();
2696 }
Mike Stump1eb44332009-09-09 15:08:12 +00002697
Richard Smithbdad7a22012-01-10 01:33:14 +00002698 if (!Name && ScopedEnumKWLoc.isValid()) {
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002699 // C++0x 7.2p2: The optional identifier shall not be omitted in the
2700 // declaration of a scoped enumeration.
2701 Diag(Tok, diag::err_scoped_enum_missing_identifier);
Richard Smithbdad7a22012-01-10 01:33:14 +00002702 ScopedEnumKWLoc = SourceLocation();
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00002703 IsScopedUsingClassTag = false;
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002704 }
2705
2706 TypeResult BaseType;
2707
Douglas Gregora61b3e72010-12-01 17:42:47 +00002708 // Parse the fixed underlying type.
Douglas Gregorb9075602011-02-22 02:55:24 +00002709 if (AllowFixedUnderlyingType && Tok.is(tok::colon)) {
Douglas Gregora61b3e72010-12-01 17:42:47 +00002710 bool PossibleBitfield = false;
2711 if (getCurScope()->getFlags() & Scope::ClassScope) {
2712 // If we're in class scope, this can either be an enum declaration with
2713 // an underlying type, or a declaration of a bitfield member. We try to
2714 // use a simple disambiguation scheme first to catch the common cases
2715 // (integer literal, sizeof); if it's still ambiguous, we then consider
2716 // anything that's a simple-type-specifier followed by '(' as an
2717 // expression. This suffices because function types are not valid
2718 // underlying types anyway.
2719 TPResult TPR = isExpressionOrTypeSpecifierSimple(NextToken().getKind());
2720 // If the next token starts an expression, we know we're parsing a
2721 // bit-field. This is the common case.
2722 if (TPR == TPResult::True())
2723 PossibleBitfield = true;
2724 // If the next token starts a type-specifier-seq, it may be either a
2725 // a fixed underlying type or the start of a function-style cast in C++;
2726 // lookahead one more token to see if it's obvious that we have a
2727 // fixed underlying type.
2728 else if (TPR == TPResult::False() &&
2729 GetLookAheadToken(2).getKind() == tok::semi) {
2730 // Consume the ':'.
2731 ConsumeToken();
2732 } else {
2733 // We have the start of a type-specifier-seq, so we have to perform
2734 // tentative parsing to determine whether we have an expression or a
2735 // type.
2736 TentativeParsingAction TPA(*this);
2737
2738 // Consume the ':'.
2739 ConsumeToken();
Richard Smithd81e9612012-02-23 01:36:12 +00002740
2741 // If we see a type specifier followed by an open-brace, we have an
2742 // ambiguity between an underlying type and a C++11 braced
2743 // function-style cast. Resolve this by always treating it as an
2744 // underlying type.
2745 // FIXME: The standard is not entirely clear on how to disambiguate in
2746 // this case.
David Blaikie4e4d0842012-03-11 07:00:24 +00002747 if ((getLangOpts().CPlusPlus &&
Richard Smithd81e9612012-02-23 01:36:12 +00002748 isCXXDeclarationSpecifier(TPResult::True()) != TPResult::True()) ||
David Blaikie4e4d0842012-03-11 07:00:24 +00002749 (!getLangOpts().CPlusPlus && !isDeclarationSpecifier(true))) {
Douglas Gregora61b3e72010-12-01 17:42:47 +00002750 // We'll parse this as a bitfield later.
2751 PossibleBitfield = true;
2752 TPA.Revert();
2753 } else {
2754 // We have a type-specifier-seq.
2755 TPA.Commit();
2756 }
2757 }
2758 } else {
2759 // Consume the ':'.
2760 ConsumeToken();
2761 }
2762
2763 if (!PossibleBitfield) {
2764 SourceRange Range;
2765 BaseType = ParseTypeName(&Range);
Douglas Gregor86f208c2011-02-22 20:32:04 +00002766
David Blaikie4e4d0842012-03-11 07:00:24 +00002767 if (!getLangOpts().CPlusPlus0x && !getLangOpts().ObjC2)
Douglas Gregor86f208c2011-02-22 20:32:04 +00002768 Diag(StartLoc, diag::ext_ms_enum_fixed_underlying_type)
2769 << Range;
David Blaikie4e4d0842012-03-11 07:00:24 +00002770 if (getLangOpts().CPlusPlus0x)
Richard Smith7fe62082011-10-15 05:09:34 +00002771 Diag(StartLoc, diag::warn_cxx98_compat_enum_fixed_underlying_type);
Douglas Gregora61b3e72010-12-01 17:42:47 +00002772 }
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002773 }
2774
Richard Smithbdad7a22012-01-10 01:33:14 +00002775 // There are four options here. If we have 'friend enum foo;' then this is a
2776 // friend declaration, and cannot have an accompanying definition. If we have
2777 // 'enum foo;', then this is a forward declaration. If we have
2778 // 'enum foo {...' then this is a definition. Otherwise we have something
2779 // like 'enum foo xyz', a reference.
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002780 //
2781 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
2782 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
2783 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
2784 //
John McCallf312b1e2010-08-26 23:41:50 +00002785 Sema::TagUseKind TUK;
Richard Smithbdad7a22012-01-10 01:33:14 +00002786 if (DS.isFriendSpecified())
2787 TUK = Sema::TUK_Friend;
2788 else if (Tok.is(tok::l_brace))
John McCallf312b1e2010-08-26 23:41:50 +00002789 TUK = Sema::TUK_Definition;
Richard Smith69730c12012-03-12 07:56:15 +00002790 else if (Tok.is(tok::semi) && DSC != DSC_type_specifier)
John McCallf312b1e2010-08-26 23:41:50 +00002791 TUK = Sema::TUK_Declaration;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002792 else
John McCallf312b1e2010-08-26 23:41:50 +00002793 TUK = Sema::TUK_Reference;
Douglas Gregor8fc6d232010-05-03 17:48:54 +00002794
2795 // enums cannot be templates, although they can be referenced from a
2796 // template.
2797 if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate &&
John McCallf312b1e2010-08-26 23:41:50 +00002798 TUK != Sema::TUK_Reference) {
Douglas Gregor8fc6d232010-05-03 17:48:54 +00002799 Diag(Tok, diag::err_enum_template);
2800
2801 // Skip the rest of this declarator, up until the comma or semicolon.
2802 SkipUntil(tok::comma, true);
2803 return;
2804 }
2805
Douglas Gregorb9075602011-02-22 02:55:24 +00002806 if (!Name && TUK != Sema::TUK_Definition) {
2807 Diag(Tok, diag::err_enumerator_unnamed_no_def);
2808
2809 // Skip the rest of this declarator, up until the comma or semicolon.
2810 SkipUntil(tok::comma, true);
2811 return;
2812 }
2813
Douglas Gregor402abb52009-05-28 23:31:59 +00002814 bool Owned = false;
John McCallc4e70192009-09-11 04:59:25 +00002815 bool IsDependent = false;
Douglas Gregor48c89f42010-04-24 16:38:41 +00002816 const char *PrevSpec = 0;
2817 unsigned DiagID;
John McCalld226f652010-08-21 09:40:31 +00002818 Decl *TagDecl = Actions.ActOnTag(getCurScope(), DeclSpec::TST_enum, TUK,
John McCall7f040a92010-12-24 02:08:15 +00002819 StartLoc, SS, Name, NameLoc, attrs.getList(),
Douglas Gregore7612302011-09-09 19:05:14 +00002820 AS, DS.getModulePrivateSpecLoc(),
John McCallf312b1e2010-08-26 23:41:50 +00002821 MultiTemplateParamsArg(Actions),
Richard Smithbdad7a22012-01-10 01:33:14 +00002822 Owned, IsDependent, ScopedEnumKWLoc,
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00002823 IsScopedUsingClassTag, BaseType);
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002824
Douglas Gregor48c89f42010-04-24 16:38:41 +00002825 if (IsDependent) {
2826 // This enum has a dependent nested-name-specifier. Handle it as a
2827 // dependent tag.
2828 if (!Name) {
2829 DS.SetTypeSpecError();
2830 Diag(Tok, diag::err_expected_type_name_after_typename);
2831 return;
2832 }
2833
Douglas Gregor23c94db2010-07-02 17:43:08 +00002834 TypeResult Type = Actions.ActOnDependentTag(getCurScope(), DeclSpec::TST_enum,
Douglas Gregor48c89f42010-04-24 16:38:41 +00002835 TUK, SS, Name, StartLoc,
2836 NameLoc);
2837 if (Type.isInvalid()) {
2838 DS.SetTypeSpecError();
2839 return;
2840 }
2841
Abramo Bagnara0daaf322011-03-16 20:16:18 +00002842 if (DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc,
2843 NameLoc.isValid() ? NameLoc : StartLoc,
2844 PrevSpec, DiagID, Type.get()))
Douglas Gregor48c89f42010-04-24 16:38:41 +00002845 Diag(StartLoc, DiagID) << PrevSpec;
2846
2847 return;
2848 }
Mike Stump1eb44332009-09-09 15:08:12 +00002849
John McCalld226f652010-08-21 09:40:31 +00002850 if (!TagDecl) {
Douglas Gregor48c89f42010-04-24 16:38:41 +00002851 // The action failed to produce an enumeration tag. If this is a
2852 // definition, consume the entire definition.
2853 if (Tok.is(tok::l_brace)) {
2854 ConsumeBrace();
2855 SkipUntil(tok::r_brace);
2856 }
2857
2858 DS.SetTypeSpecError();
2859 return;
2860 }
Richard Smithbdad7a22012-01-10 01:33:14 +00002861
2862 if (Tok.is(tok::l_brace)) {
2863 if (TUK == Sema::TUK_Friend)
2864 Diag(Tok, diag::err_friend_decl_defines_type)
2865 << SourceRange(DS.getFriendSpecLoc());
Reid Spencer5f016e22007-07-11 17:01:13 +00002866 ParseEnumBody(StartLoc, TagDecl);
Richard Smithbdad7a22012-01-10 01:33:14 +00002867 }
Mike Stump1eb44332009-09-09 15:08:12 +00002868
Abramo Bagnara0daaf322011-03-16 20:16:18 +00002869 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc,
2870 NameLoc.isValid() ? NameLoc : StartLoc,
2871 PrevSpec, DiagID, TagDecl, Owned))
John McCallfec54012009-08-03 20:12:06 +00002872 Diag(StartLoc, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00002873}
2874
2875/// ParseEnumBody - Parse a {} enclosed enumerator-list.
2876/// enumerator-list:
2877/// enumerator
2878/// enumerator-list ',' enumerator
2879/// enumerator:
2880/// enumeration-constant
2881/// enumeration-constant '=' constant-expression
2882/// enumeration-constant:
2883/// identifier
2884///
John McCalld226f652010-08-21 09:40:31 +00002885void Parser::ParseEnumBody(SourceLocation StartLoc, Decl *EnumDecl) {
Douglas Gregor074149e2009-01-05 19:45:36 +00002886 // Enter the scope of the enum body and start the definition.
2887 ParseScope EnumScope(this, Scope::DeclScope);
Douglas Gregor23c94db2010-07-02 17:43:08 +00002888 Actions.ActOnTagStartDefinition(getCurScope(), EnumDecl);
Douglas Gregor074149e2009-01-05 19:45:36 +00002889
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002890 BalancedDelimiterTracker T(*this, tok::l_brace);
2891 T.consumeOpen();
Mike Stump1eb44332009-09-09 15:08:12 +00002892
Chris Lattner7946dd32007-08-27 17:24:30 +00002893 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
David Blaikie4e4d0842012-03-11 07:00:24 +00002894 if (Tok.is(tok::r_brace) && !getLangOpts().CPlusPlus)
Fariborz Jahanian05115522010-05-28 22:23:22 +00002895 Diag(Tok, diag::error_empty_enum);
Mike Stump1eb44332009-09-09 15:08:12 +00002896
Chris Lattner5f9e2722011-07-23 10:55:15 +00002897 SmallVector<Decl *, 32> EnumConstantDecls;
Reid Spencer5f016e22007-07-11 17:01:13 +00002898
John McCalld226f652010-08-21 09:40:31 +00002899 Decl *LastEnumConstDecl = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00002900
Reid Spencer5f016e22007-07-11 17:01:13 +00002901 // Parse the enumerator-list.
Chris Lattner04d66662007-10-09 17:33:22 +00002902 while (Tok.is(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002903 IdentifierInfo *Ident = Tok.getIdentifierInfo();
2904 SourceLocation IdentLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00002905
John McCall5b629aa2010-10-22 23:36:17 +00002906 // If attributes exist after the enumerator, parse them.
John McCall0b7e6782011-03-24 11:26:52 +00002907 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00002908 MaybeParseGNUAttributes(attrs);
John McCall5b629aa2010-10-22 23:36:17 +00002909
Reid Spencer5f016e22007-07-11 17:01:13 +00002910 SourceLocation EqualLoc;
John McCall60d7b3a2010-08-24 06:29:42 +00002911 ExprResult AssignedVal;
Fariborz Jahanian5a477db2011-12-09 01:15:54 +00002912 ParsingDeclRAIIObject PD(*this);
2913
Chris Lattner04d66662007-10-09 17:33:22 +00002914 if (Tok.is(tok::equal)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002915 EqualLoc = ConsumeToken();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002916 AssignedVal = ParseConstantExpression();
2917 if (AssignedVal.isInvalid())
Reid Spencer5f016e22007-07-11 17:01:13 +00002918 SkipUntil(tok::comma, tok::r_brace, true, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00002919 }
Mike Stump1eb44332009-09-09 15:08:12 +00002920
Reid Spencer5f016e22007-07-11 17:01:13 +00002921 // Install the enumerator constant into EnumDecl.
John McCalld226f652010-08-21 09:40:31 +00002922 Decl *EnumConstDecl = Actions.ActOnEnumConstant(getCurScope(), EnumDecl,
2923 LastEnumConstDecl,
2924 IdentLoc, Ident,
John McCall7f040a92010-12-24 02:08:15 +00002925 attrs.getList(), EqualLoc,
John McCalld226f652010-08-21 09:40:31 +00002926 AssignedVal.release());
Fariborz Jahanian5a477db2011-12-09 01:15:54 +00002927 PD.complete(EnumConstDecl);
2928
Reid Spencer5f016e22007-07-11 17:01:13 +00002929 EnumConstantDecls.push_back(EnumConstDecl);
2930 LastEnumConstDecl = EnumConstDecl;
Mike Stump1eb44332009-09-09 15:08:12 +00002931
Douglas Gregor751f6922010-09-07 14:51:08 +00002932 if (Tok.is(tok::identifier)) {
2933 // We're missing a comma between enumerators.
2934 SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation);
2935 Diag(Loc, diag::err_enumerator_list_missing_comma)
2936 << FixItHint::CreateInsertion(Loc, ", ");
2937 continue;
2938 }
2939
Chris Lattner04d66662007-10-09 17:33:22 +00002940 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +00002941 break;
2942 SourceLocation CommaLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00002943
Richard Smith7fe62082011-10-15 05:09:34 +00002944 if (Tok.isNot(tok::identifier)) {
David Blaikie4e4d0842012-03-11 07:00:24 +00002945 if (!getLangOpts().C99 && !getLangOpts().CPlusPlus0x)
Richard Smith7fe62082011-10-15 05:09:34 +00002946 Diag(CommaLoc, diag::ext_enumerator_list_comma)
David Blaikie4e4d0842012-03-11 07:00:24 +00002947 << getLangOpts().CPlusPlus
Richard Smith7fe62082011-10-15 05:09:34 +00002948 << FixItHint::CreateRemoval(CommaLoc);
David Blaikie4e4d0842012-03-11 07:00:24 +00002949 else if (getLangOpts().CPlusPlus0x)
Richard Smith7fe62082011-10-15 05:09:34 +00002950 Diag(CommaLoc, diag::warn_cxx98_compat_enumerator_list_comma)
2951 << FixItHint::CreateRemoval(CommaLoc);
2952 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002953 }
Mike Stump1eb44332009-09-09 15:08:12 +00002954
Reid Spencer5f016e22007-07-11 17:01:13 +00002955 // Eat the }.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002956 T.consumeClose();
Reid Spencer5f016e22007-07-11 17:01:13 +00002957
Reid Spencer5f016e22007-07-11 17:01:13 +00002958 // If attributes exist after the identifier list, parse them.
John McCall0b7e6782011-03-24 11:26:52 +00002959 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00002960 MaybeParseGNUAttributes(attrs);
Douglas Gregor72de6672009-01-08 20:45:30 +00002961
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002962 Actions.ActOnEnumBody(StartLoc, T.getOpenLocation(), T.getCloseLocation(),
2963 EnumDecl, EnumConstantDecls.data(),
2964 EnumConstantDecls.size(), getCurScope(),
2965 attrs.getList());
Mike Stump1eb44332009-09-09 15:08:12 +00002966
Douglas Gregor72de6672009-01-08 20:45:30 +00002967 EnumScope.Exit();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002968 Actions.ActOnTagFinishDefinition(getCurScope(), EnumDecl,
2969 T.getCloseLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00002970}
2971
2972/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff5f8aa692008-02-11 23:15:56 +00002973/// start of a type-qualifier-list.
2974bool Parser::isTypeQualifier() const {
2975 switch (Tok.getKind()) {
2976 default: return false;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00002977
2978 // type-qualifier only in OpenCL
2979 case tok::kw_private:
David Blaikie4e4d0842012-03-11 07:00:24 +00002980 return getLangOpts().OpenCL;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00002981
Steve Naroff5f8aa692008-02-11 23:15:56 +00002982 // type-qualifier
2983 case tok::kw_const:
2984 case tok::kw_volatile:
2985 case tok::kw_restrict:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00002986 case tok::kw___private:
2987 case tok::kw___local:
2988 case tok::kw___global:
2989 case tok::kw___constant:
2990 case tok::kw___read_only:
2991 case tok::kw___read_write:
2992 case tok::kw___write_only:
Steve Naroff5f8aa692008-02-11 23:15:56 +00002993 return true;
2994 }
2995}
2996
Chris Lattnerb3a4e432010-02-28 18:18:36 +00002997/// isKnownToBeTypeSpecifier - Return true if we know that the specified token
2998/// is definitely a type-specifier. Return false if it isn't part of a type
2999/// specifier or if we're not sure.
3000bool Parser::isKnownToBeTypeSpecifier(const Token &Tok) const {
3001 switch (Tok.getKind()) {
3002 default: return false;
3003 // type-specifiers
3004 case tok::kw_short:
3005 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00003006 case tok::kw___int64:
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003007 case tok::kw_signed:
3008 case tok::kw_unsigned:
3009 case tok::kw__Complex:
3010 case tok::kw__Imaginary:
3011 case tok::kw_void:
3012 case tok::kw_char:
3013 case tok::kw_wchar_t:
3014 case tok::kw_char16_t:
3015 case tok::kw_char32_t:
3016 case tok::kw_int:
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00003017 case tok::kw_half:
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003018 case tok::kw_float:
3019 case tok::kw_double:
3020 case tok::kw_bool:
3021 case tok::kw__Bool:
3022 case tok::kw__Decimal32:
3023 case tok::kw__Decimal64:
3024 case tok::kw__Decimal128:
3025 case tok::kw___vector:
3026
3027 // struct-or-union-specifier (C99) or class-specifier (C++)
3028 case tok::kw_class:
3029 case tok::kw_struct:
3030 case tok::kw_union:
3031 // enum-specifier
3032 case tok::kw_enum:
3033
3034 // typedef-name
3035 case tok::annot_typename:
3036 return true;
3037 }
3038}
3039
Steve Naroff5f8aa692008-02-11 23:15:56 +00003040/// isTypeSpecifierQualifier - Return true if the current token could be the
Reid Spencer5f016e22007-07-11 17:01:13 +00003041/// start of a specifier-qualifier-list.
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003042bool Parser::isTypeSpecifierQualifier() {
Reid Spencer5f016e22007-07-11 17:01:13 +00003043 switch (Tok.getKind()) {
3044 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00003045
Chris Lattner166a8fc2009-01-04 23:41:41 +00003046 case tok::identifier: // foo::bar
John Thompson82287d12010-02-05 00:12:22 +00003047 if (TryAltiVecVectorToken())
3048 return true;
3049 // Fall through.
Douglas Gregord57959a2009-03-27 23:10:48 +00003050 case tok::kw_typename: // typename T::type
Chris Lattner166a8fc2009-01-04 23:41:41 +00003051 // Annotate typenames and C++ scope specifiers. If we get one, just
3052 // recurse to handle whatever we get.
3053 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003054 return true;
3055 if (Tok.is(tok::identifier))
3056 return false;
3057 return isTypeSpecifierQualifier();
Douglas Gregord57959a2009-03-27 23:10:48 +00003058
Chris Lattner166a8fc2009-01-04 23:41:41 +00003059 case tok::coloncolon: // ::foo::bar
3060 if (NextToken().is(tok::kw_new) || // ::new
3061 NextToken().is(tok::kw_delete)) // ::delete
3062 return false;
3063
Chris Lattner166a8fc2009-01-04 23:41:41 +00003064 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003065 return true;
3066 return isTypeSpecifierQualifier();
Mike Stump1eb44332009-09-09 15:08:12 +00003067
Reid Spencer5f016e22007-07-11 17:01:13 +00003068 // GNU attributes support.
3069 case tok::kw___attribute:
Steve Naroffd1861fd2007-07-31 12:34:36 +00003070 // GNU typeof support.
3071 case tok::kw_typeof:
Mike Stump1eb44332009-09-09 15:08:12 +00003072
Reid Spencer5f016e22007-07-11 17:01:13 +00003073 // type-specifiers
3074 case tok::kw_short:
3075 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00003076 case tok::kw___int64:
Reid Spencer5f016e22007-07-11 17:01:13 +00003077 case tok::kw_signed:
3078 case tok::kw_unsigned:
3079 case tok::kw__Complex:
3080 case tok::kw__Imaginary:
3081 case tok::kw_void:
3082 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00003083 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00003084 case tok::kw_char16_t:
3085 case tok::kw_char32_t:
Reid Spencer5f016e22007-07-11 17:01:13 +00003086 case tok::kw_int:
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00003087 case tok::kw_half:
Reid Spencer5f016e22007-07-11 17:01:13 +00003088 case tok::kw_float:
3089 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00003090 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00003091 case tok::kw__Bool:
3092 case tok::kw__Decimal32:
3093 case tok::kw__Decimal64:
3094 case tok::kw__Decimal128:
John Thompson82287d12010-02-05 00:12:22 +00003095 case tok::kw___vector:
Mike Stump1eb44332009-09-09 15:08:12 +00003096
Chris Lattner99dc9142008-04-13 18:59:07 +00003097 // struct-or-union-specifier (C99) or class-specifier (C++)
3098 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00003099 case tok::kw_struct:
3100 case tok::kw_union:
3101 // enum-specifier
3102 case tok::kw_enum:
Mike Stump1eb44332009-09-09 15:08:12 +00003103
Reid Spencer5f016e22007-07-11 17:01:13 +00003104 // type-qualifier
3105 case tok::kw_const:
3106 case tok::kw_volatile:
3107 case tok::kw_restrict:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003108
3109 // typedef-name
Chris Lattnerb31757b2009-01-06 05:06:21 +00003110 case tok::annot_typename:
Reid Spencer5f016e22007-07-11 17:01:13 +00003111 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00003112
Chris Lattner7c186be2008-10-20 00:25:30 +00003113 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
3114 case tok::less:
David Blaikie4e4d0842012-03-11 07:00:24 +00003115 return getLangOpts().ObjC1;
Mike Stump1eb44332009-09-09 15:08:12 +00003116
Steve Naroff239f0732008-12-25 14:16:32 +00003117 case tok::kw___cdecl:
3118 case tok::kw___stdcall:
3119 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00003120 case tok::kw___thiscall:
Eli Friedman290eeb02009-06-08 23:27:34 +00003121 case tok::kw___w64:
3122 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00003123 case tok::kw___ptr32:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003124 case tok::kw___pascal:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00003125 case tok::kw___unaligned:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003126
3127 case tok::kw___private:
3128 case tok::kw___local:
3129 case tok::kw___global:
3130 case tok::kw___constant:
3131 case tok::kw___read_only:
3132 case tok::kw___read_write:
3133 case tok::kw___write_only:
3134
Eli Friedman290eeb02009-06-08 23:27:34 +00003135 return true;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003136
3137 case tok::kw_private:
David Blaikie4e4d0842012-03-11 07:00:24 +00003138 return getLangOpts().OpenCL;
Eli Friedmanb001de72011-10-06 23:00:33 +00003139
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00003140 // C11 _Atomic()
Eli Friedmanb001de72011-10-06 23:00:33 +00003141 case tok::kw__Atomic:
3142 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00003143 }
3144}
3145
3146/// isDeclarationSpecifier() - Return true if the current token is part of a
3147/// declaration specifier.
Douglas Gregor9497a732010-09-16 01:51:54 +00003148///
3149/// \param DisambiguatingWithExpression True to indicate that the purpose of
3150/// this check is to disambiguate between an expression and a declaration.
3151bool Parser::isDeclarationSpecifier(bool DisambiguatingWithExpression) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003152 switch (Tok.getKind()) {
3153 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00003154
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003155 case tok::kw_private:
David Blaikie4e4d0842012-03-11 07:00:24 +00003156 return getLangOpts().OpenCL;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003157
Chris Lattner166a8fc2009-01-04 23:41:41 +00003158 case tok::identifier: // foo::bar
Steve Naroff61f72cb2009-03-09 21:12:44 +00003159 // Unfortunate hack to support "Class.factoryMethod" notation.
David Blaikie4e4d0842012-03-11 07:00:24 +00003160 if (getLangOpts().ObjC1 && NextToken().is(tok::period))
Steve Naroff61f72cb2009-03-09 21:12:44 +00003161 return false;
John Thompson82287d12010-02-05 00:12:22 +00003162 if (TryAltiVecVectorToken())
3163 return true;
3164 // Fall through.
David Blaikie42d6d0c2011-12-04 05:04:18 +00003165 case tok::kw_decltype: // decltype(T())::type
Douglas Gregord57959a2009-03-27 23:10:48 +00003166 case tok::kw_typename: // typename T::type
Chris Lattner166a8fc2009-01-04 23:41:41 +00003167 // Annotate typenames and C++ scope specifiers. If we get one, just
3168 // recurse to handle whatever we get.
3169 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003170 return true;
3171 if (Tok.is(tok::identifier))
3172 return false;
Douglas Gregor9497a732010-09-16 01:51:54 +00003173
3174 // If we're in Objective-C and we have an Objective-C class type followed
3175 // by an identifier and then either ':' or ']', in a place where an
3176 // expression is permitted, then this is probably a class message send
3177 // missing the initial '['. In this case, we won't consider this to be
3178 // the start of a declaration.
3179 if (DisambiguatingWithExpression &&
3180 isStartOfObjCClassMessageMissingOpenBracket())
3181 return false;
3182
John McCall9ba61662010-02-26 08:45:28 +00003183 return isDeclarationSpecifier();
3184
Chris Lattner166a8fc2009-01-04 23:41:41 +00003185 case tok::coloncolon: // ::foo::bar
3186 if (NextToken().is(tok::kw_new) || // ::new
3187 NextToken().is(tok::kw_delete)) // ::delete
3188 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00003189
Chris Lattner166a8fc2009-01-04 23:41:41 +00003190 // Annotate typenames and C++ scope specifiers. If we get one, just
3191 // recurse to handle whatever we get.
3192 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003193 return true;
3194 return isDeclarationSpecifier();
Mike Stump1eb44332009-09-09 15:08:12 +00003195
Reid Spencer5f016e22007-07-11 17:01:13 +00003196 // storage-class-specifier
3197 case tok::kw_typedef:
3198 case tok::kw_extern:
Steve Naroff8d54bf22007-12-18 00:16:02 +00003199 case tok::kw___private_extern__:
Reid Spencer5f016e22007-07-11 17:01:13 +00003200 case tok::kw_static:
3201 case tok::kw_auto:
3202 case tok::kw_register:
3203 case tok::kw___thread:
Mike Stump1eb44332009-09-09 15:08:12 +00003204
Douglas Gregor8d267c52011-09-09 02:06:17 +00003205 // Modules
3206 case tok::kw___module_private__:
3207
Reid Spencer5f016e22007-07-11 17:01:13 +00003208 // type-specifiers
3209 case tok::kw_short:
3210 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00003211 case tok::kw___int64:
Reid Spencer5f016e22007-07-11 17:01:13 +00003212 case tok::kw_signed:
3213 case tok::kw_unsigned:
3214 case tok::kw__Complex:
3215 case tok::kw__Imaginary:
3216 case tok::kw_void:
3217 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00003218 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00003219 case tok::kw_char16_t:
3220 case tok::kw_char32_t:
3221
Reid Spencer5f016e22007-07-11 17:01:13 +00003222 case tok::kw_int:
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00003223 case tok::kw_half:
Reid Spencer5f016e22007-07-11 17:01:13 +00003224 case tok::kw_float:
3225 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00003226 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00003227 case tok::kw__Bool:
3228 case tok::kw__Decimal32:
3229 case tok::kw__Decimal64:
3230 case tok::kw__Decimal128:
John Thompson82287d12010-02-05 00:12:22 +00003231 case tok::kw___vector:
Mike Stump1eb44332009-09-09 15:08:12 +00003232
Chris Lattner99dc9142008-04-13 18:59:07 +00003233 // struct-or-union-specifier (C99) or class-specifier (C++)
3234 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00003235 case tok::kw_struct:
3236 case tok::kw_union:
3237 // enum-specifier
3238 case tok::kw_enum:
Mike Stump1eb44332009-09-09 15:08:12 +00003239
Reid Spencer5f016e22007-07-11 17:01:13 +00003240 // type-qualifier
3241 case tok::kw_const:
3242 case tok::kw_volatile:
3243 case tok::kw_restrict:
Steve Naroffd1861fd2007-07-31 12:34:36 +00003244
Reid Spencer5f016e22007-07-11 17:01:13 +00003245 // function-specifier
3246 case tok::kw_inline:
Douglas Gregorb48fe382008-10-31 09:07:45 +00003247 case tok::kw_virtual:
3248 case tok::kw_explicit:
Chris Lattnerd6c7c182007-08-09 16:40:21 +00003249
Peter Collingbournec6eb44b2011-04-15 00:35:57 +00003250 // static_assert-declaration
3251 case tok::kw__Static_assert:
3252
Chris Lattner1ef08762007-08-09 17:01:07 +00003253 // GNU typeof support.
3254 case tok::kw_typeof:
Mike Stump1eb44332009-09-09 15:08:12 +00003255
Chris Lattner1ef08762007-08-09 17:01:07 +00003256 // GNU attributes.
Chris Lattnerd6c7c182007-08-09 16:40:21 +00003257 case tok::kw___attribute:
Reid Spencer5f016e22007-07-11 17:01:13 +00003258 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00003259
Francois Pichete3d49b42011-06-19 08:02:06 +00003260 // C++0x decltype.
David Blaikie42d6d0c2011-12-04 05:04:18 +00003261 case tok::annot_decltype:
Francois Pichete3d49b42011-06-19 08:02:06 +00003262 return true;
3263
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00003264 // C11 _Atomic()
Eli Friedmanb001de72011-10-06 23:00:33 +00003265 case tok::kw__Atomic:
3266 return true;
3267
Chris Lattnerf3948c42008-07-26 03:38:44 +00003268 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
3269 case tok::less:
David Blaikie4e4d0842012-03-11 07:00:24 +00003270 return getLangOpts().ObjC1;
Mike Stump1eb44332009-09-09 15:08:12 +00003271
Douglas Gregord9d75e52011-04-27 05:41:15 +00003272 // typedef-name
3273 case tok::annot_typename:
3274 return !DisambiguatingWithExpression ||
3275 !isStartOfObjCClassMessageMissingOpenBracket();
3276
Steve Naroff47f52092009-01-06 19:34:12 +00003277 case tok::kw___declspec:
Steve Naroff239f0732008-12-25 14:16:32 +00003278 case tok::kw___cdecl:
3279 case tok::kw___stdcall:
3280 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00003281 case tok::kw___thiscall:
Eli Friedman290eeb02009-06-08 23:27:34 +00003282 case tok::kw___w64:
3283 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00003284 case tok::kw___ptr32:
Eli Friedman290eeb02009-06-08 23:27:34 +00003285 case tok::kw___forceinline:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003286 case tok::kw___pascal:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00003287 case tok::kw___unaligned:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003288
3289 case tok::kw___private:
3290 case tok::kw___local:
3291 case tok::kw___global:
3292 case tok::kw___constant:
3293 case tok::kw___read_only:
3294 case tok::kw___read_write:
3295 case tok::kw___write_only:
3296
Eli Friedman290eeb02009-06-08 23:27:34 +00003297 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00003298 }
3299}
3300
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003301bool Parser::isConstructorDeclarator() {
3302 TentativeParsingAction TPA(*this);
3303
3304 // Parse the C++ scope specifier.
3305 CXXScopeSpec SS;
Douglas Gregorefaa93a2011-11-07 17:33:42 +00003306 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
3307 /*EnteringContext=*/true)) {
John McCall9ba61662010-02-26 08:45:28 +00003308 TPA.Revert();
3309 return false;
3310 }
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003311
3312 // Parse the constructor name.
3313 if (Tok.is(tok::identifier) || Tok.is(tok::annot_template_id)) {
3314 // We already know that we have a constructor name; just consume
3315 // the token.
3316 ConsumeToken();
3317 } else {
3318 TPA.Revert();
3319 return false;
3320 }
3321
3322 // Current class name must be followed by a left parentheses.
3323 if (Tok.isNot(tok::l_paren)) {
3324 TPA.Revert();
3325 return false;
3326 }
3327 ConsumeParen();
3328
3329 // A right parentheses or ellipsis signals that we have a constructor.
3330 if (Tok.is(tok::r_paren) || Tok.is(tok::ellipsis)) {
3331 TPA.Revert();
3332 return true;
3333 }
3334
3335 // If we need to, enter the specified scope.
3336 DeclaratorScopeObj DeclScopeObj(*this, SS);
Douglas Gregor23c94db2010-07-02 17:43:08 +00003337 if (SS.isSet() && Actions.ShouldEnterDeclaratorScope(getCurScope(), SS))
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003338 DeclScopeObj.EnterDeclaratorScope();
3339
Francois Pichetdfaa5fb2011-01-31 04:54:32 +00003340 // Optionally skip Microsoft attributes.
John McCall0b7e6782011-03-24 11:26:52 +00003341 ParsedAttributes Attrs(AttrFactory);
Francois Pichetdfaa5fb2011-01-31 04:54:32 +00003342 MaybeParseMicrosoftAttributes(Attrs);
3343
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003344 // Check whether the next token(s) are part of a declaration
3345 // specifier, in which case we have the start of a parameter and,
3346 // therefore, we know that this is a constructor.
3347 bool IsConstructor = isDeclarationSpecifier();
3348 TPA.Revert();
3349 return IsConstructor;
3350}
Reid Spencer5f016e22007-07-11 17:01:13 +00003351
3352/// ParseTypeQualifierListOpt
Dawn Perchik52fc3142010-09-03 01:29:35 +00003353/// type-qualifier-list: [C99 6.7.5]
3354/// type-qualifier
3355/// [vendor] attributes
3356/// [ only if VendorAttributesAllowed=true ]
3357/// type-qualifier-list type-qualifier
3358/// [vendor] type-qualifier-list attributes
3359/// [ only if VendorAttributesAllowed=true ]
3360/// [C++0x] attribute-specifier[opt] is allowed before cv-qualifier-seq
3361/// [ only if CXX0XAttributesAllowed=true ]
3362/// Note: vendor can be GNU, MS, etc.
Reid Spencer5f016e22007-07-11 17:01:13 +00003363///
Dawn Perchik52fc3142010-09-03 01:29:35 +00003364void Parser::ParseTypeQualifierListOpt(DeclSpec &DS,
3365 bool VendorAttributesAllowed,
Sean Huntbbd37c62009-11-21 08:43:09 +00003366 bool CXX0XAttributesAllowed) {
David Blaikie4e4d0842012-03-11 07:00:24 +00003367 if (getLangOpts().CPlusPlus0x && isCXX0XAttributeSpecifier()) {
Sean Huntbbd37c62009-11-21 08:43:09 +00003368 SourceLocation Loc = Tok.getLocation();
John McCall0b7e6782011-03-24 11:26:52 +00003369 ParsedAttributesWithRange attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00003370 ParseCXX0XAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00003371 if (CXX0XAttributesAllowed)
John McCall7f040a92010-12-24 02:08:15 +00003372 DS.takeAttributesFrom(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00003373 else
3374 Diag(Loc, diag::err_attributes_not_allowed);
3375 }
Abramo Bagnara796aa442011-03-12 11:17:06 +00003376
3377 SourceLocation EndLoc;
3378
Reid Spencer5f016e22007-07-11 17:01:13 +00003379 while (1) {
John McCallfec54012009-08-03 20:12:06 +00003380 bool isInvalid = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00003381 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00003382 unsigned DiagID = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00003383 SourceLocation Loc = Tok.getLocation();
3384
3385 switch (Tok.getKind()) {
Douglas Gregor1a480c42010-08-27 17:35:51 +00003386 case tok::code_completion:
3387 Actions.CodeCompleteTypeQualifiers(DS);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00003388 return cutOffParsing();
Douglas Gregor1a480c42010-08-27 17:35:51 +00003389
Reid Spencer5f016e22007-07-11 17:01:13 +00003390 case tok::kw_const:
John McCallfec54012009-08-03 20:12:06 +00003391 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, DiagID,
David Blaikie4e4d0842012-03-11 07:00:24 +00003392 getLangOpts());
Reid Spencer5f016e22007-07-11 17:01:13 +00003393 break;
3394 case tok::kw_volatile:
John McCallfec54012009-08-03 20:12:06 +00003395 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
David Blaikie4e4d0842012-03-11 07:00:24 +00003396 getLangOpts());
Reid Spencer5f016e22007-07-11 17:01:13 +00003397 break;
3398 case tok::kw_restrict:
John McCallfec54012009-08-03 20:12:06 +00003399 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
David Blaikie4e4d0842012-03-11 07:00:24 +00003400 getLangOpts());
Reid Spencer5f016e22007-07-11 17:01:13 +00003401 break;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003402
3403 // OpenCL qualifiers:
3404 case tok::kw_private:
David Blaikie4e4d0842012-03-11 07:00:24 +00003405 if (!getLangOpts().OpenCL)
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003406 goto DoneWithTypeQuals;
3407 case tok::kw___private:
3408 case tok::kw___global:
3409 case tok::kw___local:
3410 case tok::kw___constant:
3411 case tok::kw___read_only:
3412 case tok::kw___write_only:
3413 case tok::kw___read_write:
3414 ParseOpenCLQualifiers(DS);
3415 break;
3416
Eli Friedman290eeb02009-06-08 23:27:34 +00003417 case tok::kw___w64:
Steve Naroff86bc6cf2008-12-25 14:41:26 +00003418 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00003419 case tok::kw___ptr32:
Steve Naroff239f0732008-12-25 14:16:32 +00003420 case tok::kw___cdecl:
3421 case tok::kw___stdcall:
3422 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00003423 case tok::kw___thiscall:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00003424 case tok::kw___unaligned:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003425 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00003426 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman290eeb02009-06-08 23:27:34 +00003427 continue;
3428 }
3429 goto DoneWithTypeQuals;
Dawn Perchik52fc3142010-09-03 01:29:35 +00003430 case tok::kw___pascal:
3431 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00003432 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik52fc3142010-09-03 01:29:35 +00003433 continue;
3434 }
3435 goto DoneWithTypeQuals;
Reid Spencer5f016e22007-07-11 17:01:13 +00003436 case tok::kw___attribute:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003437 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00003438 ParseGNUAttributes(DS.getAttributes());
Chris Lattner5a69d1c2008-12-18 07:02:59 +00003439 continue; // do *not* consume the next token!
3440 }
3441 // otherwise, FALL THROUGH!
3442 default:
Steve Naroff239f0732008-12-25 14:16:32 +00003443 DoneWithTypeQuals:
Chris Lattner5a69d1c2008-12-18 07:02:59 +00003444 // If this is not a type-qualifier token, we're done reading type
3445 // qualifiers. First verify that DeclSpec's are consistent.
Douglas Gregor9b3064b2009-04-01 22:41:11 +00003446 DS.Finish(Diags, PP);
Abramo Bagnara796aa442011-03-12 11:17:06 +00003447 if (EndLoc.isValid())
3448 DS.SetRangeEnd(EndLoc);
Chris Lattner5a69d1c2008-12-18 07:02:59 +00003449 return;
Reid Spencer5f016e22007-07-11 17:01:13 +00003450 }
Chris Lattnera1fcbad2008-12-18 06:50:14 +00003451
Reid Spencer5f016e22007-07-11 17:01:13 +00003452 // If the specifier combination wasn't legal, issue a diagnostic.
3453 if (isInvalid) {
3454 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner1ab3b962008-11-18 07:48:38 +00003455 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00003456 }
Abramo Bagnara796aa442011-03-12 11:17:06 +00003457 EndLoc = ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00003458 }
3459}
3460
3461
3462/// ParseDeclarator - Parse and verify a newly-initialized declarator.
3463///
3464void Parser::ParseDeclarator(Declarator &D) {
3465 /// This implements the 'declarator' production in the C grammar, then checks
3466 /// for well-formedness and issues diagnostics.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003467 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Reid Spencer5f016e22007-07-11 17:01:13 +00003468}
3469
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003470/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
3471/// is parsed by the function passed to it. Pass null, and the direct-declarator
3472/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003473/// ptr-operator production.
3474///
Richard Smith0706df42011-10-19 21:33:05 +00003475/// If the grammar of this construct is extended, matching changes must also be
3476/// made to TryParseDeclarator and MightBeDeclarator.
3477///
Sebastian Redlf30208a2009-01-24 21:16:55 +00003478/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
3479/// [C] pointer[opt] direct-declarator
3480/// [C++] direct-declarator
3481/// [C++] ptr-operator declarator
Reid Spencer5f016e22007-07-11 17:01:13 +00003482///
3483/// pointer: [C99 6.7.5]
3484/// '*' type-qualifier-list[opt]
3485/// '*' type-qualifier-list[opt] pointer
3486///
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003487/// ptr-operator:
3488/// '*' cv-qualifier-seq[opt]
3489/// '&'
Sebastian Redl05532f22009-03-15 22:02:01 +00003490/// [C++0x] '&&'
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003491/// [GNU] '&' restrict[opt] attributes[opt]
Sebastian Redl05532f22009-03-15 22:02:01 +00003492/// [GNU?] '&&' restrict[opt] attributes[opt]
Sebastian Redlf30208a2009-01-24 21:16:55 +00003493/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003494void Parser::ParseDeclaratorInternal(Declarator &D,
3495 DirectDeclParseFunction DirectDeclParser) {
Douglas Gregor91a28862009-08-26 14:27:30 +00003496 if (Diags.hasAllExtensionsSilenced())
3497 D.setExtension();
Douglas Gregor2ccccb32010-08-23 18:23:48 +00003498
Sebastian Redlf30208a2009-01-24 21:16:55 +00003499 // C++ member pointers start with a '::' or a nested-name.
3500 // Member pointers get special handling, since there's no place for the
3501 // scope spec in the generic path below.
David Blaikie4e4d0842012-03-11 07:00:24 +00003502 if (getLangOpts().CPlusPlus &&
Chris Lattnerf919bfe2009-03-24 17:04:48 +00003503 (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) ||
3504 Tok.is(tok::annot_cxxscope))) {
Douglas Gregorefaa93a2011-11-07 17:33:42 +00003505 bool EnteringContext = D.getContext() == Declarator::FileContext ||
3506 D.getContext() == Declarator::MemberContext;
Sebastian Redlf30208a2009-01-24 21:16:55 +00003507 CXXScopeSpec SS;
Douglas Gregorefaa93a2011-11-07 17:33:42 +00003508 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), EnteringContext);
John McCall9ba61662010-02-26 08:45:28 +00003509
Jeffrey Yasskinedc28772010-04-07 23:29:58 +00003510 if (SS.isNotEmpty()) {
Mike Stump1eb44332009-09-09 15:08:12 +00003511 if (Tok.isNot(tok::star)) {
Sebastian Redlf30208a2009-01-24 21:16:55 +00003512 // The scope spec really belongs to the direct-declarator.
3513 D.getCXXScopeSpec() = SS;
3514 if (DirectDeclParser)
3515 (this->*DirectDeclParser)(D);
3516 return;
3517 }
3518
3519 SourceLocation Loc = ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00003520 D.SetRangeEnd(Loc);
John McCall0b7e6782011-03-24 11:26:52 +00003521 DeclSpec DS(AttrFactory);
Sebastian Redlf30208a2009-01-24 21:16:55 +00003522 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00003523 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00003524
3525 // Recurse to parse whatever is left.
3526 ParseDeclaratorInternal(D, DirectDeclParser);
3527
3528 // Sema will have to catch (syntactically invalid) pointers into global
3529 // scope. It has to catch pointers into namespace scope anyway.
3530 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
John McCall0b7e6782011-03-24 11:26:52 +00003531 Loc),
3532 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003533 /* Don't replace range end. */SourceLocation());
Sebastian Redlf30208a2009-01-24 21:16:55 +00003534 return;
3535 }
3536 }
3537
3538 tok::TokenKind Kind = Tok.getKind();
Steve Naroff5618bd42008-08-27 16:04:49 +00003539 // Not a pointer, C++ reference, or block.
Chris Lattner9af55002009-03-27 04:18:06 +00003540 if (Kind != tok::star && Kind != tok::caret &&
David Blaikie4e4d0842012-03-11 07:00:24 +00003541 (Kind != tok::amp || !getLangOpts().CPlusPlus) &&
Sebastian Redl743de1f2009-03-23 00:00:23 +00003542 // We parse rvalue refs in C++03, because otherwise the errors are scary.
David Blaikie4e4d0842012-03-11 07:00:24 +00003543 (Kind != tok::ampamp || !getLangOpts().CPlusPlus)) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003544 if (DirectDeclParser)
3545 (this->*DirectDeclParser)(D);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003546 return;
3547 }
Sebastian Redlf30208a2009-01-24 21:16:55 +00003548
Sebastian Redl05532f22009-03-15 22:02:01 +00003549 // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference,
3550 // '&&' -> rvalue reference
Sebastian Redl743de1f2009-03-23 00:00:23 +00003551 SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&.
Sebastian Redlab197ba2009-02-09 18:23:29 +00003552 D.SetRangeEnd(Loc);
Reid Spencer5f016e22007-07-11 17:01:13 +00003553
Chris Lattner9af55002009-03-27 04:18:06 +00003554 if (Kind == tok::star || Kind == tok::caret) {
Chris Lattner76549142008-02-21 01:32:26 +00003555 // Is a pointer.
John McCall0b7e6782011-03-24 11:26:52 +00003556 DeclSpec DS(AttrFactory);
Sebastian Redlf30208a2009-01-24 21:16:55 +00003557
Reid Spencer5f016e22007-07-11 17:01:13 +00003558 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00003559 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00003560
Reid Spencer5f016e22007-07-11 17:01:13 +00003561 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003562 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroff5618bd42008-08-27 16:04:49 +00003563 if (Kind == tok::star)
3564 // Remember that we parsed a pointer type, and remember the type-quals.
3565 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
Chandler Carruthd067c072011-02-23 18:51:59 +00003566 DS.getConstSpecLoc(),
3567 DS.getVolatileSpecLoc(),
John McCall0b7e6782011-03-24 11:26:52 +00003568 DS.getRestrictSpecLoc()),
3569 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003570 SourceLocation());
Steve Naroff5618bd42008-08-27 16:04:49 +00003571 else
3572 // Remember that we parsed a Block type, and remember the type-quals.
Mike Stump1eb44332009-09-09 15:08:12 +00003573 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
John McCall0b7e6782011-03-24 11:26:52 +00003574 Loc),
3575 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003576 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00003577 } else {
3578 // Is a reference
John McCall0b7e6782011-03-24 11:26:52 +00003579 DeclSpec DS(AttrFactory);
Reid Spencer5f016e22007-07-11 17:01:13 +00003580
Sebastian Redl743de1f2009-03-23 00:00:23 +00003581 // Complain about rvalue references in C++03, but then go on and build
3582 // the declarator.
Richard Smith7fe62082011-10-15 05:09:34 +00003583 if (Kind == tok::ampamp)
David Blaikie4e4d0842012-03-11 07:00:24 +00003584 Diag(Loc, getLangOpts().CPlusPlus0x ?
Richard Smith7fe62082011-10-15 05:09:34 +00003585 diag::warn_cxx98_compat_rvalue_reference :
3586 diag::ext_rvalue_reference);
Sebastian Redl743de1f2009-03-23 00:00:23 +00003587
Reid Spencer5f016e22007-07-11 17:01:13 +00003588 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
3589 // cv-qualifiers are introduced through the use of a typedef or of a
3590 // template type argument, in which case the cv-qualifiers are ignored.
3591 //
3592 // [GNU] Retricted references are allowed.
3593 // [GNU] Attributes on references are allowed.
Sean Huntbbd37c62009-11-21 08:43:09 +00003594 // [C++0x] Attributes on references are not allowed.
3595 ParseTypeQualifierListOpt(DS, true, false);
Sebastian Redlab197ba2009-02-09 18:23:29 +00003596 D.ExtendWithDeclSpec(DS);
Reid Spencer5f016e22007-07-11 17:01:13 +00003597
3598 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
3599 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
3600 Diag(DS.getConstSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00003601 diag::err_invalid_reference_qualifier_application) << "const";
Reid Spencer5f016e22007-07-11 17:01:13 +00003602 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
3603 Diag(DS.getVolatileSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00003604 diag::err_invalid_reference_qualifier_application) << "volatile";
Reid Spencer5f016e22007-07-11 17:01:13 +00003605 }
3606
3607 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003608 ParseDeclaratorInternal(D, DirectDeclParser);
Reid Spencer5f016e22007-07-11 17:01:13 +00003609
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00003610 if (D.getNumTypeObjects() > 0) {
3611 // C++ [dcl.ref]p4: There shall be no references to references.
3612 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
3613 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattnerda83bac2008-11-19 07:37:42 +00003614 if (const IdentifierInfo *II = D.getIdentifier())
3615 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
3616 << II;
3617 else
3618 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
3619 << "type name";
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00003620
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003621 // Once we've complained about the reference-to-reference, we
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00003622 // can go ahead and build the (technically ill-formed)
3623 // declarator: reference collapsing will take care of it.
3624 }
3625 }
3626
Reid Spencer5f016e22007-07-11 17:01:13 +00003627 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner76549142008-02-21 01:32:26 +00003628 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
Sebastian Redl05532f22009-03-15 22:02:01 +00003629 Kind == tok::amp),
John McCall0b7e6782011-03-24 11:26:52 +00003630 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003631 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00003632 }
3633}
3634
3635/// ParseDirectDeclarator
3636/// direct-declarator: [C99 6.7.5]
Douglas Gregor42a552f2008-11-05 20:51:48 +00003637/// [C99] identifier
Reid Spencer5f016e22007-07-11 17:01:13 +00003638/// '(' declarator ')'
3639/// [GNU] '(' attributes declarator ')'
3640/// [C90] direct-declarator '[' constant-expression[opt] ']'
3641/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
3642/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
3643/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
3644/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
3645/// direct-declarator '(' parameter-type-list ')'
3646/// direct-declarator '(' identifier-list[opt] ')'
3647/// [GNU] direct-declarator '(' parameter-forward-declarations
3648/// parameter-type-list[opt] ')'
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003649/// [C++] direct-declarator '(' parameter-declaration-clause ')'
3650/// cv-qualifier-seq[opt] exception-specification[opt]
Douglas Gregorb48fe382008-10-31 09:07:45 +00003651/// [C++] declarator-id
Douglas Gregor42a552f2008-11-05 20:51:48 +00003652///
3653/// declarator-id: [C++ 8]
Douglas Gregora8bc8c92010-12-23 22:44:42 +00003654/// '...'[opt] id-expression
Douglas Gregor42a552f2008-11-05 20:51:48 +00003655/// '::'[opt] nested-name-specifier[opt] type-name
3656///
3657/// id-expression: [C++ 5.1]
3658/// unqualified-id
Douglas Gregordb422df2009-09-25 21:45:23 +00003659/// qualified-id
Douglas Gregor42a552f2008-11-05 20:51:48 +00003660///
3661/// unqualified-id: [C++ 5.1]
Mike Stump1eb44332009-09-09 15:08:12 +00003662/// identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003663/// operator-function-id
Douglas Gregordb422df2009-09-25 21:45:23 +00003664/// conversion-function-id
Mike Stump1eb44332009-09-09 15:08:12 +00003665/// '~' class-name
Douglas Gregor39a8de12009-02-25 19:37:18 +00003666/// template-id
Argyrios Kyrtzidisc7ed9c62008-11-07 22:02:30 +00003667///
Reid Spencer5f016e22007-07-11 17:01:13 +00003668void Parser::ParseDirectDeclarator(Declarator &D) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00003669 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003670
David Blaikie4e4d0842012-03-11 07:00:24 +00003671 if (getLangOpts().CPlusPlus && D.mayHaveIdentifier()) {
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003672 // ParseDeclaratorInternal might already have parsed the scope.
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00003673 if (D.getCXXScopeSpec().isEmpty()) {
Douglas Gregorefaa93a2011-11-07 17:33:42 +00003674 bool EnteringContext = D.getContext() == Declarator::FileContext ||
3675 D.getContext() == Declarator::MemberContext;
3676 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec(), ParsedType(),
3677 EnteringContext);
John McCall9ba61662010-02-26 08:45:28 +00003678 }
3679
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00003680 if (D.getCXXScopeSpec().isValid()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00003681 if (Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
John McCalle7e278b2009-12-11 20:04:54 +00003682 // Change the declaration context for name lookup, until this function
3683 // is exited (and the declarator has been parsed).
3684 DeclScopeObj.EnterDeclaratorScope();
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00003685 }
3686
Douglas Gregora8bc8c92010-12-23 22:44:42 +00003687 // C++0x [dcl.fct]p14:
3688 // There is a syntactic ambiguity when an ellipsis occurs at the end
3689 // of a parameter-declaration-clause without a preceding comma. In
3690 // this case, the ellipsis is parsed as part of the
3691 // abstract-declarator if the type of the parameter names a template
3692 // parameter pack that has not been expanded; otherwise, it is parsed
3693 // as part of the parameter-declaration-clause.
3694 if (Tok.is(tok::ellipsis) &&
3695 !((D.getContext() == Declarator::PrototypeContext ||
3696 D.getContext() == Declarator::BlockLiteralContext) &&
Douglas Gregora8bc8c92010-12-23 22:44:42 +00003697 NextToken().is(tok::r_paren) &&
3698 !Actions.containsUnexpandedParameterPacks(D)))
3699 D.setEllipsisLoc(ConsumeToken());
3700
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003701 if (Tok.is(tok::identifier) || Tok.is(tok::kw_operator) ||
3702 Tok.is(tok::annot_template_id) || Tok.is(tok::tilde)) {
3703 // We found something that indicates the start of an unqualified-id.
3704 // Parse that unqualified-id.
John McCallba9d8532010-04-13 06:39:49 +00003705 bool AllowConstructorName;
3706 if (D.getDeclSpec().hasTypeSpecifier())
3707 AllowConstructorName = false;
3708 else if (D.getCXXScopeSpec().isSet())
3709 AllowConstructorName =
3710 (D.getContext() == Declarator::FileContext ||
3711 (D.getContext() == Declarator::MemberContext &&
3712 D.getDeclSpec().isFriendSpecified()));
3713 else
3714 AllowConstructorName = (D.getContext() == Declarator::MemberContext);
3715
Abramo Bagnarae4b92762012-01-27 09:46:47 +00003716 SourceLocation TemplateKWLoc;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003717 if (ParseUnqualifiedId(D.getCXXScopeSpec(),
3718 /*EnteringContext=*/true,
3719 /*AllowDestructorName=*/true,
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003720 AllowConstructorName,
John McCallb3d87482010-08-24 05:47:05 +00003721 ParsedType(),
Abramo Bagnarae4b92762012-01-27 09:46:47 +00003722 TemplateKWLoc,
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00003723 D.getName()) ||
3724 // Once we're past the identifier, if the scope was bad, mark the
3725 // whole declarator bad.
3726 D.getCXXScopeSpec().isInvalid()) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00003727 D.SetIdentifier(0, Tok.getLocation());
3728 D.setInvalidType(true);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003729 } else {
3730 // Parsed the unqualified-id; update range information and move along.
3731 if (D.getSourceRange().getBegin().isInvalid())
3732 D.SetRangeBegin(D.getName().getSourceRange().getBegin());
3733 D.SetRangeEnd(D.getName().getSourceRange().getEnd());
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003734 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003735 goto PastIdentifier;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00003736 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003737 } else if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
David Blaikie4e4d0842012-03-11 07:00:24 +00003738 assert(!getLangOpts().CPlusPlus &&
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00003739 "There's a C++-specific check for tok::identifier above");
3740 assert(Tok.getIdentifierInfo() && "Not an identifier?");
3741 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
3742 ConsumeToken();
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003743 goto PastIdentifier;
3744 }
3745
3746 if (Tok.is(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003747 // direct-declarator: '(' declarator ')'
3748 // direct-declarator: '(' attributes declarator ')'
3749 // Example: 'char (*X)' or 'int (*XX)(void)'
3750 ParseParenDeclarator(D);
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003751
3752 // If the declarator was parenthesized, we entered the declarator
3753 // scope when parsing the parenthesized declarator, then exited
3754 // the scope already. Re-enter the scope, if we need to.
3755 if (D.getCXXScopeSpec().isSet()) {
Fariborz Jahanian46877cd2010-08-17 23:50:37 +00003756 // If there was an error parsing parenthesized declarator, declarator
3757 // scope may have been enterred before. Don't do it again.
3758 if (!D.isInvalidType() &&
3759 Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003760 // Change the declaration context for name lookup, until this function
3761 // is exited (and the declarator has been parsed).
Fariborz Jahanian46877cd2010-08-17 23:50:37 +00003762 DeclScopeObj.EnterDeclaratorScope();
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003763 }
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00003764 } else if (D.mayOmitIdentifier()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003765 // This could be something simple like "int" (in which case the declarator
3766 // portion is empty), if an abstract-declarator is allowed.
3767 D.SetIdentifier(0, Tok.getLocation());
3768 } else {
Douglas Gregore950d4b2009-03-06 23:28:18 +00003769 if (D.getContext() == Declarator::MemberContext)
3770 Diag(Tok, diag::err_expected_member_name_or_semi)
3771 << D.getDeclSpec().getSourceRange();
David Blaikie4e4d0842012-03-11 07:00:24 +00003772 else if (getLangOpts().CPlusPlus)
3773 Diag(Tok, diag::err_expected_unqualified_id) << getLangOpts().CPlusPlus;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003774 else
Chris Lattner1ab3b962008-11-18 07:48:38 +00003775 Diag(Tok, diag::err_expected_ident_lparen);
Reid Spencer5f016e22007-07-11 17:01:13 +00003776 D.SetIdentifier(0, Tok.getLocation());
Chris Lattner1f6f54b2008-11-11 06:13:16 +00003777 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00003778 }
Mike Stump1eb44332009-09-09 15:08:12 +00003779
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00003780 PastIdentifier:
Reid Spencer5f016e22007-07-11 17:01:13 +00003781 assert(D.isPastIdentifier() &&
3782 "Haven't past the location of the identifier yet?");
Mike Stump1eb44332009-09-09 15:08:12 +00003783
Sean Huntbbd37c62009-11-21 08:43:09 +00003784 // Don't parse attributes unless we have an identifier.
John McCall7f040a92010-12-24 02:08:15 +00003785 if (D.getIdentifier())
3786 MaybeParseCXX0XAttributes(D);
Sean Huntbbd37c62009-11-21 08:43:09 +00003787
Reid Spencer5f016e22007-07-11 17:01:13 +00003788 while (1) {
Chris Lattner04d66662007-10-09 17:33:22 +00003789 if (Tok.is(tok::l_paren)) {
David Blaikie42d6d0c2011-12-04 05:04:18 +00003790 // Enter function-declaration scope, limiting any declarators to the
3791 // function prototype scope, including parameter declarators.
3792 ParseScope PrototypeScope(this,
3793 Scope::FunctionPrototypeScope|Scope::DeclScope);
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00003794 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
3795 // In such a case, check if we actually have a function declarator; if it
3796 // is not, the declarator has been fully parsed.
David Blaikie4e4d0842012-03-11 07:00:24 +00003797 if (getLangOpts().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
Chris Lattner7399ee02008-10-20 02:05:46 +00003798 // When not in file scope, warn for ambiguous function declarators, just
3799 // in case the author intended it as a variable definition.
3800 bool warnIfAmbiguous = D.getContext() != Declarator::FileContext;
3801 if (!isCXXFunctionDeclarator(warnIfAmbiguous))
3802 break;
3803 }
John McCall0b7e6782011-03-24 11:26:52 +00003804 ParsedAttributes attrs(AttrFactory);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003805 BalancedDelimiterTracker T(*this, tok::l_paren);
3806 T.consumeOpen();
3807 ParseFunctionDeclarator(D, attrs, T);
David Blaikie42d6d0c2011-12-04 05:04:18 +00003808 PrototypeScope.Exit();
Chris Lattner04d66662007-10-09 17:33:22 +00003809 } else if (Tok.is(tok::l_square)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003810 ParseBracketDeclarator(D);
3811 } else {
3812 break;
3813 }
3814 }
David Blaikie42d6d0c2011-12-04 05:04:18 +00003815}
Reid Spencer5f016e22007-07-11 17:01:13 +00003816
Chris Lattneref4715c2008-04-06 05:45:57 +00003817/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
3818/// only called before the identifier, so these are most likely just grouping
Mike Stump1eb44332009-09-09 15:08:12 +00003819/// parens for precedence. If we find that these are actually function
Chris Lattneref4715c2008-04-06 05:45:57 +00003820/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
3821///
3822/// direct-declarator:
3823/// '(' declarator ')'
3824/// [GNU] '(' attributes declarator ')'
Chris Lattner7399ee02008-10-20 02:05:46 +00003825/// direct-declarator '(' parameter-type-list ')'
3826/// direct-declarator '(' identifier-list[opt] ')'
3827/// [GNU] direct-declarator '(' parameter-forward-declarations
3828/// parameter-type-list[opt] ')'
Chris Lattneref4715c2008-04-06 05:45:57 +00003829///
3830void Parser::ParseParenDeclarator(Declarator &D) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003831 BalancedDelimiterTracker T(*this, tok::l_paren);
3832 T.consumeOpen();
3833
Chris Lattneref4715c2008-04-06 05:45:57 +00003834 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
Mike Stump1eb44332009-09-09 15:08:12 +00003835
Chris Lattner7399ee02008-10-20 02:05:46 +00003836 // Eat any attributes before we look at whether this is a grouping or function
3837 // declarator paren. If this is a grouping paren, the attribute applies to
3838 // the type being built up, for example:
3839 // int (__attribute__(()) *x)(long y)
3840 // If this ends up not being a grouping paren, the attribute applies to the
3841 // first argument, for example:
3842 // int (__attribute__(()) int x)
3843 // In either case, we need to eat any attributes to be able to determine what
3844 // sort of paren this is.
3845 //
John McCall0b7e6782011-03-24 11:26:52 +00003846 ParsedAttributes attrs(AttrFactory);
Chris Lattner7399ee02008-10-20 02:05:46 +00003847 bool RequiresArg = false;
3848 if (Tok.is(tok::kw___attribute)) {
John McCall7f040a92010-12-24 02:08:15 +00003849 ParseGNUAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00003850
Chris Lattner7399ee02008-10-20 02:05:46 +00003851 // We require that the argument list (if this is a non-grouping paren) be
3852 // present even if the attribute list was empty.
3853 RequiresArg = true;
3854 }
Steve Naroff239f0732008-12-25 14:16:32 +00003855 // Eat any Microsoft extensions.
Eli Friedman290eeb02009-06-08 23:27:34 +00003856 if (Tok.is(tok::kw___cdecl) || Tok.is(tok::kw___stdcall) ||
Douglas Gregorf813a2c2010-05-18 16:57:00 +00003857 Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___fastcall) ||
Francois Pichet3bd9aa42011-08-18 09:59:55 +00003858 Tok.is(tok::kw___w64) || Tok.is(tok::kw___ptr64) ||
Francois Pichet58fd97a2011-08-25 00:36:46 +00003859 Tok.is(tok::kw___ptr32) || Tok.is(tok::kw___unaligned)) {
John McCall7f040a92010-12-24 02:08:15 +00003860 ParseMicrosoftTypeAttributes(attrs);
Eli Friedman290eeb02009-06-08 23:27:34 +00003861 }
Dawn Perchik52fc3142010-09-03 01:29:35 +00003862 // Eat any Borland extensions.
Ted Kremenek8113ecf2010-11-10 05:59:39 +00003863 if (Tok.is(tok::kw___pascal))
John McCall7f040a92010-12-24 02:08:15 +00003864 ParseBorlandTypeAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00003865
Chris Lattneref4715c2008-04-06 05:45:57 +00003866 // If we haven't past the identifier yet (or where the identifier would be
3867 // stored, if this is an abstract declarator), then this is probably just
3868 // grouping parens. However, if this could be an abstract-declarator, then
3869 // this could also be the start of function arguments (consider 'void()').
3870 bool isGrouping;
Mike Stump1eb44332009-09-09 15:08:12 +00003871
Chris Lattneref4715c2008-04-06 05:45:57 +00003872 if (!D.mayOmitIdentifier()) {
3873 // If this can't be an abstract-declarator, this *must* be a grouping
3874 // paren, because we haven't seen the identifier yet.
3875 isGrouping = true;
3876 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
David Blaikie4e4d0842012-03-11 07:00:24 +00003877 (getLangOpts().CPlusPlus && Tok.is(tok::ellipsis)) || // C++ int(...)
Chris Lattneref4715c2008-04-06 05:45:57 +00003878 isDeclarationSpecifier()) { // 'int(int)' is a function.
3879 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
3880 // considered to be a type, not a K&R identifier-list.
3881 isGrouping = false;
3882 } else {
3883 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
3884 isGrouping = true;
3885 }
Mike Stump1eb44332009-09-09 15:08:12 +00003886
Chris Lattneref4715c2008-04-06 05:45:57 +00003887 // If this is a grouping paren, handle:
3888 // direct-declarator: '(' declarator ')'
3889 // direct-declarator: '(' attributes declarator ')'
3890 if (isGrouping) {
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00003891 bool hadGroupingParens = D.hasGroupingParens();
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00003892 D.setGroupingParens(true);
3893
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003894 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattneref4715c2008-04-06 05:45:57 +00003895 // Match the ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003896 T.consumeClose();
3897 D.AddTypeInfo(DeclaratorChunk::getParen(T.getOpenLocation(),
3898 T.getCloseLocation()),
3899 attrs, T.getCloseLocation());
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00003900
3901 D.setGroupingParens(hadGroupingParens);
Chris Lattneref4715c2008-04-06 05:45:57 +00003902 return;
3903 }
Mike Stump1eb44332009-09-09 15:08:12 +00003904
Chris Lattneref4715c2008-04-06 05:45:57 +00003905 // Okay, if this wasn't a grouping paren, it must be the start of a function
3906 // argument list. Recognize that this declarator will never have an
Chris Lattner7399ee02008-10-20 02:05:46 +00003907 // identifier (and remember where it would have been), then call into
3908 // ParseFunctionDeclarator to handle of argument list.
Chris Lattneref4715c2008-04-06 05:45:57 +00003909 D.SetIdentifier(0, Tok.getLocation());
3910
David Blaikie42d6d0c2011-12-04 05:04:18 +00003911 // Enter function-declaration scope, limiting any declarators to the
3912 // function prototype scope, including parameter declarators.
3913 ParseScope PrototypeScope(this,
3914 Scope::FunctionPrototypeScope|Scope::DeclScope);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003915 ParseFunctionDeclarator(D, attrs, T, RequiresArg);
David Blaikie42d6d0c2011-12-04 05:04:18 +00003916 PrototypeScope.Exit();
Chris Lattneref4715c2008-04-06 05:45:57 +00003917}
3918
3919/// ParseFunctionDeclarator - We are after the identifier and have parsed the
3920/// declarator D up to a paren, which indicates that we are parsing function
3921/// arguments.
Reid Spencer5f016e22007-07-11 17:01:13 +00003922///
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00003923/// If attrs is non-null, then the caller parsed those arguments immediately
Chris Lattner7399ee02008-10-20 02:05:46 +00003924/// after the open paren - they should be considered to be the first argument of
3925/// a parameter. If RequiresArg is true, then the first argument of the
3926/// function is required to be present and required to not be an identifier
3927/// list.
3928///
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00003929/// For C++, after the parameter-list, it also parses cv-qualifier-seq[opt],
3930/// (C++0x) ref-qualifier[opt], exception-specification[opt], and
3931/// (C++0x) trailing-return-type[opt].
3932///
3933/// [C++0x] exception-specification:
3934/// dynamic-exception-specification
3935/// noexcept-specification
3936///
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003937void Parser::ParseFunctionDeclarator(Declarator &D,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00003938 ParsedAttributes &attrs,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003939 BalancedDelimiterTracker &Tracker,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00003940 bool RequiresArg) {
David Blaikie42d6d0c2011-12-04 05:04:18 +00003941 assert(getCurScope()->isFunctionPrototypeScope() &&
3942 "Should call from a Function scope");
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00003943 // lparen is already consumed!
3944 assert(D.isPastIdentifier() && "Should not call before identifier!");
3945
3946 // This should be true when the function has typed arguments.
3947 // Otherwise, it is treated as a K&R-style function.
3948 bool HasProto = false;
3949 // Build up an array of information about the parsed arguments.
Chris Lattner5f9e2722011-07-23 10:55:15 +00003950 SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00003951 // Remember where we see an ellipsis, if any.
3952 SourceLocation EllipsisLoc;
3953
3954 DeclSpec DS(AttrFactory);
3955 bool RefQualifierIsLValueRef = true;
3956 SourceLocation RefQualifierLoc;
Douglas Gregor43f51032011-10-19 06:04:55 +00003957 SourceLocation ConstQualifierLoc;
3958 SourceLocation VolatileQualifierLoc;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00003959 ExceptionSpecificationType ESpecType = EST_None;
3960 SourceRange ESpecRange;
Chris Lattner5f9e2722011-07-23 10:55:15 +00003961 SmallVector<ParsedType, 2> DynamicExceptions;
3962 SmallVector<SourceRange, 2> DynamicExceptionRanges;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00003963 ExprResult NoexceptExpr;
3964 ParsedType TrailingReturnType;
3965
James Molloy16f1f712012-02-29 10:24:19 +00003966 Actions.ActOnStartFunctionDeclarator();
3967
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00003968 SourceLocation EndLoc;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00003969 if (isFunctionDeclaratorIdentifierList()) {
3970 if (RequiresArg)
3971 Diag(Tok, diag::err_argument_required_after_attribute);
3972
3973 ParseFunctionDeclaratorIdentifierList(D, ParamInfo);
3974
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003975 Tracker.consumeClose();
3976 EndLoc = Tracker.getCloseLocation();
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00003977 } else {
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00003978 if (Tok.isNot(tok::r_paren))
3979 ParseParameterDeclarationClause(D, attrs, ParamInfo, EllipsisLoc);
3980 else if (RequiresArg)
3981 Diag(Tok, diag::err_argument_required_after_attribute);
3982
David Blaikie4e4d0842012-03-11 07:00:24 +00003983 HasProto = ParamInfo.size() || getLangOpts().CPlusPlus;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00003984
3985 // If we have the closing ')', eat it.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003986 Tracker.consumeClose();
3987 EndLoc = Tracker.getCloseLocation();
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00003988
David Blaikie4e4d0842012-03-11 07:00:24 +00003989 if (getLangOpts().CPlusPlus) {
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00003990 MaybeParseCXX0XAttributes(attrs);
3991
3992 // Parse cv-qualifier-seq[opt].
3993 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Douglas Gregor43f51032011-10-19 06:04:55 +00003994 if (!DS.getSourceRange().getEnd().isInvalid()) {
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00003995 EndLoc = DS.getSourceRange().getEnd();
Douglas Gregor43f51032011-10-19 06:04:55 +00003996 ConstQualifierLoc = DS.getConstSpecLoc();
3997 VolatileQualifierLoc = DS.getVolatileSpecLoc();
3998 }
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00003999
4000 // Parse ref-qualifier[opt].
4001 if (Tok.is(tok::amp) || Tok.is(tok::ampamp)) {
David Blaikie4e4d0842012-03-11 07:00:24 +00004002 Diag(Tok, getLangOpts().CPlusPlus0x ?
Richard Smith7fe62082011-10-15 05:09:34 +00004003 diag::warn_cxx98_compat_ref_qualifier :
4004 diag::ext_ref_qualifier);
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004005
4006 RefQualifierIsLValueRef = Tok.is(tok::amp);
4007 RefQualifierLoc = ConsumeToken();
4008 EndLoc = RefQualifierLoc;
4009 }
4010
4011 // Parse exception-specification[opt].
4012 ESpecType = MaybeParseExceptionSpecification(ESpecRange,
4013 DynamicExceptions,
4014 DynamicExceptionRanges,
4015 NoexceptExpr);
4016 if (ESpecType != EST_None)
4017 EndLoc = ESpecRange.getEnd();
4018
4019 // Parse trailing-return-type[opt].
David Blaikie4e4d0842012-03-11 07:00:24 +00004020 if (getLangOpts().CPlusPlus0x && Tok.is(tok::arrow)) {
Richard Smith7fe62082011-10-15 05:09:34 +00004021 Diag(Tok, diag::warn_cxx98_compat_trailing_return_type);
Douglas Gregorae7902c2011-08-04 15:30:47 +00004022 SourceRange Range;
4023 TrailingReturnType = ParseTrailingReturnType(Range).get();
4024 if (Range.getEnd().isValid())
4025 EndLoc = Range.getEnd();
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004026 }
4027 }
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004028 }
4029
4030 // Remember that we parsed a function type, and remember the attributes.
4031 D.AddTypeInfo(DeclaratorChunk::getFunction(HasProto,
4032 /*isVariadic=*/EllipsisLoc.isValid(),
4033 EllipsisLoc,
4034 ParamInfo.data(), ParamInfo.size(),
4035 DS.getTypeQualifiers(),
4036 RefQualifierIsLValueRef,
Douglas Gregor43f51032011-10-19 06:04:55 +00004037 RefQualifierLoc, ConstQualifierLoc,
4038 VolatileQualifierLoc,
Douglas Gregor90ebed02011-07-13 21:47:47 +00004039 /*MutableLoc=*/SourceLocation(),
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004040 ESpecType, ESpecRange.getBegin(),
4041 DynamicExceptions.data(),
4042 DynamicExceptionRanges.data(),
4043 DynamicExceptions.size(),
4044 NoexceptExpr.isUsable() ?
4045 NoexceptExpr.get() : 0,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004046 Tracker.getOpenLocation(),
4047 EndLoc, D,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004048 TrailingReturnType),
4049 attrs, EndLoc);
James Molloy16f1f712012-02-29 10:24:19 +00004050
4051 Actions.ActOnEndFunctionDeclarator();
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004052}
4053
4054/// isFunctionDeclaratorIdentifierList - This parameter list may have an
4055/// identifier list form for a K&R-style function: void foo(a,b,c)
4056///
4057/// Note that identifier-lists are only allowed for normal declarators, not for
4058/// abstract-declarators.
4059bool Parser::isFunctionDeclaratorIdentifierList() {
David Blaikie4e4d0842012-03-11 07:00:24 +00004060 return !getLangOpts().CPlusPlus
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004061 && Tok.is(tok::identifier)
4062 && !TryAltiVecVectorToken()
4063 // K&R identifier lists can't have typedefs as identifiers, per C99
4064 // 6.7.5.3p11.
4065 && (TryAnnotateTypeOrScopeToken() || !Tok.is(tok::annot_typename))
4066 // Identifier lists follow a really simple grammar: the identifiers can
4067 // be followed *only* by a ", identifier" or ")". However, K&R
4068 // identifier lists are really rare in the brave new modern world, and
4069 // it is very common for someone to typo a type in a non-K&R style
4070 // list. If we are presented with something like: "void foo(intptr x,
4071 // float y)", we don't want to start parsing the function declarator as
4072 // though it is a K&R style declarator just because intptr is an
4073 // invalid type.
4074 //
4075 // To handle this, we check to see if the token after the first
4076 // identifier is a "," or ")". Only then do we parse it as an
4077 // identifier list.
4078 && (NextToken().is(tok::comma) || NextToken().is(tok::r_paren));
4079}
4080
4081/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
4082/// we found a K&R-style identifier list instead of a typed parameter list.
4083///
4084/// After returning, ParamInfo will hold the parsed parameters.
4085///
4086/// identifier-list: [C99 6.7.5]
4087/// identifier
4088/// identifier-list ',' identifier
4089///
4090void Parser::ParseFunctionDeclaratorIdentifierList(
4091 Declarator &D,
Chris Lattner5f9e2722011-07-23 10:55:15 +00004092 SmallVector<DeclaratorChunk::ParamInfo, 16> &ParamInfo) {
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004093 // If there was no identifier specified for the declarator, either we are in
4094 // an abstract-declarator, or we are in a parameter declarator which was found
4095 // to be abstract. In abstract-declarators, identifier lists are not valid:
4096 // diagnose this.
4097 if (!D.getIdentifier())
4098 Diag(Tok, diag::ext_ident_list_in_param);
4099
4100 // Maintain an efficient lookup of params we have seen so far.
4101 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
4102
4103 while (1) {
4104 // If this isn't an identifier, report the error and skip until ')'.
4105 if (Tok.isNot(tok::identifier)) {
4106 Diag(Tok, diag::err_expected_ident);
4107 SkipUntil(tok::r_paren, /*StopAtSemi=*/true, /*DontConsume=*/true);
4108 // Forget we parsed anything.
4109 ParamInfo.clear();
4110 return;
4111 }
4112
4113 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
4114
4115 // Reject 'typedef int y; int test(x, y)', but continue parsing.
4116 if (Actions.getTypeName(*ParmII, Tok.getLocation(), getCurScope()))
4117 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
4118
4119 // Verify that the argument identifier has not already been mentioned.
4120 if (!ParamsSoFar.insert(ParmII)) {
4121 Diag(Tok, diag::err_param_redefinition) << ParmII;
4122 } else {
4123 // Remember this identifier in ParamInfo.
4124 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
4125 Tok.getLocation(),
4126 0));
4127 }
4128
4129 // Eat the identifier.
4130 ConsumeToken();
4131
4132 // The list continues if we see a comma.
4133 if (Tok.isNot(tok::comma))
4134 break;
4135 ConsumeToken();
4136 }
4137}
4138
4139/// ParseParameterDeclarationClause - Parse a (possibly empty) parameter-list
4140/// after the opening parenthesis. This function will not parse a K&R-style
4141/// identifier list.
4142///
4143/// D is the declarator being parsed. If attrs is non-null, then the caller
4144/// parsed those arguments immediately after the open paren - they should be
4145/// considered to be the first argument of a parameter.
4146///
4147/// After returning, ParamInfo will hold the parsed parameters. EllipsisLoc will
4148/// be the location of the ellipsis, if any was parsed.
4149///
Reid Spencer5f016e22007-07-11 17:01:13 +00004150/// parameter-type-list: [C99 6.7.5]
4151/// parameter-list
4152/// parameter-list ',' '...'
Douglas Gregored5d6512009-09-22 21:41:40 +00004153/// [C++] parameter-list '...'
Reid Spencer5f016e22007-07-11 17:01:13 +00004154///
4155/// parameter-list: [C99 6.7.5]
4156/// parameter-declaration
4157/// parameter-list ',' parameter-declaration
4158///
4159/// parameter-declaration: [C99 6.7.5]
4160/// declaration-specifiers declarator
Chris Lattner04421082008-04-08 04:40:51 +00004161/// [C++] declaration-specifiers declarator '=' assignment-expression
Reid Spencer5f016e22007-07-11 17:01:13 +00004162/// [GNU] declaration-specifiers declarator attributes
Sebastian Redl50de12f2009-03-24 22:27:57 +00004163/// declaration-specifiers abstract-declarator[opt]
4164/// [C++] declaration-specifiers abstract-declarator[opt]
Chris Lattner8123a952008-04-10 02:22:51 +00004165/// '=' assignment-expression
Reid Spencer5f016e22007-07-11 17:01:13 +00004166/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
4167///
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004168void Parser::ParseParameterDeclarationClause(
4169 Declarator &D,
4170 ParsedAttributes &attrs,
Chris Lattner5f9e2722011-07-23 10:55:15 +00004171 SmallVector<DeclaratorChunk::ParamInfo, 16> &ParamInfo,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004172 SourceLocation &EllipsisLoc) {
Mike Stump1eb44332009-09-09 15:08:12 +00004173
Chris Lattnerf97409f2008-04-06 06:57:35 +00004174 while (1) {
4175 if (Tok.is(tok::ellipsis)) {
Douglas Gregor965acbb2009-02-18 07:07:28 +00004176 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chris Lattnerf97409f2008-04-06 06:57:35 +00004177 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00004178 }
Mike Stump1eb44332009-09-09 15:08:12 +00004179
Chris Lattnerf97409f2008-04-06 06:57:35 +00004180 // Parse the declaration-specifiers.
John McCall54abf7d2009-11-04 02:18:39 +00004181 // Just use the ParsingDeclaration "scope" of the declarator.
John McCall0b7e6782011-03-24 11:26:52 +00004182 DeclSpec DS(AttrFactory);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004183
John McCall7f040a92010-12-24 02:08:15 +00004184 // Skip any Microsoft attributes before a param.
David Blaikie4e4d0842012-03-11 07:00:24 +00004185 if (getLangOpts().MicrosoftExt && Tok.is(tok::l_square))
John McCall7f040a92010-12-24 02:08:15 +00004186 ParseMicrosoftAttributes(DS.getAttributes());
4187
4188 SourceLocation DSStart = Tok.getLocation();
Chris Lattner7399ee02008-10-20 02:05:46 +00004189
4190 // If the caller parsed attributes for the first argument, add them now.
John McCall7f040a92010-12-24 02:08:15 +00004191 // Take them so that we only apply the attributes to the first parameter.
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004192 // FIXME: If we saw an ellipsis first, this code is not reached. Are the
4193 // attributes lost? Should they even be allowed?
4194 // FIXME: If we can leave the attributes in the token stream somehow, we can
4195 // get rid of a parameter (attrs) and this statement. It might be too much
4196 // hassle.
John McCall7f040a92010-12-24 02:08:15 +00004197 DS.takeAttributesFrom(attrs);
4198
Chris Lattnere64c5492009-02-27 18:38:20 +00004199 ParseDeclarationSpecifiers(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00004200
Chris Lattnerf97409f2008-04-06 06:57:35 +00004201 // Parse the declarator. This is "PrototypeContext", because we must
4202 // accept either 'declarator' or 'abstract-declarator' here.
4203 Declarator ParmDecl(DS, Declarator::PrototypeContext);
4204 ParseDeclarator(ParmDecl);
4205
4206 // Parse GNU attributes, if present.
John McCall7f040a92010-12-24 02:08:15 +00004207 MaybeParseGNUAttributes(ParmDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00004208
Chris Lattnerf97409f2008-04-06 06:57:35 +00004209 // Remember this parsed parameter in ParamInfo.
4210 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
Mike Stump1eb44332009-09-09 15:08:12 +00004211
Douglas Gregor72b505b2008-12-16 21:30:33 +00004212 // DefArgToks is used when the parsing of default arguments needs
4213 // to be delayed.
4214 CachedTokens *DefArgToks = 0;
4215
Chris Lattnerf97409f2008-04-06 06:57:35 +00004216 // If no parameter was specified, verify that *something* was specified,
4217 // otherwise we have a missing type and identifier.
Chris Lattnere64c5492009-02-27 18:38:20 +00004218 if (DS.isEmpty() && ParmDecl.getIdentifier() == 0 &&
4219 ParmDecl.getNumTypeObjects() == 0) {
Chris Lattnerf97409f2008-04-06 06:57:35 +00004220 // Completely missing, emit error.
4221 Diag(DSStart, diag::err_missing_param);
4222 } else {
4223 // Otherwise, we have something. Add it and let semantic analysis try
4224 // to grok it and add the result to the ParamInfo we are building.
Mike Stump1eb44332009-09-09 15:08:12 +00004225
Chris Lattnerf97409f2008-04-06 06:57:35 +00004226 // Inform the actions module about the parameter declarator, so it gets
4227 // added to the current scope.
John McCalld226f652010-08-21 09:40:31 +00004228 Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl);
Chris Lattner04421082008-04-08 04:40:51 +00004229
4230 // Parse the default argument, if any. We parse the default
4231 // arguments in all dialects; the semantic analysis in
4232 // ActOnParamDefaultArgument will reject the default argument in
4233 // C.
4234 if (Tok.is(tok::equal)) {
Douglas Gregor61366e92008-12-24 00:01:03 +00004235 SourceLocation EqualLoc = Tok.getLocation();
4236
Chris Lattner04421082008-04-08 04:40:51 +00004237 // Parse the default argument
Douglas Gregor72b505b2008-12-16 21:30:33 +00004238 if (D.getContext() == Declarator::MemberContext) {
4239 // If we're inside a class definition, cache the tokens
4240 // corresponding to the default argument. We'll actually parse
4241 // them when we see the end of the class definition.
4242 // FIXME: Templates will require something similar.
4243 // FIXME: Can we use a smart pointer for Toks?
4244 DefArgToks = new CachedTokens;
4245
Mike Stump1eb44332009-09-09 15:08:12 +00004246 if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks,
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +00004247 /*StopAtSemi=*/true,
4248 /*ConsumeFinalToken=*/false)) {
Douglas Gregor72b505b2008-12-16 21:30:33 +00004249 delete DefArgToks;
4250 DefArgToks = 0;
Douglas Gregor61366e92008-12-24 00:01:03 +00004251 Actions.ActOnParamDefaultArgumentError(Param);
Argyrios Kyrtzidis2b602ad2010-08-06 09:47:24 +00004252 } else {
4253 // Mark the end of the default argument so that we know when to
4254 // stop when we parse it later on.
4255 Token DefArgEnd;
4256 DefArgEnd.startToken();
4257 DefArgEnd.setKind(tok::cxx_defaultarg_end);
4258 DefArgEnd.setLocation(Tok.getLocation());
4259 DefArgToks->push_back(DefArgEnd);
Mike Stump1eb44332009-09-09 15:08:12 +00004260 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc,
Anders Carlsson5e300d12009-06-12 16:51:40 +00004261 (*DefArgToks)[1].getLocation());
Argyrios Kyrtzidis2b602ad2010-08-06 09:47:24 +00004262 }
Chris Lattner04421082008-04-08 04:40:51 +00004263 } else {
Douglas Gregor72b505b2008-12-16 21:30:33 +00004264 // Consume the '='.
Douglas Gregor61366e92008-12-24 00:01:03 +00004265 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00004266
Douglas Gregorbe0f7bd2010-09-11 20:24:53 +00004267 // The argument isn't actually potentially evaluated unless it is
4268 // used.
4269 EnterExpressionEvaluationContext Eval(Actions,
Douglas Gregorccc1b5e2012-02-21 00:37:24 +00004270 Sema::PotentiallyEvaluatedIfUsed,
4271 Param);
Douglas Gregorbe0f7bd2010-09-11 20:24:53 +00004272
John McCall60d7b3a2010-08-24 06:29:42 +00004273 ExprResult DefArgResult(ParseAssignmentExpression());
Douglas Gregor72b505b2008-12-16 21:30:33 +00004274 if (DefArgResult.isInvalid()) {
4275 Actions.ActOnParamDefaultArgumentError(Param);
4276 SkipUntil(tok::comma, tok::r_paren, true, true);
4277 } else {
4278 // Inform the actions module about the default argument
4279 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
John McCall9ae2f072010-08-23 23:25:46 +00004280 DefArgResult.take());
Douglas Gregor72b505b2008-12-16 21:30:33 +00004281 }
Chris Lattner04421082008-04-08 04:40:51 +00004282 }
4283 }
Mike Stump1eb44332009-09-09 15:08:12 +00004284
4285 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
4286 ParmDecl.getIdentifierLoc(), Param,
Douglas Gregor72b505b2008-12-16 21:30:33 +00004287 DefArgToks));
Chris Lattnerf97409f2008-04-06 06:57:35 +00004288 }
4289
4290 // If the next token is a comma, consume it and keep reading arguments.
Douglas Gregored5d6512009-09-22 21:41:40 +00004291 if (Tok.isNot(tok::comma)) {
4292 if (Tok.is(tok::ellipsis)) {
Douglas Gregored5d6512009-09-22 21:41:40 +00004293 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
4294
David Blaikie4e4d0842012-03-11 07:00:24 +00004295 if (!getLangOpts().CPlusPlus) {
Douglas Gregored5d6512009-09-22 21:41:40 +00004296 // We have ellipsis without a preceding ',', which is ill-formed
4297 // in C. Complain and provide the fix.
4298 Diag(EllipsisLoc, diag::err_missing_comma_before_ellipsis)
Douglas Gregor849b2432010-03-31 17:46:05 +00004299 << FixItHint::CreateInsertion(EllipsisLoc, ", ");
Douglas Gregored5d6512009-09-22 21:41:40 +00004300 }
4301 }
4302
4303 break;
4304 }
Mike Stump1eb44332009-09-09 15:08:12 +00004305
Chris Lattnerf97409f2008-04-06 06:57:35 +00004306 // Consume the comma.
4307 ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00004308 }
Mike Stump1eb44332009-09-09 15:08:12 +00004309
Chris Lattner66d28652008-04-06 06:34:08 +00004310}
Chris Lattneref4715c2008-04-06 05:45:57 +00004311
Reid Spencer5f016e22007-07-11 17:01:13 +00004312/// [C90] direct-declarator '[' constant-expression[opt] ']'
4313/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
4314/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
4315/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
4316/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
4317void Parser::ParseBracketDeclarator(Declarator &D) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004318 BalancedDelimiterTracker T(*this, tok::l_square);
4319 T.consumeOpen();
Mike Stump1eb44332009-09-09 15:08:12 +00004320
Chris Lattner378c7e42008-12-18 07:27:21 +00004321 // C array syntax has many features, but by-far the most common is [] and [4].
4322 // This code does a fast path to handle some of the most obvious cases.
4323 if (Tok.getKind() == tok::r_square) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004324 T.consumeClose();
John McCall0b7e6782011-03-24 11:26:52 +00004325 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00004326 MaybeParseCXX0XAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00004327
Chris Lattner378c7e42008-12-18 07:27:21 +00004328 // Remember that we parsed the empty array type.
John McCall60d7b3a2010-08-24 06:29:42 +00004329 ExprResult NumElements;
John McCall0b7e6782011-03-24 11:26:52 +00004330 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004331 T.getOpenLocation(),
4332 T.getCloseLocation()),
4333 attrs, T.getCloseLocation());
Chris Lattner378c7e42008-12-18 07:27:21 +00004334 return;
4335 } else if (Tok.getKind() == tok::numeric_constant &&
4336 GetLookAheadToken(1).is(tok::r_square)) {
4337 // [4] is very common. Parse the numeric constant expression.
Richard Smith36f5cfe2012-03-09 08:00:36 +00004338 ExprResult ExprRes(Actions.ActOnNumericConstant(Tok, getCurScope()));
Chris Lattner378c7e42008-12-18 07:27:21 +00004339 ConsumeToken();
4340
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004341 T.consumeClose();
John McCall0b7e6782011-03-24 11:26:52 +00004342 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00004343 MaybeParseCXX0XAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00004344
Chris Lattner378c7e42008-12-18 07:27:21 +00004345 // Remember that we parsed a array type, and remember its features.
John McCall0b7e6782011-03-24 11:26:52 +00004346 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, 0,
John McCall7f040a92010-12-24 02:08:15 +00004347 ExprRes.release(),
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004348 T.getOpenLocation(),
4349 T.getCloseLocation()),
4350 attrs, T.getCloseLocation());
Chris Lattner378c7e42008-12-18 07:27:21 +00004351 return;
4352 }
Mike Stump1eb44332009-09-09 15:08:12 +00004353
Reid Spencer5f016e22007-07-11 17:01:13 +00004354 // If valid, this location is the position where we read the 'static' keyword.
4355 SourceLocation StaticLoc;
Chris Lattner04d66662007-10-09 17:33:22 +00004356 if (Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00004357 StaticLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00004358
Reid Spencer5f016e22007-07-11 17:01:13 +00004359 // If there is a type-qualifier-list, read it now.
Chris Lattnera1fcbad2008-12-18 06:50:14 +00004360 // Type qualifiers in an array subscript are a C99 feature.
John McCall0b7e6782011-03-24 11:26:52 +00004361 DeclSpec DS(AttrFactory);
Chris Lattner5a69d1c2008-12-18 07:02:59 +00004362 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Mike Stump1eb44332009-09-09 15:08:12 +00004363
Reid Spencer5f016e22007-07-11 17:01:13 +00004364 // If we haven't already read 'static', check to see if there is one after the
4365 // type-qualifier-list.
Chris Lattner04d66662007-10-09 17:33:22 +00004366 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00004367 StaticLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00004368
Reid Spencer5f016e22007-07-11 17:01:13 +00004369 // Handle "direct-declarator [ type-qual-list[opt] * ]".
4370 bool isStar = false;
John McCall60d7b3a2010-08-24 06:29:42 +00004371 ExprResult NumElements;
Mike Stump1eb44332009-09-09 15:08:12 +00004372
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00004373 // Handle the case where we have '[*]' as the array size. However, a leading
4374 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
4375 // the the token after the star is a ']'. Since stars in arrays are
4376 // infrequent, use of lookahead is not costly here.
4377 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattnera711dd02008-04-06 05:27:21 +00004378 ConsumeToken(); // Eat the '*'.
Reid Spencer5f016e22007-07-11 17:01:13 +00004379
Chris Lattnera1fcbad2008-12-18 06:50:14 +00004380 if (StaticLoc.isValid()) {
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00004381 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattnera1fcbad2008-12-18 06:50:14 +00004382 StaticLoc = SourceLocation(); // Drop the static.
4383 }
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00004384 isStar = true;
Chris Lattner04d66662007-10-09 17:33:22 +00004385 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner378c7e42008-12-18 07:27:21 +00004386 // Note, in C89, this production uses the constant-expr production instead
4387 // of assignment-expr. The only difference is that assignment-expr allows
4388 // things like '=' and '*='. Sema rejects these in C89 mode because they
4389 // are not i-c-e's, so we don't need to distinguish between the two here.
Mike Stump1eb44332009-09-09 15:08:12 +00004390
Douglas Gregore0762c92009-06-19 23:52:42 +00004391 // Parse the constant-expression or assignment-expression now (depending
4392 // on dialect).
David Blaikie4e4d0842012-03-11 07:00:24 +00004393 if (getLangOpts().CPlusPlus) {
Douglas Gregore0762c92009-06-19 23:52:42 +00004394 NumElements = ParseConstantExpression();
Eli Friedman71b8fb52012-01-21 01:01:51 +00004395 } else {
4396 EnterExpressionEvaluationContext Unevaluated(Actions,
4397 Sema::ConstantEvaluated);
Douglas Gregore0762c92009-06-19 23:52:42 +00004398 NumElements = ParseAssignmentExpression();
Eli Friedman71b8fb52012-01-21 01:01:51 +00004399 }
Reid Spencer5f016e22007-07-11 17:01:13 +00004400 }
Mike Stump1eb44332009-09-09 15:08:12 +00004401
Reid Spencer5f016e22007-07-11 17:01:13 +00004402 // If there was an error parsing the assignment-expression, recover.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00004403 if (NumElements.isInvalid()) {
Chris Lattner5cb10d32009-04-24 22:30:50 +00004404 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00004405 // If the expression was invalid, skip it.
4406 SkipUntil(tok::r_square);
4407 return;
4408 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00004409
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004410 T.consumeClose();
Sebastian Redlab197ba2009-02-09 18:23:29 +00004411
John McCall0b7e6782011-03-24 11:26:52 +00004412 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00004413 MaybeParseCXX0XAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00004414
Chris Lattner378c7e42008-12-18 07:27:21 +00004415 // Remember that we parsed a array type, and remember its features.
John McCall0b7e6782011-03-24 11:26:52 +00004416 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
Reid Spencer5f016e22007-07-11 17:01:13 +00004417 StaticLoc.isValid(), isStar,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00004418 NumElements.release(),
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004419 T.getOpenLocation(),
4420 T.getCloseLocation()),
4421 attrs, T.getCloseLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00004422}
4423
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004424/// [GNU] typeof-specifier:
4425/// typeof ( expressions )
4426/// typeof ( type-name )
4427/// [GNU/C++] typeof unary-expression
Steve Naroffd1861fd2007-07-31 12:34:36 +00004428///
4429void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner04d66662007-10-09 17:33:22 +00004430 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004431 Token OpTok = Tok;
Steve Naroffd1861fd2007-07-31 12:34:36 +00004432 SourceLocation StartLoc = ConsumeToken();
4433
John McCallcfb708c2010-01-13 20:03:27 +00004434 const bool hasParens = Tok.is(tok::l_paren);
4435
Eli Friedman71b8fb52012-01-21 01:01:51 +00004436 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated);
4437
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004438 bool isCastExpr;
John McCallb3d87482010-08-24 05:47:05 +00004439 ParsedType CastTy;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004440 SourceRange CastRange;
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00004441 ExprResult Operand = ParseExprAfterUnaryExprOrTypeTrait(OpTok, isCastExpr,
4442 CastTy, CastRange);
John McCallcfb708c2010-01-13 20:03:27 +00004443 if (hasParens)
4444 DS.setTypeofParensRange(CastRange);
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004445
4446 if (CastRange.getEnd().isInvalid())
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004447 // FIXME: Not accurate, the range gets one token more than it should.
4448 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004449 else
4450 DS.SetRangeEnd(CastRange.getEnd());
Mike Stump1eb44332009-09-09 15:08:12 +00004451
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004452 if (isCastExpr) {
4453 if (!CastTy) {
4454 DS.SetTypeSpecError();
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004455 return;
Douglas Gregor809070a2009-02-18 17:45:20 +00004456 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004457
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004458 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00004459 unsigned DiagID;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004460 // Check for duplicate type specifiers (e.g. "int typeof(int)").
4461 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00004462 DiagID, CastTy))
4463 Diag(StartLoc, DiagID) << PrevSpec;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004464 return;
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004465 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004466
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004467 // If we get here, the operand to the typeof was an expresion.
4468 if (Operand.isInvalid()) {
4469 DS.SetTypeSpecError();
Steve Naroff9dfa7b42007-08-02 02:53:48 +00004470 return;
Steve Naroffd1861fd2007-07-31 12:34:36 +00004471 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004472
Eli Friedman71b8fb52012-01-21 01:01:51 +00004473 // We might need to transform the operand if it is potentially evaluated.
4474 Operand = Actions.HandleExprEvaluationContextForTypeof(Operand.get());
4475 if (Operand.isInvalid()) {
4476 DS.SetTypeSpecError();
4477 return;
4478 }
4479
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004480 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00004481 unsigned DiagID;
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004482 // Check for duplicate type specifiers (e.g. "int typeof(int)").
4483 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
John McCallb3d87482010-08-24 05:47:05 +00004484 DiagID, Operand.get()))
John McCallfec54012009-08-03 20:12:06 +00004485 Diag(StartLoc, DiagID) << PrevSpec;
Steve Naroffd1861fd2007-07-31 12:34:36 +00004486}
Chris Lattner1b492422010-02-28 18:33:55 +00004487
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00004488/// [C11] atomic-specifier:
Eli Friedmanb001de72011-10-06 23:00:33 +00004489/// _Atomic ( type-name )
4490///
4491void Parser::ParseAtomicSpecifier(DeclSpec &DS) {
4492 assert(Tok.is(tok::kw__Atomic) && "Not an atomic specifier");
4493
4494 SourceLocation StartLoc = ConsumeToken();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004495 BalancedDelimiterTracker T(*this, tok::l_paren);
4496 if (T.expectAndConsume(diag::err_expected_lparen_after, "_Atomic")) {
Eli Friedmanb001de72011-10-06 23:00:33 +00004497 SkipUntil(tok::r_paren);
4498 return;
4499 }
4500
4501 TypeResult Result = ParseTypeName();
4502 if (Result.isInvalid()) {
4503 SkipUntil(tok::r_paren);
4504 return;
4505 }
4506
4507 // Match the ')'
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004508 T.consumeClose();
Eli Friedmanb001de72011-10-06 23:00:33 +00004509
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004510 if (T.getCloseLocation().isInvalid())
Eli Friedmanb001de72011-10-06 23:00:33 +00004511 return;
4512
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004513 DS.setTypeofParensRange(T.getRange());
4514 DS.SetRangeEnd(T.getCloseLocation());
Eli Friedmanb001de72011-10-06 23:00:33 +00004515
4516 const char *PrevSpec = 0;
4517 unsigned DiagID;
4518 if (DS.SetTypeSpecType(DeclSpec::TST_atomic, StartLoc, PrevSpec,
4519 DiagID, Result.release()))
4520 Diag(StartLoc, DiagID) << PrevSpec;
4521}
4522
Chris Lattner1b492422010-02-28 18:33:55 +00004523
4524/// TryAltiVecVectorTokenOutOfLine - Out of line body that should only be called
4525/// from TryAltiVecVectorToken.
4526bool Parser::TryAltiVecVectorTokenOutOfLine() {
4527 Token Next = NextToken();
4528 switch (Next.getKind()) {
4529 default: return false;
4530 case tok::kw_short:
4531 case tok::kw_long:
4532 case tok::kw_signed:
4533 case tok::kw_unsigned:
4534 case tok::kw_void:
4535 case tok::kw_char:
4536 case tok::kw_int:
4537 case tok::kw_float:
4538 case tok::kw_double:
4539 case tok::kw_bool:
4540 case tok::kw___pixel:
4541 Tok.setKind(tok::kw___vector);
4542 return true;
4543 case tok::identifier:
4544 if (Next.getIdentifierInfo() == Ident_pixel) {
4545 Tok.setKind(tok::kw___vector);
4546 return true;
4547 }
4548 return false;
4549 }
4550}
4551
4552bool Parser::TryAltiVecTokenOutOfLine(DeclSpec &DS, SourceLocation Loc,
4553 const char *&PrevSpec, unsigned &DiagID,
4554 bool &isInvalid) {
4555 if (Tok.getIdentifierInfo() == Ident_vector) {
4556 Token Next = NextToken();
4557 switch (Next.getKind()) {
4558 case tok::kw_short:
4559 case tok::kw_long:
4560 case tok::kw_signed:
4561 case tok::kw_unsigned:
4562 case tok::kw_void:
4563 case tok::kw_char:
4564 case tok::kw_int:
4565 case tok::kw_float:
4566 case tok::kw_double:
4567 case tok::kw_bool:
4568 case tok::kw___pixel:
4569 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
4570 return true;
4571 case tok::identifier:
4572 if (Next.getIdentifierInfo() == Ident_pixel) {
4573 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
4574 return true;
4575 }
4576 break;
4577 default:
4578 break;
4579 }
Douglas Gregora8f031f2010-06-16 15:28:57 +00004580 } else if ((Tok.getIdentifierInfo() == Ident_pixel) &&
Chris Lattner1b492422010-02-28 18:33:55 +00004581 DS.isTypeAltiVecVector()) {
4582 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
4583 return true;
4584 }
4585 return false;
4586}