blob: d4b5a2b5bfe75f10b0a89e95be3ba68f416f32be [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) ))
Argyrios Kyrtzidis635a9b42011-09-13 16:05:53 +0000198 SourceLocation RParen = ConsumeParen();
199 Attrs.addNew(AttrName, SourceRange(AttrNameLoc, RParen), 0, AttrNameLoc,
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000200 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)) {
Argyrios Kyrtzidis635a9b42011-09-13 16:05:53 +0000222 SourceLocation RParen = ConsumeParen();
223 Attrs.addNew(AttrName, SourceRange(AttrNameLoc, RParen), 0, AttrNameLoc,
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000224 ParmName, ParmLoc, ArgExprs.take(), ArgExprs.size());
225 }
226 }
227 } else { // not an identifier
228 switch (Tok.getKind()) {
Argyrios Kyrtzidis635a9b42011-09-13 16:05:53 +0000229 case tok::r_paren: {
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000230 // parse a possibly empty comma separated list of expressions
231 // __attribute__(( nonnull() ))
Argyrios Kyrtzidis635a9b42011-09-13 16:05:53 +0000232 SourceLocation RParen = ConsumeParen();
233 Attrs.addNew(AttrName, SourceRange(AttrNameLoc, RParen), 0, AttrNameLoc,
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000234 0, SourceLocation(), 0, 0);
235 break;
Argyrios Kyrtzidis635a9b42011-09-13 16:05:53 +0000236 }
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000237 case tok::kw_char:
238 case tok::kw_wchar_t:
239 case tok::kw_char16_t:
240 case tok::kw_char32_t:
241 case tok::kw_bool:
242 case tok::kw_short:
243 case tok::kw_int:
244 case tok::kw_long:
245 case tok::kw___int64:
246 case tok::kw_signed:
247 case tok::kw_unsigned:
248 case tok::kw_float:
249 case tok::kw_double:
250 case tok::kw_void:
251 case tok::kw_typeof: {
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000252 // If it's a builtin type name, eat it and expect a rparen
253 // __attribute__(( vec_type_hint(char) ))
Argyrios Kyrtzidis635a9b42011-09-13 16:05:53 +0000254 SourceLocation EndLoc = ConsumeToken();
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000255 if (Tok.is(tok::r_paren))
Argyrios Kyrtzidis635a9b42011-09-13 16:05:53 +0000256 EndLoc = ConsumeParen();
257 AttributeList *attr
258 = Attrs.addNew(AttrName, SourceRange(AttrNameLoc, EndLoc), 0,
259 AttrNameLoc, 0, SourceLocation(), 0, 0);
260 if (attr->getKind() == AttributeList::AT_IBOutletCollection)
261 Diag(Tok, diag::err_iboutletcollection_builtintype);
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000262 break;
263 }
264 default:
265 // __attribute__(( aligned(16) ))
266 ExprVector ArgExprs(Actions);
267 bool ArgExprsOk = true;
268
269 // now parse the list of expressions
270 while (1) {
271 ExprResult ArgExpr(ParseAssignmentExpression());
272 if (ArgExpr.isInvalid()) {
273 ArgExprsOk = false;
274 SkipUntil(tok::r_paren);
275 break;
276 } else {
277 ArgExprs.push_back(ArgExpr.release());
278 }
279 if (Tok.isNot(tok::comma))
280 break;
281 ConsumeToken(); // Eat the comma, move to the next argument
282 }
283 // Match the ')'.
284 if (ArgExprsOk && Tok.is(tok::r_paren)) {
Argyrios Kyrtzidis635a9b42011-09-13 16:05:53 +0000285 SourceLocation RParen = ConsumeParen();
286 Attrs.addNew(AttrName, SourceRange(AttrNameLoc, RParen), 0,
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000287 AttrNameLoc, 0, SourceLocation(),
288 ArgExprs.take(), ArgExprs.size());
289 }
290 break;
291 }
292 }
293}
294
295
Eli Friedman06de2b52009-06-08 07:21:15 +0000296/// ParseMicrosoftDeclSpec - Parse an __declspec construct
297///
298/// [MS] decl-specifier:
299/// __declspec ( extended-decl-modifier-seq )
300///
301/// [MS] extended-decl-modifier-seq:
302/// extended-decl-modifier[opt]
303/// extended-decl-modifier extended-decl-modifier-seq
304
John McCall53fa7142010-12-24 02:08:15 +0000305void Parser::ParseMicrosoftDeclSpec(ParsedAttributes &attrs) {
Steve Naroff3a9b7e02008-12-24 20:59:21 +0000306 assert(Tok.is(tok::kw___declspec) && "Not a declspec!");
Eli Friedman06de2b52009-06-08 07:21:15 +0000307
Steve Naroff3a9b7e02008-12-24 20:59:21 +0000308 ConsumeToken();
Eli Friedman06de2b52009-06-08 07:21:15 +0000309 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
310 "declspec")) {
311 SkipUntil(tok::r_paren, true); // skip until ) or ;
John McCall53fa7142010-12-24 02:08:15 +0000312 return;
Eli Friedman06de2b52009-06-08 07:21:15 +0000313 }
Francois Pichetdcf88932011-05-07 19:04:49 +0000314
Eli Friedman53339e02009-06-08 23:27:34 +0000315 while (Tok.getIdentifierInfo()) {
Eli Friedman06de2b52009-06-08 07:21:15 +0000316 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
317 SourceLocation AttrNameLoc = ConsumeToken();
Francois Pichetdcf88932011-05-07 19:04:49 +0000318
319 // FIXME: Remove this when we have proper __declspec(property()) support.
320 // Just skip everything inside property().
321 if (AttrName->getName() == "property") {
322 ConsumeParen();
323 SkipUntil(tok::r_paren);
324 }
Eli Friedman06de2b52009-06-08 07:21:15 +0000325 if (Tok.is(tok::l_paren)) {
326 ConsumeParen();
327 // FIXME: This doesn't parse __declspec(property(get=get_func_name))
328 // correctly.
John McCalldadc5752010-08-24 06:29:42 +0000329 ExprResult ArgExpr(ParseAssignmentExpression());
Eli Friedman06de2b52009-06-08 07:21:15 +0000330 if (!ArgExpr.isInvalid()) {
John McCall37ad5512010-08-23 06:44:23 +0000331 Expr *ExprList = ArgExpr.take();
John McCall084e83d2011-03-24 11:26:52 +0000332 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
333 SourceLocation(), &ExprList, 1, true);
Eli Friedman06de2b52009-06-08 07:21:15 +0000334 }
335 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
336 SkipUntil(tok::r_paren, false);
337 } else {
John McCall084e83d2011-03-24 11:26:52 +0000338 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc,
339 0, SourceLocation(), 0, 0, true);
Eli Friedman06de2b52009-06-08 07:21:15 +0000340 }
341 }
342 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
343 SkipUntil(tok::r_paren, false);
John McCall53fa7142010-12-24 02:08:15 +0000344 return;
Eli Friedman53339e02009-06-08 23:27:34 +0000345}
346
John McCall53fa7142010-12-24 02:08:15 +0000347void Parser::ParseMicrosoftTypeAttributes(ParsedAttributes &attrs) {
Eli Friedman53339e02009-06-08 23:27:34 +0000348 // Treat these like attributes
349 // FIXME: Allow Sema to distinguish between these and real attributes!
350 while (Tok.is(tok::kw___fastcall) || Tok.is(tok::kw___stdcall) ||
Douglas Gregora941dca2010-05-18 16:57:00 +0000351 Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___cdecl) ||
Francois Pichet17ed0202011-08-18 09:59:55 +0000352 Tok.is(tok::kw___ptr64) || Tok.is(tok::kw___w64) ||
Francois Pichetf2fb4112011-08-25 00:36:46 +0000353 Tok.is(tok::kw___ptr32) ||
Francois Pichet17ed0202011-08-18 09:59:55 +0000354 Tok.is(tok::kw___unaligned)) {
Eli Friedman53339e02009-06-08 23:27:34 +0000355 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
356 SourceLocation AttrNameLoc = ConsumeToken();
Francois Pichetf2fb4112011-08-25 00:36:46 +0000357 if (Tok.is(tok::kw___ptr64) || Tok.is(tok::kw___w64) ||
358 Tok.is(tok::kw___ptr32))
Eli Friedman53339e02009-06-08 23:27:34 +0000359 // FIXME: Support these properly!
360 continue;
John McCall084e83d2011-03-24 11:26:52 +0000361 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
362 SourceLocation(), 0, 0, true);
Eli Friedman53339e02009-06-08 23:27:34 +0000363 }
Steve Naroff3a9b7e02008-12-24 20:59:21 +0000364}
365
John McCall53fa7142010-12-24 02:08:15 +0000366void Parser::ParseBorlandTypeAttributes(ParsedAttributes &attrs) {
Dawn Perchik335e16b2010-09-03 01:29:35 +0000367 // Treat these like attributes
368 while (Tok.is(tok::kw___pascal)) {
369 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
370 SourceLocation AttrNameLoc = ConsumeToken();
John McCall084e83d2011-03-24 11:26:52 +0000371 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
372 SourceLocation(), 0, 0, true);
Dawn Perchik335e16b2010-09-03 01:29:35 +0000373 }
John McCall53fa7142010-12-24 02:08:15 +0000374}
375
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +0000376void Parser::ParseOpenCLAttributes(ParsedAttributes &attrs) {
377 // Treat these like attributes
378 while (Tok.is(tok::kw___kernel)) {
379 SourceLocation AttrNameLoc = ConsumeToken();
John McCall084e83d2011-03-24 11:26:52 +0000380 attrs.addNew(PP.getIdentifierInfo("opencl_kernel_function"),
381 AttrNameLoc, 0, AttrNameLoc, 0,
382 SourceLocation(), 0, 0, false);
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +0000383 }
384}
385
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000386void Parser::ParseOpenCLQualifiers(DeclSpec &DS) {
387 SourceLocation Loc = Tok.getLocation();
388 switch(Tok.getKind()) {
389 // OpenCL qualifiers:
390 case tok::kw___private:
391 case tok::kw_private:
John McCall084e83d2011-03-24 11:26:52 +0000392 DS.getAttributes().addNewInteger(
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000393 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000394 PP.getIdentifierInfo("address_space"), Loc, 0);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000395 break;
396
397 case tok::kw___global:
John McCall084e83d2011-03-24 11:26:52 +0000398 DS.getAttributes().addNewInteger(
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000399 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000400 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_global);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000401 break;
402
403 case tok::kw___local:
John McCall084e83d2011-03-24 11:26:52 +0000404 DS.getAttributes().addNewInteger(
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000405 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000406 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_local);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000407 break;
408
409 case tok::kw___constant:
John McCall084e83d2011-03-24 11:26:52 +0000410 DS.getAttributes().addNewInteger(
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000411 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000412 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_constant);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000413 break;
414
415 case tok::kw___read_only:
John McCall084e83d2011-03-24 11:26:52 +0000416 DS.getAttributes().addNewInteger(
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000417 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000418 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_read_only);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000419 break;
420
421 case tok::kw___write_only:
John McCall084e83d2011-03-24 11:26:52 +0000422 DS.getAttributes().addNewInteger(
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000423 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000424 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_write_only);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000425 break;
426
427 case tok::kw___read_write:
John McCall084e83d2011-03-24 11:26:52 +0000428 DS.getAttributes().addNewInteger(
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000429 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000430 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_read_write);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000431 break;
432 default: break;
433 }
434}
435
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000436/// \brief Parse a version number.
437///
438/// version:
439/// simple-integer
440/// simple-integer ',' simple-integer
441/// simple-integer ',' simple-integer ',' simple-integer
442VersionTuple Parser::ParseVersionTuple(SourceRange &Range) {
443 Range = Tok.getLocation();
444
445 if (!Tok.is(tok::numeric_constant)) {
446 Diag(Tok, diag::err_expected_version);
447 SkipUntil(tok::comma, tok::r_paren, true, true, true);
448 return VersionTuple();
449 }
450
451 // Parse the major (and possibly minor and subminor) versions, which
452 // are stored in the numeric constant. We utilize a quirk of the
453 // lexer, which is that it handles something like 1.2.3 as a single
454 // numeric constant, rather than two separate tokens.
455 llvm::SmallString<512> Buffer;
456 Buffer.resize(Tok.getLength()+1);
457 const char *ThisTokBegin = &Buffer[0];
458
459 // Get the spelling of the token, which eliminates trigraphs, etc.
460 bool Invalid = false;
461 unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin, &Invalid);
462 if (Invalid)
463 return VersionTuple();
464
465 // Parse the major version.
466 unsigned AfterMajor = 0;
467 unsigned Major = 0;
468 while (AfterMajor < ActualLength && isdigit(ThisTokBegin[AfterMajor])) {
469 Major = Major * 10 + ThisTokBegin[AfterMajor] - '0';
470 ++AfterMajor;
471 }
472
473 if (AfterMajor == 0) {
474 Diag(Tok, diag::err_expected_version);
475 SkipUntil(tok::comma, tok::r_paren, true, true, true);
476 return VersionTuple();
477 }
478
479 if (AfterMajor == ActualLength) {
480 ConsumeToken();
481
482 // We only had a single version component.
483 if (Major == 0) {
484 Diag(Tok, diag::err_zero_version);
485 return VersionTuple();
486 }
487
488 return VersionTuple(Major);
489 }
490
491 if (ThisTokBegin[AfterMajor] != '.' || (AfterMajor + 1 == ActualLength)) {
492 Diag(Tok, diag::err_expected_version);
493 SkipUntil(tok::comma, tok::r_paren, true, true, true);
494 return VersionTuple();
495 }
496
497 // Parse the minor version.
498 unsigned AfterMinor = AfterMajor + 1;
499 unsigned Minor = 0;
500 while (AfterMinor < ActualLength && isdigit(ThisTokBegin[AfterMinor])) {
501 Minor = Minor * 10 + ThisTokBegin[AfterMinor] - '0';
502 ++AfterMinor;
503 }
504
505 if (AfterMinor == ActualLength) {
506 ConsumeToken();
507
508 // We had major.minor.
509 if (Major == 0 && Minor == 0) {
510 Diag(Tok, diag::err_zero_version);
511 return VersionTuple();
512 }
513
514 return VersionTuple(Major, Minor);
515 }
516
517 // If what follows is not a '.', we have a problem.
518 if (ThisTokBegin[AfterMinor] != '.') {
519 Diag(Tok, diag::err_expected_version);
520 SkipUntil(tok::comma, tok::r_paren, true, true, true);
521 return VersionTuple();
522 }
523
524 // Parse the subminor version.
525 unsigned AfterSubminor = AfterMinor + 1;
526 unsigned Subminor = 0;
527 while (AfterSubminor < ActualLength && isdigit(ThisTokBegin[AfterSubminor])) {
528 Subminor = Subminor * 10 + ThisTokBegin[AfterSubminor] - '0';
529 ++AfterSubminor;
530 }
531
532 if (AfterSubminor != ActualLength) {
533 Diag(Tok, diag::err_expected_version);
534 SkipUntil(tok::comma, tok::r_paren, true, true, true);
535 return VersionTuple();
536 }
537 ConsumeToken();
538 return VersionTuple(Major, Minor, Subminor);
539}
540
541/// \brief Parse the contents of the "availability" attribute.
542///
543/// availability-attribute:
544/// 'availability' '(' platform ',' version-arg-list ')'
545///
546/// platform:
547/// identifier
548///
549/// version-arg-list:
550/// version-arg
551/// version-arg ',' version-arg-list
552///
553/// version-arg:
554/// 'introduced' '=' version
555/// 'deprecated' '=' version
556/// 'removed' = version
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000557/// 'unavailable'
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000558void Parser::ParseAvailabilityAttribute(IdentifierInfo &Availability,
559 SourceLocation AvailabilityLoc,
560 ParsedAttributes &attrs,
561 SourceLocation *endLoc) {
562 SourceLocation PlatformLoc;
563 IdentifierInfo *Platform = 0;
564
565 enum { Introduced, Deprecated, Obsoleted, Unknown };
566 AvailabilityChange Changes[Unknown];
567
568 // Opening '('.
569 SourceLocation LParenLoc;
570 if (!Tok.is(tok::l_paren)) {
571 Diag(Tok, diag::err_expected_lparen);
572 return;
573 }
574 LParenLoc = ConsumeParen();
575
576 // Parse the platform name,
577 if (Tok.isNot(tok::identifier)) {
578 Diag(Tok, diag::err_availability_expected_platform);
579 SkipUntil(tok::r_paren);
580 return;
581 }
582 Platform = Tok.getIdentifierInfo();
583 PlatformLoc = ConsumeToken();
584
585 // Parse the ',' following the platform name.
586 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "", tok::r_paren))
587 return;
588
589 // If we haven't grabbed the pointers for the identifiers
590 // "introduced", "deprecated", and "obsoleted", do so now.
591 if (!Ident_introduced) {
592 Ident_introduced = PP.getIdentifierInfo("introduced");
593 Ident_deprecated = PP.getIdentifierInfo("deprecated");
594 Ident_obsoleted = PP.getIdentifierInfo("obsoleted");
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000595 Ident_unavailable = PP.getIdentifierInfo("unavailable");
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000596 }
597
598 // Parse the set of introductions/deprecations/removals.
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000599 SourceLocation UnavailableLoc;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000600 do {
601 if (Tok.isNot(tok::identifier)) {
602 Diag(Tok, diag::err_availability_expected_change);
603 SkipUntil(tok::r_paren);
604 return;
605 }
606 IdentifierInfo *Keyword = Tok.getIdentifierInfo();
607 SourceLocation KeywordLoc = ConsumeToken();
608
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000609 if (Keyword == Ident_unavailable) {
610 if (UnavailableLoc.isValid()) {
611 Diag(KeywordLoc, diag::err_availability_redundant)
612 << Keyword << SourceRange(UnavailableLoc);
613 }
614 UnavailableLoc = KeywordLoc;
615
616 if (Tok.isNot(tok::comma))
617 break;
618
619 ConsumeToken();
620 continue;
621 }
622
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000623 if (Tok.isNot(tok::equal)) {
624 Diag(Tok, diag::err_expected_equal_after)
625 << Keyword;
626 SkipUntil(tok::r_paren);
627 return;
628 }
629 ConsumeToken();
630
631 SourceRange VersionRange;
632 VersionTuple Version = ParseVersionTuple(VersionRange);
633
634 if (Version.empty()) {
635 SkipUntil(tok::r_paren);
636 return;
637 }
638
639 unsigned Index;
640 if (Keyword == Ident_introduced)
641 Index = Introduced;
642 else if (Keyword == Ident_deprecated)
643 Index = Deprecated;
644 else if (Keyword == Ident_obsoleted)
645 Index = Obsoleted;
646 else
647 Index = Unknown;
648
649 if (Index < Unknown) {
650 if (!Changes[Index].KeywordLoc.isInvalid()) {
651 Diag(KeywordLoc, diag::err_availability_redundant)
652 << Keyword
653 << SourceRange(Changes[Index].KeywordLoc,
654 Changes[Index].VersionRange.getEnd());
655 }
656
657 Changes[Index].KeywordLoc = KeywordLoc;
658 Changes[Index].Version = Version;
659 Changes[Index].VersionRange = VersionRange;
660 } else {
661 Diag(KeywordLoc, diag::err_availability_unknown_change)
662 << Keyword << VersionRange;
663 }
664
665 if (Tok.isNot(tok::comma))
666 break;
667
668 ConsumeToken();
669 } while (true);
670
671 // Closing ')'.
672 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
673 if (RParenLoc.isInvalid())
674 return;
675
676 if (endLoc)
677 *endLoc = RParenLoc;
678
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000679 // The 'unavailable' availability cannot be combined with any other
680 // availability changes. Make sure that hasn't happened.
681 if (UnavailableLoc.isValid()) {
682 bool Complained = false;
683 for (unsigned Index = Introduced; Index != Unknown; ++Index) {
684 if (Changes[Index].KeywordLoc.isValid()) {
685 if (!Complained) {
686 Diag(UnavailableLoc, diag::warn_availability_and_unavailable)
687 << SourceRange(Changes[Index].KeywordLoc,
688 Changes[Index].VersionRange.getEnd());
689 Complained = true;
690 }
691
692 // Clear out the availability.
693 Changes[Index] = AvailabilityChange();
694 }
695 }
696 }
697
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000698 // Record this attribute
Argyrios Kyrtzidis635a9b42011-09-13 16:05:53 +0000699 attrs.addNew(&Availability, SourceRange(AvailabilityLoc, RParenLoc),
John McCall084e83d2011-03-24 11:26:52 +0000700 0, SourceLocation(),
701 Platform, PlatformLoc,
702 Changes[Introduced],
703 Changes[Deprecated],
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000704 Changes[Obsoleted],
705 UnavailableLoc, false, false);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000706}
707
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000708
709// Late Parsed Attributes:
710// See other examples of late parsing in lib/Parse/ParseCXXInlineMethods
711
712void Parser::LateParsedDeclaration::ParseLexedAttributes() {}
713
714void Parser::LateParsedClass::ParseLexedAttributes() {
715 Self->ParseLexedAttributes(*Class);
716}
717
718void Parser::LateParsedAttribute::ParseLexedAttributes() {
719 Self->ParseLexedAttribute(*this);
720}
721
722/// Wrapper class which calls ParseLexedAttribute, after setting up the
723/// scope appropriately.
724void Parser::ParseLexedAttributes(ParsingClass &Class) {
725 // Deal with templates
726 // FIXME: Test cases to make sure this does the right thing for templates.
727 bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope;
728 ParseScope ClassTemplateScope(this, Scope::TemplateParamScope,
729 HasTemplateScope);
730 if (HasTemplateScope)
731 Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate);
732
733 // Set or update the scope flags to include Scope::ThisScope.
734 bool AlreadyHasClassScope = Class.TopLevelClass;
735 unsigned ScopeFlags = Scope::ClassScope|Scope::DeclScope|Scope::ThisScope;
736 ParseScope ClassScope(this, ScopeFlags, !AlreadyHasClassScope);
737 ParseScopeFlags ClassScopeFlags(this, ScopeFlags, AlreadyHasClassScope);
738
739 for (unsigned i = 0, ni = Class.LateParsedDeclarations.size(); i < ni; ++i) {
740 Class.LateParsedDeclarations[i]->ParseLexedAttributes();
741 }
742}
743
744/// \brief Finish parsing an attribute for which parsing was delayed.
745/// This will be called at the end of parsing a class declaration
746/// for each LateParsedAttribute. We consume the saved tokens and
747/// create an attribute with the arguments filled in. We add this
748/// to the Attribute list for the decl.
749void Parser::ParseLexedAttribute(LateParsedAttribute &LA) {
750 // Save the current token position.
751 SourceLocation OrigLoc = Tok.getLocation();
752
753 // Append the current token at the end of the new token stream so that it
754 // doesn't get lost.
755 LA.Toks.push_back(Tok);
756 PP.EnterTokenStream(LA.Toks.data(), LA.Toks.size(), true, false);
757 // Consume the previously pushed token.
758 ConsumeAnyToken();
759
760 ParsedAttributes Attrs(AttrFactory);
761 SourceLocation endLoc;
762
Caitlin Sadowski990d5712011-09-08 17:42:31 +0000763 // If the Decl is templatized, add template parameters to scope.
764 bool HasTemplateScope = LA.D && LA.D->isTemplateDecl();
765 ParseScope TempScope(this, Scope::TemplateParamScope, HasTemplateScope);
766 if (HasTemplateScope)
767 Actions.ActOnReenterTemplateScope(Actions.CurScope, LA.D);
768
769 // If the Decl is on a function, add function parameters to the scope.
770 bool HasFunctionScope = LA.D && LA.D->isFunctionOrFunctionTemplate();
771 ParseScope FnScope(this, Scope::FnScope|Scope::DeclScope, HasFunctionScope);
772 if (HasFunctionScope)
773 Actions.ActOnReenterFunctionContext(Actions.CurScope, LA.D);
774
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000775 ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, &endLoc);
776
Caitlin Sadowski990d5712011-09-08 17:42:31 +0000777 if (HasFunctionScope) {
778 Actions.ActOnExitFunctionContext();
779 FnScope.Exit(); // Pop scope, and remove Decls from IdResolver
780 }
781 if (HasTemplateScope) {
782 TempScope.Exit();
783 }
784
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000785 // Late parsed attributes must be attached to Decls by hand. If the
786 // LA.D is not set, then this was not done properly.
787 assert(LA.D && "No decl attached to late parsed attribute");
788 Actions.ActOnFinishDelayedAttribute(getCurScope(), LA.D, Attrs);
789
790 if (Tok.getLocation() != OrigLoc) {
791 // Due to a parsing error, we either went over the cached tokens or
792 // there are still cached tokens left, so we skip the leftover tokens.
793 // Since this is an uncommon situation that should be avoided, use the
794 // expensive isBeforeInTranslationUnit call.
795 if (PP.getSourceManager().isBeforeInTranslationUnit(Tok.getLocation(),
796 OrigLoc))
797 while (Tok.getLocation() != OrigLoc && Tok.isNot(tok::eof))
798 ConsumeAnyToken();
799 }
800}
801
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000802/// \brief Wrapper around a case statement checking if AttrName is
803/// one of the thread safety attributes
804bool Parser::IsThreadSafetyAttribute(llvm::StringRef AttrName){
805 return llvm::StringSwitch<bool>(AttrName)
806 .Case("guarded_by", true)
807 .Case("guarded_var", true)
808 .Case("pt_guarded_by", true)
809 .Case("pt_guarded_var", true)
810 .Case("lockable", true)
811 .Case("scoped_lockable", true)
812 .Case("no_thread_safety_analysis", true)
813 .Case("acquired_after", true)
814 .Case("acquired_before", true)
815 .Case("exclusive_lock_function", true)
816 .Case("shared_lock_function", true)
817 .Case("exclusive_trylock_function", true)
818 .Case("shared_trylock_function", true)
819 .Case("unlock_function", true)
820 .Case("lock_returned", true)
821 .Case("locks_excluded", true)
822 .Case("exclusive_locks_required", true)
823 .Case("shared_locks_required", true)
824 .Default(false);
825}
826
827/// \brief Parse the contents of thread safety attributes. These
828/// should always be parsed as an expression list.
829///
830/// We need to special case the parsing due to the fact that if the first token
831/// of the first argument is an identifier, the main parse loop will store
832/// that token as a "parameter" and the rest of
833/// the arguments will be added to a list of "arguments". However,
834/// subsequent tokens in the first argument are lost. We instead parse each
835/// argument as an expression and add all arguments to the list of "arguments".
836/// In future, we will take advantage of this special case to also
837/// deal with some argument scoping issues here (for example, referring to a
838/// function parameter in the attribute on that function).
839void Parser::ParseThreadSafetyAttribute(IdentifierInfo &AttrName,
840 SourceLocation AttrNameLoc,
841 ParsedAttributes &Attrs,
842 SourceLocation *EndLoc) {
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000843 assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('");
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000844
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000845 SourceLocation LeftParenLoc = Tok.getLocation();
846 ConsumeParen();
847
848 ExprVector ArgExprs(Actions);
849 bool ArgExprsOk = true;
850
851 // now parse the list of expressions
852 while (1) {
853 ExprResult ArgExpr(ParseAssignmentExpression());
854 if (ArgExpr.isInvalid()) {
855 ArgExprsOk = false;
856 MatchRHSPunctuation(tok::r_paren, LeftParenLoc);
857 break;
858 } else {
859 ArgExprs.push_back(ArgExpr.release());
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000860 }
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000861 if (Tok.isNot(tok::comma))
862 break;
863 ConsumeToken(); // Eat the comma, move to the next argument
864 }
865 // Match the ')'.
866 if (ArgExprsOk && Tok.is(tok::r_paren)) {
867 if (EndLoc)
868 *EndLoc = Tok.getLocation();
869 ConsumeParen();
870 Attrs.addNew(&AttrName, AttrNameLoc, 0, AttrNameLoc, 0, SourceLocation(),
871 ArgExprs.take(), ArgExprs.size());
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000872 }
873}
874
John McCall53fa7142010-12-24 02:08:15 +0000875void Parser::DiagnoseProhibitedAttributes(ParsedAttributesWithRange &attrs) {
876 Diag(attrs.Range.getBegin(), diag::err_attributes_not_allowed)
877 << attrs.Range;
Dawn Perchik335e16b2010-09-03 01:29:35 +0000878}
879
Chris Lattner53361ac2006-08-10 05:19:57 +0000880/// ParseDeclaration - Parse a full 'declaration', which consists of
881/// declaration-specifiers, some number of declarators, and a semicolon.
Chris Lattner49836b42009-04-02 04:16:50 +0000882/// 'Context' should be a Declarator::TheContext value. This returns the
883/// location of the semicolon in DeclEnd.
Chris Lattnera5235172007-08-25 06:57:03 +0000884///
885/// declaration: [C99 6.7]
886/// block-declaration ->
887/// simple-declaration
888/// others [FIXME]
Douglas Gregoreb31f392008-12-01 23:54:00 +0000889/// [C++] template-declaration
Chris Lattnera5235172007-08-25 06:57:03 +0000890/// [C++] namespace-definition
Douglas Gregord7c4d982008-12-30 03:27:21 +0000891/// [C++] using-directive
Douglas Gregor77b50e12009-06-22 23:06:13 +0000892/// [C++] using-declaration
Peter Collingbourne3d9cbdc2011-04-15 00:35:57 +0000893/// [C++0x/C1X] static_assert-declaration
Chris Lattnera5235172007-08-25 06:57:03 +0000894/// others... [FIXME]
895///
Fariborz Jahanian1db5c942010-09-28 20:42:35 +0000896Parser::DeclGroupPtrTy Parser::ParseDeclaration(StmtVector &Stmts,
897 unsigned Context,
Alexis Hunt96d5c762009-11-21 08:43:09 +0000898 SourceLocation &DeclEnd,
John McCall53fa7142010-12-24 02:08:15 +0000899 ParsedAttributesWithRange &attrs) {
Argyrios Kyrtzidis355094e2010-06-17 10:52:18 +0000900 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Fariborz Jahanian59b75282011-08-30 17:10:52 +0000901 // Must temporarily exit the objective-c container scope for
902 // parsing c none objective-c decls.
903 ObjCDeclContextSwitch ObjCDC(*this);
Argyrios Kyrtzidis355094e2010-06-17 10:52:18 +0000904
John McCall48871652010-08-21 09:40:31 +0000905 Decl *SingleDecl = 0;
Richard Smithcd1c0552011-07-01 19:46:12 +0000906 Decl *OwnedType = 0;
Chris Lattnera5235172007-08-25 06:57:03 +0000907 switch (Tok.getKind()) {
Douglas Gregoreb31f392008-12-01 23:54:00 +0000908 case tok::kw_template:
Douglas Gregor23996282009-05-12 21:31:51 +0000909 case tok::kw_export:
John McCall53fa7142010-12-24 02:08:15 +0000910 ProhibitAttributes(attrs);
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000911 SingleDecl = ParseDeclarationStartingWithTemplate(Context, DeclEnd);
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000912 break;
Sebastian Redl67667942010-08-27 23:12:46 +0000913 case tok::kw_inline:
Sebastian Redl5a5f2c72010-08-31 00:36:45 +0000914 // Could be the start of an inline namespace. Allowed as an ext in C++03.
915 if (getLang().CPlusPlus && NextToken().is(tok::kw_namespace)) {
John McCall53fa7142010-12-24 02:08:15 +0000916 ProhibitAttributes(attrs);
Sebastian Redl67667942010-08-27 23:12:46 +0000917 SourceLocation InlineLoc = ConsumeToken();
918 SingleDecl = ParseNamespace(Context, DeclEnd, InlineLoc);
919 break;
920 }
John McCall53fa7142010-12-24 02:08:15 +0000921 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs,
Fariborz Jahanian1db5c942010-09-28 20:42:35 +0000922 true);
Chris Lattnera5235172007-08-25 06:57:03 +0000923 case tok::kw_namespace:
John McCall53fa7142010-12-24 02:08:15 +0000924 ProhibitAttributes(attrs);
Chris Lattner49836b42009-04-02 04:16:50 +0000925 SingleDecl = ParseNamespace(Context, DeclEnd);
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000926 break;
Douglas Gregord7c4d982008-12-30 03:27:21 +0000927 case tok::kw_using:
John McCall9b72f892010-11-10 02:40:36 +0000928 SingleDecl = ParseUsingDirectiveOrDeclaration(Context, ParsedTemplateInfo(),
Richard Smithcd1c0552011-07-01 19:46:12 +0000929 DeclEnd, attrs, &OwnedType);
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000930 break;
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000931 case tok::kw_static_assert:
Peter Collingbourne3d9cbdc2011-04-15 00:35:57 +0000932 case tok::kw__Static_assert:
John McCall53fa7142010-12-24 02:08:15 +0000933 ProhibitAttributes(attrs);
Chris Lattner49836b42009-04-02 04:16:50 +0000934 SingleDecl = ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000935 break;
Chris Lattnera5235172007-08-25 06:57:03 +0000936 default:
John McCall53fa7142010-12-24 02:08:15 +0000937 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs, true);
Chris Lattnera5235172007-08-25 06:57:03 +0000938 }
Alexis Hunt96d5c762009-11-21 08:43:09 +0000939
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000940 // This routine returns a DeclGroup, if the thing we parsed only contains a
Richard Smithcd1c0552011-07-01 19:46:12 +0000941 // single decl, convert it now. Alias declarations can also declare a type;
942 // include that too if it is present.
943 return Actions.ConvertDeclToDeclGroup(SingleDecl, OwnedType);
Chris Lattnera5235172007-08-25 06:57:03 +0000944}
945
946/// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl]
947/// declaration-specifiers init-declarator-list[opt] ';'
948///[C90/C++]init-declarator-list ';' [TODO]
949/// [OMP] threadprivate-directive [TODO]
Chris Lattner32dc41c2009-03-29 17:27:48 +0000950///
Richard Smith02e85f32011-04-14 22:09:26 +0000951/// for-range-declaration: [C++0x 6.5p1: stmt.ranged]
952/// attribute-specifier-seq[opt] type-specifier-seq declarator
953///
Chris Lattner32dc41c2009-03-29 17:27:48 +0000954/// If RequireSemi is false, this does not check for a ';' at the end of the
Chris Lattner005fc1b2010-04-05 18:18:31 +0000955/// declaration. If it is true, it checks for and eats it.
Richard Smith02e85f32011-04-14 22:09:26 +0000956///
957/// If FRI is non-null, we might be parsing a for-range-declaration instead
958/// of a simple-declaration. If we find that we are, we also parse the
959/// for-range-initializer, and place it here.
Fariborz Jahanian1db5c942010-09-28 20:42:35 +0000960Parser::DeclGroupPtrTy Parser::ParseSimpleDeclaration(StmtVector &Stmts,
961 unsigned Context,
Alexis Hunt96d5c762009-11-21 08:43:09 +0000962 SourceLocation &DeclEnd,
John McCall53fa7142010-12-24 02:08:15 +0000963 ParsedAttributes &attrs,
Richard Smith02e85f32011-04-14 22:09:26 +0000964 bool RequireSemi,
965 ForRangeInit *FRI) {
Chris Lattner53361ac2006-08-10 05:19:57 +0000966 // Parse the common declaration-specifiers piece.
John McCall28a6aea2009-11-04 02:18:39 +0000967 ParsingDeclSpec DS(*this);
John McCall53fa7142010-12-24 02:08:15 +0000968 DS.takeAttributesFrom(attrs);
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000969
Douglas Gregor9de54ea2010-01-13 17:31:36 +0000970 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS_none,
Richard Smith30482bc2011-02-20 03:19:35 +0000971 getDeclSpecContextFromDeclaratorContext(Context));
Fariborz Jahanian1db5c942010-09-28 20:42:35 +0000972 StmtResult R = Actions.ActOnVlaStmt(DS);
973 if (R.isUsable())
974 Stmts.push_back(R.release());
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000975
Chris Lattner0e894622006-08-13 19:58:17 +0000976 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
977 // declaration-specifiers init-declarator-list[opt] ';'
Chris Lattner76c72282007-10-09 17:33:22 +0000978 if (Tok.is(tok::semi)) {
Chris Lattner005fc1b2010-04-05 18:18:31 +0000979 if (RequireSemi) ConsumeToken();
John McCall48871652010-08-21 09:40:31 +0000980 Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none,
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000981 DS);
John McCall28a6aea2009-11-04 02:18:39 +0000982 DS.complete(TheDecl);
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000983 return Actions.ConvertDeclToDeclGroup(TheDecl);
Chris Lattner0e894622006-08-13 19:58:17 +0000984 }
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000985
986 return ParseDeclGroup(DS, Context, /*FunctionDefs=*/ false, &DeclEnd, FRI);
John McCalld5a36322009-11-03 19:26:08 +0000987}
Mike Stump11289f42009-09-09 15:08:12 +0000988
John McCalld5a36322009-11-03 19:26:08 +0000989/// ParseDeclGroup - Having concluded that this is either a function
990/// definition or a group of object declarations, actually parse the
991/// result.
John McCall28a6aea2009-11-04 02:18:39 +0000992Parser::DeclGroupPtrTy Parser::ParseDeclGroup(ParsingDeclSpec &DS,
993 unsigned Context,
John McCalld5a36322009-11-03 19:26:08 +0000994 bool AllowFunctionDefinitions,
Richard Smith02e85f32011-04-14 22:09:26 +0000995 SourceLocation *DeclEnd,
996 ForRangeInit *FRI) {
John McCalld5a36322009-11-03 19:26:08 +0000997 // Parse the first declarator.
John McCall28a6aea2009-11-04 02:18:39 +0000998 ParsingDeclarator D(*this, DS, static_cast<Declarator::TheContext>(Context));
John McCalld5a36322009-11-03 19:26:08 +0000999 ParseDeclarator(D);
Chris Lattner32dc41c2009-03-29 17:27:48 +00001000
John McCalld5a36322009-11-03 19:26:08 +00001001 // Bail out if the first declarator didn't seem well-formed.
1002 if (!D.hasName() && !D.mayOmitIdentifier()) {
1003 // Skip until ; or }.
1004 SkipUntil(tok::r_brace, true, true);
1005 if (Tok.is(tok::semi))
1006 ConsumeToken();
1007 return DeclGroupPtrTy();
Chris Lattnerefb0f112009-03-29 17:18:04 +00001008 }
Mike Stump11289f42009-09-09 15:08:12 +00001009
Chris Lattnerdbb1e932010-07-11 22:24:20 +00001010 // Check to see if we have a function *definition* which must have a body.
1011 if (AllowFunctionDefinitions && D.isFunctionDeclarator() &&
1012 // Look at the next token to make sure that this isn't a function
1013 // declaration. We have to check this because __attribute__ might be the
1014 // start of a function definition in GCC-extended K&R C.
1015 !isDeclarationAfterDeclarator()) {
1016
Chris Lattner13901342010-07-11 22:42:07 +00001017 if (isStartOfFunctionDefinition(D)) {
John McCalld5a36322009-11-03 19:26:08 +00001018 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
1019 Diag(Tok, diag::err_function_declared_typedef);
1020
1021 // Recover by treating the 'typedef' as spurious.
1022 DS.ClearStorageClassSpecs();
1023 }
1024
John McCall48871652010-08-21 09:40:31 +00001025 Decl *TheDecl = ParseFunctionDefinition(D);
John McCalld5a36322009-11-03 19:26:08 +00001026 return Actions.ConvertDeclToDeclGroup(TheDecl);
Chris Lattner13901342010-07-11 22:42:07 +00001027 }
1028
1029 if (isDeclarationSpecifier()) {
1030 // If there is an invalid declaration specifier right after the function
1031 // prototype, then we must be in a missing semicolon case where this isn't
1032 // actually a body. Just fall through into the code that handles it as a
1033 // prototype, and let the top-level code handle the erroneous declspec
1034 // where it would otherwise expect a comma or semicolon.
John McCalld5a36322009-11-03 19:26:08 +00001035 } else {
1036 Diag(Tok, diag::err_expected_fn_body);
1037 SkipUntil(tok::semi);
1038 return DeclGroupPtrTy();
1039 }
1040 }
1041
Richard Smith02e85f32011-04-14 22:09:26 +00001042 if (ParseAttributesAfterDeclarator(D))
1043 return DeclGroupPtrTy();
1044
1045 // C++0x [stmt.iter]p1: Check if we have a for-range-declarator. If so, we
1046 // must parse and analyze the for-range-initializer before the declaration is
1047 // analyzed.
1048 if (FRI && Tok.is(tok::colon)) {
1049 FRI->ColonLoc = ConsumeToken();
Sebastian Redl3da34892011-06-05 12:23:16 +00001050 if (Tok.is(tok::l_brace))
1051 FRI->RangeExpr = ParseBraceInitializer();
1052 else
1053 FRI->RangeExpr = ParseExpression();
Richard Smith02e85f32011-04-14 22:09:26 +00001054 Decl *ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
1055 Actions.ActOnCXXForRangeDecl(ThisDecl);
1056 Actions.FinalizeDeclaration(ThisDecl);
1057 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS, &ThisDecl, 1);
1058 }
1059
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001060 SmallVector<Decl *, 8> DeclsInGroup;
Richard Smith02e85f32011-04-14 22:09:26 +00001061 Decl *FirstDecl = ParseDeclarationAfterDeclaratorAndAttributes(D);
John McCall28a6aea2009-11-04 02:18:39 +00001062 D.complete(FirstDecl);
John McCall48871652010-08-21 09:40:31 +00001063 if (FirstDecl)
John McCalld5a36322009-11-03 19:26:08 +00001064 DeclsInGroup.push_back(FirstDecl);
1065
1066 // If we don't have a comma, it is either the end of the list (a ';') or an
1067 // error, bail out.
1068 while (Tok.is(tok::comma)) {
1069 // Consume the comma.
Chris Lattnerefb0f112009-03-29 17:18:04 +00001070 ConsumeToken();
John McCalld5a36322009-11-03 19:26:08 +00001071
1072 // Parse the next declarator.
1073 D.clear();
1074
1075 // Accept attributes in an init-declarator. In the first declarator in a
1076 // declaration, these would be part of the declspec. In subsequent
1077 // declarators, they become part of the declarator itself, so that they
1078 // don't apply to declarators after *this* one. Examples:
1079 // short __attribute__((common)) var; -> declspec
1080 // short var __attribute__((common)); -> declarator
1081 // short x, __attribute__((common)) var; -> declarator
John McCall53fa7142010-12-24 02:08:15 +00001082 MaybeParseGNUAttributes(D);
John McCalld5a36322009-11-03 19:26:08 +00001083
1084 ParseDeclarator(D);
1085
John McCall48871652010-08-21 09:40:31 +00001086 Decl *ThisDecl = ParseDeclarationAfterDeclarator(D);
John McCall28a6aea2009-11-04 02:18:39 +00001087 D.complete(ThisDecl);
John McCall48871652010-08-21 09:40:31 +00001088 if (ThisDecl)
John McCalld5a36322009-11-03 19:26:08 +00001089 DeclsInGroup.push_back(ThisDecl);
1090 }
1091
1092 if (DeclEnd)
1093 *DeclEnd = Tok.getLocation();
1094
1095 if (Context != Declarator::ForContext &&
1096 ExpectAndConsume(tok::semi,
1097 Context == Declarator::FileContext
1098 ? diag::err_invalid_token_after_toplevel_declarator
1099 : diag::err_expected_semi_declaration)) {
Chris Lattner13901342010-07-11 22:42:07 +00001100 // Okay, there was no semicolon and one was expected. If we see a
1101 // declaration specifier, just assume it was missing and continue parsing.
1102 // Otherwise things are very confused and we skip to recover.
1103 if (!isDeclarationSpecifier()) {
1104 SkipUntil(tok::r_brace, true, true);
1105 if (Tok.is(tok::semi))
1106 ConsumeToken();
1107 }
John McCalld5a36322009-11-03 19:26:08 +00001108 }
1109
Douglas Gregor0be31a22010-07-02 17:43:08 +00001110 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS,
John McCalld5a36322009-11-03 19:26:08 +00001111 DeclsInGroup.data(),
1112 DeclsInGroup.size());
Chris Lattner53361ac2006-08-10 05:19:57 +00001113}
1114
Richard Smith02e85f32011-04-14 22:09:26 +00001115/// Parse an optional simple-asm-expr and attributes, and attach them to a
1116/// declarator. Returns true on an error.
1117bool Parser::ParseAttributesAfterDeclarator(Declarator &D) {
1118 // If a simple-asm-expr is present, parse it.
1119 if (Tok.is(tok::kw_asm)) {
1120 SourceLocation Loc;
1121 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
1122 if (AsmLabel.isInvalid()) {
1123 SkipUntil(tok::semi, true, true);
1124 return true;
1125 }
1126
1127 D.setAsmLabel(AsmLabel.release());
1128 D.SetRangeEnd(Loc);
1129 }
1130
1131 MaybeParseGNUAttributes(D);
1132 return false;
1133}
1134
Douglas Gregor23996282009-05-12 21:31:51 +00001135/// \brief Parse 'declaration' after parsing 'declaration-specifiers
1136/// declarator'. This method parses the remainder of the declaration
1137/// (including any attributes or initializer, among other things) and
1138/// finalizes the declaration.
Chris Lattnerf0f3baa2006-08-14 00:15:20 +00001139///
Chris Lattnerf0f3baa2006-08-14 00:15:20 +00001140/// init-declarator: [C99 6.7]
1141/// declarator
1142/// declarator '=' initializer
Chris Lattner6d7e6342006-08-15 03:41:14 +00001143/// [GNU] declarator simple-asm-expr[opt] attributes[opt]
1144/// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00001145/// [C++] declarator initializer[opt]
1146///
1147/// [C++] initializer:
1148/// [C++] '=' initializer-clause
1149/// [C++] '(' expression-list ')'
Sebastian Redlf769df52009-03-24 22:27:57 +00001150/// [C++0x] '=' 'default' [TODO]
1151/// [C++0x] '=' 'delete'
Sebastian Redl3da34892011-06-05 12:23:16 +00001152/// [C++0x] braced-init-list
Sebastian Redlf769df52009-03-24 22:27:57 +00001153///
1154/// According to the standard grammar, =default and =delete are function
1155/// definitions, but that definitely doesn't fit with the parser here.
Chris Lattnerf0f3baa2006-08-14 00:15:20 +00001156///
John McCall48871652010-08-21 09:40:31 +00001157Decl *Parser::ParseDeclarationAfterDeclarator(Declarator &D,
Douglas Gregorb52fabb2009-06-23 23:11:28 +00001158 const ParsedTemplateInfo &TemplateInfo) {
Richard Smith02e85f32011-04-14 22:09:26 +00001159 if (ParseAttributesAfterDeclarator(D))
1160 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00001161
Richard Smith02e85f32011-04-14 22:09:26 +00001162 return ParseDeclarationAfterDeclaratorAndAttributes(D, TemplateInfo);
1163}
Mike Stump11289f42009-09-09 15:08:12 +00001164
Richard Smith02e85f32011-04-14 22:09:26 +00001165Decl *Parser::ParseDeclarationAfterDeclaratorAndAttributes(Declarator &D,
1166 const ParsedTemplateInfo &TemplateInfo) {
Douglas Gregor23996282009-05-12 21:31:51 +00001167 // Inform the current actions module that we just parsed this declarator.
John McCall48871652010-08-21 09:40:31 +00001168 Decl *ThisDecl = 0;
Douglas Gregor450f00842009-09-25 18:43:00 +00001169 switch (TemplateInfo.Kind) {
1170 case ParsedTemplateInfo::NonTemplate:
Douglas Gregor0be31a22010-07-02 17:43:08 +00001171 ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
Douglas Gregor450f00842009-09-25 18:43:00 +00001172 break;
1173
1174 case ParsedTemplateInfo::Template:
1175 case ParsedTemplateInfo::ExplicitSpecialization:
Douglas Gregor0be31a22010-07-02 17:43:08 +00001176 ThisDecl = Actions.ActOnTemplateDeclarator(getCurScope(),
John McCallfaf5fb42010-08-26 23:41:50 +00001177 MultiTemplateParamsArg(Actions,
Douglas Gregorb52fabb2009-06-23 23:11:28 +00001178 TemplateInfo.TemplateParams->data(),
1179 TemplateInfo.TemplateParams->size()),
Douglas Gregor450f00842009-09-25 18:43:00 +00001180 D);
1181 break;
1182
1183 case ParsedTemplateInfo::ExplicitInstantiation: {
John McCall48871652010-08-21 09:40:31 +00001184 DeclResult ThisRes
Douglas Gregor0be31a22010-07-02 17:43:08 +00001185 = Actions.ActOnExplicitInstantiation(getCurScope(),
Douglas Gregor450f00842009-09-25 18:43:00 +00001186 TemplateInfo.ExternLoc,
1187 TemplateInfo.TemplateLoc,
1188 D);
1189 if (ThisRes.isInvalid()) {
1190 SkipUntil(tok::semi, true, true);
John McCall48871652010-08-21 09:40:31 +00001191 return 0;
Douglas Gregor450f00842009-09-25 18:43:00 +00001192 }
1193
1194 ThisDecl = ThisRes.get();
1195 break;
1196 }
1197 }
Mike Stump11289f42009-09-09 15:08:12 +00001198
Richard Smith30482bc2011-02-20 03:19:35 +00001199 bool TypeContainsAuto =
1200 D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto;
1201
Douglas Gregor23996282009-05-12 21:31:51 +00001202 // Parse declarator '=' initializer.
Argyrios Kyrtzidisb5c7c512010-10-08 02:39:23 +00001203 if (isTokenEqualOrMistypedEqualEqual(
1204 diag::err_invalid_equalequal_after_declarator)) {
Douglas Gregor23996282009-05-12 21:31:51 +00001205 ConsumeToken();
Anders Carlsson991285e2010-09-24 21:25:25 +00001206 if (Tok.is(tok::kw_delete)) {
Alexis Hunt5a7fa252011-05-12 06:15:49 +00001207 if (D.isFunctionDeclarator())
1208 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
1209 << 1 /* delete */;
1210 else
1211 Diag(ConsumeToken(), diag::err_deleted_non_function);
Alexis Hunt5dafebc2011-05-06 01:42:00 +00001212 } else if (Tok.is(tok::kw_default)) {
Alexis Hunt5a7fa252011-05-12 06:15:49 +00001213 if (D.isFunctionDeclarator())
1214 Diag(Tok, diag::err_default_delete_in_multiple_declaration)
1215 << 1 /* delete */;
1216 else
1217 Diag(ConsumeToken(), diag::err_default_special_members);
Douglas Gregor23996282009-05-12 21:31:51 +00001218 } else {
John McCall1f4ee7b2009-12-19 09:28:58 +00001219 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
1220 EnterScope(0);
Douglas Gregor0be31a22010-07-02 17:43:08 +00001221 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
John McCall1f4ee7b2009-12-19 09:28:58 +00001222 }
Argyrios Kyrtzidis3df19782009-06-17 22:50:06 +00001223
Douglas Gregor7aa6b222010-05-30 01:49:25 +00001224 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001225 Actions.CodeCompleteInitializer(getCurScope(), ThisDecl);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001226 cutOffParsing();
1227 return 0;
Douglas Gregor7aa6b222010-05-30 01:49:25 +00001228 }
1229
John McCalldadc5752010-08-24 06:29:42 +00001230 ExprResult Init(ParseInitializer());
Argyrios Kyrtzidis3df19782009-06-17 22:50:06 +00001231
John McCall1f4ee7b2009-12-19 09:28:58 +00001232 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001233 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
John McCall1f4ee7b2009-12-19 09:28:58 +00001234 ExitScope();
1235 }
Argyrios Kyrtzidis3df19782009-06-17 22:50:06 +00001236
Douglas Gregor23996282009-05-12 21:31:51 +00001237 if (Init.isInvalid()) {
Douglas Gregor604c3022010-03-01 18:27:54 +00001238 SkipUntil(tok::comma, true, true);
1239 Actions.ActOnInitializerError(ThisDecl);
1240 } else
Richard Smith30482bc2011-02-20 03:19:35 +00001241 Actions.AddInitializerToDecl(ThisDecl, Init.take(),
1242 /*DirectInit=*/false, TypeContainsAuto);
Douglas Gregor23996282009-05-12 21:31:51 +00001243 }
1244 } else if (Tok.is(tok::l_paren)) {
1245 // Parse C++ direct initializer: '(' expression-list ')'
1246 SourceLocation LParenLoc = ConsumeParen();
1247 ExprVector Exprs(Actions);
1248 CommaLocsTy CommaLocs;
1249
Douglas Gregor613bf102009-12-22 17:47:17 +00001250 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
1251 EnterScope(0);
Douglas Gregor0be31a22010-07-02 17:43:08 +00001252 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregor613bf102009-12-22 17:47:17 +00001253 }
1254
Douglas Gregor23996282009-05-12 21:31:51 +00001255 if (ParseExpressionList(Exprs, CommaLocs)) {
1256 SkipUntil(tok::r_paren);
Douglas Gregor613bf102009-12-22 17:47:17 +00001257
1258 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001259 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregor613bf102009-12-22 17:47:17 +00001260 ExitScope();
1261 }
Douglas Gregor23996282009-05-12 21:31:51 +00001262 } else {
1263 // Match the ')'.
1264 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
1265
1266 assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() &&
1267 "Unexpected number of commas!");
Douglas Gregor613bf102009-12-22 17:47:17 +00001268
1269 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001270 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregor613bf102009-12-22 17:47:17 +00001271 ExitScope();
1272 }
1273
Douglas Gregor23996282009-05-12 21:31:51 +00001274 Actions.AddCXXDirectInitializerToDecl(ThisDecl, LParenLoc,
1275 move_arg(Exprs),
Richard Smith30482bc2011-02-20 03:19:35 +00001276 RParenLoc,
1277 TypeContainsAuto);
Douglas Gregor23996282009-05-12 21:31:51 +00001278 }
Sebastian Redl3da34892011-06-05 12:23:16 +00001279 } else if (getLang().CPlusPlus0x && Tok.is(tok::l_brace)) {
1280 // Parse C++0x braced-init-list.
1281 if (D.getCXXScopeSpec().isSet()) {
1282 EnterScope(0);
1283 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
1284 }
1285
1286 ExprResult Init(ParseBraceInitializer());
1287
1288 if (D.getCXXScopeSpec().isSet()) {
1289 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
1290 ExitScope();
1291 }
1292
1293 if (Init.isInvalid()) {
1294 Actions.ActOnInitializerError(ThisDecl);
1295 } else
1296 Actions.AddInitializerToDecl(ThisDecl, Init.take(),
1297 /*DirectInit=*/true, TypeContainsAuto);
1298
Douglas Gregor23996282009-05-12 21:31:51 +00001299 } else {
Richard Smith30482bc2011-02-20 03:19:35 +00001300 Actions.ActOnUninitializedDecl(ThisDecl, TypeContainsAuto);
Douglas Gregor23996282009-05-12 21:31:51 +00001301 }
1302
Richard Smithb2bc2e62011-02-21 20:05:19 +00001303 Actions.FinalizeDeclaration(ThisDecl);
1304
Douglas Gregor23996282009-05-12 21:31:51 +00001305 return ThisDecl;
1306}
1307
Chris Lattner1890ac82006-08-13 01:16:23 +00001308/// ParseSpecifierQualifierList
1309/// specifier-qualifier-list:
1310/// type-specifier specifier-qualifier-list[opt]
1311/// type-qualifier specifier-qualifier-list[opt]
Chris Lattnere37e2332006-08-15 04:50:22 +00001312/// [GNU] attributes specifier-qualifier-list[opt]
Chris Lattner1890ac82006-08-13 01:16:23 +00001313///
Richard Smithcd1c0552011-07-01 19:46:12 +00001314void Parser::ParseSpecifierQualifierList(DeclSpec &DS, AccessSpecifier AS) {
Chris Lattner1890ac82006-08-13 01:16:23 +00001315 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
1316 /// parse declaration-specifiers and complain about extra stuff.
Richard Smithcd1c0552011-07-01 19:46:12 +00001317 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS);
Mike Stump11289f42009-09-09 15:08:12 +00001318
Chris Lattner1890ac82006-08-13 01:16:23 +00001319 // Validate declspec for type-name.
1320 unsigned Specs = DS.getParsedSpecifiers();
Chris Lattnera723ba92009-04-14 21:16:09 +00001321 if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers() &&
John McCall53fa7142010-12-24 02:08:15 +00001322 !DS.hasAttributes())
Chris Lattner1890ac82006-08-13 01:16:23 +00001323 Diag(Tok, diag::err_typename_requires_specqual);
Mike Stump11289f42009-09-09 15:08:12 +00001324
Chris Lattner1b22eed2006-11-28 05:12:07 +00001325 // Issue diagnostic and remove storage class if present.
Chris Lattner1890ac82006-08-13 01:16:23 +00001326 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
Chris Lattner1b22eed2006-11-28 05:12:07 +00001327 if (DS.getStorageClassSpecLoc().isValid())
1328 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
1329 else
1330 Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass);
Chris Lattnera925dc62006-11-28 04:33:46 +00001331 DS.ClearStorageClassSpecs();
Chris Lattner1890ac82006-08-13 01:16:23 +00001332 }
Mike Stump11289f42009-09-09 15:08:12 +00001333
Chris Lattner1b22eed2006-11-28 05:12:07 +00001334 // Issue diagnostic and remove function specfier if present.
Chris Lattner1890ac82006-08-13 01:16:23 +00001335 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Douglas Gregor61956c42008-10-31 09:07:45 +00001336 if (DS.isInlineSpecified())
1337 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
1338 if (DS.isVirtualSpecified())
1339 Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec);
1340 if (DS.isExplicitSpecified())
1341 Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec);
Chris Lattnera925dc62006-11-28 04:33:46 +00001342 DS.ClearFunctionSpecs();
Chris Lattner1890ac82006-08-13 01:16:23 +00001343 }
1344}
Chris Lattner53361ac2006-08-10 05:19:57 +00001345
Chris Lattner6cc055a2009-04-12 20:42:31 +00001346/// isValidAfterIdentifierInDeclaratorAfterDeclSpec - Return true if the
1347/// specified token is valid after the identifier in a declarator which
1348/// immediately follows the declspec. For example, these things are valid:
1349///
1350/// int x [ 4]; // direct-declarator
1351/// int x ( int y); // direct-declarator
1352/// int(int x ) // direct-declarator
1353/// int x ; // simple-declaration
1354/// int x = 17; // init-declarator-list
1355/// int x , y; // init-declarator-list
1356/// int x __asm__ ("foo"); // init-declarator-list
Chris Lattnera723ba92009-04-14 21:16:09 +00001357/// int x : 4; // struct-declarator
Chris Lattner2b988c12009-04-12 22:29:43 +00001358/// int x { 5}; // C++'0x unified initializers
Chris Lattner6cc055a2009-04-12 20:42:31 +00001359///
1360/// This is not, because 'x' does not immediately follow the declspec (though
1361/// ')' happens to be valid anyway).
1362/// int (x)
1363///
1364static bool isValidAfterIdentifierInDeclarator(const Token &T) {
1365 return T.is(tok::l_square) || T.is(tok::l_paren) || T.is(tok::r_paren) ||
1366 T.is(tok::semi) || T.is(tok::comma) || T.is(tok::equal) ||
Chris Lattnera723ba92009-04-14 21:16:09 +00001367 T.is(tok::kw_asm) || T.is(tok::l_brace) || T.is(tok::colon);
Chris Lattner6cc055a2009-04-12 20:42:31 +00001368}
1369
Chris Lattner20a0c612009-04-14 21:34:55 +00001370
1371/// ParseImplicitInt - This method is called when we have an non-typename
1372/// identifier in a declspec (which normally terminates the decl spec) when
1373/// the declspec has no type specifier. In this case, the declspec is either
1374/// malformed or is "implicit int" (in K&R and C89).
1375///
1376/// This method handles diagnosing this prettily and returns false if the
1377/// declspec is done being processed. If it recovers and thinks there may be
1378/// other pieces of declspec after it, it returns true.
1379///
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001380bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS,
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001381 const ParsedTemplateInfo &TemplateInfo,
Chris Lattner20a0c612009-04-14 21:34:55 +00001382 AccessSpecifier AS) {
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001383 assert(Tok.is(tok::identifier) && "should have identifier");
Mike Stump11289f42009-09-09 15:08:12 +00001384
Chris Lattner20a0c612009-04-14 21:34:55 +00001385 SourceLocation Loc = Tok.getLocation();
1386 // If we see an identifier that is not a type name, we normally would
1387 // parse it as the identifer being declared. However, when a typename
1388 // is typo'd or the definition is not included, this will incorrectly
1389 // parse the typename as the identifier name and fall over misparsing
1390 // later parts of the diagnostic.
1391 //
1392 // As such, we try to do some look-ahead in cases where this would
1393 // otherwise be an "implicit-int" case to see if this is invalid. For
1394 // example: "static foo_t x = 4;" In this case, if we parsed foo_t as
1395 // an identifier with implicit int, we'd get a parse error because the
1396 // next token is obviously invalid for a type. Parse these as a case
1397 // with an invalid type specifier.
1398 assert(!DS.hasTypeSpecifier() && "Type specifier checked above");
Mike Stump11289f42009-09-09 15:08:12 +00001399
Chris Lattner20a0c612009-04-14 21:34:55 +00001400 // Since we know that this either implicit int (which is rare) or an
1401 // error, we'd do lookahead to try to do better recovery.
1402 if (isValidAfterIdentifierInDeclarator(NextToken())) {
1403 // If this token is valid for implicit int, e.g. "static x = 4", then
1404 // we just avoid eating the identifier, so it will be parsed as the
1405 // identifier in the declarator.
1406 return false;
1407 }
Mike Stump11289f42009-09-09 15:08:12 +00001408
Chris Lattner20a0c612009-04-14 21:34:55 +00001409 // Otherwise, if we don't consume this token, we are going to emit an
1410 // error anyway. Try to recover from various common problems. Check
1411 // to see if this was a reference to a tag name without a tag specified.
1412 // This is a common problem in C (saying 'foo' instead of 'struct foo').
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001413 //
1414 // C++ doesn't need this, and isTagName doesn't take SS.
1415 if (SS == 0) {
Argyrios Kyrtzidis1f329402011-04-21 17:29:47 +00001416 const char *TagName = 0, *FixitTagName = 0;
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001417 tok::TokenKind TagKind = tok::unknown;
Mike Stump11289f42009-09-09 15:08:12 +00001418
Douglas Gregor0be31a22010-07-02 17:43:08 +00001419 switch (Actions.isTagName(*Tok.getIdentifierInfo(), getCurScope())) {
Chris Lattner20a0c612009-04-14 21:34:55 +00001420 default: break;
Argyrios Kyrtzidis1f329402011-04-21 17:29:47 +00001421 case DeclSpec::TST_enum:
1422 TagName="enum" ; FixitTagName = "enum " ; TagKind=tok::kw_enum ;break;
1423 case DeclSpec::TST_union:
1424 TagName="union" ; FixitTagName = "union " ;TagKind=tok::kw_union ;break;
1425 case DeclSpec::TST_struct:
1426 TagName="struct"; FixitTagName = "struct ";TagKind=tok::kw_struct;break;
1427 case DeclSpec::TST_class:
1428 TagName="class" ; FixitTagName = "class " ;TagKind=tok::kw_class ;break;
Chris Lattner20a0c612009-04-14 21:34:55 +00001429 }
Mike Stump11289f42009-09-09 15:08:12 +00001430
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001431 if (TagName) {
1432 Diag(Loc, diag::err_use_of_tag_name_without_tag)
John McCall38200b02010-02-14 01:03:10 +00001433 << Tok.getIdentifierInfo() << TagName << getLang().CPlusPlus
Argyrios Kyrtzidis1f329402011-04-21 17:29:47 +00001434 << FixItHint::CreateInsertion(Tok.getLocation(),FixitTagName);
Mike Stump11289f42009-09-09 15:08:12 +00001435
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001436 // Parse this as a tag as if the missing tag were present.
1437 if (TagKind == tok::kw_enum)
Douglas Gregordc70c3a2010-03-02 17:53:14 +00001438 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS);
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001439 else
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001440 ParseClassSpecifier(TagKind, Loc, DS, TemplateInfo, AS);
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001441 return true;
1442 }
Chris Lattner20a0c612009-04-14 21:34:55 +00001443 }
Mike Stump11289f42009-09-09 15:08:12 +00001444
Douglas Gregor15e56022009-10-13 23:27:22 +00001445 // This is almost certainly an invalid type name. Let the action emit a
1446 // diagnostic and attempt to recover.
John McCallba7bf592010-08-24 05:47:05 +00001447 ParsedType T;
Douglas Gregor15e56022009-10-13 23:27:22 +00001448 if (Actions.DiagnoseUnknownTypeName(*Tok.getIdentifierInfo(), Loc,
Douglas Gregor0be31a22010-07-02 17:43:08 +00001449 getCurScope(), SS, T)) {
Douglas Gregor15e56022009-10-13 23:27:22 +00001450 // The action emitted a diagnostic, so we don't have to.
1451 if (T) {
1452 // The action has suggested that the type T could be used. Set that as
1453 // the type in the declaration specifiers, consume the would-be type
1454 // name token, and we're done.
1455 const char *PrevSpec;
1456 unsigned DiagID;
John McCallba7bf592010-08-24 05:47:05 +00001457 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID, T);
Douglas Gregor15e56022009-10-13 23:27:22 +00001458 DS.SetRangeEnd(Tok.getLocation());
1459 ConsumeToken();
1460
1461 // There may be other declaration specifiers after this.
1462 return true;
1463 }
1464
1465 // Fall through; the action had no suggestion for us.
1466 } else {
1467 // The action did not emit a diagnostic, so emit one now.
1468 SourceRange R;
1469 if (SS) R = SS->getRange();
1470 Diag(Loc, diag::err_unknown_typename) << Tok.getIdentifierInfo() << R;
1471 }
Mike Stump11289f42009-09-09 15:08:12 +00001472
Douglas Gregor15e56022009-10-13 23:27:22 +00001473 // Mark this as an error.
Chris Lattner20a0c612009-04-14 21:34:55 +00001474 const char *PrevSpec;
John McCall49bfce42009-08-03 20:12:06 +00001475 unsigned DiagID;
1476 DS.SetTypeSpecType(DeclSpec::TST_error, Loc, PrevSpec, DiagID);
Chris Lattner20a0c612009-04-14 21:34:55 +00001477 DS.SetRangeEnd(Tok.getLocation());
1478 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001479
Chris Lattner20a0c612009-04-14 21:34:55 +00001480 // TODO: Could inject an invalid typedef decl in an enclosing scope to
1481 // avoid rippling error messages on subsequent uses of the same type,
1482 // could be useful if #include was forgotten.
1483 return false;
1484}
1485
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001486/// \brief Determine the declaration specifier context from the declarator
1487/// context.
1488///
1489/// \param Context the declarator context, which is one of the
1490/// Declarator::TheContext enumerator values.
1491Parser::DeclSpecContext
1492Parser::getDeclSpecContextFromDeclaratorContext(unsigned Context) {
1493 if (Context == Declarator::MemberContext)
1494 return DSC_class;
1495 if (Context == Declarator::FileContext)
1496 return DSC_top_level;
1497 return DSC_normal;
1498}
1499
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001500/// ParseDeclarationSpecifiers
1501/// declaration-specifiers: [C99 6.7]
Chris Lattner3b561a32006-08-13 00:12:11 +00001502/// storage-class-specifier declaration-specifiers[opt]
1503/// type-specifier declaration-specifiers[opt]
Chris Lattner3b561a32006-08-13 00:12:11 +00001504/// [C99] function-specifier declaration-specifiers[opt]
Chris Lattnere37e2332006-08-15 04:50:22 +00001505/// [GNU] attributes declaration-specifiers[opt]
Douglas Gregor26701a42011-09-09 02:06:17 +00001506/// [Clang] '__module_private__' declaration-specifiers[opt]
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001507///
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001508/// storage-class-specifier: [C99 6.7.1]
Chris Lattnerda48a8e2006-08-04 05:25:55 +00001509/// 'typedef'
1510/// 'extern'
1511/// 'static'
1512/// 'auto'
1513/// 'register'
Sebastian Redlccdfaba2008-11-14 23:42:31 +00001514/// [C++] 'mutable'
Chris Lattnerda48a8e2006-08-04 05:25:55 +00001515/// [GNU] '__thread'
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001516/// function-specifier: [C99 6.7.4]
Chris Lattner3b561a32006-08-13 00:12:11 +00001517/// [C99] 'inline'
Douglas Gregor61956c42008-10-31 09:07:45 +00001518/// [C++] 'virtual'
1519/// [C++] 'explicit'
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00001520/// [OpenCL] '__kernel'
Anders Carlssoncd8db412009-05-06 04:46:28 +00001521/// 'friend': [C++ dcl.friend]
Sebastian Redl39c2a8b2009-11-05 15:47:02 +00001522/// 'constexpr': [C++0x dcl.constexpr]
Anders Carlssoncd8db412009-05-06 04:46:28 +00001523
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001524///
Douglas Gregorb9bd8a92008-12-24 02:52:09 +00001525void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001526 const ParsedTemplateInfo &TemplateInfo,
John McCall07e91c02009-08-06 02:15:43 +00001527 AccessSpecifier AS,
Douglas Gregor0e7dde52011-04-24 05:37:28 +00001528 DeclSpecContext DSContext) {
1529 if (DS.getSourceRange().isInvalid()) {
1530 DS.SetRangeStart(Tok.getLocation());
1531 DS.SetRangeEnd(Tok.getLocation());
1532 }
1533
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001534 while (1) {
John McCall49bfce42009-08-03 20:12:06 +00001535 bool isInvalid = false;
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001536 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00001537 unsigned DiagID = 0;
1538
Chris Lattner4d8f8732006-11-28 05:05:08 +00001539 SourceLocation Loc = Tok.getLocation();
Douglas Gregor450c75a2008-11-07 15:42:26 +00001540
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001541 switch (Tok.getKind()) {
Mike Stump11289f42009-09-09 15:08:12 +00001542 default:
Chris Lattner0974b232008-07-26 00:20:22 +00001543 DoneWithDeclSpec:
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001544 // If this is not a declaration specifier token, we're done reading decl
1545 // specifiers. First verify that DeclSpec's are consistent.
Douglas Gregore3e01a22009-04-01 22:41:11 +00001546 DS.Finish(Diags, PP);
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001547 return;
Mike Stump11289f42009-09-09 15:08:12 +00001548
Douglas Gregorc49f5b22010-08-23 18:23:48 +00001549 case tok::code_completion: {
John McCallfaf5fb42010-08-26 23:41:50 +00001550 Sema::ParserCompletionContext CCC = Sema::PCC_Namespace;
Douglas Gregorc49f5b22010-08-23 18:23:48 +00001551 if (DS.hasTypeSpecifier()) {
1552 bool AllowNonIdentifiers
1553 = (getCurScope()->getFlags() & (Scope::ControlScope |
1554 Scope::BlockScope |
1555 Scope::TemplateParamScope |
1556 Scope::FunctionPrototypeScope |
1557 Scope::AtCatchScope)) == 0;
1558 bool AllowNestedNameSpecifiers
1559 = DSContext == DSC_top_level ||
1560 (DSContext == DSC_class && DS.isFriendSpecified());
1561
Douglas Gregorbfcea8b2010-09-16 15:14:18 +00001562 Actions.CodeCompleteDeclSpec(getCurScope(), DS,
1563 AllowNonIdentifiers,
1564 AllowNestedNameSpecifiers);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001565 return cutOffParsing();
Douglas Gregorc49f5b22010-08-23 18:23:48 +00001566 }
1567
Douglas Gregor80039242011-02-15 20:33:25 +00001568 if (getCurScope()->getFnParent() || getCurScope()->getBlockParent())
1569 CCC = Sema::PCC_LocalDeclarationSpecifiers;
1570 else if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate)
John McCallfaf5fb42010-08-26 23:41:50 +00001571 CCC = DSContext == DSC_class? Sema::PCC_MemberTemplate
1572 : Sema::PCC_Template;
Douglas Gregorc49f5b22010-08-23 18:23:48 +00001573 else if (DSContext == DSC_class)
John McCallfaf5fb42010-08-26 23:41:50 +00001574 CCC = Sema::PCC_Class;
Douglas Gregorc49f5b22010-08-23 18:23:48 +00001575 else if (ObjCImpDecl)
John McCallfaf5fb42010-08-26 23:41:50 +00001576 CCC = Sema::PCC_ObjCImplementation;
Douglas Gregorc49f5b22010-08-23 18:23:48 +00001577
1578 Actions.CodeCompleteOrdinaryName(getCurScope(), CCC);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001579 return cutOffParsing();
Douglas Gregorc49f5b22010-08-23 18:23:48 +00001580 }
1581
Chris Lattnerbd31aa32009-01-05 00:07:25 +00001582 case tok::coloncolon: // ::foo::bar
John McCall1f476a12010-02-26 08:45:28 +00001583 // C++ scope specifier. Annotate and loop, or bail out on error.
1584 if (TryAnnotateCXXScopeToken(true)) {
1585 if (!DS.hasTypeSpecifier())
1586 DS.SetTypeSpecError();
1587 goto DoneWithDeclSpec;
1588 }
John McCall8bc2a702010-03-01 18:20:46 +00001589 if (Tok.is(tok::coloncolon)) // ::new or ::delete
1590 goto DoneWithDeclSpec;
John McCall1f476a12010-02-26 08:45:28 +00001591 continue;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001592
1593 case tok::annot_cxxscope: {
1594 if (DS.hasTypeSpecifier())
1595 goto DoneWithDeclSpec;
1596
John McCall9dab4e62009-12-12 11:40:51 +00001597 CXXScopeSpec SS;
Douglas Gregor869ad452011-02-24 17:54:50 +00001598 Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(),
1599 Tok.getAnnotationRange(),
1600 SS);
John McCall9dab4e62009-12-12 11:40:51 +00001601
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001602 // We are looking for a qualified typename.
Douglas Gregor167fa622009-03-25 15:40:00 +00001603 Token Next = NextToken();
Mike Stump11289f42009-09-09 15:08:12 +00001604 if (Next.is(tok::annot_template_id) &&
Douglas Gregor167fa622009-03-25 15:40:00 +00001605 static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue())
Douglas Gregorb67535d2009-03-31 00:43:58 +00001606 ->Kind == TNK_Type_template) {
Douglas Gregor167fa622009-03-25 15:40:00 +00001607 // We have a qualified template-id, e.g., N::A<int>
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001608
1609 // C++ [class.qual]p2:
1610 // In a lookup in which the constructor is an acceptable lookup
1611 // result and the nested-name-specifier nominates a class C:
1612 //
1613 // - if the name specified after the
1614 // nested-name-specifier, when looked up in C, is the
1615 // injected-class-name of C (Clause 9), or
1616 //
1617 // - if the name specified after the nested-name-specifier
1618 // is the same as the identifier or the
1619 // simple-template-id's template-name in the last
1620 // component of the nested-name-specifier,
1621 //
1622 // the name is instead considered to name the constructor of
1623 // class C.
1624 //
1625 // Thus, if the template-name is actually the constructor
1626 // name, then the code is ill-formed; this interpretation is
1627 // reinforced by the NAD status of core issue 635.
Argyrios Kyrtzidisc0c5dd22011-06-22 06:09:49 +00001628 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Next);
John McCall84821e72010-04-13 06:39:49 +00001629 if ((DSContext == DSC_top_level ||
1630 (DSContext == DSC_class && DS.isFriendSpecified())) &&
1631 TemplateId->Name &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00001632 Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) {
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001633 if (isConstructorDeclarator()) {
1634 // The user meant this to be an out-of-line constructor
1635 // definition, but template arguments are not allowed
1636 // there. Just allow this as a constructor; we'll
1637 // complain about it later.
1638 goto DoneWithDeclSpec;
1639 }
1640
1641 // The user meant this to name a type, but it actually names
1642 // a constructor with some extraneous template
1643 // arguments. Complain, then parse it as a type as the user
1644 // intended.
1645 Diag(TemplateId->TemplateNameLoc,
1646 diag::err_out_of_line_template_id_names_constructor)
1647 << TemplateId->Name;
1648 }
1649
John McCall9dab4e62009-12-12 11:40:51 +00001650 DS.getTypeSpecScope() = SS;
1651 ConsumeToken(); // The C++ scope.
Mike Stump11289f42009-09-09 15:08:12 +00001652 assert(Tok.is(tok::annot_template_id) &&
Douglas Gregor167fa622009-03-25 15:40:00 +00001653 "ParseOptionalCXXScopeSpecifier not working");
Douglas Gregore7c20652011-03-02 00:47:37 +00001654 AnnotateTemplateIdTokenAsType();
Douglas Gregor167fa622009-03-25 15:40:00 +00001655 continue;
1656 }
1657
Douglas Gregorc5790df2009-09-28 07:26:33 +00001658 if (Next.is(tok::annot_typename)) {
John McCall9dab4e62009-12-12 11:40:51 +00001659 DS.getTypeSpecScope() = SS;
1660 ConsumeToken(); // The C++ scope.
John McCallba7bf592010-08-24 05:47:05 +00001661 if (Tok.getAnnotationValue()) {
1662 ParsedType T = getTypeAnnotation(Tok);
Nico Weber77430342010-11-22 10:30:56 +00001663 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename,
1664 Tok.getAnnotationEndLoc(),
John McCallba7bf592010-08-24 05:47:05 +00001665 PrevSpec, DiagID, T);
1666 }
Douglas Gregorc5790df2009-09-28 07:26:33 +00001667 else
1668 DS.SetTypeSpecError();
1669 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1670 ConsumeToken(); // The typename
1671 }
1672
Douglas Gregor167fa622009-03-25 15:40:00 +00001673 if (Next.isNot(tok::identifier))
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001674 goto DoneWithDeclSpec;
1675
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001676 // If we're in a context where the identifier could be a class name,
1677 // check whether this is a constructor declaration.
John McCall84821e72010-04-13 06:39:49 +00001678 if ((DSContext == DSC_top_level ||
1679 (DSContext == DSC_class && DS.isFriendSpecified())) &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00001680 Actions.isCurrentClassName(*Next.getIdentifierInfo(), getCurScope(),
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001681 &SS)) {
1682 if (isConstructorDeclarator())
1683 goto DoneWithDeclSpec;
1684
1685 // As noted in C++ [class.qual]p2 (cited above), when the name
1686 // of the class is qualified in a context where it could name
1687 // a constructor, its a constructor name. However, we've
1688 // looked at the declarator, and the user probably meant this
1689 // to be a type. Complain that it isn't supposed to be treated
1690 // as a type, then proceed to parse it as a type.
1691 Diag(Next.getLocation(), diag::err_out_of_line_type_names_constructor)
1692 << Next.getIdentifierInfo();
1693 }
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001694
John McCallba7bf592010-08-24 05:47:05 +00001695 ParsedType TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(),
1696 Next.getLocation(),
Douglas Gregor844cb502011-03-01 18:12:44 +00001697 getCurScope(), &SS,
1698 false, false, ParsedType(),
1699 /*NonTrivialSourceInfo=*/true);
Douglas Gregor8bf42052009-02-09 18:46:07 +00001700
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001701 // If the referenced identifier is not a type, then this declspec is
1702 // erroneous: We already checked about that it has no type specifier, and
1703 // C++ doesn't have implicit int. Diagnose it as a typo w.r.t. to the
Mike Stump11289f42009-09-09 15:08:12 +00001704 // typename.
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001705 if (TypeRep == 0) {
1706 ConsumeToken(); // Eat the scope spec so the identifier is current.
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001707 if (ParseImplicitInt(DS, &SS, TemplateInfo, AS)) continue;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001708 goto DoneWithDeclSpec;
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001709 }
Mike Stump11289f42009-09-09 15:08:12 +00001710
John McCall9dab4e62009-12-12 11:40:51 +00001711 DS.getTypeSpecScope() = SS;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001712 ConsumeToken(); // The C++ scope.
1713
Douglas Gregor9817f4a2009-02-09 15:09:02 +00001714 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00001715 DiagID, TypeRep);
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001716 if (isInvalid)
1717 break;
Mike Stump11289f42009-09-09 15:08:12 +00001718
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001719 DS.SetRangeEnd(Tok.getLocation());
1720 ConsumeToken(); // The typename.
1721
1722 continue;
1723 }
Mike Stump11289f42009-09-09 15:08:12 +00001724
Chris Lattnere387d9e2009-01-21 19:48:37 +00001725 case tok::annot_typename: {
John McCallba7bf592010-08-24 05:47:05 +00001726 if (Tok.getAnnotationValue()) {
1727 ParsedType T = getTypeAnnotation(Tok);
Nico Weber7f8bb362010-11-22 12:50:03 +00001728 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallba7bf592010-08-24 05:47:05 +00001729 DiagID, T);
1730 } else
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00001731 DS.SetTypeSpecError();
Chris Lattner005fc1b2010-04-05 18:18:31 +00001732
1733 if (isInvalid)
1734 break;
1735
Chris Lattnere387d9e2009-01-21 19:48:37 +00001736 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1737 ConsumeToken(); // The typename
Mike Stump11289f42009-09-09 15:08:12 +00001738
Chris Lattnere387d9e2009-01-21 19:48:37 +00001739 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1740 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001741 // Objective-C interface.
1742 if (Tok.is(tok::less) && getLang().ObjC1)
1743 ParseObjCProtocolQualifiers(DS);
1744
Chris Lattnere387d9e2009-01-21 19:48:37 +00001745 continue;
1746 }
Mike Stump11289f42009-09-09 15:08:12 +00001747
Douglas Gregor06873092011-04-28 15:48:45 +00001748 case tok::kw___is_signed:
1749 // GNU libstdc++ 4.4 uses __is_signed as an identifier, but Clang
1750 // typically treats it as a trait. If we see __is_signed as it appears
1751 // in libstdc++, e.g.,
1752 //
1753 // static const bool __is_signed;
1754 //
1755 // then treat __is_signed as an identifier rather than as a keyword.
1756 if (DS.getTypeSpecType() == TST_bool &&
1757 DS.getTypeQualifiers() == DeclSpec::TQ_const &&
1758 DS.getStorageClassSpec() == DeclSpec::SCS_static) {
1759 Tok.getIdentifierInfo()->RevertTokenIDToIdentifier();
1760 Tok.setKind(tok::identifier);
1761 }
1762
1763 // We're done with the declaration-specifiers.
1764 goto DoneWithDeclSpec;
1765
Chris Lattner16fac4f2008-07-26 01:18:38 +00001766 // typedef-name
1767 case tok::identifier: {
Chris Lattnerbd31aa32009-01-05 00:07:25 +00001768 // In C++, check to see if this is a scope specifier like foo::bar::, if
1769 // so handle it as such. This is important for ctor parsing.
John McCall1f476a12010-02-26 08:45:28 +00001770 if (getLang().CPlusPlus) {
1771 if (TryAnnotateCXXScopeToken(true)) {
1772 if (!DS.hasTypeSpecifier())
1773 DS.SetTypeSpecError();
1774 goto DoneWithDeclSpec;
1775 }
1776 if (!Tok.is(tok::identifier))
1777 continue;
1778 }
Mike Stump11289f42009-09-09 15:08:12 +00001779
Chris Lattner16fac4f2008-07-26 01:18:38 +00001780 // This identifier can only be a typedef name if we haven't already seen
1781 // a type-specifier. Without this check we misparse:
1782 // typedef int X; struct Y { short X; }; as 'short int'.
1783 if (DS.hasTypeSpecifier())
1784 goto DoneWithDeclSpec;
Mike Stump11289f42009-09-09 15:08:12 +00001785
John Thompson22334602010-02-05 00:12:22 +00001786 // Check for need to substitute AltiVec keyword tokens.
1787 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
1788 break;
1789
Chris Lattner16fac4f2008-07-26 01:18:38 +00001790 // It has to be available as a typedef too!
John McCallba7bf592010-08-24 05:47:05 +00001791 ParsedType TypeRep =
1792 Actions.getTypeName(*Tok.getIdentifierInfo(),
1793 Tok.getLocation(), getCurScope());
Douglas Gregor8bf42052009-02-09 18:46:07 +00001794
Chris Lattner6cc055a2009-04-12 20:42:31 +00001795 // If this is not a typedef name, don't parse it as part of the declspec,
1796 // it must be an implicit int or an error.
John McCallba7bf592010-08-24 05:47:05 +00001797 if (!TypeRep) {
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001798 if (ParseImplicitInt(DS, 0, TemplateInfo, AS)) continue;
Chris Lattner16fac4f2008-07-26 01:18:38 +00001799 goto DoneWithDeclSpec;
Chris Lattner6cc055a2009-04-12 20:42:31 +00001800 }
Douglas Gregor8bf42052009-02-09 18:46:07 +00001801
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001802 // If we're in a context where the identifier could be a class name,
1803 // check whether this is a constructor declaration.
1804 if (getLang().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00001805 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), getCurScope()) &&
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001806 isConstructorDeclarator())
Douglas Gregor61956c42008-10-31 09:07:45 +00001807 goto DoneWithDeclSpec;
1808
Douglas Gregor9817f4a2009-02-09 15:09:02 +00001809 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00001810 DiagID, TypeRep);
Chris Lattner16fac4f2008-07-26 01:18:38 +00001811 if (isInvalid)
1812 break;
Mike Stump11289f42009-09-09 15:08:12 +00001813
Chris Lattner16fac4f2008-07-26 01:18:38 +00001814 DS.SetRangeEnd(Tok.getLocation());
1815 ConsumeToken(); // The identifier
1816
1817 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1818 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001819 // Objective-C interface.
1820 if (Tok.is(tok::less) && getLang().ObjC1)
1821 ParseObjCProtocolQualifiers(DS);
1822
Steve Naroffcd5e7822008-09-22 10:28:57 +00001823 // Need to support trailing type qualifiers (e.g. "id<p> const").
1824 // If a type specifier follows, it will be diagnosed elsewhere.
1825 continue;
Chris Lattner16fac4f2008-07-26 01:18:38 +00001826 }
Douglas Gregor7f741122009-02-25 19:37:18 +00001827
1828 // type-name
1829 case tok::annot_template_id: {
Argyrios Kyrtzidisc0c5dd22011-06-22 06:09:49 +00001830 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregorb67535d2009-03-31 00:43:58 +00001831 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor7f741122009-02-25 19:37:18 +00001832 // This template-id does not refer to a type name, so we're
1833 // done with the type-specifiers.
1834 goto DoneWithDeclSpec;
1835 }
1836
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001837 // If we're in a context where the template-id could be a
1838 // constructor name or specialization, check whether this is a
1839 // constructor declaration.
1840 if (getLang().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00001841 Actions.isCurrentClassName(*TemplateId->Name, getCurScope()) &&
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001842 isConstructorDeclarator())
1843 goto DoneWithDeclSpec;
1844
Douglas Gregor7f741122009-02-25 19:37:18 +00001845 // Turn the template-id annotation token into a type annotation
1846 // token, then try again to parse it as a type-specifier.
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00001847 AnnotateTemplateIdTokenAsType();
Douglas Gregor7f741122009-02-25 19:37:18 +00001848 continue;
1849 }
1850
Chris Lattnere37e2332006-08-15 04:50:22 +00001851 // GNU attributes support.
1852 case tok::kw___attribute:
John McCall53fa7142010-12-24 02:08:15 +00001853 ParseGNUAttributes(DS.getAttributes());
Chris Lattnerb95cca02006-10-17 03:01:08 +00001854 continue;
Steve Naroff3a9b7e02008-12-24 20:59:21 +00001855
1856 // Microsoft declspec support.
1857 case tok::kw___declspec:
John McCall53fa7142010-12-24 02:08:15 +00001858 ParseMicrosoftDeclSpec(DS.getAttributes());
Steve Naroff3a9b7e02008-12-24 20:59:21 +00001859 continue;
Mike Stump11289f42009-09-09 15:08:12 +00001860
Steve Naroff44ac7772008-12-25 14:16:32 +00001861 // Microsoft single token adornments.
Steve Narofff9c29d42008-12-25 14:41:26 +00001862 case tok::kw___forceinline:
Eli Friedman53339e02009-06-08 23:27:34 +00001863 // FIXME: Add handling here!
1864 break;
1865
1866 case tok::kw___ptr64:
Francois Pichetf2fb4112011-08-25 00:36:46 +00001867 case tok::kw___ptr32:
Steve Narofff9c29d42008-12-25 14:41:26 +00001868 case tok::kw___w64:
Steve Naroff44ac7772008-12-25 14:16:32 +00001869 case tok::kw___cdecl:
1870 case tok::kw___stdcall:
1871 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00001872 case tok::kw___thiscall:
Francois Pichet17ed0202011-08-18 09:59:55 +00001873 case tok::kw___unaligned:
John McCall53fa7142010-12-24 02:08:15 +00001874 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman53339e02009-06-08 23:27:34 +00001875 continue;
1876
Dawn Perchik335e16b2010-09-03 01:29:35 +00001877 // Borland single token adornments.
1878 case tok::kw___pascal:
John McCall53fa7142010-12-24 02:08:15 +00001879 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik335e16b2010-09-03 01:29:35 +00001880 continue;
1881
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00001882 // OpenCL single token adornments.
1883 case tok::kw___kernel:
1884 ParseOpenCLAttributes(DS.getAttributes());
1885 continue;
1886
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001887 // storage-class-specifier
1888 case tok::kw_typedef:
John McCall49bfce42009-08-03 20:12:06 +00001889 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_typedef, Loc, PrevSpec,
Peter Collingbournede32b202011-02-11 19:59:54 +00001890 DiagID, getLang());
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001891 break;
1892 case tok::kw_extern:
Chris Lattner353f5742006-11-28 04:50:12 +00001893 if (DS.isThreadSpecified())
Chris Lattner6d29c102008-11-18 07:48:38 +00001894 Diag(Tok, diag::ext_thread_before) << "extern";
John McCall49bfce42009-08-03 20:12:06 +00001895 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_extern, Loc, PrevSpec,
Peter Collingbournede32b202011-02-11 19:59:54 +00001896 DiagID, getLang());
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001897 break;
Steve Naroff2050b0d2007-12-18 00:16:02 +00001898 case tok::kw___private_extern__:
Chris Lattner371ed4e2008-04-06 06:57:35 +00001899 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_private_extern, Loc,
Peter Collingbournede32b202011-02-11 19:59:54 +00001900 PrevSpec, DiagID, getLang());
Steve Naroff2050b0d2007-12-18 00:16:02 +00001901 break;
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001902 case tok::kw_static:
Chris Lattner353f5742006-11-28 04:50:12 +00001903 if (DS.isThreadSpecified())
Chris Lattner6d29c102008-11-18 07:48:38 +00001904 Diag(Tok, diag::ext_thread_before) << "static";
John McCall49bfce42009-08-03 20:12:06 +00001905 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_static, Loc, PrevSpec,
Peter Collingbournede32b202011-02-11 19:59:54 +00001906 DiagID, getLang());
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001907 break;
1908 case tok::kw_auto:
Douglas Gregor1e989862011-03-14 21:43:30 +00001909 if (getLang().CPlusPlus0x) {
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00001910 if (isKnownToBeTypeSpecifier(GetLookAheadToken(1))) {
1911 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, Loc, PrevSpec,
Richard Smith58c74332011-09-04 19:54:14 +00001912 DiagID, getLang());
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00001913 if (!isInvalid)
Richard Smith58c74332011-09-04 19:54:14 +00001914 Diag(Tok, diag::ext_auto_storage_class)
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00001915 << FixItHint::CreateRemoval(DS.getStorageClassSpecLoc());
Richard Smith58c74332011-09-04 19:54:14 +00001916 } else
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00001917 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec,
1918 DiagID);
Richard Smith58c74332011-09-04 19:54:14 +00001919 } else
John McCall49bfce42009-08-03 20:12:06 +00001920 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, Loc, PrevSpec,
Peter Collingbournede32b202011-02-11 19:59:54 +00001921 DiagID, getLang());
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001922 break;
1923 case tok::kw_register:
John McCall49bfce42009-08-03 20:12:06 +00001924 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_register, Loc, PrevSpec,
Peter Collingbournede32b202011-02-11 19:59:54 +00001925 DiagID, getLang());
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001926 break;
Sebastian Redlccdfaba2008-11-14 23:42:31 +00001927 case tok::kw_mutable:
John McCall49bfce42009-08-03 20:12:06 +00001928 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_mutable, Loc, PrevSpec,
Peter Collingbournede32b202011-02-11 19:59:54 +00001929 DiagID, getLang());
Sebastian Redlccdfaba2008-11-14 23:42:31 +00001930 break;
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001931 case tok::kw___thread:
John McCall49bfce42009-08-03 20:12:06 +00001932 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001933 break;
Mike Stump11289f42009-09-09 15:08:12 +00001934
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001935 // function-specifier
1936 case tok::kw_inline:
John McCall49bfce42009-08-03 20:12:06 +00001937 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec, DiagID);
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001938 break;
Douglas Gregor61956c42008-10-31 09:07:45 +00001939 case tok::kw_virtual:
John McCall49bfce42009-08-03 20:12:06 +00001940 isInvalid = DS.SetFunctionSpecVirtual(Loc, PrevSpec, DiagID);
Douglas Gregor61956c42008-10-31 09:07:45 +00001941 break;
Douglas Gregor61956c42008-10-31 09:07:45 +00001942 case tok::kw_explicit:
John McCall49bfce42009-08-03 20:12:06 +00001943 isInvalid = DS.SetFunctionSpecExplicit(Loc, PrevSpec, DiagID);
Douglas Gregor61956c42008-10-31 09:07:45 +00001944 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00001945
Anders Carlssoncd8db412009-05-06 04:46:28 +00001946 // friend
1947 case tok::kw_friend:
John McCall07e91c02009-08-06 02:15:43 +00001948 if (DSContext == DSC_class)
1949 isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID);
1950 else {
1951 PrevSpec = ""; // not actually used by the diagnostic
1952 DiagID = diag::err_friend_invalid_in_context;
1953 isInvalid = true;
1954 }
Anders Carlssoncd8db412009-05-06 04:46:28 +00001955 break;
Mike Stump11289f42009-09-09 15:08:12 +00001956
Douglas Gregor26701a42011-09-09 02:06:17 +00001957 // Modules
1958 case tok::kw___module_private__:
1959 isInvalid = DS.setModulePrivateSpec(Loc, PrevSpec, DiagID);
1960 break;
1961
Sebastian Redl39c2a8b2009-11-05 15:47:02 +00001962 // constexpr
1963 case tok::kw_constexpr:
1964 isInvalid = DS.SetConstexprSpec(Loc, PrevSpec, DiagID);
1965 break;
1966
Chris Lattnere387d9e2009-01-21 19:48:37 +00001967 // type-specifier
1968 case tok::kw_short:
John McCall49bfce42009-08-03 20:12:06 +00001969 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec,
1970 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001971 break;
1972 case tok::kw_long:
1973 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCall49bfce42009-08-03 20:12:06 +00001974 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
1975 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001976 else
John McCall49bfce42009-08-03 20:12:06 +00001977 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
1978 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001979 break;
Francois Pichet84133e42011-04-28 01:59:37 +00001980 case tok::kw___int64:
1981 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
1982 DiagID);
1983 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00001984 case tok::kw_signed:
John McCall49bfce42009-08-03 20:12:06 +00001985 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec,
1986 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001987 break;
1988 case tok::kw_unsigned:
John McCall49bfce42009-08-03 20:12:06 +00001989 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
1990 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001991 break;
1992 case tok::kw__Complex:
John McCall49bfce42009-08-03 20:12:06 +00001993 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
1994 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001995 break;
1996 case tok::kw__Imaginary:
John McCall49bfce42009-08-03 20:12:06 +00001997 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
1998 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001999 break;
2000 case tok::kw_void:
John McCall49bfce42009-08-03 20:12:06 +00002001 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec,
2002 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002003 break;
2004 case tok::kw_char:
John McCall49bfce42009-08-03 20:12:06 +00002005 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec,
2006 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002007 break;
2008 case tok::kw_int:
John McCall49bfce42009-08-03 20:12:06 +00002009 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec,
2010 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002011 break;
2012 case tok::kw_float:
John McCall49bfce42009-08-03 20:12:06 +00002013 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec,
2014 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002015 break;
2016 case tok::kw_double:
John McCall49bfce42009-08-03 20:12:06 +00002017 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec,
2018 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002019 break;
2020 case tok::kw_wchar_t:
John McCall49bfce42009-08-03 20:12:06 +00002021 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec,
2022 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002023 break;
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00002024 case tok::kw_char16_t:
John McCall49bfce42009-08-03 20:12:06 +00002025 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec,
2026 DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00002027 break;
2028 case tok::kw_char32_t:
John McCall49bfce42009-08-03 20:12:06 +00002029 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec,
2030 DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00002031 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00002032 case tok::kw_bool:
2033 case tok::kw__Bool:
Argyrios Kyrtzidis20ee5ae2010-11-16 18:18:13 +00002034 if (Tok.is(tok::kw_bool) &&
2035 DS.getTypeSpecType() != DeclSpec::TST_unspecified &&
2036 DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
2037 PrevSpec = ""; // Not used by the diagnostic.
2038 DiagID = diag::err_bool_redeclaration;
Fariborz Jahanian2b059992011-04-19 21:42:37 +00002039 // For better error recovery.
2040 Tok.setKind(tok::identifier);
Argyrios Kyrtzidis20ee5ae2010-11-16 18:18:13 +00002041 isInvalid = true;
2042 } else {
2043 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec,
2044 DiagID);
2045 }
Chris Lattnere387d9e2009-01-21 19:48:37 +00002046 break;
2047 case tok::kw__Decimal32:
John McCall49bfce42009-08-03 20:12:06 +00002048 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
2049 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002050 break;
2051 case tok::kw__Decimal64:
John McCall49bfce42009-08-03 20:12:06 +00002052 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
2053 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002054 break;
2055 case tok::kw__Decimal128:
John McCall49bfce42009-08-03 20:12:06 +00002056 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
2057 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002058 break;
John Thompson22334602010-02-05 00:12:22 +00002059 case tok::kw___vector:
2060 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
2061 break;
2062 case tok::kw___pixel:
2063 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
2064 break;
John McCall39439732011-04-09 22:50:59 +00002065 case tok::kw___unknown_anytype:
2066 isInvalid = DS.SetTypeSpecType(TST_unknown_anytype, Loc,
2067 PrevSpec, DiagID);
2068 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00002069
2070 // class-specifier:
2071 case tok::kw_class:
2072 case tok::kw_struct:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00002073 case tok::kw_union: {
2074 tok::TokenKind Kind = Tok.getKind();
2075 ConsumeToken();
Douglas Gregor1b57ff32009-05-12 23:25:50 +00002076 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002077 continue;
Chris Lattnerffaa0e62009-04-12 21:49:30 +00002078 }
Chris Lattnere387d9e2009-01-21 19:48:37 +00002079
2080 // enum-specifier:
2081 case tok::kw_enum:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00002082 ConsumeToken();
Douglas Gregordc70c3a2010-03-02 17:53:14 +00002083 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002084 continue;
2085
2086 // cv-qualifier:
2087 case tok::kw_const:
John McCall49bfce42009-08-03 20:12:06 +00002088 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec, DiagID,
2089 getLang());
Chris Lattnere387d9e2009-01-21 19:48:37 +00002090 break;
2091 case tok::kw_volatile:
John McCall49bfce42009-08-03 20:12:06 +00002092 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
2093 getLang());
Chris Lattnere387d9e2009-01-21 19:48:37 +00002094 break;
2095 case tok::kw_restrict:
John McCall49bfce42009-08-03 20:12:06 +00002096 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
2097 getLang());
Chris Lattnere387d9e2009-01-21 19:48:37 +00002098 break;
2099
Douglas Gregor333489b2009-03-27 23:10:48 +00002100 // C++ typename-specifier:
2101 case tok::kw_typename:
John McCall1f476a12010-02-26 08:45:28 +00002102 if (TryAnnotateTypeOrScopeToken()) {
2103 DS.SetTypeSpecError();
2104 goto DoneWithDeclSpec;
2105 }
2106 if (!Tok.is(tok::kw_typename))
Douglas Gregor333489b2009-03-27 23:10:48 +00002107 continue;
2108 break;
2109
Chris Lattnere387d9e2009-01-21 19:48:37 +00002110 // GNU typeof support.
2111 case tok::kw_typeof:
2112 ParseTypeofSpecifier(DS);
2113 continue;
2114
Anders Carlsson74948d02009-06-24 17:47:40 +00002115 case tok::kw_decltype:
2116 ParseDecltypeSpecifier(DS);
2117 continue;
2118
Alexis Hunt4a257072011-05-19 05:37:45 +00002119 case tok::kw___underlying_type:
2120 ParseUnderlyingTypeSpecifier(DS);
2121
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00002122 // OpenCL qualifiers:
2123 case tok::kw_private:
2124 if (!getLang().OpenCL)
2125 goto DoneWithDeclSpec;
2126 case tok::kw___private:
2127 case tok::kw___global:
2128 case tok::kw___local:
2129 case tok::kw___constant:
2130 case tok::kw___read_only:
2131 case tok::kw___write_only:
2132 case tok::kw___read_write:
2133 ParseOpenCLQualifiers(DS);
2134 break;
2135
Steve Naroffcfdf6162008-06-05 00:02:44 +00002136 case tok::less:
Chris Lattner16fac4f2008-07-26 01:18:38 +00002137 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattner0974b232008-07-26 00:20:22 +00002138 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
2139 // but we support it.
Chris Lattner16fac4f2008-07-26 01:18:38 +00002140 if (DS.hasTypeSpecifier() || !getLang().ObjC1)
Chris Lattner0974b232008-07-26 00:20:22 +00002141 goto DoneWithDeclSpec;
Mike Stump11289f42009-09-09 15:08:12 +00002142
Douglas Gregor3a001f42010-11-19 17:10:50 +00002143 if (!ParseObjCProtocolQualifiers(DS))
2144 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
2145 << FixItHint::CreateInsertion(Loc, "id")
2146 << SourceRange(Loc, DS.getSourceRange().getEnd());
Douglas Gregor06e41ae2010-10-21 23:17:00 +00002147
2148 // Need to support trailing type qualifiers (e.g. "id<p> const").
2149 // If a type specifier follows, it will be diagnosed elsewhere.
2150 continue;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002151 }
John McCall49bfce42009-08-03 20:12:06 +00002152 // If the specifier wasn't legal, issue a diagnostic.
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002153 if (isInvalid) {
2154 assert(PrevSpec && "Method did not return previous specifier!");
John McCall49bfce42009-08-03 20:12:06 +00002155 assert(DiagID);
Douglas Gregora05f5ab2010-08-23 14:34:43 +00002156
2157 if (DiagID == diag::ext_duplicate_declspec)
2158 Diag(Tok, DiagID)
2159 << PrevSpec << FixItHint::CreateRemoval(Tok.getLocation());
2160 else
2161 Diag(Tok, DiagID) << PrevSpec;
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002162 }
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00002163
Chris Lattner2e232092008-03-13 06:29:04 +00002164 DS.SetRangeEnd(Tok.getLocation());
Fariborz Jahanian2b059992011-04-19 21:42:37 +00002165 if (DiagID != diag::err_bool_redeclaration)
2166 ConsumeToken();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002167 }
2168}
Douglas Gregoreb31f392008-12-01 23:54:00 +00002169
Chris Lattnera448d752009-01-06 06:59:53 +00002170/// ParseOptionalTypeSpecifier - Try to parse a single type-specifier. We
Douglas Gregor450c75a2008-11-07 15:42:26 +00002171/// primarily follow the C++ grammar with additions for C99 and GNU,
2172/// which together subsume the C grammar. Note that the C++
2173/// type-specifier also includes the C type-qualifier (for const,
2174/// volatile, and C99 restrict). Returns true if a type-specifier was
2175/// found (and parsed), false otherwise.
2176///
2177/// type-specifier: [C++ 7.1.5]
2178/// simple-type-specifier
2179/// class-specifier
2180/// enum-specifier
2181/// elaborated-type-specifier [TODO]
2182/// cv-qualifier
2183///
2184/// cv-qualifier: [C++ 7.1.5.1]
2185/// 'const'
2186/// 'volatile'
2187/// [C99] 'restrict'
2188///
2189/// simple-type-specifier: [ C++ 7.1.5.2]
2190/// '::'[opt] nested-name-specifier[opt] type-name [TODO]
2191/// '::'[opt] nested-name-specifier 'template' template-id [TODO]
2192/// 'char'
2193/// 'wchar_t'
2194/// 'bool'
2195/// 'short'
2196/// 'int'
2197/// 'long'
2198/// 'signed'
2199/// 'unsigned'
2200/// 'float'
2201/// 'double'
2202/// 'void'
2203/// [C99] '_Bool'
2204/// [C99] '_Complex'
2205/// [C99] '_Imaginary' // Removed in TC2?
2206/// [GNU] '_Decimal32'
2207/// [GNU] '_Decimal64'
2208/// [GNU] '_Decimal128'
2209/// [GNU] typeof-specifier
2210/// [OBJC] class-name objc-protocol-refs[opt] [TODO]
2211/// [OBJC] typedef-name objc-protocol-refs[opt] [TODO]
Anders Carlsson74948d02009-06-24 17:47:40 +00002212/// [C++0x] 'decltype' ( expression )
John Thompson22334602010-02-05 00:12:22 +00002213/// [AltiVec] '__vector'
John McCall49bfce42009-08-03 20:12:06 +00002214bool Parser::ParseOptionalTypeSpecifier(DeclSpec &DS, bool& isInvalid,
Chris Lattnera448d752009-01-06 06:59:53 +00002215 const char *&PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00002216 unsigned &DiagID,
Sebastian Redl2b372722010-02-03 21:21:43 +00002217 const ParsedTemplateInfo &TemplateInfo,
2218 bool SuppressDeclarations) {
Douglas Gregor450c75a2008-11-07 15:42:26 +00002219 SourceLocation Loc = Tok.getLocation();
2220
2221 switch (Tok.getKind()) {
Chris Lattner020bab92009-01-04 23:41:41 +00002222 case tok::identifier: // foo::bar
Douglas Gregorb8eaf292010-04-15 23:40:53 +00002223 // If we already have a type specifier, this identifier is not a type.
2224 if (DS.getTypeSpecType() != DeclSpec::TST_unspecified ||
2225 DS.getTypeSpecWidth() != DeclSpec::TSW_unspecified ||
2226 DS.getTypeSpecSign() != DeclSpec::TSS_unspecified)
2227 return false;
John Thompson22334602010-02-05 00:12:22 +00002228 // Check for need to substitute AltiVec keyword tokens.
2229 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
2230 break;
2231 // Fall through.
Douglas Gregor333489b2009-03-27 23:10:48 +00002232 case tok::kw_typename: // typename foo::bar
Chris Lattner020bab92009-01-04 23:41:41 +00002233 // Annotate typenames and C++ scope specifiers. If we get one, just
2234 // recurse to handle whatever we get.
2235 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00002236 return true;
2237 if (Tok.is(tok::identifier))
2238 return false;
2239 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
2240 TemplateInfo, SuppressDeclarations);
Chris Lattner020bab92009-01-04 23:41:41 +00002241 case tok::coloncolon: // ::foo::bar
2242 if (NextToken().is(tok::kw_new) || // ::new
2243 NextToken().is(tok::kw_delete)) // ::delete
2244 return false;
Mike Stump11289f42009-09-09 15:08:12 +00002245
Chris Lattner020bab92009-01-04 23:41:41 +00002246 // Annotate typenames and C++ scope specifiers. If we get one, just
2247 // recurse to handle whatever we get.
2248 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00002249 return true;
2250 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
2251 TemplateInfo, SuppressDeclarations);
Mike Stump11289f42009-09-09 15:08:12 +00002252
Douglas Gregor450c75a2008-11-07 15:42:26 +00002253 // simple-type-specifier:
Chris Lattnera8a3f732009-01-06 05:06:21 +00002254 case tok::annot_typename: {
John McCallba7bf592010-08-24 05:47:05 +00002255 if (ParsedType T = getTypeAnnotation(Tok)) {
Nico Weber77430342010-11-22 10:30:56 +00002256 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename,
2257 Tok.getAnnotationEndLoc(), PrevSpec,
John McCallba7bf592010-08-24 05:47:05 +00002258 DiagID, T);
2259 } else
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00002260 DS.SetTypeSpecError();
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002261 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
2262 ConsumeToken(); // The typename
Mike Stump11289f42009-09-09 15:08:12 +00002263
Douglas Gregor450c75a2008-11-07 15:42:26 +00002264 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
2265 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
2266 // Objective-C interface. If we don't have Objective-C or a '<', this is
2267 // just a normal reference to a typedef name.
Douglas Gregor06e41ae2010-10-21 23:17:00 +00002268 if (Tok.is(tok::less) && getLang().ObjC1)
2269 ParseObjCProtocolQualifiers(DS);
2270
Douglas Gregor450c75a2008-11-07 15:42:26 +00002271 return true;
2272 }
2273
2274 case tok::kw_short:
John McCall49bfce42009-08-03 20:12:06 +00002275 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00002276 break;
2277 case tok::kw_long:
2278 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCall49bfce42009-08-03 20:12:06 +00002279 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
2280 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00002281 else
John McCall49bfce42009-08-03 20:12:06 +00002282 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2283 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00002284 break;
Francois Pichet84133e42011-04-28 01:59:37 +00002285 case tok::kw___int64:
2286 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2287 DiagID);
2288 break;
Douglas Gregor450c75a2008-11-07 15:42:26 +00002289 case tok::kw_signed:
John McCall49bfce42009-08-03 20:12:06 +00002290 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00002291 break;
2292 case tok::kw_unsigned:
John McCall49bfce42009-08-03 20:12:06 +00002293 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
2294 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00002295 break;
2296 case tok::kw__Complex:
John McCall49bfce42009-08-03 20:12:06 +00002297 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
2298 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00002299 break;
2300 case tok::kw__Imaginary:
John McCall49bfce42009-08-03 20:12:06 +00002301 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
2302 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00002303 break;
2304 case tok::kw_void:
John McCall49bfce42009-08-03 20:12:06 +00002305 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00002306 break;
2307 case tok::kw_char:
John McCall49bfce42009-08-03 20:12:06 +00002308 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00002309 break;
2310 case tok::kw_int:
John McCall49bfce42009-08-03 20:12:06 +00002311 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00002312 break;
2313 case tok::kw_float:
John McCall49bfce42009-08-03 20:12:06 +00002314 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00002315 break;
2316 case tok::kw_double:
John McCall49bfce42009-08-03 20:12:06 +00002317 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00002318 break;
2319 case tok::kw_wchar_t:
John McCall49bfce42009-08-03 20:12:06 +00002320 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00002321 break;
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00002322 case tok::kw_char16_t:
John McCall49bfce42009-08-03 20:12:06 +00002323 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00002324 break;
2325 case tok::kw_char32_t:
John McCall49bfce42009-08-03 20:12:06 +00002326 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00002327 break;
Douglas Gregor450c75a2008-11-07 15:42:26 +00002328 case tok::kw_bool:
2329 case tok::kw__Bool:
John McCall49bfce42009-08-03 20:12:06 +00002330 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00002331 break;
2332 case tok::kw__Decimal32:
John McCall49bfce42009-08-03 20:12:06 +00002333 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
2334 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00002335 break;
2336 case tok::kw__Decimal64:
John McCall49bfce42009-08-03 20:12:06 +00002337 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
2338 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00002339 break;
2340 case tok::kw__Decimal128:
John McCall49bfce42009-08-03 20:12:06 +00002341 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
2342 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00002343 break;
John Thompson22334602010-02-05 00:12:22 +00002344 case tok::kw___vector:
2345 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
2346 break;
2347 case tok::kw___pixel:
2348 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
2349 break;
2350
Douglas Gregor450c75a2008-11-07 15:42:26 +00002351 // class-specifier:
2352 case tok::kw_class:
2353 case tok::kw_struct:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00002354 case tok::kw_union: {
2355 tok::TokenKind Kind = Tok.getKind();
2356 ConsumeToken();
Sebastian Redl2b372722010-02-03 21:21:43 +00002357 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS_none,
2358 SuppressDeclarations);
Douglas Gregor450c75a2008-11-07 15:42:26 +00002359 return true;
Chris Lattnerffaa0e62009-04-12 21:49:30 +00002360 }
Douglas Gregor450c75a2008-11-07 15:42:26 +00002361
2362 // enum-specifier:
2363 case tok::kw_enum:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00002364 ConsumeToken();
Douglas Gregordc70c3a2010-03-02 17:53:14 +00002365 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS_none);
Douglas Gregor450c75a2008-11-07 15:42:26 +00002366 return true;
2367
2368 // cv-qualifier:
2369 case tok::kw_const:
2370 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00002371 DiagID, getLang());
Douglas Gregor450c75a2008-11-07 15:42:26 +00002372 break;
2373 case tok::kw_volatile:
2374 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00002375 DiagID, getLang());
Douglas Gregor450c75a2008-11-07 15:42:26 +00002376 break;
2377 case tok::kw_restrict:
2378 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00002379 DiagID, getLang());
Douglas Gregor450c75a2008-11-07 15:42:26 +00002380 break;
2381
2382 // GNU typeof support.
2383 case tok::kw_typeof:
2384 ParseTypeofSpecifier(DS);
2385 return true;
2386
Anders Carlsson74948d02009-06-24 17:47:40 +00002387 // C++0x decltype support.
2388 case tok::kw_decltype:
2389 ParseDecltypeSpecifier(DS);
2390 return true;
Mike Stump11289f42009-09-09 15:08:12 +00002391
Alexis Hunt4a257072011-05-19 05:37:45 +00002392 // C++0x type traits support.
2393 case tok::kw___underlying_type:
2394 ParseUnderlyingTypeSpecifier(DS);
2395 return true;
2396
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00002397 // OpenCL qualifiers:
2398 case tok::kw_private:
2399 if (!getLang().OpenCL)
2400 return false;
2401 case tok::kw___private:
2402 case tok::kw___global:
2403 case tok::kw___local:
2404 case tok::kw___constant:
2405 case tok::kw___read_only:
2406 case tok::kw___write_only:
2407 case tok::kw___read_write:
2408 ParseOpenCLQualifiers(DS);
2409 break;
2410
Anders Carlssonbae27372009-06-26 23:44:14 +00002411 // C++0x auto support.
2412 case tok::kw_auto:
Richard Smith50658642011-09-04 20:24:20 +00002413 // This is only called in situations where a storage-class specifier is
2414 // illegal, so we can assume an auto type specifier was intended even in
2415 // C++98. In C++98 mode, DeclSpec::Finish will produce an appropriate
2416 // extension diagnostic.
2417 if (!getLang().CPlusPlus)
Anders Carlssonbae27372009-06-26 23:44:14 +00002418 return false;
2419
John McCall49bfce42009-08-03 20:12:06 +00002420 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec, DiagID);
Anders Carlssonbae27372009-06-26 23:44:14 +00002421 break;
Dawn Perchik335e16b2010-09-03 01:29:35 +00002422
Eli Friedman53339e02009-06-08 23:27:34 +00002423 case tok::kw___ptr64:
Francois Pichetf2fb4112011-08-25 00:36:46 +00002424 case tok::kw___ptr32:
Eli Friedman53339e02009-06-08 23:27:34 +00002425 case tok::kw___w64:
Steve Naroff44ac7772008-12-25 14:16:32 +00002426 case tok::kw___cdecl:
2427 case tok::kw___stdcall:
2428 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00002429 case tok::kw___thiscall:
Francois Pichet17ed0202011-08-18 09:59:55 +00002430 case tok::kw___unaligned:
John McCall53fa7142010-12-24 02:08:15 +00002431 ParseMicrosoftTypeAttributes(DS.getAttributes());
Chris Lattner78ecd4f2009-01-21 19:19:26 +00002432 return true;
Steve Naroff44ac7772008-12-25 14:16:32 +00002433
Dawn Perchik335e16b2010-09-03 01:29:35 +00002434 case tok::kw___pascal:
John McCall53fa7142010-12-24 02:08:15 +00002435 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik335e16b2010-09-03 01:29:35 +00002436 return true;
2437
Douglas Gregor450c75a2008-11-07 15:42:26 +00002438 default:
2439 // Not a type-specifier; do nothing.
2440 return false;
2441 }
2442
2443 // If the specifier combination wasn't legal, issue a diagnostic.
2444 if (isInvalid) {
2445 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner6d29c102008-11-18 07:48:38 +00002446 // Pick between error or extwarn.
Chris Lattner6d29c102008-11-18 07:48:38 +00002447 Diag(Tok, DiagID) << PrevSpec;
Douglas Gregor450c75a2008-11-07 15:42:26 +00002448 }
2449 DS.SetRangeEnd(Tok.getLocation());
2450 ConsumeToken(); // whatever we parsed above.
2451 return true;
2452}
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002453
Chris Lattner70ae4912007-10-29 04:42:53 +00002454/// ParseStructDeclaration - Parse a struct declaration without the terminating
2455/// semicolon.
2456///
Chris Lattner90a26b02007-01-23 04:38:16 +00002457/// struct-declaration:
Chris Lattner70ae4912007-10-29 04:42:53 +00002458/// specifier-qualifier-list struct-declarator-list
Chris Lattner736ed5d2007-06-09 05:59:07 +00002459/// [GNU] __extension__ struct-declaration
Chris Lattner70ae4912007-10-29 04:42:53 +00002460/// [GNU] specifier-qualifier-list
Chris Lattner90a26b02007-01-23 04:38:16 +00002461/// struct-declarator-list:
2462/// struct-declarator
2463/// struct-declarator-list ',' struct-declarator
2464/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
2465/// struct-declarator:
2466/// declarator
2467/// [GNU] declarator attributes[opt]
2468/// declarator[opt] ':' constant-expression
2469/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
2470///
Chris Lattnera12405b2008-04-10 06:46:29 +00002471void Parser::
John McCallcfefb6d2009-11-03 02:38:08 +00002472ParseStructDeclaration(DeclSpec &DS, FieldCallback &Fields) {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00002473
Chris Lattnerf02ef3e2008-10-20 06:45:43 +00002474 if (Tok.is(tok::kw___extension__)) {
2475 // __extension__ silences extension warnings in the subexpression.
2476 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroff97170802007-08-20 22:28:22 +00002477 ConsumeToken();
Chris Lattnerf02ef3e2008-10-20 06:45:43 +00002478 return ParseStructDeclaration(DS, Fields);
2479 }
Mike Stump11289f42009-09-09 15:08:12 +00002480
Steve Naroff97170802007-08-20 22:28:22 +00002481 // Parse the common specifier-qualifiers-list piece.
Steve Naroff97170802007-08-20 22:28:22 +00002482 ParseSpecifierQualifierList(DS);
Mike Stump11289f42009-09-09 15:08:12 +00002483
Douglas Gregorc6f58fe2009-01-12 22:49:06 +00002484 // If there are no declarators, this is a free-standing declaration
2485 // specifier. Let the actions module cope with it.
Chris Lattner76c72282007-10-09 17:33:22 +00002486 if (Tok.is(tok::semi)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00002487 Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none, DS);
Steve Naroff97170802007-08-20 22:28:22 +00002488 return;
2489 }
2490
2491 // Read struct-declarators until we find the semicolon.
John McCallcfefb6d2009-11-03 02:38:08 +00002492 bool FirstDeclarator = true;
Steve Naroff97170802007-08-20 22:28:22 +00002493 while (1) {
John McCall28a6aea2009-11-04 02:18:39 +00002494 ParsingDeclRAIIObject PD(*this);
John McCallcfefb6d2009-11-03 02:38:08 +00002495 FieldDeclarator DeclaratorInfo(DS);
2496
2497 // Attributes are only allowed here on successive declarators.
John McCall53fa7142010-12-24 02:08:15 +00002498 if (!FirstDeclarator)
2499 MaybeParseGNUAttributes(DeclaratorInfo.D);
Mike Stump11289f42009-09-09 15:08:12 +00002500
Steve Naroff97170802007-08-20 22:28:22 +00002501 /// struct-declarator: declarator
2502 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattner17c3b1f2009-12-10 01:59:24 +00002503 if (Tok.isNot(tok::colon)) {
2504 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
2505 ColonProtectionRAIIObject X(*this);
Chris Lattnera12405b2008-04-10 06:46:29 +00002506 ParseDeclarator(DeclaratorInfo.D);
Chris Lattner17c3b1f2009-12-10 01:59:24 +00002507 }
Mike Stump11289f42009-09-09 15:08:12 +00002508
Chris Lattner76c72282007-10-09 17:33:22 +00002509 if (Tok.is(tok::colon)) {
Steve Naroff97170802007-08-20 22:28:22 +00002510 ConsumeToken();
John McCalldadc5752010-08-24 06:29:42 +00002511 ExprResult Res(ParseConstantExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002512 if (Res.isInvalid())
Steve Naroff97170802007-08-20 22:28:22 +00002513 SkipUntil(tok::semi, true, true);
Chris Lattner32295d32008-04-10 06:15:14 +00002514 else
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00002515 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroff97170802007-08-20 22:28:22 +00002516 }
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002517
Steve Naroff97170802007-08-20 22:28:22 +00002518 // If attributes exist after the declarator, parse them.
John McCall53fa7142010-12-24 02:08:15 +00002519 MaybeParseGNUAttributes(DeclaratorInfo.D);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002520
John McCallcfefb6d2009-11-03 02:38:08 +00002521 // We're done with this declarator; invoke the callback.
John McCall48871652010-08-21 09:40:31 +00002522 Decl *D = Fields.invoke(DeclaratorInfo);
John McCall28a6aea2009-11-04 02:18:39 +00002523 PD.complete(D);
John McCallcfefb6d2009-11-03 02:38:08 +00002524
Steve Naroff97170802007-08-20 22:28:22 +00002525 // If we don't have a comma, it is either the end of the list (a ';')
2526 // or an error, bail out.
Chris Lattner76c72282007-10-09 17:33:22 +00002527 if (Tok.isNot(tok::comma))
Chris Lattner70ae4912007-10-29 04:42:53 +00002528 return;
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002529
Steve Naroff97170802007-08-20 22:28:22 +00002530 // Consume the comma.
2531 ConsumeToken();
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002532
John McCallcfefb6d2009-11-03 02:38:08 +00002533 FirstDeclarator = false;
Steve Naroff97170802007-08-20 22:28:22 +00002534 }
Steve Naroff97170802007-08-20 22:28:22 +00002535}
2536
2537/// ParseStructUnionBody
2538/// struct-contents:
2539/// struct-declaration-list
2540/// [EXT] empty
2541/// [GNU] "struct-declaration-list" without terminatoring ';'
2542/// struct-declaration-list:
2543/// struct-declaration
2544/// struct-declaration-list struct-declaration
Chris Lattner535b8302008-06-21 19:39:06 +00002545/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroff97170802007-08-20 22:28:22 +00002546///
Chris Lattner1300fb92007-01-23 23:42:53 +00002547void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
John McCall48871652010-08-21 09:40:31 +00002548 unsigned TagType, Decl *TagDecl) {
John McCallfaf5fb42010-08-26 23:41:50 +00002549 PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc,
2550 "parsing struct/union body");
Mike Stump11289f42009-09-09 15:08:12 +00002551
Chris Lattner90a26b02007-01-23 04:38:16 +00002552 SourceLocation LBraceLoc = ConsumeBrace();
Mike Stump11289f42009-09-09 15:08:12 +00002553
Douglas Gregor658b9552009-01-09 22:42:13 +00002554 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
Douglas Gregor0be31a22010-07-02 17:43:08 +00002555 Actions.ActOnTagStartDefinition(getCurScope(), TagDecl);
Douglas Gregor82ac25e2009-01-08 20:45:30 +00002556
Chris Lattner7b9ace62007-01-23 20:11:08 +00002557 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
2558 // C++.
Douglas Gregor556877c2008-04-13 21:30:24 +00002559 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Douglas Gregorda2955e2010-07-29 14:29:34 +00002560 Diag(Tok, diag::ext_empty_struct_union)
2561 << (TagType == TST_union);
Chris Lattner7b9ace62007-01-23 20:11:08 +00002562
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002563 SmallVector<Decl *, 32> FieldDecls;
Chris Lattnera12405b2008-04-10 06:46:29 +00002564
Chris Lattner7b9ace62007-01-23 20:11:08 +00002565 // While we still have something to read, read the declarations in the struct.
Chris Lattner76c72282007-10-09 17:33:22 +00002566 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Chris Lattner90a26b02007-01-23 04:38:16 +00002567 // Each iteration of this loop reads one struct-declaration.
Mike Stump11289f42009-09-09 15:08:12 +00002568
Chris Lattner736ed5d2007-06-09 05:59:07 +00002569 // Check for extraneous top-level semicolon.
Chris Lattner76c72282007-10-09 17:33:22 +00002570 if (Tok.is(tok::semi)) {
Douglas Gregore3e01a22009-04-01 22:41:11 +00002571 Diag(Tok, diag::ext_extra_struct_semi)
Douglas Gregor13d05682010-06-16 23:08:59 +00002572 << DeclSpec::getSpecifierName((DeclSpec::TST)TagType)
Douglas Gregora771f462010-03-31 17:46:05 +00002573 << FixItHint::CreateRemoval(Tok.getLocation());
Chris Lattner36e46a22007-06-09 05:49:55 +00002574 ConsumeToken();
2575 continue;
2576 }
Chris Lattnera12405b2008-04-10 06:46:29 +00002577
2578 // Parse all the comma separated declarators.
John McCall084e83d2011-03-24 11:26:52 +00002579 DeclSpec DS(AttrFactory);
Mike Stump11289f42009-09-09 15:08:12 +00002580
John McCallcfefb6d2009-11-03 02:38:08 +00002581 if (!Tok.is(tok::at)) {
2582 struct CFieldCallback : FieldCallback {
2583 Parser &P;
John McCall48871652010-08-21 09:40:31 +00002584 Decl *TagDecl;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002585 SmallVectorImpl<Decl *> &FieldDecls;
John McCallcfefb6d2009-11-03 02:38:08 +00002586
John McCall48871652010-08-21 09:40:31 +00002587 CFieldCallback(Parser &P, Decl *TagDecl,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002588 SmallVectorImpl<Decl *> &FieldDecls) :
John McCallcfefb6d2009-11-03 02:38:08 +00002589 P(P), TagDecl(TagDecl), FieldDecls(FieldDecls) {}
2590
John McCall48871652010-08-21 09:40:31 +00002591 virtual Decl *invoke(FieldDeclarator &FD) {
John McCallcfefb6d2009-11-03 02:38:08 +00002592 // Install the declarator into the current TagDecl.
John McCall48871652010-08-21 09:40:31 +00002593 Decl *Field = P.Actions.ActOnField(P.getCurScope(), TagDecl,
John McCall5e6253b2009-11-03 21:13:47 +00002594 FD.D.getDeclSpec().getSourceRange().getBegin(),
2595 FD.D, FD.BitfieldSize);
John McCallcfefb6d2009-11-03 02:38:08 +00002596 FieldDecls.push_back(Field);
2597 return Field;
Douglas Gregor66a985d2009-08-26 14:27:30 +00002598 }
John McCallcfefb6d2009-11-03 02:38:08 +00002599 } Callback(*this, TagDecl, FieldDecls);
2600
2601 ParseStructDeclaration(DS, Callback);
Chris Lattner535b8302008-06-21 19:39:06 +00002602 } else { // Handle @defs
2603 ConsumeToken();
2604 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
2605 Diag(Tok, diag::err_unexpected_at);
Chris Lattner245c5332010-02-02 00:37:27 +00002606 SkipUntil(tok::semi, true);
Chris Lattner535b8302008-06-21 19:39:06 +00002607 continue;
2608 }
2609 ConsumeToken();
2610 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
2611 if (!Tok.is(tok::identifier)) {
2612 Diag(Tok, diag::err_expected_ident);
Chris Lattner245c5332010-02-02 00:37:27 +00002613 SkipUntil(tok::semi, true);
Chris Lattner535b8302008-06-21 19:39:06 +00002614 continue;
2615 }
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002616 SmallVector<Decl *, 16> Fields;
Douglas Gregor0be31a22010-07-02 17:43:08 +00002617 Actions.ActOnDefs(getCurScope(), TagDecl, Tok.getLocation(),
Douglas Gregor91f84212008-12-11 16:49:14 +00002618 Tok.getIdentifierInfo(), Fields);
Chris Lattner535b8302008-06-21 19:39:06 +00002619 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
2620 ConsumeToken();
2621 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
Mike Stump11289f42009-09-09 15:08:12 +00002622 }
Chris Lattner736ed5d2007-06-09 05:59:07 +00002623
Chris Lattner76c72282007-10-09 17:33:22 +00002624 if (Tok.is(tok::semi)) {
Chris Lattner90a26b02007-01-23 04:38:16 +00002625 ConsumeToken();
Chris Lattner76c72282007-10-09 17:33:22 +00002626 } else if (Tok.is(tok::r_brace)) {
Chris Lattner245c5332010-02-02 00:37:27 +00002627 ExpectAndConsume(tok::semi, diag::ext_expected_semi_decl_list);
Chris Lattner0c7e82d2007-06-09 05:54:40 +00002628 break;
Chris Lattner90a26b02007-01-23 04:38:16 +00002629 } else {
Chris Lattner245c5332010-02-02 00:37:27 +00002630 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
2631 // Skip to end of block or statement to avoid ext-warning on extra ';'.
Chris Lattner90a26b02007-01-23 04:38:16 +00002632 SkipUntil(tok::r_brace, true, true);
Chris Lattner245c5332010-02-02 00:37:27 +00002633 // If we stopped at a ';', eat it.
2634 if (Tok.is(tok::semi)) ConsumeToken();
Chris Lattner90a26b02007-01-23 04:38:16 +00002635 }
2636 }
Mike Stump11289f42009-09-09 15:08:12 +00002637
Steve Naroff33a1e802007-10-29 21:38:07 +00002638 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Mike Stump11289f42009-09-09 15:08:12 +00002639
John McCall084e83d2011-03-24 11:26:52 +00002640 ParsedAttributes attrs(AttrFactory);
Chris Lattner90a26b02007-01-23 04:38:16 +00002641 // If attributes exist after struct contents, parse them.
John McCall53fa7142010-12-24 02:08:15 +00002642 MaybeParseGNUAttributes(attrs);
Daniel Dunbar15619c72008-10-03 02:03:53 +00002643
Douglas Gregor0be31a22010-07-02 17:43:08 +00002644 Actions.ActOnFields(getCurScope(),
David Blaikie751c5582011-09-22 02:58:26 +00002645 RecordLoc, TagDecl, FieldDecls,
Daniel Dunbar15619c72008-10-03 02:03:53 +00002646 LBraceLoc, RBraceLoc,
John McCall53fa7142010-12-24 02:08:15 +00002647 attrs.getList());
Douglas Gregor82ac25e2009-01-08 20:45:30 +00002648 StructScope.Exit();
Douglas Gregor0be31a22010-07-02 17:43:08 +00002649 Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl, RBraceLoc);
Chris Lattner90a26b02007-01-23 04:38:16 +00002650}
2651
Chris Lattner3b561a32006-08-13 00:12:11 +00002652/// ParseEnumSpecifier
Chris Lattner1890ac82006-08-13 01:16:23 +00002653/// enum-specifier: [C99 6.7.2.2]
Chris Lattner3b561a32006-08-13 00:12:11 +00002654/// 'enum' identifier[opt] '{' enumerator-list '}'
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002655///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Chris Lattnere37e2332006-08-15 04:50:22 +00002656/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
2657/// '}' attributes[opt]
Chris Lattner3b561a32006-08-13 00:12:11 +00002658/// 'enum' identifier
Chris Lattnere37e2332006-08-15 04:50:22 +00002659/// [GNU] 'enum' attributes[opt] identifier
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002660///
Douglas Gregor0bf31402010-10-08 23:50:27 +00002661/// [C++0x] enum-head '{' enumerator-list[opt] '}'
2662/// [C++0x] enum-head '{' enumerator-list ',' '}'
2663///
2664/// enum-head: [C++0x]
2665/// enum-key attributes[opt] identifier[opt] enum-base[opt]
2666/// enum-key attributes[opt] nested-name-specifier identifier enum-base[opt]
2667///
2668/// enum-key: [C++0x]
2669/// 'enum'
2670/// 'enum' 'class'
2671/// 'enum' 'struct'
2672///
2673/// enum-base: [C++0x]
2674/// ':' type-specifier-seq
2675///
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002676/// [C++] elaborated-type-specifier:
2677/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
2678///
Chris Lattnerffaa0e62009-04-12 21:49:30 +00002679void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregordc70c3a2010-03-02 17:53:14 +00002680 const ParsedTemplateInfo &TemplateInfo,
Chris Lattnerffaa0e62009-04-12 21:49:30 +00002681 AccessSpecifier AS) {
Chris Lattnerffbc2712007-01-25 06:05:38 +00002682 // Parse the tag portion of this.
Douglas Gregorf45b0cf2009-09-18 15:37:17 +00002683 if (Tok.is(tok::code_completion)) {
2684 // Code completion for an enum name.
Douglas Gregor0be31a22010-07-02 17:43:08 +00002685 Actions.CodeCompleteTag(getCurScope(), DeclSpec::TST_enum);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002686 return cutOffParsing();
Douglas Gregorf45b0cf2009-09-18 15:37:17 +00002687 }
John McCallcb432fa2011-07-06 05:58:41 +00002688
2689 bool IsScopedEnum = false;
2690 bool IsScopedUsingClassTag = false;
2691
2692 if (getLang().CPlusPlus0x &&
2693 (Tok.is(tok::kw_class) || Tok.is(tok::kw_struct))) {
2694 IsScopedEnum = true;
2695 IsScopedUsingClassTag = Tok.is(tok::kw_class);
2696 ConsumeToken();
2697 }
Douglas Gregorf45b0cf2009-09-18 15:37:17 +00002698
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00002699 // If attributes exist after tag, parse them.
John McCall084e83d2011-03-24 11:26:52 +00002700 ParsedAttributes attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00002701 MaybeParseGNUAttributes(attrs);
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002702
Douglas Gregor8b7d4032011-09-08 17:18:35 +00002703 bool AllowFixedUnderlyingType
Francois Pichet0706d202011-09-17 17:15:52 +00002704 = getLang().CPlusPlus0x || getLang().MicrosoftExt || getLang().ObjC2;
John McCallcb432fa2011-07-06 05:58:41 +00002705
Abramo Bagnarad7548482010-05-19 21:37:53 +00002706 CXXScopeSpec &SS = DS.getTypeSpecScope();
John McCall1f476a12010-02-26 08:45:28 +00002707 if (getLang().CPlusPlus) {
John McCallcb432fa2011-07-06 05:58:41 +00002708 // "enum foo : bar;" is not a potential typo for "enum foo::bar;"
2709 // if a fixed underlying type is allowed.
2710 ColonProtectionRAIIObject X(*this, AllowFixedUnderlyingType);
2711
John McCallba7bf592010-08-24 05:47:05 +00002712 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), false))
John McCall1f476a12010-02-26 08:45:28 +00002713 return;
2714
2715 if (SS.isSet() && Tok.isNot(tok::identifier)) {
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002716 Diag(Tok, diag::err_expected_ident);
2717 if (Tok.isNot(tok::l_brace)) {
2718 // Has no name and is not a definition.
2719 // Skip the rest of this declarator, up until the comma or semicolon.
2720 SkipUntil(tok::comma, true);
2721 return;
2722 }
2723 }
2724 }
Mike Stump11289f42009-09-09 15:08:12 +00002725
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00002726 // Must have either 'enum name' or 'enum {...}'.
Douglas Gregor6cd5ae42011-02-22 02:55:24 +00002727 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace) &&
2728 (AllowFixedUnderlyingType && Tok.isNot(tok::colon))) {
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00002729 Diag(Tok, diag::err_expected_ident_lbrace);
Mike Stump11289f42009-09-09 15:08:12 +00002730
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00002731 // Skip the rest of this declarator, up until the comma or semicolon.
2732 SkipUntil(tok::comma, true);
Chris Lattner3b561a32006-08-13 00:12:11 +00002733 return;
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00002734 }
Mike Stump11289f42009-09-09 15:08:12 +00002735
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00002736 // If an identifier is present, consume and remember it.
2737 IdentifierInfo *Name = 0;
2738 SourceLocation NameLoc;
2739 if (Tok.is(tok::identifier)) {
2740 Name = Tok.getIdentifierInfo();
2741 NameLoc = ConsumeToken();
2742 }
Mike Stump11289f42009-09-09 15:08:12 +00002743
Douglas Gregor0bf31402010-10-08 23:50:27 +00002744 if (!Name && IsScopedEnum) {
2745 // C++0x 7.2p2: The optional identifier shall not be omitted in the
2746 // declaration of a scoped enumeration.
2747 Diag(Tok, diag::err_scoped_enum_missing_identifier);
2748 IsScopedEnum = false;
Abramo Bagnara0e05e242010-12-03 18:54:17 +00002749 IsScopedUsingClassTag = false;
Douglas Gregor0bf31402010-10-08 23:50:27 +00002750 }
2751
2752 TypeResult BaseType;
2753
Douglas Gregord1f69f62010-12-01 17:42:47 +00002754 // Parse the fixed underlying type.
Douglas Gregor6cd5ae42011-02-22 02:55:24 +00002755 if (AllowFixedUnderlyingType && Tok.is(tok::colon)) {
Douglas Gregord1f69f62010-12-01 17:42:47 +00002756 bool PossibleBitfield = false;
2757 if (getCurScope()->getFlags() & Scope::ClassScope) {
2758 // If we're in class scope, this can either be an enum declaration with
2759 // an underlying type, or a declaration of a bitfield member. We try to
2760 // use a simple disambiguation scheme first to catch the common cases
2761 // (integer literal, sizeof); if it's still ambiguous, we then consider
2762 // anything that's a simple-type-specifier followed by '(' as an
2763 // expression. This suffices because function types are not valid
2764 // underlying types anyway.
2765 TPResult TPR = isExpressionOrTypeSpecifierSimple(NextToken().getKind());
2766 // If the next token starts an expression, we know we're parsing a
2767 // bit-field. This is the common case.
2768 if (TPR == TPResult::True())
2769 PossibleBitfield = true;
2770 // If the next token starts a type-specifier-seq, it may be either a
2771 // a fixed underlying type or the start of a function-style cast in C++;
2772 // lookahead one more token to see if it's obvious that we have a
2773 // fixed underlying type.
2774 else if (TPR == TPResult::False() &&
2775 GetLookAheadToken(2).getKind() == tok::semi) {
2776 // Consume the ':'.
2777 ConsumeToken();
2778 } else {
2779 // We have the start of a type-specifier-seq, so we have to perform
2780 // tentative parsing to determine whether we have an expression or a
2781 // type.
2782 TentativeParsingAction TPA(*this);
2783
2784 // Consume the ':'.
2785 ConsumeToken();
2786
Douglas Gregora1aec292011-02-22 20:32:04 +00002787 if ((getLang().CPlusPlus &&
2788 isCXXDeclarationSpecifier() != TPResult::True()) ||
2789 (!getLang().CPlusPlus && !isDeclarationSpecifier(true))) {
Douglas Gregord1f69f62010-12-01 17:42:47 +00002790 // We'll parse this as a bitfield later.
2791 PossibleBitfield = true;
2792 TPA.Revert();
2793 } else {
2794 // We have a type-specifier-seq.
2795 TPA.Commit();
2796 }
2797 }
2798 } else {
2799 // Consume the ':'.
2800 ConsumeToken();
2801 }
2802
2803 if (!PossibleBitfield) {
2804 SourceRange Range;
2805 BaseType = ParseTypeName(&Range);
Douglas Gregora1aec292011-02-22 20:32:04 +00002806
Douglas Gregor8b7d4032011-09-08 17:18:35 +00002807 if (!getLang().CPlusPlus0x && !getLang().ObjC2)
Douglas Gregora1aec292011-02-22 20:32:04 +00002808 Diag(StartLoc, diag::ext_ms_enum_fixed_underlying_type)
2809 << Range;
Douglas Gregord1f69f62010-12-01 17:42:47 +00002810 }
Douglas Gregor0bf31402010-10-08 23:50:27 +00002811 }
2812
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00002813 // There are three options here. If we have 'enum foo;', then this is a
2814 // forward declaration. If we have 'enum foo {...' then this is a
2815 // definition. Otherwise we have something like 'enum foo xyz', a reference.
2816 //
2817 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
2818 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
2819 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
2820 //
John McCallfaf5fb42010-08-26 23:41:50 +00002821 Sema::TagUseKind TUK;
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00002822 if (Tok.is(tok::l_brace))
John McCallfaf5fb42010-08-26 23:41:50 +00002823 TUK = Sema::TUK_Definition;
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00002824 else if (Tok.is(tok::semi))
John McCallfaf5fb42010-08-26 23:41:50 +00002825 TUK = Sema::TUK_Declaration;
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00002826 else
John McCallfaf5fb42010-08-26 23:41:50 +00002827 TUK = Sema::TUK_Reference;
Douglas Gregorcbbf3e32010-05-03 17:48:54 +00002828
2829 // enums cannot be templates, although they can be referenced from a
2830 // template.
2831 if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate &&
John McCallfaf5fb42010-08-26 23:41:50 +00002832 TUK != Sema::TUK_Reference) {
Douglas Gregorcbbf3e32010-05-03 17:48:54 +00002833 Diag(Tok, diag::err_enum_template);
2834
2835 // Skip the rest of this declarator, up until the comma or semicolon.
2836 SkipUntil(tok::comma, true);
2837 return;
2838 }
2839
Douglas Gregor6cd5ae42011-02-22 02:55:24 +00002840 if (!Name && TUK != Sema::TUK_Definition) {
2841 Diag(Tok, diag::err_enumerator_unnamed_no_def);
2842
2843 // Skip the rest of this declarator, up until the comma or semicolon.
2844 SkipUntil(tok::comma, true);
2845 return;
2846 }
2847
Douglas Gregord6ab8742009-05-28 23:31:59 +00002848 bool Owned = false;
John McCall7f41d982009-09-11 04:59:25 +00002849 bool IsDependent = false;
Douglas Gregorba41d012010-04-24 16:38:41 +00002850 const char *PrevSpec = 0;
2851 unsigned DiagID;
John McCall48871652010-08-21 09:40:31 +00002852 Decl *TagDecl = Actions.ActOnTag(getCurScope(), DeclSpec::TST_enum, TUK,
John McCall53fa7142010-12-24 02:08:15 +00002853 StartLoc, SS, Name, NameLoc, attrs.getList(),
Douglas Gregor2820e692011-09-09 19:05:14 +00002854 AS, DS.getModulePrivateSpecLoc(),
John McCallfaf5fb42010-08-26 23:41:50 +00002855 MultiTemplateParamsArg(Actions),
Douglas Gregor0bf31402010-10-08 23:50:27 +00002856 Owned, IsDependent, IsScopedEnum,
Abramo Bagnara0e05e242010-12-03 18:54:17 +00002857 IsScopedUsingClassTag, BaseType);
Douglas Gregor0bf31402010-10-08 23:50:27 +00002858
Douglas Gregorba41d012010-04-24 16:38:41 +00002859 if (IsDependent) {
2860 // This enum has a dependent nested-name-specifier. Handle it as a
2861 // dependent tag.
2862 if (!Name) {
2863 DS.SetTypeSpecError();
2864 Diag(Tok, diag::err_expected_type_name_after_typename);
2865 return;
2866 }
2867
Douglas Gregor0be31a22010-07-02 17:43:08 +00002868 TypeResult Type = Actions.ActOnDependentTag(getCurScope(), DeclSpec::TST_enum,
Douglas Gregorba41d012010-04-24 16:38:41 +00002869 TUK, SS, Name, StartLoc,
2870 NameLoc);
2871 if (Type.isInvalid()) {
2872 DS.SetTypeSpecError();
2873 return;
2874 }
2875
Abramo Bagnara9875a3c2011-03-16 20:16:18 +00002876 if (DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc,
2877 NameLoc.isValid() ? NameLoc : StartLoc,
2878 PrevSpec, DiagID, Type.get()))
Douglas Gregorba41d012010-04-24 16:38:41 +00002879 Diag(StartLoc, DiagID) << PrevSpec;
2880
2881 return;
2882 }
Mike Stump11289f42009-09-09 15:08:12 +00002883
John McCall48871652010-08-21 09:40:31 +00002884 if (!TagDecl) {
Douglas Gregorba41d012010-04-24 16:38:41 +00002885 // The action failed to produce an enumeration tag. If this is a
2886 // definition, consume the entire definition.
2887 if (Tok.is(tok::l_brace)) {
2888 ConsumeBrace();
2889 SkipUntil(tok::r_brace);
2890 }
2891
2892 DS.SetTypeSpecError();
2893 return;
2894 }
2895
Chris Lattner76c72282007-10-09 17:33:22 +00002896 if (Tok.is(tok::l_brace))
Chris Lattnerc1915e22007-01-25 07:29:02 +00002897 ParseEnumBody(StartLoc, TagDecl);
Mike Stump11289f42009-09-09 15:08:12 +00002898
Abramo Bagnara9875a3c2011-03-16 20:16:18 +00002899 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc,
2900 NameLoc.isValid() ? NameLoc : StartLoc,
2901 PrevSpec, DiagID, TagDecl, Owned))
John McCall49bfce42009-08-03 20:12:06 +00002902 Diag(StartLoc, DiagID) << PrevSpec;
Chris Lattner3b561a32006-08-13 00:12:11 +00002903}
2904
Chris Lattnerc1915e22007-01-25 07:29:02 +00002905/// ParseEnumBody - Parse a {} enclosed enumerator-list.
2906/// enumerator-list:
2907/// enumerator
2908/// enumerator-list ',' enumerator
2909/// enumerator:
2910/// enumeration-constant
2911/// enumeration-constant '=' constant-expression
2912/// enumeration-constant:
2913/// identifier
2914///
John McCall48871652010-08-21 09:40:31 +00002915void Parser::ParseEnumBody(SourceLocation StartLoc, Decl *EnumDecl) {
Douglas Gregor07665a62009-01-05 19:45:36 +00002916 // Enter the scope of the enum body and start the definition.
2917 ParseScope EnumScope(this, Scope::DeclScope);
Douglas Gregor0be31a22010-07-02 17:43:08 +00002918 Actions.ActOnTagStartDefinition(getCurScope(), EnumDecl);
Douglas Gregor07665a62009-01-05 19:45:36 +00002919
Chris Lattnerc1915e22007-01-25 07:29:02 +00002920 SourceLocation LBraceLoc = ConsumeBrace();
Mike Stump11289f42009-09-09 15:08:12 +00002921
Chris Lattner37256fb2007-08-27 17:24:30 +00002922 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
Chris Lattner76c72282007-10-09 17:33:22 +00002923 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Fariborz Jahanian6e814922010-05-28 22:23:22 +00002924 Diag(Tok, diag::error_empty_enum);
Mike Stump11289f42009-09-09 15:08:12 +00002925
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002926 SmallVector<Decl *, 32> EnumConstantDecls;
Chris Lattnerc1915e22007-01-25 07:29:02 +00002927
John McCall48871652010-08-21 09:40:31 +00002928 Decl *LastEnumConstDecl = 0;
Mike Stump11289f42009-09-09 15:08:12 +00002929
Chris Lattnerc1915e22007-01-25 07:29:02 +00002930 // Parse the enumerator-list.
Chris Lattner76c72282007-10-09 17:33:22 +00002931 while (Tok.is(tok::identifier)) {
Chris Lattnerc1915e22007-01-25 07:29:02 +00002932 IdentifierInfo *Ident = Tok.getIdentifierInfo();
2933 SourceLocation IdentLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00002934
John McCall811a0f52010-10-22 23:36:17 +00002935 // If attributes exist after the enumerator, parse them.
John McCall084e83d2011-03-24 11:26:52 +00002936 ParsedAttributes attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00002937 MaybeParseGNUAttributes(attrs);
John McCall811a0f52010-10-22 23:36:17 +00002938
Chris Lattnerc1915e22007-01-25 07:29:02 +00002939 SourceLocation EqualLoc;
John McCalldadc5752010-08-24 06:29:42 +00002940 ExprResult AssignedVal;
Chris Lattner76c72282007-10-09 17:33:22 +00002941 if (Tok.is(tok::equal)) {
Chris Lattnerc1915e22007-01-25 07:29:02 +00002942 EqualLoc = ConsumeToken();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002943 AssignedVal = ParseConstantExpression();
2944 if (AssignedVal.isInvalid())
Chris Lattnerda6c2ce2007-04-27 19:13:15 +00002945 SkipUntil(tok::comma, tok::r_brace, true, true);
Chris Lattnerc1915e22007-01-25 07:29:02 +00002946 }
Mike Stump11289f42009-09-09 15:08:12 +00002947
Chris Lattnerc1915e22007-01-25 07:29:02 +00002948 // Install the enumerator constant into EnumDecl.
John McCall48871652010-08-21 09:40:31 +00002949 Decl *EnumConstDecl = Actions.ActOnEnumConstant(getCurScope(), EnumDecl,
2950 LastEnumConstDecl,
2951 IdentLoc, Ident,
John McCall53fa7142010-12-24 02:08:15 +00002952 attrs.getList(), EqualLoc,
John McCall48871652010-08-21 09:40:31 +00002953 AssignedVal.release());
Chris Lattner4ef40012007-06-11 01:28:17 +00002954 EnumConstantDecls.push_back(EnumConstDecl);
2955 LastEnumConstDecl = EnumConstDecl;
Mike Stump11289f42009-09-09 15:08:12 +00002956
Douglas Gregorce66d022010-09-07 14:51:08 +00002957 if (Tok.is(tok::identifier)) {
2958 // We're missing a comma between enumerators.
2959 SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation);
2960 Diag(Loc, diag::err_enumerator_list_missing_comma)
2961 << FixItHint::CreateInsertion(Loc, ", ");
2962 continue;
2963 }
2964
Chris Lattner76c72282007-10-09 17:33:22 +00002965 if (Tok.isNot(tok::comma))
Chris Lattnerc1915e22007-01-25 07:29:02 +00002966 break;
2967 SourceLocation CommaLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00002968
2969 if (Tok.isNot(tok::identifier) &&
Douglas Gregore3e01a22009-04-01 22:41:11 +00002970 !(getLang().C99 || getLang().CPlusPlus0x))
2971 Diag(CommaLoc, diag::ext_enumerator_list_comma)
2972 << getLang().CPlusPlus
Douglas Gregora771f462010-03-31 17:46:05 +00002973 << FixItHint::CreateRemoval(CommaLoc);
Chris Lattnerc1915e22007-01-25 07:29:02 +00002974 }
Mike Stump11289f42009-09-09 15:08:12 +00002975
Chris Lattnerc1915e22007-01-25 07:29:02 +00002976 // Eat the }.
Mike Stump6814d1c2009-05-16 07:06:02 +00002977 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Chris Lattnerc1915e22007-01-25 07:29:02 +00002978
Chris Lattnerc1915e22007-01-25 07:29:02 +00002979 // If attributes exist after the identifier list, parse them.
John McCall084e83d2011-03-24 11:26:52 +00002980 ParsedAttributes attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00002981 MaybeParseGNUAttributes(attrs);
Douglas Gregor82ac25e2009-01-08 20:45:30 +00002982
Edward O'Callaghanc69169d2009-08-08 14:36:57 +00002983 Actions.ActOnEnumBody(StartLoc, LBraceLoc, RBraceLoc, EnumDecl,
2984 EnumConstantDecls.data(), EnumConstantDecls.size(),
John McCall53fa7142010-12-24 02:08:15 +00002985 getCurScope(), attrs.getList());
Mike Stump11289f42009-09-09 15:08:12 +00002986
Douglas Gregor82ac25e2009-01-08 20:45:30 +00002987 EnumScope.Exit();
Douglas Gregor0be31a22010-07-02 17:43:08 +00002988 Actions.ActOnTagFinishDefinition(getCurScope(), EnumDecl, RBraceLoc);
Chris Lattnerc1915e22007-01-25 07:29:02 +00002989}
Chris Lattner3b561a32006-08-13 00:12:11 +00002990
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002991/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff69e8f9e2008-02-11 23:15:56 +00002992/// start of a type-qualifier-list.
2993bool Parser::isTypeQualifier() const {
2994 switch (Tok.getKind()) {
2995 default: return false;
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00002996
2997 // type-qualifier only in OpenCL
2998 case tok::kw_private:
2999 return getLang().OpenCL;
3000
Steve Naroff69e8f9e2008-02-11 23:15:56 +00003001 // type-qualifier
3002 case tok::kw_const:
3003 case tok::kw_volatile:
3004 case tok::kw_restrict:
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003005 case tok::kw___private:
3006 case tok::kw___local:
3007 case tok::kw___global:
3008 case tok::kw___constant:
3009 case tok::kw___read_only:
3010 case tok::kw___read_write:
3011 case tok::kw___write_only:
Steve Naroff69e8f9e2008-02-11 23:15:56 +00003012 return true;
3013 }
3014}
3015
Chris Lattnerfd48afe2010-02-28 18:18:36 +00003016/// isKnownToBeTypeSpecifier - Return true if we know that the specified token
3017/// is definitely a type-specifier. Return false if it isn't part of a type
3018/// specifier or if we're not sure.
3019bool Parser::isKnownToBeTypeSpecifier(const Token &Tok) const {
3020 switch (Tok.getKind()) {
3021 default: return false;
3022 // type-specifiers
3023 case tok::kw_short:
3024 case tok::kw_long:
Francois Pichet84133e42011-04-28 01:59:37 +00003025 case tok::kw___int64:
Chris Lattnerfd48afe2010-02-28 18:18:36 +00003026 case tok::kw_signed:
3027 case tok::kw_unsigned:
3028 case tok::kw__Complex:
3029 case tok::kw__Imaginary:
3030 case tok::kw_void:
3031 case tok::kw_char:
3032 case tok::kw_wchar_t:
3033 case tok::kw_char16_t:
3034 case tok::kw_char32_t:
3035 case tok::kw_int:
3036 case tok::kw_float:
3037 case tok::kw_double:
3038 case tok::kw_bool:
3039 case tok::kw__Bool:
3040 case tok::kw__Decimal32:
3041 case tok::kw__Decimal64:
3042 case tok::kw__Decimal128:
3043 case tok::kw___vector:
3044
3045 // struct-or-union-specifier (C99) or class-specifier (C++)
3046 case tok::kw_class:
3047 case tok::kw_struct:
3048 case tok::kw_union:
3049 // enum-specifier
3050 case tok::kw_enum:
3051
3052 // typedef-name
3053 case tok::annot_typename:
3054 return true;
3055 }
3056}
3057
Steve Naroff69e8f9e2008-02-11 23:15:56 +00003058/// isTypeSpecifierQualifier - Return true if the current token could be the
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003059/// start of a specifier-qualifier-list.
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003060bool Parser::isTypeSpecifierQualifier() {
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003061 switch (Tok.getKind()) {
3062 default: return false;
Mike Stump11289f42009-09-09 15:08:12 +00003063
Chris Lattner020bab92009-01-04 23:41:41 +00003064 case tok::identifier: // foo::bar
John Thompson22334602010-02-05 00:12:22 +00003065 if (TryAltiVecVectorToken())
3066 return true;
3067 // Fall through.
Douglas Gregor333489b2009-03-27 23:10:48 +00003068 case tok::kw_typename: // typename T::type
Chris Lattner020bab92009-01-04 23:41:41 +00003069 // Annotate typenames and C++ scope specifiers. If we get one, just
3070 // recurse to handle whatever we get.
3071 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00003072 return true;
3073 if (Tok.is(tok::identifier))
3074 return false;
3075 return isTypeSpecifierQualifier();
Douglas Gregor333489b2009-03-27 23:10:48 +00003076
Chris Lattner020bab92009-01-04 23:41:41 +00003077 case tok::coloncolon: // ::foo::bar
3078 if (NextToken().is(tok::kw_new) || // ::new
3079 NextToken().is(tok::kw_delete)) // ::delete
3080 return false;
3081
Chris Lattner020bab92009-01-04 23:41:41 +00003082 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00003083 return true;
3084 return isTypeSpecifierQualifier();
Mike Stump11289f42009-09-09 15:08:12 +00003085
Chris Lattnere37e2332006-08-15 04:50:22 +00003086 // GNU attributes support.
3087 case tok::kw___attribute:
Steve Naroffad373bd2007-07-31 12:34:36 +00003088 // GNU typeof support.
3089 case tok::kw_typeof:
Mike Stump11289f42009-09-09 15:08:12 +00003090
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003091 // type-specifiers
3092 case tok::kw_short:
3093 case tok::kw_long:
Francois Pichet84133e42011-04-28 01:59:37 +00003094 case tok::kw___int64:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003095 case tok::kw_signed:
3096 case tok::kw_unsigned:
3097 case tok::kw__Complex:
3098 case tok::kw__Imaginary:
3099 case tok::kw_void:
3100 case tok::kw_char:
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +00003101 case tok::kw_wchar_t:
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00003102 case tok::kw_char16_t:
3103 case tok::kw_char32_t:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003104 case tok::kw_int:
3105 case tok::kw_float:
3106 case tok::kw_double:
Chris Lattnerbb31a422007-11-15 05:25:19 +00003107 case tok::kw_bool:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003108 case tok::kw__Bool:
3109 case tok::kw__Decimal32:
3110 case tok::kw__Decimal64:
3111 case tok::kw__Decimal128:
John Thompson22334602010-02-05 00:12:22 +00003112 case tok::kw___vector:
Mike Stump11289f42009-09-09 15:08:12 +00003113
Chris Lattner861a2262008-04-13 18:59:07 +00003114 // struct-or-union-specifier (C99) or class-specifier (C++)
3115 case tok::kw_class:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003116 case tok::kw_struct:
3117 case tok::kw_union:
3118 // enum-specifier
3119 case tok::kw_enum:
Mike Stump11289f42009-09-09 15:08:12 +00003120
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003121 // type-qualifier
3122 case tok::kw_const:
3123 case tok::kw_volatile:
3124 case tok::kw_restrict:
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003125
3126 // typedef-name
Chris Lattnera8a3f732009-01-06 05:06:21 +00003127 case tok::annot_typename:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003128 return true;
Mike Stump11289f42009-09-09 15:08:12 +00003129
Chris Lattner409bf7d2008-10-20 00:25:30 +00003130 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
3131 case tok::less:
3132 return getLang().ObjC1;
Mike Stump11289f42009-09-09 15:08:12 +00003133
Steve Naroff44ac7772008-12-25 14:16:32 +00003134 case tok::kw___cdecl:
3135 case tok::kw___stdcall:
3136 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00003137 case tok::kw___thiscall:
Eli Friedman53339e02009-06-08 23:27:34 +00003138 case tok::kw___w64:
3139 case tok::kw___ptr64:
Francois Pichetf2fb4112011-08-25 00:36:46 +00003140 case tok::kw___ptr32:
Dawn Perchik335e16b2010-09-03 01:29:35 +00003141 case tok::kw___pascal:
Francois Pichet17ed0202011-08-18 09:59:55 +00003142 case tok::kw___unaligned:
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003143
3144 case tok::kw___private:
3145 case tok::kw___local:
3146 case tok::kw___global:
3147 case tok::kw___constant:
3148 case tok::kw___read_only:
3149 case tok::kw___read_write:
3150 case tok::kw___write_only:
3151
Eli Friedman53339e02009-06-08 23:27:34 +00003152 return true;
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003153
3154 case tok::kw_private:
3155 return getLang().OpenCL;
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003156 }
3157}
3158
Chris Lattneracd58a32006-08-06 17:24:14 +00003159/// isDeclarationSpecifier() - Return true if the current token is part of a
3160/// declaration specifier.
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00003161///
3162/// \param DisambiguatingWithExpression True to indicate that the purpose of
3163/// this check is to disambiguate between an expression and a declaration.
3164bool Parser::isDeclarationSpecifier(bool DisambiguatingWithExpression) {
Chris Lattneracd58a32006-08-06 17:24:14 +00003165 switch (Tok.getKind()) {
3166 default: return false;
Mike Stump11289f42009-09-09 15:08:12 +00003167
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003168 case tok::kw_private:
3169 return getLang().OpenCL;
3170
Chris Lattner020bab92009-01-04 23:41:41 +00003171 case tok::identifier: // foo::bar
Steve Naroff9527bbf2009-03-09 21:12:44 +00003172 // Unfortunate hack to support "Class.factoryMethod" notation.
3173 if (getLang().ObjC1 && NextToken().is(tok::period))
3174 return false;
John Thompson22334602010-02-05 00:12:22 +00003175 if (TryAltiVecVectorToken())
3176 return true;
3177 // Fall through.
Douglas Gregor333489b2009-03-27 23:10:48 +00003178 case tok::kw_typename: // typename T::type
Chris Lattner020bab92009-01-04 23:41:41 +00003179 // Annotate typenames and C++ scope specifiers. If we get one, just
3180 // recurse to handle whatever we get.
3181 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00003182 return true;
3183 if (Tok.is(tok::identifier))
3184 return false;
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00003185
3186 // If we're in Objective-C and we have an Objective-C class type followed
3187 // by an identifier and then either ':' or ']', in a place where an
3188 // expression is permitted, then this is probably a class message send
3189 // missing the initial '['. In this case, we won't consider this to be
3190 // the start of a declaration.
3191 if (DisambiguatingWithExpression &&
3192 isStartOfObjCClassMessageMissingOpenBracket())
3193 return false;
3194
John McCall1f476a12010-02-26 08:45:28 +00003195 return isDeclarationSpecifier();
3196
Chris Lattner020bab92009-01-04 23:41:41 +00003197 case tok::coloncolon: // ::foo::bar
3198 if (NextToken().is(tok::kw_new) || // ::new
3199 NextToken().is(tok::kw_delete)) // ::delete
3200 return false;
Mike Stump11289f42009-09-09 15:08:12 +00003201
Chris Lattner020bab92009-01-04 23:41:41 +00003202 // Annotate typenames and C++ scope specifiers. If we get one, just
3203 // recurse to handle whatever we get.
3204 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00003205 return true;
3206 return isDeclarationSpecifier();
Mike Stump11289f42009-09-09 15:08:12 +00003207
Chris Lattneracd58a32006-08-06 17:24:14 +00003208 // storage-class-specifier
3209 case tok::kw_typedef:
3210 case tok::kw_extern:
Steve Naroff2050b0d2007-12-18 00:16:02 +00003211 case tok::kw___private_extern__:
Chris Lattneracd58a32006-08-06 17:24:14 +00003212 case tok::kw_static:
3213 case tok::kw_auto:
3214 case tok::kw_register:
3215 case tok::kw___thread:
Mike Stump11289f42009-09-09 15:08:12 +00003216
Douglas Gregor26701a42011-09-09 02:06:17 +00003217 // Modules
3218 case tok::kw___module_private__:
3219
Chris Lattneracd58a32006-08-06 17:24:14 +00003220 // type-specifiers
3221 case tok::kw_short:
3222 case tok::kw_long:
Francois Pichet84133e42011-04-28 01:59:37 +00003223 case tok::kw___int64:
Chris Lattneracd58a32006-08-06 17:24:14 +00003224 case tok::kw_signed:
3225 case tok::kw_unsigned:
3226 case tok::kw__Complex:
3227 case tok::kw__Imaginary:
3228 case tok::kw_void:
3229 case tok::kw_char:
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +00003230 case tok::kw_wchar_t:
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00003231 case tok::kw_char16_t:
3232 case tok::kw_char32_t:
3233
Chris Lattneracd58a32006-08-06 17:24:14 +00003234 case tok::kw_int:
3235 case tok::kw_float:
3236 case tok::kw_double:
Chris Lattnerbb31a422007-11-15 05:25:19 +00003237 case tok::kw_bool:
Chris Lattneracd58a32006-08-06 17:24:14 +00003238 case tok::kw__Bool:
3239 case tok::kw__Decimal32:
3240 case tok::kw__Decimal64:
3241 case tok::kw__Decimal128:
John Thompson22334602010-02-05 00:12:22 +00003242 case tok::kw___vector:
Mike Stump11289f42009-09-09 15:08:12 +00003243
Chris Lattner861a2262008-04-13 18:59:07 +00003244 // struct-or-union-specifier (C99) or class-specifier (C++)
3245 case tok::kw_class:
Chris Lattneracd58a32006-08-06 17:24:14 +00003246 case tok::kw_struct:
3247 case tok::kw_union:
3248 // enum-specifier
3249 case tok::kw_enum:
Mike Stump11289f42009-09-09 15:08:12 +00003250
Chris Lattneracd58a32006-08-06 17:24:14 +00003251 // type-qualifier
3252 case tok::kw_const:
3253 case tok::kw_volatile:
3254 case tok::kw_restrict:
Steve Naroffad373bd2007-07-31 12:34:36 +00003255
Chris Lattneracd58a32006-08-06 17:24:14 +00003256 // function-specifier
3257 case tok::kw_inline:
Douglas Gregor61956c42008-10-31 09:07:45 +00003258 case tok::kw_virtual:
3259 case tok::kw_explicit:
Chris Lattner7b20dc72007-08-09 16:40:21 +00003260
Peter Collingbourne3d9cbdc2011-04-15 00:35:57 +00003261 // static_assert-declaration
3262 case tok::kw__Static_assert:
3263
Chris Lattner599e47e2007-08-09 17:01:07 +00003264 // GNU typeof support.
3265 case tok::kw_typeof:
Mike Stump11289f42009-09-09 15:08:12 +00003266
Chris Lattner599e47e2007-08-09 17:01:07 +00003267 // GNU attributes.
Chris Lattner7b20dc72007-08-09 16:40:21 +00003268 case tok::kw___attribute:
Chris Lattneracd58a32006-08-06 17:24:14 +00003269 return true;
Mike Stump11289f42009-09-09 15:08:12 +00003270
Francois Pichete878cb62011-06-19 08:02:06 +00003271 // C++0x decltype.
3272 case tok::kw_decltype:
3273 return true;
3274
Chris Lattner8b2ec162008-07-26 03:38:44 +00003275 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
3276 case tok::less:
3277 return getLang().ObjC1;
Mike Stump11289f42009-09-09 15:08:12 +00003278
Douglas Gregor19b7acf2011-04-27 05:41:15 +00003279 // typedef-name
3280 case tok::annot_typename:
3281 return !DisambiguatingWithExpression ||
3282 !isStartOfObjCClassMessageMissingOpenBracket();
3283
Steve Narofff192fab2009-01-06 19:34:12 +00003284 case tok::kw___declspec:
Steve Naroff44ac7772008-12-25 14:16:32 +00003285 case tok::kw___cdecl:
3286 case tok::kw___stdcall:
3287 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00003288 case tok::kw___thiscall:
Eli Friedman53339e02009-06-08 23:27:34 +00003289 case tok::kw___w64:
3290 case tok::kw___ptr64:
Francois Pichetf2fb4112011-08-25 00:36:46 +00003291 case tok::kw___ptr32:
Eli Friedman53339e02009-06-08 23:27:34 +00003292 case tok::kw___forceinline:
Dawn Perchik335e16b2010-09-03 01:29:35 +00003293 case tok::kw___pascal:
Francois Pichet17ed0202011-08-18 09:59:55 +00003294 case tok::kw___unaligned:
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003295
3296 case tok::kw___private:
3297 case tok::kw___local:
3298 case tok::kw___global:
3299 case tok::kw___constant:
3300 case tok::kw___read_only:
3301 case tok::kw___read_write:
3302 case tok::kw___write_only:
3303
Eli Friedman53339e02009-06-08 23:27:34 +00003304 return true;
Chris Lattneracd58a32006-08-06 17:24:14 +00003305 }
3306}
3307
Douglas Gregor9de54ea2010-01-13 17:31:36 +00003308bool Parser::isConstructorDeclarator() {
3309 TentativeParsingAction TPA(*this);
3310
3311 // Parse the C++ scope specifier.
3312 CXXScopeSpec SS;
John McCallba7bf592010-08-24 05:47:05 +00003313 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), true)) {
John McCall1f476a12010-02-26 08:45:28 +00003314 TPA.Revert();
3315 return false;
3316 }
Douglas Gregor9de54ea2010-01-13 17:31:36 +00003317
3318 // Parse the constructor name.
3319 if (Tok.is(tok::identifier) || Tok.is(tok::annot_template_id)) {
3320 // We already know that we have a constructor name; just consume
3321 // the token.
3322 ConsumeToken();
3323 } else {
3324 TPA.Revert();
3325 return false;
3326 }
3327
3328 // Current class name must be followed by a left parentheses.
3329 if (Tok.isNot(tok::l_paren)) {
3330 TPA.Revert();
3331 return false;
3332 }
3333 ConsumeParen();
3334
3335 // A right parentheses or ellipsis signals that we have a constructor.
3336 if (Tok.is(tok::r_paren) || Tok.is(tok::ellipsis)) {
3337 TPA.Revert();
3338 return true;
3339 }
3340
3341 // If we need to, enter the specified scope.
3342 DeclaratorScopeObj DeclScopeObj(*this, SS);
Douglas Gregor0be31a22010-07-02 17:43:08 +00003343 if (SS.isSet() && Actions.ShouldEnterDeclaratorScope(getCurScope(), SS))
Douglas Gregor9de54ea2010-01-13 17:31:36 +00003344 DeclScopeObj.EnterDeclaratorScope();
3345
Francois Pichet79f3a872011-01-31 04:54:32 +00003346 // Optionally skip Microsoft attributes.
John McCall084e83d2011-03-24 11:26:52 +00003347 ParsedAttributes Attrs(AttrFactory);
Francois Pichet79f3a872011-01-31 04:54:32 +00003348 MaybeParseMicrosoftAttributes(Attrs);
3349
Douglas Gregor9de54ea2010-01-13 17:31:36 +00003350 // Check whether the next token(s) are part of a declaration
3351 // specifier, in which case we have the start of a parameter and,
3352 // therefore, we know that this is a constructor.
3353 bool IsConstructor = isDeclarationSpecifier();
3354 TPA.Revert();
3355 return IsConstructor;
3356}
Chris Lattnerb9093cd2006-08-04 04:39:53 +00003357
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003358/// ParseTypeQualifierListOpt
Dawn Perchik335e16b2010-09-03 01:29:35 +00003359/// type-qualifier-list: [C99 6.7.5]
3360/// type-qualifier
3361/// [vendor] attributes
3362/// [ only if VendorAttributesAllowed=true ]
3363/// type-qualifier-list type-qualifier
3364/// [vendor] type-qualifier-list attributes
3365/// [ only if VendorAttributesAllowed=true ]
3366/// [C++0x] attribute-specifier[opt] is allowed before cv-qualifier-seq
3367/// [ only if CXX0XAttributesAllowed=true ]
3368/// Note: vendor can be GNU, MS, etc.
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003369///
Dawn Perchik335e16b2010-09-03 01:29:35 +00003370void Parser::ParseTypeQualifierListOpt(DeclSpec &DS,
3371 bool VendorAttributesAllowed,
Alexis Hunt96d5c762009-11-21 08:43:09 +00003372 bool CXX0XAttributesAllowed) {
3373 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier()) {
3374 SourceLocation Loc = Tok.getLocation();
John McCall084e83d2011-03-24 11:26:52 +00003375 ParsedAttributesWithRange attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00003376 ParseCXX0XAttributes(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +00003377 if (CXX0XAttributesAllowed)
John McCall53fa7142010-12-24 02:08:15 +00003378 DS.takeAttributesFrom(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +00003379 else
3380 Diag(Loc, diag::err_attributes_not_allowed);
3381 }
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +00003382
3383 SourceLocation EndLoc;
3384
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003385 while (1) {
John McCall49bfce42009-08-03 20:12:06 +00003386 bool isInvalid = false;
Chris Lattnerd9c3c592006-08-05 06:26:47 +00003387 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00003388 unsigned DiagID = 0;
Chris Lattner60809f52006-11-28 05:18:46 +00003389 SourceLocation Loc = Tok.getLocation();
Chris Lattnerd9c3c592006-08-05 06:26:47 +00003390
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003391 switch (Tok.getKind()) {
Douglas Gregor28c78432010-08-27 17:35:51 +00003392 case tok::code_completion:
3393 Actions.CodeCompleteTypeQualifiers(DS);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00003394 return cutOffParsing();
Douglas Gregor28c78432010-08-27 17:35:51 +00003395
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003396 case tok::kw_const:
John McCall49bfce42009-08-03 20:12:06 +00003397 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, DiagID,
3398 getLang());
Chris Lattnerd9c3c592006-08-05 06:26:47 +00003399 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003400 case tok::kw_volatile:
John McCall49bfce42009-08-03 20:12:06 +00003401 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
3402 getLang());
Chris Lattnerd9c3c592006-08-05 06:26:47 +00003403 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003404 case tok::kw_restrict:
John McCall49bfce42009-08-03 20:12:06 +00003405 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
3406 getLang());
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003407 break;
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003408
3409 // OpenCL qualifiers:
3410 case tok::kw_private:
3411 if (!getLang().OpenCL)
3412 goto DoneWithTypeQuals;
3413 case tok::kw___private:
3414 case tok::kw___global:
3415 case tok::kw___local:
3416 case tok::kw___constant:
3417 case tok::kw___read_only:
3418 case tok::kw___write_only:
3419 case tok::kw___read_write:
3420 ParseOpenCLQualifiers(DS);
3421 break;
3422
Eli Friedman53339e02009-06-08 23:27:34 +00003423 case tok::kw___w64:
Steve Narofff9c29d42008-12-25 14:41:26 +00003424 case tok::kw___ptr64:
Francois Pichetf2fb4112011-08-25 00:36:46 +00003425 case tok::kw___ptr32:
Steve Naroff44ac7772008-12-25 14:16:32 +00003426 case tok::kw___cdecl:
3427 case tok::kw___stdcall:
3428 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00003429 case tok::kw___thiscall:
Francois Pichet17ed0202011-08-18 09:59:55 +00003430 case tok::kw___unaligned:
Dawn Perchik335e16b2010-09-03 01:29:35 +00003431 if (VendorAttributesAllowed) {
John McCall53fa7142010-12-24 02:08:15 +00003432 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman53339e02009-06-08 23:27:34 +00003433 continue;
3434 }
3435 goto DoneWithTypeQuals;
Dawn Perchik335e16b2010-09-03 01:29:35 +00003436 case tok::kw___pascal:
3437 if (VendorAttributesAllowed) {
John McCall53fa7142010-12-24 02:08:15 +00003438 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik335e16b2010-09-03 01:29:35 +00003439 continue;
3440 }
3441 goto DoneWithTypeQuals;
Chris Lattnere37e2332006-08-15 04:50:22 +00003442 case tok::kw___attribute:
Dawn Perchik335e16b2010-09-03 01:29:35 +00003443 if (VendorAttributesAllowed) {
John McCall53fa7142010-12-24 02:08:15 +00003444 ParseGNUAttributes(DS.getAttributes());
Chris Lattnercf0bab22008-12-18 07:02:59 +00003445 continue; // do *not* consume the next token!
3446 }
3447 // otherwise, FALL THROUGH!
3448 default:
Steve Naroff44ac7772008-12-25 14:16:32 +00003449 DoneWithTypeQuals:
Chris Lattnercf0bab22008-12-18 07:02:59 +00003450 // If this is not a type-qualifier token, we're done reading type
3451 // qualifiers. First verify that DeclSpec's are consistent.
Douglas Gregore3e01a22009-04-01 22:41:11 +00003452 DS.Finish(Diags, PP);
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +00003453 if (EndLoc.isValid())
3454 DS.SetRangeEnd(EndLoc);
Chris Lattnercf0bab22008-12-18 07:02:59 +00003455 return;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003456 }
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00003457
Chris Lattnerd9c3c592006-08-05 06:26:47 +00003458 // If the specifier combination wasn't legal, issue a diagnostic.
3459 if (isInvalid) {
3460 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner6d29c102008-11-18 07:48:38 +00003461 Diag(Tok, DiagID) << PrevSpec;
Chris Lattnerd9c3c592006-08-05 06:26:47 +00003462 }
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +00003463 EndLoc = ConsumeToken();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003464 }
3465}
3466
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00003467
3468/// ParseDeclarator - Parse and verify a newly-initialized declarator.
3469///
3470void Parser::ParseDeclarator(Declarator &D) {
3471 /// This implements the 'declarator' production in the C grammar, then checks
3472 /// for well-formedness and issues diagnostics.
Sebastian Redlbd150f42008-11-21 19:14:01 +00003473 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00003474}
3475
Sebastian Redlbd150f42008-11-21 19:14:01 +00003476/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
3477/// is parsed by the function passed to it. Pass null, and the direct-declarator
3478/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregordbc5daf2008-11-07 20:08:42 +00003479/// ptr-operator production.
3480///
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00003481/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
3482/// [C] pointer[opt] direct-declarator
3483/// [C++] direct-declarator
3484/// [C++] ptr-operator declarator
Chris Lattner6c7416c2006-08-07 00:19:33 +00003485///
3486/// pointer: [C99 6.7.5]
3487/// '*' type-qualifier-list[opt]
3488/// '*' type-qualifier-list[opt] pointer
3489///
Douglas Gregordbc5daf2008-11-07 20:08:42 +00003490/// ptr-operator:
3491/// '*' cv-qualifier-seq[opt]
3492/// '&'
Sebastian Redled0f3b02009-03-15 22:02:01 +00003493/// [C++0x] '&&'
Douglas Gregordbc5daf2008-11-07 20:08:42 +00003494/// [GNU] '&' restrict[opt] attributes[opt]
Sebastian Redled0f3b02009-03-15 22:02:01 +00003495/// [GNU?] '&&' restrict[opt] attributes[opt]
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00003496/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Sebastian Redlbd150f42008-11-21 19:14:01 +00003497void Parser::ParseDeclaratorInternal(Declarator &D,
3498 DirectDeclParseFunction DirectDeclParser) {
Douglas Gregor66a985d2009-08-26 14:27:30 +00003499 if (Diags.hasAllExtensionsSilenced())
3500 D.setExtension();
Douglas Gregorc49f5b22010-08-23 18:23:48 +00003501
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00003502 // C++ member pointers start with a '::' or a nested-name.
3503 // Member pointers get special handling, since there's no place for the
3504 // scope spec in the generic path below.
Chris Lattner803802d2009-03-24 17:04:48 +00003505 if (getLang().CPlusPlus &&
3506 (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) ||
3507 Tok.is(tok::annot_cxxscope))) {
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00003508 CXXScopeSpec SS;
John McCallba7bf592010-08-24 05:47:05 +00003509 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), true); // ignore fail
John McCall1f476a12010-02-26 08:45:28 +00003510
Jeffrey Yasskin4e150f82010-04-07 23:29:58 +00003511 if (SS.isNotEmpty()) {
Mike Stump11289f42009-09-09 15:08:12 +00003512 if (Tok.isNot(tok::star)) {
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00003513 // The scope spec really belongs to the direct-declarator.
3514 D.getCXXScopeSpec() = SS;
3515 if (DirectDeclParser)
3516 (this->*DirectDeclParser)(D);
3517 return;
3518 }
3519
3520 SourceLocation Loc = ConsumeToken();
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003521 D.SetRangeEnd(Loc);
John McCall084e83d2011-03-24 11:26:52 +00003522 DeclSpec DS(AttrFactory);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00003523 ParseTypeQualifierListOpt(DS);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003524 D.ExtendWithDeclSpec(DS);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00003525
3526 // Recurse to parse whatever is left.
3527 ParseDeclaratorInternal(D, DirectDeclParser);
3528
3529 // Sema will have to catch (syntactically invalid) pointers into global
3530 // scope. It has to catch pointers into namespace scope anyway.
3531 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
John McCall084e83d2011-03-24 11:26:52 +00003532 Loc),
3533 DS.getAttributes(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003534 /* Don't replace range end. */SourceLocation());
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00003535 return;
3536 }
3537 }
3538
3539 tok::TokenKind Kind = Tok.getKind();
Steve Naroffec33ed92008-08-27 16:04:49 +00003540 // Not a pointer, C++ reference, or block.
Chris Lattner9eac9312009-03-27 04:18:06 +00003541 if (Kind != tok::star && Kind != tok::caret &&
Chris Lattner803802d2009-03-24 17:04:48 +00003542 (Kind != tok::amp || !getLang().CPlusPlus) &&
Sebastian Redl3b27be62009-03-23 00:00:23 +00003543 // We parse rvalue refs in C++03, because otherwise the errors are scary.
Chris Lattner9eac9312009-03-27 04:18:06 +00003544 (Kind != tok::ampamp || !getLang().CPlusPlus)) {
Sebastian Redlbd150f42008-11-21 19:14:01 +00003545 if (DirectDeclParser)
3546 (this->*DirectDeclParser)(D);
Douglas Gregordbc5daf2008-11-07 20:08:42 +00003547 return;
3548 }
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00003549
Sebastian Redled0f3b02009-03-15 22:02:01 +00003550 // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference,
3551 // '&&' -> rvalue reference
Sebastian Redl3b27be62009-03-23 00:00:23 +00003552 SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&.
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003553 D.SetRangeEnd(Loc);
Bill Wendling3708c182007-05-27 10:15:43 +00003554
Chris Lattner9eac9312009-03-27 04:18:06 +00003555 if (Kind == tok::star || Kind == tok::caret) {
Chris Lattner788404f2008-02-21 01:32:26 +00003556 // Is a pointer.
John McCall084e83d2011-03-24 11:26:52 +00003557 DeclSpec DS(AttrFactory);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00003558
Bill Wendling3708c182007-05-27 10:15:43 +00003559 ParseTypeQualifierListOpt(DS);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003560 D.ExtendWithDeclSpec(DS);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00003561
Bill Wendling3708c182007-05-27 10:15:43 +00003562 // Recursively parse the declarator.
Sebastian Redlbd150f42008-11-21 19:14:01 +00003563 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroffec33ed92008-08-27 16:04:49 +00003564 if (Kind == tok::star)
3565 // Remember that we parsed a pointer type, and remember the type-quals.
3566 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
Chandler Carruthe71b378d2011-02-23 18:51:59 +00003567 DS.getConstSpecLoc(),
3568 DS.getVolatileSpecLoc(),
John McCall084e83d2011-03-24 11:26:52 +00003569 DS.getRestrictSpecLoc()),
3570 DS.getAttributes(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003571 SourceLocation());
Steve Naroffec33ed92008-08-27 16:04:49 +00003572 else
3573 // Remember that we parsed a Block type, and remember the type-quals.
Mike Stump11289f42009-09-09 15:08:12 +00003574 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
John McCall084e83d2011-03-24 11:26:52 +00003575 Loc),
3576 DS.getAttributes(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003577 SourceLocation());
Bill Wendling3708c182007-05-27 10:15:43 +00003578 } else {
3579 // Is a reference
John McCall084e83d2011-03-24 11:26:52 +00003580 DeclSpec DS(AttrFactory);
Bill Wendling93efb222007-06-02 23:28:54 +00003581
Sebastian Redl3b27be62009-03-23 00:00:23 +00003582 // Complain about rvalue references in C++03, but then go on and build
3583 // the declarator.
3584 if (Kind == tok::ampamp && !getLang().CPlusPlus0x)
Douglas Gregor00984992011-01-25 02:17:32 +00003585 Diag(Loc, diag::ext_rvalue_reference);
Sebastian Redl3b27be62009-03-23 00:00:23 +00003586
Bill Wendling93efb222007-06-02 23:28:54 +00003587 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
3588 // cv-qualifiers are introduced through the use of a typedef or of a
3589 // template type argument, in which case the cv-qualifiers are ignored.
3590 //
3591 // [GNU] Retricted references are allowed.
3592 // [GNU] Attributes on references are allowed.
Alexis Hunt96d5c762009-11-21 08:43:09 +00003593 // [C++0x] Attributes on references are not allowed.
3594 ParseTypeQualifierListOpt(DS, true, false);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003595 D.ExtendWithDeclSpec(DS);
Bill Wendling93efb222007-06-02 23:28:54 +00003596
3597 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
3598 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
3599 Diag(DS.getConstSpecLoc(),
Chris Lattner6d29c102008-11-18 07:48:38 +00003600 diag::err_invalid_reference_qualifier_application) << "const";
Bill Wendling93efb222007-06-02 23:28:54 +00003601 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
3602 Diag(DS.getVolatileSpecLoc(),
Chris Lattner6d29c102008-11-18 07:48:38 +00003603 diag::err_invalid_reference_qualifier_application) << "volatile";
Bill Wendling93efb222007-06-02 23:28:54 +00003604 }
Bill Wendling3708c182007-05-27 10:15:43 +00003605
3606 // Recursively parse the declarator.
Sebastian Redlbd150f42008-11-21 19:14:01 +00003607 ParseDeclaratorInternal(D, DirectDeclParser);
Bill Wendling3708c182007-05-27 10:15:43 +00003608
Douglas Gregor66583c52008-11-03 15:51:28 +00003609 if (D.getNumTypeObjects() > 0) {
3610 // C++ [dcl.ref]p4: There shall be no references to references.
3611 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
3612 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattnerebad6a22008-11-19 07:37:42 +00003613 if (const IdentifierInfo *II = D.getIdentifier())
3614 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
3615 << II;
3616 else
3617 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
3618 << "type name";
Douglas Gregor66583c52008-11-03 15:51:28 +00003619
Sebastian Redlbd150f42008-11-21 19:14:01 +00003620 // Once we've complained about the reference-to-reference, we
Douglas Gregor66583c52008-11-03 15:51:28 +00003621 // can go ahead and build the (technically ill-formed)
3622 // declarator: reference collapsing will take care of it.
3623 }
3624 }
3625
Bill Wendling3708c182007-05-27 10:15:43 +00003626 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner788404f2008-02-21 01:32:26 +00003627 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
Sebastian Redled0f3b02009-03-15 22:02:01 +00003628 Kind == tok::amp),
John McCall084e83d2011-03-24 11:26:52 +00003629 DS.getAttributes(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003630 SourceLocation());
Bill Wendling3708c182007-05-27 10:15:43 +00003631 }
Chris Lattner6c7416c2006-08-07 00:19:33 +00003632}
3633
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003634/// ParseDirectDeclarator
3635/// direct-declarator: [C99 6.7.5]
Douglas Gregor831c93f2008-11-05 20:51:48 +00003636/// [C99] identifier
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003637/// '(' declarator ')'
3638/// [GNU] '(' attributes declarator ')'
Chris Lattnere8074e62006-08-06 18:30:15 +00003639/// [C90] direct-declarator '[' constant-expression[opt] ']'
3640/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
3641/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
3642/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
3643/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003644/// direct-declarator '(' parameter-type-list ')'
3645/// direct-declarator '(' identifier-list[opt] ')'
3646/// [GNU] direct-declarator '(' parameter-forward-declarations
3647/// parameter-type-list[opt] ')'
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003648/// [C++] direct-declarator '(' parameter-declaration-clause ')'
3649/// cv-qualifier-seq[opt] exception-specification[opt]
Douglas Gregor61956c42008-10-31 09:07:45 +00003650/// [C++] declarator-id
Douglas Gregor831c93f2008-11-05 20:51:48 +00003651///
3652/// declarator-id: [C++ 8]
Douglas Gregor27b4c162010-12-23 22:44:42 +00003653/// '...'[opt] id-expression
Douglas Gregor831c93f2008-11-05 20:51:48 +00003654/// '::'[opt] nested-name-specifier[opt] type-name
3655///
3656/// id-expression: [C++ 5.1]
3657/// unqualified-id
Douglas Gregord90fd522009-09-25 21:45:23 +00003658/// qualified-id
Douglas Gregor831c93f2008-11-05 20:51:48 +00003659///
3660/// unqualified-id: [C++ 5.1]
Mike Stump11289f42009-09-09 15:08:12 +00003661/// identifier
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003662/// operator-function-id
Douglas Gregord90fd522009-09-25 21:45:23 +00003663/// conversion-function-id
Mike Stump11289f42009-09-09 15:08:12 +00003664/// '~' class-name
Douglas Gregor7f741122009-02-25 19:37:18 +00003665/// template-id
Argyrios Kyrtzidise4426352008-11-07 22:02:30 +00003666///
Chris Lattneracd58a32006-08-06 17:24:14 +00003667void Parser::ParseDirectDeclarator(Declarator &D) {
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00003668 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003669
Douglas Gregor7861a802009-11-03 01:35:08 +00003670 if (getLang().CPlusPlus && D.mayHaveIdentifier()) {
3671 // ParseDeclaratorInternal might already have parsed the scope.
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00003672 if (D.getCXXScopeSpec().isEmpty()) {
John McCallba7bf592010-08-24 05:47:05 +00003673 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec(), ParsedType(), true);
John McCall1f476a12010-02-26 08:45:28 +00003674 }
3675
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00003676 if (D.getCXXScopeSpec().isValid()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00003677 if (Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
John McCall2b058ef2009-12-11 20:04:54 +00003678 // Change the declaration context for name lookup, until this function
3679 // is exited (and the declarator has been parsed).
3680 DeclScopeObj.EnterDeclaratorScope();
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00003681 }
3682
Douglas Gregor27b4c162010-12-23 22:44:42 +00003683 // C++0x [dcl.fct]p14:
3684 // There is a syntactic ambiguity when an ellipsis occurs at the end
3685 // of a parameter-declaration-clause without a preceding comma. In
3686 // this case, the ellipsis is parsed as part of the
3687 // abstract-declarator if the type of the parameter names a template
3688 // parameter pack that has not been expanded; otherwise, it is parsed
3689 // as part of the parameter-declaration-clause.
3690 if (Tok.is(tok::ellipsis) &&
3691 !((D.getContext() == Declarator::PrototypeContext ||
3692 D.getContext() == Declarator::BlockLiteralContext) &&
Douglas Gregor27b4c162010-12-23 22:44:42 +00003693 NextToken().is(tok::r_paren) &&
3694 !Actions.containsUnexpandedParameterPacks(D)))
3695 D.setEllipsisLoc(ConsumeToken());
3696
Douglas Gregor7861a802009-11-03 01:35:08 +00003697 if (Tok.is(tok::identifier) || Tok.is(tok::kw_operator) ||
3698 Tok.is(tok::annot_template_id) || Tok.is(tok::tilde)) {
3699 // We found something that indicates the start of an unqualified-id.
3700 // Parse that unqualified-id.
John McCall84821e72010-04-13 06:39:49 +00003701 bool AllowConstructorName;
3702 if (D.getDeclSpec().hasTypeSpecifier())
3703 AllowConstructorName = false;
3704 else if (D.getCXXScopeSpec().isSet())
3705 AllowConstructorName =
3706 (D.getContext() == Declarator::FileContext ||
3707 (D.getContext() == Declarator::MemberContext &&
3708 D.getDeclSpec().isFriendSpecified()));
3709 else
3710 AllowConstructorName = (D.getContext() == Declarator::MemberContext);
3711
Douglas Gregor7861a802009-11-03 01:35:08 +00003712 if (ParseUnqualifiedId(D.getCXXScopeSpec(),
3713 /*EnteringContext=*/true,
3714 /*AllowDestructorName=*/true,
Douglas Gregor9de54ea2010-01-13 17:31:36 +00003715 AllowConstructorName,
John McCallba7bf592010-08-24 05:47:05 +00003716 ParsedType(),
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00003717 D.getName()) ||
3718 // Once we're past the identifier, if the scope was bad, mark the
3719 // whole declarator bad.
3720 D.getCXXScopeSpec().isInvalid()) {
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00003721 D.SetIdentifier(0, Tok.getLocation());
3722 D.setInvalidType(true);
Douglas Gregor7861a802009-11-03 01:35:08 +00003723 } else {
3724 // Parsed the unqualified-id; update range information and move along.
3725 if (D.getSourceRange().getBegin().isInvalid())
3726 D.SetRangeBegin(D.getName().getSourceRange().getBegin());
3727 D.SetRangeEnd(D.getName().getSourceRange().getEnd());
Douglas Gregordbc5daf2008-11-07 20:08:42 +00003728 }
Douglas Gregor7861a802009-11-03 01:35:08 +00003729 goto PastIdentifier;
Douglas Gregor11d0c4c2008-11-06 22:13:31 +00003730 }
Douglas Gregor7861a802009-11-03 01:35:08 +00003731 } else if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00003732 assert(!getLang().CPlusPlus &&
3733 "There's a C++-specific check for tok::identifier above");
3734 assert(Tok.getIdentifierInfo() && "Not an identifier?");
3735 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
3736 ConsumeToken();
Douglas Gregor7861a802009-11-03 01:35:08 +00003737 goto PastIdentifier;
3738 }
3739
3740 if (Tok.is(tok::l_paren)) {
Chris Lattneracd58a32006-08-06 17:24:14 +00003741 // direct-declarator: '(' declarator ')'
Chris Lattnere37e2332006-08-15 04:50:22 +00003742 // direct-declarator: '(' attributes declarator ')'
Chris Lattneracd58a32006-08-06 17:24:14 +00003743 // Example: 'char (*X)' or 'int (*XX)(void)'
3744 ParseParenDeclarator(D);
Douglas Gregor9de54ea2010-01-13 17:31:36 +00003745
3746 // If the declarator was parenthesized, we entered the declarator
3747 // scope when parsing the parenthesized declarator, then exited
3748 // the scope already. Re-enter the scope, if we need to.
3749 if (D.getCXXScopeSpec().isSet()) {
Fariborz Jahanian358acd52010-08-17 23:50:37 +00003750 // If there was an error parsing parenthesized declarator, declarator
3751 // scope may have been enterred before. Don't do it again.
3752 if (!D.isInvalidType() &&
3753 Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
Douglas Gregor9de54ea2010-01-13 17:31:36 +00003754 // Change the declaration context for name lookup, until this function
3755 // is exited (and the declarator has been parsed).
Fariborz Jahanian358acd52010-08-17 23:50:37 +00003756 DeclScopeObj.EnterDeclaratorScope();
Douglas Gregor9de54ea2010-01-13 17:31:36 +00003757 }
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00003758 } else if (D.mayOmitIdentifier()) {
Chris Lattneracd58a32006-08-06 17:24:14 +00003759 // This could be something simple like "int" (in which case the declarator
3760 // portion is empty), if an abstract-declarator is allowed.
3761 D.SetIdentifier(0, Tok.getLocation());
3762 } else {
Douglas Gregord9f92e22009-03-06 23:28:18 +00003763 if (D.getContext() == Declarator::MemberContext)
3764 Diag(Tok, diag::err_expected_member_name_or_semi)
3765 << D.getDeclSpec().getSourceRange();
3766 else if (getLang().CPlusPlus)
Douglas Gregor30d60cb2009-11-03 19:44:04 +00003767 Diag(Tok, diag::err_expected_unqualified_id) << getLang().CPlusPlus;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003768 else
Chris Lattner6d29c102008-11-18 07:48:38 +00003769 Diag(Tok, diag::err_expected_ident_lparen);
Chris Lattnereec40f92006-08-06 21:55:29 +00003770 D.SetIdentifier(0, Tok.getLocation());
Chris Lattner8c5dd732008-11-11 06:13:16 +00003771 D.setInvalidType(true);
Chris Lattneracd58a32006-08-06 17:24:14 +00003772 }
Mike Stump11289f42009-09-09 15:08:12 +00003773
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00003774 PastIdentifier:
Chris Lattneracd58a32006-08-06 17:24:14 +00003775 assert(D.isPastIdentifier() &&
3776 "Haven't past the location of the identifier yet?");
Mike Stump11289f42009-09-09 15:08:12 +00003777
Alexis Hunt96d5c762009-11-21 08:43:09 +00003778 // Don't parse attributes unless we have an identifier.
John McCall53fa7142010-12-24 02:08:15 +00003779 if (D.getIdentifier())
3780 MaybeParseCXX0XAttributes(D);
Alexis Hunt96d5c762009-11-21 08:43:09 +00003781
Chris Lattneracd58a32006-08-06 17:24:14 +00003782 while (1) {
Chris Lattner76c72282007-10-09 17:33:22 +00003783 if (Tok.is(tok::l_paren)) {
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00003784 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
3785 // In such a case, check if we actually have a function declarator; if it
3786 // is not, the declarator has been fully parsed.
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003787 if (getLang().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
3788 // When not in file scope, warn for ambiguous function declarators, just
3789 // in case the author intended it as a variable definition.
3790 bool warnIfAmbiguous = D.getContext() != Declarator::FileContext;
3791 if (!isCXXFunctionDeclarator(warnIfAmbiguous))
3792 break;
3793 }
John McCall084e83d2011-03-24 11:26:52 +00003794 ParsedAttributes attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00003795 ParseFunctionDeclarator(ConsumeParen(), D, attrs);
Chris Lattner76c72282007-10-09 17:33:22 +00003796 } else if (Tok.is(tok::l_square)) {
Chris Lattnere8074e62006-08-06 18:30:15 +00003797 ParseBracketDeclarator(D);
Chris Lattneracd58a32006-08-06 17:24:14 +00003798 } else {
3799 break;
3800 }
3801 }
3802}
3803
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003804/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
3805/// only called before the identifier, so these are most likely just grouping
Mike Stump11289f42009-09-09 15:08:12 +00003806/// parens for precedence. If we find that these are actually function
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003807/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
3808///
3809/// direct-declarator:
3810/// '(' declarator ')'
3811/// [GNU] '(' attributes declarator ')'
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003812/// direct-declarator '(' parameter-type-list ')'
3813/// direct-declarator '(' identifier-list[opt] ')'
3814/// [GNU] direct-declarator '(' parameter-forward-declarations
3815/// parameter-type-list[opt] ')'
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003816///
3817void Parser::ParseParenDeclarator(Declarator &D) {
3818 SourceLocation StartLoc = ConsumeParen();
3819 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
Mike Stump11289f42009-09-09 15:08:12 +00003820
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003821 // Eat any attributes before we look at whether this is a grouping or function
3822 // declarator paren. If this is a grouping paren, the attribute applies to
3823 // the type being built up, for example:
3824 // int (__attribute__(()) *x)(long y)
3825 // If this ends up not being a grouping paren, the attribute applies to the
3826 // first argument, for example:
3827 // int (__attribute__(()) int x)
3828 // In either case, we need to eat any attributes to be able to determine what
3829 // sort of paren this is.
3830 //
John McCall084e83d2011-03-24 11:26:52 +00003831 ParsedAttributes attrs(AttrFactory);
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003832 bool RequiresArg = false;
3833 if (Tok.is(tok::kw___attribute)) {
John McCall53fa7142010-12-24 02:08:15 +00003834 ParseGNUAttributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +00003835
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003836 // We require that the argument list (if this is a non-grouping paren) be
3837 // present even if the attribute list was empty.
3838 RequiresArg = true;
3839 }
Steve Naroff44ac7772008-12-25 14:16:32 +00003840 // Eat any Microsoft extensions.
Eli Friedman53339e02009-06-08 23:27:34 +00003841 if (Tok.is(tok::kw___cdecl) || Tok.is(tok::kw___stdcall) ||
Douglas Gregora941dca2010-05-18 16:57:00 +00003842 Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___fastcall) ||
Francois Pichet17ed0202011-08-18 09:59:55 +00003843 Tok.is(tok::kw___w64) || Tok.is(tok::kw___ptr64) ||
Francois Pichetf2fb4112011-08-25 00:36:46 +00003844 Tok.is(tok::kw___ptr32) || Tok.is(tok::kw___unaligned)) {
John McCall53fa7142010-12-24 02:08:15 +00003845 ParseMicrosoftTypeAttributes(attrs);
Eli Friedman53339e02009-06-08 23:27:34 +00003846 }
Dawn Perchik335e16b2010-09-03 01:29:35 +00003847 // Eat any Borland extensions.
Ted Kremenek5eec2b02010-11-10 05:59:39 +00003848 if (Tok.is(tok::kw___pascal))
John McCall53fa7142010-12-24 02:08:15 +00003849 ParseBorlandTypeAttributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +00003850
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003851 // If we haven't past the identifier yet (or where the identifier would be
3852 // stored, if this is an abstract declarator), then this is probably just
3853 // grouping parens. However, if this could be an abstract-declarator, then
3854 // this could also be the start of function arguments (consider 'void()').
3855 bool isGrouping;
Mike Stump11289f42009-09-09 15:08:12 +00003856
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003857 if (!D.mayOmitIdentifier()) {
3858 // If this can't be an abstract-declarator, this *must* be a grouping
3859 // paren, because we haven't seen the identifier yet.
3860 isGrouping = true;
3861 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Argyrios Kyrtzidise8addf52008-10-06 00:07:55 +00003862 (getLang().CPlusPlus && Tok.is(tok::ellipsis)) || // C++ int(...)
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003863 isDeclarationSpecifier()) { // 'int(int)' is a function.
3864 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
3865 // considered to be a type, not a K&R identifier-list.
3866 isGrouping = false;
3867 } else {
3868 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
3869 isGrouping = true;
3870 }
Mike Stump11289f42009-09-09 15:08:12 +00003871
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003872 // If this is a grouping paren, handle:
3873 // direct-declarator: '(' declarator ')'
3874 // direct-declarator: '(' attributes declarator ')'
3875 if (isGrouping) {
Argyrios Kyrtzidis8ae36842008-10-07 10:21:57 +00003876 bool hadGroupingParens = D.hasGroupingParens();
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00003877 D.setGroupingParens(true);
3878
Sebastian Redlbd150f42008-11-21 19:14:01 +00003879 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003880 // Match the ')'.
Abramo Bagnara924a8f32010-12-10 16:29:40 +00003881 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_paren, StartLoc);
John McCall084e83d2011-03-24 11:26:52 +00003882 D.AddTypeInfo(DeclaratorChunk::getParen(StartLoc, EndLoc),
3883 attrs, EndLoc);
Argyrios Kyrtzidis8ae36842008-10-07 10:21:57 +00003884
3885 D.setGroupingParens(hadGroupingParens);
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003886 return;
3887 }
Mike Stump11289f42009-09-09 15:08:12 +00003888
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003889 // Okay, if this wasn't a grouping paren, it must be the start of a function
3890 // argument list. Recognize that this declarator will never have an
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003891 // identifier (and remember where it would have been), then call into
3892 // ParseFunctionDeclarator to handle of argument list.
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003893 D.SetIdentifier(0, Tok.getLocation());
3894
John McCall53fa7142010-12-24 02:08:15 +00003895 ParseFunctionDeclarator(StartLoc, D, attrs, RequiresArg);
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003896}
3897
3898/// ParseFunctionDeclarator - We are after the identifier and have parsed the
3899/// declarator D up to a paren, which indicates that we are parsing function
3900/// arguments.
Chris Lattneracd58a32006-08-06 17:24:14 +00003901///
Douglas Gregor9e66af42011-07-05 16:44:18 +00003902/// If attrs is non-null, then the caller parsed those arguments immediately
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003903/// after the open paren - they should be considered to be the first argument of
3904/// a parameter. If RequiresArg is true, then the first argument of the
3905/// function is required to be present and required to not be an identifier
3906/// list.
3907///
Douglas Gregor9e66af42011-07-05 16:44:18 +00003908/// For C++, after the parameter-list, it also parses cv-qualifier-seq[opt],
3909/// (C++0x) ref-qualifier[opt], exception-specification[opt], and
3910/// (C++0x) trailing-return-type[opt].
3911///
3912/// [C++0x] exception-specification:
3913/// dynamic-exception-specification
3914/// noexcept-specification
3915///
3916void Parser::ParseFunctionDeclarator(SourceLocation LParenLoc, Declarator &D,
3917 ParsedAttributes &attrs,
3918 bool RequiresArg) {
3919 // lparen is already consumed!
3920 assert(D.isPastIdentifier() && "Should not call before identifier!");
3921
3922 // This should be true when the function has typed arguments.
3923 // Otherwise, it is treated as a K&R-style function.
3924 bool HasProto = false;
3925 // Build up an array of information about the parsed arguments.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003926 SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Douglas Gregor9e66af42011-07-05 16:44:18 +00003927 // Remember where we see an ellipsis, if any.
3928 SourceLocation EllipsisLoc;
3929
3930 DeclSpec DS(AttrFactory);
3931 bool RefQualifierIsLValueRef = true;
3932 SourceLocation RefQualifierLoc;
3933 ExceptionSpecificationType ESpecType = EST_None;
3934 SourceRange ESpecRange;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003935 SmallVector<ParsedType, 2> DynamicExceptions;
3936 SmallVector<SourceRange, 2> DynamicExceptionRanges;
Douglas Gregor9e66af42011-07-05 16:44:18 +00003937 ExprResult NoexceptExpr;
3938 ParsedType TrailingReturnType;
3939
3940 SourceLocation EndLoc;
3941
3942 if (isFunctionDeclaratorIdentifierList()) {
3943 if (RequiresArg)
3944 Diag(Tok, diag::err_argument_required_after_attribute);
3945
3946 ParseFunctionDeclaratorIdentifierList(D, ParamInfo);
3947
3948 EndLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
3949 } else {
3950 // Enter function-declaration scope, limiting any declarators to the
3951 // function prototype scope, including parameter declarators.
3952 ParseScope PrototypeScope(this,
3953 Scope::FunctionPrototypeScope|Scope::DeclScope);
3954
3955 if (Tok.isNot(tok::r_paren))
3956 ParseParameterDeclarationClause(D, attrs, ParamInfo, EllipsisLoc);
3957 else if (RequiresArg)
3958 Diag(Tok, diag::err_argument_required_after_attribute);
3959
3960 HasProto = ParamInfo.size() || getLang().CPlusPlus;
3961
3962 // If we have the closing ')', eat it.
3963 EndLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
3964
3965 if (getLang().CPlusPlus) {
3966 MaybeParseCXX0XAttributes(attrs);
3967
3968 // Parse cv-qualifier-seq[opt].
3969 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
3970 if (!DS.getSourceRange().getEnd().isInvalid())
3971 EndLoc = DS.getSourceRange().getEnd();
3972
3973 // Parse ref-qualifier[opt].
3974 if (Tok.is(tok::amp) || Tok.is(tok::ampamp)) {
3975 if (!getLang().CPlusPlus0x)
3976 Diag(Tok, diag::ext_ref_qualifier);
3977
3978 RefQualifierIsLValueRef = Tok.is(tok::amp);
3979 RefQualifierLoc = ConsumeToken();
3980 EndLoc = RefQualifierLoc;
3981 }
3982
3983 // Parse exception-specification[opt].
3984 ESpecType = MaybeParseExceptionSpecification(ESpecRange,
3985 DynamicExceptions,
3986 DynamicExceptionRanges,
3987 NoexceptExpr);
3988 if (ESpecType != EST_None)
3989 EndLoc = ESpecRange.getEnd();
3990
3991 // Parse trailing-return-type[opt].
3992 if (getLang().CPlusPlus0x && Tok.is(tok::arrow)) {
Douglas Gregordb0b9f12011-08-04 15:30:47 +00003993 SourceRange Range;
3994 TrailingReturnType = ParseTrailingReturnType(Range).get();
3995 if (Range.getEnd().isValid())
3996 EndLoc = Range.getEnd();
Douglas Gregor9e66af42011-07-05 16:44:18 +00003997 }
3998 }
3999
4000 // Leave prototype scope.
4001 PrototypeScope.Exit();
4002 }
4003
4004 // Remember that we parsed a function type, and remember the attributes.
4005 D.AddTypeInfo(DeclaratorChunk::getFunction(HasProto,
4006 /*isVariadic=*/EllipsisLoc.isValid(),
4007 EllipsisLoc,
4008 ParamInfo.data(), ParamInfo.size(),
4009 DS.getTypeQualifiers(),
4010 RefQualifierIsLValueRef,
4011 RefQualifierLoc,
Douglas Gregorad69e652011-07-13 21:47:47 +00004012 /*MutableLoc=*/SourceLocation(),
Douglas Gregor9e66af42011-07-05 16:44:18 +00004013 ESpecType, ESpecRange.getBegin(),
4014 DynamicExceptions.data(),
4015 DynamicExceptionRanges.data(),
4016 DynamicExceptions.size(),
4017 NoexceptExpr.isUsable() ?
4018 NoexceptExpr.get() : 0,
4019 LParenLoc, EndLoc, D,
4020 TrailingReturnType),
4021 attrs, EndLoc);
4022}
4023
4024/// isFunctionDeclaratorIdentifierList - This parameter list may have an
4025/// identifier list form for a K&R-style function: void foo(a,b,c)
4026///
4027/// Note that identifier-lists are only allowed for normal declarators, not for
4028/// abstract-declarators.
4029bool Parser::isFunctionDeclaratorIdentifierList() {
4030 return !getLang().CPlusPlus
4031 && Tok.is(tok::identifier)
4032 && !TryAltiVecVectorToken()
4033 // K&R identifier lists can't have typedefs as identifiers, per C99
4034 // 6.7.5.3p11.
4035 && (TryAnnotateTypeOrScopeToken() || !Tok.is(tok::annot_typename))
4036 // Identifier lists follow a really simple grammar: the identifiers can
4037 // be followed *only* by a ", identifier" or ")". However, K&R
4038 // identifier lists are really rare in the brave new modern world, and
4039 // it is very common for someone to typo a type in a non-K&R style
4040 // list. If we are presented with something like: "void foo(intptr x,
4041 // float y)", we don't want to start parsing the function declarator as
4042 // though it is a K&R style declarator just because intptr is an
4043 // invalid type.
4044 //
4045 // To handle this, we check to see if the token after the first
4046 // identifier is a "," or ")". Only then do we parse it as an
4047 // identifier list.
4048 && (NextToken().is(tok::comma) || NextToken().is(tok::r_paren));
4049}
4050
4051/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
4052/// we found a K&R-style identifier list instead of a typed parameter list.
4053///
4054/// After returning, ParamInfo will hold the parsed parameters.
4055///
4056/// identifier-list: [C99 6.7.5]
4057/// identifier
4058/// identifier-list ',' identifier
4059///
4060void Parser::ParseFunctionDeclaratorIdentifierList(
4061 Declarator &D,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004062 SmallVector<DeclaratorChunk::ParamInfo, 16> &ParamInfo) {
Douglas Gregor9e66af42011-07-05 16:44:18 +00004063 // If there was no identifier specified for the declarator, either we are in
4064 // an abstract-declarator, or we are in a parameter declarator which was found
4065 // to be abstract. In abstract-declarators, identifier lists are not valid:
4066 // diagnose this.
4067 if (!D.getIdentifier())
4068 Diag(Tok, diag::ext_ident_list_in_param);
4069
4070 // Maintain an efficient lookup of params we have seen so far.
4071 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
4072
4073 while (1) {
4074 // If this isn't an identifier, report the error and skip until ')'.
4075 if (Tok.isNot(tok::identifier)) {
4076 Diag(Tok, diag::err_expected_ident);
4077 SkipUntil(tok::r_paren, /*StopAtSemi=*/true, /*DontConsume=*/true);
4078 // Forget we parsed anything.
4079 ParamInfo.clear();
4080 return;
4081 }
4082
4083 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
4084
4085 // Reject 'typedef int y; int test(x, y)', but continue parsing.
4086 if (Actions.getTypeName(*ParmII, Tok.getLocation(), getCurScope()))
4087 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
4088
4089 // Verify that the argument identifier has not already been mentioned.
4090 if (!ParamsSoFar.insert(ParmII)) {
4091 Diag(Tok, diag::err_param_redefinition) << ParmII;
4092 } else {
4093 // Remember this identifier in ParamInfo.
4094 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
4095 Tok.getLocation(),
4096 0));
4097 }
4098
4099 // Eat the identifier.
4100 ConsumeToken();
4101
4102 // The list continues if we see a comma.
4103 if (Tok.isNot(tok::comma))
4104 break;
4105 ConsumeToken();
4106 }
4107}
4108
4109/// ParseParameterDeclarationClause - Parse a (possibly empty) parameter-list
4110/// after the opening parenthesis. This function will not parse a K&R-style
4111/// identifier list.
4112///
4113/// D is the declarator being parsed. If attrs is non-null, then the caller
4114/// parsed those arguments immediately after the open paren - they should be
4115/// considered to be the first argument of a parameter.
4116///
4117/// After returning, ParamInfo will hold the parsed parameters. EllipsisLoc will
4118/// be the location of the ellipsis, if any was parsed.
4119///
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004120/// parameter-type-list: [C99 6.7.5]
4121/// parameter-list
4122/// parameter-list ',' '...'
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00004123/// [C++] parameter-list '...'
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004124///
4125/// parameter-list: [C99 6.7.5]
4126/// parameter-declaration
4127/// parameter-list ',' parameter-declaration
4128///
4129/// parameter-declaration: [C99 6.7.5]
4130/// declaration-specifiers declarator
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00004131/// [C++] declaration-specifiers declarator '=' assignment-expression
Chris Lattnere37e2332006-08-15 04:50:22 +00004132/// [GNU] declaration-specifiers declarator attributes
Sebastian Redlf769df52009-03-24 22:27:57 +00004133/// declaration-specifiers abstract-declarator[opt]
4134/// [C++] declaration-specifiers abstract-declarator[opt]
Chris Lattner58258242008-04-10 02:22:51 +00004135/// '=' assignment-expression
Chris Lattnere37e2332006-08-15 04:50:22 +00004136/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004137///
Douglas Gregor9e66af42011-07-05 16:44:18 +00004138void Parser::ParseParameterDeclarationClause(
4139 Declarator &D,
4140 ParsedAttributes &attrs,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004141 SmallVector<DeclaratorChunk::ParamInfo, 16> &ParamInfo,
Douglas Gregor9e66af42011-07-05 16:44:18 +00004142 SourceLocation &EllipsisLoc) {
Mike Stump11289f42009-09-09 15:08:12 +00004143
Chris Lattner371ed4e2008-04-06 06:57:35 +00004144 while (1) {
4145 if (Tok.is(tok::ellipsis)) {
Douglas Gregor94349fd2009-02-18 07:07:28 +00004146 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chris Lattner371ed4e2008-04-06 06:57:35 +00004147 break;
Chris Lattneracd58a32006-08-06 17:24:14 +00004148 }
Mike Stump11289f42009-09-09 15:08:12 +00004149
Chris Lattner371ed4e2008-04-06 06:57:35 +00004150 // Parse the declaration-specifiers.
John McCall28a6aea2009-11-04 02:18:39 +00004151 // Just use the ParsingDeclaration "scope" of the declarator.
John McCall084e83d2011-03-24 11:26:52 +00004152 DeclSpec DS(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00004153
4154 // Skip any Microsoft attributes before a param.
Francois Pichet0706d202011-09-17 17:15:52 +00004155 if (getLang().MicrosoftExt && Tok.is(tok::l_square))
John McCall53fa7142010-12-24 02:08:15 +00004156 ParseMicrosoftAttributes(DS.getAttributes());
4157
4158 SourceLocation DSStart = Tok.getLocation();
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00004159
4160 // If the caller parsed attributes for the first argument, add them now.
John McCall53fa7142010-12-24 02:08:15 +00004161 // Take them so that we only apply the attributes to the first parameter.
Douglas Gregor9e66af42011-07-05 16:44:18 +00004162 // FIXME: If we saw an ellipsis first, this code is not reached. Are the
4163 // attributes lost? Should they even be allowed?
4164 // FIXME: If we can leave the attributes in the token stream somehow, we can
4165 // get rid of a parameter (attrs) and this statement. It might be too much
4166 // hassle.
John McCall53fa7142010-12-24 02:08:15 +00004167 DS.takeAttributesFrom(attrs);
4168
Chris Lattnerde39c3e2009-02-27 18:38:20 +00004169 ParseDeclarationSpecifiers(DS);
Mike Stump11289f42009-09-09 15:08:12 +00004170
Chris Lattner371ed4e2008-04-06 06:57:35 +00004171 // Parse the declarator. This is "PrototypeContext", because we must
4172 // accept either 'declarator' or 'abstract-declarator' here.
4173 Declarator ParmDecl(DS, Declarator::PrototypeContext);
4174 ParseDeclarator(ParmDecl);
4175
4176 // Parse GNU attributes, if present.
John McCall53fa7142010-12-24 02:08:15 +00004177 MaybeParseGNUAttributes(ParmDecl);
Mike Stump11289f42009-09-09 15:08:12 +00004178
Chris Lattner371ed4e2008-04-06 06:57:35 +00004179 // Remember this parsed parameter in ParamInfo.
4180 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
Mike Stump11289f42009-09-09 15:08:12 +00004181
Douglas Gregor4d87df52008-12-16 21:30:33 +00004182 // DefArgToks is used when the parsing of default arguments needs
4183 // to be delayed.
4184 CachedTokens *DefArgToks = 0;
4185
Chris Lattner371ed4e2008-04-06 06:57:35 +00004186 // If no parameter was specified, verify that *something* was specified,
4187 // otherwise we have a missing type and identifier.
Chris Lattnerde39c3e2009-02-27 18:38:20 +00004188 if (DS.isEmpty() && ParmDecl.getIdentifier() == 0 &&
4189 ParmDecl.getNumTypeObjects() == 0) {
Chris Lattner371ed4e2008-04-06 06:57:35 +00004190 // Completely missing, emit error.
4191 Diag(DSStart, diag::err_missing_param);
4192 } else {
4193 // Otherwise, we have something. Add it and let semantic analysis try
4194 // to grok it and add the result to the ParamInfo we are building.
Mike Stump11289f42009-09-09 15:08:12 +00004195
Chris Lattner371ed4e2008-04-06 06:57:35 +00004196 // Inform the actions module about the parameter declarator, so it gets
4197 // added to the current scope.
John McCall48871652010-08-21 09:40:31 +00004198 Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl);
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00004199
4200 // Parse the default argument, if any. We parse the default
4201 // arguments in all dialects; the semantic analysis in
4202 // ActOnParamDefaultArgument will reject the default argument in
4203 // C.
4204 if (Tok.is(tok::equal)) {
Douglas Gregor58354032008-12-24 00:01:03 +00004205 SourceLocation EqualLoc = Tok.getLocation();
4206
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00004207 // Parse the default argument
Douglas Gregor4d87df52008-12-16 21:30:33 +00004208 if (D.getContext() == Declarator::MemberContext) {
4209 // If we're inside a class definition, cache the tokens
4210 // corresponding to the default argument. We'll actually parse
4211 // them when we see the end of the class definition.
4212 // FIXME: Templates will require something similar.
4213 // FIXME: Can we use a smart pointer for Toks?
4214 DefArgToks = new CachedTokens;
4215
Mike Stump11289f42009-09-09 15:08:12 +00004216 if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks,
Argyrios Kyrtzidis8d7bdba2010-04-23 21:20:12 +00004217 /*StopAtSemi=*/true,
4218 /*ConsumeFinalToken=*/false)) {
Douglas Gregor4d87df52008-12-16 21:30:33 +00004219 delete DefArgToks;
4220 DefArgToks = 0;
Douglas Gregor58354032008-12-24 00:01:03 +00004221 Actions.ActOnParamDefaultArgumentError(Param);
Argyrios Kyrtzidis249179c2010-08-06 09:47:24 +00004222 } else {
4223 // Mark the end of the default argument so that we know when to
4224 // stop when we parse it later on.
4225 Token DefArgEnd;
4226 DefArgEnd.startToken();
4227 DefArgEnd.setKind(tok::cxx_defaultarg_end);
4228 DefArgEnd.setLocation(Tok.getLocation());
4229 DefArgToks->push_back(DefArgEnd);
Mike Stump11289f42009-09-09 15:08:12 +00004230 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc,
Anders Carlsson84613c42009-06-12 16:51:40 +00004231 (*DefArgToks)[1].getLocation());
Argyrios Kyrtzidis249179c2010-08-06 09:47:24 +00004232 }
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00004233 } else {
Douglas Gregor4d87df52008-12-16 21:30:33 +00004234 // Consume the '='.
Douglas Gregor58354032008-12-24 00:01:03 +00004235 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00004236
Douglas Gregor8a01b2a2010-09-11 20:24:53 +00004237 // The argument isn't actually potentially evaluated unless it is
4238 // used.
4239 EnterExpressionEvaluationContext Eval(Actions,
4240 Sema::PotentiallyEvaluatedIfUsed);
4241
John McCalldadc5752010-08-24 06:29:42 +00004242 ExprResult DefArgResult(ParseAssignmentExpression());
Douglas Gregor4d87df52008-12-16 21:30:33 +00004243 if (DefArgResult.isInvalid()) {
4244 Actions.ActOnParamDefaultArgumentError(Param);
4245 SkipUntil(tok::comma, tok::r_paren, true, true);
4246 } else {
4247 // Inform the actions module about the default argument
4248 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
John McCallb268a282010-08-23 23:25:46 +00004249 DefArgResult.take());
Douglas Gregor4d87df52008-12-16 21:30:33 +00004250 }
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00004251 }
4252 }
Mike Stump11289f42009-09-09 15:08:12 +00004253
4254 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
4255 ParmDecl.getIdentifierLoc(), Param,
Douglas Gregor4d87df52008-12-16 21:30:33 +00004256 DefArgToks));
Chris Lattner371ed4e2008-04-06 06:57:35 +00004257 }
4258
4259 // If the next token is a comma, consume it and keep reading arguments.
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00004260 if (Tok.isNot(tok::comma)) {
4261 if (Tok.is(tok::ellipsis)) {
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00004262 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
4263
4264 if (!getLang().CPlusPlus) {
4265 // We have ellipsis without a preceding ',', which is ill-formed
4266 // in C. Complain and provide the fix.
4267 Diag(EllipsisLoc, diag::err_missing_comma_before_ellipsis)
Douglas Gregora771f462010-03-31 17:46:05 +00004268 << FixItHint::CreateInsertion(EllipsisLoc, ", ");
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00004269 }
4270 }
4271
4272 break;
4273 }
Mike Stump11289f42009-09-09 15:08:12 +00004274
Chris Lattner371ed4e2008-04-06 06:57:35 +00004275 // Consume the comma.
4276 ConsumeToken();
Chris Lattneracd58a32006-08-06 17:24:14 +00004277 }
Mike Stump11289f42009-09-09 15:08:12 +00004278
Chris Lattner6c940e62008-04-06 06:34:08 +00004279}
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004280
Chris Lattnere8074e62006-08-06 18:30:15 +00004281/// [C90] direct-declarator '[' constant-expression[opt] ']'
4282/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
4283/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
4284/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
4285/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
4286void Parser::ParseBracketDeclarator(Declarator &D) {
Chris Lattner04132372006-10-16 06:12:55 +00004287 SourceLocation StartLoc = ConsumeBracket();
Mike Stump11289f42009-09-09 15:08:12 +00004288
Chris Lattner84a11622008-12-18 07:27:21 +00004289 // C array syntax has many features, but by-far the most common is [] and [4].
4290 // This code does a fast path to handle some of the most obvious cases.
4291 if (Tok.getKind() == tok::r_square) {
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004292 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
John McCall084e83d2011-03-24 11:26:52 +00004293 ParsedAttributes attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00004294 MaybeParseCXX0XAttributes(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +00004295
Chris Lattner84a11622008-12-18 07:27:21 +00004296 // Remember that we parsed the empty array type.
John McCalldadc5752010-08-24 06:29:42 +00004297 ExprResult NumElements;
John McCall084e83d2011-03-24 11:26:52 +00004298 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0,
Douglas Gregor04318252009-07-06 15:59:29 +00004299 StartLoc, EndLoc),
John McCall084e83d2011-03-24 11:26:52 +00004300 attrs, EndLoc);
Chris Lattner84a11622008-12-18 07:27:21 +00004301 return;
4302 } else if (Tok.getKind() == tok::numeric_constant &&
4303 GetLookAheadToken(1).is(tok::r_square)) {
4304 // [4] is very common. Parse the numeric constant expression.
John McCalldadc5752010-08-24 06:29:42 +00004305 ExprResult ExprRes(Actions.ActOnNumericConstant(Tok));
Chris Lattner84a11622008-12-18 07:27:21 +00004306 ConsumeToken();
4307
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004308 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
John McCall084e83d2011-03-24 11:26:52 +00004309 ParsedAttributes attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00004310 MaybeParseCXX0XAttributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +00004311
Chris Lattner84a11622008-12-18 07:27:21 +00004312 // Remember that we parsed a array type, and remember its features.
John McCall084e83d2011-03-24 11:26:52 +00004313 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, 0,
John McCall53fa7142010-12-24 02:08:15 +00004314 ExprRes.release(),
Douglas Gregor04318252009-07-06 15:59:29 +00004315 StartLoc, EndLoc),
John McCall084e83d2011-03-24 11:26:52 +00004316 attrs, EndLoc);
Chris Lattner84a11622008-12-18 07:27:21 +00004317 return;
4318 }
Mike Stump11289f42009-09-09 15:08:12 +00004319
Chris Lattnere8074e62006-08-06 18:30:15 +00004320 // If valid, this location is the position where we read the 'static' keyword.
4321 SourceLocation StaticLoc;
Chris Lattner76c72282007-10-09 17:33:22 +00004322 if (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 // If there is a type-qualifier-list, read it now.
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00004326 // Type qualifiers in an array subscript are a C99 feature.
John McCall084e83d2011-03-24 11:26:52 +00004327 DeclSpec DS(AttrFactory);
Chris Lattnercf0bab22008-12-18 07:02:59 +00004328 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Mike Stump11289f42009-09-09 15:08:12 +00004329
Chris Lattnere8074e62006-08-06 18:30:15 +00004330 // If we haven't already read 'static', check to see if there is one after the
4331 // type-qualifier-list.
Chris Lattner76c72282007-10-09 17:33:22 +00004332 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Chris Lattneraf635312006-10-16 06:06:51 +00004333 StaticLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00004334
Chris Lattnere8074e62006-08-06 18:30:15 +00004335 // Handle "direct-declarator [ type-qual-list[opt] * ]".
Chris Lattnere8074e62006-08-06 18:30:15 +00004336 bool isStar = false;
John McCalldadc5752010-08-24 06:29:42 +00004337 ExprResult NumElements;
Mike Stump11289f42009-09-09 15:08:12 +00004338
Chris Lattner521ff2b2008-04-06 05:26:30 +00004339 // Handle the case where we have '[*]' as the array size. However, a leading
4340 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
4341 // the the token after the star is a ']'. Since stars in arrays are
4342 // infrequent, use of lookahead is not costly here.
4343 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattnerc439f0d2008-04-06 05:27:21 +00004344 ConsumeToken(); // Eat the '*'.
Chris Lattner1906f802006-08-06 19:14:46 +00004345
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00004346 if (StaticLoc.isValid()) {
Chris Lattner521ff2b2008-04-06 05:26:30 +00004347 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00004348 StaticLoc = SourceLocation(); // Drop the static.
4349 }
Chris Lattner521ff2b2008-04-06 05:26:30 +00004350 isStar = true;
Chris Lattner76c72282007-10-09 17:33:22 +00004351 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner84a11622008-12-18 07:27:21 +00004352 // Note, in C89, this production uses the constant-expr production instead
4353 // of assignment-expr. The only difference is that assignment-expr allows
4354 // things like '=' and '*='. Sema rejects these in C89 mode because they
4355 // are not i-c-e's, so we don't need to distinguish between the two here.
Mike Stump11289f42009-09-09 15:08:12 +00004356
Douglas Gregorc9c02ed2009-06-19 23:52:42 +00004357 // Parse the constant-expression or assignment-expression now (depending
4358 // on dialect).
4359 if (getLang().CPlusPlus)
4360 NumElements = ParseConstantExpression();
4361 else
4362 NumElements = ParseAssignmentExpression();
Chris Lattner62591722006-08-12 18:40:58 +00004363 }
Mike Stump11289f42009-09-09 15:08:12 +00004364
Chris Lattner62591722006-08-12 18:40:58 +00004365 // If there was an error parsing the assignment-expression, recover.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00004366 if (NumElements.isInvalid()) {
Chris Lattnercd2a8c52009-04-24 22:30:50 +00004367 D.setInvalidType(true);
Chris Lattner62591722006-08-12 18:40:58 +00004368 // If the expression was invalid, skip it.
4369 SkipUntil(tok::r_square);
4370 return;
Chris Lattnere8074e62006-08-06 18:30:15 +00004371 }
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004372
4373 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
4374
John McCall084e83d2011-03-24 11:26:52 +00004375 ParsedAttributes attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00004376 MaybeParseCXX0XAttributes(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +00004377
Chris Lattner84a11622008-12-18 07:27:21 +00004378 // Remember that we parsed a array type, and remember its features.
John McCall084e83d2011-03-24 11:26:52 +00004379 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
Chris Lattnercbc426d2006-12-02 06:43:02 +00004380 StaticLoc.isValid(), isStar,
Douglas Gregor04318252009-07-06 15:59:29 +00004381 NumElements.release(),
4382 StartLoc, EndLoc),
John McCall084e83d2011-03-24 11:26:52 +00004383 attrs, EndLoc);
Chris Lattnere8074e62006-08-06 18:30:15 +00004384}
4385
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00004386/// [GNU] typeof-specifier:
4387/// typeof ( expressions )
4388/// typeof ( type-name )
4389/// [GNU/C++] typeof unary-expression
Steve Naroffad373bd2007-07-31 12:34:36 +00004390///
4391void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner76c72282007-10-09 17:33:22 +00004392 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00004393 Token OpTok = Tok;
Steve Naroffad373bd2007-07-31 12:34:36 +00004394 SourceLocation StartLoc = ConsumeToken();
4395
John McCalle8595032010-01-13 20:03:27 +00004396 const bool hasParens = Tok.is(tok::l_paren);
4397
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00004398 bool isCastExpr;
John McCallba7bf592010-08-24 05:47:05 +00004399 ParsedType CastTy;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00004400 SourceRange CastRange;
Peter Collingbournee190dee2011-03-11 19:24:49 +00004401 ExprResult Operand = ParseExprAfterUnaryExprOrTypeTrait(OpTok, isCastExpr,
4402 CastTy, CastRange);
John McCalle8595032010-01-13 20:03:27 +00004403 if (hasParens)
4404 DS.setTypeofParensRange(CastRange);
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00004405
4406 if (CastRange.getEnd().isInvalid())
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00004407 // FIXME: Not accurate, the range gets one token more than it should.
4408 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00004409 else
4410 DS.SetRangeEnd(CastRange.getEnd());
Mike Stump11289f42009-09-09 15:08:12 +00004411
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00004412 if (isCastExpr) {
4413 if (!CastTy) {
4414 DS.SetTypeSpecError();
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00004415 return;
Douglas Gregor220cac52009-02-18 17:45:20 +00004416 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00004417
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00004418 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00004419 unsigned DiagID;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00004420 // Check for duplicate type specifiers (e.g. "int typeof(int)").
4421 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00004422 DiagID, CastTy))
4423 Diag(StartLoc, DiagID) << PrevSpec;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00004424 return;
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00004425 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00004426
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00004427 // If we get here, the operand to the typeof was an expresion.
4428 if (Operand.isInvalid()) {
4429 DS.SetTypeSpecError();
Steve Naroff4bd2f712007-08-02 02:53:48 +00004430 return;
Steve Naroffad373bd2007-07-31 12:34:36 +00004431 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00004432
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00004433 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00004434 unsigned DiagID;
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00004435 // Check for duplicate type specifiers (e.g. "int typeof(int)").
4436 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
John McCallba7bf592010-08-24 05:47:05 +00004437 DiagID, Operand.get()))
John McCall49bfce42009-08-03 20:12:06 +00004438 Diag(StartLoc, DiagID) << PrevSpec;
Steve Naroffad373bd2007-07-31 12:34:36 +00004439}
Chris Lattner73a9c7d2010-02-28 18:33:55 +00004440
4441
4442/// TryAltiVecVectorTokenOutOfLine - Out of line body that should only be called
4443/// from TryAltiVecVectorToken.
4444bool Parser::TryAltiVecVectorTokenOutOfLine() {
4445 Token Next = NextToken();
4446 switch (Next.getKind()) {
4447 default: return false;
4448 case tok::kw_short:
4449 case tok::kw_long:
4450 case tok::kw_signed:
4451 case tok::kw_unsigned:
4452 case tok::kw_void:
4453 case tok::kw_char:
4454 case tok::kw_int:
4455 case tok::kw_float:
4456 case tok::kw_double:
4457 case tok::kw_bool:
4458 case tok::kw___pixel:
4459 Tok.setKind(tok::kw___vector);
4460 return true;
4461 case tok::identifier:
4462 if (Next.getIdentifierInfo() == Ident_pixel) {
4463 Tok.setKind(tok::kw___vector);
4464 return true;
4465 }
4466 return false;
4467 }
4468}
4469
4470bool Parser::TryAltiVecTokenOutOfLine(DeclSpec &DS, SourceLocation Loc,
4471 const char *&PrevSpec, unsigned &DiagID,
4472 bool &isInvalid) {
4473 if (Tok.getIdentifierInfo() == Ident_vector) {
4474 Token Next = NextToken();
4475 switch (Next.getKind()) {
4476 case tok::kw_short:
4477 case tok::kw_long:
4478 case tok::kw_signed:
4479 case tok::kw_unsigned:
4480 case tok::kw_void:
4481 case tok::kw_char:
4482 case tok::kw_int:
4483 case tok::kw_float:
4484 case tok::kw_double:
4485 case tok::kw_bool:
4486 case tok::kw___pixel:
4487 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
4488 return true;
4489 case tok::identifier:
4490 if (Next.getIdentifierInfo() == Ident_pixel) {
4491 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
4492 return true;
4493 }
4494 break;
4495 default:
4496 break;
4497 }
Douglas Gregor9938e3b2010-06-16 15:28:57 +00004498 } else if ((Tok.getIdentifierInfo() == Ident_pixel) &&
Chris Lattner73a9c7d2010-02-28 18:33:55 +00004499 DS.isTypeAltiVecVector()) {
4500 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
4501 return true;
4502 }
4503 return false;
4504}