blob: 6de9e1b4ebb4c16bd7e5bdfdb5963d8a7e453204 [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"
Caitlin Sadowskib51e0312011-08-09 17:59:31 +000022#include "llvm/ADT/StringSwitch.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000023using namespace clang;
24
25//===----------------------------------------------------------------------===//
26// C99 6.7: Declarations.
27//===----------------------------------------------------------------------===//
28
29/// ParseTypeName
30/// type-name: [C99 6.7.6]
31/// specifier-qualifier-list abstract-declarator[opt]
Sebastian Redl4c5d3202008-11-21 19:14:01 +000032///
33/// Called type-id in C++.
Douglas Gregor683a81f2011-01-31 16:09:46 +000034TypeResult Parser::ParseTypeName(SourceRange *Range,
John McCallf85e1932011-06-15 23:02:42 +000035 Declarator::TheContext Context,
Richard Smithc89edf52011-07-01 19:46:12 +000036 AccessSpecifier AS,
37 Decl **OwnedType) {
Reid Spencer5f016e22007-07-11 17:01:13 +000038 // Parse the common declaration-specifiers piece.
John McCall0b7e6782011-03-24 11:26:52 +000039 DeclSpec DS(AttrFactory);
Richard Smithc89edf52011-07-01 19:46:12 +000040 ParseSpecifierQualifierList(DS, AS);
41 if (OwnedType)
42 *OwnedType = DS.isTypeSpecOwned() ? DS.getRepAsDecl() : 0;
Sebastian Redlef65f062009-05-29 18:02:33 +000043
Reid Spencer5f016e22007-07-11 17:01:13 +000044 // Parse the abstract-declarator, if present.
Douglas Gregor683a81f2011-01-31 16:09:46 +000045 Declarator DeclaratorInfo(DS, Context);
Reid Spencer5f016e22007-07-11 17:01:13 +000046 ParseDeclarator(DeclaratorInfo);
Sebastian Redlef65f062009-05-29 18:02:33 +000047 if (Range)
48 *Range = DeclaratorInfo.getSourceRange();
49
Chris Lattnereaaebc72009-04-25 08:06:05 +000050 if (DeclaratorInfo.isInvalidType())
Douglas Gregor809070a2009-02-18 17:45:20 +000051 return true;
52
Douglas Gregor23c94db2010-07-02 17:43:08 +000053 return Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
Reid Spencer5f016e22007-07-11 17:01:13 +000054}
55
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +000056
57/// isAttributeLateParsed - Return true if the attribute has arguments that
58/// require late parsing.
59static bool isAttributeLateParsed(const IdentifierInfo &II) {
60 return llvm::StringSwitch<bool>(II.getName())
61#include "clang/Parse/AttrLateParsed.inc"
62 .Default(false);
63}
64
65
Sean Huntbbd37c62009-11-21 08:43:09 +000066/// ParseGNUAttributes - Parse a non-empty attributes list.
Reid Spencer5f016e22007-07-11 17:01:13 +000067///
68/// [GNU] attributes:
69/// attribute
70/// attributes attribute
71///
72/// [GNU] attribute:
73/// '__attribute__' '(' '(' attribute-list ')' ')'
74///
75/// [GNU] attribute-list:
76/// attrib
77/// attribute_list ',' attrib
78///
79/// [GNU] attrib:
80/// empty
81/// attrib-name
82/// attrib-name '(' identifier ')'
83/// attrib-name '(' identifier ',' nonempty-expr-list ')'
84/// attrib-name '(' argument-expression-list [C99 6.5.2] ')'
85///
86/// [GNU] attrib-name:
87/// identifier
88/// typespec
89/// typequal
90/// storageclass
Mike Stump1eb44332009-09-09 15:08:12 +000091///
Reid Spencer5f016e22007-07-11 17:01:13 +000092/// FIXME: The GCC grammar/code for this construct implies we need two
Mike Stump1eb44332009-09-09 15:08:12 +000093/// token lookahead. Comment from gcc: "If they start with an identifier
94/// which is followed by a comma or close parenthesis, then the arguments
Reid Spencer5f016e22007-07-11 17:01:13 +000095/// start with that identifier; otherwise they are an expression list."
96///
97/// At the moment, I am not doing 2 token lookahead. I am also unaware of
98/// any attributes that don't work (based on my limited testing). Most
99/// attributes are very simple in practice. Until we find a bug, I don't see
100/// a pressing need to implement the 2 token lookahead.
101
John McCall7f040a92010-12-24 02:08:15 +0000102void Parser::ParseGNUAttributes(ParsedAttributes &attrs,
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000103 SourceLocation *endLoc,
104 LateParsedAttrList *LateAttrs) {
Sean Huntbbd37c62009-11-21 08:43:09 +0000105 assert(Tok.is(tok::kw___attribute) && "Not a GNU attribute list!");
Mike Stump1eb44332009-09-09 15:08:12 +0000106
Chris Lattner04d66662007-10-09 17:33:22 +0000107 while (Tok.is(tok::kw___attribute)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000108 ConsumeToken();
109 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
110 "attribute")) {
111 SkipUntil(tok::r_paren, true); // skip until ) or ;
John McCall7f040a92010-12-24 02:08:15 +0000112 return;
Reid Spencer5f016e22007-07-11 17:01:13 +0000113 }
114 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, "(")) {
115 SkipUntil(tok::r_paren, true); // skip until ) or ;
John McCall7f040a92010-12-24 02:08:15 +0000116 return;
Reid Spencer5f016e22007-07-11 17:01:13 +0000117 }
118 // Parse the attribute-list. e.g. __attribute__(( weak, alias("__f") ))
Chris Lattner04d66662007-10-09 17:33:22 +0000119 while (Tok.is(tok::identifier) || isDeclarationSpecifier() ||
120 Tok.is(tok::comma)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000121 if (Tok.is(tok::comma)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000122 // allows for empty/non-empty attributes. ((__vector_size__(16),,,,))
123 ConsumeToken();
124 continue;
125 }
126 // we have an identifier or declaration specifier (const, int, etc.)
127 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
128 SourceLocation AttrNameLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000129
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000130 if (Tok.is(tok::l_paren)) {
131 // handle "parameterized" attributes
132 if (LateAttrs && !ClassStack.empty() &&
133 isAttributeLateParsed(*AttrName)) {
134 // Delayed parsing is only available for attributes that occur
135 // in certain locations within a class scope.
136 LateParsedAttribute *LA =
137 new LateParsedAttribute(this, *AttrName, AttrNameLoc);
138 LateAttrs->push_back(LA);
139 getCurrentClass().LateParsedDeclarations.push_back(LA);
Mike Stump1eb44332009-09-09 15:08:12 +0000140
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000141 // consume everything up to and including the matching right parens
142 ConsumeAndStoreUntil(tok::r_paren, LA->Toks, true, false);
Mike Stump1eb44332009-09-09 15:08:12 +0000143
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000144 Token Eof;
145 Eof.startToken();
146 Eof.setLocation(Tok.getLocation());
147 LA->Toks.push_back(Eof);
148 } else {
149 ParseGNUAttributeArgs(AttrName, AttrNameLoc, attrs, endLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000150 }
151 } else {
John McCall0b7e6782011-03-24 11:26:52 +0000152 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc,
153 0, SourceLocation(), 0, 0);
Reid Spencer5f016e22007-07-11 17:01:13 +0000154 }
155 }
156 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
Reid Spencer5f016e22007-07-11 17:01:13 +0000157 SkipUntil(tok::r_paren, false);
Sean Huntbbd37c62009-11-21 08:43:09 +0000158 SourceLocation Loc = Tok.getLocation();
Sebastian Redlab197ba2009-02-09 18:23:29 +0000159 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) {
160 SkipUntil(tok::r_paren, false);
161 }
John McCall7f040a92010-12-24 02:08:15 +0000162 if (endLoc)
163 *endLoc = Loc;
Reid Spencer5f016e22007-07-11 17:01:13 +0000164 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000165}
166
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000167
168/// Parse the arguments to a parameterized GNU attribute
169void Parser::ParseGNUAttributeArgs(IdentifierInfo *AttrName,
170 SourceLocation AttrNameLoc,
171 ParsedAttributes &Attrs,
172 SourceLocation *EndLoc) {
173
174 assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('");
175
176 // Availability attributes have their own grammar.
177 if (AttrName->isStr("availability")) {
178 ParseAvailabilityAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc);
179 return;
180 }
181 // Thread safety attributes fit into the FIXME case above, so we
182 // just parse the arguments as a list of expressions
183 if (IsThreadSafetyAttribute(AttrName->getName())) {
184 ParseThreadSafetyAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc);
185 return;
186 }
187
188 ConsumeParen(); // ignore the left paren loc for now
189
190 if (Tok.is(tok::identifier)) {
191 IdentifierInfo *ParmName = Tok.getIdentifierInfo();
192 SourceLocation ParmLoc = ConsumeToken();
193
194 if (Tok.is(tok::r_paren)) {
195 // __attribute__(( mode(byte) ))
Argyrios Kyrtzidisffcc3102011-09-13 16:05:53 +0000196 SourceLocation RParen = ConsumeParen();
197 Attrs.addNew(AttrName, SourceRange(AttrNameLoc, RParen), 0, AttrNameLoc,
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000198 ParmName, ParmLoc, 0, 0);
199 } else if (Tok.is(tok::comma)) {
200 ConsumeToken();
201 // __attribute__(( format(printf, 1, 2) ))
202 ExprVector ArgExprs(Actions);
203 bool ArgExprsOk = true;
204
205 // now parse the non-empty comma separated list of expressions
206 while (1) {
207 ExprResult ArgExpr(ParseAssignmentExpression());
208 if (ArgExpr.isInvalid()) {
209 ArgExprsOk = false;
210 SkipUntil(tok::r_paren);
211 break;
212 } else {
213 ArgExprs.push_back(ArgExpr.release());
214 }
215 if (Tok.isNot(tok::comma))
216 break;
217 ConsumeToken(); // Eat the comma, move to the next argument
218 }
219 if (ArgExprsOk && Tok.is(tok::r_paren)) {
Argyrios Kyrtzidisffcc3102011-09-13 16:05:53 +0000220 SourceLocation RParen = ConsumeParen();
221 Attrs.addNew(AttrName, SourceRange(AttrNameLoc, RParen), 0, AttrNameLoc,
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000222 ParmName, ParmLoc, ArgExprs.take(), ArgExprs.size());
223 }
224 }
225 } else { // not an identifier
226 switch (Tok.getKind()) {
Argyrios Kyrtzidisffcc3102011-09-13 16:05:53 +0000227 case tok::r_paren: {
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000228 // parse a possibly empty comma separated list of expressions
229 // __attribute__(( nonnull() ))
Argyrios Kyrtzidisffcc3102011-09-13 16:05:53 +0000230 SourceLocation RParen = ConsumeParen();
231 Attrs.addNew(AttrName, SourceRange(AttrNameLoc, RParen), 0, AttrNameLoc,
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000232 0, SourceLocation(), 0, 0);
233 break;
Argyrios Kyrtzidisffcc3102011-09-13 16:05:53 +0000234 }
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000235 case tok::kw_char:
236 case tok::kw_wchar_t:
237 case tok::kw_char16_t:
238 case tok::kw_char32_t:
239 case tok::kw_bool:
240 case tok::kw_short:
241 case tok::kw_int:
242 case tok::kw_long:
243 case tok::kw___int64:
244 case tok::kw_signed:
245 case tok::kw_unsigned:
246 case tok::kw_float:
247 case tok::kw_double:
248 case tok::kw_void:
249 case tok::kw_typeof: {
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000250 // If it's a builtin type name, eat it and expect a rparen
251 // __attribute__(( vec_type_hint(char) ))
Argyrios Kyrtzidisffcc3102011-09-13 16:05:53 +0000252 SourceLocation EndLoc = ConsumeToken();
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000253 if (Tok.is(tok::r_paren))
Argyrios Kyrtzidisffcc3102011-09-13 16:05:53 +0000254 EndLoc = ConsumeParen();
255 AttributeList *attr
256 = Attrs.addNew(AttrName, SourceRange(AttrNameLoc, EndLoc), 0,
257 AttrNameLoc, 0, SourceLocation(), 0, 0);
258 if (attr->getKind() == AttributeList::AT_IBOutletCollection)
259 Diag(Tok, diag::err_iboutletcollection_builtintype);
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000260 break;
261 }
262 default:
263 // __attribute__(( aligned(16) ))
264 ExprVector ArgExprs(Actions);
265 bool ArgExprsOk = true;
266
267 // now parse the list of expressions
268 while (1) {
269 ExprResult ArgExpr(ParseAssignmentExpression());
270 if (ArgExpr.isInvalid()) {
271 ArgExprsOk = false;
272 SkipUntil(tok::r_paren);
273 break;
274 } else {
275 ArgExprs.push_back(ArgExpr.release());
276 }
277 if (Tok.isNot(tok::comma))
278 break;
279 ConsumeToken(); // Eat the comma, move to the next argument
280 }
281 // Match the ')'.
282 if (ArgExprsOk && Tok.is(tok::r_paren)) {
Argyrios Kyrtzidisffcc3102011-09-13 16:05:53 +0000283 SourceLocation RParen = ConsumeParen();
284 Attrs.addNew(AttrName, SourceRange(AttrNameLoc, RParen), 0,
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000285 AttrNameLoc, 0, SourceLocation(),
286 ArgExprs.take(), ArgExprs.size());
287 }
288 break;
289 }
290 }
291}
292
293
Eli Friedmana23b4852009-06-08 07:21:15 +0000294/// ParseMicrosoftDeclSpec - Parse an __declspec construct
295///
296/// [MS] decl-specifier:
297/// __declspec ( extended-decl-modifier-seq )
298///
299/// [MS] extended-decl-modifier-seq:
300/// extended-decl-modifier[opt]
301/// extended-decl-modifier extended-decl-modifier-seq
302
John McCall7f040a92010-12-24 02:08:15 +0000303void Parser::ParseMicrosoftDeclSpec(ParsedAttributes &attrs) {
Steve Narofff59e17e2008-12-24 20:59:21 +0000304 assert(Tok.is(tok::kw___declspec) && "Not a declspec!");
Eli Friedmana23b4852009-06-08 07:21:15 +0000305
Steve Narofff59e17e2008-12-24 20:59:21 +0000306 ConsumeToken();
Eli Friedmana23b4852009-06-08 07:21:15 +0000307 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
308 "declspec")) {
309 SkipUntil(tok::r_paren, true); // skip until ) or ;
John McCall7f040a92010-12-24 02:08:15 +0000310 return;
Eli Friedmana23b4852009-06-08 07:21:15 +0000311 }
Francois Pichet373197b2011-05-07 19:04:49 +0000312
Eli Friedman290eeb02009-06-08 23:27:34 +0000313 while (Tok.getIdentifierInfo()) {
Eli Friedmana23b4852009-06-08 07:21:15 +0000314 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
315 SourceLocation AttrNameLoc = ConsumeToken();
Francois Pichet373197b2011-05-07 19:04:49 +0000316
317 // FIXME: Remove this when we have proper __declspec(property()) support.
318 // Just skip everything inside property().
319 if (AttrName->getName() == "property") {
320 ConsumeParen();
321 SkipUntil(tok::r_paren);
322 }
Eli Friedmana23b4852009-06-08 07:21:15 +0000323 if (Tok.is(tok::l_paren)) {
324 ConsumeParen();
325 // FIXME: This doesn't parse __declspec(property(get=get_func_name))
326 // correctly.
John McCall60d7b3a2010-08-24 06:29:42 +0000327 ExprResult ArgExpr(ParseAssignmentExpression());
Eli Friedmana23b4852009-06-08 07:21:15 +0000328 if (!ArgExpr.isInvalid()) {
John McCallca0408f2010-08-23 06:44:23 +0000329 Expr *ExprList = ArgExpr.take();
John McCall0b7e6782011-03-24 11:26:52 +0000330 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
331 SourceLocation(), &ExprList, 1, true);
Eli Friedmana23b4852009-06-08 07:21:15 +0000332 }
333 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
334 SkipUntil(tok::r_paren, false);
335 } else {
John McCall0b7e6782011-03-24 11:26:52 +0000336 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc,
337 0, SourceLocation(), 0, 0, true);
Eli Friedmana23b4852009-06-08 07:21:15 +0000338 }
339 }
340 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
341 SkipUntil(tok::r_paren, false);
John McCall7f040a92010-12-24 02:08:15 +0000342 return;
Eli Friedman290eeb02009-06-08 23:27:34 +0000343}
344
John McCall7f040a92010-12-24 02:08:15 +0000345void Parser::ParseMicrosoftTypeAttributes(ParsedAttributes &attrs) {
Eli Friedman290eeb02009-06-08 23:27:34 +0000346 // Treat these like attributes
347 // FIXME: Allow Sema to distinguish between these and real attributes!
348 while (Tok.is(tok::kw___fastcall) || Tok.is(tok::kw___stdcall) ||
Douglas Gregorf813a2c2010-05-18 16:57:00 +0000349 Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___cdecl) ||
Francois Pichet3bd9aa42011-08-18 09:59:55 +0000350 Tok.is(tok::kw___ptr64) || Tok.is(tok::kw___w64) ||
Francois Pichet58fd97a2011-08-25 00:36:46 +0000351 Tok.is(tok::kw___ptr32) ||
Francois Pichet3bd9aa42011-08-18 09:59:55 +0000352 Tok.is(tok::kw___unaligned)) {
Eli Friedman290eeb02009-06-08 23:27:34 +0000353 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
354 SourceLocation AttrNameLoc = ConsumeToken();
Francois Pichet58fd97a2011-08-25 00:36:46 +0000355 if (Tok.is(tok::kw___ptr64) || Tok.is(tok::kw___w64) ||
356 Tok.is(tok::kw___ptr32))
Eli Friedman290eeb02009-06-08 23:27:34 +0000357 // FIXME: Support these properly!
358 continue;
John McCall0b7e6782011-03-24 11:26:52 +0000359 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
360 SourceLocation(), 0, 0, true);
Eli Friedman290eeb02009-06-08 23:27:34 +0000361 }
Steve Narofff59e17e2008-12-24 20:59:21 +0000362}
363
John McCall7f040a92010-12-24 02:08:15 +0000364void Parser::ParseBorlandTypeAttributes(ParsedAttributes &attrs) {
Dawn Perchik52fc3142010-09-03 01:29:35 +0000365 // Treat these like attributes
366 while (Tok.is(tok::kw___pascal)) {
367 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
368 SourceLocation AttrNameLoc = ConsumeToken();
John McCall0b7e6782011-03-24 11:26:52 +0000369 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
370 SourceLocation(), 0, 0, true);
Dawn Perchik52fc3142010-09-03 01:29:35 +0000371 }
John McCall7f040a92010-12-24 02:08:15 +0000372}
373
Peter Collingbournef315fa82011-02-14 01:42:53 +0000374void Parser::ParseOpenCLAttributes(ParsedAttributes &attrs) {
375 // Treat these like attributes
376 while (Tok.is(tok::kw___kernel)) {
377 SourceLocation AttrNameLoc = ConsumeToken();
John McCall0b7e6782011-03-24 11:26:52 +0000378 attrs.addNew(PP.getIdentifierInfo("opencl_kernel_function"),
379 AttrNameLoc, 0, AttrNameLoc, 0,
380 SourceLocation(), 0, 0, false);
Peter Collingbournef315fa82011-02-14 01:42:53 +0000381 }
382}
383
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000384void Parser::ParseOpenCLQualifiers(DeclSpec &DS) {
385 SourceLocation Loc = Tok.getLocation();
386 switch(Tok.getKind()) {
387 // OpenCL qualifiers:
388 case tok::kw___private:
389 case tok::kw_private:
John McCall0b7e6782011-03-24 11:26:52 +0000390 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000391 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000392 PP.getIdentifierInfo("address_space"), Loc, 0);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000393 break;
394
395 case tok::kw___global:
John McCall0b7e6782011-03-24 11:26:52 +0000396 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000397 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000398 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_global);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000399 break;
400
401 case tok::kw___local:
John McCall0b7e6782011-03-24 11:26:52 +0000402 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000403 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000404 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_local);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000405 break;
406
407 case tok::kw___constant:
John McCall0b7e6782011-03-24 11:26:52 +0000408 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000409 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000410 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_constant);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000411 break;
412
413 case tok::kw___read_only:
John McCall0b7e6782011-03-24 11:26:52 +0000414 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000415 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000416 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_read_only);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000417 break;
418
419 case tok::kw___write_only:
John McCall0b7e6782011-03-24 11:26:52 +0000420 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000421 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000422 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_write_only);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000423 break;
424
425 case tok::kw___read_write:
John McCall0b7e6782011-03-24 11:26:52 +0000426 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000427 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000428 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_read_write);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000429 break;
430 default: break;
431 }
432}
433
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000434/// \brief Parse a version number.
435///
436/// version:
437/// simple-integer
438/// simple-integer ',' simple-integer
439/// simple-integer ',' simple-integer ',' simple-integer
440VersionTuple Parser::ParseVersionTuple(SourceRange &Range) {
441 Range = Tok.getLocation();
442
443 if (!Tok.is(tok::numeric_constant)) {
444 Diag(Tok, diag::err_expected_version);
445 SkipUntil(tok::comma, tok::r_paren, true, true, true);
446 return VersionTuple();
447 }
448
449 // Parse the major (and possibly minor and subminor) versions, which
450 // are stored in the numeric constant. We utilize a quirk of the
451 // lexer, which is that it handles something like 1.2.3 as a single
452 // numeric constant, rather than two separate tokens.
453 llvm::SmallString<512> Buffer;
454 Buffer.resize(Tok.getLength()+1);
455 const char *ThisTokBegin = &Buffer[0];
456
457 // Get the spelling of the token, which eliminates trigraphs, etc.
458 bool Invalid = false;
459 unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin, &Invalid);
460 if (Invalid)
461 return VersionTuple();
462
463 // Parse the major version.
464 unsigned AfterMajor = 0;
465 unsigned Major = 0;
466 while (AfterMajor < ActualLength && isdigit(ThisTokBegin[AfterMajor])) {
467 Major = Major * 10 + ThisTokBegin[AfterMajor] - '0';
468 ++AfterMajor;
469 }
470
471 if (AfterMajor == 0) {
472 Diag(Tok, diag::err_expected_version);
473 SkipUntil(tok::comma, tok::r_paren, true, true, true);
474 return VersionTuple();
475 }
476
477 if (AfterMajor == ActualLength) {
478 ConsumeToken();
479
480 // We only had a single version component.
481 if (Major == 0) {
482 Diag(Tok, diag::err_zero_version);
483 return VersionTuple();
484 }
485
486 return VersionTuple(Major);
487 }
488
489 if (ThisTokBegin[AfterMajor] != '.' || (AfterMajor + 1 == ActualLength)) {
490 Diag(Tok, diag::err_expected_version);
491 SkipUntil(tok::comma, tok::r_paren, true, true, true);
492 return VersionTuple();
493 }
494
495 // Parse the minor version.
496 unsigned AfterMinor = AfterMajor + 1;
497 unsigned Minor = 0;
498 while (AfterMinor < ActualLength && isdigit(ThisTokBegin[AfterMinor])) {
499 Minor = Minor * 10 + ThisTokBegin[AfterMinor] - '0';
500 ++AfterMinor;
501 }
502
503 if (AfterMinor == ActualLength) {
504 ConsumeToken();
505
506 // We had major.minor.
507 if (Major == 0 && Minor == 0) {
508 Diag(Tok, diag::err_zero_version);
509 return VersionTuple();
510 }
511
512 return VersionTuple(Major, Minor);
513 }
514
515 // If what follows is not a '.', we have a problem.
516 if (ThisTokBegin[AfterMinor] != '.') {
517 Diag(Tok, diag::err_expected_version);
518 SkipUntil(tok::comma, tok::r_paren, true, true, true);
519 return VersionTuple();
520 }
521
522 // Parse the subminor version.
523 unsigned AfterSubminor = AfterMinor + 1;
524 unsigned Subminor = 0;
525 while (AfterSubminor < ActualLength && isdigit(ThisTokBegin[AfterSubminor])) {
526 Subminor = Subminor * 10 + ThisTokBegin[AfterSubminor] - '0';
527 ++AfterSubminor;
528 }
529
530 if (AfterSubminor != ActualLength) {
531 Diag(Tok, diag::err_expected_version);
532 SkipUntil(tok::comma, tok::r_paren, true, true, true);
533 return VersionTuple();
534 }
535 ConsumeToken();
536 return VersionTuple(Major, Minor, Subminor);
537}
538
539/// \brief Parse the contents of the "availability" attribute.
540///
541/// availability-attribute:
542/// 'availability' '(' platform ',' version-arg-list ')'
543///
544/// platform:
545/// identifier
546///
547/// version-arg-list:
548/// version-arg
549/// version-arg ',' version-arg-list
550///
551/// version-arg:
552/// 'introduced' '=' version
553/// 'deprecated' '=' version
554/// 'removed' = version
Douglas Gregorb53e4172011-03-26 03:35:55 +0000555/// 'unavailable'
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000556void Parser::ParseAvailabilityAttribute(IdentifierInfo &Availability,
557 SourceLocation AvailabilityLoc,
558 ParsedAttributes &attrs,
559 SourceLocation *endLoc) {
560 SourceLocation PlatformLoc;
561 IdentifierInfo *Platform = 0;
562
563 enum { Introduced, Deprecated, Obsoleted, Unknown };
564 AvailabilityChange Changes[Unknown];
565
566 // Opening '('.
567 SourceLocation LParenLoc;
568 if (!Tok.is(tok::l_paren)) {
569 Diag(Tok, diag::err_expected_lparen);
570 return;
571 }
572 LParenLoc = ConsumeParen();
573
574 // Parse the platform name,
575 if (Tok.isNot(tok::identifier)) {
576 Diag(Tok, diag::err_availability_expected_platform);
577 SkipUntil(tok::r_paren);
578 return;
579 }
580 Platform = Tok.getIdentifierInfo();
581 PlatformLoc = ConsumeToken();
582
583 // Parse the ',' following the platform name.
584 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "", tok::r_paren))
585 return;
586
587 // If we haven't grabbed the pointers for the identifiers
588 // "introduced", "deprecated", and "obsoleted", do so now.
589 if (!Ident_introduced) {
590 Ident_introduced = PP.getIdentifierInfo("introduced");
591 Ident_deprecated = PP.getIdentifierInfo("deprecated");
592 Ident_obsoleted = PP.getIdentifierInfo("obsoleted");
Douglas Gregorb53e4172011-03-26 03:35:55 +0000593 Ident_unavailable = PP.getIdentifierInfo("unavailable");
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000594 }
595
596 // Parse the set of introductions/deprecations/removals.
Douglas Gregorb53e4172011-03-26 03:35:55 +0000597 SourceLocation UnavailableLoc;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000598 do {
599 if (Tok.isNot(tok::identifier)) {
600 Diag(Tok, diag::err_availability_expected_change);
601 SkipUntil(tok::r_paren);
602 return;
603 }
604 IdentifierInfo *Keyword = Tok.getIdentifierInfo();
605 SourceLocation KeywordLoc = ConsumeToken();
606
Douglas Gregorb53e4172011-03-26 03:35:55 +0000607 if (Keyword == Ident_unavailable) {
608 if (UnavailableLoc.isValid()) {
609 Diag(KeywordLoc, diag::err_availability_redundant)
610 << Keyword << SourceRange(UnavailableLoc);
611 }
612 UnavailableLoc = KeywordLoc;
613
614 if (Tok.isNot(tok::comma))
615 break;
616
617 ConsumeToken();
618 continue;
619 }
620
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000621 if (Tok.isNot(tok::equal)) {
622 Diag(Tok, diag::err_expected_equal_after)
623 << Keyword;
624 SkipUntil(tok::r_paren);
625 return;
626 }
627 ConsumeToken();
628
629 SourceRange VersionRange;
630 VersionTuple Version = ParseVersionTuple(VersionRange);
631
632 if (Version.empty()) {
633 SkipUntil(tok::r_paren);
634 return;
635 }
636
637 unsigned Index;
638 if (Keyword == Ident_introduced)
639 Index = Introduced;
640 else if (Keyword == Ident_deprecated)
641 Index = Deprecated;
642 else if (Keyword == Ident_obsoleted)
643 Index = Obsoleted;
644 else
645 Index = Unknown;
646
647 if (Index < Unknown) {
648 if (!Changes[Index].KeywordLoc.isInvalid()) {
649 Diag(KeywordLoc, diag::err_availability_redundant)
650 << Keyword
651 << SourceRange(Changes[Index].KeywordLoc,
652 Changes[Index].VersionRange.getEnd());
653 }
654
655 Changes[Index].KeywordLoc = KeywordLoc;
656 Changes[Index].Version = Version;
657 Changes[Index].VersionRange = VersionRange;
658 } else {
659 Diag(KeywordLoc, diag::err_availability_unknown_change)
660 << Keyword << VersionRange;
661 }
662
663 if (Tok.isNot(tok::comma))
664 break;
665
666 ConsumeToken();
667 } while (true);
668
669 // Closing ')'.
670 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
671 if (RParenLoc.isInvalid())
672 return;
673
674 if (endLoc)
675 *endLoc = RParenLoc;
676
Douglas Gregorb53e4172011-03-26 03:35:55 +0000677 // The 'unavailable' availability cannot be combined with any other
678 // availability changes. Make sure that hasn't happened.
679 if (UnavailableLoc.isValid()) {
680 bool Complained = false;
681 for (unsigned Index = Introduced; Index != Unknown; ++Index) {
682 if (Changes[Index].KeywordLoc.isValid()) {
683 if (!Complained) {
684 Diag(UnavailableLoc, diag::warn_availability_and_unavailable)
685 << SourceRange(Changes[Index].KeywordLoc,
686 Changes[Index].VersionRange.getEnd());
687 Complained = true;
688 }
689
690 // Clear out the availability.
691 Changes[Index] = AvailabilityChange();
692 }
693 }
694 }
695
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000696 // Record this attribute
Argyrios Kyrtzidisffcc3102011-09-13 16:05:53 +0000697 attrs.addNew(&Availability, SourceRange(AvailabilityLoc, RParenLoc),
John McCall0b7e6782011-03-24 11:26:52 +0000698 0, SourceLocation(),
699 Platform, PlatformLoc,
700 Changes[Introduced],
701 Changes[Deprecated],
Douglas Gregorb53e4172011-03-26 03:35:55 +0000702 Changes[Obsoleted],
703 UnavailableLoc, false, false);
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000704}
705
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000706
707// Late Parsed Attributes:
708// See other examples of late parsing in lib/Parse/ParseCXXInlineMethods
709
710void Parser::LateParsedDeclaration::ParseLexedAttributes() {}
711
712void Parser::LateParsedClass::ParseLexedAttributes() {
713 Self->ParseLexedAttributes(*Class);
714}
715
716void Parser::LateParsedAttribute::ParseLexedAttributes() {
717 Self->ParseLexedAttribute(*this);
718}
719
720/// Wrapper class which calls ParseLexedAttribute, after setting up the
721/// scope appropriately.
722void Parser::ParseLexedAttributes(ParsingClass &Class) {
723 // Deal with templates
724 // FIXME: Test cases to make sure this does the right thing for templates.
725 bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope;
726 ParseScope ClassTemplateScope(this, Scope::TemplateParamScope,
727 HasTemplateScope);
728 if (HasTemplateScope)
729 Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate);
730
731 // Set or update the scope flags to include Scope::ThisScope.
732 bool AlreadyHasClassScope = Class.TopLevelClass;
733 unsigned ScopeFlags = Scope::ClassScope|Scope::DeclScope|Scope::ThisScope;
734 ParseScope ClassScope(this, ScopeFlags, !AlreadyHasClassScope);
735 ParseScopeFlags ClassScopeFlags(this, ScopeFlags, AlreadyHasClassScope);
736
737 for (unsigned i = 0, ni = Class.LateParsedDeclarations.size(); i < ni; ++i) {
738 Class.LateParsedDeclarations[i]->ParseLexedAttributes();
739 }
740}
741
742/// \brief Finish parsing an attribute for which parsing was delayed.
743/// This will be called at the end of parsing a class declaration
744/// for each LateParsedAttribute. We consume the saved tokens and
745/// create an attribute with the arguments filled in. We add this
746/// to the Attribute list for the decl.
747void Parser::ParseLexedAttribute(LateParsedAttribute &LA) {
748 // Save the current token position.
749 SourceLocation OrigLoc = Tok.getLocation();
750
751 // Append the current token at the end of the new token stream so that it
752 // doesn't get lost.
753 LA.Toks.push_back(Tok);
754 PP.EnterTokenStream(LA.Toks.data(), LA.Toks.size(), true, false);
755 // Consume the previously pushed token.
756 ConsumeAnyToken();
757
758 ParsedAttributes Attrs(AttrFactory);
759 SourceLocation endLoc;
760
Caitlin Sadowskied9d84a2011-09-08 17:42:31 +0000761 // If the Decl is templatized, add template parameters to scope.
762 bool HasTemplateScope = LA.D && LA.D->isTemplateDecl();
763 ParseScope TempScope(this, Scope::TemplateParamScope, HasTemplateScope);
764 if (HasTemplateScope)
765 Actions.ActOnReenterTemplateScope(Actions.CurScope, LA.D);
766
767 // If the Decl is on a function, add function parameters to the scope.
768 bool HasFunctionScope = LA.D && LA.D->isFunctionOrFunctionTemplate();
769 ParseScope FnScope(this, Scope::FnScope|Scope::DeclScope, HasFunctionScope);
770 if (HasFunctionScope)
771 Actions.ActOnReenterFunctionContext(Actions.CurScope, LA.D);
772
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000773 ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, &endLoc);
774
Caitlin Sadowskied9d84a2011-09-08 17:42:31 +0000775 if (HasFunctionScope) {
776 Actions.ActOnExitFunctionContext();
777 FnScope.Exit(); // Pop scope, and remove Decls from IdResolver
778 }
779 if (HasTemplateScope) {
780 TempScope.Exit();
781 }
782
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000783 // Late parsed attributes must be attached to Decls by hand. If the
784 // LA.D is not set, then this was not done properly.
785 assert(LA.D && "No decl attached to late parsed attribute");
786 Actions.ActOnFinishDelayedAttribute(getCurScope(), LA.D, Attrs);
787
788 if (Tok.getLocation() != OrigLoc) {
789 // Due to a parsing error, we either went over the cached tokens or
790 // there are still cached tokens left, so we skip the leftover tokens.
791 // Since this is an uncommon situation that should be avoided, use the
792 // expensive isBeforeInTranslationUnit call.
793 if (PP.getSourceManager().isBeforeInTranslationUnit(Tok.getLocation(),
794 OrigLoc))
795 while (Tok.getLocation() != OrigLoc && Tok.isNot(tok::eof))
796 ConsumeAnyToken();
797 }
798}
799
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000800/// \brief Wrapper around a case statement checking if AttrName is
801/// one of the thread safety attributes
802bool Parser::IsThreadSafetyAttribute(llvm::StringRef AttrName){
803 return llvm::StringSwitch<bool>(AttrName)
804 .Case("guarded_by", true)
805 .Case("guarded_var", true)
806 .Case("pt_guarded_by", true)
807 .Case("pt_guarded_var", true)
808 .Case("lockable", true)
809 .Case("scoped_lockable", true)
810 .Case("no_thread_safety_analysis", true)
811 .Case("acquired_after", true)
812 .Case("acquired_before", true)
813 .Case("exclusive_lock_function", true)
814 .Case("shared_lock_function", true)
815 .Case("exclusive_trylock_function", true)
816 .Case("shared_trylock_function", true)
817 .Case("unlock_function", true)
818 .Case("lock_returned", true)
819 .Case("locks_excluded", true)
820 .Case("exclusive_locks_required", true)
821 .Case("shared_locks_required", true)
822 .Default(false);
823}
824
825/// \brief Parse the contents of thread safety attributes. These
826/// should always be parsed as an expression list.
827///
828/// We need to special case the parsing due to the fact that if the first token
829/// of the first argument is an identifier, the main parse loop will store
830/// that token as a "parameter" and the rest of
831/// the arguments will be added to a list of "arguments". However,
832/// subsequent tokens in the first argument are lost. We instead parse each
833/// argument as an expression and add all arguments to the list of "arguments".
834/// In future, we will take advantage of this special case to also
835/// deal with some argument scoping issues here (for example, referring to a
836/// function parameter in the attribute on that function).
837void Parser::ParseThreadSafetyAttribute(IdentifierInfo &AttrName,
838 SourceLocation AttrNameLoc,
839 ParsedAttributes &Attrs,
840 SourceLocation *EndLoc) {
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000841 assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('");
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000842
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000843 SourceLocation LeftParenLoc = Tok.getLocation();
844 ConsumeParen();
845
846 ExprVector ArgExprs(Actions);
847 bool ArgExprsOk = true;
848
849 // now parse the list of expressions
850 while (1) {
851 ExprResult ArgExpr(ParseAssignmentExpression());
852 if (ArgExpr.isInvalid()) {
853 ArgExprsOk = false;
854 MatchRHSPunctuation(tok::r_paren, LeftParenLoc);
855 break;
856 } else {
857 ArgExprs.push_back(ArgExpr.release());
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000858 }
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000859 if (Tok.isNot(tok::comma))
860 break;
861 ConsumeToken(); // Eat the comma, move to the next argument
862 }
863 // Match the ')'.
864 if (ArgExprsOk && Tok.is(tok::r_paren)) {
865 if (EndLoc)
866 *EndLoc = Tok.getLocation();
867 ConsumeParen();
868 Attrs.addNew(&AttrName, AttrNameLoc, 0, AttrNameLoc, 0, SourceLocation(),
869 ArgExprs.take(), ArgExprs.size());
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000870 }
871}
872
John McCall7f040a92010-12-24 02:08:15 +0000873void Parser::DiagnoseProhibitedAttributes(ParsedAttributesWithRange &attrs) {
874 Diag(attrs.Range.getBegin(), diag::err_attributes_not_allowed)
875 << attrs.Range;
Dawn Perchik52fc3142010-09-03 01:29:35 +0000876}
877
Reid Spencer5f016e22007-07-11 17:01:13 +0000878/// ParseDeclaration - Parse a full 'declaration', which consists of
879/// declaration-specifiers, some number of declarators, and a semicolon.
Chris Lattner97144fc2009-04-02 04:16:50 +0000880/// 'Context' should be a Declarator::TheContext value. This returns the
881/// location of the semicolon in DeclEnd.
Chris Lattner8f08cb72007-08-25 06:57:03 +0000882///
883/// declaration: [C99 6.7]
884/// block-declaration ->
885/// simple-declaration
886/// others [FIXME]
Douglas Gregoradcac882008-12-01 23:54:00 +0000887/// [C++] template-declaration
Chris Lattner8f08cb72007-08-25 06:57:03 +0000888/// [C++] namespace-definition
Douglas Gregorf780abc2008-12-30 03:27:21 +0000889/// [C++] using-directive
Douglas Gregord7f37bf2009-06-22 23:06:13 +0000890/// [C++] using-declaration
Peter Collingbournec6eb44b2011-04-15 00:35:57 +0000891/// [C++0x/C1X] static_assert-declaration
Chris Lattner8f08cb72007-08-25 06:57:03 +0000892/// others... [FIXME]
893///
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +0000894Parser::DeclGroupPtrTy Parser::ParseDeclaration(StmtVector &Stmts,
895 unsigned Context,
Sean Huntbbd37c62009-11-21 08:43:09 +0000896 SourceLocation &DeclEnd,
John McCall7f040a92010-12-24 02:08:15 +0000897 ParsedAttributesWithRange &attrs) {
Argyrios Kyrtzidis36d36802010-06-17 10:52:18 +0000898 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Fariborz Jahaniane8cff362011-08-30 17:10:52 +0000899 // Must temporarily exit the objective-c container scope for
900 // parsing c none objective-c decls.
901 ObjCDeclContextSwitch ObjCDC(*this);
Argyrios Kyrtzidis36d36802010-06-17 10:52:18 +0000902
John McCalld226f652010-08-21 09:40:31 +0000903 Decl *SingleDecl = 0;
Richard Smithc89edf52011-07-01 19:46:12 +0000904 Decl *OwnedType = 0;
Chris Lattner8f08cb72007-08-25 06:57:03 +0000905 switch (Tok.getKind()) {
Douglas Gregoradcac882008-12-01 23:54:00 +0000906 case tok::kw_template:
Douglas Gregor1426e532009-05-12 21:31:51 +0000907 case tok::kw_export:
John McCall7f040a92010-12-24 02:08:15 +0000908 ProhibitAttributes(attrs);
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000909 SingleDecl = ParseDeclarationStartingWithTemplate(Context, DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +0000910 break;
Sebastian Redld078e642010-08-27 23:12:46 +0000911 case tok::kw_inline:
Sebastian Redl88e64ca2010-08-31 00:36:45 +0000912 // Could be the start of an inline namespace. Allowed as an ext in C++03.
913 if (getLang().CPlusPlus && NextToken().is(tok::kw_namespace)) {
John McCall7f040a92010-12-24 02:08:15 +0000914 ProhibitAttributes(attrs);
Sebastian Redld078e642010-08-27 23:12:46 +0000915 SourceLocation InlineLoc = ConsumeToken();
916 SingleDecl = ParseNamespace(Context, DeclEnd, InlineLoc);
917 break;
918 }
John McCall7f040a92010-12-24 02:08:15 +0000919 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs,
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +0000920 true);
Chris Lattner8f08cb72007-08-25 06:57:03 +0000921 case tok::kw_namespace:
John McCall7f040a92010-12-24 02:08:15 +0000922 ProhibitAttributes(attrs);
Chris Lattner97144fc2009-04-02 04:16:50 +0000923 SingleDecl = ParseNamespace(Context, DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +0000924 break;
Douglas Gregorf780abc2008-12-30 03:27:21 +0000925 case tok::kw_using:
John McCall78b81052010-11-10 02:40:36 +0000926 SingleDecl = ParseUsingDirectiveOrDeclaration(Context, ParsedTemplateInfo(),
Richard Smithc89edf52011-07-01 19:46:12 +0000927 DeclEnd, attrs, &OwnedType);
Chris Lattner682bf922009-03-29 16:50:03 +0000928 break;
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000929 case tok::kw_static_assert:
Peter Collingbournec6eb44b2011-04-15 00:35:57 +0000930 case tok::kw__Static_assert:
John McCall7f040a92010-12-24 02:08:15 +0000931 ProhibitAttributes(attrs);
Chris Lattner97144fc2009-04-02 04:16:50 +0000932 SingleDecl = ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +0000933 break;
Chris Lattner8f08cb72007-08-25 06:57:03 +0000934 default:
John McCall7f040a92010-12-24 02:08:15 +0000935 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs, true);
Chris Lattner8f08cb72007-08-25 06:57:03 +0000936 }
Sean Huntbbd37c62009-11-21 08:43:09 +0000937
Chris Lattner682bf922009-03-29 16:50:03 +0000938 // This routine returns a DeclGroup, if the thing we parsed only contains a
Richard Smithc89edf52011-07-01 19:46:12 +0000939 // single decl, convert it now. Alias declarations can also declare a type;
940 // include that too if it is present.
941 return Actions.ConvertDeclToDeclGroup(SingleDecl, OwnedType);
Chris Lattner8f08cb72007-08-25 06:57:03 +0000942}
943
944/// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl]
945/// declaration-specifiers init-declarator-list[opt] ';'
946///[C90/C++]init-declarator-list ';' [TODO]
947/// [OMP] threadprivate-directive [TODO]
Chris Lattnercd147752009-03-29 17:27:48 +0000948///
Richard Smithad762fc2011-04-14 22:09:26 +0000949/// for-range-declaration: [C++0x 6.5p1: stmt.ranged]
950/// attribute-specifier-seq[opt] type-specifier-seq declarator
951///
Chris Lattnercd147752009-03-29 17:27:48 +0000952/// If RequireSemi is false, this does not check for a ';' at the end of the
Chris Lattner5c5db552010-04-05 18:18:31 +0000953/// declaration. If it is true, it checks for and eats it.
Richard Smithad762fc2011-04-14 22:09:26 +0000954///
955/// If FRI is non-null, we might be parsing a for-range-declaration instead
956/// of a simple-declaration. If we find that we are, we also parse the
957/// for-range-initializer, and place it here.
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +0000958Parser::DeclGroupPtrTy Parser::ParseSimpleDeclaration(StmtVector &Stmts,
959 unsigned Context,
Sean Huntbbd37c62009-11-21 08:43:09 +0000960 SourceLocation &DeclEnd,
John McCall7f040a92010-12-24 02:08:15 +0000961 ParsedAttributes &attrs,
Richard Smithad762fc2011-04-14 22:09:26 +0000962 bool RequireSemi,
963 ForRangeInit *FRI) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000964 // Parse the common declaration-specifiers piece.
John McCall54abf7d2009-11-04 02:18:39 +0000965 ParsingDeclSpec DS(*this);
John McCall7f040a92010-12-24 02:08:15 +0000966 DS.takeAttributesFrom(attrs);
Douglas Gregor312eadb2011-04-24 05:37:28 +0000967
Douglas Gregor0efc2c12010-01-13 17:31:36 +0000968 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS_none,
Richard Smith34b41d92011-02-20 03:19:35 +0000969 getDeclSpecContextFromDeclaratorContext(Context));
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +0000970 StmtResult R = Actions.ActOnVlaStmt(DS);
971 if (R.isUsable())
972 Stmts.push_back(R.release());
Douglas Gregor312eadb2011-04-24 05:37:28 +0000973
Reid Spencer5f016e22007-07-11 17:01:13 +0000974 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
975 // declaration-specifiers init-declarator-list[opt] ';'
Chris Lattner04d66662007-10-09 17:33:22 +0000976 if (Tok.is(tok::semi)) {
Chris Lattner5c5db552010-04-05 18:18:31 +0000977 if (RequireSemi) ConsumeToken();
John McCalld226f652010-08-21 09:40:31 +0000978 Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none,
Douglas Gregor312eadb2011-04-24 05:37:28 +0000979 DS);
John McCall54abf7d2009-11-04 02:18:39 +0000980 DS.complete(TheDecl);
Chris Lattner682bf922009-03-29 16:50:03 +0000981 return Actions.ConvertDeclToDeclGroup(TheDecl);
Reid Spencer5f016e22007-07-11 17:01:13 +0000982 }
Douglas Gregor312eadb2011-04-24 05:37:28 +0000983
984 return ParseDeclGroup(DS, Context, /*FunctionDefs=*/ false, &DeclEnd, FRI);
John McCalld8ac0572009-11-03 19:26:08 +0000985}
Mike Stump1eb44332009-09-09 15:08:12 +0000986
John McCalld8ac0572009-11-03 19:26:08 +0000987/// ParseDeclGroup - Having concluded that this is either a function
988/// definition or a group of object declarations, actually parse the
989/// result.
John McCall54abf7d2009-11-04 02:18:39 +0000990Parser::DeclGroupPtrTy Parser::ParseDeclGroup(ParsingDeclSpec &DS,
991 unsigned Context,
John McCalld8ac0572009-11-03 19:26:08 +0000992 bool AllowFunctionDefinitions,
Richard Smithad762fc2011-04-14 22:09:26 +0000993 SourceLocation *DeclEnd,
994 ForRangeInit *FRI) {
John McCalld8ac0572009-11-03 19:26:08 +0000995 // Parse the first declarator.
John McCall54abf7d2009-11-04 02:18:39 +0000996 ParsingDeclarator D(*this, DS, static_cast<Declarator::TheContext>(Context));
John McCalld8ac0572009-11-03 19:26:08 +0000997 ParseDeclarator(D);
Chris Lattnercd147752009-03-29 17:27:48 +0000998
John McCalld8ac0572009-11-03 19:26:08 +0000999 // Bail out if the first declarator didn't seem well-formed.
1000 if (!D.hasName() && !D.mayOmitIdentifier()) {
1001 // Skip until ; or }.
1002 SkipUntil(tok::r_brace, true, true);
1003 if (Tok.is(tok::semi))
1004 ConsumeToken();
1005 return DeclGroupPtrTy();
Chris Lattner23c4b182009-03-29 17:18:04 +00001006 }
Mike Stump1eb44332009-09-09 15:08:12 +00001007
Chris Lattnerc82daef2010-07-11 22:24:20 +00001008 // Check to see if we have a function *definition* which must have a body.
1009 if (AllowFunctionDefinitions && D.isFunctionDeclarator() &&
1010 // Look at the next token to make sure that this isn't a function
1011 // declaration. We have to check this because __attribute__ might be the
1012 // start of a function definition in GCC-extended K&R C.
1013 !isDeclarationAfterDeclarator()) {
1014
Chris Lattner004659a2010-07-11 22:42:07 +00001015 if (isStartOfFunctionDefinition(D)) {
John McCalld8ac0572009-11-03 19:26:08 +00001016 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
1017 Diag(Tok, diag::err_function_declared_typedef);
1018
1019 // Recover by treating the 'typedef' as spurious.
1020 DS.ClearStorageClassSpecs();
1021 }
1022
John McCalld226f652010-08-21 09:40:31 +00001023 Decl *TheDecl = ParseFunctionDefinition(D);
John McCalld8ac0572009-11-03 19:26:08 +00001024 return Actions.ConvertDeclToDeclGroup(TheDecl);
Chris Lattner004659a2010-07-11 22:42:07 +00001025 }
1026
1027 if (isDeclarationSpecifier()) {
1028 // If there is an invalid declaration specifier right after the function
1029 // prototype, then we must be in a missing semicolon case where this isn't
1030 // actually a body. Just fall through into the code that handles it as a
1031 // prototype, and let the top-level code handle the erroneous declspec
1032 // where it would otherwise expect a comma or semicolon.
John McCalld8ac0572009-11-03 19:26:08 +00001033 } else {
1034 Diag(Tok, diag::err_expected_fn_body);
1035 SkipUntil(tok::semi);
1036 return DeclGroupPtrTy();
1037 }
1038 }
1039
Richard Smithad762fc2011-04-14 22:09:26 +00001040 if (ParseAttributesAfterDeclarator(D))
1041 return DeclGroupPtrTy();
1042
1043 // C++0x [stmt.iter]p1: Check if we have a for-range-declarator. If so, we
1044 // must parse and analyze the for-range-initializer before the declaration is
1045 // analyzed.
1046 if (FRI && Tok.is(tok::colon)) {
1047 FRI->ColonLoc = ConsumeToken();
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001048 if (Tok.is(tok::l_brace))
1049 FRI->RangeExpr = ParseBraceInitializer();
1050 else
1051 FRI->RangeExpr = ParseExpression();
Richard Smithad762fc2011-04-14 22:09:26 +00001052 Decl *ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
1053 Actions.ActOnCXXForRangeDecl(ThisDecl);
1054 Actions.FinalizeDeclaration(ThisDecl);
1055 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS, &ThisDecl, 1);
1056 }
1057
Chris Lattner5f9e2722011-07-23 10:55:15 +00001058 SmallVector<Decl *, 8> DeclsInGroup;
Richard Smithad762fc2011-04-14 22:09:26 +00001059 Decl *FirstDecl = ParseDeclarationAfterDeclaratorAndAttributes(D);
John McCall54abf7d2009-11-04 02:18:39 +00001060 D.complete(FirstDecl);
John McCalld226f652010-08-21 09:40:31 +00001061 if (FirstDecl)
John McCalld8ac0572009-11-03 19:26:08 +00001062 DeclsInGroup.push_back(FirstDecl);
1063
1064 // If we don't have a comma, it is either the end of the list (a ';') or an
1065 // error, bail out.
1066 while (Tok.is(tok::comma)) {
1067 // Consume the comma.
Chris Lattner23c4b182009-03-29 17:18:04 +00001068 ConsumeToken();
John McCalld8ac0572009-11-03 19:26:08 +00001069
1070 // Parse the next declarator.
1071 D.clear();
1072
1073 // Accept attributes in an init-declarator. In the first declarator in a
1074 // declaration, these would be part of the declspec. In subsequent
1075 // declarators, they become part of the declarator itself, so that they
1076 // don't apply to declarators after *this* one. Examples:
1077 // short __attribute__((common)) var; -> declspec
1078 // short var __attribute__((common)); -> declarator
1079 // short x, __attribute__((common)) var; -> declarator
John McCall7f040a92010-12-24 02:08:15 +00001080 MaybeParseGNUAttributes(D);
John McCalld8ac0572009-11-03 19:26:08 +00001081
1082 ParseDeclarator(D);
1083
John McCalld226f652010-08-21 09:40:31 +00001084 Decl *ThisDecl = ParseDeclarationAfterDeclarator(D);
John McCall54abf7d2009-11-04 02:18:39 +00001085 D.complete(ThisDecl);
John McCalld226f652010-08-21 09:40:31 +00001086 if (ThisDecl)
John McCalld8ac0572009-11-03 19:26:08 +00001087 DeclsInGroup.push_back(ThisDecl);
1088 }
1089
1090 if (DeclEnd)
1091 *DeclEnd = Tok.getLocation();
1092
1093 if (Context != Declarator::ForContext &&
1094 ExpectAndConsume(tok::semi,
1095 Context == Declarator::FileContext
1096 ? diag::err_invalid_token_after_toplevel_declarator
1097 : diag::err_expected_semi_declaration)) {
Chris Lattner004659a2010-07-11 22:42:07 +00001098 // Okay, there was no semicolon and one was expected. If we see a
1099 // declaration specifier, just assume it was missing and continue parsing.
1100 // Otherwise things are very confused and we skip to recover.
1101 if (!isDeclarationSpecifier()) {
1102 SkipUntil(tok::r_brace, true, true);
1103 if (Tok.is(tok::semi))
1104 ConsumeToken();
1105 }
John McCalld8ac0572009-11-03 19:26:08 +00001106 }
1107
Douglas Gregor23c94db2010-07-02 17:43:08 +00001108 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS,
John McCalld8ac0572009-11-03 19:26:08 +00001109 DeclsInGroup.data(),
1110 DeclsInGroup.size());
Reid Spencer5f016e22007-07-11 17:01:13 +00001111}
1112
Richard Smithad762fc2011-04-14 22:09:26 +00001113/// Parse an optional simple-asm-expr and attributes, and attach them to a
1114/// declarator. Returns true on an error.
1115bool Parser::ParseAttributesAfterDeclarator(Declarator &D) {
1116 // If a simple-asm-expr is present, parse it.
1117 if (Tok.is(tok::kw_asm)) {
1118 SourceLocation Loc;
1119 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
1120 if (AsmLabel.isInvalid()) {
1121 SkipUntil(tok::semi, true, true);
1122 return true;
1123 }
1124
1125 D.setAsmLabel(AsmLabel.release());
1126 D.SetRangeEnd(Loc);
1127 }
1128
1129 MaybeParseGNUAttributes(D);
1130 return false;
1131}
1132
Douglas Gregor1426e532009-05-12 21:31:51 +00001133/// \brief Parse 'declaration' after parsing 'declaration-specifiers
1134/// declarator'. This method parses the remainder of the declaration
1135/// (including any attributes or initializer, among other things) and
1136/// finalizes the declaration.
Reid Spencer5f016e22007-07-11 17:01:13 +00001137///
Reid Spencer5f016e22007-07-11 17:01:13 +00001138/// init-declarator: [C99 6.7]
1139/// declarator
1140/// declarator '=' initializer
1141/// [GNU] declarator simple-asm-expr[opt] attributes[opt]
1142/// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00001143/// [C++] declarator initializer[opt]
1144///
1145/// [C++] initializer:
1146/// [C++] '=' initializer-clause
1147/// [C++] '(' expression-list ')'
Sebastian Redl50de12f2009-03-24 22:27:57 +00001148/// [C++0x] '=' 'default' [TODO]
1149/// [C++0x] '=' 'delete'
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001150/// [C++0x] braced-init-list
Sebastian Redl50de12f2009-03-24 22:27:57 +00001151///
1152/// According to the standard grammar, =default and =delete are function
1153/// definitions, but that definitely doesn't fit with the parser here.
Reid Spencer5f016e22007-07-11 17:01:13 +00001154///
John McCalld226f652010-08-21 09:40:31 +00001155Decl *Parser::ParseDeclarationAfterDeclarator(Declarator &D,
Douglas Gregore542c862009-06-23 23:11:28 +00001156 const ParsedTemplateInfo &TemplateInfo) {
Richard Smithad762fc2011-04-14 22:09:26 +00001157 if (ParseAttributesAfterDeclarator(D))
1158 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001159
Richard Smithad762fc2011-04-14 22:09:26 +00001160 return ParseDeclarationAfterDeclaratorAndAttributes(D, TemplateInfo);
1161}
Mike Stump1eb44332009-09-09 15:08:12 +00001162
Richard Smithad762fc2011-04-14 22:09:26 +00001163Decl *Parser::ParseDeclarationAfterDeclaratorAndAttributes(Declarator &D,
1164 const ParsedTemplateInfo &TemplateInfo) {
Douglas Gregor1426e532009-05-12 21:31:51 +00001165 // Inform the current actions module that we just parsed this declarator.
John McCalld226f652010-08-21 09:40:31 +00001166 Decl *ThisDecl = 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +00001167 switch (TemplateInfo.Kind) {
1168 case ParsedTemplateInfo::NonTemplate:
Douglas Gregor23c94db2010-07-02 17:43:08 +00001169 ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
Douglas Gregord5a423b2009-09-25 18:43:00 +00001170 break;
1171
1172 case ParsedTemplateInfo::Template:
1173 case ParsedTemplateInfo::ExplicitSpecialization:
Douglas Gregor23c94db2010-07-02 17:43:08 +00001174 ThisDecl = Actions.ActOnTemplateDeclarator(getCurScope(),
John McCallf312b1e2010-08-26 23:41:50 +00001175 MultiTemplateParamsArg(Actions,
Douglas Gregore542c862009-06-23 23:11:28 +00001176 TemplateInfo.TemplateParams->data(),
1177 TemplateInfo.TemplateParams->size()),
Douglas Gregord5a423b2009-09-25 18:43:00 +00001178 D);
1179 break;
1180
1181 case ParsedTemplateInfo::ExplicitInstantiation: {
John McCalld226f652010-08-21 09:40:31 +00001182 DeclResult ThisRes
Douglas Gregor23c94db2010-07-02 17:43:08 +00001183 = Actions.ActOnExplicitInstantiation(getCurScope(),
Douglas Gregord5a423b2009-09-25 18:43:00 +00001184 TemplateInfo.ExternLoc,
1185 TemplateInfo.TemplateLoc,
1186 D);
1187 if (ThisRes.isInvalid()) {
1188 SkipUntil(tok::semi, true, true);
John McCalld226f652010-08-21 09:40:31 +00001189 return 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +00001190 }
1191
1192 ThisDecl = ThisRes.get();
1193 break;
1194 }
1195 }
Mike Stump1eb44332009-09-09 15:08:12 +00001196
Richard Smith34b41d92011-02-20 03:19:35 +00001197 bool TypeContainsAuto =
1198 D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto;
1199
Douglas Gregor1426e532009-05-12 21:31:51 +00001200 // Parse declarator '=' initializer.
Argyrios Kyrtzidisa6eb5f82010-10-08 02:39:23 +00001201 if (isTokenEqualOrMistypedEqualEqual(
1202 diag::err_invalid_equalequal_after_declarator)) {
Douglas Gregor1426e532009-05-12 21:31:51 +00001203 ConsumeToken();
Anders Carlsson37bf9d22010-09-24 21:25:25 +00001204 if (Tok.is(tok::kw_delete)) {
Sean Hunte4246a62011-05-12 06:15:49 +00001205 if (D.isFunctionDeclarator())
1206 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
1207 << 1 /* delete */;
1208 else
1209 Diag(ConsumeToken(), diag::err_deleted_non_function);
Sean Huntfe2695e2011-05-06 01:42:00 +00001210 } else if (Tok.is(tok::kw_default)) {
Sean Hunte4246a62011-05-12 06:15:49 +00001211 if (D.isFunctionDeclarator())
1212 Diag(Tok, diag::err_default_delete_in_multiple_declaration)
1213 << 1 /* delete */;
1214 else
1215 Diag(ConsumeToken(), diag::err_default_special_members);
Douglas Gregor1426e532009-05-12 21:31:51 +00001216 } else {
John McCall731ad842009-12-19 09:28:58 +00001217 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
1218 EnterScope(0);
Douglas Gregor23c94db2010-07-02 17:43:08 +00001219 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
John McCall731ad842009-12-19 09:28:58 +00001220 }
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +00001221
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +00001222 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001223 Actions.CodeCompleteInitializer(getCurScope(), ThisDecl);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001224 cutOffParsing();
1225 return 0;
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +00001226 }
1227
John McCall60d7b3a2010-08-24 06:29:42 +00001228 ExprResult Init(ParseInitializer());
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +00001229
John McCall731ad842009-12-19 09:28:58 +00001230 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001231 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
John McCall731ad842009-12-19 09:28:58 +00001232 ExitScope();
1233 }
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +00001234
Douglas Gregor1426e532009-05-12 21:31:51 +00001235 if (Init.isInvalid()) {
Douglas Gregor00225542010-03-01 18:27:54 +00001236 SkipUntil(tok::comma, true, true);
1237 Actions.ActOnInitializerError(ThisDecl);
1238 } else
Richard Smith34b41d92011-02-20 03:19:35 +00001239 Actions.AddInitializerToDecl(ThisDecl, Init.take(),
1240 /*DirectInit=*/false, TypeContainsAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +00001241 }
1242 } else if (Tok.is(tok::l_paren)) {
1243 // Parse C++ direct initializer: '(' expression-list ')'
1244 SourceLocation LParenLoc = ConsumeParen();
1245 ExprVector Exprs(Actions);
1246 CommaLocsTy CommaLocs;
1247
Douglas Gregorb4debae2009-12-22 17:47:17 +00001248 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
1249 EnterScope(0);
Douglas Gregor23c94db2010-07-02 17:43:08 +00001250 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001251 }
1252
Douglas Gregor1426e532009-05-12 21:31:51 +00001253 if (ParseExpressionList(Exprs, CommaLocs)) {
1254 SkipUntil(tok::r_paren);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001255
1256 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001257 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001258 ExitScope();
1259 }
Douglas Gregor1426e532009-05-12 21:31:51 +00001260 } else {
1261 // Match the ')'.
1262 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
1263
1264 assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() &&
1265 "Unexpected number of commas!");
Douglas Gregorb4debae2009-12-22 17:47:17 +00001266
1267 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001268 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001269 ExitScope();
1270 }
1271
Douglas Gregor1426e532009-05-12 21:31:51 +00001272 Actions.AddCXXDirectInitializerToDecl(ThisDecl, LParenLoc,
1273 move_arg(Exprs),
Richard Smith34b41d92011-02-20 03:19:35 +00001274 RParenLoc,
1275 TypeContainsAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +00001276 }
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001277 } else if (getLang().CPlusPlus0x && Tok.is(tok::l_brace)) {
1278 // Parse C++0x braced-init-list.
1279 if (D.getCXXScopeSpec().isSet()) {
1280 EnterScope(0);
1281 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
1282 }
1283
1284 ExprResult Init(ParseBraceInitializer());
1285
1286 if (D.getCXXScopeSpec().isSet()) {
1287 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
1288 ExitScope();
1289 }
1290
1291 if (Init.isInvalid()) {
1292 Actions.ActOnInitializerError(ThisDecl);
1293 } else
1294 Actions.AddInitializerToDecl(ThisDecl, Init.take(),
1295 /*DirectInit=*/true, TypeContainsAuto);
1296
Douglas Gregor1426e532009-05-12 21:31:51 +00001297 } else {
Richard Smith34b41d92011-02-20 03:19:35 +00001298 Actions.ActOnUninitializedDecl(ThisDecl, TypeContainsAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +00001299 }
1300
Richard Smith483b9f32011-02-21 20:05:19 +00001301 Actions.FinalizeDeclaration(ThisDecl);
1302
Douglas Gregor1426e532009-05-12 21:31:51 +00001303 return ThisDecl;
1304}
1305
Reid Spencer5f016e22007-07-11 17:01:13 +00001306/// ParseSpecifierQualifierList
1307/// specifier-qualifier-list:
1308/// type-specifier specifier-qualifier-list[opt]
1309/// type-qualifier specifier-qualifier-list[opt]
1310/// [GNU] attributes specifier-qualifier-list[opt]
1311///
Richard Smithc89edf52011-07-01 19:46:12 +00001312void Parser::ParseSpecifierQualifierList(DeclSpec &DS, AccessSpecifier AS) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001313 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
1314 /// parse declaration-specifiers and complain about extra stuff.
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001315 /// TODO: diagnose attribute-specifiers and alignment-specifiers.
Richard Smithc89edf52011-07-01 19:46:12 +00001316 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS);
Mike Stump1eb44332009-09-09 15:08:12 +00001317
Reid Spencer5f016e22007-07-11 17:01:13 +00001318 // Validate declspec for type-name.
1319 unsigned Specs = DS.getParsedSpecifiers();
Chris Lattnerb6645dd2009-04-14 21:16:09 +00001320 if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers() &&
John McCall7f040a92010-12-24 02:08:15 +00001321 !DS.hasAttributes())
Reid Spencer5f016e22007-07-11 17:01:13 +00001322 Diag(Tok, diag::err_typename_requires_specqual);
Mike Stump1eb44332009-09-09 15:08:12 +00001323
Reid Spencer5f016e22007-07-11 17:01:13 +00001324 // Issue diagnostic and remove storage class if present.
1325 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
1326 if (DS.getStorageClassSpecLoc().isValid())
1327 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
1328 else
1329 Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass);
1330 DS.ClearStorageClassSpecs();
1331 }
Mike Stump1eb44332009-09-09 15:08:12 +00001332
Reid Spencer5f016e22007-07-11 17:01:13 +00001333 // Issue diagnostic and remove function specfier if present.
1334 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Douglas Gregorb48fe382008-10-31 09:07:45 +00001335 if (DS.isInlineSpecified())
1336 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
1337 if (DS.isVirtualSpecified())
1338 Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec);
1339 if (DS.isExplicitSpecified())
1340 Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec);
Reid Spencer5f016e22007-07-11 17:01:13 +00001341 DS.ClearFunctionSpecs();
1342 }
1343}
1344
Chris Lattnerc199ab32009-04-12 20:42:31 +00001345/// isValidAfterIdentifierInDeclaratorAfterDeclSpec - Return true if the
1346/// specified token is valid after the identifier in a declarator which
1347/// immediately follows the declspec. For example, these things are valid:
1348///
1349/// int x [ 4]; // direct-declarator
1350/// int x ( int y); // direct-declarator
1351/// int(int x ) // direct-declarator
1352/// int x ; // simple-declaration
1353/// int x = 17; // init-declarator-list
1354/// int x , y; // init-declarator-list
1355/// int x __asm__ ("foo"); // init-declarator-list
Chris Lattnerb6645dd2009-04-14 21:16:09 +00001356/// int x : 4; // struct-declarator
Chris Lattnerc83c27a2009-04-12 22:29:43 +00001357/// int x { 5}; // C++'0x unified initializers
Chris Lattnerc199ab32009-04-12 20:42:31 +00001358///
1359/// This is not, because 'x' does not immediately follow the declspec (though
1360/// ')' happens to be valid anyway).
1361/// int (x)
1362///
1363static bool isValidAfterIdentifierInDeclarator(const Token &T) {
1364 return T.is(tok::l_square) || T.is(tok::l_paren) || T.is(tok::r_paren) ||
1365 T.is(tok::semi) || T.is(tok::comma) || T.is(tok::equal) ||
Chris Lattnerb6645dd2009-04-14 21:16:09 +00001366 T.is(tok::kw_asm) || T.is(tok::l_brace) || T.is(tok::colon);
Chris Lattnerc199ab32009-04-12 20:42:31 +00001367}
1368
Chris Lattnere40c2952009-04-14 21:34:55 +00001369
1370/// ParseImplicitInt - This method is called when we have an non-typename
1371/// identifier in a declspec (which normally terminates the decl spec) when
1372/// the declspec has no type specifier. In this case, the declspec is either
1373/// malformed or is "implicit int" (in K&R and C89).
1374///
1375/// This method handles diagnosing this prettily and returns false if the
1376/// declspec is done being processed. If it recovers and thinks there may be
1377/// other pieces of declspec after it, it returns true.
1378///
Chris Lattnerf4382f52009-04-14 22:17:06 +00001379bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001380 const ParsedTemplateInfo &TemplateInfo,
Chris Lattnere40c2952009-04-14 21:34:55 +00001381 AccessSpecifier AS) {
Chris Lattnerf4382f52009-04-14 22:17:06 +00001382 assert(Tok.is(tok::identifier) && "should have identifier");
Mike Stump1eb44332009-09-09 15:08:12 +00001383
Chris Lattnere40c2952009-04-14 21:34:55 +00001384 SourceLocation Loc = Tok.getLocation();
1385 // If we see an identifier that is not a type name, we normally would
1386 // parse it as the identifer being declared. However, when a typename
1387 // is typo'd or the definition is not included, this will incorrectly
1388 // parse the typename as the identifier name and fall over misparsing
1389 // later parts of the diagnostic.
1390 //
1391 // As such, we try to do some look-ahead in cases where this would
1392 // otherwise be an "implicit-int" case to see if this is invalid. For
1393 // example: "static foo_t x = 4;" In this case, if we parsed foo_t as
1394 // an identifier with implicit int, we'd get a parse error because the
1395 // next token is obviously invalid for a type. Parse these as a case
1396 // with an invalid type specifier.
1397 assert(!DS.hasTypeSpecifier() && "Type specifier checked above");
Mike Stump1eb44332009-09-09 15:08:12 +00001398
Chris Lattnere40c2952009-04-14 21:34:55 +00001399 // Since we know that this either implicit int (which is rare) or an
1400 // error, we'd do lookahead to try to do better recovery.
1401 if (isValidAfterIdentifierInDeclarator(NextToken())) {
1402 // If this token is valid for implicit int, e.g. "static x = 4", then
1403 // we just avoid eating the identifier, so it will be parsed as the
1404 // identifier in the declarator.
1405 return false;
1406 }
Mike Stump1eb44332009-09-09 15:08:12 +00001407
Chris Lattnere40c2952009-04-14 21:34:55 +00001408 // Otherwise, if we don't consume this token, we are going to emit an
1409 // error anyway. Try to recover from various common problems. Check
1410 // to see if this was a reference to a tag name without a tag specified.
1411 // This is a common problem in C (saying 'foo' instead of 'struct foo').
Chris Lattnerf4382f52009-04-14 22:17:06 +00001412 //
1413 // C++ doesn't need this, and isTagName doesn't take SS.
1414 if (SS == 0) {
Argyrios Kyrtzidisb8a9d3b2011-04-21 17:29:47 +00001415 const char *TagName = 0, *FixitTagName = 0;
Chris Lattnerf4382f52009-04-14 22:17:06 +00001416 tok::TokenKind TagKind = tok::unknown;
Mike Stump1eb44332009-09-09 15:08:12 +00001417
Douglas Gregor23c94db2010-07-02 17:43:08 +00001418 switch (Actions.isTagName(*Tok.getIdentifierInfo(), getCurScope())) {
Chris Lattnere40c2952009-04-14 21:34:55 +00001419 default: break;
Argyrios Kyrtzidisb8a9d3b2011-04-21 17:29:47 +00001420 case DeclSpec::TST_enum:
1421 TagName="enum" ; FixitTagName = "enum " ; TagKind=tok::kw_enum ;break;
1422 case DeclSpec::TST_union:
1423 TagName="union" ; FixitTagName = "union " ;TagKind=tok::kw_union ;break;
1424 case DeclSpec::TST_struct:
1425 TagName="struct"; FixitTagName = "struct ";TagKind=tok::kw_struct;break;
1426 case DeclSpec::TST_class:
1427 TagName="class" ; FixitTagName = "class " ;TagKind=tok::kw_class ;break;
Chris Lattnere40c2952009-04-14 21:34:55 +00001428 }
Mike Stump1eb44332009-09-09 15:08:12 +00001429
Chris Lattnerf4382f52009-04-14 22:17:06 +00001430 if (TagName) {
1431 Diag(Loc, diag::err_use_of_tag_name_without_tag)
John McCall23e907a2010-02-14 01:03:10 +00001432 << Tok.getIdentifierInfo() << TagName << getLang().CPlusPlus
Argyrios Kyrtzidisb8a9d3b2011-04-21 17:29:47 +00001433 << FixItHint::CreateInsertion(Tok.getLocation(),FixitTagName);
Mike Stump1eb44332009-09-09 15:08:12 +00001434
Chris Lattnerf4382f52009-04-14 22:17:06 +00001435 // Parse this as a tag as if the missing tag were present.
1436 if (TagKind == tok::kw_enum)
Douglas Gregor9b9edd62010-03-02 17:53:14 +00001437 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS);
Chris Lattnerf4382f52009-04-14 22:17:06 +00001438 else
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001439 ParseClassSpecifier(TagKind, Loc, DS, TemplateInfo, AS);
Chris Lattnerf4382f52009-04-14 22:17:06 +00001440 return true;
1441 }
Chris Lattnere40c2952009-04-14 21:34:55 +00001442 }
Mike Stump1eb44332009-09-09 15:08:12 +00001443
Douglas Gregora786fdb2009-10-13 23:27:22 +00001444 // This is almost certainly an invalid type name. Let the action emit a
1445 // diagnostic and attempt to recover.
John McCallb3d87482010-08-24 05:47:05 +00001446 ParsedType T;
Douglas Gregora786fdb2009-10-13 23:27:22 +00001447 if (Actions.DiagnoseUnknownTypeName(*Tok.getIdentifierInfo(), Loc,
Douglas Gregor23c94db2010-07-02 17:43:08 +00001448 getCurScope(), SS, T)) {
Douglas Gregora786fdb2009-10-13 23:27:22 +00001449 // The action emitted a diagnostic, so we don't have to.
1450 if (T) {
1451 // The action has suggested that the type T could be used. Set that as
1452 // the type in the declaration specifiers, consume the would-be type
1453 // name token, and we're done.
1454 const char *PrevSpec;
1455 unsigned DiagID;
John McCallb3d87482010-08-24 05:47:05 +00001456 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID, T);
Douglas Gregora786fdb2009-10-13 23:27:22 +00001457 DS.SetRangeEnd(Tok.getLocation());
1458 ConsumeToken();
1459
1460 // There may be other declaration specifiers after this.
1461 return true;
1462 }
1463
1464 // Fall through; the action had no suggestion for us.
1465 } else {
1466 // The action did not emit a diagnostic, so emit one now.
1467 SourceRange R;
1468 if (SS) R = SS->getRange();
1469 Diag(Loc, diag::err_unknown_typename) << Tok.getIdentifierInfo() << R;
1470 }
Mike Stump1eb44332009-09-09 15:08:12 +00001471
Douglas Gregora786fdb2009-10-13 23:27:22 +00001472 // Mark this as an error.
Chris Lattnere40c2952009-04-14 21:34:55 +00001473 const char *PrevSpec;
John McCallfec54012009-08-03 20:12:06 +00001474 unsigned DiagID;
1475 DS.SetTypeSpecType(DeclSpec::TST_error, Loc, PrevSpec, DiagID);
Chris Lattnere40c2952009-04-14 21:34:55 +00001476 DS.SetRangeEnd(Tok.getLocation());
1477 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001478
Chris Lattnere40c2952009-04-14 21:34:55 +00001479 // TODO: Could inject an invalid typedef decl in an enclosing scope to
1480 // avoid rippling error messages on subsequent uses of the same type,
1481 // could be useful if #include was forgotten.
1482 return false;
1483}
1484
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001485/// \brief Determine the declaration specifier context from the declarator
1486/// context.
1487///
1488/// \param Context the declarator context, which is one of the
1489/// Declarator::TheContext enumerator values.
1490Parser::DeclSpecContext
1491Parser::getDeclSpecContextFromDeclaratorContext(unsigned Context) {
1492 if (Context == Declarator::MemberContext)
1493 return DSC_class;
1494 if (Context == Declarator::FileContext)
1495 return DSC_top_level;
1496 return DSC_normal;
1497}
1498
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001499/// ParseAlignArgument - Parse the argument to an alignment-specifier.
1500///
1501/// FIXME: Simply returns an alignof() expression if the argument is a
1502/// type. Ideally, the type should be propagated directly into Sema.
1503///
1504/// [C1X/C++0x] type-id
1505/// [C1X] constant-expression
1506/// [C++0x] assignment-expression
1507ExprResult Parser::ParseAlignArgument(SourceLocation Start) {
1508 if (isTypeIdInParens()) {
1509 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated);
1510 SourceLocation TypeLoc = Tok.getLocation();
1511 ParsedType Ty = ParseTypeName().get();
1512 SourceRange TypeRange(Start, Tok.getLocation());
1513 return Actions.ActOnUnaryExprOrTypeTraitExpr(TypeLoc, UETT_AlignOf, true,
1514 Ty.getAsOpaquePtr(), TypeRange);
1515 } else
1516 return ParseConstantExpression();
1517}
1518
1519/// ParseAlignmentSpecifier - Parse an alignment-specifier, and add the
1520/// attribute to Attrs.
1521///
1522/// alignment-specifier:
1523/// [C1X] '_Alignas' '(' type-id ')'
1524/// [C1X] '_Alignas' '(' constant-expression ')'
1525/// [C++0x] 'alignas' '(' type-id ')'
1526/// [C++0x] 'alignas' '(' assignment-expression ')'
1527void Parser::ParseAlignmentSpecifier(ParsedAttributes &Attrs,
1528 SourceLocation *endLoc) {
1529 assert((Tok.is(tok::kw_alignas) || Tok.is(tok::kw__Alignas)) &&
1530 "Not an alignment-specifier!");
1531
1532 SourceLocation KWLoc = Tok.getLocation();
1533 ConsumeToken();
1534
1535 SourceLocation ParamLoc = Tok.getLocation();
1536 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen))
1537 return;
1538
1539 ExprResult ArgExpr = ParseAlignArgument(ParamLoc);
1540 if (ArgExpr.isInvalid()) {
1541 SkipUntil(tok::r_paren);
1542 return;
1543 }
1544
1545 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, ParamLoc);
1546 if (endLoc)
1547 *endLoc = RParenLoc;
1548
1549 ExprVector ArgExprs(Actions);
1550 ArgExprs.push_back(ArgExpr.release());
1551 Attrs.addNew(PP.getIdentifierInfo("aligned"), KWLoc, 0, KWLoc,
1552 0, ParamLoc, ArgExprs.take(), 1, false, true);
1553}
1554
Reid Spencer5f016e22007-07-11 17:01:13 +00001555/// ParseDeclarationSpecifiers
1556/// declaration-specifiers: [C99 6.7]
1557/// storage-class-specifier declaration-specifiers[opt]
1558/// type-specifier declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00001559/// [C99] function-specifier declaration-specifiers[opt]
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001560/// [C1X] alignment-specifier declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00001561/// [GNU] attributes declaration-specifiers[opt]
Douglas Gregor8d267c52011-09-09 02:06:17 +00001562/// [Clang] '__module_private__' declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00001563///
1564/// storage-class-specifier: [C99 6.7.1]
1565/// 'typedef'
1566/// 'extern'
1567/// 'static'
1568/// 'auto'
1569/// 'register'
Sebastian Redl669d5d72008-11-14 23:42:31 +00001570/// [C++] 'mutable'
Reid Spencer5f016e22007-07-11 17:01:13 +00001571/// [GNU] '__thread'
Reid Spencer5f016e22007-07-11 17:01:13 +00001572/// function-specifier: [C99 6.7.4]
1573/// [C99] 'inline'
Douglas Gregorb48fe382008-10-31 09:07:45 +00001574/// [C++] 'virtual'
1575/// [C++] 'explicit'
Peter Collingbournef315fa82011-02-14 01:42:53 +00001576/// [OpenCL] '__kernel'
Anders Carlssonf47f7a12009-05-06 04:46:28 +00001577/// 'friend': [C++ dcl.friend]
Sebastian Redl2ac67232009-11-05 15:47:02 +00001578/// 'constexpr': [C++0x dcl.constexpr]
Anders Carlssonf47f7a12009-05-06 04:46:28 +00001579
Reid Spencer5f016e22007-07-11 17:01:13 +00001580///
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +00001581void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001582 const ParsedTemplateInfo &TemplateInfo,
John McCall67d1a672009-08-06 02:15:43 +00001583 AccessSpecifier AS,
Douglas Gregor312eadb2011-04-24 05:37:28 +00001584 DeclSpecContext DSContext) {
1585 if (DS.getSourceRange().isInvalid()) {
1586 DS.SetRangeStart(Tok.getLocation());
1587 DS.SetRangeEnd(Tok.getLocation());
1588 }
1589
Reid Spencer5f016e22007-07-11 17:01:13 +00001590 while (1) {
John McCallfec54012009-08-03 20:12:06 +00001591 bool isInvalid = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001592 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00001593 unsigned DiagID = 0;
1594
Reid Spencer5f016e22007-07-11 17:01:13 +00001595 SourceLocation Loc = Tok.getLocation();
Douglas Gregor12e083c2008-11-07 15:42:26 +00001596
Reid Spencer5f016e22007-07-11 17:01:13 +00001597 switch (Tok.getKind()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001598 default:
Chris Lattnerbce61352008-07-26 00:20:22 +00001599 DoneWithDeclSpec:
Peter Collingbournef1907682011-09-29 18:03:57 +00001600 // [C++0x] decl-specifier-seq: decl-specifier attribute-specifier-seq[opt]
1601 MaybeParseCXX0XAttributes(DS.getAttributes());
1602
Reid Spencer5f016e22007-07-11 17:01:13 +00001603 // If this is not a declaration specifier token, we're done reading decl
1604 // specifiers. First verify that DeclSpec's are consistent.
Douglas Gregor9b3064b2009-04-01 22:41:11 +00001605 DS.Finish(Diags, PP);
Reid Spencer5f016e22007-07-11 17:01:13 +00001606 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001607
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001608 case tok::code_completion: {
John McCallf312b1e2010-08-26 23:41:50 +00001609 Sema::ParserCompletionContext CCC = Sema::PCC_Namespace;
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001610 if (DS.hasTypeSpecifier()) {
1611 bool AllowNonIdentifiers
1612 = (getCurScope()->getFlags() & (Scope::ControlScope |
1613 Scope::BlockScope |
1614 Scope::TemplateParamScope |
1615 Scope::FunctionPrototypeScope |
1616 Scope::AtCatchScope)) == 0;
1617 bool AllowNestedNameSpecifiers
1618 = DSContext == DSC_top_level ||
1619 (DSContext == DSC_class && DS.isFriendSpecified());
1620
Douglas Gregorc7b6d882010-09-16 15:14:18 +00001621 Actions.CodeCompleteDeclSpec(getCurScope(), DS,
1622 AllowNonIdentifiers,
1623 AllowNestedNameSpecifiers);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001624 return cutOffParsing();
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001625 }
1626
Douglas Gregor68e3c2e2011-02-15 20:33:25 +00001627 if (getCurScope()->getFnParent() || getCurScope()->getBlockParent())
1628 CCC = Sema::PCC_LocalDeclarationSpecifiers;
1629 else if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate)
John McCallf312b1e2010-08-26 23:41:50 +00001630 CCC = DSContext == DSC_class? Sema::PCC_MemberTemplate
1631 : Sema::PCC_Template;
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001632 else if (DSContext == DSC_class)
John McCallf312b1e2010-08-26 23:41:50 +00001633 CCC = Sema::PCC_Class;
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001634 else if (ObjCImpDecl)
John McCallf312b1e2010-08-26 23:41:50 +00001635 CCC = Sema::PCC_ObjCImplementation;
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001636
1637 Actions.CodeCompleteOrdinaryName(getCurScope(), CCC);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001638 return cutOffParsing();
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001639 }
1640
Chris Lattner5e02c472009-01-05 00:07:25 +00001641 case tok::coloncolon: // ::foo::bar
John McCall9ba61662010-02-26 08:45:28 +00001642 // C++ scope specifier. Annotate and loop, or bail out on error.
1643 if (TryAnnotateCXXScopeToken(true)) {
1644 if (!DS.hasTypeSpecifier())
1645 DS.SetTypeSpecError();
1646 goto DoneWithDeclSpec;
1647 }
John McCall2e0a7152010-03-01 18:20:46 +00001648 if (Tok.is(tok::coloncolon)) // ::new or ::delete
1649 goto DoneWithDeclSpec;
John McCall9ba61662010-02-26 08:45:28 +00001650 continue;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001651
1652 case tok::annot_cxxscope: {
1653 if (DS.hasTypeSpecifier())
1654 goto DoneWithDeclSpec;
1655
John McCallaa87d332009-12-12 11:40:51 +00001656 CXXScopeSpec SS;
Douglas Gregorc34348a2011-02-24 17:54:50 +00001657 Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(),
1658 Tok.getAnnotationRange(),
1659 SS);
John McCallaa87d332009-12-12 11:40:51 +00001660
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001661 // We are looking for a qualified typename.
Douglas Gregor9135c722009-03-25 15:40:00 +00001662 Token Next = NextToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001663 if (Next.is(tok::annot_template_id) &&
Douglas Gregor9135c722009-03-25 15:40:00 +00001664 static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue())
Douglas Gregorc45c2322009-03-31 00:43:58 +00001665 ->Kind == TNK_Type_template) {
Douglas Gregor9135c722009-03-25 15:40:00 +00001666 // We have a qualified template-id, e.g., N::A<int>
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001667
1668 // C++ [class.qual]p2:
1669 // In a lookup in which the constructor is an acceptable lookup
1670 // result and the nested-name-specifier nominates a class C:
1671 //
1672 // - if the name specified after the
1673 // nested-name-specifier, when looked up in C, is the
1674 // injected-class-name of C (Clause 9), or
1675 //
1676 // - if the name specified after the nested-name-specifier
1677 // is the same as the identifier or the
1678 // simple-template-id's template-name in the last
1679 // component of the nested-name-specifier,
1680 //
1681 // the name is instead considered to name the constructor of
1682 // class C.
1683 //
1684 // Thus, if the template-name is actually the constructor
1685 // name, then the code is ill-formed; this interpretation is
1686 // reinforced by the NAD status of core issue 635.
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +00001687 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Next);
John McCallba9d8532010-04-13 06:39:49 +00001688 if ((DSContext == DSC_top_level ||
1689 (DSContext == DSC_class && DS.isFriendSpecified())) &&
1690 TemplateId->Name &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00001691 Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) {
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001692 if (isConstructorDeclarator()) {
1693 // The user meant this to be an out-of-line constructor
1694 // definition, but template arguments are not allowed
1695 // there. Just allow this as a constructor; we'll
1696 // complain about it later.
1697 goto DoneWithDeclSpec;
1698 }
1699
1700 // The user meant this to name a type, but it actually names
1701 // a constructor with some extraneous template
1702 // arguments. Complain, then parse it as a type as the user
1703 // intended.
1704 Diag(TemplateId->TemplateNameLoc,
1705 diag::err_out_of_line_template_id_names_constructor)
1706 << TemplateId->Name;
1707 }
1708
John McCallaa87d332009-12-12 11:40:51 +00001709 DS.getTypeSpecScope() = SS;
1710 ConsumeToken(); // The C++ scope.
Mike Stump1eb44332009-09-09 15:08:12 +00001711 assert(Tok.is(tok::annot_template_id) &&
Douglas Gregor9135c722009-03-25 15:40:00 +00001712 "ParseOptionalCXXScopeSpecifier not working");
Douglas Gregor059101f2011-03-02 00:47:37 +00001713 AnnotateTemplateIdTokenAsType();
Douglas Gregor9135c722009-03-25 15:40:00 +00001714 continue;
1715 }
1716
Douglas Gregor9d7b3532009-09-28 07:26:33 +00001717 if (Next.is(tok::annot_typename)) {
John McCallaa87d332009-12-12 11:40:51 +00001718 DS.getTypeSpecScope() = SS;
1719 ConsumeToken(); // The C++ scope.
John McCallb3d87482010-08-24 05:47:05 +00001720 if (Tok.getAnnotationValue()) {
1721 ParsedType T = getTypeAnnotation(Tok);
Nico Weber253e80b2010-11-22 10:30:56 +00001722 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename,
1723 Tok.getAnnotationEndLoc(),
John McCallb3d87482010-08-24 05:47:05 +00001724 PrevSpec, DiagID, T);
1725 }
Douglas Gregor9d7b3532009-09-28 07:26:33 +00001726 else
1727 DS.SetTypeSpecError();
1728 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1729 ConsumeToken(); // The typename
1730 }
1731
Douglas Gregor9135c722009-03-25 15:40:00 +00001732 if (Next.isNot(tok::identifier))
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001733 goto DoneWithDeclSpec;
1734
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001735 // If we're in a context where the identifier could be a class name,
1736 // check whether this is a constructor declaration.
John McCallba9d8532010-04-13 06:39:49 +00001737 if ((DSContext == DSC_top_level ||
1738 (DSContext == DSC_class && DS.isFriendSpecified())) &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00001739 Actions.isCurrentClassName(*Next.getIdentifierInfo(), getCurScope(),
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001740 &SS)) {
1741 if (isConstructorDeclarator())
1742 goto DoneWithDeclSpec;
1743
1744 // As noted in C++ [class.qual]p2 (cited above), when the name
1745 // of the class is qualified in a context where it could name
1746 // a constructor, its a constructor name. However, we've
1747 // looked at the declarator, and the user probably meant this
1748 // to be a type. Complain that it isn't supposed to be treated
1749 // as a type, then proceed to parse it as a type.
1750 Diag(Next.getLocation(), diag::err_out_of_line_type_names_constructor)
1751 << Next.getIdentifierInfo();
1752 }
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001753
John McCallb3d87482010-08-24 05:47:05 +00001754 ParsedType TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(),
1755 Next.getLocation(),
Douglas Gregor9e876872011-03-01 18:12:44 +00001756 getCurScope(), &SS,
1757 false, false, ParsedType(),
1758 /*NonTrivialSourceInfo=*/true);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001759
Chris Lattnerf4382f52009-04-14 22:17:06 +00001760 // If the referenced identifier is not a type, then this declspec is
1761 // erroneous: We already checked about that it has no type specifier, and
1762 // C++ doesn't have implicit int. Diagnose it as a typo w.r.t. to the
Mike Stump1eb44332009-09-09 15:08:12 +00001763 // typename.
Chris Lattnerf4382f52009-04-14 22:17:06 +00001764 if (TypeRep == 0) {
1765 ConsumeToken(); // Eat the scope spec so the identifier is current.
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001766 if (ParseImplicitInt(DS, &SS, TemplateInfo, AS)) continue;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001767 goto DoneWithDeclSpec;
Chris Lattnerf4382f52009-04-14 22:17:06 +00001768 }
Mike Stump1eb44332009-09-09 15:08:12 +00001769
John McCallaa87d332009-12-12 11:40:51 +00001770 DS.getTypeSpecScope() = SS;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001771 ConsumeToken(); // The C++ scope.
1772
Douglas Gregor1a51b4a2009-02-09 15:09:02 +00001773 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001774 DiagID, TypeRep);
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001775 if (isInvalid)
1776 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001777
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001778 DS.SetRangeEnd(Tok.getLocation());
1779 ConsumeToken(); // The typename.
1780
1781 continue;
1782 }
Mike Stump1eb44332009-09-09 15:08:12 +00001783
Chris Lattner80d0c892009-01-21 19:48:37 +00001784 case tok::annot_typename: {
John McCallb3d87482010-08-24 05:47:05 +00001785 if (Tok.getAnnotationValue()) {
1786 ParsedType T = getTypeAnnotation(Tok);
Nico Weberc43271e2010-11-22 12:50:03 +00001787 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallb3d87482010-08-24 05:47:05 +00001788 DiagID, T);
1789 } else
Douglas Gregor31a19b62009-04-01 21:51:26 +00001790 DS.SetTypeSpecError();
Chris Lattner5c5db552010-04-05 18:18:31 +00001791
1792 if (isInvalid)
1793 break;
1794
Chris Lattner80d0c892009-01-21 19:48:37 +00001795 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1796 ConsumeToken(); // The typename
Mike Stump1eb44332009-09-09 15:08:12 +00001797
Chris Lattner80d0c892009-01-21 19:48:37 +00001798 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1799 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001800 // Objective-C interface.
1801 if (Tok.is(tok::less) && getLang().ObjC1)
1802 ParseObjCProtocolQualifiers(DS);
1803
Chris Lattner80d0c892009-01-21 19:48:37 +00001804 continue;
1805 }
Mike Stump1eb44332009-09-09 15:08:12 +00001806
Douglas Gregorbfad9152011-04-28 15:48:45 +00001807 case tok::kw___is_signed:
1808 // GNU libstdc++ 4.4 uses __is_signed as an identifier, but Clang
1809 // typically treats it as a trait. If we see __is_signed as it appears
1810 // in libstdc++, e.g.,
1811 //
1812 // static const bool __is_signed;
1813 //
1814 // then treat __is_signed as an identifier rather than as a keyword.
1815 if (DS.getTypeSpecType() == TST_bool &&
1816 DS.getTypeQualifiers() == DeclSpec::TQ_const &&
1817 DS.getStorageClassSpec() == DeclSpec::SCS_static) {
1818 Tok.getIdentifierInfo()->RevertTokenIDToIdentifier();
1819 Tok.setKind(tok::identifier);
1820 }
1821
1822 // We're done with the declaration-specifiers.
1823 goto DoneWithDeclSpec;
1824
Chris Lattner3bd934a2008-07-26 01:18:38 +00001825 // typedef-name
1826 case tok::identifier: {
Chris Lattner5e02c472009-01-05 00:07:25 +00001827 // In C++, check to see if this is a scope specifier like foo::bar::, if
1828 // so handle it as such. This is important for ctor parsing.
John McCall9ba61662010-02-26 08:45:28 +00001829 if (getLang().CPlusPlus) {
1830 if (TryAnnotateCXXScopeToken(true)) {
1831 if (!DS.hasTypeSpecifier())
1832 DS.SetTypeSpecError();
1833 goto DoneWithDeclSpec;
1834 }
1835 if (!Tok.is(tok::identifier))
1836 continue;
1837 }
Mike Stump1eb44332009-09-09 15:08:12 +00001838
Chris Lattner3bd934a2008-07-26 01:18:38 +00001839 // This identifier can only be a typedef name if we haven't already seen
1840 // a type-specifier. Without this check we misparse:
1841 // typedef int X; struct Y { short X; }; as 'short int'.
1842 if (DS.hasTypeSpecifier())
1843 goto DoneWithDeclSpec;
Mike Stump1eb44332009-09-09 15:08:12 +00001844
John Thompson82287d12010-02-05 00:12:22 +00001845 // Check for need to substitute AltiVec keyword tokens.
1846 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
1847 break;
1848
Chris Lattner3bd934a2008-07-26 01:18:38 +00001849 // It has to be available as a typedef too!
John McCallb3d87482010-08-24 05:47:05 +00001850 ParsedType TypeRep =
1851 Actions.getTypeName(*Tok.getIdentifierInfo(),
1852 Tok.getLocation(), getCurScope());
Douglas Gregor55f6b142009-02-09 18:46:07 +00001853
Chris Lattnerc199ab32009-04-12 20:42:31 +00001854 // If this is not a typedef name, don't parse it as part of the declspec,
1855 // it must be an implicit int or an error.
John McCallb3d87482010-08-24 05:47:05 +00001856 if (!TypeRep) {
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001857 if (ParseImplicitInt(DS, 0, TemplateInfo, AS)) continue;
Chris Lattner3bd934a2008-07-26 01:18:38 +00001858 goto DoneWithDeclSpec;
Chris Lattnerc199ab32009-04-12 20:42:31 +00001859 }
Douglas Gregor55f6b142009-02-09 18:46:07 +00001860
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001861 // If we're in a context where the identifier could be a class name,
1862 // check whether this is a constructor declaration.
1863 if (getLang().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00001864 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), getCurScope()) &&
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001865 isConstructorDeclarator())
Douglas Gregorb48fe382008-10-31 09:07:45 +00001866 goto DoneWithDeclSpec;
1867
Douglas Gregor1a51b4a2009-02-09 15:09:02 +00001868 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001869 DiagID, TypeRep);
Chris Lattner3bd934a2008-07-26 01:18:38 +00001870 if (isInvalid)
1871 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001872
Chris Lattner3bd934a2008-07-26 01:18:38 +00001873 DS.SetRangeEnd(Tok.getLocation());
1874 ConsumeToken(); // The identifier
1875
1876 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1877 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001878 // Objective-C interface.
1879 if (Tok.is(tok::less) && getLang().ObjC1)
1880 ParseObjCProtocolQualifiers(DS);
1881
Steve Naroff4f9b9f12008-09-22 10:28:57 +00001882 // Need to support trailing type qualifiers (e.g. "id<p> const").
1883 // If a type specifier follows, it will be diagnosed elsewhere.
1884 continue;
Chris Lattner3bd934a2008-07-26 01:18:38 +00001885 }
Douglas Gregor39a8de12009-02-25 19:37:18 +00001886
1887 // type-name
1888 case tok::annot_template_id: {
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +00001889 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregorc45c2322009-03-31 00:43:58 +00001890 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor39a8de12009-02-25 19:37:18 +00001891 // This template-id does not refer to a type name, so we're
1892 // done with the type-specifiers.
1893 goto DoneWithDeclSpec;
1894 }
1895
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001896 // If we're in a context where the template-id could be a
1897 // constructor name or specialization, check whether this is a
1898 // constructor declaration.
1899 if (getLang().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00001900 Actions.isCurrentClassName(*TemplateId->Name, getCurScope()) &&
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001901 isConstructorDeclarator())
1902 goto DoneWithDeclSpec;
1903
Douglas Gregor39a8de12009-02-25 19:37:18 +00001904 // Turn the template-id annotation token into a type annotation
1905 // token, then try again to parse it as a type-specifier.
Douglas Gregor31a19b62009-04-01 21:51:26 +00001906 AnnotateTemplateIdTokenAsType();
Douglas Gregor39a8de12009-02-25 19:37:18 +00001907 continue;
1908 }
1909
Reid Spencer5f016e22007-07-11 17:01:13 +00001910 // GNU attributes support.
1911 case tok::kw___attribute:
John McCall7f040a92010-12-24 02:08:15 +00001912 ParseGNUAttributes(DS.getAttributes());
Reid Spencer5f016e22007-07-11 17:01:13 +00001913 continue;
Steve Narofff59e17e2008-12-24 20:59:21 +00001914
1915 // Microsoft declspec support.
1916 case tok::kw___declspec:
John McCall7f040a92010-12-24 02:08:15 +00001917 ParseMicrosoftDeclSpec(DS.getAttributes());
Steve Narofff59e17e2008-12-24 20:59:21 +00001918 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001919
Steve Naroff239f0732008-12-25 14:16:32 +00001920 // Microsoft single token adornments.
Steve Naroff86bc6cf2008-12-25 14:41:26 +00001921 case tok::kw___forceinline:
Eli Friedman290eeb02009-06-08 23:27:34 +00001922 // FIXME: Add handling here!
1923 break;
1924
1925 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00001926 case tok::kw___ptr32:
Steve Naroff86bc6cf2008-12-25 14:41:26 +00001927 case tok::kw___w64:
Steve Naroff239f0732008-12-25 14:16:32 +00001928 case tok::kw___cdecl:
1929 case tok::kw___stdcall:
1930 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00001931 case tok::kw___thiscall:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00001932 case tok::kw___unaligned:
John McCall7f040a92010-12-24 02:08:15 +00001933 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman290eeb02009-06-08 23:27:34 +00001934 continue;
1935
Dawn Perchik52fc3142010-09-03 01:29:35 +00001936 // Borland single token adornments.
1937 case tok::kw___pascal:
John McCall7f040a92010-12-24 02:08:15 +00001938 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik52fc3142010-09-03 01:29:35 +00001939 continue;
1940
Peter Collingbournef315fa82011-02-14 01:42:53 +00001941 // OpenCL single token adornments.
1942 case tok::kw___kernel:
1943 ParseOpenCLAttributes(DS.getAttributes());
1944 continue;
1945
Reid Spencer5f016e22007-07-11 17:01:13 +00001946 // storage-class-specifier
1947 case tok::kw_typedef:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00001948 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_typedef, Loc,
1949 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001950 break;
1951 case tok::kw_extern:
1952 if (DS.isThreadSpecified())
Chris Lattner1ab3b962008-11-18 07:48:38 +00001953 Diag(Tok, diag::ext_thread_before) << "extern";
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00001954 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_extern, Loc,
1955 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001956 break;
Steve Naroff8d54bf22007-12-18 00:16:02 +00001957 case tok::kw___private_extern__:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00001958 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_private_extern,
1959 Loc, PrevSpec, DiagID);
Steve Naroff8d54bf22007-12-18 00:16:02 +00001960 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00001961 case tok::kw_static:
1962 if (DS.isThreadSpecified())
Chris Lattner1ab3b962008-11-18 07:48:38 +00001963 Diag(Tok, diag::ext_thread_before) << "static";
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00001964 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_static, Loc,
1965 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001966 break;
1967 case tok::kw_auto:
Douglas Gregor18d8b792011-03-14 21:43:30 +00001968 if (getLang().CPlusPlus0x) {
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00001969 if (isKnownToBeTypeSpecifier(GetLookAheadToken(1))) {
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00001970 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc,
1971 PrevSpec, DiagID);
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00001972 if (!isInvalid)
Richard Smith8f4fb192011-09-04 19:54:14 +00001973 Diag(Tok, diag::ext_auto_storage_class)
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00001974 << FixItHint::CreateRemoval(DS.getStorageClassSpecLoc());
Richard Smith8f4fb192011-09-04 19:54:14 +00001975 } else
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00001976 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec,
1977 DiagID);
Richard Smith8f4fb192011-09-04 19:54:14 +00001978 } else
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00001979 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc,
1980 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001981 break;
1982 case tok::kw_register:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00001983 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_register, Loc,
1984 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001985 break;
Sebastian Redl669d5d72008-11-14 23:42:31 +00001986 case tok::kw_mutable:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00001987 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_mutable, Loc,
1988 PrevSpec, DiagID);
Sebastian Redl669d5d72008-11-14 23:42:31 +00001989 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00001990 case tok::kw___thread:
John McCallfec54012009-08-03 20:12:06 +00001991 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001992 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001993
Reid Spencer5f016e22007-07-11 17:01:13 +00001994 // function-specifier
1995 case tok::kw_inline:
John McCallfec54012009-08-03 20:12:06 +00001996 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001997 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +00001998 case tok::kw_virtual:
John McCallfec54012009-08-03 20:12:06 +00001999 isInvalid = DS.SetFunctionSpecVirtual(Loc, PrevSpec, DiagID);
Douglas Gregorb48fe382008-10-31 09:07:45 +00002000 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +00002001 case tok::kw_explicit:
John McCallfec54012009-08-03 20:12:06 +00002002 isInvalid = DS.SetFunctionSpecExplicit(Loc, PrevSpec, DiagID);
Douglas Gregorb48fe382008-10-31 09:07:45 +00002003 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002004
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002005 // alignment-specifier
2006 case tok::kw__Alignas:
2007 if (!getLang().C1X)
2008 Diag(Tok, diag::ext_c1x_alignas);
2009 ParseAlignmentSpecifier(DS.getAttributes());
2010 continue;
2011
Anders Carlssonf47f7a12009-05-06 04:46:28 +00002012 // friend
2013 case tok::kw_friend:
John McCall67d1a672009-08-06 02:15:43 +00002014 if (DSContext == DSC_class)
2015 isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID);
2016 else {
2017 PrevSpec = ""; // not actually used by the diagnostic
2018 DiagID = diag::err_friend_invalid_in_context;
2019 isInvalid = true;
2020 }
Anders Carlssonf47f7a12009-05-06 04:46:28 +00002021 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002022
Douglas Gregor8d267c52011-09-09 02:06:17 +00002023 // Modules
2024 case tok::kw___module_private__:
2025 isInvalid = DS.setModulePrivateSpec(Loc, PrevSpec, DiagID);
2026 break;
2027
Sebastian Redl2ac67232009-11-05 15:47:02 +00002028 // constexpr
2029 case tok::kw_constexpr:
2030 isInvalid = DS.SetConstexprSpec(Loc, PrevSpec, DiagID);
2031 break;
2032
Chris Lattner80d0c892009-01-21 19:48:37 +00002033 // type-specifier
2034 case tok::kw_short:
John McCallfec54012009-08-03 20:12:06 +00002035 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec,
2036 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002037 break;
2038 case tok::kw_long:
2039 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCallfec54012009-08-03 20:12:06 +00002040 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
2041 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002042 else
John McCallfec54012009-08-03 20:12:06 +00002043 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2044 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002045 break;
Francois Pichet338d7f72011-04-28 01:59:37 +00002046 case tok::kw___int64:
2047 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2048 DiagID);
2049 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002050 case tok::kw_signed:
John McCallfec54012009-08-03 20:12:06 +00002051 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec,
2052 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002053 break;
2054 case tok::kw_unsigned:
John McCallfec54012009-08-03 20:12:06 +00002055 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
2056 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002057 break;
2058 case tok::kw__Complex:
John McCallfec54012009-08-03 20:12:06 +00002059 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
2060 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002061 break;
2062 case tok::kw__Imaginary:
John McCallfec54012009-08-03 20:12:06 +00002063 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
2064 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002065 break;
2066 case tok::kw_void:
John McCallfec54012009-08-03 20:12:06 +00002067 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec,
2068 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002069 break;
2070 case tok::kw_char:
John McCallfec54012009-08-03 20:12:06 +00002071 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec,
2072 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002073 break;
2074 case tok::kw_int:
John McCallfec54012009-08-03 20:12:06 +00002075 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec,
2076 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002077 break;
2078 case tok::kw_float:
John McCallfec54012009-08-03 20:12:06 +00002079 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec,
2080 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002081 break;
2082 case tok::kw_double:
John McCallfec54012009-08-03 20:12:06 +00002083 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec,
2084 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002085 break;
2086 case tok::kw_wchar_t:
John McCallfec54012009-08-03 20:12:06 +00002087 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec,
2088 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002089 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002090 case tok::kw_char16_t:
John McCallfec54012009-08-03 20:12:06 +00002091 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec,
2092 DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002093 break;
2094 case tok::kw_char32_t:
John McCallfec54012009-08-03 20:12:06 +00002095 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec,
2096 DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002097 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002098 case tok::kw_bool:
2099 case tok::kw__Bool:
Argyrios Kyrtzidis4383e182010-11-16 18:18:13 +00002100 if (Tok.is(tok::kw_bool) &&
2101 DS.getTypeSpecType() != DeclSpec::TST_unspecified &&
2102 DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
2103 PrevSpec = ""; // Not used by the diagnostic.
2104 DiagID = diag::err_bool_redeclaration;
Fariborz Jahaniane106a0b2011-04-19 21:42:37 +00002105 // For better error recovery.
2106 Tok.setKind(tok::identifier);
Argyrios Kyrtzidis4383e182010-11-16 18:18:13 +00002107 isInvalid = true;
2108 } else {
2109 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec,
2110 DiagID);
2111 }
Chris Lattner80d0c892009-01-21 19:48:37 +00002112 break;
2113 case tok::kw__Decimal32:
John McCallfec54012009-08-03 20:12:06 +00002114 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
2115 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002116 break;
2117 case tok::kw__Decimal64:
John McCallfec54012009-08-03 20:12:06 +00002118 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
2119 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002120 break;
2121 case tok::kw__Decimal128:
John McCallfec54012009-08-03 20:12:06 +00002122 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
2123 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002124 break;
John Thompson82287d12010-02-05 00:12:22 +00002125 case tok::kw___vector:
2126 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
2127 break;
2128 case tok::kw___pixel:
2129 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
2130 break;
John McCalla5fc4722011-04-09 22:50:59 +00002131 case tok::kw___unknown_anytype:
2132 isInvalid = DS.SetTypeSpecType(TST_unknown_anytype, Loc,
2133 PrevSpec, DiagID);
2134 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002135
2136 // class-specifier:
2137 case tok::kw_class:
2138 case tok::kw_struct:
Chris Lattner4c97d762009-04-12 21:49:30 +00002139 case tok::kw_union: {
2140 tok::TokenKind Kind = Tok.getKind();
2141 ConsumeToken();
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00002142 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS);
Chris Lattner80d0c892009-01-21 19:48:37 +00002143 continue;
Chris Lattner4c97d762009-04-12 21:49:30 +00002144 }
Chris Lattner80d0c892009-01-21 19:48:37 +00002145
2146 // enum-specifier:
2147 case tok::kw_enum:
Chris Lattner4c97d762009-04-12 21:49:30 +00002148 ConsumeToken();
Douglas Gregor9b9edd62010-03-02 17:53:14 +00002149 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS);
Chris Lattner80d0c892009-01-21 19:48:37 +00002150 continue;
2151
2152 // cv-qualifier:
2153 case tok::kw_const:
John McCallfec54012009-08-03 20:12:06 +00002154 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec, DiagID,
2155 getLang());
Chris Lattner80d0c892009-01-21 19:48:37 +00002156 break;
2157 case tok::kw_volatile:
John McCallfec54012009-08-03 20:12:06 +00002158 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
2159 getLang());
Chris Lattner80d0c892009-01-21 19:48:37 +00002160 break;
2161 case tok::kw_restrict:
John McCallfec54012009-08-03 20:12:06 +00002162 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
2163 getLang());
Chris Lattner80d0c892009-01-21 19:48:37 +00002164 break;
2165
Douglas Gregord57959a2009-03-27 23:10:48 +00002166 // C++ typename-specifier:
2167 case tok::kw_typename:
John McCall9ba61662010-02-26 08:45:28 +00002168 if (TryAnnotateTypeOrScopeToken()) {
2169 DS.SetTypeSpecError();
2170 goto DoneWithDeclSpec;
2171 }
2172 if (!Tok.is(tok::kw_typename))
Douglas Gregord57959a2009-03-27 23:10:48 +00002173 continue;
2174 break;
2175
Chris Lattner80d0c892009-01-21 19:48:37 +00002176 // GNU typeof support.
2177 case tok::kw_typeof:
2178 ParseTypeofSpecifier(DS);
2179 continue;
2180
Anders Carlsson6fd634f2009-06-24 17:47:40 +00002181 case tok::kw_decltype:
2182 ParseDecltypeSpecifier(DS);
2183 continue;
2184
Sean Huntdb5d44b2011-05-19 05:37:45 +00002185 case tok::kw___underlying_type:
2186 ParseUnderlyingTypeSpecifier(DS);
Eli Friedmanb001de72011-10-06 23:00:33 +00002187 continue;
2188
2189 case tok::kw__Atomic:
2190 ParseAtomicSpecifier(DS);
2191 continue;
Sean Huntdb5d44b2011-05-19 05:37:45 +00002192
Peter Collingbourne207f4d82011-03-18 22:38:29 +00002193 // OpenCL qualifiers:
2194 case tok::kw_private:
2195 if (!getLang().OpenCL)
2196 goto DoneWithDeclSpec;
2197 case tok::kw___private:
2198 case tok::kw___global:
2199 case tok::kw___local:
2200 case tok::kw___constant:
2201 case tok::kw___read_only:
2202 case tok::kw___write_only:
2203 case tok::kw___read_write:
2204 ParseOpenCLQualifiers(DS);
2205 break;
2206
Steve Naroffd3ded1f2008-06-05 00:02:44 +00002207 case tok::less:
Chris Lattner3bd934a2008-07-26 01:18:38 +00002208 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattnerbce61352008-07-26 00:20:22 +00002209 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
2210 // but we support it.
Chris Lattner3bd934a2008-07-26 01:18:38 +00002211 if (DS.hasTypeSpecifier() || !getLang().ObjC1)
Chris Lattnerbce61352008-07-26 00:20:22 +00002212 goto DoneWithDeclSpec;
Mike Stump1eb44332009-09-09 15:08:12 +00002213
Douglas Gregor46f936e2010-11-19 17:10:50 +00002214 if (!ParseObjCProtocolQualifiers(DS))
2215 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
2216 << FixItHint::CreateInsertion(Loc, "id")
2217 << SourceRange(Loc, DS.getSourceRange().getEnd());
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00002218
2219 // Need to support trailing type qualifiers (e.g. "id<p> const").
2220 // If a type specifier follows, it will be diagnosed elsewhere.
2221 continue;
Reid Spencer5f016e22007-07-11 17:01:13 +00002222 }
John McCallfec54012009-08-03 20:12:06 +00002223 // If the specifier wasn't legal, issue a diagnostic.
Reid Spencer5f016e22007-07-11 17:01:13 +00002224 if (isInvalid) {
2225 assert(PrevSpec && "Method did not return previous specifier!");
John McCallfec54012009-08-03 20:12:06 +00002226 assert(DiagID);
Douglas Gregorae2fb142010-08-23 14:34:43 +00002227
2228 if (DiagID == diag::ext_duplicate_declspec)
2229 Diag(Tok, DiagID)
2230 << PrevSpec << FixItHint::CreateRemoval(Tok.getLocation());
2231 else
2232 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00002233 }
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002234
Chris Lattner81c018d2008-03-13 06:29:04 +00002235 DS.SetRangeEnd(Tok.getLocation());
Fariborz Jahaniane106a0b2011-04-19 21:42:37 +00002236 if (DiagID != diag::err_bool_redeclaration)
2237 ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00002238 }
2239}
Douglas Gregoradcac882008-12-01 23:54:00 +00002240
Chris Lattner7a0ab5f2009-01-06 06:59:53 +00002241/// ParseOptionalTypeSpecifier - Try to parse a single type-specifier. We
Douglas Gregor12e083c2008-11-07 15:42:26 +00002242/// primarily follow the C++ grammar with additions for C99 and GNU,
2243/// which together subsume the C grammar. Note that the C++
2244/// type-specifier also includes the C type-qualifier (for const,
2245/// volatile, and C99 restrict). Returns true if a type-specifier was
2246/// found (and parsed), false otherwise.
2247///
2248/// type-specifier: [C++ 7.1.5]
2249/// simple-type-specifier
2250/// class-specifier
2251/// enum-specifier
2252/// elaborated-type-specifier [TODO]
2253/// cv-qualifier
2254///
2255/// cv-qualifier: [C++ 7.1.5.1]
2256/// 'const'
2257/// 'volatile'
2258/// [C99] 'restrict'
2259///
2260/// simple-type-specifier: [ C++ 7.1.5.2]
2261/// '::'[opt] nested-name-specifier[opt] type-name [TODO]
2262/// '::'[opt] nested-name-specifier 'template' template-id [TODO]
2263/// 'char'
2264/// 'wchar_t'
2265/// 'bool'
2266/// 'short'
2267/// 'int'
2268/// 'long'
2269/// 'signed'
2270/// 'unsigned'
2271/// 'float'
2272/// 'double'
2273/// 'void'
2274/// [C99] '_Bool'
2275/// [C99] '_Complex'
2276/// [C99] '_Imaginary' // Removed in TC2?
2277/// [GNU] '_Decimal32'
2278/// [GNU] '_Decimal64'
2279/// [GNU] '_Decimal128'
2280/// [GNU] typeof-specifier
2281/// [OBJC] class-name objc-protocol-refs[opt] [TODO]
2282/// [OBJC] typedef-name objc-protocol-refs[opt] [TODO]
Anders Carlsson6fd634f2009-06-24 17:47:40 +00002283/// [C++0x] 'decltype' ( expression )
John Thompson82287d12010-02-05 00:12:22 +00002284/// [AltiVec] '__vector'
John McCallfec54012009-08-03 20:12:06 +00002285bool Parser::ParseOptionalTypeSpecifier(DeclSpec &DS, bool& isInvalid,
Chris Lattner7a0ab5f2009-01-06 06:59:53 +00002286 const char *&PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00002287 unsigned &DiagID,
Sebastian Redld9bafa72010-02-03 21:21:43 +00002288 const ParsedTemplateInfo &TemplateInfo,
2289 bool SuppressDeclarations) {
Douglas Gregor12e083c2008-11-07 15:42:26 +00002290 SourceLocation Loc = Tok.getLocation();
2291
2292 switch (Tok.getKind()) {
Chris Lattner166a8fc2009-01-04 23:41:41 +00002293 case tok::identifier: // foo::bar
Douglas Gregorc0b39642010-04-15 23:40:53 +00002294 // If we already have a type specifier, this identifier is not a type.
2295 if (DS.getTypeSpecType() != DeclSpec::TST_unspecified ||
2296 DS.getTypeSpecWidth() != DeclSpec::TSW_unspecified ||
2297 DS.getTypeSpecSign() != DeclSpec::TSS_unspecified)
2298 return false;
John Thompson82287d12010-02-05 00:12:22 +00002299 // Check for need to substitute AltiVec keyword tokens.
2300 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
2301 break;
2302 // Fall through.
Douglas Gregord57959a2009-03-27 23:10:48 +00002303 case tok::kw_typename: // typename foo::bar
Chris Lattner166a8fc2009-01-04 23:41:41 +00002304 // Annotate typenames and C++ scope specifiers. If we get one, just
2305 // recurse to handle whatever we get.
Kaelyn Uhrainfac94672011-10-11 01:02:41 +00002306 if (TryAnnotateTypeOrScopeToken(/*EnteringContext=*/false,
2307 /*NeedType=*/true))
John McCall9ba61662010-02-26 08:45:28 +00002308 return true;
2309 if (Tok.is(tok::identifier))
2310 return false;
2311 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
2312 TemplateInfo, SuppressDeclarations);
Chris Lattner166a8fc2009-01-04 23:41:41 +00002313 case tok::coloncolon: // ::foo::bar
2314 if (NextToken().is(tok::kw_new) || // ::new
2315 NextToken().is(tok::kw_delete)) // ::delete
2316 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002317
Chris Lattner166a8fc2009-01-04 23:41:41 +00002318 // Annotate typenames and C++ scope specifiers. If we get one, just
2319 // recurse to handle whatever we get.
Kaelyn Uhrainfac94672011-10-11 01:02:41 +00002320 if (TryAnnotateTypeOrScopeToken(/*EnteringContext=*/false,
2321 /*NeedType=*/true))
John McCall9ba61662010-02-26 08:45:28 +00002322 return true;
2323 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
2324 TemplateInfo, SuppressDeclarations);
Mike Stump1eb44332009-09-09 15:08:12 +00002325
Douglas Gregor12e083c2008-11-07 15:42:26 +00002326 // simple-type-specifier:
Chris Lattnerb31757b2009-01-06 05:06:21 +00002327 case tok::annot_typename: {
John McCallb3d87482010-08-24 05:47:05 +00002328 if (ParsedType T = getTypeAnnotation(Tok)) {
Nico Weber253e80b2010-11-22 10:30:56 +00002329 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename,
2330 Tok.getAnnotationEndLoc(), PrevSpec,
John McCallb3d87482010-08-24 05:47:05 +00002331 DiagID, T);
2332 } else
Douglas Gregor31a19b62009-04-01 21:51:26 +00002333 DS.SetTypeSpecError();
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002334 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
2335 ConsumeToken(); // The typename
Mike Stump1eb44332009-09-09 15:08:12 +00002336
Douglas Gregor12e083c2008-11-07 15:42:26 +00002337 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
2338 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
2339 // Objective-C interface. If we don't have Objective-C or a '<', this is
2340 // just a normal reference to a typedef name.
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00002341 if (Tok.is(tok::less) && getLang().ObjC1)
2342 ParseObjCProtocolQualifiers(DS);
2343
Douglas Gregor12e083c2008-11-07 15:42:26 +00002344 return true;
2345 }
2346
2347 case tok::kw_short:
John McCallfec54012009-08-03 20:12:06 +00002348 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002349 break;
2350 case tok::kw_long:
2351 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCallfec54012009-08-03 20:12:06 +00002352 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
2353 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002354 else
John McCallfec54012009-08-03 20:12:06 +00002355 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2356 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002357 break;
Francois Pichet338d7f72011-04-28 01:59:37 +00002358 case tok::kw___int64:
2359 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2360 DiagID);
2361 break;
Douglas Gregor12e083c2008-11-07 15:42:26 +00002362 case tok::kw_signed:
John McCallfec54012009-08-03 20:12:06 +00002363 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002364 break;
2365 case tok::kw_unsigned:
John McCallfec54012009-08-03 20:12:06 +00002366 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
2367 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002368 break;
2369 case tok::kw__Complex:
John McCallfec54012009-08-03 20:12:06 +00002370 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
2371 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002372 break;
2373 case tok::kw__Imaginary:
John McCallfec54012009-08-03 20:12:06 +00002374 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
2375 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002376 break;
2377 case tok::kw_void:
John McCallfec54012009-08-03 20:12:06 +00002378 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002379 break;
2380 case tok::kw_char:
John McCallfec54012009-08-03 20:12:06 +00002381 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002382 break;
2383 case tok::kw_int:
John McCallfec54012009-08-03 20:12:06 +00002384 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002385 break;
2386 case tok::kw_float:
John McCallfec54012009-08-03 20:12:06 +00002387 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002388 break;
2389 case tok::kw_double:
John McCallfec54012009-08-03 20:12:06 +00002390 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002391 break;
2392 case tok::kw_wchar_t:
John McCallfec54012009-08-03 20:12:06 +00002393 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002394 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002395 case tok::kw_char16_t:
John McCallfec54012009-08-03 20:12:06 +00002396 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002397 break;
2398 case tok::kw_char32_t:
John McCallfec54012009-08-03 20:12:06 +00002399 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002400 break;
Douglas Gregor12e083c2008-11-07 15:42:26 +00002401 case tok::kw_bool:
2402 case tok::kw__Bool:
John McCallfec54012009-08-03 20:12:06 +00002403 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002404 break;
2405 case tok::kw__Decimal32:
John McCallfec54012009-08-03 20:12:06 +00002406 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
2407 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002408 break;
2409 case tok::kw__Decimal64:
John McCallfec54012009-08-03 20:12:06 +00002410 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
2411 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002412 break;
2413 case tok::kw__Decimal128:
John McCallfec54012009-08-03 20:12:06 +00002414 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
2415 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002416 break;
John Thompson82287d12010-02-05 00:12:22 +00002417 case tok::kw___vector:
2418 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
2419 break;
2420 case tok::kw___pixel:
2421 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
2422 break;
2423
Douglas Gregor12e083c2008-11-07 15:42:26 +00002424 // class-specifier:
2425 case tok::kw_class:
2426 case tok::kw_struct:
Chris Lattner4c97d762009-04-12 21:49:30 +00002427 case tok::kw_union: {
2428 tok::TokenKind Kind = Tok.getKind();
2429 ConsumeToken();
Sebastian Redld9bafa72010-02-03 21:21:43 +00002430 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS_none,
2431 SuppressDeclarations);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002432 return true;
Chris Lattner4c97d762009-04-12 21:49:30 +00002433 }
Douglas Gregor12e083c2008-11-07 15:42:26 +00002434
2435 // enum-specifier:
2436 case tok::kw_enum:
Chris Lattner4c97d762009-04-12 21:49:30 +00002437 ConsumeToken();
Douglas Gregor9b9edd62010-03-02 17:53:14 +00002438 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS_none);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002439 return true;
2440
2441 // cv-qualifier:
2442 case tok::kw_const:
2443 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00002444 DiagID, getLang());
Douglas Gregor12e083c2008-11-07 15:42:26 +00002445 break;
2446 case tok::kw_volatile:
2447 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00002448 DiagID, getLang());
Douglas Gregor12e083c2008-11-07 15:42:26 +00002449 break;
2450 case tok::kw_restrict:
2451 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00002452 DiagID, getLang());
Douglas Gregor12e083c2008-11-07 15:42:26 +00002453 break;
2454
2455 // GNU typeof support.
2456 case tok::kw_typeof:
2457 ParseTypeofSpecifier(DS);
2458 return true;
2459
Anders Carlsson6fd634f2009-06-24 17:47:40 +00002460 // C++0x decltype support.
2461 case tok::kw_decltype:
2462 ParseDecltypeSpecifier(DS);
2463 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00002464
Sean Huntdb5d44b2011-05-19 05:37:45 +00002465 // C++0x type traits support.
2466 case tok::kw___underlying_type:
2467 ParseUnderlyingTypeSpecifier(DS);
2468 return true;
2469
Eli Friedmanb001de72011-10-06 23:00:33 +00002470 case tok::kw__Atomic:
2471 ParseAtomicSpecifier(DS);
2472 return true;
2473
Peter Collingbourne207f4d82011-03-18 22:38:29 +00002474 // OpenCL qualifiers:
2475 case tok::kw_private:
2476 if (!getLang().OpenCL)
2477 return false;
2478 case tok::kw___private:
2479 case tok::kw___global:
2480 case tok::kw___local:
2481 case tok::kw___constant:
2482 case tok::kw___read_only:
2483 case tok::kw___write_only:
2484 case tok::kw___read_write:
2485 ParseOpenCLQualifiers(DS);
2486 break;
2487
Anders Carlsson0b7f7892009-06-26 23:44:14 +00002488 // C++0x auto support.
2489 case tok::kw_auto:
Richard Smith87e96eb2011-09-04 20:24:20 +00002490 // This is only called in situations where a storage-class specifier is
2491 // illegal, so we can assume an auto type specifier was intended even in
2492 // C++98. In C++98 mode, DeclSpec::Finish will produce an appropriate
2493 // extension diagnostic.
2494 if (!getLang().CPlusPlus)
Anders Carlsson0b7f7892009-06-26 23:44:14 +00002495 return false;
2496
John McCallfec54012009-08-03 20:12:06 +00002497 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec, DiagID);
Anders Carlsson0b7f7892009-06-26 23:44:14 +00002498 break;
Dawn Perchik52fc3142010-09-03 01:29:35 +00002499
Eli Friedman290eeb02009-06-08 23:27:34 +00002500 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00002501 case tok::kw___ptr32:
Eli Friedman290eeb02009-06-08 23:27:34 +00002502 case tok::kw___w64:
Steve Naroff239f0732008-12-25 14:16:32 +00002503 case tok::kw___cdecl:
2504 case tok::kw___stdcall:
2505 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00002506 case tok::kw___thiscall:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00002507 case tok::kw___unaligned:
John McCall7f040a92010-12-24 02:08:15 +00002508 ParseMicrosoftTypeAttributes(DS.getAttributes());
Chris Lattner837acd02009-01-21 19:19:26 +00002509 return true;
Steve Naroff239f0732008-12-25 14:16:32 +00002510
Dawn Perchik52fc3142010-09-03 01:29:35 +00002511 case tok::kw___pascal:
John McCall7f040a92010-12-24 02:08:15 +00002512 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik52fc3142010-09-03 01:29:35 +00002513 return true;
2514
Douglas Gregor12e083c2008-11-07 15:42:26 +00002515 default:
2516 // Not a type-specifier; do nothing.
2517 return false;
2518 }
2519
2520 // If the specifier combination wasn't legal, issue a diagnostic.
2521 if (isInvalid) {
2522 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner1ab3b962008-11-18 07:48:38 +00002523 // Pick between error or extwarn.
Chris Lattner1ab3b962008-11-18 07:48:38 +00002524 Diag(Tok, DiagID) << PrevSpec;
Douglas Gregor12e083c2008-11-07 15:42:26 +00002525 }
2526 DS.SetRangeEnd(Tok.getLocation());
2527 ConsumeToken(); // whatever we parsed above.
2528 return true;
2529}
Reid Spencer5f016e22007-07-11 17:01:13 +00002530
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002531/// ParseStructDeclaration - Parse a struct declaration without the terminating
2532/// semicolon.
2533///
Reid Spencer5f016e22007-07-11 17:01:13 +00002534/// struct-declaration:
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002535/// specifier-qualifier-list struct-declarator-list
Reid Spencer5f016e22007-07-11 17:01:13 +00002536/// [GNU] __extension__ struct-declaration
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002537/// [GNU] specifier-qualifier-list
Reid Spencer5f016e22007-07-11 17:01:13 +00002538/// struct-declarator-list:
2539/// struct-declarator
2540/// struct-declarator-list ',' struct-declarator
2541/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
2542/// struct-declarator:
2543/// declarator
2544/// [GNU] declarator attributes[opt]
2545/// declarator[opt] ':' constant-expression
2546/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
2547///
Chris Lattnere1359422008-04-10 06:46:29 +00002548void Parser::
John McCallbdd563e2009-11-03 02:38:08 +00002549ParseStructDeclaration(DeclSpec &DS, FieldCallback &Fields) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002550
Chris Lattnerc46d1a12008-10-20 06:45:43 +00002551 if (Tok.is(tok::kw___extension__)) {
2552 // __extension__ silences extension warnings in the subexpression.
2553 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroff28a7ca82007-08-20 22:28:22 +00002554 ConsumeToken();
Chris Lattnerc46d1a12008-10-20 06:45:43 +00002555 return ParseStructDeclaration(DS, Fields);
2556 }
Mike Stump1eb44332009-09-09 15:08:12 +00002557
Steve Naroff28a7ca82007-08-20 22:28:22 +00002558 // Parse the common specifier-qualifiers-list piece.
Steve Naroff28a7ca82007-08-20 22:28:22 +00002559 ParseSpecifierQualifierList(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00002560
Douglas Gregor4920f1f2009-01-12 22:49:06 +00002561 // If there are no declarators, this is a free-standing declaration
2562 // specifier. Let the actions module cope with it.
Chris Lattner04d66662007-10-09 17:33:22 +00002563 if (Tok.is(tok::semi)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00002564 Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none, DS);
Steve Naroff28a7ca82007-08-20 22:28:22 +00002565 return;
2566 }
2567
2568 // Read struct-declarators until we find the semicolon.
John McCallbdd563e2009-11-03 02:38:08 +00002569 bool FirstDeclarator = true;
Steve Naroff28a7ca82007-08-20 22:28:22 +00002570 while (1) {
John McCall54abf7d2009-11-04 02:18:39 +00002571 ParsingDeclRAIIObject PD(*this);
John McCallbdd563e2009-11-03 02:38:08 +00002572 FieldDeclarator DeclaratorInfo(DS);
2573
2574 // Attributes are only allowed here on successive declarators.
John McCall7f040a92010-12-24 02:08:15 +00002575 if (!FirstDeclarator)
2576 MaybeParseGNUAttributes(DeclaratorInfo.D);
Mike Stump1eb44332009-09-09 15:08:12 +00002577
Steve Naroff28a7ca82007-08-20 22:28:22 +00002578 /// struct-declarator: declarator
2579 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattnera1efc8c2009-12-10 01:59:24 +00002580 if (Tok.isNot(tok::colon)) {
2581 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
2582 ColonProtectionRAIIObject X(*this);
Chris Lattnere1359422008-04-10 06:46:29 +00002583 ParseDeclarator(DeclaratorInfo.D);
Chris Lattnera1efc8c2009-12-10 01:59:24 +00002584 }
Mike Stump1eb44332009-09-09 15:08:12 +00002585
Chris Lattner04d66662007-10-09 17:33:22 +00002586 if (Tok.is(tok::colon)) {
Steve Naroff28a7ca82007-08-20 22:28:22 +00002587 ConsumeToken();
John McCall60d7b3a2010-08-24 06:29:42 +00002588 ExprResult Res(ParseConstantExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002589 if (Res.isInvalid())
Steve Naroff28a7ca82007-08-20 22:28:22 +00002590 SkipUntil(tok::semi, true, true);
Chris Lattner60b1e3e2008-04-10 06:15:14 +00002591 else
Sebastian Redleffa8d12008-12-10 00:02:53 +00002592 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroff28a7ca82007-08-20 22:28:22 +00002593 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00002594
Steve Naroff28a7ca82007-08-20 22:28:22 +00002595 // If attributes exist after the declarator, parse them.
John McCall7f040a92010-12-24 02:08:15 +00002596 MaybeParseGNUAttributes(DeclaratorInfo.D);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002597
John McCallbdd563e2009-11-03 02:38:08 +00002598 // We're done with this declarator; invoke the callback.
John McCalld226f652010-08-21 09:40:31 +00002599 Decl *D = Fields.invoke(DeclaratorInfo);
John McCall54abf7d2009-11-04 02:18:39 +00002600 PD.complete(D);
John McCallbdd563e2009-11-03 02:38:08 +00002601
Steve Naroff28a7ca82007-08-20 22:28:22 +00002602 // If we don't have a comma, it is either the end of the list (a ';')
2603 // or an error, bail out.
Chris Lattner04d66662007-10-09 17:33:22 +00002604 if (Tok.isNot(tok::comma))
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002605 return;
Sebastian Redlab197ba2009-02-09 18:23:29 +00002606
Steve Naroff28a7ca82007-08-20 22:28:22 +00002607 // Consume the comma.
2608 ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00002609
John McCallbdd563e2009-11-03 02:38:08 +00002610 FirstDeclarator = false;
Steve Naroff28a7ca82007-08-20 22:28:22 +00002611 }
Steve Naroff28a7ca82007-08-20 22:28:22 +00002612}
2613
2614/// ParseStructUnionBody
2615/// struct-contents:
2616/// struct-declaration-list
2617/// [EXT] empty
2618/// [GNU] "struct-declaration-list" without terminatoring ';'
2619/// struct-declaration-list:
2620/// struct-declaration
2621/// struct-declaration-list struct-declaration
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002622/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroff28a7ca82007-08-20 22:28:22 +00002623///
Reid Spencer5f016e22007-07-11 17:01:13 +00002624void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
John McCalld226f652010-08-21 09:40:31 +00002625 unsigned TagType, Decl *TagDecl) {
John McCallf312b1e2010-08-26 23:41:50 +00002626 PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc,
2627 "parsing struct/union body");
Mike Stump1eb44332009-09-09 15:08:12 +00002628
Reid Spencer5f016e22007-07-11 17:01:13 +00002629 SourceLocation LBraceLoc = ConsumeBrace();
Mike Stump1eb44332009-09-09 15:08:12 +00002630
Douglas Gregor3218c4b2009-01-09 22:42:13 +00002631 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
Douglas Gregor23c94db2010-07-02 17:43:08 +00002632 Actions.ActOnTagStartDefinition(getCurScope(), TagDecl);
Douglas Gregor72de6672009-01-08 20:45:30 +00002633
Reid Spencer5f016e22007-07-11 17:01:13 +00002634 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
2635 // C++.
Douglas Gregore37ac4f2008-04-13 21:30:24 +00002636 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Douglas Gregor03332962010-07-29 14:29:34 +00002637 Diag(Tok, diag::ext_empty_struct_union)
2638 << (TagType == TST_union);
Reid Spencer5f016e22007-07-11 17:01:13 +00002639
Chris Lattner5f9e2722011-07-23 10:55:15 +00002640 SmallVector<Decl *, 32> FieldDecls;
Chris Lattnere1359422008-04-10 06:46:29 +00002641
Reid Spencer5f016e22007-07-11 17:01:13 +00002642 // While we still have something to read, read the declarations in the struct.
Chris Lattner04d66662007-10-09 17:33:22 +00002643 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002644 // Each iteration of this loop reads one struct-declaration.
Mike Stump1eb44332009-09-09 15:08:12 +00002645
Reid Spencer5f016e22007-07-11 17:01:13 +00002646 // Check for extraneous top-level semicolon.
Chris Lattner04d66662007-10-09 17:33:22 +00002647 if (Tok.is(tok::semi)) {
Douglas Gregor9b3064b2009-04-01 22:41:11 +00002648 Diag(Tok, diag::ext_extra_struct_semi)
Douglas Gregorf13ca062010-06-16 23:08:59 +00002649 << DeclSpec::getSpecifierName((DeclSpec::TST)TagType)
Douglas Gregor849b2432010-03-31 17:46:05 +00002650 << FixItHint::CreateRemoval(Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00002651 ConsumeToken();
2652 continue;
2653 }
Chris Lattnere1359422008-04-10 06:46:29 +00002654
2655 // Parse all the comma separated declarators.
John McCall0b7e6782011-03-24 11:26:52 +00002656 DeclSpec DS(AttrFactory);
Mike Stump1eb44332009-09-09 15:08:12 +00002657
John McCallbdd563e2009-11-03 02:38:08 +00002658 if (!Tok.is(tok::at)) {
2659 struct CFieldCallback : FieldCallback {
2660 Parser &P;
John McCalld226f652010-08-21 09:40:31 +00002661 Decl *TagDecl;
Chris Lattner5f9e2722011-07-23 10:55:15 +00002662 SmallVectorImpl<Decl *> &FieldDecls;
John McCallbdd563e2009-11-03 02:38:08 +00002663
John McCalld226f652010-08-21 09:40:31 +00002664 CFieldCallback(Parser &P, Decl *TagDecl,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002665 SmallVectorImpl<Decl *> &FieldDecls) :
John McCallbdd563e2009-11-03 02:38:08 +00002666 P(P), TagDecl(TagDecl), FieldDecls(FieldDecls) {}
2667
John McCalld226f652010-08-21 09:40:31 +00002668 virtual Decl *invoke(FieldDeclarator &FD) {
John McCallbdd563e2009-11-03 02:38:08 +00002669 // Install the declarator into the current TagDecl.
John McCalld226f652010-08-21 09:40:31 +00002670 Decl *Field = P.Actions.ActOnField(P.getCurScope(), TagDecl,
John McCall4ba39712009-11-03 21:13:47 +00002671 FD.D.getDeclSpec().getSourceRange().getBegin(),
2672 FD.D, FD.BitfieldSize);
John McCallbdd563e2009-11-03 02:38:08 +00002673 FieldDecls.push_back(Field);
2674 return Field;
Douglas Gregor91a28862009-08-26 14:27:30 +00002675 }
John McCallbdd563e2009-11-03 02:38:08 +00002676 } Callback(*this, TagDecl, FieldDecls);
2677
2678 ParseStructDeclaration(DS, Callback);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002679 } else { // Handle @defs
2680 ConsumeToken();
2681 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
2682 Diag(Tok, diag::err_unexpected_at);
Chris Lattner3e156ad2010-02-02 00:37:27 +00002683 SkipUntil(tok::semi, true);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002684 continue;
2685 }
2686 ConsumeToken();
2687 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
2688 if (!Tok.is(tok::identifier)) {
2689 Diag(Tok, diag::err_expected_ident);
Chris Lattner3e156ad2010-02-02 00:37:27 +00002690 SkipUntil(tok::semi, true);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002691 continue;
2692 }
Chris Lattner5f9e2722011-07-23 10:55:15 +00002693 SmallVector<Decl *, 16> Fields;
Douglas Gregor23c94db2010-07-02 17:43:08 +00002694 Actions.ActOnDefs(getCurScope(), TagDecl, Tok.getLocation(),
Douglas Gregor44b43212008-12-11 16:49:14 +00002695 Tok.getIdentifierInfo(), Fields);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002696 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
2697 ConsumeToken();
2698 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
Mike Stump1eb44332009-09-09 15:08:12 +00002699 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002700
Chris Lattner04d66662007-10-09 17:33:22 +00002701 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002702 ConsumeToken();
Chris Lattner04d66662007-10-09 17:33:22 +00002703 } else if (Tok.is(tok::r_brace)) {
Chris Lattner3e156ad2010-02-02 00:37:27 +00002704 ExpectAndConsume(tok::semi, diag::ext_expected_semi_decl_list);
Reid Spencer5f016e22007-07-11 17:01:13 +00002705 break;
2706 } else {
Chris Lattner3e156ad2010-02-02 00:37:27 +00002707 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
2708 // Skip to end of block or statement to avoid ext-warning on extra ';'.
Reid Spencer5f016e22007-07-11 17:01:13 +00002709 SkipUntil(tok::r_brace, true, true);
Chris Lattner3e156ad2010-02-02 00:37:27 +00002710 // If we stopped at a ';', eat it.
2711 if (Tok.is(tok::semi)) ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00002712 }
2713 }
Mike Stump1eb44332009-09-09 15:08:12 +00002714
Steve Naroff60fccee2007-10-29 21:38:07 +00002715 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00002716
John McCall0b7e6782011-03-24 11:26:52 +00002717 ParsedAttributes attrs(AttrFactory);
Reid Spencer5f016e22007-07-11 17:01:13 +00002718 // If attributes exist after struct contents, parse them.
John McCall7f040a92010-12-24 02:08:15 +00002719 MaybeParseGNUAttributes(attrs);
Daniel Dunbar1bfe1c22008-10-03 02:03:53 +00002720
Douglas Gregor23c94db2010-07-02 17:43:08 +00002721 Actions.ActOnFields(getCurScope(),
David Blaikie77b6de02011-09-22 02:58:26 +00002722 RecordLoc, TagDecl, FieldDecls,
Daniel Dunbar1bfe1c22008-10-03 02:03:53 +00002723 LBraceLoc, RBraceLoc,
John McCall7f040a92010-12-24 02:08:15 +00002724 attrs.getList());
Douglas Gregor72de6672009-01-08 20:45:30 +00002725 StructScope.Exit();
Douglas Gregor23c94db2010-07-02 17:43:08 +00002726 Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl, RBraceLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00002727}
2728
Reid Spencer5f016e22007-07-11 17:01:13 +00002729/// ParseEnumSpecifier
2730/// enum-specifier: [C99 6.7.2.2]
2731/// 'enum' identifier[opt] '{' enumerator-list '}'
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002732///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Reid Spencer5f016e22007-07-11 17:01:13 +00002733/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
2734/// '}' attributes[opt]
2735/// 'enum' identifier
2736/// [GNU] 'enum' attributes[opt] identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002737///
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002738/// [C++0x] enum-head '{' enumerator-list[opt] '}'
2739/// [C++0x] enum-head '{' enumerator-list ',' '}'
2740///
2741/// enum-head: [C++0x]
2742/// enum-key attributes[opt] identifier[opt] enum-base[opt]
2743/// enum-key attributes[opt] nested-name-specifier identifier enum-base[opt]
2744///
2745/// enum-key: [C++0x]
2746/// 'enum'
2747/// 'enum' 'class'
2748/// 'enum' 'struct'
2749///
2750/// enum-base: [C++0x]
2751/// ':' type-specifier-seq
2752///
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002753/// [C++] elaborated-type-specifier:
2754/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
2755///
Chris Lattner4c97d762009-04-12 21:49:30 +00002756void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregor9b9edd62010-03-02 17:53:14 +00002757 const ParsedTemplateInfo &TemplateInfo,
Chris Lattner4c97d762009-04-12 21:49:30 +00002758 AccessSpecifier AS) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002759 // Parse the tag portion of this.
Douglas Gregor374929f2009-09-18 15:37:17 +00002760 if (Tok.is(tok::code_completion)) {
2761 // Code completion for an enum name.
Douglas Gregor23c94db2010-07-02 17:43:08 +00002762 Actions.CodeCompleteTag(getCurScope(), DeclSpec::TST_enum);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002763 return cutOffParsing();
Douglas Gregor374929f2009-09-18 15:37:17 +00002764 }
John McCall57c13002011-07-06 05:58:41 +00002765
2766 bool IsScopedEnum = false;
2767 bool IsScopedUsingClassTag = false;
2768
2769 if (getLang().CPlusPlus0x &&
2770 (Tok.is(tok::kw_class) || Tok.is(tok::kw_struct))) {
2771 IsScopedEnum = true;
2772 IsScopedUsingClassTag = Tok.is(tok::kw_class);
2773 ConsumeToken();
2774 }
Douglas Gregor374929f2009-09-18 15:37:17 +00002775
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002776 // If attributes exist after tag, parse them.
John McCall0b7e6782011-03-24 11:26:52 +00002777 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00002778 MaybeParseGNUAttributes(attrs);
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002779
Douglas Gregor5471bc82011-09-08 17:18:35 +00002780 bool AllowFixedUnderlyingType
Francois Pichet62ec1f22011-09-17 17:15:52 +00002781 = getLang().CPlusPlus0x || getLang().MicrosoftExt || getLang().ObjC2;
John McCall57c13002011-07-06 05:58:41 +00002782
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00002783 CXXScopeSpec &SS = DS.getTypeSpecScope();
John McCall9ba61662010-02-26 08:45:28 +00002784 if (getLang().CPlusPlus) {
John McCall57c13002011-07-06 05:58:41 +00002785 // "enum foo : bar;" is not a potential typo for "enum foo::bar;"
2786 // if a fixed underlying type is allowed.
2787 ColonProtectionRAIIObject X(*this, AllowFixedUnderlyingType);
2788
John McCallb3d87482010-08-24 05:47:05 +00002789 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), false))
John McCall9ba61662010-02-26 08:45:28 +00002790 return;
2791
2792 if (SS.isSet() && Tok.isNot(tok::identifier)) {
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002793 Diag(Tok, diag::err_expected_ident);
2794 if (Tok.isNot(tok::l_brace)) {
2795 // Has no name and is not a definition.
2796 // Skip the rest of this declarator, up until the comma or semicolon.
2797 SkipUntil(tok::comma, true);
2798 return;
2799 }
2800 }
2801 }
Mike Stump1eb44332009-09-09 15:08:12 +00002802
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002803 // Must have either 'enum name' or 'enum {...}'.
Douglas Gregorb9075602011-02-22 02:55:24 +00002804 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace) &&
2805 (AllowFixedUnderlyingType && Tok.isNot(tok::colon))) {
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002806 Diag(Tok, diag::err_expected_ident_lbrace);
Mike Stump1eb44332009-09-09 15:08:12 +00002807
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002808 // Skip the rest of this declarator, up until the comma or semicolon.
2809 SkipUntil(tok::comma, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00002810 return;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002811 }
Mike Stump1eb44332009-09-09 15:08:12 +00002812
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002813 // If an identifier is present, consume and remember it.
2814 IdentifierInfo *Name = 0;
2815 SourceLocation NameLoc;
2816 if (Tok.is(tok::identifier)) {
2817 Name = Tok.getIdentifierInfo();
2818 NameLoc = ConsumeToken();
2819 }
Mike Stump1eb44332009-09-09 15:08:12 +00002820
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002821 if (!Name && IsScopedEnum) {
2822 // C++0x 7.2p2: The optional identifier shall not be omitted in the
2823 // declaration of a scoped enumeration.
2824 Diag(Tok, diag::err_scoped_enum_missing_identifier);
2825 IsScopedEnum = false;
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00002826 IsScopedUsingClassTag = false;
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002827 }
2828
2829 TypeResult BaseType;
2830
Douglas Gregora61b3e72010-12-01 17:42:47 +00002831 // Parse the fixed underlying type.
Douglas Gregorb9075602011-02-22 02:55:24 +00002832 if (AllowFixedUnderlyingType && Tok.is(tok::colon)) {
Douglas Gregora61b3e72010-12-01 17:42:47 +00002833 bool PossibleBitfield = false;
2834 if (getCurScope()->getFlags() & Scope::ClassScope) {
2835 // If we're in class scope, this can either be an enum declaration with
2836 // an underlying type, or a declaration of a bitfield member. We try to
2837 // use a simple disambiguation scheme first to catch the common cases
2838 // (integer literal, sizeof); if it's still ambiguous, we then consider
2839 // anything that's a simple-type-specifier followed by '(' as an
2840 // expression. This suffices because function types are not valid
2841 // underlying types anyway.
2842 TPResult TPR = isExpressionOrTypeSpecifierSimple(NextToken().getKind());
2843 // If the next token starts an expression, we know we're parsing a
2844 // bit-field. This is the common case.
2845 if (TPR == TPResult::True())
2846 PossibleBitfield = true;
2847 // If the next token starts a type-specifier-seq, it may be either a
2848 // a fixed underlying type or the start of a function-style cast in C++;
2849 // lookahead one more token to see if it's obvious that we have a
2850 // fixed underlying type.
2851 else if (TPR == TPResult::False() &&
2852 GetLookAheadToken(2).getKind() == tok::semi) {
2853 // Consume the ':'.
2854 ConsumeToken();
2855 } else {
2856 // We have the start of a type-specifier-seq, so we have to perform
2857 // tentative parsing to determine whether we have an expression or a
2858 // type.
2859 TentativeParsingAction TPA(*this);
2860
2861 // Consume the ':'.
2862 ConsumeToken();
2863
Douglas Gregor86f208c2011-02-22 20:32:04 +00002864 if ((getLang().CPlusPlus &&
2865 isCXXDeclarationSpecifier() != TPResult::True()) ||
2866 (!getLang().CPlusPlus && !isDeclarationSpecifier(true))) {
Douglas Gregora61b3e72010-12-01 17:42:47 +00002867 // We'll parse this as a bitfield later.
2868 PossibleBitfield = true;
2869 TPA.Revert();
2870 } else {
2871 // We have a type-specifier-seq.
2872 TPA.Commit();
2873 }
2874 }
2875 } else {
2876 // Consume the ':'.
2877 ConsumeToken();
2878 }
2879
2880 if (!PossibleBitfield) {
2881 SourceRange Range;
2882 BaseType = ParseTypeName(&Range);
Douglas Gregor86f208c2011-02-22 20:32:04 +00002883
Douglas Gregor5471bc82011-09-08 17:18:35 +00002884 if (!getLang().CPlusPlus0x && !getLang().ObjC2)
Douglas Gregor86f208c2011-02-22 20:32:04 +00002885 Diag(StartLoc, diag::ext_ms_enum_fixed_underlying_type)
2886 << Range;
Douglas Gregora61b3e72010-12-01 17:42:47 +00002887 }
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002888 }
2889
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002890 // There are three options here. If we have 'enum foo;', then this is a
2891 // forward declaration. If we have 'enum foo {...' then this is a
2892 // definition. Otherwise we have something like 'enum foo xyz', a reference.
2893 //
2894 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
2895 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
2896 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
2897 //
John McCallf312b1e2010-08-26 23:41:50 +00002898 Sema::TagUseKind TUK;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002899 if (Tok.is(tok::l_brace))
John McCallf312b1e2010-08-26 23:41:50 +00002900 TUK = Sema::TUK_Definition;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002901 else if (Tok.is(tok::semi))
John McCallf312b1e2010-08-26 23:41:50 +00002902 TUK = Sema::TUK_Declaration;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002903 else
John McCallf312b1e2010-08-26 23:41:50 +00002904 TUK = Sema::TUK_Reference;
Douglas Gregor8fc6d232010-05-03 17:48:54 +00002905
2906 // enums cannot be templates, although they can be referenced from a
2907 // template.
2908 if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate &&
John McCallf312b1e2010-08-26 23:41:50 +00002909 TUK != Sema::TUK_Reference) {
Douglas Gregor8fc6d232010-05-03 17:48:54 +00002910 Diag(Tok, diag::err_enum_template);
2911
2912 // Skip the rest of this declarator, up until the comma or semicolon.
2913 SkipUntil(tok::comma, true);
2914 return;
2915 }
2916
Douglas Gregorb9075602011-02-22 02:55:24 +00002917 if (!Name && TUK != Sema::TUK_Definition) {
2918 Diag(Tok, diag::err_enumerator_unnamed_no_def);
2919
2920 // Skip the rest of this declarator, up until the comma or semicolon.
2921 SkipUntil(tok::comma, true);
2922 return;
2923 }
2924
Douglas Gregor402abb52009-05-28 23:31:59 +00002925 bool Owned = false;
John McCallc4e70192009-09-11 04:59:25 +00002926 bool IsDependent = false;
Douglas Gregor48c89f42010-04-24 16:38:41 +00002927 const char *PrevSpec = 0;
2928 unsigned DiagID;
John McCalld226f652010-08-21 09:40:31 +00002929 Decl *TagDecl = Actions.ActOnTag(getCurScope(), DeclSpec::TST_enum, TUK,
John McCall7f040a92010-12-24 02:08:15 +00002930 StartLoc, SS, Name, NameLoc, attrs.getList(),
Douglas Gregore7612302011-09-09 19:05:14 +00002931 AS, DS.getModulePrivateSpecLoc(),
John McCallf312b1e2010-08-26 23:41:50 +00002932 MultiTemplateParamsArg(Actions),
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002933 Owned, IsDependent, IsScopedEnum,
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00002934 IsScopedUsingClassTag, BaseType);
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002935
Douglas Gregor48c89f42010-04-24 16:38:41 +00002936 if (IsDependent) {
2937 // This enum has a dependent nested-name-specifier. Handle it as a
2938 // dependent tag.
2939 if (!Name) {
2940 DS.SetTypeSpecError();
2941 Diag(Tok, diag::err_expected_type_name_after_typename);
2942 return;
2943 }
2944
Douglas Gregor23c94db2010-07-02 17:43:08 +00002945 TypeResult Type = Actions.ActOnDependentTag(getCurScope(), DeclSpec::TST_enum,
Douglas Gregor48c89f42010-04-24 16:38:41 +00002946 TUK, SS, Name, StartLoc,
2947 NameLoc);
2948 if (Type.isInvalid()) {
2949 DS.SetTypeSpecError();
2950 return;
2951 }
2952
Abramo Bagnara0daaf322011-03-16 20:16:18 +00002953 if (DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc,
2954 NameLoc.isValid() ? NameLoc : StartLoc,
2955 PrevSpec, DiagID, Type.get()))
Douglas Gregor48c89f42010-04-24 16:38:41 +00002956 Diag(StartLoc, DiagID) << PrevSpec;
2957
2958 return;
2959 }
Mike Stump1eb44332009-09-09 15:08:12 +00002960
John McCalld226f652010-08-21 09:40:31 +00002961 if (!TagDecl) {
Douglas Gregor48c89f42010-04-24 16:38:41 +00002962 // The action failed to produce an enumeration tag. If this is a
2963 // definition, consume the entire definition.
2964 if (Tok.is(tok::l_brace)) {
2965 ConsumeBrace();
2966 SkipUntil(tok::r_brace);
2967 }
2968
2969 DS.SetTypeSpecError();
2970 return;
2971 }
2972
Chris Lattner04d66662007-10-09 17:33:22 +00002973 if (Tok.is(tok::l_brace))
Reid Spencer5f016e22007-07-11 17:01:13 +00002974 ParseEnumBody(StartLoc, TagDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00002975
Abramo Bagnara0daaf322011-03-16 20:16:18 +00002976 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc,
2977 NameLoc.isValid() ? NameLoc : StartLoc,
2978 PrevSpec, DiagID, TagDecl, Owned))
John McCallfec54012009-08-03 20:12:06 +00002979 Diag(StartLoc, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00002980}
2981
2982/// ParseEnumBody - Parse a {} enclosed enumerator-list.
2983/// enumerator-list:
2984/// enumerator
2985/// enumerator-list ',' enumerator
2986/// enumerator:
2987/// enumeration-constant
2988/// enumeration-constant '=' constant-expression
2989/// enumeration-constant:
2990/// identifier
2991///
John McCalld226f652010-08-21 09:40:31 +00002992void Parser::ParseEnumBody(SourceLocation StartLoc, Decl *EnumDecl) {
Douglas Gregor074149e2009-01-05 19:45:36 +00002993 // Enter the scope of the enum body and start the definition.
2994 ParseScope EnumScope(this, Scope::DeclScope);
Douglas Gregor23c94db2010-07-02 17:43:08 +00002995 Actions.ActOnTagStartDefinition(getCurScope(), EnumDecl);
Douglas Gregor074149e2009-01-05 19:45:36 +00002996
Reid Spencer5f016e22007-07-11 17:01:13 +00002997 SourceLocation LBraceLoc = ConsumeBrace();
Mike Stump1eb44332009-09-09 15:08:12 +00002998
Chris Lattner7946dd32007-08-27 17:24:30 +00002999 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
Chris Lattner04d66662007-10-09 17:33:22 +00003000 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Fariborz Jahanian05115522010-05-28 22:23:22 +00003001 Diag(Tok, diag::error_empty_enum);
Mike Stump1eb44332009-09-09 15:08:12 +00003002
Chris Lattner5f9e2722011-07-23 10:55:15 +00003003 SmallVector<Decl *, 32> EnumConstantDecls;
Reid Spencer5f016e22007-07-11 17:01:13 +00003004
John McCalld226f652010-08-21 09:40:31 +00003005 Decl *LastEnumConstDecl = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00003006
Reid Spencer5f016e22007-07-11 17:01:13 +00003007 // Parse the enumerator-list.
Chris Lattner04d66662007-10-09 17:33:22 +00003008 while (Tok.is(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003009 IdentifierInfo *Ident = Tok.getIdentifierInfo();
3010 SourceLocation IdentLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00003011
John McCall5b629aa2010-10-22 23:36:17 +00003012 // If attributes exist after the enumerator, parse them.
John McCall0b7e6782011-03-24 11:26:52 +00003013 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00003014 MaybeParseGNUAttributes(attrs);
John McCall5b629aa2010-10-22 23:36:17 +00003015
Reid Spencer5f016e22007-07-11 17:01:13 +00003016 SourceLocation EqualLoc;
John McCall60d7b3a2010-08-24 06:29:42 +00003017 ExprResult AssignedVal;
Chris Lattner04d66662007-10-09 17:33:22 +00003018 if (Tok.is(tok::equal)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003019 EqualLoc = ConsumeToken();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00003020 AssignedVal = ParseConstantExpression();
3021 if (AssignedVal.isInvalid())
Reid Spencer5f016e22007-07-11 17:01:13 +00003022 SkipUntil(tok::comma, tok::r_brace, true, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00003023 }
Mike Stump1eb44332009-09-09 15:08:12 +00003024
Reid Spencer5f016e22007-07-11 17:01:13 +00003025 // Install the enumerator constant into EnumDecl.
John McCalld226f652010-08-21 09:40:31 +00003026 Decl *EnumConstDecl = Actions.ActOnEnumConstant(getCurScope(), EnumDecl,
3027 LastEnumConstDecl,
3028 IdentLoc, Ident,
John McCall7f040a92010-12-24 02:08:15 +00003029 attrs.getList(), EqualLoc,
John McCalld226f652010-08-21 09:40:31 +00003030 AssignedVal.release());
Reid Spencer5f016e22007-07-11 17:01:13 +00003031 EnumConstantDecls.push_back(EnumConstDecl);
3032 LastEnumConstDecl = EnumConstDecl;
Mike Stump1eb44332009-09-09 15:08:12 +00003033
Douglas Gregor751f6922010-09-07 14:51:08 +00003034 if (Tok.is(tok::identifier)) {
3035 // We're missing a comma between enumerators.
3036 SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation);
3037 Diag(Loc, diag::err_enumerator_list_missing_comma)
3038 << FixItHint::CreateInsertion(Loc, ", ");
3039 continue;
3040 }
3041
Chris Lattner04d66662007-10-09 17:33:22 +00003042 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +00003043 break;
3044 SourceLocation CommaLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00003045
3046 if (Tok.isNot(tok::identifier) &&
Douglas Gregor9b3064b2009-04-01 22:41:11 +00003047 !(getLang().C99 || getLang().CPlusPlus0x))
3048 Diag(CommaLoc, diag::ext_enumerator_list_comma)
3049 << getLang().CPlusPlus
Douglas Gregor849b2432010-03-31 17:46:05 +00003050 << FixItHint::CreateRemoval(CommaLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00003051 }
Mike Stump1eb44332009-09-09 15:08:12 +00003052
Reid Spencer5f016e22007-07-11 17:01:13 +00003053 // Eat the }.
Mike Stumpc6e35aa2009-05-16 07:06:02 +00003054 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00003055
Reid Spencer5f016e22007-07-11 17:01:13 +00003056 // If attributes exist after the identifier list, parse them.
John McCall0b7e6782011-03-24 11:26:52 +00003057 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00003058 MaybeParseGNUAttributes(attrs);
Douglas Gregor72de6672009-01-08 20:45:30 +00003059
Edward O'Callaghanfee13812009-08-08 14:36:57 +00003060 Actions.ActOnEnumBody(StartLoc, LBraceLoc, RBraceLoc, EnumDecl,
3061 EnumConstantDecls.data(), EnumConstantDecls.size(),
John McCall7f040a92010-12-24 02:08:15 +00003062 getCurScope(), attrs.getList());
Mike Stump1eb44332009-09-09 15:08:12 +00003063
Douglas Gregor72de6672009-01-08 20:45:30 +00003064 EnumScope.Exit();
Douglas Gregor23c94db2010-07-02 17:43:08 +00003065 Actions.ActOnTagFinishDefinition(getCurScope(), EnumDecl, RBraceLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00003066}
3067
3068/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff5f8aa692008-02-11 23:15:56 +00003069/// start of a type-qualifier-list.
3070bool Parser::isTypeQualifier() const {
3071 switch (Tok.getKind()) {
3072 default: return false;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003073
3074 // type-qualifier only in OpenCL
3075 case tok::kw_private:
3076 return getLang().OpenCL;
3077
Steve Naroff5f8aa692008-02-11 23:15:56 +00003078 // type-qualifier
3079 case tok::kw_const:
3080 case tok::kw_volatile:
3081 case tok::kw_restrict:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003082 case tok::kw___private:
3083 case tok::kw___local:
3084 case tok::kw___global:
3085 case tok::kw___constant:
3086 case tok::kw___read_only:
3087 case tok::kw___read_write:
3088 case tok::kw___write_only:
Steve Naroff5f8aa692008-02-11 23:15:56 +00003089 return true;
3090 }
3091}
3092
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003093/// isKnownToBeTypeSpecifier - Return true if we know that the specified token
3094/// is definitely a type-specifier. Return false if it isn't part of a type
3095/// specifier or if we're not sure.
3096bool Parser::isKnownToBeTypeSpecifier(const Token &Tok) const {
3097 switch (Tok.getKind()) {
3098 default: return false;
3099 // type-specifiers
3100 case tok::kw_short:
3101 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00003102 case tok::kw___int64:
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003103 case tok::kw_signed:
3104 case tok::kw_unsigned:
3105 case tok::kw__Complex:
3106 case tok::kw__Imaginary:
3107 case tok::kw_void:
3108 case tok::kw_char:
3109 case tok::kw_wchar_t:
3110 case tok::kw_char16_t:
3111 case tok::kw_char32_t:
3112 case tok::kw_int:
3113 case tok::kw_float:
3114 case tok::kw_double:
3115 case tok::kw_bool:
3116 case tok::kw__Bool:
3117 case tok::kw__Decimal32:
3118 case tok::kw__Decimal64:
3119 case tok::kw__Decimal128:
3120 case tok::kw___vector:
3121
3122 // struct-or-union-specifier (C99) or class-specifier (C++)
3123 case tok::kw_class:
3124 case tok::kw_struct:
3125 case tok::kw_union:
3126 // enum-specifier
3127 case tok::kw_enum:
3128
3129 // typedef-name
3130 case tok::annot_typename:
3131 return true;
3132 }
3133}
3134
Steve Naroff5f8aa692008-02-11 23:15:56 +00003135/// isTypeSpecifierQualifier - Return true if the current token could be the
Reid Spencer5f016e22007-07-11 17:01:13 +00003136/// start of a specifier-qualifier-list.
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003137bool Parser::isTypeSpecifierQualifier() {
Reid Spencer5f016e22007-07-11 17:01:13 +00003138 switch (Tok.getKind()) {
3139 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00003140
Chris Lattner166a8fc2009-01-04 23:41:41 +00003141 case tok::identifier: // foo::bar
John Thompson82287d12010-02-05 00:12:22 +00003142 if (TryAltiVecVectorToken())
3143 return true;
3144 // Fall through.
Douglas Gregord57959a2009-03-27 23:10:48 +00003145 case tok::kw_typename: // typename T::type
Chris Lattner166a8fc2009-01-04 23:41:41 +00003146 // Annotate typenames and C++ scope specifiers. If we get one, just
3147 // recurse to handle whatever we get.
3148 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003149 return true;
3150 if (Tok.is(tok::identifier))
3151 return false;
3152 return isTypeSpecifierQualifier();
Douglas Gregord57959a2009-03-27 23:10:48 +00003153
Chris Lattner166a8fc2009-01-04 23:41:41 +00003154 case tok::coloncolon: // ::foo::bar
3155 if (NextToken().is(tok::kw_new) || // ::new
3156 NextToken().is(tok::kw_delete)) // ::delete
3157 return false;
3158
Chris Lattner166a8fc2009-01-04 23:41:41 +00003159 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003160 return true;
3161 return isTypeSpecifierQualifier();
Mike Stump1eb44332009-09-09 15:08:12 +00003162
Reid Spencer5f016e22007-07-11 17:01:13 +00003163 // GNU attributes support.
3164 case tok::kw___attribute:
Steve Naroffd1861fd2007-07-31 12:34:36 +00003165 // GNU typeof support.
3166 case tok::kw_typeof:
Mike Stump1eb44332009-09-09 15:08:12 +00003167
Reid Spencer5f016e22007-07-11 17:01:13 +00003168 // type-specifiers
3169 case tok::kw_short:
3170 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00003171 case tok::kw___int64:
Reid Spencer5f016e22007-07-11 17:01:13 +00003172 case tok::kw_signed:
3173 case tok::kw_unsigned:
3174 case tok::kw__Complex:
3175 case tok::kw__Imaginary:
3176 case tok::kw_void:
3177 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00003178 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00003179 case tok::kw_char16_t:
3180 case tok::kw_char32_t:
Reid Spencer5f016e22007-07-11 17:01:13 +00003181 case tok::kw_int:
3182 case tok::kw_float:
3183 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00003184 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00003185 case tok::kw__Bool:
3186 case tok::kw__Decimal32:
3187 case tok::kw__Decimal64:
3188 case tok::kw__Decimal128:
John Thompson82287d12010-02-05 00:12:22 +00003189 case tok::kw___vector:
Mike Stump1eb44332009-09-09 15:08:12 +00003190
Chris Lattner99dc9142008-04-13 18:59:07 +00003191 // struct-or-union-specifier (C99) or class-specifier (C++)
3192 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00003193 case tok::kw_struct:
3194 case tok::kw_union:
3195 // enum-specifier
3196 case tok::kw_enum:
Mike Stump1eb44332009-09-09 15:08:12 +00003197
Reid Spencer5f016e22007-07-11 17:01:13 +00003198 // type-qualifier
3199 case tok::kw_const:
3200 case tok::kw_volatile:
3201 case tok::kw_restrict:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003202
3203 // typedef-name
Chris Lattnerb31757b2009-01-06 05:06:21 +00003204 case tok::annot_typename:
Reid Spencer5f016e22007-07-11 17:01:13 +00003205 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00003206
Chris Lattner7c186be2008-10-20 00:25:30 +00003207 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
3208 case tok::less:
3209 return getLang().ObjC1;
Mike Stump1eb44332009-09-09 15:08:12 +00003210
Steve Naroff239f0732008-12-25 14:16:32 +00003211 case tok::kw___cdecl:
3212 case tok::kw___stdcall:
3213 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00003214 case tok::kw___thiscall:
Eli Friedman290eeb02009-06-08 23:27:34 +00003215 case tok::kw___w64:
3216 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00003217 case tok::kw___ptr32:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003218 case tok::kw___pascal:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00003219 case tok::kw___unaligned:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003220
3221 case tok::kw___private:
3222 case tok::kw___local:
3223 case tok::kw___global:
3224 case tok::kw___constant:
3225 case tok::kw___read_only:
3226 case tok::kw___read_write:
3227 case tok::kw___write_only:
3228
Eli Friedman290eeb02009-06-08 23:27:34 +00003229 return true;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003230
3231 case tok::kw_private:
3232 return getLang().OpenCL;
Eli Friedmanb001de72011-10-06 23:00:33 +00003233
3234 // C1x _Atomic()
3235 case tok::kw__Atomic:
3236 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00003237 }
3238}
3239
3240/// isDeclarationSpecifier() - Return true if the current token is part of a
3241/// declaration specifier.
Douglas Gregor9497a732010-09-16 01:51:54 +00003242///
3243/// \param DisambiguatingWithExpression True to indicate that the purpose of
3244/// this check is to disambiguate between an expression and a declaration.
3245bool Parser::isDeclarationSpecifier(bool DisambiguatingWithExpression) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003246 switch (Tok.getKind()) {
3247 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00003248
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003249 case tok::kw_private:
3250 return getLang().OpenCL;
3251
Chris Lattner166a8fc2009-01-04 23:41:41 +00003252 case tok::identifier: // foo::bar
Steve Naroff61f72cb2009-03-09 21:12:44 +00003253 // Unfortunate hack to support "Class.factoryMethod" notation.
3254 if (getLang().ObjC1 && NextToken().is(tok::period))
3255 return false;
John Thompson82287d12010-02-05 00:12:22 +00003256 if (TryAltiVecVectorToken())
3257 return true;
3258 // Fall through.
Douglas Gregord57959a2009-03-27 23:10:48 +00003259 case tok::kw_typename: // typename T::type
Chris Lattner166a8fc2009-01-04 23:41:41 +00003260 // Annotate typenames and C++ scope specifiers. If we get one, just
3261 // recurse to handle whatever we get.
3262 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003263 return true;
3264 if (Tok.is(tok::identifier))
3265 return false;
Douglas Gregor9497a732010-09-16 01:51:54 +00003266
3267 // If we're in Objective-C and we have an Objective-C class type followed
3268 // by an identifier and then either ':' or ']', in a place where an
3269 // expression is permitted, then this is probably a class message send
3270 // missing the initial '['. In this case, we won't consider this to be
3271 // the start of a declaration.
3272 if (DisambiguatingWithExpression &&
3273 isStartOfObjCClassMessageMissingOpenBracket())
3274 return false;
3275
John McCall9ba61662010-02-26 08:45:28 +00003276 return isDeclarationSpecifier();
3277
Chris Lattner166a8fc2009-01-04 23:41:41 +00003278 case tok::coloncolon: // ::foo::bar
3279 if (NextToken().is(tok::kw_new) || // ::new
3280 NextToken().is(tok::kw_delete)) // ::delete
3281 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00003282
Chris Lattner166a8fc2009-01-04 23:41:41 +00003283 // Annotate typenames and C++ scope specifiers. If we get one, just
3284 // recurse to handle whatever we get.
3285 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003286 return true;
3287 return isDeclarationSpecifier();
Mike Stump1eb44332009-09-09 15:08:12 +00003288
Reid Spencer5f016e22007-07-11 17:01:13 +00003289 // storage-class-specifier
3290 case tok::kw_typedef:
3291 case tok::kw_extern:
Steve Naroff8d54bf22007-12-18 00:16:02 +00003292 case tok::kw___private_extern__:
Reid Spencer5f016e22007-07-11 17:01:13 +00003293 case tok::kw_static:
3294 case tok::kw_auto:
3295 case tok::kw_register:
3296 case tok::kw___thread:
Mike Stump1eb44332009-09-09 15:08:12 +00003297
Douglas Gregor8d267c52011-09-09 02:06:17 +00003298 // Modules
3299 case tok::kw___module_private__:
3300
Reid Spencer5f016e22007-07-11 17:01:13 +00003301 // type-specifiers
3302 case tok::kw_short:
3303 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00003304 case tok::kw___int64:
Reid Spencer5f016e22007-07-11 17:01:13 +00003305 case tok::kw_signed:
3306 case tok::kw_unsigned:
3307 case tok::kw__Complex:
3308 case tok::kw__Imaginary:
3309 case tok::kw_void:
3310 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00003311 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00003312 case tok::kw_char16_t:
3313 case tok::kw_char32_t:
3314
Reid Spencer5f016e22007-07-11 17:01:13 +00003315 case tok::kw_int:
3316 case tok::kw_float:
3317 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00003318 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00003319 case tok::kw__Bool:
3320 case tok::kw__Decimal32:
3321 case tok::kw__Decimal64:
3322 case tok::kw__Decimal128:
John Thompson82287d12010-02-05 00:12:22 +00003323 case tok::kw___vector:
Mike Stump1eb44332009-09-09 15:08:12 +00003324
Chris Lattner99dc9142008-04-13 18:59:07 +00003325 // struct-or-union-specifier (C99) or class-specifier (C++)
3326 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00003327 case tok::kw_struct:
3328 case tok::kw_union:
3329 // enum-specifier
3330 case tok::kw_enum:
Mike Stump1eb44332009-09-09 15:08:12 +00003331
Reid Spencer5f016e22007-07-11 17:01:13 +00003332 // type-qualifier
3333 case tok::kw_const:
3334 case tok::kw_volatile:
3335 case tok::kw_restrict:
Steve Naroffd1861fd2007-07-31 12:34:36 +00003336
Reid Spencer5f016e22007-07-11 17:01:13 +00003337 // function-specifier
3338 case tok::kw_inline:
Douglas Gregorb48fe382008-10-31 09:07:45 +00003339 case tok::kw_virtual:
3340 case tok::kw_explicit:
Chris Lattnerd6c7c182007-08-09 16:40:21 +00003341
Peter Collingbournec6eb44b2011-04-15 00:35:57 +00003342 // static_assert-declaration
3343 case tok::kw__Static_assert:
3344
Chris Lattner1ef08762007-08-09 17:01:07 +00003345 // GNU typeof support.
3346 case tok::kw_typeof:
Mike Stump1eb44332009-09-09 15:08:12 +00003347
Chris Lattner1ef08762007-08-09 17:01:07 +00003348 // GNU attributes.
Chris Lattnerd6c7c182007-08-09 16:40:21 +00003349 case tok::kw___attribute:
Reid Spencer5f016e22007-07-11 17:01:13 +00003350 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00003351
Francois Pichete3d49b42011-06-19 08:02:06 +00003352 // C++0x decltype.
3353 case tok::kw_decltype:
3354 return true;
3355
Eli Friedmanb001de72011-10-06 23:00:33 +00003356 // C1x _Atomic()
3357 case tok::kw__Atomic:
3358 return true;
3359
Chris Lattnerf3948c42008-07-26 03:38:44 +00003360 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
3361 case tok::less:
3362 return getLang().ObjC1;
Mike Stump1eb44332009-09-09 15:08:12 +00003363
Douglas Gregord9d75e52011-04-27 05:41:15 +00003364 // typedef-name
3365 case tok::annot_typename:
3366 return !DisambiguatingWithExpression ||
3367 !isStartOfObjCClassMessageMissingOpenBracket();
3368
Steve Naroff47f52092009-01-06 19:34:12 +00003369 case tok::kw___declspec:
Steve Naroff239f0732008-12-25 14:16:32 +00003370 case tok::kw___cdecl:
3371 case tok::kw___stdcall:
3372 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00003373 case tok::kw___thiscall:
Eli Friedman290eeb02009-06-08 23:27:34 +00003374 case tok::kw___w64:
3375 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00003376 case tok::kw___ptr32:
Eli Friedman290eeb02009-06-08 23:27:34 +00003377 case tok::kw___forceinline:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003378 case tok::kw___pascal:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00003379 case tok::kw___unaligned:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003380
3381 case tok::kw___private:
3382 case tok::kw___local:
3383 case tok::kw___global:
3384 case tok::kw___constant:
3385 case tok::kw___read_only:
3386 case tok::kw___read_write:
3387 case tok::kw___write_only:
3388
Eli Friedman290eeb02009-06-08 23:27:34 +00003389 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00003390 }
3391}
3392
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003393bool Parser::isConstructorDeclarator() {
3394 TentativeParsingAction TPA(*this);
3395
3396 // Parse the C++ scope specifier.
3397 CXXScopeSpec SS;
John McCallb3d87482010-08-24 05:47:05 +00003398 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), true)) {
John McCall9ba61662010-02-26 08:45:28 +00003399 TPA.Revert();
3400 return false;
3401 }
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003402
3403 // Parse the constructor name.
3404 if (Tok.is(tok::identifier) || Tok.is(tok::annot_template_id)) {
3405 // We already know that we have a constructor name; just consume
3406 // the token.
3407 ConsumeToken();
3408 } else {
3409 TPA.Revert();
3410 return false;
3411 }
3412
3413 // Current class name must be followed by a left parentheses.
3414 if (Tok.isNot(tok::l_paren)) {
3415 TPA.Revert();
3416 return false;
3417 }
3418 ConsumeParen();
3419
3420 // A right parentheses or ellipsis signals that we have a constructor.
3421 if (Tok.is(tok::r_paren) || Tok.is(tok::ellipsis)) {
3422 TPA.Revert();
3423 return true;
3424 }
3425
3426 // If we need to, enter the specified scope.
3427 DeclaratorScopeObj DeclScopeObj(*this, SS);
Douglas Gregor23c94db2010-07-02 17:43:08 +00003428 if (SS.isSet() && Actions.ShouldEnterDeclaratorScope(getCurScope(), SS))
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003429 DeclScopeObj.EnterDeclaratorScope();
3430
Francois Pichetdfaa5fb2011-01-31 04:54:32 +00003431 // Optionally skip Microsoft attributes.
John McCall0b7e6782011-03-24 11:26:52 +00003432 ParsedAttributes Attrs(AttrFactory);
Francois Pichetdfaa5fb2011-01-31 04:54:32 +00003433 MaybeParseMicrosoftAttributes(Attrs);
3434
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003435 // Check whether the next token(s) are part of a declaration
3436 // specifier, in which case we have the start of a parameter and,
3437 // therefore, we know that this is a constructor.
3438 bool IsConstructor = isDeclarationSpecifier();
3439 TPA.Revert();
3440 return IsConstructor;
3441}
Reid Spencer5f016e22007-07-11 17:01:13 +00003442
3443/// ParseTypeQualifierListOpt
Dawn Perchik52fc3142010-09-03 01:29:35 +00003444/// type-qualifier-list: [C99 6.7.5]
3445/// type-qualifier
3446/// [vendor] attributes
3447/// [ only if VendorAttributesAllowed=true ]
3448/// type-qualifier-list type-qualifier
3449/// [vendor] type-qualifier-list attributes
3450/// [ only if VendorAttributesAllowed=true ]
3451/// [C++0x] attribute-specifier[opt] is allowed before cv-qualifier-seq
3452/// [ only if CXX0XAttributesAllowed=true ]
3453/// Note: vendor can be GNU, MS, etc.
Reid Spencer5f016e22007-07-11 17:01:13 +00003454///
Dawn Perchik52fc3142010-09-03 01:29:35 +00003455void Parser::ParseTypeQualifierListOpt(DeclSpec &DS,
3456 bool VendorAttributesAllowed,
Sean Huntbbd37c62009-11-21 08:43:09 +00003457 bool CXX0XAttributesAllowed) {
3458 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier()) {
3459 SourceLocation Loc = Tok.getLocation();
John McCall0b7e6782011-03-24 11:26:52 +00003460 ParsedAttributesWithRange attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00003461 ParseCXX0XAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00003462 if (CXX0XAttributesAllowed)
John McCall7f040a92010-12-24 02:08:15 +00003463 DS.takeAttributesFrom(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00003464 else
3465 Diag(Loc, diag::err_attributes_not_allowed);
3466 }
Abramo Bagnara796aa442011-03-12 11:17:06 +00003467
3468 SourceLocation EndLoc;
3469
Reid Spencer5f016e22007-07-11 17:01:13 +00003470 while (1) {
John McCallfec54012009-08-03 20:12:06 +00003471 bool isInvalid = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00003472 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00003473 unsigned DiagID = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00003474 SourceLocation Loc = Tok.getLocation();
3475
3476 switch (Tok.getKind()) {
Douglas Gregor1a480c42010-08-27 17:35:51 +00003477 case tok::code_completion:
3478 Actions.CodeCompleteTypeQualifiers(DS);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00003479 return cutOffParsing();
Douglas Gregor1a480c42010-08-27 17:35:51 +00003480
Reid Spencer5f016e22007-07-11 17:01:13 +00003481 case tok::kw_const:
John McCallfec54012009-08-03 20:12:06 +00003482 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, DiagID,
3483 getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00003484 break;
3485 case tok::kw_volatile:
John McCallfec54012009-08-03 20:12:06 +00003486 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
3487 getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00003488 break;
3489 case tok::kw_restrict:
John McCallfec54012009-08-03 20:12:06 +00003490 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
3491 getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00003492 break;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003493
3494 // OpenCL qualifiers:
3495 case tok::kw_private:
3496 if (!getLang().OpenCL)
3497 goto DoneWithTypeQuals;
3498 case tok::kw___private:
3499 case tok::kw___global:
3500 case tok::kw___local:
3501 case tok::kw___constant:
3502 case tok::kw___read_only:
3503 case tok::kw___write_only:
3504 case tok::kw___read_write:
3505 ParseOpenCLQualifiers(DS);
3506 break;
3507
Eli Friedman290eeb02009-06-08 23:27:34 +00003508 case tok::kw___w64:
Steve Naroff86bc6cf2008-12-25 14:41:26 +00003509 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00003510 case tok::kw___ptr32:
Steve Naroff239f0732008-12-25 14:16:32 +00003511 case tok::kw___cdecl:
3512 case tok::kw___stdcall:
3513 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00003514 case tok::kw___thiscall:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00003515 case tok::kw___unaligned:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003516 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00003517 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman290eeb02009-06-08 23:27:34 +00003518 continue;
3519 }
3520 goto DoneWithTypeQuals;
Dawn Perchik52fc3142010-09-03 01:29:35 +00003521 case tok::kw___pascal:
3522 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00003523 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik52fc3142010-09-03 01:29:35 +00003524 continue;
3525 }
3526 goto DoneWithTypeQuals;
Reid Spencer5f016e22007-07-11 17:01:13 +00003527 case tok::kw___attribute:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003528 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00003529 ParseGNUAttributes(DS.getAttributes());
Chris Lattner5a69d1c2008-12-18 07:02:59 +00003530 continue; // do *not* consume the next token!
3531 }
3532 // otherwise, FALL THROUGH!
3533 default:
Steve Naroff239f0732008-12-25 14:16:32 +00003534 DoneWithTypeQuals:
Chris Lattner5a69d1c2008-12-18 07:02:59 +00003535 // If this is not a type-qualifier token, we're done reading type
3536 // qualifiers. First verify that DeclSpec's are consistent.
Douglas Gregor9b3064b2009-04-01 22:41:11 +00003537 DS.Finish(Diags, PP);
Abramo Bagnara796aa442011-03-12 11:17:06 +00003538 if (EndLoc.isValid())
3539 DS.SetRangeEnd(EndLoc);
Chris Lattner5a69d1c2008-12-18 07:02:59 +00003540 return;
Reid Spencer5f016e22007-07-11 17:01:13 +00003541 }
Chris Lattnera1fcbad2008-12-18 06:50:14 +00003542
Reid Spencer5f016e22007-07-11 17:01:13 +00003543 // If the specifier combination wasn't legal, issue a diagnostic.
3544 if (isInvalid) {
3545 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner1ab3b962008-11-18 07:48:38 +00003546 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00003547 }
Abramo Bagnara796aa442011-03-12 11:17:06 +00003548 EndLoc = ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00003549 }
3550}
3551
3552
3553/// ParseDeclarator - Parse and verify a newly-initialized declarator.
3554///
3555void Parser::ParseDeclarator(Declarator &D) {
3556 /// This implements the 'declarator' production in the C grammar, then checks
3557 /// for well-formedness and issues diagnostics.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003558 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Reid Spencer5f016e22007-07-11 17:01:13 +00003559}
3560
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003561/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
3562/// is parsed by the function passed to it. Pass null, and the direct-declarator
3563/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003564/// ptr-operator production.
3565///
Sebastian Redlf30208a2009-01-24 21:16:55 +00003566/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
3567/// [C] pointer[opt] direct-declarator
3568/// [C++] direct-declarator
3569/// [C++] ptr-operator declarator
Reid Spencer5f016e22007-07-11 17:01:13 +00003570///
3571/// pointer: [C99 6.7.5]
3572/// '*' type-qualifier-list[opt]
3573/// '*' type-qualifier-list[opt] pointer
3574///
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003575/// ptr-operator:
3576/// '*' cv-qualifier-seq[opt]
3577/// '&'
Sebastian Redl05532f22009-03-15 22:02:01 +00003578/// [C++0x] '&&'
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003579/// [GNU] '&' restrict[opt] attributes[opt]
Sebastian Redl05532f22009-03-15 22:02:01 +00003580/// [GNU?] '&&' restrict[opt] attributes[opt]
Sebastian Redlf30208a2009-01-24 21:16:55 +00003581/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003582void Parser::ParseDeclaratorInternal(Declarator &D,
3583 DirectDeclParseFunction DirectDeclParser) {
Douglas Gregor91a28862009-08-26 14:27:30 +00003584 if (Diags.hasAllExtensionsSilenced())
3585 D.setExtension();
Douglas Gregor2ccccb32010-08-23 18:23:48 +00003586
Sebastian Redlf30208a2009-01-24 21:16:55 +00003587 // C++ member pointers start with a '::' or a nested-name.
3588 // Member pointers get special handling, since there's no place for the
3589 // scope spec in the generic path below.
Chris Lattnerf919bfe2009-03-24 17:04:48 +00003590 if (getLang().CPlusPlus &&
3591 (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) ||
3592 Tok.is(tok::annot_cxxscope))) {
Sebastian Redlf30208a2009-01-24 21:16:55 +00003593 CXXScopeSpec SS;
John McCallb3d87482010-08-24 05:47:05 +00003594 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), true); // ignore fail
John McCall9ba61662010-02-26 08:45:28 +00003595
Jeffrey Yasskinedc28772010-04-07 23:29:58 +00003596 if (SS.isNotEmpty()) {
Mike Stump1eb44332009-09-09 15:08:12 +00003597 if (Tok.isNot(tok::star)) {
Sebastian Redlf30208a2009-01-24 21:16:55 +00003598 // The scope spec really belongs to the direct-declarator.
3599 D.getCXXScopeSpec() = SS;
3600 if (DirectDeclParser)
3601 (this->*DirectDeclParser)(D);
3602 return;
3603 }
3604
3605 SourceLocation Loc = ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00003606 D.SetRangeEnd(Loc);
John McCall0b7e6782011-03-24 11:26:52 +00003607 DeclSpec DS(AttrFactory);
Sebastian Redlf30208a2009-01-24 21:16:55 +00003608 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00003609 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00003610
3611 // Recurse to parse whatever is left.
3612 ParseDeclaratorInternal(D, DirectDeclParser);
3613
3614 // Sema will have to catch (syntactically invalid) pointers into global
3615 // scope. It has to catch pointers into namespace scope anyway.
3616 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
John McCall0b7e6782011-03-24 11:26:52 +00003617 Loc),
3618 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003619 /* Don't replace range end. */SourceLocation());
Sebastian Redlf30208a2009-01-24 21:16:55 +00003620 return;
3621 }
3622 }
3623
3624 tok::TokenKind Kind = Tok.getKind();
Steve Naroff5618bd42008-08-27 16:04:49 +00003625 // Not a pointer, C++ reference, or block.
Chris Lattner9af55002009-03-27 04:18:06 +00003626 if (Kind != tok::star && Kind != tok::caret &&
Chris Lattnerf919bfe2009-03-24 17:04:48 +00003627 (Kind != tok::amp || !getLang().CPlusPlus) &&
Sebastian Redl743de1f2009-03-23 00:00:23 +00003628 // We parse rvalue refs in C++03, because otherwise the errors are scary.
Chris Lattner9af55002009-03-27 04:18:06 +00003629 (Kind != tok::ampamp || !getLang().CPlusPlus)) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003630 if (DirectDeclParser)
3631 (this->*DirectDeclParser)(D);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003632 return;
3633 }
Sebastian Redlf30208a2009-01-24 21:16:55 +00003634
Sebastian Redl05532f22009-03-15 22:02:01 +00003635 // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference,
3636 // '&&' -> rvalue reference
Sebastian Redl743de1f2009-03-23 00:00:23 +00003637 SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&.
Sebastian Redlab197ba2009-02-09 18:23:29 +00003638 D.SetRangeEnd(Loc);
Reid Spencer5f016e22007-07-11 17:01:13 +00003639
Chris Lattner9af55002009-03-27 04:18:06 +00003640 if (Kind == tok::star || Kind == tok::caret) {
Chris Lattner76549142008-02-21 01:32:26 +00003641 // Is a pointer.
John McCall0b7e6782011-03-24 11:26:52 +00003642 DeclSpec DS(AttrFactory);
Sebastian Redlf30208a2009-01-24 21:16:55 +00003643
Reid Spencer5f016e22007-07-11 17:01:13 +00003644 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00003645 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00003646
Reid Spencer5f016e22007-07-11 17:01:13 +00003647 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003648 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroff5618bd42008-08-27 16:04:49 +00003649 if (Kind == tok::star)
3650 // Remember that we parsed a pointer type, and remember the type-quals.
3651 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
Chandler Carruthd067c072011-02-23 18:51:59 +00003652 DS.getConstSpecLoc(),
3653 DS.getVolatileSpecLoc(),
John McCall0b7e6782011-03-24 11:26:52 +00003654 DS.getRestrictSpecLoc()),
3655 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003656 SourceLocation());
Steve Naroff5618bd42008-08-27 16:04:49 +00003657 else
3658 // Remember that we parsed a Block type, and remember the type-quals.
Mike Stump1eb44332009-09-09 15:08:12 +00003659 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
John McCall0b7e6782011-03-24 11:26:52 +00003660 Loc),
3661 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003662 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00003663 } else {
3664 // Is a reference
John McCall0b7e6782011-03-24 11:26:52 +00003665 DeclSpec DS(AttrFactory);
Reid Spencer5f016e22007-07-11 17:01:13 +00003666
Sebastian Redl743de1f2009-03-23 00:00:23 +00003667 // Complain about rvalue references in C++03, but then go on and build
3668 // the declarator.
3669 if (Kind == tok::ampamp && !getLang().CPlusPlus0x)
Douglas Gregor16cf8f52011-01-25 02:17:32 +00003670 Diag(Loc, diag::ext_rvalue_reference);
Sebastian Redl743de1f2009-03-23 00:00:23 +00003671
Reid Spencer5f016e22007-07-11 17:01:13 +00003672 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
3673 // cv-qualifiers are introduced through the use of a typedef or of a
3674 // template type argument, in which case the cv-qualifiers are ignored.
3675 //
3676 // [GNU] Retricted references are allowed.
3677 // [GNU] Attributes on references are allowed.
Sean Huntbbd37c62009-11-21 08:43:09 +00003678 // [C++0x] Attributes on references are not allowed.
3679 ParseTypeQualifierListOpt(DS, true, false);
Sebastian Redlab197ba2009-02-09 18:23:29 +00003680 D.ExtendWithDeclSpec(DS);
Reid Spencer5f016e22007-07-11 17:01:13 +00003681
3682 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
3683 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
3684 Diag(DS.getConstSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00003685 diag::err_invalid_reference_qualifier_application) << "const";
Reid Spencer5f016e22007-07-11 17:01:13 +00003686 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
3687 Diag(DS.getVolatileSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00003688 diag::err_invalid_reference_qualifier_application) << "volatile";
Reid Spencer5f016e22007-07-11 17:01:13 +00003689 }
3690
3691 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003692 ParseDeclaratorInternal(D, DirectDeclParser);
Reid Spencer5f016e22007-07-11 17:01:13 +00003693
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00003694 if (D.getNumTypeObjects() > 0) {
3695 // C++ [dcl.ref]p4: There shall be no references to references.
3696 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
3697 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattnerda83bac2008-11-19 07:37:42 +00003698 if (const IdentifierInfo *II = D.getIdentifier())
3699 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
3700 << II;
3701 else
3702 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
3703 << "type name";
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00003704
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003705 // Once we've complained about the reference-to-reference, we
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00003706 // can go ahead and build the (technically ill-formed)
3707 // declarator: reference collapsing will take care of it.
3708 }
3709 }
3710
Reid Spencer5f016e22007-07-11 17:01:13 +00003711 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner76549142008-02-21 01:32:26 +00003712 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
Sebastian Redl05532f22009-03-15 22:02:01 +00003713 Kind == tok::amp),
John McCall0b7e6782011-03-24 11:26:52 +00003714 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003715 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00003716 }
3717}
3718
3719/// ParseDirectDeclarator
3720/// direct-declarator: [C99 6.7.5]
Douglas Gregor42a552f2008-11-05 20:51:48 +00003721/// [C99] identifier
Reid Spencer5f016e22007-07-11 17:01:13 +00003722/// '(' declarator ')'
3723/// [GNU] '(' attributes declarator ')'
3724/// [C90] direct-declarator '[' constant-expression[opt] ']'
3725/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
3726/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
3727/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
3728/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
3729/// direct-declarator '(' parameter-type-list ')'
3730/// direct-declarator '(' identifier-list[opt] ')'
3731/// [GNU] direct-declarator '(' parameter-forward-declarations
3732/// parameter-type-list[opt] ')'
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003733/// [C++] direct-declarator '(' parameter-declaration-clause ')'
3734/// cv-qualifier-seq[opt] exception-specification[opt]
Douglas Gregorb48fe382008-10-31 09:07:45 +00003735/// [C++] declarator-id
Douglas Gregor42a552f2008-11-05 20:51:48 +00003736///
3737/// declarator-id: [C++ 8]
Douglas Gregora8bc8c92010-12-23 22:44:42 +00003738/// '...'[opt] id-expression
Douglas Gregor42a552f2008-11-05 20:51:48 +00003739/// '::'[opt] nested-name-specifier[opt] type-name
3740///
3741/// id-expression: [C++ 5.1]
3742/// unqualified-id
Douglas Gregordb422df2009-09-25 21:45:23 +00003743/// qualified-id
Douglas Gregor42a552f2008-11-05 20:51:48 +00003744///
3745/// unqualified-id: [C++ 5.1]
Mike Stump1eb44332009-09-09 15:08:12 +00003746/// identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003747/// operator-function-id
Douglas Gregordb422df2009-09-25 21:45:23 +00003748/// conversion-function-id
Mike Stump1eb44332009-09-09 15:08:12 +00003749/// '~' class-name
Douglas Gregor39a8de12009-02-25 19:37:18 +00003750/// template-id
Argyrios Kyrtzidisc7ed9c62008-11-07 22:02:30 +00003751///
Reid Spencer5f016e22007-07-11 17:01:13 +00003752void Parser::ParseDirectDeclarator(Declarator &D) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00003753 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003754
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003755 if (getLang().CPlusPlus && D.mayHaveIdentifier()) {
3756 // ParseDeclaratorInternal might already have parsed the scope.
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00003757 if (D.getCXXScopeSpec().isEmpty()) {
John McCallb3d87482010-08-24 05:47:05 +00003758 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec(), ParsedType(), true);
John McCall9ba61662010-02-26 08:45:28 +00003759 }
3760
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00003761 if (D.getCXXScopeSpec().isValid()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00003762 if (Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
John McCalle7e278b2009-12-11 20:04:54 +00003763 // Change the declaration context for name lookup, until this function
3764 // is exited (and the declarator has been parsed).
3765 DeclScopeObj.EnterDeclaratorScope();
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00003766 }
3767
Douglas Gregora8bc8c92010-12-23 22:44:42 +00003768 // C++0x [dcl.fct]p14:
3769 // There is a syntactic ambiguity when an ellipsis occurs at the end
3770 // of a parameter-declaration-clause without a preceding comma. In
3771 // this case, the ellipsis is parsed as part of the
3772 // abstract-declarator if the type of the parameter names a template
3773 // parameter pack that has not been expanded; otherwise, it is parsed
3774 // as part of the parameter-declaration-clause.
3775 if (Tok.is(tok::ellipsis) &&
3776 !((D.getContext() == Declarator::PrototypeContext ||
3777 D.getContext() == Declarator::BlockLiteralContext) &&
Douglas Gregora8bc8c92010-12-23 22:44:42 +00003778 NextToken().is(tok::r_paren) &&
3779 !Actions.containsUnexpandedParameterPacks(D)))
3780 D.setEllipsisLoc(ConsumeToken());
3781
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003782 if (Tok.is(tok::identifier) || Tok.is(tok::kw_operator) ||
3783 Tok.is(tok::annot_template_id) || Tok.is(tok::tilde)) {
3784 // We found something that indicates the start of an unqualified-id.
3785 // Parse that unqualified-id.
John McCallba9d8532010-04-13 06:39:49 +00003786 bool AllowConstructorName;
3787 if (D.getDeclSpec().hasTypeSpecifier())
3788 AllowConstructorName = false;
3789 else if (D.getCXXScopeSpec().isSet())
3790 AllowConstructorName =
3791 (D.getContext() == Declarator::FileContext ||
3792 (D.getContext() == Declarator::MemberContext &&
3793 D.getDeclSpec().isFriendSpecified()));
3794 else
3795 AllowConstructorName = (D.getContext() == Declarator::MemberContext);
3796
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003797 if (ParseUnqualifiedId(D.getCXXScopeSpec(),
3798 /*EnteringContext=*/true,
3799 /*AllowDestructorName=*/true,
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003800 AllowConstructorName,
John McCallb3d87482010-08-24 05:47:05 +00003801 ParsedType(),
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00003802 D.getName()) ||
3803 // Once we're past the identifier, if the scope was bad, mark the
3804 // whole declarator bad.
3805 D.getCXXScopeSpec().isInvalid()) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00003806 D.SetIdentifier(0, Tok.getLocation());
3807 D.setInvalidType(true);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003808 } else {
3809 // Parsed the unqualified-id; update range information and move along.
3810 if (D.getSourceRange().getBegin().isInvalid())
3811 D.SetRangeBegin(D.getName().getSourceRange().getBegin());
3812 D.SetRangeEnd(D.getName().getSourceRange().getEnd());
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003813 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003814 goto PastIdentifier;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00003815 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003816 } else if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00003817 assert(!getLang().CPlusPlus &&
3818 "There's a C++-specific check for tok::identifier above");
3819 assert(Tok.getIdentifierInfo() && "Not an identifier?");
3820 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
3821 ConsumeToken();
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003822 goto PastIdentifier;
3823 }
3824
3825 if (Tok.is(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003826 // direct-declarator: '(' declarator ')'
3827 // direct-declarator: '(' attributes declarator ')'
3828 // Example: 'char (*X)' or 'int (*XX)(void)'
3829 ParseParenDeclarator(D);
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003830
3831 // If the declarator was parenthesized, we entered the declarator
3832 // scope when parsing the parenthesized declarator, then exited
3833 // the scope already. Re-enter the scope, if we need to.
3834 if (D.getCXXScopeSpec().isSet()) {
Fariborz Jahanian46877cd2010-08-17 23:50:37 +00003835 // If there was an error parsing parenthesized declarator, declarator
3836 // scope may have been enterred before. Don't do it again.
3837 if (!D.isInvalidType() &&
3838 Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003839 // Change the declaration context for name lookup, until this function
3840 // is exited (and the declarator has been parsed).
Fariborz Jahanian46877cd2010-08-17 23:50:37 +00003841 DeclScopeObj.EnterDeclaratorScope();
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003842 }
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00003843 } else if (D.mayOmitIdentifier()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003844 // This could be something simple like "int" (in which case the declarator
3845 // portion is empty), if an abstract-declarator is allowed.
3846 D.SetIdentifier(0, Tok.getLocation());
3847 } else {
Douglas Gregore950d4b2009-03-06 23:28:18 +00003848 if (D.getContext() == Declarator::MemberContext)
3849 Diag(Tok, diag::err_expected_member_name_or_semi)
3850 << D.getDeclSpec().getSourceRange();
3851 else if (getLang().CPlusPlus)
Douglas Gregor2d1c2142009-11-03 19:44:04 +00003852 Diag(Tok, diag::err_expected_unqualified_id) << getLang().CPlusPlus;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003853 else
Chris Lattner1ab3b962008-11-18 07:48:38 +00003854 Diag(Tok, diag::err_expected_ident_lparen);
Reid Spencer5f016e22007-07-11 17:01:13 +00003855 D.SetIdentifier(0, Tok.getLocation());
Chris Lattner1f6f54b2008-11-11 06:13:16 +00003856 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00003857 }
Mike Stump1eb44332009-09-09 15:08:12 +00003858
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00003859 PastIdentifier:
Reid Spencer5f016e22007-07-11 17:01:13 +00003860 assert(D.isPastIdentifier() &&
3861 "Haven't past the location of the identifier yet?");
Mike Stump1eb44332009-09-09 15:08:12 +00003862
Sean Huntbbd37c62009-11-21 08:43:09 +00003863 // Don't parse attributes unless we have an identifier.
John McCall7f040a92010-12-24 02:08:15 +00003864 if (D.getIdentifier())
3865 MaybeParseCXX0XAttributes(D);
Sean Huntbbd37c62009-11-21 08:43:09 +00003866
Reid Spencer5f016e22007-07-11 17:01:13 +00003867 while (1) {
Chris Lattner04d66662007-10-09 17:33:22 +00003868 if (Tok.is(tok::l_paren)) {
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00003869 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
3870 // In such a case, check if we actually have a function declarator; if it
3871 // is not, the declarator has been fully parsed.
Chris Lattner7399ee02008-10-20 02:05:46 +00003872 if (getLang().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
3873 // When not in file scope, warn for ambiguous function declarators, just
3874 // in case the author intended it as a variable definition.
3875 bool warnIfAmbiguous = D.getContext() != Declarator::FileContext;
3876 if (!isCXXFunctionDeclarator(warnIfAmbiguous))
3877 break;
3878 }
John McCall0b7e6782011-03-24 11:26:52 +00003879 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00003880 ParseFunctionDeclarator(ConsumeParen(), D, attrs);
Chris Lattner04d66662007-10-09 17:33:22 +00003881 } else if (Tok.is(tok::l_square)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003882 ParseBracketDeclarator(D);
3883 } else {
3884 break;
3885 }
3886 }
3887}
3888
Chris Lattneref4715c2008-04-06 05:45:57 +00003889/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
3890/// only called before the identifier, so these are most likely just grouping
Mike Stump1eb44332009-09-09 15:08:12 +00003891/// parens for precedence. If we find that these are actually function
Chris Lattneref4715c2008-04-06 05:45:57 +00003892/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
3893///
3894/// direct-declarator:
3895/// '(' declarator ')'
3896/// [GNU] '(' attributes declarator ')'
Chris Lattner7399ee02008-10-20 02:05:46 +00003897/// direct-declarator '(' parameter-type-list ')'
3898/// direct-declarator '(' identifier-list[opt] ')'
3899/// [GNU] direct-declarator '(' parameter-forward-declarations
3900/// parameter-type-list[opt] ')'
Chris Lattneref4715c2008-04-06 05:45:57 +00003901///
3902void Parser::ParseParenDeclarator(Declarator &D) {
3903 SourceLocation StartLoc = ConsumeParen();
3904 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
Mike Stump1eb44332009-09-09 15:08:12 +00003905
Chris Lattner7399ee02008-10-20 02:05:46 +00003906 // Eat any attributes before we look at whether this is a grouping or function
3907 // declarator paren. If this is a grouping paren, the attribute applies to
3908 // the type being built up, for example:
3909 // int (__attribute__(()) *x)(long y)
3910 // If this ends up not being a grouping paren, the attribute applies to the
3911 // first argument, for example:
3912 // int (__attribute__(()) int x)
3913 // In either case, we need to eat any attributes to be able to determine what
3914 // sort of paren this is.
3915 //
John McCall0b7e6782011-03-24 11:26:52 +00003916 ParsedAttributes attrs(AttrFactory);
Chris Lattner7399ee02008-10-20 02:05:46 +00003917 bool RequiresArg = false;
3918 if (Tok.is(tok::kw___attribute)) {
John McCall7f040a92010-12-24 02:08:15 +00003919 ParseGNUAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00003920
Chris Lattner7399ee02008-10-20 02:05:46 +00003921 // We require that the argument list (if this is a non-grouping paren) be
3922 // present even if the attribute list was empty.
3923 RequiresArg = true;
3924 }
Steve Naroff239f0732008-12-25 14:16:32 +00003925 // Eat any Microsoft extensions.
Eli Friedman290eeb02009-06-08 23:27:34 +00003926 if (Tok.is(tok::kw___cdecl) || Tok.is(tok::kw___stdcall) ||
Douglas Gregorf813a2c2010-05-18 16:57:00 +00003927 Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___fastcall) ||
Francois Pichet3bd9aa42011-08-18 09:59:55 +00003928 Tok.is(tok::kw___w64) || Tok.is(tok::kw___ptr64) ||
Francois Pichet58fd97a2011-08-25 00:36:46 +00003929 Tok.is(tok::kw___ptr32) || Tok.is(tok::kw___unaligned)) {
John McCall7f040a92010-12-24 02:08:15 +00003930 ParseMicrosoftTypeAttributes(attrs);
Eli Friedman290eeb02009-06-08 23:27:34 +00003931 }
Dawn Perchik52fc3142010-09-03 01:29:35 +00003932 // Eat any Borland extensions.
Ted Kremenek8113ecf2010-11-10 05:59:39 +00003933 if (Tok.is(tok::kw___pascal))
John McCall7f040a92010-12-24 02:08:15 +00003934 ParseBorlandTypeAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00003935
Chris Lattneref4715c2008-04-06 05:45:57 +00003936 // If we haven't past the identifier yet (or where the identifier would be
3937 // stored, if this is an abstract declarator), then this is probably just
3938 // grouping parens. However, if this could be an abstract-declarator, then
3939 // this could also be the start of function arguments (consider 'void()').
3940 bool isGrouping;
Mike Stump1eb44332009-09-09 15:08:12 +00003941
Chris Lattneref4715c2008-04-06 05:45:57 +00003942 if (!D.mayOmitIdentifier()) {
3943 // If this can't be an abstract-declarator, this *must* be a grouping
3944 // paren, because we haven't seen the identifier yet.
3945 isGrouping = true;
3946 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Argyrios Kyrtzidise25d2702008-10-06 00:07:55 +00003947 (getLang().CPlusPlus && Tok.is(tok::ellipsis)) || // C++ int(...)
Chris Lattneref4715c2008-04-06 05:45:57 +00003948 isDeclarationSpecifier()) { // 'int(int)' is a function.
3949 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
3950 // considered to be a type, not a K&R identifier-list.
3951 isGrouping = false;
3952 } else {
3953 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
3954 isGrouping = true;
3955 }
Mike Stump1eb44332009-09-09 15:08:12 +00003956
Chris Lattneref4715c2008-04-06 05:45:57 +00003957 // If this is a grouping paren, handle:
3958 // direct-declarator: '(' declarator ')'
3959 // direct-declarator: '(' attributes declarator ')'
3960 if (isGrouping) {
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00003961 bool hadGroupingParens = D.hasGroupingParens();
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00003962 D.setGroupingParens(true);
3963
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003964 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattneref4715c2008-04-06 05:45:57 +00003965 // Match the ')'.
Abramo Bagnara075f8f12010-12-10 16:29:40 +00003966 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_paren, StartLoc);
John McCall0b7e6782011-03-24 11:26:52 +00003967 D.AddTypeInfo(DeclaratorChunk::getParen(StartLoc, EndLoc),
3968 attrs, EndLoc);
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00003969
3970 D.setGroupingParens(hadGroupingParens);
Chris Lattneref4715c2008-04-06 05:45:57 +00003971 return;
3972 }
Mike Stump1eb44332009-09-09 15:08:12 +00003973
Chris Lattneref4715c2008-04-06 05:45:57 +00003974 // Okay, if this wasn't a grouping paren, it must be the start of a function
3975 // argument list. Recognize that this declarator will never have an
Chris Lattner7399ee02008-10-20 02:05:46 +00003976 // identifier (and remember where it would have been), then call into
3977 // ParseFunctionDeclarator to handle of argument list.
Chris Lattneref4715c2008-04-06 05:45:57 +00003978 D.SetIdentifier(0, Tok.getLocation());
3979
John McCall7f040a92010-12-24 02:08:15 +00003980 ParseFunctionDeclarator(StartLoc, D, attrs, RequiresArg);
Chris Lattneref4715c2008-04-06 05:45:57 +00003981}
3982
3983/// ParseFunctionDeclarator - We are after the identifier and have parsed the
3984/// declarator D up to a paren, which indicates that we are parsing function
3985/// arguments.
Reid Spencer5f016e22007-07-11 17:01:13 +00003986///
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00003987/// If attrs is non-null, then the caller parsed those arguments immediately
Chris Lattner7399ee02008-10-20 02:05:46 +00003988/// after the open paren - they should be considered to be the first argument of
3989/// a parameter. If RequiresArg is true, then the first argument of the
3990/// function is required to be present and required to not be an identifier
3991/// list.
3992///
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00003993/// For C++, after the parameter-list, it also parses cv-qualifier-seq[opt],
3994/// (C++0x) ref-qualifier[opt], exception-specification[opt], and
3995/// (C++0x) trailing-return-type[opt].
3996///
3997/// [C++0x] exception-specification:
3998/// dynamic-exception-specification
3999/// noexcept-specification
4000///
4001void Parser::ParseFunctionDeclarator(SourceLocation LParenLoc, Declarator &D,
4002 ParsedAttributes &attrs,
4003 bool RequiresArg) {
4004 // lparen is already consumed!
4005 assert(D.isPastIdentifier() && "Should not call before identifier!");
4006
4007 // This should be true when the function has typed arguments.
4008 // Otherwise, it is treated as a K&R-style function.
4009 bool HasProto = false;
4010 // Build up an array of information about the parsed arguments.
Chris Lattner5f9e2722011-07-23 10:55:15 +00004011 SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004012 // Remember where we see an ellipsis, if any.
4013 SourceLocation EllipsisLoc;
4014
4015 DeclSpec DS(AttrFactory);
4016 bool RefQualifierIsLValueRef = true;
4017 SourceLocation RefQualifierLoc;
4018 ExceptionSpecificationType ESpecType = EST_None;
4019 SourceRange ESpecRange;
Chris Lattner5f9e2722011-07-23 10:55:15 +00004020 SmallVector<ParsedType, 2> DynamicExceptions;
4021 SmallVector<SourceRange, 2> DynamicExceptionRanges;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004022 ExprResult NoexceptExpr;
4023 ParsedType TrailingReturnType;
4024
4025 SourceLocation EndLoc;
4026
4027 if (isFunctionDeclaratorIdentifierList()) {
4028 if (RequiresArg)
4029 Diag(Tok, diag::err_argument_required_after_attribute);
4030
4031 ParseFunctionDeclaratorIdentifierList(D, ParamInfo);
4032
4033 EndLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
4034 } else {
4035 // Enter function-declaration scope, limiting any declarators to the
4036 // function prototype scope, including parameter declarators.
4037 ParseScope PrototypeScope(this,
4038 Scope::FunctionPrototypeScope|Scope::DeclScope);
4039
4040 if (Tok.isNot(tok::r_paren))
4041 ParseParameterDeclarationClause(D, attrs, ParamInfo, EllipsisLoc);
4042 else if (RequiresArg)
4043 Diag(Tok, diag::err_argument_required_after_attribute);
4044
4045 HasProto = ParamInfo.size() || getLang().CPlusPlus;
4046
4047 // If we have the closing ')', eat it.
4048 EndLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
4049
4050 if (getLang().CPlusPlus) {
4051 MaybeParseCXX0XAttributes(attrs);
4052
4053 // Parse cv-qualifier-seq[opt].
4054 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
4055 if (!DS.getSourceRange().getEnd().isInvalid())
4056 EndLoc = DS.getSourceRange().getEnd();
4057
4058 // Parse ref-qualifier[opt].
4059 if (Tok.is(tok::amp) || Tok.is(tok::ampamp)) {
4060 if (!getLang().CPlusPlus0x)
4061 Diag(Tok, diag::ext_ref_qualifier);
4062
4063 RefQualifierIsLValueRef = Tok.is(tok::amp);
4064 RefQualifierLoc = ConsumeToken();
4065 EndLoc = RefQualifierLoc;
4066 }
4067
4068 // Parse exception-specification[opt].
4069 ESpecType = MaybeParseExceptionSpecification(ESpecRange,
4070 DynamicExceptions,
4071 DynamicExceptionRanges,
4072 NoexceptExpr);
4073 if (ESpecType != EST_None)
4074 EndLoc = ESpecRange.getEnd();
4075
4076 // Parse trailing-return-type[opt].
4077 if (getLang().CPlusPlus0x && Tok.is(tok::arrow)) {
Douglas Gregorae7902c2011-08-04 15:30:47 +00004078 SourceRange Range;
4079 TrailingReturnType = ParseTrailingReturnType(Range).get();
4080 if (Range.getEnd().isValid())
4081 EndLoc = Range.getEnd();
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004082 }
4083 }
4084
4085 // Leave prototype scope.
4086 PrototypeScope.Exit();
4087 }
4088
4089 // Remember that we parsed a function type, and remember the attributes.
4090 D.AddTypeInfo(DeclaratorChunk::getFunction(HasProto,
4091 /*isVariadic=*/EllipsisLoc.isValid(),
4092 EllipsisLoc,
4093 ParamInfo.data(), ParamInfo.size(),
4094 DS.getTypeQualifiers(),
4095 RefQualifierIsLValueRef,
4096 RefQualifierLoc,
Douglas Gregor90ebed02011-07-13 21:47:47 +00004097 /*MutableLoc=*/SourceLocation(),
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004098 ESpecType, ESpecRange.getBegin(),
4099 DynamicExceptions.data(),
4100 DynamicExceptionRanges.data(),
4101 DynamicExceptions.size(),
4102 NoexceptExpr.isUsable() ?
4103 NoexceptExpr.get() : 0,
4104 LParenLoc, EndLoc, D,
4105 TrailingReturnType),
4106 attrs, EndLoc);
4107}
4108
4109/// isFunctionDeclaratorIdentifierList - This parameter list may have an
4110/// identifier list form for a K&R-style function: void foo(a,b,c)
4111///
4112/// Note that identifier-lists are only allowed for normal declarators, not for
4113/// abstract-declarators.
4114bool Parser::isFunctionDeclaratorIdentifierList() {
4115 return !getLang().CPlusPlus
4116 && Tok.is(tok::identifier)
4117 && !TryAltiVecVectorToken()
4118 // K&R identifier lists can't have typedefs as identifiers, per C99
4119 // 6.7.5.3p11.
4120 && (TryAnnotateTypeOrScopeToken() || !Tok.is(tok::annot_typename))
4121 // Identifier lists follow a really simple grammar: the identifiers can
4122 // be followed *only* by a ", identifier" or ")". However, K&R
4123 // identifier lists are really rare in the brave new modern world, and
4124 // it is very common for someone to typo a type in a non-K&R style
4125 // list. If we are presented with something like: "void foo(intptr x,
4126 // float y)", we don't want to start parsing the function declarator as
4127 // though it is a K&R style declarator just because intptr is an
4128 // invalid type.
4129 //
4130 // To handle this, we check to see if the token after the first
4131 // identifier is a "," or ")". Only then do we parse it as an
4132 // identifier list.
4133 && (NextToken().is(tok::comma) || NextToken().is(tok::r_paren));
4134}
4135
4136/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
4137/// we found a K&R-style identifier list instead of a typed parameter list.
4138///
4139/// After returning, ParamInfo will hold the parsed parameters.
4140///
4141/// identifier-list: [C99 6.7.5]
4142/// identifier
4143/// identifier-list ',' identifier
4144///
4145void Parser::ParseFunctionDeclaratorIdentifierList(
4146 Declarator &D,
Chris Lattner5f9e2722011-07-23 10:55:15 +00004147 SmallVector<DeclaratorChunk::ParamInfo, 16> &ParamInfo) {
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004148 // If there was no identifier specified for the declarator, either we are in
4149 // an abstract-declarator, or we are in a parameter declarator which was found
4150 // to be abstract. In abstract-declarators, identifier lists are not valid:
4151 // diagnose this.
4152 if (!D.getIdentifier())
4153 Diag(Tok, diag::ext_ident_list_in_param);
4154
4155 // Maintain an efficient lookup of params we have seen so far.
4156 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
4157
4158 while (1) {
4159 // If this isn't an identifier, report the error and skip until ')'.
4160 if (Tok.isNot(tok::identifier)) {
4161 Diag(Tok, diag::err_expected_ident);
4162 SkipUntil(tok::r_paren, /*StopAtSemi=*/true, /*DontConsume=*/true);
4163 // Forget we parsed anything.
4164 ParamInfo.clear();
4165 return;
4166 }
4167
4168 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
4169
4170 // Reject 'typedef int y; int test(x, y)', but continue parsing.
4171 if (Actions.getTypeName(*ParmII, Tok.getLocation(), getCurScope()))
4172 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
4173
4174 // Verify that the argument identifier has not already been mentioned.
4175 if (!ParamsSoFar.insert(ParmII)) {
4176 Diag(Tok, diag::err_param_redefinition) << ParmII;
4177 } else {
4178 // Remember this identifier in ParamInfo.
4179 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
4180 Tok.getLocation(),
4181 0));
4182 }
4183
4184 // Eat the identifier.
4185 ConsumeToken();
4186
4187 // The list continues if we see a comma.
4188 if (Tok.isNot(tok::comma))
4189 break;
4190 ConsumeToken();
4191 }
4192}
4193
4194/// ParseParameterDeclarationClause - Parse a (possibly empty) parameter-list
4195/// after the opening parenthesis. This function will not parse a K&R-style
4196/// identifier list.
4197///
4198/// D is the declarator being parsed. If attrs is non-null, then the caller
4199/// parsed those arguments immediately after the open paren - they should be
4200/// considered to be the first argument of a parameter.
4201///
4202/// After returning, ParamInfo will hold the parsed parameters. EllipsisLoc will
4203/// be the location of the ellipsis, if any was parsed.
4204///
Reid Spencer5f016e22007-07-11 17:01:13 +00004205/// parameter-type-list: [C99 6.7.5]
4206/// parameter-list
4207/// parameter-list ',' '...'
Douglas Gregored5d6512009-09-22 21:41:40 +00004208/// [C++] parameter-list '...'
Reid Spencer5f016e22007-07-11 17:01:13 +00004209///
4210/// parameter-list: [C99 6.7.5]
4211/// parameter-declaration
4212/// parameter-list ',' parameter-declaration
4213///
4214/// parameter-declaration: [C99 6.7.5]
4215/// declaration-specifiers declarator
Chris Lattner04421082008-04-08 04:40:51 +00004216/// [C++] declaration-specifiers declarator '=' assignment-expression
Reid Spencer5f016e22007-07-11 17:01:13 +00004217/// [GNU] declaration-specifiers declarator attributes
Sebastian Redl50de12f2009-03-24 22:27:57 +00004218/// declaration-specifiers abstract-declarator[opt]
4219/// [C++] declaration-specifiers abstract-declarator[opt]
Chris Lattner8123a952008-04-10 02:22:51 +00004220/// '=' assignment-expression
Reid Spencer5f016e22007-07-11 17:01:13 +00004221/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
4222///
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004223void Parser::ParseParameterDeclarationClause(
4224 Declarator &D,
4225 ParsedAttributes &attrs,
Chris Lattner5f9e2722011-07-23 10:55:15 +00004226 SmallVector<DeclaratorChunk::ParamInfo, 16> &ParamInfo,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004227 SourceLocation &EllipsisLoc) {
Mike Stump1eb44332009-09-09 15:08:12 +00004228
Chris Lattnerf97409f2008-04-06 06:57:35 +00004229 while (1) {
4230 if (Tok.is(tok::ellipsis)) {
Douglas Gregor965acbb2009-02-18 07:07:28 +00004231 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chris Lattnerf97409f2008-04-06 06:57:35 +00004232 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00004233 }
Mike Stump1eb44332009-09-09 15:08:12 +00004234
Chris Lattnerf97409f2008-04-06 06:57:35 +00004235 // Parse the declaration-specifiers.
John McCall54abf7d2009-11-04 02:18:39 +00004236 // Just use the ParsingDeclaration "scope" of the declarator.
John McCall0b7e6782011-03-24 11:26:52 +00004237 DeclSpec DS(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00004238
4239 // Skip any Microsoft attributes before a param.
Francois Pichet62ec1f22011-09-17 17:15:52 +00004240 if (getLang().MicrosoftExt && Tok.is(tok::l_square))
John McCall7f040a92010-12-24 02:08:15 +00004241 ParseMicrosoftAttributes(DS.getAttributes());
4242
4243 SourceLocation DSStart = Tok.getLocation();
Chris Lattner7399ee02008-10-20 02:05:46 +00004244
4245 // If the caller parsed attributes for the first argument, add them now.
John McCall7f040a92010-12-24 02:08:15 +00004246 // Take them so that we only apply the attributes to the first parameter.
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004247 // FIXME: If we saw an ellipsis first, this code is not reached. Are the
4248 // attributes lost? Should they even be allowed?
4249 // FIXME: If we can leave the attributes in the token stream somehow, we can
4250 // get rid of a parameter (attrs) and this statement. It might be too much
4251 // hassle.
John McCall7f040a92010-12-24 02:08:15 +00004252 DS.takeAttributesFrom(attrs);
4253
Chris Lattnere64c5492009-02-27 18:38:20 +00004254 ParseDeclarationSpecifiers(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00004255
Chris Lattnerf97409f2008-04-06 06:57:35 +00004256 // Parse the declarator. This is "PrototypeContext", because we must
4257 // accept either 'declarator' or 'abstract-declarator' here.
4258 Declarator ParmDecl(DS, Declarator::PrototypeContext);
4259 ParseDeclarator(ParmDecl);
4260
4261 // Parse GNU attributes, if present.
John McCall7f040a92010-12-24 02:08:15 +00004262 MaybeParseGNUAttributes(ParmDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00004263
Chris Lattnerf97409f2008-04-06 06:57:35 +00004264 // Remember this parsed parameter in ParamInfo.
4265 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
Mike Stump1eb44332009-09-09 15:08:12 +00004266
Douglas Gregor72b505b2008-12-16 21:30:33 +00004267 // DefArgToks is used when the parsing of default arguments needs
4268 // to be delayed.
4269 CachedTokens *DefArgToks = 0;
4270
Chris Lattnerf97409f2008-04-06 06:57:35 +00004271 // If no parameter was specified, verify that *something* was specified,
4272 // otherwise we have a missing type and identifier.
Chris Lattnere64c5492009-02-27 18:38:20 +00004273 if (DS.isEmpty() && ParmDecl.getIdentifier() == 0 &&
4274 ParmDecl.getNumTypeObjects() == 0) {
Chris Lattnerf97409f2008-04-06 06:57:35 +00004275 // Completely missing, emit error.
4276 Diag(DSStart, diag::err_missing_param);
4277 } else {
4278 // Otherwise, we have something. Add it and let semantic analysis try
4279 // to grok it and add the result to the ParamInfo we are building.
Mike Stump1eb44332009-09-09 15:08:12 +00004280
Chris Lattnerf97409f2008-04-06 06:57:35 +00004281 // Inform the actions module about the parameter declarator, so it gets
4282 // added to the current scope.
John McCalld226f652010-08-21 09:40:31 +00004283 Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl);
Chris Lattner04421082008-04-08 04:40:51 +00004284
4285 // Parse the default argument, if any. We parse the default
4286 // arguments in all dialects; the semantic analysis in
4287 // ActOnParamDefaultArgument will reject the default argument in
4288 // C.
4289 if (Tok.is(tok::equal)) {
Douglas Gregor61366e92008-12-24 00:01:03 +00004290 SourceLocation EqualLoc = Tok.getLocation();
4291
Chris Lattner04421082008-04-08 04:40:51 +00004292 // Parse the default argument
Douglas Gregor72b505b2008-12-16 21:30:33 +00004293 if (D.getContext() == Declarator::MemberContext) {
4294 // If we're inside a class definition, cache the tokens
4295 // corresponding to the default argument. We'll actually parse
4296 // them when we see the end of the class definition.
4297 // FIXME: Templates will require something similar.
4298 // FIXME: Can we use a smart pointer for Toks?
4299 DefArgToks = new CachedTokens;
4300
Mike Stump1eb44332009-09-09 15:08:12 +00004301 if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks,
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +00004302 /*StopAtSemi=*/true,
4303 /*ConsumeFinalToken=*/false)) {
Douglas Gregor72b505b2008-12-16 21:30:33 +00004304 delete DefArgToks;
4305 DefArgToks = 0;
Douglas Gregor61366e92008-12-24 00:01:03 +00004306 Actions.ActOnParamDefaultArgumentError(Param);
Argyrios Kyrtzidis2b602ad2010-08-06 09:47:24 +00004307 } else {
4308 // Mark the end of the default argument so that we know when to
4309 // stop when we parse it later on.
4310 Token DefArgEnd;
4311 DefArgEnd.startToken();
4312 DefArgEnd.setKind(tok::cxx_defaultarg_end);
4313 DefArgEnd.setLocation(Tok.getLocation());
4314 DefArgToks->push_back(DefArgEnd);
Mike Stump1eb44332009-09-09 15:08:12 +00004315 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc,
Anders Carlsson5e300d12009-06-12 16:51:40 +00004316 (*DefArgToks)[1].getLocation());
Argyrios Kyrtzidis2b602ad2010-08-06 09:47:24 +00004317 }
Chris Lattner04421082008-04-08 04:40:51 +00004318 } else {
Douglas Gregor72b505b2008-12-16 21:30:33 +00004319 // Consume the '='.
Douglas Gregor61366e92008-12-24 00:01:03 +00004320 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00004321
Douglas Gregorbe0f7bd2010-09-11 20:24:53 +00004322 // The argument isn't actually potentially evaluated unless it is
4323 // used.
4324 EnterExpressionEvaluationContext Eval(Actions,
4325 Sema::PotentiallyEvaluatedIfUsed);
4326
John McCall60d7b3a2010-08-24 06:29:42 +00004327 ExprResult DefArgResult(ParseAssignmentExpression());
Douglas Gregor72b505b2008-12-16 21:30:33 +00004328 if (DefArgResult.isInvalid()) {
4329 Actions.ActOnParamDefaultArgumentError(Param);
4330 SkipUntil(tok::comma, tok::r_paren, true, true);
4331 } else {
4332 // Inform the actions module about the default argument
4333 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
John McCall9ae2f072010-08-23 23:25:46 +00004334 DefArgResult.take());
Douglas Gregor72b505b2008-12-16 21:30:33 +00004335 }
Chris Lattner04421082008-04-08 04:40:51 +00004336 }
4337 }
Mike Stump1eb44332009-09-09 15:08:12 +00004338
4339 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
4340 ParmDecl.getIdentifierLoc(), Param,
Douglas Gregor72b505b2008-12-16 21:30:33 +00004341 DefArgToks));
Chris Lattnerf97409f2008-04-06 06:57:35 +00004342 }
4343
4344 // If the next token is a comma, consume it and keep reading arguments.
Douglas Gregored5d6512009-09-22 21:41:40 +00004345 if (Tok.isNot(tok::comma)) {
4346 if (Tok.is(tok::ellipsis)) {
Douglas Gregored5d6512009-09-22 21:41:40 +00004347 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
4348
4349 if (!getLang().CPlusPlus) {
4350 // We have ellipsis without a preceding ',', which is ill-formed
4351 // in C. Complain and provide the fix.
4352 Diag(EllipsisLoc, diag::err_missing_comma_before_ellipsis)
Douglas Gregor849b2432010-03-31 17:46:05 +00004353 << FixItHint::CreateInsertion(EllipsisLoc, ", ");
Douglas Gregored5d6512009-09-22 21:41:40 +00004354 }
4355 }
4356
4357 break;
4358 }
Mike Stump1eb44332009-09-09 15:08:12 +00004359
Chris Lattnerf97409f2008-04-06 06:57:35 +00004360 // Consume the comma.
4361 ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00004362 }
Mike Stump1eb44332009-09-09 15:08:12 +00004363
Chris Lattner66d28652008-04-06 06:34:08 +00004364}
Chris Lattneref4715c2008-04-06 05:45:57 +00004365
Reid Spencer5f016e22007-07-11 17:01:13 +00004366/// [C90] direct-declarator '[' constant-expression[opt] ']'
4367/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
4368/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
4369/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
4370/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
4371void Parser::ParseBracketDeclarator(Declarator &D) {
4372 SourceLocation StartLoc = ConsumeBracket();
Mike Stump1eb44332009-09-09 15:08:12 +00004373
Chris Lattner378c7e42008-12-18 07:27:21 +00004374 // C array syntax has many features, but by-far the most common is [] and [4].
4375 // This code does a fast path to handle some of the most obvious cases.
4376 if (Tok.getKind() == tok::r_square) {
Sebastian Redlab197ba2009-02-09 18:23:29 +00004377 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
John McCall0b7e6782011-03-24 11:26:52 +00004378 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00004379 MaybeParseCXX0XAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00004380
Chris Lattner378c7e42008-12-18 07:27:21 +00004381 // Remember that we parsed the empty array type.
John McCall60d7b3a2010-08-24 06:29:42 +00004382 ExprResult NumElements;
John McCall0b7e6782011-03-24 11:26:52 +00004383 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00004384 StartLoc, EndLoc),
John McCall0b7e6782011-03-24 11:26:52 +00004385 attrs, EndLoc);
Chris Lattner378c7e42008-12-18 07:27:21 +00004386 return;
4387 } else if (Tok.getKind() == tok::numeric_constant &&
4388 GetLookAheadToken(1).is(tok::r_square)) {
4389 // [4] is very common. Parse the numeric constant expression.
John McCall60d7b3a2010-08-24 06:29:42 +00004390 ExprResult ExprRes(Actions.ActOnNumericConstant(Tok));
Chris Lattner378c7e42008-12-18 07:27:21 +00004391 ConsumeToken();
4392
Sebastian Redlab197ba2009-02-09 18:23:29 +00004393 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
John McCall0b7e6782011-03-24 11:26:52 +00004394 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00004395 MaybeParseCXX0XAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00004396
Chris Lattner378c7e42008-12-18 07:27:21 +00004397 // Remember that we parsed a array type, and remember its features.
John McCall0b7e6782011-03-24 11:26:52 +00004398 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, 0,
John McCall7f040a92010-12-24 02:08:15 +00004399 ExprRes.release(),
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00004400 StartLoc, EndLoc),
John McCall0b7e6782011-03-24 11:26:52 +00004401 attrs, EndLoc);
Chris Lattner378c7e42008-12-18 07:27:21 +00004402 return;
4403 }
Mike Stump1eb44332009-09-09 15:08:12 +00004404
Reid Spencer5f016e22007-07-11 17:01:13 +00004405 // If valid, this location is the position where we read the 'static' keyword.
4406 SourceLocation StaticLoc;
Chris Lattner04d66662007-10-09 17:33:22 +00004407 if (Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00004408 StaticLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00004409
Reid Spencer5f016e22007-07-11 17:01:13 +00004410 // If there is a type-qualifier-list, read it now.
Chris Lattnera1fcbad2008-12-18 06:50:14 +00004411 // Type qualifiers in an array subscript are a C99 feature.
John McCall0b7e6782011-03-24 11:26:52 +00004412 DeclSpec DS(AttrFactory);
Chris Lattner5a69d1c2008-12-18 07:02:59 +00004413 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Mike Stump1eb44332009-09-09 15:08:12 +00004414
Reid Spencer5f016e22007-07-11 17:01:13 +00004415 // If we haven't already read 'static', check to see if there is one after the
4416 // type-qualifier-list.
Chris Lattner04d66662007-10-09 17:33:22 +00004417 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00004418 StaticLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00004419
Reid Spencer5f016e22007-07-11 17:01:13 +00004420 // Handle "direct-declarator [ type-qual-list[opt] * ]".
4421 bool isStar = false;
John McCall60d7b3a2010-08-24 06:29:42 +00004422 ExprResult NumElements;
Mike Stump1eb44332009-09-09 15:08:12 +00004423
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00004424 // Handle the case where we have '[*]' as the array size. However, a leading
4425 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
4426 // the the token after the star is a ']'. Since stars in arrays are
4427 // infrequent, use of lookahead is not costly here.
4428 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattnera711dd02008-04-06 05:27:21 +00004429 ConsumeToken(); // Eat the '*'.
Reid Spencer5f016e22007-07-11 17:01:13 +00004430
Chris Lattnera1fcbad2008-12-18 06:50:14 +00004431 if (StaticLoc.isValid()) {
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00004432 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattnera1fcbad2008-12-18 06:50:14 +00004433 StaticLoc = SourceLocation(); // Drop the static.
4434 }
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00004435 isStar = true;
Chris Lattner04d66662007-10-09 17:33:22 +00004436 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner378c7e42008-12-18 07:27:21 +00004437 // Note, in C89, this production uses the constant-expr production instead
4438 // of assignment-expr. The only difference is that assignment-expr allows
4439 // things like '=' and '*='. Sema rejects these in C89 mode because they
4440 // are not i-c-e's, so we don't need to distinguish between the two here.
Mike Stump1eb44332009-09-09 15:08:12 +00004441
Douglas Gregore0762c92009-06-19 23:52:42 +00004442 // Parse the constant-expression or assignment-expression now (depending
4443 // on dialect).
4444 if (getLang().CPlusPlus)
4445 NumElements = ParseConstantExpression();
4446 else
4447 NumElements = ParseAssignmentExpression();
Reid Spencer5f016e22007-07-11 17:01:13 +00004448 }
Mike Stump1eb44332009-09-09 15:08:12 +00004449
Reid Spencer5f016e22007-07-11 17:01:13 +00004450 // If there was an error parsing the assignment-expression, recover.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00004451 if (NumElements.isInvalid()) {
Chris Lattner5cb10d32009-04-24 22:30:50 +00004452 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00004453 // If the expression was invalid, skip it.
4454 SkipUntil(tok::r_square);
4455 return;
4456 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00004457
4458 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
4459
John McCall0b7e6782011-03-24 11:26:52 +00004460 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00004461 MaybeParseCXX0XAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00004462
Chris Lattner378c7e42008-12-18 07:27:21 +00004463 // Remember that we parsed a array type, and remember its features.
John McCall0b7e6782011-03-24 11:26:52 +00004464 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
Reid Spencer5f016e22007-07-11 17:01:13 +00004465 StaticLoc.isValid(), isStar,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00004466 NumElements.release(),
4467 StartLoc, EndLoc),
John McCall0b7e6782011-03-24 11:26:52 +00004468 attrs, EndLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00004469}
4470
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004471/// [GNU] typeof-specifier:
4472/// typeof ( expressions )
4473/// typeof ( type-name )
4474/// [GNU/C++] typeof unary-expression
Steve Naroffd1861fd2007-07-31 12:34:36 +00004475///
4476void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner04d66662007-10-09 17:33:22 +00004477 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004478 Token OpTok = Tok;
Steve Naroffd1861fd2007-07-31 12:34:36 +00004479 SourceLocation StartLoc = ConsumeToken();
4480
John McCallcfb708c2010-01-13 20:03:27 +00004481 const bool hasParens = Tok.is(tok::l_paren);
4482
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004483 bool isCastExpr;
John McCallb3d87482010-08-24 05:47:05 +00004484 ParsedType CastTy;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004485 SourceRange CastRange;
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00004486 ExprResult Operand = ParseExprAfterUnaryExprOrTypeTrait(OpTok, isCastExpr,
4487 CastTy, CastRange);
John McCallcfb708c2010-01-13 20:03:27 +00004488 if (hasParens)
4489 DS.setTypeofParensRange(CastRange);
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004490
4491 if (CastRange.getEnd().isInvalid())
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004492 // FIXME: Not accurate, the range gets one token more than it should.
4493 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004494 else
4495 DS.SetRangeEnd(CastRange.getEnd());
Mike Stump1eb44332009-09-09 15:08:12 +00004496
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004497 if (isCastExpr) {
4498 if (!CastTy) {
4499 DS.SetTypeSpecError();
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004500 return;
Douglas Gregor809070a2009-02-18 17:45:20 +00004501 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004502
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004503 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00004504 unsigned DiagID;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004505 // Check for duplicate type specifiers (e.g. "int typeof(int)").
4506 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00004507 DiagID, CastTy))
4508 Diag(StartLoc, DiagID) << PrevSpec;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004509 return;
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004510 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004511
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004512 // If we get here, the operand to the typeof was an expresion.
4513 if (Operand.isInvalid()) {
4514 DS.SetTypeSpecError();
Steve Naroff9dfa7b42007-08-02 02:53:48 +00004515 return;
Steve Naroffd1861fd2007-07-31 12:34:36 +00004516 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004517
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004518 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00004519 unsigned DiagID;
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004520 // Check for duplicate type specifiers (e.g. "int typeof(int)").
4521 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
John McCallb3d87482010-08-24 05:47:05 +00004522 DiagID, Operand.get()))
John McCallfec54012009-08-03 20:12:06 +00004523 Diag(StartLoc, DiagID) << PrevSpec;
Steve Naroffd1861fd2007-07-31 12:34:36 +00004524}
Chris Lattner1b492422010-02-28 18:33:55 +00004525
Eli Friedmanb001de72011-10-06 23:00:33 +00004526/// [C1X] atomic-specifier:
4527/// _Atomic ( type-name )
4528///
4529void Parser::ParseAtomicSpecifier(DeclSpec &DS) {
4530 assert(Tok.is(tok::kw__Atomic) && "Not an atomic specifier");
4531
4532 SourceLocation StartLoc = ConsumeToken();
4533 SourceLocation LParenLoc = Tok.getLocation();
4534
4535 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
4536 "_Atomic")) {
4537 SkipUntil(tok::r_paren);
4538 return;
4539 }
4540
4541 TypeResult Result = ParseTypeName();
4542 if (Result.isInvalid()) {
4543 SkipUntil(tok::r_paren);
4544 return;
4545 }
4546
4547 // Match the ')'
4548 SourceLocation RParenLoc;
4549 if (Tok.is(tok::r_paren))
4550 RParenLoc = ConsumeParen();
4551 else
4552 MatchRHSPunctuation(tok::r_paren, LParenLoc);
4553
4554 if (RParenLoc.isInvalid())
4555 return;
4556
4557 DS.setTypeofParensRange(SourceRange(LParenLoc, RParenLoc));
4558 DS.SetRangeEnd(RParenLoc);
4559
4560 const char *PrevSpec = 0;
4561 unsigned DiagID;
4562 if (DS.SetTypeSpecType(DeclSpec::TST_atomic, StartLoc, PrevSpec,
4563 DiagID, Result.release()))
4564 Diag(StartLoc, DiagID) << PrevSpec;
4565}
4566
Chris Lattner1b492422010-02-28 18:33:55 +00004567
4568/// TryAltiVecVectorTokenOutOfLine - Out of line body that should only be called
4569/// from TryAltiVecVectorToken.
4570bool Parser::TryAltiVecVectorTokenOutOfLine() {
4571 Token Next = NextToken();
4572 switch (Next.getKind()) {
4573 default: return false;
4574 case tok::kw_short:
4575 case tok::kw_long:
4576 case tok::kw_signed:
4577 case tok::kw_unsigned:
4578 case tok::kw_void:
4579 case tok::kw_char:
4580 case tok::kw_int:
4581 case tok::kw_float:
4582 case tok::kw_double:
4583 case tok::kw_bool:
4584 case tok::kw___pixel:
4585 Tok.setKind(tok::kw___vector);
4586 return true;
4587 case tok::identifier:
4588 if (Next.getIdentifierInfo() == Ident_pixel) {
4589 Tok.setKind(tok::kw___vector);
4590 return true;
4591 }
4592 return false;
4593 }
4594}
4595
4596bool Parser::TryAltiVecTokenOutOfLine(DeclSpec &DS, SourceLocation Loc,
4597 const char *&PrevSpec, unsigned &DiagID,
4598 bool &isInvalid) {
4599 if (Tok.getIdentifierInfo() == Ident_vector) {
4600 Token Next = NextToken();
4601 switch (Next.getKind()) {
4602 case tok::kw_short:
4603 case tok::kw_long:
4604 case tok::kw_signed:
4605 case tok::kw_unsigned:
4606 case tok::kw_void:
4607 case tok::kw_char:
4608 case tok::kw_int:
4609 case tok::kw_float:
4610 case tok::kw_double:
4611 case tok::kw_bool:
4612 case tok::kw___pixel:
4613 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
4614 return true;
4615 case tok::identifier:
4616 if (Next.getIdentifierInfo() == Ident_pixel) {
4617 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
4618 return true;
4619 }
4620 break;
4621 default:
4622 break;
4623 }
Douglas Gregora8f031f2010-06-16 15:28:57 +00004624 } else if ((Tok.getIdentifierInfo() == Ident_pixel) &&
Chris Lattner1b492422010-02-28 18:33:55 +00004625 DS.isTypeAltiVecVector()) {
4626 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
4627 return true;
4628 }
4629 return false;
4630}