blob: c5cedeafbb3a5219bd150e59e7ed240de1dee828 [file] [log] [blame]
Chris Lattner7ad0fbe2006-11-05 07:46:30 +00001//===--- ParseDecl.cpp - Declaration Parsing ------------------------------===//
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Declaration portions of the Parser interfaces.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Parse/Parser.h"
Chris Lattner60f36222009-01-29 05:15:15 +000015#include "clang/Parse/ParseDiagnostic.h"
Peter Collingbourne599cb8e2011-03-18 22:38:29 +000016#include "clang/Basic/OpenCL.h"
John McCall8b0666c2010-08-20 18:27:03 +000017#include "clang/Sema/Scope.h"
18#include "clang/Sema/ParsedTemplate.h"
John McCallfaf5fb42010-08-26 23:41:50 +000019#include "clang/Sema/PrettyDeclStackTrace.h"
Chris Lattner8a9a97a2009-12-10 00:21:05 +000020#include "RAIIObjectsForParser.h"
Chris Lattnerad9ac942007-01-23 01:14:52 +000021#include "llvm/ADT/SmallSet.h"
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +000022#include "llvm/ADT/StringSwitch.h"
Chris Lattnerc0acd3d2006-07-31 05:13:43 +000023using namespace clang;
24
25//===----------------------------------------------------------------------===//
26// C99 6.7: Declarations.
27//===----------------------------------------------------------------------===//
28
Chris Lattnerf5fbd792006-08-10 23:56:11 +000029/// ParseTypeName
30/// type-name: [C99 6.7.6]
31/// specifier-qualifier-list abstract-declarator[opt]
Sebastian Redlbd150f42008-11-21 19:14:01 +000032///
33/// Called type-id in C++.
Douglas Gregor205d5e32011-01-31 16:09:46 +000034TypeResult Parser::ParseTypeName(SourceRange *Range,
John McCall31168b02011-06-15 23:02:42 +000035 Declarator::TheContext Context,
Richard Smithcd1c0552011-07-01 19:46:12 +000036 ObjCDeclSpec *objcQuals,
37 AccessSpecifier AS,
38 Decl **OwnedType) {
Chris Lattnerf5fbd792006-08-10 23:56:11 +000039 // Parse the common declaration-specifiers piece.
John McCall084e83d2011-03-24 11:26:52 +000040 DeclSpec DS(AttrFactory);
John McCall31168b02011-06-15 23:02:42 +000041 DS.setObjCQualifiers(objcQuals);
Richard Smithcd1c0552011-07-01 19:46:12 +000042 ParseSpecifierQualifierList(DS, AS);
43 if (OwnedType)
44 *OwnedType = DS.isTypeSpecOwned() ? DS.getRepAsDecl() : 0;
Sebastian Redld6434562009-05-29 18:02:33 +000045
Chris Lattnerf5fbd792006-08-10 23:56:11 +000046 // Parse the abstract-declarator, if present.
Douglas Gregor205d5e32011-01-31 16:09:46 +000047 Declarator DeclaratorInfo(DS, Context);
Chris Lattnerf5fbd792006-08-10 23:56:11 +000048 ParseDeclarator(DeclaratorInfo);
Sebastian Redld6434562009-05-29 18:02:33 +000049 if (Range)
50 *Range = DeclaratorInfo.getSourceRange();
51
Chris Lattnerf6d1c9c2009-04-25 08:06:05 +000052 if (DeclaratorInfo.isInvalidType())
Douglas Gregor220cac52009-02-18 17:45:20 +000053 return true;
54
Douglas Gregor0be31a22010-07-02 17:43:08 +000055 return Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
Chris Lattnerf5fbd792006-08-10 23:56:11 +000056}
57
Caitlin Sadowski9385dd72011-09-08 17:42:22 +000058
59/// isAttributeLateParsed - Return true if the attribute has arguments that
60/// require late parsing.
61static bool isAttributeLateParsed(const IdentifierInfo &II) {
62 return llvm::StringSwitch<bool>(II.getName())
63#include "clang/Parse/AttrLateParsed.inc"
64 .Default(false);
65}
66
67
Alexis Hunt96d5c762009-11-21 08:43:09 +000068/// ParseGNUAttributes - Parse a non-empty attributes list.
Chris Lattnerb8cd5c22006-08-15 04:10:46 +000069///
70/// [GNU] attributes:
71/// attribute
72/// attributes attribute
73///
74/// [GNU] attribute:
75/// '__attribute__' '(' '(' attribute-list ')' ')'
76///
77/// [GNU] attribute-list:
78/// attrib
79/// attribute_list ',' attrib
80///
81/// [GNU] attrib:
82/// empty
Steve Naroff0f2fe172007-06-01 17:11:19 +000083/// attrib-name
84/// attrib-name '(' identifier ')'
85/// attrib-name '(' identifier ',' nonempty-expr-list ')'
86/// attrib-name '(' argument-expression-list [C99 6.5.2] ')'
Chris Lattnerb8cd5c22006-08-15 04:10:46 +000087///
Steve Naroff0f2fe172007-06-01 17:11:19 +000088/// [GNU] attrib-name:
89/// identifier
90/// typespec
91/// typequal
92/// storageclass
Mike Stump11289f42009-09-09 15:08:12 +000093///
Steve Naroff0f2fe172007-06-01 17:11:19 +000094/// FIXME: The GCC grammar/code for this construct implies we need two
Mike Stump11289f42009-09-09 15:08:12 +000095/// token lookahead. Comment from gcc: "If they start with an identifier
96/// which is followed by a comma or close parenthesis, then the arguments
Steve Naroff0f2fe172007-06-01 17:11:19 +000097/// start with that identifier; otherwise they are an expression list."
98///
99/// At the moment, I am not doing 2 token lookahead. I am also unaware of
100/// any attributes that don't work (based on my limited testing). Most
101/// attributes are very simple in practice. Until we find a bug, I don't see
102/// a pressing need to implement the 2 token lookahead.
Chris Lattnerb8cd5c22006-08-15 04:10:46 +0000103
John McCall53fa7142010-12-24 02:08:15 +0000104void Parser::ParseGNUAttributes(ParsedAttributes &attrs,
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000105 SourceLocation *endLoc,
106 LateParsedAttrList *LateAttrs) {
Alexis Hunt96d5c762009-11-21 08:43:09 +0000107 assert(Tok.is(tok::kw___attribute) && "Not a GNU attribute list!");
Mike Stump11289f42009-09-09 15:08:12 +0000108
Chris Lattner76c72282007-10-09 17:33:22 +0000109 while (Tok.is(tok::kw___attribute)) {
Steve Naroff0f2fe172007-06-01 17:11:19 +0000110 ConsumeToken();
111 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
112 "attribute")) {
113 SkipUntil(tok::r_paren, true); // skip until ) or ;
John McCall53fa7142010-12-24 02:08:15 +0000114 return;
Steve Naroff0f2fe172007-06-01 17:11:19 +0000115 }
116 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, "(")) {
117 SkipUntil(tok::r_paren, true); // skip until ) or ;
John McCall53fa7142010-12-24 02:08:15 +0000118 return;
Steve Naroff0f2fe172007-06-01 17:11:19 +0000119 }
120 // Parse the attribute-list. e.g. __attribute__(( weak, alias("__f") ))
Chris Lattner76c72282007-10-09 17:33:22 +0000121 while (Tok.is(tok::identifier) || isDeclarationSpecifier() ||
122 Tok.is(tok::comma)) {
Mike Stump11289f42009-09-09 15:08:12 +0000123 if (Tok.is(tok::comma)) {
Steve Naroff0f2fe172007-06-01 17:11:19 +0000124 // allows for empty/non-empty attributes. ((__vector_size__(16),,,,))
125 ConsumeToken();
126 continue;
127 }
128 // we have an identifier or declaration specifier (const, int, etc.)
129 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
130 SourceLocation AttrNameLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000131
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000132 if (Tok.is(tok::l_paren)) {
133 // handle "parameterized" attributes
134 if (LateAttrs && !ClassStack.empty() &&
135 isAttributeLateParsed(*AttrName)) {
136 // Delayed parsing is only available for attributes that occur
137 // in certain locations within a class scope.
138 LateParsedAttribute *LA =
139 new LateParsedAttribute(this, *AttrName, AttrNameLoc);
140 LateAttrs->push_back(LA);
141 getCurrentClass().LateParsedDeclarations.push_back(LA);
Mike Stump11289f42009-09-09 15:08:12 +0000142
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000143 // consume everything up to and including the matching right parens
144 ConsumeAndStoreUntil(tok::r_paren, LA->Toks, true, false);
Mike Stump11289f42009-09-09 15:08:12 +0000145
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000146 Token Eof;
147 Eof.startToken();
148 Eof.setLocation(Tok.getLocation());
149 LA->Toks.push_back(Eof);
150 } else {
151 ParseGNUAttributeArgs(AttrName, AttrNameLoc, attrs, endLoc);
Steve Naroff0f2fe172007-06-01 17:11:19 +0000152 }
153 } else {
John McCall084e83d2011-03-24 11:26:52 +0000154 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc,
155 0, SourceLocation(), 0, 0);
Steve Naroff0f2fe172007-06-01 17:11:19 +0000156 }
157 }
Steve Naroff98d153c2007-06-06 23:19:11 +0000158 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
Steve Naroff98d153c2007-06-06 23:19:11 +0000159 SkipUntil(tok::r_paren, false);
Alexis Hunt96d5c762009-11-21 08:43:09 +0000160 SourceLocation Loc = Tok.getLocation();
Sebastian Redlf6591ca2009-02-09 18:23:29 +0000161 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) {
162 SkipUntil(tok::r_paren, false);
163 }
John McCall53fa7142010-12-24 02:08:15 +0000164 if (endLoc)
165 *endLoc = Loc;
Steve Naroff0f2fe172007-06-01 17:11:19 +0000166 }
Steve Naroff0f2fe172007-06-01 17:11:19 +0000167}
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000168
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000169
170/// Parse the arguments to a parameterized GNU attribute
171void Parser::ParseGNUAttributeArgs(IdentifierInfo *AttrName,
172 SourceLocation AttrNameLoc,
173 ParsedAttributes &Attrs,
174 SourceLocation *EndLoc) {
175
176 assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('");
177
178 // Availability attributes have their own grammar.
179 if (AttrName->isStr("availability")) {
180 ParseAvailabilityAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc);
181 return;
182 }
183 // Thread safety attributes fit into the FIXME case above, so we
184 // just parse the arguments as a list of expressions
185 if (IsThreadSafetyAttribute(AttrName->getName())) {
186 ParseThreadSafetyAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc);
187 return;
188 }
189
190 ConsumeParen(); // ignore the left paren loc for now
191
192 if (Tok.is(tok::identifier)) {
193 IdentifierInfo *ParmName = Tok.getIdentifierInfo();
194 SourceLocation ParmLoc = ConsumeToken();
195
196 if (Tok.is(tok::r_paren)) {
197 // __attribute__(( mode(byte) ))
198 ConsumeParen(); // ignore the right paren loc for now
199 Attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc,
200 ParmName, ParmLoc, 0, 0);
201 } else if (Tok.is(tok::comma)) {
202 ConsumeToken();
203 // __attribute__(( format(printf, 1, 2) ))
204 ExprVector ArgExprs(Actions);
205 bool ArgExprsOk = true;
206
207 // now parse the non-empty comma separated list of expressions
208 while (1) {
209 ExprResult ArgExpr(ParseAssignmentExpression());
210 if (ArgExpr.isInvalid()) {
211 ArgExprsOk = false;
212 SkipUntil(tok::r_paren);
213 break;
214 } else {
215 ArgExprs.push_back(ArgExpr.release());
216 }
217 if (Tok.isNot(tok::comma))
218 break;
219 ConsumeToken(); // Eat the comma, move to the next argument
220 }
221 if (ArgExprsOk && Tok.is(tok::r_paren)) {
222 ConsumeParen(); // ignore the right paren loc for now
223 Attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc,
224 ParmName, ParmLoc, ArgExprs.take(), ArgExprs.size());
225 }
226 }
227 } else { // not an identifier
228 switch (Tok.getKind()) {
229 case tok::r_paren:
230 // parse a possibly empty comma separated list of expressions
231 // __attribute__(( nonnull() ))
232 ConsumeParen(); // ignore the right paren loc for now
233 Attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc,
234 0, SourceLocation(), 0, 0);
235 break;
236 case tok::kw_char:
237 case tok::kw_wchar_t:
238 case tok::kw_char16_t:
239 case tok::kw_char32_t:
240 case tok::kw_bool:
241 case tok::kw_short:
242 case tok::kw_int:
243 case tok::kw_long:
244 case tok::kw___int64:
245 case tok::kw_signed:
246 case tok::kw_unsigned:
247 case tok::kw_float:
248 case tok::kw_double:
249 case tok::kw_void:
250 case tok::kw_typeof: {
251 AttributeList *attr
252 = Attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc,
253 0, SourceLocation(), 0, 0);
254 if (attr->getKind() == AttributeList::AT_IBOutletCollection)
255 Diag(Tok, diag::err_iboutletcollection_builtintype);
256 // If it's a builtin type name, eat it and expect a rparen
257 // __attribute__(( vec_type_hint(char) ))
258 ConsumeToken();
259 if (Tok.is(tok::r_paren))
260 ConsumeParen();
261 break;
262 }
263 default:
264 // __attribute__(( aligned(16) ))
265 ExprVector ArgExprs(Actions);
266 bool ArgExprsOk = true;
267
268 // now parse the list of expressions
269 while (1) {
270 ExprResult ArgExpr(ParseAssignmentExpression());
271 if (ArgExpr.isInvalid()) {
272 ArgExprsOk = false;
273 SkipUntil(tok::r_paren);
274 break;
275 } else {
276 ArgExprs.push_back(ArgExpr.release());
277 }
278 if (Tok.isNot(tok::comma))
279 break;
280 ConsumeToken(); // Eat the comma, move to the next argument
281 }
282 // Match the ')'.
283 if (ArgExprsOk && Tok.is(tok::r_paren)) {
284 ConsumeParen(); // ignore the right paren loc for now
285 Attrs.addNew(AttrName, AttrNameLoc, 0,
286 AttrNameLoc, 0, SourceLocation(),
287 ArgExprs.take(), ArgExprs.size());
288 }
289 break;
290 }
291 }
292}
293
294
Eli Friedman06de2b52009-06-08 07:21:15 +0000295/// ParseMicrosoftDeclSpec - Parse an __declspec construct
296///
297/// [MS] decl-specifier:
298/// __declspec ( extended-decl-modifier-seq )
299///
300/// [MS] extended-decl-modifier-seq:
301/// extended-decl-modifier[opt]
302/// extended-decl-modifier extended-decl-modifier-seq
303
John McCall53fa7142010-12-24 02:08:15 +0000304void Parser::ParseMicrosoftDeclSpec(ParsedAttributes &attrs) {
Steve Naroff3a9b7e02008-12-24 20:59:21 +0000305 assert(Tok.is(tok::kw___declspec) && "Not a declspec!");
Eli Friedman06de2b52009-06-08 07:21:15 +0000306
Steve Naroff3a9b7e02008-12-24 20:59:21 +0000307 ConsumeToken();
Eli Friedman06de2b52009-06-08 07:21:15 +0000308 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
309 "declspec")) {
310 SkipUntil(tok::r_paren, true); // skip until ) or ;
John McCall53fa7142010-12-24 02:08:15 +0000311 return;
Eli Friedman06de2b52009-06-08 07:21:15 +0000312 }
Francois Pichetdcf88932011-05-07 19:04:49 +0000313
Eli Friedman53339e02009-06-08 23:27:34 +0000314 while (Tok.getIdentifierInfo()) {
Eli Friedman06de2b52009-06-08 07:21:15 +0000315 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
316 SourceLocation AttrNameLoc = ConsumeToken();
Francois Pichetdcf88932011-05-07 19:04:49 +0000317
318 // FIXME: Remove this when we have proper __declspec(property()) support.
319 // Just skip everything inside property().
320 if (AttrName->getName() == "property") {
321 ConsumeParen();
322 SkipUntil(tok::r_paren);
323 }
Eli Friedman06de2b52009-06-08 07:21:15 +0000324 if (Tok.is(tok::l_paren)) {
325 ConsumeParen();
326 // FIXME: This doesn't parse __declspec(property(get=get_func_name))
327 // correctly.
John McCalldadc5752010-08-24 06:29:42 +0000328 ExprResult ArgExpr(ParseAssignmentExpression());
Eli Friedman06de2b52009-06-08 07:21:15 +0000329 if (!ArgExpr.isInvalid()) {
John McCall37ad5512010-08-23 06:44:23 +0000330 Expr *ExprList = ArgExpr.take();
John McCall084e83d2011-03-24 11:26:52 +0000331 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
332 SourceLocation(), &ExprList, 1, true);
Eli Friedman06de2b52009-06-08 07:21:15 +0000333 }
334 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
335 SkipUntil(tok::r_paren, false);
336 } else {
John McCall084e83d2011-03-24 11:26:52 +0000337 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc,
338 0, SourceLocation(), 0, 0, true);
Eli Friedman06de2b52009-06-08 07:21:15 +0000339 }
340 }
341 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
342 SkipUntil(tok::r_paren, false);
John McCall53fa7142010-12-24 02:08:15 +0000343 return;
Eli Friedman53339e02009-06-08 23:27:34 +0000344}
345
John McCall53fa7142010-12-24 02:08:15 +0000346void Parser::ParseMicrosoftTypeAttributes(ParsedAttributes &attrs) {
Eli Friedman53339e02009-06-08 23:27:34 +0000347 // Treat these like attributes
348 // FIXME: Allow Sema to distinguish between these and real attributes!
349 while (Tok.is(tok::kw___fastcall) || Tok.is(tok::kw___stdcall) ||
Douglas Gregora941dca2010-05-18 16:57:00 +0000350 Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___cdecl) ||
Francois Pichet17ed0202011-08-18 09:59:55 +0000351 Tok.is(tok::kw___ptr64) || Tok.is(tok::kw___w64) ||
Francois Pichetf2fb4112011-08-25 00:36:46 +0000352 Tok.is(tok::kw___ptr32) ||
Francois Pichet17ed0202011-08-18 09:59:55 +0000353 Tok.is(tok::kw___unaligned)) {
Eli Friedman53339e02009-06-08 23:27:34 +0000354 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
355 SourceLocation AttrNameLoc = ConsumeToken();
Francois Pichetf2fb4112011-08-25 00:36:46 +0000356 if (Tok.is(tok::kw___ptr64) || Tok.is(tok::kw___w64) ||
357 Tok.is(tok::kw___ptr32))
Eli Friedman53339e02009-06-08 23:27:34 +0000358 // FIXME: Support these properly!
359 continue;
John McCall084e83d2011-03-24 11:26:52 +0000360 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
361 SourceLocation(), 0, 0, true);
Eli Friedman53339e02009-06-08 23:27:34 +0000362 }
Steve Naroff3a9b7e02008-12-24 20:59:21 +0000363}
364
John McCall53fa7142010-12-24 02:08:15 +0000365void Parser::ParseBorlandTypeAttributes(ParsedAttributes &attrs) {
Dawn Perchik335e16b2010-09-03 01:29:35 +0000366 // Treat these like attributes
367 while (Tok.is(tok::kw___pascal)) {
368 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
369 SourceLocation AttrNameLoc = ConsumeToken();
John McCall084e83d2011-03-24 11:26:52 +0000370 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
371 SourceLocation(), 0, 0, true);
Dawn Perchik335e16b2010-09-03 01:29:35 +0000372 }
John McCall53fa7142010-12-24 02:08:15 +0000373}
374
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +0000375void Parser::ParseOpenCLAttributes(ParsedAttributes &attrs) {
376 // Treat these like attributes
377 while (Tok.is(tok::kw___kernel)) {
378 SourceLocation AttrNameLoc = ConsumeToken();
John McCall084e83d2011-03-24 11:26:52 +0000379 attrs.addNew(PP.getIdentifierInfo("opencl_kernel_function"),
380 AttrNameLoc, 0, AttrNameLoc, 0,
381 SourceLocation(), 0, 0, false);
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +0000382 }
383}
384
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000385void Parser::ParseOpenCLQualifiers(DeclSpec &DS) {
386 SourceLocation Loc = Tok.getLocation();
387 switch(Tok.getKind()) {
388 // OpenCL qualifiers:
389 case tok::kw___private:
390 case tok::kw_private:
John McCall084e83d2011-03-24 11:26:52 +0000391 DS.getAttributes().addNewInteger(
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000392 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000393 PP.getIdentifierInfo("address_space"), Loc, 0);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000394 break;
395
396 case tok::kw___global:
John McCall084e83d2011-03-24 11:26:52 +0000397 DS.getAttributes().addNewInteger(
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000398 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000399 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_global);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000400 break;
401
402 case tok::kw___local:
John McCall084e83d2011-03-24 11:26:52 +0000403 DS.getAttributes().addNewInteger(
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000404 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000405 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_local);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000406 break;
407
408 case tok::kw___constant:
John McCall084e83d2011-03-24 11:26:52 +0000409 DS.getAttributes().addNewInteger(
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000410 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000411 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_constant);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000412 break;
413
414 case tok::kw___read_only:
John McCall084e83d2011-03-24 11:26:52 +0000415 DS.getAttributes().addNewInteger(
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000416 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000417 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_read_only);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000418 break;
419
420 case tok::kw___write_only:
John McCall084e83d2011-03-24 11:26:52 +0000421 DS.getAttributes().addNewInteger(
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000422 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000423 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_write_only);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000424 break;
425
426 case tok::kw___read_write:
John McCall084e83d2011-03-24 11:26:52 +0000427 DS.getAttributes().addNewInteger(
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000428 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000429 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_read_write);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000430 break;
431 default: break;
432 }
433}
434
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000435/// \brief Parse a version number.
436///
437/// version:
438/// simple-integer
439/// simple-integer ',' simple-integer
440/// simple-integer ',' simple-integer ',' simple-integer
441VersionTuple Parser::ParseVersionTuple(SourceRange &Range) {
442 Range = Tok.getLocation();
443
444 if (!Tok.is(tok::numeric_constant)) {
445 Diag(Tok, diag::err_expected_version);
446 SkipUntil(tok::comma, tok::r_paren, true, true, true);
447 return VersionTuple();
448 }
449
450 // Parse the major (and possibly minor and subminor) versions, which
451 // are stored in the numeric constant. We utilize a quirk of the
452 // lexer, which is that it handles something like 1.2.3 as a single
453 // numeric constant, rather than two separate tokens.
454 llvm::SmallString<512> Buffer;
455 Buffer.resize(Tok.getLength()+1);
456 const char *ThisTokBegin = &Buffer[0];
457
458 // Get the spelling of the token, which eliminates trigraphs, etc.
459 bool Invalid = false;
460 unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin, &Invalid);
461 if (Invalid)
462 return VersionTuple();
463
464 // Parse the major version.
465 unsigned AfterMajor = 0;
466 unsigned Major = 0;
467 while (AfterMajor < ActualLength && isdigit(ThisTokBegin[AfterMajor])) {
468 Major = Major * 10 + ThisTokBegin[AfterMajor] - '0';
469 ++AfterMajor;
470 }
471
472 if (AfterMajor == 0) {
473 Diag(Tok, diag::err_expected_version);
474 SkipUntil(tok::comma, tok::r_paren, true, true, true);
475 return VersionTuple();
476 }
477
478 if (AfterMajor == ActualLength) {
479 ConsumeToken();
480
481 // We only had a single version component.
482 if (Major == 0) {
483 Diag(Tok, diag::err_zero_version);
484 return VersionTuple();
485 }
486
487 return VersionTuple(Major);
488 }
489
490 if (ThisTokBegin[AfterMajor] != '.' || (AfterMajor + 1 == ActualLength)) {
491 Diag(Tok, diag::err_expected_version);
492 SkipUntil(tok::comma, tok::r_paren, true, true, true);
493 return VersionTuple();
494 }
495
496 // Parse the minor version.
497 unsigned AfterMinor = AfterMajor + 1;
498 unsigned Minor = 0;
499 while (AfterMinor < ActualLength && isdigit(ThisTokBegin[AfterMinor])) {
500 Minor = Minor * 10 + ThisTokBegin[AfterMinor] - '0';
501 ++AfterMinor;
502 }
503
504 if (AfterMinor == ActualLength) {
505 ConsumeToken();
506
507 // We had major.minor.
508 if (Major == 0 && Minor == 0) {
509 Diag(Tok, diag::err_zero_version);
510 return VersionTuple();
511 }
512
513 return VersionTuple(Major, Minor);
514 }
515
516 // If what follows is not a '.', we have a problem.
517 if (ThisTokBegin[AfterMinor] != '.') {
518 Diag(Tok, diag::err_expected_version);
519 SkipUntil(tok::comma, tok::r_paren, true, true, true);
520 return VersionTuple();
521 }
522
523 // Parse the subminor version.
524 unsigned AfterSubminor = AfterMinor + 1;
525 unsigned Subminor = 0;
526 while (AfterSubminor < ActualLength && isdigit(ThisTokBegin[AfterSubminor])) {
527 Subminor = Subminor * 10 + ThisTokBegin[AfterSubminor] - '0';
528 ++AfterSubminor;
529 }
530
531 if (AfterSubminor != ActualLength) {
532 Diag(Tok, diag::err_expected_version);
533 SkipUntil(tok::comma, tok::r_paren, true, true, true);
534 return VersionTuple();
535 }
536 ConsumeToken();
537 return VersionTuple(Major, Minor, Subminor);
538}
539
540/// \brief Parse the contents of the "availability" attribute.
541///
542/// availability-attribute:
543/// 'availability' '(' platform ',' version-arg-list ')'
544///
545/// platform:
546/// identifier
547///
548/// version-arg-list:
549/// version-arg
550/// version-arg ',' version-arg-list
551///
552/// version-arg:
553/// 'introduced' '=' version
554/// 'deprecated' '=' version
555/// 'removed' = version
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000556/// 'unavailable'
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000557void Parser::ParseAvailabilityAttribute(IdentifierInfo &Availability,
558 SourceLocation AvailabilityLoc,
559 ParsedAttributes &attrs,
560 SourceLocation *endLoc) {
561 SourceLocation PlatformLoc;
562 IdentifierInfo *Platform = 0;
563
564 enum { Introduced, Deprecated, Obsoleted, Unknown };
565 AvailabilityChange Changes[Unknown];
566
567 // Opening '('.
568 SourceLocation LParenLoc;
569 if (!Tok.is(tok::l_paren)) {
570 Diag(Tok, diag::err_expected_lparen);
571 return;
572 }
573 LParenLoc = ConsumeParen();
574
575 // Parse the platform name,
576 if (Tok.isNot(tok::identifier)) {
577 Diag(Tok, diag::err_availability_expected_platform);
578 SkipUntil(tok::r_paren);
579 return;
580 }
581 Platform = Tok.getIdentifierInfo();
582 PlatformLoc = ConsumeToken();
583
584 // Parse the ',' following the platform name.
585 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "", tok::r_paren))
586 return;
587
588 // If we haven't grabbed the pointers for the identifiers
589 // "introduced", "deprecated", and "obsoleted", do so now.
590 if (!Ident_introduced) {
591 Ident_introduced = PP.getIdentifierInfo("introduced");
592 Ident_deprecated = PP.getIdentifierInfo("deprecated");
593 Ident_obsoleted = PP.getIdentifierInfo("obsoleted");
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000594 Ident_unavailable = PP.getIdentifierInfo("unavailable");
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000595 }
596
597 // Parse the set of introductions/deprecations/removals.
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000598 SourceLocation UnavailableLoc;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000599 do {
600 if (Tok.isNot(tok::identifier)) {
601 Diag(Tok, diag::err_availability_expected_change);
602 SkipUntil(tok::r_paren);
603 return;
604 }
605 IdentifierInfo *Keyword = Tok.getIdentifierInfo();
606 SourceLocation KeywordLoc = ConsumeToken();
607
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000608 if (Keyword == Ident_unavailable) {
609 if (UnavailableLoc.isValid()) {
610 Diag(KeywordLoc, diag::err_availability_redundant)
611 << Keyword << SourceRange(UnavailableLoc);
612 }
613 UnavailableLoc = KeywordLoc;
614
615 if (Tok.isNot(tok::comma))
616 break;
617
618 ConsumeToken();
619 continue;
620 }
621
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000622 if (Tok.isNot(tok::equal)) {
623 Diag(Tok, diag::err_expected_equal_after)
624 << Keyword;
625 SkipUntil(tok::r_paren);
626 return;
627 }
628 ConsumeToken();
629
630 SourceRange VersionRange;
631 VersionTuple Version = ParseVersionTuple(VersionRange);
632
633 if (Version.empty()) {
634 SkipUntil(tok::r_paren);
635 return;
636 }
637
638 unsigned Index;
639 if (Keyword == Ident_introduced)
640 Index = Introduced;
641 else if (Keyword == Ident_deprecated)
642 Index = Deprecated;
643 else if (Keyword == Ident_obsoleted)
644 Index = Obsoleted;
645 else
646 Index = Unknown;
647
648 if (Index < Unknown) {
649 if (!Changes[Index].KeywordLoc.isInvalid()) {
650 Diag(KeywordLoc, diag::err_availability_redundant)
651 << Keyword
652 << SourceRange(Changes[Index].KeywordLoc,
653 Changes[Index].VersionRange.getEnd());
654 }
655
656 Changes[Index].KeywordLoc = KeywordLoc;
657 Changes[Index].Version = Version;
658 Changes[Index].VersionRange = VersionRange;
659 } else {
660 Diag(KeywordLoc, diag::err_availability_unknown_change)
661 << Keyword << VersionRange;
662 }
663
664 if (Tok.isNot(tok::comma))
665 break;
666
667 ConsumeToken();
668 } while (true);
669
670 // Closing ')'.
671 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
672 if (RParenLoc.isInvalid())
673 return;
674
675 if (endLoc)
676 *endLoc = RParenLoc;
677
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000678 // The 'unavailable' availability cannot be combined with any other
679 // availability changes. Make sure that hasn't happened.
680 if (UnavailableLoc.isValid()) {
681 bool Complained = false;
682 for (unsigned Index = Introduced; Index != Unknown; ++Index) {
683 if (Changes[Index].KeywordLoc.isValid()) {
684 if (!Complained) {
685 Diag(UnavailableLoc, diag::warn_availability_and_unavailable)
686 << SourceRange(Changes[Index].KeywordLoc,
687 Changes[Index].VersionRange.getEnd());
688 Complained = true;
689 }
690
691 // Clear out the availability.
692 Changes[Index] = AvailabilityChange();
693 }
694 }
695 }
696
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000697 // Record this attribute
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000698 attrs.addNew(&Availability, AvailabilityLoc,
John McCall084e83d2011-03-24 11:26:52 +0000699 0, SourceLocation(),
700 Platform, PlatformLoc,
701 Changes[Introduced],
702 Changes[Deprecated],
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000703 Changes[Obsoleted],
704 UnavailableLoc, false, false);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000705}
706
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000707
708// Late Parsed Attributes:
709// See other examples of late parsing in lib/Parse/ParseCXXInlineMethods
710
711void Parser::LateParsedDeclaration::ParseLexedAttributes() {}
712
713void Parser::LateParsedClass::ParseLexedAttributes() {
714 Self->ParseLexedAttributes(*Class);
715}
716
717void Parser::LateParsedAttribute::ParseLexedAttributes() {
718 Self->ParseLexedAttribute(*this);
719}
720
721/// Wrapper class which calls ParseLexedAttribute, after setting up the
722/// scope appropriately.
723void Parser::ParseLexedAttributes(ParsingClass &Class) {
724 // Deal with templates
725 // FIXME: Test cases to make sure this does the right thing for templates.
726 bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope;
727 ParseScope ClassTemplateScope(this, Scope::TemplateParamScope,
728 HasTemplateScope);
729 if (HasTemplateScope)
730 Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate);
731
732 // Set or update the scope flags to include Scope::ThisScope.
733 bool AlreadyHasClassScope = Class.TopLevelClass;
734 unsigned ScopeFlags = Scope::ClassScope|Scope::DeclScope|Scope::ThisScope;
735 ParseScope ClassScope(this, ScopeFlags, !AlreadyHasClassScope);
736 ParseScopeFlags ClassScopeFlags(this, ScopeFlags, AlreadyHasClassScope);
737
738 for (unsigned i = 0, ni = Class.LateParsedDeclarations.size(); i < ni; ++i) {
739 Class.LateParsedDeclarations[i]->ParseLexedAttributes();
740 }
741}
742
743/// \brief Finish parsing an attribute for which parsing was delayed.
744/// This will be called at the end of parsing a class declaration
745/// for each LateParsedAttribute. We consume the saved tokens and
746/// create an attribute with the arguments filled in. We add this
747/// to the Attribute list for the decl.
748void Parser::ParseLexedAttribute(LateParsedAttribute &LA) {
749 // Save the current token position.
750 SourceLocation OrigLoc = Tok.getLocation();
751
752 // Append the current token at the end of the new token stream so that it
753 // doesn't get lost.
754 LA.Toks.push_back(Tok);
755 PP.EnterTokenStream(LA.Toks.data(), LA.Toks.size(), true, false);
756 // Consume the previously pushed token.
757 ConsumeAnyToken();
758
759 ParsedAttributes Attrs(AttrFactory);
760 SourceLocation endLoc;
761
Caitlin Sadowski990d5712011-09-08 17:42:31 +0000762 // If the Decl is templatized, add template parameters to scope.
763 bool HasTemplateScope = LA.D && LA.D->isTemplateDecl();
764 ParseScope TempScope(this, Scope::TemplateParamScope, HasTemplateScope);
765 if (HasTemplateScope)
766 Actions.ActOnReenterTemplateScope(Actions.CurScope, LA.D);
767
768 // If the Decl is on a function, add function parameters to the scope.
769 bool HasFunctionScope = LA.D && LA.D->isFunctionOrFunctionTemplate();
770 ParseScope FnScope(this, Scope::FnScope|Scope::DeclScope, HasFunctionScope);
771 if (HasFunctionScope)
772 Actions.ActOnReenterFunctionContext(Actions.CurScope, LA.D);
773
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000774 ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, &endLoc);
775
Caitlin Sadowski990d5712011-09-08 17:42:31 +0000776 if (HasFunctionScope) {
777 Actions.ActOnExitFunctionContext();
778 FnScope.Exit(); // Pop scope, and remove Decls from IdResolver
779 }
780 if (HasTemplateScope) {
781 TempScope.Exit();
782 }
783
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000784 // Late parsed attributes must be attached to Decls by hand. If the
785 // LA.D is not set, then this was not done properly.
786 assert(LA.D && "No decl attached to late parsed attribute");
787 Actions.ActOnFinishDelayedAttribute(getCurScope(), LA.D, Attrs);
788
789 if (Tok.getLocation() != OrigLoc) {
790 // Due to a parsing error, we either went over the cached tokens or
791 // there are still cached tokens left, so we skip the leftover tokens.
792 // Since this is an uncommon situation that should be avoided, use the
793 // expensive isBeforeInTranslationUnit call.
794 if (PP.getSourceManager().isBeforeInTranslationUnit(Tok.getLocation(),
795 OrigLoc))
796 while (Tok.getLocation() != OrigLoc && Tok.isNot(tok::eof))
797 ConsumeAnyToken();
798 }
799}
800
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000801/// \brief Wrapper around a case statement checking if AttrName is
802/// one of the thread safety attributes
803bool Parser::IsThreadSafetyAttribute(llvm::StringRef AttrName){
804 return llvm::StringSwitch<bool>(AttrName)
805 .Case("guarded_by", true)
806 .Case("guarded_var", true)
807 .Case("pt_guarded_by", true)
808 .Case("pt_guarded_var", true)
809 .Case("lockable", true)
810 .Case("scoped_lockable", true)
811 .Case("no_thread_safety_analysis", true)
812 .Case("acquired_after", true)
813 .Case("acquired_before", true)
814 .Case("exclusive_lock_function", true)
815 .Case("shared_lock_function", true)
816 .Case("exclusive_trylock_function", true)
817 .Case("shared_trylock_function", true)
818 .Case("unlock_function", true)
819 .Case("lock_returned", true)
820 .Case("locks_excluded", true)
821 .Case("exclusive_locks_required", true)
822 .Case("shared_locks_required", true)
823 .Default(false);
824}
825
826/// \brief Parse the contents of thread safety attributes. These
827/// should always be parsed as an expression list.
828///
829/// We need to special case the parsing due to the fact that if the first token
830/// of the first argument is an identifier, the main parse loop will store
831/// that token as a "parameter" and the rest of
832/// the arguments will be added to a list of "arguments". However,
833/// subsequent tokens in the first argument are lost. We instead parse each
834/// argument as an expression and add all arguments to the list of "arguments".
835/// In future, we will take advantage of this special case to also
836/// deal with some argument scoping issues here (for example, referring to a
837/// function parameter in the attribute on that function).
838void Parser::ParseThreadSafetyAttribute(IdentifierInfo &AttrName,
839 SourceLocation AttrNameLoc,
840 ParsedAttributes &Attrs,
841 SourceLocation *EndLoc) {
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000842 assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('");
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000843
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000844 SourceLocation LeftParenLoc = Tok.getLocation();
845 ConsumeParen();
846
847 ExprVector ArgExprs(Actions);
848 bool ArgExprsOk = true;
849
850 // now parse the list of expressions
851 while (1) {
852 ExprResult ArgExpr(ParseAssignmentExpression());
853 if (ArgExpr.isInvalid()) {
854 ArgExprsOk = false;
855 MatchRHSPunctuation(tok::r_paren, LeftParenLoc);
856 break;
857 } else {
858 ArgExprs.push_back(ArgExpr.release());
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000859 }
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000860 if (Tok.isNot(tok::comma))
861 break;
862 ConsumeToken(); // Eat the comma, move to the next argument
863 }
864 // Match the ')'.
865 if (ArgExprsOk && Tok.is(tok::r_paren)) {
866 if (EndLoc)
867 *EndLoc = Tok.getLocation();
868 ConsumeParen();
869 Attrs.addNew(&AttrName, AttrNameLoc, 0, AttrNameLoc, 0, SourceLocation(),
870 ArgExprs.take(), ArgExprs.size());
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000871 }
872}
873
John McCall53fa7142010-12-24 02:08:15 +0000874void Parser::DiagnoseProhibitedAttributes(ParsedAttributesWithRange &attrs) {
875 Diag(attrs.Range.getBegin(), diag::err_attributes_not_allowed)
876 << attrs.Range;
Dawn Perchik335e16b2010-09-03 01:29:35 +0000877}
878
Chris Lattner53361ac2006-08-10 05:19:57 +0000879/// ParseDeclaration - Parse a full 'declaration', which consists of
880/// declaration-specifiers, some number of declarators, and a semicolon.
Chris Lattner49836b42009-04-02 04:16:50 +0000881/// 'Context' should be a Declarator::TheContext value. This returns the
882/// location of the semicolon in DeclEnd.
Chris Lattnera5235172007-08-25 06:57:03 +0000883///
884/// declaration: [C99 6.7]
885/// block-declaration ->
886/// simple-declaration
887/// others [FIXME]
Douglas Gregoreb31f392008-12-01 23:54:00 +0000888/// [C++] template-declaration
Chris Lattnera5235172007-08-25 06:57:03 +0000889/// [C++] namespace-definition
Douglas Gregord7c4d982008-12-30 03:27:21 +0000890/// [C++] using-directive
Douglas Gregor77b50e12009-06-22 23:06:13 +0000891/// [C++] using-declaration
Peter Collingbourne3d9cbdc2011-04-15 00:35:57 +0000892/// [C++0x/C1X] static_assert-declaration
Chris Lattnera5235172007-08-25 06:57:03 +0000893/// others... [FIXME]
894///
Fariborz Jahanian1db5c942010-09-28 20:42:35 +0000895Parser::DeclGroupPtrTy Parser::ParseDeclaration(StmtVector &Stmts,
896 unsigned Context,
Alexis Hunt96d5c762009-11-21 08:43:09 +0000897 SourceLocation &DeclEnd,
John McCall53fa7142010-12-24 02:08:15 +0000898 ParsedAttributesWithRange &attrs) {
Argyrios Kyrtzidis355094e2010-06-17 10:52:18 +0000899 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Fariborz Jahanian59b75282011-08-30 17:10:52 +0000900 // Must temporarily exit the objective-c container scope for
901 // parsing c none objective-c decls.
902 ObjCDeclContextSwitch ObjCDC(*this);
Argyrios Kyrtzidis355094e2010-06-17 10:52:18 +0000903
John McCall48871652010-08-21 09:40:31 +0000904 Decl *SingleDecl = 0;
Richard Smithcd1c0552011-07-01 19:46:12 +0000905 Decl *OwnedType = 0;
Chris Lattnera5235172007-08-25 06:57:03 +0000906 switch (Tok.getKind()) {
Douglas Gregoreb31f392008-12-01 23:54:00 +0000907 case tok::kw_template:
Douglas Gregor23996282009-05-12 21:31:51 +0000908 case tok::kw_export:
John McCall53fa7142010-12-24 02:08:15 +0000909 ProhibitAttributes(attrs);
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000910 SingleDecl = ParseDeclarationStartingWithTemplate(Context, DeclEnd);
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000911 break;
Sebastian Redl67667942010-08-27 23:12:46 +0000912 case tok::kw_inline:
Sebastian Redl5a5f2c72010-08-31 00:36:45 +0000913 // Could be the start of an inline namespace. Allowed as an ext in C++03.
914 if (getLang().CPlusPlus && NextToken().is(tok::kw_namespace)) {
John McCall53fa7142010-12-24 02:08:15 +0000915 ProhibitAttributes(attrs);
Sebastian Redl67667942010-08-27 23:12:46 +0000916 SourceLocation InlineLoc = ConsumeToken();
917 SingleDecl = ParseNamespace(Context, DeclEnd, InlineLoc);
918 break;
919 }
John McCall53fa7142010-12-24 02:08:15 +0000920 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs,
Fariborz Jahanian1db5c942010-09-28 20:42:35 +0000921 true);
Chris Lattnera5235172007-08-25 06:57:03 +0000922 case tok::kw_namespace:
John McCall53fa7142010-12-24 02:08:15 +0000923 ProhibitAttributes(attrs);
Chris Lattner49836b42009-04-02 04:16:50 +0000924 SingleDecl = ParseNamespace(Context, DeclEnd);
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000925 break;
Douglas Gregord7c4d982008-12-30 03:27:21 +0000926 case tok::kw_using:
John McCall9b72f892010-11-10 02:40:36 +0000927 SingleDecl = ParseUsingDirectiveOrDeclaration(Context, ParsedTemplateInfo(),
Richard Smithcd1c0552011-07-01 19:46:12 +0000928 DeclEnd, attrs, &OwnedType);
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000929 break;
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000930 case tok::kw_static_assert:
Peter Collingbourne3d9cbdc2011-04-15 00:35:57 +0000931 case tok::kw__Static_assert:
John McCall53fa7142010-12-24 02:08:15 +0000932 ProhibitAttributes(attrs);
Chris Lattner49836b42009-04-02 04:16:50 +0000933 SingleDecl = ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000934 break;
Chris Lattnera5235172007-08-25 06:57:03 +0000935 default:
John McCall53fa7142010-12-24 02:08:15 +0000936 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs, true);
Chris Lattnera5235172007-08-25 06:57:03 +0000937 }
Alexis Hunt96d5c762009-11-21 08:43:09 +0000938
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000939 // This routine returns a DeclGroup, if the thing we parsed only contains a
Richard Smithcd1c0552011-07-01 19:46:12 +0000940 // single decl, convert it now. Alias declarations can also declare a type;
941 // include that too if it is present.
942 return Actions.ConvertDeclToDeclGroup(SingleDecl, OwnedType);
Chris Lattnera5235172007-08-25 06:57:03 +0000943}
944
945/// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl]
946/// declaration-specifiers init-declarator-list[opt] ';'
947///[C90/C++]init-declarator-list ';' [TODO]
948/// [OMP] threadprivate-directive [TODO]
Chris Lattner32dc41c2009-03-29 17:27:48 +0000949///
Richard Smith02e85f32011-04-14 22:09:26 +0000950/// for-range-declaration: [C++0x 6.5p1: stmt.ranged]
951/// attribute-specifier-seq[opt] type-specifier-seq declarator
952///
Chris Lattner32dc41c2009-03-29 17:27:48 +0000953/// If RequireSemi is false, this does not check for a ';' at the end of the
Chris Lattner005fc1b2010-04-05 18:18:31 +0000954/// declaration. If it is true, it checks for and eats it.
Richard Smith02e85f32011-04-14 22:09:26 +0000955///
956/// If FRI is non-null, we might be parsing a for-range-declaration instead
957/// of a simple-declaration. If we find that we are, we also parse the
958/// for-range-initializer, and place it here.
Fariborz Jahanian1db5c942010-09-28 20:42:35 +0000959Parser::DeclGroupPtrTy Parser::ParseSimpleDeclaration(StmtVector &Stmts,
960 unsigned Context,
Alexis Hunt96d5c762009-11-21 08:43:09 +0000961 SourceLocation &DeclEnd,
John McCall53fa7142010-12-24 02:08:15 +0000962 ParsedAttributes &attrs,
Richard Smith02e85f32011-04-14 22:09:26 +0000963 bool RequireSemi,
964 ForRangeInit *FRI) {
Chris Lattner53361ac2006-08-10 05:19:57 +0000965 // Parse the common declaration-specifiers piece.
John McCall28a6aea2009-11-04 02:18:39 +0000966 ParsingDeclSpec DS(*this);
John McCall53fa7142010-12-24 02:08:15 +0000967 DS.takeAttributesFrom(attrs);
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000968
Douglas Gregor9de54ea2010-01-13 17:31:36 +0000969 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS_none,
Richard Smith30482bc2011-02-20 03:19:35 +0000970 getDeclSpecContextFromDeclaratorContext(Context));
Fariborz Jahanian1db5c942010-09-28 20:42:35 +0000971 StmtResult R = Actions.ActOnVlaStmt(DS);
972 if (R.isUsable())
973 Stmts.push_back(R.release());
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000974
Chris Lattner0e894622006-08-13 19:58:17 +0000975 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
976 // declaration-specifiers init-declarator-list[opt] ';'
Chris Lattner76c72282007-10-09 17:33:22 +0000977 if (Tok.is(tok::semi)) {
Chris Lattner005fc1b2010-04-05 18:18:31 +0000978 if (RequireSemi) ConsumeToken();
John McCall48871652010-08-21 09:40:31 +0000979 Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none,
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000980 DS);
John McCall28a6aea2009-11-04 02:18:39 +0000981 DS.complete(TheDecl);
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000982 return Actions.ConvertDeclToDeclGroup(TheDecl);
Chris Lattner0e894622006-08-13 19:58:17 +0000983 }
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000984
985 return ParseDeclGroup(DS, Context, /*FunctionDefs=*/ false, &DeclEnd, FRI);
John McCalld5a36322009-11-03 19:26:08 +0000986}
Mike Stump11289f42009-09-09 15:08:12 +0000987
John McCalld5a36322009-11-03 19:26:08 +0000988/// ParseDeclGroup - Having concluded that this is either a function
989/// definition or a group of object declarations, actually parse the
990/// result.
John McCall28a6aea2009-11-04 02:18:39 +0000991Parser::DeclGroupPtrTy Parser::ParseDeclGroup(ParsingDeclSpec &DS,
992 unsigned Context,
John McCalld5a36322009-11-03 19:26:08 +0000993 bool AllowFunctionDefinitions,
Richard Smith02e85f32011-04-14 22:09:26 +0000994 SourceLocation *DeclEnd,
995 ForRangeInit *FRI) {
John McCalld5a36322009-11-03 19:26:08 +0000996 // Parse the first declarator.
John McCall28a6aea2009-11-04 02:18:39 +0000997 ParsingDeclarator D(*this, DS, static_cast<Declarator::TheContext>(Context));
John McCalld5a36322009-11-03 19:26:08 +0000998 ParseDeclarator(D);
Chris Lattner32dc41c2009-03-29 17:27:48 +0000999
John McCalld5a36322009-11-03 19:26:08 +00001000 // Bail out if the first declarator didn't seem well-formed.
1001 if (!D.hasName() && !D.mayOmitIdentifier()) {
1002 // Skip until ; or }.
1003 SkipUntil(tok::r_brace, true, true);
1004 if (Tok.is(tok::semi))
1005 ConsumeToken();
1006 return DeclGroupPtrTy();
Chris Lattnerefb0f112009-03-29 17:18:04 +00001007 }
Mike Stump11289f42009-09-09 15:08:12 +00001008
Chris Lattnerdbb1e932010-07-11 22:24:20 +00001009 // Check to see if we have a function *definition* which must have a body.
1010 if (AllowFunctionDefinitions && D.isFunctionDeclarator() &&
1011 // Look at the next token to make sure that this isn't a function
1012 // declaration. We have to check this because __attribute__ might be the
1013 // start of a function definition in GCC-extended K&R C.
1014 !isDeclarationAfterDeclarator()) {
1015
Chris Lattner13901342010-07-11 22:42:07 +00001016 if (isStartOfFunctionDefinition(D)) {
John McCalld5a36322009-11-03 19:26:08 +00001017 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
1018 Diag(Tok, diag::err_function_declared_typedef);
1019
1020 // Recover by treating the 'typedef' as spurious.
1021 DS.ClearStorageClassSpecs();
1022 }
1023
John McCall48871652010-08-21 09:40:31 +00001024 Decl *TheDecl = ParseFunctionDefinition(D);
John McCalld5a36322009-11-03 19:26:08 +00001025 return Actions.ConvertDeclToDeclGroup(TheDecl);
Chris Lattner13901342010-07-11 22:42:07 +00001026 }
1027
1028 if (isDeclarationSpecifier()) {
1029 // If there is an invalid declaration specifier right after the function
1030 // prototype, then we must be in a missing semicolon case where this isn't
1031 // actually a body. Just fall through into the code that handles it as a
1032 // prototype, and let the top-level code handle the erroneous declspec
1033 // where it would otherwise expect a comma or semicolon.
John McCalld5a36322009-11-03 19:26:08 +00001034 } else {
1035 Diag(Tok, diag::err_expected_fn_body);
1036 SkipUntil(tok::semi);
1037 return DeclGroupPtrTy();
1038 }
1039 }
1040
Richard Smith02e85f32011-04-14 22:09:26 +00001041 if (ParseAttributesAfterDeclarator(D))
1042 return DeclGroupPtrTy();
1043
1044 // C++0x [stmt.iter]p1: Check if we have a for-range-declarator. If so, we
1045 // must parse and analyze the for-range-initializer before the declaration is
1046 // analyzed.
1047 if (FRI && Tok.is(tok::colon)) {
1048 FRI->ColonLoc = ConsumeToken();
Sebastian Redl3da34892011-06-05 12:23:16 +00001049 if (Tok.is(tok::l_brace))
1050 FRI->RangeExpr = ParseBraceInitializer();
1051 else
1052 FRI->RangeExpr = ParseExpression();
Richard Smith02e85f32011-04-14 22:09:26 +00001053 Decl *ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
1054 Actions.ActOnCXXForRangeDecl(ThisDecl);
1055 Actions.FinalizeDeclaration(ThisDecl);
1056 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS, &ThisDecl, 1);
1057 }
1058
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001059 SmallVector<Decl *, 8> DeclsInGroup;
Richard Smith02e85f32011-04-14 22:09:26 +00001060 Decl *FirstDecl = ParseDeclarationAfterDeclaratorAndAttributes(D);
John McCall28a6aea2009-11-04 02:18:39 +00001061 D.complete(FirstDecl);
John McCall48871652010-08-21 09:40:31 +00001062 if (FirstDecl)
John McCalld5a36322009-11-03 19:26:08 +00001063 DeclsInGroup.push_back(FirstDecl);
1064
1065 // If we don't have a comma, it is either the end of the list (a ';') or an
1066 // error, bail out.
1067 while (Tok.is(tok::comma)) {
1068 // Consume the comma.
Chris Lattnerefb0f112009-03-29 17:18:04 +00001069 ConsumeToken();
John McCalld5a36322009-11-03 19:26:08 +00001070
1071 // Parse the next declarator.
1072 D.clear();
1073
1074 // Accept attributes in an init-declarator. In the first declarator in a
1075 // declaration, these would be part of the declspec. In subsequent
1076 // declarators, they become part of the declarator itself, so that they
1077 // don't apply to declarators after *this* one. Examples:
1078 // short __attribute__((common)) var; -> declspec
1079 // short var __attribute__((common)); -> declarator
1080 // short x, __attribute__((common)) var; -> declarator
John McCall53fa7142010-12-24 02:08:15 +00001081 MaybeParseGNUAttributes(D);
John McCalld5a36322009-11-03 19:26:08 +00001082
1083 ParseDeclarator(D);
1084
John McCall48871652010-08-21 09:40:31 +00001085 Decl *ThisDecl = ParseDeclarationAfterDeclarator(D);
John McCall28a6aea2009-11-04 02:18:39 +00001086 D.complete(ThisDecl);
John McCall48871652010-08-21 09:40:31 +00001087 if (ThisDecl)
John McCalld5a36322009-11-03 19:26:08 +00001088 DeclsInGroup.push_back(ThisDecl);
1089 }
1090
1091 if (DeclEnd)
1092 *DeclEnd = Tok.getLocation();
1093
1094 if (Context != Declarator::ForContext &&
1095 ExpectAndConsume(tok::semi,
1096 Context == Declarator::FileContext
1097 ? diag::err_invalid_token_after_toplevel_declarator
1098 : diag::err_expected_semi_declaration)) {
Chris Lattner13901342010-07-11 22:42:07 +00001099 // Okay, there was no semicolon and one was expected. If we see a
1100 // declaration specifier, just assume it was missing and continue parsing.
1101 // Otherwise things are very confused and we skip to recover.
1102 if (!isDeclarationSpecifier()) {
1103 SkipUntil(tok::r_brace, true, true);
1104 if (Tok.is(tok::semi))
1105 ConsumeToken();
1106 }
John McCalld5a36322009-11-03 19:26:08 +00001107 }
1108
Douglas Gregor0be31a22010-07-02 17:43:08 +00001109 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS,
John McCalld5a36322009-11-03 19:26:08 +00001110 DeclsInGroup.data(),
1111 DeclsInGroup.size());
Chris Lattner53361ac2006-08-10 05:19:57 +00001112}
1113
Richard Smith02e85f32011-04-14 22:09:26 +00001114/// Parse an optional simple-asm-expr and attributes, and attach them to a
1115/// declarator. Returns true on an error.
1116bool Parser::ParseAttributesAfterDeclarator(Declarator &D) {
1117 // If a simple-asm-expr is present, parse it.
1118 if (Tok.is(tok::kw_asm)) {
1119 SourceLocation Loc;
1120 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
1121 if (AsmLabel.isInvalid()) {
1122 SkipUntil(tok::semi, true, true);
1123 return true;
1124 }
1125
1126 D.setAsmLabel(AsmLabel.release());
1127 D.SetRangeEnd(Loc);
1128 }
1129
1130 MaybeParseGNUAttributes(D);
1131 return false;
1132}
1133
Douglas Gregor23996282009-05-12 21:31:51 +00001134/// \brief Parse 'declaration' after parsing 'declaration-specifiers
1135/// declarator'. This method parses the remainder of the declaration
1136/// (including any attributes or initializer, among other things) and
1137/// finalizes the declaration.
Chris Lattnerf0f3baa2006-08-14 00:15:20 +00001138///
Chris Lattnerf0f3baa2006-08-14 00:15:20 +00001139/// init-declarator: [C99 6.7]
1140/// declarator
1141/// declarator '=' initializer
Chris Lattner6d7e6342006-08-15 03:41:14 +00001142/// [GNU] declarator simple-asm-expr[opt] attributes[opt]
1143/// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00001144/// [C++] declarator initializer[opt]
1145///
1146/// [C++] initializer:
1147/// [C++] '=' initializer-clause
1148/// [C++] '(' expression-list ')'
Sebastian Redlf769df52009-03-24 22:27:57 +00001149/// [C++0x] '=' 'default' [TODO]
1150/// [C++0x] '=' 'delete'
Sebastian Redl3da34892011-06-05 12:23:16 +00001151/// [C++0x] braced-init-list
Sebastian Redlf769df52009-03-24 22:27:57 +00001152///
1153/// According to the standard grammar, =default and =delete are function
1154/// definitions, but that definitely doesn't fit with the parser here.
Chris Lattnerf0f3baa2006-08-14 00:15:20 +00001155///
John McCall48871652010-08-21 09:40:31 +00001156Decl *Parser::ParseDeclarationAfterDeclarator(Declarator &D,
Douglas Gregorb52fabb2009-06-23 23:11:28 +00001157 const ParsedTemplateInfo &TemplateInfo) {
Richard Smith02e85f32011-04-14 22:09:26 +00001158 if (ParseAttributesAfterDeclarator(D))
1159 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00001160
Richard Smith02e85f32011-04-14 22:09:26 +00001161 return ParseDeclarationAfterDeclaratorAndAttributes(D, TemplateInfo);
1162}
Mike Stump11289f42009-09-09 15:08:12 +00001163
Richard Smith02e85f32011-04-14 22:09:26 +00001164Decl *Parser::ParseDeclarationAfterDeclaratorAndAttributes(Declarator &D,
1165 const ParsedTemplateInfo &TemplateInfo) {
Douglas Gregor23996282009-05-12 21:31:51 +00001166 // Inform the current actions module that we just parsed this declarator.
John McCall48871652010-08-21 09:40:31 +00001167 Decl *ThisDecl = 0;
Douglas Gregor450f00842009-09-25 18:43:00 +00001168 switch (TemplateInfo.Kind) {
1169 case ParsedTemplateInfo::NonTemplate:
Douglas Gregor0be31a22010-07-02 17:43:08 +00001170 ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
Douglas Gregor450f00842009-09-25 18:43:00 +00001171 break;
1172
1173 case ParsedTemplateInfo::Template:
1174 case ParsedTemplateInfo::ExplicitSpecialization:
Douglas Gregor0be31a22010-07-02 17:43:08 +00001175 ThisDecl = Actions.ActOnTemplateDeclarator(getCurScope(),
John McCallfaf5fb42010-08-26 23:41:50 +00001176 MultiTemplateParamsArg(Actions,
Douglas Gregorb52fabb2009-06-23 23:11:28 +00001177 TemplateInfo.TemplateParams->data(),
1178 TemplateInfo.TemplateParams->size()),
Douglas Gregor450f00842009-09-25 18:43:00 +00001179 D);
1180 break;
1181
1182 case ParsedTemplateInfo::ExplicitInstantiation: {
John McCall48871652010-08-21 09:40:31 +00001183 DeclResult ThisRes
Douglas Gregor0be31a22010-07-02 17:43:08 +00001184 = Actions.ActOnExplicitInstantiation(getCurScope(),
Douglas Gregor450f00842009-09-25 18:43:00 +00001185 TemplateInfo.ExternLoc,
1186 TemplateInfo.TemplateLoc,
1187 D);
1188 if (ThisRes.isInvalid()) {
1189 SkipUntil(tok::semi, true, true);
John McCall48871652010-08-21 09:40:31 +00001190 return 0;
Douglas Gregor450f00842009-09-25 18:43:00 +00001191 }
1192
1193 ThisDecl = ThisRes.get();
1194 break;
1195 }
1196 }
Mike Stump11289f42009-09-09 15:08:12 +00001197
Richard Smith30482bc2011-02-20 03:19:35 +00001198 bool TypeContainsAuto =
1199 D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto;
1200
Douglas Gregor23996282009-05-12 21:31:51 +00001201 // Parse declarator '=' initializer.
Argyrios Kyrtzidisb5c7c512010-10-08 02:39:23 +00001202 if (isTokenEqualOrMistypedEqualEqual(
1203 diag::err_invalid_equalequal_after_declarator)) {
Douglas Gregor23996282009-05-12 21:31:51 +00001204 ConsumeToken();
Anders Carlsson991285e2010-09-24 21:25:25 +00001205 if (Tok.is(tok::kw_delete)) {
Alexis Hunt5a7fa252011-05-12 06:15:49 +00001206 if (D.isFunctionDeclarator())
1207 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
1208 << 1 /* delete */;
1209 else
1210 Diag(ConsumeToken(), diag::err_deleted_non_function);
Alexis Hunt5dafebc2011-05-06 01:42:00 +00001211 } else if (Tok.is(tok::kw_default)) {
Alexis Hunt5a7fa252011-05-12 06:15:49 +00001212 if (D.isFunctionDeclarator())
1213 Diag(Tok, diag::err_default_delete_in_multiple_declaration)
1214 << 1 /* delete */;
1215 else
1216 Diag(ConsumeToken(), diag::err_default_special_members);
Douglas Gregor23996282009-05-12 21:31:51 +00001217 } else {
John McCall1f4ee7b2009-12-19 09:28:58 +00001218 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
1219 EnterScope(0);
Douglas Gregor0be31a22010-07-02 17:43:08 +00001220 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
John McCall1f4ee7b2009-12-19 09:28:58 +00001221 }
Argyrios Kyrtzidis3df19782009-06-17 22:50:06 +00001222
Douglas Gregor7aa6b222010-05-30 01:49:25 +00001223 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001224 Actions.CodeCompleteInitializer(getCurScope(), ThisDecl);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001225 cutOffParsing();
1226 return 0;
Douglas Gregor7aa6b222010-05-30 01:49:25 +00001227 }
1228
John McCalldadc5752010-08-24 06:29:42 +00001229 ExprResult Init(ParseInitializer());
Argyrios Kyrtzidis3df19782009-06-17 22:50:06 +00001230
John McCall1f4ee7b2009-12-19 09:28:58 +00001231 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001232 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
John McCall1f4ee7b2009-12-19 09:28:58 +00001233 ExitScope();
1234 }
Argyrios Kyrtzidis3df19782009-06-17 22:50:06 +00001235
Douglas Gregor23996282009-05-12 21:31:51 +00001236 if (Init.isInvalid()) {
Douglas Gregor604c3022010-03-01 18:27:54 +00001237 SkipUntil(tok::comma, true, true);
1238 Actions.ActOnInitializerError(ThisDecl);
1239 } else
Richard Smith30482bc2011-02-20 03:19:35 +00001240 Actions.AddInitializerToDecl(ThisDecl, Init.take(),
1241 /*DirectInit=*/false, TypeContainsAuto);
Douglas Gregor23996282009-05-12 21:31:51 +00001242 }
1243 } else if (Tok.is(tok::l_paren)) {
1244 // Parse C++ direct initializer: '(' expression-list ')'
1245 SourceLocation LParenLoc = ConsumeParen();
1246 ExprVector Exprs(Actions);
1247 CommaLocsTy CommaLocs;
1248
Douglas Gregor613bf102009-12-22 17:47:17 +00001249 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
1250 EnterScope(0);
Douglas Gregor0be31a22010-07-02 17:43:08 +00001251 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregor613bf102009-12-22 17:47:17 +00001252 }
1253
Douglas Gregor23996282009-05-12 21:31:51 +00001254 if (ParseExpressionList(Exprs, CommaLocs)) {
1255 SkipUntil(tok::r_paren);
Douglas Gregor613bf102009-12-22 17:47:17 +00001256
1257 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001258 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregor613bf102009-12-22 17:47:17 +00001259 ExitScope();
1260 }
Douglas Gregor23996282009-05-12 21:31:51 +00001261 } else {
1262 // Match the ')'.
1263 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
1264
1265 assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() &&
1266 "Unexpected number of commas!");
Douglas Gregor613bf102009-12-22 17:47:17 +00001267
1268 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001269 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregor613bf102009-12-22 17:47:17 +00001270 ExitScope();
1271 }
1272
Douglas Gregor23996282009-05-12 21:31:51 +00001273 Actions.AddCXXDirectInitializerToDecl(ThisDecl, LParenLoc,
1274 move_arg(Exprs),
Richard Smith30482bc2011-02-20 03:19:35 +00001275 RParenLoc,
1276 TypeContainsAuto);
Douglas Gregor23996282009-05-12 21:31:51 +00001277 }
Sebastian Redl3da34892011-06-05 12:23:16 +00001278 } else if (getLang().CPlusPlus0x && Tok.is(tok::l_brace)) {
1279 // Parse C++0x braced-init-list.
1280 if (D.getCXXScopeSpec().isSet()) {
1281 EnterScope(0);
1282 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
1283 }
1284
1285 ExprResult Init(ParseBraceInitializer());
1286
1287 if (D.getCXXScopeSpec().isSet()) {
1288 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
1289 ExitScope();
1290 }
1291
1292 if (Init.isInvalid()) {
1293 Actions.ActOnInitializerError(ThisDecl);
1294 } else
1295 Actions.AddInitializerToDecl(ThisDecl, Init.take(),
1296 /*DirectInit=*/true, TypeContainsAuto);
1297
Douglas Gregor23996282009-05-12 21:31:51 +00001298 } else {
Richard Smith30482bc2011-02-20 03:19:35 +00001299 Actions.ActOnUninitializedDecl(ThisDecl, TypeContainsAuto);
Douglas Gregor23996282009-05-12 21:31:51 +00001300 }
1301
Richard Smithb2bc2e62011-02-21 20:05:19 +00001302 Actions.FinalizeDeclaration(ThisDecl);
1303
Douglas Gregor23996282009-05-12 21:31:51 +00001304 return ThisDecl;
1305}
1306
Chris Lattner1890ac82006-08-13 01:16:23 +00001307/// ParseSpecifierQualifierList
1308/// specifier-qualifier-list:
1309/// type-specifier specifier-qualifier-list[opt]
1310/// type-qualifier specifier-qualifier-list[opt]
Chris Lattnere37e2332006-08-15 04:50:22 +00001311/// [GNU] attributes specifier-qualifier-list[opt]
Chris Lattner1890ac82006-08-13 01:16:23 +00001312///
Richard Smithcd1c0552011-07-01 19:46:12 +00001313void Parser::ParseSpecifierQualifierList(DeclSpec &DS, AccessSpecifier AS) {
Chris Lattner1890ac82006-08-13 01:16:23 +00001314 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
1315 /// parse declaration-specifiers and complain about extra stuff.
Richard Smithcd1c0552011-07-01 19:46:12 +00001316 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS);
Mike Stump11289f42009-09-09 15:08:12 +00001317
Chris Lattner1890ac82006-08-13 01:16:23 +00001318 // Validate declspec for type-name.
1319 unsigned Specs = DS.getParsedSpecifiers();
Chris Lattnera723ba92009-04-14 21:16:09 +00001320 if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers() &&
John McCall53fa7142010-12-24 02:08:15 +00001321 !DS.hasAttributes())
Chris Lattner1890ac82006-08-13 01:16:23 +00001322 Diag(Tok, diag::err_typename_requires_specqual);
Mike Stump11289f42009-09-09 15:08:12 +00001323
Chris Lattner1b22eed2006-11-28 05:12:07 +00001324 // Issue diagnostic and remove storage class if present.
Chris Lattner1890ac82006-08-13 01:16:23 +00001325 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
Chris Lattner1b22eed2006-11-28 05:12:07 +00001326 if (DS.getStorageClassSpecLoc().isValid())
1327 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
1328 else
1329 Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass);
Chris Lattnera925dc62006-11-28 04:33:46 +00001330 DS.ClearStorageClassSpecs();
Chris Lattner1890ac82006-08-13 01:16:23 +00001331 }
Mike Stump11289f42009-09-09 15:08:12 +00001332
Chris Lattner1b22eed2006-11-28 05:12:07 +00001333 // Issue diagnostic and remove function specfier if present.
Chris Lattner1890ac82006-08-13 01:16:23 +00001334 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Douglas Gregor61956c42008-10-31 09:07:45 +00001335 if (DS.isInlineSpecified())
1336 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
1337 if (DS.isVirtualSpecified())
1338 Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec);
1339 if (DS.isExplicitSpecified())
1340 Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec);
Chris Lattnera925dc62006-11-28 04:33:46 +00001341 DS.ClearFunctionSpecs();
Chris Lattner1890ac82006-08-13 01:16:23 +00001342 }
1343}
Chris Lattner53361ac2006-08-10 05:19:57 +00001344
Chris Lattner6cc055a2009-04-12 20:42:31 +00001345/// isValidAfterIdentifierInDeclaratorAfterDeclSpec - Return true if the
1346/// specified token is valid after the identifier in a declarator which
1347/// immediately follows the declspec. For example, these things are valid:
1348///
1349/// int x [ 4]; // direct-declarator
1350/// int x ( int y); // direct-declarator
1351/// int(int x ) // direct-declarator
1352/// int x ; // simple-declaration
1353/// int x = 17; // init-declarator-list
1354/// int x , y; // init-declarator-list
1355/// int x __asm__ ("foo"); // init-declarator-list
Chris Lattnera723ba92009-04-14 21:16:09 +00001356/// int x : 4; // struct-declarator
Chris Lattner2b988c12009-04-12 22:29:43 +00001357/// int x { 5}; // C++'0x unified initializers
Chris Lattner6cc055a2009-04-12 20:42:31 +00001358///
1359/// This is not, because 'x' does not immediately follow the declspec (though
1360/// ')' happens to be valid anyway).
1361/// int (x)
1362///
1363static bool isValidAfterIdentifierInDeclarator(const Token &T) {
1364 return T.is(tok::l_square) || T.is(tok::l_paren) || T.is(tok::r_paren) ||
1365 T.is(tok::semi) || T.is(tok::comma) || T.is(tok::equal) ||
Chris Lattnera723ba92009-04-14 21:16:09 +00001366 T.is(tok::kw_asm) || T.is(tok::l_brace) || T.is(tok::colon);
Chris Lattner6cc055a2009-04-12 20:42:31 +00001367}
1368
Chris Lattner20a0c612009-04-14 21:34:55 +00001369
1370/// ParseImplicitInt - This method is called when we have an non-typename
1371/// identifier in a declspec (which normally terminates the decl spec) when
1372/// the declspec has no type specifier. In this case, the declspec is either
1373/// malformed or is "implicit int" (in K&R and C89).
1374///
1375/// This method handles diagnosing this prettily and returns false if the
1376/// declspec is done being processed. If it recovers and thinks there may be
1377/// other pieces of declspec after it, it returns true.
1378///
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001379bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS,
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001380 const ParsedTemplateInfo &TemplateInfo,
Chris Lattner20a0c612009-04-14 21:34:55 +00001381 AccessSpecifier AS) {
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001382 assert(Tok.is(tok::identifier) && "should have identifier");
Mike Stump11289f42009-09-09 15:08:12 +00001383
Chris Lattner20a0c612009-04-14 21:34:55 +00001384 SourceLocation Loc = Tok.getLocation();
1385 // If we see an identifier that is not a type name, we normally would
1386 // parse it as the identifer being declared. However, when a typename
1387 // is typo'd or the definition is not included, this will incorrectly
1388 // parse the typename as the identifier name and fall over misparsing
1389 // later parts of the diagnostic.
1390 //
1391 // As such, we try to do some look-ahead in cases where this would
1392 // otherwise be an "implicit-int" case to see if this is invalid. For
1393 // example: "static foo_t x = 4;" In this case, if we parsed foo_t as
1394 // an identifier with implicit int, we'd get a parse error because the
1395 // next token is obviously invalid for a type. Parse these as a case
1396 // with an invalid type specifier.
1397 assert(!DS.hasTypeSpecifier() && "Type specifier checked above");
Mike Stump11289f42009-09-09 15:08:12 +00001398
Chris Lattner20a0c612009-04-14 21:34:55 +00001399 // Since we know that this either implicit int (which is rare) or an
1400 // error, we'd do lookahead to try to do better recovery.
1401 if (isValidAfterIdentifierInDeclarator(NextToken())) {
1402 // If this token is valid for implicit int, e.g. "static x = 4", then
1403 // we just avoid eating the identifier, so it will be parsed as the
1404 // identifier in the declarator.
1405 return false;
1406 }
Mike Stump11289f42009-09-09 15:08:12 +00001407
Chris Lattner20a0c612009-04-14 21:34:55 +00001408 // Otherwise, if we don't consume this token, we are going to emit an
1409 // error anyway. Try to recover from various common problems. Check
1410 // to see if this was a reference to a tag name without a tag specified.
1411 // This is a common problem in C (saying 'foo' instead of 'struct foo').
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001412 //
1413 // C++ doesn't need this, and isTagName doesn't take SS.
1414 if (SS == 0) {
Argyrios Kyrtzidis1f329402011-04-21 17:29:47 +00001415 const char *TagName = 0, *FixitTagName = 0;
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001416 tok::TokenKind TagKind = tok::unknown;
Mike Stump11289f42009-09-09 15:08:12 +00001417
Douglas Gregor0be31a22010-07-02 17:43:08 +00001418 switch (Actions.isTagName(*Tok.getIdentifierInfo(), getCurScope())) {
Chris Lattner20a0c612009-04-14 21:34:55 +00001419 default: break;
Argyrios Kyrtzidis1f329402011-04-21 17:29:47 +00001420 case DeclSpec::TST_enum:
1421 TagName="enum" ; FixitTagName = "enum " ; TagKind=tok::kw_enum ;break;
1422 case DeclSpec::TST_union:
1423 TagName="union" ; FixitTagName = "union " ;TagKind=tok::kw_union ;break;
1424 case DeclSpec::TST_struct:
1425 TagName="struct"; FixitTagName = "struct ";TagKind=tok::kw_struct;break;
1426 case DeclSpec::TST_class:
1427 TagName="class" ; FixitTagName = "class " ;TagKind=tok::kw_class ;break;
Chris Lattner20a0c612009-04-14 21:34:55 +00001428 }
Mike Stump11289f42009-09-09 15:08:12 +00001429
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001430 if (TagName) {
1431 Diag(Loc, diag::err_use_of_tag_name_without_tag)
John McCall38200b02010-02-14 01:03:10 +00001432 << Tok.getIdentifierInfo() << TagName << getLang().CPlusPlus
Argyrios Kyrtzidis1f329402011-04-21 17:29:47 +00001433 << FixItHint::CreateInsertion(Tok.getLocation(),FixitTagName);
Mike Stump11289f42009-09-09 15:08:12 +00001434
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001435 // Parse this as a tag as if the missing tag were present.
1436 if (TagKind == tok::kw_enum)
Douglas Gregordc70c3a2010-03-02 17:53:14 +00001437 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS);
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001438 else
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001439 ParseClassSpecifier(TagKind, Loc, DS, TemplateInfo, AS);
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001440 return true;
1441 }
Chris Lattner20a0c612009-04-14 21:34:55 +00001442 }
Mike Stump11289f42009-09-09 15:08:12 +00001443
Douglas Gregor15e56022009-10-13 23:27:22 +00001444 // This is almost certainly an invalid type name. Let the action emit a
1445 // diagnostic and attempt to recover.
John McCallba7bf592010-08-24 05:47:05 +00001446 ParsedType T;
Douglas Gregor15e56022009-10-13 23:27:22 +00001447 if (Actions.DiagnoseUnknownTypeName(*Tok.getIdentifierInfo(), Loc,
Douglas Gregor0be31a22010-07-02 17:43:08 +00001448 getCurScope(), SS, T)) {
Douglas Gregor15e56022009-10-13 23:27:22 +00001449 // The action emitted a diagnostic, so we don't have to.
1450 if (T) {
1451 // The action has suggested that the type T could be used. Set that as
1452 // the type in the declaration specifiers, consume the would-be type
1453 // name token, and we're done.
1454 const char *PrevSpec;
1455 unsigned DiagID;
John McCallba7bf592010-08-24 05:47:05 +00001456 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID, T);
Douglas Gregor15e56022009-10-13 23:27:22 +00001457 DS.SetRangeEnd(Tok.getLocation());
1458 ConsumeToken();
1459
1460 // There may be other declaration specifiers after this.
1461 return true;
1462 }
1463
1464 // Fall through; the action had no suggestion for us.
1465 } else {
1466 // The action did not emit a diagnostic, so emit one now.
1467 SourceRange R;
1468 if (SS) R = SS->getRange();
1469 Diag(Loc, diag::err_unknown_typename) << Tok.getIdentifierInfo() << R;
1470 }
Mike Stump11289f42009-09-09 15:08:12 +00001471
Douglas Gregor15e56022009-10-13 23:27:22 +00001472 // Mark this as an error.
Chris Lattner20a0c612009-04-14 21:34:55 +00001473 const char *PrevSpec;
John McCall49bfce42009-08-03 20:12:06 +00001474 unsigned DiagID;
1475 DS.SetTypeSpecType(DeclSpec::TST_error, Loc, PrevSpec, DiagID);
Chris Lattner20a0c612009-04-14 21:34:55 +00001476 DS.SetRangeEnd(Tok.getLocation());
1477 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001478
Chris Lattner20a0c612009-04-14 21:34:55 +00001479 // TODO: Could inject an invalid typedef decl in an enclosing scope to
1480 // avoid rippling error messages on subsequent uses of the same type,
1481 // could be useful if #include was forgotten.
1482 return false;
1483}
1484
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001485/// \brief Determine the declaration specifier context from the declarator
1486/// context.
1487///
1488/// \param Context the declarator context, which is one of the
1489/// Declarator::TheContext enumerator values.
1490Parser::DeclSpecContext
1491Parser::getDeclSpecContextFromDeclaratorContext(unsigned Context) {
1492 if (Context == Declarator::MemberContext)
1493 return DSC_class;
1494 if (Context == Declarator::FileContext)
1495 return DSC_top_level;
1496 return DSC_normal;
1497}
1498
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001499/// ParseDeclarationSpecifiers
1500/// declaration-specifiers: [C99 6.7]
Chris Lattner3b561a32006-08-13 00:12:11 +00001501/// storage-class-specifier declaration-specifiers[opt]
1502/// type-specifier declaration-specifiers[opt]
Chris Lattner3b561a32006-08-13 00:12:11 +00001503/// [C99] function-specifier declaration-specifiers[opt]
Chris Lattnere37e2332006-08-15 04:50:22 +00001504/// [GNU] attributes declaration-specifiers[opt]
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001505///
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001506/// storage-class-specifier: [C99 6.7.1]
Chris Lattnerda48a8e2006-08-04 05:25:55 +00001507/// 'typedef'
1508/// 'extern'
1509/// 'static'
1510/// 'auto'
1511/// 'register'
Sebastian Redlccdfaba2008-11-14 23:42:31 +00001512/// [C++] 'mutable'
Chris Lattnerda48a8e2006-08-04 05:25:55 +00001513/// [GNU] '__thread'
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001514/// function-specifier: [C99 6.7.4]
Chris Lattner3b561a32006-08-13 00:12:11 +00001515/// [C99] 'inline'
Douglas Gregor61956c42008-10-31 09:07:45 +00001516/// [C++] 'virtual'
1517/// [C++] 'explicit'
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00001518/// [OpenCL] '__kernel'
Anders Carlssoncd8db412009-05-06 04:46:28 +00001519/// 'friend': [C++ dcl.friend]
Sebastian Redl39c2a8b2009-11-05 15:47:02 +00001520/// 'constexpr': [C++0x dcl.constexpr]
Anders Carlssoncd8db412009-05-06 04:46:28 +00001521
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001522///
Douglas Gregorb9bd8a92008-12-24 02:52:09 +00001523void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001524 const ParsedTemplateInfo &TemplateInfo,
John McCall07e91c02009-08-06 02:15:43 +00001525 AccessSpecifier AS,
Douglas Gregor0e7dde52011-04-24 05:37:28 +00001526 DeclSpecContext DSContext) {
1527 if (DS.getSourceRange().isInvalid()) {
1528 DS.SetRangeStart(Tok.getLocation());
1529 DS.SetRangeEnd(Tok.getLocation());
1530 }
1531
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001532 while (1) {
John McCall49bfce42009-08-03 20:12:06 +00001533 bool isInvalid = false;
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001534 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00001535 unsigned DiagID = 0;
1536
Chris Lattner4d8f8732006-11-28 05:05:08 +00001537 SourceLocation Loc = Tok.getLocation();
Douglas Gregor450c75a2008-11-07 15:42:26 +00001538
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001539 switch (Tok.getKind()) {
Mike Stump11289f42009-09-09 15:08:12 +00001540 default:
Chris Lattner0974b232008-07-26 00:20:22 +00001541 DoneWithDeclSpec:
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001542 // If this is not a declaration specifier token, we're done reading decl
1543 // specifiers. First verify that DeclSpec's are consistent.
Douglas Gregore3e01a22009-04-01 22:41:11 +00001544 DS.Finish(Diags, PP);
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001545 return;
Mike Stump11289f42009-09-09 15:08:12 +00001546
Douglas Gregorc49f5b22010-08-23 18:23:48 +00001547 case tok::code_completion: {
John McCallfaf5fb42010-08-26 23:41:50 +00001548 Sema::ParserCompletionContext CCC = Sema::PCC_Namespace;
Douglas Gregorc49f5b22010-08-23 18:23:48 +00001549 if (DS.hasTypeSpecifier()) {
1550 bool AllowNonIdentifiers
1551 = (getCurScope()->getFlags() & (Scope::ControlScope |
1552 Scope::BlockScope |
1553 Scope::TemplateParamScope |
1554 Scope::FunctionPrototypeScope |
1555 Scope::AtCatchScope)) == 0;
1556 bool AllowNestedNameSpecifiers
1557 = DSContext == DSC_top_level ||
1558 (DSContext == DSC_class && DS.isFriendSpecified());
1559
Douglas Gregorbfcea8b2010-09-16 15:14:18 +00001560 Actions.CodeCompleteDeclSpec(getCurScope(), DS,
1561 AllowNonIdentifiers,
1562 AllowNestedNameSpecifiers);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001563 return cutOffParsing();
Douglas Gregorc49f5b22010-08-23 18:23:48 +00001564 }
1565
Douglas Gregor80039242011-02-15 20:33:25 +00001566 if (getCurScope()->getFnParent() || getCurScope()->getBlockParent())
1567 CCC = Sema::PCC_LocalDeclarationSpecifiers;
1568 else if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate)
John McCallfaf5fb42010-08-26 23:41:50 +00001569 CCC = DSContext == DSC_class? Sema::PCC_MemberTemplate
1570 : Sema::PCC_Template;
Douglas Gregorc49f5b22010-08-23 18:23:48 +00001571 else if (DSContext == DSC_class)
John McCallfaf5fb42010-08-26 23:41:50 +00001572 CCC = Sema::PCC_Class;
Douglas Gregorc49f5b22010-08-23 18:23:48 +00001573 else if (ObjCImpDecl)
John McCallfaf5fb42010-08-26 23:41:50 +00001574 CCC = Sema::PCC_ObjCImplementation;
Douglas Gregorc49f5b22010-08-23 18:23:48 +00001575
1576 Actions.CodeCompleteOrdinaryName(getCurScope(), CCC);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001577 return cutOffParsing();
Douglas Gregorc49f5b22010-08-23 18:23:48 +00001578 }
1579
Chris Lattnerbd31aa32009-01-05 00:07:25 +00001580 case tok::coloncolon: // ::foo::bar
John McCall1f476a12010-02-26 08:45:28 +00001581 // C++ scope specifier. Annotate and loop, or bail out on error.
1582 if (TryAnnotateCXXScopeToken(true)) {
1583 if (!DS.hasTypeSpecifier())
1584 DS.SetTypeSpecError();
1585 goto DoneWithDeclSpec;
1586 }
John McCall8bc2a702010-03-01 18:20:46 +00001587 if (Tok.is(tok::coloncolon)) // ::new or ::delete
1588 goto DoneWithDeclSpec;
John McCall1f476a12010-02-26 08:45:28 +00001589 continue;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001590
1591 case tok::annot_cxxscope: {
1592 if (DS.hasTypeSpecifier())
1593 goto DoneWithDeclSpec;
1594
John McCall9dab4e62009-12-12 11:40:51 +00001595 CXXScopeSpec SS;
Douglas Gregor869ad452011-02-24 17:54:50 +00001596 Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(),
1597 Tok.getAnnotationRange(),
1598 SS);
John McCall9dab4e62009-12-12 11:40:51 +00001599
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001600 // We are looking for a qualified typename.
Douglas Gregor167fa622009-03-25 15:40:00 +00001601 Token Next = NextToken();
Mike Stump11289f42009-09-09 15:08:12 +00001602 if (Next.is(tok::annot_template_id) &&
Douglas Gregor167fa622009-03-25 15:40:00 +00001603 static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue())
Douglas Gregorb67535d2009-03-31 00:43:58 +00001604 ->Kind == TNK_Type_template) {
Douglas Gregor167fa622009-03-25 15:40:00 +00001605 // We have a qualified template-id, e.g., N::A<int>
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001606
1607 // C++ [class.qual]p2:
1608 // In a lookup in which the constructor is an acceptable lookup
1609 // result and the nested-name-specifier nominates a class C:
1610 //
1611 // - if the name specified after the
1612 // nested-name-specifier, when looked up in C, is the
1613 // injected-class-name of C (Clause 9), or
1614 //
1615 // - if the name specified after the nested-name-specifier
1616 // is the same as the identifier or the
1617 // simple-template-id's template-name in the last
1618 // component of the nested-name-specifier,
1619 //
1620 // the name is instead considered to name the constructor of
1621 // class C.
1622 //
1623 // Thus, if the template-name is actually the constructor
1624 // name, then the code is ill-formed; this interpretation is
1625 // reinforced by the NAD status of core issue 635.
Argyrios Kyrtzidisc0c5dd22011-06-22 06:09:49 +00001626 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Next);
John McCall84821e72010-04-13 06:39:49 +00001627 if ((DSContext == DSC_top_level ||
1628 (DSContext == DSC_class && DS.isFriendSpecified())) &&
1629 TemplateId->Name &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00001630 Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) {
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001631 if (isConstructorDeclarator()) {
1632 // The user meant this to be an out-of-line constructor
1633 // definition, but template arguments are not allowed
1634 // there. Just allow this as a constructor; we'll
1635 // complain about it later.
1636 goto DoneWithDeclSpec;
1637 }
1638
1639 // The user meant this to name a type, but it actually names
1640 // a constructor with some extraneous template
1641 // arguments. Complain, then parse it as a type as the user
1642 // intended.
1643 Diag(TemplateId->TemplateNameLoc,
1644 diag::err_out_of_line_template_id_names_constructor)
1645 << TemplateId->Name;
1646 }
1647
John McCall9dab4e62009-12-12 11:40:51 +00001648 DS.getTypeSpecScope() = SS;
1649 ConsumeToken(); // The C++ scope.
Mike Stump11289f42009-09-09 15:08:12 +00001650 assert(Tok.is(tok::annot_template_id) &&
Douglas Gregor167fa622009-03-25 15:40:00 +00001651 "ParseOptionalCXXScopeSpecifier not working");
Douglas Gregore7c20652011-03-02 00:47:37 +00001652 AnnotateTemplateIdTokenAsType();
Douglas Gregor167fa622009-03-25 15:40:00 +00001653 continue;
1654 }
1655
Douglas Gregorc5790df2009-09-28 07:26:33 +00001656 if (Next.is(tok::annot_typename)) {
John McCall9dab4e62009-12-12 11:40:51 +00001657 DS.getTypeSpecScope() = SS;
1658 ConsumeToken(); // The C++ scope.
John McCallba7bf592010-08-24 05:47:05 +00001659 if (Tok.getAnnotationValue()) {
1660 ParsedType T = getTypeAnnotation(Tok);
Nico Weber77430342010-11-22 10:30:56 +00001661 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename,
1662 Tok.getAnnotationEndLoc(),
John McCallba7bf592010-08-24 05:47:05 +00001663 PrevSpec, DiagID, T);
1664 }
Douglas Gregorc5790df2009-09-28 07:26:33 +00001665 else
1666 DS.SetTypeSpecError();
1667 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1668 ConsumeToken(); // The typename
1669 }
1670
Douglas Gregor167fa622009-03-25 15:40:00 +00001671 if (Next.isNot(tok::identifier))
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001672 goto DoneWithDeclSpec;
1673
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001674 // If we're in a context where the identifier could be a class name,
1675 // check whether this is a constructor declaration.
John McCall84821e72010-04-13 06:39:49 +00001676 if ((DSContext == DSC_top_level ||
1677 (DSContext == DSC_class && DS.isFriendSpecified())) &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00001678 Actions.isCurrentClassName(*Next.getIdentifierInfo(), getCurScope(),
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001679 &SS)) {
1680 if (isConstructorDeclarator())
1681 goto DoneWithDeclSpec;
1682
1683 // As noted in C++ [class.qual]p2 (cited above), when the name
1684 // of the class is qualified in a context where it could name
1685 // a constructor, its a constructor name. However, we've
1686 // looked at the declarator, and the user probably meant this
1687 // to be a type. Complain that it isn't supposed to be treated
1688 // as a type, then proceed to parse it as a type.
1689 Diag(Next.getLocation(), diag::err_out_of_line_type_names_constructor)
1690 << Next.getIdentifierInfo();
1691 }
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001692
John McCallba7bf592010-08-24 05:47:05 +00001693 ParsedType TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(),
1694 Next.getLocation(),
Douglas Gregor844cb502011-03-01 18:12:44 +00001695 getCurScope(), &SS,
1696 false, false, ParsedType(),
1697 /*NonTrivialSourceInfo=*/true);
Douglas Gregor8bf42052009-02-09 18:46:07 +00001698
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001699 // If the referenced identifier is not a type, then this declspec is
1700 // erroneous: We already checked about that it has no type specifier, and
1701 // C++ doesn't have implicit int. Diagnose it as a typo w.r.t. to the
Mike Stump11289f42009-09-09 15:08:12 +00001702 // typename.
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001703 if (TypeRep == 0) {
1704 ConsumeToken(); // Eat the scope spec so the identifier is current.
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001705 if (ParseImplicitInt(DS, &SS, TemplateInfo, AS)) continue;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001706 goto DoneWithDeclSpec;
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001707 }
Mike Stump11289f42009-09-09 15:08:12 +00001708
John McCall9dab4e62009-12-12 11:40:51 +00001709 DS.getTypeSpecScope() = SS;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001710 ConsumeToken(); // The C++ scope.
1711
Douglas Gregor9817f4a2009-02-09 15:09:02 +00001712 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00001713 DiagID, TypeRep);
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001714 if (isInvalid)
1715 break;
Mike Stump11289f42009-09-09 15:08:12 +00001716
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001717 DS.SetRangeEnd(Tok.getLocation());
1718 ConsumeToken(); // The typename.
1719
1720 continue;
1721 }
Mike Stump11289f42009-09-09 15:08:12 +00001722
Chris Lattnere387d9e2009-01-21 19:48:37 +00001723 case tok::annot_typename: {
John McCallba7bf592010-08-24 05:47:05 +00001724 if (Tok.getAnnotationValue()) {
1725 ParsedType T = getTypeAnnotation(Tok);
Nico Weber7f8bb362010-11-22 12:50:03 +00001726 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallba7bf592010-08-24 05:47:05 +00001727 DiagID, T);
1728 } else
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00001729 DS.SetTypeSpecError();
Chris Lattner005fc1b2010-04-05 18:18:31 +00001730
1731 if (isInvalid)
1732 break;
1733
Chris Lattnere387d9e2009-01-21 19:48:37 +00001734 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1735 ConsumeToken(); // The typename
Mike Stump11289f42009-09-09 15:08:12 +00001736
Chris Lattnere387d9e2009-01-21 19:48:37 +00001737 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1738 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001739 // Objective-C interface.
1740 if (Tok.is(tok::less) && getLang().ObjC1)
1741 ParseObjCProtocolQualifiers(DS);
1742
Chris Lattnere387d9e2009-01-21 19:48:37 +00001743 continue;
1744 }
Mike Stump11289f42009-09-09 15:08:12 +00001745
Douglas Gregor06873092011-04-28 15:48:45 +00001746 case tok::kw___is_signed:
1747 // GNU libstdc++ 4.4 uses __is_signed as an identifier, but Clang
1748 // typically treats it as a trait. If we see __is_signed as it appears
1749 // in libstdc++, e.g.,
1750 //
1751 // static const bool __is_signed;
1752 //
1753 // then treat __is_signed as an identifier rather than as a keyword.
1754 if (DS.getTypeSpecType() == TST_bool &&
1755 DS.getTypeQualifiers() == DeclSpec::TQ_const &&
1756 DS.getStorageClassSpec() == DeclSpec::SCS_static) {
1757 Tok.getIdentifierInfo()->RevertTokenIDToIdentifier();
1758 Tok.setKind(tok::identifier);
1759 }
1760
1761 // We're done with the declaration-specifiers.
1762 goto DoneWithDeclSpec;
1763
Chris Lattner16fac4f2008-07-26 01:18:38 +00001764 // typedef-name
1765 case tok::identifier: {
Chris Lattnerbd31aa32009-01-05 00:07:25 +00001766 // In C++, check to see if this is a scope specifier like foo::bar::, if
1767 // so handle it as such. This is important for ctor parsing.
John McCall1f476a12010-02-26 08:45:28 +00001768 if (getLang().CPlusPlus) {
1769 if (TryAnnotateCXXScopeToken(true)) {
1770 if (!DS.hasTypeSpecifier())
1771 DS.SetTypeSpecError();
1772 goto DoneWithDeclSpec;
1773 }
1774 if (!Tok.is(tok::identifier))
1775 continue;
1776 }
Mike Stump11289f42009-09-09 15:08:12 +00001777
Chris Lattner16fac4f2008-07-26 01:18:38 +00001778 // This identifier can only be a typedef name if we haven't already seen
1779 // a type-specifier. Without this check we misparse:
1780 // typedef int X; struct Y { short X; }; as 'short int'.
1781 if (DS.hasTypeSpecifier())
1782 goto DoneWithDeclSpec;
Mike Stump11289f42009-09-09 15:08:12 +00001783
John Thompson22334602010-02-05 00:12:22 +00001784 // Check for need to substitute AltiVec keyword tokens.
1785 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
1786 break;
1787
Chris Lattner16fac4f2008-07-26 01:18:38 +00001788 // It has to be available as a typedef too!
John McCallba7bf592010-08-24 05:47:05 +00001789 ParsedType TypeRep =
1790 Actions.getTypeName(*Tok.getIdentifierInfo(),
1791 Tok.getLocation(), getCurScope());
Douglas Gregor8bf42052009-02-09 18:46:07 +00001792
Chris Lattner6cc055a2009-04-12 20:42:31 +00001793 // If this is not a typedef name, don't parse it as part of the declspec,
1794 // it must be an implicit int or an error.
John McCallba7bf592010-08-24 05:47:05 +00001795 if (!TypeRep) {
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001796 if (ParseImplicitInt(DS, 0, TemplateInfo, AS)) continue;
Chris Lattner16fac4f2008-07-26 01:18:38 +00001797 goto DoneWithDeclSpec;
Chris Lattner6cc055a2009-04-12 20:42:31 +00001798 }
Douglas Gregor8bf42052009-02-09 18:46:07 +00001799
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001800 // If we're in a context where the identifier could be a class name,
1801 // check whether this is a constructor declaration.
1802 if (getLang().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00001803 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), getCurScope()) &&
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001804 isConstructorDeclarator())
Douglas Gregor61956c42008-10-31 09:07:45 +00001805 goto DoneWithDeclSpec;
1806
Douglas Gregor9817f4a2009-02-09 15:09:02 +00001807 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00001808 DiagID, TypeRep);
Chris Lattner16fac4f2008-07-26 01:18:38 +00001809 if (isInvalid)
1810 break;
Mike Stump11289f42009-09-09 15:08:12 +00001811
Chris Lattner16fac4f2008-07-26 01:18:38 +00001812 DS.SetRangeEnd(Tok.getLocation());
1813 ConsumeToken(); // The identifier
1814
1815 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1816 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001817 // Objective-C interface.
1818 if (Tok.is(tok::less) && getLang().ObjC1)
1819 ParseObjCProtocolQualifiers(DS);
1820
Steve Naroffcd5e7822008-09-22 10:28:57 +00001821 // Need to support trailing type qualifiers (e.g. "id<p> const").
1822 // If a type specifier follows, it will be diagnosed elsewhere.
1823 continue;
Chris Lattner16fac4f2008-07-26 01:18:38 +00001824 }
Douglas Gregor7f741122009-02-25 19:37:18 +00001825
1826 // type-name
1827 case tok::annot_template_id: {
Argyrios Kyrtzidisc0c5dd22011-06-22 06:09:49 +00001828 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregorb67535d2009-03-31 00:43:58 +00001829 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor7f741122009-02-25 19:37:18 +00001830 // This template-id does not refer to a type name, so we're
1831 // done with the type-specifiers.
1832 goto DoneWithDeclSpec;
1833 }
1834
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001835 // If we're in a context where the template-id could be a
1836 // constructor name or specialization, check whether this is a
1837 // constructor declaration.
1838 if (getLang().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00001839 Actions.isCurrentClassName(*TemplateId->Name, getCurScope()) &&
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001840 isConstructorDeclarator())
1841 goto DoneWithDeclSpec;
1842
Douglas Gregor7f741122009-02-25 19:37:18 +00001843 // Turn the template-id annotation token into a type annotation
1844 // token, then try again to parse it as a type-specifier.
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00001845 AnnotateTemplateIdTokenAsType();
Douglas Gregor7f741122009-02-25 19:37:18 +00001846 continue;
1847 }
1848
Chris Lattnere37e2332006-08-15 04:50:22 +00001849 // GNU attributes support.
1850 case tok::kw___attribute:
John McCall53fa7142010-12-24 02:08:15 +00001851 ParseGNUAttributes(DS.getAttributes());
Chris Lattnerb95cca02006-10-17 03:01:08 +00001852 continue;
Steve Naroff3a9b7e02008-12-24 20:59:21 +00001853
1854 // Microsoft declspec support.
1855 case tok::kw___declspec:
John McCall53fa7142010-12-24 02:08:15 +00001856 ParseMicrosoftDeclSpec(DS.getAttributes());
Steve Naroff3a9b7e02008-12-24 20:59:21 +00001857 continue;
Mike Stump11289f42009-09-09 15:08:12 +00001858
Steve Naroff44ac7772008-12-25 14:16:32 +00001859 // Microsoft single token adornments.
Steve Narofff9c29d42008-12-25 14:41:26 +00001860 case tok::kw___forceinline:
Eli Friedman53339e02009-06-08 23:27:34 +00001861 // FIXME: Add handling here!
1862 break;
1863
1864 case tok::kw___ptr64:
Francois Pichetf2fb4112011-08-25 00:36:46 +00001865 case tok::kw___ptr32:
Steve Narofff9c29d42008-12-25 14:41:26 +00001866 case tok::kw___w64:
Steve Naroff44ac7772008-12-25 14:16:32 +00001867 case tok::kw___cdecl:
1868 case tok::kw___stdcall:
1869 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00001870 case tok::kw___thiscall:
Francois Pichet17ed0202011-08-18 09:59:55 +00001871 case tok::kw___unaligned:
John McCall53fa7142010-12-24 02:08:15 +00001872 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman53339e02009-06-08 23:27:34 +00001873 continue;
1874
Dawn Perchik335e16b2010-09-03 01:29:35 +00001875 // Borland single token adornments.
1876 case tok::kw___pascal:
John McCall53fa7142010-12-24 02:08:15 +00001877 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik335e16b2010-09-03 01:29:35 +00001878 continue;
1879
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00001880 // OpenCL single token adornments.
1881 case tok::kw___kernel:
1882 ParseOpenCLAttributes(DS.getAttributes());
1883 continue;
1884
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001885 // storage-class-specifier
1886 case tok::kw_typedef:
John McCall49bfce42009-08-03 20:12:06 +00001887 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_typedef, Loc, PrevSpec,
Peter Collingbournede32b202011-02-11 19:59:54 +00001888 DiagID, getLang());
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001889 break;
1890 case tok::kw_extern:
Chris Lattner353f5742006-11-28 04:50:12 +00001891 if (DS.isThreadSpecified())
Chris Lattner6d29c102008-11-18 07:48:38 +00001892 Diag(Tok, diag::ext_thread_before) << "extern";
John McCall49bfce42009-08-03 20:12:06 +00001893 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_extern, Loc, PrevSpec,
Peter Collingbournede32b202011-02-11 19:59:54 +00001894 DiagID, getLang());
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001895 break;
Steve Naroff2050b0d2007-12-18 00:16:02 +00001896 case tok::kw___private_extern__:
Chris Lattner371ed4e2008-04-06 06:57:35 +00001897 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_private_extern, Loc,
Peter Collingbournede32b202011-02-11 19:59:54 +00001898 PrevSpec, DiagID, getLang());
Steve Naroff2050b0d2007-12-18 00:16:02 +00001899 break;
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001900 case tok::kw_static:
Chris Lattner353f5742006-11-28 04:50:12 +00001901 if (DS.isThreadSpecified())
Chris Lattner6d29c102008-11-18 07:48:38 +00001902 Diag(Tok, diag::ext_thread_before) << "static";
John McCall49bfce42009-08-03 20:12:06 +00001903 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_static, Loc, PrevSpec,
Peter Collingbournede32b202011-02-11 19:59:54 +00001904 DiagID, getLang());
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001905 break;
1906 case tok::kw_auto:
Douglas Gregor1e989862011-03-14 21:43:30 +00001907 if (getLang().CPlusPlus0x) {
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00001908 if (isKnownToBeTypeSpecifier(GetLookAheadToken(1))) {
1909 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, Loc, PrevSpec,
Richard Smith58c74332011-09-04 19:54:14 +00001910 DiagID, getLang());
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00001911 if (!isInvalid)
Richard Smith58c74332011-09-04 19:54:14 +00001912 Diag(Tok, diag::ext_auto_storage_class)
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00001913 << FixItHint::CreateRemoval(DS.getStorageClassSpecLoc());
Richard Smith58c74332011-09-04 19:54:14 +00001914 } else
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00001915 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec,
1916 DiagID);
Richard Smith58c74332011-09-04 19:54:14 +00001917 } else
John McCall49bfce42009-08-03 20:12:06 +00001918 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, Loc, PrevSpec,
Peter Collingbournede32b202011-02-11 19:59:54 +00001919 DiagID, getLang());
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001920 break;
1921 case tok::kw_register:
John McCall49bfce42009-08-03 20:12:06 +00001922 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_register, Loc, PrevSpec,
Peter Collingbournede32b202011-02-11 19:59:54 +00001923 DiagID, getLang());
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001924 break;
Sebastian Redlccdfaba2008-11-14 23:42:31 +00001925 case tok::kw_mutable:
John McCall49bfce42009-08-03 20:12:06 +00001926 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_mutable, Loc, PrevSpec,
Peter Collingbournede32b202011-02-11 19:59:54 +00001927 DiagID, getLang());
Sebastian Redlccdfaba2008-11-14 23:42:31 +00001928 break;
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001929 case tok::kw___thread:
John McCall49bfce42009-08-03 20:12:06 +00001930 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001931 break;
Mike Stump11289f42009-09-09 15:08:12 +00001932
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001933 // function-specifier
1934 case tok::kw_inline:
John McCall49bfce42009-08-03 20:12:06 +00001935 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec, DiagID);
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001936 break;
Douglas Gregor61956c42008-10-31 09:07:45 +00001937 case tok::kw_virtual:
John McCall49bfce42009-08-03 20:12:06 +00001938 isInvalid = DS.SetFunctionSpecVirtual(Loc, PrevSpec, DiagID);
Douglas Gregor61956c42008-10-31 09:07:45 +00001939 break;
Douglas Gregor61956c42008-10-31 09:07:45 +00001940 case tok::kw_explicit:
John McCall49bfce42009-08-03 20:12:06 +00001941 isInvalid = DS.SetFunctionSpecExplicit(Loc, PrevSpec, DiagID);
Douglas Gregor61956c42008-10-31 09:07:45 +00001942 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00001943
Anders Carlssoncd8db412009-05-06 04:46:28 +00001944 // friend
1945 case tok::kw_friend:
John McCall07e91c02009-08-06 02:15:43 +00001946 if (DSContext == DSC_class)
1947 isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID);
1948 else {
1949 PrevSpec = ""; // not actually used by the diagnostic
1950 DiagID = diag::err_friend_invalid_in_context;
1951 isInvalid = true;
1952 }
Anders Carlssoncd8db412009-05-06 04:46:28 +00001953 break;
Mike Stump11289f42009-09-09 15:08:12 +00001954
Sebastian Redl39c2a8b2009-11-05 15:47:02 +00001955 // constexpr
1956 case tok::kw_constexpr:
1957 isInvalid = DS.SetConstexprSpec(Loc, PrevSpec, DiagID);
1958 break;
1959
Chris Lattnere387d9e2009-01-21 19:48:37 +00001960 // type-specifier
1961 case tok::kw_short:
John McCall49bfce42009-08-03 20:12:06 +00001962 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec,
1963 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001964 break;
1965 case tok::kw_long:
1966 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCall49bfce42009-08-03 20:12:06 +00001967 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
1968 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001969 else
John McCall49bfce42009-08-03 20:12:06 +00001970 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
1971 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001972 break;
Francois Pichet84133e42011-04-28 01:59:37 +00001973 case tok::kw___int64:
1974 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
1975 DiagID);
1976 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00001977 case tok::kw_signed:
John McCall49bfce42009-08-03 20:12:06 +00001978 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec,
1979 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001980 break;
1981 case tok::kw_unsigned:
John McCall49bfce42009-08-03 20:12:06 +00001982 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
1983 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001984 break;
1985 case tok::kw__Complex:
John McCall49bfce42009-08-03 20:12:06 +00001986 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
1987 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001988 break;
1989 case tok::kw__Imaginary:
John McCall49bfce42009-08-03 20:12:06 +00001990 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
1991 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001992 break;
1993 case tok::kw_void:
John McCall49bfce42009-08-03 20:12:06 +00001994 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec,
1995 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001996 break;
1997 case tok::kw_char:
John McCall49bfce42009-08-03 20:12:06 +00001998 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec,
1999 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002000 break;
2001 case tok::kw_int:
John McCall49bfce42009-08-03 20:12:06 +00002002 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec,
2003 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002004 break;
2005 case tok::kw_float:
John McCall49bfce42009-08-03 20:12:06 +00002006 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec,
2007 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002008 break;
2009 case tok::kw_double:
John McCall49bfce42009-08-03 20:12:06 +00002010 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec,
2011 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002012 break;
2013 case tok::kw_wchar_t:
John McCall49bfce42009-08-03 20:12:06 +00002014 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec,
2015 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002016 break;
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00002017 case tok::kw_char16_t:
John McCall49bfce42009-08-03 20:12:06 +00002018 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec,
2019 DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00002020 break;
2021 case tok::kw_char32_t:
John McCall49bfce42009-08-03 20:12:06 +00002022 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec,
2023 DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00002024 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00002025 case tok::kw_bool:
2026 case tok::kw__Bool:
Argyrios Kyrtzidis20ee5ae2010-11-16 18:18:13 +00002027 if (Tok.is(tok::kw_bool) &&
2028 DS.getTypeSpecType() != DeclSpec::TST_unspecified &&
2029 DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
2030 PrevSpec = ""; // Not used by the diagnostic.
2031 DiagID = diag::err_bool_redeclaration;
Fariborz Jahanian2b059992011-04-19 21:42:37 +00002032 // For better error recovery.
2033 Tok.setKind(tok::identifier);
Argyrios Kyrtzidis20ee5ae2010-11-16 18:18:13 +00002034 isInvalid = true;
2035 } else {
2036 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec,
2037 DiagID);
2038 }
Chris Lattnere387d9e2009-01-21 19:48:37 +00002039 break;
2040 case tok::kw__Decimal32:
John McCall49bfce42009-08-03 20:12:06 +00002041 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
2042 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002043 break;
2044 case tok::kw__Decimal64:
John McCall49bfce42009-08-03 20:12:06 +00002045 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
2046 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002047 break;
2048 case tok::kw__Decimal128:
John McCall49bfce42009-08-03 20:12:06 +00002049 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
2050 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002051 break;
John Thompson22334602010-02-05 00:12:22 +00002052 case tok::kw___vector:
2053 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
2054 break;
2055 case tok::kw___pixel:
2056 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
2057 break;
John McCall39439732011-04-09 22:50:59 +00002058 case tok::kw___unknown_anytype:
2059 isInvalid = DS.SetTypeSpecType(TST_unknown_anytype, Loc,
2060 PrevSpec, DiagID);
2061 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00002062
2063 // class-specifier:
2064 case tok::kw_class:
2065 case tok::kw_struct:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00002066 case tok::kw_union: {
2067 tok::TokenKind Kind = Tok.getKind();
2068 ConsumeToken();
Douglas Gregor1b57ff32009-05-12 23:25:50 +00002069 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002070 continue;
Chris Lattnerffaa0e62009-04-12 21:49:30 +00002071 }
Chris Lattnere387d9e2009-01-21 19:48:37 +00002072
2073 // enum-specifier:
2074 case tok::kw_enum:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00002075 ConsumeToken();
Douglas Gregordc70c3a2010-03-02 17:53:14 +00002076 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002077 continue;
2078
2079 // cv-qualifier:
2080 case tok::kw_const:
John McCall49bfce42009-08-03 20:12:06 +00002081 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec, DiagID,
2082 getLang());
Chris Lattnere387d9e2009-01-21 19:48:37 +00002083 break;
2084 case tok::kw_volatile:
John McCall49bfce42009-08-03 20:12:06 +00002085 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
2086 getLang());
Chris Lattnere387d9e2009-01-21 19:48:37 +00002087 break;
2088 case tok::kw_restrict:
John McCall49bfce42009-08-03 20:12:06 +00002089 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
2090 getLang());
Chris Lattnere387d9e2009-01-21 19:48:37 +00002091 break;
2092
Douglas Gregor333489b2009-03-27 23:10:48 +00002093 // C++ typename-specifier:
2094 case tok::kw_typename:
John McCall1f476a12010-02-26 08:45:28 +00002095 if (TryAnnotateTypeOrScopeToken()) {
2096 DS.SetTypeSpecError();
2097 goto DoneWithDeclSpec;
2098 }
2099 if (!Tok.is(tok::kw_typename))
Douglas Gregor333489b2009-03-27 23:10:48 +00002100 continue;
2101 break;
2102
Chris Lattnere387d9e2009-01-21 19:48:37 +00002103 // GNU typeof support.
2104 case tok::kw_typeof:
2105 ParseTypeofSpecifier(DS);
2106 continue;
2107
Anders Carlsson74948d02009-06-24 17:47:40 +00002108 case tok::kw_decltype:
2109 ParseDecltypeSpecifier(DS);
2110 continue;
2111
Alexis Hunt4a257072011-05-19 05:37:45 +00002112 case tok::kw___underlying_type:
2113 ParseUnderlyingTypeSpecifier(DS);
2114
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00002115 // OpenCL qualifiers:
2116 case tok::kw_private:
2117 if (!getLang().OpenCL)
2118 goto DoneWithDeclSpec;
2119 case tok::kw___private:
2120 case tok::kw___global:
2121 case tok::kw___local:
2122 case tok::kw___constant:
2123 case tok::kw___read_only:
2124 case tok::kw___write_only:
2125 case tok::kw___read_write:
2126 ParseOpenCLQualifiers(DS);
2127 break;
2128
Steve Naroffcfdf6162008-06-05 00:02:44 +00002129 case tok::less:
Chris Lattner16fac4f2008-07-26 01:18:38 +00002130 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattner0974b232008-07-26 00:20:22 +00002131 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
2132 // but we support it.
Chris Lattner16fac4f2008-07-26 01:18:38 +00002133 if (DS.hasTypeSpecifier() || !getLang().ObjC1)
Chris Lattner0974b232008-07-26 00:20:22 +00002134 goto DoneWithDeclSpec;
Mike Stump11289f42009-09-09 15:08:12 +00002135
Douglas Gregor3a001f42010-11-19 17:10:50 +00002136 if (!ParseObjCProtocolQualifiers(DS))
2137 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
2138 << FixItHint::CreateInsertion(Loc, "id")
2139 << SourceRange(Loc, DS.getSourceRange().getEnd());
Douglas Gregor06e41ae2010-10-21 23:17:00 +00002140
2141 // Need to support trailing type qualifiers (e.g. "id<p> const").
2142 // If a type specifier follows, it will be diagnosed elsewhere.
2143 continue;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002144 }
John McCall49bfce42009-08-03 20:12:06 +00002145 // If the specifier wasn't legal, issue a diagnostic.
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002146 if (isInvalid) {
2147 assert(PrevSpec && "Method did not return previous specifier!");
John McCall49bfce42009-08-03 20:12:06 +00002148 assert(DiagID);
Douglas Gregora05f5ab2010-08-23 14:34:43 +00002149
2150 if (DiagID == diag::ext_duplicate_declspec)
2151 Diag(Tok, DiagID)
2152 << PrevSpec << FixItHint::CreateRemoval(Tok.getLocation());
2153 else
2154 Diag(Tok, DiagID) << PrevSpec;
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002155 }
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00002156
Chris Lattner2e232092008-03-13 06:29:04 +00002157 DS.SetRangeEnd(Tok.getLocation());
Fariborz Jahanian2b059992011-04-19 21:42:37 +00002158 if (DiagID != diag::err_bool_redeclaration)
2159 ConsumeToken();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002160 }
2161}
Douglas Gregoreb31f392008-12-01 23:54:00 +00002162
Chris Lattnera448d752009-01-06 06:59:53 +00002163/// ParseOptionalTypeSpecifier - Try to parse a single type-specifier. We
Douglas Gregor450c75a2008-11-07 15:42:26 +00002164/// primarily follow the C++ grammar with additions for C99 and GNU,
2165/// which together subsume the C grammar. Note that the C++
2166/// type-specifier also includes the C type-qualifier (for const,
2167/// volatile, and C99 restrict). Returns true if a type-specifier was
2168/// found (and parsed), false otherwise.
2169///
2170/// type-specifier: [C++ 7.1.5]
2171/// simple-type-specifier
2172/// class-specifier
2173/// enum-specifier
2174/// elaborated-type-specifier [TODO]
2175/// cv-qualifier
2176///
2177/// cv-qualifier: [C++ 7.1.5.1]
2178/// 'const'
2179/// 'volatile'
2180/// [C99] 'restrict'
2181///
2182/// simple-type-specifier: [ C++ 7.1.5.2]
2183/// '::'[opt] nested-name-specifier[opt] type-name [TODO]
2184/// '::'[opt] nested-name-specifier 'template' template-id [TODO]
2185/// 'char'
2186/// 'wchar_t'
2187/// 'bool'
2188/// 'short'
2189/// 'int'
2190/// 'long'
2191/// 'signed'
2192/// 'unsigned'
2193/// 'float'
2194/// 'double'
2195/// 'void'
2196/// [C99] '_Bool'
2197/// [C99] '_Complex'
2198/// [C99] '_Imaginary' // Removed in TC2?
2199/// [GNU] '_Decimal32'
2200/// [GNU] '_Decimal64'
2201/// [GNU] '_Decimal128'
2202/// [GNU] typeof-specifier
2203/// [OBJC] class-name objc-protocol-refs[opt] [TODO]
2204/// [OBJC] typedef-name objc-protocol-refs[opt] [TODO]
Anders Carlsson74948d02009-06-24 17:47:40 +00002205/// [C++0x] 'decltype' ( expression )
John Thompson22334602010-02-05 00:12:22 +00002206/// [AltiVec] '__vector'
John McCall49bfce42009-08-03 20:12:06 +00002207bool Parser::ParseOptionalTypeSpecifier(DeclSpec &DS, bool& isInvalid,
Chris Lattnera448d752009-01-06 06:59:53 +00002208 const char *&PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00002209 unsigned &DiagID,
Sebastian Redl2b372722010-02-03 21:21:43 +00002210 const ParsedTemplateInfo &TemplateInfo,
2211 bool SuppressDeclarations) {
Douglas Gregor450c75a2008-11-07 15:42:26 +00002212 SourceLocation Loc = Tok.getLocation();
2213
2214 switch (Tok.getKind()) {
Chris Lattner020bab92009-01-04 23:41:41 +00002215 case tok::identifier: // foo::bar
Douglas Gregorb8eaf292010-04-15 23:40:53 +00002216 // If we already have a type specifier, this identifier is not a type.
2217 if (DS.getTypeSpecType() != DeclSpec::TST_unspecified ||
2218 DS.getTypeSpecWidth() != DeclSpec::TSW_unspecified ||
2219 DS.getTypeSpecSign() != DeclSpec::TSS_unspecified)
2220 return false;
John Thompson22334602010-02-05 00:12:22 +00002221 // Check for need to substitute AltiVec keyword tokens.
2222 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
2223 break;
2224 // Fall through.
Douglas Gregor333489b2009-03-27 23:10:48 +00002225 case tok::kw_typename: // typename foo::bar
Chris Lattner020bab92009-01-04 23:41:41 +00002226 // Annotate typenames and C++ scope specifiers. If we get one, just
2227 // recurse to handle whatever we get.
2228 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00002229 return true;
2230 if (Tok.is(tok::identifier))
2231 return false;
2232 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
2233 TemplateInfo, SuppressDeclarations);
Chris Lattner020bab92009-01-04 23:41:41 +00002234 case tok::coloncolon: // ::foo::bar
2235 if (NextToken().is(tok::kw_new) || // ::new
2236 NextToken().is(tok::kw_delete)) // ::delete
2237 return false;
Mike Stump11289f42009-09-09 15:08:12 +00002238
Chris Lattner020bab92009-01-04 23:41:41 +00002239 // Annotate typenames and C++ scope specifiers. If we get one, just
2240 // recurse to handle whatever we get.
2241 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00002242 return true;
2243 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
2244 TemplateInfo, SuppressDeclarations);
Mike Stump11289f42009-09-09 15:08:12 +00002245
Douglas Gregor450c75a2008-11-07 15:42:26 +00002246 // simple-type-specifier:
Chris Lattnera8a3f732009-01-06 05:06:21 +00002247 case tok::annot_typename: {
John McCallba7bf592010-08-24 05:47:05 +00002248 if (ParsedType T = getTypeAnnotation(Tok)) {
Nico Weber77430342010-11-22 10:30:56 +00002249 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename,
2250 Tok.getAnnotationEndLoc(), PrevSpec,
John McCallba7bf592010-08-24 05:47:05 +00002251 DiagID, T);
2252 } else
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00002253 DS.SetTypeSpecError();
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002254 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
2255 ConsumeToken(); // The typename
Mike Stump11289f42009-09-09 15:08:12 +00002256
Douglas Gregor450c75a2008-11-07 15:42:26 +00002257 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
2258 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
2259 // Objective-C interface. If we don't have Objective-C or a '<', this is
2260 // just a normal reference to a typedef name.
Douglas Gregor06e41ae2010-10-21 23:17:00 +00002261 if (Tok.is(tok::less) && getLang().ObjC1)
2262 ParseObjCProtocolQualifiers(DS);
2263
Douglas Gregor450c75a2008-11-07 15:42:26 +00002264 return true;
2265 }
2266
2267 case tok::kw_short:
John McCall49bfce42009-08-03 20:12:06 +00002268 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00002269 break;
2270 case tok::kw_long:
2271 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCall49bfce42009-08-03 20:12:06 +00002272 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
2273 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00002274 else
John McCall49bfce42009-08-03 20:12:06 +00002275 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2276 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00002277 break;
Francois Pichet84133e42011-04-28 01:59:37 +00002278 case tok::kw___int64:
2279 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2280 DiagID);
2281 break;
Douglas Gregor450c75a2008-11-07 15:42:26 +00002282 case tok::kw_signed:
John McCall49bfce42009-08-03 20:12:06 +00002283 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00002284 break;
2285 case tok::kw_unsigned:
John McCall49bfce42009-08-03 20:12:06 +00002286 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
2287 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00002288 break;
2289 case tok::kw__Complex:
John McCall49bfce42009-08-03 20:12:06 +00002290 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
2291 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00002292 break;
2293 case tok::kw__Imaginary:
John McCall49bfce42009-08-03 20:12:06 +00002294 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
2295 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00002296 break;
2297 case tok::kw_void:
John McCall49bfce42009-08-03 20:12:06 +00002298 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00002299 break;
2300 case tok::kw_char:
John McCall49bfce42009-08-03 20:12:06 +00002301 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00002302 break;
2303 case tok::kw_int:
John McCall49bfce42009-08-03 20:12:06 +00002304 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00002305 break;
2306 case tok::kw_float:
John McCall49bfce42009-08-03 20:12:06 +00002307 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00002308 break;
2309 case tok::kw_double:
John McCall49bfce42009-08-03 20:12:06 +00002310 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00002311 break;
2312 case tok::kw_wchar_t:
John McCall49bfce42009-08-03 20:12:06 +00002313 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00002314 break;
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00002315 case tok::kw_char16_t:
John McCall49bfce42009-08-03 20:12:06 +00002316 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00002317 break;
2318 case tok::kw_char32_t:
John McCall49bfce42009-08-03 20:12:06 +00002319 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00002320 break;
Douglas Gregor450c75a2008-11-07 15:42:26 +00002321 case tok::kw_bool:
2322 case tok::kw__Bool:
John McCall49bfce42009-08-03 20:12:06 +00002323 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00002324 break;
2325 case tok::kw__Decimal32:
John McCall49bfce42009-08-03 20:12:06 +00002326 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
2327 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00002328 break;
2329 case tok::kw__Decimal64:
John McCall49bfce42009-08-03 20:12:06 +00002330 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
2331 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00002332 break;
2333 case tok::kw__Decimal128:
John McCall49bfce42009-08-03 20:12:06 +00002334 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
2335 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00002336 break;
John Thompson22334602010-02-05 00:12:22 +00002337 case tok::kw___vector:
2338 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
2339 break;
2340 case tok::kw___pixel:
2341 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
2342 break;
2343
Douglas Gregor450c75a2008-11-07 15:42:26 +00002344 // class-specifier:
2345 case tok::kw_class:
2346 case tok::kw_struct:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00002347 case tok::kw_union: {
2348 tok::TokenKind Kind = Tok.getKind();
2349 ConsumeToken();
Sebastian Redl2b372722010-02-03 21:21:43 +00002350 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS_none,
2351 SuppressDeclarations);
Douglas Gregor450c75a2008-11-07 15:42:26 +00002352 return true;
Chris Lattnerffaa0e62009-04-12 21:49:30 +00002353 }
Douglas Gregor450c75a2008-11-07 15:42:26 +00002354
2355 // enum-specifier:
2356 case tok::kw_enum:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00002357 ConsumeToken();
Douglas Gregordc70c3a2010-03-02 17:53:14 +00002358 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS_none);
Douglas Gregor450c75a2008-11-07 15:42:26 +00002359 return true;
2360
2361 // cv-qualifier:
2362 case tok::kw_const:
2363 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00002364 DiagID, getLang());
Douglas Gregor450c75a2008-11-07 15:42:26 +00002365 break;
2366 case tok::kw_volatile:
2367 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00002368 DiagID, getLang());
Douglas Gregor450c75a2008-11-07 15:42:26 +00002369 break;
2370 case tok::kw_restrict:
2371 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00002372 DiagID, getLang());
Douglas Gregor450c75a2008-11-07 15:42:26 +00002373 break;
2374
2375 // GNU typeof support.
2376 case tok::kw_typeof:
2377 ParseTypeofSpecifier(DS);
2378 return true;
2379
Anders Carlsson74948d02009-06-24 17:47:40 +00002380 // C++0x decltype support.
2381 case tok::kw_decltype:
2382 ParseDecltypeSpecifier(DS);
2383 return true;
Mike Stump11289f42009-09-09 15:08:12 +00002384
Alexis Hunt4a257072011-05-19 05:37:45 +00002385 // C++0x type traits support.
2386 case tok::kw___underlying_type:
2387 ParseUnderlyingTypeSpecifier(DS);
2388 return true;
2389
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00002390 // OpenCL qualifiers:
2391 case tok::kw_private:
2392 if (!getLang().OpenCL)
2393 return false;
2394 case tok::kw___private:
2395 case tok::kw___global:
2396 case tok::kw___local:
2397 case tok::kw___constant:
2398 case tok::kw___read_only:
2399 case tok::kw___write_only:
2400 case tok::kw___read_write:
2401 ParseOpenCLQualifiers(DS);
2402 break;
2403
Anders Carlssonbae27372009-06-26 23:44:14 +00002404 // C++0x auto support.
2405 case tok::kw_auto:
Richard Smith50658642011-09-04 20:24:20 +00002406 // This is only called in situations where a storage-class specifier is
2407 // illegal, so we can assume an auto type specifier was intended even in
2408 // C++98. In C++98 mode, DeclSpec::Finish will produce an appropriate
2409 // extension diagnostic.
2410 if (!getLang().CPlusPlus)
Anders Carlssonbae27372009-06-26 23:44:14 +00002411 return false;
2412
John McCall49bfce42009-08-03 20:12:06 +00002413 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec, DiagID);
Anders Carlssonbae27372009-06-26 23:44:14 +00002414 break;
Dawn Perchik335e16b2010-09-03 01:29:35 +00002415
Eli Friedman53339e02009-06-08 23:27:34 +00002416 case tok::kw___ptr64:
Francois Pichetf2fb4112011-08-25 00:36:46 +00002417 case tok::kw___ptr32:
Eli Friedman53339e02009-06-08 23:27:34 +00002418 case tok::kw___w64:
Steve Naroff44ac7772008-12-25 14:16:32 +00002419 case tok::kw___cdecl:
2420 case tok::kw___stdcall:
2421 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00002422 case tok::kw___thiscall:
Francois Pichet17ed0202011-08-18 09:59:55 +00002423 case tok::kw___unaligned:
John McCall53fa7142010-12-24 02:08:15 +00002424 ParseMicrosoftTypeAttributes(DS.getAttributes());
Chris Lattner78ecd4f2009-01-21 19:19:26 +00002425 return true;
Steve Naroff44ac7772008-12-25 14:16:32 +00002426
Dawn Perchik335e16b2010-09-03 01:29:35 +00002427 case tok::kw___pascal:
John McCall53fa7142010-12-24 02:08:15 +00002428 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik335e16b2010-09-03 01:29:35 +00002429 return true;
2430
Douglas Gregor450c75a2008-11-07 15:42:26 +00002431 default:
2432 // Not a type-specifier; do nothing.
2433 return false;
2434 }
2435
2436 // If the specifier combination wasn't legal, issue a diagnostic.
2437 if (isInvalid) {
2438 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner6d29c102008-11-18 07:48:38 +00002439 // Pick between error or extwarn.
Chris Lattner6d29c102008-11-18 07:48:38 +00002440 Diag(Tok, DiagID) << PrevSpec;
Douglas Gregor450c75a2008-11-07 15:42:26 +00002441 }
2442 DS.SetRangeEnd(Tok.getLocation());
2443 ConsumeToken(); // whatever we parsed above.
2444 return true;
2445}
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002446
Chris Lattner70ae4912007-10-29 04:42:53 +00002447/// ParseStructDeclaration - Parse a struct declaration without the terminating
2448/// semicolon.
2449///
Chris Lattner90a26b02007-01-23 04:38:16 +00002450/// struct-declaration:
Chris Lattner70ae4912007-10-29 04:42:53 +00002451/// specifier-qualifier-list struct-declarator-list
Chris Lattner736ed5d2007-06-09 05:59:07 +00002452/// [GNU] __extension__ struct-declaration
Chris Lattner70ae4912007-10-29 04:42:53 +00002453/// [GNU] specifier-qualifier-list
Chris Lattner90a26b02007-01-23 04:38:16 +00002454/// struct-declarator-list:
2455/// struct-declarator
2456/// struct-declarator-list ',' struct-declarator
2457/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
2458/// struct-declarator:
2459/// declarator
2460/// [GNU] declarator attributes[opt]
2461/// declarator[opt] ':' constant-expression
2462/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
2463///
Chris Lattnera12405b2008-04-10 06:46:29 +00002464void Parser::
John McCallcfefb6d2009-11-03 02:38:08 +00002465ParseStructDeclaration(DeclSpec &DS, FieldCallback &Fields) {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00002466
Chris Lattnerf02ef3e2008-10-20 06:45:43 +00002467 if (Tok.is(tok::kw___extension__)) {
2468 // __extension__ silences extension warnings in the subexpression.
2469 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroff97170802007-08-20 22:28:22 +00002470 ConsumeToken();
Chris Lattnerf02ef3e2008-10-20 06:45:43 +00002471 return ParseStructDeclaration(DS, Fields);
2472 }
Mike Stump11289f42009-09-09 15:08:12 +00002473
Steve Naroff97170802007-08-20 22:28:22 +00002474 // Parse the common specifier-qualifiers-list piece.
Steve Naroff97170802007-08-20 22:28:22 +00002475 ParseSpecifierQualifierList(DS);
Mike Stump11289f42009-09-09 15:08:12 +00002476
Douglas Gregorc6f58fe2009-01-12 22:49:06 +00002477 // If there are no declarators, this is a free-standing declaration
2478 // specifier. Let the actions module cope with it.
Chris Lattner76c72282007-10-09 17:33:22 +00002479 if (Tok.is(tok::semi)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00002480 Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none, DS);
Steve Naroff97170802007-08-20 22:28:22 +00002481 return;
2482 }
2483
2484 // Read struct-declarators until we find the semicolon.
John McCallcfefb6d2009-11-03 02:38:08 +00002485 bool FirstDeclarator = true;
Steve Naroff97170802007-08-20 22:28:22 +00002486 while (1) {
John McCall28a6aea2009-11-04 02:18:39 +00002487 ParsingDeclRAIIObject PD(*this);
John McCallcfefb6d2009-11-03 02:38:08 +00002488 FieldDeclarator DeclaratorInfo(DS);
2489
2490 // Attributes are only allowed here on successive declarators.
John McCall53fa7142010-12-24 02:08:15 +00002491 if (!FirstDeclarator)
2492 MaybeParseGNUAttributes(DeclaratorInfo.D);
Mike Stump11289f42009-09-09 15:08:12 +00002493
Steve Naroff97170802007-08-20 22:28:22 +00002494 /// struct-declarator: declarator
2495 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattner17c3b1f2009-12-10 01:59:24 +00002496 if (Tok.isNot(tok::colon)) {
2497 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
2498 ColonProtectionRAIIObject X(*this);
Chris Lattnera12405b2008-04-10 06:46:29 +00002499 ParseDeclarator(DeclaratorInfo.D);
Chris Lattner17c3b1f2009-12-10 01:59:24 +00002500 }
Mike Stump11289f42009-09-09 15:08:12 +00002501
Chris Lattner76c72282007-10-09 17:33:22 +00002502 if (Tok.is(tok::colon)) {
Steve Naroff97170802007-08-20 22:28:22 +00002503 ConsumeToken();
John McCalldadc5752010-08-24 06:29:42 +00002504 ExprResult Res(ParseConstantExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002505 if (Res.isInvalid())
Steve Naroff97170802007-08-20 22:28:22 +00002506 SkipUntil(tok::semi, true, true);
Chris Lattner32295d32008-04-10 06:15:14 +00002507 else
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00002508 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroff97170802007-08-20 22:28:22 +00002509 }
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002510
Steve Naroff97170802007-08-20 22:28:22 +00002511 // If attributes exist after the declarator, parse them.
John McCall53fa7142010-12-24 02:08:15 +00002512 MaybeParseGNUAttributes(DeclaratorInfo.D);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002513
John McCallcfefb6d2009-11-03 02:38:08 +00002514 // We're done with this declarator; invoke the callback.
John McCall48871652010-08-21 09:40:31 +00002515 Decl *D = Fields.invoke(DeclaratorInfo);
John McCall28a6aea2009-11-04 02:18:39 +00002516 PD.complete(D);
John McCallcfefb6d2009-11-03 02:38:08 +00002517
Steve Naroff97170802007-08-20 22:28:22 +00002518 // If we don't have a comma, it is either the end of the list (a ';')
2519 // or an error, bail out.
Chris Lattner76c72282007-10-09 17:33:22 +00002520 if (Tok.isNot(tok::comma))
Chris Lattner70ae4912007-10-29 04:42:53 +00002521 return;
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002522
Steve Naroff97170802007-08-20 22:28:22 +00002523 // Consume the comma.
2524 ConsumeToken();
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002525
John McCallcfefb6d2009-11-03 02:38:08 +00002526 FirstDeclarator = false;
Steve Naroff97170802007-08-20 22:28:22 +00002527 }
Steve Naroff97170802007-08-20 22:28:22 +00002528}
2529
2530/// ParseStructUnionBody
2531/// struct-contents:
2532/// struct-declaration-list
2533/// [EXT] empty
2534/// [GNU] "struct-declaration-list" without terminatoring ';'
2535/// struct-declaration-list:
2536/// struct-declaration
2537/// struct-declaration-list struct-declaration
Chris Lattner535b8302008-06-21 19:39:06 +00002538/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroff97170802007-08-20 22:28:22 +00002539///
Chris Lattner1300fb92007-01-23 23:42:53 +00002540void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
John McCall48871652010-08-21 09:40:31 +00002541 unsigned TagType, Decl *TagDecl) {
John McCallfaf5fb42010-08-26 23:41:50 +00002542 PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc,
2543 "parsing struct/union body");
Mike Stump11289f42009-09-09 15:08:12 +00002544
Chris Lattner90a26b02007-01-23 04:38:16 +00002545 SourceLocation LBraceLoc = ConsumeBrace();
Mike Stump11289f42009-09-09 15:08:12 +00002546
Douglas Gregor658b9552009-01-09 22:42:13 +00002547 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
Douglas Gregor0be31a22010-07-02 17:43:08 +00002548 Actions.ActOnTagStartDefinition(getCurScope(), TagDecl);
Douglas Gregor82ac25e2009-01-08 20:45:30 +00002549
Chris Lattner7b9ace62007-01-23 20:11:08 +00002550 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
2551 // C++.
Douglas Gregor556877c2008-04-13 21:30:24 +00002552 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Douglas Gregorda2955e2010-07-29 14:29:34 +00002553 Diag(Tok, diag::ext_empty_struct_union)
2554 << (TagType == TST_union);
Chris Lattner7b9ace62007-01-23 20:11:08 +00002555
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002556 SmallVector<Decl *, 32> FieldDecls;
Chris Lattnera12405b2008-04-10 06:46:29 +00002557
Chris Lattner7b9ace62007-01-23 20:11:08 +00002558 // While we still have something to read, read the declarations in the struct.
Chris Lattner76c72282007-10-09 17:33:22 +00002559 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Chris Lattner90a26b02007-01-23 04:38:16 +00002560 // Each iteration of this loop reads one struct-declaration.
Mike Stump11289f42009-09-09 15:08:12 +00002561
Chris Lattner736ed5d2007-06-09 05:59:07 +00002562 // Check for extraneous top-level semicolon.
Chris Lattner76c72282007-10-09 17:33:22 +00002563 if (Tok.is(tok::semi)) {
Douglas Gregore3e01a22009-04-01 22:41:11 +00002564 Diag(Tok, diag::ext_extra_struct_semi)
Douglas Gregor13d05682010-06-16 23:08:59 +00002565 << DeclSpec::getSpecifierName((DeclSpec::TST)TagType)
Douglas Gregora771f462010-03-31 17:46:05 +00002566 << FixItHint::CreateRemoval(Tok.getLocation());
Chris Lattner36e46a22007-06-09 05:49:55 +00002567 ConsumeToken();
2568 continue;
2569 }
Chris Lattnera12405b2008-04-10 06:46:29 +00002570
2571 // Parse all the comma separated declarators.
John McCall084e83d2011-03-24 11:26:52 +00002572 DeclSpec DS(AttrFactory);
Mike Stump11289f42009-09-09 15:08:12 +00002573
John McCallcfefb6d2009-11-03 02:38:08 +00002574 if (!Tok.is(tok::at)) {
2575 struct CFieldCallback : FieldCallback {
2576 Parser &P;
John McCall48871652010-08-21 09:40:31 +00002577 Decl *TagDecl;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002578 SmallVectorImpl<Decl *> &FieldDecls;
John McCallcfefb6d2009-11-03 02:38:08 +00002579
John McCall48871652010-08-21 09:40:31 +00002580 CFieldCallback(Parser &P, Decl *TagDecl,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002581 SmallVectorImpl<Decl *> &FieldDecls) :
John McCallcfefb6d2009-11-03 02:38:08 +00002582 P(P), TagDecl(TagDecl), FieldDecls(FieldDecls) {}
2583
John McCall48871652010-08-21 09:40:31 +00002584 virtual Decl *invoke(FieldDeclarator &FD) {
John McCallcfefb6d2009-11-03 02:38:08 +00002585 // Install the declarator into the current TagDecl.
John McCall48871652010-08-21 09:40:31 +00002586 Decl *Field = P.Actions.ActOnField(P.getCurScope(), TagDecl,
John McCall5e6253b2009-11-03 21:13:47 +00002587 FD.D.getDeclSpec().getSourceRange().getBegin(),
2588 FD.D, FD.BitfieldSize);
John McCallcfefb6d2009-11-03 02:38:08 +00002589 FieldDecls.push_back(Field);
2590 return Field;
Douglas Gregor66a985d2009-08-26 14:27:30 +00002591 }
John McCallcfefb6d2009-11-03 02:38:08 +00002592 } Callback(*this, TagDecl, FieldDecls);
2593
2594 ParseStructDeclaration(DS, Callback);
Chris Lattner535b8302008-06-21 19:39:06 +00002595 } else { // Handle @defs
2596 ConsumeToken();
2597 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
2598 Diag(Tok, diag::err_unexpected_at);
Chris Lattner245c5332010-02-02 00:37:27 +00002599 SkipUntil(tok::semi, true);
Chris Lattner535b8302008-06-21 19:39:06 +00002600 continue;
2601 }
2602 ConsumeToken();
2603 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
2604 if (!Tok.is(tok::identifier)) {
2605 Diag(Tok, diag::err_expected_ident);
Chris Lattner245c5332010-02-02 00:37:27 +00002606 SkipUntil(tok::semi, true);
Chris Lattner535b8302008-06-21 19:39:06 +00002607 continue;
2608 }
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002609 SmallVector<Decl *, 16> Fields;
Douglas Gregor0be31a22010-07-02 17:43:08 +00002610 Actions.ActOnDefs(getCurScope(), TagDecl, Tok.getLocation(),
Douglas Gregor91f84212008-12-11 16:49:14 +00002611 Tok.getIdentifierInfo(), Fields);
Chris Lattner535b8302008-06-21 19:39:06 +00002612 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
2613 ConsumeToken();
2614 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
Mike Stump11289f42009-09-09 15:08:12 +00002615 }
Chris Lattner736ed5d2007-06-09 05:59:07 +00002616
Chris Lattner76c72282007-10-09 17:33:22 +00002617 if (Tok.is(tok::semi)) {
Chris Lattner90a26b02007-01-23 04:38:16 +00002618 ConsumeToken();
Chris Lattner76c72282007-10-09 17:33:22 +00002619 } else if (Tok.is(tok::r_brace)) {
Chris Lattner245c5332010-02-02 00:37:27 +00002620 ExpectAndConsume(tok::semi, diag::ext_expected_semi_decl_list);
Chris Lattner0c7e82d2007-06-09 05:54:40 +00002621 break;
Chris Lattner90a26b02007-01-23 04:38:16 +00002622 } else {
Chris Lattner245c5332010-02-02 00:37:27 +00002623 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
2624 // Skip to end of block or statement to avoid ext-warning on extra ';'.
Chris Lattner90a26b02007-01-23 04:38:16 +00002625 SkipUntil(tok::r_brace, true, true);
Chris Lattner245c5332010-02-02 00:37:27 +00002626 // If we stopped at a ';', eat it.
2627 if (Tok.is(tok::semi)) ConsumeToken();
Chris Lattner90a26b02007-01-23 04:38:16 +00002628 }
2629 }
Mike Stump11289f42009-09-09 15:08:12 +00002630
Steve Naroff33a1e802007-10-29 21:38:07 +00002631 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Mike Stump11289f42009-09-09 15:08:12 +00002632
John McCall084e83d2011-03-24 11:26:52 +00002633 ParsedAttributes attrs(AttrFactory);
Chris Lattner90a26b02007-01-23 04:38:16 +00002634 // If attributes exist after struct contents, parse them.
John McCall53fa7142010-12-24 02:08:15 +00002635 MaybeParseGNUAttributes(attrs);
Daniel Dunbar15619c72008-10-03 02:03:53 +00002636
Douglas Gregor0be31a22010-07-02 17:43:08 +00002637 Actions.ActOnFields(getCurScope(),
Jay Foad7d0479f2009-05-21 09:52:38 +00002638 RecordLoc, TagDecl, FieldDecls.data(), FieldDecls.size(),
Daniel Dunbar15619c72008-10-03 02:03:53 +00002639 LBraceLoc, RBraceLoc,
John McCall53fa7142010-12-24 02:08:15 +00002640 attrs.getList());
Douglas Gregor82ac25e2009-01-08 20:45:30 +00002641 StructScope.Exit();
Douglas Gregor0be31a22010-07-02 17:43:08 +00002642 Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl, RBraceLoc);
Chris Lattner90a26b02007-01-23 04:38:16 +00002643}
2644
Chris Lattner3b561a32006-08-13 00:12:11 +00002645/// ParseEnumSpecifier
Chris Lattner1890ac82006-08-13 01:16:23 +00002646/// enum-specifier: [C99 6.7.2.2]
Chris Lattner3b561a32006-08-13 00:12:11 +00002647/// 'enum' identifier[opt] '{' enumerator-list '}'
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002648///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Chris Lattnere37e2332006-08-15 04:50:22 +00002649/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
2650/// '}' attributes[opt]
Chris Lattner3b561a32006-08-13 00:12:11 +00002651/// 'enum' identifier
Chris Lattnere37e2332006-08-15 04:50:22 +00002652/// [GNU] 'enum' attributes[opt] identifier
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002653///
Douglas Gregor0bf31402010-10-08 23:50:27 +00002654/// [C++0x] enum-head '{' enumerator-list[opt] '}'
2655/// [C++0x] enum-head '{' enumerator-list ',' '}'
2656///
2657/// enum-head: [C++0x]
2658/// enum-key attributes[opt] identifier[opt] enum-base[opt]
2659/// enum-key attributes[opt] nested-name-specifier identifier enum-base[opt]
2660///
2661/// enum-key: [C++0x]
2662/// 'enum'
2663/// 'enum' 'class'
2664/// 'enum' 'struct'
2665///
2666/// enum-base: [C++0x]
2667/// ':' type-specifier-seq
2668///
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002669/// [C++] elaborated-type-specifier:
2670/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
2671///
Chris Lattnerffaa0e62009-04-12 21:49:30 +00002672void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregordc70c3a2010-03-02 17:53:14 +00002673 const ParsedTemplateInfo &TemplateInfo,
Chris Lattnerffaa0e62009-04-12 21:49:30 +00002674 AccessSpecifier AS) {
Chris Lattnerffbc2712007-01-25 06:05:38 +00002675 // Parse the tag portion of this.
Douglas Gregorf45b0cf2009-09-18 15:37:17 +00002676 if (Tok.is(tok::code_completion)) {
2677 // Code completion for an enum name.
Douglas Gregor0be31a22010-07-02 17:43:08 +00002678 Actions.CodeCompleteTag(getCurScope(), DeclSpec::TST_enum);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002679 return cutOffParsing();
Douglas Gregorf45b0cf2009-09-18 15:37:17 +00002680 }
John McCallcb432fa2011-07-06 05:58:41 +00002681
2682 bool IsScopedEnum = false;
2683 bool IsScopedUsingClassTag = false;
2684
2685 if (getLang().CPlusPlus0x &&
2686 (Tok.is(tok::kw_class) || Tok.is(tok::kw_struct))) {
2687 IsScopedEnum = true;
2688 IsScopedUsingClassTag = Tok.is(tok::kw_class);
2689 ConsumeToken();
2690 }
Douglas Gregorf45b0cf2009-09-18 15:37:17 +00002691
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00002692 // If attributes exist after tag, parse them.
John McCall084e83d2011-03-24 11:26:52 +00002693 ParsedAttributes attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00002694 MaybeParseGNUAttributes(attrs);
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002695
Douglas Gregor8b7d4032011-09-08 17:18:35 +00002696 bool AllowFixedUnderlyingType
2697 = getLang().CPlusPlus0x || getLang().Microsoft || getLang().ObjC2;
John McCallcb432fa2011-07-06 05:58:41 +00002698
Abramo Bagnarad7548482010-05-19 21:37:53 +00002699 CXXScopeSpec &SS = DS.getTypeSpecScope();
John McCall1f476a12010-02-26 08:45:28 +00002700 if (getLang().CPlusPlus) {
John McCallcb432fa2011-07-06 05:58:41 +00002701 // "enum foo : bar;" is not a potential typo for "enum foo::bar;"
2702 // if a fixed underlying type is allowed.
2703 ColonProtectionRAIIObject X(*this, AllowFixedUnderlyingType);
2704
John McCallba7bf592010-08-24 05:47:05 +00002705 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), false))
John McCall1f476a12010-02-26 08:45:28 +00002706 return;
2707
2708 if (SS.isSet() && Tok.isNot(tok::identifier)) {
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002709 Diag(Tok, diag::err_expected_ident);
2710 if (Tok.isNot(tok::l_brace)) {
2711 // Has no name and is not a definition.
2712 // Skip the rest of this declarator, up until the comma or semicolon.
2713 SkipUntil(tok::comma, true);
2714 return;
2715 }
2716 }
2717 }
Mike Stump11289f42009-09-09 15:08:12 +00002718
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00002719 // Must have either 'enum name' or 'enum {...}'.
Douglas Gregor6cd5ae42011-02-22 02:55:24 +00002720 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace) &&
2721 (AllowFixedUnderlyingType && Tok.isNot(tok::colon))) {
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00002722 Diag(Tok, diag::err_expected_ident_lbrace);
Mike Stump11289f42009-09-09 15:08:12 +00002723
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00002724 // Skip the rest of this declarator, up until the comma or semicolon.
2725 SkipUntil(tok::comma, true);
Chris Lattner3b561a32006-08-13 00:12:11 +00002726 return;
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00002727 }
Mike Stump11289f42009-09-09 15:08:12 +00002728
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00002729 // If an identifier is present, consume and remember it.
2730 IdentifierInfo *Name = 0;
2731 SourceLocation NameLoc;
2732 if (Tok.is(tok::identifier)) {
2733 Name = Tok.getIdentifierInfo();
2734 NameLoc = ConsumeToken();
2735 }
Mike Stump11289f42009-09-09 15:08:12 +00002736
Douglas Gregor0bf31402010-10-08 23:50:27 +00002737 if (!Name && IsScopedEnum) {
2738 // C++0x 7.2p2: The optional identifier shall not be omitted in the
2739 // declaration of a scoped enumeration.
2740 Diag(Tok, diag::err_scoped_enum_missing_identifier);
2741 IsScopedEnum = false;
Abramo Bagnara0e05e242010-12-03 18:54:17 +00002742 IsScopedUsingClassTag = false;
Douglas Gregor0bf31402010-10-08 23:50:27 +00002743 }
2744
2745 TypeResult BaseType;
2746
Douglas Gregord1f69f62010-12-01 17:42:47 +00002747 // Parse the fixed underlying type.
Douglas Gregor6cd5ae42011-02-22 02:55:24 +00002748 if (AllowFixedUnderlyingType && Tok.is(tok::colon)) {
Douglas Gregord1f69f62010-12-01 17:42:47 +00002749 bool PossibleBitfield = false;
2750 if (getCurScope()->getFlags() & Scope::ClassScope) {
2751 // If we're in class scope, this can either be an enum declaration with
2752 // an underlying type, or a declaration of a bitfield member. We try to
2753 // use a simple disambiguation scheme first to catch the common cases
2754 // (integer literal, sizeof); if it's still ambiguous, we then consider
2755 // anything that's a simple-type-specifier followed by '(' as an
2756 // expression. This suffices because function types are not valid
2757 // underlying types anyway.
2758 TPResult TPR = isExpressionOrTypeSpecifierSimple(NextToken().getKind());
2759 // If the next token starts an expression, we know we're parsing a
2760 // bit-field. This is the common case.
2761 if (TPR == TPResult::True())
2762 PossibleBitfield = true;
2763 // If the next token starts a type-specifier-seq, it may be either a
2764 // a fixed underlying type or the start of a function-style cast in C++;
2765 // lookahead one more token to see if it's obvious that we have a
2766 // fixed underlying type.
2767 else if (TPR == TPResult::False() &&
2768 GetLookAheadToken(2).getKind() == tok::semi) {
2769 // Consume the ':'.
2770 ConsumeToken();
2771 } else {
2772 // We have the start of a type-specifier-seq, so we have to perform
2773 // tentative parsing to determine whether we have an expression or a
2774 // type.
2775 TentativeParsingAction TPA(*this);
2776
2777 // Consume the ':'.
2778 ConsumeToken();
2779
Douglas Gregora1aec292011-02-22 20:32:04 +00002780 if ((getLang().CPlusPlus &&
2781 isCXXDeclarationSpecifier() != TPResult::True()) ||
2782 (!getLang().CPlusPlus && !isDeclarationSpecifier(true))) {
Douglas Gregord1f69f62010-12-01 17:42:47 +00002783 // We'll parse this as a bitfield later.
2784 PossibleBitfield = true;
2785 TPA.Revert();
2786 } else {
2787 // We have a type-specifier-seq.
2788 TPA.Commit();
2789 }
2790 }
2791 } else {
2792 // Consume the ':'.
2793 ConsumeToken();
2794 }
2795
2796 if (!PossibleBitfield) {
2797 SourceRange Range;
2798 BaseType = ParseTypeName(&Range);
Douglas Gregora1aec292011-02-22 20:32:04 +00002799
Douglas Gregor8b7d4032011-09-08 17:18:35 +00002800 if (!getLang().CPlusPlus0x && !getLang().ObjC2)
Douglas Gregora1aec292011-02-22 20:32:04 +00002801 Diag(StartLoc, diag::ext_ms_enum_fixed_underlying_type)
2802 << Range;
Douglas Gregord1f69f62010-12-01 17:42:47 +00002803 }
Douglas Gregor0bf31402010-10-08 23:50:27 +00002804 }
2805
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00002806 // There are three options here. If we have 'enum foo;', then this is a
2807 // forward declaration. If we have 'enum foo {...' then this is a
2808 // definition. Otherwise we have something like 'enum foo xyz', a reference.
2809 //
2810 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
2811 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
2812 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
2813 //
John McCallfaf5fb42010-08-26 23:41:50 +00002814 Sema::TagUseKind TUK;
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00002815 if (Tok.is(tok::l_brace))
John McCallfaf5fb42010-08-26 23:41:50 +00002816 TUK = Sema::TUK_Definition;
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00002817 else if (Tok.is(tok::semi))
John McCallfaf5fb42010-08-26 23:41:50 +00002818 TUK = Sema::TUK_Declaration;
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00002819 else
John McCallfaf5fb42010-08-26 23:41:50 +00002820 TUK = Sema::TUK_Reference;
Douglas Gregorcbbf3e32010-05-03 17:48:54 +00002821
2822 // enums cannot be templates, although they can be referenced from a
2823 // template.
2824 if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate &&
John McCallfaf5fb42010-08-26 23:41:50 +00002825 TUK != Sema::TUK_Reference) {
Douglas Gregorcbbf3e32010-05-03 17:48:54 +00002826 Diag(Tok, diag::err_enum_template);
2827
2828 // Skip the rest of this declarator, up until the comma or semicolon.
2829 SkipUntil(tok::comma, true);
2830 return;
2831 }
2832
Douglas Gregor6cd5ae42011-02-22 02:55:24 +00002833 if (!Name && TUK != Sema::TUK_Definition) {
2834 Diag(Tok, diag::err_enumerator_unnamed_no_def);
2835
2836 // Skip the rest of this declarator, up until the comma or semicolon.
2837 SkipUntil(tok::comma, true);
2838 return;
2839 }
2840
Douglas Gregord6ab8742009-05-28 23:31:59 +00002841 bool Owned = false;
John McCall7f41d982009-09-11 04:59:25 +00002842 bool IsDependent = false;
Douglas Gregorba41d012010-04-24 16:38:41 +00002843 const char *PrevSpec = 0;
2844 unsigned DiagID;
John McCall48871652010-08-21 09:40:31 +00002845 Decl *TagDecl = Actions.ActOnTag(getCurScope(), DeclSpec::TST_enum, TUK,
John McCall53fa7142010-12-24 02:08:15 +00002846 StartLoc, SS, Name, NameLoc, attrs.getList(),
John McCall48871652010-08-21 09:40:31 +00002847 AS,
John McCallfaf5fb42010-08-26 23:41:50 +00002848 MultiTemplateParamsArg(Actions),
Douglas Gregor0bf31402010-10-08 23:50:27 +00002849 Owned, IsDependent, IsScopedEnum,
Abramo Bagnara0e05e242010-12-03 18:54:17 +00002850 IsScopedUsingClassTag, BaseType);
Douglas Gregor0bf31402010-10-08 23:50:27 +00002851
Douglas Gregorba41d012010-04-24 16:38:41 +00002852 if (IsDependent) {
2853 // This enum has a dependent nested-name-specifier. Handle it as a
2854 // dependent tag.
2855 if (!Name) {
2856 DS.SetTypeSpecError();
2857 Diag(Tok, diag::err_expected_type_name_after_typename);
2858 return;
2859 }
2860
Douglas Gregor0be31a22010-07-02 17:43:08 +00002861 TypeResult Type = Actions.ActOnDependentTag(getCurScope(), DeclSpec::TST_enum,
Douglas Gregorba41d012010-04-24 16:38:41 +00002862 TUK, SS, Name, StartLoc,
2863 NameLoc);
2864 if (Type.isInvalid()) {
2865 DS.SetTypeSpecError();
2866 return;
2867 }
2868
Abramo Bagnara9875a3c2011-03-16 20:16:18 +00002869 if (DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc,
2870 NameLoc.isValid() ? NameLoc : StartLoc,
2871 PrevSpec, DiagID, Type.get()))
Douglas Gregorba41d012010-04-24 16:38:41 +00002872 Diag(StartLoc, DiagID) << PrevSpec;
2873
2874 return;
2875 }
Mike Stump11289f42009-09-09 15:08:12 +00002876
John McCall48871652010-08-21 09:40:31 +00002877 if (!TagDecl) {
Douglas Gregorba41d012010-04-24 16:38:41 +00002878 // The action failed to produce an enumeration tag. If this is a
2879 // definition, consume the entire definition.
2880 if (Tok.is(tok::l_brace)) {
2881 ConsumeBrace();
2882 SkipUntil(tok::r_brace);
2883 }
2884
2885 DS.SetTypeSpecError();
2886 return;
2887 }
2888
Chris Lattner76c72282007-10-09 17:33:22 +00002889 if (Tok.is(tok::l_brace))
Chris Lattnerc1915e22007-01-25 07:29:02 +00002890 ParseEnumBody(StartLoc, TagDecl);
Mike Stump11289f42009-09-09 15:08:12 +00002891
Abramo Bagnara9875a3c2011-03-16 20:16:18 +00002892 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc,
2893 NameLoc.isValid() ? NameLoc : StartLoc,
2894 PrevSpec, DiagID, TagDecl, Owned))
John McCall49bfce42009-08-03 20:12:06 +00002895 Diag(StartLoc, DiagID) << PrevSpec;
Chris Lattner3b561a32006-08-13 00:12:11 +00002896}
2897
Chris Lattnerc1915e22007-01-25 07:29:02 +00002898/// ParseEnumBody - Parse a {} enclosed enumerator-list.
2899/// enumerator-list:
2900/// enumerator
2901/// enumerator-list ',' enumerator
2902/// enumerator:
2903/// enumeration-constant
2904/// enumeration-constant '=' constant-expression
2905/// enumeration-constant:
2906/// identifier
2907///
John McCall48871652010-08-21 09:40:31 +00002908void Parser::ParseEnumBody(SourceLocation StartLoc, Decl *EnumDecl) {
Douglas Gregor07665a62009-01-05 19:45:36 +00002909 // Enter the scope of the enum body and start the definition.
2910 ParseScope EnumScope(this, Scope::DeclScope);
Douglas Gregor0be31a22010-07-02 17:43:08 +00002911 Actions.ActOnTagStartDefinition(getCurScope(), EnumDecl);
Douglas Gregor07665a62009-01-05 19:45:36 +00002912
Chris Lattnerc1915e22007-01-25 07:29:02 +00002913 SourceLocation LBraceLoc = ConsumeBrace();
Mike Stump11289f42009-09-09 15:08:12 +00002914
Chris Lattner37256fb2007-08-27 17:24:30 +00002915 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
Chris Lattner76c72282007-10-09 17:33:22 +00002916 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Fariborz Jahanian6e814922010-05-28 22:23:22 +00002917 Diag(Tok, diag::error_empty_enum);
Mike Stump11289f42009-09-09 15:08:12 +00002918
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002919 SmallVector<Decl *, 32> EnumConstantDecls;
Chris Lattnerc1915e22007-01-25 07:29:02 +00002920
John McCall48871652010-08-21 09:40:31 +00002921 Decl *LastEnumConstDecl = 0;
Mike Stump11289f42009-09-09 15:08:12 +00002922
Chris Lattnerc1915e22007-01-25 07:29:02 +00002923 // Parse the enumerator-list.
Chris Lattner76c72282007-10-09 17:33:22 +00002924 while (Tok.is(tok::identifier)) {
Chris Lattnerc1915e22007-01-25 07:29:02 +00002925 IdentifierInfo *Ident = Tok.getIdentifierInfo();
2926 SourceLocation IdentLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00002927
John McCall811a0f52010-10-22 23:36:17 +00002928 // If attributes exist after the enumerator, parse them.
John McCall084e83d2011-03-24 11:26:52 +00002929 ParsedAttributes attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00002930 MaybeParseGNUAttributes(attrs);
John McCall811a0f52010-10-22 23:36:17 +00002931
Chris Lattnerc1915e22007-01-25 07:29:02 +00002932 SourceLocation EqualLoc;
John McCalldadc5752010-08-24 06:29:42 +00002933 ExprResult AssignedVal;
Chris Lattner76c72282007-10-09 17:33:22 +00002934 if (Tok.is(tok::equal)) {
Chris Lattnerc1915e22007-01-25 07:29:02 +00002935 EqualLoc = ConsumeToken();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002936 AssignedVal = ParseConstantExpression();
2937 if (AssignedVal.isInvalid())
Chris Lattnerda6c2ce2007-04-27 19:13:15 +00002938 SkipUntil(tok::comma, tok::r_brace, true, true);
Chris Lattnerc1915e22007-01-25 07:29:02 +00002939 }
Mike Stump11289f42009-09-09 15:08:12 +00002940
Chris Lattnerc1915e22007-01-25 07:29:02 +00002941 // Install the enumerator constant into EnumDecl.
John McCall48871652010-08-21 09:40:31 +00002942 Decl *EnumConstDecl = Actions.ActOnEnumConstant(getCurScope(), EnumDecl,
2943 LastEnumConstDecl,
2944 IdentLoc, Ident,
John McCall53fa7142010-12-24 02:08:15 +00002945 attrs.getList(), EqualLoc,
John McCall48871652010-08-21 09:40:31 +00002946 AssignedVal.release());
Chris Lattner4ef40012007-06-11 01:28:17 +00002947 EnumConstantDecls.push_back(EnumConstDecl);
2948 LastEnumConstDecl = EnumConstDecl;
Mike Stump11289f42009-09-09 15:08:12 +00002949
Douglas Gregorce66d022010-09-07 14:51:08 +00002950 if (Tok.is(tok::identifier)) {
2951 // We're missing a comma between enumerators.
2952 SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation);
2953 Diag(Loc, diag::err_enumerator_list_missing_comma)
2954 << FixItHint::CreateInsertion(Loc, ", ");
2955 continue;
2956 }
2957
Chris Lattner76c72282007-10-09 17:33:22 +00002958 if (Tok.isNot(tok::comma))
Chris Lattnerc1915e22007-01-25 07:29:02 +00002959 break;
2960 SourceLocation CommaLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00002961
2962 if (Tok.isNot(tok::identifier) &&
Douglas Gregore3e01a22009-04-01 22:41:11 +00002963 !(getLang().C99 || getLang().CPlusPlus0x))
2964 Diag(CommaLoc, diag::ext_enumerator_list_comma)
2965 << getLang().CPlusPlus
Douglas Gregora771f462010-03-31 17:46:05 +00002966 << FixItHint::CreateRemoval(CommaLoc);
Chris Lattnerc1915e22007-01-25 07:29:02 +00002967 }
Mike Stump11289f42009-09-09 15:08:12 +00002968
Chris Lattnerc1915e22007-01-25 07:29:02 +00002969 // Eat the }.
Mike Stump6814d1c2009-05-16 07:06:02 +00002970 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Chris Lattnerc1915e22007-01-25 07:29:02 +00002971
Chris Lattnerc1915e22007-01-25 07:29:02 +00002972 // If attributes exist after the identifier list, parse them.
John McCall084e83d2011-03-24 11:26:52 +00002973 ParsedAttributes attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00002974 MaybeParseGNUAttributes(attrs);
Douglas Gregor82ac25e2009-01-08 20:45:30 +00002975
Edward O'Callaghanc69169d2009-08-08 14:36:57 +00002976 Actions.ActOnEnumBody(StartLoc, LBraceLoc, RBraceLoc, EnumDecl,
2977 EnumConstantDecls.data(), EnumConstantDecls.size(),
John McCall53fa7142010-12-24 02:08:15 +00002978 getCurScope(), attrs.getList());
Mike Stump11289f42009-09-09 15:08:12 +00002979
Douglas Gregor82ac25e2009-01-08 20:45:30 +00002980 EnumScope.Exit();
Douglas Gregor0be31a22010-07-02 17:43:08 +00002981 Actions.ActOnTagFinishDefinition(getCurScope(), EnumDecl, RBraceLoc);
Chris Lattnerc1915e22007-01-25 07:29:02 +00002982}
Chris Lattner3b561a32006-08-13 00:12:11 +00002983
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002984/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff69e8f9e2008-02-11 23:15:56 +00002985/// start of a type-qualifier-list.
2986bool Parser::isTypeQualifier() const {
2987 switch (Tok.getKind()) {
2988 default: return false;
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00002989
2990 // type-qualifier only in OpenCL
2991 case tok::kw_private:
2992 return getLang().OpenCL;
2993
Steve Naroff69e8f9e2008-02-11 23:15:56 +00002994 // type-qualifier
2995 case tok::kw_const:
2996 case tok::kw_volatile:
2997 case tok::kw_restrict:
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00002998 case tok::kw___private:
2999 case tok::kw___local:
3000 case tok::kw___global:
3001 case tok::kw___constant:
3002 case tok::kw___read_only:
3003 case tok::kw___read_write:
3004 case tok::kw___write_only:
Steve Naroff69e8f9e2008-02-11 23:15:56 +00003005 return true;
3006 }
3007}
3008
Chris Lattnerfd48afe2010-02-28 18:18:36 +00003009/// isKnownToBeTypeSpecifier - Return true if we know that the specified token
3010/// is definitely a type-specifier. Return false if it isn't part of a type
3011/// specifier or if we're not sure.
3012bool Parser::isKnownToBeTypeSpecifier(const Token &Tok) const {
3013 switch (Tok.getKind()) {
3014 default: return false;
3015 // type-specifiers
3016 case tok::kw_short:
3017 case tok::kw_long:
Francois Pichet84133e42011-04-28 01:59:37 +00003018 case tok::kw___int64:
Chris Lattnerfd48afe2010-02-28 18:18:36 +00003019 case tok::kw_signed:
3020 case tok::kw_unsigned:
3021 case tok::kw__Complex:
3022 case tok::kw__Imaginary:
3023 case tok::kw_void:
3024 case tok::kw_char:
3025 case tok::kw_wchar_t:
3026 case tok::kw_char16_t:
3027 case tok::kw_char32_t:
3028 case tok::kw_int:
3029 case tok::kw_float:
3030 case tok::kw_double:
3031 case tok::kw_bool:
3032 case tok::kw__Bool:
3033 case tok::kw__Decimal32:
3034 case tok::kw__Decimal64:
3035 case tok::kw__Decimal128:
3036 case tok::kw___vector:
3037
3038 // struct-or-union-specifier (C99) or class-specifier (C++)
3039 case tok::kw_class:
3040 case tok::kw_struct:
3041 case tok::kw_union:
3042 // enum-specifier
3043 case tok::kw_enum:
3044
3045 // typedef-name
3046 case tok::annot_typename:
3047 return true;
3048 }
3049}
3050
Steve Naroff69e8f9e2008-02-11 23:15:56 +00003051/// isTypeSpecifierQualifier - Return true if the current token could be the
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003052/// start of a specifier-qualifier-list.
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003053bool Parser::isTypeSpecifierQualifier() {
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003054 switch (Tok.getKind()) {
3055 default: return false;
Mike Stump11289f42009-09-09 15:08:12 +00003056
Chris Lattner020bab92009-01-04 23:41:41 +00003057 case tok::identifier: // foo::bar
John Thompson22334602010-02-05 00:12:22 +00003058 if (TryAltiVecVectorToken())
3059 return true;
3060 // Fall through.
Douglas Gregor333489b2009-03-27 23:10:48 +00003061 case tok::kw_typename: // typename T::type
Chris Lattner020bab92009-01-04 23:41:41 +00003062 // Annotate typenames and C++ scope specifiers. If we get one, just
3063 // recurse to handle whatever we get.
3064 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00003065 return true;
3066 if (Tok.is(tok::identifier))
3067 return false;
3068 return isTypeSpecifierQualifier();
Douglas Gregor333489b2009-03-27 23:10:48 +00003069
Chris Lattner020bab92009-01-04 23:41:41 +00003070 case tok::coloncolon: // ::foo::bar
3071 if (NextToken().is(tok::kw_new) || // ::new
3072 NextToken().is(tok::kw_delete)) // ::delete
3073 return false;
3074
Chris Lattner020bab92009-01-04 23:41:41 +00003075 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00003076 return true;
3077 return isTypeSpecifierQualifier();
Mike Stump11289f42009-09-09 15:08:12 +00003078
Chris Lattnere37e2332006-08-15 04:50:22 +00003079 // GNU attributes support.
3080 case tok::kw___attribute:
Steve Naroffad373bd2007-07-31 12:34:36 +00003081 // GNU typeof support.
3082 case tok::kw_typeof:
Mike Stump11289f42009-09-09 15:08:12 +00003083
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003084 // type-specifiers
3085 case tok::kw_short:
3086 case tok::kw_long:
Francois Pichet84133e42011-04-28 01:59:37 +00003087 case tok::kw___int64:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003088 case tok::kw_signed:
3089 case tok::kw_unsigned:
3090 case tok::kw__Complex:
3091 case tok::kw__Imaginary:
3092 case tok::kw_void:
3093 case tok::kw_char:
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +00003094 case tok::kw_wchar_t:
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00003095 case tok::kw_char16_t:
3096 case tok::kw_char32_t:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003097 case tok::kw_int:
3098 case tok::kw_float:
3099 case tok::kw_double:
Chris Lattnerbb31a422007-11-15 05:25:19 +00003100 case tok::kw_bool:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003101 case tok::kw__Bool:
3102 case tok::kw__Decimal32:
3103 case tok::kw__Decimal64:
3104 case tok::kw__Decimal128:
John Thompson22334602010-02-05 00:12:22 +00003105 case tok::kw___vector:
Mike Stump11289f42009-09-09 15:08:12 +00003106
Chris Lattner861a2262008-04-13 18:59:07 +00003107 // struct-or-union-specifier (C99) or class-specifier (C++)
3108 case tok::kw_class:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003109 case tok::kw_struct:
3110 case tok::kw_union:
3111 // enum-specifier
3112 case tok::kw_enum:
Mike Stump11289f42009-09-09 15:08:12 +00003113
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003114 // type-qualifier
3115 case tok::kw_const:
3116 case tok::kw_volatile:
3117 case tok::kw_restrict:
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003118
3119 // typedef-name
Chris Lattnera8a3f732009-01-06 05:06:21 +00003120 case tok::annot_typename:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003121 return true;
Mike Stump11289f42009-09-09 15:08:12 +00003122
Chris Lattner409bf7d2008-10-20 00:25:30 +00003123 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
3124 case tok::less:
3125 return getLang().ObjC1;
Mike Stump11289f42009-09-09 15:08:12 +00003126
Steve Naroff44ac7772008-12-25 14:16:32 +00003127 case tok::kw___cdecl:
3128 case tok::kw___stdcall:
3129 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00003130 case tok::kw___thiscall:
Eli Friedman53339e02009-06-08 23:27:34 +00003131 case tok::kw___w64:
3132 case tok::kw___ptr64:
Francois Pichetf2fb4112011-08-25 00:36:46 +00003133 case tok::kw___ptr32:
Dawn Perchik335e16b2010-09-03 01:29:35 +00003134 case tok::kw___pascal:
Francois Pichet17ed0202011-08-18 09:59:55 +00003135 case tok::kw___unaligned:
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003136
3137 case tok::kw___private:
3138 case tok::kw___local:
3139 case tok::kw___global:
3140 case tok::kw___constant:
3141 case tok::kw___read_only:
3142 case tok::kw___read_write:
3143 case tok::kw___write_only:
3144
Eli Friedman53339e02009-06-08 23:27:34 +00003145 return true;
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003146
3147 case tok::kw_private:
3148 return getLang().OpenCL;
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003149 }
3150}
3151
Chris Lattneracd58a32006-08-06 17:24:14 +00003152/// isDeclarationSpecifier() - Return true if the current token is part of a
3153/// declaration specifier.
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00003154///
3155/// \param DisambiguatingWithExpression True to indicate that the purpose of
3156/// this check is to disambiguate between an expression and a declaration.
3157bool Parser::isDeclarationSpecifier(bool DisambiguatingWithExpression) {
Chris Lattneracd58a32006-08-06 17:24:14 +00003158 switch (Tok.getKind()) {
3159 default: return false;
Mike Stump11289f42009-09-09 15:08:12 +00003160
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003161 case tok::kw_private:
3162 return getLang().OpenCL;
3163
Chris Lattner020bab92009-01-04 23:41:41 +00003164 case tok::identifier: // foo::bar
Steve Naroff9527bbf2009-03-09 21:12:44 +00003165 // Unfortunate hack to support "Class.factoryMethod" notation.
3166 if (getLang().ObjC1 && NextToken().is(tok::period))
3167 return false;
John Thompson22334602010-02-05 00:12:22 +00003168 if (TryAltiVecVectorToken())
3169 return true;
3170 // Fall through.
Douglas Gregor333489b2009-03-27 23:10:48 +00003171 case tok::kw_typename: // typename T::type
Chris Lattner020bab92009-01-04 23:41:41 +00003172 // Annotate typenames and C++ scope specifiers. If we get one, just
3173 // recurse to handle whatever we get.
3174 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00003175 return true;
3176 if (Tok.is(tok::identifier))
3177 return false;
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00003178
3179 // If we're in Objective-C and we have an Objective-C class type followed
3180 // by an identifier and then either ':' or ']', in a place where an
3181 // expression is permitted, then this is probably a class message send
3182 // missing the initial '['. In this case, we won't consider this to be
3183 // the start of a declaration.
3184 if (DisambiguatingWithExpression &&
3185 isStartOfObjCClassMessageMissingOpenBracket())
3186 return false;
3187
John McCall1f476a12010-02-26 08:45:28 +00003188 return isDeclarationSpecifier();
3189
Chris Lattner020bab92009-01-04 23:41:41 +00003190 case tok::coloncolon: // ::foo::bar
3191 if (NextToken().is(tok::kw_new) || // ::new
3192 NextToken().is(tok::kw_delete)) // ::delete
3193 return false;
Mike Stump11289f42009-09-09 15:08:12 +00003194
Chris Lattner020bab92009-01-04 23:41:41 +00003195 // Annotate typenames and C++ scope specifiers. If we get one, just
3196 // recurse to handle whatever we get.
3197 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00003198 return true;
3199 return isDeclarationSpecifier();
Mike Stump11289f42009-09-09 15:08:12 +00003200
Chris Lattneracd58a32006-08-06 17:24:14 +00003201 // storage-class-specifier
3202 case tok::kw_typedef:
3203 case tok::kw_extern:
Steve Naroff2050b0d2007-12-18 00:16:02 +00003204 case tok::kw___private_extern__:
Chris Lattneracd58a32006-08-06 17:24:14 +00003205 case tok::kw_static:
3206 case tok::kw_auto:
3207 case tok::kw_register:
3208 case tok::kw___thread:
Mike Stump11289f42009-09-09 15:08:12 +00003209
Chris Lattneracd58a32006-08-06 17:24:14 +00003210 // type-specifiers
3211 case tok::kw_short:
3212 case tok::kw_long:
Francois Pichet84133e42011-04-28 01:59:37 +00003213 case tok::kw___int64:
Chris Lattneracd58a32006-08-06 17:24:14 +00003214 case tok::kw_signed:
3215 case tok::kw_unsigned:
3216 case tok::kw__Complex:
3217 case tok::kw__Imaginary:
3218 case tok::kw_void:
3219 case tok::kw_char:
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +00003220 case tok::kw_wchar_t:
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00003221 case tok::kw_char16_t:
3222 case tok::kw_char32_t:
3223
Chris Lattneracd58a32006-08-06 17:24:14 +00003224 case tok::kw_int:
3225 case tok::kw_float:
3226 case tok::kw_double:
Chris Lattnerbb31a422007-11-15 05:25:19 +00003227 case tok::kw_bool:
Chris Lattneracd58a32006-08-06 17:24:14 +00003228 case tok::kw__Bool:
3229 case tok::kw__Decimal32:
3230 case tok::kw__Decimal64:
3231 case tok::kw__Decimal128:
John Thompson22334602010-02-05 00:12:22 +00003232 case tok::kw___vector:
Mike Stump11289f42009-09-09 15:08:12 +00003233
Chris Lattner861a2262008-04-13 18:59:07 +00003234 // struct-or-union-specifier (C99) or class-specifier (C++)
3235 case tok::kw_class:
Chris Lattneracd58a32006-08-06 17:24:14 +00003236 case tok::kw_struct:
3237 case tok::kw_union:
3238 // enum-specifier
3239 case tok::kw_enum:
Mike Stump11289f42009-09-09 15:08:12 +00003240
Chris Lattneracd58a32006-08-06 17:24:14 +00003241 // type-qualifier
3242 case tok::kw_const:
3243 case tok::kw_volatile:
3244 case tok::kw_restrict:
Steve Naroffad373bd2007-07-31 12:34:36 +00003245
Chris Lattneracd58a32006-08-06 17:24:14 +00003246 // function-specifier
3247 case tok::kw_inline:
Douglas Gregor61956c42008-10-31 09:07:45 +00003248 case tok::kw_virtual:
3249 case tok::kw_explicit:
Chris Lattner7b20dc72007-08-09 16:40:21 +00003250
Peter Collingbourne3d9cbdc2011-04-15 00:35:57 +00003251 // static_assert-declaration
3252 case tok::kw__Static_assert:
3253
Chris Lattner599e47e2007-08-09 17:01:07 +00003254 // GNU typeof support.
3255 case tok::kw_typeof:
Mike Stump11289f42009-09-09 15:08:12 +00003256
Chris Lattner599e47e2007-08-09 17:01:07 +00003257 // GNU attributes.
Chris Lattner7b20dc72007-08-09 16:40:21 +00003258 case tok::kw___attribute:
Chris Lattneracd58a32006-08-06 17:24:14 +00003259 return true;
Mike Stump11289f42009-09-09 15:08:12 +00003260
Francois Pichete878cb62011-06-19 08:02:06 +00003261 // C++0x decltype.
3262 case tok::kw_decltype:
3263 return true;
3264
Chris Lattner8b2ec162008-07-26 03:38:44 +00003265 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
3266 case tok::less:
3267 return getLang().ObjC1;
Mike Stump11289f42009-09-09 15:08:12 +00003268
Douglas Gregor19b7acf2011-04-27 05:41:15 +00003269 // typedef-name
3270 case tok::annot_typename:
3271 return !DisambiguatingWithExpression ||
3272 !isStartOfObjCClassMessageMissingOpenBracket();
3273
Steve Narofff192fab2009-01-06 19:34:12 +00003274 case tok::kw___declspec:
Steve Naroff44ac7772008-12-25 14:16:32 +00003275 case tok::kw___cdecl:
3276 case tok::kw___stdcall:
3277 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00003278 case tok::kw___thiscall:
Eli Friedman53339e02009-06-08 23:27:34 +00003279 case tok::kw___w64:
3280 case tok::kw___ptr64:
Francois Pichetf2fb4112011-08-25 00:36:46 +00003281 case tok::kw___ptr32:
Eli Friedman53339e02009-06-08 23:27:34 +00003282 case tok::kw___forceinline:
Dawn Perchik335e16b2010-09-03 01:29:35 +00003283 case tok::kw___pascal:
Francois Pichet17ed0202011-08-18 09:59:55 +00003284 case tok::kw___unaligned:
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003285
3286 case tok::kw___private:
3287 case tok::kw___local:
3288 case tok::kw___global:
3289 case tok::kw___constant:
3290 case tok::kw___read_only:
3291 case tok::kw___read_write:
3292 case tok::kw___write_only:
3293
Eli Friedman53339e02009-06-08 23:27:34 +00003294 return true;
Chris Lattneracd58a32006-08-06 17:24:14 +00003295 }
3296}
3297
Douglas Gregor9de54ea2010-01-13 17:31:36 +00003298bool Parser::isConstructorDeclarator() {
3299 TentativeParsingAction TPA(*this);
3300
3301 // Parse the C++ scope specifier.
3302 CXXScopeSpec SS;
John McCallba7bf592010-08-24 05:47:05 +00003303 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), true)) {
John McCall1f476a12010-02-26 08:45:28 +00003304 TPA.Revert();
3305 return false;
3306 }
Douglas Gregor9de54ea2010-01-13 17:31:36 +00003307
3308 // Parse the constructor name.
3309 if (Tok.is(tok::identifier) || Tok.is(tok::annot_template_id)) {
3310 // We already know that we have a constructor name; just consume
3311 // the token.
3312 ConsumeToken();
3313 } else {
3314 TPA.Revert();
3315 return false;
3316 }
3317
3318 // Current class name must be followed by a left parentheses.
3319 if (Tok.isNot(tok::l_paren)) {
3320 TPA.Revert();
3321 return false;
3322 }
3323 ConsumeParen();
3324
3325 // A right parentheses or ellipsis signals that we have a constructor.
3326 if (Tok.is(tok::r_paren) || Tok.is(tok::ellipsis)) {
3327 TPA.Revert();
3328 return true;
3329 }
3330
3331 // If we need to, enter the specified scope.
3332 DeclaratorScopeObj DeclScopeObj(*this, SS);
Douglas Gregor0be31a22010-07-02 17:43:08 +00003333 if (SS.isSet() && Actions.ShouldEnterDeclaratorScope(getCurScope(), SS))
Douglas Gregor9de54ea2010-01-13 17:31:36 +00003334 DeclScopeObj.EnterDeclaratorScope();
3335
Francois Pichet79f3a872011-01-31 04:54:32 +00003336 // Optionally skip Microsoft attributes.
John McCall084e83d2011-03-24 11:26:52 +00003337 ParsedAttributes Attrs(AttrFactory);
Francois Pichet79f3a872011-01-31 04:54:32 +00003338 MaybeParseMicrosoftAttributes(Attrs);
3339
Douglas Gregor9de54ea2010-01-13 17:31:36 +00003340 // Check whether the next token(s) are part of a declaration
3341 // specifier, in which case we have the start of a parameter and,
3342 // therefore, we know that this is a constructor.
3343 bool IsConstructor = isDeclarationSpecifier();
3344 TPA.Revert();
3345 return IsConstructor;
3346}
Chris Lattnerb9093cd2006-08-04 04:39:53 +00003347
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003348/// ParseTypeQualifierListOpt
Dawn Perchik335e16b2010-09-03 01:29:35 +00003349/// type-qualifier-list: [C99 6.7.5]
3350/// type-qualifier
3351/// [vendor] attributes
3352/// [ only if VendorAttributesAllowed=true ]
3353/// type-qualifier-list type-qualifier
3354/// [vendor] type-qualifier-list attributes
3355/// [ only if VendorAttributesAllowed=true ]
3356/// [C++0x] attribute-specifier[opt] is allowed before cv-qualifier-seq
3357/// [ only if CXX0XAttributesAllowed=true ]
3358/// Note: vendor can be GNU, MS, etc.
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003359///
Dawn Perchik335e16b2010-09-03 01:29:35 +00003360void Parser::ParseTypeQualifierListOpt(DeclSpec &DS,
3361 bool VendorAttributesAllowed,
Alexis Hunt96d5c762009-11-21 08:43:09 +00003362 bool CXX0XAttributesAllowed) {
3363 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier()) {
3364 SourceLocation Loc = Tok.getLocation();
John McCall084e83d2011-03-24 11:26:52 +00003365 ParsedAttributesWithRange attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00003366 ParseCXX0XAttributes(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +00003367 if (CXX0XAttributesAllowed)
John McCall53fa7142010-12-24 02:08:15 +00003368 DS.takeAttributesFrom(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +00003369 else
3370 Diag(Loc, diag::err_attributes_not_allowed);
3371 }
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +00003372
3373 SourceLocation EndLoc;
3374
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003375 while (1) {
John McCall49bfce42009-08-03 20:12:06 +00003376 bool isInvalid = false;
Chris Lattnerd9c3c592006-08-05 06:26:47 +00003377 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00003378 unsigned DiagID = 0;
Chris Lattner60809f52006-11-28 05:18:46 +00003379 SourceLocation Loc = Tok.getLocation();
Chris Lattnerd9c3c592006-08-05 06:26:47 +00003380
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003381 switch (Tok.getKind()) {
Douglas Gregor28c78432010-08-27 17:35:51 +00003382 case tok::code_completion:
3383 Actions.CodeCompleteTypeQualifiers(DS);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00003384 return cutOffParsing();
Douglas Gregor28c78432010-08-27 17:35:51 +00003385
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003386 case tok::kw_const:
John McCall49bfce42009-08-03 20:12:06 +00003387 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, DiagID,
3388 getLang());
Chris Lattnerd9c3c592006-08-05 06:26:47 +00003389 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003390 case tok::kw_volatile:
John McCall49bfce42009-08-03 20:12:06 +00003391 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
3392 getLang());
Chris Lattnerd9c3c592006-08-05 06:26:47 +00003393 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003394 case tok::kw_restrict:
John McCall49bfce42009-08-03 20:12:06 +00003395 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
3396 getLang());
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003397 break;
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003398
3399 // OpenCL qualifiers:
3400 case tok::kw_private:
3401 if (!getLang().OpenCL)
3402 goto DoneWithTypeQuals;
3403 case tok::kw___private:
3404 case tok::kw___global:
3405 case tok::kw___local:
3406 case tok::kw___constant:
3407 case tok::kw___read_only:
3408 case tok::kw___write_only:
3409 case tok::kw___read_write:
3410 ParseOpenCLQualifiers(DS);
3411 break;
3412
Eli Friedman53339e02009-06-08 23:27:34 +00003413 case tok::kw___w64:
Steve Narofff9c29d42008-12-25 14:41:26 +00003414 case tok::kw___ptr64:
Francois Pichetf2fb4112011-08-25 00:36:46 +00003415 case tok::kw___ptr32:
Steve Naroff44ac7772008-12-25 14:16:32 +00003416 case tok::kw___cdecl:
3417 case tok::kw___stdcall:
3418 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00003419 case tok::kw___thiscall:
Francois Pichet17ed0202011-08-18 09:59:55 +00003420 case tok::kw___unaligned:
Dawn Perchik335e16b2010-09-03 01:29:35 +00003421 if (VendorAttributesAllowed) {
John McCall53fa7142010-12-24 02:08:15 +00003422 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman53339e02009-06-08 23:27:34 +00003423 continue;
3424 }
3425 goto DoneWithTypeQuals;
Dawn Perchik335e16b2010-09-03 01:29:35 +00003426 case tok::kw___pascal:
3427 if (VendorAttributesAllowed) {
John McCall53fa7142010-12-24 02:08:15 +00003428 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik335e16b2010-09-03 01:29:35 +00003429 continue;
3430 }
3431 goto DoneWithTypeQuals;
Chris Lattnere37e2332006-08-15 04:50:22 +00003432 case tok::kw___attribute:
Dawn Perchik335e16b2010-09-03 01:29:35 +00003433 if (VendorAttributesAllowed) {
John McCall53fa7142010-12-24 02:08:15 +00003434 ParseGNUAttributes(DS.getAttributes());
Chris Lattnercf0bab22008-12-18 07:02:59 +00003435 continue; // do *not* consume the next token!
3436 }
3437 // otherwise, FALL THROUGH!
3438 default:
Steve Naroff44ac7772008-12-25 14:16:32 +00003439 DoneWithTypeQuals:
Chris Lattnercf0bab22008-12-18 07:02:59 +00003440 // If this is not a type-qualifier token, we're done reading type
3441 // qualifiers. First verify that DeclSpec's are consistent.
Douglas Gregore3e01a22009-04-01 22:41:11 +00003442 DS.Finish(Diags, PP);
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +00003443 if (EndLoc.isValid())
3444 DS.SetRangeEnd(EndLoc);
Chris Lattnercf0bab22008-12-18 07:02:59 +00003445 return;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003446 }
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00003447
Chris Lattnerd9c3c592006-08-05 06:26:47 +00003448 // If the specifier combination wasn't legal, issue a diagnostic.
3449 if (isInvalid) {
3450 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner6d29c102008-11-18 07:48:38 +00003451 Diag(Tok, DiagID) << PrevSpec;
Chris Lattnerd9c3c592006-08-05 06:26:47 +00003452 }
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +00003453 EndLoc = ConsumeToken();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003454 }
3455}
3456
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00003457
3458/// ParseDeclarator - Parse and verify a newly-initialized declarator.
3459///
3460void Parser::ParseDeclarator(Declarator &D) {
3461 /// This implements the 'declarator' production in the C grammar, then checks
3462 /// for well-formedness and issues diagnostics.
Sebastian Redlbd150f42008-11-21 19:14:01 +00003463 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00003464}
3465
Sebastian Redlbd150f42008-11-21 19:14:01 +00003466/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
3467/// is parsed by the function passed to it. Pass null, and the direct-declarator
3468/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregordbc5daf2008-11-07 20:08:42 +00003469/// ptr-operator production.
3470///
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00003471/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
3472/// [C] pointer[opt] direct-declarator
3473/// [C++] direct-declarator
3474/// [C++] ptr-operator declarator
Chris Lattner6c7416c2006-08-07 00:19:33 +00003475///
3476/// pointer: [C99 6.7.5]
3477/// '*' type-qualifier-list[opt]
3478/// '*' type-qualifier-list[opt] pointer
3479///
Douglas Gregordbc5daf2008-11-07 20:08:42 +00003480/// ptr-operator:
3481/// '*' cv-qualifier-seq[opt]
3482/// '&'
Sebastian Redled0f3b02009-03-15 22:02:01 +00003483/// [C++0x] '&&'
Douglas Gregordbc5daf2008-11-07 20:08:42 +00003484/// [GNU] '&' restrict[opt] attributes[opt]
Sebastian Redled0f3b02009-03-15 22:02:01 +00003485/// [GNU?] '&&' restrict[opt] attributes[opt]
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00003486/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Sebastian Redlbd150f42008-11-21 19:14:01 +00003487void Parser::ParseDeclaratorInternal(Declarator &D,
3488 DirectDeclParseFunction DirectDeclParser) {
Douglas Gregor66a985d2009-08-26 14:27:30 +00003489 if (Diags.hasAllExtensionsSilenced())
3490 D.setExtension();
Douglas Gregorc49f5b22010-08-23 18:23:48 +00003491
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00003492 // C++ member pointers start with a '::' or a nested-name.
3493 // Member pointers get special handling, since there's no place for the
3494 // scope spec in the generic path below.
Chris Lattner803802d2009-03-24 17:04:48 +00003495 if (getLang().CPlusPlus &&
3496 (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) ||
3497 Tok.is(tok::annot_cxxscope))) {
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00003498 CXXScopeSpec SS;
John McCallba7bf592010-08-24 05:47:05 +00003499 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), true); // ignore fail
John McCall1f476a12010-02-26 08:45:28 +00003500
Jeffrey Yasskin4e150f82010-04-07 23:29:58 +00003501 if (SS.isNotEmpty()) {
Mike Stump11289f42009-09-09 15:08:12 +00003502 if (Tok.isNot(tok::star)) {
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00003503 // The scope spec really belongs to the direct-declarator.
3504 D.getCXXScopeSpec() = SS;
3505 if (DirectDeclParser)
3506 (this->*DirectDeclParser)(D);
3507 return;
3508 }
3509
3510 SourceLocation Loc = ConsumeToken();
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003511 D.SetRangeEnd(Loc);
John McCall084e83d2011-03-24 11:26:52 +00003512 DeclSpec DS(AttrFactory);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00003513 ParseTypeQualifierListOpt(DS);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003514 D.ExtendWithDeclSpec(DS);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00003515
3516 // Recurse to parse whatever is left.
3517 ParseDeclaratorInternal(D, DirectDeclParser);
3518
3519 // Sema will have to catch (syntactically invalid) pointers into global
3520 // scope. It has to catch pointers into namespace scope anyway.
3521 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
John McCall084e83d2011-03-24 11:26:52 +00003522 Loc),
3523 DS.getAttributes(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003524 /* Don't replace range end. */SourceLocation());
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00003525 return;
3526 }
3527 }
3528
3529 tok::TokenKind Kind = Tok.getKind();
Steve Naroffec33ed92008-08-27 16:04:49 +00003530 // Not a pointer, C++ reference, or block.
Chris Lattner9eac9312009-03-27 04:18:06 +00003531 if (Kind != tok::star && Kind != tok::caret &&
Chris Lattner803802d2009-03-24 17:04:48 +00003532 (Kind != tok::amp || !getLang().CPlusPlus) &&
Sebastian Redl3b27be62009-03-23 00:00:23 +00003533 // We parse rvalue refs in C++03, because otherwise the errors are scary.
Chris Lattner9eac9312009-03-27 04:18:06 +00003534 (Kind != tok::ampamp || !getLang().CPlusPlus)) {
Sebastian Redlbd150f42008-11-21 19:14:01 +00003535 if (DirectDeclParser)
3536 (this->*DirectDeclParser)(D);
Douglas Gregordbc5daf2008-11-07 20:08:42 +00003537 return;
3538 }
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00003539
Sebastian Redled0f3b02009-03-15 22:02:01 +00003540 // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference,
3541 // '&&' -> rvalue reference
Sebastian Redl3b27be62009-03-23 00:00:23 +00003542 SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&.
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003543 D.SetRangeEnd(Loc);
Bill Wendling3708c182007-05-27 10:15:43 +00003544
Chris Lattner9eac9312009-03-27 04:18:06 +00003545 if (Kind == tok::star || Kind == tok::caret) {
Chris Lattner788404f2008-02-21 01:32:26 +00003546 // Is a pointer.
John McCall084e83d2011-03-24 11:26:52 +00003547 DeclSpec DS(AttrFactory);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00003548
Bill Wendling3708c182007-05-27 10:15:43 +00003549 ParseTypeQualifierListOpt(DS);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003550 D.ExtendWithDeclSpec(DS);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00003551
Bill Wendling3708c182007-05-27 10:15:43 +00003552 // Recursively parse the declarator.
Sebastian Redlbd150f42008-11-21 19:14:01 +00003553 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroffec33ed92008-08-27 16:04:49 +00003554 if (Kind == tok::star)
3555 // Remember that we parsed a pointer type, and remember the type-quals.
3556 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
Chandler Carruthe71b378d2011-02-23 18:51:59 +00003557 DS.getConstSpecLoc(),
3558 DS.getVolatileSpecLoc(),
John McCall084e83d2011-03-24 11:26:52 +00003559 DS.getRestrictSpecLoc()),
3560 DS.getAttributes(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003561 SourceLocation());
Steve Naroffec33ed92008-08-27 16:04:49 +00003562 else
3563 // Remember that we parsed a Block type, and remember the type-quals.
Mike Stump11289f42009-09-09 15:08:12 +00003564 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
John McCall084e83d2011-03-24 11:26:52 +00003565 Loc),
3566 DS.getAttributes(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003567 SourceLocation());
Bill Wendling3708c182007-05-27 10:15:43 +00003568 } else {
3569 // Is a reference
John McCall084e83d2011-03-24 11:26:52 +00003570 DeclSpec DS(AttrFactory);
Bill Wendling93efb222007-06-02 23:28:54 +00003571
Sebastian Redl3b27be62009-03-23 00:00:23 +00003572 // Complain about rvalue references in C++03, but then go on and build
3573 // the declarator.
3574 if (Kind == tok::ampamp && !getLang().CPlusPlus0x)
Douglas Gregor00984992011-01-25 02:17:32 +00003575 Diag(Loc, diag::ext_rvalue_reference);
Sebastian Redl3b27be62009-03-23 00:00:23 +00003576
Bill Wendling93efb222007-06-02 23:28:54 +00003577 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
3578 // cv-qualifiers are introduced through the use of a typedef or of a
3579 // template type argument, in which case the cv-qualifiers are ignored.
3580 //
3581 // [GNU] Retricted references are allowed.
3582 // [GNU] Attributes on references are allowed.
Alexis Hunt96d5c762009-11-21 08:43:09 +00003583 // [C++0x] Attributes on references are not allowed.
3584 ParseTypeQualifierListOpt(DS, true, false);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003585 D.ExtendWithDeclSpec(DS);
Bill Wendling93efb222007-06-02 23:28:54 +00003586
3587 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
3588 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
3589 Diag(DS.getConstSpecLoc(),
Chris Lattner6d29c102008-11-18 07:48:38 +00003590 diag::err_invalid_reference_qualifier_application) << "const";
Bill Wendling93efb222007-06-02 23:28:54 +00003591 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
3592 Diag(DS.getVolatileSpecLoc(),
Chris Lattner6d29c102008-11-18 07:48:38 +00003593 diag::err_invalid_reference_qualifier_application) << "volatile";
Bill Wendling93efb222007-06-02 23:28:54 +00003594 }
Bill Wendling3708c182007-05-27 10:15:43 +00003595
3596 // Recursively parse the declarator.
Sebastian Redlbd150f42008-11-21 19:14:01 +00003597 ParseDeclaratorInternal(D, DirectDeclParser);
Bill Wendling3708c182007-05-27 10:15:43 +00003598
Douglas Gregor66583c52008-11-03 15:51:28 +00003599 if (D.getNumTypeObjects() > 0) {
3600 // C++ [dcl.ref]p4: There shall be no references to references.
3601 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
3602 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattnerebad6a22008-11-19 07:37:42 +00003603 if (const IdentifierInfo *II = D.getIdentifier())
3604 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
3605 << II;
3606 else
3607 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
3608 << "type name";
Douglas Gregor66583c52008-11-03 15:51:28 +00003609
Sebastian Redlbd150f42008-11-21 19:14:01 +00003610 // Once we've complained about the reference-to-reference, we
Douglas Gregor66583c52008-11-03 15:51:28 +00003611 // can go ahead and build the (technically ill-formed)
3612 // declarator: reference collapsing will take care of it.
3613 }
3614 }
3615
Bill Wendling3708c182007-05-27 10:15:43 +00003616 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner788404f2008-02-21 01:32:26 +00003617 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
Sebastian Redled0f3b02009-03-15 22:02:01 +00003618 Kind == tok::amp),
John McCall084e83d2011-03-24 11:26:52 +00003619 DS.getAttributes(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003620 SourceLocation());
Bill Wendling3708c182007-05-27 10:15:43 +00003621 }
Chris Lattner6c7416c2006-08-07 00:19:33 +00003622}
3623
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003624/// ParseDirectDeclarator
3625/// direct-declarator: [C99 6.7.5]
Douglas Gregor831c93f2008-11-05 20:51:48 +00003626/// [C99] identifier
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003627/// '(' declarator ')'
3628/// [GNU] '(' attributes declarator ')'
Chris Lattnere8074e62006-08-06 18:30:15 +00003629/// [C90] direct-declarator '[' constant-expression[opt] ']'
3630/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
3631/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
3632/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
3633/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003634/// direct-declarator '(' parameter-type-list ')'
3635/// direct-declarator '(' identifier-list[opt] ')'
3636/// [GNU] direct-declarator '(' parameter-forward-declarations
3637/// parameter-type-list[opt] ')'
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003638/// [C++] direct-declarator '(' parameter-declaration-clause ')'
3639/// cv-qualifier-seq[opt] exception-specification[opt]
Douglas Gregor61956c42008-10-31 09:07:45 +00003640/// [C++] declarator-id
Douglas Gregor831c93f2008-11-05 20:51:48 +00003641///
3642/// declarator-id: [C++ 8]
Douglas Gregor27b4c162010-12-23 22:44:42 +00003643/// '...'[opt] id-expression
Douglas Gregor831c93f2008-11-05 20:51:48 +00003644/// '::'[opt] nested-name-specifier[opt] type-name
3645///
3646/// id-expression: [C++ 5.1]
3647/// unqualified-id
Douglas Gregord90fd522009-09-25 21:45:23 +00003648/// qualified-id
Douglas Gregor831c93f2008-11-05 20:51:48 +00003649///
3650/// unqualified-id: [C++ 5.1]
Mike Stump11289f42009-09-09 15:08:12 +00003651/// identifier
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003652/// operator-function-id
Douglas Gregord90fd522009-09-25 21:45:23 +00003653/// conversion-function-id
Mike Stump11289f42009-09-09 15:08:12 +00003654/// '~' class-name
Douglas Gregor7f741122009-02-25 19:37:18 +00003655/// template-id
Argyrios Kyrtzidise4426352008-11-07 22:02:30 +00003656///
Chris Lattneracd58a32006-08-06 17:24:14 +00003657void Parser::ParseDirectDeclarator(Declarator &D) {
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00003658 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003659
Douglas Gregor7861a802009-11-03 01:35:08 +00003660 if (getLang().CPlusPlus && D.mayHaveIdentifier()) {
3661 // ParseDeclaratorInternal might already have parsed the scope.
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00003662 if (D.getCXXScopeSpec().isEmpty()) {
John McCallba7bf592010-08-24 05:47:05 +00003663 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec(), ParsedType(), true);
John McCall1f476a12010-02-26 08:45:28 +00003664 }
3665
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00003666 if (D.getCXXScopeSpec().isValid()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00003667 if (Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
John McCall2b058ef2009-12-11 20:04:54 +00003668 // Change the declaration context for name lookup, until this function
3669 // is exited (and the declarator has been parsed).
3670 DeclScopeObj.EnterDeclaratorScope();
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00003671 }
3672
Douglas Gregor27b4c162010-12-23 22:44:42 +00003673 // C++0x [dcl.fct]p14:
3674 // There is a syntactic ambiguity when an ellipsis occurs at the end
3675 // of a parameter-declaration-clause without a preceding comma. In
3676 // this case, the ellipsis is parsed as part of the
3677 // abstract-declarator if the type of the parameter names a template
3678 // parameter pack that has not been expanded; otherwise, it is parsed
3679 // as part of the parameter-declaration-clause.
3680 if (Tok.is(tok::ellipsis) &&
3681 !((D.getContext() == Declarator::PrototypeContext ||
3682 D.getContext() == Declarator::BlockLiteralContext) &&
Douglas Gregor27b4c162010-12-23 22:44:42 +00003683 NextToken().is(tok::r_paren) &&
3684 !Actions.containsUnexpandedParameterPacks(D)))
3685 D.setEllipsisLoc(ConsumeToken());
3686
Douglas Gregor7861a802009-11-03 01:35:08 +00003687 if (Tok.is(tok::identifier) || Tok.is(tok::kw_operator) ||
3688 Tok.is(tok::annot_template_id) || Tok.is(tok::tilde)) {
3689 // We found something that indicates the start of an unqualified-id.
3690 // Parse that unqualified-id.
John McCall84821e72010-04-13 06:39:49 +00003691 bool AllowConstructorName;
3692 if (D.getDeclSpec().hasTypeSpecifier())
3693 AllowConstructorName = false;
3694 else if (D.getCXXScopeSpec().isSet())
3695 AllowConstructorName =
3696 (D.getContext() == Declarator::FileContext ||
3697 (D.getContext() == Declarator::MemberContext &&
3698 D.getDeclSpec().isFriendSpecified()));
3699 else
3700 AllowConstructorName = (D.getContext() == Declarator::MemberContext);
3701
Douglas Gregor7861a802009-11-03 01:35:08 +00003702 if (ParseUnqualifiedId(D.getCXXScopeSpec(),
3703 /*EnteringContext=*/true,
3704 /*AllowDestructorName=*/true,
Douglas Gregor9de54ea2010-01-13 17:31:36 +00003705 AllowConstructorName,
John McCallba7bf592010-08-24 05:47:05 +00003706 ParsedType(),
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00003707 D.getName()) ||
3708 // Once we're past the identifier, if the scope was bad, mark the
3709 // whole declarator bad.
3710 D.getCXXScopeSpec().isInvalid()) {
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00003711 D.SetIdentifier(0, Tok.getLocation());
3712 D.setInvalidType(true);
Douglas Gregor7861a802009-11-03 01:35:08 +00003713 } else {
3714 // Parsed the unqualified-id; update range information and move along.
3715 if (D.getSourceRange().getBegin().isInvalid())
3716 D.SetRangeBegin(D.getName().getSourceRange().getBegin());
3717 D.SetRangeEnd(D.getName().getSourceRange().getEnd());
Douglas Gregordbc5daf2008-11-07 20:08:42 +00003718 }
Douglas Gregor7861a802009-11-03 01:35:08 +00003719 goto PastIdentifier;
Douglas Gregor11d0c4c2008-11-06 22:13:31 +00003720 }
Douglas Gregor7861a802009-11-03 01:35:08 +00003721 } else if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00003722 assert(!getLang().CPlusPlus &&
3723 "There's a C++-specific check for tok::identifier above");
3724 assert(Tok.getIdentifierInfo() && "Not an identifier?");
3725 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
3726 ConsumeToken();
Douglas Gregor7861a802009-11-03 01:35:08 +00003727 goto PastIdentifier;
3728 }
3729
3730 if (Tok.is(tok::l_paren)) {
Chris Lattneracd58a32006-08-06 17:24:14 +00003731 // direct-declarator: '(' declarator ')'
Chris Lattnere37e2332006-08-15 04:50:22 +00003732 // direct-declarator: '(' attributes declarator ')'
Chris Lattneracd58a32006-08-06 17:24:14 +00003733 // Example: 'char (*X)' or 'int (*XX)(void)'
3734 ParseParenDeclarator(D);
Douglas Gregor9de54ea2010-01-13 17:31:36 +00003735
3736 // If the declarator was parenthesized, we entered the declarator
3737 // scope when parsing the parenthesized declarator, then exited
3738 // the scope already. Re-enter the scope, if we need to.
3739 if (D.getCXXScopeSpec().isSet()) {
Fariborz Jahanian358acd52010-08-17 23:50:37 +00003740 // If there was an error parsing parenthesized declarator, declarator
3741 // scope may have been enterred before. Don't do it again.
3742 if (!D.isInvalidType() &&
3743 Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
Douglas Gregor9de54ea2010-01-13 17:31:36 +00003744 // Change the declaration context for name lookup, until this function
3745 // is exited (and the declarator has been parsed).
Fariborz Jahanian358acd52010-08-17 23:50:37 +00003746 DeclScopeObj.EnterDeclaratorScope();
Douglas Gregor9de54ea2010-01-13 17:31:36 +00003747 }
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00003748 } else if (D.mayOmitIdentifier()) {
Chris Lattneracd58a32006-08-06 17:24:14 +00003749 // This could be something simple like "int" (in which case the declarator
3750 // portion is empty), if an abstract-declarator is allowed.
3751 D.SetIdentifier(0, Tok.getLocation());
3752 } else {
Douglas Gregord9f92e22009-03-06 23:28:18 +00003753 if (D.getContext() == Declarator::MemberContext)
3754 Diag(Tok, diag::err_expected_member_name_or_semi)
3755 << D.getDeclSpec().getSourceRange();
3756 else if (getLang().CPlusPlus)
Douglas Gregor30d60cb2009-11-03 19:44:04 +00003757 Diag(Tok, diag::err_expected_unqualified_id) << getLang().CPlusPlus;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003758 else
Chris Lattner6d29c102008-11-18 07:48:38 +00003759 Diag(Tok, diag::err_expected_ident_lparen);
Chris Lattnereec40f92006-08-06 21:55:29 +00003760 D.SetIdentifier(0, Tok.getLocation());
Chris Lattner8c5dd732008-11-11 06:13:16 +00003761 D.setInvalidType(true);
Chris Lattneracd58a32006-08-06 17:24:14 +00003762 }
Mike Stump11289f42009-09-09 15:08:12 +00003763
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00003764 PastIdentifier:
Chris Lattneracd58a32006-08-06 17:24:14 +00003765 assert(D.isPastIdentifier() &&
3766 "Haven't past the location of the identifier yet?");
Mike Stump11289f42009-09-09 15:08:12 +00003767
Alexis Hunt96d5c762009-11-21 08:43:09 +00003768 // Don't parse attributes unless we have an identifier.
John McCall53fa7142010-12-24 02:08:15 +00003769 if (D.getIdentifier())
3770 MaybeParseCXX0XAttributes(D);
Alexis Hunt96d5c762009-11-21 08:43:09 +00003771
Chris Lattneracd58a32006-08-06 17:24:14 +00003772 while (1) {
Chris Lattner76c72282007-10-09 17:33:22 +00003773 if (Tok.is(tok::l_paren)) {
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00003774 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
3775 // In such a case, check if we actually have a function declarator; if it
3776 // is not, the declarator has been fully parsed.
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003777 if (getLang().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
3778 // When not in file scope, warn for ambiguous function declarators, just
3779 // in case the author intended it as a variable definition.
3780 bool warnIfAmbiguous = D.getContext() != Declarator::FileContext;
3781 if (!isCXXFunctionDeclarator(warnIfAmbiguous))
3782 break;
3783 }
John McCall084e83d2011-03-24 11:26:52 +00003784 ParsedAttributes attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00003785 ParseFunctionDeclarator(ConsumeParen(), D, attrs);
Chris Lattner76c72282007-10-09 17:33:22 +00003786 } else if (Tok.is(tok::l_square)) {
Chris Lattnere8074e62006-08-06 18:30:15 +00003787 ParseBracketDeclarator(D);
Chris Lattneracd58a32006-08-06 17:24:14 +00003788 } else {
3789 break;
3790 }
3791 }
3792}
3793
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003794/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
3795/// only called before the identifier, so these are most likely just grouping
Mike Stump11289f42009-09-09 15:08:12 +00003796/// parens for precedence. If we find that these are actually function
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003797/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
3798///
3799/// direct-declarator:
3800/// '(' declarator ')'
3801/// [GNU] '(' attributes declarator ')'
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003802/// direct-declarator '(' parameter-type-list ')'
3803/// direct-declarator '(' identifier-list[opt] ')'
3804/// [GNU] direct-declarator '(' parameter-forward-declarations
3805/// parameter-type-list[opt] ')'
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003806///
3807void Parser::ParseParenDeclarator(Declarator &D) {
3808 SourceLocation StartLoc = ConsumeParen();
3809 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
Mike Stump11289f42009-09-09 15:08:12 +00003810
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003811 // Eat any attributes before we look at whether this is a grouping or function
3812 // declarator paren. If this is a grouping paren, the attribute applies to
3813 // the type being built up, for example:
3814 // int (__attribute__(()) *x)(long y)
3815 // If this ends up not being a grouping paren, the attribute applies to the
3816 // first argument, for example:
3817 // int (__attribute__(()) int x)
3818 // In either case, we need to eat any attributes to be able to determine what
3819 // sort of paren this is.
3820 //
John McCall084e83d2011-03-24 11:26:52 +00003821 ParsedAttributes attrs(AttrFactory);
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003822 bool RequiresArg = false;
3823 if (Tok.is(tok::kw___attribute)) {
John McCall53fa7142010-12-24 02:08:15 +00003824 ParseGNUAttributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +00003825
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003826 // We require that the argument list (if this is a non-grouping paren) be
3827 // present even if the attribute list was empty.
3828 RequiresArg = true;
3829 }
Steve Naroff44ac7772008-12-25 14:16:32 +00003830 // Eat any Microsoft extensions.
Eli Friedman53339e02009-06-08 23:27:34 +00003831 if (Tok.is(tok::kw___cdecl) || Tok.is(tok::kw___stdcall) ||
Douglas Gregora941dca2010-05-18 16:57:00 +00003832 Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___fastcall) ||
Francois Pichet17ed0202011-08-18 09:59:55 +00003833 Tok.is(tok::kw___w64) || Tok.is(tok::kw___ptr64) ||
Francois Pichetf2fb4112011-08-25 00:36:46 +00003834 Tok.is(tok::kw___ptr32) || Tok.is(tok::kw___unaligned)) {
John McCall53fa7142010-12-24 02:08:15 +00003835 ParseMicrosoftTypeAttributes(attrs);
Eli Friedman53339e02009-06-08 23:27:34 +00003836 }
Dawn Perchik335e16b2010-09-03 01:29:35 +00003837 // Eat any Borland extensions.
Ted Kremenek5eec2b02010-11-10 05:59:39 +00003838 if (Tok.is(tok::kw___pascal))
John McCall53fa7142010-12-24 02:08:15 +00003839 ParseBorlandTypeAttributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +00003840
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003841 // If we haven't past the identifier yet (or where the identifier would be
3842 // stored, if this is an abstract declarator), then this is probably just
3843 // grouping parens. However, if this could be an abstract-declarator, then
3844 // this could also be the start of function arguments (consider 'void()').
3845 bool isGrouping;
Mike Stump11289f42009-09-09 15:08:12 +00003846
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003847 if (!D.mayOmitIdentifier()) {
3848 // If this can't be an abstract-declarator, this *must* be a grouping
3849 // paren, because we haven't seen the identifier yet.
3850 isGrouping = true;
3851 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Argyrios Kyrtzidise8addf52008-10-06 00:07:55 +00003852 (getLang().CPlusPlus && Tok.is(tok::ellipsis)) || // C++ int(...)
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003853 isDeclarationSpecifier()) { // 'int(int)' is a function.
3854 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
3855 // considered to be a type, not a K&R identifier-list.
3856 isGrouping = false;
3857 } else {
3858 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
3859 isGrouping = true;
3860 }
Mike Stump11289f42009-09-09 15:08:12 +00003861
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003862 // If this is a grouping paren, handle:
3863 // direct-declarator: '(' declarator ')'
3864 // direct-declarator: '(' attributes declarator ')'
3865 if (isGrouping) {
Argyrios Kyrtzidis8ae36842008-10-07 10:21:57 +00003866 bool hadGroupingParens = D.hasGroupingParens();
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00003867 D.setGroupingParens(true);
3868
Sebastian Redlbd150f42008-11-21 19:14:01 +00003869 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003870 // Match the ')'.
Abramo Bagnara924a8f32010-12-10 16:29:40 +00003871 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_paren, StartLoc);
John McCall084e83d2011-03-24 11:26:52 +00003872 D.AddTypeInfo(DeclaratorChunk::getParen(StartLoc, EndLoc),
3873 attrs, EndLoc);
Argyrios Kyrtzidis8ae36842008-10-07 10:21:57 +00003874
3875 D.setGroupingParens(hadGroupingParens);
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003876 return;
3877 }
Mike Stump11289f42009-09-09 15:08:12 +00003878
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003879 // Okay, if this wasn't a grouping paren, it must be the start of a function
3880 // argument list. Recognize that this declarator will never have an
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003881 // identifier (and remember where it would have been), then call into
3882 // ParseFunctionDeclarator to handle of argument list.
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003883 D.SetIdentifier(0, Tok.getLocation());
3884
John McCall53fa7142010-12-24 02:08:15 +00003885 ParseFunctionDeclarator(StartLoc, D, attrs, RequiresArg);
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003886}
3887
3888/// ParseFunctionDeclarator - We are after the identifier and have parsed the
3889/// declarator D up to a paren, which indicates that we are parsing function
3890/// arguments.
Chris Lattneracd58a32006-08-06 17:24:14 +00003891///
Douglas Gregor9e66af42011-07-05 16:44:18 +00003892/// If attrs is non-null, then the caller parsed those arguments immediately
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003893/// after the open paren - they should be considered to be the first argument of
3894/// a parameter. If RequiresArg is true, then the first argument of the
3895/// function is required to be present and required to not be an identifier
3896/// list.
3897///
Douglas Gregor9e66af42011-07-05 16:44:18 +00003898/// For C++, after the parameter-list, it also parses cv-qualifier-seq[opt],
3899/// (C++0x) ref-qualifier[opt], exception-specification[opt], and
3900/// (C++0x) trailing-return-type[opt].
3901///
3902/// [C++0x] exception-specification:
3903/// dynamic-exception-specification
3904/// noexcept-specification
3905///
3906void Parser::ParseFunctionDeclarator(SourceLocation LParenLoc, Declarator &D,
3907 ParsedAttributes &attrs,
3908 bool RequiresArg) {
3909 // lparen is already consumed!
3910 assert(D.isPastIdentifier() && "Should not call before identifier!");
3911
3912 // This should be true when the function has typed arguments.
3913 // Otherwise, it is treated as a K&R-style function.
3914 bool HasProto = false;
3915 // Build up an array of information about the parsed arguments.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003916 SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Douglas Gregor9e66af42011-07-05 16:44:18 +00003917 // Remember where we see an ellipsis, if any.
3918 SourceLocation EllipsisLoc;
3919
3920 DeclSpec DS(AttrFactory);
3921 bool RefQualifierIsLValueRef = true;
3922 SourceLocation RefQualifierLoc;
3923 ExceptionSpecificationType ESpecType = EST_None;
3924 SourceRange ESpecRange;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003925 SmallVector<ParsedType, 2> DynamicExceptions;
3926 SmallVector<SourceRange, 2> DynamicExceptionRanges;
Douglas Gregor9e66af42011-07-05 16:44:18 +00003927 ExprResult NoexceptExpr;
3928 ParsedType TrailingReturnType;
3929
3930 SourceLocation EndLoc;
3931
3932 if (isFunctionDeclaratorIdentifierList()) {
3933 if (RequiresArg)
3934 Diag(Tok, diag::err_argument_required_after_attribute);
3935
3936 ParseFunctionDeclaratorIdentifierList(D, ParamInfo);
3937
3938 EndLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
3939 } else {
3940 // Enter function-declaration scope, limiting any declarators to the
3941 // function prototype scope, including parameter declarators.
3942 ParseScope PrototypeScope(this,
3943 Scope::FunctionPrototypeScope|Scope::DeclScope);
3944
3945 if (Tok.isNot(tok::r_paren))
3946 ParseParameterDeclarationClause(D, attrs, ParamInfo, EllipsisLoc);
3947 else if (RequiresArg)
3948 Diag(Tok, diag::err_argument_required_after_attribute);
3949
3950 HasProto = ParamInfo.size() || getLang().CPlusPlus;
3951
3952 // If we have the closing ')', eat it.
3953 EndLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
3954
3955 if (getLang().CPlusPlus) {
3956 MaybeParseCXX0XAttributes(attrs);
3957
3958 // Parse cv-qualifier-seq[opt].
3959 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
3960 if (!DS.getSourceRange().getEnd().isInvalid())
3961 EndLoc = DS.getSourceRange().getEnd();
3962
3963 // Parse ref-qualifier[opt].
3964 if (Tok.is(tok::amp) || Tok.is(tok::ampamp)) {
3965 if (!getLang().CPlusPlus0x)
3966 Diag(Tok, diag::ext_ref_qualifier);
3967
3968 RefQualifierIsLValueRef = Tok.is(tok::amp);
3969 RefQualifierLoc = ConsumeToken();
3970 EndLoc = RefQualifierLoc;
3971 }
3972
3973 // Parse exception-specification[opt].
3974 ESpecType = MaybeParseExceptionSpecification(ESpecRange,
3975 DynamicExceptions,
3976 DynamicExceptionRanges,
3977 NoexceptExpr);
3978 if (ESpecType != EST_None)
3979 EndLoc = ESpecRange.getEnd();
3980
3981 // Parse trailing-return-type[opt].
3982 if (getLang().CPlusPlus0x && Tok.is(tok::arrow)) {
Douglas Gregordb0b9f12011-08-04 15:30:47 +00003983 SourceRange Range;
3984 TrailingReturnType = ParseTrailingReturnType(Range).get();
3985 if (Range.getEnd().isValid())
3986 EndLoc = Range.getEnd();
Douglas Gregor9e66af42011-07-05 16:44:18 +00003987 }
3988 }
3989
3990 // Leave prototype scope.
3991 PrototypeScope.Exit();
3992 }
3993
3994 // Remember that we parsed a function type, and remember the attributes.
3995 D.AddTypeInfo(DeclaratorChunk::getFunction(HasProto,
3996 /*isVariadic=*/EllipsisLoc.isValid(),
3997 EllipsisLoc,
3998 ParamInfo.data(), ParamInfo.size(),
3999 DS.getTypeQualifiers(),
4000 RefQualifierIsLValueRef,
4001 RefQualifierLoc,
Douglas Gregorad69e652011-07-13 21:47:47 +00004002 /*MutableLoc=*/SourceLocation(),
Douglas Gregor9e66af42011-07-05 16:44:18 +00004003 ESpecType, ESpecRange.getBegin(),
4004 DynamicExceptions.data(),
4005 DynamicExceptionRanges.data(),
4006 DynamicExceptions.size(),
4007 NoexceptExpr.isUsable() ?
4008 NoexceptExpr.get() : 0,
4009 LParenLoc, EndLoc, D,
4010 TrailingReturnType),
4011 attrs, EndLoc);
4012}
4013
4014/// isFunctionDeclaratorIdentifierList - This parameter list may have an
4015/// identifier list form for a K&R-style function: void foo(a,b,c)
4016///
4017/// Note that identifier-lists are only allowed for normal declarators, not for
4018/// abstract-declarators.
4019bool Parser::isFunctionDeclaratorIdentifierList() {
4020 return !getLang().CPlusPlus
4021 && Tok.is(tok::identifier)
4022 && !TryAltiVecVectorToken()
4023 // K&R identifier lists can't have typedefs as identifiers, per C99
4024 // 6.7.5.3p11.
4025 && (TryAnnotateTypeOrScopeToken() || !Tok.is(tok::annot_typename))
4026 // Identifier lists follow a really simple grammar: the identifiers can
4027 // be followed *only* by a ", identifier" or ")". However, K&R
4028 // identifier lists are really rare in the brave new modern world, and
4029 // it is very common for someone to typo a type in a non-K&R style
4030 // list. If we are presented with something like: "void foo(intptr x,
4031 // float y)", we don't want to start parsing the function declarator as
4032 // though it is a K&R style declarator just because intptr is an
4033 // invalid type.
4034 //
4035 // To handle this, we check to see if the token after the first
4036 // identifier is a "," or ")". Only then do we parse it as an
4037 // identifier list.
4038 && (NextToken().is(tok::comma) || NextToken().is(tok::r_paren));
4039}
4040
4041/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
4042/// we found a K&R-style identifier list instead of a typed parameter list.
4043///
4044/// After returning, ParamInfo will hold the parsed parameters.
4045///
4046/// identifier-list: [C99 6.7.5]
4047/// identifier
4048/// identifier-list ',' identifier
4049///
4050void Parser::ParseFunctionDeclaratorIdentifierList(
4051 Declarator &D,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004052 SmallVector<DeclaratorChunk::ParamInfo, 16> &ParamInfo) {
Douglas Gregor9e66af42011-07-05 16:44:18 +00004053 // If there was no identifier specified for the declarator, either we are in
4054 // an abstract-declarator, or we are in a parameter declarator which was found
4055 // to be abstract. In abstract-declarators, identifier lists are not valid:
4056 // diagnose this.
4057 if (!D.getIdentifier())
4058 Diag(Tok, diag::ext_ident_list_in_param);
4059
4060 // Maintain an efficient lookup of params we have seen so far.
4061 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
4062
4063 while (1) {
4064 // If this isn't an identifier, report the error and skip until ')'.
4065 if (Tok.isNot(tok::identifier)) {
4066 Diag(Tok, diag::err_expected_ident);
4067 SkipUntil(tok::r_paren, /*StopAtSemi=*/true, /*DontConsume=*/true);
4068 // Forget we parsed anything.
4069 ParamInfo.clear();
4070 return;
4071 }
4072
4073 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
4074
4075 // Reject 'typedef int y; int test(x, y)', but continue parsing.
4076 if (Actions.getTypeName(*ParmII, Tok.getLocation(), getCurScope()))
4077 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
4078
4079 // Verify that the argument identifier has not already been mentioned.
4080 if (!ParamsSoFar.insert(ParmII)) {
4081 Diag(Tok, diag::err_param_redefinition) << ParmII;
4082 } else {
4083 // Remember this identifier in ParamInfo.
4084 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
4085 Tok.getLocation(),
4086 0));
4087 }
4088
4089 // Eat the identifier.
4090 ConsumeToken();
4091
4092 // The list continues if we see a comma.
4093 if (Tok.isNot(tok::comma))
4094 break;
4095 ConsumeToken();
4096 }
4097}
4098
4099/// ParseParameterDeclarationClause - Parse a (possibly empty) parameter-list
4100/// after the opening parenthesis. This function will not parse a K&R-style
4101/// identifier list.
4102///
4103/// D is the declarator being parsed. If attrs is non-null, then the caller
4104/// parsed those arguments immediately after the open paren - they should be
4105/// considered to be the first argument of a parameter.
4106///
4107/// After returning, ParamInfo will hold the parsed parameters. EllipsisLoc will
4108/// be the location of the ellipsis, if any was parsed.
4109///
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004110/// parameter-type-list: [C99 6.7.5]
4111/// parameter-list
4112/// parameter-list ',' '...'
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00004113/// [C++] parameter-list '...'
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004114///
4115/// parameter-list: [C99 6.7.5]
4116/// parameter-declaration
4117/// parameter-list ',' parameter-declaration
4118///
4119/// parameter-declaration: [C99 6.7.5]
4120/// declaration-specifiers declarator
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00004121/// [C++] declaration-specifiers declarator '=' assignment-expression
Chris Lattnere37e2332006-08-15 04:50:22 +00004122/// [GNU] declaration-specifiers declarator attributes
Sebastian Redlf769df52009-03-24 22:27:57 +00004123/// declaration-specifiers abstract-declarator[opt]
4124/// [C++] declaration-specifiers abstract-declarator[opt]
Chris Lattner58258242008-04-10 02:22:51 +00004125/// '=' assignment-expression
Chris Lattnere37e2332006-08-15 04:50:22 +00004126/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004127///
Douglas Gregor9e66af42011-07-05 16:44:18 +00004128void Parser::ParseParameterDeclarationClause(
4129 Declarator &D,
4130 ParsedAttributes &attrs,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004131 SmallVector<DeclaratorChunk::ParamInfo, 16> &ParamInfo,
Douglas Gregor9e66af42011-07-05 16:44:18 +00004132 SourceLocation &EllipsisLoc) {
Mike Stump11289f42009-09-09 15:08:12 +00004133
Chris Lattner371ed4e2008-04-06 06:57:35 +00004134 while (1) {
4135 if (Tok.is(tok::ellipsis)) {
Douglas Gregor94349fd2009-02-18 07:07:28 +00004136 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chris Lattner371ed4e2008-04-06 06:57:35 +00004137 break;
Chris Lattneracd58a32006-08-06 17:24:14 +00004138 }
Mike Stump11289f42009-09-09 15:08:12 +00004139
Chris Lattner371ed4e2008-04-06 06:57:35 +00004140 // Parse the declaration-specifiers.
John McCall28a6aea2009-11-04 02:18:39 +00004141 // Just use the ParsingDeclaration "scope" of the declarator.
John McCall084e83d2011-03-24 11:26:52 +00004142 DeclSpec DS(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00004143
4144 // Skip any Microsoft attributes before a param.
4145 if (getLang().Microsoft && Tok.is(tok::l_square))
4146 ParseMicrosoftAttributes(DS.getAttributes());
4147
4148 SourceLocation DSStart = Tok.getLocation();
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00004149
4150 // If the caller parsed attributes for the first argument, add them now.
John McCall53fa7142010-12-24 02:08:15 +00004151 // Take them so that we only apply the attributes to the first parameter.
Douglas Gregor9e66af42011-07-05 16:44:18 +00004152 // FIXME: If we saw an ellipsis first, this code is not reached. Are the
4153 // attributes lost? Should they even be allowed?
4154 // FIXME: If we can leave the attributes in the token stream somehow, we can
4155 // get rid of a parameter (attrs) and this statement. It might be too much
4156 // hassle.
John McCall53fa7142010-12-24 02:08:15 +00004157 DS.takeAttributesFrom(attrs);
4158
Chris Lattnerde39c3e2009-02-27 18:38:20 +00004159 ParseDeclarationSpecifiers(DS);
Mike Stump11289f42009-09-09 15:08:12 +00004160
Chris Lattner371ed4e2008-04-06 06:57:35 +00004161 // Parse the declarator. This is "PrototypeContext", because we must
4162 // accept either 'declarator' or 'abstract-declarator' here.
4163 Declarator ParmDecl(DS, Declarator::PrototypeContext);
4164 ParseDeclarator(ParmDecl);
4165
4166 // Parse GNU attributes, if present.
John McCall53fa7142010-12-24 02:08:15 +00004167 MaybeParseGNUAttributes(ParmDecl);
Mike Stump11289f42009-09-09 15:08:12 +00004168
Chris Lattner371ed4e2008-04-06 06:57:35 +00004169 // Remember this parsed parameter in ParamInfo.
4170 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
Mike Stump11289f42009-09-09 15:08:12 +00004171
Douglas Gregor4d87df52008-12-16 21:30:33 +00004172 // DefArgToks is used when the parsing of default arguments needs
4173 // to be delayed.
4174 CachedTokens *DefArgToks = 0;
4175
Chris Lattner371ed4e2008-04-06 06:57:35 +00004176 // If no parameter was specified, verify that *something* was specified,
4177 // otherwise we have a missing type and identifier.
Chris Lattnerde39c3e2009-02-27 18:38:20 +00004178 if (DS.isEmpty() && ParmDecl.getIdentifier() == 0 &&
4179 ParmDecl.getNumTypeObjects() == 0) {
Chris Lattner371ed4e2008-04-06 06:57:35 +00004180 // Completely missing, emit error.
4181 Diag(DSStart, diag::err_missing_param);
4182 } else {
4183 // Otherwise, we have something. Add it and let semantic analysis try
4184 // to grok it and add the result to the ParamInfo we are building.
Mike Stump11289f42009-09-09 15:08:12 +00004185
Chris Lattner371ed4e2008-04-06 06:57:35 +00004186 // Inform the actions module about the parameter declarator, so it gets
4187 // added to the current scope.
John McCall48871652010-08-21 09:40:31 +00004188 Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl);
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00004189
4190 // Parse the default argument, if any. We parse the default
4191 // arguments in all dialects; the semantic analysis in
4192 // ActOnParamDefaultArgument will reject the default argument in
4193 // C.
4194 if (Tok.is(tok::equal)) {
Douglas Gregor58354032008-12-24 00:01:03 +00004195 SourceLocation EqualLoc = Tok.getLocation();
4196
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00004197 // Parse the default argument
Douglas Gregor4d87df52008-12-16 21:30:33 +00004198 if (D.getContext() == Declarator::MemberContext) {
4199 // If we're inside a class definition, cache the tokens
4200 // corresponding to the default argument. We'll actually parse
4201 // them when we see the end of the class definition.
4202 // FIXME: Templates will require something similar.
4203 // FIXME: Can we use a smart pointer for Toks?
4204 DefArgToks = new CachedTokens;
4205
Mike Stump11289f42009-09-09 15:08:12 +00004206 if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks,
Argyrios Kyrtzidis8d7bdba2010-04-23 21:20:12 +00004207 /*StopAtSemi=*/true,
4208 /*ConsumeFinalToken=*/false)) {
Douglas Gregor4d87df52008-12-16 21:30:33 +00004209 delete DefArgToks;
4210 DefArgToks = 0;
Douglas Gregor58354032008-12-24 00:01:03 +00004211 Actions.ActOnParamDefaultArgumentError(Param);
Argyrios Kyrtzidis249179c2010-08-06 09:47:24 +00004212 } else {
4213 // Mark the end of the default argument so that we know when to
4214 // stop when we parse it later on.
4215 Token DefArgEnd;
4216 DefArgEnd.startToken();
4217 DefArgEnd.setKind(tok::cxx_defaultarg_end);
4218 DefArgEnd.setLocation(Tok.getLocation());
4219 DefArgToks->push_back(DefArgEnd);
Mike Stump11289f42009-09-09 15:08:12 +00004220 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc,
Anders Carlsson84613c42009-06-12 16:51:40 +00004221 (*DefArgToks)[1].getLocation());
Argyrios Kyrtzidis249179c2010-08-06 09:47:24 +00004222 }
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00004223 } else {
Douglas Gregor4d87df52008-12-16 21:30:33 +00004224 // Consume the '='.
Douglas Gregor58354032008-12-24 00:01:03 +00004225 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00004226
Douglas Gregor8a01b2a2010-09-11 20:24:53 +00004227 // The argument isn't actually potentially evaluated unless it is
4228 // used.
4229 EnterExpressionEvaluationContext Eval(Actions,
4230 Sema::PotentiallyEvaluatedIfUsed);
4231
John McCalldadc5752010-08-24 06:29:42 +00004232 ExprResult DefArgResult(ParseAssignmentExpression());
Douglas Gregor4d87df52008-12-16 21:30:33 +00004233 if (DefArgResult.isInvalid()) {
4234 Actions.ActOnParamDefaultArgumentError(Param);
4235 SkipUntil(tok::comma, tok::r_paren, true, true);
4236 } else {
4237 // Inform the actions module about the default argument
4238 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
John McCallb268a282010-08-23 23:25:46 +00004239 DefArgResult.take());
Douglas Gregor4d87df52008-12-16 21:30:33 +00004240 }
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00004241 }
4242 }
Mike Stump11289f42009-09-09 15:08:12 +00004243
4244 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
4245 ParmDecl.getIdentifierLoc(), Param,
Douglas Gregor4d87df52008-12-16 21:30:33 +00004246 DefArgToks));
Chris Lattner371ed4e2008-04-06 06:57:35 +00004247 }
4248
4249 // If the next token is a comma, consume it and keep reading arguments.
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00004250 if (Tok.isNot(tok::comma)) {
4251 if (Tok.is(tok::ellipsis)) {
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00004252 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
4253
4254 if (!getLang().CPlusPlus) {
4255 // We have ellipsis without a preceding ',', which is ill-formed
4256 // in C. Complain and provide the fix.
4257 Diag(EllipsisLoc, diag::err_missing_comma_before_ellipsis)
Douglas Gregora771f462010-03-31 17:46:05 +00004258 << FixItHint::CreateInsertion(EllipsisLoc, ", ");
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00004259 }
4260 }
4261
4262 break;
4263 }
Mike Stump11289f42009-09-09 15:08:12 +00004264
Chris Lattner371ed4e2008-04-06 06:57:35 +00004265 // Consume the comma.
4266 ConsumeToken();
Chris Lattneracd58a32006-08-06 17:24:14 +00004267 }
Mike Stump11289f42009-09-09 15:08:12 +00004268
Chris Lattner6c940e62008-04-06 06:34:08 +00004269}
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004270
Chris Lattnere8074e62006-08-06 18:30:15 +00004271/// [C90] direct-declarator '[' constant-expression[opt] ']'
4272/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
4273/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
4274/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
4275/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
4276void Parser::ParseBracketDeclarator(Declarator &D) {
Chris Lattner04132372006-10-16 06:12:55 +00004277 SourceLocation StartLoc = ConsumeBracket();
Mike Stump11289f42009-09-09 15:08:12 +00004278
Chris Lattner84a11622008-12-18 07:27:21 +00004279 // C array syntax has many features, but by-far the most common is [] and [4].
4280 // This code does a fast path to handle some of the most obvious cases.
4281 if (Tok.getKind() == tok::r_square) {
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004282 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
John McCall084e83d2011-03-24 11:26:52 +00004283 ParsedAttributes attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00004284 MaybeParseCXX0XAttributes(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +00004285
Chris Lattner84a11622008-12-18 07:27:21 +00004286 // Remember that we parsed the empty array type.
John McCalldadc5752010-08-24 06:29:42 +00004287 ExprResult NumElements;
John McCall084e83d2011-03-24 11:26:52 +00004288 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0,
Douglas Gregor04318252009-07-06 15:59:29 +00004289 StartLoc, EndLoc),
John McCall084e83d2011-03-24 11:26:52 +00004290 attrs, EndLoc);
Chris Lattner84a11622008-12-18 07:27:21 +00004291 return;
4292 } else if (Tok.getKind() == tok::numeric_constant &&
4293 GetLookAheadToken(1).is(tok::r_square)) {
4294 // [4] is very common. Parse the numeric constant expression.
John McCalldadc5752010-08-24 06:29:42 +00004295 ExprResult ExprRes(Actions.ActOnNumericConstant(Tok));
Chris Lattner84a11622008-12-18 07:27:21 +00004296 ConsumeToken();
4297
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004298 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
John McCall084e83d2011-03-24 11:26:52 +00004299 ParsedAttributes attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00004300 MaybeParseCXX0XAttributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +00004301
Chris Lattner84a11622008-12-18 07:27:21 +00004302 // Remember that we parsed a array type, and remember its features.
John McCall084e83d2011-03-24 11:26:52 +00004303 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, 0,
John McCall53fa7142010-12-24 02:08:15 +00004304 ExprRes.release(),
Douglas Gregor04318252009-07-06 15:59:29 +00004305 StartLoc, EndLoc),
John McCall084e83d2011-03-24 11:26:52 +00004306 attrs, EndLoc);
Chris Lattner84a11622008-12-18 07:27:21 +00004307 return;
4308 }
Mike Stump11289f42009-09-09 15:08:12 +00004309
Chris Lattnere8074e62006-08-06 18:30:15 +00004310 // If valid, this location is the position where we read the 'static' keyword.
4311 SourceLocation StaticLoc;
Chris Lattner76c72282007-10-09 17:33:22 +00004312 if (Tok.is(tok::kw_static))
Chris Lattneraf635312006-10-16 06:06:51 +00004313 StaticLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00004314
Chris Lattnere8074e62006-08-06 18:30:15 +00004315 // If there is a type-qualifier-list, read it now.
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00004316 // Type qualifiers in an array subscript are a C99 feature.
John McCall084e83d2011-03-24 11:26:52 +00004317 DeclSpec DS(AttrFactory);
Chris Lattnercf0bab22008-12-18 07:02:59 +00004318 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Mike Stump11289f42009-09-09 15:08:12 +00004319
Chris Lattnere8074e62006-08-06 18:30:15 +00004320 // If we haven't already read 'static', check to see if there is one after the
4321 // type-qualifier-list.
Chris Lattner76c72282007-10-09 17:33:22 +00004322 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Chris Lattneraf635312006-10-16 06:06:51 +00004323 StaticLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00004324
Chris Lattnere8074e62006-08-06 18:30:15 +00004325 // Handle "direct-declarator [ type-qual-list[opt] * ]".
Chris Lattnere8074e62006-08-06 18:30:15 +00004326 bool isStar = false;
John McCalldadc5752010-08-24 06:29:42 +00004327 ExprResult NumElements;
Mike Stump11289f42009-09-09 15:08:12 +00004328
Chris Lattner521ff2b2008-04-06 05:26:30 +00004329 // Handle the case where we have '[*]' as the array size. However, a leading
4330 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
4331 // the the token after the star is a ']'. Since stars in arrays are
4332 // infrequent, use of lookahead is not costly here.
4333 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattnerc439f0d2008-04-06 05:27:21 +00004334 ConsumeToken(); // Eat the '*'.
Chris Lattner1906f802006-08-06 19:14:46 +00004335
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00004336 if (StaticLoc.isValid()) {
Chris Lattner521ff2b2008-04-06 05:26:30 +00004337 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00004338 StaticLoc = SourceLocation(); // Drop the static.
4339 }
Chris Lattner521ff2b2008-04-06 05:26:30 +00004340 isStar = true;
Chris Lattner76c72282007-10-09 17:33:22 +00004341 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner84a11622008-12-18 07:27:21 +00004342 // Note, in C89, this production uses the constant-expr production instead
4343 // of assignment-expr. The only difference is that assignment-expr allows
4344 // things like '=' and '*='. Sema rejects these in C89 mode because they
4345 // are not i-c-e's, so we don't need to distinguish between the two here.
Mike Stump11289f42009-09-09 15:08:12 +00004346
Douglas Gregorc9c02ed2009-06-19 23:52:42 +00004347 // Parse the constant-expression or assignment-expression now (depending
4348 // on dialect).
4349 if (getLang().CPlusPlus)
4350 NumElements = ParseConstantExpression();
4351 else
4352 NumElements = ParseAssignmentExpression();
Chris Lattner62591722006-08-12 18:40:58 +00004353 }
Mike Stump11289f42009-09-09 15:08:12 +00004354
Chris Lattner62591722006-08-12 18:40:58 +00004355 // If there was an error parsing the assignment-expression, recover.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00004356 if (NumElements.isInvalid()) {
Chris Lattnercd2a8c52009-04-24 22:30:50 +00004357 D.setInvalidType(true);
Chris Lattner62591722006-08-12 18:40:58 +00004358 // If the expression was invalid, skip it.
4359 SkipUntil(tok::r_square);
4360 return;
Chris Lattnere8074e62006-08-06 18:30:15 +00004361 }
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004362
4363 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
4364
John McCall084e83d2011-03-24 11:26:52 +00004365 ParsedAttributes attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00004366 MaybeParseCXX0XAttributes(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +00004367
Chris Lattner84a11622008-12-18 07:27:21 +00004368 // Remember that we parsed a array type, and remember its features.
John McCall084e83d2011-03-24 11:26:52 +00004369 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
Chris Lattnercbc426d2006-12-02 06:43:02 +00004370 StaticLoc.isValid(), isStar,
Douglas Gregor04318252009-07-06 15:59:29 +00004371 NumElements.release(),
4372 StartLoc, EndLoc),
John McCall084e83d2011-03-24 11:26:52 +00004373 attrs, EndLoc);
Chris Lattnere8074e62006-08-06 18:30:15 +00004374}
4375
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00004376/// [GNU] typeof-specifier:
4377/// typeof ( expressions )
4378/// typeof ( type-name )
4379/// [GNU/C++] typeof unary-expression
Steve Naroffad373bd2007-07-31 12:34:36 +00004380///
4381void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner76c72282007-10-09 17:33:22 +00004382 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00004383 Token OpTok = Tok;
Steve Naroffad373bd2007-07-31 12:34:36 +00004384 SourceLocation StartLoc = ConsumeToken();
4385
John McCalle8595032010-01-13 20:03:27 +00004386 const bool hasParens = Tok.is(tok::l_paren);
4387
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00004388 bool isCastExpr;
John McCallba7bf592010-08-24 05:47:05 +00004389 ParsedType CastTy;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00004390 SourceRange CastRange;
Peter Collingbournee190dee2011-03-11 19:24:49 +00004391 ExprResult Operand = ParseExprAfterUnaryExprOrTypeTrait(OpTok, isCastExpr,
4392 CastTy, CastRange);
John McCalle8595032010-01-13 20:03:27 +00004393 if (hasParens)
4394 DS.setTypeofParensRange(CastRange);
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00004395
4396 if (CastRange.getEnd().isInvalid())
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00004397 // FIXME: Not accurate, the range gets one token more than it should.
4398 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00004399 else
4400 DS.SetRangeEnd(CastRange.getEnd());
Mike Stump11289f42009-09-09 15:08:12 +00004401
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00004402 if (isCastExpr) {
4403 if (!CastTy) {
4404 DS.SetTypeSpecError();
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00004405 return;
Douglas Gregor220cac52009-02-18 17:45:20 +00004406 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00004407
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00004408 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00004409 unsigned DiagID;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00004410 // Check for duplicate type specifiers (e.g. "int typeof(int)").
4411 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00004412 DiagID, CastTy))
4413 Diag(StartLoc, DiagID) << PrevSpec;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00004414 return;
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00004415 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00004416
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00004417 // If we get here, the operand to the typeof was an expresion.
4418 if (Operand.isInvalid()) {
4419 DS.SetTypeSpecError();
Steve Naroff4bd2f712007-08-02 02:53:48 +00004420 return;
Steve Naroffad373bd2007-07-31 12:34:36 +00004421 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00004422
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00004423 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00004424 unsigned DiagID;
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00004425 // Check for duplicate type specifiers (e.g. "int typeof(int)").
4426 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
John McCallba7bf592010-08-24 05:47:05 +00004427 DiagID, Operand.get()))
John McCall49bfce42009-08-03 20:12:06 +00004428 Diag(StartLoc, DiagID) << PrevSpec;
Steve Naroffad373bd2007-07-31 12:34:36 +00004429}
Chris Lattner73a9c7d2010-02-28 18:33:55 +00004430
4431
4432/// TryAltiVecVectorTokenOutOfLine - Out of line body that should only be called
4433/// from TryAltiVecVectorToken.
4434bool Parser::TryAltiVecVectorTokenOutOfLine() {
4435 Token Next = NextToken();
4436 switch (Next.getKind()) {
4437 default: return false;
4438 case tok::kw_short:
4439 case tok::kw_long:
4440 case tok::kw_signed:
4441 case tok::kw_unsigned:
4442 case tok::kw_void:
4443 case tok::kw_char:
4444 case tok::kw_int:
4445 case tok::kw_float:
4446 case tok::kw_double:
4447 case tok::kw_bool:
4448 case tok::kw___pixel:
4449 Tok.setKind(tok::kw___vector);
4450 return true;
4451 case tok::identifier:
4452 if (Next.getIdentifierInfo() == Ident_pixel) {
4453 Tok.setKind(tok::kw___vector);
4454 return true;
4455 }
4456 return false;
4457 }
4458}
4459
4460bool Parser::TryAltiVecTokenOutOfLine(DeclSpec &DS, SourceLocation Loc,
4461 const char *&PrevSpec, unsigned &DiagID,
4462 bool &isInvalid) {
4463 if (Tok.getIdentifierInfo() == Ident_vector) {
4464 Token Next = NextToken();
4465 switch (Next.getKind()) {
4466 case tok::kw_short:
4467 case tok::kw_long:
4468 case tok::kw_signed:
4469 case tok::kw_unsigned:
4470 case tok::kw_void:
4471 case tok::kw_char:
4472 case tok::kw_int:
4473 case tok::kw_float:
4474 case tok::kw_double:
4475 case tok::kw_bool:
4476 case tok::kw___pixel:
4477 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
4478 return true;
4479 case tok::identifier:
4480 if (Next.getIdentifierInfo() == Ident_pixel) {
4481 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
4482 return true;
4483 }
4484 break;
4485 default:
4486 break;
4487 }
Douglas Gregor9938e3b2010-06-16 15:28:57 +00004488 } else if ((Tok.getIdentifierInfo() == Ident_pixel) &&
Chris Lattner73a9c7d2010-02-28 18:33:55 +00004489 DS.isTypeAltiVecVector()) {
4490 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
4491 return true;
4492 }
4493 return false;
4494}