blob: 948309a8ec7c582ca3b0ba7f02619c18b1d84cf5 [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 '('.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000567 BalancedDelimiterTracker T(*this, tok::l_paren);
568 if (T.consumeOpen()) {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000569 Diag(Tok, diag::err_expected_lparen);
570 return;
571 }
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000572
573 // Parse the platform name,
574 if (Tok.isNot(tok::identifier)) {
575 Diag(Tok, diag::err_availability_expected_platform);
576 SkipUntil(tok::r_paren);
577 return;
578 }
579 Platform = Tok.getIdentifierInfo();
580 PlatformLoc = ConsumeToken();
581
582 // Parse the ',' following the platform name.
583 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "", tok::r_paren))
584 return;
585
586 // If we haven't grabbed the pointers for the identifiers
587 // "introduced", "deprecated", and "obsoleted", do so now.
588 if (!Ident_introduced) {
589 Ident_introduced = PP.getIdentifierInfo("introduced");
590 Ident_deprecated = PP.getIdentifierInfo("deprecated");
591 Ident_obsoleted = PP.getIdentifierInfo("obsoleted");
Douglas Gregorb53e4172011-03-26 03:35:55 +0000592 Ident_unavailable = PP.getIdentifierInfo("unavailable");
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000593 }
594
595 // Parse the set of introductions/deprecations/removals.
Douglas Gregorb53e4172011-03-26 03:35:55 +0000596 SourceLocation UnavailableLoc;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000597 do {
598 if (Tok.isNot(tok::identifier)) {
599 Diag(Tok, diag::err_availability_expected_change);
600 SkipUntil(tok::r_paren);
601 return;
602 }
603 IdentifierInfo *Keyword = Tok.getIdentifierInfo();
604 SourceLocation KeywordLoc = ConsumeToken();
605
Douglas Gregorb53e4172011-03-26 03:35:55 +0000606 if (Keyword == Ident_unavailable) {
607 if (UnavailableLoc.isValid()) {
608 Diag(KeywordLoc, diag::err_availability_redundant)
609 << Keyword << SourceRange(UnavailableLoc);
610 }
611 UnavailableLoc = KeywordLoc;
612
613 if (Tok.isNot(tok::comma))
614 break;
615
616 ConsumeToken();
617 continue;
618 }
619
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000620 if (Tok.isNot(tok::equal)) {
621 Diag(Tok, diag::err_expected_equal_after)
622 << Keyword;
623 SkipUntil(tok::r_paren);
624 return;
625 }
626 ConsumeToken();
627
628 SourceRange VersionRange;
629 VersionTuple Version = ParseVersionTuple(VersionRange);
630
631 if (Version.empty()) {
632 SkipUntil(tok::r_paren);
633 return;
634 }
635
636 unsigned Index;
637 if (Keyword == Ident_introduced)
638 Index = Introduced;
639 else if (Keyword == Ident_deprecated)
640 Index = Deprecated;
641 else if (Keyword == Ident_obsoleted)
642 Index = Obsoleted;
643 else
644 Index = Unknown;
645
646 if (Index < Unknown) {
647 if (!Changes[Index].KeywordLoc.isInvalid()) {
648 Diag(KeywordLoc, diag::err_availability_redundant)
649 << Keyword
650 << SourceRange(Changes[Index].KeywordLoc,
651 Changes[Index].VersionRange.getEnd());
652 }
653
654 Changes[Index].KeywordLoc = KeywordLoc;
655 Changes[Index].Version = Version;
656 Changes[Index].VersionRange = VersionRange;
657 } else {
658 Diag(KeywordLoc, diag::err_availability_unknown_change)
659 << Keyword << VersionRange;
660 }
661
662 if (Tok.isNot(tok::comma))
663 break;
664
665 ConsumeToken();
666 } while (true);
667
668 // Closing ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000669 if (T.consumeClose())
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000670 return;
671
672 if (endLoc)
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000673 *endLoc = T.getCloseLocation();
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000674
Douglas Gregorb53e4172011-03-26 03:35:55 +0000675 // The 'unavailable' availability cannot be combined with any other
676 // availability changes. Make sure that hasn't happened.
677 if (UnavailableLoc.isValid()) {
678 bool Complained = false;
679 for (unsigned Index = Introduced; Index != Unknown; ++Index) {
680 if (Changes[Index].KeywordLoc.isValid()) {
681 if (!Complained) {
682 Diag(UnavailableLoc, diag::warn_availability_and_unavailable)
683 << SourceRange(Changes[Index].KeywordLoc,
684 Changes[Index].VersionRange.getEnd());
685 Complained = true;
686 }
687
688 // Clear out the availability.
689 Changes[Index] = AvailabilityChange();
690 }
691 }
692 }
693
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000694 // Record this attribute
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000695 attrs.addNew(&Availability,
696 SourceRange(AvailabilityLoc, T.getCloseLocation()),
John McCall0b7e6782011-03-24 11:26:52 +0000697 0, SourceLocation(),
698 Platform, PlatformLoc,
699 Changes[Introduced],
700 Changes[Deprecated],
Douglas Gregorb53e4172011-03-26 03:35:55 +0000701 Changes[Obsoleted],
702 UnavailableLoc, false, false);
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000703}
704
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000705
706// Late Parsed Attributes:
707// See other examples of late parsing in lib/Parse/ParseCXXInlineMethods
708
709void Parser::LateParsedDeclaration::ParseLexedAttributes() {}
710
711void Parser::LateParsedClass::ParseLexedAttributes() {
712 Self->ParseLexedAttributes(*Class);
713}
714
715void Parser::LateParsedAttribute::ParseLexedAttributes() {
716 Self->ParseLexedAttribute(*this);
717}
718
719/// Wrapper class which calls ParseLexedAttribute, after setting up the
720/// scope appropriately.
721void Parser::ParseLexedAttributes(ParsingClass &Class) {
722 // Deal with templates
723 // FIXME: Test cases to make sure this does the right thing for templates.
724 bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope;
725 ParseScope ClassTemplateScope(this, Scope::TemplateParamScope,
726 HasTemplateScope);
727 if (HasTemplateScope)
728 Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate);
729
730 // Set or update the scope flags to include Scope::ThisScope.
731 bool AlreadyHasClassScope = Class.TopLevelClass;
732 unsigned ScopeFlags = Scope::ClassScope|Scope::DeclScope|Scope::ThisScope;
733 ParseScope ClassScope(this, ScopeFlags, !AlreadyHasClassScope);
734 ParseScopeFlags ClassScopeFlags(this, ScopeFlags, AlreadyHasClassScope);
735
736 for (unsigned i = 0, ni = Class.LateParsedDeclarations.size(); i < ni; ++i) {
737 Class.LateParsedDeclarations[i]->ParseLexedAttributes();
738 }
739}
740
741/// \brief Finish parsing an attribute for which parsing was delayed.
742/// This will be called at the end of parsing a class declaration
743/// for each LateParsedAttribute. We consume the saved tokens and
744/// create an attribute with the arguments filled in. We add this
745/// to the Attribute list for the decl.
746void Parser::ParseLexedAttribute(LateParsedAttribute &LA) {
747 // Save the current token position.
748 SourceLocation OrigLoc = Tok.getLocation();
749
750 // Append the current token at the end of the new token stream so that it
751 // doesn't get lost.
752 LA.Toks.push_back(Tok);
753 PP.EnterTokenStream(LA.Toks.data(), LA.Toks.size(), true, false);
754 // Consume the previously pushed token.
755 ConsumeAnyToken();
756
757 ParsedAttributes Attrs(AttrFactory);
758 SourceLocation endLoc;
759
Caitlin Sadowskied9d84a2011-09-08 17:42:31 +0000760 // If the Decl is templatized, add template parameters to scope.
761 bool HasTemplateScope = LA.D && LA.D->isTemplateDecl();
762 ParseScope TempScope(this, Scope::TemplateParamScope, HasTemplateScope);
763 if (HasTemplateScope)
764 Actions.ActOnReenterTemplateScope(Actions.CurScope, LA.D);
765
766 // If the Decl is on a function, add function parameters to the scope.
767 bool HasFunctionScope = LA.D && LA.D->isFunctionOrFunctionTemplate();
768 ParseScope FnScope(this, Scope::FnScope|Scope::DeclScope, HasFunctionScope);
769 if (HasFunctionScope)
770 Actions.ActOnReenterFunctionContext(Actions.CurScope, LA.D);
771
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000772 ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, &endLoc);
773
Caitlin Sadowskied9d84a2011-09-08 17:42:31 +0000774 if (HasFunctionScope) {
775 Actions.ActOnExitFunctionContext();
776 FnScope.Exit(); // Pop scope, and remove Decls from IdResolver
777 }
778 if (HasTemplateScope) {
779 TempScope.Exit();
780 }
781
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000782 // Late parsed attributes must be attached to Decls by hand. If the
783 // LA.D is not set, then this was not done properly.
784 assert(LA.D && "No decl attached to late parsed attribute");
785 Actions.ActOnFinishDelayedAttribute(getCurScope(), LA.D, Attrs);
786
787 if (Tok.getLocation() != OrigLoc) {
788 // Due to a parsing error, we either went over the cached tokens or
789 // there are still cached tokens left, so we skip the leftover tokens.
790 // Since this is an uncommon situation that should be avoided, use the
791 // expensive isBeforeInTranslationUnit call.
792 if (PP.getSourceManager().isBeforeInTranslationUnit(Tok.getLocation(),
793 OrigLoc))
794 while (Tok.getLocation() != OrigLoc && Tok.isNot(tok::eof))
795 ConsumeAnyToken();
796 }
797}
798
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000799/// \brief Wrapper around a case statement checking if AttrName is
800/// one of the thread safety attributes
801bool Parser::IsThreadSafetyAttribute(llvm::StringRef AttrName){
802 return llvm::StringSwitch<bool>(AttrName)
803 .Case("guarded_by", true)
804 .Case("guarded_var", true)
805 .Case("pt_guarded_by", true)
806 .Case("pt_guarded_var", true)
807 .Case("lockable", true)
808 .Case("scoped_lockable", true)
809 .Case("no_thread_safety_analysis", true)
810 .Case("acquired_after", true)
811 .Case("acquired_before", true)
812 .Case("exclusive_lock_function", true)
813 .Case("shared_lock_function", true)
814 .Case("exclusive_trylock_function", true)
815 .Case("shared_trylock_function", true)
816 .Case("unlock_function", true)
817 .Case("lock_returned", true)
818 .Case("locks_excluded", true)
819 .Case("exclusive_locks_required", true)
820 .Case("shared_locks_required", true)
821 .Default(false);
822}
823
824/// \brief Parse the contents of thread safety attributes. These
825/// should always be parsed as an expression list.
826///
827/// We need to special case the parsing due to the fact that if the first token
828/// of the first argument is an identifier, the main parse loop will store
829/// that token as a "parameter" and the rest of
830/// the arguments will be added to a list of "arguments". However,
831/// subsequent tokens in the first argument are lost. We instead parse each
832/// argument as an expression and add all arguments to the list of "arguments".
833/// In future, we will take advantage of this special case to also
834/// deal with some argument scoping issues here (for example, referring to a
835/// function parameter in the attribute on that function).
836void Parser::ParseThreadSafetyAttribute(IdentifierInfo &AttrName,
837 SourceLocation AttrNameLoc,
838 ParsedAttributes &Attrs,
839 SourceLocation *EndLoc) {
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000840 assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('");
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000841
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000842 BalancedDelimiterTracker T(*this, tok::l_paren);
843 T.consumeOpen();
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000844
845 ExprVector ArgExprs(Actions);
846 bool ArgExprsOk = true;
847
848 // now parse the list of expressions
849 while (1) {
850 ExprResult ArgExpr(ParseAssignmentExpression());
851 if (ArgExpr.isInvalid()) {
852 ArgExprsOk = false;
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000853 T.consumeClose();
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000854 break;
855 } else {
856 ArgExprs.push_back(ArgExpr.release());
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000857 }
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000858 if (Tok.isNot(tok::comma))
859 break;
860 ConsumeToken(); // Eat the comma, move to the next argument
861 }
862 // Match the ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000863 if (ArgExprsOk && !T.consumeClose()) {
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000864 Attrs.addNew(&AttrName, AttrNameLoc, 0, AttrNameLoc, 0, SourceLocation(),
865 ArgExprs.take(), ArgExprs.size());
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000866 }
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000867 if (EndLoc)
868 *EndLoc = T.getCloseLocation();
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000869}
870
John McCall7f040a92010-12-24 02:08:15 +0000871void Parser::DiagnoseProhibitedAttributes(ParsedAttributesWithRange &attrs) {
872 Diag(attrs.Range.getBegin(), diag::err_attributes_not_allowed)
873 << attrs.Range;
Dawn Perchik52fc3142010-09-03 01:29:35 +0000874}
875
Reid Spencer5f016e22007-07-11 17:01:13 +0000876/// ParseDeclaration - Parse a full 'declaration', which consists of
877/// declaration-specifiers, some number of declarators, and a semicolon.
Chris Lattner97144fc2009-04-02 04:16:50 +0000878/// 'Context' should be a Declarator::TheContext value. This returns the
879/// location of the semicolon in DeclEnd.
Chris Lattner8f08cb72007-08-25 06:57:03 +0000880///
881/// declaration: [C99 6.7]
882/// block-declaration ->
883/// simple-declaration
884/// others [FIXME]
Douglas Gregoradcac882008-12-01 23:54:00 +0000885/// [C++] template-declaration
Chris Lattner8f08cb72007-08-25 06:57:03 +0000886/// [C++] namespace-definition
Douglas Gregorf780abc2008-12-30 03:27:21 +0000887/// [C++] using-directive
Douglas Gregord7f37bf2009-06-22 23:06:13 +0000888/// [C++] using-declaration
Peter Collingbournec6eb44b2011-04-15 00:35:57 +0000889/// [C++0x/C1X] static_assert-declaration
Chris Lattner8f08cb72007-08-25 06:57:03 +0000890/// others... [FIXME]
891///
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +0000892Parser::DeclGroupPtrTy Parser::ParseDeclaration(StmtVector &Stmts,
893 unsigned Context,
Sean Huntbbd37c62009-11-21 08:43:09 +0000894 SourceLocation &DeclEnd,
John McCall7f040a92010-12-24 02:08:15 +0000895 ParsedAttributesWithRange &attrs) {
Argyrios Kyrtzidis36d36802010-06-17 10:52:18 +0000896 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Fariborz Jahaniane8cff362011-08-30 17:10:52 +0000897 // Must temporarily exit the objective-c container scope for
898 // parsing c none objective-c decls.
899 ObjCDeclContextSwitch ObjCDC(*this);
Argyrios Kyrtzidis36d36802010-06-17 10:52:18 +0000900
John McCalld226f652010-08-21 09:40:31 +0000901 Decl *SingleDecl = 0;
Richard Smithc89edf52011-07-01 19:46:12 +0000902 Decl *OwnedType = 0;
Chris Lattner8f08cb72007-08-25 06:57:03 +0000903 switch (Tok.getKind()) {
Douglas Gregoradcac882008-12-01 23:54:00 +0000904 case tok::kw_template:
Douglas Gregor1426e532009-05-12 21:31:51 +0000905 case tok::kw_export:
John McCall7f040a92010-12-24 02:08:15 +0000906 ProhibitAttributes(attrs);
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000907 SingleDecl = ParseDeclarationStartingWithTemplate(Context, DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +0000908 break;
Sebastian Redld078e642010-08-27 23:12:46 +0000909 case tok::kw_inline:
Sebastian Redl88e64ca2010-08-31 00:36:45 +0000910 // Could be the start of an inline namespace. Allowed as an ext in C++03.
911 if (getLang().CPlusPlus && NextToken().is(tok::kw_namespace)) {
John McCall7f040a92010-12-24 02:08:15 +0000912 ProhibitAttributes(attrs);
Sebastian Redld078e642010-08-27 23:12:46 +0000913 SourceLocation InlineLoc = ConsumeToken();
914 SingleDecl = ParseNamespace(Context, DeclEnd, InlineLoc);
915 break;
916 }
John McCall7f040a92010-12-24 02:08:15 +0000917 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs,
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +0000918 true);
Chris Lattner8f08cb72007-08-25 06:57:03 +0000919 case tok::kw_namespace:
John McCall7f040a92010-12-24 02:08:15 +0000920 ProhibitAttributes(attrs);
Chris Lattner97144fc2009-04-02 04:16:50 +0000921 SingleDecl = ParseNamespace(Context, DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +0000922 break;
Douglas Gregorf780abc2008-12-30 03:27:21 +0000923 case tok::kw_using:
John McCall78b81052010-11-10 02:40:36 +0000924 SingleDecl = ParseUsingDirectiveOrDeclaration(Context, ParsedTemplateInfo(),
Richard Smithc89edf52011-07-01 19:46:12 +0000925 DeclEnd, attrs, &OwnedType);
Chris Lattner682bf922009-03-29 16:50:03 +0000926 break;
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000927 case tok::kw_static_assert:
Peter Collingbournec6eb44b2011-04-15 00:35:57 +0000928 case tok::kw__Static_assert:
John McCall7f040a92010-12-24 02:08:15 +0000929 ProhibitAttributes(attrs);
Chris Lattner97144fc2009-04-02 04:16:50 +0000930 SingleDecl = ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +0000931 break;
Chris Lattner8f08cb72007-08-25 06:57:03 +0000932 default:
John McCall7f040a92010-12-24 02:08:15 +0000933 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs, true);
Chris Lattner8f08cb72007-08-25 06:57:03 +0000934 }
Sean Huntbbd37c62009-11-21 08:43:09 +0000935
Chris Lattner682bf922009-03-29 16:50:03 +0000936 // This routine returns a DeclGroup, if the thing we parsed only contains a
Richard Smithc89edf52011-07-01 19:46:12 +0000937 // single decl, convert it now. Alias declarations can also declare a type;
938 // include that too if it is present.
939 return Actions.ConvertDeclToDeclGroup(SingleDecl, OwnedType);
Chris Lattner8f08cb72007-08-25 06:57:03 +0000940}
941
942/// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl]
943/// declaration-specifiers init-declarator-list[opt] ';'
944///[C90/C++]init-declarator-list ';' [TODO]
945/// [OMP] threadprivate-directive [TODO]
Chris Lattnercd147752009-03-29 17:27:48 +0000946///
Richard Smithad762fc2011-04-14 22:09:26 +0000947/// for-range-declaration: [C++0x 6.5p1: stmt.ranged]
948/// attribute-specifier-seq[opt] type-specifier-seq declarator
949///
Chris Lattnercd147752009-03-29 17:27:48 +0000950/// If RequireSemi is false, this does not check for a ';' at the end of the
Chris Lattner5c5db552010-04-05 18:18:31 +0000951/// declaration. If it is true, it checks for and eats it.
Richard Smithad762fc2011-04-14 22:09:26 +0000952///
953/// If FRI is non-null, we might be parsing a for-range-declaration instead
954/// of a simple-declaration. If we find that we are, we also parse the
955/// for-range-initializer, and place it here.
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +0000956Parser::DeclGroupPtrTy Parser::ParseSimpleDeclaration(StmtVector &Stmts,
957 unsigned Context,
Sean Huntbbd37c62009-11-21 08:43:09 +0000958 SourceLocation &DeclEnd,
John McCall7f040a92010-12-24 02:08:15 +0000959 ParsedAttributes &attrs,
Richard Smithad762fc2011-04-14 22:09:26 +0000960 bool RequireSemi,
961 ForRangeInit *FRI) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000962 // Parse the common declaration-specifiers piece.
John McCall54abf7d2009-11-04 02:18:39 +0000963 ParsingDeclSpec DS(*this);
John McCall7f040a92010-12-24 02:08:15 +0000964 DS.takeAttributesFrom(attrs);
Douglas Gregor312eadb2011-04-24 05:37:28 +0000965
Douglas Gregor0efc2c12010-01-13 17:31:36 +0000966 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS_none,
Richard Smith34b41d92011-02-20 03:19:35 +0000967 getDeclSpecContextFromDeclaratorContext(Context));
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +0000968 StmtResult R = Actions.ActOnVlaStmt(DS);
969 if (R.isUsable())
970 Stmts.push_back(R.release());
Douglas Gregor312eadb2011-04-24 05:37:28 +0000971
Reid Spencer5f016e22007-07-11 17:01:13 +0000972 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
973 // declaration-specifiers init-declarator-list[opt] ';'
Chris Lattner04d66662007-10-09 17:33:22 +0000974 if (Tok.is(tok::semi)) {
Chris Lattner5c5db552010-04-05 18:18:31 +0000975 if (RequireSemi) ConsumeToken();
John McCalld226f652010-08-21 09:40:31 +0000976 Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none,
Douglas Gregor312eadb2011-04-24 05:37:28 +0000977 DS);
John McCall54abf7d2009-11-04 02:18:39 +0000978 DS.complete(TheDecl);
Chris Lattner682bf922009-03-29 16:50:03 +0000979 return Actions.ConvertDeclToDeclGroup(TheDecl);
Reid Spencer5f016e22007-07-11 17:01:13 +0000980 }
Douglas Gregor312eadb2011-04-24 05:37:28 +0000981
982 return ParseDeclGroup(DS, Context, /*FunctionDefs=*/ false, &DeclEnd, FRI);
John McCalld8ac0572009-11-03 19:26:08 +0000983}
Mike Stump1eb44332009-09-09 15:08:12 +0000984
John McCalld8ac0572009-11-03 19:26:08 +0000985/// ParseDeclGroup - Having concluded that this is either a function
986/// definition or a group of object declarations, actually parse the
987/// result.
John McCall54abf7d2009-11-04 02:18:39 +0000988Parser::DeclGroupPtrTy Parser::ParseDeclGroup(ParsingDeclSpec &DS,
989 unsigned Context,
John McCalld8ac0572009-11-03 19:26:08 +0000990 bool AllowFunctionDefinitions,
Richard Smithad762fc2011-04-14 22:09:26 +0000991 SourceLocation *DeclEnd,
992 ForRangeInit *FRI) {
John McCalld8ac0572009-11-03 19:26:08 +0000993 // Parse the first declarator.
John McCall54abf7d2009-11-04 02:18:39 +0000994 ParsingDeclarator D(*this, DS, static_cast<Declarator::TheContext>(Context));
John McCalld8ac0572009-11-03 19:26:08 +0000995 ParseDeclarator(D);
Chris Lattnercd147752009-03-29 17:27:48 +0000996
John McCalld8ac0572009-11-03 19:26:08 +0000997 // Bail out if the first declarator didn't seem well-formed.
998 if (!D.hasName() && !D.mayOmitIdentifier()) {
999 // Skip until ; or }.
1000 SkipUntil(tok::r_brace, true, true);
1001 if (Tok.is(tok::semi))
1002 ConsumeToken();
1003 return DeclGroupPtrTy();
Chris Lattner23c4b182009-03-29 17:18:04 +00001004 }
Mike Stump1eb44332009-09-09 15:08:12 +00001005
Chris Lattnerc82daef2010-07-11 22:24:20 +00001006 // Check to see if we have a function *definition* which must have a body.
1007 if (AllowFunctionDefinitions && D.isFunctionDeclarator() &&
1008 // Look at the next token to make sure that this isn't a function
1009 // declaration. We have to check this because __attribute__ might be the
1010 // start of a function definition in GCC-extended K&R C.
1011 !isDeclarationAfterDeclarator()) {
1012
Chris Lattner004659a2010-07-11 22:42:07 +00001013 if (isStartOfFunctionDefinition(D)) {
John McCalld8ac0572009-11-03 19:26:08 +00001014 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
1015 Diag(Tok, diag::err_function_declared_typedef);
1016
1017 // Recover by treating the 'typedef' as spurious.
1018 DS.ClearStorageClassSpecs();
1019 }
1020
John McCalld226f652010-08-21 09:40:31 +00001021 Decl *TheDecl = ParseFunctionDefinition(D);
John McCalld8ac0572009-11-03 19:26:08 +00001022 return Actions.ConvertDeclToDeclGroup(TheDecl);
Chris Lattner004659a2010-07-11 22:42:07 +00001023 }
1024
1025 if (isDeclarationSpecifier()) {
1026 // If there is an invalid declaration specifier right after the function
1027 // prototype, then we must be in a missing semicolon case where this isn't
1028 // actually a body. Just fall through into the code that handles it as a
1029 // prototype, and let the top-level code handle the erroneous declspec
1030 // where it would otherwise expect a comma or semicolon.
John McCalld8ac0572009-11-03 19:26:08 +00001031 } else {
1032 Diag(Tok, diag::err_expected_fn_body);
1033 SkipUntil(tok::semi);
1034 return DeclGroupPtrTy();
1035 }
1036 }
1037
Richard Smithad762fc2011-04-14 22:09:26 +00001038 if (ParseAttributesAfterDeclarator(D))
1039 return DeclGroupPtrTy();
1040
1041 // C++0x [stmt.iter]p1: Check if we have a for-range-declarator. If so, we
1042 // must parse and analyze the for-range-initializer before the declaration is
1043 // analyzed.
1044 if (FRI && Tok.is(tok::colon)) {
1045 FRI->ColonLoc = ConsumeToken();
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001046 if (Tok.is(tok::l_brace))
1047 FRI->RangeExpr = ParseBraceInitializer();
1048 else
1049 FRI->RangeExpr = ParseExpression();
Richard Smithad762fc2011-04-14 22:09:26 +00001050 Decl *ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
1051 Actions.ActOnCXXForRangeDecl(ThisDecl);
1052 Actions.FinalizeDeclaration(ThisDecl);
1053 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS, &ThisDecl, 1);
1054 }
1055
Chris Lattner5f9e2722011-07-23 10:55:15 +00001056 SmallVector<Decl *, 8> DeclsInGroup;
Richard Smithad762fc2011-04-14 22:09:26 +00001057 Decl *FirstDecl = ParseDeclarationAfterDeclaratorAndAttributes(D);
John McCall54abf7d2009-11-04 02:18:39 +00001058 D.complete(FirstDecl);
John McCalld226f652010-08-21 09:40:31 +00001059 if (FirstDecl)
John McCalld8ac0572009-11-03 19:26:08 +00001060 DeclsInGroup.push_back(FirstDecl);
1061
1062 // If we don't have a comma, it is either the end of the list (a ';') or an
1063 // error, bail out.
1064 while (Tok.is(tok::comma)) {
1065 // Consume the comma.
Chris Lattner23c4b182009-03-29 17:18:04 +00001066 ConsumeToken();
John McCalld8ac0572009-11-03 19:26:08 +00001067
1068 // Parse the next declarator.
1069 D.clear();
1070
1071 // Accept attributes in an init-declarator. In the first declarator in a
1072 // declaration, these would be part of the declspec. In subsequent
1073 // declarators, they become part of the declarator itself, so that they
1074 // don't apply to declarators after *this* one. Examples:
1075 // short __attribute__((common)) var; -> declspec
1076 // short var __attribute__((common)); -> declarator
1077 // short x, __attribute__((common)) var; -> declarator
John McCall7f040a92010-12-24 02:08:15 +00001078 MaybeParseGNUAttributes(D);
John McCalld8ac0572009-11-03 19:26:08 +00001079
1080 ParseDeclarator(D);
1081
John McCalld226f652010-08-21 09:40:31 +00001082 Decl *ThisDecl = ParseDeclarationAfterDeclarator(D);
John McCall54abf7d2009-11-04 02:18:39 +00001083 D.complete(ThisDecl);
John McCalld226f652010-08-21 09:40:31 +00001084 if (ThisDecl)
John McCalld8ac0572009-11-03 19:26:08 +00001085 DeclsInGroup.push_back(ThisDecl);
1086 }
1087
1088 if (DeclEnd)
1089 *DeclEnd = Tok.getLocation();
1090
1091 if (Context != Declarator::ForContext &&
1092 ExpectAndConsume(tok::semi,
1093 Context == Declarator::FileContext
1094 ? diag::err_invalid_token_after_toplevel_declarator
1095 : diag::err_expected_semi_declaration)) {
Chris Lattner004659a2010-07-11 22:42:07 +00001096 // Okay, there was no semicolon and one was expected. If we see a
1097 // declaration specifier, just assume it was missing and continue parsing.
1098 // Otherwise things are very confused and we skip to recover.
1099 if (!isDeclarationSpecifier()) {
1100 SkipUntil(tok::r_brace, true, true);
1101 if (Tok.is(tok::semi))
1102 ConsumeToken();
1103 }
John McCalld8ac0572009-11-03 19:26:08 +00001104 }
1105
Douglas Gregor23c94db2010-07-02 17:43:08 +00001106 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS,
John McCalld8ac0572009-11-03 19:26:08 +00001107 DeclsInGroup.data(),
1108 DeclsInGroup.size());
Reid Spencer5f016e22007-07-11 17:01:13 +00001109}
1110
Richard Smithad762fc2011-04-14 22:09:26 +00001111/// Parse an optional simple-asm-expr and attributes, and attach them to a
1112/// declarator. Returns true on an error.
1113bool Parser::ParseAttributesAfterDeclarator(Declarator &D) {
1114 // If a simple-asm-expr is present, parse it.
1115 if (Tok.is(tok::kw_asm)) {
1116 SourceLocation Loc;
1117 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
1118 if (AsmLabel.isInvalid()) {
1119 SkipUntil(tok::semi, true, true);
1120 return true;
1121 }
1122
1123 D.setAsmLabel(AsmLabel.release());
1124 D.SetRangeEnd(Loc);
1125 }
1126
1127 MaybeParseGNUAttributes(D);
1128 return false;
1129}
1130
Douglas Gregor1426e532009-05-12 21:31:51 +00001131/// \brief Parse 'declaration' after parsing 'declaration-specifiers
1132/// declarator'. This method parses the remainder of the declaration
1133/// (including any attributes or initializer, among other things) and
1134/// finalizes the declaration.
Reid Spencer5f016e22007-07-11 17:01:13 +00001135///
Reid Spencer5f016e22007-07-11 17:01:13 +00001136/// init-declarator: [C99 6.7]
1137/// declarator
1138/// declarator '=' initializer
1139/// [GNU] declarator simple-asm-expr[opt] attributes[opt]
1140/// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00001141/// [C++] declarator initializer[opt]
1142///
1143/// [C++] initializer:
1144/// [C++] '=' initializer-clause
1145/// [C++] '(' expression-list ')'
Sebastian Redl50de12f2009-03-24 22:27:57 +00001146/// [C++0x] '=' 'default' [TODO]
1147/// [C++0x] '=' 'delete'
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001148/// [C++0x] braced-init-list
Sebastian Redl50de12f2009-03-24 22:27:57 +00001149///
1150/// According to the standard grammar, =default and =delete are function
1151/// definitions, but that definitely doesn't fit with the parser here.
Reid Spencer5f016e22007-07-11 17:01:13 +00001152///
John McCalld226f652010-08-21 09:40:31 +00001153Decl *Parser::ParseDeclarationAfterDeclarator(Declarator &D,
Douglas Gregore542c862009-06-23 23:11:28 +00001154 const ParsedTemplateInfo &TemplateInfo) {
Richard Smithad762fc2011-04-14 22:09:26 +00001155 if (ParseAttributesAfterDeclarator(D))
1156 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001157
Richard Smithad762fc2011-04-14 22:09:26 +00001158 return ParseDeclarationAfterDeclaratorAndAttributes(D, TemplateInfo);
1159}
Mike Stump1eb44332009-09-09 15:08:12 +00001160
Richard Smithad762fc2011-04-14 22:09:26 +00001161Decl *Parser::ParseDeclarationAfterDeclaratorAndAttributes(Declarator &D,
1162 const ParsedTemplateInfo &TemplateInfo) {
Douglas Gregor1426e532009-05-12 21:31:51 +00001163 // Inform the current actions module that we just parsed this declarator.
John McCalld226f652010-08-21 09:40:31 +00001164 Decl *ThisDecl = 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +00001165 switch (TemplateInfo.Kind) {
1166 case ParsedTemplateInfo::NonTemplate:
Douglas Gregor23c94db2010-07-02 17:43:08 +00001167 ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
Douglas Gregord5a423b2009-09-25 18:43:00 +00001168 break;
1169
1170 case ParsedTemplateInfo::Template:
1171 case ParsedTemplateInfo::ExplicitSpecialization:
Douglas Gregor23c94db2010-07-02 17:43:08 +00001172 ThisDecl = Actions.ActOnTemplateDeclarator(getCurScope(),
John McCallf312b1e2010-08-26 23:41:50 +00001173 MultiTemplateParamsArg(Actions,
Douglas Gregore542c862009-06-23 23:11:28 +00001174 TemplateInfo.TemplateParams->data(),
1175 TemplateInfo.TemplateParams->size()),
Douglas Gregord5a423b2009-09-25 18:43:00 +00001176 D);
1177 break;
1178
1179 case ParsedTemplateInfo::ExplicitInstantiation: {
John McCalld226f652010-08-21 09:40:31 +00001180 DeclResult ThisRes
Douglas Gregor23c94db2010-07-02 17:43:08 +00001181 = Actions.ActOnExplicitInstantiation(getCurScope(),
Douglas Gregord5a423b2009-09-25 18:43:00 +00001182 TemplateInfo.ExternLoc,
1183 TemplateInfo.TemplateLoc,
1184 D);
1185 if (ThisRes.isInvalid()) {
1186 SkipUntil(tok::semi, true, true);
John McCalld226f652010-08-21 09:40:31 +00001187 return 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +00001188 }
1189
1190 ThisDecl = ThisRes.get();
1191 break;
1192 }
1193 }
Mike Stump1eb44332009-09-09 15:08:12 +00001194
Richard Smith34b41d92011-02-20 03:19:35 +00001195 bool TypeContainsAuto =
1196 D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto;
1197
Douglas Gregor1426e532009-05-12 21:31:51 +00001198 // Parse declarator '=' initializer.
Argyrios Kyrtzidisa6eb5f82010-10-08 02:39:23 +00001199 if (isTokenEqualOrMistypedEqualEqual(
1200 diag::err_invalid_equalequal_after_declarator)) {
Douglas Gregor1426e532009-05-12 21:31:51 +00001201 ConsumeToken();
Anders Carlsson37bf9d22010-09-24 21:25:25 +00001202 if (Tok.is(tok::kw_delete)) {
Sean Hunte4246a62011-05-12 06:15:49 +00001203 if (D.isFunctionDeclarator())
1204 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
1205 << 1 /* delete */;
1206 else
1207 Diag(ConsumeToken(), diag::err_deleted_non_function);
Sean Huntfe2695e2011-05-06 01:42:00 +00001208 } else if (Tok.is(tok::kw_default)) {
Sean Hunte4246a62011-05-12 06:15:49 +00001209 if (D.isFunctionDeclarator())
1210 Diag(Tok, diag::err_default_delete_in_multiple_declaration)
1211 << 1 /* delete */;
1212 else
1213 Diag(ConsumeToken(), diag::err_default_special_members);
Douglas Gregor1426e532009-05-12 21:31:51 +00001214 } else {
John McCall731ad842009-12-19 09:28:58 +00001215 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
1216 EnterScope(0);
Douglas Gregor23c94db2010-07-02 17:43:08 +00001217 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
John McCall731ad842009-12-19 09:28:58 +00001218 }
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +00001219
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +00001220 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001221 Actions.CodeCompleteInitializer(getCurScope(), ThisDecl);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001222 cutOffParsing();
1223 return 0;
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +00001224 }
1225
John McCall60d7b3a2010-08-24 06:29:42 +00001226 ExprResult Init(ParseInitializer());
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +00001227
John McCall731ad842009-12-19 09:28:58 +00001228 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001229 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
John McCall731ad842009-12-19 09:28:58 +00001230 ExitScope();
1231 }
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +00001232
Douglas Gregor1426e532009-05-12 21:31:51 +00001233 if (Init.isInvalid()) {
Douglas Gregor00225542010-03-01 18:27:54 +00001234 SkipUntil(tok::comma, true, true);
1235 Actions.ActOnInitializerError(ThisDecl);
1236 } else
Richard Smith34b41d92011-02-20 03:19:35 +00001237 Actions.AddInitializerToDecl(ThisDecl, Init.take(),
1238 /*DirectInit=*/false, TypeContainsAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +00001239 }
1240 } else if (Tok.is(tok::l_paren)) {
1241 // Parse C++ direct initializer: '(' expression-list ')'
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001242 BalancedDelimiterTracker T(*this, tok::l_paren);
1243 T.consumeOpen();
1244
Douglas Gregor1426e532009-05-12 21:31:51 +00001245 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 ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001262 T.consumeClose();
Douglas Gregor1426e532009-05-12 21:31:51 +00001263
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 Gregor4a8dfb52011-10-12 16:37:45 +00001272 Actions.AddCXXDirectInitializerToDecl(ThisDecl, T.getOpenLocation(),
Douglas Gregor1426e532009-05-12 21:31:51 +00001273 move_arg(Exprs),
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001274 T.getCloseLocation(),
Richard Smith34b41d92011-02-20 03:19:35 +00001275 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.
Richard Smith7fe62082011-10-15 05:09:34 +00001279 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
1280
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001281 if (D.getCXXScopeSpec().isSet()) {
1282 EnterScope(0);
1283 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
1284 }
1285
1286 ExprResult Init(ParseBraceInitializer());
1287
1288 if (D.getCXXScopeSpec().isSet()) {
1289 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
1290 ExitScope();
1291 }
1292
1293 if (Init.isInvalid()) {
1294 Actions.ActOnInitializerError(ThisDecl);
1295 } else
1296 Actions.AddInitializerToDecl(ThisDecl, Init.take(),
1297 /*DirectInit=*/true, TypeContainsAuto);
1298
Douglas Gregor1426e532009-05-12 21:31:51 +00001299 } else {
Richard Smith34b41d92011-02-20 03:19:35 +00001300 Actions.ActOnUninitializedDecl(ThisDecl, TypeContainsAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +00001301 }
1302
Richard Smith483b9f32011-02-21 20:05:19 +00001303 Actions.FinalizeDeclaration(ThisDecl);
1304
Douglas Gregor1426e532009-05-12 21:31:51 +00001305 return ThisDecl;
1306}
1307
Reid Spencer5f016e22007-07-11 17:01:13 +00001308/// ParseSpecifierQualifierList
1309/// specifier-qualifier-list:
1310/// type-specifier specifier-qualifier-list[opt]
1311/// type-qualifier specifier-qualifier-list[opt]
1312/// [GNU] attributes specifier-qualifier-list[opt]
1313///
Richard Smithc89edf52011-07-01 19:46:12 +00001314void Parser::ParseSpecifierQualifierList(DeclSpec &DS, AccessSpecifier AS) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001315 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
1316 /// parse declaration-specifiers and complain about extra stuff.
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001317 /// TODO: diagnose attribute-specifiers and alignment-specifiers.
Richard Smithc89edf52011-07-01 19:46:12 +00001318 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS);
Mike Stump1eb44332009-09-09 15:08:12 +00001319
Reid Spencer5f016e22007-07-11 17:01:13 +00001320 // Validate declspec for type-name.
1321 unsigned Specs = DS.getParsedSpecifiers();
Chris Lattnerb6645dd2009-04-14 21:16:09 +00001322 if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers() &&
John McCall7f040a92010-12-24 02:08:15 +00001323 !DS.hasAttributes())
Reid Spencer5f016e22007-07-11 17:01:13 +00001324 Diag(Tok, diag::err_typename_requires_specqual);
Mike Stump1eb44332009-09-09 15:08:12 +00001325
Reid Spencer5f016e22007-07-11 17:01:13 +00001326 // Issue diagnostic and remove storage class if present.
1327 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
1328 if (DS.getStorageClassSpecLoc().isValid())
1329 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
1330 else
1331 Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass);
1332 DS.ClearStorageClassSpecs();
1333 }
Mike Stump1eb44332009-09-09 15:08:12 +00001334
Reid Spencer5f016e22007-07-11 17:01:13 +00001335 // Issue diagnostic and remove function specfier if present.
1336 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Douglas Gregorb48fe382008-10-31 09:07:45 +00001337 if (DS.isInlineSpecified())
1338 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
1339 if (DS.isVirtualSpecified())
1340 Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec);
1341 if (DS.isExplicitSpecified())
1342 Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec);
Reid Spencer5f016e22007-07-11 17:01:13 +00001343 DS.ClearFunctionSpecs();
1344 }
1345}
1346
Chris Lattnerc199ab32009-04-12 20:42:31 +00001347/// isValidAfterIdentifierInDeclaratorAfterDeclSpec - Return true if the
1348/// specified token is valid after the identifier in a declarator which
1349/// immediately follows the declspec. For example, these things are valid:
1350///
1351/// int x [ 4]; // direct-declarator
1352/// int x ( int y); // direct-declarator
1353/// int(int x ) // direct-declarator
1354/// int x ; // simple-declaration
1355/// int x = 17; // init-declarator-list
1356/// int x , y; // init-declarator-list
1357/// int x __asm__ ("foo"); // init-declarator-list
Chris Lattnerb6645dd2009-04-14 21:16:09 +00001358/// int x : 4; // struct-declarator
Chris Lattnerc83c27a2009-04-12 22:29:43 +00001359/// int x { 5}; // C++'0x unified initializers
Chris Lattnerc199ab32009-04-12 20:42:31 +00001360///
1361/// This is not, because 'x' does not immediately follow the declspec (though
1362/// ')' happens to be valid anyway).
1363/// int (x)
1364///
1365static bool isValidAfterIdentifierInDeclarator(const Token &T) {
1366 return T.is(tok::l_square) || T.is(tok::l_paren) || T.is(tok::r_paren) ||
1367 T.is(tok::semi) || T.is(tok::comma) || T.is(tok::equal) ||
Chris Lattnerb6645dd2009-04-14 21:16:09 +00001368 T.is(tok::kw_asm) || T.is(tok::l_brace) || T.is(tok::colon);
Chris Lattnerc199ab32009-04-12 20:42:31 +00001369}
1370
Chris Lattnere40c2952009-04-14 21:34:55 +00001371
1372/// ParseImplicitInt - This method is called when we have an non-typename
1373/// identifier in a declspec (which normally terminates the decl spec) when
1374/// the declspec has no type specifier. In this case, the declspec is either
1375/// malformed or is "implicit int" (in K&R and C89).
1376///
1377/// This method handles diagnosing this prettily and returns false if the
1378/// declspec is done being processed. If it recovers and thinks there may be
1379/// other pieces of declspec after it, it returns true.
1380///
Chris Lattnerf4382f52009-04-14 22:17:06 +00001381bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001382 const ParsedTemplateInfo &TemplateInfo,
Chris Lattnere40c2952009-04-14 21:34:55 +00001383 AccessSpecifier AS) {
Chris Lattnerf4382f52009-04-14 22:17:06 +00001384 assert(Tok.is(tok::identifier) && "should have identifier");
Mike Stump1eb44332009-09-09 15:08:12 +00001385
Chris Lattnere40c2952009-04-14 21:34:55 +00001386 SourceLocation Loc = Tok.getLocation();
1387 // If we see an identifier that is not a type name, we normally would
1388 // parse it as the identifer being declared. However, when a typename
1389 // is typo'd or the definition is not included, this will incorrectly
1390 // parse the typename as the identifier name and fall over misparsing
1391 // later parts of the diagnostic.
1392 //
1393 // As such, we try to do some look-ahead in cases where this would
1394 // otherwise be an "implicit-int" case to see if this is invalid. For
1395 // example: "static foo_t x = 4;" In this case, if we parsed foo_t as
1396 // an identifier with implicit int, we'd get a parse error because the
1397 // next token is obviously invalid for a type. Parse these as a case
1398 // with an invalid type specifier.
1399 assert(!DS.hasTypeSpecifier() && "Type specifier checked above");
Mike Stump1eb44332009-09-09 15:08:12 +00001400
Chris Lattnere40c2952009-04-14 21:34:55 +00001401 // Since we know that this either implicit int (which is rare) or an
1402 // error, we'd do lookahead to try to do better recovery.
1403 if (isValidAfterIdentifierInDeclarator(NextToken())) {
1404 // If this token is valid for implicit int, e.g. "static x = 4", then
1405 // we just avoid eating the identifier, so it will be parsed as the
1406 // identifier in the declarator.
1407 return false;
1408 }
Mike Stump1eb44332009-09-09 15:08:12 +00001409
Chris Lattnere40c2952009-04-14 21:34:55 +00001410 // Otherwise, if we don't consume this token, we are going to emit an
1411 // error anyway. Try to recover from various common problems. Check
1412 // to see if this was a reference to a tag name without a tag specified.
1413 // This is a common problem in C (saying 'foo' instead of 'struct foo').
Chris Lattnerf4382f52009-04-14 22:17:06 +00001414 //
1415 // C++ doesn't need this, and isTagName doesn't take SS.
1416 if (SS == 0) {
Argyrios Kyrtzidisb8a9d3b2011-04-21 17:29:47 +00001417 const char *TagName = 0, *FixitTagName = 0;
Chris Lattnerf4382f52009-04-14 22:17:06 +00001418 tok::TokenKind TagKind = tok::unknown;
Mike Stump1eb44332009-09-09 15:08:12 +00001419
Douglas Gregor23c94db2010-07-02 17:43:08 +00001420 switch (Actions.isTagName(*Tok.getIdentifierInfo(), getCurScope())) {
Chris Lattnere40c2952009-04-14 21:34:55 +00001421 default: break;
Argyrios Kyrtzidisb8a9d3b2011-04-21 17:29:47 +00001422 case DeclSpec::TST_enum:
1423 TagName="enum" ; FixitTagName = "enum " ; TagKind=tok::kw_enum ;break;
1424 case DeclSpec::TST_union:
1425 TagName="union" ; FixitTagName = "union " ;TagKind=tok::kw_union ;break;
1426 case DeclSpec::TST_struct:
1427 TagName="struct"; FixitTagName = "struct ";TagKind=tok::kw_struct;break;
1428 case DeclSpec::TST_class:
1429 TagName="class" ; FixitTagName = "class " ;TagKind=tok::kw_class ;break;
Chris Lattnere40c2952009-04-14 21:34:55 +00001430 }
Mike Stump1eb44332009-09-09 15:08:12 +00001431
Chris Lattnerf4382f52009-04-14 22:17:06 +00001432 if (TagName) {
1433 Diag(Loc, diag::err_use_of_tag_name_without_tag)
John McCall23e907a2010-02-14 01:03:10 +00001434 << Tok.getIdentifierInfo() << TagName << getLang().CPlusPlus
Argyrios Kyrtzidisb8a9d3b2011-04-21 17:29:47 +00001435 << FixItHint::CreateInsertion(Tok.getLocation(),FixitTagName);
Mike Stump1eb44332009-09-09 15:08:12 +00001436
Chris Lattnerf4382f52009-04-14 22:17:06 +00001437 // Parse this as a tag as if the missing tag were present.
1438 if (TagKind == tok::kw_enum)
Douglas Gregor9b9edd62010-03-02 17:53:14 +00001439 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS);
Chris Lattnerf4382f52009-04-14 22:17:06 +00001440 else
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001441 ParseClassSpecifier(TagKind, Loc, DS, TemplateInfo, AS);
Chris Lattnerf4382f52009-04-14 22:17:06 +00001442 return true;
1443 }
Chris Lattnere40c2952009-04-14 21:34:55 +00001444 }
Mike Stump1eb44332009-09-09 15:08:12 +00001445
Douglas Gregora786fdb2009-10-13 23:27:22 +00001446 // This is almost certainly an invalid type name. Let the action emit a
1447 // diagnostic and attempt to recover.
John McCallb3d87482010-08-24 05:47:05 +00001448 ParsedType T;
Douglas Gregora786fdb2009-10-13 23:27:22 +00001449 if (Actions.DiagnoseUnknownTypeName(*Tok.getIdentifierInfo(), Loc,
Douglas Gregor23c94db2010-07-02 17:43:08 +00001450 getCurScope(), SS, T)) {
Douglas Gregora786fdb2009-10-13 23:27:22 +00001451 // The action emitted a diagnostic, so we don't have to.
1452 if (T) {
1453 // The action has suggested that the type T could be used. Set that as
1454 // the type in the declaration specifiers, consume the would-be type
1455 // name token, and we're done.
1456 const char *PrevSpec;
1457 unsigned DiagID;
John McCallb3d87482010-08-24 05:47:05 +00001458 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID, T);
Douglas Gregora786fdb2009-10-13 23:27:22 +00001459 DS.SetRangeEnd(Tok.getLocation());
1460 ConsumeToken();
1461
1462 // There may be other declaration specifiers after this.
1463 return true;
1464 }
1465
1466 // Fall through; the action had no suggestion for us.
1467 } else {
1468 // The action did not emit a diagnostic, so emit one now.
1469 SourceRange R;
1470 if (SS) R = SS->getRange();
1471 Diag(Loc, diag::err_unknown_typename) << Tok.getIdentifierInfo() << R;
1472 }
Mike Stump1eb44332009-09-09 15:08:12 +00001473
Douglas Gregora786fdb2009-10-13 23:27:22 +00001474 // Mark this as an error.
Chris Lattnere40c2952009-04-14 21:34:55 +00001475 const char *PrevSpec;
John McCallfec54012009-08-03 20:12:06 +00001476 unsigned DiagID;
1477 DS.SetTypeSpecType(DeclSpec::TST_error, Loc, PrevSpec, DiagID);
Chris Lattnere40c2952009-04-14 21:34:55 +00001478 DS.SetRangeEnd(Tok.getLocation());
1479 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001480
Chris Lattnere40c2952009-04-14 21:34:55 +00001481 // TODO: Could inject an invalid typedef decl in an enclosing scope to
1482 // avoid rippling error messages on subsequent uses of the same type,
1483 // could be useful if #include was forgotten.
1484 return false;
1485}
1486
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001487/// \brief Determine the declaration specifier context from the declarator
1488/// context.
1489///
1490/// \param Context the declarator context, which is one of the
1491/// Declarator::TheContext enumerator values.
1492Parser::DeclSpecContext
1493Parser::getDeclSpecContextFromDeclaratorContext(unsigned Context) {
1494 if (Context == Declarator::MemberContext)
1495 return DSC_class;
1496 if (Context == Declarator::FileContext)
1497 return DSC_top_level;
1498 return DSC_normal;
1499}
1500
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001501/// ParseAlignArgument - Parse the argument to an alignment-specifier.
1502///
1503/// FIXME: Simply returns an alignof() expression if the argument is a
1504/// type. Ideally, the type should be propagated directly into Sema.
1505///
1506/// [C1X/C++0x] type-id
1507/// [C1X] constant-expression
1508/// [C++0x] assignment-expression
1509ExprResult Parser::ParseAlignArgument(SourceLocation Start) {
1510 if (isTypeIdInParens()) {
1511 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated);
1512 SourceLocation TypeLoc = Tok.getLocation();
1513 ParsedType Ty = ParseTypeName().get();
1514 SourceRange TypeRange(Start, Tok.getLocation());
1515 return Actions.ActOnUnaryExprOrTypeTraitExpr(TypeLoc, UETT_AlignOf, true,
1516 Ty.getAsOpaquePtr(), TypeRange);
1517 } else
1518 return ParseConstantExpression();
1519}
1520
1521/// ParseAlignmentSpecifier - Parse an alignment-specifier, and add the
1522/// attribute to Attrs.
1523///
1524/// alignment-specifier:
1525/// [C1X] '_Alignas' '(' type-id ')'
1526/// [C1X] '_Alignas' '(' constant-expression ')'
1527/// [C++0x] 'alignas' '(' type-id ')'
1528/// [C++0x] 'alignas' '(' assignment-expression ')'
1529void Parser::ParseAlignmentSpecifier(ParsedAttributes &Attrs,
1530 SourceLocation *endLoc) {
1531 assert((Tok.is(tok::kw_alignas) || Tok.is(tok::kw__Alignas)) &&
1532 "Not an alignment-specifier!");
1533
1534 SourceLocation KWLoc = Tok.getLocation();
1535 ConsumeToken();
1536
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001537 BalancedDelimiterTracker T(*this, tok::l_paren);
1538 if (T.expectAndConsume(diag::err_expected_lparen))
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001539 return;
1540
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001541 ExprResult ArgExpr = ParseAlignArgument(T.getOpenLocation());
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001542 if (ArgExpr.isInvalid()) {
1543 SkipUntil(tok::r_paren);
1544 return;
1545 }
1546
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001547 T.consumeClose();
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001548 if (endLoc)
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001549 *endLoc = T.getCloseLocation();
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001550
1551 ExprVector ArgExprs(Actions);
1552 ArgExprs.push_back(ArgExpr.release());
1553 Attrs.addNew(PP.getIdentifierInfo("aligned"), KWLoc, 0, KWLoc,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001554 0, T.getOpenLocation(), ArgExprs.take(), 1, false, true);
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001555}
1556
Reid Spencer5f016e22007-07-11 17:01:13 +00001557/// ParseDeclarationSpecifiers
1558/// declaration-specifiers: [C99 6.7]
1559/// storage-class-specifier declaration-specifiers[opt]
1560/// type-specifier declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00001561/// [C99] function-specifier declaration-specifiers[opt]
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001562/// [C1X] alignment-specifier declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00001563/// [GNU] attributes declaration-specifiers[opt]
Douglas Gregor8d267c52011-09-09 02:06:17 +00001564/// [Clang] '__module_private__' declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00001565///
1566/// storage-class-specifier: [C99 6.7.1]
1567/// 'typedef'
1568/// 'extern'
1569/// 'static'
1570/// 'auto'
1571/// 'register'
Sebastian Redl669d5d72008-11-14 23:42:31 +00001572/// [C++] 'mutable'
Reid Spencer5f016e22007-07-11 17:01:13 +00001573/// [GNU] '__thread'
Reid Spencer5f016e22007-07-11 17:01:13 +00001574/// function-specifier: [C99 6.7.4]
1575/// [C99] 'inline'
Douglas Gregorb48fe382008-10-31 09:07:45 +00001576/// [C++] 'virtual'
1577/// [C++] 'explicit'
Peter Collingbournef315fa82011-02-14 01:42:53 +00001578/// [OpenCL] '__kernel'
Anders Carlssonf47f7a12009-05-06 04:46:28 +00001579/// 'friend': [C++ dcl.friend]
Sebastian Redl2ac67232009-11-05 15:47:02 +00001580/// 'constexpr': [C++0x dcl.constexpr]
Anders Carlssonf47f7a12009-05-06 04:46:28 +00001581
Reid Spencer5f016e22007-07-11 17:01:13 +00001582///
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +00001583void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001584 const ParsedTemplateInfo &TemplateInfo,
John McCall67d1a672009-08-06 02:15:43 +00001585 AccessSpecifier AS,
Douglas Gregor312eadb2011-04-24 05:37:28 +00001586 DeclSpecContext DSContext) {
1587 if (DS.getSourceRange().isInvalid()) {
1588 DS.SetRangeStart(Tok.getLocation());
1589 DS.SetRangeEnd(Tok.getLocation());
1590 }
1591
Reid Spencer5f016e22007-07-11 17:01:13 +00001592 while (1) {
John McCallfec54012009-08-03 20:12:06 +00001593 bool isInvalid = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001594 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00001595 unsigned DiagID = 0;
1596
Reid Spencer5f016e22007-07-11 17:01:13 +00001597 SourceLocation Loc = Tok.getLocation();
Douglas Gregor12e083c2008-11-07 15:42:26 +00001598
Reid Spencer5f016e22007-07-11 17:01:13 +00001599 switch (Tok.getKind()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001600 default:
Chris Lattnerbce61352008-07-26 00:20:22 +00001601 DoneWithDeclSpec:
Peter Collingbournef1907682011-09-29 18:03:57 +00001602 // [C++0x] decl-specifier-seq: decl-specifier attribute-specifier-seq[opt]
1603 MaybeParseCXX0XAttributes(DS.getAttributes());
1604
Reid Spencer5f016e22007-07-11 17:01:13 +00001605 // If this is not a declaration specifier token, we're done reading decl
1606 // specifiers. First verify that DeclSpec's are consistent.
Douglas Gregor9b3064b2009-04-01 22:41:11 +00001607 DS.Finish(Diags, PP);
Reid Spencer5f016e22007-07-11 17:01:13 +00001608 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001609
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001610 case tok::code_completion: {
John McCallf312b1e2010-08-26 23:41:50 +00001611 Sema::ParserCompletionContext CCC = Sema::PCC_Namespace;
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001612 if (DS.hasTypeSpecifier()) {
1613 bool AllowNonIdentifiers
1614 = (getCurScope()->getFlags() & (Scope::ControlScope |
1615 Scope::BlockScope |
1616 Scope::TemplateParamScope |
1617 Scope::FunctionPrototypeScope |
1618 Scope::AtCatchScope)) == 0;
1619 bool AllowNestedNameSpecifiers
1620 = DSContext == DSC_top_level ||
1621 (DSContext == DSC_class && DS.isFriendSpecified());
1622
Douglas Gregorc7b6d882010-09-16 15:14:18 +00001623 Actions.CodeCompleteDeclSpec(getCurScope(), DS,
1624 AllowNonIdentifiers,
1625 AllowNestedNameSpecifiers);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001626 return cutOffParsing();
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001627 }
1628
Douglas Gregor68e3c2e2011-02-15 20:33:25 +00001629 if (getCurScope()->getFnParent() || getCurScope()->getBlockParent())
1630 CCC = Sema::PCC_LocalDeclarationSpecifiers;
1631 else if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate)
John McCallf312b1e2010-08-26 23:41:50 +00001632 CCC = DSContext == DSC_class? Sema::PCC_MemberTemplate
1633 : Sema::PCC_Template;
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001634 else if (DSContext == DSC_class)
John McCallf312b1e2010-08-26 23:41:50 +00001635 CCC = Sema::PCC_Class;
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001636 else if (ObjCImpDecl)
John McCallf312b1e2010-08-26 23:41:50 +00001637 CCC = Sema::PCC_ObjCImplementation;
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001638
1639 Actions.CodeCompleteOrdinaryName(getCurScope(), CCC);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001640 return cutOffParsing();
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001641 }
1642
Chris Lattner5e02c472009-01-05 00:07:25 +00001643 case tok::coloncolon: // ::foo::bar
John McCall9ba61662010-02-26 08:45:28 +00001644 // C++ scope specifier. Annotate and loop, or bail out on error.
1645 if (TryAnnotateCXXScopeToken(true)) {
1646 if (!DS.hasTypeSpecifier())
1647 DS.SetTypeSpecError();
1648 goto DoneWithDeclSpec;
1649 }
John McCall2e0a7152010-03-01 18:20:46 +00001650 if (Tok.is(tok::coloncolon)) // ::new or ::delete
1651 goto DoneWithDeclSpec;
John McCall9ba61662010-02-26 08:45:28 +00001652 continue;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001653
1654 case tok::annot_cxxscope: {
1655 if (DS.hasTypeSpecifier())
1656 goto DoneWithDeclSpec;
1657
John McCallaa87d332009-12-12 11:40:51 +00001658 CXXScopeSpec SS;
Douglas Gregorc34348a2011-02-24 17:54:50 +00001659 Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(),
1660 Tok.getAnnotationRange(),
1661 SS);
John McCallaa87d332009-12-12 11:40:51 +00001662
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001663 // We are looking for a qualified typename.
Douglas Gregor9135c722009-03-25 15:40:00 +00001664 Token Next = NextToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001665 if (Next.is(tok::annot_template_id) &&
Douglas Gregor9135c722009-03-25 15:40:00 +00001666 static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue())
Douglas Gregorc45c2322009-03-31 00:43:58 +00001667 ->Kind == TNK_Type_template) {
Douglas Gregor9135c722009-03-25 15:40:00 +00001668 // We have a qualified template-id, e.g., N::A<int>
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001669
1670 // C++ [class.qual]p2:
1671 // In a lookup in which the constructor is an acceptable lookup
1672 // result and the nested-name-specifier nominates a class C:
1673 //
1674 // - if the name specified after the
1675 // nested-name-specifier, when looked up in C, is the
1676 // injected-class-name of C (Clause 9), or
1677 //
1678 // - if the name specified after the nested-name-specifier
1679 // is the same as the identifier or the
1680 // simple-template-id's template-name in the last
1681 // component of the nested-name-specifier,
1682 //
1683 // the name is instead considered to name the constructor of
1684 // class C.
1685 //
1686 // Thus, if the template-name is actually the constructor
1687 // name, then the code is ill-formed; this interpretation is
1688 // reinforced by the NAD status of core issue 635.
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +00001689 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Next);
John McCallba9d8532010-04-13 06:39:49 +00001690 if ((DSContext == DSC_top_level ||
1691 (DSContext == DSC_class && DS.isFriendSpecified())) &&
1692 TemplateId->Name &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00001693 Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) {
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001694 if (isConstructorDeclarator()) {
1695 // The user meant this to be an out-of-line constructor
1696 // definition, but template arguments are not allowed
1697 // there. Just allow this as a constructor; we'll
1698 // complain about it later.
1699 goto DoneWithDeclSpec;
1700 }
1701
1702 // The user meant this to name a type, but it actually names
1703 // a constructor with some extraneous template
1704 // arguments. Complain, then parse it as a type as the user
1705 // intended.
1706 Diag(TemplateId->TemplateNameLoc,
1707 diag::err_out_of_line_template_id_names_constructor)
1708 << TemplateId->Name;
1709 }
1710
John McCallaa87d332009-12-12 11:40:51 +00001711 DS.getTypeSpecScope() = SS;
1712 ConsumeToken(); // The C++ scope.
Mike Stump1eb44332009-09-09 15:08:12 +00001713 assert(Tok.is(tok::annot_template_id) &&
Douglas Gregor9135c722009-03-25 15:40:00 +00001714 "ParseOptionalCXXScopeSpecifier not working");
Douglas Gregor059101f2011-03-02 00:47:37 +00001715 AnnotateTemplateIdTokenAsType();
Douglas Gregor9135c722009-03-25 15:40:00 +00001716 continue;
1717 }
1718
Douglas Gregor9d7b3532009-09-28 07:26:33 +00001719 if (Next.is(tok::annot_typename)) {
John McCallaa87d332009-12-12 11:40:51 +00001720 DS.getTypeSpecScope() = SS;
1721 ConsumeToken(); // The C++ scope.
John McCallb3d87482010-08-24 05:47:05 +00001722 if (Tok.getAnnotationValue()) {
1723 ParsedType T = getTypeAnnotation(Tok);
Nico Weber253e80b2010-11-22 10:30:56 +00001724 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename,
1725 Tok.getAnnotationEndLoc(),
John McCallb3d87482010-08-24 05:47:05 +00001726 PrevSpec, DiagID, T);
1727 }
Douglas Gregor9d7b3532009-09-28 07:26:33 +00001728 else
1729 DS.SetTypeSpecError();
1730 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1731 ConsumeToken(); // The typename
1732 }
1733
Douglas Gregor9135c722009-03-25 15:40:00 +00001734 if (Next.isNot(tok::identifier))
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001735 goto DoneWithDeclSpec;
1736
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001737 // If we're in a context where the identifier could be a class name,
1738 // check whether this is a constructor declaration.
John McCallba9d8532010-04-13 06:39:49 +00001739 if ((DSContext == DSC_top_level ||
1740 (DSContext == DSC_class && DS.isFriendSpecified())) &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00001741 Actions.isCurrentClassName(*Next.getIdentifierInfo(), getCurScope(),
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001742 &SS)) {
1743 if (isConstructorDeclarator())
1744 goto DoneWithDeclSpec;
1745
1746 // As noted in C++ [class.qual]p2 (cited above), when the name
1747 // of the class is qualified in a context where it could name
1748 // a constructor, its a constructor name. However, we've
1749 // looked at the declarator, and the user probably meant this
1750 // to be a type. Complain that it isn't supposed to be treated
1751 // as a type, then proceed to parse it as a type.
1752 Diag(Next.getLocation(), diag::err_out_of_line_type_names_constructor)
1753 << Next.getIdentifierInfo();
1754 }
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001755
John McCallb3d87482010-08-24 05:47:05 +00001756 ParsedType TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(),
1757 Next.getLocation(),
Douglas Gregor9e876872011-03-01 18:12:44 +00001758 getCurScope(), &SS,
1759 false, false, ParsedType(),
1760 /*NonTrivialSourceInfo=*/true);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001761
Chris Lattnerf4382f52009-04-14 22:17:06 +00001762 // If the referenced identifier is not a type, then this declspec is
1763 // erroneous: We already checked about that it has no type specifier, and
1764 // C++ doesn't have implicit int. Diagnose it as a typo w.r.t. to the
Mike Stump1eb44332009-09-09 15:08:12 +00001765 // typename.
Chris Lattnerf4382f52009-04-14 22:17:06 +00001766 if (TypeRep == 0) {
1767 ConsumeToken(); // Eat the scope spec so the identifier is current.
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001768 if (ParseImplicitInt(DS, &SS, TemplateInfo, AS)) continue;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001769 goto DoneWithDeclSpec;
Chris Lattnerf4382f52009-04-14 22:17:06 +00001770 }
Mike Stump1eb44332009-09-09 15:08:12 +00001771
John McCallaa87d332009-12-12 11:40:51 +00001772 DS.getTypeSpecScope() = SS;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001773 ConsumeToken(); // The C++ scope.
1774
Douglas Gregor1a51b4a2009-02-09 15:09:02 +00001775 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001776 DiagID, TypeRep);
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001777 if (isInvalid)
1778 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001779
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001780 DS.SetRangeEnd(Tok.getLocation());
1781 ConsumeToken(); // The typename.
1782
1783 continue;
1784 }
Mike Stump1eb44332009-09-09 15:08:12 +00001785
Chris Lattner80d0c892009-01-21 19:48:37 +00001786 case tok::annot_typename: {
John McCallb3d87482010-08-24 05:47:05 +00001787 if (Tok.getAnnotationValue()) {
1788 ParsedType T = getTypeAnnotation(Tok);
Nico Weberc43271e2010-11-22 12:50:03 +00001789 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallb3d87482010-08-24 05:47:05 +00001790 DiagID, T);
1791 } else
Douglas Gregor31a19b62009-04-01 21:51:26 +00001792 DS.SetTypeSpecError();
Chris Lattner5c5db552010-04-05 18:18:31 +00001793
1794 if (isInvalid)
1795 break;
1796
Chris Lattner80d0c892009-01-21 19:48:37 +00001797 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1798 ConsumeToken(); // The typename
Mike Stump1eb44332009-09-09 15:08:12 +00001799
Chris Lattner80d0c892009-01-21 19:48:37 +00001800 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1801 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001802 // Objective-C interface.
1803 if (Tok.is(tok::less) && getLang().ObjC1)
1804 ParseObjCProtocolQualifiers(DS);
1805
Chris Lattner80d0c892009-01-21 19:48:37 +00001806 continue;
1807 }
Mike Stump1eb44332009-09-09 15:08:12 +00001808
Douglas Gregorbfad9152011-04-28 15:48:45 +00001809 case tok::kw___is_signed:
1810 // GNU libstdc++ 4.4 uses __is_signed as an identifier, but Clang
1811 // typically treats it as a trait. If we see __is_signed as it appears
1812 // in libstdc++, e.g.,
1813 //
1814 // static const bool __is_signed;
1815 //
1816 // then treat __is_signed as an identifier rather than as a keyword.
1817 if (DS.getTypeSpecType() == TST_bool &&
1818 DS.getTypeQualifiers() == DeclSpec::TQ_const &&
1819 DS.getStorageClassSpec() == DeclSpec::SCS_static) {
1820 Tok.getIdentifierInfo()->RevertTokenIDToIdentifier();
1821 Tok.setKind(tok::identifier);
1822 }
1823
1824 // We're done with the declaration-specifiers.
1825 goto DoneWithDeclSpec;
1826
Chris Lattner3bd934a2008-07-26 01:18:38 +00001827 // typedef-name
1828 case tok::identifier: {
Chris Lattner5e02c472009-01-05 00:07:25 +00001829 // In C++, check to see if this is a scope specifier like foo::bar::, if
1830 // so handle it as such. This is important for ctor parsing.
John McCall9ba61662010-02-26 08:45:28 +00001831 if (getLang().CPlusPlus) {
1832 if (TryAnnotateCXXScopeToken(true)) {
1833 if (!DS.hasTypeSpecifier())
1834 DS.SetTypeSpecError();
1835 goto DoneWithDeclSpec;
1836 }
1837 if (!Tok.is(tok::identifier))
1838 continue;
1839 }
Mike Stump1eb44332009-09-09 15:08:12 +00001840
Chris Lattner3bd934a2008-07-26 01:18:38 +00001841 // This identifier can only be a typedef name if we haven't already seen
1842 // a type-specifier. Without this check we misparse:
1843 // typedef int X; struct Y { short X; }; as 'short int'.
1844 if (DS.hasTypeSpecifier())
1845 goto DoneWithDeclSpec;
Mike Stump1eb44332009-09-09 15:08:12 +00001846
John Thompson82287d12010-02-05 00:12:22 +00001847 // Check for need to substitute AltiVec keyword tokens.
1848 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
1849 break;
1850
Chris Lattner3bd934a2008-07-26 01:18:38 +00001851 // It has to be available as a typedef too!
John McCallb3d87482010-08-24 05:47:05 +00001852 ParsedType TypeRep =
1853 Actions.getTypeName(*Tok.getIdentifierInfo(),
1854 Tok.getLocation(), getCurScope());
Douglas Gregor55f6b142009-02-09 18:46:07 +00001855
Chris Lattnerc199ab32009-04-12 20:42:31 +00001856 // If this is not a typedef name, don't parse it as part of the declspec,
1857 // it must be an implicit int or an error.
John McCallb3d87482010-08-24 05:47:05 +00001858 if (!TypeRep) {
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001859 if (ParseImplicitInt(DS, 0, TemplateInfo, AS)) continue;
Chris Lattner3bd934a2008-07-26 01:18:38 +00001860 goto DoneWithDeclSpec;
Chris Lattnerc199ab32009-04-12 20:42:31 +00001861 }
Douglas Gregor55f6b142009-02-09 18:46:07 +00001862
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001863 // If we're in a context where the identifier could be a class name,
1864 // check whether this is a constructor declaration.
1865 if (getLang().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00001866 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), getCurScope()) &&
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001867 isConstructorDeclarator())
Douglas Gregorb48fe382008-10-31 09:07:45 +00001868 goto DoneWithDeclSpec;
1869
Douglas Gregor1a51b4a2009-02-09 15:09:02 +00001870 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001871 DiagID, TypeRep);
Chris Lattner3bd934a2008-07-26 01:18:38 +00001872 if (isInvalid)
1873 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001874
Chris Lattner3bd934a2008-07-26 01:18:38 +00001875 DS.SetRangeEnd(Tok.getLocation());
1876 ConsumeToken(); // The identifier
1877
1878 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1879 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001880 // Objective-C interface.
1881 if (Tok.is(tok::less) && getLang().ObjC1)
1882 ParseObjCProtocolQualifiers(DS);
1883
Steve Naroff4f9b9f12008-09-22 10:28:57 +00001884 // Need to support trailing type qualifiers (e.g. "id<p> const").
1885 // If a type specifier follows, it will be diagnosed elsewhere.
1886 continue;
Chris Lattner3bd934a2008-07-26 01:18:38 +00001887 }
Douglas Gregor39a8de12009-02-25 19:37:18 +00001888
1889 // type-name
1890 case tok::annot_template_id: {
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +00001891 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregorc45c2322009-03-31 00:43:58 +00001892 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor39a8de12009-02-25 19:37:18 +00001893 // This template-id does not refer to a type name, so we're
1894 // done with the type-specifiers.
1895 goto DoneWithDeclSpec;
1896 }
1897
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001898 // If we're in a context where the template-id could be a
1899 // constructor name or specialization, check whether this is a
1900 // constructor declaration.
1901 if (getLang().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00001902 Actions.isCurrentClassName(*TemplateId->Name, getCurScope()) &&
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001903 isConstructorDeclarator())
1904 goto DoneWithDeclSpec;
1905
Douglas Gregor39a8de12009-02-25 19:37:18 +00001906 // Turn the template-id annotation token into a type annotation
1907 // token, then try again to parse it as a type-specifier.
Douglas Gregor31a19b62009-04-01 21:51:26 +00001908 AnnotateTemplateIdTokenAsType();
Douglas Gregor39a8de12009-02-25 19:37:18 +00001909 continue;
1910 }
1911
Reid Spencer5f016e22007-07-11 17:01:13 +00001912 // GNU attributes support.
1913 case tok::kw___attribute:
John McCall7f040a92010-12-24 02:08:15 +00001914 ParseGNUAttributes(DS.getAttributes());
Reid Spencer5f016e22007-07-11 17:01:13 +00001915 continue;
Steve Narofff59e17e2008-12-24 20:59:21 +00001916
1917 // Microsoft declspec support.
1918 case tok::kw___declspec:
John McCall7f040a92010-12-24 02:08:15 +00001919 ParseMicrosoftDeclSpec(DS.getAttributes());
Steve Narofff59e17e2008-12-24 20:59:21 +00001920 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001921
Steve Naroff239f0732008-12-25 14:16:32 +00001922 // Microsoft single token adornments.
Steve Naroff86bc6cf2008-12-25 14:41:26 +00001923 case tok::kw___forceinline:
Eli Friedman290eeb02009-06-08 23:27:34 +00001924 // FIXME: Add handling here!
1925 break;
1926
1927 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00001928 case tok::kw___ptr32:
Steve Naroff86bc6cf2008-12-25 14:41:26 +00001929 case tok::kw___w64:
Steve Naroff239f0732008-12-25 14:16:32 +00001930 case tok::kw___cdecl:
1931 case tok::kw___stdcall:
1932 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00001933 case tok::kw___thiscall:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00001934 case tok::kw___unaligned:
John McCall7f040a92010-12-24 02:08:15 +00001935 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman290eeb02009-06-08 23:27:34 +00001936 continue;
1937
Dawn Perchik52fc3142010-09-03 01:29:35 +00001938 // Borland single token adornments.
1939 case tok::kw___pascal:
John McCall7f040a92010-12-24 02:08:15 +00001940 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik52fc3142010-09-03 01:29:35 +00001941 continue;
1942
Peter Collingbournef315fa82011-02-14 01:42:53 +00001943 // OpenCL single token adornments.
1944 case tok::kw___kernel:
1945 ParseOpenCLAttributes(DS.getAttributes());
1946 continue;
1947
Reid Spencer5f016e22007-07-11 17:01:13 +00001948 // storage-class-specifier
1949 case tok::kw_typedef:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00001950 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_typedef, Loc,
1951 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001952 break;
1953 case tok::kw_extern:
1954 if (DS.isThreadSpecified())
Chris Lattner1ab3b962008-11-18 07:48:38 +00001955 Diag(Tok, diag::ext_thread_before) << "extern";
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00001956 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_extern, Loc,
1957 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001958 break;
Steve Naroff8d54bf22007-12-18 00:16:02 +00001959 case tok::kw___private_extern__:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00001960 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_private_extern,
1961 Loc, PrevSpec, DiagID);
Steve Naroff8d54bf22007-12-18 00:16:02 +00001962 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00001963 case tok::kw_static:
1964 if (DS.isThreadSpecified())
Chris Lattner1ab3b962008-11-18 07:48:38 +00001965 Diag(Tok, diag::ext_thread_before) << "static";
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00001966 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_static, Loc,
1967 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001968 break;
1969 case tok::kw_auto:
Douglas Gregor18d8b792011-03-14 21:43:30 +00001970 if (getLang().CPlusPlus0x) {
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00001971 if (isKnownToBeTypeSpecifier(GetLookAheadToken(1))) {
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00001972 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc,
1973 PrevSpec, DiagID);
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00001974 if (!isInvalid)
Richard Smith8f4fb192011-09-04 19:54:14 +00001975 Diag(Tok, diag::ext_auto_storage_class)
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00001976 << FixItHint::CreateRemoval(DS.getStorageClassSpecLoc());
Richard Smith8f4fb192011-09-04 19:54:14 +00001977 } else
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00001978 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec,
1979 DiagID);
Richard Smith8f4fb192011-09-04 19:54:14 +00001980 } else
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00001981 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc,
1982 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001983 break;
1984 case tok::kw_register:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00001985 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_register, Loc,
1986 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001987 break;
Sebastian Redl669d5d72008-11-14 23:42:31 +00001988 case tok::kw_mutable:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00001989 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_mutable, Loc,
1990 PrevSpec, DiagID);
Sebastian Redl669d5d72008-11-14 23:42:31 +00001991 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00001992 case tok::kw___thread:
John McCallfec54012009-08-03 20:12:06 +00001993 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001994 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001995
Reid Spencer5f016e22007-07-11 17:01:13 +00001996 // function-specifier
1997 case tok::kw_inline:
John McCallfec54012009-08-03 20:12:06 +00001998 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001999 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +00002000 case tok::kw_virtual:
John McCallfec54012009-08-03 20:12:06 +00002001 isInvalid = DS.SetFunctionSpecVirtual(Loc, PrevSpec, DiagID);
Douglas Gregorb48fe382008-10-31 09:07:45 +00002002 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +00002003 case tok::kw_explicit:
John McCallfec54012009-08-03 20:12:06 +00002004 isInvalid = DS.SetFunctionSpecExplicit(Loc, PrevSpec, DiagID);
Douglas Gregorb48fe382008-10-31 09:07:45 +00002005 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002006
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002007 // alignment-specifier
2008 case tok::kw__Alignas:
2009 if (!getLang().C1X)
2010 Diag(Tok, diag::ext_c1x_alignas);
2011 ParseAlignmentSpecifier(DS.getAttributes());
2012 continue;
2013
Anders Carlssonf47f7a12009-05-06 04:46:28 +00002014 // friend
2015 case tok::kw_friend:
John McCall67d1a672009-08-06 02:15:43 +00002016 if (DSContext == DSC_class)
2017 isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID);
2018 else {
2019 PrevSpec = ""; // not actually used by the diagnostic
2020 DiagID = diag::err_friend_invalid_in_context;
2021 isInvalid = true;
2022 }
Anders Carlssonf47f7a12009-05-06 04:46:28 +00002023 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002024
Douglas Gregor8d267c52011-09-09 02:06:17 +00002025 // Modules
2026 case tok::kw___module_private__:
2027 isInvalid = DS.setModulePrivateSpec(Loc, PrevSpec, DiagID);
2028 break;
2029
Sebastian Redl2ac67232009-11-05 15:47:02 +00002030 // constexpr
2031 case tok::kw_constexpr:
2032 isInvalid = DS.SetConstexprSpec(Loc, PrevSpec, DiagID);
2033 break;
2034
Chris Lattner80d0c892009-01-21 19:48:37 +00002035 // type-specifier
2036 case tok::kw_short:
John McCallfec54012009-08-03 20:12:06 +00002037 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec,
2038 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002039 break;
2040 case tok::kw_long:
2041 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCallfec54012009-08-03 20:12:06 +00002042 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
2043 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002044 else
John McCallfec54012009-08-03 20:12:06 +00002045 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2046 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002047 break;
Francois Pichet338d7f72011-04-28 01:59:37 +00002048 case tok::kw___int64:
2049 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2050 DiagID);
2051 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002052 case tok::kw_signed:
John McCallfec54012009-08-03 20:12:06 +00002053 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec,
2054 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002055 break;
2056 case tok::kw_unsigned:
John McCallfec54012009-08-03 20:12:06 +00002057 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
2058 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002059 break;
2060 case tok::kw__Complex:
John McCallfec54012009-08-03 20:12:06 +00002061 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
2062 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002063 break;
2064 case tok::kw__Imaginary:
John McCallfec54012009-08-03 20:12:06 +00002065 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
2066 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002067 break;
2068 case tok::kw_void:
John McCallfec54012009-08-03 20:12:06 +00002069 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec,
2070 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002071 break;
2072 case tok::kw_char:
John McCallfec54012009-08-03 20:12:06 +00002073 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec,
2074 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002075 break;
2076 case tok::kw_int:
John McCallfec54012009-08-03 20:12:06 +00002077 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec,
2078 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002079 break;
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00002080 case tok::kw_half:
2081 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_half, Loc, PrevSpec,
2082 DiagID);
2083 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002084 case tok::kw_float:
John McCallfec54012009-08-03 20:12:06 +00002085 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec,
2086 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002087 break;
2088 case tok::kw_double:
John McCallfec54012009-08-03 20:12:06 +00002089 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec,
2090 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002091 break;
2092 case tok::kw_wchar_t:
John McCallfec54012009-08-03 20:12:06 +00002093 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec,
2094 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002095 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002096 case tok::kw_char16_t:
John McCallfec54012009-08-03 20:12:06 +00002097 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec,
2098 DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002099 break;
2100 case tok::kw_char32_t:
John McCallfec54012009-08-03 20:12:06 +00002101 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec,
2102 DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002103 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002104 case tok::kw_bool:
2105 case tok::kw__Bool:
Argyrios Kyrtzidis4383e182010-11-16 18:18:13 +00002106 if (Tok.is(tok::kw_bool) &&
2107 DS.getTypeSpecType() != DeclSpec::TST_unspecified &&
2108 DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
2109 PrevSpec = ""; // Not used by the diagnostic.
2110 DiagID = diag::err_bool_redeclaration;
Fariborz Jahaniane106a0b2011-04-19 21:42:37 +00002111 // For better error recovery.
2112 Tok.setKind(tok::identifier);
Argyrios Kyrtzidis4383e182010-11-16 18:18:13 +00002113 isInvalid = true;
2114 } else {
2115 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec,
2116 DiagID);
2117 }
Chris Lattner80d0c892009-01-21 19:48:37 +00002118 break;
2119 case tok::kw__Decimal32:
John McCallfec54012009-08-03 20:12:06 +00002120 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
2121 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002122 break;
2123 case tok::kw__Decimal64:
John McCallfec54012009-08-03 20:12:06 +00002124 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
2125 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002126 break;
2127 case tok::kw__Decimal128:
John McCallfec54012009-08-03 20:12:06 +00002128 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
2129 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002130 break;
John Thompson82287d12010-02-05 00:12:22 +00002131 case tok::kw___vector:
2132 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
2133 break;
2134 case tok::kw___pixel:
2135 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
2136 break;
John McCalla5fc4722011-04-09 22:50:59 +00002137 case tok::kw___unknown_anytype:
2138 isInvalid = DS.SetTypeSpecType(TST_unknown_anytype, Loc,
2139 PrevSpec, DiagID);
2140 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002141
2142 // class-specifier:
2143 case tok::kw_class:
2144 case tok::kw_struct:
Chris Lattner4c97d762009-04-12 21:49:30 +00002145 case tok::kw_union: {
2146 tok::TokenKind Kind = Tok.getKind();
2147 ConsumeToken();
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00002148 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS);
Chris Lattner80d0c892009-01-21 19:48:37 +00002149 continue;
Chris Lattner4c97d762009-04-12 21:49:30 +00002150 }
Chris Lattner80d0c892009-01-21 19:48:37 +00002151
2152 // enum-specifier:
2153 case tok::kw_enum:
Chris Lattner4c97d762009-04-12 21:49:30 +00002154 ConsumeToken();
Douglas Gregor9b9edd62010-03-02 17:53:14 +00002155 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS);
Chris Lattner80d0c892009-01-21 19:48:37 +00002156 continue;
2157
2158 // cv-qualifier:
2159 case tok::kw_const:
John McCallfec54012009-08-03 20:12:06 +00002160 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec, DiagID,
2161 getLang());
Chris Lattner80d0c892009-01-21 19:48:37 +00002162 break;
2163 case tok::kw_volatile:
John McCallfec54012009-08-03 20:12:06 +00002164 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
2165 getLang());
Chris Lattner80d0c892009-01-21 19:48:37 +00002166 break;
2167 case tok::kw_restrict:
John McCallfec54012009-08-03 20:12:06 +00002168 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
2169 getLang());
Chris Lattner80d0c892009-01-21 19:48:37 +00002170 break;
2171
Douglas Gregord57959a2009-03-27 23:10:48 +00002172 // C++ typename-specifier:
2173 case tok::kw_typename:
John McCall9ba61662010-02-26 08:45:28 +00002174 if (TryAnnotateTypeOrScopeToken()) {
2175 DS.SetTypeSpecError();
2176 goto DoneWithDeclSpec;
2177 }
2178 if (!Tok.is(tok::kw_typename))
Douglas Gregord57959a2009-03-27 23:10:48 +00002179 continue;
2180 break;
2181
Chris Lattner80d0c892009-01-21 19:48:37 +00002182 // GNU typeof support.
2183 case tok::kw_typeof:
2184 ParseTypeofSpecifier(DS);
2185 continue;
2186
Anders Carlsson6fd634f2009-06-24 17:47:40 +00002187 case tok::kw_decltype:
2188 ParseDecltypeSpecifier(DS);
2189 continue;
2190
Sean Huntdb5d44b2011-05-19 05:37:45 +00002191 case tok::kw___underlying_type:
2192 ParseUnderlyingTypeSpecifier(DS);
Eli Friedmanb001de72011-10-06 23:00:33 +00002193 continue;
2194
2195 case tok::kw__Atomic:
2196 ParseAtomicSpecifier(DS);
2197 continue;
Sean Huntdb5d44b2011-05-19 05:37:45 +00002198
Peter Collingbourne207f4d82011-03-18 22:38:29 +00002199 // OpenCL qualifiers:
2200 case tok::kw_private:
2201 if (!getLang().OpenCL)
2202 goto DoneWithDeclSpec;
2203 case tok::kw___private:
2204 case tok::kw___global:
2205 case tok::kw___local:
2206 case tok::kw___constant:
2207 case tok::kw___read_only:
2208 case tok::kw___write_only:
2209 case tok::kw___read_write:
2210 ParseOpenCLQualifiers(DS);
2211 break;
2212
Steve Naroffd3ded1f2008-06-05 00:02:44 +00002213 case tok::less:
Chris Lattner3bd934a2008-07-26 01:18:38 +00002214 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattnerbce61352008-07-26 00:20:22 +00002215 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
2216 // but we support it.
Chris Lattner3bd934a2008-07-26 01:18:38 +00002217 if (DS.hasTypeSpecifier() || !getLang().ObjC1)
Chris Lattnerbce61352008-07-26 00:20:22 +00002218 goto DoneWithDeclSpec;
Mike Stump1eb44332009-09-09 15:08:12 +00002219
Douglas Gregor46f936e2010-11-19 17:10:50 +00002220 if (!ParseObjCProtocolQualifiers(DS))
2221 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
2222 << FixItHint::CreateInsertion(Loc, "id")
2223 << SourceRange(Loc, DS.getSourceRange().getEnd());
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00002224
2225 // Need to support trailing type qualifiers (e.g. "id<p> const").
2226 // If a type specifier follows, it will be diagnosed elsewhere.
2227 continue;
Reid Spencer5f016e22007-07-11 17:01:13 +00002228 }
John McCallfec54012009-08-03 20:12:06 +00002229 // If the specifier wasn't legal, issue a diagnostic.
Reid Spencer5f016e22007-07-11 17:01:13 +00002230 if (isInvalid) {
2231 assert(PrevSpec && "Method did not return previous specifier!");
John McCallfec54012009-08-03 20:12:06 +00002232 assert(DiagID);
Douglas Gregorae2fb142010-08-23 14:34:43 +00002233
2234 if (DiagID == diag::ext_duplicate_declspec)
2235 Diag(Tok, DiagID)
2236 << PrevSpec << FixItHint::CreateRemoval(Tok.getLocation());
2237 else
2238 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00002239 }
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002240
Chris Lattner81c018d2008-03-13 06:29:04 +00002241 DS.SetRangeEnd(Tok.getLocation());
Fariborz Jahaniane106a0b2011-04-19 21:42:37 +00002242 if (DiagID != diag::err_bool_redeclaration)
2243 ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00002244 }
2245}
Douglas Gregoradcac882008-12-01 23:54:00 +00002246
Chris Lattner7a0ab5f2009-01-06 06:59:53 +00002247/// ParseOptionalTypeSpecifier - Try to parse a single type-specifier. We
Douglas Gregor12e083c2008-11-07 15:42:26 +00002248/// primarily follow the C++ grammar with additions for C99 and GNU,
2249/// which together subsume the C grammar. Note that the C++
2250/// type-specifier also includes the C type-qualifier (for const,
2251/// volatile, and C99 restrict). Returns true if a type-specifier was
2252/// found (and parsed), false otherwise.
2253///
2254/// type-specifier: [C++ 7.1.5]
2255/// simple-type-specifier
2256/// class-specifier
2257/// enum-specifier
2258/// elaborated-type-specifier [TODO]
2259/// cv-qualifier
2260///
2261/// cv-qualifier: [C++ 7.1.5.1]
2262/// 'const'
2263/// 'volatile'
2264/// [C99] 'restrict'
2265///
2266/// simple-type-specifier: [ C++ 7.1.5.2]
2267/// '::'[opt] nested-name-specifier[opt] type-name [TODO]
2268/// '::'[opt] nested-name-specifier 'template' template-id [TODO]
2269/// 'char'
2270/// 'wchar_t'
2271/// 'bool'
2272/// 'short'
2273/// 'int'
2274/// 'long'
2275/// 'signed'
2276/// 'unsigned'
2277/// 'float'
2278/// 'double'
2279/// 'void'
2280/// [C99] '_Bool'
2281/// [C99] '_Complex'
2282/// [C99] '_Imaginary' // Removed in TC2?
2283/// [GNU] '_Decimal32'
2284/// [GNU] '_Decimal64'
2285/// [GNU] '_Decimal128'
2286/// [GNU] typeof-specifier
2287/// [OBJC] class-name objc-protocol-refs[opt] [TODO]
2288/// [OBJC] typedef-name objc-protocol-refs[opt] [TODO]
Anders Carlsson6fd634f2009-06-24 17:47:40 +00002289/// [C++0x] 'decltype' ( expression )
John Thompson82287d12010-02-05 00:12:22 +00002290/// [AltiVec] '__vector'
John McCallfec54012009-08-03 20:12:06 +00002291bool Parser::ParseOptionalTypeSpecifier(DeclSpec &DS, bool& isInvalid,
Chris Lattner7a0ab5f2009-01-06 06:59:53 +00002292 const char *&PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00002293 unsigned &DiagID,
Sebastian Redld9bafa72010-02-03 21:21:43 +00002294 const ParsedTemplateInfo &TemplateInfo,
2295 bool SuppressDeclarations) {
Douglas Gregor12e083c2008-11-07 15:42:26 +00002296 SourceLocation Loc = Tok.getLocation();
2297
2298 switch (Tok.getKind()) {
Chris Lattner166a8fc2009-01-04 23:41:41 +00002299 case tok::identifier: // foo::bar
Douglas Gregorc0b39642010-04-15 23:40:53 +00002300 // If we already have a type specifier, this identifier is not a type.
2301 if (DS.getTypeSpecType() != DeclSpec::TST_unspecified ||
2302 DS.getTypeSpecWidth() != DeclSpec::TSW_unspecified ||
2303 DS.getTypeSpecSign() != DeclSpec::TSS_unspecified)
2304 return false;
John Thompson82287d12010-02-05 00:12:22 +00002305 // Check for need to substitute AltiVec keyword tokens.
2306 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
2307 break;
2308 // Fall through.
Douglas Gregord57959a2009-03-27 23:10:48 +00002309 case tok::kw_typename: // typename foo::bar
Chris Lattner166a8fc2009-01-04 23:41:41 +00002310 // Annotate typenames and C++ scope specifiers. If we get one, just
2311 // recurse to handle whatever we get.
Kaelyn Uhrainfac94672011-10-11 01:02:41 +00002312 if (TryAnnotateTypeOrScopeToken(/*EnteringContext=*/false,
2313 /*NeedType=*/true))
John McCall9ba61662010-02-26 08:45:28 +00002314 return true;
2315 if (Tok.is(tok::identifier))
2316 return false;
2317 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
2318 TemplateInfo, SuppressDeclarations);
Chris Lattner166a8fc2009-01-04 23:41:41 +00002319 case tok::coloncolon: // ::foo::bar
2320 if (NextToken().is(tok::kw_new) || // ::new
2321 NextToken().is(tok::kw_delete)) // ::delete
2322 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002323
Chris Lattner166a8fc2009-01-04 23:41:41 +00002324 // Annotate typenames and C++ scope specifiers. If we get one, just
2325 // recurse to handle whatever we get.
Kaelyn Uhrainfac94672011-10-11 01:02:41 +00002326 if (TryAnnotateTypeOrScopeToken(/*EnteringContext=*/false,
2327 /*NeedType=*/true))
John McCall9ba61662010-02-26 08:45:28 +00002328 return true;
2329 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
2330 TemplateInfo, SuppressDeclarations);
Mike Stump1eb44332009-09-09 15:08:12 +00002331
Douglas Gregor12e083c2008-11-07 15:42:26 +00002332 // simple-type-specifier:
Chris Lattnerb31757b2009-01-06 05:06:21 +00002333 case tok::annot_typename: {
John McCallb3d87482010-08-24 05:47:05 +00002334 if (ParsedType T = getTypeAnnotation(Tok)) {
Nico Weber253e80b2010-11-22 10:30:56 +00002335 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename,
2336 Tok.getAnnotationEndLoc(), PrevSpec,
John McCallb3d87482010-08-24 05:47:05 +00002337 DiagID, T);
2338 } else
Douglas Gregor31a19b62009-04-01 21:51:26 +00002339 DS.SetTypeSpecError();
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002340 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
2341 ConsumeToken(); // The typename
Mike Stump1eb44332009-09-09 15:08:12 +00002342
Douglas Gregor12e083c2008-11-07 15:42:26 +00002343 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
2344 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
2345 // Objective-C interface. If we don't have Objective-C or a '<', this is
2346 // just a normal reference to a typedef name.
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00002347 if (Tok.is(tok::less) && getLang().ObjC1)
2348 ParseObjCProtocolQualifiers(DS);
2349
Douglas Gregor12e083c2008-11-07 15:42:26 +00002350 return true;
2351 }
2352
2353 case tok::kw_short:
John McCallfec54012009-08-03 20:12:06 +00002354 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002355 break;
2356 case tok::kw_long:
2357 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCallfec54012009-08-03 20:12:06 +00002358 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
2359 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002360 else
John McCallfec54012009-08-03 20:12:06 +00002361 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2362 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002363 break;
Francois Pichet338d7f72011-04-28 01:59:37 +00002364 case tok::kw___int64:
2365 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2366 DiagID);
2367 break;
Douglas Gregor12e083c2008-11-07 15:42:26 +00002368 case tok::kw_signed:
John McCallfec54012009-08-03 20:12:06 +00002369 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002370 break;
2371 case tok::kw_unsigned:
John McCallfec54012009-08-03 20:12:06 +00002372 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
2373 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002374 break;
2375 case tok::kw__Complex:
John McCallfec54012009-08-03 20:12:06 +00002376 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
2377 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002378 break;
2379 case tok::kw__Imaginary:
John McCallfec54012009-08-03 20:12:06 +00002380 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
2381 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002382 break;
2383 case tok::kw_void:
John McCallfec54012009-08-03 20:12:06 +00002384 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002385 break;
2386 case tok::kw_char:
John McCallfec54012009-08-03 20:12:06 +00002387 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002388 break;
2389 case tok::kw_int:
John McCallfec54012009-08-03 20:12:06 +00002390 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002391 break;
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00002392 case tok::kw_half:
2393 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_half, Loc, PrevSpec, DiagID);
2394 break;
Douglas Gregor12e083c2008-11-07 15:42:26 +00002395 case tok::kw_float:
John McCallfec54012009-08-03 20:12:06 +00002396 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002397 break;
2398 case tok::kw_double:
John McCallfec54012009-08-03 20:12:06 +00002399 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002400 break;
2401 case tok::kw_wchar_t:
John McCallfec54012009-08-03 20:12:06 +00002402 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002403 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002404 case tok::kw_char16_t:
John McCallfec54012009-08-03 20:12:06 +00002405 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002406 break;
2407 case tok::kw_char32_t:
John McCallfec54012009-08-03 20:12:06 +00002408 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002409 break;
Douglas Gregor12e083c2008-11-07 15:42:26 +00002410 case tok::kw_bool:
2411 case tok::kw__Bool:
John McCallfec54012009-08-03 20:12:06 +00002412 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002413 break;
2414 case tok::kw__Decimal32:
John McCallfec54012009-08-03 20:12:06 +00002415 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
2416 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002417 break;
2418 case tok::kw__Decimal64:
John McCallfec54012009-08-03 20:12:06 +00002419 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
2420 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002421 break;
2422 case tok::kw__Decimal128:
John McCallfec54012009-08-03 20:12:06 +00002423 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
2424 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002425 break;
John Thompson82287d12010-02-05 00:12:22 +00002426 case tok::kw___vector:
2427 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
2428 break;
2429 case tok::kw___pixel:
2430 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
2431 break;
2432
Douglas Gregor12e083c2008-11-07 15:42:26 +00002433 // class-specifier:
2434 case tok::kw_class:
2435 case tok::kw_struct:
Chris Lattner4c97d762009-04-12 21:49:30 +00002436 case tok::kw_union: {
2437 tok::TokenKind Kind = Tok.getKind();
2438 ConsumeToken();
Sebastian Redld9bafa72010-02-03 21:21:43 +00002439 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS_none,
2440 SuppressDeclarations);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002441 return true;
Chris Lattner4c97d762009-04-12 21:49:30 +00002442 }
Douglas Gregor12e083c2008-11-07 15:42:26 +00002443
2444 // enum-specifier:
2445 case tok::kw_enum:
Chris Lattner4c97d762009-04-12 21:49:30 +00002446 ConsumeToken();
Douglas Gregor9b9edd62010-03-02 17:53:14 +00002447 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS_none);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002448 return true;
2449
2450 // cv-qualifier:
2451 case tok::kw_const:
2452 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00002453 DiagID, getLang());
Douglas Gregor12e083c2008-11-07 15:42:26 +00002454 break;
2455 case tok::kw_volatile:
2456 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00002457 DiagID, getLang());
Douglas Gregor12e083c2008-11-07 15:42:26 +00002458 break;
2459 case tok::kw_restrict:
2460 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00002461 DiagID, getLang());
Douglas Gregor12e083c2008-11-07 15:42:26 +00002462 break;
2463
2464 // GNU typeof support.
2465 case tok::kw_typeof:
2466 ParseTypeofSpecifier(DS);
2467 return true;
2468
Anders Carlsson6fd634f2009-06-24 17:47:40 +00002469 // C++0x decltype support.
2470 case tok::kw_decltype:
2471 ParseDecltypeSpecifier(DS);
2472 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00002473
Sean Huntdb5d44b2011-05-19 05:37:45 +00002474 // C++0x type traits support.
2475 case tok::kw___underlying_type:
2476 ParseUnderlyingTypeSpecifier(DS);
2477 return true;
2478
Eli Friedmanb001de72011-10-06 23:00:33 +00002479 case tok::kw__Atomic:
2480 ParseAtomicSpecifier(DS);
2481 return true;
2482
Peter Collingbourne207f4d82011-03-18 22:38:29 +00002483 // OpenCL qualifiers:
2484 case tok::kw_private:
2485 if (!getLang().OpenCL)
2486 return false;
2487 case tok::kw___private:
2488 case tok::kw___global:
2489 case tok::kw___local:
2490 case tok::kw___constant:
2491 case tok::kw___read_only:
2492 case tok::kw___write_only:
2493 case tok::kw___read_write:
2494 ParseOpenCLQualifiers(DS);
2495 break;
2496
Anders Carlsson0b7f7892009-06-26 23:44:14 +00002497 // C++0x auto support.
2498 case tok::kw_auto:
Richard Smith87e96eb2011-09-04 20:24:20 +00002499 // This is only called in situations where a storage-class specifier is
2500 // illegal, so we can assume an auto type specifier was intended even in
2501 // C++98. In C++98 mode, DeclSpec::Finish will produce an appropriate
2502 // extension diagnostic.
2503 if (!getLang().CPlusPlus)
Anders Carlsson0b7f7892009-06-26 23:44:14 +00002504 return false;
2505
John McCallfec54012009-08-03 20:12:06 +00002506 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec, DiagID);
Anders Carlsson0b7f7892009-06-26 23:44:14 +00002507 break;
Dawn Perchik52fc3142010-09-03 01:29:35 +00002508
Eli Friedman290eeb02009-06-08 23:27:34 +00002509 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00002510 case tok::kw___ptr32:
Eli Friedman290eeb02009-06-08 23:27:34 +00002511 case tok::kw___w64:
Steve Naroff239f0732008-12-25 14:16:32 +00002512 case tok::kw___cdecl:
2513 case tok::kw___stdcall:
2514 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00002515 case tok::kw___thiscall:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00002516 case tok::kw___unaligned:
John McCall7f040a92010-12-24 02:08:15 +00002517 ParseMicrosoftTypeAttributes(DS.getAttributes());
Chris Lattner837acd02009-01-21 19:19:26 +00002518 return true;
Steve Naroff239f0732008-12-25 14:16:32 +00002519
Dawn Perchik52fc3142010-09-03 01:29:35 +00002520 case tok::kw___pascal:
John McCall7f040a92010-12-24 02:08:15 +00002521 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik52fc3142010-09-03 01:29:35 +00002522 return true;
2523
Douglas Gregor12e083c2008-11-07 15:42:26 +00002524 default:
2525 // Not a type-specifier; do nothing.
2526 return false;
2527 }
2528
2529 // If the specifier combination wasn't legal, issue a diagnostic.
2530 if (isInvalid) {
2531 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner1ab3b962008-11-18 07:48:38 +00002532 // Pick between error or extwarn.
Chris Lattner1ab3b962008-11-18 07:48:38 +00002533 Diag(Tok, DiagID) << PrevSpec;
Douglas Gregor12e083c2008-11-07 15:42:26 +00002534 }
2535 DS.SetRangeEnd(Tok.getLocation());
2536 ConsumeToken(); // whatever we parsed above.
2537 return true;
2538}
Reid Spencer5f016e22007-07-11 17:01:13 +00002539
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002540/// ParseStructDeclaration - Parse a struct declaration without the terminating
2541/// semicolon.
2542///
Reid Spencer5f016e22007-07-11 17:01:13 +00002543/// struct-declaration:
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002544/// specifier-qualifier-list struct-declarator-list
Reid Spencer5f016e22007-07-11 17:01:13 +00002545/// [GNU] __extension__ struct-declaration
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002546/// [GNU] specifier-qualifier-list
Reid Spencer5f016e22007-07-11 17:01:13 +00002547/// struct-declarator-list:
2548/// struct-declarator
2549/// struct-declarator-list ',' struct-declarator
2550/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
2551/// struct-declarator:
2552/// declarator
2553/// [GNU] declarator attributes[opt]
2554/// declarator[opt] ':' constant-expression
2555/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
2556///
Chris Lattnere1359422008-04-10 06:46:29 +00002557void Parser::
John McCallbdd563e2009-11-03 02:38:08 +00002558ParseStructDeclaration(DeclSpec &DS, FieldCallback &Fields) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002559
Chris Lattnerc46d1a12008-10-20 06:45:43 +00002560 if (Tok.is(tok::kw___extension__)) {
2561 // __extension__ silences extension warnings in the subexpression.
2562 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroff28a7ca82007-08-20 22:28:22 +00002563 ConsumeToken();
Chris Lattnerc46d1a12008-10-20 06:45:43 +00002564 return ParseStructDeclaration(DS, Fields);
2565 }
Mike Stump1eb44332009-09-09 15:08:12 +00002566
Steve Naroff28a7ca82007-08-20 22:28:22 +00002567 // Parse the common specifier-qualifiers-list piece.
Steve Naroff28a7ca82007-08-20 22:28:22 +00002568 ParseSpecifierQualifierList(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00002569
Douglas Gregor4920f1f2009-01-12 22:49:06 +00002570 // If there are no declarators, this is a free-standing declaration
2571 // specifier. Let the actions module cope with it.
Chris Lattner04d66662007-10-09 17:33:22 +00002572 if (Tok.is(tok::semi)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00002573 Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none, DS);
Steve Naroff28a7ca82007-08-20 22:28:22 +00002574 return;
2575 }
2576
2577 // Read struct-declarators until we find the semicolon.
John McCallbdd563e2009-11-03 02:38:08 +00002578 bool FirstDeclarator = true;
Steve Naroff28a7ca82007-08-20 22:28:22 +00002579 while (1) {
John McCall54abf7d2009-11-04 02:18:39 +00002580 ParsingDeclRAIIObject PD(*this);
John McCallbdd563e2009-11-03 02:38:08 +00002581 FieldDeclarator DeclaratorInfo(DS);
2582
2583 // Attributes are only allowed here on successive declarators.
John McCall7f040a92010-12-24 02:08:15 +00002584 if (!FirstDeclarator)
2585 MaybeParseGNUAttributes(DeclaratorInfo.D);
Mike Stump1eb44332009-09-09 15:08:12 +00002586
Steve Naroff28a7ca82007-08-20 22:28:22 +00002587 /// struct-declarator: declarator
2588 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattnera1efc8c2009-12-10 01:59:24 +00002589 if (Tok.isNot(tok::colon)) {
2590 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
2591 ColonProtectionRAIIObject X(*this);
Chris Lattnere1359422008-04-10 06:46:29 +00002592 ParseDeclarator(DeclaratorInfo.D);
Chris Lattnera1efc8c2009-12-10 01:59:24 +00002593 }
Mike Stump1eb44332009-09-09 15:08:12 +00002594
Chris Lattner04d66662007-10-09 17:33:22 +00002595 if (Tok.is(tok::colon)) {
Steve Naroff28a7ca82007-08-20 22:28:22 +00002596 ConsumeToken();
John McCall60d7b3a2010-08-24 06:29:42 +00002597 ExprResult Res(ParseConstantExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002598 if (Res.isInvalid())
Steve Naroff28a7ca82007-08-20 22:28:22 +00002599 SkipUntil(tok::semi, true, true);
Chris Lattner60b1e3e2008-04-10 06:15:14 +00002600 else
Sebastian Redleffa8d12008-12-10 00:02:53 +00002601 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroff28a7ca82007-08-20 22:28:22 +00002602 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00002603
Steve Naroff28a7ca82007-08-20 22:28:22 +00002604 // If attributes exist after the declarator, parse them.
John McCall7f040a92010-12-24 02:08:15 +00002605 MaybeParseGNUAttributes(DeclaratorInfo.D);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002606
John McCallbdd563e2009-11-03 02:38:08 +00002607 // We're done with this declarator; invoke the callback.
John McCalld226f652010-08-21 09:40:31 +00002608 Decl *D = Fields.invoke(DeclaratorInfo);
John McCall54abf7d2009-11-04 02:18:39 +00002609 PD.complete(D);
John McCallbdd563e2009-11-03 02:38:08 +00002610
Steve Naroff28a7ca82007-08-20 22:28:22 +00002611 // If we don't have a comma, it is either the end of the list (a ';')
2612 // or an error, bail out.
Chris Lattner04d66662007-10-09 17:33:22 +00002613 if (Tok.isNot(tok::comma))
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002614 return;
Sebastian Redlab197ba2009-02-09 18:23:29 +00002615
Steve Naroff28a7ca82007-08-20 22:28:22 +00002616 // Consume the comma.
2617 ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00002618
John McCallbdd563e2009-11-03 02:38:08 +00002619 FirstDeclarator = false;
Steve Naroff28a7ca82007-08-20 22:28:22 +00002620 }
Steve Naroff28a7ca82007-08-20 22:28:22 +00002621}
2622
2623/// ParseStructUnionBody
2624/// struct-contents:
2625/// struct-declaration-list
2626/// [EXT] empty
2627/// [GNU] "struct-declaration-list" without terminatoring ';'
2628/// struct-declaration-list:
2629/// struct-declaration
2630/// struct-declaration-list struct-declaration
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002631/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroff28a7ca82007-08-20 22:28:22 +00002632///
Reid Spencer5f016e22007-07-11 17:01:13 +00002633void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
John McCalld226f652010-08-21 09:40:31 +00002634 unsigned TagType, Decl *TagDecl) {
John McCallf312b1e2010-08-26 23:41:50 +00002635 PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc,
2636 "parsing struct/union body");
Mike Stump1eb44332009-09-09 15:08:12 +00002637
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002638 BalancedDelimiterTracker T(*this, tok::l_brace);
2639 if (T.consumeOpen())
2640 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002641
Douglas Gregor3218c4b2009-01-09 22:42:13 +00002642 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
Douglas Gregor23c94db2010-07-02 17:43:08 +00002643 Actions.ActOnTagStartDefinition(getCurScope(), TagDecl);
Douglas Gregor72de6672009-01-08 20:45:30 +00002644
Reid Spencer5f016e22007-07-11 17:01:13 +00002645 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
2646 // C++.
Douglas Gregore37ac4f2008-04-13 21:30:24 +00002647 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Douglas Gregor03332962010-07-29 14:29:34 +00002648 Diag(Tok, diag::ext_empty_struct_union)
2649 << (TagType == TST_union);
Reid Spencer5f016e22007-07-11 17:01:13 +00002650
Chris Lattner5f9e2722011-07-23 10:55:15 +00002651 SmallVector<Decl *, 32> FieldDecls;
Chris Lattnere1359422008-04-10 06:46:29 +00002652
Reid Spencer5f016e22007-07-11 17:01:13 +00002653 // While we still have something to read, read the declarations in the struct.
Chris Lattner04d66662007-10-09 17:33:22 +00002654 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002655 // Each iteration of this loop reads one struct-declaration.
Mike Stump1eb44332009-09-09 15:08:12 +00002656
Reid Spencer5f016e22007-07-11 17:01:13 +00002657 // Check for extraneous top-level semicolon.
Chris Lattner04d66662007-10-09 17:33:22 +00002658 if (Tok.is(tok::semi)) {
Douglas Gregor9b3064b2009-04-01 22:41:11 +00002659 Diag(Tok, diag::ext_extra_struct_semi)
Douglas Gregorf13ca062010-06-16 23:08:59 +00002660 << DeclSpec::getSpecifierName((DeclSpec::TST)TagType)
Douglas Gregor849b2432010-03-31 17:46:05 +00002661 << FixItHint::CreateRemoval(Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00002662 ConsumeToken();
2663 continue;
2664 }
Chris Lattnere1359422008-04-10 06:46:29 +00002665
2666 // Parse all the comma separated declarators.
John McCall0b7e6782011-03-24 11:26:52 +00002667 DeclSpec DS(AttrFactory);
Mike Stump1eb44332009-09-09 15:08:12 +00002668
John McCallbdd563e2009-11-03 02:38:08 +00002669 if (!Tok.is(tok::at)) {
2670 struct CFieldCallback : FieldCallback {
2671 Parser &P;
John McCalld226f652010-08-21 09:40:31 +00002672 Decl *TagDecl;
Chris Lattner5f9e2722011-07-23 10:55:15 +00002673 SmallVectorImpl<Decl *> &FieldDecls;
John McCallbdd563e2009-11-03 02:38:08 +00002674
John McCalld226f652010-08-21 09:40:31 +00002675 CFieldCallback(Parser &P, Decl *TagDecl,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002676 SmallVectorImpl<Decl *> &FieldDecls) :
John McCallbdd563e2009-11-03 02:38:08 +00002677 P(P), TagDecl(TagDecl), FieldDecls(FieldDecls) {}
2678
John McCalld226f652010-08-21 09:40:31 +00002679 virtual Decl *invoke(FieldDeclarator &FD) {
John McCallbdd563e2009-11-03 02:38:08 +00002680 // Install the declarator into the current TagDecl.
John McCalld226f652010-08-21 09:40:31 +00002681 Decl *Field = P.Actions.ActOnField(P.getCurScope(), TagDecl,
John McCall4ba39712009-11-03 21:13:47 +00002682 FD.D.getDeclSpec().getSourceRange().getBegin(),
2683 FD.D, FD.BitfieldSize);
John McCallbdd563e2009-11-03 02:38:08 +00002684 FieldDecls.push_back(Field);
2685 return Field;
Douglas Gregor91a28862009-08-26 14:27:30 +00002686 }
John McCallbdd563e2009-11-03 02:38:08 +00002687 } Callback(*this, TagDecl, FieldDecls);
2688
2689 ParseStructDeclaration(DS, Callback);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002690 } else { // Handle @defs
2691 ConsumeToken();
2692 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
2693 Diag(Tok, diag::err_unexpected_at);
Chris Lattner3e156ad2010-02-02 00:37:27 +00002694 SkipUntil(tok::semi, true);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002695 continue;
2696 }
2697 ConsumeToken();
2698 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
2699 if (!Tok.is(tok::identifier)) {
2700 Diag(Tok, diag::err_expected_ident);
Chris Lattner3e156ad2010-02-02 00:37:27 +00002701 SkipUntil(tok::semi, true);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002702 continue;
2703 }
Chris Lattner5f9e2722011-07-23 10:55:15 +00002704 SmallVector<Decl *, 16> Fields;
Douglas Gregor23c94db2010-07-02 17:43:08 +00002705 Actions.ActOnDefs(getCurScope(), TagDecl, Tok.getLocation(),
Douglas Gregor44b43212008-12-11 16:49:14 +00002706 Tok.getIdentifierInfo(), Fields);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002707 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
2708 ConsumeToken();
2709 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
Mike Stump1eb44332009-09-09 15:08:12 +00002710 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002711
Chris Lattner04d66662007-10-09 17:33:22 +00002712 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002713 ConsumeToken();
Chris Lattner04d66662007-10-09 17:33:22 +00002714 } else if (Tok.is(tok::r_brace)) {
Chris Lattner3e156ad2010-02-02 00:37:27 +00002715 ExpectAndConsume(tok::semi, diag::ext_expected_semi_decl_list);
Reid Spencer5f016e22007-07-11 17:01:13 +00002716 break;
2717 } else {
Chris Lattner3e156ad2010-02-02 00:37:27 +00002718 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
2719 // Skip to end of block or statement to avoid ext-warning on extra ';'.
Reid Spencer5f016e22007-07-11 17:01:13 +00002720 SkipUntil(tok::r_brace, true, true);
Chris Lattner3e156ad2010-02-02 00:37:27 +00002721 // If we stopped at a ';', eat it.
2722 if (Tok.is(tok::semi)) ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00002723 }
2724 }
Mike Stump1eb44332009-09-09 15:08:12 +00002725
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002726 T.consumeClose();
Mike Stump1eb44332009-09-09 15:08:12 +00002727
John McCall0b7e6782011-03-24 11:26:52 +00002728 ParsedAttributes attrs(AttrFactory);
Reid Spencer5f016e22007-07-11 17:01:13 +00002729 // If attributes exist after struct contents, parse them.
John McCall7f040a92010-12-24 02:08:15 +00002730 MaybeParseGNUAttributes(attrs);
Daniel Dunbar1bfe1c22008-10-03 02:03:53 +00002731
Douglas Gregor23c94db2010-07-02 17:43:08 +00002732 Actions.ActOnFields(getCurScope(),
David Blaikie77b6de02011-09-22 02:58:26 +00002733 RecordLoc, TagDecl, FieldDecls,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002734 T.getOpenLocation(), T.getCloseLocation(),
John McCall7f040a92010-12-24 02:08:15 +00002735 attrs.getList());
Douglas Gregor72de6672009-01-08 20:45:30 +00002736 StructScope.Exit();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002737 Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl,
2738 T.getCloseLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00002739}
2740
Reid Spencer5f016e22007-07-11 17:01:13 +00002741/// ParseEnumSpecifier
2742/// enum-specifier: [C99 6.7.2.2]
2743/// 'enum' identifier[opt] '{' enumerator-list '}'
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002744///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Reid Spencer5f016e22007-07-11 17:01:13 +00002745/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
2746/// '}' attributes[opt]
2747/// 'enum' identifier
2748/// [GNU] 'enum' attributes[opt] identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002749///
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002750/// [C++0x] enum-head '{' enumerator-list[opt] '}'
2751/// [C++0x] enum-head '{' enumerator-list ',' '}'
2752///
2753/// enum-head: [C++0x]
2754/// enum-key attributes[opt] identifier[opt] enum-base[opt]
2755/// enum-key attributes[opt] nested-name-specifier identifier enum-base[opt]
2756///
2757/// enum-key: [C++0x]
2758/// 'enum'
2759/// 'enum' 'class'
2760/// 'enum' 'struct'
2761///
2762/// enum-base: [C++0x]
2763/// ':' type-specifier-seq
2764///
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002765/// [C++] elaborated-type-specifier:
2766/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
2767///
Chris Lattner4c97d762009-04-12 21:49:30 +00002768void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregor9b9edd62010-03-02 17:53:14 +00002769 const ParsedTemplateInfo &TemplateInfo,
Chris Lattner4c97d762009-04-12 21:49:30 +00002770 AccessSpecifier AS) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002771 // Parse the tag portion of this.
Douglas Gregor374929f2009-09-18 15:37:17 +00002772 if (Tok.is(tok::code_completion)) {
2773 // Code completion for an enum name.
Douglas Gregor23c94db2010-07-02 17:43:08 +00002774 Actions.CodeCompleteTag(getCurScope(), DeclSpec::TST_enum);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002775 return cutOffParsing();
Douglas Gregor374929f2009-09-18 15:37:17 +00002776 }
John McCall57c13002011-07-06 05:58:41 +00002777
2778 bool IsScopedEnum = false;
2779 bool IsScopedUsingClassTag = false;
2780
2781 if (getLang().CPlusPlus0x &&
2782 (Tok.is(tok::kw_class) || Tok.is(tok::kw_struct))) {
Richard Smith7fe62082011-10-15 05:09:34 +00002783 Diag(Tok, diag::warn_cxx98_compat_scoped_enum);
John McCall57c13002011-07-06 05:58:41 +00002784 IsScopedEnum = true;
2785 IsScopedUsingClassTag = Tok.is(tok::kw_class);
2786 ConsumeToken();
2787 }
Douglas Gregor374929f2009-09-18 15:37:17 +00002788
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002789 // If attributes exist after tag, parse them.
John McCall0b7e6782011-03-24 11:26:52 +00002790 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00002791 MaybeParseGNUAttributes(attrs);
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002792
Douglas Gregor5471bc82011-09-08 17:18:35 +00002793 bool AllowFixedUnderlyingType
Francois Pichet62ec1f22011-09-17 17:15:52 +00002794 = getLang().CPlusPlus0x || getLang().MicrosoftExt || getLang().ObjC2;
John McCall57c13002011-07-06 05:58:41 +00002795
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00002796 CXXScopeSpec &SS = DS.getTypeSpecScope();
John McCall9ba61662010-02-26 08:45:28 +00002797 if (getLang().CPlusPlus) {
John McCall57c13002011-07-06 05:58:41 +00002798 // "enum foo : bar;" is not a potential typo for "enum foo::bar;"
2799 // if a fixed underlying type is allowed.
2800 ColonProtectionRAIIObject X(*this, AllowFixedUnderlyingType);
2801
John McCallb3d87482010-08-24 05:47:05 +00002802 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), false))
John McCall9ba61662010-02-26 08:45:28 +00002803 return;
2804
2805 if (SS.isSet() && Tok.isNot(tok::identifier)) {
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002806 Diag(Tok, diag::err_expected_ident);
2807 if (Tok.isNot(tok::l_brace)) {
2808 // Has no name and is not a definition.
2809 // Skip the rest of this declarator, up until the comma or semicolon.
2810 SkipUntil(tok::comma, true);
2811 return;
2812 }
2813 }
2814 }
Mike Stump1eb44332009-09-09 15:08:12 +00002815
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002816 // Must have either 'enum name' or 'enum {...}'.
Douglas Gregorb9075602011-02-22 02:55:24 +00002817 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace) &&
2818 (AllowFixedUnderlyingType && Tok.isNot(tok::colon))) {
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002819 Diag(Tok, diag::err_expected_ident_lbrace);
Mike Stump1eb44332009-09-09 15:08:12 +00002820
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002821 // Skip the rest of this declarator, up until the comma or semicolon.
2822 SkipUntil(tok::comma, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00002823 return;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002824 }
Mike Stump1eb44332009-09-09 15:08:12 +00002825
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002826 // If an identifier is present, consume and remember it.
2827 IdentifierInfo *Name = 0;
2828 SourceLocation NameLoc;
2829 if (Tok.is(tok::identifier)) {
2830 Name = Tok.getIdentifierInfo();
2831 NameLoc = ConsumeToken();
2832 }
Mike Stump1eb44332009-09-09 15:08:12 +00002833
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002834 if (!Name && IsScopedEnum) {
2835 // C++0x 7.2p2: The optional identifier shall not be omitted in the
2836 // declaration of a scoped enumeration.
2837 Diag(Tok, diag::err_scoped_enum_missing_identifier);
2838 IsScopedEnum = false;
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00002839 IsScopedUsingClassTag = false;
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002840 }
2841
2842 TypeResult BaseType;
2843
Douglas Gregora61b3e72010-12-01 17:42:47 +00002844 // Parse the fixed underlying type.
Douglas Gregorb9075602011-02-22 02:55:24 +00002845 if (AllowFixedUnderlyingType && Tok.is(tok::colon)) {
Douglas Gregora61b3e72010-12-01 17:42:47 +00002846 bool PossibleBitfield = false;
2847 if (getCurScope()->getFlags() & Scope::ClassScope) {
2848 // If we're in class scope, this can either be an enum declaration with
2849 // an underlying type, or a declaration of a bitfield member. We try to
2850 // use a simple disambiguation scheme first to catch the common cases
2851 // (integer literal, sizeof); if it's still ambiguous, we then consider
2852 // anything that's a simple-type-specifier followed by '(' as an
2853 // expression. This suffices because function types are not valid
2854 // underlying types anyway.
2855 TPResult TPR = isExpressionOrTypeSpecifierSimple(NextToken().getKind());
2856 // If the next token starts an expression, we know we're parsing a
2857 // bit-field. This is the common case.
2858 if (TPR == TPResult::True())
2859 PossibleBitfield = true;
2860 // If the next token starts a type-specifier-seq, it may be either a
2861 // a fixed underlying type or the start of a function-style cast in C++;
2862 // lookahead one more token to see if it's obvious that we have a
2863 // fixed underlying type.
2864 else if (TPR == TPResult::False() &&
2865 GetLookAheadToken(2).getKind() == tok::semi) {
2866 // Consume the ':'.
2867 ConsumeToken();
2868 } else {
2869 // We have the start of a type-specifier-seq, so we have to perform
2870 // tentative parsing to determine whether we have an expression or a
2871 // type.
2872 TentativeParsingAction TPA(*this);
2873
2874 // Consume the ':'.
2875 ConsumeToken();
2876
Douglas Gregor86f208c2011-02-22 20:32:04 +00002877 if ((getLang().CPlusPlus &&
2878 isCXXDeclarationSpecifier() != TPResult::True()) ||
2879 (!getLang().CPlusPlus && !isDeclarationSpecifier(true))) {
Douglas Gregora61b3e72010-12-01 17:42:47 +00002880 // We'll parse this as a bitfield later.
2881 PossibleBitfield = true;
2882 TPA.Revert();
2883 } else {
2884 // We have a type-specifier-seq.
2885 TPA.Commit();
2886 }
2887 }
2888 } else {
2889 // Consume the ':'.
2890 ConsumeToken();
2891 }
2892
2893 if (!PossibleBitfield) {
2894 SourceRange Range;
2895 BaseType = ParseTypeName(&Range);
Douglas Gregor86f208c2011-02-22 20:32:04 +00002896
Douglas Gregor5471bc82011-09-08 17:18:35 +00002897 if (!getLang().CPlusPlus0x && !getLang().ObjC2)
Douglas Gregor86f208c2011-02-22 20:32:04 +00002898 Diag(StartLoc, diag::ext_ms_enum_fixed_underlying_type)
2899 << Range;
Richard Smith7fe62082011-10-15 05:09:34 +00002900 if (getLang().CPlusPlus0x)
2901 Diag(StartLoc, diag::warn_cxx98_compat_enum_fixed_underlying_type);
Douglas Gregora61b3e72010-12-01 17:42:47 +00002902 }
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002903 }
2904
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002905 // There are three options here. If we have 'enum foo;', then this is a
2906 // forward declaration. If we have 'enum foo {...' then this is a
2907 // definition. Otherwise we have something like 'enum foo xyz', a reference.
2908 //
2909 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
2910 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
2911 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
2912 //
John McCallf312b1e2010-08-26 23:41:50 +00002913 Sema::TagUseKind TUK;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002914 if (Tok.is(tok::l_brace))
John McCallf312b1e2010-08-26 23:41:50 +00002915 TUK = Sema::TUK_Definition;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002916 else if (Tok.is(tok::semi))
John McCallf312b1e2010-08-26 23:41:50 +00002917 TUK = Sema::TUK_Declaration;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002918 else
John McCallf312b1e2010-08-26 23:41:50 +00002919 TUK = Sema::TUK_Reference;
Douglas Gregor8fc6d232010-05-03 17:48:54 +00002920
2921 // enums cannot be templates, although they can be referenced from a
2922 // template.
2923 if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate &&
John McCallf312b1e2010-08-26 23:41:50 +00002924 TUK != Sema::TUK_Reference) {
Douglas Gregor8fc6d232010-05-03 17:48:54 +00002925 Diag(Tok, diag::err_enum_template);
2926
2927 // Skip the rest of this declarator, up until the comma or semicolon.
2928 SkipUntil(tok::comma, true);
2929 return;
2930 }
2931
Douglas Gregorb9075602011-02-22 02:55:24 +00002932 if (!Name && TUK != Sema::TUK_Definition) {
2933 Diag(Tok, diag::err_enumerator_unnamed_no_def);
2934
2935 // Skip the rest of this declarator, up until the comma or semicolon.
2936 SkipUntil(tok::comma, true);
2937 return;
2938 }
2939
Douglas Gregor402abb52009-05-28 23:31:59 +00002940 bool Owned = false;
John McCallc4e70192009-09-11 04:59:25 +00002941 bool IsDependent = false;
Douglas Gregor48c89f42010-04-24 16:38:41 +00002942 const char *PrevSpec = 0;
2943 unsigned DiagID;
John McCalld226f652010-08-21 09:40:31 +00002944 Decl *TagDecl = Actions.ActOnTag(getCurScope(), DeclSpec::TST_enum, TUK,
John McCall7f040a92010-12-24 02:08:15 +00002945 StartLoc, SS, Name, NameLoc, attrs.getList(),
Douglas Gregore7612302011-09-09 19:05:14 +00002946 AS, DS.getModulePrivateSpecLoc(),
John McCallf312b1e2010-08-26 23:41:50 +00002947 MultiTemplateParamsArg(Actions),
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002948 Owned, IsDependent, IsScopedEnum,
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00002949 IsScopedUsingClassTag, BaseType);
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002950
Douglas Gregor48c89f42010-04-24 16:38:41 +00002951 if (IsDependent) {
2952 // This enum has a dependent nested-name-specifier. Handle it as a
2953 // dependent tag.
2954 if (!Name) {
2955 DS.SetTypeSpecError();
2956 Diag(Tok, diag::err_expected_type_name_after_typename);
2957 return;
2958 }
2959
Douglas Gregor23c94db2010-07-02 17:43:08 +00002960 TypeResult Type = Actions.ActOnDependentTag(getCurScope(), DeclSpec::TST_enum,
Douglas Gregor48c89f42010-04-24 16:38:41 +00002961 TUK, SS, Name, StartLoc,
2962 NameLoc);
2963 if (Type.isInvalid()) {
2964 DS.SetTypeSpecError();
2965 return;
2966 }
2967
Abramo Bagnara0daaf322011-03-16 20:16:18 +00002968 if (DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc,
2969 NameLoc.isValid() ? NameLoc : StartLoc,
2970 PrevSpec, DiagID, Type.get()))
Douglas Gregor48c89f42010-04-24 16:38:41 +00002971 Diag(StartLoc, DiagID) << PrevSpec;
2972
2973 return;
2974 }
Mike Stump1eb44332009-09-09 15:08:12 +00002975
John McCalld226f652010-08-21 09:40:31 +00002976 if (!TagDecl) {
Douglas Gregor48c89f42010-04-24 16:38:41 +00002977 // The action failed to produce an enumeration tag. If this is a
2978 // definition, consume the entire definition.
2979 if (Tok.is(tok::l_brace)) {
2980 ConsumeBrace();
2981 SkipUntil(tok::r_brace);
2982 }
2983
2984 DS.SetTypeSpecError();
2985 return;
2986 }
2987
Chris Lattner04d66662007-10-09 17:33:22 +00002988 if (Tok.is(tok::l_brace))
Reid Spencer5f016e22007-07-11 17:01:13 +00002989 ParseEnumBody(StartLoc, TagDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00002990
Abramo Bagnara0daaf322011-03-16 20:16:18 +00002991 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc,
2992 NameLoc.isValid() ? NameLoc : StartLoc,
2993 PrevSpec, DiagID, TagDecl, Owned))
John McCallfec54012009-08-03 20:12:06 +00002994 Diag(StartLoc, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00002995}
2996
2997/// ParseEnumBody - Parse a {} enclosed enumerator-list.
2998/// enumerator-list:
2999/// enumerator
3000/// enumerator-list ',' enumerator
3001/// enumerator:
3002/// enumeration-constant
3003/// enumeration-constant '=' constant-expression
3004/// enumeration-constant:
3005/// identifier
3006///
John McCalld226f652010-08-21 09:40:31 +00003007void Parser::ParseEnumBody(SourceLocation StartLoc, Decl *EnumDecl) {
Douglas Gregor074149e2009-01-05 19:45:36 +00003008 // Enter the scope of the enum body and start the definition.
3009 ParseScope EnumScope(this, Scope::DeclScope);
Douglas Gregor23c94db2010-07-02 17:43:08 +00003010 Actions.ActOnTagStartDefinition(getCurScope(), EnumDecl);
Douglas Gregor074149e2009-01-05 19:45:36 +00003011
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003012 BalancedDelimiterTracker T(*this, tok::l_brace);
3013 T.consumeOpen();
Mike Stump1eb44332009-09-09 15:08:12 +00003014
Chris Lattner7946dd32007-08-27 17:24:30 +00003015 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
Chris Lattner04d66662007-10-09 17:33:22 +00003016 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Fariborz Jahanian05115522010-05-28 22:23:22 +00003017 Diag(Tok, diag::error_empty_enum);
Mike Stump1eb44332009-09-09 15:08:12 +00003018
Chris Lattner5f9e2722011-07-23 10:55:15 +00003019 SmallVector<Decl *, 32> EnumConstantDecls;
Reid Spencer5f016e22007-07-11 17:01:13 +00003020
John McCalld226f652010-08-21 09:40:31 +00003021 Decl *LastEnumConstDecl = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00003022
Reid Spencer5f016e22007-07-11 17:01:13 +00003023 // Parse the enumerator-list.
Chris Lattner04d66662007-10-09 17:33:22 +00003024 while (Tok.is(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003025 IdentifierInfo *Ident = Tok.getIdentifierInfo();
3026 SourceLocation IdentLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00003027
John McCall5b629aa2010-10-22 23:36:17 +00003028 // If attributes exist after the enumerator, parse them.
John McCall0b7e6782011-03-24 11:26:52 +00003029 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00003030 MaybeParseGNUAttributes(attrs);
John McCall5b629aa2010-10-22 23:36:17 +00003031
Reid Spencer5f016e22007-07-11 17:01:13 +00003032 SourceLocation EqualLoc;
John McCall60d7b3a2010-08-24 06:29:42 +00003033 ExprResult AssignedVal;
Chris Lattner04d66662007-10-09 17:33:22 +00003034 if (Tok.is(tok::equal)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003035 EqualLoc = ConsumeToken();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00003036 AssignedVal = ParseConstantExpression();
3037 if (AssignedVal.isInvalid())
Reid Spencer5f016e22007-07-11 17:01:13 +00003038 SkipUntil(tok::comma, tok::r_brace, true, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00003039 }
Mike Stump1eb44332009-09-09 15:08:12 +00003040
Reid Spencer5f016e22007-07-11 17:01:13 +00003041 // Install the enumerator constant into EnumDecl.
John McCalld226f652010-08-21 09:40:31 +00003042 Decl *EnumConstDecl = Actions.ActOnEnumConstant(getCurScope(), EnumDecl,
3043 LastEnumConstDecl,
3044 IdentLoc, Ident,
John McCall7f040a92010-12-24 02:08:15 +00003045 attrs.getList(), EqualLoc,
John McCalld226f652010-08-21 09:40:31 +00003046 AssignedVal.release());
Reid Spencer5f016e22007-07-11 17:01:13 +00003047 EnumConstantDecls.push_back(EnumConstDecl);
3048 LastEnumConstDecl = EnumConstDecl;
Mike Stump1eb44332009-09-09 15:08:12 +00003049
Douglas Gregor751f6922010-09-07 14:51:08 +00003050 if (Tok.is(tok::identifier)) {
3051 // We're missing a comma between enumerators.
3052 SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation);
3053 Diag(Loc, diag::err_enumerator_list_missing_comma)
3054 << FixItHint::CreateInsertion(Loc, ", ");
3055 continue;
3056 }
3057
Chris Lattner04d66662007-10-09 17:33:22 +00003058 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +00003059 break;
3060 SourceLocation CommaLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00003061
Richard Smith7fe62082011-10-15 05:09:34 +00003062 if (Tok.isNot(tok::identifier)) {
3063 if (!getLang().C99 && !getLang().CPlusPlus0x)
3064 Diag(CommaLoc, diag::ext_enumerator_list_comma)
3065 << getLang().CPlusPlus
3066 << FixItHint::CreateRemoval(CommaLoc);
3067 else if (getLang().CPlusPlus0x)
3068 Diag(CommaLoc, diag::warn_cxx98_compat_enumerator_list_comma)
3069 << FixItHint::CreateRemoval(CommaLoc);
3070 }
Reid Spencer5f016e22007-07-11 17:01:13 +00003071 }
Mike Stump1eb44332009-09-09 15:08:12 +00003072
Reid Spencer5f016e22007-07-11 17:01:13 +00003073 // Eat the }.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003074 T.consumeClose();
Reid Spencer5f016e22007-07-11 17:01:13 +00003075
Reid Spencer5f016e22007-07-11 17:01:13 +00003076 // If attributes exist after the identifier list, parse them.
John McCall0b7e6782011-03-24 11:26:52 +00003077 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00003078 MaybeParseGNUAttributes(attrs);
Douglas Gregor72de6672009-01-08 20:45:30 +00003079
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003080 Actions.ActOnEnumBody(StartLoc, T.getOpenLocation(), T.getCloseLocation(),
3081 EnumDecl, EnumConstantDecls.data(),
3082 EnumConstantDecls.size(), getCurScope(),
3083 attrs.getList());
Mike Stump1eb44332009-09-09 15:08:12 +00003084
Douglas Gregor72de6672009-01-08 20:45:30 +00003085 EnumScope.Exit();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003086 Actions.ActOnTagFinishDefinition(getCurScope(), EnumDecl,
3087 T.getCloseLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00003088}
3089
3090/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff5f8aa692008-02-11 23:15:56 +00003091/// start of a type-qualifier-list.
3092bool Parser::isTypeQualifier() const {
3093 switch (Tok.getKind()) {
3094 default: return false;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003095
3096 // type-qualifier only in OpenCL
3097 case tok::kw_private:
3098 return getLang().OpenCL;
3099
Steve Naroff5f8aa692008-02-11 23:15:56 +00003100 // type-qualifier
3101 case tok::kw_const:
3102 case tok::kw_volatile:
3103 case tok::kw_restrict:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003104 case tok::kw___private:
3105 case tok::kw___local:
3106 case tok::kw___global:
3107 case tok::kw___constant:
3108 case tok::kw___read_only:
3109 case tok::kw___read_write:
3110 case tok::kw___write_only:
Steve Naroff5f8aa692008-02-11 23:15:56 +00003111 return true;
3112 }
3113}
3114
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003115/// isKnownToBeTypeSpecifier - Return true if we know that the specified token
3116/// is definitely a type-specifier. Return false if it isn't part of a type
3117/// specifier or if we're not sure.
3118bool Parser::isKnownToBeTypeSpecifier(const Token &Tok) const {
3119 switch (Tok.getKind()) {
3120 default: return false;
3121 // type-specifiers
3122 case tok::kw_short:
3123 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00003124 case tok::kw___int64:
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003125 case tok::kw_signed:
3126 case tok::kw_unsigned:
3127 case tok::kw__Complex:
3128 case tok::kw__Imaginary:
3129 case tok::kw_void:
3130 case tok::kw_char:
3131 case tok::kw_wchar_t:
3132 case tok::kw_char16_t:
3133 case tok::kw_char32_t:
3134 case tok::kw_int:
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00003135 case tok::kw_half:
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003136 case tok::kw_float:
3137 case tok::kw_double:
3138 case tok::kw_bool:
3139 case tok::kw__Bool:
3140 case tok::kw__Decimal32:
3141 case tok::kw__Decimal64:
3142 case tok::kw__Decimal128:
3143 case tok::kw___vector:
3144
3145 // struct-or-union-specifier (C99) or class-specifier (C++)
3146 case tok::kw_class:
3147 case tok::kw_struct:
3148 case tok::kw_union:
3149 // enum-specifier
3150 case tok::kw_enum:
3151
3152 // typedef-name
3153 case tok::annot_typename:
3154 return true;
3155 }
3156}
3157
Steve Naroff5f8aa692008-02-11 23:15:56 +00003158/// isTypeSpecifierQualifier - Return true if the current token could be the
Reid Spencer5f016e22007-07-11 17:01:13 +00003159/// start of a specifier-qualifier-list.
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003160bool Parser::isTypeSpecifierQualifier() {
Reid Spencer5f016e22007-07-11 17:01:13 +00003161 switch (Tok.getKind()) {
3162 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00003163
Chris Lattner166a8fc2009-01-04 23:41:41 +00003164 case tok::identifier: // foo::bar
John Thompson82287d12010-02-05 00:12:22 +00003165 if (TryAltiVecVectorToken())
3166 return true;
3167 // Fall through.
Douglas Gregord57959a2009-03-27 23:10:48 +00003168 case tok::kw_typename: // typename T::type
Chris Lattner166a8fc2009-01-04 23:41:41 +00003169 // Annotate typenames and C++ scope specifiers. If we get one, just
3170 // recurse to handle whatever we get.
3171 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003172 return true;
3173 if (Tok.is(tok::identifier))
3174 return false;
3175 return isTypeSpecifierQualifier();
Douglas Gregord57959a2009-03-27 23:10:48 +00003176
Chris Lattner166a8fc2009-01-04 23:41:41 +00003177 case tok::coloncolon: // ::foo::bar
3178 if (NextToken().is(tok::kw_new) || // ::new
3179 NextToken().is(tok::kw_delete)) // ::delete
3180 return false;
3181
Chris Lattner166a8fc2009-01-04 23:41:41 +00003182 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003183 return true;
3184 return isTypeSpecifierQualifier();
Mike Stump1eb44332009-09-09 15:08:12 +00003185
Reid Spencer5f016e22007-07-11 17:01:13 +00003186 // GNU attributes support.
3187 case tok::kw___attribute:
Steve Naroffd1861fd2007-07-31 12:34:36 +00003188 // GNU typeof support.
3189 case tok::kw_typeof:
Mike Stump1eb44332009-09-09 15:08:12 +00003190
Reid Spencer5f016e22007-07-11 17:01:13 +00003191 // type-specifiers
3192 case tok::kw_short:
3193 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00003194 case tok::kw___int64:
Reid Spencer5f016e22007-07-11 17:01:13 +00003195 case tok::kw_signed:
3196 case tok::kw_unsigned:
3197 case tok::kw__Complex:
3198 case tok::kw__Imaginary:
3199 case tok::kw_void:
3200 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00003201 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00003202 case tok::kw_char16_t:
3203 case tok::kw_char32_t:
Reid Spencer5f016e22007-07-11 17:01:13 +00003204 case tok::kw_int:
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00003205 case tok::kw_half:
Reid Spencer5f016e22007-07-11 17:01:13 +00003206 case tok::kw_float:
3207 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00003208 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00003209 case tok::kw__Bool:
3210 case tok::kw__Decimal32:
3211 case tok::kw__Decimal64:
3212 case tok::kw__Decimal128:
John Thompson82287d12010-02-05 00:12:22 +00003213 case tok::kw___vector:
Mike Stump1eb44332009-09-09 15:08:12 +00003214
Chris Lattner99dc9142008-04-13 18:59:07 +00003215 // struct-or-union-specifier (C99) or class-specifier (C++)
3216 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00003217 case tok::kw_struct:
3218 case tok::kw_union:
3219 // enum-specifier
3220 case tok::kw_enum:
Mike Stump1eb44332009-09-09 15:08:12 +00003221
Reid Spencer5f016e22007-07-11 17:01:13 +00003222 // type-qualifier
3223 case tok::kw_const:
3224 case tok::kw_volatile:
3225 case tok::kw_restrict:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003226
3227 // typedef-name
Chris Lattnerb31757b2009-01-06 05:06:21 +00003228 case tok::annot_typename:
Reid Spencer5f016e22007-07-11 17:01:13 +00003229 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00003230
Chris Lattner7c186be2008-10-20 00:25:30 +00003231 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
3232 case tok::less:
3233 return getLang().ObjC1;
Mike Stump1eb44332009-09-09 15:08:12 +00003234
Steve Naroff239f0732008-12-25 14:16:32 +00003235 case tok::kw___cdecl:
3236 case tok::kw___stdcall:
3237 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00003238 case tok::kw___thiscall:
Eli Friedman290eeb02009-06-08 23:27:34 +00003239 case tok::kw___w64:
3240 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00003241 case tok::kw___ptr32:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003242 case tok::kw___pascal:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00003243 case tok::kw___unaligned:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003244
3245 case tok::kw___private:
3246 case tok::kw___local:
3247 case tok::kw___global:
3248 case tok::kw___constant:
3249 case tok::kw___read_only:
3250 case tok::kw___read_write:
3251 case tok::kw___write_only:
3252
Eli Friedman290eeb02009-06-08 23:27:34 +00003253 return true;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003254
3255 case tok::kw_private:
3256 return getLang().OpenCL;
Eli Friedmanb001de72011-10-06 23:00:33 +00003257
3258 // C1x _Atomic()
3259 case tok::kw__Atomic:
3260 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00003261 }
3262}
3263
3264/// isDeclarationSpecifier() - Return true if the current token is part of a
3265/// declaration specifier.
Douglas Gregor9497a732010-09-16 01:51:54 +00003266///
3267/// \param DisambiguatingWithExpression True to indicate that the purpose of
3268/// this check is to disambiguate between an expression and a declaration.
3269bool Parser::isDeclarationSpecifier(bool DisambiguatingWithExpression) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003270 switch (Tok.getKind()) {
3271 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00003272
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003273 case tok::kw_private:
3274 return getLang().OpenCL;
3275
Chris Lattner166a8fc2009-01-04 23:41:41 +00003276 case tok::identifier: // foo::bar
Steve Naroff61f72cb2009-03-09 21:12:44 +00003277 // Unfortunate hack to support "Class.factoryMethod" notation.
3278 if (getLang().ObjC1 && NextToken().is(tok::period))
3279 return false;
John Thompson82287d12010-02-05 00:12:22 +00003280 if (TryAltiVecVectorToken())
3281 return true;
3282 // Fall through.
Douglas Gregord57959a2009-03-27 23:10:48 +00003283 case tok::kw_typename: // typename T::type
Chris Lattner166a8fc2009-01-04 23:41:41 +00003284 // Annotate typenames and C++ scope specifiers. If we get one, just
3285 // recurse to handle whatever we get.
3286 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003287 return true;
3288 if (Tok.is(tok::identifier))
3289 return false;
Douglas Gregor9497a732010-09-16 01:51:54 +00003290
3291 // If we're in Objective-C and we have an Objective-C class type followed
3292 // by an identifier and then either ':' or ']', in a place where an
3293 // expression is permitted, then this is probably a class message send
3294 // missing the initial '['. In this case, we won't consider this to be
3295 // the start of a declaration.
3296 if (DisambiguatingWithExpression &&
3297 isStartOfObjCClassMessageMissingOpenBracket())
3298 return false;
3299
John McCall9ba61662010-02-26 08:45:28 +00003300 return isDeclarationSpecifier();
3301
Chris Lattner166a8fc2009-01-04 23:41:41 +00003302 case tok::coloncolon: // ::foo::bar
3303 if (NextToken().is(tok::kw_new) || // ::new
3304 NextToken().is(tok::kw_delete)) // ::delete
3305 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00003306
Chris Lattner166a8fc2009-01-04 23:41:41 +00003307 // Annotate typenames and C++ scope specifiers. If we get one, just
3308 // recurse to handle whatever we get.
3309 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003310 return true;
3311 return isDeclarationSpecifier();
Mike Stump1eb44332009-09-09 15:08:12 +00003312
Reid Spencer5f016e22007-07-11 17:01:13 +00003313 // storage-class-specifier
3314 case tok::kw_typedef:
3315 case tok::kw_extern:
Steve Naroff8d54bf22007-12-18 00:16:02 +00003316 case tok::kw___private_extern__:
Reid Spencer5f016e22007-07-11 17:01:13 +00003317 case tok::kw_static:
3318 case tok::kw_auto:
3319 case tok::kw_register:
3320 case tok::kw___thread:
Mike Stump1eb44332009-09-09 15:08:12 +00003321
Douglas Gregor8d267c52011-09-09 02:06:17 +00003322 // Modules
3323 case tok::kw___module_private__:
3324
Reid Spencer5f016e22007-07-11 17:01:13 +00003325 // type-specifiers
3326 case tok::kw_short:
3327 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00003328 case tok::kw___int64:
Reid Spencer5f016e22007-07-11 17:01:13 +00003329 case tok::kw_signed:
3330 case tok::kw_unsigned:
3331 case tok::kw__Complex:
3332 case tok::kw__Imaginary:
3333 case tok::kw_void:
3334 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00003335 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00003336 case tok::kw_char16_t:
3337 case tok::kw_char32_t:
3338
Reid Spencer5f016e22007-07-11 17:01:13 +00003339 case tok::kw_int:
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00003340 case tok::kw_half:
Reid Spencer5f016e22007-07-11 17:01:13 +00003341 case tok::kw_float:
3342 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00003343 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00003344 case tok::kw__Bool:
3345 case tok::kw__Decimal32:
3346 case tok::kw__Decimal64:
3347 case tok::kw__Decimal128:
John Thompson82287d12010-02-05 00:12:22 +00003348 case tok::kw___vector:
Mike Stump1eb44332009-09-09 15:08:12 +00003349
Chris Lattner99dc9142008-04-13 18:59:07 +00003350 // struct-or-union-specifier (C99) or class-specifier (C++)
3351 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00003352 case tok::kw_struct:
3353 case tok::kw_union:
3354 // enum-specifier
3355 case tok::kw_enum:
Mike Stump1eb44332009-09-09 15:08:12 +00003356
Reid Spencer5f016e22007-07-11 17:01:13 +00003357 // type-qualifier
3358 case tok::kw_const:
3359 case tok::kw_volatile:
3360 case tok::kw_restrict:
Steve Naroffd1861fd2007-07-31 12:34:36 +00003361
Reid Spencer5f016e22007-07-11 17:01:13 +00003362 // function-specifier
3363 case tok::kw_inline:
Douglas Gregorb48fe382008-10-31 09:07:45 +00003364 case tok::kw_virtual:
3365 case tok::kw_explicit:
Chris Lattnerd6c7c182007-08-09 16:40:21 +00003366
Peter Collingbournec6eb44b2011-04-15 00:35:57 +00003367 // static_assert-declaration
3368 case tok::kw__Static_assert:
3369
Chris Lattner1ef08762007-08-09 17:01:07 +00003370 // GNU typeof support.
3371 case tok::kw_typeof:
Mike Stump1eb44332009-09-09 15:08:12 +00003372
Chris Lattner1ef08762007-08-09 17:01:07 +00003373 // GNU attributes.
Chris Lattnerd6c7c182007-08-09 16:40:21 +00003374 case tok::kw___attribute:
Reid Spencer5f016e22007-07-11 17:01:13 +00003375 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00003376
Francois Pichete3d49b42011-06-19 08:02:06 +00003377 // C++0x decltype.
3378 case tok::kw_decltype:
3379 return true;
3380
Eli Friedmanb001de72011-10-06 23:00:33 +00003381 // C1x _Atomic()
3382 case tok::kw__Atomic:
3383 return true;
3384
Chris Lattnerf3948c42008-07-26 03:38:44 +00003385 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
3386 case tok::less:
3387 return getLang().ObjC1;
Mike Stump1eb44332009-09-09 15:08:12 +00003388
Douglas Gregord9d75e52011-04-27 05:41:15 +00003389 // typedef-name
3390 case tok::annot_typename:
3391 return !DisambiguatingWithExpression ||
3392 !isStartOfObjCClassMessageMissingOpenBracket();
3393
Steve Naroff47f52092009-01-06 19:34:12 +00003394 case tok::kw___declspec:
Steve Naroff239f0732008-12-25 14:16:32 +00003395 case tok::kw___cdecl:
3396 case tok::kw___stdcall:
3397 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00003398 case tok::kw___thiscall:
Eli Friedman290eeb02009-06-08 23:27:34 +00003399 case tok::kw___w64:
3400 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00003401 case tok::kw___ptr32:
Eli Friedman290eeb02009-06-08 23:27:34 +00003402 case tok::kw___forceinline:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003403 case tok::kw___pascal:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00003404 case tok::kw___unaligned:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003405
3406 case tok::kw___private:
3407 case tok::kw___local:
3408 case tok::kw___global:
3409 case tok::kw___constant:
3410 case tok::kw___read_only:
3411 case tok::kw___read_write:
3412 case tok::kw___write_only:
3413
Eli Friedman290eeb02009-06-08 23:27:34 +00003414 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00003415 }
3416}
3417
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003418bool Parser::isConstructorDeclarator() {
3419 TentativeParsingAction TPA(*this);
3420
3421 // Parse the C++ scope specifier.
3422 CXXScopeSpec SS;
John McCallb3d87482010-08-24 05:47:05 +00003423 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), true)) {
John McCall9ba61662010-02-26 08:45:28 +00003424 TPA.Revert();
3425 return false;
3426 }
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003427
3428 // Parse the constructor name.
3429 if (Tok.is(tok::identifier) || Tok.is(tok::annot_template_id)) {
3430 // We already know that we have a constructor name; just consume
3431 // the token.
3432 ConsumeToken();
3433 } else {
3434 TPA.Revert();
3435 return false;
3436 }
3437
3438 // Current class name must be followed by a left parentheses.
3439 if (Tok.isNot(tok::l_paren)) {
3440 TPA.Revert();
3441 return false;
3442 }
3443 ConsumeParen();
3444
3445 // A right parentheses or ellipsis signals that we have a constructor.
3446 if (Tok.is(tok::r_paren) || Tok.is(tok::ellipsis)) {
3447 TPA.Revert();
3448 return true;
3449 }
3450
3451 // If we need to, enter the specified scope.
3452 DeclaratorScopeObj DeclScopeObj(*this, SS);
Douglas Gregor23c94db2010-07-02 17:43:08 +00003453 if (SS.isSet() && Actions.ShouldEnterDeclaratorScope(getCurScope(), SS))
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003454 DeclScopeObj.EnterDeclaratorScope();
3455
Francois Pichetdfaa5fb2011-01-31 04:54:32 +00003456 // Optionally skip Microsoft attributes.
John McCall0b7e6782011-03-24 11:26:52 +00003457 ParsedAttributes Attrs(AttrFactory);
Francois Pichetdfaa5fb2011-01-31 04:54:32 +00003458 MaybeParseMicrosoftAttributes(Attrs);
3459
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003460 // Check whether the next token(s) are part of a declaration
3461 // specifier, in which case we have the start of a parameter and,
3462 // therefore, we know that this is a constructor.
3463 bool IsConstructor = isDeclarationSpecifier();
3464 TPA.Revert();
3465 return IsConstructor;
3466}
Reid Spencer5f016e22007-07-11 17:01:13 +00003467
3468/// ParseTypeQualifierListOpt
Dawn Perchik52fc3142010-09-03 01:29:35 +00003469/// type-qualifier-list: [C99 6.7.5]
3470/// type-qualifier
3471/// [vendor] attributes
3472/// [ only if VendorAttributesAllowed=true ]
3473/// type-qualifier-list type-qualifier
3474/// [vendor] type-qualifier-list attributes
3475/// [ only if VendorAttributesAllowed=true ]
3476/// [C++0x] attribute-specifier[opt] is allowed before cv-qualifier-seq
3477/// [ only if CXX0XAttributesAllowed=true ]
3478/// Note: vendor can be GNU, MS, etc.
Reid Spencer5f016e22007-07-11 17:01:13 +00003479///
Dawn Perchik52fc3142010-09-03 01:29:35 +00003480void Parser::ParseTypeQualifierListOpt(DeclSpec &DS,
3481 bool VendorAttributesAllowed,
Sean Huntbbd37c62009-11-21 08:43:09 +00003482 bool CXX0XAttributesAllowed) {
3483 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier()) {
3484 SourceLocation Loc = Tok.getLocation();
John McCall0b7e6782011-03-24 11:26:52 +00003485 ParsedAttributesWithRange attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00003486 ParseCXX0XAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00003487 if (CXX0XAttributesAllowed)
John McCall7f040a92010-12-24 02:08:15 +00003488 DS.takeAttributesFrom(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00003489 else
3490 Diag(Loc, diag::err_attributes_not_allowed);
3491 }
Abramo Bagnara796aa442011-03-12 11:17:06 +00003492
3493 SourceLocation EndLoc;
3494
Reid Spencer5f016e22007-07-11 17:01:13 +00003495 while (1) {
John McCallfec54012009-08-03 20:12:06 +00003496 bool isInvalid = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00003497 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00003498 unsigned DiagID = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00003499 SourceLocation Loc = Tok.getLocation();
3500
3501 switch (Tok.getKind()) {
Douglas Gregor1a480c42010-08-27 17:35:51 +00003502 case tok::code_completion:
3503 Actions.CodeCompleteTypeQualifiers(DS);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00003504 return cutOffParsing();
Douglas Gregor1a480c42010-08-27 17:35:51 +00003505
Reid Spencer5f016e22007-07-11 17:01:13 +00003506 case tok::kw_const:
John McCallfec54012009-08-03 20:12:06 +00003507 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, DiagID,
3508 getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00003509 break;
3510 case tok::kw_volatile:
John McCallfec54012009-08-03 20:12:06 +00003511 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
3512 getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00003513 break;
3514 case tok::kw_restrict:
John McCallfec54012009-08-03 20:12:06 +00003515 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
3516 getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00003517 break;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003518
3519 // OpenCL qualifiers:
3520 case tok::kw_private:
3521 if (!getLang().OpenCL)
3522 goto DoneWithTypeQuals;
3523 case tok::kw___private:
3524 case tok::kw___global:
3525 case tok::kw___local:
3526 case tok::kw___constant:
3527 case tok::kw___read_only:
3528 case tok::kw___write_only:
3529 case tok::kw___read_write:
3530 ParseOpenCLQualifiers(DS);
3531 break;
3532
Eli Friedman290eeb02009-06-08 23:27:34 +00003533 case tok::kw___w64:
Steve Naroff86bc6cf2008-12-25 14:41:26 +00003534 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00003535 case tok::kw___ptr32:
Steve Naroff239f0732008-12-25 14:16:32 +00003536 case tok::kw___cdecl:
3537 case tok::kw___stdcall:
3538 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00003539 case tok::kw___thiscall:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00003540 case tok::kw___unaligned:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003541 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00003542 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman290eeb02009-06-08 23:27:34 +00003543 continue;
3544 }
3545 goto DoneWithTypeQuals;
Dawn Perchik52fc3142010-09-03 01:29:35 +00003546 case tok::kw___pascal:
3547 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00003548 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik52fc3142010-09-03 01:29:35 +00003549 continue;
3550 }
3551 goto DoneWithTypeQuals;
Reid Spencer5f016e22007-07-11 17:01:13 +00003552 case tok::kw___attribute:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003553 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00003554 ParseGNUAttributes(DS.getAttributes());
Chris Lattner5a69d1c2008-12-18 07:02:59 +00003555 continue; // do *not* consume the next token!
3556 }
3557 // otherwise, FALL THROUGH!
3558 default:
Steve Naroff239f0732008-12-25 14:16:32 +00003559 DoneWithTypeQuals:
Chris Lattner5a69d1c2008-12-18 07:02:59 +00003560 // If this is not a type-qualifier token, we're done reading type
3561 // qualifiers. First verify that DeclSpec's are consistent.
Douglas Gregor9b3064b2009-04-01 22:41:11 +00003562 DS.Finish(Diags, PP);
Abramo Bagnara796aa442011-03-12 11:17:06 +00003563 if (EndLoc.isValid())
3564 DS.SetRangeEnd(EndLoc);
Chris Lattner5a69d1c2008-12-18 07:02:59 +00003565 return;
Reid Spencer5f016e22007-07-11 17:01:13 +00003566 }
Chris Lattnera1fcbad2008-12-18 06:50:14 +00003567
Reid Spencer5f016e22007-07-11 17:01:13 +00003568 // If the specifier combination wasn't legal, issue a diagnostic.
3569 if (isInvalid) {
3570 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner1ab3b962008-11-18 07:48:38 +00003571 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00003572 }
Abramo Bagnara796aa442011-03-12 11:17:06 +00003573 EndLoc = ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00003574 }
3575}
3576
3577
3578/// ParseDeclarator - Parse and verify a newly-initialized declarator.
3579///
3580void Parser::ParseDeclarator(Declarator &D) {
3581 /// This implements the 'declarator' production in the C grammar, then checks
3582 /// for well-formedness and issues diagnostics.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003583 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Reid Spencer5f016e22007-07-11 17:01:13 +00003584}
3585
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003586/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
3587/// is parsed by the function passed to it. Pass null, and the direct-declarator
3588/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003589/// ptr-operator production.
3590///
Sebastian Redlf30208a2009-01-24 21:16:55 +00003591/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
3592/// [C] pointer[opt] direct-declarator
3593/// [C++] direct-declarator
3594/// [C++] ptr-operator declarator
Reid Spencer5f016e22007-07-11 17:01:13 +00003595///
3596/// pointer: [C99 6.7.5]
3597/// '*' type-qualifier-list[opt]
3598/// '*' type-qualifier-list[opt] pointer
3599///
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003600/// ptr-operator:
3601/// '*' cv-qualifier-seq[opt]
3602/// '&'
Sebastian Redl05532f22009-03-15 22:02:01 +00003603/// [C++0x] '&&'
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003604/// [GNU] '&' restrict[opt] attributes[opt]
Sebastian Redl05532f22009-03-15 22:02:01 +00003605/// [GNU?] '&&' restrict[opt] attributes[opt]
Sebastian Redlf30208a2009-01-24 21:16:55 +00003606/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003607void Parser::ParseDeclaratorInternal(Declarator &D,
3608 DirectDeclParseFunction DirectDeclParser) {
Douglas Gregor91a28862009-08-26 14:27:30 +00003609 if (Diags.hasAllExtensionsSilenced())
3610 D.setExtension();
Douglas Gregor2ccccb32010-08-23 18:23:48 +00003611
Sebastian Redlf30208a2009-01-24 21:16:55 +00003612 // C++ member pointers start with a '::' or a nested-name.
3613 // Member pointers get special handling, since there's no place for the
3614 // scope spec in the generic path below.
Chris Lattnerf919bfe2009-03-24 17:04:48 +00003615 if (getLang().CPlusPlus &&
3616 (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) ||
3617 Tok.is(tok::annot_cxxscope))) {
Sebastian Redlf30208a2009-01-24 21:16:55 +00003618 CXXScopeSpec SS;
John McCallb3d87482010-08-24 05:47:05 +00003619 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), true); // ignore fail
John McCall9ba61662010-02-26 08:45:28 +00003620
Jeffrey Yasskinedc28772010-04-07 23:29:58 +00003621 if (SS.isNotEmpty()) {
Mike Stump1eb44332009-09-09 15:08:12 +00003622 if (Tok.isNot(tok::star)) {
Sebastian Redlf30208a2009-01-24 21:16:55 +00003623 // The scope spec really belongs to the direct-declarator.
3624 D.getCXXScopeSpec() = SS;
3625 if (DirectDeclParser)
3626 (this->*DirectDeclParser)(D);
3627 return;
3628 }
3629
3630 SourceLocation Loc = ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00003631 D.SetRangeEnd(Loc);
John McCall0b7e6782011-03-24 11:26:52 +00003632 DeclSpec DS(AttrFactory);
Sebastian Redlf30208a2009-01-24 21:16:55 +00003633 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00003634 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00003635
3636 // Recurse to parse whatever is left.
3637 ParseDeclaratorInternal(D, DirectDeclParser);
3638
3639 // Sema will have to catch (syntactically invalid) pointers into global
3640 // scope. It has to catch pointers into namespace scope anyway.
3641 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
John McCall0b7e6782011-03-24 11:26:52 +00003642 Loc),
3643 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003644 /* Don't replace range end. */SourceLocation());
Sebastian Redlf30208a2009-01-24 21:16:55 +00003645 return;
3646 }
3647 }
3648
3649 tok::TokenKind Kind = Tok.getKind();
Steve Naroff5618bd42008-08-27 16:04:49 +00003650 // Not a pointer, C++ reference, or block.
Chris Lattner9af55002009-03-27 04:18:06 +00003651 if (Kind != tok::star && Kind != tok::caret &&
Chris Lattnerf919bfe2009-03-24 17:04:48 +00003652 (Kind != tok::amp || !getLang().CPlusPlus) &&
Sebastian Redl743de1f2009-03-23 00:00:23 +00003653 // We parse rvalue refs in C++03, because otherwise the errors are scary.
Chris Lattner9af55002009-03-27 04:18:06 +00003654 (Kind != tok::ampamp || !getLang().CPlusPlus)) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003655 if (DirectDeclParser)
3656 (this->*DirectDeclParser)(D);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003657 return;
3658 }
Sebastian Redlf30208a2009-01-24 21:16:55 +00003659
Sebastian Redl05532f22009-03-15 22:02:01 +00003660 // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference,
3661 // '&&' -> rvalue reference
Sebastian Redl743de1f2009-03-23 00:00:23 +00003662 SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&.
Sebastian Redlab197ba2009-02-09 18:23:29 +00003663 D.SetRangeEnd(Loc);
Reid Spencer5f016e22007-07-11 17:01:13 +00003664
Chris Lattner9af55002009-03-27 04:18:06 +00003665 if (Kind == tok::star || Kind == tok::caret) {
Chris Lattner76549142008-02-21 01:32:26 +00003666 // Is a pointer.
John McCall0b7e6782011-03-24 11:26:52 +00003667 DeclSpec DS(AttrFactory);
Sebastian Redlf30208a2009-01-24 21:16:55 +00003668
Reid Spencer5f016e22007-07-11 17:01:13 +00003669 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00003670 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00003671
Reid Spencer5f016e22007-07-11 17:01:13 +00003672 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003673 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroff5618bd42008-08-27 16:04:49 +00003674 if (Kind == tok::star)
3675 // Remember that we parsed a pointer type, and remember the type-quals.
3676 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
Chandler Carruthd067c072011-02-23 18:51:59 +00003677 DS.getConstSpecLoc(),
3678 DS.getVolatileSpecLoc(),
John McCall0b7e6782011-03-24 11:26:52 +00003679 DS.getRestrictSpecLoc()),
3680 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003681 SourceLocation());
Steve Naroff5618bd42008-08-27 16:04:49 +00003682 else
3683 // Remember that we parsed a Block type, and remember the type-quals.
Mike Stump1eb44332009-09-09 15:08:12 +00003684 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
John McCall0b7e6782011-03-24 11:26:52 +00003685 Loc),
3686 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003687 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00003688 } else {
3689 // Is a reference
John McCall0b7e6782011-03-24 11:26:52 +00003690 DeclSpec DS(AttrFactory);
Reid Spencer5f016e22007-07-11 17:01:13 +00003691
Sebastian Redl743de1f2009-03-23 00:00:23 +00003692 // Complain about rvalue references in C++03, but then go on and build
3693 // the declarator.
Richard Smith7fe62082011-10-15 05:09:34 +00003694 if (Kind == tok::ampamp)
3695 Diag(Loc, getLang().CPlusPlus0x ?
3696 diag::warn_cxx98_compat_rvalue_reference :
3697 diag::ext_rvalue_reference);
Sebastian Redl743de1f2009-03-23 00:00:23 +00003698
Reid Spencer5f016e22007-07-11 17:01:13 +00003699 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
3700 // cv-qualifiers are introduced through the use of a typedef or of a
3701 // template type argument, in which case the cv-qualifiers are ignored.
3702 //
3703 // [GNU] Retricted references are allowed.
3704 // [GNU] Attributes on references are allowed.
Sean Huntbbd37c62009-11-21 08:43:09 +00003705 // [C++0x] Attributes on references are not allowed.
3706 ParseTypeQualifierListOpt(DS, true, false);
Sebastian Redlab197ba2009-02-09 18:23:29 +00003707 D.ExtendWithDeclSpec(DS);
Reid Spencer5f016e22007-07-11 17:01:13 +00003708
3709 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
3710 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
3711 Diag(DS.getConstSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00003712 diag::err_invalid_reference_qualifier_application) << "const";
Reid Spencer5f016e22007-07-11 17:01:13 +00003713 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
3714 Diag(DS.getVolatileSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00003715 diag::err_invalid_reference_qualifier_application) << "volatile";
Reid Spencer5f016e22007-07-11 17:01:13 +00003716 }
3717
3718 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003719 ParseDeclaratorInternal(D, DirectDeclParser);
Reid Spencer5f016e22007-07-11 17:01:13 +00003720
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00003721 if (D.getNumTypeObjects() > 0) {
3722 // C++ [dcl.ref]p4: There shall be no references to references.
3723 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
3724 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattnerda83bac2008-11-19 07:37:42 +00003725 if (const IdentifierInfo *II = D.getIdentifier())
3726 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
3727 << II;
3728 else
3729 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
3730 << "type name";
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00003731
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003732 // Once we've complained about the reference-to-reference, we
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00003733 // can go ahead and build the (technically ill-formed)
3734 // declarator: reference collapsing will take care of it.
3735 }
3736 }
3737
Reid Spencer5f016e22007-07-11 17:01:13 +00003738 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner76549142008-02-21 01:32:26 +00003739 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
Sebastian Redl05532f22009-03-15 22:02:01 +00003740 Kind == tok::amp),
John McCall0b7e6782011-03-24 11:26:52 +00003741 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003742 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00003743 }
3744}
3745
3746/// ParseDirectDeclarator
3747/// direct-declarator: [C99 6.7.5]
Douglas Gregor42a552f2008-11-05 20:51:48 +00003748/// [C99] identifier
Reid Spencer5f016e22007-07-11 17:01:13 +00003749/// '(' declarator ')'
3750/// [GNU] '(' attributes declarator ')'
3751/// [C90] direct-declarator '[' constant-expression[opt] ']'
3752/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
3753/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
3754/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
3755/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
3756/// direct-declarator '(' parameter-type-list ')'
3757/// direct-declarator '(' identifier-list[opt] ')'
3758/// [GNU] direct-declarator '(' parameter-forward-declarations
3759/// parameter-type-list[opt] ')'
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003760/// [C++] direct-declarator '(' parameter-declaration-clause ')'
3761/// cv-qualifier-seq[opt] exception-specification[opt]
Douglas Gregorb48fe382008-10-31 09:07:45 +00003762/// [C++] declarator-id
Douglas Gregor42a552f2008-11-05 20:51:48 +00003763///
3764/// declarator-id: [C++ 8]
Douglas Gregora8bc8c92010-12-23 22:44:42 +00003765/// '...'[opt] id-expression
Douglas Gregor42a552f2008-11-05 20:51:48 +00003766/// '::'[opt] nested-name-specifier[opt] type-name
3767///
3768/// id-expression: [C++ 5.1]
3769/// unqualified-id
Douglas Gregordb422df2009-09-25 21:45:23 +00003770/// qualified-id
Douglas Gregor42a552f2008-11-05 20:51:48 +00003771///
3772/// unqualified-id: [C++ 5.1]
Mike Stump1eb44332009-09-09 15:08:12 +00003773/// identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003774/// operator-function-id
Douglas Gregordb422df2009-09-25 21:45:23 +00003775/// conversion-function-id
Mike Stump1eb44332009-09-09 15:08:12 +00003776/// '~' class-name
Douglas Gregor39a8de12009-02-25 19:37:18 +00003777/// template-id
Argyrios Kyrtzidisc7ed9c62008-11-07 22:02:30 +00003778///
Reid Spencer5f016e22007-07-11 17:01:13 +00003779void Parser::ParseDirectDeclarator(Declarator &D) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00003780 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003781
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003782 if (getLang().CPlusPlus && D.mayHaveIdentifier()) {
3783 // ParseDeclaratorInternal might already have parsed the scope.
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00003784 if (D.getCXXScopeSpec().isEmpty()) {
John McCallb3d87482010-08-24 05:47:05 +00003785 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec(), ParsedType(), true);
John McCall9ba61662010-02-26 08:45:28 +00003786 }
3787
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00003788 if (D.getCXXScopeSpec().isValid()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00003789 if (Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
John McCalle7e278b2009-12-11 20:04:54 +00003790 // Change the declaration context for name lookup, until this function
3791 // is exited (and the declarator has been parsed).
3792 DeclScopeObj.EnterDeclaratorScope();
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00003793 }
3794
Douglas Gregora8bc8c92010-12-23 22:44:42 +00003795 // C++0x [dcl.fct]p14:
3796 // There is a syntactic ambiguity when an ellipsis occurs at the end
3797 // of a parameter-declaration-clause without a preceding comma. In
3798 // this case, the ellipsis is parsed as part of the
3799 // abstract-declarator if the type of the parameter names a template
3800 // parameter pack that has not been expanded; otherwise, it is parsed
3801 // as part of the parameter-declaration-clause.
3802 if (Tok.is(tok::ellipsis) &&
3803 !((D.getContext() == Declarator::PrototypeContext ||
3804 D.getContext() == Declarator::BlockLiteralContext) &&
Douglas Gregora8bc8c92010-12-23 22:44:42 +00003805 NextToken().is(tok::r_paren) &&
3806 !Actions.containsUnexpandedParameterPacks(D)))
3807 D.setEllipsisLoc(ConsumeToken());
3808
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003809 if (Tok.is(tok::identifier) || Tok.is(tok::kw_operator) ||
3810 Tok.is(tok::annot_template_id) || Tok.is(tok::tilde)) {
3811 // We found something that indicates the start of an unqualified-id.
3812 // Parse that unqualified-id.
John McCallba9d8532010-04-13 06:39:49 +00003813 bool AllowConstructorName;
3814 if (D.getDeclSpec().hasTypeSpecifier())
3815 AllowConstructorName = false;
3816 else if (D.getCXXScopeSpec().isSet())
3817 AllowConstructorName =
3818 (D.getContext() == Declarator::FileContext ||
3819 (D.getContext() == Declarator::MemberContext &&
3820 D.getDeclSpec().isFriendSpecified()));
3821 else
3822 AllowConstructorName = (D.getContext() == Declarator::MemberContext);
3823
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003824 if (ParseUnqualifiedId(D.getCXXScopeSpec(),
3825 /*EnteringContext=*/true,
3826 /*AllowDestructorName=*/true,
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003827 AllowConstructorName,
John McCallb3d87482010-08-24 05:47:05 +00003828 ParsedType(),
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00003829 D.getName()) ||
3830 // Once we're past the identifier, if the scope was bad, mark the
3831 // whole declarator bad.
3832 D.getCXXScopeSpec().isInvalid()) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00003833 D.SetIdentifier(0, Tok.getLocation());
3834 D.setInvalidType(true);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003835 } else {
3836 // Parsed the unqualified-id; update range information and move along.
3837 if (D.getSourceRange().getBegin().isInvalid())
3838 D.SetRangeBegin(D.getName().getSourceRange().getBegin());
3839 D.SetRangeEnd(D.getName().getSourceRange().getEnd());
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003840 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003841 goto PastIdentifier;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00003842 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003843 } else if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00003844 assert(!getLang().CPlusPlus &&
3845 "There's a C++-specific check for tok::identifier above");
3846 assert(Tok.getIdentifierInfo() && "Not an identifier?");
3847 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
3848 ConsumeToken();
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003849 goto PastIdentifier;
3850 }
3851
3852 if (Tok.is(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003853 // direct-declarator: '(' declarator ')'
3854 // direct-declarator: '(' attributes declarator ')'
3855 // Example: 'char (*X)' or 'int (*XX)(void)'
3856 ParseParenDeclarator(D);
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003857
3858 // If the declarator was parenthesized, we entered the declarator
3859 // scope when parsing the parenthesized declarator, then exited
3860 // the scope already. Re-enter the scope, if we need to.
3861 if (D.getCXXScopeSpec().isSet()) {
Fariborz Jahanian46877cd2010-08-17 23:50:37 +00003862 // If there was an error parsing parenthesized declarator, declarator
3863 // scope may have been enterred before. Don't do it again.
3864 if (!D.isInvalidType() &&
3865 Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003866 // Change the declaration context for name lookup, until this function
3867 // is exited (and the declarator has been parsed).
Fariborz Jahanian46877cd2010-08-17 23:50:37 +00003868 DeclScopeObj.EnterDeclaratorScope();
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003869 }
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00003870 } else if (D.mayOmitIdentifier()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003871 // This could be something simple like "int" (in which case the declarator
3872 // portion is empty), if an abstract-declarator is allowed.
3873 D.SetIdentifier(0, Tok.getLocation());
3874 } else {
Douglas Gregore950d4b2009-03-06 23:28:18 +00003875 if (D.getContext() == Declarator::MemberContext)
3876 Diag(Tok, diag::err_expected_member_name_or_semi)
3877 << D.getDeclSpec().getSourceRange();
3878 else if (getLang().CPlusPlus)
Douglas Gregor2d1c2142009-11-03 19:44:04 +00003879 Diag(Tok, diag::err_expected_unqualified_id) << getLang().CPlusPlus;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003880 else
Chris Lattner1ab3b962008-11-18 07:48:38 +00003881 Diag(Tok, diag::err_expected_ident_lparen);
Reid Spencer5f016e22007-07-11 17:01:13 +00003882 D.SetIdentifier(0, Tok.getLocation());
Chris Lattner1f6f54b2008-11-11 06:13:16 +00003883 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00003884 }
Mike Stump1eb44332009-09-09 15:08:12 +00003885
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00003886 PastIdentifier:
Reid Spencer5f016e22007-07-11 17:01:13 +00003887 assert(D.isPastIdentifier() &&
3888 "Haven't past the location of the identifier yet?");
Mike Stump1eb44332009-09-09 15:08:12 +00003889
Sean Huntbbd37c62009-11-21 08:43:09 +00003890 // Don't parse attributes unless we have an identifier.
John McCall7f040a92010-12-24 02:08:15 +00003891 if (D.getIdentifier())
3892 MaybeParseCXX0XAttributes(D);
Sean Huntbbd37c62009-11-21 08:43:09 +00003893
Reid Spencer5f016e22007-07-11 17:01:13 +00003894 while (1) {
Chris Lattner04d66662007-10-09 17:33:22 +00003895 if (Tok.is(tok::l_paren)) {
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00003896 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
3897 // In such a case, check if we actually have a function declarator; if it
3898 // is not, the declarator has been fully parsed.
Chris Lattner7399ee02008-10-20 02:05:46 +00003899 if (getLang().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
3900 // When not in file scope, warn for ambiguous function declarators, just
3901 // in case the author intended it as a variable definition.
3902 bool warnIfAmbiguous = D.getContext() != Declarator::FileContext;
3903 if (!isCXXFunctionDeclarator(warnIfAmbiguous))
3904 break;
3905 }
John McCall0b7e6782011-03-24 11:26:52 +00003906 ParsedAttributes attrs(AttrFactory);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003907 BalancedDelimiterTracker T(*this, tok::l_paren);
3908 T.consumeOpen();
3909 ParseFunctionDeclarator(D, attrs, T);
Chris Lattner04d66662007-10-09 17:33:22 +00003910 } else if (Tok.is(tok::l_square)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003911 ParseBracketDeclarator(D);
3912 } else {
3913 break;
3914 }
3915 }
3916}
3917
Chris Lattneref4715c2008-04-06 05:45:57 +00003918/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
3919/// only called before the identifier, so these are most likely just grouping
Mike Stump1eb44332009-09-09 15:08:12 +00003920/// parens for precedence. If we find that these are actually function
Chris Lattneref4715c2008-04-06 05:45:57 +00003921/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
3922///
3923/// direct-declarator:
3924/// '(' declarator ')'
3925/// [GNU] '(' attributes declarator ')'
Chris Lattner7399ee02008-10-20 02:05:46 +00003926/// direct-declarator '(' parameter-type-list ')'
3927/// direct-declarator '(' identifier-list[opt] ')'
3928/// [GNU] direct-declarator '(' parameter-forward-declarations
3929/// parameter-type-list[opt] ')'
Chris Lattneref4715c2008-04-06 05:45:57 +00003930///
3931void Parser::ParseParenDeclarator(Declarator &D) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003932 BalancedDelimiterTracker T(*this, tok::l_paren);
3933 T.consumeOpen();
3934
Chris Lattneref4715c2008-04-06 05:45:57 +00003935 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
Mike Stump1eb44332009-09-09 15:08:12 +00003936
Chris Lattner7399ee02008-10-20 02:05:46 +00003937 // Eat any attributes before we look at whether this is a grouping or function
3938 // declarator paren. If this is a grouping paren, the attribute applies to
3939 // the type being built up, for example:
3940 // int (__attribute__(()) *x)(long y)
3941 // If this ends up not being a grouping paren, the attribute applies to the
3942 // first argument, for example:
3943 // int (__attribute__(()) int x)
3944 // In either case, we need to eat any attributes to be able to determine what
3945 // sort of paren this is.
3946 //
John McCall0b7e6782011-03-24 11:26:52 +00003947 ParsedAttributes attrs(AttrFactory);
Chris Lattner7399ee02008-10-20 02:05:46 +00003948 bool RequiresArg = false;
3949 if (Tok.is(tok::kw___attribute)) {
John McCall7f040a92010-12-24 02:08:15 +00003950 ParseGNUAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00003951
Chris Lattner7399ee02008-10-20 02:05:46 +00003952 // We require that the argument list (if this is a non-grouping paren) be
3953 // present even if the attribute list was empty.
3954 RequiresArg = true;
3955 }
Steve Naroff239f0732008-12-25 14:16:32 +00003956 // Eat any Microsoft extensions.
Eli Friedman290eeb02009-06-08 23:27:34 +00003957 if (Tok.is(tok::kw___cdecl) || Tok.is(tok::kw___stdcall) ||
Douglas Gregorf813a2c2010-05-18 16:57:00 +00003958 Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___fastcall) ||
Francois Pichet3bd9aa42011-08-18 09:59:55 +00003959 Tok.is(tok::kw___w64) || Tok.is(tok::kw___ptr64) ||
Francois Pichet58fd97a2011-08-25 00:36:46 +00003960 Tok.is(tok::kw___ptr32) || Tok.is(tok::kw___unaligned)) {
John McCall7f040a92010-12-24 02:08:15 +00003961 ParseMicrosoftTypeAttributes(attrs);
Eli Friedman290eeb02009-06-08 23:27:34 +00003962 }
Dawn Perchik52fc3142010-09-03 01:29:35 +00003963 // Eat any Borland extensions.
Ted Kremenek8113ecf2010-11-10 05:59:39 +00003964 if (Tok.is(tok::kw___pascal))
John McCall7f040a92010-12-24 02:08:15 +00003965 ParseBorlandTypeAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00003966
Chris Lattneref4715c2008-04-06 05:45:57 +00003967 // If we haven't past the identifier yet (or where the identifier would be
3968 // stored, if this is an abstract declarator), then this is probably just
3969 // grouping parens. However, if this could be an abstract-declarator, then
3970 // this could also be the start of function arguments (consider 'void()').
3971 bool isGrouping;
Mike Stump1eb44332009-09-09 15:08:12 +00003972
Chris Lattneref4715c2008-04-06 05:45:57 +00003973 if (!D.mayOmitIdentifier()) {
3974 // If this can't be an abstract-declarator, this *must* be a grouping
3975 // paren, because we haven't seen the identifier yet.
3976 isGrouping = true;
3977 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Argyrios Kyrtzidise25d2702008-10-06 00:07:55 +00003978 (getLang().CPlusPlus && Tok.is(tok::ellipsis)) || // C++ int(...)
Chris Lattneref4715c2008-04-06 05:45:57 +00003979 isDeclarationSpecifier()) { // 'int(int)' is a function.
3980 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
3981 // considered to be a type, not a K&R identifier-list.
3982 isGrouping = false;
3983 } else {
3984 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
3985 isGrouping = true;
3986 }
Mike Stump1eb44332009-09-09 15:08:12 +00003987
Chris Lattneref4715c2008-04-06 05:45:57 +00003988 // If this is a grouping paren, handle:
3989 // direct-declarator: '(' declarator ')'
3990 // direct-declarator: '(' attributes declarator ')'
3991 if (isGrouping) {
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00003992 bool hadGroupingParens = D.hasGroupingParens();
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00003993 D.setGroupingParens(true);
3994
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003995 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattneref4715c2008-04-06 05:45:57 +00003996 // Match the ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003997 T.consumeClose();
3998 D.AddTypeInfo(DeclaratorChunk::getParen(T.getOpenLocation(),
3999 T.getCloseLocation()),
4000 attrs, T.getCloseLocation());
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00004001
4002 D.setGroupingParens(hadGroupingParens);
Chris Lattneref4715c2008-04-06 05:45:57 +00004003 return;
4004 }
Mike Stump1eb44332009-09-09 15:08:12 +00004005
Chris Lattneref4715c2008-04-06 05:45:57 +00004006 // Okay, if this wasn't a grouping paren, it must be the start of a function
4007 // argument list. Recognize that this declarator will never have an
Chris Lattner7399ee02008-10-20 02:05:46 +00004008 // identifier (and remember where it would have been), then call into
4009 // ParseFunctionDeclarator to handle of argument list.
Chris Lattneref4715c2008-04-06 05:45:57 +00004010 D.SetIdentifier(0, Tok.getLocation());
4011
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004012 ParseFunctionDeclarator(D, attrs, T, RequiresArg);
Chris Lattneref4715c2008-04-06 05:45:57 +00004013}
4014
4015/// ParseFunctionDeclarator - We are after the identifier and have parsed the
4016/// declarator D up to a paren, which indicates that we are parsing function
4017/// arguments.
Reid Spencer5f016e22007-07-11 17:01:13 +00004018///
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004019/// If attrs is non-null, then the caller parsed those arguments immediately
Chris Lattner7399ee02008-10-20 02:05:46 +00004020/// after the open paren - they should be considered to be the first argument of
4021/// a parameter. If RequiresArg is true, then the first argument of the
4022/// function is required to be present and required to not be an identifier
4023/// list.
4024///
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004025/// For C++, after the parameter-list, it also parses cv-qualifier-seq[opt],
4026/// (C++0x) ref-qualifier[opt], exception-specification[opt], and
4027/// (C++0x) trailing-return-type[opt].
4028///
4029/// [C++0x] exception-specification:
4030/// dynamic-exception-specification
4031/// noexcept-specification
4032///
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004033void Parser::ParseFunctionDeclarator(Declarator &D,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004034 ParsedAttributes &attrs,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004035 BalancedDelimiterTracker &Tracker,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004036 bool RequiresArg) {
4037 // lparen is already consumed!
4038 assert(D.isPastIdentifier() && "Should not call before identifier!");
4039
4040 // This should be true when the function has typed arguments.
4041 // Otherwise, it is treated as a K&R-style function.
4042 bool HasProto = false;
4043 // Build up an array of information about the parsed arguments.
Chris Lattner5f9e2722011-07-23 10:55:15 +00004044 SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004045 // Remember where we see an ellipsis, if any.
4046 SourceLocation EllipsisLoc;
4047
4048 DeclSpec DS(AttrFactory);
4049 bool RefQualifierIsLValueRef = true;
4050 SourceLocation RefQualifierLoc;
4051 ExceptionSpecificationType ESpecType = EST_None;
4052 SourceRange ESpecRange;
Chris Lattner5f9e2722011-07-23 10:55:15 +00004053 SmallVector<ParsedType, 2> DynamicExceptions;
4054 SmallVector<SourceRange, 2> DynamicExceptionRanges;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004055 ExprResult NoexceptExpr;
4056 ParsedType TrailingReturnType;
4057
4058 SourceLocation EndLoc;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004059 if (isFunctionDeclaratorIdentifierList()) {
4060 if (RequiresArg)
4061 Diag(Tok, diag::err_argument_required_after_attribute);
4062
4063 ParseFunctionDeclaratorIdentifierList(D, ParamInfo);
4064
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004065 Tracker.consumeClose();
4066 EndLoc = Tracker.getCloseLocation();
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004067 } else {
4068 // Enter function-declaration scope, limiting any declarators to the
4069 // function prototype scope, including parameter declarators.
4070 ParseScope PrototypeScope(this,
4071 Scope::FunctionPrototypeScope|Scope::DeclScope);
4072
4073 if (Tok.isNot(tok::r_paren))
4074 ParseParameterDeclarationClause(D, attrs, ParamInfo, EllipsisLoc);
4075 else if (RequiresArg)
4076 Diag(Tok, diag::err_argument_required_after_attribute);
4077
4078 HasProto = ParamInfo.size() || getLang().CPlusPlus;
4079
4080 // If we have the closing ')', eat it.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004081 Tracker.consumeClose();
4082 EndLoc = Tracker.getCloseLocation();
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004083
4084 if (getLang().CPlusPlus) {
4085 MaybeParseCXX0XAttributes(attrs);
4086
4087 // Parse cv-qualifier-seq[opt].
4088 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
4089 if (!DS.getSourceRange().getEnd().isInvalid())
4090 EndLoc = DS.getSourceRange().getEnd();
4091
4092 // Parse ref-qualifier[opt].
4093 if (Tok.is(tok::amp) || Tok.is(tok::ampamp)) {
Richard Smith7fe62082011-10-15 05:09:34 +00004094 Diag(Tok, getLang().CPlusPlus0x ?
4095 diag::warn_cxx98_compat_ref_qualifier :
4096 diag::ext_ref_qualifier);
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004097
4098 RefQualifierIsLValueRef = Tok.is(tok::amp);
4099 RefQualifierLoc = ConsumeToken();
4100 EndLoc = RefQualifierLoc;
4101 }
4102
4103 // Parse exception-specification[opt].
4104 ESpecType = MaybeParseExceptionSpecification(ESpecRange,
4105 DynamicExceptions,
4106 DynamicExceptionRanges,
4107 NoexceptExpr);
4108 if (ESpecType != EST_None)
4109 EndLoc = ESpecRange.getEnd();
4110
4111 // Parse trailing-return-type[opt].
4112 if (getLang().CPlusPlus0x && Tok.is(tok::arrow)) {
Richard Smith7fe62082011-10-15 05:09:34 +00004113 Diag(Tok, diag::warn_cxx98_compat_trailing_return_type);
Douglas Gregorae7902c2011-08-04 15:30:47 +00004114 SourceRange Range;
4115 TrailingReturnType = ParseTrailingReturnType(Range).get();
4116 if (Range.getEnd().isValid())
4117 EndLoc = Range.getEnd();
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004118 }
4119 }
4120
4121 // Leave prototype scope.
4122 PrototypeScope.Exit();
4123 }
4124
4125 // Remember that we parsed a function type, and remember the attributes.
4126 D.AddTypeInfo(DeclaratorChunk::getFunction(HasProto,
4127 /*isVariadic=*/EllipsisLoc.isValid(),
4128 EllipsisLoc,
4129 ParamInfo.data(), ParamInfo.size(),
4130 DS.getTypeQualifiers(),
4131 RefQualifierIsLValueRef,
4132 RefQualifierLoc,
Douglas Gregor90ebed02011-07-13 21:47:47 +00004133 /*MutableLoc=*/SourceLocation(),
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004134 ESpecType, ESpecRange.getBegin(),
4135 DynamicExceptions.data(),
4136 DynamicExceptionRanges.data(),
4137 DynamicExceptions.size(),
4138 NoexceptExpr.isUsable() ?
4139 NoexceptExpr.get() : 0,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004140 Tracker.getOpenLocation(),
4141 EndLoc, D,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004142 TrailingReturnType),
4143 attrs, EndLoc);
4144}
4145
4146/// isFunctionDeclaratorIdentifierList - This parameter list may have an
4147/// identifier list form for a K&R-style function: void foo(a,b,c)
4148///
4149/// Note that identifier-lists are only allowed for normal declarators, not for
4150/// abstract-declarators.
4151bool Parser::isFunctionDeclaratorIdentifierList() {
4152 return !getLang().CPlusPlus
4153 && Tok.is(tok::identifier)
4154 && !TryAltiVecVectorToken()
4155 // K&R identifier lists can't have typedefs as identifiers, per C99
4156 // 6.7.5.3p11.
4157 && (TryAnnotateTypeOrScopeToken() || !Tok.is(tok::annot_typename))
4158 // Identifier lists follow a really simple grammar: the identifiers can
4159 // be followed *only* by a ", identifier" or ")". However, K&R
4160 // identifier lists are really rare in the brave new modern world, and
4161 // it is very common for someone to typo a type in a non-K&R style
4162 // list. If we are presented with something like: "void foo(intptr x,
4163 // float y)", we don't want to start parsing the function declarator as
4164 // though it is a K&R style declarator just because intptr is an
4165 // invalid type.
4166 //
4167 // To handle this, we check to see if the token after the first
4168 // identifier is a "," or ")". Only then do we parse it as an
4169 // identifier list.
4170 && (NextToken().is(tok::comma) || NextToken().is(tok::r_paren));
4171}
4172
4173/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
4174/// we found a K&R-style identifier list instead of a typed parameter list.
4175///
4176/// After returning, ParamInfo will hold the parsed parameters.
4177///
4178/// identifier-list: [C99 6.7.5]
4179/// identifier
4180/// identifier-list ',' identifier
4181///
4182void Parser::ParseFunctionDeclaratorIdentifierList(
4183 Declarator &D,
Chris Lattner5f9e2722011-07-23 10:55:15 +00004184 SmallVector<DeclaratorChunk::ParamInfo, 16> &ParamInfo) {
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004185 // If there was no identifier specified for the declarator, either we are in
4186 // an abstract-declarator, or we are in a parameter declarator which was found
4187 // to be abstract. In abstract-declarators, identifier lists are not valid:
4188 // diagnose this.
4189 if (!D.getIdentifier())
4190 Diag(Tok, diag::ext_ident_list_in_param);
4191
4192 // Maintain an efficient lookup of params we have seen so far.
4193 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
4194
4195 while (1) {
4196 // If this isn't an identifier, report the error and skip until ')'.
4197 if (Tok.isNot(tok::identifier)) {
4198 Diag(Tok, diag::err_expected_ident);
4199 SkipUntil(tok::r_paren, /*StopAtSemi=*/true, /*DontConsume=*/true);
4200 // Forget we parsed anything.
4201 ParamInfo.clear();
4202 return;
4203 }
4204
4205 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
4206
4207 // Reject 'typedef int y; int test(x, y)', but continue parsing.
4208 if (Actions.getTypeName(*ParmII, Tok.getLocation(), getCurScope()))
4209 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
4210
4211 // Verify that the argument identifier has not already been mentioned.
4212 if (!ParamsSoFar.insert(ParmII)) {
4213 Diag(Tok, diag::err_param_redefinition) << ParmII;
4214 } else {
4215 // Remember this identifier in ParamInfo.
4216 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
4217 Tok.getLocation(),
4218 0));
4219 }
4220
4221 // Eat the identifier.
4222 ConsumeToken();
4223
4224 // The list continues if we see a comma.
4225 if (Tok.isNot(tok::comma))
4226 break;
4227 ConsumeToken();
4228 }
4229}
4230
4231/// ParseParameterDeclarationClause - Parse a (possibly empty) parameter-list
4232/// after the opening parenthesis. This function will not parse a K&R-style
4233/// identifier list.
4234///
4235/// D is the declarator being parsed. If attrs is non-null, then the caller
4236/// parsed those arguments immediately after the open paren - they should be
4237/// considered to be the first argument of a parameter.
4238///
4239/// After returning, ParamInfo will hold the parsed parameters. EllipsisLoc will
4240/// be the location of the ellipsis, if any was parsed.
4241///
Reid Spencer5f016e22007-07-11 17:01:13 +00004242/// parameter-type-list: [C99 6.7.5]
4243/// parameter-list
4244/// parameter-list ',' '...'
Douglas Gregored5d6512009-09-22 21:41:40 +00004245/// [C++] parameter-list '...'
Reid Spencer5f016e22007-07-11 17:01:13 +00004246///
4247/// parameter-list: [C99 6.7.5]
4248/// parameter-declaration
4249/// parameter-list ',' parameter-declaration
4250///
4251/// parameter-declaration: [C99 6.7.5]
4252/// declaration-specifiers declarator
Chris Lattner04421082008-04-08 04:40:51 +00004253/// [C++] declaration-specifiers declarator '=' assignment-expression
Reid Spencer5f016e22007-07-11 17:01:13 +00004254/// [GNU] declaration-specifiers declarator attributes
Sebastian Redl50de12f2009-03-24 22:27:57 +00004255/// declaration-specifiers abstract-declarator[opt]
4256/// [C++] declaration-specifiers abstract-declarator[opt]
Chris Lattner8123a952008-04-10 02:22:51 +00004257/// '=' assignment-expression
Reid Spencer5f016e22007-07-11 17:01:13 +00004258/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
4259///
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004260void Parser::ParseParameterDeclarationClause(
4261 Declarator &D,
4262 ParsedAttributes &attrs,
Chris Lattner5f9e2722011-07-23 10:55:15 +00004263 SmallVector<DeclaratorChunk::ParamInfo, 16> &ParamInfo,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004264 SourceLocation &EllipsisLoc) {
Mike Stump1eb44332009-09-09 15:08:12 +00004265
Chris Lattnerf97409f2008-04-06 06:57:35 +00004266 while (1) {
4267 if (Tok.is(tok::ellipsis)) {
Douglas Gregor965acbb2009-02-18 07:07:28 +00004268 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chris Lattnerf97409f2008-04-06 06:57:35 +00004269 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00004270 }
Mike Stump1eb44332009-09-09 15:08:12 +00004271
Chris Lattnerf97409f2008-04-06 06:57:35 +00004272 // Parse the declaration-specifiers.
John McCall54abf7d2009-11-04 02:18:39 +00004273 // Just use the ParsingDeclaration "scope" of the declarator.
John McCall0b7e6782011-03-24 11:26:52 +00004274 DeclSpec DS(AttrFactory);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004275
John McCall7f040a92010-12-24 02:08:15 +00004276 // Skip any Microsoft attributes before a param.
Francois Pichet62ec1f22011-09-17 17:15:52 +00004277 if (getLang().MicrosoftExt && Tok.is(tok::l_square))
John McCall7f040a92010-12-24 02:08:15 +00004278 ParseMicrosoftAttributes(DS.getAttributes());
4279
4280 SourceLocation DSStart = Tok.getLocation();
Chris Lattner7399ee02008-10-20 02:05:46 +00004281
4282 // If the caller parsed attributes for the first argument, add them now.
John McCall7f040a92010-12-24 02:08:15 +00004283 // Take them so that we only apply the attributes to the first parameter.
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004284 // FIXME: If we saw an ellipsis first, this code is not reached. Are the
4285 // attributes lost? Should they even be allowed?
4286 // FIXME: If we can leave the attributes in the token stream somehow, we can
4287 // get rid of a parameter (attrs) and this statement. It might be too much
4288 // hassle.
John McCall7f040a92010-12-24 02:08:15 +00004289 DS.takeAttributesFrom(attrs);
4290
Chris Lattnere64c5492009-02-27 18:38:20 +00004291 ParseDeclarationSpecifiers(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00004292
Chris Lattnerf97409f2008-04-06 06:57:35 +00004293 // Parse the declarator. This is "PrototypeContext", because we must
4294 // accept either 'declarator' or 'abstract-declarator' here.
4295 Declarator ParmDecl(DS, Declarator::PrototypeContext);
4296 ParseDeclarator(ParmDecl);
4297
4298 // Parse GNU attributes, if present.
John McCall7f040a92010-12-24 02:08:15 +00004299 MaybeParseGNUAttributes(ParmDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00004300
Chris Lattnerf97409f2008-04-06 06:57:35 +00004301 // Remember this parsed parameter in ParamInfo.
4302 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
Mike Stump1eb44332009-09-09 15:08:12 +00004303
Douglas Gregor72b505b2008-12-16 21:30:33 +00004304 // DefArgToks is used when the parsing of default arguments needs
4305 // to be delayed.
4306 CachedTokens *DefArgToks = 0;
4307
Chris Lattnerf97409f2008-04-06 06:57:35 +00004308 // If no parameter was specified, verify that *something* was specified,
4309 // otherwise we have a missing type and identifier.
Chris Lattnere64c5492009-02-27 18:38:20 +00004310 if (DS.isEmpty() && ParmDecl.getIdentifier() == 0 &&
4311 ParmDecl.getNumTypeObjects() == 0) {
Chris Lattnerf97409f2008-04-06 06:57:35 +00004312 // Completely missing, emit error.
4313 Diag(DSStart, diag::err_missing_param);
4314 } else {
4315 // Otherwise, we have something. Add it and let semantic analysis try
4316 // to grok it and add the result to the ParamInfo we are building.
Mike Stump1eb44332009-09-09 15:08:12 +00004317
Chris Lattnerf97409f2008-04-06 06:57:35 +00004318 // Inform the actions module about the parameter declarator, so it gets
4319 // added to the current scope.
John McCalld226f652010-08-21 09:40:31 +00004320 Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl);
Chris Lattner04421082008-04-08 04:40:51 +00004321
4322 // Parse the default argument, if any. We parse the default
4323 // arguments in all dialects; the semantic analysis in
4324 // ActOnParamDefaultArgument will reject the default argument in
4325 // C.
4326 if (Tok.is(tok::equal)) {
Douglas Gregor61366e92008-12-24 00:01:03 +00004327 SourceLocation EqualLoc = Tok.getLocation();
4328
Chris Lattner04421082008-04-08 04:40:51 +00004329 // Parse the default argument
Douglas Gregor72b505b2008-12-16 21:30:33 +00004330 if (D.getContext() == Declarator::MemberContext) {
4331 // If we're inside a class definition, cache the tokens
4332 // corresponding to the default argument. We'll actually parse
4333 // them when we see the end of the class definition.
4334 // FIXME: Templates will require something similar.
4335 // FIXME: Can we use a smart pointer for Toks?
4336 DefArgToks = new CachedTokens;
4337
Mike Stump1eb44332009-09-09 15:08:12 +00004338 if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks,
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +00004339 /*StopAtSemi=*/true,
4340 /*ConsumeFinalToken=*/false)) {
Douglas Gregor72b505b2008-12-16 21:30:33 +00004341 delete DefArgToks;
4342 DefArgToks = 0;
Douglas Gregor61366e92008-12-24 00:01:03 +00004343 Actions.ActOnParamDefaultArgumentError(Param);
Argyrios Kyrtzidis2b602ad2010-08-06 09:47:24 +00004344 } else {
4345 // Mark the end of the default argument so that we know when to
4346 // stop when we parse it later on.
4347 Token DefArgEnd;
4348 DefArgEnd.startToken();
4349 DefArgEnd.setKind(tok::cxx_defaultarg_end);
4350 DefArgEnd.setLocation(Tok.getLocation());
4351 DefArgToks->push_back(DefArgEnd);
Mike Stump1eb44332009-09-09 15:08:12 +00004352 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc,
Anders Carlsson5e300d12009-06-12 16:51:40 +00004353 (*DefArgToks)[1].getLocation());
Argyrios Kyrtzidis2b602ad2010-08-06 09:47:24 +00004354 }
Chris Lattner04421082008-04-08 04:40:51 +00004355 } else {
Douglas Gregor72b505b2008-12-16 21:30:33 +00004356 // Consume the '='.
Douglas Gregor61366e92008-12-24 00:01:03 +00004357 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00004358
Douglas Gregorbe0f7bd2010-09-11 20:24:53 +00004359 // The argument isn't actually potentially evaluated unless it is
4360 // used.
4361 EnterExpressionEvaluationContext Eval(Actions,
4362 Sema::PotentiallyEvaluatedIfUsed);
4363
John McCall60d7b3a2010-08-24 06:29:42 +00004364 ExprResult DefArgResult(ParseAssignmentExpression());
Douglas Gregor72b505b2008-12-16 21:30:33 +00004365 if (DefArgResult.isInvalid()) {
4366 Actions.ActOnParamDefaultArgumentError(Param);
4367 SkipUntil(tok::comma, tok::r_paren, true, true);
4368 } else {
4369 // Inform the actions module about the default argument
4370 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
John McCall9ae2f072010-08-23 23:25:46 +00004371 DefArgResult.take());
Douglas Gregor72b505b2008-12-16 21:30:33 +00004372 }
Chris Lattner04421082008-04-08 04:40:51 +00004373 }
4374 }
Mike Stump1eb44332009-09-09 15:08:12 +00004375
4376 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
4377 ParmDecl.getIdentifierLoc(), Param,
Douglas Gregor72b505b2008-12-16 21:30:33 +00004378 DefArgToks));
Chris Lattnerf97409f2008-04-06 06:57:35 +00004379 }
4380
4381 // If the next token is a comma, consume it and keep reading arguments.
Douglas Gregored5d6512009-09-22 21:41:40 +00004382 if (Tok.isNot(tok::comma)) {
4383 if (Tok.is(tok::ellipsis)) {
Douglas Gregored5d6512009-09-22 21:41:40 +00004384 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
4385
4386 if (!getLang().CPlusPlus) {
4387 // We have ellipsis without a preceding ',', which is ill-formed
4388 // in C. Complain and provide the fix.
4389 Diag(EllipsisLoc, diag::err_missing_comma_before_ellipsis)
Douglas Gregor849b2432010-03-31 17:46:05 +00004390 << FixItHint::CreateInsertion(EllipsisLoc, ", ");
Douglas Gregored5d6512009-09-22 21:41:40 +00004391 }
4392 }
4393
4394 break;
4395 }
Mike Stump1eb44332009-09-09 15:08:12 +00004396
Chris Lattnerf97409f2008-04-06 06:57:35 +00004397 // Consume the comma.
4398 ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00004399 }
Mike Stump1eb44332009-09-09 15:08:12 +00004400
Chris Lattner66d28652008-04-06 06:34:08 +00004401}
Chris Lattneref4715c2008-04-06 05:45:57 +00004402
Reid Spencer5f016e22007-07-11 17:01:13 +00004403/// [C90] direct-declarator '[' constant-expression[opt] ']'
4404/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
4405/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
4406/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
4407/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
4408void Parser::ParseBracketDeclarator(Declarator &D) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004409 BalancedDelimiterTracker T(*this, tok::l_square);
4410 T.consumeOpen();
Mike Stump1eb44332009-09-09 15:08:12 +00004411
Chris Lattner378c7e42008-12-18 07:27:21 +00004412 // C array syntax has many features, but by-far the most common is [] and [4].
4413 // This code does a fast path to handle some of the most obvious cases.
4414 if (Tok.getKind() == tok::r_square) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004415 T.consumeClose();
John McCall0b7e6782011-03-24 11:26:52 +00004416 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00004417 MaybeParseCXX0XAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00004418
Chris Lattner378c7e42008-12-18 07:27:21 +00004419 // Remember that we parsed the empty array type.
John McCall60d7b3a2010-08-24 06:29:42 +00004420 ExprResult NumElements;
John McCall0b7e6782011-03-24 11:26:52 +00004421 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004422 T.getOpenLocation(),
4423 T.getCloseLocation()),
4424 attrs, T.getCloseLocation());
Chris Lattner378c7e42008-12-18 07:27:21 +00004425 return;
4426 } else if (Tok.getKind() == tok::numeric_constant &&
4427 GetLookAheadToken(1).is(tok::r_square)) {
4428 // [4] is very common. Parse the numeric constant expression.
John McCall60d7b3a2010-08-24 06:29:42 +00004429 ExprResult ExprRes(Actions.ActOnNumericConstant(Tok));
Chris Lattner378c7e42008-12-18 07:27:21 +00004430 ConsumeToken();
4431
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004432 T.consumeClose();
John McCall0b7e6782011-03-24 11:26:52 +00004433 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00004434 MaybeParseCXX0XAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00004435
Chris Lattner378c7e42008-12-18 07:27:21 +00004436 // Remember that we parsed a array type, and remember its features.
John McCall0b7e6782011-03-24 11:26:52 +00004437 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, 0,
John McCall7f040a92010-12-24 02:08:15 +00004438 ExprRes.release(),
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004439 T.getOpenLocation(),
4440 T.getCloseLocation()),
4441 attrs, T.getCloseLocation());
Chris Lattner378c7e42008-12-18 07:27:21 +00004442 return;
4443 }
Mike Stump1eb44332009-09-09 15:08:12 +00004444
Reid Spencer5f016e22007-07-11 17:01:13 +00004445 // If valid, this location is the position where we read the 'static' keyword.
4446 SourceLocation StaticLoc;
Chris Lattner04d66662007-10-09 17:33:22 +00004447 if (Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00004448 StaticLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00004449
Reid Spencer5f016e22007-07-11 17:01:13 +00004450 // If there is a type-qualifier-list, read it now.
Chris Lattnera1fcbad2008-12-18 06:50:14 +00004451 // Type qualifiers in an array subscript are a C99 feature.
John McCall0b7e6782011-03-24 11:26:52 +00004452 DeclSpec DS(AttrFactory);
Chris Lattner5a69d1c2008-12-18 07:02:59 +00004453 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Mike Stump1eb44332009-09-09 15:08:12 +00004454
Reid Spencer5f016e22007-07-11 17:01:13 +00004455 // If we haven't already read 'static', check to see if there is one after the
4456 // type-qualifier-list.
Chris Lattner04d66662007-10-09 17:33:22 +00004457 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00004458 StaticLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00004459
Reid Spencer5f016e22007-07-11 17:01:13 +00004460 // Handle "direct-declarator [ type-qual-list[opt] * ]".
4461 bool isStar = false;
John McCall60d7b3a2010-08-24 06:29:42 +00004462 ExprResult NumElements;
Mike Stump1eb44332009-09-09 15:08:12 +00004463
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00004464 // Handle the case where we have '[*]' as the array size. However, a leading
4465 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
4466 // the the token after the star is a ']'. Since stars in arrays are
4467 // infrequent, use of lookahead is not costly here.
4468 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattnera711dd02008-04-06 05:27:21 +00004469 ConsumeToken(); // Eat the '*'.
Reid Spencer5f016e22007-07-11 17:01:13 +00004470
Chris Lattnera1fcbad2008-12-18 06:50:14 +00004471 if (StaticLoc.isValid()) {
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00004472 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattnera1fcbad2008-12-18 06:50:14 +00004473 StaticLoc = SourceLocation(); // Drop the static.
4474 }
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00004475 isStar = true;
Chris Lattner04d66662007-10-09 17:33:22 +00004476 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner378c7e42008-12-18 07:27:21 +00004477 // Note, in C89, this production uses the constant-expr production instead
4478 // of assignment-expr. The only difference is that assignment-expr allows
4479 // things like '=' and '*='. Sema rejects these in C89 mode because they
4480 // are not i-c-e's, so we don't need to distinguish between the two here.
Mike Stump1eb44332009-09-09 15:08:12 +00004481
Douglas Gregore0762c92009-06-19 23:52:42 +00004482 // Parse the constant-expression or assignment-expression now (depending
4483 // on dialect).
4484 if (getLang().CPlusPlus)
4485 NumElements = ParseConstantExpression();
4486 else
4487 NumElements = ParseAssignmentExpression();
Reid Spencer5f016e22007-07-11 17:01:13 +00004488 }
Mike Stump1eb44332009-09-09 15:08:12 +00004489
Reid Spencer5f016e22007-07-11 17:01:13 +00004490 // If there was an error parsing the assignment-expression, recover.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00004491 if (NumElements.isInvalid()) {
Chris Lattner5cb10d32009-04-24 22:30:50 +00004492 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00004493 // If the expression was invalid, skip it.
4494 SkipUntil(tok::r_square);
4495 return;
4496 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00004497
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004498 T.consumeClose();
Sebastian Redlab197ba2009-02-09 18:23:29 +00004499
John McCall0b7e6782011-03-24 11:26:52 +00004500 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00004501 MaybeParseCXX0XAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00004502
Chris Lattner378c7e42008-12-18 07:27:21 +00004503 // Remember that we parsed a array type, and remember its features.
John McCall0b7e6782011-03-24 11:26:52 +00004504 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
Reid Spencer5f016e22007-07-11 17:01:13 +00004505 StaticLoc.isValid(), isStar,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00004506 NumElements.release(),
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004507 T.getOpenLocation(),
4508 T.getCloseLocation()),
4509 attrs, T.getCloseLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00004510}
4511
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004512/// [GNU] typeof-specifier:
4513/// typeof ( expressions )
4514/// typeof ( type-name )
4515/// [GNU/C++] typeof unary-expression
Steve Naroffd1861fd2007-07-31 12:34:36 +00004516///
4517void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner04d66662007-10-09 17:33:22 +00004518 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004519 Token OpTok = Tok;
Steve Naroffd1861fd2007-07-31 12:34:36 +00004520 SourceLocation StartLoc = ConsumeToken();
4521
John McCallcfb708c2010-01-13 20:03:27 +00004522 const bool hasParens = Tok.is(tok::l_paren);
4523
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004524 bool isCastExpr;
John McCallb3d87482010-08-24 05:47:05 +00004525 ParsedType CastTy;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004526 SourceRange CastRange;
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00004527 ExprResult Operand = ParseExprAfterUnaryExprOrTypeTrait(OpTok, isCastExpr,
4528 CastTy, CastRange);
John McCallcfb708c2010-01-13 20:03:27 +00004529 if (hasParens)
4530 DS.setTypeofParensRange(CastRange);
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004531
4532 if (CastRange.getEnd().isInvalid())
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004533 // FIXME: Not accurate, the range gets one token more than it should.
4534 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004535 else
4536 DS.SetRangeEnd(CastRange.getEnd());
Mike Stump1eb44332009-09-09 15:08:12 +00004537
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004538 if (isCastExpr) {
4539 if (!CastTy) {
4540 DS.SetTypeSpecError();
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004541 return;
Douglas Gregor809070a2009-02-18 17:45:20 +00004542 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004543
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004544 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00004545 unsigned DiagID;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004546 // Check for duplicate type specifiers (e.g. "int typeof(int)").
4547 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00004548 DiagID, CastTy))
4549 Diag(StartLoc, DiagID) << PrevSpec;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004550 return;
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004551 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004552
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004553 // If we get here, the operand to the typeof was an expresion.
4554 if (Operand.isInvalid()) {
4555 DS.SetTypeSpecError();
Steve Naroff9dfa7b42007-08-02 02:53:48 +00004556 return;
Steve Naroffd1861fd2007-07-31 12:34:36 +00004557 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004558
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004559 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00004560 unsigned DiagID;
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004561 // Check for duplicate type specifiers (e.g. "int typeof(int)").
4562 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
John McCallb3d87482010-08-24 05:47:05 +00004563 DiagID, Operand.get()))
John McCallfec54012009-08-03 20:12:06 +00004564 Diag(StartLoc, DiagID) << PrevSpec;
Steve Naroffd1861fd2007-07-31 12:34:36 +00004565}
Chris Lattner1b492422010-02-28 18:33:55 +00004566
Eli Friedmanb001de72011-10-06 23:00:33 +00004567/// [C1X] atomic-specifier:
4568/// _Atomic ( type-name )
4569///
4570void Parser::ParseAtomicSpecifier(DeclSpec &DS) {
4571 assert(Tok.is(tok::kw__Atomic) && "Not an atomic specifier");
4572
4573 SourceLocation StartLoc = ConsumeToken();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004574 BalancedDelimiterTracker T(*this, tok::l_paren);
4575 if (T.expectAndConsume(diag::err_expected_lparen_after, "_Atomic")) {
Eli Friedmanb001de72011-10-06 23:00:33 +00004576 SkipUntil(tok::r_paren);
4577 return;
4578 }
4579
4580 TypeResult Result = ParseTypeName();
4581 if (Result.isInvalid()) {
4582 SkipUntil(tok::r_paren);
4583 return;
4584 }
4585
4586 // Match the ')'
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004587 T.consumeClose();
Eli Friedmanb001de72011-10-06 23:00:33 +00004588
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004589 if (T.getCloseLocation().isInvalid())
Eli Friedmanb001de72011-10-06 23:00:33 +00004590 return;
4591
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004592 DS.setTypeofParensRange(T.getRange());
4593 DS.SetRangeEnd(T.getCloseLocation());
Eli Friedmanb001de72011-10-06 23:00:33 +00004594
4595 const char *PrevSpec = 0;
4596 unsigned DiagID;
4597 if (DS.SetTypeSpecType(DeclSpec::TST_atomic, StartLoc, PrevSpec,
4598 DiagID, Result.release()))
4599 Diag(StartLoc, DiagID) << PrevSpec;
4600}
4601
Chris Lattner1b492422010-02-28 18:33:55 +00004602
4603/// TryAltiVecVectorTokenOutOfLine - Out of line body that should only be called
4604/// from TryAltiVecVectorToken.
4605bool Parser::TryAltiVecVectorTokenOutOfLine() {
4606 Token Next = NextToken();
4607 switch (Next.getKind()) {
4608 default: return false;
4609 case tok::kw_short:
4610 case tok::kw_long:
4611 case tok::kw_signed:
4612 case tok::kw_unsigned:
4613 case tok::kw_void:
4614 case tok::kw_char:
4615 case tok::kw_int:
4616 case tok::kw_float:
4617 case tok::kw_double:
4618 case tok::kw_bool:
4619 case tok::kw___pixel:
4620 Tok.setKind(tok::kw___vector);
4621 return true;
4622 case tok::identifier:
4623 if (Next.getIdentifierInfo() == Ident_pixel) {
4624 Tok.setKind(tok::kw___vector);
4625 return true;
4626 }
4627 return false;
4628 }
4629}
4630
4631bool Parser::TryAltiVecTokenOutOfLine(DeclSpec &DS, SourceLocation Loc,
4632 const char *&PrevSpec, unsigned &DiagID,
4633 bool &isInvalid) {
4634 if (Tok.getIdentifierInfo() == Ident_vector) {
4635 Token Next = NextToken();
4636 switch (Next.getKind()) {
4637 case tok::kw_short:
4638 case tok::kw_long:
4639 case tok::kw_signed:
4640 case tok::kw_unsigned:
4641 case tok::kw_void:
4642 case tok::kw_char:
4643 case tok::kw_int:
4644 case tok::kw_float:
4645 case tok::kw_double:
4646 case tok::kw_bool:
4647 case tok::kw___pixel:
4648 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
4649 return true;
4650 case tok::identifier:
4651 if (Next.getIdentifierInfo() == Ident_pixel) {
4652 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
4653 return true;
4654 }
4655 break;
4656 default:
4657 break;
4658 }
Douglas Gregora8f031f2010-06-16 15:28:57 +00004659 } else if ((Tok.getIdentifierInfo() == Ident_pixel) &&
Chris Lattner1b492422010-02-28 18:33:55 +00004660 DS.isTypeAltiVecVector()) {
4661 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
4662 return true;
4663 }
4664 return false;
4665}