blob: 8329757b631252c60b4de63f79e9ff2d661ef149 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- ParseDecl.cpp - Declaration Parsing ------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Declaration portions of the Parser interfaces.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Parse/Parser.h"
Chris Lattner500d3292009-01-29 05:15:15 +000015#include "clang/Parse/ParseDiagnostic.h"
Peter Collingbourne207f4d82011-03-18 22:38:29 +000016#include "clang/Basic/OpenCL.h"
John McCall19510852010-08-20 18:27:03 +000017#include "clang/Sema/Scope.h"
18#include "clang/Sema/ParsedTemplate.h"
John McCallf312b1e2010-08-26 23:41:50 +000019#include "clang/Sema/PrettyDeclStackTrace.h"
Chris Lattnerd167ca02009-12-10 00:21:05 +000020#include "RAIIObjectsForParser.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000021#include "llvm/ADT/SmallSet.h"
Caitlin Sadowskib51e0312011-08-09 17:59:31 +000022#include "llvm/ADT/StringSwitch.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000023using namespace clang;
24
25//===----------------------------------------------------------------------===//
26// C99 6.7: Declarations.
27//===----------------------------------------------------------------------===//
28
29/// ParseTypeName
30/// type-name: [C99 6.7.6]
31/// specifier-qualifier-list abstract-declarator[opt]
Sebastian Redl4c5d3202008-11-21 19:14:01 +000032///
33/// Called type-id in C++.
Douglas Gregor683a81f2011-01-31 16:09:46 +000034TypeResult Parser::ParseTypeName(SourceRange *Range,
John McCallf85e1932011-06-15 23:02:42 +000035 Declarator::TheContext Context,
Richard Smithc89edf52011-07-01 19:46:12 +000036 ObjCDeclSpec *objcQuals,
37 AccessSpecifier AS,
38 Decl **OwnedType) {
Reid Spencer5f016e22007-07-11 17:01:13 +000039 // Parse the common declaration-specifiers piece.
John McCall0b7e6782011-03-24 11:26:52 +000040 DeclSpec DS(AttrFactory);
John McCallf85e1932011-06-15 23:02:42 +000041 DS.setObjCQualifiers(objcQuals);
Richard Smithc89edf52011-07-01 19:46:12 +000042 ParseSpecifierQualifierList(DS, AS);
43 if (OwnedType)
44 *OwnedType = DS.isTypeSpecOwned() ? DS.getRepAsDecl() : 0;
Sebastian Redlef65f062009-05-29 18:02:33 +000045
Reid Spencer5f016e22007-07-11 17:01:13 +000046 // Parse the abstract-declarator, if present.
Douglas Gregor683a81f2011-01-31 16:09:46 +000047 Declarator DeclaratorInfo(DS, Context);
Reid Spencer5f016e22007-07-11 17:01:13 +000048 ParseDeclarator(DeclaratorInfo);
Sebastian Redlef65f062009-05-29 18:02:33 +000049 if (Range)
50 *Range = DeclaratorInfo.getSourceRange();
51
Chris Lattnereaaebc72009-04-25 08:06:05 +000052 if (DeclaratorInfo.isInvalidType())
Douglas Gregor809070a2009-02-18 17:45:20 +000053 return true;
54
Douglas Gregor23c94db2010-07-02 17:43:08 +000055 return Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
Reid Spencer5f016e22007-07-11 17:01:13 +000056}
57
Sean Huntbbd37c62009-11-21 08:43:09 +000058/// ParseGNUAttributes - Parse a non-empty attributes list.
Reid Spencer5f016e22007-07-11 17:01:13 +000059///
60/// [GNU] attributes:
61/// attribute
62/// attributes attribute
63///
64/// [GNU] attribute:
65/// '__attribute__' '(' '(' attribute-list ')' ')'
66///
67/// [GNU] attribute-list:
68/// attrib
69/// attribute_list ',' attrib
70///
71/// [GNU] attrib:
72/// empty
73/// attrib-name
74/// attrib-name '(' identifier ')'
75/// attrib-name '(' identifier ',' nonempty-expr-list ')'
76/// attrib-name '(' argument-expression-list [C99 6.5.2] ')'
77///
78/// [GNU] attrib-name:
79/// identifier
80/// typespec
81/// typequal
82/// storageclass
Mike Stump1eb44332009-09-09 15:08:12 +000083///
Reid Spencer5f016e22007-07-11 17:01:13 +000084/// FIXME: The GCC grammar/code for this construct implies we need two
Mike Stump1eb44332009-09-09 15:08:12 +000085/// token lookahead. Comment from gcc: "If they start with an identifier
86/// which is followed by a comma or close parenthesis, then the arguments
Reid Spencer5f016e22007-07-11 17:01:13 +000087/// start with that identifier; otherwise they are an expression list."
88///
89/// At the moment, I am not doing 2 token lookahead. I am also unaware of
90/// any attributes that don't work (based on my limited testing). Most
91/// attributes are very simple in practice. Until we find a bug, I don't see
92/// a pressing need to implement the 2 token lookahead.
93
John McCall7f040a92010-12-24 02:08:15 +000094void Parser::ParseGNUAttributes(ParsedAttributes &attrs,
95 SourceLocation *endLoc) {
Sean Huntbbd37c62009-11-21 08:43:09 +000096 assert(Tok.is(tok::kw___attribute) && "Not a GNU attribute list!");
Mike Stump1eb44332009-09-09 15:08:12 +000097
Chris Lattner04d66662007-10-09 17:33:22 +000098 while (Tok.is(tok::kw___attribute)) {
Reid Spencer5f016e22007-07-11 17:01:13 +000099 ConsumeToken();
100 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
101 "attribute")) {
102 SkipUntil(tok::r_paren, true); // skip until ) or ;
John McCall7f040a92010-12-24 02:08:15 +0000103 return;
Reid Spencer5f016e22007-07-11 17:01:13 +0000104 }
105 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, "(")) {
106 SkipUntil(tok::r_paren, true); // skip until ) or ;
John McCall7f040a92010-12-24 02:08:15 +0000107 return;
Reid Spencer5f016e22007-07-11 17:01:13 +0000108 }
109 // Parse the attribute-list. e.g. __attribute__(( weak, alias("__f") ))
Chris Lattner04d66662007-10-09 17:33:22 +0000110 while (Tok.is(tok::identifier) || isDeclarationSpecifier() ||
111 Tok.is(tok::comma)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000112
113 if (Tok.is(tok::comma)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000114 // allows for empty/non-empty attributes. ((__vector_size__(16),,,,))
115 ConsumeToken();
116 continue;
117 }
118 // we have an identifier or declaration specifier (const, int, etc.)
119 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
120 SourceLocation AttrNameLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000121
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000122 // Availability attributes have their own grammar.
123 if (AttrName->isStr("availability"))
124 ParseAvailabilityAttribute(*AttrName, AttrNameLoc, attrs, endLoc);
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000125 // Thread safety attributes fit into the FIXME case above, so we
126 // just parse the arguments as a list of expressions
127 else if (IsThreadSafetyAttribute(AttrName->getName()))
128 ParseThreadSafetyAttribute(*AttrName, AttrNameLoc, attrs, endLoc);
Douglas Gregorec1afbf2010-03-16 19:09:18 +0000129 // check if we have a "parameterized" attribute
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000130 else if (Tok.is(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000131 ConsumeParen(); // ignore the left paren loc for now
Mike Stump1eb44332009-09-09 15:08:12 +0000132
Chris Lattner04d66662007-10-09 17:33:22 +0000133 if (Tok.is(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000134 IdentifierInfo *ParmName = Tok.getIdentifierInfo();
135 SourceLocation ParmLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000136
137 if (Tok.is(tok::r_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000138 // __attribute__(( mode(byte) ))
139 ConsumeParen(); // ignore the right paren loc for now
John McCall0b7e6782011-03-24 11:26:52 +0000140 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc,
141 ParmName, ParmLoc, 0, 0);
Chris Lattner04d66662007-10-09 17:33:22 +0000142 } else if (Tok.is(tok::comma)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000143 ConsumeToken();
144 // __attribute__(( format(printf, 1, 2) ))
Sebastian Redla55e52c2008-11-25 22:21:31 +0000145 ExprVector ArgExprs(Actions);
Reid Spencer5f016e22007-07-11 17:01:13 +0000146 bool ArgExprsOk = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000147
Reid Spencer5f016e22007-07-11 17:01:13 +0000148 // now parse the non-empty comma separated list of expressions
149 while (1) {
John McCall60d7b3a2010-08-24 06:29:42 +0000150 ExprResult ArgExpr(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000151 if (ArgExpr.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000152 ArgExprsOk = false;
153 SkipUntil(tok::r_paren);
154 break;
155 } else {
Sebastian Redleffa8d12008-12-10 00:02:53 +0000156 ArgExprs.push_back(ArgExpr.release());
Reid Spencer5f016e22007-07-11 17:01:13 +0000157 }
Chris Lattner04d66662007-10-09 17:33:22 +0000158 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +0000159 break;
160 ConsumeToken(); // Eat the comma, move to the next argument
161 }
Chris Lattner04d66662007-10-09 17:33:22 +0000162 if (ArgExprsOk && Tok.is(tok::r_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000163 ConsumeParen(); // ignore the right paren loc for now
John McCall0b7e6782011-03-24 11:26:52 +0000164 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc,
165 ParmName, ParmLoc, ArgExprs.take(), ArgExprs.size());
Reid Spencer5f016e22007-07-11 17:01:13 +0000166 }
167 }
168 } else { // not an identifier
Nate Begeman6f3d8382009-06-26 06:32:41 +0000169 switch (Tok.getKind()) {
170 case tok::r_paren:
Reid Spencer5f016e22007-07-11 17:01:13 +0000171 // parse a possibly empty comma separated list of expressions
Reid Spencer5f016e22007-07-11 17:01:13 +0000172 // __attribute__(( nonnull() ))
173 ConsumeParen(); // ignore the right paren loc for now
John McCall0b7e6782011-03-24 11:26:52 +0000174 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc,
175 0, SourceLocation(), 0, 0);
Nate Begeman6f3d8382009-06-26 06:32:41 +0000176 break;
177 case tok::kw_char:
178 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000179 case tok::kw_char16_t:
180 case tok::kw_char32_t:
Nate Begeman6f3d8382009-06-26 06:32:41 +0000181 case tok::kw_bool:
182 case tok::kw_short:
183 case tok::kw_int:
184 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +0000185 case tok::kw___int64:
Nate Begeman6f3d8382009-06-26 06:32:41 +0000186 case tok::kw_signed:
187 case tok::kw_unsigned:
188 case tok::kw_float:
189 case tok::kw_double:
190 case tok::kw_void:
John McCall7f040a92010-12-24 02:08:15 +0000191 case tok::kw_typeof: {
192 AttributeList *attr
John McCall0b7e6782011-03-24 11:26:52 +0000193 = attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc,
194 0, SourceLocation(), 0, 0);
John McCall7f040a92010-12-24 02:08:15 +0000195 if (attr->getKind() == AttributeList::AT_IBOutletCollection)
Fariborz Jahanian1b72fa72010-08-17 23:19:16 +0000196 Diag(Tok, diag::err_iboutletcollection_builtintype);
Nate Begeman6f3d8382009-06-26 06:32:41 +0000197 // If it's a builtin type name, eat it and expect a rparen
198 // __attribute__(( vec_type_hint(char) ))
199 ConsumeToken();
Nate Begeman6f3d8382009-06-26 06:32:41 +0000200 if (Tok.is(tok::r_paren))
201 ConsumeParen();
202 break;
John McCall7f040a92010-12-24 02:08:15 +0000203 }
Nate Begeman6f3d8382009-06-26 06:32:41 +0000204 default:
Reid Spencer5f016e22007-07-11 17:01:13 +0000205 // __attribute__(( aligned(16) ))
Sebastian Redla55e52c2008-11-25 22:21:31 +0000206 ExprVector ArgExprs(Actions);
Reid Spencer5f016e22007-07-11 17:01:13 +0000207 bool ArgExprsOk = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000208
Reid Spencer5f016e22007-07-11 17:01:13 +0000209 // now parse the list of expressions
210 while (1) {
John McCall60d7b3a2010-08-24 06:29:42 +0000211 ExprResult ArgExpr(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000212 if (ArgExpr.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000213 ArgExprsOk = false;
214 SkipUntil(tok::r_paren);
215 break;
216 } else {
Sebastian Redleffa8d12008-12-10 00:02:53 +0000217 ArgExprs.push_back(ArgExpr.release());
Reid Spencer5f016e22007-07-11 17:01:13 +0000218 }
Chris Lattner04d66662007-10-09 17:33:22 +0000219 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +0000220 break;
221 ConsumeToken(); // Eat the comma, move to the next argument
222 }
223 // Match the ')'.
Chris Lattner04d66662007-10-09 17:33:22 +0000224 if (ArgExprsOk && Tok.is(tok::r_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000225 ConsumeParen(); // ignore the right paren loc for now
John McCall0b7e6782011-03-24 11:26:52 +0000226 attrs.addNew(AttrName, AttrNameLoc, 0,
227 AttrNameLoc, 0, SourceLocation(),
228 ArgExprs.take(), ArgExprs.size());
Reid Spencer5f016e22007-07-11 17:01:13 +0000229 }
Nate Begeman6f3d8382009-06-26 06:32:41 +0000230 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000231 }
232 }
233 } else {
John McCall0b7e6782011-03-24 11:26:52 +0000234 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc,
235 0, SourceLocation(), 0, 0);
Reid Spencer5f016e22007-07-11 17:01:13 +0000236 }
237 }
238 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
Reid Spencer5f016e22007-07-11 17:01:13 +0000239 SkipUntil(tok::r_paren, false);
Sean Huntbbd37c62009-11-21 08:43:09 +0000240 SourceLocation Loc = Tok.getLocation();
Sebastian Redlab197ba2009-02-09 18:23:29 +0000241 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) {
242 SkipUntil(tok::r_paren, false);
243 }
John McCall7f040a92010-12-24 02:08:15 +0000244 if (endLoc)
245 *endLoc = Loc;
Reid Spencer5f016e22007-07-11 17:01:13 +0000246 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000247}
248
Eli Friedmana23b4852009-06-08 07:21:15 +0000249/// ParseMicrosoftDeclSpec - Parse an __declspec construct
250///
251/// [MS] decl-specifier:
252/// __declspec ( extended-decl-modifier-seq )
253///
254/// [MS] extended-decl-modifier-seq:
255/// extended-decl-modifier[opt]
256/// extended-decl-modifier extended-decl-modifier-seq
257
John McCall7f040a92010-12-24 02:08:15 +0000258void Parser::ParseMicrosoftDeclSpec(ParsedAttributes &attrs) {
Steve Narofff59e17e2008-12-24 20:59:21 +0000259 assert(Tok.is(tok::kw___declspec) && "Not a declspec!");
Eli Friedmana23b4852009-06-08 07:21:15 +0000260
Steve Narofff59e17e2008-12-24 20:59:21 +0000261 ConsumeToken();
Eli Friedmana23b4852009-06-08 07:21:15 +0000262 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
263 "declspec")) {
264 SkipUntil(tok::r_paren, true); // skip until ) or ;
John McCall7f040a92010-12-24 02:08:15 +0000265 return;
Eli Friedmana23b4852009-06-08 07:21:15 +0000266 }
Francois Pichet373197b2011-05-07 19:04:49 +0000267
Eli Friedman290eeb02009-06-08 23:27:34 +0000268 while (Tok.getIdentifierInfo()) {
Eli Friedmana23b4852009-06-08 07:21:15 +0000269 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
270 SourceLocation AttrNameLoc = ConsumeToken();
Francois Pichet373197b2011-05-07 19:04:49 +0000271
272 // FIXME: Remove this when we have proper __declspec(property()) support.
273 // Just skip everything inside property().
274 if (AttrName->getName() == "property") {
275 ConsumeParen();
276 SkipUntil(tok::r_paren);
277 }
Eli Friedmana23b4852009-06-08 07:21:15 +0000278 if (Tok.is(tok::l_paren)) {
279 ConsumeParen();
280 // FIXME: This doesn't parse __declspec(property(get=get_func_name))
281 // correctly.
John McCall60d7b3a2010-08-24 06:29:42 +0000282 ExprResult ArgExpr(ParseAssignmentExpression());
Eli Friedmana23b4852009-06-08 07:21:15 +0000283 if (!ArgExpr.isInvalid()) {
John McCallca0408f2010-08-23 06:44:23 +0000284 Expr *ExprList = ArgExpr.take();
John McCall0b7e6782011-03-24 11:26:52 +0000285 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
286 SourceLocation(), &ExprList, 1, true);
Eli Friedmana23b4852009-06-08 07:21:15 +0000287 }
288 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
289 SkipUntil(tok::r_paren, false);
290 } else {
John McCall0b7e6782011-03-24 11:26:52 +0000291 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc,
292 0, SourceLocation(), 0, 0, true);
Eli Friedmana23b4852009-06-08 07:21:15 +0000293 }
294 }
295 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
296 SkipUntil(tok::r_paren, false);
John McCall7f040a92010-12-24 02:08:15 +0000297 return;
Eli Friedman290eeb02009-06-08 23:27:34 +0000298}
299
John McCall7f040a92010-12-24 02:08:15 +0000300void Parser::ParseMicrosoftTypeAttributes(ParsedAttributes &attrs) {
Eli Friedman290eeb02009-06-08 23:27:34 +0000301 // Treat these like attributes
302 // FIXME: Allow Sema to distinguish between these and real attributes!
303 while (Tok.is(tok::kw___fastcall) || Tok.is(tok::kw___stdcall) ||
Douglas Gregorf813a2c2010-05-18 16:57:00 +0000304 Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___cdecl) ||
Francois Pichet3bd9aa42011-08-18 09:59:55 +0000305 Tok.is(tok::kw___ptr64) || Tok.is(tok::kw___w64) ||
Francois Pichet58fd97a2011-08-25 00:36:46 +0000306 Tok.is(tok::kw___ptr32) ||
Francois Pichet3bd9aa42011-08-18 09:59:55 +0000307 Tok.is(tok::kw___unaligned)) {
Eli Friedman290eeb02009-06-08 23:27:34 +0000308 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
309 SourceLocation AttrNameLoc = ConsumeToken();
Francois Pichet58fd97a2011-08-25 00:36:46 +0000310 if (Tok.is(tok::kw___ptr64) || Tok.is(tok::kw___w64) ||
311 Tok.is(tok::kw___ptr32))
Eli Friedman290eeb02009-06-08 23:27:34 +0000312 // FIXME: Support these properly!
313 continue;
John McCall0b7e6782011-03-24 11:26:52 +0000314 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
315 SourceLocation(), 0, 0, true);
Eli Friedman290eeb02009-06-08 23:27:34 +0000316 }
Steve Narofff59e17e2008-12-24 20:59:21 +0000317}
318
John McCall7f040a92010-12-24 02:08:15 +0000319void Parser::ParseBorlandTypeAttributes(ParsedAttributes &attrs) {
Dawn Perchik52fc3142010-09-03 01:29:35 +0000320 // Treat these like attributes
321 while (Tok.is(tok::kw___pascal)) {
322 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
323 SourceLocation AttrNameLoc = ConsumeToken();
John McCall0b7e6782011-03-24 11:26:52 +0000324 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
325 SourceLocation(), 0, 0, true);
Dawn Perchik52fc3142010-09-03 01:29:35 +0000326 }
John McCall7f040a92010-12-24 02:08:15 +0000327}
328
Peter Collingbournef315fa82011-02-14 01:42:53 +0000329void Parser::ParseOpenCLAttributes(ParsedAttributes &attrs) {
330 // Treat these like attributes
331 while (Tok.is(tok::kw___kernel)) {
332 SourceLocation AttrNameLoc = ConsumeToken();
John McCall0b7e6782011-03-24 11:26:52 +0000333 attrs.addNew(PP.getIdentifierInfo("opencl_kernel_function"),
334 AttrNameLoc, 0, AttrNameLoc, 0,
335 SourceLocation(), 0, 0, false);
Peter Collingbournef315fa82011-02-14 01:42:53 +0000336 }
337}
338
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000339void Parser::ParseOpenCLQualifiers(DeclSpec &DS) {
340 SourceLocation Loc = Tok.getLocation();
341 switch(Tok.getKind()) {
342 // OpenCL qualifiers:
343 case tok::kw___private:
344 case tok::kw_private:
John McCall0b7e6782011-03-24 11:26:52 +0000345 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000346 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000347 PP.getIdentifierInfo("address_space"), Loc, 0);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000348 break;
349
350 case tok::kw___global:
John McCall0b7e6782011-03-24 11:26:52 +0000351 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000352 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000353 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_global);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000354 break;
355
356 case tok::kw___local:
John McCall0b7e6782011-03-24 11:26:52 +0000357 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000358 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000359 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_local);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000360 break;
361
362 case tok::kw___constant:
John McCall0b7e6782011-03-24 11:26:52 +0000363 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000364 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000365 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_constant);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000366 break;
367
368 case tok::kw___read_only:
John McCall0b7e6782011-03-24 11:26:52 +0000369 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000370 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000371 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_read_only);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000372 break;
373
374 case tok::kw___write_only:
John McCall0b7e6782011-03-24 11:26:52 +0000375 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000376 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000377 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_write_only);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000378 break;
379
380 case tok::kw___read_write:
John McCall0b7e6782011-03-24 11:26:52 +0000381 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000382 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000383 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_read_write);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000384 break;
385 default: break;
386 }
387}
388
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000389/// \brief Parse a version number.
390///
391/// version:
392/// simple-integer
393/// simple-integer ',' simple-integer
394/// simple-integer ',' simple-integer ',' simple-integer
395VersionTuple Parser::ParseVersionTuple(SourceRange &Range) {
396 Range = Tok.getLocation();
397
398 if (!Tok.is(tok::numeric_constant)) {
399 Diag(Tok, diag::err_expected_version);
400 SkipUntil(tok::comma, tok::r_paren, true, true, true);
401 return VersionTuple();
402 }
403
404 // Parse the major (and possibly minor and subminor) versions, which
405 // are stored in the numeric constant. We utilize a quirk of the
406 // lexer, which is that it handles something like 1.2.3 as a single
407 // numeric constant, rather than two separate tokens.
408 llvm::SmallString<512> Buffer;
409 Buffer.resize(Tok.getLength()+1);
410 const char *ThisTokBegin = &Buffer[0];
411
412 // Get the spelling of the token, which eliminates trigraphs, etc.
413 bool Invalid = false;
414 unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin, &Invalid);
415 if (Invalid)
416 return VersionTuple();
417
418 // Parse the major version.
419 unsigned AfterMajor = 0;
420 unsigned Major = 0;
421 while (AfterMajor < ActualLength && isdigit(ThisTokBegin[AfterMajor])) {
422 Major = Major * 10 + ThisTokBegin[AfterMajor] - '0';
423 ++AfterMajor;
424 }
425
426 if (AfterMajor == 0) {
427 Diag(Tok, diag::err_expected_version);
428 SkipUntil(tok::comma, tok::r_paren, true, true, true);
429 return VersionTuple();
430 }
431
432 if (AfterMajor == ActualLength) {
433 ConsumeToken();
434
435 // We only had a single version component.
436 if (Major == 0) {
437 Diag(Tok, diag::err_zero_version);
438 return VersionTuple();
439 }
440
441 return VersionTuple(Major);
442 }
443
444 if (ThisTokBegin[AfterMajor] != '.' || (AfterMajor + 1 == ActualLength)) {
445 Diag(Tok, diag::err_expected_version);
446 SkipUntil(tok::comma, tok::r_paren, true, true, true);
447 return VersionTuple();
448 }
449
450 // Parse the minor version.
451 unsigned AfterMinor = AfterMajor + 1;
452 unsigned Minor = 0;
453 while (AfterMinor < ActualLength && isdigit(ThisTokBegin[AfterMinor])) {
454 Minor = Minor * 10 + ThisTokBegin[AfterMinor] - '0';
455 ++AfterMinor;
456 }
457
458 if (AfterMinor == ActualLength) {
459 ConsumeToken();
460
461 // We had major.minor.
462 if (Major == 0 && Minor == 0) {
463 Diag(Tok, diag::err_zero_version);
464 return VersionTuple();
465 }
466
467 return VersionTuple(Major, Minor);
468 }
469
470 // If what follows is not a '.', we have a problem.
471 if (ThisTokBegin[AfterMinor] != '.') {
472 Diag(Tok, diag::err_expected_version);
473 SkipUntil(tok::comma, tok::r_paren, true, true, true);
474 return VersionTuple();
475 }
476
477 // Parse the subminor version.
478 unsigned AfterSubminor = AfterMinor + 1;
479 unsigned Subminor = 0;
480 while (AfterSubminor < ActualLength && isdigit(ThisTokBegin[AfterSubminor])) {
481 Subminor = Subminor * 10 + ThisTokBegin[AfterSubminor] - '0';
482 ++AfterSubminor;
483 }
484
485 if (AfterSubminor != ActualLength) {
486 Diag(Tok, diag::err_expected_version);
487 SkipUntil(tok::comma, tok::r_paren, true, true, true);
488 return VersionTuple();
489 }
490 ConsumeToken();
491 return VersionTuple(Major, Minor, Subminor);
492}
493
494/// \brief Parse the contents of the "availability" attribute.
495///
496/// availability-attribute:
497/// 'availability' '(' platform ',' version-arg-list ')'
498///
499/// platform:
500/// identifier
501///
502/// version-arg-list:
503/// version-arg
504/// version-arg ',' version-arg-list
505///
506/// version-arg:
507/// 'introduced' '=' version
508/// 'deprecated' '=' version
509/// 'removed' = version
Douglas Gregorb53e4172011-03-26 03:35:55 +0000510/// 'unavailable'
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000511void Parser::ParseAvailabilityAttribute(IdentifierInfo &Availability,
512 SourceLocation AvailabilityLoc,
513 ParsedAttributes &attrs,
514 SourceLocation *endLoc) {
515 SourceLocation PlatformLoc;
516 IdentifierInfo *Platform = 0;
517
518 enum { Introduced, Deprecated, Obsoleted, Unknown };
519 AvailabilityChange Changes[Unknown];
520
521 // Opening '('.
522 SourceLocation LParenLoc;
523 if (!Tok.is(tok::l_paren)) {
524 Diag(Tok, diag::err_expected_lparen);
525 return;
526 }
527 LParenLoc = ConsumeParen();
528
529 // Parse the platform name,
530 if (Tok.isNot(tok::identifier)) {
531 Diag(Tok, diag::err_availability_expected_platform);
532 SkipUntil(tok::r_paren);
533 return;
534 }
535 Platform = Tok.getIdentifierInfo();
536 PlatformLoc = ConsumeToken();
537
538 // Parse the ',' following the platform name.
539 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "", tok::r_paren))
540 return;
541
542 // If we haven't grabbed the pointers for the identifiers
543 // "introduced", "deprecated", and "obsoleted", do so now.
544 if (!Ident_introduced) {
545 Ident_introduced = PP.getIdentifierInfo("introduced");
546 Ident_deprecated = PP.getIdentifierInfo("deprecated");
547 Ident_obsoleted = PP.getIdentifierInfo("obsoleted");
Douglas Gregorb53e4172011-03-26 03:35:55 +0000548 Ident_unavailable = PP.getIdentifierInfo("unavailable");
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000549 }
550
551 // Parse the set of introductions/deprecations/removals.
Douglas Gregorb53e4172011-03-26 03:35:55 +0000552 SourceLocation UnavailableLoc;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000553 do {
554 if (Tok.isNot(tok::identifier)) {
555 Diag(Tok, diag::err_availability_expected_change);
556 SkipUntil(tok::r_paren);
557 return;
558 }
559 IdentifierInfo *Keyword = Tok.getIdentifierInfo();
560 SourceLocation KeywordLoc = ConsumeToken();
561
Douglas Gregorb53e4172011-03-26 03:35:55 +0000562 if (Keyword == Ident_unavailable) {
563 if (UnavailableLoc.isValid()) {
564 Diag(KeywordLoc, diag::err_availability_redundant)
565 << Keyword << SourceRange(UnavailableLoc);
566 }
567 UnavailableLoc = KeywordLoc;
568
569 if (Tok.isNot(tok::comma))
570 break;
571
572 ConsumeToken();
573 continue;
574 }
575
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000576 if (Tok.isNot(tok::equal)) {
577 Diag(Tok, diag::err_expected_equal_after)
578 << Keyword;
579 SkipUntil(tok::r_paren);
580 return;
581 }
582 ConsumeToken();
583
584 SourceRange VersionRange;
585 VersionTuple Version = ParseVersionTuple(VersionRange);
586
587 if (Version.empty()) {
588 SkipUntil(tok::r_paren);
589 return;
590 }
591
592 unsigned Index;
593 if (Keyword == Ident_introduced)
594 Index = Introduced;
595 else if (Keyword == Ident_deprecated)
596 Index = Deprecated;
597 else if (Keyword == Ident_obsoleted)
598 Index = Obsoleted;
599 else
600 Index = Unknown;
601
602 if (Index < Unknown) {
603 if (!Changes[Index].KeywordLoc.isInvalid()) {
604 Diag(KeywordLoc, diag::err_availability_redundant)
605 << Keyword
606 << SourceRange(Changes[Index].KeywordLoc,
607 Changes[Index].VersionRange.getEnd());
608 }
609
610 Changes[Index].KeywordLoc = KeywordLoc;
611 Changes[Index].Version = Version;
612 Changes[Index].VersionRange = VersionRange;
613 } else {
614 Diag(KeywordLoc, diag::err_availability_unknown_change)
615 << Keyword << VersionRange;
616 }
617
618 if (Tok.isNot(tok::comma))
619 break;
620
621 ConsumeToken();
622 } while (true);
623
624 // Closing ')'.
625 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
626 if (RParenLoc.isInvalid())
627 return;
628
629 if (endLoc)
630 *endLoc = RParenLoc;
631
Douglas Gregorb53e4172011-03-26 03:35:55 +0000632 // The 'unavailable' availability cannot be combined with any other
633 // availability changes. Make sure that hasn't happened.
634 if (UnavailableLoc.isValid()) {
635 bool Complained = false;
636 for (unsigned Index = Introduced; Index != Unknown; ++Index) {
637 if (Changes[Index].KeywordLoc.isValid()) {
638 if (!Complained) {
639 Diag(UnavailableLoc, diag::warn_availability_and_unavailable)
640 << SourceRange(Changes[Index].KeywordLoc,
641 Changes[Index].VersionRange.getEnd());
642 Complained = true;
643 }
644
645 // Clear out the availability.
646 Changes[Index] = AvailabilityChange();
647 }
648 }
649 }
650
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000651 // Record this attribute
Douglas Gregorb53e4172011-03-26 03:35:55 +0000652 attrs.addNew(&Availability, AvailabilityLoc,
John McCall0b7e6782011-03-24 11:26:52 +0000653 0, SourceLocation(),
654 Platform, PlatformLoc,
655 Changes[Introduced],
656 Changes[Deprecated],
Douglas Gregorb53e4172011-03-26 03:35:55 +0000657 Changes[Obsoleted],
658 UnavailableLoc, false, false);
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000659}
660
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000661/// \brief Wrapper around a case statement checking if AttrName is
662/// one of the thread safety attributes
663bool Parser::IsThreadSafetyAttribute(llvm::StringRef AttrName){
664 return llvm::StringSwitch<bool>(AttrName)
665 .Case("guarded_by", true)
666 .Case("guarded_var", true)
667 .Case("pt_guarded_by", true)
668 .Case("pt_guarded_var", true)
669 .Case("lockable", true)
670 .Case("scoped_lockable", true)
671 .Case("no_thread_safety_analysis", true)
672 .Case("acquired_after", true)
673 .Case("acquired_before", true)
674 .Case("exclusive_lock_function", true)
675 .Case("shared_lock_function", true)
676 .Case("exclusive_trylock_function", true)
677 .Case("shared_trylock_function", true)
678 .Case("unlock_function", true)
679 .Case("lock_returned", true)
680 .Case("locks_excluded", true)
681 .Case("exclusive_locks_required", true)
682 .Case("shared_locks_required", true)
683 .Default(false);
684}
685
686/// \brief Parse the contents of thread safety attributes. These
687/// should always be parsed as an expression list.
688///
689/// We need to special case the parsing due to the fact that if the first token
690/// of the first argument is an identifier, the main parse loop will store
691/// that token as a "parameter" and the rest of
692/// the arguments will be added to a list of "arguments". However,
693/// subsequent tokens in the first argument are lost. We instead parse each
694/// argument as an expression and add all arguments to the list of "arguments".
695/// In future, we will take advantage of this special case to also
696/// deal with some argument scoping issues here (for example, referring to a
697/// function parameter in the attribute on that function).
698void Parser::ParseThreadSafetyAttribute(IdentifierInfo &AttrName,
699 SourceLocation AttrNameLoc,
700 ParsedAttributes &Attrs,
701 SourceLocation *EndLoc) {
702
703 if (Tok.is(tok::l_paren)) {
704 SourceLocation LeftParenLoc = Tok.getLocation();
705 ConsumeParen(); // ignore the left paren loc for now
706
707 ExprVector ArgExprs(Actions);
708 bool ArgExprsOk = true;
709
710 // now parse the list of expressions
711 while (1) {
712 ExprResult ArgExpr(ParseAssignmentExpression());
713 if (ArgExpr.isInvalid()) {
714 ArgExprsOk = false;
715 MatchRHSPunctuation(tok::r_paren, LeftParenLoc);
716 break;
717 } else {
718 ArgExprs.push_back(ArgExpr.release());
719 }
720 if (Tok.isNot(tok::comma))
721 break;
722 ConsumeToken(); // Eat the comma, move to the next argument
723 }
724 // Match the ')'.
725 if (ArgExprsOk && Tok.is(tok::r_paren)) {
726 ConsumeParen(); // ignore the right paren loc for now
727 Attrs.addNew(&AttrName, AttrNameLoc, 0, AttrNameLoc, 0, SourceLocation(),
728 ArgExprs.take(), ArgExprs.size());
729 }
730 } else {
731 Attrs.addNew(&AttrName, AttrNameLoc, 0, AttrNameLoc,
732 0, SourceLocation(), 0, 0);
733 }
734}
735
John McCall7f040a92010-12-24 02:08:15 +0000736void Parser::DiagnoseProhibitedAttributes(ParsedAttributesWithRange &attrs) {
737 Diag(attrs.Range.getBegin(), diag::err_attributes_not_allowed)
738 << attrs.Range;
Dawn Perchik52fc3142010-09-03 01:29:35 +0000739}
740
Reid Spencer5f016e22007-07-11 17:01:13 +0000741/// ParseDeclaration - Parse a full 'declaration', which consists of
742/// declaration-specifiers, some number of declarators, and a semicolon.
Chris Lattner97144fc2009-04-02 04:16:50 +0000743/// 'Context' should be a Declarator::TheContext value. This returns the
744/// location of the semicolon in DeclEnd.
Chris Lattner8f08cb72007-08-25 06:57:03 +0000745///
746/// declaration: [C99 6.7]
747/// block-declaration ->
748/// simple-declaration
749/// others [FIXME]
Douglas Gregoradcac882008-12-01 23:54:00 +0000750/// [C++] template-declaration
Chris Lattner8f08cb72007-08-25 06:57:03 +0000751/// [C++] namespace-definition
Douglas Gregorf780abc2008-12-30 03:27:21 +0000752/// [C++] using-directive
Douglas Gregord7f37bf2009-06-22 23:06:13 +0000753/// [C++] using-declaration
Peter Collingbournec6eb44b2011-04-15 00:35:57 +0000754/// [C++0x/C1X] static_assert-declaration
Chris Lattner8f08cb72007-08-25 06:57:03 +0000755/// others... [FIXME]
756///
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +0000757Parser::DeclGroupPtrTy Parser::ParseDeclaration(StmtVector &Stmts,
758 unsigned Context,
Sean Huntbbd37c62009-11-21 08:43:09 +0000759 SourceLocation &DeclEnd,
John McCall7f040a92010-12-24 02:08:15 +0000760 ParsedAttributesWithRange &attrs) {
Argyrios Kyrtzidis36d36802010-06-17 10:52:18 +0000761 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Fariborz Jahaniane8cff362011-08-30 17:10:52 +0000762 // Must temporarily exit the objective-c container scope for
763 // parsing c none objective-c decls.
764 ObjCDeclContextSwitch ObjCDC(*this);
Argyrios Kyrtzidis36d36802010-06-17 10:52:18 +0000765
John McCalld226f652010-08-21 09:40:31 +0000766 Decl *SingleDecl = 0;
Richard Smithc89edf52011-07-01 19:46:12 +0000767 Decl *OwnedType = 0;
Chris Lattner8f08cb72007-08-25 06:57:03 +0000768 switch (Tok.getKind()) {
Douglas Gregoradcac882008-12-01 23:54:00 +0000769 case tok::kw_template:
Douglas Gregor1426e532009-05-12 21:31:51 +0000770 case tok::kw_export:
John McCall7f040a92010-12-24 02:08:15 +0000771 ProhibitAttributes(attrs);
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000772 SingleDecl = ParseDeclarationStartingWithTemplate(Context, DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +0000773 break;
Sebastian Redld078e642010-08-27 23:12:46 +0000774 case tok::kw_inline:
Sebastian Redl88e64ca2010-08-31 00:36:45 +0000775 // Could be the start of an inline namespace. Allowed as an ext in C++03.
776 if (getLang().CPlusPlus && NextToken().is(tok::kw_namespace)) {
John McCall7f040a92010-12-24 02:08:15 +0000777 ProhibitAttributes(attrs);
Sebastian Redld078e642010-08-27 23:12:46 +0000778 SourceLocation InlineLoc = ConsumeToken();
779 SingleDecl = ParseNamespace(Context, DeclEnd, InlineLoc);
780 break;
781 }
John McCall7f040a92010-12-24 02:08:15 +0000782 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs,
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +0000783 true);
Chris Lattner8f08cb72007-08-25 06:57:03 +0000784 case tok::kw_namespace:
John McCall7f040a92010-12-24 02:08:15 +0000785 ProhibitAttributes(attrs);
Chris Lattner97144fc2009-04-02 04:16:50 +0000786 SingleDecl = ParseNamespace(Context, DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +0000787 break;
Douglas Gregorf780abc2008-12-30 03:27:21 +0000788 case tok::kw_using:
John McCall78b81052010-11-10 02:40:36 +0000789 SingleDecl = ParseUsingDirectiveOrDeclaration(Context, ParsedTemplateInfo(),
Richard Smithc89edf52011-07-01 19:46:12 +0000790 DeclEnd, attrs, &OwnedType);
Chris Lattner682bf922009-03-29 16:50:03 +0000791 break;
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000792 case tok::kw_static_assert:
Peter Collingbournec6eb44b2011-04-15 00:35:57 +0000793 case tok::kw__Static_assert:
John McCall7f040a92010-12-24 02:08:15 +0000794 ProhibitAttributes(attrs);
Chris Lattner97144fc2009-04-02 04:16:50 +0000795 SingleDecl = ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +0000796 break;
Chris Lattner8f08cb72007-08-25 06:57:03 +0000797 default:
John McCall7f040a92010-12-24 02:08:15 +0000798 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs, true);
Chris Lattner8f08cb72007-08-25 06:57:03 +0000799 }
Sean Huntbbd37c62009-11-21 08:43:09 +0000800
Chris Lattner682bf922009-03-29 16:50:03 +0000801 // This routine returns a DeclGroup, if the thing we parsed only contains a
Richard Smithc89edf52011-07-01 19:46:12 +0000802 // single decl, convert it now. Alias declarations can also declare a type;
803 // include that too if it is present.
804 return Actions.ConvertDeclToDeclGroup(SingleDecl, OwnedType);
Chris Lattner8f08cb72007-08-25 06:57:03 +0000805}
806
807/// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl]
808/// declaration-specifiers init-declarator-list[opt] ';'
809///[C90/C++]init-declarator-list ';' [TODO]
810/// [OMP] threadprivate-directive [TODO]
Chris Lattnercd147752009-03-29 17:27:48 +0000811///
Richard Smithad762fc2011-04-14 22:09:26 +0000812/// for-range-declaration: [C++0x 6.5p1: stmt.ranged]
813/// attribute-specifier-seq[opt] type-specifier-seq declarator
814///
Chris Lattnercd147752009-03-29 17:27:48 +0000815/// If RequireSemi is false, this does not check for a ';' at the end of the
Chris Lattner5c5db552010-04-05 18:18:31 +0000816/// declaration. If it is true, it checks for and eats it.
Richard Smithad762fc2011-04-14 22:09:26 +0000817///
818/// If FRI is non-null, we might be parsing a for-range-declaration instead
819/// of a simple-declaration. If we find that we are, we also parse the
820/// for-range-initializer, and place it here.
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +0000821Parser::DeclGroupPtrTy Parser::ParseSimpleDeclaration(StmtVector &Stmts,
822 unsigned Context,
Sean Huntbbd37c62009-11-21 08:43:09 +0000823 SourceLocation &DeclEnd,
John McCall7f040a92010-12-24 02:08:15 +0000824 ParsedAttributes &attrs,
Richard Smithad762fc2011-04-14 22:09:26 +0000825 bool RequireSemi,
826 ForRangeInit *FRI) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000827 // Parse the common declaration-specifiers piece.
John McCall54abf7d2009-11-04 02:18:39 +0000828 ParsingDeclSpec DS(*this);
John McCall7f040a92010-12-24 02:08:15 +0000829 DS.takeAttributesFrom(attrs);
Douglas Gregor312eadb2011-04-24 05:37:28 +0000830
Douglas Gregor0efc2c12010-01-13 17:31:36 +0000831 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS_none,
Richard Smith34b41d92011-02-20 03:19:35 +0000832 getDeclSpecContextFromDeclaratorContext(Context));
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +0000833 StmtResult R = Actions.ActOnVlaStmt(DS);
834 if (R.isUsable())
835 Stmts.push_back(R.release());
Douglas Gregor312eadb2011-04-24 05:37:28 +0000836
Reid Spencer5f016e22007-07-11 17:01:13 +0000837 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
838 // declaration-specifiers init-declarator-list[opt] ';'
Chris Lattner04d66662007-10-09 17:33:22 +0000839 if (Tok.is(tok::semi)) {
Chris Lattner5c5db552010-04-05 18:18:31 +0000840 if (RequireSemi) ConsumeToken();
John McCalld226f652010-08-21 09:40:31 +0000841 Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none,
Douglas Gregor312eadb2011-04-24 05:37:28 +0000842 DS);
John McCall54abf7d2009-11-04 02:18:39 +0000843 DS.complete(TheDecl);
Chris Lattner682bf922009-03-29 16:50:03 +0000844 return Actions.ConvertDeclToDeclGroup(TheDecl);
Reid Spencer5f016e22007-07-11 17:01:13 +0000845 }
Douglas Gregor312eadb2011-04-24 05:37:28 +0000846
847 return ParseDeclGroup(DS, Context, /*FunctionDefs=*/ false, &DeclEnd, FRI);
John McCalld8ac0572009-11-03 19:26:08 +0000848}
Mike Stump1eb44332009-09-09 15:08:12 +0000849
John McCalld8ac0572009-11-03 19:26:08 +0000850/// ParseDeclGroup - Having concluded that this is either a function
851/// definition or a group of object declarations, actually parse the
852/// result.
John McCall54abf7d2009-11-04 02:18:39 +0000853Parser::DeclGroupPtrTy Parser::ParseDeclGroup(ParsingDeclSpec &DS,
854 unsigned Context,
John McCalld8ac0572009-11-03 19:26:08 +0000855 bool AllowFunctionDefinitions,
Richard Smithad762fc2011-04-14 22:09:26 +0000856 SourceLocation *DeclEnd,
857 ForRangeInit *FRI) {
John McCalld8ac0572009-11-03 19:26:08 +0000858 // Parse the first declarator.
John McCall54abf7d2009-11-04 02:18:39 +0000859 ParsingDeclarator D(*this, DS, static_cast<Declarator::TheContext>(Context));
John McCalld8ac0572009-11-03 19:26:08 +0000860 ParseDeclarator(D);
Chris Lattnercd147752009-03-29 17:27:48 +0000861
John McCalld8ac0572009-11-03 19:26:08 +0000862 // Bail out if the first declarator didn't seem well-formed.
863 if (!D.hasName() && !D.mayOmitIdentifier()) {
864 // Skip until ; or }.
865 SkipUntil(tok::r_brace, true, true);
866 if (Tok.is(tok::semi))
867 ConsumeToken();
868 return DeclGroupPtrTy();
Chris Lattner23c4b182009-03-29 17:18:04 +0000869 }
Mike Stump1eb44332009-09-09 15:08:12 +0000870
Chris Lattnerc82daef2010-07-11 22:24:20 +0000871 // Check to see if we have a function *definition* which must have a body.
872 if (AllowFunctionDefinitions && D.isFunctionDeclarator() &&
873 // Look at the next token to make sure that this isn't a function
874 // declaration. We have to check this because __attribute__ might be the
875 // start of a function definition in GCC-extended K&R C.
876 !isDeclarationAfterDeclarator()) {
877
Chris Lattner004659a2010-07-11 22:42:07 +0000878 if (isStartOfFunctionDefinition(D)) {
John McCalld8ac0572009-11-03 19:26:08 +0000879 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
880 Diag(Tok, diag::err_function_declared_typedef);
881
882 // Recover by treating the 'typedef' as spurious.
883 DS.ClearStorageClassSpecs();
884 }
885
John McCalld226f652010-08-21 09:40:31 +0000886 Decl *TheDecl = ParseFunctionDefinition(D);
John McCalld8ac0572009-11-03 19:26:08 +0000887 return Actions.ConvertDeclToDeclGroup(TheDecl);
Chris Lattner004659a2010-07-11 22:42:07 +0000888 }
889
890 if (isDeclarationSpecifier()) {
891 // If there is an invalid declaration specifier right after the function
892 // prototype, then we must be in a missing semicolon case where this isn't
893 // actually a body. Just fall through into the code that handles it as a
894 // prototype, and let the top-level code handle the erroneous declspec
895 // where it would otherwise expect a comma or semicolon.
John McCalld8ac0572009-11-03 19:26:08 +0000896 } else {
897 Diag(Tok, diag::err_expected_fn_body);
898 SkipUntil(tok::semi);
899 return DeclGroupPtrTy();
900 }
901 }
902
Richard Smithad762fc2011-04-14 22:09:26 +0000903 if (ParseAttributesAfterDeclarator(D))
904 return DeclGroupPtrTy();
905
906 // C++0x [stmt.iter]p1: Check if we have a for-range-declarator. If so, we
907 // must parse and analyze the for-range-initializer before the declaration is
908 // analyzed.
909 if (FRI && Tok.is(tok::colon)) {
910 FRI->ColonLoc = ConsumeToken();
Sebastian Redldbef1bb2011-06-05 12:23:16 +0000911 if (Tok.is(tok::l_brace))
912 FRI->RangeExpr = ParseBraceInitializer();
913 else
914 FRI->RangeExpr = ParseExpression();
Richard Smithad762fc2011-04-14 22:09:26 +0000915 Decl *ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
916 Actions.ActOnCXXForRangeDecl(ThisDecl);
917 Actions.FinalizeDeclaration(ThisDecl);
918 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS, &ThisDecl, 1);
919 }
920
Chris Lattner5f9e2722011-07-23 10:55:15 +0000921 SmallVector<Decl *, 8> DeclsInGroup;
Richard Smithad762fc2011-04-14 22:09:26 +0000922 Decl *FirstDecl = ParseDeclarationAfterDeclaratorAndAttributes(D);
John McCall54abf7d2009-11-04 02:18:39 +0000923 D.complete(FirstDecl);
John McCalld226f652010-08-21 09:40:31 +0000924 if (FirstDecl)
John McCalld8ac0572009-11-03 19:26:08 +0000925 DeclsInGroup.push_back(FirstDecl);
926
927 // If we don't have a comma, it is either the end of the list (a ';') or an
928 // error, bail out.
929 while (Tok.is(tok::comma)) {
930 // Consume the comma.
Chris Lattner23c4b182009-03-29 17:18:04 +0000931 ConsumeToken();
John McCalld8ac0572009-11-03 19:26:08 +0000932
933 // Parse the next declarator.
934 D.clear();
935
936 // Accept attributes in an init-declarator. In the first declarator in a
937 // declaration, these would be part of the declspec. In subsequent
938 // declarators, they become part of the declarator itself, so that they
939 // don't apply to declarators after *this* one. Examples:
940 // short __attribute__((common)) var; -> declspec
941 // short var __attribute__((common)); -> declarator
942 // short x, __attribute__((common)) var; -> declarator
John McCall7f040a92010-12-24 02:08:15 +0000943 MaybeParseGNUAttributes(D);
John McCalld8ac0572009-11-03 19:26:08 +0000944
945 ParseDeclarator(D);
946
John McCalld226f652010-08-21 09:40:31 +0000947 Decl *ThisDecl = ParseDeclarationAfterDeclarator(D);
John McCall54abf7d2009-11-04 02:18:39 +0000948 D.complete(ThisDecl);
John McCalld226f652010-08-21 09:40:31 +0000949 if (ThisDecl)
John McCalld8ac0572009-11-03 19:26:08 +0000950 DeclsInGroup.push_back(ThisDecl);
951 }
952
953 if (DeclEnd)
954 *DeclEnd = Tok.getLocation();
955
956 if (Context != Declarator::ForContext &&
957 ExpectAndConsume(tok::semi,
958 Context == Declarator::FileContext
959 ? diag::err_invalid_token_after_toplevel_declarator
960 : diag::err_expected_semi_declaration)) {
Chris Lattner004659a2010-07-11 22:42:07 +0000961 // Okay, there was no semicolon and one was expected. If we see a
962 // declaration specifier, just assume it was missing and continue parsing.
963 // Otherwise things are very confused and we skip to recover.
964 if (!isDeclarationSpecifier()) {
965 SkipUntil(tok::r_brace, true, true);
966 if (Tok.is(tok::semi))
967 ConsumeToken();
968 }
John McCalld8ac0572009-11-03 19:26:08 +0000969 }
970
Douglas Gregor23c94db2010-07-02 17:43:08 +0000971 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS,
John McCalld8ac0572009-11-03 19:26:08 +0000972 DeclsInGroup.data(),
973 DeclsInGroup.size());
Reid Spencer5f016e22007-07-11 17:01:13 +0000974}
975
Richard Smithad762fc2011-04-14 22:09:26 +0000976/// Parse an optional simple-asm-expr and attributes, and attach them to a
977/// declarator. Returns true on an error.
978bool Parser::ParseAttributesAfterDeclarator(Declarator &D) {
979 // If a simple-asm-expr is present, parse it.
980 if (Tok.is(tok::kw_asm)) {
981 SourceLocation Loc;
982 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
983 if (AsmLabel.isInvalid()) {
984 SkipUntil(tok::semi, true, true);
985 return true;
986 }
987
988 D.setAsmLabel(AsmLabel.release());
989 D.SetRangeEnd(Loc);
990 }
991
992 MaybeParseGNUAttributes(D);
993 return false;
994}
995
Douglas Gregor1426e532009-05-12 21:31:51 +0000996/// \brief Parse 'declaration' after parsing 'declaration-specifiers
997/// declarator'. This method parses the remainder of the declaration
998/// (including any attributes or initializer, among other things) and
999/// finalizes the declaration.
Reid Spencer5f016e22007-07-11 17:01:13 +00001000///
Reid Spencer5f016e22007-07-11 17:01:13 +00001001/// init-declarator: [C99 6.7]
1002/// declarator
1003/// declarator '=' initializer
1004/// [GNU] declarator simple-asm-expr[opt] attributes[opt]
1005/// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00001006/// [C++] declarator initializer[opt]
1007///
1008/// [C++] initializer:
1009/// [C++] '=' initializer-clause
1010/// [C++] '(' expression-list ')'
Sebastian Redl50de12f2009-03-24 22:27:57 +00001011/// [C++0x] '=' 'default' [TODO]
1012/// [C++0x] '=' 'delete'
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001013/// [C++0x] braced-init-list
Sebastian Redl50de12f2009-03-24 22:27:57 +00001014///
1015/// According to the standard grammar, =default and =delete are function
1016/// definitions, but that definitely doesn't fit with the parser here.
Reid Spencer5f016e22007-07-11 17:01:13 +00001017///
John McCalld226f652010-08-21 09:40:31 +00001018Decl *Parser::ParseDeclarationAfterDeclarator(Declarator &D,
Douglas Gregore542c862009-06-23 23:11:28 +00001019 const ParsedTemplateInfo &TemplateInfo) {
Richard Smithad762fc2011-04-14 22:09:26 +00001020 if (ParseAttributesAfterDeclarator(D))
1021 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001022
Richard Smithad762fc2011-04-14 22:09:26 +00001023 return ParseDeclarationAfterDeclaratorAndAttributes(D, TemplateInfo);
1024}
Mike Stump1eb44332009-09-09 15:08:12 +00001025
Richard Smithad762fc2011-04-14 22:09:26 +00001026Decl *Parser::ParseDeclarationAfterDeclaratorAndAttributes(Declarator &D,
1027 const ParsedTemplateInfo &TemplateInfo) {
Douglas Gregor1426e532009-05-12 21:31:51 +00001028 // Inform the current actions module that we just parsed this declarator.
John McCalld226f652010-08-21 09:40:31 +00001029 Decl *ThisDecl = 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +00001030 switch (TemplateInfo.Kind) {
1031 case ParsedTemplateInfo::NonTemplate:
Douglas Gregor23c94db2010-07-02 17:43:08 +00001032 ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
Douglas Gregord5a423b2009-09-25 18:43:00 +00001033 break;
1034
1035 case ParsedTemplateInfo::Template:
1036 case ParsedTemplateInfo::ExplicitSpecialization:
Douglas Gregor23c94db2010-07-02 17:43:08 +00001037 ThisDecl = Actions.ActOnTemplateDeclarator(getCurScope(),
John McCallf312b1e2010-08-26 23:41:50 +00001038 MultiTemplateParamsArg(Actions,
Douglas Gregore542c862009-06-23 23:11:28 +00001039 TemplateInfo.TemplateParams->data(),
1040 TemplateInfo.TemplateParams->size()),
Douglas Gregord5a423b2009-09-25 18:43:00 +00001041 D);
1042 break;
1043
1044 case ParsedTemplateInfo::ExplicitInstantiation: {
John McCalld226f652010-08-21 09:40:31 +00001045 DeclResult ThisRes
Douglas Gregor23c94db2010-07-02 17:43:08 +00001046 = Actions.ActOnExplicitInstantiation(getCurScope(),
Douglas Gregord5a423b2009-09-25 18:43:00 +00001047 TemplateInfo.ExternLoc,
1048 TemplateInfo.TemplateLoc,
1049 D);
1050 if (ThisRes.isInvalid()) {
1051 SkipUntil(tok::semi, true, true);
John McCalld226f652010-08-21 09:40:31 +00001052 return 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +00001053 }
1054
1055 ThisDecl = ThisRes.get();
1056 break;
1057 }
1058 }
Mike Stump1eb44332009-09-09 15:08:12 +00001059
Richard Smith34b41d92011-02-20 03:19:35 +00001060 bool TypeContainsAuto =
1061 D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto;
1062
Douglas Gregor1426e532009-05-12 21:31:51 +00001063 // Parse declarator '=' initializer.
Argyrios Kyrtzidisa6eb5f82010-10-08 02:39:23 +00001064 if (isTokenEqualOrMistypedEqualEqual(
1065 diag::err_invalid_equalequal_after_declarator)) {
Douglas Gregor1426e532009-05-12 21:31:51 +00001066 ConsumeToken();
Anders Carlsson37bf9d22010-09-24 21:25:25 +00001067 if (Tok.is(tok::kw_delete)) {
Sean Hunte4246a62011-05-12 06:15:49 +00001068 if (D.isFunctionDeclarator())
1069 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
1070 << 1 /* delete */;
1071 else
1072 Diag(ConsumeToken(), diag::err_deleted_non_function);
Sean Huntfe2695e2011-05-06 01:42:00 +00001073 } else if (Tok.is(tok::kw_default)) {
Sean Hunte4246a62011-05-12 06:15:49 +00001074 if (D.isFunctionDeclarator())
1075 Diag(Tok, diag::err_default_delete_in_multiple_declaration)
1076 << 1 /* delete */;
1077 else
1078 Diag(ConsumeToken(), diag::err_default_special_members);
Douglas Gregor1426e532009-05-12 21:31:51 +00001079 } else {
John McCall731ad842009-12-19 09:28:58 +00001080 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
1081 EnterScope(0);
Douglas Gregor23c94db2010-07-02 17:43:08 +00001082 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
John McCall731ad842009-12-19 09:28:58 +00001083 }
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +00001084
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +00001085 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001086 Actions.CodeCompleteInitializer(getCurScope(), ThisDecl);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001087 cutOffParsing();
1088 return 0;
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +00001089 }
1090
John McCall60d7b3a2010-08-24 06:29:42 +00001091 ExprResult Init(ParseInitializer());
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +00001092
John McCall731ad842009-12-19 09:28:58 +00001093 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001094 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
John McCall731ad842009-12-19 09:28:58 +00001095 ExitScope();
1096 }
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +00001097
Douglas Gregor1426e532009-05-12 21:31:51 +00001098 if (Init.isInvalid()) {
Douglas Gregor00225542010-03-01 18:27:54 +00001099 SkipUntil(tok::comma, true, true);
1100 Actions.ActOnInitializerError(ThisDecl);
1101 } else
Richard Smith34b41d92011-02-20 03:19:35 +00001102 Actions.AddInitializerToDecl(ThisDecl, Init.take(),
1103 /*DirectInit=*/false, TypeContainsAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +00001104 }
1105 } else if (Tok.is(tok::l_paren)) {
1106 // Parse C++ direct initializer: '(' expression-list ')'
1107 SourceLocation LParenLoc = ConsumeParen();
1108 ExprVector Exprs(Actions);
1109 CommaLocsTy CommaLocs;
1110
Douglas Gregorb4debae2009-12-22 17:47:17 +00001111 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
1112 EnterScope(0);
Douglas Gregor23c94db2010-07-02 17:43:08 +00001113 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001114 }
1115
Douglas Gregor1426e532009-05-12 21:31:51 +00001116 if (ParseExpressionList(Exprs, CommaLocs)) {
1117 SkipUntil(tok::r_paren);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001118
1119 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001120 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001121 ExitScope();
1122 }
Douglas Gregor1426e532009-05-12 21:31:51 +00001123 } else {
1124 // Match the ')'.
1125 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
1126
1127 assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() &&
1128 "Unexpected number of commas!");
Douglas Gregorb4debae2009-12-22 17:47:17 +00001129
1130 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001131 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001132 ExitScope();
1133 }
1134
Douglas Gregor1426e532009-05-12 21:31:51 +00001135 Actions.AddCXXDirectInitializerToDecl(ThisDecl, LParenLoc,
1136 move_arg(Exprs),
Richard Smith34b41d92011-02-20 03:19:35 +00001137 RParenLoc,
1138 TypeContainsAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +00001139 }
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001140 } else if (getLang().CPlusPlus0x && Tok.is(tok::l_brace)) {
1141 // Parse C++0x braced-init-list.
1142 if (D.getCXXScopeSpec().isSet()) {
1143 EnterScope(0);
1144 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
1145 }
1146
1147 ExprResult Init(ParseBraceInitializer());
1148
1149 if (D.getCXXScopeSpec().isSet()) {
1150 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
1151 ExitScope();
1152 }
1153
1154 if (Init.isInvalid()) {
1155 Actions.ActOnInitializerError(ThisDecl);
1156 } else
1157 Actions.AddInitializerToDecl(ThisDecl, Init.take(),
1158 /*DirectInit=*/true, TypeContainsAuto);
1159
Douglas Gregor1426e532009-05-12 21:31:51 +00001160 } else {
Richard Smith34b41d92011-02-20 03:19:35 +00001161 Actions.ActOnUninitializedDecl(ThisDecl, TypeContainsAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +00001162 }
1163
Richard Smith483b9f32011-02-21 20:05:19 +00001164 Actions.FinalizeDeclaration(ThisDecl);
1165
Douglas Gregor1426e532009-05-12 21:31:51 +00001166 return ThisDecl;
1167}
1168
Reid Spencer5f016e22007-07-11 17:01:13 +00001169/// ParseSpecifierQualifierList
1170/// specifier-qualifier-list:
1171/// type-specifier specifier-qualifier-list[opt]
1172/// type-qualifier specifier-qualifier-list[opt]
1173/// [GNU] attributes specifier-qualifier-list[opt]
1174///
Richard Smithc89edf52011-07-01 19:46:12 +00001175void Parser::ParseSpecifierQualifierList(DeclSpec &DS, AccessSpecifier AS) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001176 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
1177 /// parse declaration-specifiers and complain about extra stuff.
Richard Smithc89edf52011-07-01 19:46:12 +00001178 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS);
Mike Stump1eb44332009-09-09 15:08:12 +00001179
Reid Spencer5f016e22007-07-11 17:01:13 +00001180 // Validate declspec for type-name.
1181 unsigned Specs = DS.getParsedSpecifiers();
Chris Lattnerb6645dd2009-04-14 21:16:09 +00001182 if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers() &&
John McCall7f040a92010-12-24 02:08:15 +00001183 !DS.hasAttributes())
Reid Spencer5f016e22007-07-11 17:01:13 +00001184 Diag(Tok, diag::err_typename_requires_specqual);
Mike Stump1eb44332009-09-09 15:08:12 +00001185
Reid Spencer5f016e22007-07-11 17:01:13 +00001186 // Issue diagnostic and remove storage class if present.
1187 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
1188 if (DS.getStorageClassSpecLoc().isValid())
1189 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
1190 else
1191 Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass);
1192 DS.ClearStorageClassSpecs();
1193 }
Mike Stump1eb44332009-09-09 15:08:12 +00001194
Reid Spencer5f016e22007-07-11 17:01:13 +00001195 // Issue diagnostic and remove function specfier if present.
1196 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Douglas Gregorb48fe382008-10-31 09:07:45 +00001197 if (DS.isInlineSpecified())
1198 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
1199 if (DS.isVirtualSpecified())
1200 Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec);
1201 if (DS.isExplicitSpecified())
1202 Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec);
Reid Spencer5f016e22007-07-11 17:01:13 +00001203 DS.ClearFunctionSpecs();
1204 }
1205}
1206
Chris Lattnerc199ab32009-04-12 20:42:31 +00001207/// isValidAfterIdentifierInDeclaratorAfterDeclSpec - Return true if the
1208/// specified token is valid after the identifier in a declarator which
1209/// immediately follows the declspec. For example, these things are valid:
1210///
1211/// int x [ 4]; // direct-declarator
1212/// int x ( int y); // direct-declarator
1213/// int(int x ) // direct-declarator
1214/// int x ; // simple-declaration
1215/// int x = 17; // init-declarator-list
1216/// int x , y; // init-declarator-list
1217/// int x __asm__ ("foo"); // init-declarator-list
Chris Lattnerb6645dd2009-04-14 21:16:09 +00001218/// int x : 4; // struct-declarator
Chris Lattnerc83c27a2009-04-12 22:29:43 +00001219/// int x { 5}; // C++'0x unified initializers
Chris Lattnerc199ab32009-04-12 20:42:31 +00001220///
1221/// This is not, because 'x' does not immediately follow the declspec (though
1222/// ')' happens to be valid anyway).
1223/// int (x)
1224///
1225static bool isValidAfterIdentifierInDeclarator(const Token &T) {
1226 return T.is(tok::l_square) || T.is(tok::l_paren) || T.is(tok::r_paren) ||
1227 T.is(tok::semi) || T.is(tok::comma) || T.is(tok::equal) ||
Chris Lattnerb6645dd2009-04-14 21:16:09 +00001228 T.is(tok::kw_asm) || T.is(tok::l_brace) || T.is(tok::colon);
Chris Lattnerc199ab32009-04-12 20:42:31 +00001229}
1230
Chris Lattnere40c2952009-04-14 21:34:55 +00001231
1232/// ParseImplicitInt - This method is called when we have an non-typename
1233/// identifier in a declspec (which normally terminates the decl spec) when
1234/// the declspec has no type specifier. In this case, the declspec is either
1235/// malformed or is "implicit int" (in K&R and C89).
1236///
1237/// This method handles diagnosing this prettily and returns false if the
1238/// declspec is done being processed. If it recovers and thinks there may be
1239/// other pieces of declspec after it, it returns true.
1240///
Chris Lattnerf4382f52009-04-14 22:17:06 +00001241bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001242 const ParsedTemplateInfo &TemplateInfo,
Chris Lattnere40c2952009-04-14 21:34:55 +00001243 AccessSpecifier AS) {
Chris Lattnerf4382f52009-04-14 22:17:06 +00001244 assert(Tok.is(tok::identifier) && "should have identifier");
Mike Stump1eb44332009-09-09 15:08:12 +00001245
Chris Lattnere40c2952009-04-14 21:34:55 +00001246 SourceLocation Loc = Tok.getLocation();
1247 // If we see an identifier that is not a type name, we normally would
1248 // parse it as the identifer being declared. However, when a typename
1249 // is typo'd or the definition is not included, this will incorrectly
1250 // parse the typename as the identifier name and fall over misparsing
1251 // later parts of the diagnostic.
1252 //
1253 // As such, we try to do some look-ahead in cases where this would
1254 // otherwise be an "implicit-int" case to see if this is invalid. For
1255 // example: "static foo_t x = 4;" In this case, if we parsed foo_t as
1256 // an identifier with implicit int, we'd get a parse error because the
1257 // next token is obviously invalid for a type. Parse these as a case
1258 // with an invalid type specifier.
1259 assert(!DS.hasTypeSpecifier() && "Type specifier checked above");
Mike Stump1eb44332009-09-09 15:08:12 +00001260
Chris Lattnere40c2952009-04-14 21:34:55 +00001261 // Since we know that this either implicit int (which is rare) or an
1262 // error, we'd do lookahead to try to do better recovery.
1263 if (isValidAfterIdentifierInDeclarator(NextToken())) {
1264 // If this token is valid for implicit int, e.g. "static x = 4", then
1265 // we just avoid eating the identifier, so it will be parsed as the
1266 // identifier in the declarator.
1267 return false;
1268 }
Mike Stump1eb44332009-09-09 15:08:12 +00001269
Chris Lattnere40c2952009-04-14 21:34:55 +00001270 // Otherwise, if we don't consume this token, we are going to emit an
1271 // error anyway. Try to recover from various common problems. Check
1272 // to see if this was a reference to a tag name without a tag specified.
1273 // This is a common problem in C (saying 'foo' instead of 'struct foo').
Chris Lattnerf4382f52009-04-14 22:17:06 +00001274 //
1275 // C++ doesn't need this, and isTagName doesn't take SS.
1276 if (SS == 0) {
Argyrios Kyrtzidisb8a9d3b2011-04-21 17:29:47 +00001277 const char *TagName = 0, *FixitTagName = 0;
Chris Lattnerf4382f52009-04-14 22:17:06 +00001278 tok::TokenKind TagKind = tok::unknown;
Mike Stump1eb44332009-09-09 15:08:12 +00001279
Douglas Gregor23c94db2010-07-02 17:43:08 +00001280 switch (Actions.isTagName(*Tok.getIdentifierInfo(), getCurScope())) {
Chris Lattnere40c2952009-04-14 21:34:55 +00001281 default: break;
Argyrios Kyrtzidisb8a9d3b2011-04-21 17:29:47 +00001282 case DeclSpec::TST_enum:
1283 TagName="enum" ; FixitTagName = "enum " ; TagKind=tok::kw_enum ;break;
1284 case DeclSpec::TST_union:
1285 TagName="union" ; FixitTagName = "union " ;TagKind=tok::kw_union ;break;
1286 case DeclSpec::TST_struct:
1287 TagName="struct"; FixitTagName = "struct ";TagKind=tok::kw_struct;break;
1288 case DeclSpec::TST_class:
1289 TagName="class" ; FixitTagName = "class " ;TagKind=tok::kw_class ;break;
Chris Lattnere40c2952009-04-14 21:34:55 +00001290 }
Mike Stump1eb44332009-09-09 15:08:12 +00001291
Chris Lattnerf4382f52009-04-14 22:17:06 +00001292 if (TagName) {
1293 Diag(Loc, diag::err_use_of_tag_name_without_tag)
John McCall23e907a2010-02-14 01:03:10 +00001294 << Tok.getIdentifierInfo() << TagName << getLang().CPlusPlus
Argyrios Kyrtzidisb8a9d3b2011-04-21 17:29:47 +00001295 << FixItHint::CreateInsertion(Tok.getLocation(),FixitTagName);
Mike Stump1eb44332009-09-09 15:08:12 +00001296
Chris Lattnerf4382f52009-04-14 22:17:06 +00001297 // Parse this as a tag as if the missing tag were present.
1298 if (TagKind == tok::kw_enum)
Douglas Gregor9b9edd62010-03-02 17:53:14 +00001299 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS);
Chris Lattnerf4382f52009-04-14 22:17:06 +00001300 else
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001301 ParseClassSpecifier(TagKind, Loc, DS, TemplateInfo, AS);
Chris Lattnerf4382f52009-04-14 22:17:06 +00001302 return true;
1303 }
Chris Lattnere40c2952009-04-14 21:34:55 +00001304 }
Mike Stump1eb44332009-09-09 15:08:12 +00001305
Douglas Gregora786fdb2009-10-13 23:27:22 +00001306 // This is almost certainly an invalid type name. Let the action emit a
1307 // diagnostic and attempt to recover.
John McCallb3d87482010-08-24 05:47:05 +00001308 ParsedType T;
Douglas Gregora786fdb2009-10-13 23:27:22 +00001309 if (Actions.DiagnoseUnknownTypeName(*Tok.getIdentifierInfo(), Loc,
Douglas Gregor23c94db2010-07-02 17:43:08 +00001310 getCurScope(), SS, T)) {
Douglas Gregora786fdb2009-10-13 23:27:22 +00001311 // The action emitted a diagnostic, so we don't have to.
1312 if (T) {
1313 // The action has suggested that the type T could be used. Set that as
1314 // the type in the declaration specifiers, consume the would-be type
1315 // name token, and we're done.
1316 const char *PrevSpec;
1317 unsigned DiagID;
John McCallb3d87482010-08-24 05:47:05 +00001318 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID, T);
Douglas Gregora786fdb2009-10-13 23:27:22 +00001319 DS.SetRangeEnd(Tok.getLocation());
1320 ConsumeToken();
1321
1322 // There may be other declaration specifiers after this.
1323 return true;
1324 }
1325
1326 // Fall through; the action had no suggestion for us.
1327 } else {
1328 // The action did not emit a diagnostic, so emit one now.
1329 SourceRange R;
1330 if (SS) R = SS->getRange();
1331 Diag(Loc, diag::err_unknown_typename) << Tok.getIdentifierInfo() << R;
1332 }
Mike Stump1eb44332009-09-09 15:08:12 +00001333
Douglas Gregora786fdb2009-10-13 23:27:22 +00001334 // Mark this as an error.
Chris Lattnere40c2952009-04-14 21:34:55 +00001335 const char *PrevSpec;
John McCallfec54012009-08-03 20:12:06 +00001336 unsigned DiagID;
1337 DS.SetTypeSpecType(DeclSpec::TST_error, Loc, PrevSpec, DiagID);
Chris Lattnere40c2952009-04-14 21:34:55 +00001338 DS.SetRangeEnd(Tok.getLocation());
1339 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001340
Chris Lattnere40c2952009-04-14 21:34:55 +00001341 // TODO: Could inject an invalid typedef decl in an enclosing scope to
1342 // avoid rippling error messages on subsequent uses of the same type,
1343 // could be useful if #include was forgotten.
1344 return false;
1345}
1346
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001347/// \brief Determine the declaration specifier context from the declarator
1348/// context.
1349///
1350/// \param Context the declarator context, which is one of the
1351/// Declarator::TheContext enumerator values.
1352Parser::DeclSpecContext
1353Parser::getDeclSpecContextFromDeclaratorContext(unsigned Context) {
1354 if (Context == Declarator::MemberContext)
1355 return DSC_class;
1356 if (Context == Declarator::FileContext)
1357 return DSC_top_level;
1358 return DSC_normal;
1359}
1360
Reid Spencer5f016e22007-07-11 17:01:13 +00001361/// ParseDeclarationSpecifiers
1362/// declaration-specifiers: [C99 6.7]
1363/// storage-class-specifier declaration-specifiers[opt]
1364/// type-specifier declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00001365/// [C99] function-specifier declaration-specifiers[opt]
1366/// [GNU] attributes declaration-specifiers[opt]
1367///
1368/// storage-class-specifier: [C99 6.7.1]
1369/// 'typedef'
1370/// 'extern'
1371/// 'static'
1372/// 'auto'
1373/// 'register'
Sebastian Redl669d5d72008-11-14 23:42:31 +00001374/// [C++] 'mutable'
Reid Spencer5f016e22007-07-11 17:01:13 +00001375/// [GNU] '__thread'
Reid Spencer5f016e22007-07-11 17:01:13 +00001376/// function-specifier: [C99 6.7.4]
1377/// [C99] 'inline'
Douglas Gregorb48fe382008-10-31 09:07:45 +00001378/// [C++] 'virtual'
1379/// [C++] 'explicit'
Peter Collingbournef315fa82011-02-14 01:42:53 +00001380/// [OpenCL] '__kernel'
Anders Carlssonf47f7a12009-05-06 04:46:28 +00001381/// 'friend': [C++ dcl.friend]
Sebastian Redl2ac67232009-11-05 15:47:02 +00001382/// 'constexpr': [C++0x dcl.constexpr]
Anders Carlssonf47f7a12009-05-06 04:46:28 +00001383
Reid Spencer5f016e22007-07-11 17:01:13 +00001384///
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +00001385void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001386 const ParsedTemplateInfo &TemplateInfo,
John McCall67d1a672009-08-06 02:15:43 +00001387 AccessSpecifier AS,
Douglas Gregor312eadb2011-04-24 05:37:28 +00001388 DeclSpecContext DSContext) {
1389 if (DS.getSourceRange().isInvalid()) {
1390 DS.SetRangeStart(Tok.getLocation());
1391 DS.SetRangeEnd(Tok.getLocation());
1392 }
1393
Reid Spencer5f016e22007-07-11 17:01:13 +00001394 while (1) {
John McCallfec54012009-08-03 20:12:06 +00001395 bool isInvalid = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001396 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00001397 unsigned DiagID = 0;
1398
Reid Spencer5f016e22007-07-11 17:01:13 +00001399 SourceLocation Loc = Tok.getLocation();
Douglas Gregor12e083c2008-11-07 15:42:26 +00001400
Reid Spencer5f016e22007-07-11 17:01:13 +00001401 switch (Tok.getKind()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001402 default:
Chris Lattnerbce61352008-07-26 00:20:22 +00001403 DoneWithDeclSpec:
Reid Spencer5f016e22007-07-11 17:01:13 +00001404 // If this is not a declaration specifier token, we're done reading decl
1405 // specifiers. First verify that DeclSpec's are consistent.
Douglas Gregor9b3064b2009-04-01 22:41:11 +00001406 DS.Finish(Diags, PP);
Reid Spencer5f016e22007-07-11 17:01:13 +00001407 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001408
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001409 case tok::code_completion: {
John McCallf312b1e2010-08-26 23:41:50 +00001410 Sema::ParserCompletionContext CCC = Sema::PCC_Namespace;
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001411 if (DS.hasTypeSpecifier()) {
1412 bool AllowNonIdentifiers
1413 = (getCurScope()->getFlags() & (Scope::ControlScope |
1414 Scope::BlockScope |
1415 Scope::TemplateParamScope |
1416 Scope::FunctionPrototypeScope |
1417 Scope::AtCatchScope)) == 0;
1418 bool AllowNestedNameSpecifiers
1419 = DSContext == DSC_top_level ||
1420 (DSContext == DSC_class && DS.isFriendSpecified());
1421
Douglas Gregorc7b6d882010-09-16 15:14:18 +00001422 Actions.CodeCompleteDeclSpec(getCurScope(), DS,
1423 AllowNonIdentifiers,
1424 AllowNestedNameSpecifiers);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001425 return cutOffParsing();
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001426 }
1427
Douglas Gregor68e3c2e2011-02-15 20:33:25 +00001428 if (getCurScope()->getFnParent() || getCurScope()->getBlockParent())
1429 CCC = Sema::PCC_LocalDeclarationSpecifiers;
1430 else if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate)
John McCallf312b1e2010-08-26 23:41:50 +00001431 CCC = DSContext == DSC_class? Sema::PCC_MemberTemplate
1432 : Sema::PCC_Template;
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001433 else if (DSContext == DSC_class)
John McCallf312b1e2010-08-26 23:41:50 +00001434 CCC = Sema::PCC_Class;
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001435 else if (ObjCImpDecl)
John McCallf312b1e2010-08-26 23:41:50 +00001436 CCC = Sema::PCC_ObjCImplementation;
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001437
1438 Actions.CodeCompleteOrdinaryName(getCurScope(), CCC);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001439 return cutOffParsing();
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001440 }
1441
Chris Lattner5e02c472009-01-05 00:07:25 +00001442 case tok::coloncolon: // ::foo::bar
John McCall9ba61662010-02-26 08:45:28 +00001443 // C++ scope specifier. Annotate and loop, or bail out on error.
1444 if (TryAnnotateCXXScopeToken(true)) {
1445 if (!DS.hasTypeSpecifier())
1446 DS.SetTypeSpecError();
1447 goto DoneWithDeclSpec;
1448 }
John McCall2e0a7152010-03-01 18:20:46 +00001449 if (Tok.is(tok::coloncolon)) // ::new or ::delete
1450 goto DoneWithDeclSpec;
John McCall9ba61662010-02-26 08:45:28 +00001451 continue;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001452
1453 case tok::annot_cxxscope: {
1454 if (DS.hasTypeSpecifier())
1455 goto DoneWithDeclSpec;
1456
John McCallaa87d332009-12-12 11:40:51 +00001457 CXXScopeSpec SS;
Douglas Gregorc34348a2011-02-24 17:54:50 +00001458 Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(),
1459 Tok.getAnnotationRange(),
1460 SS);
John McCallaa87d332009-12-12 11:40:51 +00001461
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001462 // We are looking for a qualified typename.
Douglas Gregor9135c722009-03-25 15:40:00 +00001463 Token Next = NextToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001464 if (Next.is(tok::annot_template_id) &&
Douglas Gregor9135c722009-03-25 15:40:00 +00001465 static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue())
Douglas Gregorc45c2322009-03-31 00:43:58 +00001466 ->Kind == TNK_Type_template) {
Douglas Gregor9135c722009-03-25 15:40:00 +00001467 // We have a qualified template-id, e.g., N::A<int>
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001468
1469 // C++ [class.qual]p2:
1470 // In a lookup in which the constructor is an acceptable lookup
1471 // result and the nested-name-specifier nominates a class C:
1472 //
1473 // - if the name specified after the
1474 // nested-name-specifier, when looked up in C, is the
1475 // injected-class-name of C (Clause 9), or
1476 //
1477 // - if the name specified after the nested-name-specifier
1478 // is the same as the identifier or the
1479 // simple-template-id's template-name in the last
1480 // component of the nested-name-specifier,
1481 //
1482 // the name is instead considered to name the constructor of
1483 // class C.
1484 //
1485 // Thus, if the template-name is actually the constructor
1486 // name, then the code is ill-formed; this interpretation is
1487 // reinforced by the NAD status of core issue 635.
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +00001488 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Next);
John McCallba9d8532010-04-13 06:39:49 +00001489 if ((DSContext == DSC_top_level ||
1490 (DSContext == DSC_class && DS.isFriendSpecified())) &&
1491 TemplateId->Name &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00001492 Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) {
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001493 if (isConstructorDeclarator()) {
1494 // The user meant this to be an out-of-line constructor
1495 // definition, but template arguments are not allowed
1496 // there. Just allow this as a constructor; we'll
1497 // complain about it later.
1498 goto DoneWithDeclSpec;
1499 }
1500
1501 // The user meant this to name a type, but it actually names
1502 // a constructor with some extraneous template
1503 // arguments. Complain, then parse it as a type as the user
1504 // intended.
1505 Diag(TemplateId->TemplateNameLoc,
1506 diag::err_out_of_line_template_id_names_constructor)
1507 << TemplateId->Name;
1508 }
1509
John McCallaa87d332009-12-12 11:40:51 +00001510 DS.getTypeSpecScope() = SS;
1511 ConsumeToken(); // The C++ scope.
Mike Stump1eb44332009-09-09 15:08:12 +00001512 assert(Tok.is(tok::annot_template_id) &&
Douglas Gregor9135c722009-03-25 15:40:00 +00001513 "ParseOptionalCXXScopeSpecifier not working");
Douglas Gregor059101f2011-03-02 00:47:37 +00001514 AnnotateTemplateIdTokenAsType();
Douglas Gregor9135c722009-03-25 15:40:00 +00001515 continue;
1516 }
1517
Douglas Gregor9d7b3532009-09-28 07:26:33 +00001518 if (Next.is(tok::annot_typename)) {
John McCallaa87d332009-12-12 11:40:51 +00001519 DS.getTypeSpecScope() = SS;
1520 ConsumeToken(); // The C++ scope.
John McCallb3d87482010-08-24 05:47:05 +00001521 if (Tok.getAnnotationValue()) {
1522 ParsedType T = getTypeAnnotation(Tok);
Nico Weber253e80b2010-11-22 10:30:56 +00001523 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename,
1524 Tok.getAnnotationEndLoc(),
John McCallb3d87482010-08-24 05:47:05 +00001525 PrevSpec, DiagID, T);
1526 }
Douglas Gregor9d7b3532009-09-28 07:26:33 +00001527 else
1528 DS.SetTypeSpecError();
1529 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1530 ConsumeToken(); // The typename
1531 }
1532
Douglas Gregor9135c722009-03-25 15:40:00 +00001533 if (Next.isNot(tok::identifier))
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001534 goto DoneWithDeclSpec;
1535
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001536 // If we're in a context where the identifier could be a class name,
1537 // check whether this is a constructor declaration.
John McCallba9d8532010-04-13 06:39:49 +00001538 if ((DSContext == DSC_top_level ||
1539 (DSContext == DSC_class && DS.isFriendSpecified())) &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00001540 Actions.isCurrentClassName(*Next.getIdentifierInfo(), getCurScope(),
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001541 &SS)) {
1542 if (isConstructorDeclarator())
1543 goto DoneWithDeclSpec;
1544
1545 // As noted in C++ [class.qual]p2 (cited above), when the name
1546 // of the class is qualified in a context where it could name
1547 // a constructor, its a constructor name. However, we've
1548 // looked at the declarator, and the user probably meant this
1549 // to be a type. Complain that it isn't supposed to be treated
1550 // as a type, then proceed to parse it as a type.
1551 Diag(Next.getLocation(), diag::err_out_of_line_type_names_constructor)
1552 << Next.getIdentifierInfo();
1553 }
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001554
John McCallb3d87482010-08-24 05:47:05 +00001555 ParsedType TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(),
1556 Next.getLocation(),
Douglas Gregor9e876872011-03-01 18:12:44 +00001557 getCurScope(), &SS,
1558 false, false, ParsedType(),
1559 /*NonTrivialSourceInfo=*/true);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001560
Chris Lattnerf4382f52009-04-14 22:17:06 +00001561 // If the referenced identifier is not a type, then this declspec is
1562 // erroneous: We already checked about that it has no type specifier, and
1563 // C++ doesn't have implicit int. Diagnose it as a typo w.r.t. to the
Mike Stump1eb44332009-09-09 15:08:12 +00001564 // typename.
Chris Lattnerf4382f52009-04-14 22:17:06 +00001565 if (TypeRep == 0) {
1566 ConsumeToken(); // Eat the scope spec so the identifier is current.
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001567 if (ParseImplicitInt(DS, &SS, TemplateInfo, AS)) continue;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001568 goto DoneWithDeclSpec;
Chris Lattnerf4382f52009-04-14 22:17:06 +00001569 }
Mike Stump1eb44332009-09-09 15:08:12 +00001570
John McCallaa87d332009-12-12 11:40:51 +00001571 DS.getTypeSpecScope() = SS;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001572 ConsumeToken(); // The C++ scope.
1573
Douglas Gregor1a51b4a2009-02-09 15:09:02 +00001574 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001575 DiagID, TypeRep);
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001576 if (isInvalid)
1577 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001578
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001579 DS.SetRangeEnd(Tok.getLocation());
1580 ConsumeToken(); // The typename.
1581
1582 continue;
1583 }
Mike Stump1eb44332009-09-09 15:08:12 +00001584
Chris Lattner80d0c892009-01-21 19:48:37 +00001585 case tok::annot_typename: {
John McCallb3d87482010-08-24 05:47:05 +00001586 if (Tok.getAnnotationValue()) {
1587 ParsedType T = getTypeAnnotation(Tok);
Nico Weberc43271e2010-11-22 12:50:03 +00001588 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallb3d87482010-08-24 05:47:05 +00001589 DiagID, T);
1590 } else
Douglas Gregor31a19b62009-04-01 21:51:26 +00001591 DS.SetTypeSpecError();
Chris Lattner5c5db552010-04-05 18:18:31 +00001592
1593 if (isInvalid)
1594 break;
1595
Chris Lattner80d0c892009-01-21 19:48:37 +00001596 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1597 ConsumeToken(); // The typename
Mike Stump1eb44332009-09-09 15:08:12 +00001598
Chris Lattner80d0c892009-01-21 19:48:37 +00001599 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1600 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001601 // Objective-C interface.
1602 if (Tok.is(tok::less) && getLang().ObjC1)
1603 ParseObjCProtocolQualifiers(DS);
1604
Chris Lattner80d0c892009-01-21 19:48:37 +00001605 continue;
1606 }
Mike Stump1eb44332009-09-09 15:08:12 +00001607
Douglas Gregorbfad9152011-04-28 15:48:45 +00001608 case tok::kw___is_signed:
1609 // GNU libstdc++ 4.4 uses __is_signed as an identifier, but Clang
1610 // typically treats it as a trait. If we see __is_signed as it appears
1611 // in libstdc++, e.g.,
1612 //
1613 // static const bool __is_signed;
1614 //
1615 // then treat __is_signed as an identifier rather than as a keyword.
1616 if (DS.getTypeSpecType() == TST_bool &&
1617 DS.getTypeQualifiers() == DeclSpec::TQ_const &&
1618 DS.getStorageClassSpec() == DeclSpec::SCS_static) {
1619 Tok.getIdentifierInfo()->RevertTokenIDToIdentifier();
1620 Tok.setKind(tok::identifier);
1621 }
1622
1623 // We're done with the declaration-specifiers.
1624 goto DoneWithDeclSpec;
1625
Chris Lattner3bd934a2008-07-26 01:18:38 +00001626 // typedef-name
1627 case tok::identifier: {
Chris Lattner5e02c472009-01-05 00:07:25 +00001628 // In C++, check to see if this is a scope specifier like foo::bar::, if
1629 // so handle it as such. This is important for ctor parsing.
John McCall9ba61662010-02-26 08:45:28 +00001630 if (getLang().CPlusPlus) {
1631 if (TryAnnotateCXXScopeToken(true)) {
1632 if (!DS.hasTypeSpecifier())
1633 DS.SetTypeSpecError();
1634 goto DoneWithDeclSpec;
1635 }
1636 if (!Tok.is(tok::identifier))
1637 continue;
1638 }
Mike Stump1eb44332009-09-09 15:08:12 +00001639
Chris Lattner3bd934a2008-07-26 01:18:38 +00001640 // This identifier can only be a typedef name if we haven't already seen
1641 // a type-specifier. Without this check we misparse:
1642 // typedef int X; struct Y { short X; }; as 'short int'.
1643 if (DS.hasTypeSpecifier())
1644 goto DoneWithDeclSpec;
Mike Stump1eb44332009-09-09 15:08:12 +00001645
John Thompson82287d12010-02-05 00:12:22 +00001646 // Check for need to substitute AltiVec keyword tokens.
1647 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
1648 break;
1649
Chris Lattner3bd934a2008-07-26 01:18:38 +00001650 // It has to be available as a typedef too!
John McCallb3d87482010-08-24 05:47:05 +00001651 ParsedType TypeRep =
1652 Actions.getTypeName(*Tok.getIdentifierInfo(),
1653 Tok.getLocation(), getCurScope());
Douglas Gregor55f6b142009-02-09 18:46:07 +00001654
Chris Lattnerc199ab32009-04-12 20:42:31 +00001655 // If this is not a typedef name, don't parse it as part of the declspec,
1656 // it must be an implicit int or an error.
John McCallb3d87482010-08-24 05:47:05 +00001657 if (!TypeRep) {
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001658 if (ParseImplicitInt(DS, 0, TemplateInfo, AS)) continue;
Chris Lattner3bd934a2008-07-26 01:18:38 +00001659 goto DoneWithDeclSpec;
Chris Lattnerc199ab32009-04-12 20:42:31 +00001660 }
Douglas Gregor55f6b142009-02-09 18:46:07 +00001661
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001662 // If we're in a context where the identifier could be a class name,
1663 // check whether this is a constructor declaration.
1664 if (getLang().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00001665 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), getCurScope()) &&
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001666 isConstructorDeclarator())
Douglas Gregorb48fe382008-10-31 09:07:45 +00001667 goto DoneWithDeclSpec;
1668
Douglas Gregor1a51b4a2009-02-09 15:09:02 +00001669 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001670 DiagID, TypeRep);
Chris Lattner3bd934a2008-07-26 01:18:38 +00001671 if (isInvalid)
1672 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001673
Chris Lattner3bd934a2008-07-26 01:18:38 +00001674 DS.SetRangeEnd(Tok.getLocation());
1675 ConsumeToken(); // The identifier
1676
1677 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1678 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001679 // Objective-C interface.
1680 if (Tok.is(tok::less) && getLang().ObjC1)
1681 ParseObjCProtocolQualifiers(DS);
1682
Steve Naroff4f9b9f12008-09-22 10:28:57 +00001683 // Need to support trailing type qualifiers (e.g. "id<p> const").
1684 // If a type specifier follows, it will be diagnosed elsewhere.
1685 continue;
Chris Lattner3bd934a2008-07-26 01:18:38 +00001686 }
Douglas Gregor39a8de12009-02-25 19:37:18 +00001687
1688 // type-name
1689 case tok::annot_template_id: {
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +00001690 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregorc45c2322009-03-31 00:43:58 +00001691 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor39a8de12009-02-25 19:37:18 +00001692 // This template-id does not refer to a type name, so we're
1693 // done with the type-specifiers.
1694 goto DoneWithDeclSpec;
1695 }
1696
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001697 // If we're in a context where the template-id could be a
1698 // constructor name or specialization, check whether this is a
1699 // constructor declaration.
1700 if (getLang().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00001701 Actions.isCurrentClassName(*TemplateId->Name, getCurScope()) &&
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001702 isConstructorDeclarator())
1703 goto DoneWithDeclSpec;
1704
Douglas Gregor39a8de12009-02-25 19:37:18 +00001705 // Turn the template-id annotation token into a type annotation
1706 // token, then try again to parse it as a type-specifier.
Douglas Gregor31a19b62009-04-01 21:51:26 +00001707 AnnotateTemplateIdTokenAsType();
Douglas Gregor39a8de12009-02-25 19:37:18 +00001708 continue;
1709 }
1710
Reid Spencer5f016e22007-07-11 17:01:13 +00001711 // GNU attributes support.
1712 case tok::kw___attribute:
John McCall7f040a92010-12-24 02:08:15 +00001713 ParseGNUAttributes(DS.getAttributes());
Reid Spencer5f016e22007-07-11 17:01:13 +00001714 continue;
Steve Narofff59e17e2008-12-24 20:59:21 +00001715
1716 // Microsoft declspec support.
1717 case tok::kw___declspec:
John McCall7f040a92010-12-24 02:08:15 +00001718 ParseMicrosoftDeclSpec(DS.getAttributes());
Steve Narofff59e17e2008-12-24 20:59:21 +00001719 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001720
Steve Naroff239f0732008-12-25 14:16:32 +00001721 // Microsoft single token adornments.
Steve Naroff86bc6cf2008-12-25 14:41:26 +00001722 case tok::kw___forceinline:
Eli Friedman290eeb02009-06-08 23:27:34 +00001723 // FIXME: Add handling here!
1724 break;
1725
1726 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00001727 case tok::kw___ptr32:
Steve Naroff86bc6cf2008-12-25 14:41:26 +00001728 case tok::kw___w64:
Steve Naroff239f0732008-12-25 14:16:32 +00001729 case tok::kw___cdecl:
1730 case tok::kw___stdcall:
1731 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00001732 case tok::kw___thiscall:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00001733 case tok::kw___unaligned:
John McCall7f040a92010-12-24 02:08:15 +00001734 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman290eeb02009-06-08 23:27:34 +00001735 continue;
1736
Dawn Perchik52fc3142010-09-03 01:29:35 +00001737 // Borland single token adornments.
1738 case tok::kw___pascal:
John McCall7f040a92010-12-24 02:08:15 +00001739 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik52fc3142010-09-03 01:29:35 +00001740 continue;
1741
Peter Collingbournef315fa82011-02-14 01:42:53 +00001742 // OpenCL single token adornments.
1743 case tok::kw___kernel:
1744 ParseOpenCLAttributes(DS.getAttributes());
1745 continue;
1746
Reid Spencer5f016e22007-07-11 17:01:13 +00001747 // storage-class-specifier
1748 case tok::kw_typedef:
John McCallfec54012009-08-03 20:12:06 +00001749 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_typedef, Loc, PrevSpec,
Peter Collingbournee2f82f72011-02-11 19:59:54 +00001750 DiagID, getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00001751 break;
1752 case tok::kw_extern:
1753 if (DS.isThreadSpecified())
Chris Lattner1ab3b962008-11-18 07:48:38 +00001754 Diag(Tok, diag::ext_thread_before) << "extern";
John McCallfec54012009-08-03 20:12:06 +00001755 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_extern, Loc, PrevSpec,
Peter Collingbournee2f82f72011-02-11 19:59:54 +00001756 DiagID, getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00001757 break;
Steve Naroff8d54bf22007-12-18 00:16:02 +00001758 case tok::kw___private_extern__:
Chris Lattnerf97409f2008-04-06 06:57:35 +00001759 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_private_extern, Loc,
Peter Collingbournee2f82f72011-02-11 19:59:54 +00001760 PrevSpec, DiagID, getLang());
Steve Naroff8d54bf22007-12-18 00:16:02 +00001761 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00001762 case tok::kw_static:
1763 if (DS.isThreadSpecified())
Chris Lattner1ab3b962008-11-18 07:48:38 +00001764 Diag(Tok, diag::ext_thread_before) << "static";
John McCallfec54012009-08-03 20:12:06 +00001765 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_static, Loc, PrevSpec,
Peter Collingbournee2f82f72011-02-11 19:59:54 +00001766 DiagID, getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00001767 break;
1768 case tok::kw_auto:
Douglas Gregor18d8b792011-03-14 21:43:30 +00001769 if (getLang().CPlusPlus0x) {
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00001770 if (isKnownToBeTypeSpecifier(GetLookAheadToken(1))) {
1771 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, Loc, PrevSpec,
1772 DiagID, getLang());
1773 if (!isInvalid)
1774 Diag(Tok, diag::auto_storage_class)
1775 << FixItHint::CreateRemoval(DS.getStorageClassSpecLoc());
1776 }
1777 else
1778 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec,
1779 DiagID);
1780 }
Anders Carlssone89d1592009-06-26 18:41:36 +00001781 else
John McCallfec54012009-08-03 20:12:06 +00001782 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, Loc, PrevSpec,
Peter Collingbournee2f82f72011-02-11 19:59:54 +00001783 DiagID, getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00001784 break;
1785 case tok::kw_register:
John McCallfec54012009-08-03 20:12:06 +00001786 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_register, Loc, PrevSpec,
Peter Collingbournee2f82f72011-02-11 19:59:54 +00001787 DiagID, getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00001788 break;
Sebastian Redl669d5d72008-11-14 23:42:31 +00001789 case tok::kw_mutable:
John McCallfec54012009-08-03 20:12:06 +00001790 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_mutable, Loc, PrevSpec,
Peter Collingbournee2f82f72011-02-11 19:59:54 +00001791 DiagID, getLang());
Sebastian Redl669d5d72008-11-14 23:42:31 +00001792 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00001793 case tok::kw___thread:
John McCallfec54012009-08-03 20:12:06 +00001794 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001795 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001796
Reid Spencer5f016e22007-07-11 17:01:13 +00001797 // function-specifier
1798 case tok::kw_inline:
John McCallfec54012009-08-03 20:12:06 +00001799 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001800 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +00001801 case tok::kw_virtual:
John McCallfec54012009-08-03 20:12:06 +00001802 isInvalid = DS.SetFunctionSpecVirtual(Loc, PrevSpec, DiagID);
Douglas Gregorb48fe382008-10-31 09:07:45 +00001803 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +00001804 case tok::kw_explicit:
John McCallfec54012009-08-03 20:12:06 +00001805 isInvalid = DS.SetFunctionSpecExplicit(Loc, PrevSpec, DiagID);
Douglas Gregorb48fe382008-10-31 09:07:45 +00001806 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00001807
Anders Carlssonf47f7a12009-05-06 04:46:28 +00001808 // friend
1809 case tok::kw_friend:
John McCall67d1a672009-08-06 02:15:43 +00001810 if (DSContext == DSC_class)
1811 isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID);
1812 else {
1813 PrevSpec = ""; // not actually used by the diagnostic
1814 DiagID = diag::err_friend_invalid_in_context;
1815 isInvalid = true;
1816 }
Anders Carlssonf47f7a12009-05-06 04:46:28 +00001817 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001818
Sebastian Redl2ac67232009-11-05 15:47:02 +00001819 // constexpr
1820 case tok::kw_constexpr:
1821 isInvalid = DS.SetConstexprSpec(Loc, PrevSpec, DiagID);
1822 break;
1823
Chris Lattner80d0c892009-01-21 19:48:37 +00001824 // type-specifier
1825 case tok::kw_short:
John McCallfec54012009-08-03 20:12:06 +00001826 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec,
1827 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001828 break;
1829 case tok::kw_long:
1830 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCallfec54012009-08-03 20:12:06 +00001831 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
1832 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001833 else
John McCallfec54012009-08-03 20:12:06 +00001834 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
1835 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001836 break;
Francois Pichet338d7f72011-04-28 01:59:37 +00001837 case tok::kw___int64:
1838 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
1839 DiagID);
1840 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00001841 case tok::kw_signed:
John McCallfec54012009-08-03 20:12:06 +00001842 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec,
1843 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001844 break;
1845 case tok::kw_unsigned:
John McCallfec54012009-08-03 20:12:06 +00001846 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
1847 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001848 break;
1849 case tok::kw__Complex:
John McCallfec54012009-08-03 20:12:06 +00001850 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
1851 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001852 break;
1853 case tok::kw__Imaginary:
John McCallfec54012009-08-03 20:12:06 +00001854 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
1855 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001856 break;
1857 case tok::kw_void:
John McCallfec54012009-08-03 20:12:06 +00001858 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec,
1859 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001860 break;
1861 case tok::kw_char:
John McCallfec54012009-08-03 20:12:06 +00001862 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec,
1863 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001864 break;
1865 case tok::kw_int:
John McCallfec54012009-08-03 20:12:06 +00001866 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec,
1867 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001868 break;
1869 case tok::kw_float:
John McCallfec54012009-08-03 20:12:06 +00001870 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec,
1871 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001872 break;
1873 case tok::kw_double:
John McCallfec54012009-08-03 20:12:06 +00001874 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec,
1875 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001876 break;
1877 case tok::kw_wchar_t:
John McCallfec54012009-08-03 20:12:06 +00001878 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec,
1879 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001880 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001881 case tok::kw_char16_t:
John McCallfec54012009-08-03 20:12:06 +00001882 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec,
1883 DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001884 break;
1885 case tok::kw_char32_t:
John McCallfec54012009-08-03 20:12:06 +00001886 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec,
1887 DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001888 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00001889 case tok::kw_bool:
1890 case tok::kw__Bool:
Argyrios Kyrtzidis4383e182010-11-16 18:18:13 +00001891 if (Tok.is(tok::kw_bool) &&
1892 DS.getTypeSpecType() != DeclSpec::TST_unspecified &&
1893 DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
1894 PrevSpec = ""; // Not used by the diagnostic.
1895 DiagID = diag::err_bool_redeclaration;
Fariborz Jahaniane106a0b2011-04-19 21:42:37 +00001896 // For better error recovery.
1897 Tok.setKind(tok::identifier);
Argyrios Kyrtzidis4383e182010-11-16 18:18:13 +00001898 isInvalid = true;
1899 } else {
1900 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec,
1901 DiagID);
1902 }
Chris Lattner80d0c892009-01-21 19:48:37 +00001903 break;
1904 case tok::kw__Decimal32:
John McCallfec54012009-08-03 20:12:06 +00001905 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
1906 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001907 break;
1908 case tok::kw__Decimal64:
John McCallfec54012009-08-03 20:12:06 +00001909 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
1910 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001911 break;
1912 case tok::kw__Decimal128:
John McCallfec54012009-08-03 20:12:06 +00001913 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
1914 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001915 break;
John Thompson82287d12010-02-05 00:12:22 +00001916 case tok::kw___vector:
1917 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
1918 break;
1919 case tok::kw___pixel:
1920 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
1921 break;
John McCalla5fc4722011-04-09 22:50:59 +00001922 case tok::kw___unknown_anytype:
1923 isInvalid = DS.SetTypeSpecType(TST_unknown_anytype, Loc,
1924 PrevSpec, DiagID);
1925 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00001926
1927 // class-specifier:
1928 case tok::kw_class:
1929 case tok::kw_struct:
Chris Lattner4c97d762009-04-12 21:49:30 +00001930 case tok::kw_union: {
1931 tok::TokenKind Kind = Tok.getKind();
1932 ConsumeToken();
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001933 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS);
Chris Lattner80d0c892009-01-21 19:48:37 +00001934 continue;
Chris Lattner4c97d762009-04-12 21:49:30 +00001935 }
Chris Lattner80d0c892009-01-21 19:48:37 +00001936
1937 // enum-specifier:
1938 case tok::kw_enum:
Chris Lattner4c97d762009-04-12 21:49:30 +00001939 ConsumeToken();
Douglas Gregor9b9edd62010-03-02 17:53:14 +00001940 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS);
Chris Lattner80d0c892009-01-21 19:48:37 +00001941 continue;
1942
1943 // cv-qualifier:
1944 case tok::kw_const:
John McCallfec54012009-08-03 20:12:06 +00001945 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec, DiagID,
1946 getLang());
Chris Lattner80d0c892009-01-21 19:48:37 +00001947 break;
1948 case tok::kw_volatile:
John McCallfec54012009-08-03 20:12:06 +00001949 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
1950 getLang());
Chris Lattner80d0c892009-01-21 19:48:37 +00001951 break;
1952 case tok::kw_restrict:
John McCallfec54012009-08-03 20:12:06 +00001953 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
1954 getLang());
Chris Lattner80d0c892009-01-21 19:48:37 +00001955 break;
1956
Douglas Gregord57959a2009-03-27 23:10:48 +00001957 // C++ typename-specifier:
1958 case tok::kw_typename:
John McCall9ba61662010-02-26 08:45:28 +00001959 if (TryAnnotateTypeOrScopeToken()) {
1960 DS.SetTypeSpecError();
1961 goto DoneWithDeclSpec;
1962 }
1963 if (!Tok.is(tok::kw_typename))
Douglas Gregord57959a2009-03-27 23:10:48 +00001964 continue;
1965 break;
1966
Chris Lattner80d0c892009-01-21 19:48:37 +00001967 // GNU typeof support.
1968 case tok::kw_typeof:
1969 ParseTypeofSpecifier(DS);
1970 continue;
1971
Anders Carlsson6fd634f2009-06-24 17:47:40 +00001972 case tok::kw_decltype:
1973 ParseDecltypeSpecifier(DS);
1974 continue;
1975
Sean Huntdb5d44b2011-05-19 05:37:45 +00001976 case tok::kw___underlying_type:
1977 ParseUnderlyingTypeSpecifier(DS);
1978
Peter Collingbourne207f4d82011-03-18 22:38:29 +00001979 // OpenCL qualifiers:
1980 case tok::kw_private:
1981 if (!getLang().OpenCL)
1982 goto DoneWithDeclSpec;
1983 case tok::kw___private:
1984 case tok::kw___global:
1985 case tok::kw___local:
1986 case tok::kw___constant:
1987 case tok::kw___read_only:
1988 case tok::kw___write_only:
1989 case tok::kw___read_write:
1990 ParseOpenCLQualifiers(DS);
1991 break;
1992
Steve Naroffd3ded1f2008-06-05 00:02:44 +00001993 case tok::less:
Chris Lattner3bd934a2008-07-26 01:18:38 +00001994 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattnerbce61352008-07-26 00:20:22 +00001995 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
1996 // but we support it.
Chris Lattner3bd934a2008-07-26 01:18:38 +00001997 if (DS.hasTypeSpecifier() || !getLang().ObjC1)
Chris Lattnerbce61352008-07-26 00:20:22 +00001998 goto DoneWithDeclSpec;
Mike Stump1eb44332009-09-09 15:08:12 +00001999
Douglas Gregor46f936e2010-11-19 17:10:50 +00002000 if (!ParseObjCProtocolQualifiers(DS))
2001 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
2002 << FixItHint::CreateInsertion(Loc, "id")
2003 << SourceRange(Loc, DS.getSourceRange().getEnd());
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00002004
2005 // Need to support trailing type qualifiers (e.g. "id<p> const").
2006 // If a type specifier follows, it will be diagnosed elsewhere.
2007 continue;
Reid Spencer5f016e22007-07-11 17:01:13 +00002008 }
John McCallfec54012009-08-03 20:12:06 +00002009 // If the specifier wasn't legal, issue a diagnostic.
Reid Spencer5f016e22007-07-11 17:01:13 +00002010 if (isInvalid) {
2011 assert(PrevSpec && "Method did not return previous specifier!");
John McCallfec54012009-08-03 20:12:06 +00002012 assert(DiagID);
Douglas Gregorae2fb142010-08-23 14:34:43 +00002013
2014 if (DiagID == diag::ext_duplicate_declspec)
2015 Diag(Tok, DiagID)
2016 << PrevSpec << FixItHint::CreateRemoval(Tok.getLocation());
2017 else
2018 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00002019 }
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002020
Chris Lattner81c018d2008-03-13 06:29:04 +00002021 DS.SetRangeEnd(Tok.getLocation());
Fariborz Jahaniane106a0b2011-04-19 21:42:37 +00002022 if (DiagID != diag::err_bool_redeclaration)
2023 ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00002024 }
2025}
Douglas Gregoradcac882008-12-01 23:54:00 +00002026
Chris Lattner7a0ab5f2009-01-06 06:59:53 +00002027/// ParseOptionalTypeSpecifier - Try to parse a single type-specifier. We
Douglas Gregor12e083c2008-11-07 15:42:26 +00002028/// primarily follow the C++ grammar with additions for C99 and GNU,
2029/// which together subsume the C grammar. Note that the C++
2030/// type-specifier also includes the C type-qualifier (for const,
2031/// volatile, and C99 restrict). Returns true if a type-specifier was
2032/// found (and parsed), false otherwise.
2033///
2034/// type-specifier: [C++ 7.1.5]
2035/// simple-type-specifier
2036/// class-specifier
2037/// enum-specifier
2038/// elaborated-type-specifier [TODO]
2039/// cv-qualifier
2040///
2041/// cv-qualifier: [C++ 7.1.5.1]
2042/// 'const'
2043/// 'volatile'
2044/// [C99] 'restrict'
2045///
2046/// simple-type-specifier: [ C++ 7.1.5.2]
2047/// '::'[opt] nested-name-specifier[opt] type-name [TODO]
2048/// '::'[opt] nested-name-specifier 'template' template-id [TODO]
2049/// 'char'
2050/// 'wchar_t'
2051/// 'bool'
2052/// 'short'
2053/// 'int'
2054/// 'long'
2055/// 'signed'
2056/// 'unsigned'
2057/// 'float'
2058/// 'double'
2059/// 'void'
2060/// [C99] '_Bool'
2061/// [C99] '_Complex'
2062/// [C99] '_Imaginary' // Removed in TC2?
2063/// [GNU] '_Decimal32'
2064/// [GNU] '_Decimal64'
2065/// [GNU] '_Decimal128'
2066/// [GNU] typeof-specifier
2067/// [OBJC] class-name objc-protocol-refs[opt] [TODO]
2068/// [OBJC] typedef-name objc-protocol-refs[opt] [TODO]
Anders Carlsson6fd634f2009-06-24 17:47:40 +00002069/// [C++0x] 'decltype' ( expression )
John Thompson82287d12010-02-05 00:12:22 +00002070/// [AltiVec] '__vector'
John McCallfec54012009-08-03 20:12:06 +00002071bool Parser::ParseOptionalTypeSpecifier(DeclSpec &DS, bool& isInvalid,
Chris Lattner7a0ab5f2009-01-06 06:59:53 +00002072 const char *&PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00002073 unsigned &DiagID,
Sebastian Redld9bafa72010-02-03 21:21:43 +00002074 const ParsedTemplateInfo &TemplateInfo,
2075 bool SuppressDeclarations) {
Douglas Gregor12e083c2008-11-07 15:42:26 +00002076 SourceLocation Loc = Tok.getLocation();
2077
2078 switch (Tok.getKind()) {
Chris Lattner166a8fc2009-01-04 23:41:41 +00002079 case tok::identifier: // foo::bar
Douglas Gregorc0b39642010-04-15 23:40:53 +00002080 // If we already have a type specifier, this identifier is not a type.
2081 if (DS.getTypeSpecType() != DeclSpec::TST_unspecified ||
2082 DS.getTypeSpecWidth() != DeclSpec::TSW_unspecified ||
2083 DS.getTypeSpecSign() != DeclSpec::TSS_unspecified)
2084 return false;
John Thompson82287d12010-02-05 00:12:22 +00002085 // Check for need to substitute AltiVec keyword tokens.
2086 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
2087 break;
2088 // Fall through.
Douglas Gregord57959a2009-03-27 23:10:48 +00002089 case tok::kw_typename: // typename foo::bar
Chris Lattner166a8fc2009-01-04 23:41:41 +00002090 // Annotate typenames and C++ scope specifiers. If we get one, just
2091 // recurse to handle whatever we get.
2092 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00002093 return true;
2094 if (Tok.is(tok::identifier))
2095 return false;
2096 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
2097 TemplateInfo, SuppressDeclarations);
Chris Lattner166a8fc2009-01-04 23:41:41 +00002098 case tok::coloncolon: // ::foo::bar
2099 if (NextToken().is(tok::kw_new) || // ::new
2100 NextToken().is(tok::kw_delete)) // ::delete
2101 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002102
Chris Lattner166a8fc2009-01-04 23:41:41 +00002103 // Annotate typenames and C++ scope specifiers. If we get one, just
2104 // recurse to handle whatever we get.
2105 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00002106 return true;
2107 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
2108 TemplateInfo, SuppressDeclarations);
Mike Stump1eb44332009-09-09 15:08:12 +00002109
Douglas Gregor12e083c2008-11-07 15:42:26 +00002110 // simple-type-specifier:
Chris Lattnerb31757b2009-01-06 05:06:21 +00002111 case tok::annot_typename: {
John McCallb3d87482010-08-24 05:47:05 +00002112 if (ParsedType T = getTypeAnnotation(Tok)) {
Nico Weber253e80b2010-11-22 10:30:56 +00002113 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename,
2114 Tok.getAnnotationEndLoc(), PrevSpec,
John McCallb3d87482010-08-24 05:47:05 +00002115 DiagID, T);
2116 } else
Douglas Gregor31a19b62009-04-01 21:51:26 +00002117 DS.SetTypeSpecError();
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002118 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
2119 ConsumeToken(); // The typename
Mike Stump1eb44332009-09-09 15:08:12 +00002120
Douglas Gregor12e083c2008-11-07 15:42:26 +00002121 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
2122 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
2123 // Objective-C interface. If we don't have Objective-C or a '<', this is
2124 // just a normal reference to a typedef name.
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00002125 if (Tok.is(tok::less) && getLang().ObjC1)
2126 ParseObjCProtocolQualifiers(DS);
2127
Douglas Gregor12e083c2008-11-07 15:42:26 +00002128 return true;
2129 }
2130
2131 case tok::kw_short:
John McCallfec54012009-08-03 20:12:06 +00002132 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002133 break;
2134 case tok::kw_long:
2135 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCallfec54012009-08-03 20:12:06 +00002136 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
2137 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002138 else
John McCallfec54012009-08-03 20:12:06 +00002139 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2140 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002141 break;
Francois Pichet338d7f72011-04-28 01:59:37 +00002142 case tok::kw___int64:
2143 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2144 DiagID);
2145 break;
Douglas Gregor12e083c2008-11-07 15:42:26 +00002146 case tok::kw_signed:
John McCallfec54012009-08-03 20:12:06 +00002147 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002148 break;
2149 case tok::kw_unsigned:
John McCallfec54012009-08-03 20:12:06 +00002150 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
2151 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002152 break;
2153 case tok::kw__Complex:
John McCallfec54012009-08-03 20:12:06 +00002154 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
2155 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002156 break;
2157 case tok::kw__Imaginary:
John McCallfec54012009-08-03 20:12:06 +00002158 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
2159 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002160 break;
2161 case tok::kw_void:
John McCallfec54012009-08-03 20:12:06 +00002162 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002163 break;
2164 case tok::kw_char:
John McCallfec54012009-08-03 20:12:06 +00002165 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002166 break;
2167 case tok::kw_int:
John McCallfec54012009-08-03 20:12:06 +00002168 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002169 break;
2170 case tok::kw_float:
John McCallfec54012009-08-03 20:12:06 +00002171 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002172 break;
2173 case tok::kw_double:
John McCallfec54012009-08-03 20:12:06 +00002174 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002175 break;
2176 case tok::kw_wchar_t:
John McCallfec54012009-08-03 20:12:06 +00002177 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002178 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002179 case tok::kw_char16_t:
John McCallfec54012009-08-03 20:12:06 +00002180 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002181 break;
2182 case tok::kw_char32_t:
John McCallfec54012009-08-03 20:12:06 +00002183 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002184 break;
Douglas Gregor12e083c2008-11-07 15:42:26 +00002185 case tok::kw_bool:
2186 case tok::kw__Bool:
John McCallfec54012009-08-03 20:12:06 +00002187 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002188 break;
2189 case tok::kw__Decimal32:
John McCallfec54012009-08-03 20:12:06 +00002190 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
2191 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002192 break;
2193 case tok::kw__Decimal64:
John McCallfec54012009-08-03 20:12:06 +00002194 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
2195 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002196 break;
2197 case tok::kw__Decimal128:
John McCallfec54012009-08-03 20:12:06 +00002198 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
2199 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002200 break;
John Thompson82287d12010-02-05 00:12:22 +00002201 case tok::kw___vector:
2202 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
2203 break;
2204 case tok::kw___pixel:
2205 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
2206 break;
2207
Douglas Gregor12e083c2008-11-07 15:42:26 +00002208 // class-specifier:
2209 case tok::kw_class:
2210 case tok::kw_struct:
Chris Lattner4c97d762009-04-12 21:49:30 +00002211 case tok::kw_union: {
2212 tok::TokenKind Kind = Tok.getKind();
2213 ConsumeToken();
Sebastian Redld9bafa72010-02-03 21:21:43 +00002214 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS_none,
2215 SuppressDeclarations);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002216 return true;
Chris Lattner4c97d762009-04-12 21:49:30 +00002217 }
Douglas Gregor12e083c2008-11-07 15:42:26 +00002218
2219 // enum-specifier:
2220 case tok::kw_enum:
Chris Lattner4c97d762009-04-12 21:49:30 +00002221 ConsumeToken();
Douglas Gregor9b9edd62010-03-02 17:53:14 +00002222 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS_none);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002223 return true;
2224
2225 // cv-qualifier:
2226 case tok::kw_const:
2227 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00002228 DiagID, getLang());
Douglas Gregor12e083c2008-11-07 15:42:26 +00002229 break;
2230 case tok::kw_volatile:
2231 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00002232 DiagID, getLang());
Douglas Gregor12e083c2008-11-07 15:42:26 +00002233 break;
2234 case tok::kw_restrict:
2235 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00002236 DiagID, getLang());
Douglas Gregor12e083c2008-11-07 15:42:26 +00002237 break;
2238
2239 // GNU typeof support.
2240 case tok::kw_typeof:
2241 ParseTypeofSpecifier(DS);
2242 return true;
2243
Anders Carlsson6fd634f2009-06-24 17:47:40 +00002244 // C++0x decltype support.
2245 case tok::kw_decltype:
2246 ParseDecltypeSpecifier(DS);
2247 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00002248
Sean Huntdb5d44b2011-05-19 05:37:45 +00002249 // C++0x type traits support.
2250 case tok::kw___underlying_type:
2251 ParseUnderlyingTypeSpecifier(DS);
2252 return true;
2253
Peter Collingbourne207f4d82011-03-18 22:38:29 +00002254 // OpenCL qualifiers:
2255 case tok::kw_private:
2256 if (!getLang().OpenCL)
2257 return false;
2258 case tok::kw___private:
2259 case tok::kw___global:
2260 case tok::kw___local:
2261 case tok::kw___constant:
2262 case tok::kw___read_only:
2263 case tok::kw___write_only:
2264 case tok::kw___read_write:
2265 ParseOpenCLQualifiers(DS);
2266 break;
2267
Anders Carlsson0b7f7892009-06-26 23:44:14 +00002268 // C++0x auto support.
2269 case tok::kw_auto:
2270 if (!getLang().CPlusPlus0x)
2271 return false;
2272
John McCallfec54012009-08-03 20:12:06 +00002273 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec, DiagID);
Anders Carlsson0b7f7892009-06-26 23:44:14 +00002274 break;
Dawn Perchik52fc3142010-09-03 01:29:35 +00002275
Eli Friedman290eeb02009-06-08 23:27:34 +00002276 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00002277 case tok::kw___ptr32:
Eli Friedman290eeb02009-06-08 23:27:34 +00002278 case tok::kw___w64:
Steve Naroff239f0732008-12-25 14:16:32 +00002279 case tok::kw___cdecl:
2280 case tok::kw___stdcall:
2281 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00002282 case tok::kw___thiscall:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00002283 case tok::kw___unaligned:
John McCall7f040a92010-12-24 02:08:15 +00002284 ParseMicrosoftTypeAttributes(DS.getAttributes());
Chris Lattner837acd02009-01-21 19:19:26 +00002285 return true;
Steve Naroff239f0732008-12-25 14:16:32 +00002286
Dawn Perchik52fc3142010-09-03 01:29:35 +00002287 case tok::kw___pascal:
John McCall7f040a92010-12-24 02:08:15 +00002288 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik52fc3142010-09-03 01:29:35 +00002289 return true;
2290
Douglas Gregor12e083c2008-11-07 15:42:26 +00002291 default:
2292 // Not a type-specifier; do nothing.
2293 return false;
2294 }
2295
2296 // If the specifier combination wasn't legal, issue a diagnostic.
2297 if (isInvalid) {
2298 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner1ab3b962008-11-18 07:48:38 +00002299 // Pick between error or extwarn.
Chris Lattner1ab3b962008-11-18 07:48:38 +00002300 Diag(Tok, DiagID) << PrevSpec;
Douglas Gregor12e083c2008-11-07 15:42:26 +00002301 }
2302 DS.SetRangeEnd(Tok.getLocation());
2303 ConsumeToken(); // whatever we parsed above.
2304 return true;
2305}
Reid Spencer5f016e22007-07-11 17:01:13 +00002306
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002307/// ParseStructDeclaration - Parse a struct declaration without the terminating
2308/// semicolon.
2309///
Reid Spencer5f016e22007-07-11 17:01:13 +00002310/// struct-declaration:
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002311/// specifier-qualifier-list struct-declarator-list
Reid Spencer5f016e22007-07-11 17:01:13 +00002312/// [GNU] __extension__ struct-declaration
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002313/// [GNU] specifier-qualifier-list
Reid Spencer5f016e22007-07-11 17:01:13 +00002314/// struct-declarator-list:
2315/// struct-declarator
2316/// struct-declarator-list ',' struct-declarator
2317/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
2318/// struct-declarator:
2319/// declarator
2320/// [GNU] declarator attributes[opt]
2321/// declarator[opt] ':' constant-expression
2322/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
2323///
Chris Lattnere1359422008-04-10 06:46:29 +00002324void Parser::
John McCallbdd563e2009-11-03 02:38:08 +00002325ParseStructDeclaration(DeclSpec &DS, FieldCallback &Fields) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002326
Chris Lattnerc46d1a12008-10-20 06:45:43 +00002327 if (Tok.is(tok::kw___extension__)) {
2328 // __extension__ silences extension warnings in the subexpression.
2329 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroff28a7ca82007-08-20 22:28:22 +00002330 ConsumeToken();
Chris Lattnerc46d1a12008-10-20 06:45:43 +00002331 return ParseStructDeclaration(DS, Fields);
2332 }
Mike Stump1eb44332009-09-09 15:08:12 +00002333
Steve Naroff28a7ca82007-08-20 22:28:22 +00002334 // Parse the common specifier-qualifiers-list piece.
Steve Naroff28a7ca82007-08-20 22:28:22 +00002335 ParseSpecifierQualifierList(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00002336
Douglas Gregor4920f1f2009-01-12 22:49:06 +00002337 // If there are no declarators, this is a free-standing declaration
2338 // specifier. Let the actions module cope with it.
Chris Lattner04d66662007-10-09 17:33:22 +00002339 if (Tok.is(tok::semi)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00002340 Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none, DS);
Steve Naroff28a7ca82007-08-20 22:28:22 +00002341 return;
2342 }
2343
2344 // Read struct-declarators until we find the semicolon.
John McCallbdd563e2009-11-03 02:38:08 +00002345 bool FirstDeclarator = true;
Steve Naroff28a7ca82007-08-20 22:28:22 +00002346 while (1) {
John McCall54abf7d2009-11-04 02:18:39 +00002347 ParsingDeclRAIIObject PD(*this);
John McCallbdd563e2009-11-03 02:38:08 +00002348 FieldDeclarator DeclaratorInfo(DS);
2349
2350 // Attributes are only allowed here on successive declarators.
John McCall7f040a92010-12-24 02:08:15 +00002351 if (!FirstDeclarator)
2352 MaybeParseGNUAttributes(DeclaratorInfo.D);
Mike Stump1eb44332009-09-09 15:08:12 +00002353
Steve Naroff28a7ca82007-08-20 22:28:22 +00002354 /// struct-declarator: declarator
2355 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattnera1efc8c2009-12-10 01:59:24 +00002356 if (Tok.isNot(tok::colon)) {
2357 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
2358 ColonProtectionRAIIObject X(*this);
Chris Lattnere1359422008-04-10 06:46:29 +00002359 ParseDeclarator(DeclaratorInfo.D);
Chris Lattnera1efc8c2009-12-10 01:59:24 +00002360 }
Mike Stump1eb44332009-09-09 15:08:12 +00002361
Chris Lattner04d66662007-10-09 17:33:22 +00002362 if (Tok.is(tok::colon)) {
Steve Naroff28a7ca82007-08-20 22:28:22 +00002363 ConsumeToken();
John McCall60d7b3a2010-08-24 06:29:42 +00002364 ExprResult Res(ParseConstantExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002365 if (Res.isInvalid())
Steve Naroff28a7ca82007-08-20 22:28:22 +00002366 SkipUntil(tok::semi, true, true);
Chris Lattner60b1e3e2008-04-10 06:15:14 +00002367 else
Sebastian Redleffa8d12008-12-10 00:02:53 +00002368 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroff28a7ca82007-08-20 22:28:22 +00002369 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00002370
Steve Naroff28a7ca82007-08-20 22:28:22 +00002371 // If attributes exist after the declarator, parse them.
John McCall7f040a92010-12-24 02:08:15 +00002372 MaybeParseGNUAttributes(DeclaratorInfo.D);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002373
John McCallbdd563e2009-11-03 02:38:08 +00002374 // We're done with this declarator; invoke the callback.
John McCalld226f652010-08-21 09:40:31 +00002375 Decl *D = Fields.invoke(DeclaratorInfo);
John McCall54abf7d2009-11-04 02:18:39 +00002376 PD.complete(D);
John McCallbdd563e2009-11-03 02:38:08 +00002377
Steve Naroff28a7ca82007-08-20 22:28:22 +00002378 // If we don't have a comma, it is either the end of the list (a ';')
2379 // or an error, bail out.
Chris Lattner04d66662007-10-09 17:33:22 +00002380 if (Tok.isNot(tok::comma))
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002381 return;
Sebastian Redlab197ba2009-02-09 18:23:29 +00002382
Steve Naroff28a7ca82007-08-20 22:28:22 +00002383 // Consume the comma.
2384 ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00002385
John McCallbdd563e2009-11-03 02:38:08 +00002386 FirstDeclarator = false;
Steve Naroff28a7ca82007-08-20 22:28:22 +00002387 }
Steve Naroff28a7ca82007-08-20 22:28:22 +00002388}
2389
2390/// ParseStructUnionBody
2391/// struct-contents:
2392/// struct-declaration-list
2393/// [EXT] empty
2394/// [GNU] "struct-declaration-list" without terminatoring ';'
2395/// struct-declaration-list:
2396/// struct-declaration
2397/// struct-declaration-list struct-declaration
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002398/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroff28a7ca82007-08-20 22:28:22 +00002399///
Reid Spencer5f016e22007-07-11 17:01:13 +00002400void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
John McCalld226f652010-08-21 09:40:31 +00002401 unsigned TagType, Decl *TagDecl) {
John McCallf312b1e2010-08-26 23:41:50 +00002402 PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc,
2403 "parsing struct/union body");
Mike Stump1eb44332009-09-09 15:08:12 +00002404
Reid Spencer5f016e22007-07-11 17:01:13 +00002405 SourceLocation LBraceLoc = ConsumeBrace();
Mike Stump1eb44332009-09-09 15:08:12 +00002406
Douglas Gregor3218c4b2009-01-09 22:42:13 +00002407 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
Douglas Gregor23c94db2010-07-02 17:43:08 +00002408 Actions.ActOnTagStartDefinition(getCurScope(), TagDecl);
Douglas Gregor72de6672009-01-08 20:45:30 +00002409
Reid Spencer5f016e22007-07-11 17:01:13 +00002410 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
2411 // C++.
Douglas Gregore37ac4f2008-04-13 21:30:24 +00002412 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Douglas Gregor03332962010-07-29 14:29:34 +00002413 Diag(Tok, diag::ext_empty_struct_union)
2414 << (TagType == TST_union);
Reid Spencer5f016e22007-07-11 17:01:13 +00002415
Chris Lattner5f9e2722011-07-23 10:55:15 +00002416 SmallVector<Decl *, 32> FieldDecls;
Chris Lattnere1359422008-04-10 06:46:29 +00002417
Reid Spencer5f016e22007-07-11 17:01:13 +00002418 // While we still have something to read, read the declarations in the struct.
Chris Lattner04d66662007-10-09 17:33:22 +00002419 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002420 // Each iteration of this loop reads one struct-declaration.
Mike Stump1eb44332009-09-09 15:08:12 +00002421
Reid Spencer5f016e22007-07-11 17:01:13 +00002422 // Check for extraneous top-level semicolon.
Chris Lattner04d66662007-10-09 17:33:22 +00002423 if (Tok.is(tok::semi)) {
Douglas Gregor9b3064b2009-04-01 22:41:11 +00002424 Diag(Tok, diag::ext_extra_struct_semi)
Douglas Gregorf13ca062010-06-16 23:08:59 +00002425 << DeclSpec::getSpecifierName((DeclSpec::TST)TagType)
Douglas Gregor849b2432010-03-31 17:46:05 +00002426 << FixItHint::CreateRemoval(Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00002427 ConsumeToken();
2428 continue;
2429 }
Chris Lattnere1359422008-04-10 06:46:29 +00002430
2431 // Parse all the comma separated declarators.
John McCall0b7e6782011-03-24 11:26:52 +00002432 DeclSpec DS(AttrFactory);
Mike Stump1eb44332009-09-09 15:08:12 +00002433
John McCallbdd563e2009-11-03 02:38:08 +00002434 if (!Tok.is(tok::at)) {
2435 struct CFieldCallback : FieldCallback {
2436 Parser &P;
John McCalld226f652010-08-21 09:40:31 +00002437 Decl *TagDecl;
Chris Lattner5f9e2722011-07-23 10:55:15 +00002438 SmallVectorImpl<Decl *> &FieldDecls;
John McCallbdd563e2009-11-03 02:38:08 +00002439
John McCalld226f652010-08-21 09:40:31 +00002440 CFieldCallback(Parser &P, Decl *TagDecl,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002441 SmallVectorImpl<Decl *> &FieldDecls) :
John McCallbdd563e2009-11-03 02:38:08 +00002442 P(P), TagDecl(TagDecl), FieldDecls(FieldDecls) {}
2443
John McCalld226f652010-08-21 09:40:31 +00002444 virtual Decl *invoke(FieldDeclarator &FD) {
John McCallbdd563e2009-11-03 02:38:08 +00002445 // Install the declarator into the current TagDecl.
John McCalld226f652010-08-21 09:40:31 +00002446 Decl *Field = P.Actions.ActOnField(P.getCurScope(), TagDecl,
John McCall4ba39712009-11-03 21:13:47 +00002447 FD.D.getDeclSpec().getSourceRange().getBegin(),
2448 FD.D, FD.BitfieldSize);
John McCallbdd563e2009-11-03 02:38:08 +00002449 FieldDecls.push_back(Field);
2450 return Field;
Douglas Gregor91a28862009-08-26 14:27:30 +00002451 }
John McCallbdd563e2009-11-03 02:38:08 +00002452 } Callback(*this, TagDecl, FieldDecls);
2453
2454 ParseStructDeclaration(DS, Callback);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002455 } else { // Handle @defs
2456 ConsumeToken();
2457 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
2458 Diag(Tok, diag::err_unexpected_at);
Chris Lattner3e156ad2010-02-02 00:37:27 +00002459 SkipUntil(tok::semi, true);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002460 continue;
2461 }
2462 ConsumeToken();
2463 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
2464 if (!Tok.is(tok::identifier)) {
2465 Diag(Tok, diag::err_expected_ident);
Chris Lattner3e156ad2010-02-02 00:37:27 +00002466 SkipUntil(tok::semi, true);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002467 continue;
2468 }
Chris Lattner5f9e2722011-07-23 10:55:15 +00002469 SmallVector<Decl *, 16> Fields;
Douglas Gregor23c94db2010-07-02 17:43:08 +00002470 Actions.ActOnDefs(getCurScope(), TagDecl, Tok.getLocation(),
Douglas Gregor44b43212008-12-11 16:49:14 +00002471 Tok.getIdentifierInfo(), Fields);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002472 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
2473 ConsumeToken();
2474 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
Mike Stump1eb44332009-09-09 15:08:12 +00002475 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002476
Chris Lattner04d66662007-10-09 17:33:22 +00002477 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002478 ConsumeToken();
Chris Lattner04d66662007-10-09 17:33:22 +00002479 } else if (Tok.is(tok::r_brace)) {
Chris Lattner3e156ad2010-02-02 00:37:27 +00002480 ExpectAndConsume(tok::semi, diag::ext_expected_semi_decl_list);
Reid Spencer5f016e22007-07-11 17:01:13 +00002481 break;
2482 } else {
Chris Lattner3e156ad2010-02-02 00:37:27 +00002483 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
2484 // Skip to end of block or statement to avoid ext-warning on extra ';'.
Reid Spencer5f016e22007-07-11 17:01:13 +00002485 SkipUntil(tok::r_brace, true, true);
Chris Lattner3e156ad2010-02-02 00:37:27 +00002486 // If we stopped at a ';', eat it.
2487 if (Tok.is(tok::semi)) ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00002488 }
2489 }
Mike Stump1eb44332009-09-09 15:08:12 +00002490
Steve Naroff60fccee2007-10-29 21:38:07 +00002491 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00002492
John McCall0b7e6782011-03-24 11:26:52 +00002493 ParsedAttributes attrs(AttrFactory);
Reid Spencer5f016e22007-07-11 17:01:13 +00002494 // If attributes exist after struct contents, parse them.
John McCall7f040a92010-12-24 02:08:15 +00002495 MaybeParseGNUAttributes(attrs);
Daniel Dunbar1bfe1c22008-10-03 02:03:53 +00002496
Douglas Gregor23c94db2010-07-02 17:43:08 +00002497 Actions.ActOnFields(getCurScope(),
Jay Foadbeaaccd2009-05-21 09:52:38 +00002498 RecordLoc, TagDecl, FieldDecls.data(), FieldDecls.size(),
Daniel Dunbar1bfe1c22008-10-03 02:03:53 +00002499 LBraceLoc, RBraceLoc,
John McCall7f040a92010-12-24 02:08:15 +00002500 attrs.getList());
Douglas Gregor72de6672009-01-08 20:45:30 +00002501 StructScope.Exit();
Douglas Gregor23c94db2010-07-02 17:43:08 +00002502 Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl, RBraceLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00002503}
2504
Reid Spencer5f016e22007-07-11 17:01:13 +00002505/// ParseEnumSpecifier
2506/// enum-specifier: [C99 6.7.2.2]
2507/// 'enum' identifier[opt] '{' enumerator-list '}'
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002508///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Reid Spencer5f016e22007-07-11 17:01:13 +00002509/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
2510/// '}' attributes[opt]
2511/// 'enum' identifier
2512/// [GNU] 'enum' attributes[opt] identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002513///
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002514/// [C++0x] enum-head '{' enumerator-list[opt] '}'
2515/// [C++0x] enum-head '{' enumerator-list ',' '}'
2516///
2517/// enum-head: [C++0x]
2518/// enum-key attributes[opt] identifier[opt] enum-base[opt]
2519/// enum-key attributes[opt] nested-name-specifier identifier enum-base[opt]
2520///
2521/// enum-key: [C++0x]
2522/// 'enum'
2523/// 'enum' 'class'
2524/// 'enum' 'struct'
2525///
2526/// enum-base: [C++0x]
2527/// ':' type-specifier-seq
2528///
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002529/// [C++] elaborated-type-specifier:
2530/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
2531///
Chris Lattner4c97d762009-04-12 21:49:30 +00002532void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregor9b9edd62010-03-02 17:53:14 +00002533 const ParsedTemplateInfo &TemplateInfo,
Chris Lattner4c97d762009-04-12 21:49:30 +00002534 AccessSpecifier AS) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002535 // Parse the tag portion of this.
Douglas Gregor374929f2009-09-18 15:37:17 +00002536 if (Tok.is(tok::code_completion)) {
2537 // Code completion for an enum name.
Douglas Gregor23c94db2010-07-02 17:43:08 +00002538 Actions.CodeCompleteTag(getCurScope(), DeclSpec::TST_enum);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002539 return cutOffParsing();
Douglas Gregor374929f2009-09-18 15:37:17 +00002540 }
John McCall57c13002011-07-06 05:58:41 +00002541
2542 bool IsScopedEnum = false;
2543 bool IsScopedUsingClassTag = false;
2544
2545 if (getLang().CPlusPlus0x &&
2546 (Tok.is(tok::kw_class) || Tok.is(tok::kw_struct))) {
2547 IsScopedEnum = true;
2548 IsScopedUsingClassTag = Tok.is(tok::kw_class);
2549 ConsumeToken();
2550 }
Douglas Gregor374929f2009-09-18 15:37:17 +00002551
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002552 // If attributes exist after tag, parse them.
John McCall0b7e6782011-03-24 11:26:52 +00002553 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00002554 MaybeParseGNUAttributes(attrs);
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002555
John McCall57c13002011-07-06 05:58:41 +00002556 bool AllowFixedUnderlyingType = getLang().CPlusPlus0x || getLang().Microsoft;
2557
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00002558 CXXScopeSpec &SS = DS.getTypeSpecScope();
John McCall9ba61662010-02-26 08:45:28 +00002559 if (getLang().CPlusPlus) {
John McCall57c13002011-07-06 05:58:41 +00002560 // "enum foo : bar;" is not a potential typo for "enum foo::bar;"
2561 // if a fixed underlying type is allowed.
2562 ColonProtectionRAIIObject X(*this, AllowFixedUnderlyingType);
2563
John McCallb3d87482010-08-24 05:47:05 +00002564 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), false))
John McCall9ba61662010-02-26 08:45:28 +00002565 return;
2566
2567 if (SS.isSet() && Tok.isNot(tok::identifier)) {
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002568 Diag(Tok, diag::err_expected_ident);
2569 if (Tok.isNot(tok::l_brace)) {
2570 // Has no name and is not a definition.
2571 // Skip the rest of this declarator, up until the comma or semicolon.
2572 SkipUntil(tok::comma, true);
2573 return;
2574 }
2575 }
2576 }
Mike Stump1eb44332009-09-09 15:08:12 +00002577
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002578 // Must have either 'enum name' or 'enum {...}'.
Douglas Gregorb9075602011-02-22 02:55:24 +00002579 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace) &&
2580 (AllowFixedUnderlyingType && Tok.isNot(tok::colon))) {
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002581 Diag(Tok, diag::err_expected_ident_lbrace);
Mike Stump1eb44332009-09-09 15:08:12 +00002582
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002583 // Skip the rest of this declarator, up until the comma or semicolon.
2584 SkipUntil(tok::comma, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00002585 return;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002586 }
Mike Stump1eb44332009-09-09 15:08:12 +00002587
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002588 // If an identifier is present, consume and remember it.
2589 IdentifierInfo *Name = 0;
2590 SourceLocation NameLoc;
2591 if (Tok.is(tok::identifier)) {
2592 Name = Tok.getIdentifierInfo();
2593 NameLoc = ConsumeToken();
2594 }
Mike Stump1eb44332009-09-09 15:08:12 +00002595
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002596 if (!Name && IsScopedEnum) {
2597 // C++0x 7.2p2: The optional identifier shall not be omitted in the
2598 // declaration of a scoped enumeration.
2599 Diag(Tok, diag::err_scoped_enum_missing_identifier);
2600 IsScopedEnum = false;
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00002601 IsScopedUsingClassTag = false;
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002602 }
2603
2604 TypeResult BaseType;
2605
Douglas Gregora61b3e72010-12-01 17:42:47 +00002606 // Parse the fixed underlying type.
Douglas Gregorb9075602011-02-22 02:55:24 +00002607 if (AllowFixedUnderlyingType && Tok.is(tok::colon)) {
Douglas Gregora61b3e72010-12-01 17:42:47 +00002608 bool PossibleBitfield = false;
2609 if (getCurScope()->getFlags() & Scope::ClassScope) {
2610 // If we're in class scope, this can either be an enum declaration with
2611 // an underlying type, or a declaration of a bitfield member. We try to
2612 // use a simple disambiguation scheme first to catch the common cases
2613 // (integer literal, sizeof); if it's still ambiguous, we then consider
2614 // anything that's a simple-type-specifier followed by '(' as an
2615 // expression. This suffices because function types are not valid
2616 // underlying types anyway.
2617 TPResult TPR = isExpressionOrTypeSpecifierSimple(NextToken().getKind());
2618 // If the next token starts an expression, we know we're parsing a
2619 // bit-field. This is the common case.
2620 if (TPR == TPResult::True())
2621 PossibleBitfield = true;
2622 // If the next token starts a type-specifier-seq, it may be either a
2623 // a fixed underlying type or the start of a function-style cast in C++;
2624 // lookahead one more token to see if it's obvious that we have a
2625 // fixed underlying type.
2626 else if (TPR == TPResult::False() &&
2627 GetLookAheadToken(2).getKind() == tok::semi) {
2628 // Consume the ':'.
2629 ConsumeToken();
2630 } else {
2631 // We have the start of a type-specifier-seq, so we have to perform
2632 // tentative parsing to determine whether we have an expression or a
2633 // type.
2634 TentativeParsingAction TPA(*this);
2635
2636 // Consume the ':'.
2637 ConsumeToken();
2638
Douglas Gregor86f208c2011-02-22 20:32:04 +00002639 if ((getLang().CPlusPlus &&
2640 isCXXDeclarationSpecifier() != TPResult::True()) ||
2641 (!getLang().CPlusPlus && !isDeclarationSpecifier(true))) {
Douglas Gregora61b3e72010-12-01 17:42:47 +00002642 // We'll parse this as a bitfield later.
2643 PossibleBitfield = true;
2644 TPA.Revert();
2645 } else {
2646 // We have a type-specifier-seq.
2647 TPA.Commit();
2648 }
2649 }
2650 } else {
2651 // Consume the ':'.
2652 ConsumeToken();
2653 }
2654
2655 if (!PossibleBitfield) {
2656 SourceRange Range;
2657 BaseType = ParseTypeName(&Range);
Douglas Gregor86f208c2011-02-22 20:32:04 +00002658
2659 if (!getLang().CPlusPlus0x)
2660 Diag(StartLoc, diag::ext_ms_enum_fixed_underlying_type)
2661 << Range;
Douglas Gregora61b3e72010-12-01 17:42:47 +00002662 }
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002663 }
2664
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002665 // There are three options here. If we have 'enum foo;', then this is a
2666 // forward declaration. If we have 'enum foo {...' then this is a
2667 // definition. Otherwise we have something like 'enum foo xyz', a reference.
2668 //
2669 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
2670 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
2671 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
2672 //
John McCallf312b1e2010-08-26 23:41:50 +00002673 Sema::TagUseKind TUK;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002674 if (Tok.is(tok::l_brace))
John McCallf312b1e2010-08-26 23:41:50 +00002675 TUK = Sema::TUK_Definition;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002676 else if (Tok.is(tok::semi))
John McCallf312b1e2010-08-26 23:41:50 +00002677 TUK = Sema::TUK_Declaration;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002678 else
John McCallf312b1e2010-08-26 23:41:50 +00002679 TUK = Sema::TUK_Reference;
Douglas Gregor8fc6d232010-05-03 17:48:54 +00002680
2681 // enums cannot be templates, although they can be referenced from a
2682 // template.
2683 if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate &&
John McCallf312b1e2010-08-26 23:41:50 +00002684 TUK != Sema::TUK_Reference) {
Douglas Gregor8fc6d232010-05-03 17:48:54 +00002685 Diag(Tok, diag::err_enum_template);
2686
2687 // Skip the rest of this declarator, up until the comma or semicolon.
2688 SkipUntil(tok::comma, true);
2689 return;
2690 }
2691
Douglas Gregorb9075602011-02-22 02:55:24 +00002692 if (!Name && TUK != Sema::TUK_Definition) {
2693 Diag(Tok, diag::err_enumerator_unnamed_no_def);
2694
2695 // Skip the rest of this declarator, up until the comma or semicolon.
2696 SkipUntil(tok::comma, true);
2697 return;
2698 }
2699
Douglas Gregor402abb52009-05-28 23:31:59 +00002700 bool Owned = false;
John McCallc4e70192009-09-11 04:59:25 +00002701 bool IsDependent = false;
Douglas Gregor48c89f42010-04-24 16:38:41 +00002702 const char *PrevSpec = 0;
2703 unsigned DiagID;
John McCalld226f652010-08-21 09:40:31 +00002704 Decl *TagDecl = Actions.ActOnTag(getCurScope(), DeclSpec::TST_enum, TUK,
John McCall7f040a92010-12-24 02:08:15 +00002705 StartLoc, SS, Name, NameLoc, attrs.getList(),
John McCalld226f652010-08-21 09:40:31 +00002706 AS,
John McCallf312b1e2010-08-26 23:41:50 +00002707 MultiTemplateParamsArg(Actions),
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002708 Owned, IsDependent, IsScopedEnum,
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00002709 IsScopedUsingClassTag, BaseType);
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002710
Douglas Gregor48c89f42010-04-24 16:38:41 +00002711 if (IsDependent) {
2712 // This enum has a dependent nested-name-specifier. Handle it as a
2713 // dependent tag.
2714 if (!Name) {
2715 DS.SetTypeSpecError();
2716 Diag(Tok, diag::err_expected_type_name_after_typename);
2717 return;
2718 }
2719
Douglas Gregor23c94db2010-07-02 17:43:08 +00002720 TypeResult Type = Actions.ActOnDependentTag(getCurScope(), DeclSpec::TST_enum,
Douglas Gregor48c89f42010-04-24 16:38:41 +00002721 TUK, SS, Name, StartLoc,
2722 NameLoc);
2723 if (Type.isInvalid()) {
2724 DS.SetTypeSpecError();
2725 return;
2726 }
2727
Abramo Bagnara0daaf322011-03-16 20:16:18 +00002728 if (DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc,
2729 NameLoc.isValid() ? NameLoc : StartLoc,
2730 PrevSpec, DiagID, Type.get()))
Douglas Gregor48c89f42010-04-24 16:38:41 +00002731 Diag(StartLoc, DiagID) << PrevSpec;
2732
2733 return;
2734 }
Mike Stump1eb44332009-09-09 15:08:12 +00002735
John McCalld226f652010-08-21 09:40:31 +00002736 if (!TagDecl) {
Douglas Gregor48c89f42010-04-24 16:38:41 +00002737 // The action failed to produce an enumeration tag. If this is a
2738 // definition, consume the entire definition.
2739 if (Tok.is(tok::l_brace)) {
2740 ConsumeBrace();
2741 SkipUntil(tok::r_brace);
2742 }
2743
2744 DS.SetTypeSpecError();
2745 return;
2746 }
2747
Chris Lattner04d66662007-10-09 17:33:22 +00002748 if (Tok.is(tok::l_brace))
Reid Spencer5f016e22007-07-11 17:01:13 +00002749 ParseEnumBody(StartLoc, TagDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00002750
Abramo Bagnara0daaf322011-03-16 20:16:18 +00002751 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc,
2752 NameLoc.isValid() ? NameLoc : StartLoc,
2753 PrevSpec, DiagID, TagDecl, Owned))
John McCallfec54012009-08-03 20:12:06 +00002754 Diag(StartLoc, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00002755}
2756
2757/// ParseEnumBody - Parse a {} enclosed enumerator-list.
2758/// enumerator-list:
2759/// enumerator
2760/// enumerator-list ',' enumerator
2761/// enumerator:
2762/// enumeration-constant
2763/// enumeration-constant '=' constant-expression
2764/// enumeration-constant:
2765/// identifier
2766///
John McCalld226f652010-08-21 09:40:31 +00002767void Parser::ParseEnumBody(SourceLocation StartLoc, Decl *EnumDecl) {
Douglas Gregor074149e2009-01-05 19:45:36 +00002768 // Enter the scope of the enum body and start the definition.
2769 ParseScope EnumScope(this, Scope::DeclScope);
Douglas Gregor23c94db2010-07-02 17:43:08 +00002770 Actions.ActOnTagStartDefinition(getCurScope(), EnumDecl);
Douglas Gregor074149e2009-01-05 19:45:36 +00002771
Reid Spencer5f016e22007-07-11 17:01:13 +00002772 SourceLocation LBraceLoc = ConsumeBrace();
Mike Stump1eb44332009-09-09 15:08:12 +00002773
Chris Lattner7946dd32007-08-27 17:24:30 +00002774 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
Chris Lattner04d66662007-10-09 17:33:22 +00002775 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Fariborz Jahanian05115522010-05-28 22:23:22 +00002776 Diag(Tok, diag::error_empty_enum);
Mike Stump1eb44332009-09-09 15:08:12 +00002777
Chris Lattner5f9e2722011-07-23 10:55:15 +00002778 SmallVector<Decl *, 32> EnumConstantDecls;
Reid Spencer5f016e22007-07-11 17:01:13 +00002779
John McCalld226f652010-08-21 09:40:31 +00002780 Decl *LastEnumConstDecl = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00002781
Reid Spencer5f016e22007-07-11 17:01:13 +00002782 // Parse the enumerator-list.
Chris Lattner04d66662007-10-09 17:33:22 +00002783 while (Tok.is(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002784 IdentifierInfo *Ident = Tok.getIdentifierInfo();
2785 SourceLocation IdentLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00002786
John McCall5b629aa2010-10-22 23:36:17 +00002787 // If attributes exist after the enumerator, parse them.
John McCall0b7e6782011-03-24 11:26:52 +00002788 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00002789 MaybeParseGNUAttributes(attrs);
John McCall5b629aa2010-10-22 23:36:17 +00002790
Reid Spencer5f016e22007-07-11 17:01:13 +00002791 SourceLocation EqualLoc;
John McCall60d7b3a2010-08-24 06:29:42 +00002792 ExprResult AssignedVal;
Chris Lattner04d66662007-10-09 17:33:22 +00002793 if (Tok.is(tok::equal)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002794 EqualLoc = ConsumeToken();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002795 AssignedVal = ParseConstantExpression();
2796 if (AssignedVal.isInvalid())
Reid Spencer5f016e22007-07-11 17:01:13 +00002797 SkipUntil(tok::comma, tok::r_brace, true, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00002798 }
Mike Stump1eb44332009-09-09 15:08:12 +00002799
Reid Spencer5f016e22007-07-11 17:01:13 +00002800 // Install the enumerator constant into EnumDecl.
John McCalld226f652010-08-21 09:40:31 +00002801 Decl *EnumConstDecl = Actions.ActOnEnumConstant(getCurScope(), EnumDecl,
2802 LastEnumConstDecl,
2803 IdentLoc, Ident,
John McCall7f040a92010-12-24 02:08:15 +00002804 attrs.getList(), EqualLoc,
John McCalld226f652010-08-21 09:40:31 +00002805 AssignedVal.release());
Reid Spencer5f016e22007-07-11 17:01:13 +00002806 EnumConstantDecls.push_back(EnumConstDecl);
2807 LastEnumConstDecl = EnumConstDecl;
Mike Stump1eb44332009-09-09 15:08:12 +00002808
Douglas Gregor751f6922010-09-07 14:51:08 +00002809 if (Tok.is(tok::identifier)) {
2810 // We're missing a comma between enumerators.
2811 SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation);
2812 Diag(Loc, diag::err_enumerator_list_missing_comma)
2813 << FixItHint::CreateInsertion(Loc, ", ");
2814 continue;
2815 }
2816
Chris Lattner04d66662007-10-09 17:33:22 +00002817 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +00002818 break;
2819 SourceLocation CommaLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00002820
2821 if (Tok.isNot(tok::identifier) &&
Douglas Gregor9b3064b2009-04-01 22:41:11 +00002822 !(getLang().C99 || getLang().CPlusPlus0x))
2823 Diag(CommaLoc, diag::ext_enumerator_list_comma)
2824 << getLang().CPlusPlus
Douglas Gregor849b2432010-03-31 17:46:05 +00002825 << FixItHint::CreateRemoval(CommaLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00002826 }
Mike Stump1eb44332009-09-09 15:08:12 +00002827
Reid Spencer5f016e22007-07-11 17:01:13 +00002828 // Eat the }.
Mike Stumpc6e35aa2009-05-16 07:06:02 +00002829 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00002830
Reid Spencer5f016e22007-07-11 17:01:13 +00002831 // If attributes exist after the identifier list, parse them.
John McCall0b7e6782011-03-24 11:26:52 +00002832 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00002833 MaybeParseGNUAttributes(attrs);
Douglas Gregor72de6672009-01-08 20:45:30 +00002834
Edward O'Callaghanfee13812009-08-08 14:36:57 +00002835 Actions.ActOnEnumBody(StartLoc, LBraceLoc, RBraceLoc, EnumDecl,
2836 EnumConstantDecls.data(), EnumConstantDecls.size(),
John McCall7f040a92010-12-24 02:08:15 +00002837 getCurScope(), attrs.getList());
Mike Stump1eb44332009-09-09 15:08:12 +00002838
Douglas Gregor72de6672009-01-08 20:45:30 +00002839 EnumScope.Exit();
Douglas Gregor23c94db2010-07-02 17:43:08 +00002840 Actions.ActOnTagFinishDefinition(getCurScope(), EnumDecl, RBraceLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00002841}
2842
2843/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff5f8aa692008-02-11 23:15:56 +00002844/// start of a type-qualifier-list.
2845bool Parser::isTypeQualifier() const {
2846 switch (Tok.getKind()) {
2847 default: return false;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00002848
2849 // type-qualifier only in OpenCL
2850 case tok::kw_private:
2851 return getLang().OpenCL;
2852
Steve Naroff5f8aa692008-02-11 23:15:56 +00002853 // type-qualifier
2854 case tok::kw_const:
2855 case tok::kw_volatile:
2856 case tok::kw_restrict:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00002857 case tok::kw___private:
2858 case tok::kw___local:
2859 case tok::kw___global:
2860 case tok::kw___constant:
2861 case tok::kw___read_only:
2862 case tok::kw___read_write:
2863 case tok::kw___write_only:
Steve Naroff5f8aa692008-02-11 23:15:56 +00002864 return true;
2865 }
2866}
2867
Chris Lattnerb3a4e432010-02-28 18:18:36 +00002868/// isKnownToBeTypeSpecifier - Return true if we know that the specified token
2869/// is definitely a type-specifier. Return false if it isn't part of a type
2870/// specifier or if we're not sure.
2871bool Parser::isKnownToBeTypeSpecifier(const Token &Tok) const {
2872 switch (Tok.getKind()) {
2873 default: return false;
2874 // type-specifiers
2875 case tok::kw_short:
2876 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00002877 case tok::kw___int64:
Chris Lattnerb3a4e432010-02-28 18:18:36 +00002878 case tok::kw_signed:
2879 case tok::kw_unsigned:
2880 case tok::kw__Complex:
2881 case tok::kw__Imaginary:
2882 case tok::kw_void:
2883 case tok::kw_char:
2884 case tok::kw_wchar_t:
2885 case tok::kw_char16_t:
2886 case tok::kw_char32_t:
2887 case tok::kw_int:
2888 case tok::kw_float:
2889 case tok::kw_double:
2890 case tok::kw_bool:
2891 case tok::kw__Bool:
2892 case tok::kw__Decimal32:
2893 case tok::kw__Decimal64:
2894 case tok::kw__Decimal128:
2895 case tok::kw___vector:
2896
2897 // struct-or-union-specifier (C99) or class-specifier (C++)
2898 case tok::kw_class:
2899 case tok::kw_struct:
2900 case tok::kw_union:
2901 // enum-specifier
2902 case tok::kw_enum:
2903
2904 // typedef-name
2905 case tok::annot_typename:
2906 return true;
2907 }
2908}
2909
Steve Naroff5f8aa692008-02-11 23:15:56 +00002910/// isTypeSpecifierQualifier - Return true if the current token could be the
Reid Spencer5f016e22007-07-11 17:01:13 +00002911/// start of a specifier-qualifier-list.
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002912bool Parser::isTypeSpecifierQualifier() {
Reid Spencer5f016e22007-07-11 17:01:13 +00002913 switch (Tok.getKind()) {
2914 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002915
Chris Lattner166a8fc2009-01-04 23:41:41 +00002916 case tok::identifier: // foo::bar
John Thompson82287d12010-02-05 00:12:22 +00002917 if (TryAltiVecVectorToken())
2918 return true;
2919 // Fall through.
Douglas Gregord57959a2009-03-27 23:10:48 +00002920 case tok::kw_typename: // typename T::type
Chris Lattner166a8fc2009-01-04 23:41:41 +00002921 // Annotate typenames and C++ scope specifiers. If we get one, just
2922 // recurse to handle whatever we get.
2923 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00002924 return true;
2925 if (Tok.is(tok::identifier))
2926 return false;
2927 return isTypeSpecifierQualifier();
Douglas Gregord57959a2009-03-27 23:10:48 +00002928
Chris Lattner166a8fc2009-01-04 23:41:41 +00002929 case tok::coloncolon: // ::foo::bar
2930 if (NextToken().is(tok::kw_new) || // ::new
2931 NextToken().is(tok::kw_delete)) // ::delete
2932 return false;
2933
Chris Lattner166a8fc2009-01-04 23:41:41 +00002934 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00002935 return true;
2936 return isTypeSpecifierQualifier();
Mike Stump1eb44332009-09-09 15:08:12 +00002937
Reid Spencer5f016e22007-07-11 17:01:13 +00002938 // GNU attributes support.
2939 case tok::kw___attribute:
Steve Naroffd1861fd2007-07-31 12:34:36 +00002940 // GNU typeof support.
2941 case tok::kw_typeof:
Mike Stump1eb44332009-09-09 15:08:12 +00002942
Reid Spencer5f016e22007-07-11 17:01:13 +00002943 // type-specifiers
2944 case tok::kw_short:
2945 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00002946 case tok::kw___int64:
Reid Spencer5f016e22007-07-11 17:01:13 +00002947 case tok::kw_signed:
2948 case tok::kw_unsigned:
2949 case tok::kw__Complex:
2950 case tok::kw__Imaginary:
2951 case tok::kw_void:
2952 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00002953 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002954 case tok::kw_char16_t:
2955 case tok::kw_char32_t:
Reid Spencer5f016e22007-07-11 17:01:13 +00002956 case tok::kw_int:
2957 case tok::kw_float:
2958 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00002959 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00002960 case tok::kw__Bool:
2961 case tok::kw__Decimal32:
2962 case tok::kw__Decimal64:
2963 case tok::kw__Decimal128:
John Thompson82287d12010-02-05 00:12:22 +00002964 case tok::kw___vector:
Mike Stump1eb44332009-09-09 15:08:12 +00002965
Chris Lattner99dc9142008-04-13 18:59:07 +00002966 // struct-or-union-specifier (C99) or class-specifier (C++)
2967 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00002968 case tok::kw_struct:
2969 case tok::kw_union:
2970 // enum-specifier
2971 case tok::kw_enum:
Mike Stump1eb44332009-09-09 15:08:12 +00002972
Reid Spencer5f016e22007-07-11 17:01:13 +00002973 // type-qualifier
2974 case tok::kw_const:
2975 case tok::kw_volatile:
2976 case tok::kw_restrict:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002977
2978 // typedef-name
Chris Lattnerb31757b2009-01-06 05:06:21 +00002979 case tok::annot_typename:
Reid Spencer5f016e22007-07-11 17:01:13 +00002980 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00002981
Chris Lattner7c186be2008-10-20 00:25:30 +00002982 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
2983 case tok::less:
2984 return getLang().ObjC1;
Mike Stump1eb44332009-09-09 15:08:12 +00002985
Steve Naroff239f0732008-12-25 14:16:32 +00002986 case tok::kw___cdecl:
2987 case tok::kw___stdcall:
2988 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00002989 case tok::kw___thiscall:
Eli Friedman290eeb02009-06-08 23:27:34 +00002990 case tok::kw___w64:
2991 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00002992 case tok::kw___ptr32:
Dawn Perchik52fc3142010-09-03 01:29:35 +00002993 case tok::kw___pascal:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00002994 case tok::kw___unaligned:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00002995
2996 case tok::kw___private:
2997 case tok::kw___local:
2998 case tok::kw___global:
2999 case tok::kw___constant:
3000 case tok::kw___read_only:
3001 case tok::kw___read_write:
3002 case tok::kw___write_only:
3003
Eli Friedman290eeb02009-06-08 23:27:34 +00003004 return true;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003005
3006 case tok::kw_private:
3007 return getLang().OpenCL;
Reid Spencer5f016e22007-07-11 17:01:13 +00003008 }
3009}
3010
3011/// isDeclarationSpecifier() - Return true if the current token is part of a
3012/// declaration specifier.
Douglas Gregor9497a732010-09-16 01:51:54 +00003013///
3014/// \param DisambiguatingWithExpression True to indicate that the purpose of
3015/// this check is to disambiguate between an expression and a declaration.
3016bool Parser::isDeclarationSpecifier(bool DisambiguatingWithExpression) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003017 switch (Tok.getKind()) {
3018 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00003019
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003020 case tok::kw_private:
3021 return getLang().OpenCL;
3022
Chris Lattner166a8fc2009-01-04 23:41:41 +00003023 case tok::identifier: // foo::bar
Steve Naroff61f72cb2009-03-09 21:12:44 +00003024 // Unfortunate hack to support "Class.factoryMethod" notation.
3025 if (getLang().ObjC1 && NextToken().is(tok::period))
3026 return false;
John Thompson82287d12010-02-05 00:12:22 +00003027 if (TryAltiVecVectorToken())
3028 return true;
3029 // Fall through.
Douglas Gregord57959a2009-03-27 23:10:48 +00003030 case tok::kw_typename: // typename T::type
Chris Lattner166a8fc2009-01-04 23:41:41 +00003031 // Annotate typenames and C++ scope specifiers. If we get one, just
3032 // recurse to handle whatever we get.
3033 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003034 return true;
3035 if (Tok.is(tok::identifier))
3036 return false;
Douglas Gregor9497a732010-09-16 01:51:54 +00003037
3038 // If we're in Objective-C and we have an Objective-C class type followed
3039 // by an identifier and then either ':' or ']', in a place where an
3040 // expression is permitted, then this is probably a class message send
3041 // missing the initial '['. In this case, we won't consider this to be
3042 // the start of a declaration.
3043 if (DisambiguatingWithExpression &&
3044 isStartOfObjCClassMessageMissingOpenBracket())
3045 return false;
3046
John McCall9ba61662010-02-26 08:45:28 +00003047 return isDeclarationSpecifier();
3048
Chris Lattner166a8fc2009-01-04 23:41:41 +00003049 case tok::coloncolon: // ::foo::bar
3050 if (NextToken().is(tok::kw_new) || // ::new
3051 NextToken().is(tok::kw_delete)) // ::delete
3052 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00003053
Chris Lattner166a8fc2009-01-04 23:41:41 +00003054 // Annotate typenames and C++ scope specifiers. If we get one, just
3055 // recurse to handle whatever we get.
3056 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003057 return true;
3058 return isDeclarationSpecifier();
Mike Stump1eb44332009-09-09 15:08:12 +00003059
Reid Spencer5f016e22007-07-11 17:01:13 +00003060 // storage-class-specifier
3061 case tok::kw_typedef:
3062 case tok::kw_extern:
Steve Naroff8d54bf22007-12-18 00:16:02 +00003063 case tok::kw___private_extern__:
Reid Spencer5f016e22007-07-11 17:01:13 +00003064 case tok::kw_static:
3065 case tok::kw_auto:
3066 case tok::kw_register:
3067 case tok::kw___thread:
Mike Stump1eb44332009-09-09 15:08:12 +00003068
Reid Spencer5f016e22007-07-11 17:01:13 +00003069 // type-specifiers
3070 case tok::kw_short:
3071 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00003072 case tok::kw___int64:
Reid Spencer5f016e22007-07-11 17:01:13 +00003073 case tok::kw_signed:
3074 case tok::kw_unsigned:
3075 case tok::kw__Complex:
3076 case tok::kw__Imaginary:
3077 case tok::kw_void:
3078 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00003079 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00003080 case tok::kw_char16_t:
3081 case tok::kw_char32_t:
3082
Reid Spencer5f016e22007-07-11 17:01:13 +00003083 case tok::kw_int:
3084 case tok::kw_float:
3085 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00003086 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00003087 case tok::kw__Bool:
3088 case tok::kw__Decimal32:
3089 case tok::kw__Decimal64:
3090 case tok::kw__Decimal128:
John Thompson82287d12010-02-05 00:12:22 +00003091 case tok::kw___vector:
Mike Stump1eb44332009-09-09 15:08:12 +00003092
Chris Lattner99dc9142008-04-13 18:59:07 +00003093 // struct-or-union-specifier (C99) or class-specifier (C++)
3094 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00003095 case tok::kw_struct:
3096 case tok::kw_union:
3097 // enum-specifier
3098 case tok::kw_enum:
Mike Stump1eb44332009-09-09 15:08:12 +00003099
Reid Spencer5f016e22007-07-11 17:01:13 +00003100 // type-qualifier
3101 case tok::kw_const:
3102 case tok::kw_volatile:
3103 case tok::kw_restrict:
Steve Naroffd1861fd2007-07-31 12:34:36 +00003104
Reid Spencer5f016e22007-07-11 17:01:13 +00003105 // function-specifier
3106 case tok::kw_inline:
Douglas Gregorb48fe382008-10-31 09:07:45 +00003107 case tok::kw_virtual:
3108 case tok::kw_explicit:
Chris Lattnerd6c7c182007-08-09 16:40:21 +00003109
Peter Collingbournec6eb44b2011-04-15 00:35:57 +00003110 // static_assert-declaration
3111 case tok::kw__Static_assert:
3112
Chris Lattner1ef08762007-08-09 17:01:07 +00003113 // GNU typeof support.
3114 case tok::kw_typeof:
Mike Stump1eb44332009-09-09 15:08:12 +00003115
Chris Lattner1ef08762007-08-09 17:01:07 +00003116 // GNU attributes.
Chris Lattnerd6c7c182007-08-09 16:40:21 +00003117 case tok::kw___attribute:
Reid Spencer5f016e22007-07-11 17:01:13 +00003118 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00003119
Francois Pichete3d49b42011-06-19 08:02:06 +00003120 // C++0x decltype.
3121 case tok::kw_decltype:
3122 return true;
3123
Chris Lattnerf3948c42008-07-26 03:38:44 +00003124 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
3125 case tok::less:
3126 return getLang().ObjC1;
Mike Stump1eb44332009-09-09 15:08:12 +00003127
Douglas Gregord9d75e52011-04-27 05:41:15 +00003128 // typedef-name
3129 case tok::annot_typename:
3130 return !DisambiguatingWithExpression ||
3131 !isStartOfObjCClassMessageMissingOpenBracket();
3132
Steve Naroff47f52092009-01-06 19:34:12 +00003133 case tok::kw___declspec:
Steve Naroff239f0732008-12-25 14:16:32 +00003134 case tok::kw___cdecl:
3135 case tok::kw___stdcall:
3136 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00003137 case tok::kw___thiscall:
Eli Friedman290eeb02009-06-08 23:27:34 +00003138 case tok::kw___w64:
3139 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00003140 case tok::kw___ptr32:
Eli Friedman290eeb02009-06-08 23:27:34 +00003141 case tok::kw___forceinline:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003142 case tok::kw___pascal:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00003143 case tok::kw___unaligned:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003144
3145 case tok::kw___private:
3146 case tok::kw___local:
3147 case tok::kw___global:
3148 case tok::kw___constant:
3149 case tok::kw___read_only:
3150 case tok::kw___read_write:
3151 case tok::kw___write_only:
3152
Eli Friedman290eeb02009-06-08 23:27:34 +00003153 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00003154 }
3155}
3156
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003157bool Parser::isConstructorDeclarator() {
3158 TentativeParsingAction TPA(*this);
3159
3160 // Parse the C++ scope specifier.
3161 CXXScopeSpec SS;
John McCallb3d87482010-08-24 05:47:05 +00003162 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), true)) {
John McCall9ba61662010-02-26 08:45:28 +00003163 TPA.Revert();
3164 return false;
3165 }
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003166
3167 // Parse the constructor name.
3168 if (Tok.is(tok::identifier) || Tok.is(tok::annot_template_id)) {
3169 // We already know that we have a constructor name; just consume
3170 // the token.
3171 ConsumeToken();
3172 } else {
3173 TPA.Revert();
3174 return false;
3175 }
3176
3177 // Current class name must be followed by a left parentheses.
3178 if (Tok.isNot(tok::l_paren)) {
3179 TPA.Revert();
3180 return false;
3181 }
3182 ConsumeParen();
3183
3184 // A right parentheses or ellipsis signals that we have a constructor.
3185 if (Tok.is(tok::r_paren) || Tok.is(tok::ellipsis)) {
3186 TPA.Revert();
3187 return true;
3188 }
3189
3190 // If we need to, enter the specified scope.
3191 DeclaratorScopeObj DeclScopeObj(*this, SS);
Douglas Gregor23c94db2010-07-02 17:43:08 +00003192 if (SS.isSet() && Actions.ShouldEnterDeclaratorScope(getCurScope(), SS))
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003193 DeclScopeObj.EnterDeclaratorScope();
3194
Francois Pichetdfaa5fb2011-01-31 04:54:32 +00003195 // Optionally skip Microsoft attributes.
John McCall0b7e6782011-03-24 11:26:52 +00003196 ParsedAttributes Attrs(AttrFactory);
Francois Pichetdfaa5fb2011-01-31 04:54:32 +00003197 MaybeParseMicrosoftAttributes(Attrs);
3198
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003199 // Check whether the next token(s) are part of a declaration
3200 // specifier, in which case we have the start of a parameter and,
3201 // therefore, we know that this is a constructor.
3202 bool IsConstructor = isDeclarationSpecifier();
3203 TPA.Revert();
3204 return IsConstructor;
3205}
Reid Spencer5f016e22007-07-11 17:01:13 +00003206
3207/// ParseTypeQualifierListOpt
Dawn Perchik52fc3142010-09-03 01:29:35 +00003208/// type-qualifier-list: [C99 6.7.5]
3209/// type-qualifier
3210/// [vendor] attributes
3211/// [ only if VendorAttributesAllowed=true ]
3212/// type-qualifier-list type-qualifier
3213/// [vendor] type-qualifier-list attributes
3214/// [ only if VendorAttributesAllowed=true ]
3215/// [C++0x] attribute-specifier[opt] is allowed before cv-qualifier-seq
3216/// [ only if CXX0XAttributesAllowed=true ]
3217/// Note: vendor can be GNU, MS, etc.
Reid Spencer5f016e22007-07-11 17:01:13 +00003218///
Dawn Perchik52fc3142010-09-03 01:29:35 +00003219void Parser::ParseTypeQualifierListOpt(DeclSpec &DS,
3220 bool VendorAttributesAllowed,
Sean Huntbbd37c62009-11-21 08:43:09 +00003221 bool CXX0XAttributesAllowed) {
3222 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier()) {
3223 SourceLocation Loc = Tok.getLocation();
John McCall0b7e6782011-03-24 11:26:52 +00003224 ParsedAttributesWithRange attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00003225 ParseCXX0XAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00003226 if (CXX0XAttributesAllowed)
John McCall7f040a92010-12-24 02:08:15 +00003227 DS.takeAttributesFrom(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00003228 else
3229 Diag(Loc, diag::err_attributes_not_allowed);
3230 }
Abramo Bagnara796aa442011-03-12 11:17:06 +00003231
3232 SourceLocation EndLoc;
3233
Reid Spencer5f016e22007-07-11 17:01:13 +00003234 while (1) {
John McCallfec54012009-08-03 20:12:06 +00003235 bool isInvalid = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00003236 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00003237 unsigned DiagID = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00003238 SourceLocation Loc = Tok.getLocation();
3239
3240 switch (Tok.getKind()) {
Douglas Gregor1a480c42010-08-27 17:35:51 +00003241 case tok::code_completion:
3242 Actions.CodeCompleteTypeQualifiers(DS);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00003243 return cutOffParsing();
Douglas Gregor1a480c42010-08-27 17:35:51 +00003244
Reid Spencer5f016e22007-07-11 17:01:13 +00003245 case tok::kw_const:
John McCallfec54012009-08-03 20:12:06 +00003246 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, DiagID,
3247 getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00003248 break;
3249 case tok::kw_volatile:
John McCallfec54012009-08-03 20:12:06 +00003250 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
3251 getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00003252 break;
3253 case tok::kw_restrict:
John McCallfec54012009-08-03 20:12:06 +00003254 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
3255 getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00003256 break;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003257
3258 // OpenCL qualifiers:
3259 case tok::kw_private:
3260 if (!getLang().OpenCL)
3261 goto DoneWithTypeQuals;
3262 case tok::kw___private:
3263 case tok::kw___global:
3264 case tok::kw___local:
3265 case tok::kw___constant:
3266 case tok::kw___read_only:
3267 case tok::kw___write_only:
3268 case tok::kw___read_write:
3269 ParseOpenCLQualifiers(DS);
3270 break;
3271
Eli Friedman290eeb02009-06-08 23:27:34 +00003272 case tok::kw___w64:
Steve Naroff86bc6cf2008-12-25 14:41:26 +00003273 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00003274 case tok::kw___ptr32:
Steve Naroff239f0732008-12-25 14:16:32 +00003275 case tok::kw___cdecl:
3276 case tok::kw___stdcall:
3277 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00003278 case tok::kw___thiscall:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00003279 case tok::kw___unaligned:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003280 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00003281 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman290eeb02009-06-08 23:27:34 +00003282 continue;
3283 }
3284 goto DoneWithTypeQuals;
Dawn Perchik52fc3142010-09-03 01:29:35 +00003285 case tok::kw___pascal:
3286 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00003287 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik52fc3142010-09-03 01:29:35 +00003288 continue;
3289 }
3290 goto DoneWithTypeQuals;
Reid Spencer5f016e22007-07-11 17:01:13 +00003291 case tok::kw___attribute:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003292 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00003293 ParseGNUAttributes(DS.getAttributes());
Chris Lattner5a69d1c2008-12-18 07:02:59 +00003294 continue; // do *not* consume the next token!
3295 }
3296 // otherwise, FALL THROUGH!
3297 default:
Steve Naroff239f0732008-12-25 14:16:32 +00003298 DoneWithTypeQuals:
Chris Lattner5a69d1c2008-12-18 07:02:59 +00003299 // If this is not a type-qualifier token, we're done reading type
3300 // qualifiers. First verify that DeclSpec's are consistent.
Douglas Gregor9b3064b2009-04-01 22:41:11 +00003301 DS.Finish(Diags, PP);
Abramo Bagnara796aa442011-03-12 11:17:06 +00003302 if (EndLoc.isValid())
3303 DS.SetRangeEnd(EndLoc);
Chris Lattner5a69d1c2008-12-18 07:02:59 +00003304 return;
Reid Spencer5f016e22007-07-11 17:01:13 +00003305 }
Chris Lattnera1fcbad2008-12-18 06:50:14 +00003306
Reid Spencer5f016e22007-07-11 17:01:13 +00003307 // If the specifier combination wasn't legal, issue a diagnostic.
3308 if (isInvalid) {
3309 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner1ab3b962008-11-18 07:48:38 +00003310 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00003311 }
Abramo Bagnara796aa442011-03-12 11:17:06 +00003312 EndLoc = ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00003313 }
3314}
3315
3316
3317/// ParseDeclarator - Parse and verify a newly-initialized declarator.
3318///
3319void Parser::ParseDeclarator(Declarator &D) {
3320 /// This implements the 'declarator' production in the C grammar, then checks
3321 /// for well-formedness and issues diagnostics.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003322 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Reid Spencer5f016e22007-07-11 17:01:13 +00003323}
3324
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003325/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
3326/// is parsed by the function passed to it. Pass null, and the direct-declarator
3327/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003328/// ptr-operator production.
3329///
Sebastian Redlf30208a2009-01-24 21:16:55 +00003330/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
3331/// [C] pointer[opt] direct-declarator
3332/// [C++] direct-declarator
3333/// [C++] ptr-operator declarator
Reid Spencer5f016e22007-07-11 17:01:13 +00003334///
3335/// pointer: [C99 6.7.5]
3336/// '*' type-qualifier-list[opt]
3337/// '*' type-qualifier-list[opt] pointer
3338///
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003339/// ptr-operator:
3340/// '*' cv-qualifier-seq[opt]
3341/// '&'
Sebastian Redl05532f22009-03-15 22:02:01 +00003342/// [C++0x] '&&'
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003343/// [GNU] '&' restrict[opt] attributes[opt]
Sebastian Redl05532f22009-03-15 22:02:01 +00003344/// [GNU?] '&&' restrict[opt] attributes[opt]
Sebastian Redlf30208a2009-01-24 21:16:55 +00003345/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003346void Parser::ParseDeclaratorInternal(Declarator &D,
3347 DirectDeclParseFunction DirectDeclParser) {
Douglas Gregor91a28862009-08-26 14:27:30 +00003348 if (Diags.hasAllExtensionsSilenced())
3349 D.setExtension();
Douglas Gregor2ccccb32010-08-23 18:23:48 +00003350
Sebastian Redlf30208a2009-01-24 21:16:55 +00003351 // C++ member pointers start with a '::' or a nested-name.
3352 // Member pointers get special handling, since there's no place for the
3353 // scope spec in the generic path below.
Chris Lattnerf919bfe2009-03-24 17:04:48 +00003354 if (getLang().CPlusPlus &&
3355 (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) ||
3356 Tok.is(tok::annot_cxxscope))) {
Sebastian Redlf30208a2009-01-24 21:16:55 +00003357 CXXScopeSpec SS;
John McCallb3d87482010-08-24 05:47:05 +00003358 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), true); // ignore fail
John McCall9ba61662010-02-26 08:45:28 +00003359
Jeffrey Yasskinedc28772010-04-07 23:29:58 +00003360 if (SS.isNotEmpty()) {
Mike Stump1eb44332009-09-09 15:08:12 +00003361 if (Tok.isNot(tok::star)) {
Sebastian Redlf30208a2009-01-24 21:16:55 +00003362 // The scope spec really belongs to the direct-declarator.
3363 D.getCXXScopeSpec() = SS;
3364 if (DirectDeclParser)
3365 (this->*DirectDeclParser)(D);
3366 return;
3367 }
3368
3369 SourceLocation Loc = ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00003370 D.SetRangeEnd(Loc);
John McCall0b7e6782011-03-24 11:26:52 +00003371 DeclSpec DS(AttrFactory);
Sebastian Redlf30208a2009-01-24 21:16:55 +00003372 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00003373 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00003374
3375 // Recurse to parse whatever is left.
3376 ParseDeclaratorInternal(D, DirectDeclParser);
3377
3378 // Sema will have to catch (syntactically invalid) pointers into global
3379 // scope. It has to catch pointers into namespace scope anyway.
3380 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
John McCall0b7e6782011-03-24 11:26:52 +00003381 Loc),
3382 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003383 /* Don't replace range end. */SourceLocation());
Sebastian Redlf30208a2009-01-24 21:16:55 +00003384 return;
3385 }
3386 }
3387
3388 tok::TokenKind Kind = Tok.getKind();
Steve Naroff5618bd42008-08-27 16:04:49 +00003389 // Not a pointer, C++ reference, or block.
Chris Lattner9af55002009-03-27 04:18:06 +00003390 if (Kind != tok::star && Kind != tok::caret &&
Chris Lattnerf919bfe2009-03-24 17:04:48 +00003391 (Kind != tok::amp || !getLang().CPlusPlus) &&
Sebastian Redl743de1f2009-03-23 00:00:23 +00003392 // We parse rvalue refs in C++03, because otherwise the errors are scary.
Chris Lattner9af55002009-03-27 04:18:06 +00003393 (Kind != tok::ampamp || !getLang().CPlusPlus)) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003394 if (DirectDeclParser)
3395 (this->*DirectDeclParser)(D);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003396 return;
3397 }
Sebastian Redlf30208a2009-01-24 21:16:55 +00003398
Sebastian Redl05532f22009-03-15 22:02:01 +00003399 // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference,
3400 // '&&' -> rvalue reference
Sebastian Redl743de1f2009-03-23 00:00:23 +00003401 SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&.
Sebastian Redlab197ba2009-02-09 18:23:29 +00003402 D.SetRangeEnd(Loc);
Reid Spencer5f016e22007-07-11 17:01:13 +00003403
Chris Lattner9af55002009-03-27 04:18:06 +00003404 if (Kind == tok::star || Kind == tok::caret) {
Chris Lattner76549142008-02-21 01:32:26 +00003405 // Is a pointer.
John McCall0b7e6782011-03-24 11:26:52 +00003406 DeclSpec DS(AttrFactory);
Sebastian Redlf30208a2009-01-24 21:16:55 +00003407
Reid Spencer5f016e22007-07-11 17:01:13 +00003408 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00003409 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00003410
Reid Spencer5f016e22007-07-11 17:01:13 +00003411 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003412 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroff5618bd42008-08-27 16:04:49 +00003413 if (Kind == tok::star)
3414 // Remember that we parsed a pointer type, and remember the type-quals.
3415 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
Chandler Carruthd067c072011-02-23 18:51:59 +00003416 DS.getConstSpecLoc(),
3417 DS.getVolatileSpecLoc(),
John McCall0b7e6782011-03-24 11:26:52 +00003418 DS.getRestrictSpecLoc()),
3419 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003420 SourceLocation());
Steve Naroff5618bd42008-08-27 16:04:49 +00003421 else
3422 // Remember that we parsed a Block type, and remember the type-quals.
Mike Stump1eb44332009-09-09 15:08:12 +00003423 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
John McCall0b7e6782011-03-24 11:26:52 +00003424 Loc),
3425 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003426 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00003427 } else {
3428 // Is a reference
John McCall0b7e6782011-03-24 11:26:52 +00003429 DeclSpec DS(AttrFactory);
Reid Spencer5f016e22007-07-11 17:01:13 +00003430
Sebastian Redl743de1f2009-03-23 00:00:23 +00003431 // Complain about rvalue references in C++03, but then go on and build
3432 // the declarator.
3433 if (Kind == tok::ampamp && !getLang().CPlusPlus0x)
Douglas Gregor16cf8f52011-01-25 02:17:32 +00003434 Diag(Loc, diag::ext_rvalue_reference);
Sebastian Redl743de1f2009-03-23 00:00:23 +00003435
Reid Spencer5f016e22007-07-11 17:01:13 +00003436 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
3437 // cv-qualifiers are introduced through the use of a typedef or of a
3438 // template type argument, in which case the cv-qualifiers are ignored.
3439 //
3440 // [GNU] Retricted references are allowed.
3441 // [GNU] Attributes on references are allowed.
Sean Huntbbd37c62009-11-21 08:43:09 +00003442 // [C++0x] Attributes on references are not allowed.
3443 ParseTypeQualifierListOpt(DS, true, false);
Sebastian Redlab197ba2009-02-09 18:23:29 +00003444 D.ExtendWithDeclSpec(DS);
Reid Spencer5f016e22007-07-11 17:01:13 +00003445
3446 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
3447 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
3448 Diag(DS.getConstSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00003449 diag::err_invalid_reference_qualifier_application) << "const";
Reid Spencer5f016e22007-07-11 17:01:13 +00003450 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
3451 Diag(DS.getVolatileSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00003452 diag::err_invalid_reference_qualifier_application) << "volatile";
Reid Spencer5f016e22007-07-11 17:01:13 +00003453 }
3454
3455 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003456 ParseDeclaratorInternal(D, DirectDeclParser);
Reid Spencer5f016e22007-07-11 17:01:13 +00003457
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00003458 if (D.getNumTypeObjects() > 0) {
3459 // C++ [dcl.ref]p4: There shall be no references to references.
3460 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
3461 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattnerda83bac2008-11-19 07:37:42 +00003462 if (const IdentifierInfo *II = D.getIdentifier())
3463 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
3464 << II;
3465 else
3466 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
3467 << "type name";
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00003468
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003469 // Once we've complained about the reference-to-reference, we
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00003470 // can go ahead and build the (technically ill-formed)
3471 // declarator: reference collapsing will take care of it.
3472 }
3473 }
3474
Reid Spencer5f016e22007-07-11 17:01:13 +00003475 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner76549142008-02-21 01:32:26 +00003476 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
Sebastian Redl05532f22009-03-15 22:02:01 +00003477 Kind == tok::amp),
John McCall0b7e6782011-03-24 11:26:52 +00003478 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003479 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00003480 }
3481}
3482
3483/// ParseDirectDeclarator
3484/// direct-declarator: [C99 6.7.5]
Douglas Gregor42a552f2008-11-05 20:51:48 +00003485/// [C99] identifier
Reid Spencer5f016e22007-07-11 17:01:13 +00003486/// '(' declarator ')'
3487/// [GNU] '(' attributes declarator ')'
3488/// [C90] direct-declarator '[' constant-expression[opt] ']'
3489/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
3490/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
3491/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
3492/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
3493/// direct-declarator '(' parameter-type-list ')'
3494/// direct-declarator '(' identifier-list[opt] ')'
3495/// [GNU] direct-declarator '(' parameter-forward-declarations
3496/// parameter-type-list[opt] ')'
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003497/// [C++] direct-declarator '(' parameter-declaration-clause ')'
3498/// cv-qualifier-seq[opt] exception-specification[opt]
Douglas Gregorb48fe382008-10-31 09:07:45 +00003499/// [C++] declarator-id
Douglas Gregor42a552f2008-11-05 20:51:48 +00003500///
3501/// declarator-id: [C++ 8]
Douglas Gregora8bc8c92010-12-23 22:44:42 +00003502/// '...'[opt] id-expression
Douglas Gregor42a552f2008-11-05 20:51:48 +00003503/// '::'[opt] nested-name-specifier[opt] type-name
3504///
3505/// id-expression: [C++ 5.1]
3506/// unqualified-id
Douglas Gregordb422df2009-09-25 21:45:23 +00003507/// qualified-id
Douglas Gregor42a552f2008-11-05 20:51:48 +00003508///
3509/// unqualified-id: [C++ 5.1]
Mike Stump1eb44332009-09-09 15:08:12 +00003510/// identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003511/// operator-function-id
Douglas Gregordb422df2009-09-25 21:45:23 +00003512/// conversion-function-id
Mike Stump1eb44332009-09-09 15:08:12 +00003513/// '~' class-name
Douglas Gregor39a8de12009-02-25 19:37:18 +00003514/// template-id
Argyrios Kyrtzidisc7ed9c62008-11-07 22:02:30 +00003515///
Reid Spencer5f016e22007-07-11 17:01:13 +00003516void Parser::ParseDirectDeclarator(Declarator &D) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00003517 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003518
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003519 if (getLang().CPlusPlus && D.mayHaveIdentifier()) {
3520 // ParseDeclaratorInternal might already have parsed the scope.
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00003521 if (D.getCXXScopeSpec().isEmpty()) {
John McCallb3d87482010-08-24 05:47:05 +00003522 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec(), ParsedType(), true);
John McCall9ba61662010-02-26 08:45:28 +00003523 }
3524
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00003525 if (D.getCXXScopeSpec().isValid()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00003526 if (Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
John McCalle7e278b2009-12-11 20:04:54 +00003527 // Change the declaration context for name lookup, until this function
3528 // is exited (and the declarator has been parsed).
3529 DeclScopeObj.EnterDeclaratorScope();
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00003530 }
3531
Douglas Gregora8bc8c92010-12-23 22:44:42 +00003532 // C++0x [dcl.fct]p14:
3533 // There is a syntactic ambiguity when an ellipsis occurs at the end
3534 // of a parameter-declaration-clause without a preceding comma. In
3535 // this case, the ellipsis is parsed as part of the
3536 // abstract-declarator if the type of the parameter names a template
3537 // parameter pack that has not been expanded; otherwise, it is parsed
3538 // as part of the parameter-declaration-clause.
3539 if (Tok.is(tok::ellipsis) &&
3540 !((D.getContext() == Declarator::PrototypeContext ||
3541 D.getContext() == Declarator::BlockLiteralContext) &&
Douglas Gregora8bc8c92010-12-23 22:44:42 +00003542 NextToken().is(tok::r_paren) &&
3543 !Actions.containsUnexpandedParameterPacks(D)))
3544 D.setEllipsisLoc(ConsumeToken());
3545
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003546 if (Tok.is(tok::identifier) || Tok.is(tok::kw_operator) ||
3547 Tok.is(tok::annot_template_id) || Tok.is(tok::tilde)) {
3548 // We found something that indicates the start of an unqualified-id.
3549 // Parse that unqualified-id.
John McCallba9d8532010-04-13 06:39:49 +00003550 bool AllowConstructorName;
3551 if (D.getDeclSpec().hasTypeSpecifier())
3552 AllowConstructorName = false;
3553 else if (D.getCXXScopeSpec().isSet())
3554 AllowConstructorName =
3555 (D.getContext() == Declarator::FileContext ||
3556 (D.getContext() == Declarator::MemberContext &&
3557 D.getDeclSpec().isFriendSpecified()));
3558 else
3559 AllowConstructorName = (D.getContext() == Declarator::MemberContext);
3560
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003561 if (ParseUnqualifiedId(D.getCXXScopeSpec(),
3562 /*EnteringContext=*/true,
3563 /*AllowDestructorName=*/true,
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003564 AllowConstructorName,
John McCallb3d87482010-08-24 05:47:05 +00003565 ParsedType(),
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00003566 D.getName()) ||
3567 // Once we're past the identifier, if the scope was bad, mark the
3568 // whole declarator bad.
3569 D.getCXXScopeSpec().isInvalid()) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00003570 D.SetIdentifier(0, Tok.getLocation());
3571 D.setInvalidType(true);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003572 } else {
3573 // Parsed the unqualified-id; update range information and move along.
3574 if (D.getSourceRange().getBegin().isInvalid())
3575 D.SetRangeBegin(D.getName().getSourceRange().getBegin());
3576 D.SetRangeEnd(D.getName().getSourceRange().getEnd());
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003577 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003578 goto PastIdentifier;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00003579 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003580 } else if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00003581 assert(!getLang().CPlusPlus &&
3582 "There's a C++-specific check for tok::identifier above");
3583 assert(Tok.getIdentifierInfo() && "Not an identifier?");
3584 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
3585 ConsumeToken();
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003586 goto PastIdentifier;
3587 }
3588
3589 if (Tok.is(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003590 // direct-declarator: '(' declarator ')'
3591 // direct-declarator: '(' attributes declarator ')'
3592 // Example: 'char (*X)' or 'int (*XX)(void)'
3593 ParseParenDeclarator(D);
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003594
3595 // If the declarator was parenthesized, we entered the declarator
3596 // scope when parsing the parenthesized declarator, then exited
3597 // the scope already. Re-enter the scope, if we need to.
3598 if (D.getCXXScopeSpec().isSet()) {
Fariborz Jahanian46877cd2010-08-17 23:50:37 +00003599 // If there was an error parsing parenthesized declarator, declarator
3600 // scope may have been enterred before. Don't do it again.
3601 if (!D.isInvalidType() &&
3602 Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003603 // Change the declaration context for name lookup, until this function
3604 // is exited (and the declarator has been parsed).
Fariborz Jahanian46877cd2010-08-17 23:50:37 +00003605 DeclScopeObj.EnterDeclaratorScope();
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003606 }
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00003607 } else if (D.mayOmitIdentifier()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003608 // This could be something simple like "int" (in which case the declarator
3609 // portion is empty), if an abstract-declarator is allowed.
3610 D.SetIdentifier(0, Tok.getLocation());
3611 } else {
Douglas Gregore950d4b2009-03-06 23:28:18 +00003612 if (D.getContext() == Declarator::MemberContext)
3613 Diag(Tok, diag::err_expected_member_name_or_semi)
3614 << D.getDeclSpec().getSourceRange();
3615 else if (getLang().CPlusPlus)
Douglas Gregor2d1c2142009-11-03 19:44:04 +00003616 Diag(Tok, diag::err_expected_unqualified_id) << getLang().CPlusPlus;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003617 else
Chris Lattner1ab3b962008-11-18 07:48:38 +00003618 Diag(Tok, diag::err_expected_ident_lparen);
Reid Spencer5f016e22007-07-11 17:01:13 +00003619 D.SetIdentifier(0, Tok.getLocation());
Chris Lattner1f6f54b2008-11-11 06:13:16 +00003620 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00003621 }
Mike Stump1eb44332009-09-09 15:08:12 +00003622
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00003623 PastIdentifier:
Reid Spencer5f016e22007-07-11 17:01:13 +00003624 assert(D.isPastIdentifier() &&
3625 "Haven't past the location of the identifier yet?");
Mike Stump1eb44332009-09-09 15:08:12 +00003626
Sean Huntbbd37c62009-11-21 08:43:09 +00003627 // Don't parse attributes unless we have an identifier.
John McCall7f040a92010-12-24 02:08:15 +00003628 if (D.getIdentifier())
3629 MaybeParseCXX0XAttributes(D);
Sean Huntbbd37c62009-11-21 08:43:09 +00003630
Reid Spencer5f016e22007-07-11 17:01:13 +00003631 while (1) {
Chris Lattner04d66662007-10-09 17:33:22 +00003632 if (Tok.is(tok::l_paren)) {
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00003633 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
3634 // In such a case, check if we actually have a function declarator; if it
3635 // is not, the declarator has been fully parsed.
Chris Lattner7399ee02008-10-20 02:05:46 +00003636 if (getLang().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
3637 // When not in file scope, warn for ambiguous function declarators, just
3638 // in case the author intended it as a variable definition.
3639 bool warnIfAmbiguous = D.getContext() != Declarator::FileContext;
3640 if (!isCXXFunctionDeclarator(warnIfAmbiguous))
3641 break;
3642 }
John McCall0b7e6782011-03-24 11:26:52 +00003643 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00003644 ParseFunctionDeclarator(ConsumeParen(), D, attrs);
Chris Lattner04d66662007-10-09 17:33:22 +00003645 } else if (Tok.is(tok::l_square)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003646 ParseBracketDeclarator(D);
3647 } else {
3648 break;
3649 }
3650 }
3651}
3652
Chris Lattneref4715c2008-04-06 05:45:57 +00003653/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
3654/// only called before the identifier, so these are most likely just grouping
Mike Stump1eb44332009-09-09 15:08:12 +00003655/// parens for precedence. If we find that these are actually function
Chris Lattneref4715c2008-04-06 05:45:57 +00003656/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
3657///
3658/// direct-declarator:
3659/// '(' declarator ')'
3660/// [GNU] '(' attributes declarator ')'
Chris Lattner7399ee02008-10-20 02:05:46 +00003661/// direct-declarator '(' parameter-type-list ')'
3662/// direct-declarator '(' identifier-list[opt] ')'
3663/// [GNU] direct-declarator '(' parameter-forward-declarations
3664/// parameter-type-list[opt] ')'
Chris Lattneref4715c2008-04-06 05:45:57 +00003665///
3666void Parser::ParseParenDeclarator(Declarator &D) {
3667 SourceLocation StartLoc = ConsumeParen();
3668 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
Mike Stump1eb44332009-09-09 15:08:12 +00003669
Chris Lattner7399ee02008-10-20 02:05:46 +00003670 // Eat any attributes before we look at whether this is a grouping or function
3671 // declarator paren. If this is a grouping paren, the attribute applies to
3672 // the type being built up, for example:
3673 // int (__attribute__(()) *x)(long y)
3674 // If this ends up not being a grouping paren, the attribute applies to the
3675 // first argument, for example:
3676 // int (__attribute__(()) int x)
3677 // In either case, we need to eat any attributes to be able to determine what
3678 // sort of paren this is.
3679 //
John McCall0b7e6782011-03-24 11:26:52 +00003680 ParsedAttributes attrs(AttrFactory);
Chris Lattner7399ee02008-10-20 02:05:46 +00003681 bool RequiresArg = false;
3682 if (Tok.is(tok::kw___attribute)) {
John McCall7f040a92010-12-24 02:08:15 +00003683 ParseGNUAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00003684
Chris Lattner7399ee02008-10-20 02:05:46 +00003685 // We require that the argument list (if this is a non-grouping paren) be
3686 // present even if the attribute list was empty.
3687 RequiresArg = true;
3688 }
Steve Naroff239f0732008-12-25 14:16:32 +00003689 // Eat any Microsoft extensions.
Eli Friedman290eeb02009-06-08 23:27:34 +00003690 if (Tok.is(tok::kw___cdecl) || Tok.is(tok::kw___stdcall) ||
Douglas Gregorf813a2c2010-05-18 16:57:00 +00003691 Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___fastcall) ||
Francois Pichet3bd9aa42011-08-18 09:59:55 +00003692 Tok.is(tok::kw___w64) || Tok.is(tok::kw___ptr64) ||
Francois Pichet58fd97a2011-08-25 00:36:46 +00003693 Tok.is(tok::kw___ptr32) || Tok.is(tok::kw___unaligned)) {
John McCall7f040a92010-12-24 02:08:15 +00003694 ParseMicrosoftTypeAttributes(attrs);
Eli Friedman290eeb02009-06-08 23:27:34 +00003695 }
Dawn Perchik52fc3142010-09-03 01:29:35 +00003696 // Eat any Borland extensions.
Ted Kremenek8113ecf2010-11-10 05:59:39 +00003697 if (Tok.is(tok::kw___pascal))
John McCall7f040a92010-12-24 02:08:15 +00003698 ParseBorlandTypeAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00003699
Chris Lattneref4715c2008-04-06 05:45:57 +00003700 // If we haven't past the identifier yet (or where the identifier would be
3701 // stored, if this is an abstract declarator), then this is probably just
3702 // grouping parens. However, if this could be an abstract-declarator, then
3703 // this could also be the start of function arguments (consider 'void()').
3704 bool isGrouping;
Mike Stump1eb44332009-09-09 15:08:12 +00003705
Chris Lattneref4715c2008-04-06 05:45:57 +00003706 if (!D.mayOmitIdentifier()) {
3707 // If this can't be an abstract-declarator, this *must* be a grouping
3708 // paren, because we haven't seen the identifier yet.
3709 isGrouping = true;
3710 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Argyrios Kyrtzidise25d2702008-10-06 00:07:55 +00003711 (getLang().CPlusPlus && Tok.is(tok::ellipsis)) || // C++ int(...)
Chris Lattneref4715c2008-04-06 05:45:57 +00003712 isDeclarationSpecifier()) { // 'int(int)' is a function.
3713 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
3714 // considered to be a type, not a K&R identifier-list.
3715 isGrouping = false;
3716 } else {
3717 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
3718 isGrouping = true;
3719 }
Mike Stump1eb44332009-09-09 15:08:12 +00003720
Chris Lattneref4715c2008-04-06 05:45:57 +00003721 // If this is a grouping paren, handle:
3722 // direct-declarator: '(' declarator ')'
3723 // direct-declarator: '(' attributes declarator ')'
3724 if (isGrouping) {
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00003725 bool hadGroupingParens = D.hasGroupingParens();
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00003726 D.setGroupingParens(true);
3727
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003728 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattneref4715c2008-04-06 05:45:57 +00003729 // Match the ')'.
Abramo Bagnara075f8f12010-12-10 16:29:40 +00003730 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_paren, StartLoc);
John McCall0b7e6782011-03-24 11:26:52 +00003731 D.AddTypeInfo(DeclaratorChunk::getParen(StartLoc, EndLoc),
3732 attrs, EndLoc);
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00003733
3734 D.setGroupingParens(hadGroupingParens);
Chris Lattneref4715c2008-04-06 05:45:57 +00003735 return;
3736 }
Mike Stump1eb44332009-09-09 15:08:12 +00003737
Chris Lattneref4715c2008-04-06 05:45:57 +00003738 // Okay, if this wasn't a grouping paren, it must be the start of a function
3739 // argument list. Recognize that this declarator will never have an
Chris Lattner7399ee02008-10-20 02:05:46 +00003740 // identifier (and remember where it would have been), then call into
3741 // ParseFunctionDeclarator to handle of argument list.
Chris Lattneref4715c2008-04-06 05:45:57 +00003742 D.SetIdentifier(0, Tok.getLocation());
3743
John McCall7f040a92010-12-24 02:08:15 +00003744 ParseFunctionDeclarator(StartLoc, D, attrs, RequiresArg);
Chris Lattneref4715c2008-04-06 05:45:57 +00003745}
3746
3747/// ParseFunctionDeclarator - We are after the identifier and have parsed the
3748/// declarator D up to a paren, which indicates that we are parsing function
3749/// arguments.
Reid Spencer5f016e22007-07-11 17:01:13 +00003750///
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00003751/// If attrs is non-null, then the caller parsed those arguments immediately
Chris Lattner7399ee02008-10-20 02:05:46 +00003752/// after the open paren - they should be considered to be the first argument of
3753/// a parameter. If RequiresArg is true, then the first argument of the
3754/// function is required to be present and required to not be an identifier
3755/// list.
3756///
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00003757/// For C++, after the parameter-list, it also parses cv-qualifier-seq[opt],
3758/// (C++0x) ref-qualifier[opt], exception-specification[opt], and
3759/// (C++0x) trailing-return-type[opt].
3760///
3761/// [C++0x] exception-specification:
3762/// dynamic-exception-specification
3763/// noexcept-specification
3764///
3765void Parser::ParseFunctionDeclarator(SourceLocation LParenLoc, Declarator &D,
3766 ParsedAttributes &attrs,
3767 bool RequiresArg) {
3768 // lparen is already consumed!
3769 assert(D.isPastIdentifier() && "Should not call before identifier!");
3770
3771 // This should be true when the function has typed arguments.
3772 // Otherwise, it is treated as a K&R-style function.
3773 bool HasProto = false;
3774 // Build up an array of information about the parsed arguments.
Chris Lattner5f9e2722011-07-23 10:55:15 +00003775 SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00003776 // Remember where we see an ellipsis, if any.
3777 SourceLocation EllipsisLoc;
3778
3779 DeclSpec DS(AttrFactory);
3780 bool RefQualifierIsLValueRef = true;
3781 SourceLocation RefQualifierLoc;
3782 ExceptionSpecificationType ESpecType = EST_None;
3783 SourceRange ESpecRange;
Chris Lattner5f9e2722011-07-23 10:55:15 +00003784 SmallVector<ParsedType, 2> DynamicExceptions;
3785 SmallVector<SourceRange, 2> DynamicExceptionRanges;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00003786 ExprResult NoexceptExpr;
3787 ParsedType TrailingReturnType;
3788
3789 SourceLocation EndLoc;
3790
3791 if (isFunctionDeclaratorIdentifierList()) {
3792 if (RequiresArg)
3793 Diag(Tok, diag::err_argument_required_after_attribute);
3794
3795 ParseFunctionDeclaratorIdentifierList(D, ParamInfo);
3796
3797 EndLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
3798 } else {
3799 // Enter function-declaration scope, limiting any declarators to the
3800 // function prototype scope, including parameter declarators.
3801 ParseScope PrototypeScope(this,
3802 Scope::FunctionPrototypeScope|Scope::DeclScope);
3803
3804 if (Tok.isNot(tok::r_paren))
3805 ParseParameterDeclarationClause(D, attrs, ParamInfo, EllipsisLoc);
3806 else if (RequiresArg)
3807 Diag(Tok, diag::err_argument_required_after_attribute);
3808
3809 HasProto = ParamInfo.size() || getLang().CPlusPlus;
3810
3811 // If we have the closing ')', eat it.
3812 EndLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
3813
3814 if (getLang().CPlusPlus) {
3815 MaybeParseCXX0XAttributes(attrs);
3816
3817 // Parse cv-qualifier-seq[opt].
3818 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
3819 if (!DS.getSourceRange().getEnd().isInvalid())
3820 EndLoc = DS.getSourceRange().getEnd();
3821
3822 // Parse ref-qualifier[opt].
3823 if (Tok.is(tok::amp) || Tok.is(tok::ampamp)) {
3824 if (!getLang().CPlusPlus0x)
3825 Diag(Tok, diag::ext_ref_qualifier);
3826
3827 RefQualifierIsLValueRef = Tok.is(tok::amp);
3828 RefQualifierLoc = ConsumeToken();
3829 EndLoc = RefQualifierLoc;
3830 }
3831
3832 // Parse exception-specification[opt].
3833 ESpecType = MaybeParseExceptionSpecification(ESpecRange,
3834 DynamicExceptions,
3835 DynamicExceptionRanges,
3836 NoexceptExpr);
3837 if (ESpecType != EST_None)
3838 EndLoc = ESpecRange.getEnd();
3839
3840 // Parse trailing-return-type[opt].
3841 if (getLang().CPlusPlus0x && Tok.is(tok::arrow)) {
Douglas Gregorae7902c2011-08-04 15:30:47 +00003842 SourceRange Range;
3843 TrailingReturnType = ParseTrailingReturnType(Range).get();
3844 if (Range.getEnd().isValid())
3845 EndLoc = Range.getEnd();
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00003846 }
3847 }
3848
3849 // Leave prototype scope.
3850 PrototypeScope.Exit();
3851 }
3852
3853 // Remember that we parsed a function type, and remember the attributes.
3854 D.AddTypeInfo(DeclaratorChunk::getFunction(HasProto,
3855 /*isVariadic=*/EllipsisLoc.isValid(),
3856 EllipsisLoc,
3857 ParamInfo.data(), ParamInfo.size(),
3858 DS.getTypeQualifiers(),
3859 RefQualifierIsLValueRef,
3860 RefQualifierLoc,
Douglas Gregor90ebed02011-07-13 21:47:47 +00003861 /*MutableLoc=*/SourceLocation(),
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00003862 ESpecType, ESpecRange.getBegin(),
3863 DynamicExceptions.data(),
3864 DynamicExceptionRanges.data(),
3865 DynamicExceptions.size(),
3866 NoexceptExpr.isUsable() ?
3867 NoexceptExpr.get() : 0,
3868 LParenLoc, EndLoc, D,
3869 TrailingReturnType),
3870 attrs, EndLoc);
3871}
3872
3873/// isFunctionDeclaratorIdentifierList - This parameter list may have an
3874/// identifier list form for a K&R-style function: void foo(a,b,c)
3875///
3876/// Note that identifier-lists are only allowed for normal declarators, not for
3877/// abstract-declarators.
3878bool Parser::isFunctionDeclaratorIdentifierList() {
3879 return !getLang().CPlusPlus
3880 && Tok.is(tok::identifier)
3881 && !TryAltiVecVectorToken()
3882 // K&R identifier lists can't have typedefs as identifiers, per C99
3883 // 6.7.5.3p11.
3884 && (TryAnnotateTypeOrScopeToken() || !Tok.is(tok::annot_typename))
3885 // Identifier lists follow a really simple grammar: the identifiers can
3886 // be followed *only* by a ", identifier" or ")". However, K&R
3887 // identifier lists are really rare in the brave new modern world, and
3888 // it is very common for someone to typo a type in a non-K&R style
3889 // list. If we are presented with something like: "void foo(intptr x,
3890 // float y)", we don't want to start parsing the function declarator as
3891 // though it is a K&R style declarator just because intptr is an
3892 // invalid type.
3893 //
3894 // To handle this, we check to see if the token after the first
3895 // identifier is a "," or ")". Only then do we parse it as an
3896 // identifier list.
3897 && (NextToken().is(tok::comma) || NextToken().is(tok::r_paren));
3898}
3899
3900/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
3901/// we found a K&R-style identifier list instead of a typed parameter list.
3902///
3903/// After returning, ParamInfo will hold the parsed parameters.
3904///
3905/// identifier-list: [C99 6.7.5]
3906/// identifier
3907/// identifier-list ',' identifier
3908///
3909void Parser::ParseFunctionDeclaratorIdentifierList(
3910 Declarator &D,
Chris Lattner5f9e2722011-07-23 10:55:15 +00003911 SmallVector<DeclaratorChunk::ParamInfo, 16> &ParamInfo) {
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00003912 // If there was no identifier specified for the declarator, either we are in
3913 // an abstract-declarator, or we are in a parameter declarator which was found
3914 // to be abstract. In abstract-declarators, identifier lists are not valid:
3915 // diagnose this.
3916 if (!D.getIdentifier())
3917 Diag(Tok, diag::ext_ident_list_in_param);
3918
3919 // Maintain an efficient lookup of params we have seen so far.
3920 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
3921
3922 while (1) {
3923 // If this isn't an identifier, report the error and skip until ')'.
3924 if (Tok.isNot(tok::identifier)) {
3925 Diag(Tok, diag::err_expected_ident);
3926 SkipUntil(tok::r_paren, /*StopAtSemi=*/true, /*DontConsume=*/true);
3927 // Forget we parsed anything.
3928 ParamInfo.clear();
3929 return;
3930 }
3931
3932 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
3933
3934 // Reject 'typedef int y; int test(x, y)', but continue parsing.
3935 if (Actions.getTypeName(*ParmII, Tok.getLocation(), getCurScope()))
3936 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
3937
3938 // Verify that the argument identifier has not already been mentioned.
3939 if (!ParamsSoFar.insert(ParmII)) {
3940 Diag(Tok, diag::err_param_redefinition) << ParmII;
3941 } else {
3942 // Remember this identifier in ParamInfo.
3943 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
3944 Tok.getLocation(),
3945 0));
3946 }
3947
3948 // Eat the identifier.
3949 ConsumeToken();
3950
3951 // The list continues if we see a comma.
3952 if (Tok.isNot(tok::comma))
3953 break;
3954 ConsumeToken();
3955 }
3956}
3957
3958/// ParseParameterDeclarationClause - Parse a (possibly empty) parameter-list
3959/// after the opening parenthesis. This function will not parse a K&R-style
3960/// identifier list.
3961///
3962/// D is the declarator being parsed. If attrs is non-null, then the caller
3963/// parsed those arguments immediately after the open paren - they should be
3964/// considered to be the first argument of a parameter.
3965///
3966/// After returning, ParamInfo will hold the parsed parameters. EllipsisLoc will
3967/// be the location of the ellipsis, if any was parsed.
3968///
Reid Spencer5f016e22007-07-11 17:01:13 +00003969/// parameter-type-list: [C99 6.7.5]
3970/// parameter-list
3971/// parameter-list ',' '...'
Douglas Gregored5d6512009-09-22 21:41:40 +00003972/// [C++] parameter-list '...'
Reid Spencer5f016e22007-07-11 17:01:13 +00003973///
3974/// parameter-list: [C99 6.7.5]
3975/// parameter-declaration
3976/// parameter-list ',' parameter-declaration
3977///
3978/// parameter-declaration: [C99 6.7.5]
3979/// declaration-specifiers declarator
Chris Lattner04421082008-04-08 04:40:51 +00003980/// [C++] declaration-specifiers declarator '=' assignment-expression
Reid Spencer5f016e22007-07-11 17:01:13 +00003981/// [GNU] declaration-specifiers declarator attributes
Sebastian Redl50de12f2009-03-24 22:27:57 +00003982/// declaration-specifiers abstract-declarator[opt]
3983/// [C++] declaration-specifiers abstract-declarator[opt]
Chris Lattner8123a952008-04-10 02:22:51 +00003984/// '=' assignment-expression
Reid Spencer5f016e22007-07-11 17:01:13 +00003985/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
3986///
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00003987void Parser::ParseParameterDeclarationClause(
3988 Declarator &D,
3989 ParsedAttributes &attrs,
Chris Lattner5f9e2722011-07-23 10:55:15 +00003990 SmallVector<DeclaratorChunk::ParamInfo, 16> &ParamInfo,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00003991 SourceLocation &EllipsisLoc) {
Mike Stump1eb44332009-09-09 15:08:12 +00003992
Chris Lattnerf97409f2008-04-06 06:57:35 +00003993 while (1) {
3994 if (Tok.is(tok::ellipsis)) {
Douglas Gregor965acbb2009-02-18 07:07:28 +00003995 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chris Lattnerf97409f2008-04-06 06:57:35 +00003996 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00003997 }
Mike Stump1eb44332009-09-09 15:08:12 +00003998
Chris Lattnerf97409f2008-04-06 06:57:35 +00003999 // Parse the declaration-specifiers.
John McCall54abf7d2009-11-04 02:18:39 +00004000 // Just use the ParsingDeclaration "scope" of the declarator.
John McCall0b7e6782011-03-24 11:26:52 +00004001 DeclSpec DS(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00004002
4003 // Skip any Microsoft attributes before a param.
4004 if (getLang().Microsoft && Tok.is(tok::l_square))
4005 ParseMicrosoftAttributes(DS.getAttributes());
4006
4007 SourceLocation DSStart = Tok.getLocation();
Chris Lattner7399ee02008-10-20 02:05:46 +00004008
4009 // If the caller parsed attributes for the first argument, add them now.
John McCall7f040a92010-12-24 02:08:15 +00004010 // Take them so that we only apply the attributes to the first parameter.
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004011 // FIXME: If we saw an ellipsis first, this code is not reached. Are the
4012 // attributes lost? Should they even be allowed?
4013 // FIXME: If we can leave the attributes in the token stream somehow, we can
4014 // get rid of a parameter (attrs) and this statement. It might be too much
4015 // hassle.
John McCall7f040a92010-12-24 02:08:15 +00004016 DS.takeAttributesFrom(attrs);
4017
Chris Lattnere64c5492009-02-27 18:38:20 +00004018 ParseDeclarationSpecifiers(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00004019
Chris Lattnerf97409f2008-04-06 06:57:35 +00004020 // Parse the declarator. This is "PrototypeContext", because we must
4021 // accept either 'declarator' or 'abstract-declarator' here.
4022 Declarator ParmDecl(DS, Declarator::PrototypeContext);
4023 ParseDeclarator(ParmDecl);
4024
4025 // Parse GNU attributes, if present.
John McCall7f040a92010-12-24 02:08:15 +00004026 MaybeParseGNUAttributes(ParmDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00004027
Chris Lattnerf97409f2008-04-06 06:57:35 +00004028 // Remember this parsed parameter in ParamInfo.
4029 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
Mike Stump1eb44332009-09-09 15:08:12 +00004030
Douglas Gregor72b505b2008-12-16 21:30:33 +00004031 // DefArgToks is used when the parsing of default arguments needs
4032 // to be delayed.
4033 CachedTokens *DefArgToks = 0;
4034
Chris Lattnerf97409f2008-04-06 06:57:35 +00004035 // If no parameter was specified, verify that *something* was specified,
4036 // otherwise we have a missing type and identifier.
Chris Lattnere64c5492009-02-27 18:38:20 +00004037 if (DS.isEmpty() && ParmDecl.getIdentifier() == 0 &&
4038 ParmDecl.getNumTypeObjects() == 0) {
Chris Lattnerf97409f2008-04-06 06:57:35 +00004039 // Completely missing, emit error.
4040 Diag(DSStart, diag::err_missing_param);
4041 } else {
4042 // Otherwise, we have something. Add it and let semantic analysis try
4043 // to grok it and add the result to the ParamInfo we are building.
Mike Stump1eb44332009-09-09 15:08:12 +00004044
Chris Lattnerf97409f2008-04-06 06:57:35 +00004045 // Inform the actions module about the parameter declarator, so it gets
4046 // added to the current scope.
John McCalld226f652010-08-21 09:40:31 +00004047 Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl);
Chris Lattner04421082008-04-08 04:40:51 +00004048
4049 // Parse the default argument, if any. We parse the default
4050 // arguments in all dialects; the semantic analysis in
4051 // ActOnParamDefaultArgument will reject the default argument in
4052 // C.
4053 if (Tok.is(tok::equal)) {
Douglas Gregor61366e92008-12-24 00:01:03 +00004054 SourceLocation EqualLoc = Tok.getLocation();
4055
Chris Lattner04421082008-04-08 04:40:51 +00004056 // Parse the default argument
Douglas Gregor72b505b2008-12-16 21:30:33 +00004057 if (D.getContext() == Declarator::MemberContext) {
4058 // If we're inside a class definition, cache the tokens
4059 // corresponding to the default argument. We'll actually parse
4060 // them when we see the end of the class definition.
4061 // FIXME: Templates will require something similar.
4062 // FIXME: Can we use a smart pointer for Toks?
4063 DefArgToks = new CachedTokens;
4064
Mike Stump1eb44332009-09-09 15:08:12 +00004065 if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks,
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +00004066 /*StopAtSemi=*/true,
4067 /*ConsumeFinalToken=*/false)) {
Douglas Gregor72b505b2008-12-16 21:30:33 +00004068 delete DefArgToks;
4069 DefArgToks = 0;
Douglas Gregor61366e92008-12-24 00:01:03 +00004070 Actions.ActOnParamDefaultArgumentError(Param);
Argyrios Kyrtzidis2b602ad2010-08-06 09:47:24 +00004071 } else {
4072 // Mark the end of the default argument so that we know when to
4073 // stop when we parse it later on.
4074 Token DefArgEnd;
4075 DefArgEnd.startToken();
4076 DefArgEnd.setKind(tok::cxx_defaultarg_end);
4077 DefArgEnd.setLocation(Tok.getLocation());
4078 DefArgToks->push_back(DefArgEnd);
Mike Stump1eb44332009-09-09 15:08:12 +00004079 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc,
Anders Carlsson5e300d12009-06-12 16:51:40 +00004080 (*DefArgToks)[1].getLocation());
Argyrios Kyrtzidis2b602ad2010-08-06 09:47:24 +00004081 }
Chris Lattner04421082008-04-08 04:40:51 +00004082 } else {
Douglas Gregor72b505b2008-12-16 21:30:33 +00004083 // Consume the '='.
Douglas Gregor61366e92008-12-24 00:01:03 +00004084 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00004085
Douglas Gregorbe0f7bd2010-09-11 20:24:53 +00004086 // The argument isn't actually potentially evaluated unless it is
4087 // used.
4088 EnterExpressionEvaluationContext Eval(Actions,
4089 Sema::PotentiallyEvaluatedIfUsed);
4090
John McCall60d7b3a2010-08-24 06:29:42 +00004091 ExprResult DefArgResult(ParseAssignmentExpression());
Douglas Gregor72b505b2008-12-16 21:30:33 +00004092 if (DefArgResult.isInvalid()) {
4093 Actions.ActOnParamDefaultArgumentError(Param);
4094 SkipUntil(tok::comma, tok::r_paren, true, true);
4095 } else {
4096 // Inform the actions module about the default argument
4097 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
John McCall9ae2f072010-08-23 23:25:46 +00004098 DefArgResult.take());
Douglas Gregor72b505b2008-12-16 21:30:33 +00004099 }
Chris Lattner04421082008-04-08 04:40:51 +00004100 }
4101 }
Mike Stump1eb44332009-09-09 15:08:12 +00004102
4103 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
4104 ParmDecl.getIdentifierLoc(), Param,
Douglas Gregor72b505b2008-12-16 21:30:33 +00004105 DefArgToks));
Chris Lattnerf97409f2008-04-06 06:57:35 +00004106 }
4107
4108 // If the next token is a comma, consume it and keep reading arguments.
Douglas Gregored5d6512009-09-22 21:41:40 +00004109 if (Tok.isNot(tok::comma)) {
4110 if (Tok.is(tok::ellipsis)) {
Douglas Gregored5d6512009-09-22 21:41:40 +00004111 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
4112
4113 if (!getLang().CPlusPlus) {
4114 // We have ellipsis without a preceding ',', which is ill-formed
4115 // in C. Complain and provide the fix.
4116 Diag(EllipsisLoc, diag::err_missing_comma_before_ellipsis)
Douglas Gregor849b2432010-03-31 17:46:05 +00004117 << FixItHint::CreateInsertion(EllipsisLoc, ", ");
Douglas Gregored5d6512009-09-22 21:41:40 +00004118 }
4119 }
4120
4121 break;
4122 }
Mike Stump1eb44332009-09-09 15:08:12 +00004123
Chris Lattnerf97409f2008-04-06 06:57:35 +00004124 // Consume the comma.
4125 ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00004126 }
Mike Stump1eb44332009-09-09 15:08:12 +00004127
Chris Lattner66d28652008-04-06 06:34:08 +00004128}
Chris Lattneref4715c2008-04-06 05:45:57 +00004129
Reid Spencer5f016e22007-07-11 17:01:13 +00004130/// [C90] direct-declarator '[' constant-expression[opt] ']'
4131/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
4132/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
4133/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
4134/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
4135void Parser::ParseBracketDeclarator(Declarator &D) {
4136 SourceLocation StartLoc = ConsumeBracket();
Mike Stump1eb44332009-09-09 15:08:12 +00004137
Chris Lattner378c7e42008-12-18 07:27:21 +00004138 // C array syntax has many features, but by-far the most common is [] and [4].
4139 // This code does a fast path to handle some of the most obvious cases.
4140 if (Tok.getKind() == tok::r_square) {
Sebastian Redlab197ba2009-02-09 18:23:29 +00004141 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
John McCall0b7e6782011-03-24 11:26:52 +00004142 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00004143 MaybeParseCXX0XAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00004144
Chris Lattner378c7e42008-12-18 07:27:21 +00004145 // Remember that we parsed the empty array type.
John McCall60d7b3a2010-08-24 06:29:42 +00004146 ExprResult NumElements;
John McCall0b7e6782011-03-24 11:26:52 +00004147 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00004148 StartLoc, EndLoc),
John McCall0b7e6782011-03-24 11:26:52 +00004149 attrs, EndLoc);
Chris Lattner378c7e42008-12-18 07:27:21 +00004150 return;
4151 } else if (Tok.getKind() == tok::numeric_constant &&
4152 GetLookAheadToken(1).is(tok::r_square)) {
4153 // [4] is very common. Parse the numeric constant expression.
John McCall60d7b3a2010-08-24 06:29:42 +00004154 ExprResult ExprRes(Actions.ActOnNumericConstant(Tok));
Chris Lattner378c7e42008-12-18 07:27:21 +00004155 ConsumeToken();
4156
Sebastian Redlab197ba2009-02-09 18:23:29 +00004157 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
John McCall0b7e6782011-03-24 11:26:52 +00004158 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00004159 MaybeParseCXX0XAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00004160
Chris Lattner378c7e42008-12-18 07:27:21 +00004161 // Remember that we parsed a array type, and remember its features.
John McCall0b7e6782011-03-24 11:26:52 +00004162 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, 0,
John McCall7f040a92010-12-24 02:08:15 +00004163 ExprRes.release(),
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00004164 StartLoc, EndLoc),
John McCall0b7e6782011-03-24 11:26:52 +00004165 attrs, EndLoc);
Chris Lattner378c7e42008-12-18 07:27:21 +00004166 return;
4167 }
Mike Stump1eb44332009-09-09 15:08:12 +00004168
Reid Spencer5f016e22007-07-11 17:01:13 +00004169 // If valid, this location is the position where we read the 'static' keyword.
4170 SourceLocation StaticLoc;
Chris Lattner04d66662007-10-09 17:33:22 +00004171 if (Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00004172 StaticLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00004173
Reid Spencer5f016e22007-07-11 17:01:13 +00004174 // If there is a type-qualifier-list, read it now.
Chris Lattnera1fcbad2008-12-18 06:50:14 +00004175 // Type qualifiers in an array subscript are a C99 feature.
John McCall0b7e6782011-03-24 11:26:52 +00004176 DeclSpec DS(AttrFactory);
Chris Lattner5a69d1c2008-12-18 07:02:59 +00004177 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Mike Stump1eb44332009-09-09 15:08:12 +00004178
Reid Spencer5f016e22007-07-11 17:01:13 +00004179 // If we haven't already read 'static', check to see if there is one after the
4180 // type-qualifier-list.
Chris Lattner04d66662007-10-09 17:33:22 +00004181 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00004182 StaticLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00004183
Reid Spencer5f016e22007-07-11 17:01:13 +00004184 // Handle "direct-declarator [ type-qual-list[opt] * ]".
4185 bool isStar = false;
John McCall60d7b3a2010-08-24 06:29:42 +00004186 ExprResult NumElements;
Mike Stump1eb44332009-09-09 15:08:12 +00004187
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00004188 // Handle the case where we have '[*]' as the array size. However, a leading
4189 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
4190 // the the token after the star is a ']'. Since stars in arrays are
4191 // infrequent, use of lookahead is not costly here.
4192 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattnera711dd02008-04-06 05:27:21 +00004193 ConsumeToken(); // Eat the '*'.
Reid Spencer5f016e22007-07-11 17:01:13 +00004194
Chris Lattnera1fcbad2008-12-18 06:50:14 +00004195 if (StaticLoc.isValid()) {
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00004196 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattnera1fcbad2008-12-18 06:50:14 +00004197 StaticLoc = SourceLocation(); // Drop the static.
4198 }
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00004199 isStar = true;
Chris Lattner04d66662007-10-09 17:33:22 +00004200 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner378c7e42008-12-18 07:27:21 +00004201 // Note, in C89, this production uses the constant-expr production instead
4202 // of assignment-expr. The only difference is that assignment-expr allows
4203 // things like '=' and '*='. Sema rejects these in C89 mode because they
4204 // are not i-c-e's, so we don't need to distinguish between the two here.
Mike Stump1eb44332009-09-09 15:08:12 +00004205
Douglas Gregore0762c92009-06-19 23:52:42 +00004206 // Parse the constant-expression or assignment-expression now (depending
4207 // on dialect).
4208 if (getLang().CPlusPlus)
4209 NumElements = ParseConstantExpression();
4210 else
4211 NumElements = ParseAssignmentExpression();
Reid Spencer5f016e22007-07-11 17:01:13 +00004212 }
Mike Stump1eb44332009-09-09 15:08:12 +00004213
Reid Spencer5f016e22007-07-11 17:01:13 +00004214 // If there was an error parsing the assignment-expression, recover.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00004215 if (NumElements.isInvalid()) {
Chris Lattner5cb10d32009-04-24 22:30:50 +00004216 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00004217 // If the expression was invalid, skip it.
4218 SkipUntil(tok::r_square);
4219 return;
4220 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00004221
4222 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
4223
John McCall0b7e6782011-03-24 11:26:52 +00004224 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00004225 MaybeParseCXX0XAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00004226
Chris Lattner378c7e42008-12-18 07:27:21 +00004227 // Remember that we parsed a array type, and remember its features.
John McCall0b7e6782011-03-24 11:26:52 +00004228 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
Reid Spencer5f016e22007-07-11 17:01:13 +00004229 StaticLoc.isValid(), isStar,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00004230 NumElements.release(),
4231 StartLoc, EndLoc),
John McCall0b7e6782011-03-24 11:26:52 +00004232 attrs, EndLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00004233}
4234
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004235/// [GNU] typeof-specifier:
4236/// typeof ( expressions )
4237/// typeof ( type-name )
4238/// [GNU/C++] typeof unary-expression
Steve Naroffd1861fd2007-07-31 12:34:36 +00004239///
4240void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner04d66662007-10-09 17:33:22 +00004241 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004242 Token OpTok = Tok;
Steve Naroffd1861fd2007-07-31 12:34:36 +00004243 SourceLocation StartLoc = ConsumeToken();
4244
John McCallcfb708c2010-01-13 20:03:27 +00004245 const bool hasParens = Tok.is(tok::l_paren);
4246
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004247 bool isCastExpr;
John McCallb3d87482010-08-24 05:47:05 +00004248 ParsedType CastTy;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004249 SourceRange CastRange;
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00004250 ExprResult Operand = ParseExprAfterUnaryExprOrTypeTrait(OpTok, isCastExpr,
4251 CastTy, CastRange);
John McCallcfb708c2010-01-13 20:03:27 +00004252 if (hasParens)
4253 DS.setTypeofParensRange(CastRange);
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004254
4255 if (CastRange.getEnd().isInvalid())
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004256 // FIXME: Not accurate, the range gets one token more than it should.
4257 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004258 else
4259 DS.SetRangeEnd(CastRange.getEnd());
Mike Stump1eb44332009-09-09 15:08:12 +00004260
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004261 if (isCastExpr) {
4262 if (!CastTy) {
4263 DS.SetTypeSpecError();
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004264 return;
Douglas Gregor809070a2009-02-18 17:45:20 +00004265 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004266
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004267 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00004268 unsigned DiagID;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004269 // Check for duplicate type specifiers (e.g. "int typeof(int)").
4270 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00004271 DiagID, CastTy))
4272 Diag(StartLoc, DiagID) << PrevSpec;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004273 return;
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004274 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004275
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004276 // If we get here, the operand to the typeof was an expresion.
4277 if (Operand.isInvalid()) {
4278 DS.SetTypeSpecError();
Steve Naroff9dfa7b42007-08-02 02:53:48 +00004279 return;
Steve Naroffd1861fd2007-07-31 12:34:36 +00004280 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004281
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004282 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00004283 unsigned DiagID;
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004284 // Check for duplicate type specifiers (e.g. "int typeof(int)").
4285 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
John McCallb3d87482010-08-24 05:47:05 +00004286 DiagID, Operand.get()))
John McCallfec54012009-08-03 20:12:06 +00004287 Diag(StartLoc, DiagID) << PrevSpec;
Steve Naroffd1861fd2007-07-31 12:34:36 +00004288}
Chris Lattner1b492422010-02-28 18:33:55 +00004289
4290
4291/// TryAltiVecVectorTokenOutOfLine - Out of line body that should only be called
4292/// from TryAltiVecVectorToken.
4293bool Parser::TryAltiVecVectorTokenOutOfLine() {
4294 Token Next = NextToken();
4295 switch (Next.getKind()) {
4296 default: return false;
4297 case tok::kw_short:
4298 case tok::kw_long:
4299 case tok::kw_signed:
4300 case tok::kw_unsigned:
4301 case tok::kw_void:
4302 case tok::kw_char:
4303 case tok::kw_int:
4304 case tok::kw_float:
4305 case tok::kw_double:
4306 case tok::kw_bool:
4307 case tok::kw___pixel:
4308 Tok.setKind(tok::kw___vector);
4309 return true;
4310 case tok::identifier:
4311 if (Next.getIdentifierInfo() == Ident_pixel) {
4312 Tok.setKind(tok::kw___vector);
4313 return true;
4314 }
4315 return false;
4316 }
4317}
4318
4319bool Parser::TryAltiVecTokenOutOfLine(DeclSpec &DS, SourceLocation Loc,
4320 const char *&PrevSpec, unsigned &DiagID,
4321 bool &isInvalid) {
4322 if (Tok.getIdentifierInfo() == Ident_vector) {
4323 Token Next = NextToken();
4324 switch (Next.getKind()) {
4325 case tok::kw_short:
4326 case tok::kw_long:
4327 case tok::kw_signed:
4328 case tok::kw_unsigned:
4329 case tok::kw_void:
4330 case tok::kw_char:
4331 case tok::kw_int:
4332 case tok::kw_float:
4333 case tok::kw_double:
4334 case tok::kw_bool:
4335 case tok::kw___pixel:
4336 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
4337 return true;
4338 case tok::identifier:
4339 if (Next.getIdentifierInfo() == Ident_pixel) {
4340 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
4341 return true;
4342 }
4343 break;
4344 default:
4345 break;
4346 }
Douglas Gregora8f031f2010-06-16 15:28:57 +00004347 } else if ((Tok.getIdentifierInfo() == Ident_pixel) &&
Chris Lattner1b492422010-02-28 18:33:55 +00004348 DS.isTypeAltiVecVector()) {
4349 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
4350 return true;
4351 }
4352 return false;
4353}