blob: 022fe3615789b944e6444e9514621440b4a4b86d [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"
22using namespace clang;
23
24//===----------------------------------------------------------------------===//
25// C99 6.7: Declarations.
26//===----------------------------------------------------------------------===//
27
28/// ParseTypeName
29/// type-name: [C99 6.7.6]
30/// specifier-qualifier-list abstract-declarator[opt]
Sebastian Redl4c5d3202008-11-21 19:14:01 +000031///
32/// Called type-id in C++.
Douglas Gregor683a81f2011-01-31 16:09:46 +000033TypeResult Parser::ParseTypeName(SourceRange *Range,
John McCallf85e1932011-06-15 23:02:42 +000034 Declarator::TheContext Context,
Richard Smithc89edf52011-07-01 19:46:12 +000035 ObjCDeclSpec *objcQuals,
36 AccessSpecifier AS,
37 Decl **OwnedType) {
Reid Spencer5f016e22007-07-11 17:01:13 +000038 // Parse the common declaration-specifiers piece.
John McCall0b7e6782011-03-24 11:26:52 +000039 DeclSpec DS(AttrFactory);
John McCallf85e1932011-06-15 23:02:42 +000040 DS.setObjCQualifiers(objcQuals);
Richard Smithc89edf52011-07-01 19:46:12 +000041 ParseSpecifierQualifierList(DS, AS);
42 if (OwnedType)
43 *OwnedType = DS.isTypeSpecOwned() ? DS.getRepAsDecl() : 0;
Sebastian Redlef65f062009-05-29 18:02:33 +000044
Reid Spencer5f016e22007-07-11 17:01:13 +000045 // Parse the abstract-declarator, if present.
Douglas Gregor683a81f2011-01-31 16:09:46 +000046 Declarator DeclaratorInfo(DS, Context);
Reid Spencer5f016e22007-07-11 17:01:13 +000047 ParseDeclarator(DeclaratorInfo);
Sebastian Redlef65f062009-05-29 18:02:33 +000048 if (Range)
49 *Range = DeclaratorInfo.getSourceRange();
50
Chris Lattnereaaebc72009-04-25 08:06:05 +000051 if (DeclaratorInfo.isInvalidType())
Douglas Gregor809070a2009-02-18 17:45:20 +000052 return true;
53
Douglas Gregor23c94db2010-07-02 17:43:08 +000054 return Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
Reid Spencer5f016e22007-07-11 17:01:13 +000055}
56
Sean Huntbbd37c62009-11-21 08:43:09 +000057/// ParseGNUAttributes - Parse a non-empty attributes list.
Reid Spencer5f016e22007-07-11 17:01:13 +000058///
59/// [GNU] attributes:
60/// attribute
61/// attributes attribute
62///
63/// [GNU] attribute:
64/// '__attribute__' '(' '(' attribute-list ')' ')'
65///
66/// [GNU] attribute-list:
67/// attrib
68/// attribute_list ',' attrib
69///
70/// [GNU] attrib:
71/// empty
72/// attrib-name
73/// attrib-name '(' identifier ')'
74/// attrib-name '(' identifier ',' nonempty-expr-list ')'
75/// attrib-name '(' argument-expression-list [C99 6.5.2] ')'
76///
77/// [GNU] attrib-name:
78/// identifier
79/// typespec
80/// typequal
81/// storageclass
Mike Stump1eb44332009-09-09 15:08:12 +000082///
Reid Spencer5f016e22007-07-11 17:01:13 +000083/// FIXME: The GCC grammar/code for this construct implies we need two
Mike Stump1eb44332009-09-09 15:08:12 +000084/// token lookahead. Comment from gcc: "If they start with an identifier
85/// which is followed by a comma or close parenthesis, then the arguments
Reid Spencer5f016e22007-07-11 17:01:13 +000086/// start with that identifier; otherwise they are an expression list."
87///
88/// At the moment, I am not doing 2 token lookahead. I am also unaware of
89/// any attributes that don't work (based on my limited testing). Most
90/// attributes are very simple in practice. Until we find a bug, I don't see
91/// a pressing need to implement the 2 token lookahead.
92
John McCall7f040a92010-12-24 02:08:15 +000093void Parser::ParseGNUAttributes(ParsedAttributes &attrs,
94 SourceLocation *endLoc) {
Sean Huntbbd37c62009-11-21 08:43:09 +000095 assert(Tok.is(tok::kw___attribute) && "Not a GNU attribute list!");
Mike Stump1eb44332009-09-09 15:08:12 +000096
Chris Lattner04d66662007-10-09 17:33:22 +000097 while (Tok.is(tok::kw___attribute)) {
Reid Spencer5f016e22007-07-11 17:01:13 +000098 ConsumeToken();
99 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
100 "attribute")) {
101 SkipUntil(tok::r_paren, true); // skip until ) or ;
John McCall7f040a92010-12-24 02:08:15 +0000102 return;
Reid Spencer5f016e22007-07-11 17:01:13 +0000103 }
104 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, "(")) {
105 SkipUntil(tok::r_paren, true); // skip until ) or ;
John McCall7f040a92010-12-24 02:08:15 +0000106 return;
Reid Spencer5f016e22007-07-11 17:01:13 +0000107 }
108 // Parse the attribute-list. e.g. __attribute__(( weak, alias("__f") ))
Chris Lattner04d66662007-10-09 17:33:22 +0000109 while (Tok.is(tok::identifier) || isDeclarationSpecifier() ||
110 Tok.is(tok::comma)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000111
112 if (Tok.is(tok::comma)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000113 // allows for empty/non-empty attributes. ((__vector_size__(16),,,,))
114 ConsumeToken();
115 continue;
116 }
117 // we have an identifier or declaration specifier (const, int, etc.)
118 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
119 SourceLocation AttrNameLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000120
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000121 // Availability attributes have their own grammar.
122 if (AttrName->isStr("availability"))
123 ParseAvailabilityAttribute(*AttrName, AttrNameLoc, attrs, endLoc);
Douglas Gregorec1afbf2010-03-16 19:09:18 +0000124 // check if we have a "parameterized" attribute
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000125 else if (Tok.is(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000126 ConsumeParen(); // ignore the left paren loc for now
Mike Stump1eb44332009-09-09 15:08:12 +0000127
Chris Lattner04d66662007-10-09 17:33:22 +0000128 if (Tok.is(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000129 IdentifierInfo *ParmName = Tok.getIdentifierInfo();
130 SourceLocation ParmLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000131
132 if (Tok.is(tok::r_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000133 // __attribute__(( mode(byte) ))
134 ConsumeParen(); // ignore the right paren loc for now
John McCall0b7e6782011-03-24 11:26:52 +0000135 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc,
136 ParmName, ParmLoc, 0, 0);
Chris Lattner04d66662007-10-09 17:33:22 +0000137 } else if (Tok.is(tok::comma)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000138 ConsumeToken();
139 // __attribute__(( format(printf, 1, 2) ))
Sebastian Redla55e52c2008-11-25 22:21:31 +0000140 ExprVector ArgExprs(Actions);
Reid Spencer5f016e22007-07-11 17:01:13 +0000141 bool ArgExprsOk = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000142
Reid Spencer5f016e22007-07-11 17:01:13 +0000143 // now parse the non-empty comma separated list of expressions
144 while (1) {
John McCall60d7b3a2010-08-24 06:29:42 +0000145 ExprResult ArgExpr(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000146 if (ArgExpr.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000147 ArgExprsOk = false;
148 SkipUntil(tok::r_paren);
149 break;
150 } else {
Sebastian Redleffa8d12008-12-10 00:02:53 +0000151 ArgExprs.push_back(ArgExpr.release());
Reid Spencer5f016e22007-07-11 17:01:13 +0000152 }
Chris Lattner04d66662007-10-09 17:33:22 +0000153 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +0000154 break;
155 ConsumeToken(); // Eat the comma, move to the next argument
156 }
Chris Lattner04d66662007-10-09 17:33:22 +0000157 if (ArgExprsOk && Tok.is(tok::r_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000158 ConsumeParen(); // ignore the right paren loc for now
John McCall0b7e6782011-03-24 11:26:52 +0000159 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc,
160 ParmName, ParmLoc, ArgExprs.take(), ArgExprs.size());
Reid Spencer5f016e22007-07-11 17:01:13 +0000161 }
162 }
163 } else { // not an identifier
Nate Begeman6f3d8382009-06-26 06:32:41 +0000164 switch (Tok.getKind()) {
165 case tok::r_paren:
Reid Spencer5f016e22007-07-11 17:01:13 +0000166 // parse a possibly empty comma separated list of expressions
Reid Spencer5f016e22007-07-11 17:01:13 +0000167 // __attribute__(( nonnull() ))
168 ConsumeParen(); // ignore the right paren loc for now
John McCall0b7e6782011-03-24 11:26:52 +0000169 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc,
170 0, SourceLocation(), 0, 0);
Nate Begeman6f3d8382009-06-26 06:32:41 +0000171 break;
172 case tok::kw_char:
173 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000174 case tok::kw_char16_t:
175 case tok::kw_char32_t:
Nate Begeman6f3d8382009-06-26 06:32:41 +0000176 case tok::kw_bool:
177 case tok::kw_short:
178 case tok::kw_int:
179 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +0000180 case tok::kw___int64:
Nate Begeman6f3d8382009-06-26 06:32:41 +0000181 case tok::kw_signed:
182 case tok::kw_unsigned:
183 case tok::kw_float:
184 case tok::kw_double:
185 case tok::kw_void:
John McCall7f040a92010-12-24 02:08:15 +0000186 case tok::kw_typeof: {
187 AttributeList *attr
John McCall0b7e6782011-03-24 11:26:52 +0000188 = attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc,
189 0, SourceLocation(), 0, 0);
John McCall7f040a92010-12-24 02:08:15 +0000190 if (attr->getKind() == AttributeList::AT_IBOutletCollection)
Fariborz Jahanian1b72fa72010-08-17 23:19:16 +0000191 Diag(Tok, diag::err_iboutletcollection_builtintype);
Nate Begeman6f3d8382009-06-26 06:32:41 +0000192 // If it's a builtin type name, eat it and expect a rparen
193 // __attribute__(( vec_type_hint(char) ))
194 ConsumeToken();
Nate Begeman6f3d8382009-06-26 06:32:41 +0000195 if (Tok.is(tok::r_paren))
196 ConsumeParen();
197 break;
John McCall7f040a92010-12-24 02:08:15 +0000198 }
Nate Begeman6f3d8382009-06-26 06:32:41 +0000199 default:
Reid Spencer5f016e22007-07-11 17:01:13 +0000200 // __attribute__(( aligned(16) ))
Sebastian Redla55e52c2008-11-25 22:21:31 +0000201 ExprVector ArgExprs(Actions);
Reid Spencer5f016e22007-07-11 17:01:13 +0000202 bool ArgExprsOk = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000203
Reid Spencer5f016e22007-07-11 17:01:13 +0000204 // now parse the list of expressions
205 while (1) {
John McCall60d7b3a2010-08-24 06:29:42 +0000206 ExprResult ArgExpr(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000207 if (ArgExpr.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000208 ArgExprsOk = false;
209 SkipUntil(tok::r_paren);
210 break;
211 } else {
Sebastian Redleffa8d12008-12-10 00:02:53 +0000212 ArgExprs.push_back(ArgExpr.release());
Reid Spencer5f016e22007-07-11 17:01:13 +0000213 }
Chris Lattner04d66662007-10-09 17:33:22 +0000214 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +0000215 break;
216 ConsumeToken(); // Eat the comma, move to the next argument
217 }
218 // Match the ')'.
Chris Lattner04d66662007-10-09 17:33:22 +0000219 if (ArgExprsOk && Tok.is(tok::r_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000220 ConsumeParen(); // ignore the right paren loc for now
John McCall0b7e6782011-03-24 11:26:52 +0000221 attrs.addNew(AttrName, AttrNameLoc, 0,
222 AttrNameLoc, 0, SourceLocation(),
223 ArgExprs.take(), ArgExprs.size());
Reid Spencer5f016e22007-07-11 17:01:13 +0000224 }
Nate Begeman6f3d8382009-06-26 06:32:41 +0000225 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000226 }
227 }
228 } else {
John McCall0b7e6782011-03-24 11:26:52 +0000229 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc,
230 0, SourceLocation(), 0, 0);
Reid Spencer5f016e22007-07-11 17:01:13 +0000231 }
232 }
233 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
Reid Spencer5f016e22007-07-11 17:01:13 +0000234 SkipUntil(tok::r_paren, false);
Sean Huntbbd37c62009-11-21 08:43:09 +0000235 SourceLocation Loc = Tok.getLocation();
Sebastian Redlab197ba2009-02-09 18:23:29 +0000236 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) {
237 SkipUntil(tok::r_paren, false);
238 }
John McCall7f040a92010-12-24 02:08:15 +0000239 if (endLoc)
240 *endLoc = Loc;
Reid Spencer5f016e22007-07-11 17:01:13 +0000241 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000242}
243
Eli Friedmana23b4852009-06-08 07:21:15 +0000244/// ParseMicrosoftDeclSpec - Parse an __declspec construct
245///
246/// [MS] decl-specifier:
247/// __declspec ( extended-decl-modifier-seq )
248///
249/// [MS] extended-decl-modifier-seq:
250/// extended-decl-modifier[opt]
251/// extended-decl-modifier extended-decl-modifier-seq
252
John McCall7f040a92010-12-24 02:08:15 +0000253void Parser::ParseMicrosoftDeclSpec(ParsedAttributes &attrs) {
Steve Narofff59e17e2008-12-24 20:59:21 +0000254 assert(Tok.is(tok::kw___declspec) && "Not a declspec!");
Eli Friedmana23b4852009-06-08 07:21:15 +0000255
Steve Narofff59e17e2008-12-24 20:59:21 +0000256 ConsumeToken();
Eli Friedmana23b4852009-06-08 07:21:15 +0000257 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
258 "declspec")) {
259 SkipUntil(tok::r_paren, true); // skip until ) or ;
John McCall7f040a92010-12-24 02:08:15 +0000260 return;
Eli Friedmana23b4852009-06-08 07:21:15 +0000261 }
Francois Pichet373197b2011-05-07 19:04:49 +0000262
Eli Friedman290eeb02009-06-08 23:27:34 +0000263 while (Tok.getIdentifierInfo()) {
Eli Friedmana23b4852009-06-08 07:21:15 +0000264 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
265 SourceLocation AttrNameLoc = ConsumeToken();
Francois Pichet373197b2011-05-07 19:04:49 +0000266
267 // FIXME: Remove this when we have proper __declspec(property()) support.
268 // Just skip everything inside property().
269 if (AttrName->getName() == "property") {
270 ConsumeParen();
271 SkipUntil(tok::r_paren);
272 }
Eli Friedmana23b4852009-06-08 07:21:15 +0000273 if (Tok.is(tok::l_paren)) {
274 ConsumeParen();
275 // FIXME: This doesn't parse __declspec(property(get=get_func_name))
276 // correctly.
John McCall60d7b3a2010-08-24 06:29:42 +0000277 ExprResult ArgExpr(ParseAssignmentExpression());
Eli Friedmana23b4852009-06-08 07:21:15 +0000278 if (!ArgExpr.isInvalid()) {
John McCallca0408f2010-08-23 06:44:23 +0000279 Expr *ExprList = ArgExpr.take();
John McCall0b7e6782011-03-24 11:26:52 +0000280 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
281 SourceLocation(), &ExprList, 1, true);
Eli Friedmana23b4852009-06-08 07:21:15 +0000282 }
283 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
284 SkipUntil(tok::r_paren, false);
285 } else {
John McCall0b7e6782011-03-24 11:26:52 +0000286 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc,
287 0, SourceLocation(), 0, 0, true);
Eli Friedmana23b4852009-06-08 07:21:15 +0000288 }
289 }
290 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
291 SkipUntil(tok::r_paren, false);
John McCall7f040a92010-12-24 02:08:15 +0000292 return;
Eli Friedman290eeb02009-06-08 23:27:34 +0000293}
294
John McCall7f040a92010-12-24 02:08:15 +0000295void Parser::ParseMicrosoftTypeAttributes(ParsedAttributes &attrs) {
Eli Friedman290eeb02009-06-08 23:27:34 +0000296 // Treat these like attributes
297 // FIXME: Allow Sema to distinguish between these and real attributes!
298 while (Tok.is(tok::kw___fastcall) || Tok.is(tok::kw___stdcall) ||
Douglas Gregorf813a2c2010-05-18 16:57:00 +0000299 Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___cdecl) ||
300 Tok.is(tok::kw___ptr64) || Tok.is(tok::kw___w64)) {
Eli Friedman290eeb02009-06-08 23:27:34 +0000301 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
302 SourceLocation AttrNameLoc = ConsumeToken();
303 if (Tok.is(tok::kw___ptr64) || Tok.is(tok::kw___w64))
304 // FIXME: Support these properly!
305 continue;
John McCall0b7e6782011-03-24 11:26:52 +0000306 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
307 SourceLocation(), 0, 0, true);
Eli Friedman290eeb02009-06-08 23:27:34 +0000308 }
Steve Narofff59e17e2008-12-24 20:59:21 +0000309}
310
John McCall7f040a92010-12-24 02:08:15 +0000311void Parser::ParseBorlandTypeAttributes(ParsedAttributes &attrs) {
Dawn Perchik52fc3142010-09-03 01:29:35 +0000312 // Treat these like attributes
313 while (Tok.is(tok::kw___pascal)) {
314 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
315 SourceLocation AttrNameLoc = ConsumeToken();
John McCall0b7e6782011-03-24 11:26:52 +0000316 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
317 SourceLocation(), 0, 0, true);
Dawn Perchik52fc3142010-09-03 01:29:35 +0000318 }
John McCall7f040a92010-12-24 02:08:15 +0000319}
320
Peter Collingbournef315fa82011-02-14 01:42:53 +0000321void Parser::ParseOpenCLAttributes(ParsedAttributes &attrs) {
322 // Treat these like attributes
323 while (Tok.is(tok::kw___kernel)) {
324 SourceLocation AttrNameLoc = ConsumeToken();
John McCall0b7e6782011-03-24 11:26:52 +0000325 attrs.addNew(PP.getIdentifierInfo("opencl_kernel_function"),
326 AttrNameLoc, 0, AttrNameLoc, 0,
327 SourceLocation(), 0, 0, false);
Peter Collingbournef315fa82011-02-14 01:42:53 +0000328 }
329}
330
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000331void Parser::ParseOpenCLQualifiers(DeclSpec &DS) {
332 SourceLocation Loc = Tok.getLocation();
333 switch(Tok.getKind()) {
334 // OpenCL qualifiers:
335 case tok::kw___private:
336 case tok::kw_private:
John McCall0b7e6782011-03-24 11:26:52 +0000337 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000338 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000339 PP.getIdentifierInfo("address_space"), Loc, 0);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000340 break;
341
342 case tok::kw___global:
John McCall0b7e6782011-03-24 11:26:52 +0000343 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000344 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000345 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_global);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000346 break;
347
348 case tok::kw___local:
John McCall0b7e6782011-03-24 11:26:52 +0000349 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000350 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000351 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_local);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000352 break;
353
354 case tok::kw___constant:
John McCall0b7e6782011-03-24 11:26:52 +0000355 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000356 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000357 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_constant);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000358 break;
359
360 case tok::kw___read_only:
John McCall0b7e6782011-03-24 11:26:52 +0000361 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000362 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000363 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_read_only);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000364 break;
365
366 case tok::kw___write_only:
John McCall0b7e6782011-03-24 11:26:52 +0000367 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000368 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000369 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_write_only);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000370 break;
371
372 case tok::kw___read_write:
John McCall0b7e6782011-03-24 11:26:52 +0000373 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000374 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000375 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_read_write);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000376 break;
377 default: break;
378 }
379}
380
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000381/// \brief Parse a version number.
382///
383/// version:
384/// simple-integer
385/// simple-integer ',' simple-integer
386/// simple-integer ',' simple-integer ',' simple-integer
387VersionTuple Parser::ParseVersionTuple(SourceRange &Range) {
388 Range = Tok.getLocation();
389
390 if (!Tok.is(tok::numeric_constant)) {
391 Diag(Tok, diag::err_expected_version);
392 SkipUntil(tok::comma, tok::r_paren, true, true, true);
393 return VersionTuple();
394 }
395
396 // Parse the major (and possibly minor and subminor) versions, which
397 // are stored in the numeric constant. We utilize a quirk of the
398 // lexer, which is that it handles something like 1.2.3 as a single
399 // numeric constant, rather than two separate tokens.
400 llvm::SmallString<512> Buffer;
401 Buffer.resize(Tok.getLength()+1);
402 const char *ThisTokBegin = &Buffer[0];
403
404 // Get the spelling of the token, which eliminates trigraphs, etc.
405 bool Invalid = false;
406 unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin, &Invalid);
407 if (Invalid)
408 return VersionTuple();
409
410 // Parse the major version.
411 unsigned AfterMajor = 0;
412 unsigned Major = 0;
413 while (AfterMajor < ActualLength && isdigit(ThisTokBegin[AfterMajor])) {
414 Major = Major * 10 + ThisTokBegin[AfterMajor] - '0';
415 ++AfterMajor;
416 }
417
418 if (AfterMajor == 0) {
419 Diag(Tok, diag::err_expected_version);
420 SkipUntil(tok::comma, tok::r_paren, true, true, true);
421 return VersionTuple();
422 }
423
424 if (AfterMajor == ActualLength) {
425 ConsumeToken();
426
427 // We only had a single version component.
428 if (Major == 0) {
429 Diag(Tok, diag::err_zero_version);
430 return VersionTuple();
431 }
432
433 return VersionTuple(Major);
434 }
435
436 if (ThisTokBegin[AfterMajor] != '.' || (AfterMajor + 1 == ActualLength)) {
437 Diag(Tok, diag::err_expected_version);
438 SkipUntil(tok::comma, tok::r_paren, true, true, true);
439 return VersionTuple();
440 }
441
442 // Parse the minor version.
443 unsigned AfterMinor = AfterMajor + 1;
444 unsigned Minor = 0;
445 while (AfterMinor < ActualLength && isdigit(ThisTokBegin[AfterMinor])) {
446 Minor = Minor * 10 + ThisTokBegin[AfterMinor] - '0';
447 ++AfterMinor;
448 }
449
450 if (AfterMinor == ActualLength) {
451 ConsumeToken();
452
453 // We had major.minor.
454 if (Major == 0 && Minor == 0) {
455 Diag(Tok, diag::err_zero_version);
456 return VersionTuple();
457 }
458
459 return VersionTuple(Major, Minor);
460 }
461
462 // If what follows is not a '.', we have a problem.
463 if (ThisTokBegin[AfterMinor] != '.') {
464 Diag(Tok, diag::err_expected_version);
465 SkipUntil(tok::comma, tok::r_paren, true, true, true);
466 return VersionTuple();
467 }
468
469 // Parse the subminor version.
470 unsigned AfterSubminor = AfterMinor + 1;
471 unsigned Subminor = 0;
472 while (AfterSubminor < ActualLength && isdigit(ThisTokBegin[AfterSubminor])) {
473 Subminor = Subminor * 10 + ThisTokBegin[AfterSubminor] - '0';
474 ++AfterSubminor;
475 }
476
477 if (AfterSubminor != ActualLength) {
478 Diag(Tok, diag::err_expected_version);
479 SkipUntil(tok::comma, tok::r_paren, true, true, true);
480 return VersionTuple();
481 }
482 ConsumeToken();
483 return VersionTuple(Major, Minor, Subminor);
484}
485
486/// \brief Parse the contents of the "availability" attribute.
487///
488/// availability-attribute:
489/// 'availability' '(' platform ',' version-arg-list ')'
490///
491/// platform:
492/// identifier
493///
494/// version-arg-list:
495/// version-arg
496/// version-arg ',' version-arg-list
497///
498/// version-arg:
499/// 'introduced' '=' version
500/// 'deprecated' '=' version
501/// 'removed' = version
Douglas Gregorb53e4172011-03-26 03:35:55 +0000502/// 'unavailable'
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000503void Parser::ParseAvailabilityAttribute(IdentifierInfo &Availability,
504 SourceLocation AvailabilityLoc,
505 ParsedAttributes &attrs,
506 SourceLocation *endLoc) {
507 SourceLocation PlatformLoc;
508 IdentifierInfo *Platform = 0;
509
510 enum { Introduced, Deprecated, Obsoleted, Unknown };
511 AvailabilityChange Changes[Unknown];
512
513 // Opening '('.
514 SourceLocation LParenLoc;
515 if (!Tok.is(tok::l_paren)) {
516 Diag(Tok, diag::err_expected_lparen);
517 return;
518 }
519 LParenLoc = ConsumeParen();
520
521 // Parse the platform name,
522 if (Tok.isNot(tok::identifier)) {
523 Diag(Tok, diag::err_availability_expected_platform);
524 SkipUntil(tok::r_paren);
525 return;
526 }
527 Platform = Tok.getIdentifierInfo();
528 PlatformLoc = ConsumeToken();
529
530 // Parse the ',' following the platform name.
531 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "", tok::r_paren))
532 return;
533
534 // If we haven't grabbed the pointers for the identifiers
535 // "introduced", "deprecated", and "obsoleted", do so now.
536 if (!Ident_introduced) {
537 Ident_introduced = PP.getIdentifierInfo("introduced");
538 Ident_deprecated = PP.getIdentifierInfo("deprecated");
539 Ident_obsoleted = PP.getIdentifierInfo("obsoleted");
Douglas Gregorb53e4172011-03-26 03:35:55 +0000540 Ident_unavailable = PP.getIdentifierInfo("unavailable");
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000541 }
542
543 // Parse the set of introductions/deprecations/removals.
Douglas Gregorb53e4172011-03-26 03:35:55 +0000544 SourceLocation UnavailableLoc;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000545 do {
546 if (Tok.isNot(tok::identifier)) {
547 Diag(Tok, diag::err_availability_expected_change);
548 SkipUntil(tok::r_paren);
549 return;
550 }
551 IdentifierInfo *Keyword = Tok.getIdentifierInfo();
552 SourceLocation KeywordLoc = ConsumeToken();
553
Douglas Gregorb53e4172011-03-26 03:35:55 +0000554 if (Keyword == Ident_unavailable) {
555 if (UnavailableLoc.isValid()) {
556 Diag(KeywordLoc, diag::err_availability_redundant)
557 << Keyword << SourceRange(UnavailableLoc);
558 }
559 UnavailableLoc = KeywordLoc;
560
561 if (Tok.isNot(tok::comma))
562 break;
563
564 ConsumeToken();
565 continue;
566 }
567
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000568 if (Tok.isNot(tok::equal)) {
569 Diag(Tok, diag::err_expected_equal_after)
570 << Keyword;
571 SkipUntil(tok::r_paren);
572 return;
573 }
574 ConsumeToken();
575
576 SourceRange VersionRange;
577 VersionTuple Version = ParseVersionTuple(VersionRange);
578
579 if (Version.empty()) {
580 SkipUntil(tok::r_paren);
581 return;
582 }
583
584 unsigned Index;
585 if (Keyword == Ident_introduced)
586 Index = Introduced;
587 else if (Keyword == Ident_deprecated)
588 Index = Deprecated;
589 else if (Keyword == Ident_obsoleted)
590 Index = Obsoleted;
591 else
592 Index = Unknown;
593
594 if (Index < Unknown) {
595 if (!Changes[Index].KeywordLoc.isInvalid()) {
596 Diag(KeywordLoc, diag::err_availability_redundant)
597 << Keyword
598 << SourceRange(Changes[Index].KeywordLoc,
599 Changes[Index].VersionRange.getEnd());
600 }
601
602 Changes[Index].KeywordLoc = KeywordLoc;
603 Changes[Index].Version = Version;
604 Changes[Index].VersionRange = VersionRange;
605 } else {
606 Diag(KeywordLoc, diag::err_availability_unknown_change)
607 << Keyword << VersionRange;
608 }
609
610 if (Tok.isNot(tok::comma))
611 break;
612
613 ConsumeToken();
614 } while (true);
615
616 // Closing ')'.
617 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
618 if (RParenLoc.isInvalid())
619 return;
620
621 if (endLoc)
622 *endLoc = RParenLoc;
623
Douglas Gregorb53e4172011-03-26 03:35:55 +0000624 // The 'unavailable' availability cannot be combined with any other
625 // availability changes. Make sure that hasn't happened.
626 if (UnavailableLoc.isValid()) {
627 bool Complained = false;
628 for (unsigned Index = Introduced; Index != Unknown; ++Index) {
629 if (Changes[Index].KeywordLoc.isValid()) {
630 if (!Complained) {
631 Diag(UnavailableLoc, diag::warn_availability_and_unavailable)
632 << SourceRange(Changes[Index].KeywordLoc,
633 Changes[Index].VersionRange.getEnd());
634 Complained = true;
635 }
636
637 // Clear out the availability.
638 Changes[Index] = AvailabilityChange();
639 }
640 }
641 }
642
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000643 // Record this attribute
Douglas Gregorb53e4172011-03-26 03:35:55 +0000644 attrs.addNew(&Availability, AvailabilityLoc,
John McCall0b7e6782011-03-24 11:26:52 +0000645 0, SourceLocation(),
646 Platform, PlatformLoc,
647 Changes[Introduced],
648 Changes[Deprecated],
Douglas Gregorb53e4172011-03-26 03:35:55 +0000649 Changes[Obsoleted],
650 UnavailableLoc, false, false);
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000651}
652
John McCall7f040a92010-12-24 02:08:15 +0000653void Parser::DiagnoseProhibitedAttributes(ParsedAttributesWithRange &attrs) {
654 Diag(attrs.Range.getBegin(), diag::err_attributes_not_allowed)
655 << attrs.Range;
Dawn Perchik52fc3142010-09-03 01:29:35 +0000656}
657
Reid Spencer5f016e22007-07-11 17:01:13 +0000658/// ParseDeclaration - Parse a full 'declaration', which consists of
659/// declaration-specifiers, some number of declarators, and a semicolon.
Chris Lattner97144fc2009-04-02 04:16:50 +0000660/// 'Context' should be a Declarator::TheContext value. This returns the
661/// location of the semicolon in DeclEnd.
Chris Lattner8f08cb72007-08-25 06:57:03 +0000662///
663/// declaration: [C99 6.7]
664/// block-declaration ->
665/// simple-declaration
666/// others [FIXME]
Douglas Gregoradcac882008-12-01 23:54:00 +0000667/// [C++] template-declaration
Chris Lattner8f08cb72007-08-25 06:57:03 +0000668/// [C++] namespace-definition
Douglas Gregorf780abc2008-12-30 03:27:21 +0000669/// [C++] using-directive
Douglas Gregord7f37bf2009-06-22 23:06:13 +0000670/// [C++] using-declaration
Peter Collingbournec6eb44b2011-04-15 00:35:57 +0000671/// [C++0x/C1X] static_assert-declaration
Chris Lattner8f08cb72007-08-25 06:57:03 +0000672/// others... [FIXME]
673///
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +0000674Parser::DeclGroupPtrTy Parser::ParseDeclaration(StmtVector &Stmts,
675 unsigned Context,
Sean Huntbbd37c62009-11-21 08:43:09 +0000676 SourceLocation &DeclEnd,
John McCall7f040a92010-12-24 02:08:15 +0000677 ParsedAttributesWithRange &attrs) {
Argyrios Kyrtzidis36d36802010-06-17 10:52:18 +0000678 ParenBraceBracketBalancer BalancerRAIIObj(*this);
679
John McCalld226f652010-08-21 09:40:31 +0000680 Decl *SingleDecl = 0;
Richard Smithc89edf52011-07-01 19:46:12 +0000681 Decl *OwnedType = 0;
Chris Lattner8f08cb72007-08-25 06:57:03 +0000682 switch (Tok.getKind()) {
Douglas Gregoradcac882008-12-01 23:54:00 +0000683 case tok::kw_template:
Douglas Gregor1426e532009-05-12 21:31:51 +0000684 case tok::kw_export:
John McCall7f040a92010-12-24 02:08:15 +0000685 ProhibitAttributes(attrs);
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000686 SingleDecl = ParseDeclarationStartingWithTemplate(Context, DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +0000687 break;
Sebastian Redld078e642010-08-27 23:12:46 +0000688 case tok::kw_inline:
Sebastian Redl88e64ca2010-08-31 00:36:45 +0000689 // Could be the start of an inline namespace. Allowed as an ext in C++03.
690 if (getLang().CPlusPlus && NextToken().is(tok::kw_namespace)) {
John McCall7f040a92010-12-24 02:08:15 +0000691 ProhibitAttributes(attrs);
Sebastian Redld078e642010-08-27 23:12:46 +0000692 SourceLocation InlineLoc = ConsumeToken();
693 SingleDecl = ParseNamespace(Context, DeclEnd, InlineLoc);
694 break;
695 }
John McCall7f040a92010-12-24 02:08:15 +0000696 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs,
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +0000697 true);
Chris Lattner8f08cb72007-08-25 06:57:03 +0000698 case tok::kw_namespace:
John McCall7f040a92010-12-24 02:08:15 +0000699 ProhibitAttributes(attrs);
Chris Lattner97144fc2009-04-02 04:16:50 +0000700 SingleDecl = ParseNamespace(Context, DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +0000701 break;
Douglas Gregorf780abc2008-12-30 03:27:21 +0000702 case tok::kw_using:
John McCall78b81052010-11-10 02:40:36 +0000703 SingleDecl = ParseUsingDirectiveOrDeclaration(Context, ParsedTemplateInfo(),
Richard Smithc89edf52011-07-01 19:46:12 +0000704 DeclEnd, attrs, &OwnedType);
Chris Lattner682bf922009-03-29 16:50:03 +0000705 break;
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000706 case tok::kw_static_assert:
Peter Collingbournec6eb44b2011-04-15 00:35:57 +0000707 case tok::kw__Static_assert:
John McCall7f040a92010-12-24 02:08:15 +0000708 ProhibitAttributes(attrs);
Chris Lattner97144fc2009-04-02 04:16:50 +0000709 SingleDecl = ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +0000710 break;
Chris Lattner8f08cb72007-08-25 06:57:03 +0000711 default:
John McCall7f040a92010-12-24 02:08:15 +0000712 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs, true);
Chris Lattner8f08cb72007-08-25 06:57:03 +0000713 }
Sean Huntbbd37c62009-11-21 08:43:09 +0000714
Chris Lattner682bf922009-03-29 16:50:03 +0000715 // This routine returns a DeclGroup, if the thing we parsed only contains a
Richard Smithc89edf52011-07-01 19:46:12 +0000716 // single decl, convert it now. Alias declarations can also declare a type;
717 // include that too if it is present.
718 return Actions.ConvertDeclToDeclGroup(SingleDecl, OwnedType);
Chris Lattner8f08cb72007-08-25 06:57:03 +0000719}
720
721/// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl]
722/// declaration-specifiers init-declarator-list[opt] ';'
723///[C90/C++]init-declarator-list ';' [TODO]
724/// [OMP] threadprivate-directive [TODO]
Chris Lattnercd147752009-03-29 17:27:48 +0000725///
Richard Smithad762fc2011-04-14 22:09:26 +0000726/// for-range-declaration: [C++0x 6.5p1: stmt.ranged]
727/// attribute-specifier-seq[opt] type-specifier-seq declarator
728///
Chris Lattnercd147752009-03-29 17:27:48 +0000729/// If RequireSemi is false, this does not check for a ';' at the end of the
Chris Lattner5c5db552010-04-05 18:18:31 +0000730/// declaration. If it is true, it checks for and eats it.
Richard Smithad762fc2011-04-14 22:09:26 +0000731///
732/// If FRI is non-null, we might be parsing a for-range-declaration instead
733/// of a simple-declaration. If we find that we are, we also parse the
734/// for-range-initializer, and place it here.
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +0000735Parser::DeclGroupPtrTy Parser::ParseSimpleDeclaration(StmtVector &Stmts,
736 unsigned Context,
Sean Huntbbd37c62009-11-21 08:43:09 +0000737 SourceLocation &DeclEnd,
John McCall7f040a92010-12-24 02:08:15 +0000738 ParsedAttributes &attrs,
Richard Smithad762fc2011-04-14 22:09:26 +0000739 bool RequireSemi,
740 ForRangeInit *FRI) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000741 // Parse the common declaration-specifiers piece.
John McCall54abf7d2009-11-04 02:18:39 +0000742 ParsingDeclSpec DS(*this);
John McCall7f040a92010-12-24 02:08:15 +0000743 DS.takeAttributesFrom(attrs);
Douglas Gregor312eadb2011-04-24 05:37:28 +0000744
Douglas Gregor0efc2c12010-01-13 17:31:36 +0000745 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS_none,
Richard Smith34b41d92011-02-20 03:19:35 +0000746 getDeclSpecContextFromDeclaratorContext(Context));
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +0000747 StmtResult R = Actions.ActOnVlaStmt(DS);
748 if (R.isUsable())
749 Stmts.push_back(R.release());
Douglas Gregor312eadb2011-04-24 05:37:28 +0000750
Reid Spencer5f016e22007-07-11 17:01:13 +0000751 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
752 // declaration-specifiers init-declarator-list[opt] ';'
Chris Lattner04d66662007-10-09 17:33:22 +0000753 if (Tok.is(tok::semi)) {
Chris Lattner5c5db552010-04-05 18:18:31 +0000754 if (RequireSemi) ConsumeToken();
John McCalld226f652010-08-21 09:40:31 +0000755 Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none,
Douglas Gregor312eadb2011-04-24 05:37:28 +0000756 DS);
John McCall54abf7d2009-11-04 02:18:39 +0000757 DS.complete(TheDecl);
Chris Lattner682bf922009-03-29 16:50:03 +0000758 return Actions.ConvertDeclToDeclGroup(TheDecl);
Reid Spencer5f016e22007-07-11 17:01:13 +0000759 }
Douglas Gregor312eadb2011-04-24 05:37:28 +0000760
761 return ParseDeclGroup(DS, Context, /*FunctionDefs=*/ false, &DeclEnd, FRI);
John McCalld8ac0572009-11-03 19:26:08 +0000762}
Mike Stump1eb44332009-09-09 15:08:12 +0000763
John McCalld8ac0572009-11-03 19:26:08 +0000764/// ParseDeclGroup - Having concluded that this is either a function
765/// definition or a group of object declarations, actually parse the
766/// result.
John McCall54abf7d2009-11-04 02:18:39 +0000767Parser::DeclGroupPtrTy Parser::ParseDeclGroup(ParsingDeclSpec &DS,
768 unsigned Context,
John McCalld8ac0572009-11-03 19:26:08 +0000769 bool AllowFunctionDefinitions,
Richard Smithad762fc2011-04-14 22:09:26 +0000770 SourceLocation *DeclEnd,
771 ForRangeInit *FRI) {
John McCalld8ac0572009-11-03 19:26:08 +0000772 // Parse the first declarator.
John McCall54abf7d2009-11-04 02:18:39 +0000773 ParsingDeclarator D(*this, DS, static_cast<Declarator::TheContext>(Context));
John McCalld8ac0572009-11-03 19:26:08 +0000774 ParseDeclarator(D);
Chris Lattnercd147752009-03-29 17:27:48 +0000775
John McCalld8ac0572009-11-03 19:26:08 +0000776 // Bail out if the first declarator didn't seem well-formed.
777 if (!D.hasName() && !D.mayOmitIdentifier()) {
778 // Skip until ; or }.
779 SkipUntil(tok::r_brace, true, true);
780 if (Tok.is(tok::semi))
781 ConsumeToken();
782 return DeclGroupPtrTy();
Chris Lattner23c4b182009-03-29 17:18:04 +0000783 }
Mike Stump1eb44332009-09-09 15:08:12 +0000784
Chris Lattnerc82daef2010-07-11 22:24:20 +0000785 // Check to see if we have a function *definition* which must have a body.
786 if (AllowFunctionDefinitions && D.isFunctionDeclarator() &&
787 // Look at the next token to make sure that this isn't a function
788 // declaration. We have to check this because __attribute__ might be the
789 // start of a function definition in GCC-extended K&R C.
790 !isDeclarationAfterDeclarator()) {
791
Chris Lattner004659a2010-07-11 22:42:07 +0000792 if (isStartOfFunctionDefinition(D)) {
John McCalld8ac0572009-11-03 19:26:08 +0000793 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
794 Diag(Tok, diag::err_function_declared_typedef);
795
796 // Recover by treating the 'typedef' as spurious.
797 DS.ClearStorageClassSpecs();
798 }
799
John McCalld226f652010-08-21 09:40:31 +0000800 Decl *TheDecl = ParseFunctionDefinition(D);
John McCalld8ac0572009-11-03 19:26:08 +0000801 return Actions.ConvertDeclToDeclGroup(TheDecl);
Chris Lattner004659a2010-07-11 22:42:07 +0000802 }
803
804 if (isDeclarationSpecifier()) {
805 // If there is an invalid declaration specifier right after the function
806 // prototype, then we must be in a missing semicolon case where this isn't
807 // actually a body. Just fall through into the code that handles it as a
808 // prototype, and let the top-level code handle the erroneous declspec
809 // where it would otherwise expect a comma or semicolon.
John McCalld8ac0572009-11-03 19:26:08 +0000810 } else {
811 Diag(Tok, diag::err_expected_fn_body);
812 SkipUntil(tok::semi);
813 return DeclGroupPtrTy();
814 }
815 }
816
Richard Smithad762fc2011-04-14 22:09:26 +0000817 if (ParseAttributesAfterDeclarator(D))
818 return DeclGroupPtrTy();
819
820 // C++0x [stmt.iter]p1: Check if we have a for-range-declarator. If so, we
821 // must parse and analyze the for-range-initializer before the declaration is
822 // analyzed.
823 if (FRI && Tok.is(tok::colon)) {
824 FRI->ColonLoc = ConsumeToken();
Sebastian Redldbef1bb2011-06-05 12:23:16 +0000825 if (Tok.is(tok::l_brace))
826 FRI->RangeExpr = ParseBraceInitializer();
827 else
828 FRI->RangeExpr = ParseExpression();
Richard Smithad762fc2011-04-14 22:09:26 +0000829 Decl *ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
830 Actions.ActOnCXXForRangeDecl(ThisDecl);
831 Actions.FinalizeDeclaration(ThisDecl);
832 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS, &ThisDecl, 1);
833 }
834
John McCalld226f652010-08-21 09:40:31 +0000835 llvm::SmallVector<Decl *, 8> DeclsInGroup;
Richard Smithad762fc2011-04-14 22:09:26 +0000836 Decl *FirstDecl = ParseDeclarationAfterDeclaratorAndAttributes(D);
John McCall54abf7d2009-11-04 02:18:39 +0000837 D.complete(FirstDecl);
John McCalld226f652010-08-21 09:40:31 +0000838 if (FirstDecl)
John McCalld8ac0572009-11-03 19:26:08 +0000839 DeclsInGroup.push_back(FirstDecl);
840
841 // If we don't have a comma, it is either the end of the list (a ';') or an
842 // error, bail out.
843 while (Tok.is(tok::comma)) {
844 // Consume the comma.
Chris Lattner23c4b182009-03-29 17:18:04 +0000845 ConsumeToken();
John McCalld8ac0572009-11-03 19:26:08 +0000846
847 // Parse the next declarator.
848 D.clear();
849
850 // Accept attributes in an init-declarator. In the first declarator in a
851 // declaration, these would be part of the declspec. In subsequent
852 // declarators, they become part of the declarator itself, so that they
853 // don't apply to declarators after *this* one. Examples:
854 // short __attribute__((common)) var; -> declspec
855 // short var __attribute__((common)); -> declarator
856 // short x, __attribute__((common)) var; -> declarator
John McCall7f040a92010-12-24 02:08:15 +0000857 MaybeParseGNUAttributes(D);
John McCalld8ac0572009-11-03 19:26:08 +0000858
859 ParseDeclarator(D);
860
John McCalld226f652010-08-21 09:40:31 +0000861 Decl *ThisDecl = ParseDeclarationAfterDeclarator(D);
John McCall54abf7d2009-11-04 02:18:39 +0000862 D.complete(ThisDecl);
John McCalld226f652010-08-21 09:40:31 +0000863 if (ThisDecl)
John McCalld8ac0572009-11-03 19:26:08 +0000864 DeclsInGroup.push_back(ThisDecl);
865 }
866
867 if (DeclEnd)
868 *DeclEnd = Tok.getLocation();
869
870 if (Context != Declarator::ForContext &&
871 ExpectAndConsume(tok::semi,
872 Context == Declarator::FileContext
873 ? diag::err_invalid_token_after_toplevel_declarator
874 : diag::err_expected_semi_declaration)) {
Chris Lattner004659a2010-07-11 22:42:07 +0000875 // Okay, there was no semicolon and one was expected. If we see a
876 // declaration specifier, just assume it was missing and continue parsing.
877 // Otherwise things are very confused and we skip to recover.
878 if (!isDeclarationSpecifier()) {
879 SkipUntil(tok::r_brace, true, true);
880 if (Tok.is(tok::semi))
881 ConsumeToken();
882 }
John McCalld8ac0572009-11-03 19:26:08 +0000883 }
884
Douglas Gregor23c94db2010-07-02 17:43:08 +0000885 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS,
John McCalld8ac0572009-11-03 19:26:08 +0000886 DeclsInGroup.data(),
887 DeclsInGroup.size());
Reid Spencer5f016e22007-07-11 17:01:13 +0000888}
889
Richard Smithad762fc2011-04-14 22:09:26 +0000890/// Parse an optional simple-asm-expr and attributes, and attach them to a
891/// declarator. Returns true on an error.
892bool Parser::ParseAttributesAfterDeclarator(Declarator &D) {
893 // If a simple-asm-expr is present, parse it.
894 if (Tok.is(tok::kw_asm)) {
895 SourceLocation Loc;
896 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
897 if (AsmLabel.isInvalid()) {
898 SkipUntil(tok::semi, true, true);
899 return true;
900 }
901
902 D.setAsmLabel(AsmLabel.release());
903 D.SetRangeEnd(Loc);
904 }
905
906 MaybeParseGNUAttributes(D);
907 return false;
908}
909
Douglas Gregor1426e532009-05-12 21:31:51 +0000910/// \brief Parse 'declaration' after parsing 'declaration-specifiers
911/// declarator'. This method parses the remainder of the declaration
912/// (including any attributes or initializer, among other things) and
913/// finalizes the declaration.
Reid Spencer5f016e22007-07-11 17:01:13 +0000914///
Reid Spencer5f016e22007-07-11 17:01:13 +0000915/// init-declarator: [C99 6.7]
916/// declarator
917/// declarator '=' initializer
918/// [GNU] declarator simple-asm-expr[opt] attributes[opt]
919/// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +0000920/// [C++] declarator initializer[opt]
921///
922/// [C++] initializer:
923/// [C++] '=' initializer-clause
924/// [C++] '(' expression-list ')'
Sebastian Redl50de12f2009-03-24 22:27:57 +0000925/// [C++0x] '=' 'default' [TODO]
926/// [C++0x] '=' 'delete'
Sebastian Redldbef1bb2011-06-05 12:23:16 +0000927/// [C++0x] braced-init-list
Sebastian Redl50de12f2009-03-24 22:27:57 +0000928///
929/// According to the standard grammar, =default and =delete are function
930/// definitions, but that definitely doesn't fit with the parser here.
Reid Spencer5f016e22007-07-11 17:01:13 +0000931///
John McCalld226f652010-08-21 09:40:31 +0000932Decl *Parser::ParseDeclarationAfterDeclarator(Declarator &D,
Douglas Gregore542c862009-06-23 23:11:28 +0000933 const ParsedTemplateInfo &TemplateInfo) {
Richard Smithad762fc2011-04-14 22:09:26 +0000934 if (ParseAttributesAfterDeclarator(D))
935 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000936
Richard Smithad762fc2011-04-14 22:09:26 +0000937 return ParseDeclarationAfterDeclaratorAndAttributes(D, TemplateInfo);
938}
Mike Stump1eb44332009-09-09 15:08:12 +0000939
Richard Smithad762fc2011-04-14 22:09:26 +0000940Decl *Parser::ParseDeclarationAfterDeclaratorAndAttributes(Declarator &D,
941 const ParsedTemplateInfo &TemplateInfo) {
Douglas Gregor1426e532009-05-12 21:31:51 +0000942 // Inform the current actions module that we just parsed this declarator.
John McCalld226f652010-08-21 09:40:31 +0000943 Decl *ThisDecl = 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +0000944 switch (TemplateInfo.Kind) {
945 case ParsedTemplateInfo::NonTemplate:
Douglas Gregor23c94db2010-07-02 17:43:08 +0000946 ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
Douglas Gregord5a423b2009-09-25 18:43:00 +0000947 break;
948
949 case ParsedTemplateInfo::Template:
950 case ParsedTemplateInfo::ExplicitSpecialization:
Douglas Gregor23c94db2010-07-02 17:43:08 +0000951 ThisDecl = Actions.ActOnTemplateDeclarator(getCurScope(),
John McCallf312b1e2010-08-26 23:41:50 +0000952 MultiTemplateParamsArg(Actions,
Douglas Gregore542c862009-06-23 23:11:28 +0000953 TemplateInfo.TemplateParams->data(),
954 TemplateInfo.TemplateParams->size()),
Douglas Gregord5a423b2009-09-25 18:43:00 +0000955 D);
956 break;
957
958 case ParsedTemplateInfo::ExplicitInstantiation: {
John McCalld226f652010-08-21 09:40:31 +0000959 DeclResult ThisRes
Douglas Gregor23c94db2010-07-02 17:43:08 +0000960 = Actions.ActOnExplicitInstantiation(getCurScope(),
Douglas Gregord5a423b2009-09-25 18:43:00 +0000961 TemplateInfo.ExternLoc,
962 TemplateInfo.TemplateLoc,
963 D);
964 if (ThisRes.isInvalid()) {
965 SkipUntil(tok::semi, true, true);
John McCalld226f652010-08-21 09:40:31 +0000966 return 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +0000967 }
968
969 ThisDecl = ThisRes.get();
970 break;
971 }
972 }
Mike Stump1eb44332009-09-09 15:08:12 +0000973
Richard Smith34b41d92011-02-20 03:19:35 +0000974 bool TypeContainsAuto =
975 D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto;
976
Douglas Gregor1426e532009-05-12 21:31:51 +0000977 // Parse declarator '=' initializer.
Argyrios Kyrtzidisa6eb5f82010-10-08 02:39:23 +0000978 if (isTokenEqualOrMistypedEqualEqual(
979 diag::err_invalid_equalequal_after_declarator)) {
Douglas Gregor1426e532009-05-12 21:31:51 +0000980 ConsumeToken();
Anders Carlsson37bf9d22010-09-24 21:25:25 +0000981 if (Tok.is(tok::kw_delete)) {
Sean Hunte4246a62011-05-12 06:15:49 +0000982 if (D.isFunctionDeclarator())
983 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
984 << 1 /* delete */;
985 else
986 Diag(ConsumeToken(), diag::err_deleted_non_function);
Sean Huntfe2695e2011-05-06 01:42:00 +0000987 } else if (Tok.is(tok::kw_default)) {
Sean Hunte4246a62011-05-12 06:15:49 +0000988 if (D.isFunctionDeclarator())
989 Diag(Tok, diag::err_default_delete_in_multiple_declaration)
990 << 1 /* delete */;
991 else
992 Diag(ConsumeToken(), diag::err_default_special_members);
Douglas Gregor1426e532009-05-12 21:31:51 +0000993 } else {
John McCall731ad842009-12-19 09:28:58 +0000994 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
995 EnterScope(0);
Douglas Gregor23c94db2010-07-02 17:43:08 +0000996 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
John McCall731ad842009-12-19 09:28:58 +0000997 }
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +0000998
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +0000999 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001000 Actions.CodeCompleteInitializer(getCurScope(), ThisDecl);
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +00001001 ConsumeCodeCompletionToken();
1002 SkipUntil(tok::comma, true, true);
1003 return ThisDecl;
1004 }
1005
John McCall60d7b3a2010-08-24 06:29:42 +00001006 ExprResult Init(ParseInitializer());
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +00001007
John McCall731ad842009-12-19 09:28:58 +00001008 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001009 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
John McCall731ad842009-12-19 09:28:58 +00001010 ExitScope();
1011 }
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +00001012
Douglas Gregor1426e532009-05-12 21:31:51 +00001013 if (Init.isInvalid()) {
Douglas Gregor00225542010-03-01 18:27:54 +00001014 SkipUntil(tok::comma, true, true);
1015 Actions.ActOnInitializerError(ThisDecl);
1016 } else
Richard Smith34b41d92011-02-20 03:19:35 +00001017 Actions.AddInitializerToDecl(ThisDecl, Init.take(),
1018 /*DirectInit=*/false, TypeContainsAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +00001019 }
1020 } else if (Tok.is(tok::l_paren)) {
1021 // Parse C++ direct initializer: '(' expression-list ')'
1022 SourceLocation LParenLoc = ConsumeParen();
1023 ExprVector Exprs(Actions);
1024 CommaLocsTy CommaLocs;
1025
Douglas Gregorb4debae2009-12-22 17:47:17 +00001026 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
1027 EnterScope(0);
Douglas Gregor23c94db2010-07-02 17:43:08 +00001028 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001029 }
1030
Douglas Gregor1426e532009-05-12 21:31:51 +00001031 if (ParseExpressionList(Exprs, CommaLocs)) {
1032 SkipUntil(tok::r_paren);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001033
1034 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001035 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001036 ExitScope();
1037 }
Douglas Gregor1426e532009-05-12 21:31:51 +00001038 } else {
1039 // Match the ')'.
1040 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
1041
1042 assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() &&
1043 "Unexpected number of commas!");
Douglas Gregorb4debae2009-12-22 17:47:17 +00001044
1045 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001046 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001047 ExitScope();
1048 }
1049
Douglas Gregor1426e532009-05-12 21:31:51 +00001050 Actions.AddCXXDirectInitializerToDecl(ThisDecl, LParenLoc,
1051 move_arg(Exprs),
Richard Smith34b41d92011-02-20 03:19:35 +00001052 RParenLoc,
1053 TypeContainsAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +00001054 }
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001055 } else if (getLang().CPlusPlus0x && Tok.is(tok::l_brace)) {
1056 // Parse C++0x braced-init-list.
1057 if (D.getCXXScopeSpec().isSet()) {
1058 EnterScope(0);
1059 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
1060 }
1061
1062 ExprResult Init(ParseBraceInitializer());
1063
1064 if (D.getCXXScopeSpec().isSet()) {
1065 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
1066 ExitScope();
1067 }
1068
1069 if (Init.isInvalid()) {
1070 Actions.ActOnInitializerError(ThisDecl);
1071 } else
1072 Actions.AddInitializerToDecl(ThisDecl, Init.take(),
1073 /*DirectInit=*/true, TypeContainsAuto);
1074
Douglas Gregor1426e532009-05-12 21:31:51 +00001075 } else {
Richard Smith34b41d92011-02-20 03:19:35 +00001076 Actions.ActOnUninitializedDecl(ThisDecl, TypeContainsAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +00001077 }
1078
Richard Smith483b9f32011-02-21 20:05:19 +00001079 Actions.FinalizeDeclaration(ThisDecl);
1080
Douglas Gregor1426e532009-05-12 21:31:51 +00001081 return ThisDecl;
1082}
1083
Reid Spencer5f016e22007-07-11 17:01:13 +00001084/// ParseSpecifierQualifierList
1085/// specifier-qualifier-list:
1086/// type-specifier specifier-qualifier-list[opt]
1087/// type-qualifier specifier-qualifier-list[opt]
1088/// [GNU] attributes specifier-qualifier-list[opt]
1089///
Richard Smithc89edf52011-07-01 19:46:12 +00001090void Parser::ParseSpecifierQualifierList(DeclSpec &DS, AccessSpecifier AS) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001091 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
1092 /// parse declaration-specifiers and complain about extra stuff.
Richard Smithc89edf52011-07-01 19:46:12 +00001093 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS);
Mike Stump1eb44332009-09-09 15:08:12 +00001094
Reid Spencer5f016e22007-07-11 17:01:13 +00001095 // Validate declspec for type-name.
1096 unsigned Specs = DS.getParsedSpecifiers();
Chris Lattnerb6645dd2009-04-14 21:16:09 +00001097 if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers() &&
John McCall7f040a92010-12-24 02:08:15 +00001098 !DS.hasAttributes())
Reid Spencer5f016e22007-07-11 17:01:13 +00001099 Diag(Tok, diag::err_typename_requires_specqual);
Mike Stump1eb44332009-09-09 15:08:12 +00001100
Reid Spencer5f016e22007-07-11 17:01:13 +00001101 // Issue diagnostic and remove storage class if present.
1102 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
1103 if (DS.getStorageClassSpecLoc().isValid())
1104 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
1105 else
1106 Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass);
1107 DS.ClearStorageClassSpecs();
1108 }
Mike Stump1eb44332009-09-09 15:08:12 +00001109
Reid Spencer5f016e22007-07-11 17:01:13 +00001110 // Issue diagnostic and remove function specfier if present.
1111 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Douglas Gregorb48fe382008-10-31 09:07:45 +00001112 if (DS.isInlineSpecified())
1113 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
1114 if (DS.isVirtualSpecified())
1115 Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec);
1116 if (DS.isExplicitSpecified())
1117 Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec);
Reid Spencer5f016e22007-07-11 17:01:13 +00001118 DS.ClearFunctionSpecs();
1119 }
1120}
1121
Chris Lattnerc199ab32009-04-12 20:42:31 +00001122/// isValidAfterIdentifierInDeclaratorAfterDeclSpec - Return true if the
1123/// specified token is valid after the identifier in a declarator which
1124/// immediately follows the declspec. For example, these things are valid:
1125///
1126/// int x [ 4]; // direct-declarator
1127/// int x ( int y); // direct-declarator
1128/// int(int x ) // direct-declarator
1129/// int x ; // simple-declaration
1130/// int x = 17; // init-declarator-list
1131/// int x , y; // init-declarator-list
1132/// int x __asm__ ("foo"); // init-declarator-list
Chris Lattnerb6645dd2009-04-14 21:16:09 +00001133/// int x : 4; // struct-declarator
Chris Lattnerc83c27a2009-04-12 22:29:43 +00001134/// int x { 5}; // C++'0x unified initializers
Chris Lattnerc199ab32009-04-12 20:42:31 +00001135///
1136/// This is not, because 'x' does not immediately follow the declspec (though
1137/// ')' happens to be valid anyway).
1138/// int (x)
1139///
1140static bool isValidAfterIdentifierInDeclarator(const Token &T) {
1141 return T.is(tok::l_square) || T.is(tok::l_paren) || T.is(tok::r_paren) ||
1142 T.is(tok::semi) || T.is(tok::comma) || T.is(tok::equal) ||
Chris Lattnerb6645dd2009-04-14 21:16:09 +00001143 T.is(tok::kw_asm) || T.is(tok::l_brace) || T.is(tok::colon);
Chris Lattnerc199ab32009-04-12 20:42:31 +00001144}
1145
Chris Lattnere40c2952009-04-14 21:34:55 +00001146
1147/// ParseImplicitInt - This method is called when we have an non-typename
1148/// identifier in a declspec (which normally terminates the decl spec) when
1149/// the declspec has no type specifier. In this case, the declspec is either
1150/// malformed or is "implicit int" (in K&R and C89).
1151///
1152/// This method handles diagnosing this prettily and returns false if the
1153/// declspec is done being processed. If it recovers and thinks there may be
1154/// other pieces of declspec after it, it returns true.
1155///
Chris Lattnerf4382f52009-04-14 22:17:06 +00001156bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001157 const ParsedTemplateInfo &TemplateInfo,
Chris Lattnere40c2952009-04-14 21:34:55 +00001158 AccessSpecifier AS) {
Chris Lattnerf4382f52009-04-14 22:17:06 +00001159 assert(Tok.is(tok::identifier) && "should have identifier");
Mike Stump1eb44332009-09-09 15:08:12 +00001160
Chris Lattnere40c2952009-04-14 21:34:55 +00001161 SourceLocation Loc = Tok.getLocation();
1162 // If we see an identifier that is not a type name, we normally would
1163 // parse it as the identifer being declared. However, when a typename
1164 // is typo'd or the definition is not included, this will incorrectly
1165 // parse the typename as the identifier name and fall over misparsing
1166 // later parts of the diagnostic.
1167 //
1168 // As such, we try to do some look-ahead in cases where this would
1169 // otherwise be an "implicit-int" case to see if this is invalid. For
1170 // example: "static foo_t x = 4;" In this case, if we parsed foo_t as
1171 // an identifier with implicit int, we'd get a parse error because the
1172 // next token is obviously invalid for a type. Parse these as a case
1173 // with an invalid type specifier.
1174 assert(!DS.hasTypeSpecifier() && "Type specifier checked above");
Mike Stump1eb44332009-09-09 15:08:12 +00001175
Chris Lattnere40c2952009-04-14 21:34:55 +00001176 // Since we know that this either implicit int (which is rare) or an
1177 // error, we'd do lookahead to try to do better recovery.
1178 if (isValidAfterIdentifierInDeclarator(NextToken())) {
1179 // If this token is valid for implicit int, e.g. "static x = 4", then
1180 // we just avoid eating the identifier, so it will be parsed as the
1181 // identifier in the declarator.
1182 return false;
1183 }
Mike Stump1eb44332009-09-09 15:08:12 +00001184
Chris Lattnere40c2952009-04-14 21:34:55 +00001185 // Otherwise, if we don't consume this token, we are going to emit an
1186 // error anyway. Try to recover from various common problems. Check
1187 // to see if this was a reference to a tag name without a tag specified.
1188 // This is a common problem in C (saying 'foo' instead of 'struct foo').
Chris Lattnerf4382f52009-04-14 22:17:06 +00001189 //
1190 // C++ doesn't need this, and isTagName doesn't take SS.
1191 if (SS == 0) {
Argyrios Kyrtzidisb8a9d3b2011-04-21 17:29:47 +00001192 const char *TagName = 0, *FixitTagName = 0;
Chris Lattnerf4382f52009-04-14 22:17:06 +00001193 tok::TokenKind TagKind = tok::unknown;
Mike Stump1eb44332009-09-09 15:08:12 +00001194
Douglas Gregor23c94db2010-07-02 17:43:08 +00001195 switch (Actions.isTagName(*Tok.getIdentifierInfo(), getCurScope())) {
Chris Lattnere40c2952009-04-14 21:34:55 +00001196 default: break;
Argyrios Kyrtzidisb8a9d3b2011-04-21 17:29:47 +00001197 case DeclSpec::TST_enum:
1198 TagName="enum" ; FixitTagName = "enum " ; TagKind=tok::kw_enum ;break;
1199 case DeclSpec::TST_union:
1200 TagName="union" ; FixitTagName = "union " ;TagKind=tok::kw_union ;break;
1201 case DeclSpec::TST_struct:
1202 TagName="struct"; FixitTagName = "struct ";TagKind=tok::kw_struct;break;
1203 case DeclSpec::TST_class:
1204 TagName="class" ; FixitTagName = "class " ;TagKind=tok::kw_class ;break;
Chris Lattnere40c2952009-04-14 21:34:55 +00001205 }
Mike Stump1eb44332009-09-09 15:08:12 +00001206
Chris Lattnerf4382f52009-04-14 22:17:06 +00001207 if (TagName) {
1208 Diag(Loc, diag::err_use_of_tag_name_without_tag)
John McCall23e907a2010-02-14 01:03:10 +00001209 << Tok.getIdentifierInfo() << TagName << getLang().CPlusPlus
Argyrios Kyrtzidisb8a9d3b2011-04-21 17:29:47 +00001210 << FixItHint::CreateInsertion(Tok.getLocation(),FixitTagName);
Mike Stump1eb44332009-09-09 15:08:12 +00001211
Chris Lattnerf4382f52009-04-14 22:17:06 +00001212 // Parse this as a tag as if the missing tag were present.
1213 if (TagKind == tok::kw_enum)
Douglas Gregor9b9edd62010-03-02 17:53:14 +00001214 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS);
Chris Lattnerf4382f52009-04-14 22:17:06 +00001215 else
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001216 ParseClassSpecifier(TagKind, Loc, DS, TemplateInfo, AS);
Chris Lattnerf4382f52009-04-14 22:17:06 +00001217 return true;
1218 }
Chris Lattnere40c2952009-04-14 21:34:55 +00001219 }
Mike Stump1eb44332009-09-09 15:08:12 +00001220
Douglas Gregora786fdb2009-10-13 23:27:22 +00001221 // This is almost certainly an invalid type name. Let the action emit a
1222 // diagnostic and attempt to recover.
John McCallb3d87482010-08-24 05:47:05 +00001223 ParsedType T;
Douglas Gregora786fdb2009-10-13 23:27:22 +00001224 if (Actions.DiagnoseUnknownTypeName(*Tok.getIdentifierInfo(), Loc,
Douglas Gregor23c94db2010-07-02 17:43:08 +00001225 getCurScope(), SS, T)) {
Douglas Gregora786fdb2009-10-13 23:27:22 +00001226 // The action emitted a diagnostic, so we don't have to.
1227 if (T) {
1228 // The action has suggested that the type T could be used. Set that as
1229 // the type in the declaration specifiers, consume the would-be type
1230 // name token, and we're done.
1231 const char *PrevSpec;
1232 unsigned DiagID;
John McCallb3d87482010-08-24 05:47:05 +00001233 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID, T);
Douglas Gregora786fdb2009-10-13 23:27:22 +00001234 DS.SetRangeEnd(Tok.getLocation());
1235 ConsumeToken();
1236
1237 // There may be other declaration specifiers after this.
1238 return true;
1239 }
1240
1241 // Fall through; the action had no suggestion for us.
1242 } else {
1243 // The action did not emit a diagnostic, so emit one now.
1244 SourceRange R;
1245 if (SS) R = SS->getRange();
1246 Diag(Loc, diag::err_unknown_typename) << Tok.getIdentifierInfo() << R;
1247 }
Mike Stump1eb44332009-09-09 15:08:12 +00001248
Douglas Gregora786fdb2009-10-13 23:27:22 +00001249 // Mark this as an error.
Chris Lattnere40c2952009-04-14 21:34:55 +00001250 const char *PrevSpec;
John McCallfec54012009-08-03 20:12:06 +00001251 unsigned DiagID;
1252 DS.SetTypeSpecType(DeclSpec::TST_error, Loc, PrevSpec, DiagID);
Chris Lattnere40c2952009-04-14 21:34:55 +00001253 DS.SetRangeEnd(Tok.getLocation());
1254 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001255
Chris Lattnere40c2952009-04-14 21:34:55 +00001256 // TODO: Could inject an invalid typedef decl in an enclosing scope to
1257 // avoid rippling error messages on subsequent uses of the same type,
1258 // could be useful if #include was forgotten.
1259 return false;
1260}
1261
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001262/// \brief Determine the declaration specifier context from the declarator
1263/// context.
1264///
1265/// \param Context the declarator context, which is one of the
1266/// Declarator::TheContext enumerator values.
1267Parser::DeclSpecContext
1268Parser::getDeclSpecContextFromDeclaratorContext(unsigned Context) {
1269 if (Context == Declarator::MemberContext)
1270 return DSC_class;
1271 if (Context == Declarator::FileContext)
1272 return DSC_top_level;
1273 return DSC_normal;
1274}
1275
Reid Spencer5f016e22007-07-11 17:01:13 +00001276/// ParseDeclarationSpecifiers
1277/// declaration-specifiers: [C99 6.7]
1278/// storage-class-specifier declaration-specifiers[opt]
1279/// type-specifier declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00001280/// [C99] function-specifier declaration-specifiers[opt]
1281/// [GNU] attributes declaration-specifiers[opt]
1282///
1283/// storage-class-specifier: [C99 6.7.1]
1284/// 'typedef'
1285/// 'extern'
1286/// 'static'
1287/// 'auto'
1288/// 'register'
Sebastian Redl669d5d72008-11-14 23:42:31 +00001289/// [C++] 'mutable'
Reid Spencer5f016e22007-07-11 17:01:13 +00001290/// [GNU] '__thread'
Reid Spencer5f016e22007-07-11 17:01:13 +00001291/// function-specifier: [C99 6.7.4]
1292/// [C99] 'inline'
Douglas Gregorb48fe382008-10-31 09:07:45 +00001293/// [C++] 'virtual'
1294/// [C++] 'explicit'
Peter Collingbournef315fa82011-02-14 01:42:53 +00001295/// [OpenCL] '__kernel'
Anders Carlssonf47f7a12009-05-06 04:46:28 +00001296/// 'friend': [C++ dcl.friend]
Sebastian Redl2ac67232009-11-05 15:47:02 +00001297/// 'constexpr': [C++0x dcl.constexpr]
Anders Carlssonf47f7a12009-05-06 04:46:28 +00001298
Reid Spencer5f016e22007-07-11 17:01:13 +00001299///
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +00001300void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001301 const ParsedTemplateInfo &TemplateInfo,
John McCall67d1a672009-08-06 02:15:43 +00001302 AccessSpecifier AS,
Douglas Gregor312eadb2011-04-24 05:37:28 +00001303 DeclSpecContext DSContext) {
1304 if (DS.getSourceRange().isInvalid()) {
1305 DS.SetRangeStart(Tok.getLocation());
1306 DS.SetRangeEnd(Tok.getLocation());
1307 }
1308
Reid Spencer5f016e22007-07-11 17:01:13 +00001309 while (1) {
John McCallfec54012009-08-03 20:12:06 +00001310 bool isInvalid = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001311 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00001312 unsigned DiagID = 0;
1313
Reid Spencer5f016e22007-07-11 17:01:13 +00001314 SourceLocation Loc = Tok.getLocation();
Douglas Gregor12e083c2008-11-07 15:42:26 +00001315
Reid Spencer5f016e22007-07-11 17:01:13 +00001316 switch (Tok.getKind()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001317 default:
Chris Lattnerbce61352008-07-26 00:20:22 +00001318 DoneWithDeclSpec:
Reid Spencer5f016e22007-07-11 17:01:13 +00001319 // If this is not a declaration specifier token, we're done reading decl
1320 // specifiers. First verify that DeclSpec's are consistent.
Douglas Gregor9b3064b2009-04-01 22:41:11 +00001321 DS.Finish(Diags, PP);
Reid Spencer5f016e22007-07-11 17:01:13 +00001322 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001323
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001324 case tok::code_completion: {
John McCallf312b1e2010-08-26 23:41:50 +00001325 Sema::ParserCompletionContext CCC = Sema::PCC_Namespace;
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001326 if (DS.hasTypeSpecifier()) {
1327 bool AllowNonIdentifiers
1328 = (getCurScope()->getFlags() & (Scope::ControlScope |
1329 Scope::BlockScope |
1330 Scope::TemplateParamScope |
1331 Scope::FunctionPrototypeScope |
1332 Scope::AtCatchScope)) == 0;
1333 bool AllowNestedNameSpecifiers
1334 = DSContext == DSC_top_level ||
1335 (DSContext == DSC_class && DS.isFriendSpecified());
1336
Douglas Gregorc7b6d882010-09-16 15:14:18 +00001337 Actions.CodeCompleteDeclSpec(getCurScope(), DS,
1338 AllowNonIdentifiers,
1339 AllowNestedNameSpecifiers);
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001340 ConsumeCodeCompletionToken();
1341 return;
1342 }
1343
Douglas Gregor68e3c2e2011-02-15 20:33:25 +00001344 if (getCurScope()->getFnParent() || getCurScope()->getBlockParent())
1345 CCC = Sema::PCC_LocalDeclarationSpecifiers;
1346 else if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate)
John McCallf312b1e2010-08-26 23:41:50 +00001347 CCC = DSContext == DSC_class? Sema::PCC_MemberTemplate
1348 : Sema::PCC_Template;
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001349 else if (DSContext == DSC_class)
John McCallf312b1e2010-08-26 23:41:50 +00001350 CCC = Sema::PCC_Class;
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001351 else if (ObjCImpDecl)
John McCallf312b1e2010-08-26 23:41:50 +00001352 CCC = Sema::PCC_ObjCImplementation;
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001353
1354 Actions.CodeCompleteOrdinaryName(getCurScope(), CCC);
1355 ConsumeCodeCompletionToken();
1356 return;
1357 }
1358
Chris Lattner5e02c472009-01-05 00:07:25 +00001359 case tok::coloncolon: // ::foo::bar
John McCall9ba61662010-02-26 08:45:28 +00001360 // C++ scope specifier. Annotate and loop, or bail out on error.
1361 if (TryAnnotateCXXScopeToken(true)) {
1362 if (!DS.hasTypeSpecifier())
1363 DS.SetTypeSpecError();
1364 goto DoneWithDeclSpec;
1365 }
John McCall2e0a7152010-03-01 18:20:46 +00001366 if (Tok.is(tok::coloncolon)) // ::new or ::delete
1367 goto DoneWithDeclSpec;
John McCall9ba61662010-02-26 08:45:28 +00001368 continue;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001369
1370 case tok::annot_cxxscope: {
1371 if (DS.hasTypeSpecifier())
1372 goto DoneWithDeclSpec;
1373
John McCallaa87d332009-12-12 11:40:51 +00001374 CXXScopeSpec SS;
Douglas Gregorc34348a2011-02-24 17:54:50 +00001375 Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(),
1376 Tok.getAnnotationRange(),
1377 SS);
John McCallaa87d332009-12-12 11:40:51 +00001378
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001379 // We are looking for a qualified typename.
Douglas Gregor9135c722009-03-25 15:40:00 +00001380 Token Next = NextToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001381 if (Next.is(tok::annot_template_id) &&
Douglas Gregor9135c722009-03-25 15:40:00 +00001382 static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue())
Douglas Gregorc45c2322009-03-31 00:43:58 +00001383 ->Kind == TNK_Type_template) {
Douglas Gregor9135c722009-03-25 15:40:00 +00001384 // We have a qualified template-id, e.g., N::A<int>
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001385
1386 // C++ [class.qual]p2:
1387 // In a lookup in which the constructor is an acceptable lookup
1388 // result and the nested-name-specifier nominates a class C:
1389 //
1390 // - if the name specified after the
1391 // nested-name-specifier, when looked up in C, is the
1392 // injected-class-name of C (Clause 9), or
1393 //
1394 // - if the name specified after the nested-name-specifier
1395 // is the same as the identifier or the
1396 // simple-template-id's template-name in the last
1397 // component of the nested-name-specifier,
1398 //
1399 // the name is instead considered to name the constructor of
1400 // class C.
1401 //
1402 // Thus, if the template-name is actually the constructor
1403 // name, then the code is ill-formed; this interpretation is
1404 // reinforced by the NAD status of core issue 635.
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +00001405 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Next);
John McCallba9d8532010-04-13 06:39:49 +00001406 if ((DSContext == DSC_top_level ||
1407 (DSContext == DSC_class && DS.isFriendSpecified())) &&
1408 TemplateId->Name &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00001409 Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) {
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001410 if (isConstructorDeclarator()) {
1411 // The user meant this to be an out-of-line constructor
1412 // definition, but template arguments are not allowed
1413 // there. Just allow this as a constructor; we'll
1414 // complain about it later.
1415 goto DoneWithDeclSpec;
1416 }
1417
1418 // The user meant this to name a type, but it actually names
1419 // a constructor with some extraneous template
1420 // arguments. Complain, then parse it as a type as the user
1421 // intended.
1422 Diag(TemplateId->TemplateNameLoc,
1423 diag::err_out_of_line_template_id_names_constructor)
1424 << TemplateId->Name;
1425 }
1426
John McCallaa87d332009-12-12 11:40:51 +00001427 DS.getTypeSpecScope() = SS;
1428 ConsumeToken(); // The C++ scope.
Mike Stump1eb44332009-09-09 15:08:12 +00001429 assert(Tok.is(tok::annot_template_id) &&
Douglas Gregor9135c722009-03-25 15:40:00 +00001430 "ParseOptionalCXXScopeSpecifier not working");
Douglas Gregor059101f2011-03-02 00:47:37 +00001431 AnnotateTemplateIdTokenAsType();
Douglas Gregor9135c722009-03-25 15:40:00 +00001432 continue;
1433 }
1434
Douglas Gregor9d7b3532009-09-28 07:26:33 +00001435 if (Next.is(tok::annot_typename)) {
John McCallaa87d332009-12-12 11:40:51 +00001436 DS.getTypeSpecScope() = SS;
1437 ConsumeToken(); // The C++ scope.
John McCallb3d87482010-08-24 05:47:05 +00001438 if (Tok.getAnnotationValue()) {
1439 ParsedType T = getTypeAnnotation(Tok);
Nico Weber253e80b2010-11-22 10:30:56 +00001440 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename,
1441 Tok.getAnnotationEndLoc(),
John McCallb3d87482010-08-24 05:47:05 +00001442 PrevSpec, DiagID, T);
1443 }
Douglas Gregor9d7b3532009-09-28 07:26:33 +00001444 else
1445 DS.SetTypeSpecError();
1446 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1447 ConsumeToken(); // The typename
1448 }
1449
Douglas Gregor9135c722009-03-25 15:40:00 +00001450 if (Next.isNot(tok::identifier))
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001451 goto DoneWithDeclSpec;
1452
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001453 // If we're in a context where the identifier could be a class name,
1454 // check whether this is a constructor declaration.
John McCallba9d8532010-04-13 06:39:49 +00001455 if ((DSContext == DSC_top_level ||
1456 (DSContext == DSC_class && DS.isFriendSpecified())) &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00001457 Actions.isCurrentClassName(*Next.getIdentifierInfo(), getCurScope(),
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001458 &SS)) {
1459 if (isConstructorDeclarator())
1460 goto DoneWithDeclSpec;
1461
1462 // As noted in C++ [class.qual]p2 (cited above), when the name
1463 // of the class is qualified in a context where it could name
1464 // a constructor, its a constructor name. However, we've
1465 // looked at the declarator, and the user probably meant this
1466 // to be a type. Complain that it isn't supposed to be treated
1467 // as a type, then proceed to parse it as a type.
1468 Diag(Next.getLocation(), diag::err_out_of_line_type_names_constructor)
1469 << Next.getIdentifierInfo();
1470 }
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001471
John McCallb3d87482010-08-24 05:47:05 +00001472 ParsedType TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(),
1473 Next.getLocation(),
Douglas Gregor9e876872011-03-01 18:12:44 +00001474 getCurScope(), &SS,
1475 false, false, ParsedType(),
1476 /*NonTrivialSourceInfo=*/true);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001477
Chris Lattnerf4382f52009-04-14 22:17:06 +00001478 // If the referenced identifier is not a type, then this declspec is
1479 // erroneous: We already checked about that it has no type specifier, and
1480 // C++ doesn't have implicit int. Diagnose it as a typo w.r.t. to the
Mike Stump1eb44332009-09-09 15:08:12 +00001481 // typename.
Chris Lattnerf4382f52009-04-14 22:17:06 +00001482 if (TypeRep == 0) {
1483 ConsumeToken(); // Eat the scope spec so the identifier is current.
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001484 if (ParseImplicitInt(DS, &SS, TemplateInfo, AS)) continue;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001485 goto DoneWithDeclSpec;
Chris Lattnerf4382f52009-04-14 22:17:06 +00001486 }
Mike Stump1eb44332009-09-09 15:08:12 +00001487
John McCallaa87d332009-12-12 11:40:51 +00001488 DS.getTypeSpecScope() = SS;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001489 ConsumeToken(); // The C++ scope.
1490
Douglas Gregor1a51b4a2009-02-09 15:09:02 +00001491 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001492 DiagID, TypeRep);
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001493 if (isInvalid)
1494 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001495
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001496 DS.SetRangeEnd(Tok.getLocation());
1497 ConsumeToken(); // The typename.
1498
1499 continue;
1500 }
Mike Stump1eb44332009-09-09 15:08:12 +00001501
Chris Lattner80d0c892009-01-21 19:48:37 +00001502 case tok::annot_typename: {
John McCallb3d87482010-08-24 05:47:05 +00001503 if (Tok.getAnnotationValue()) {
1504 ParsedType T = getTypeAnnotation(Tok);
Nico Weberc43271e2010-11-22 12:50:03 +00001505 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallb3d87482010-08-24 05:47:05 +00001506 DiagID, T);
1507 } else
Douglas Gregor31a19b62009-04-01 21:51:26 +00001508 DS.SetTypeSpecError();
Chris Lattner5c5db552010-04-05 18:18:31 +00001509
1510 if (isInvalid)
1511 break;
1512
Chris Lattner80d0c892009-01-21 19:48:37 +00001513 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1514 ConsumeToken(); // The typename
Mike Stump1eb44332009-09-09 15:08:12 +00001515
Chris Lattner80d0c892009-01-21 19:48:37 +00001516 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1517 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001518 // Objective-C interface.
1519 if (Tok.is(tok::less) && getLang().ObjC1)
1520 ParseObjCProtocolQualifiers(DS);
1521
Chris Lattner80d0c892009-01-21 19:48:37 +00001522 continue;
1523 }
Mike Stump1eb44332009-09-09 15:08:12 +00001524
Douglas Gregorbfad9152011-04-28 15:48:45 +00001525 case tok::kw___is_signed:
1526 // GNU libstdc++ 4.4 uses __is_signed as an identifier, but Clang
1527 // typically treats it as a trait. If we see __is_signed as it appears
1528 // in libstdc++, e.g.,
1529 //
1530 // static const bool __is_signed;
1531 //
1532 // then treat __is_signed as an identifier rather than as a keyword.
1533 if (DS.getTypeSpecType() == TST_bool &&
1534 DS.getTypeQualifiers() == DeclSpec::TQ_const &&
1535 DS.getStorageClassSpec() == DeclSpec::SCS_static) {
1536 Tok.getIdentifierInfo()->RevertTokenIDToIdentifier();
1537 Tok.setKind(tok::identifier);
1538 }
1539
1540 // We're done with the declaration-specifiers.
1541 goto DoneWithDeclSpec;
1542
Chris Lattner3bd934a2008-07-26 01:18:38 +00001543 // typedef-name
1544 case tok::identifier: {
Chris Lattner5e02c472009-01-05 00:07:25 +00001545 // In C++, check to see if this is a scope specifier like foo::bar::, if
1546 // so handle it as such. This is important for ctor parsing.
John McCall9ba61662010-02-26 08:45:28 +00001547 if (getLang().CPlusPlus) {
1548 if (TryAnnotateCXXScopeToken(true)) {
1549 if (!DS.hasTypeSpecifier())
1550 DS.SetTypeSpecError();
1551 goto DoneWithDeclSpec;
1552 }
1553 if (!Tok.is(tok::identifier))
1554 continue;
1555 }
Mike Stump1eb44332009-09-09 15:08:12 +00001556
Chris Lattner3bd934a2008-07-26 01:18:38 +00001557 // This identifier can only be a typedef name if we haven't already seen
1558 // a type-specifier. Without this check we misparse:
1559 // typedef int X; struct Y { short X; }; as 'short int'.
1560 if (DS.hasTypeSpecifier())
1561 goto DoneWithDeclSpec;
Mike Stump1eb44332009-09-09 15:08:12 +00001562
John Thompson82287d12010-02-05 00:12:22 +00001563 // Check for need to substitute AltiVec keyword tokens.
1564 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
1565 break;
1566
Chris Lattner3bd934a2008-07-26 01:18:38 +00001567 // It has to be available as a typedef too!
John McCallb3d87482010-08-24 05:47:05 +00001568 ParsedType TypeRep =
1569 Actions.getTypeName(*Tok.getIdentifierInfo(),
1570 Tok.getLocation(), getCurScope());
Douglas Gregor55f6b142009-02-09 18:46:07 +00001571
Chris Lattnerc199ab32009-04-12 20:42:31 +00001572 // If this is not a typedef name, don't parse it as part of the declspec,
1573 // it must be an implicit int or an error.
John McCallb3d87482010-08-24 05:47:05 +00001574 if (!TypeRep) {
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001575 if (ParseImplicitInt(DS, 0, TemplateInfo, AS)) continue;
Chris Lattner3bd934a2008-07-26 01:18:38 +00001576 goto DoneWithDeclSpec;
Chris Lattnerc199ab32009-04-12 20:42:31 +00001577 }
Douglas Gregor55f6b142009-02-09 18:46:07 +00001578
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001579 // If we're in a context where the identifier could be a class name,
1580 // check whether this is a constructor declaration.
1581 if (getLang().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00001582 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), getCurScope()) &&
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001583 isConstructorDeclarator())
Douglas Gregorb48fe382008-10-31 09:07:45 +00001584 goto DoneWithDeclSpec;
1585
Douglas Gregor1a51b4a2009-02-09 15:09:02 +00001586 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001587 DiagID, TypeRep);
Chris Lattner3bd934a2008-07-26 01:18:38 +00001588 if (isInvalid)
1589 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001590
Chris Lattner3bd934a2008-07-26 01:18:38 +00001591 DS.SetRangeEnd(Tok.getLocation());
1592 ConsumeToken(); // The identifier
1593
1594 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1595 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001596 // Objective-C interface.
1597 if (Tok.is(tok::less) && getLang().ObjC1)
1598 ParseObjCProtocolQualifiers(DS);
1599
Steve Naroff4f9b9f12008-09-22 10:28:57 +00001600 // Need to support trailing type qualifiers (e.g. "id<p> const").
1601 // If a type specifier follows, it will be diagnosed elsewhere.
1602 continue;
Chris Lattner3bd934a2008-07-26 01:18:38 +00001603 }
Douglas Gregor39a8de12009-02-25 19:37:18 +00001604
1605 // type-name
1606 case tok::annot_template_id: {
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +00001607 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregorc45c2322009-03-31 00:43:58 +00001608 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor39a8de12009-02-25 19:37:18 +00001609 // This template-id does not refer to a type name, so we're
1610 // done with the type-specifiers.
1611 goto DoneWithDeclSpec;
1612 }
1613
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001614 // If we're in a context where the template-id could be a
1615 // constructor name or specialization, check whether this is a
1616 // constructor declaration.
1617 if (getLang().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00001618 Actions.isCurrentClassName(*TemplateId->Name, getCurScope()) &&
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001619 isConstructorDeclarator())
1620 goto DoneWithDeclSpec;
1621
Douglas Gregor39a8de12009-02-25 19:37:18 +00001622 // Turn the template-id annotation token into a type annotation
1623 // token, then try again to parse it as a type-specifier.
Douglas Gregor31a19b62009-04-01 21:51:26 +00001624 AnnotateTemplateIdTokenAsType();
Douglas Gregor39a8de12009-02-25 19:37:18 +00001625 continue;
1626 }
1627
Reid Spencer5f016e22007-07-11 17:01:13 +00001628 // GNU attributes support.
1629 case tok::kw___attribute:
John McCall7f040a92010-12-24 02:08:15 +00001630 ParseGNUAttributes(DS.getAttributes());
Reid Spencer5f016e22007-07-11 17:01:13 +00001631 continue;
Steve Narofff59e17e2008-12-24 20:59:21 +00001632
1633 // Microsoft declspec support.
1634 case tok::kw___declspec:
John McCall7f040a92010-12-24 02:08:15 +00001635 ParseMicrosoftDeclSpec(DS.getAttributes());
Steve Narofff59e17e2008-12-24 20:59:21 +00001636 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001637
Steve Naroff239f0732008-12-25 14:16:32 +00001638 // Microsoft single token adornments.
Steve Naroff86bc6cf2008-12-25 14:41:26 +00001639 case tok::kw___forceinline:
Eli Friedman290eeb02009-06-08 23:27:34 +00001640 // FIXME: Add handling here!
1641 break;
1642
1643 case tok::kw___ptr64:
Steve Naroff86bc6cf2008-12-25 14:41:26 +00001644 case tok::kw___w64:
Steve Naroff239f0732008-12-25 14:16:32 +00001645 case tok::kw___cdecl:
1646 case tok::kw___stdcall:
1647 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00001648 case tok::kw___thiscall:
John McCall7f040a92010-12-24 02:08:15 +00001649 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman290eeb02009-06-08 23:27:34 +00001650 continue;
1651
Dawn Perchik52fc3142010-09-03 01:29:35 +00001652 // Borland single token adornments.
1653 case tok::kw___pascal:
John McCall7f040a92010-12-24 02:08:15 +00001654 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik52fc3142010-09-03 01:29:35 +00001655 continue;
1656
Peter Collingbournef315fa82011-02-14 01:42:53 +00001657 // OpenCL single token adornments.
1658 case tok::kw___kernel:
1659 ParseOpenCLAttributes(DS.getAttributes());
1660 continue;
1661
Reid Spencer5f016e22007-07-11 17:01:13 +00001662 // storage-class-specifier
1663 case tok::kw_typedef:
John McCallfec54012009-08-03 20:12:06 +00001664 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_typedef, Loc, PrevSpec,
Peter Collingbournee2f82f72011-02-11 19:59:54 +00001665 DiagID, getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00001666 break;
1667 case tok::kw_extern:
1668 if (DS.isThreadSpecified())
Chris Lattner1ab3b962008-11-18 07:48:38 +00001669 Diag(Tok, diag::ext_thread_before) << "extern";
John McCallfec54012009-08-03 20:12:06 +00001670 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_extern, Loc, PrevSpec,
Peter Collingbournee2f82f72011-02-11 19:59:54 +00001671 DiagID, getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00001672 break;
Steve Naroff8d54bf22007-12-18 00:16:02 +00001673 case tok::kw___private_extern__:
Chris Lattnerf97409f2008-04-06 06:57:35 +00001674 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_private_extern, Loc,
Peter Collingbournee2f82f72011-02-11 19:59:54 +00001675 PrevSpec, DiagID, getLang());
Steve Naroff8d54bf22007-12-18 00:16:02 +00001676 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00001677 case tok::kw_static:
1678 if (DS.isThreadSpecified())
Chris Lattner1ab3b962008-11-18 07:48:38 +00001679 Diag(Tok, diag::ext_thread_before) << "static";
John McCallfec54012009-08-03 20:12:06 +00001680 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_static, Loc, PrevSpec,
Peter Collingbournee2f82f72011-02-11 19:59:54 +00001681 DiagID, getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00001682 break;
1683 case tok::kw_auto:
Douglas Gregor18d8b792011-03-14 21:43:30 +00001684 if (getLang().CPlusPlus0x) {
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00001685 if (isKnownToBeTypeSpecifier(GetLookAheadToken(1))) {
1686 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, Loc, PrevSpec,
1687 DiagID, getLang());
1688 if (!isInvalid)
1689 Diag(Tok, diag::auto_storage_class)
1690 << FixItHint::CreateRemoval(DS.getStorageClassSpecLoc());
1691 }
1692 else
1693 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec,
1694 DiagID);
1695 }
Anders Carlssone89d1592009-06-26 18:41:36 +00001696 else
John McCallfec54012009-08-03 20:12:06 +00001697 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, Loc, PrevSpec,
Peter Collingbournee2f82f72011-02-11 19:59:54 +00001698 DiagID, getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00001699 break;
1700 case tok::kw_register:
John McCallfec54012009-08-03 20:12:06 +00001701 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_register, Loc, PrevSpec,
Peter Collingbournee2f82f72011-02-11 19:59:54 +00001702 DiagID, getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00001703 break;
Sebastian Redl669d5d72008-11-14 23:42:31 +00001704 case tok::kw_mutable:
John McCallfec54012009-08-03 20:12:06 +00001705 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_mutable, Loc, PrevSpec,
Peter Collingbournee2f82f72011-02-11 19:59:54 +00001706 DiagID, getLang());
Sebastian Redl669d5d72008-11-14 23:42:31 +00001707 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00001708 case tok::kw___thread:
John McCallfec54012009-08-03 20:12:06 +00001709 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001710 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001711
Reid Spencer5f016e22007-07-11 17:01:13 +00001712 // function-specifier
1713 case tok::kw_inline:
John McCallfec54012009-08-03 20:12:06 +00001714 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001715 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +00001716 case tok::kw_virtual:
John McCallfec54012009-08-03 20:12:06 +00001717 isInvalid = DS.SetFunctionSpecVirtual(Loc, PrevSpec, DiagID);
Douglas Gregorb48fe382008-10-31 09:07:45 +00001718 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +00001719 case tok::kw_explicit:
John McCallfec54012009-08-03 20:12:06 +00001720 isInvalid = DS.SetFunctionSpecExplicit(Loc, PrevSpec, DiagID);
Douglas Gregorb48fe382008-10-31 09:07:45 +00001721 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00001722
Anders Carlssonf47f7a12009-05-06 04:46:28 +00001723 // friend
1724 case tok::kw_friend:
John McCall67d1a672009-08-06 02:15:43 +00001725 if (DSContext == DSC_class)
1726 isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID);
1727 else {
1728 PrevSpec = ""; // not actually used by the diagnostic
1729 DiagID = diag::err_friend_invalid_in_context;
1730 isInvalid = true;
1731 }
Anders Carlssonf47f7a12009-05-06 04:46:28 +00001732 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001733
Sebastian Redl2ac67232009-11-05 15:47:02 +00001734 // constexpr
1735 case tok::kw_constexpr:
1736 isInvalid = DS.SetConstexprSpec(Loc, PrevSpec, DiagID);
1737 break;
1738
Chris Lattner80d0c892009-01-21 19:48:37 +00001739 // type-specifier
1740 case tok::kw_short:
John McCallfec54012009-08-03 20:12:06 +00001741 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec,
1742 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001743 break;
1744 case tok::kw_long:
1745 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCallfec54012009-08-03 20:12:06 +00001746 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
1747 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001748 else
John McCallfec54012009-08-03 20:12:06 +00001749 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
1750 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001751 break;
Francois Pichet338d7f72011-04-28 01:59:37 +00001752 case tok::kw___int64:
1753 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
1754 DiagID);
1755 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00001756 case tok::kw_signed:
John McCallfec54012009-08-03 20:12:06 +00001757 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec,
1758 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001759 break;
1760 case tok::kw_unsigned:
John McCallfec54012009-08-03 20:12:06 +00001761 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
1762 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001763 break;
1764 case tok::kw__Complex:
John McCallfec54012009-08-03 20:12:06 +00001765 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
1766 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001767 break;
1768 case tok::kw__Imaginary:
John McCallfec54012009-08-03 20:12:06 +00001769 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
1770 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001771 break;
1772 case tok::kw_void:
John McCallfec54012009-08-03 20:12:06 +00001773 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec,
1774 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001775 break;
1776 case tok::kw_char:
John McCallfec54012009-08-03 20:12:06 +00001777 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec,
1778 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001779 break;
1780 case tok::kw_int:
John McCallfec54012009-08-03 20:12:06 +00001781 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec,
1782 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001783 break;
1784 case tok::kw_float:
John McCallfec54012009-08-03 20:12:06 +00001785 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec,
1786 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001787 break;
1788 case tok::kw_double:
John McCallfec54012009-08-03 20:12:06 +00001789 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec,
1790 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001791 break;
1792 case tok::kw_wchar_t:
John McCallfec54012009-08-03 20:12:06 +00001793 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec,
1794 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001795 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001796 case tok::kw_char16_t:
John McCallfec54012009-08-03 20:12:06 +00001797 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec,
1798 DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001799 break;
1800 case tok::kw_char32_t:
John McCallfec54012009-08-03 20:12:06 +00001801 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec,
1802 DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001803 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00001804 case tok::kw_bool:
1805 case tok::kw__Bool:
Argyrios Kyrtzidis4383e182010-11-16 18:18:13 +00001806 if (Tok.is(tok::kw_bool) &&
1807 DS.getTypeSpecType() != DeclSpec::TST_unspecified &&
1808 DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
1809 PrevSpec = ""; // Not used by the diagnostic.
1810 DiagID = diag::err_bool_redeclaration;
Fariborz Jahaniane106a0b2011-04-19 21:42:37 +00001811 // For better error recovery.
1812 Tok.setKind(tok::identifier);
Argyrios Kyrtzidis4383e182010-11-16 18:18:13 +00001813 isInvalid = true;
1814 } else {
1815 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec,
1816 DiagID);
1817 }
Chris Lattner80d0c892009-01-21 19:48:37 +00001818 break;
1819 case tok::kw__Decimal32:
John McCallfec54012009-08-03 20:12:06 +00001820 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
1821 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001822 break;
1823 case tok::kw__Decimal64:
John McCallfec54012009-08-03 20:12:06 +00001824 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
1825 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001826 break;
1827 case tok::kw__Decimal128:
John McCallfec54012009-08-03 20:12:06 +00001828 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
1829 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001830 break;
John Thompson82287d12010-02-05 00:12:22 +00001831 case tok::kw___vector:
1832 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
1833 break;
1834 case tok::kw___pixel:
1835 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
1836 break;
John McCalla5fc4722011-04-09 22:50:59 +00001837 case tok::kw___unknown_anytype:
1838 isInvalid = DS.SetTypeSpecType(TST_unknown_anytype, Loc,
1839 PrevSpec, DiagID);
1840 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00001841
1842 // class-specifier:
1843 case tok::kw_class:
1844 case tok::kw_struct:
Chris Lattner4c97d762009-04-12 21:49:30 +00001845 case tok::kw_union: {
1846 tok::TokenKind Kind = Tok.getKind();
1847 ConsumeToken();
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001848 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS);
Chris Lattner80d0c892009-01-21 19:48:37 +00001849 continue;
Chris Lattner4c97d762009-04-12 21:49:30 +00001850 }
Chris Lattner80d0c892009-01-21 19:48:37 +00001851
1852 // enum-specifier:
1853 case tok::kw_enum:
Chris Lattner4c97d762009-04-12 21:49:30 +00001854 ConsumeToken();
Douglas Gregor9b9edd62010-03-02 17:53:14 +00001855 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS);
Chris Lattner80d0c892009-01-21 19:48:37 +00001856 continue;
1857
1858 // cv-qualifier:
1859 case tok::kw_const:
John McCallfec54012009-08-03 20:12:06 +00001860 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec, DiagID,
1861 getLang());
Chris Lattner80d0c892009-01-21 19:48:37 +00001862 break;
1863 case tok::kw_volatile:
John McCallfec54012009-08-03 20:12:06 +00001864 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
1865 getLang());
Chris Lattner80d0c892009-01-21 19:48:37 +00001866 break;
1867 case tok::kw_restrict:
John McCallfec54012009-08-03 20:12:06 +00001868 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
1869 getLang());
Chris Lattner80d0c892009-01-21 19:48:37 +00001870 break;
1871
Douglas Gregord57959a2009-03-27 23:10:48 +00001872 // C++ typename-specifier:
1873 case tok::kw_typename:
John McCall9ba61662010-02-26 08:45:28 +00001874 if (TryAnnotateTypeOrScopeToken()) {
1875 DS.SetTypeSpecError();
1876 goto DoneWithDeclSpec;
1877 }
1878 if (!Tok.is(tok::kw_typename))
Douglas Gregord57959a2009-03-27 23:10:48 +00001879 continue;
1880 break;
1881
Chris Lattner80d0c892009-01-21 19:48:37 +00001882 // GNU typeof support.
1883 case tok::kw_typeof:
1884 ParseTypeofSpecifier(DS);
1885 continue;
1886
Anders Carlsson6fd634f2009-06-24 17:47:40 +00001887 case tok::kw_decltype:
1888 ParseDecltypeSpecifier(DS);
1889 continue;
1890
Sean Huntdb5d44b2011-05-19 05:37:45 +00001891 case tok::kw___underlying_type:
1892 ParseUnderlyingTypeSpecifier(DS);
1893
Peter Collingbourne207f4d82011-03-18 22:38:29 +00001894 // OpenCL qualifiers:
1895 case tok::kw_private:
1896 if (!getLang().OpenCL)
1897 goto DoneWithDeclSpec;
1898 case tok::kw___private:
1899 case tok::kw___global:
1900 case tok::kw___local:
1901 case tok::kw___constant:
1902 case tok::kw___read_only:
1903 case tok::kw___write_only:
1904 case tok::kw___read_write:
1905 ParseOpenCLQualifiers(DS);
1906 break;
1907
Steve Naroffd3ded1f2008-06-05 00:02:44 +00001908 case tok::less:
Chris Lattner3bd934a2008-07-26 01:18:38 +00001909 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattnerbce61352008-07-26 00:20:22 +00001910 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
1911 // but we support it.
Chris Lattner3bd934a2008-07-26 01:18:38 +00001912 if (DS.hasTypeSpecifier() || !getLang().ObjC1)
Chris Lattnerbce61352008-07-26 00:20:22 +00001913 goto DoneWithDeclSpec;
Mike Stump1eb44332009-09-09 15:08:12 +00001914
Douglas Gregor46f936e2010-11-19 17:10:50 +00001915 if (!ParseObjCProtocolQualifiers(DS))
1916 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
1917 << FixItHint::CreateInsertion(Loc, "id")
1918 << SourceRange(Loc, DS.getSourceRange().getEnd());
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001919
1920 // Need to support trailing type qualifiers (e.g. "id<p> const").
1921 // If a type specifier follows, it will be diagnosed elsewhere.
1922 continue;
Reid Spencer5f016e22007-07-11 17:01:13 +00001923 }
John McCallfec54012009-08-03 20:12:06 +00001924 // If the specifier wasn't legal, issue a diagnostic.
Reid Spencer5f016e22007-07-11 17:01:13 +00001925 if (isInvalid) {
1926 assert(PrevSpec && "Method did not return previous specifier!");
John McCallfec54012009-08-03 20:12:06 +00001927 assert(DiagID);
Douglas Gregorae2fb142010-08-23 14:34:43 +00001928
1929 if (DiagID == diag::ext_duplicate_declspec)
1930 Diag(Tok, DiagID)
1931 << PrevSpec << FixItHint::CreateRemoval(Tok.getLocation());
1932 else
1933 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00001934 }
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00001935
Chris Lattner81c018d2008-03-13 06:29:04 +00001936 DS.SetRangeEnd(Tok.getLocation());
Fariborz Jahaniane106a0b2011-04-19 21:42:37 +00001937 if (DiagID != diag::err_bool_redeclaration)
1938 ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00001939 }
1940}
Douglas Gregoradcac882008-12-01 23:54:00 +00001941
Chris Lattner7a0ab5f2009-01-06 06:59:53 +00001942/// ParseOptionalTypeSpecifier - Try to parse a single type-specifier. We
Douglas Gregor12e083c2008-11-07 15:42:26 +00001943/// primarily follow the C++ grammar with additions for C99 and GNU,
1944/// which together subsume the C grammar. Note that the C++
1945/// type-specifier also includes the C type-qualifier (for const,
1946/// volatile, and C99 restrict). Returns true if a type-specifier was
1947/// found (and parsed), false otherwise.
1948///
1949/// type-specifier: [C++ 7.1.5]
1950/// simple-type-specifier
1951/// class-specifier
1952/// enum-specifier
1953/// elaborated-type-specifier [TODO]
1954/// cv-qualifier
1955///
1956/// cv-qualifier: [C++ 7.1.5.1]
1957/// 'const'
1958/// 'volatile'
1959/// [C99] 'restrict'
1960///
1961/// simple-type-specifier: [ C++ 7.1.5.2]
1962/// '::'[opt] nested-name-specifier[opt] type-name [TODO]
1963/// '::'[opt] nested-name-specifier 'template' template-id [TODO]
1964/// 'char'
1965/// 'wchar_t'
1966/// 'bool'
1967/// 'short'
1968/// 'int'
1969/// 'long'
1970/// 'signed'
1971/// 'unsigned'
1972/// 'float'
1973/// 'double'
1974/// 'void'
1975/// [C99] '_Bool'
1976/// [C99] '_Complex'
1977/// [C99] '_Imaginary' // Removed in TC2?
1978/// [GNU] '_Decimal32'
1979/// [GNU] '_Decimal64'
1980/// [GNU] '_Decimal128'
1981/// [GNU] typeof-specifier
1982/// [OBJC] class-name objc-protocol-refs[opt] [TODO]
1983/// [OBJC] typedef-name objc-protocol-refs[opt] [TODO]
Anders Carlsson6fd634f2009-06-24 17:47:40 +00001984/// [C++0x] 'decltype' ( expression )
John Thompson82287d12010-02-05 00:12:22 +00001985/// [AltiVec] '__vector'
John McCallfec54012009-08-03 20:12:06 +00001986bool Parser::ParseOptionalTypeSpecifier(DeclSpec &DS, bool& isInvalid,
Chris Lattner7a0ab5f2009-01-06 06:59:53 +00001987 const char *&PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001988 unsigned &DiagID,
Sebastian Redld9bafa72010-02-03 21:21:43 +00001989 const ParsedTemplateInfo &TemplateInfo,
1990 bool SuppressDeclarations) {
Douglas Gregor12e083c2008-11-07 15:42:26 +00001991 SourceLocation Loc = Tok.getLocation();
1992
1993 switch (Tok.getKind()) {
Chris Lattner166a8fc2009-01-04 23:41:41 +00001994 case tok::identifier: // foo::bar
Douglas Gregorc0b39642010-04-15 23:40:53 +00001995 // If we already have a type specifier, this identifier is not a type.
1996 if (DS.getTypeSpecType() != DeclSpec::TST_unspecified ||
1997 DS.getTypeSpecWidth() != DeclSpec::TSW_unspecified ||
1998 DS.getTypeSpecSign() != DeclSpec::TSS_unspecified)
1999 return false;
John Thompson82287d12010-02-05 00:12:22 +00002000 // Check for need to substitute AltiVec keyword tokens.
2001 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
2002 break;
2003 // Fall through.
Douglas Gregord57959a2009-03-27 23:10:48 +00002004 case tok::kw_typename: // typename foo::bar
Chris Lattner166a8fc2009-01-04 23:41:41 +00002005 // Annotate typenames and C++ scope specifiers. If we get one, just
2006 // recurse to handle whatever we get.
2007 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00002008 return true;
2009 if (Tok.is(tok::identifier))
2010 return false;
2011 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
2012 TemplateInfo, SuppressDeclarations);
Chris Lattner166a8fc2009-01-04 23:41:41 +00002013 case tok::coloncolon: // ::foo::bar
2014 if (NextToken().is(tok::kw_new) || // ::new
2015 NextToken().is(tok::kw_delete)) // ::delete
2016 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002017
Chris Lattner166a8fc2009-01-04 23:41:41 +00002018 // Annotate typenames and C++ scope specifiers. If we get one, just
2019 // recurse to handle whatever we get.
2020 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00002021 return true;
2022 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
2023 TemplateInfo, SuppressDeclarations);
Mike Stump1eb44332009-09-09 15:08:12 +00002024
Douglas Gregor12e083c2008-11-07 15:42:26 +00002025 // simple-type-specifier:
Chris Lattnerb31757b2009-01-06 05:06:21 +00002026 case tok::annot_typename: {
John McCallb3d87482010-08-24 05:47:05 +00002027 if (ParsedType T = getTypeAnnotation(Tok)) {
Nico Weber253e80b2010-11-22 10:30:56 +00002028 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename,
2029 Tok.getAnnotationEndLoc(), PrevSpec,
John McCallb3d87482010-08-24 05:47:05 +00002030 DiagID, T);
2031 } else
Douglas Gregor31a19b62009-04-01 21:51:26 +00002032 DS.SetTypeSpecError();
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002033 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
2034 ConsumeToken(); // The typename
Mike Stump1eb44332009-09-09 15:08:12 +00002035
Douglas Gregor12e083c2008-11-07 15:42:26 +00002036 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
2037 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
2038 // Objective-C interface. If we don't have Objective-C or a '<', this is
2039 // just a normal reference to a typedef name.
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00002040 if (Tok.is(tok::less) && getLang().ObjC1)
2041 ParseObjCProtocolQualifiers(DS);
2042
Douglas Gregor12e083c2008-11-07 15:42:26 +00002043 return true;
2044 }
2045
2046 case tok::kw_short:
John McCallfec54012009-08-03 20:12:06 +00002047 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002048 break;
2049 case tok::kw_long:
2050 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCallfec54012009-08-03 20:12:06 +00002051 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
2052 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002053 else
John McCallfec54012009-08-03 20:12:06 +00002054 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2055 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002056 break;
Francois Pichet338d7f72011-04-28 01:59:37 +00002057 case tok::kw___int64:
2058 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2059 DiagID);
2060 break;
Douglas Gregor12e083c2008-11-07 15:42:26 +00002061 case tok::kw_signed:
John McCallfec54012009-08-03 20:12:06 +00002062 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002063 break;
2064 case tok::kw_unsigned:
John McCallfec54012009-08-03 20:12:06 +00002065 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
2066 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002067 break;
2068 case tok::kw__Complex:
John McCallfec54012009-08-03 20:12:06 +00002069 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
2070 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002071 break;
2072 case tok::kw__Imaginary:
John McCallfec54012009-08-03 20:12:06 +00002073 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
2074 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002075 break;
2076 case tok::kw_void:
John McCallfec54012009-08-03 20:12:06 +00002077 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002078 break;
2079 case tok::kw_char:
John McCallfec54012009-08-03 20:12:06 +00002080 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002081 break;
2082 case tok::kw_int:
John McCallfec54012009-08-03 20:12:06 +00002083 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002084 break;
2085 case tok::kw_float:
John McCallfec54012009-08-03 20:12:06 +00002086 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002087 break;
2088 case tok::kw_double:
John McCallfec54012009-08-03 20:12:06 +00002089 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002090 break;
2091 case tok::kw_wchar_t:
John McCallfec54012009-08-03 20:12:06 +00002092 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002093 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002094 case tok::kw_char16_t:
John McCallfec54012009-08-03 20:12:06 +00002095 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002096 break;
2097 case tok::kw_char32_t:
John McCallfec54012009-08-03 20:12:06 +00002098 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002099 break;
Douglas Gregor12e083c2008-11-07 15:42:26 +00002100 case tok::kw_bool:
2101 case tok::kw__Bool:
John McCallfec54012009-08-03 20:12:06 +00002102 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002103 break;
2104 case tok::kw__Decimal32:
John McCallfec54012009-08-03 20:12:06 +00002105 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
2106 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002107 break;
2108 case tok::kw__Decimal64:
John McCallfec54012009-08-03 20:12:06 +00002109 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
2110 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002111 break;
2112 case tok::kw__Decimal128:
John McCallfec54012009-08-03 20:12:06 +00002113 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
2114 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002115 break;
John Thompson82287d12010-02-05 00:12:22 +00002116 case tok::kw___vector:
2117 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
2118 break;
2119 case tok::kw___pixel:
2120 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
2121 break;
2122
Douglas Gregor12e083c2008-11-07 15:42:26 +00002123 // class-specifier:
2124 case tok::kw_class:
2125 case tok::kw_struct:
Chris Lattner4c97d762009-04-12 21:49:30 +00002126 case tok::kw_union: {
2127 tok::TokenKind Kind = Tok.getKind();
2128 ConsumeToken();
Sebastian Redld9bafa72010-02-03 21:21:43 +00002129 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS_none,
2130 SuppressDeclarations);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002131 return true;
Chris Lattner4c97d762009-04-12 21:49:30 +00002132 }
Douglas Gregor12e083c2008-11-07 15:42:26 +00002133
2134 // enum-specifier:
2135 case tok::kw_enum:
Chris Lattner4c97d762009-04-12 21:49:30 +00002136 ConsumeToken();
Douglas Gregor9b9edd62010-03-02 17:53:14 +00002137 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS_none);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002138 return true;
2139
2140 // cv-qualifier:
2141 case tok::kw_const:
2142 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00002143 DiagID, getLang());
Douglas Gregor12e083c2008-11-07 15:42:26 +00002144 break;
2145 case tok::kw_volatile:
2146 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00002147 DiagID, getLang());
Douglas Gregor12e083c2008-11-07 15:42:26 +00002148 break;
2149 case tok::kw_restrict:
2150 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00002151 DiagID, getLang());
Douglas Gregor12e083c2008-11-07 15:42:26 +00002152 break;
2153
2154 // GNU typeof support.
2155 case tok::kw_typeof:
2156 ParseTypeofSpecifier(DS);
2157 return true;
2158
Anders Carlsson6fd634f2009-06-24 17:47:40 +00002159 // C++0x decltype support.
2160 case tok::kw_decltype:
2161 ParseDecltypeSpecifier(DS);
2162 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00002163
Sean Huntdb5d44b2011-05-19 05:37:45 +00002164 // C++0x type traits support.
2165 case tok::kw___underlying_type:
2166 ParseUnderlyingTypeSpecifier(DS);
2167 return true;
2168
Peter Collingbourne207f4d82011-03-18 22:38:29 +00002169 // OpenCL qualifiers:
2170 case tok::kw_private:
2171 if (!getLang().OpenCL)
2172 return false;
2173 case tok::kw___private:
2174 case tok::kw___global:
2175 case tok::kw___local:
2176 case tok::kw___constant:
2177 case tok::kw___read_only:
2178 case tok::kw___write_only:
2179 case tok::kw___read_write:
2180 ParseOpenCLQualifiers(DS);
2181 break;
2182
Anders Carlsson0b7f7892009-06-26 23:44:14 +00002183 // C++0x auto support.
2184 case tok::kw_auto:
2185 if (!getLang().CPlusPlus0x)
2186 return false;
2187
John McCallfec54012009-08-03 20:12:06 +00002188 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec, DiagID);
Anders Carlsson0b7f7892009-06-26 23:44:14 +00002189 break;
Dawn Perchik52fc3142010-09-03 01:29:35 +00002190
Eli Friedman290eeb02009-06-08 23:27:34 +00002191 case tok::kw___ptr64:
2192 case tok::kw___w64:
Steve Naroff239f0732008-12-25 14:16:32 +00002193 case tok::kw___cdecl:
2194 case tok::kw___stdcall:
2195 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00002196 case tok::kw___thiscall:
John McCall7f040a92010-12-24 02:08:15 +00002197 ParseMicrosoftTypeAttributes(DS.getAttributes());
Chris Lattner837acd02009-01-21 19:19:26 +00002198 return true;
Steve Naroff239f0732008-12-25 14:16:32 +00002199
Dawn Perchik52fc3142010-09-03 01:29:35 +00002200 case tok::kw___pascal:
John McCall7f040a92010-12-24 02:08:15 +00002201 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik52fc3142010-09-03 01:29:35 +00002202 return true;
2203
Douglas Gregor12e083c2008-11-07 15:42:26 +00002204 default:
2205 // Not a type-specifier; do nothing.
2206 return false;
2207 }
2208
2209 // If the specifier combination wasn't legal, issue a diagnostic.
2210 if (isInvalid) {
2211 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner1ab3b962008-11-18 07:48:38 +00002212 // Pick between error or extwarn.
Chris Lattner1ab3b962008-11-18 07:48:38 +00002213 Diag(Tok, DiagID) << PrevSpec;
Douglas Gregor12e083c2008-11-07 15:42:26 +00002214 }
2215 DS.SetRangeEnd(Tok.getLocation());
2216 ConsumeToken(); // whatever we parsed above.
2217 return true;
2218}
Reid Spencer5f016e22007-07-11 17:01:13 +00002219
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002220/// ParseStructDeclaration - Parse a struct declaration without the terminating
2221/// semicolon.
2222///
Reid Spencer5f016e22007-07-11 17:01:13 +00002223/// struct-declaration:
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002224/// specifier-qualifier-list struct-declarator-list
Reid Spencer5f016e22007-07-11 17:01:13 +00002225/// [GNU] __extension__ struct-declaration
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002226/// [GNU] specifier-qualifier-list
Reid Spencer5f016e22007-07-11 17:01:13 +00002227/// struct-declarator-list:
2228/// struct-declarator
2229/// struct-declarator-list ',' struct-declarator
2230/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
2231/// struct-declarator:
2232/// declarator
2233/// [GNU] declarator attributes[opt]
2234/// declarator[opt] ':' constant-expression
2235/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
2236///
Chris Lattnere1359422008-04-10 06:46:29 +00002237void Parser::
John McCallbdd563e2009-11-03 02:38:08 +00002238ParseStructDeclaration(DeclSpec &DS, FieldCallback &Fields) {
Chris Lattnerc46d1a12008-10-20 06:45:43 +00002239 if (Tok.is(tok::kw___extension__)) {
2240 // __extension__ silences extension warnings in the subexpression.
2241 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroff28a7ca82007-08-20 22:28:22 +00002242 ConsumeToken();
Chris Lattnerc46d1a12008-10-20 06:45:43 +00002243 return ParseStructDeclaration(DS, Fields);
2244 }
Mike Stump1eb44332009-09-09 15:08:12 +00002245
Steve Naroff28a7ca82007-08-20 22:28:22 +00002246 // Parse the common specifier-qualifiers-list piece.
Steve Naroff28a7ca82007-08-20 22:28:22 +00002247 ParseSpecifierQualifierList(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00002248
Douglas Gregor4920f1f2009-01-12 22:49:06 +00002249 // If there are no declarators, this is a free-standing declaration
2250 // specifier. Let the actions module cope with it.
Chris Lattner04d66662007-10-09 17:33:22 +00002251 if (Tok.is(tok::semi)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00002252 Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none, DS);
Steve Naroff28a7ca82007-08-20 22:28:22 +00002253 return;
2254 }
2255
2256 // Read struct-declarators until we find the semicolon.
John McCallbdd563e2009-11-03 02:38:08 +00002257 bool FirstDeclarator = true;
Steve Naroff28a7ca82007-08-20 22:28:22 +00002258 while (1) {
John McCall54abf7d2009-11-04 02:18:39 +00002259 ParsingDeclRAIIObject PD(*this);
John McCallbdd563e2009-11-03 02:38:08 +00002260 FieldDeclarator DeclaratorInfo(DS);
2261
2262 // Attributes are only allowed here on successive declarators.
John McCall7f040a92010-12-24 02:08:15 +00002263 if (!FirstDeclarator)
2264 MaybeParseGNUAttributes(DeclaratorInfo.D);
Mike Stump1eb44332009-09-09 15:08:12 +00002265
Steve Naroff28a7ca82007-08-20 22:28:22 +00002266 /// struct-declarator: declarator
2267 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattnera1efc8c2009-12-10 01:59:24 +00002268 if (Tok.isNot(tok::colon)) {
2269 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
2270 ColonProtectionRAIIObject X(*this);
Chris Lattnere1359422008-04-10 06:46:29 +00002271 ParseDeclarator(DeclaratorInfo.D);
Chris Lattnera1efc8c2009-12-10 01:59:24 +00002272 }
Mike Stump1eb44332009-09-09 15:08:12 +00002273
Chris Lattner04d66662007-10-09 17:33:22 +00002274 if (Tok.is(tok::colon)) {
Steve Naroff28a7ca82007-08-20 22:28:22 +00002275 ConsumeToken();
John McCall60d7b3a2010-08-24 06:29:42 +00002276 ExprResult Res(ParseConstantExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002277 if (Res.isInvalid())
Steve Naroff28a7ca82007-08-20 22:28:22 +00002278 SkipUntil(tok::semi, true, true);
Chris Lattner60b1e3e2008-04-10 06:15:14 +00002279 else
Sebastian Redleffa8d12008-12-10 00:02:53 +00002280 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroff28a7ca82007-08-20 22:28:22 +00002281 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00002282
Steve Naroff28a7ca82007-08-20 22:28:22 +00002283 // If attributes exist after the declarator, parse them.
John McCall7f040a92010-12-24 02:08:15 +00002284 MaybeParseGNUAttributes(DeclaratorInfo.D);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002285
John McCallbdd563e2009-11-03 02:38:08 +00002286 // We're done with this declarator; invoke the callback.
John McCalld226f652010-08-21 09:40:31 +00002287 Decl *D = Fields.invoke(DeclaratorInfo);
John McCall54abf7d2009-11-04 02:18:39 +00002288 PD.complete(D);
John McCallbdd563e2009-11-03 02:38:08 +00002289
Steve Naroff28a7ca82007-08-20 22:28:22 +00002290 // If we don't have a comma, it is either the end of the list (a ';')
2291 // or an error, bail out.
Chris Lattner04d66662007-10-09 17:33:22 +00002292 if (Tok.isNot(tok::comma))
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002293 return;
Sebastian Redlab197ba2009-02-09 18:23:29 +00002294
Steve Naroff28a7ca82007-08-20 22:28:22 +00002295 // Consume the comma.
2296 ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00002297
John McCallbdd563e2009-11-03 02:38:08 +00002298 FirstDeclarator = false;
Steve Naroff28a7ca82007-08-20 22:28:22 +00002299 }
Steve Naroff28a7ca82007-08-20 22:28:22 +00002300}
2301
2302/// ParseStructUnionBody
2303/// struct-contents:
2304/// struct-declaration-list
2305/// [EXT] empty
2306/// [GNU] "struct-declaration-list" without terminatoring ';'
2307/// struct-declaration-list:
2308/// struct-declaration
2309/// struct-declaration-list struct-declaration
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002310/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroff28a7ca82007-08-20 22:28:22 +00002311///
Reid Spencer5f016e22007-07-11 17:01:13 +00002312void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
John McCalld226f652010-08-21 09:40:31 +00002313 unsigned TagType, Decl *TagDecl) {
John McCallf312b1e2010-08-26 23:41:50 +00002314 PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc,
2315 "parsing struct/union body");
Mike Stump1eb44332009-09-09 15:08:12 +00002316
Reid Spencer5f016e22007-07-11 17:01:13 +00002317 SourceLocation LBraceLoc = ConsumeBrace();
Mike Stump1eb44332009-09-09 15:08:12 +00002318
Douglas Gregor3218c4b2009-01-09 22:42:13 +00002319 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
Douglas Gregor23c94db2010-07-02 17:43:08 +00002320 Actions.ActOnTagStartDefinition(getCurScope(), TagDecl);
Douglas Gregor72de6672009-01-08 20:45:30 +00002321
Reid Spencer5f016e22007-07-11 17:01:13 +00002322 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
2323 // C++.
Douglas Gregore37ac4f2008-04-13 21:30:24 +00002324 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Douglas Gregor03332962010-07-29 14:29:34 +00002325 Diag(Tok, diag::ext_empty_struct_union)
2326 << (TagType == TST_union);
Reid Spencer5f016e22007-07-11 17:01:13 +00002327
John McCalld226f652010-08-21 09:40:31 +00002328 llvm::SmallVector<Decl *, 32> FieldDecls;
Chris Lattnere1359422008-04-10 06:46:29 +00002329
Reid Spencer5f016e22007-07-11 17:01:13 +00002330 // While we still have something to read, read the declarations in the struct.
Chris Lattner04d66662007-10-09 17:33:22 +00002331 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002332 // Each iteration of this loop reads one struct-declaration.
Mike Stump1eb44332009-09-09 15:08:12 +00002333
Reid Spencer5f016e22007-07-11 17:01:13 +00002334 // Check for extraneous top-level semicolon.
Chris Lattner04d66662007-10-09 17:33:22 +00002335 if (Tok.is(tok::semi)) {
Douglas Gregor9b3064b2009-04-01 22:41:11 +00002336 Diag(Tok, diag::ext_extra_struct_semi)
Douglas Gregorf13ca062010-06-16 23:08:59 +00002337 << DeclSpec::getSpecifierName((DeclSpec::TST)TagType)
Douglas Gregor849b2432010-03-31 17:46:05 +00002338 << FixItHint::CreateRemoval(Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00002339 ConsumeToken();
2340 continue;
2341 }
Chris Lattnere1359422008-04-10 06:46:29 +00002342
2343 // Parse all the comma separated declarators.
John McCall0b7e6782011-03-24 11:26:52 +00002344 DeclSpec DS(AttrFactory);
Mike Stump1eb44332009-09-09 15:08:12 +00002345
John McCallbdd563e2009-11-03 02:38:08 +00002346 if (!Tok.is(tok::at)) {
2347 struct CFieldCallback : FieldCallback {
2348 Parser &P;
John McCalld226f652010-08-21 09:40:31 +00002349 Decl *TagDecl;
2350 llvm::SmallVectorImpl<Decl *> &FieldDecls;
John McCallbdd563e2009-11-03 02:38:08 +00002351
John McCalld226f652010-08-21 09:40:31 +00002352 CFieldCallback(Parser &P, Decl *TagDecl,
2353 llvm::SmallVectorImpl<Decl *> &FieldDecls) :
John McCallbdd563e2009-11-03 02:38:08 +00002354 P(P), TagDecl(TagDecl), FieldDecls(FieldDecls) {}
2355
John McCalld226f652010-08-21 09:40:31 +00002356 virtual Decl *invoke(FieldDeclarator &FD) {
John McCallbdd563e2009-11-03 02:38:08 +00002357 // Install the declarator into the current TagDecl.
John McCalld226f652010-08-21 09:40:31 +00002358 Decl *Field = P.Actions.ActOnField(P.getCurScope(), TagDecl,
John McCall4ba39712009-11-03 21:13:47 +00002359 FD.D.getDeclSpec().getSourceRange().getBegin(),
2360 FD.D, FD.BitfieldSize);
John McCallbdd563e2009-11-03 02:38:08 +00002361 FieldDecls.push_back(Field);
2362 return Field;
Douglas Gregor91a28862009-08-26 14:27:30 +00002363 }
John McCallbdd563e2009-11-03 02:38:08 +00002364 } Callback(*this, TagDecl, FieldDecls);
2365
2366 ParseStructDeclaration(DS, Callback);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002367 } else { // Handle @defs
2368 ConsumeToken();
2369 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
2370 Diag(Tok, diag::err_unexpected_at);
Chris Lattner3e156ad2010-02-02 00:37:27 +00002371 SkipUntil(tok::semi, true);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002372 continue;
2373 }
2374 ConsumeToken();
2375 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
2376 if (!Tok.is(tok::identifier)) {
2377 Diag(Tok, diag::err_expected_ident);
Chris Lattner3e156ad2010-02-02 00:37:27 +00002378 SkipUntil(tok::semi, true);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002379 continue;
2380 }
John McCalld226f652010-08-21 09:40:31 +00002381 llvm::SmallVector<Decl *, 16> Fields;
Douglas Gregor23c94db2010-07-02 17:43:08 +00002382 Actions.ActOnDefs(getCurScope(), TagDecl, Tok.getLocation(),
Douglas Gregor44b43212008-12-11 16:49:14 +00002383 Tok.getIdentifierInfo(), Fields);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002384 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
2385 ConsumeToken();
2386 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
Mike Stump1eb44332009-09-09 15:08:12 +00002387 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002388
Chris Lattner04d66662007-10-09 17:33:22 +00002389 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002390 ConsumeToken();
Chris Lattner04d66662007-10-09 17:33:22 +00002391 } else if (Tok.is(tok::r_brace)) {
Chris Lattner3e156ad2010-02-02 00:37:27 +00002392 ExpectAndConsume(tok::semi, diag::ext_expected_semi_decl_list);
Reid Spencer5f016e22007-07-11 17:01:13 +00002393 break;
2394 } else {
Chris Lattner3e156ad2010-02-02 00:37:27 +00002395 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
2396 // Skip to end of block or statement to avoid ext-warning on extra ';'.
Reid Spencer5f016e22007-07-11 17:01:13 +00002397 SkipUntil(tok::r_brace, true, true);
Chris Lattner3e156ad2010-02-02 00:37:27 +00002398 // If we stopped at a ';', eat it.
2399 if (Tok.is(tok::semi)) ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00002400 }
2401 }
Mike Stump1eb44332009-09-09 15:08:12 +00002402
Steve Naroff60fccee2007-10-29 21:38:07 +00002403 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00002404
John McCall0b7e6782011-03-24 11:26:52 +00002405 ParsedAttributes attrs(AttrFactory);
Reid Spencer5f016e22007-07-11 17:01:13 +00002406 // If attributes exist after struct contents, parse them.
John McCall7f040a92010-12-24 02:08:15 +00002407 MaybeParseGNUAttributes(attrs);
Daniel Dunbar1bfe1c22008-10-03 02:03:53 +00002408
Douglas Gregor23c94db2010-07-02 17:43:08 +00002409 Actions.ActOnFields(getCurScope(),
Jay Foadbeaaccd2009-05-21 09:52:38 +00002410 RecordLoc, TagDecl, FieldDecls.data(), FieldDecls.size(),
Daniel Dunbar1bfe1c22008-10-03 02:03:53 +00002411 LBraceLoc, RBraceLoc,
John McCall7f040a92010-12-24 02:08:15 +00002412 attrs.getList());
Douglas Gregor72de6672009-01-08 20:45:30 +00002413 StructScope.Exit();
Douglas Gregor23c94db2010-07-02 17:43:08 +00002414 Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl, RBraceLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00002415}
2416
Reid Spencer5f016e22007-07-11 17:01:13 +00002417/// ParseEnumSpecifier
2418/// enum-specifier: [C99 6.7.2.2]
2419/// 'enum' identifier[opt] '{' enumerator-list '}'
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002420///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Reid Spencer5f016e22007-07-11 17:01:13 +00002421/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
2422/// '}' attributes[opt]
2423/// 'enum' identifier
2424/// [GNU] 'enum' attributes[opt] identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002425///
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002426/// [C++0x] enum-head '{' enumerator-list[opt] '}'
2427/// [C++0x] enum-head '{' enumerator-list ',' '}'
2428///
2429/// enum-head: [C++0x]
2430/// enum-key attributes[opt] identifier[opt] enum-base[opt]
2431/// enum-key attributes[opt] nested-name-specifier identifier enum-base[opt]
2432///
2433/// enum-key: [C++0x]
2434/// 'enum'
2435/// 'enum' 'class'
2436/// 'enum' 'struct'
2437///
2438/// enum-base: [C++0x]
2439/// ':' type-specifier-seq
2440///
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002441/// [C++] elaborated-type-specifier:
2442/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
2443///
Chris Lattner4c97d762009-04-12 21:49:30 +00002444void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregor9b9edd62010-03-02 17:53:14 +00002445 const ParsedTemplateInfo &TemplateInfo,
Chris Lattner4c97d762009-04-12 21:49:30 +00002446 AccessSpecifier AS) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002447 // Parse the tag portion of this.
Douglas Gregor374929f2009-09-18 15:37:17 +00002448 if (Tok.is(tok::code_completion)) {
2449 // Code completion for an enum name.
Douglas Gregor23c94db2010-07-02 17:43:08 +00002450 Actions.CodeCompleteTag(getCurScope(), DeclSpec::TST_enum);
Douglas Gregordc845342010-05-25 05:58:43 +00002451 ConsumeCodeCompletionToken();
Douglas Gregor374929f2009-09-18 15:37:17 +00002452 }
2453
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002454 // If attributes exist after tag, parse them.
John McCall0b7e6782011-03-24 11:26:52 +00002455 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00002456 MaybeParseGNUAttributes(attrs);
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002457
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00002458 CXXScopeSpec &SS = DS.getTypeSpecScope();
John McCall9ba61662010-02-26 08:45:28 +00002459 if (getLang().CPlusPlus) {
John McCallb3d87482010-08-24 05:47:05 +00002460 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), false))
John McCall9ba61662010-02-26 08:45:28 +00002461 return;
2462
2463 if (SS.isSet() && Tok.isNot(tok::identifier)) {
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002464 Diag(Tok, diag::err_expected_ident);
2465 if (Tok.isNot(tok::l_brace)) {
2466 // Has no name and is not a definition.
2467 // Skip the rest of this declarator, up until the comma or semicolon.
2468 SkipUntil(tok::comma, true);
2469 return;
2470 }
2471 }
2472 }
Mike Stump1eb44332009-09-09 15:08:12 +00002473
Douglas Gregor86f208c2011-02-22 20:32:04 +00002474 bool AllowFixedUnderlyingType = getLang().CPlusPlus0x || getLang().Microsoft;
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002475 bool IsScopedEnum = false;
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00002476 bool IsScopedUsingClassTag = false;
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002477
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00002478 if (getLang().CPlusPlus0x &&
2479 (Tok.is(tok::kw_class) || Tok.is(tok::kw_struct))) {
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002480 IsScopedEnum = true;
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00002481 IsScopedUsingClassTag = Tok.is(tok::kw_class);
2482 ConsumeToken();
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002483 }
2484
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002485 // Must have either 'enum name' or 'enum {...}'.
Douglas Gregorb9075602011-02-22 02:55:24 +00002486 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace) &&
2487 (AllowFixedUnderlyingType && Tok.isNot(tok::colon))) {
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002488 Diag(Tok, diag::err_expected_ident_lbrace);
Mike Stump1eb44332009-09-09 15:08:12 +00002489
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002490 // Skip the rest of this declarator, up until the comma or semicolon.
2491 SkipUntil(tok::comma, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00002492 return;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002493 }
Mike Stump1eb44332009-09-09 15:08:12 +00002494
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002495 // If an identifier is present, consume and remember it.
2496 IdentifierInfo *Name = 0;
2497 SourceLocation NameLoc;
2498 if (Tok.is(tok::identifier)) {
2499 Name = Tok.getIdentifierInfo();
2500 NameLoc = ConsumeToken();
2501 }
Mike Stump1eb44332009-09-09 15:08:12 +00002502
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002503 if (!Name && IsScopedEnum) {
2504 // C++0x 7.2p2: The optional identifier shall not be omitted in the
2505 // declaration of a scoped enumeration.
2506 Diag(Tok, diag::err_scoped_enum_missing_identifier);
2507 IsScopedEnum = false;
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00002508 IsScopedUsingClassTag = false;
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002509 }
2510
2511 TypeResult BaseType;
2512
Douglas Gregora61b3e72010-12-01 17:42:47 +00002513 // Parse the fixed underlying type.
Douglas Gregorb9075602011-02-22 02:55:24 +00002514 if (AllowFixedUnderlyingType && Tok.is(tok::colon)) {
Douglas Gregora61b3e72010-12-01 17:42:47 +00002515 bool PossibleBitfield = false;
2516 if (getCurScope()->getFlags() & Scope::ClassScope) {
2517 // If we're in class scope, this can either be an enum declaration with
2518 // an underlying type, or a declaration of a bitfield member. We try to
2519 // use a simple disambiguation scheme first to catch the common cases
2520 // (integer literal, sizeof); if it's still ambiguous, we then consider
2521 // anything that's a simple-type-specifier followed by '(' as an
2522 // expression. This suffices because function types are not valid
2523 // underlying types anyway.
2524 TPResult TPR = isExpressionOrTypeSpecifierSimple(NextToken().getKind());
2525 // If the next token starts an expression, we know we're parsing a
2526 // bit-field. This is the common case.
2527 if (TPR == TPResult::True())
2528 PossibleBitfield = true;
2529 // If the next token starts a type-specifier-seq, it may be either a
2530 // a fixed underlying type or the start of a function-style cast in C++;
2531 // lookahead one more token to see if it's obvious that we have a
2532 // fixed underlying type.
2533 else if (TPR == TPResult::False() &&
2534 GetLookAheadToken(2).getKind() == tok::semi) {
2535 // Consume the ':'.
2536 ConsumeToken();
2537 } else {
2538 // We have the start of a type-specifier-seq, so we have to perform
2539 // tentative parsing to determine whether we have an expression or a
2540 // type.
2541 TentativeParsingAction TPA(*this);
2542
2543 // Consume the ':'.
2544 ConsumeToken();
2545
Douglas Gregor86f208c2011-02-22 20:32:04 +00002546 if ((getLang().CPlusPlus &&
2547 isCXXDeclarationSpecifier() != TPResult::True()) ||
2548 (!getLang().CPlusPlus && !isDeclarationSpecifier(true))) {
Douglas Gregora61b3e72010-12-01 17:42:47 +00002549 // We'll parse this as a bitfield later.
2550 PossibleBitfield = true;
2551 TPA.Revert();
2552 } else {
2553 // We have a type-specifier-seq.
2554 TPA.Commit();
2555 }
2556 }
2557 } else {
2558 // Consume the ':'.
2559 ConsumeToken();
2560 }
2561
2562 if (!PossibleBitfield) {
2563 SourceRange Range;
2564 BaseType = ParseTypeName(&Range);
Douglas Gregor86f208c2011-02-22 20:32:04 +00002565
2566 if (!getLang().CPlusPlus0x)
2567 Diag(StartLoc, diag::ext_ms_enum_fixed_underlying_type)
2568 << Range;
Douglas Gregora61b3e72010-12-01 17:42:47 +00002569 }
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002570 }
2571
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002572 // There are three options here. If we have 'enum foo;', then this is a
2573 // forward declaration. If we have 'enum foo {...' then this is a
2574 // definition. Otherwise we have something like 'enum foo xyz', a reference.
2575 //
2576 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
2577 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
2578 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
2579 //
John McCallf312b1e2010-08-26 23:41:50 +00002580 Sema::TagUseKind TUK;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002581 if (Tok.is(tok::l_brace))
John McCallf312b1e2010-08-26 23:41:50 +00002582 TUK = Sema::TUK_Definition;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002583 else if (Tok.is(tok::semi))
John McCallf312b1e2010-08-26 23:41:50 +00002584 TUK = Sema::TUK_Declaration;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002585 else
John McCallf312b1e2010-08-26 23:41:50 +00002586 TUK = Sema::TUK_Reference;
Douglas Gregor8fc6d232010-05-03 17:48:54 +00002587
2588 // enums cannot be templates, although they can be referenced from a
2589 // template.
2590 if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate &&
John McCallf312b1e2010-08-26 23:41:50 +00002591 TUK != Sema::TUK_Reference) {
Douglas Gregor8fc6d232010-05-03 17:48:54 +00002592 Diag(Tok, diag::err_enum_template);
2593
2594 // Skip the rest of this declarator, up until the comma or semicolon.
2595 SkipUntil(tok::comma, true);
2596 return;
2597 }
2598
Douglas Gregorb9075602011-02-22 02:55:24 +00002599 if (!Name && TUK != Sema::TUK_Definition) {
2600 Diag(Tok, diag::err_enumerator_unnamed_no_def);
2601
2602 // Skip the rest of this declarator, up until the comma or semicolon.
2603 SkipUntil(tok::comma, true);
2604 return;
2605 }
2606
Douglas Gregor402abb52009-05-28 23:31:59 +00002607 bool Owned = false;
John McCallc4e70192009-09-11 04:59:25 +00002608 bool IsDependent = false;
Douglas Gregor48c89f42010-04-24 16:38:41 +00002609 const char *PrevSpec = 0;
2610 unsigned DiagID;
John McCalld226f652010-08-21 09:40:31 +00002611 Decl *TagDecl = Actions.ActOnTag(getCurScope(), DeclSpec::TST_enum, TUK,
John McCall7f040a92010-12-24 02:08:15 +00002612 StartLoc, SS, Name, NameLoc, attrs.getList(),
John McCalld226f652010-08-21 09:40:31 +00002613 AS,
John McCallf312b1e2010-08-26 23:41:50 +00002614 MultiTemplateParamsArg(Actions),
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002615 Owned, IsDependent, IsScopedEnum,
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00002616 IsScopedUsingClassTag, BaseType);
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002617
Douglas Gregor48c89f42010-04-24 16:38:41 +00002618 if (IsDependent) {
2619 // This enum has a dependent nested-name-specifier. Handle it as a
2620 // dependent tag.
2621 if (!Name) {
2622 DS.SetTypeSpecError();
2623 Diag(Tok, diag::err_expected_type_name_after_typename);
2624 return;
2625 }
2626
Douglas Gregor23c94db2010-07-02 17:43:08 +00002627 TypeResult Type = Actions.ActOnDependentTag(getCurScope(), DeclSpec::TST_enum,
Douglas Gregor48c89f42010-04-24 16:38:41 +00002628 TUK, SS, Name, StartLoc,
2629 NameLoc);
2630 if (Type.isInvalid()) {
2631 DS.SetTypeSpecError();
2632 return;
2633 }
2634
Abramo Bagnara0daaf322011-03-16 20:16:18 +00002635 if (DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc,
2636 NameLoc.isValid() ? NameLoc : StartLoc,
2637 PrevSpec, DiagID, Type.get()))
Douglas Gregor48c89f42010-04-24 16:38:41 +00002638 Diag(StartLoc, DiagID) << PrevSpec;
2639
2640 return;
2641 }
Mike Stump1eb44332009-09-09 15:08:12 +00002642
John McCalld226f652010-08-21 09:40:31 +00002643 if (!TagDecl) {
Douglas Gregor48c89f42010-04-24 16:38:41 +00002644 // The action failed to produce an enumeration tag. If this is a
2645 // definition, consume the entire definition.
2646 if (Tok.is(tok::l_brace)) {
2647 ConsumeBrace();
2648 SkipUntil(tok::r_brace);
2649 }
2650
2651 DS.SetTypeSpecError();
2652 return;
2653 }
2654
Chris Lattner04d66662007-10-09 17:33:22 +00002655 if (Tok.is(tok::l_brace))
Reid Spencer5f016e22007-07-11 17:01:13 +00002656 ParseEnumBody(StartLoc, TagDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00002657
Abramo Bagnara0daaf322011-03-16 20:16:18 +00002658 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc,
2659 NameLoc.isValid() ? NameLoc : StartLoc,
2660 PrevSpec, DiagID, TagDecl, Owned))
John McCallfec54012009-08-03 20:12:06 +00002661 Diag(StartLoc, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00002662}
2663
2664/// ParseEnumBody - Parse a {} enclosed enumerator-list.
2665/// enumerator-list:
2666/// enumerator
2667/// enumerator-list ',' enumerator
2668/// enumerator:
2669/// enumeration-constant
2670/// enumeration-constant '=' constant-expression
2671/// enumeration-constant:
2672/// identifier
2673///
John McCalld226f652010-08-21 09:40:31 +00002674void Parser::ParseEnumBody(SourceLocation StartLoc, Decl *EnumDecl) {
Douglas Gregor074149e2009-01-05 19:45:36 +00002675 // Enter the scope of the enum body and start the definition.
2676 ParseScope EnumScope(this, Scope::DeclScope);
Douglas Gregor23c94db2010-07-02 17:43:08 +00002677 Actions.ActOnTagStartDefinition(getCurScope(), EnumDecl);
Douglas Gregor074149e2009-01-05 19:45:36 +00002678
Reid Spencer5f016e22007-07-11 17:01:13 +00002679 SourceLocation LBraceLoc = ConsumeBrace();
Mike Stump1eb44332009-09-09 15:08:12 +00002680
Chris Lattner7946dd32007-08-27 17:24:30 +00002681 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
Chris Lattner04d66662007-10-09 17:33:22 +00002682 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Fariborz Jahanian05115522010-05-28 22:23:22 +00002683 Diag(Tok, diag::error_empty_enum);
Mike Stump1eb44332009-09-09 15:08:12 +00002684
John McCalld226f652010-08-21 09:40:31 +00002685 llvm::SmallVector<Decl *, 32> EnumConstantDecls;
Reid Spencer5f016e22007-07-11 17:01:13 +00002686
John McCalld226f652010-08-21 09:40:31 +00002687 Decl *LastEnumConstDecl = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00002688
Reid Spencer5f016e22007-07-11 17:01:13 +00002689 // Parse the enumerator-list.
Chris Lattner04d66662007-10-09 17:33:22 +00002690 while (Tok.is(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002691 IdentifierInfo *Ident = Tok.getIdentifierInfo();
2692 SourceLocation IdentLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00002693
John McCall5b629aa2010-10-22 23:36:17 +00002694 // If attributes exist after the enumerator, parse them.
John McCall0b7e6782011-03-24 11:26:52 +00002695 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00002696 MaybeParseGNUAttributes(attrs);
John McCall5b629aa2010-10-22 23:36:17 +00002697
Reid Spencer5f016e22007-07-11 17:01:13 +00002698 SourceLocation EqualLoc;
John McCall60d7b3a2010-08-24 06:29:42 +00002699 ExprResult AssignedVal;
Chris Lattner04d66662007-10-09 17:33:22 +00002700 if (Tok.is(tok::equal)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002701 EqualLoc = ConsumeToken();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002702 AssignedVal = ParseConstantExpression();
2703 if (AssignedVal.isInvalid())
Reid Spencer5f016e22007-07-11 17:01:13 +00002704 SkipUntil(tok::comma, tok::r_brace, true, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00002705 }
Mike Stump1eb44332009-09-09 15:08:12 +00002706
Reid Spencer5f016e22007-07-11 17:01:13 +00002707 // Install the enumerator constant into EnumDecl.
John McCalld226f652010-08-21 09:40:31 +00002708 Decl *EnumConstDecl = Actions.ActOnEnumConstant(getCurScope(), EnumDecl,
2709 LastEnumConstDecl,
2710 IdentLoc, Ident,
John McCall7f040a92010-12-24 02:08:15 +00002711 attrs.getList(), EqualLoc,
John McCalld226f652010-08-21 09:40:31 +00002712 AssignedVal.release());
Reid Spencer5f016e22007-07-11 17:01:13 +00002713 EnumConstantDecls.push_back(EnumConstDecl);
2714 LastEnumConstDecl = EnumConstDecl;
Mike Stump1eb44332009-09-09 15:08:12 +00002715
Douglas Gregor751f6922010-09-07 14:51:08 +00002716 if (Tok.is(tok::identifier)) {
2717 // We're missing a comma between enumerators.
2718 SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation);
2719 Diag(Loc, diag::err_enumerator_list_missing_comma)
2720 << FixItHint::CreateInsertion(Loc, ", ");
2721 continue;
2722 }
2723
Chris Lattner04d66662007-10-09 17:33:22 +00002724 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +00002725 break;
2726 SourceLocation CommaLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00002727
2728 if (Tok.isNot(tok::identifier) &&
Douglas Gregor9b3064b2009-04-01 22:41:11 +00002729 !(getLang().C99 || getLang().CPlusPlus0x))
2730 Diag(CommaLoc, diag::ext_enumerator_list_comma)
2731 << getLang().CPlusPlus
Douglas Gregor849b2432010-03-31 17:46:05 +00002732 << FixItHint::CreateRemoval(CommaLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00002733 }
Mike Stump1eb44332009-09-09 15:08:12 +00002734
Reid Spencer5f016e22007-07-11 17:01:13 +00002735 // Eat the }.
Mike Stumpc6e35aa2009-05-16 07:06:02 +00002736 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00002737
Reid Spencer5f016e22007-07-11 17:01:13 +00002738 // If attributes exist after the identifier list, parse them.
John McCall0b7e6782011-03-24 11:26:52 +00002739 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00002740 MaybeParseGNUAttributes(attrs);
Douglas Gregor72de6672009-01-08 20:45:30 +00002741
Edward O'Callaghanfee13812009-08-08 14:36:57 +00002742 Actions.ActOnEnumBody(StartLoc, LBraceLoc, RBraceLoc, EnumDecl,
2743 EnumConstantDecls.data(), EnumConstantDecls.size(),
John McCall7f040a92010-12-24 02:08:15 +00002744 getCurScope(), attrs.getList());
Mike Stump1eb44332009-09-09 15:08:12 +00002745
Douglas Gregor72de6672009-01-08 20:45:30 +00002746 EnumScope.Exit();
Douglas Gregor23c94db2010-07-02 17:43:08 +00002747 Actions.ActOnTagFinishDefinition(getCurScope(), EnumDecl, RBraceLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00002748}
2749
2750/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff5f8aa692008-02-11 23:15:56 +00002751/// start of a type-qualifier-list.
2752bool Parser::isTypeQualifier() const {
2753 switch (Tok.getKind()) {
2754 default: return false;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00002755
2756 // type-qualifier only in OpenCL
2757 case tok::kw_private:
2758 return getLang().OpenCL;
2759
Steve Naroff5f8aa692008-02-11 23:15:56 +00002760 // type-qualifier
2761 case tok::kw_const:
2762 case tok::kw_volatile:
2763 case tok::kw_restrict:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00002764 case tok::kw___private:
2765 case tok::kw___local:
2766 case tok::kw___global:
2767 case tok::kw___constant:
2768 case tok::kw___read_only:
2769 case tok::kw___read_write:
2770 case tok::kw___write_only:
Steve Naroff5f8aa692008-02-11 23:15:56 +00002771 return true;
2772 }
2773}
2774
Chris Lattnerb3a4e432010-02-28 18:18:36 +00002775/// isKnownToBeTypeSpecifier - Return true if we know that the specified token
2776/// is definitely a type-specifier. Return false if it isn't part of a type
2777/// specifier or if we're not sure.
2778bool Parser::isKnownToBeTypeSpecifier(const Token &Tok) const {
2779 switch (Tok.getKind()) {
2780 default: return false;
2781 // type-specifiers
2782 case tok::kw_short:
2783 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00002784 case tok::kw___int64:
Chris Lattnerb3a4e432010-02-28 18:18:36 +00002785 case tok::kw_signed:
2786 case tok::kw_unsigned:
2787 case tok::kw__Complex:
2788 case tok::kw__Imaginary:
2789 case tok::kw_void:
2790 case tok::kw_char:
2791 case tok::kw_wchar_t:
2792 case tok::kw_char16_t:
2793 case tok::kw_char32_t:
2794 case tok::kw_int:
2795 case tok::kw_float:
2796 case tok::kw_double:
2797 case tok::kw_bool:
2798 case tok::kw__Bool:
2799 case tok::kw__Decimal32:
2800 case tok::kw__Decimal64:
2801 case tok::kw__Decimal128:
2802 case tok::kw___vector:
2803
2804 // struct-or-union-specifier (C99) or class-specifier (C++)
2805 case tok::kw_class:
2806 case tok::kw_struct:
2807 case tok::kw_union:
2808 // enum-specifier
2809 case tok::kw_enum:
2810
2811 // typedef-name
2812 case tok::annot_typename:
2813 return true;
2814 }
2815}
2816
Steve Naroff5f8aa692008-02-11 23:15:56 +00002817/// isTypeSpecifierQualifier - Return true if the current token could be the
Reid Spencer5f016e22007-07-11 17:01:13 +00002818/// start of a specifier-qualifier-list.
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002819bool Parser::isTypeSpecifierQualifier() {
Reid Spencer5f016e22007-07-11 17:01:13 +00002820 switch (Tok.getKind()) {
2821 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002822
Chris Lattner166a8fc2009-01-04 23:41:41 +00002823 case tok::identifier: // foo::bar
John Thompson82287d12010-02-05 00:12:22 +00002824 if (TryAltiVecVectorToken())
2825 return true;
2826 // Fall through.
Douglas Gregord57959a2009-03-27 23:10:48 +00002827 case tok::kw_typename: // typename T::type
Chris Lattner166a8fc2009-01-04 23:41:41 +00002828 // Annotate typenames and C++ scope specifiers. If we get one, just
2829 // recurse to handle whatever we get.
2830 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00002831 return true;
2832 if (Tok.is(tok::identifier))
2833 return false;
2834 return isTypeSpecifierQualifier();
Douglas Gregord57959a2009-03-27 23:10:48 +00002835
Chris Lattner166a8fc2009-01-04 23:41:41 +00002836 case tok::coloncolon: // ::foo::bar
2837 if (NextToken().is(tok::kw_new) || // ::new
2838 NextToken().is(tok::kw_delete)) // ::delete
2839 return false;
2840
Chris Lattner166a8fc2009-01-04 23:41:41 +00002841 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00002842 return true;
2843 return isTypeSpecifierQualifier();
Mike Stump1eb44332009-09-09 15:08:12 +00002844
Reid Spencer5f016e22007-07-11 17:01:13 +00002845 // GNU attributes support.
2846 case tok::kw___attribute:
Steve Naroffd1861fd2007-07-31 12:34:36 +00002847 // GNU typeof support.
2848 case tok::kw_typeof:
Mike Stump1eb44332009-09-09 15:08:12 +00002849
Reid Spencer5f016e22007-07-11 17:01:13 +00002850 // type-specifiers
2851 case tok::kw_short:
2852 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00002853 case tok::kw___int64:
Reid Spencer5f016e22007-07-11 17:01:13 +00002854 case tok::kw_signed:
2855 case tok::kw_unsigned:
2856 case tok::kw__Complex:
2857 case tok::kw__Imaginary:
2858 case tok::kw_void:
2859 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00002860 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002861 case tok::kw_char16_t:
2862 case tok::kw_char32_t:
Reid Spencer5f016e22007-07-11 17:01:13 +00002863 case tok::kw_int:
2864 case tok::kw_float:
2865 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00002866 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00002867 case tok::kw__Bool:
2868 case tok::kw__Decimal32:
2869 case tok::kw__Decimal64:
2870 case tok::kw__Decimal128:
John Thompson82287d12010-02-05 00:12:22 +00002871 case tok::kw___vector:
Mike Stump1eb44332009-09-09 15:08:12 +00002872
Chris Lattner99dc9142008-04-13 18:59:07 +00002873 // struct-or-union-specifier (C99) or class-specifier (C++)
2874 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00002875 case tok::kw_struct:
2876 case tok::kw_union:
2877 // enum-specifier
2878 case tok::kw_enum:
Mike Stump1eb44332009-09-09 15:08:12 +00002879
Reid Spencer5f016e22007-07-11 17:01:13 +00002880 // type-qualifier
2881 case tok::kw_const:
2882 case tok::kw_volatile:
2883 case tok::kw_restrict:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002884
2885 // typedef-name
Chris Lattnerb31757b2009-01-06 05:06:21 +00002886 case tok::annot_typename:
Reid Spencer5f016e22007-07-11 17:01:13 +00002887 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00002888
Chris Lattner7c186be2008-10-20 00:25:30 +00002889 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
2890 case tok::less:
2891 return getLang().ObjC1;
Mike Stump1eb44332009-09-09 15:08:12 +00002892
Steve Naroff239f0732008-12-25 14:16:32 +00002893 case tok::kw___cdecl:
2894 case tok::kw___stdcall:
2895 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00002896 case tok::kw___thiscall:
Eli Friedman290eeb02009-06-08 23:27:34 +00002897 case tok::kw___w64:
2898 case tok::kw___ptr64:
Dawn Perchik52fc3142010-09-03 01:29:35 +00002899 case tok::kw___pascal:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00002900
2901 case tok::kw___private:
2902 case tok::kw___local:
2903 case tok::kw___global:
2904 case tok::kw___constant:
2905 case tok::kw___read_only:
2906 case tok::kw___read_write:
2907 case tok::kw___write_only:
2908
Eli Friedman290eeb02009-06-08 23:27:34 +00002909 return true;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00002910
2911 case tok::kw_private:
2912 return getLang().OpenCL;
Reid Spencer5f016e22007-07-11 17:01:13 +00002913 }
2914}
2915
2916/// isDeclarationSpecifier() - Return true if the current token is part of a
2917/// declaration specifier.
Douglas Gregor9497a732010-09-16 01:51:54 +00002918///
2919/// \param DisambiguatingWithExpression True to indicate that the purpose of
2920/// this check is to disambiguate between an expression and a declaration.
2921bool Parser::isDeclarationSpecifier(bool DisambiguatingWithExpression) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002922 switch (Tok.getKind()) {
2923 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002924
Peter Collingbourne207f4d82011-03-18 22:38:29 +00002925 case tok::kw_private:
2926 return getLang().OpenCL;
2927
Chris Lattner166a8fc2009-01-04 23:41:41 +00002928 case tok::identifier: // foo::bar
Steve Naroff61f72cb2009-03-09 21:12:44 +00002929 // Unfortunate hack to support "Class.factoryMethod" notation.
2930 if (getLang().ObjC1 && NextToken().is(tok::period))
2931 return false;
John Thompson82287d12010-02-05 00:12:22 +00002932 if (TryAltiVecVectorToken())
2933 return true;
2934 // Fall through.
Douglas Gregord57959a2009-03-27 23:10:48 +00002935 case tok::kw_typename: // typename T::type
Chris Lattner166a8fc2009-01-04 23:41:41 +00002936 // Annotate typenames and C++ scope specifiers. If we get one, just
2937 // recurse to handle whatever we get.
2938 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00002939 return true;
2940 if (Tok.is(tok::identifier))
2941 return false;
Douglas Gregor9497a732010-09-16 01:51:54 +00002942
2943 // If we're in Objective-C and we have an Objective-C class type followed
2944 // by an identifier and then either ':' or ']', in a place where an
2945 // expression is permitted, then this is probably a class message send
2946 // missing the initial '['. In this case, we won't consider this to be
2947 // the start of a declaration.
2948 if (DisambiguatingWithExpression &&
2949 isStartOfObjCClassMessageMissingOpenBracket())
2950 return false;
2951
John McCall9ba61662010-02-26 08:45:28 +00002952 return isDeclarationSpecifier();
2953
Chris Lattner166a8fc2009-01-04 23:41:41 +00002954 case tok::coloncolon: // ::foo::bar
2955 if (NextToken().is(tok::kw_new) || // ::new
2956 NextToken().is(tok::kw_delete)) // ::delete
2957 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002958
Chris Lattner166a8fc2009-01-04 23:41:41 +00002959 // Annotate typenames and C++ scope specifiers. If we get one, just
2960 // recurse to handle whatever we get.
2961 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00002962 return true;
2963 return isDeclarationSpecifier();
Mike Stump1eb44332009-09-09 15:08:12 +00002964
Reid Spencer5f016e22007-07-11 17:01:13 +00002965 // storage-class-specifier
2966 case tok::kw_typedef:
2967 case tok::kw_extern:
Steve Naroff8d54bf22007-12-18 00:16:02 +00002968 case tok::kw___private_extern__:
Reid Spencer5f016e22007-07-11 17:01:13 +00002969 case tok::kw_static:
2970 case tok::kw_auto:
2971 case tok::kw_register:
2972 case tok::kw___thread:
Mike Stump1eb44332009-09-09 15:08:12 +00002973
Reid Spencer5f016e22007-07-11 17:01:13 +00002974 // type-specifiers
2975 case tok::kw_short:
2976 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00002977 case tok::kw___int64:
Reid Spencer5f016e22007-07-11 17:01:13 +00002978 case tok::kw_signed:
2979 case tok::kw_unsigned:
2980 case tok::kw__Complex:
2981 case tok::kw__Imaginary:
2982 case tok::kw_void:
2983 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00002984 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002985 case tok::kw_char16_t:
2986 case tok::kw_char32_t:
2987
Reid Spencer5f016e22007-07-11 17:01:13 +00002988 case tok::kw_int:
2989 case tok::kw_float:
2990 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00002991 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00002992 case tok::kw__Bool:
2993 case tok::kw__Decimal32:
2994 case tok::kw__Decimal64:
2995 case tok::kw__Decimal128:
John Thompson82287d12010-02-05 00:12:22 +00002996 case tok::kw___vector:
Mike Stump1eb44332009-09-09 15:08:12 +00002997
Chris Lattner99dc9142008-04-13 18:59:07 +00002998 // struct-or-union-specifier (C99) or class-specifier (C++)
2999 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00003000 case tok::kw_struct:
3001 case tok::kw_union:
3002 // enum-specifier
3003 case tok::kw_enum:
Mike Stump1eb44332009-09-09 15:08:12 +00003004
Reid Spencer5f016e22007-07-11 17:01:13 +00003005 // type-qualifier
3006 case tok::kw_const:
3007 case tok::kw_volatile:
3008 case tok::kw_restrict:
Steve Naroffd1861fd2007-07-31 12:34:36 +00003009
Reid Spencer5f016e22007-07-11 17:01:13 +00003010 // function-specifier
3011 case tok::kw_inline:
Douglas Gregorb48fe382008-10-31 09:07:45 +00003012 case tok::kw_virtual:
3013 case tok::kw_explicit:
Chris Lattnerd6c7c182007-08-09 16:40:21 +00003014
Peter Collingbournec6eb44b2011-04-15 00:35:57 +00003015 // static_assert-declaration
3016 case tok::kw__Static_assert:
3017
Chris Lattner1ef08762007-08-09 17:01:07 +00003018 // GNU typeof support.
3019 case tok::kw_typeof:
Mike Stump1eb44332009-09-09 15:08:12 +00003020
Chris Lattner1ef08762007-08-09 17:01:07 +00003021 // GNU attributes.
Chris Lattnerd6c7c182007-08-09 16:40:21 +00003022 case tok::kw___attribute:
Reid Spencer5f016e22007-07-11 17:01:13 +00003023 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00003024
Francois Pichete3d49b42011-06-19 08:02:06 +00003025 // C++0x decltype.
3026 case tok::kw_decltype:
3027 return true;
3028
Chris Lattnerf3948c42008-07-26 03:38:44 +00003029 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
3030 case tok::less:
3031 return getLang().ObjC1;
Mike Stump1eb44332009-09-09 15:08:12 +00003032
Douglas Gregord9d75e52011-04-27 05:41:15 +00003033 // typedef-name
3034 case tok::annot_typename:
3035 return !DisambiguatingWithExpression ||
3036 !isStartOfObjCClassMessageMissingOpenBracket();
3037
Steve Naroff47f52092009-01-06 19:34:12 +00003038 case tok::kw___declspec:
Steve Naroff239f0732008-12-25 14:16:32 +00003039 case tok::kw___cdecl:
3040 case tok::kw___stdcall:
3041 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00003042 case tok::kw___thiscall:
Eli Friedman290eeb02009-06-08 23:27:34 +00003043 case tok::kw___w64:
3044 case tok::kw___ptr64:
3045 case tok::kw___forceinline:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003046 case tok::kw___pascal:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003047
3048 case tok::kw___private:
3049 case tok::kw___local:
3050 case tok::kw___global:
3051 case tok::kw___constant:
3052 case tok::kw___read_only:
3053 case tok::kw___read_write:
3054 case tok::kw___write_only:
3055
Eli Friedman290eeb02009-06-08 23:27:34 +00003056 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00003057 }
3058}
3059
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003060bool Parser::isConstructorDeclarator() {
3061 TentativeParsingAction TPA(*this);
3062
3063 // Parse the C++ scope specifier.
3064 CXXScopeSpec SS;
John McCallb3d87482010-08-24 05:47:05 +00003065 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), true)) {
John McCall9ba61662010-02-26 08:45:28 +00003066 TPA.Revert();
3067 return false;
3068 }
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003069
3070 // Parse the constructor name.
3071 if (Tok.is(tok::identifier) || Tok.is(tok::annot_template_id)) {
3072 // We already know that we have a constructor name; just consume
3073 // the token.
3074 ConsumeToken();
3075 } else {
3076 TPA.Revert();
3077 return false;
3078 }
3079
3080 // Current class name must be followed by a left parentheses.
3081 if (Tok.isNot(tok::l_paren)) {
3082 TPA.Revert();
3083 return false;
3084 }
3085 ConsumeParen();
3086
3087 // A right parentheses or ellipsis signals that we have a constructor.
3088 if (Tok.is(tok::r_paren) || Tok.is(tok::ellipsis)) {
3089 TPA.Revert();
3090 return true;
3091 }
3092
3093 // If we need to, enter the specified scope.
3094 DeclaratorScopeObj DeclScopeObj(*this, SS);
Douglas Gregor23c94db2010-07-02 17:43:08 +00003095 if (SS.isSet() && Actions.ShouldEnterDeclaratorScope(getCurScope(), SS))
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003096 DeclScopeObj.EnterDeclaratorScope();
3097
Francois Pichetdfaa5fb2011-01-31 04:54:32 +00003098 // Optionally skip Microsoft attributes.
John McCall0b7e6782011-03-24 11:26:52 +00003099 ParsedAttributes Attrs(AttrFactory);
Francois Pichetdfaa5fb2011-01-31 04:54:32 +00003100 MaybeParseMicrosoftAttributes(Attrs);
3101
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003102 // Check whether the next token(s) are part of a declaration
3103 // specifier, in which case we have the start of a parameter and,
3104 // therefore, we know that this is a constructor.
3105 bool IsConstructor = isDeclarationSpecifier();
3106 TPA.Revert();
3107 return IsConstructor;
3108}
Reid Spencer5f016e22007-07-11 17:01:13 +00003109
3110/// ParseTypeQualifierListOpt
Dawn Perchik52fc3142010-09-03 01:29:35 +00003111/// type-qualifier-list: [C99 6.7.5]
3112/// type-qualifier
3113/// [vendor] attributes
3114/// [ only if VendorAttributesAllowed=true ]
3115/// type-qualifier-list type-qualifier
3116/// [vendor] type-qualifier-list attributes
3117/// [ only if VendorAttributesAllowed=true ]
3118/// [C++0x] attribute-specifier[opt] is allowed before cv-qualifier-seq
3119/// [ only if CXX0XAttributesAllowed=true ]
3120/// Note: vendor can be GNU, MS, etc.
Reid Spencer5f016e22007-07-11 17:01:13 +00003121///
Dawn Perchik52fc3142010-09-03 01:29:35 +00003122void Parser::ParseTypeQualifierListOpt(DeclSpec &DS,
3123 bool VendorAttributesAllowed,
Sean Huntbbd37c62009-11-21 08:43:09 +00003124 bool CXX0XAttributesAllowed) {
3125 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier()) {
3126 SourceLocation Loc = Tok.getLocation();
John McCall0b7e6782011-03-24 11:26:52 +00003127 ParsedAttributesWithRange attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00003128 ParseCXX0XAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00003129 if (CXX0XAttributesAllowed)
John McCall7f040a92010-12-24 02:08:15 +00003130 DS.takeAttributesFrom(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00003131 else
3132 Diag(Loc, diag::err_attributes_not_allowed);
3133 }
Abramo Bagnara796aa442011-03-12 11:17:06 +00003134
3135 SourceLocation EndLoc;
3136
Reid Spencer5f016e22007-07-11 17:01:13 +00003137 while (1) {
John McCallfec54012009-08-03 20:12:06 +00003138 bool isInvalid = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00003139 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00003140 unsigned DiagID = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00003141 SourceLocation Loc = Tok.getLocation();
3142
3143 switch (Tok.getKind()) {
Douglas Gregor1a480c42010-08-27 17:35:51 +00003144 case tok::code_completion:
3145 Actions.CodeCompleteTypeQualifiers(DS);
3146 ConsumeCodeCompletionToken();
3147 break;
3148
Reid Spencer5f016e22007-07-11 17:01:13 +00003149 case tok::kw_const:
John McCallfec54012009-08-03 20:12:06 +00003150 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, DiagID,
3151 getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00003152 break;
3153 case tok::kw_volatile:
John McCallfec54012009-08-03 20:12:06 +00003154 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
3155 getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00003156 break;
3157 case tok::kw_restrict:
John McCallfec54012009-08-03 20:12:06 +00003158 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
3159 getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00003160 break;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003161
3162 // OpenCL qualifiers:
3163 case tok::kw_private:
3164 if (!getLang().OpenCL)
3165 goto DoneWithTypeQuals;
3166 case tok::kw___private:
3167 case tok::kw___global:
3168 case tok::kw___local:
3169 case tok::kw___constant:
3170 case tok::kw___read_only:
3171 case tok::kw___write_only:
3172 case tok::kw___read_write:
3173 ParseOpenCLQualifiers(DS);
3174 break;
3175
Eli Friedman290eeb02009-06-08 23:27:34 +00003176 case tok::kw___w64:
Steve Naroff86bc6cf2008-12-25 14:41:26 +00003177 case tok::kw___ptr64:
Steve Naroff239f0732008-12-25 14:16:32 +00003178 case tok::kw___cdecl:
3179 case tok::kw___stdcall:
3180 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00003181 case tok::kw___thiscall:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003182 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00003183 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman290eeb02009-06-08 23:27:34 +00003184 continue;
3185 }
3186 goto DoneWithTypeQuals;
Dawn Perchik52fc3142010-09-03 01:29:35 +00003187 case tok::kw___pascal:
3188 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00003189 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik52fc3142010-09-03 01:29:35 +00003190 continue;
3191 }
3192 goto DoneWithTypeQuals;
Reid Spencer5f016e22007-07-11 17:01:13 +00003193 case tok::kw___attribute:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003194 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00003195 ParseGNUAttributes(DS.getAttributes());
Chris Lattner5a69d1c2008-12-18 07:02:59 +00003196 continue; // do *not* consume the next token!
3197 }
3198 // otherwise, FALL THROUGH!
3199 default:
Steve Naroff239f0732008-12-25 14:16:32 +00003200 DoneWithTypeQuals:
Chris Lattner5a69d1c2008-12-18 07:02:59 +00003201 // If this is not a type-qualifier token, we're done reading type
3202 // qualifiers. First verify that DeclSpec's are consistent.
Douglas Gregor9b3064b2009-04-01 22:41:11 +00003203 DS.Finish(Diags, PP);
Abramo Bagnara796aa442011-03-12 11:17:06 +00003204 if (EndLoc.isValid())
3205 DS.SetRangeEnd(EndLoc);
Chris Lattner5a69d1c2008-12-18 07:02:59 +00003206 return;
Reid Spencer5f016e22007-07-11 17:01:13 +00003207 }
Chris Lattnera1fcbad2008-12-18 06:50:14 +00003208
Reid Spencer5f016e22007-07-11 17:01:13 +00003209 // If the specifier combination wasn't legal, issue a diagnostic.
3210 if (isInvalid) {
3211 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner1ab3b962008-11-18 07:48:38 +00003212 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00003213 }
Abramo Bagnara796aa442011-03-12 11:17:06 +00003214 EndLoc = ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00003215 }
3216}
3217
3218
3219/// ParseDeclarator - Parse and verify a newly-initialized declarator.
3220///
3221void Parser::ParseDeclarator(Declarator &D) {
3222 /// This implements the 'declarator' production in the C grammar, then checks
3223 /// for well-formedness and issues diagnostics.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003224 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Reid Spencer5f016e22007-07-11 17:01:13 +00003225}
3226
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003227/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
3228/// is parsed by the function passed to it. Pass null, and the direct-declarator
3229/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003230/// ptr-operator production.
3231///
Sebastian Redlf30208a2009-01-24 21:16:55 +00003232/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
3233/// [C] pointer[opt] direct-declarator
3234/// [C++] direct-declarator
3235/// [C++] ptr-operator declarator
Reid Spencer5f016e22007-07-11 17:01:13 +00003236///
3237/// pointer: [C99 6.7.5]
3238/// '*' type-qualifier-list[opt]
3239/// '*' type-qualifier-list[opt] pointer
3240///
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003241/// ptr-operator:
3242/// '*' cv-qualifier-seq[opt]
3243/// '&'
Sebastian Redl05532f22009-03-15 22:02:01 +00003244/// [C++0x] '&&'
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003245/// [GNU] '&' restrict[opt] attributes[opt]
Sebastian Redl05532f22009-03-15 22:02:01 +00003246/// [GNU?] '&&' restrict[opt] attributes[opt]
Sebastian Redlf30208a2009-01-24 21:16:55 +00003247/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003248void Parser::ParseDeclaratorInternal(Declarator &D,
3249 DirectDeclParseFunction DirectDeclParser) {
Douglas Gregor91a28862009-08-26 14:27:30 +00003250 if (Diags.hasAllExtensionsSilenced())
3251 D.setExtension();
Douglas Gregor2ccccb32010-08-23 18:23:48 +00003252
Sebastian Redlf30208a2009-01-24 21:16:55 +00003253 // C++ member pointers start with a '::' or a nested-name.
3254 // Member pointers get special handling, since there's no place for the
3255 // scope spec in the generic path below.
Chris Lattnerf919bfe2009-03-24 17:04:48 +00003256 if (getLang().CPlusPlus &&
3257 (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) ||
3258 Tok.is(tok::annot_cxxscope))) {
Sebastian Redlf30208a2009-01-24 21:16:55 +00003259 CXXScopeSpec SS;
John McCallb3d87482010-08-24 05:47:05 +00003260 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), true); // ignore fail
John McCall9ba61662010-02-26 08:45:28 +00003261
Jeffrey Yasskinedc28772010-04-07 23:29:58 +00003262 if (SS.isNotEmpty()) {
Mike Stump1eb44332009-09-09 15:08:12 +00003263 if (Tok.isNot(tok::star)) {
Sebastian Redlf30208a2009-01-24 21:16:55 +00003264 // The scope spec really belongs to the direct-declarator.
3265 D.getCXXScopeSpec() = SS;
3266 if (DirectDeclParser)
3267 (this->*DirectDeclParser)(D);
3268 return;
3269 }
3270
3271 SourceLocation Loc = ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00003272 D.SetRangeEnd(Loc);
John McCall0b7e6782011-03-24 11:26:52 +00003273 DeclSpec DS(AttrFactory);
Sebastian Redlf30208a2009-01-24 21:16:55 +00003274 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00003275 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00003276
3277 // Recurse to parse whatever is left.
3278 ParseDeclaratorInternal(D, DirectDeclParser);
3279
3280 // Sema will have to catch (syntactically invalid) pointers into global
3281 // scope. It has to catch pointers into namespace scope anyway.
3282 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
John McCall0b7e6782011-03-24 11:26:52 +00003283 Loc),
3284 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003285 /* Don't replace range end. */SourceLocation());
Sebastian Redlf30208a2009-01-24 21:16:55 +00003286 return;
3287 }
3288 }
3289
3290 tok::TokenKind Kind = Tok.getKind();
Steve Naroff5618bd42008-08-27 16:04:49 +00003291 // Not a pointer, C++ reference, or block.
Chris Lattner9af55002009-03-27 04:18:06 +00003292 if (Kind != tok::star && Kind != tok::caret &&
Chris Lattnerf919bfe2009-03-24 17:04:48 +00003293 (Kind != tok::amp || !getLang().CPlusPlus) &&
Sebastian Redl743de1f2009-03-23 00:00:23 +00003294 // We parse rvalue refs in C++03, because otherwise the errors are scary.
Chris Lattner9af55002009-03-27 04:18:06 +00003295 (Kind != tok::ampamp || !getLang().CPlusPlus)) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003296 if (DirectDeclParser)
3297 (this->*DirectDeclParser)(D);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003298 return;
3299 }
Sebastian Redlf30208a2009-01-24 21:16:55 +00003300
Sebastian Redl05532f22009-03-15 22:02:01 +00003301 // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference,
3302 // '&&' -> rvalue reference
Sebastian Redl743de1f2009-03-23 00:00:23 +00003303 SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&.
Sebastian Redlab197ba2009-02-09 18:23:29 +00003304 D.SetRangeEnd(Loc);
Reid Spencer5f016e22007-07-11 17:01:13 +00003305
Chris Lattner9af55002009-03-27 04:18:06 +00003306 if (Kind == tok::star || Kind == tok::caret) {
Chris Lattner76549142008-02-21 01:32:26 +00003307 // Is a pointer.
John McCall0b7e6782011-03-24 11:26:52 +00003308 DeclSpec DS(AttrFactory);
Sebastian Redlf30208a2009-01-24 21:16:55 +00003309
Reid Spencer5f016e22007-07-11 17:01:13 +00003310 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00003311 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00003312
Reid Spencer5f016e22007-07-11 17:01:13 +00003313 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003314 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroff5618bd42008-08-27 16:04:49 +00003315 if (Kind == tok::star)
3316 // Remember that we parsed a pointer type, and remember the type-quals.
3317 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
Chandler Carruthd067c072011-02-23 18:51:59 +00003318 DS.getConstSpecLoc(),
3319 DS.getVolatileSpecLoc(),
John McCall0b7e6782011-03-24 11:26:52 +00003320 DS.getRestrictSpecLoc()),
3321 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003322 SourceLocation());
Steve Naroff5618bd42008-08-27 16:04:49 +00003323 else
3324 // Remember that we parsed a Block type, and remember the type-quals.
Mike Stump1eb44332009-09-09 15:08:12 +00003325 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
John McCall0b7e6782011-03-24 11:26:52 +00003326 Loc),
3327 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003328 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00003329 } else {
3330 // Is a reference
John McCall0b7e6782011-03-24 11:26:52 +00003331 DeclSpec DS(AttrFactory);
Reid Spencer5f016e22007-07-11 17:01:13 +00003332
Sebastian Redl743de1f2009-03-23 00:00:23 +00003333 // Complain about rvalue references in C++03, but then go on and build
3334 // the declarator.
3335 if (Kind == tok::ampamp && !getLang().CPlusPlus0x)
Douglas Gregor16cf8f52011-01-25 02:17:32 +00003336 Diag(Loc, diag::ext_rvalue_reference);
Sebastian Redl743de1f2009-03-23 00:00:23 +00003337
Reid Spencer5f016e22007-07-11 17:01:13 +00003338 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
3339 // cv-qualifiers are introduced through the use of a typedef or of a
3340 // template type argument, in which case the cv-qualifiers are ignored.
3341 //
3342 // [GNU] Retricted references are allowed.
3343 // [GNU] Attributes on references are allowed.
Sean Huntbbd37c62009-11-21 08:43:09 +00003344 // [C++0x] Attributes on references are not allowed.
3345 ParseTypeQualifierListOpt(DS, true, false);
Sebastian Redlab197ba2009-02-09 18:23:29 +00003346 D.ExtendWithDeclSpec(DS);
Reid Spencer5f016e22007-07-11 17:01:13 +00003347
3348 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
3349 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
3350 Diag(DS.getConstSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00003351 diag::err_invalid_reference_qualifier_application) << "const";
Reid Spencer5f016e22007-07-11 17:01:13 +00003352 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
3353 Diag(DS.getVolatileSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00003354 diag::err_invalid_reference_qualifier_application) << "volatile";
Reid Spencer5f016e22007-07-11 17:01:13 +00003355 }
3356
3357 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003358 ParseDeclaratorInternal(D, DirectDeclParser);
Reid Spencer5f016e22007-07-11 17:01:13 +00003359
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00003360 if (D.getNumTypeObjects() > 0) {
3361 // C++ [dcl.ref]p4: There shall be no references to references.
3362 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
3363 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattnerda83bac2008-11-19 07:37:42 +00003364 if (const IdentifierInfo *II = D.getIdentifier())
3365 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
3366 << II;
3367 else
3368 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
3369 << "type name";
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00003370
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003371 // Once we've complained about the reference-to-reference, we
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00003372 // can go ahead and build the (technically ill-formed)
3373 // declarator: reference collapsing will take care of it.
3374 }
3375 }
3376
Reid Spencer5f016e22007-07-11 17:01:13 +00003377 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner76549142008-02-21 01:32:26 +00003378 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
Sebastian Redl05532f22009-03-15 22:02:01 +00003379 Kind == tok::amp),
John McCall0b7e6782011-03-24 11:26:52 +00003380 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003381 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00003382 }
3383}
3384
3385/// ParseDirectDeclarator
3386/// direct-declarator: [C99 6.7.5]
Douglas Gregor42a552f2008-11-05 20:51:48 +00003387/// [C99] identifier
Reid Spencer5f016e22007-07-11 17:01:13 +00003388/// '(' declarator ')'
3389/// [GNU] '(' attributes declarator ')'
3390/// [C90] direct-declarator '[' constant-expression[opt] ']'
3391/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
3392/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
3393/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
3394/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
3395/// direct-declarator '(' parameter-type-list ')'
3396/// direct-declarator '(' identifier-list[opt] ')'
3397/// [GNU] direct-declarator '(' parameter-forward-declarations
3398/// parameter-type-list[opt] ')'
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003399/// [C++] direct-declarator '(' parameter-declaration-clause ')'
3400/// cv-qualifier-seq[opt] exception-specification[opt]
Douglas Gregorb48fe382008-10-31 09:07:45 +00003401/// [C++] declarator-id
Douglas Gregor42a552f2008-11-05 20:51:48 +00003402///
3403/// declarator-id: [C++ 8]
Douglas Gregora8bc8c92010-12-23 22:44:42 +00003404/// '...'[opt] id-expression
Douglas Gregor42a552f2008-11-05 20:51:48 +00003405/// '::'[opt] nested-name-specifier[opt] type-name
3406///
3407/// id-expression: [C++ 5.1]
3408/// unqualified-id
Douglas Gregordb422df2009-09-25 21:45:23 +00003409/// qualified-id
Douglas Gregor42a552f2008-11-05 20:51:48 +00003410///
3411/// unqualified-id: [C++ 5.1]
Mike Stump1eb44332009-09-09 15:08:12 +00003412/// identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003413/// operator-function-id
Douglas Gregordb422df2009-09-25 21:45:23 +00003414/// conversion-function-id
Mike Stump1eb44332009-09-09 15:08:12 +00003415/// '~' class-name
Douglas Gregor39a8de12009-02-25 19:37:18 +00003416/// template-id
Argyrios Kyrtzidisc7ed9c62008-11-07 22:02:30 +00003417///
Reid Spencer5f016e22007-07-11 17:01:13 +00003418void Parser::ParseDirectDeclarator(Declarator &D) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00003419 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003420
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003421 if (getLang().CPlusPlus && D.mayHaveIdentifier()) {
3422 // ParseDeclaratorInternal might already have parsed the scope.
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00003423 if (D.getCXXScopeSpec().isEmpty()) {
John McCallb3d87482010-08-24 05:47:05 +00003424 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec(), ParsedType(), true);
John McCall9ba61662010-02-26 08:45:28 +00003425 }
3426
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00003427 if (D.getCXXScopeSpec().isValid()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00003428 if (Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
John McCalle7e278b2009-12-11 20:04:54 +00003429 // Change the declaration context for name lookup, until this function
3430 // is exited (and the declarator has been parsed).
3431 DeclScopeObj.EnterDeclaratorScope();
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00003432 }
3433
Douglas Gregora8bc8c92010-12-23 22:44:42 +00003434 // C++0x [dcl.fct]p14:
3435 // There is a syntactic ambiguity when an ellipsis occurs at the end
3436 // of a parameter-declaration-clause without a preceding comma. In
3437 // this case, the ellipsis is parsed as part of the
3438 // abstract-declarator if the type of the parameter names a template
3439 // parameter pack that has not been expanded; otherwise, it is parsed
3440 // as part of the parameter-declaration-clause.
3441 if (Tok.is(tok::ellipsis) &&
3442 !((D.getContext() == Declarator::PrototypeContext ||
3443 D.getContext() == Declarator::BlockLiteralContext) &&
Douglas Gregora8bc8c92010-12-23 22:44:42 +00003444 NextToken().is(tok::r_paren) &&
3445 !Actions.containsUnexpandedParameterPacks(D)))
3446 D.setEllipsisLoc(ConsumeToken());
3447
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003448 if (Tok.is(tok::identifier) || Tok.is(tok::kw_operator) ||
3449 Tok.is(tok::annot_template_id) || Tok.is(tok::tilde)) {
3450 // We found something that indicates the start of an unqualified-id.
3451 // Parse that unqualified-id.
John McCallba9d8532010-04-13 06:39:49 +00003452 bool AllowConstructorName;
3453 if (D.getDeclSpec().hasTypeSpecifier())
3454 AllowConstructorName = false;
3455 else if (D.getCXXScopeSpec().isSet())
3456 AllowConstructorName =
3457 (D.getContext() == Declarator::FileContext ||
3458 (D.getContext() == Declarator::MemberContext &&
3459 D.getDeclSpec().isFriendSpecified()));
3460 else
3461 AllowConstructorName = (D.getContext() == Declarator::MemberContext);
3462
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003463 if (ParseUnqualifiedId(D.getCXXScopeSpec(),
3464 /*EnteringContext=*/true,
3465 /*AllowDestructorName=*/true,
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003466 AllowConstructorName,
John McCallb3d87482010-08-24 05:47:05 +00003467 ParsedType(),
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00003468 D.getName()) ||
3469 // Once we're past the identifier, if the scope was bad, mark the
3470 // whole declarator bad.
3471 D.getCXXScopeSpec().isInvalid()) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00003472 D.SetIdentifier(0, Tok.getLocation());
3473 D.setInvalidType(true);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003474 } else {
3475 // Parsed the unqualified-id; update range information and move along.
3476 if (D.getSourceRange().getBegin().isInvalid())
3477 D.SetRangeBegin(D.getName().getSourceRange().getBegin());
3478 D.SetRangeEnd(D.getName().getSourceRange().getEnd());
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003479 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003480 goto PastIdentifier;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00003481 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003482 } else if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00003483 assert(!getLang().CPlusPlus &&
3484 "There's a C++-specific check for tok::identifier above");
3485 assert(Tok.getIdentifierInfo() && "Not an identifier?");
3486 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
3487 ConsumeToken();
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003488 goto PastIdentifier;
3489 }
3490
3491 if (Tok.is(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003492 // direct-declarator: '(' declarator ')'
3493 // direct-declarator: '(' attributes declarator ')'
3494 // Example: 'char (*X)' or 'int (*XX)(void)'
3495 ParseParenDeclarator(D);
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003496
3497 // If the declarator was parenthesized, we entered the declarator
3498 // scope when parsing the parenthesized declarator, then exited
3499 // the scope already. Re-enter the scope, if we need to.
3500 if (D.getCXXScopeSpec().isSet()) {
Fariborz Jahanian46877cd2010-08-17 23:50:37 +00003501 // If there was an error parsing parenthesized declarator, declarator
3502 // scope may have been enterred before. Don't do it again.
3503 if (!D.isInvalidType() &&
3504 Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003505 // Change the declaration context for name lookup, until this function
3506 // is exited (and the declarator has been parsed).
Fariborz Jahanian46877cd2010-08-17 23:50:37 +00003507 DeclScopeObj.EnterDeclaratorScope();
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003508 }
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00003509 } else if (D.mayOmitIdentifier()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003510 // This could be something simple like "int" (in which case the declarator
3511 // portion is empty), if an abstract-declarator is allowed.
3512 D.SetIdentifier(0, Tok.getLocation());
3513 } else {
Douglas Gregore950d4b2009-03-06 23:28:18 +00003514 if (D.getContext() == Declarator::MemberContext)
3515 Diag(Tok, diag::err_expected_member_name_or_semi)
3516 << D.getDeclSpec().getSourceRange();
3517 else if (getLang().CPlusPlus)
Douglas Gregor2d1c2142009-11-03 19:44:04 +00003518 Diag(Tok, diag::err_expected_unqualified_id) << getLang().CPlusPlus;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003519 else
Chris Lattner1ab3b962008-11-18 07:48:38 +00003520 Diag(Tok, diag::err_expected_ident_lparen);
Reid Spencer5f016e22007-07-11 17:01:13 +00003521 D.SetIdentifier(0, Tok.getLocation());
Chris Lattner1f6f54b2008-11-11 06:13:16 +00003522 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00003523 }
Mike Stump1eb44332009-09-09 15:08:12 +00003524
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00003525 PastIdentifier:
Reid Spencer5f016e22007-07-11 17:01:13 +00003526 assert(D.isPastIdentifier() &&
3527 "Haven't past the location of the identifier yet?");
Mike Stump1eb44332009-09-09 15:08:12 +00003528
Sean Huntbbd37c62009-11-21 08:43:09 +00003529 // Don't parse attributes unless we have an identifier.
John McCall7f040a92010-12-24 02:08:15 +00003530 if (D.getIdentifier())
3531 MaybeParseCXX0XAttributes(D);
Sean Huntbbd37c62009-11-21 08:43:09 +00003532
Reid Spencer5f016e22007-07-11 17:01:13 +00003533 while (1) {
Chris Lattner04d66662007-10-09 17:33:22 +00003534 if (Tok.is(tok::l_paren)) {
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00003535 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
3536 // In such a case, check if we actually have a function declarator; if it
3537 // is not, the declarator has been fully parsed.
Chris Lattner7399ee02008-10-20 02:05:46 +00003538 if (getLang().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
3539 // When not in file scope, warn for ambiguous function declarators, just
3540 // in case the author intended it as a variable definition.
3541 bool warnIfAmbiguous = D.getContext() != Declarator::FileContext;
3542 if (!isCXXFunctionDeclarator(warnIfAmbiguous))
3543 break;
3544 }
John McCall0b7e6782011-03-24 11:26:52 +00003545 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00003546 ParseFunctionDeclarator(ConsumeParen(), D, attrs);
Chris Lattner04d66662007-10-09 17:33:22 +00003547 } else if (Tok.is(tok::l_square)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003548 ParseBracketDeclarator(D);
3549 } else {
3550 break;
3551 }
3552 }
3553}
3554
Chris Lattneref4715c2008-04-06 05:45:57 +00003555/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
3556/// only called before the identifier, so these are most likely just grouping
Mike Stump1eb44332009-09-09 15:08:12 +00003557/// parens for precedence. If we find that these are actually function
Chris Lattneref4715c2008-04-06 05:45:57 +00003558/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
3559///
3560/// direct-declarator:
3561/// '(' declarator ')'
3562/// [GNU] '(' attributes declarator ')'
Chris Lattner7399ee02008-10-20 02:05:46 +00003563/// direct-declarator '(' parameter-type-list ')'
3564/// direct-declarator '(' identifier-list[opt] ')'
3565/// [GNU] direct-declarator '(' parameter-forward-declarations
3566/// parameter-type-list[opt] ')'
Chris Lattneref4715c2008-04-06 05:45:57 +00003567///
3568void Parser::ParseParenDeclarator(Declarator &D) {
3569 SourceLocation StartLoc = ConsumeParen();
3570 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
Mike Stump1eb44332009-09-09 15:08:12 +00003571
Chris Lattner7399ee02008-10-20 02:05:46 +00003572 // Eat any attributes before we look at whether this is a grouping or function
3573 // declarator paren. If this is a grouping paren, the attribute applies to
3574 // the type being built up, for example:
3575 // int (__attribute__(()) *x)(long y)
3576 // If this ends up not being a grouping paren, the attribute applies to the
3577 // first argument, for example:
3578 // int (__attribute__(()) int x)
3579 // In either case, we need to eat any attributes to be able to determine what
3580 // sort of paren this is.
3581 //
John McCall0b7e6782011-03-24 11:26:52 +00003582 ParsedAttributes attrs(AttrFactory);
Chris Lattner7399ee02008-10-20 02:05:46 +00003583 bool RequiresArg = false;
3584 if (Tok.is(tok::kw___attribute)) {
John McCall7f040a92010-12-24 02:08:15 +00003585 ParseGNUAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00003586
Chris Lattner7399ee02008-10-20 02:05:46 +00003587 // We require that the argument list (if this is a non-grouping paren) be
3588 // present even if the attribute list was empty.
3589 RequiresArg = true;
3590 }
Steve Naroff239f0732008-12-25 14:16:32 +00003591 // Eat any Microsoft extensions.
Eli Friedman290eeb02009-06-08 23:27:34 +00003592 if (Tok.is(tok::kw___cdecl) || Tok.is(tok::kw___stdcall) ||
Douglas Gregorf813a2c2010-05-18 16:57:00 +00003593 Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___fastcall) ||
3594 Tok.is(tok::kw___w64) || Tok.is(tok::kw___ptr64)) {
John McCall7f040a92010-12-24 02:08:15 +00003595 ParseMicrosoftTypeAttributes(attrs);
Eli Friedman290eeb02009-06-08 23:27:34 +00003596 }
Dawn Perchik52fc3142010-09-03 01:29:35 +00003597 // Eat any Borland extensions.
Ted Kremenek8113ecf2010-11-10 05:59:39 +00003598 if (Tok.is(tok::kw___pascal))
John McCall7f040a92010-12-24 02:08:15 +00003599 ParseBorlandTypeAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00003600
Chris Lattneref4715c2008-04-06 05:45:57 +00003601 // If we haven't past the identifier yet (or where the identifier would be
3602 // stored, if this is an abstract declarator), then this is probably just
3603 // grouping parens. However, if this could be an abstract-declarator, then
3604 // this could also be the start of function arguments (consider 'void()').
3605 bool isGrouping;
Mike Stump1eb44332009-09-09 15:08:12 +00003606
Chris Lattneref4715c2008-04-06 05:45:57 +00003607 if (!D.mayOmitIdentifier()) {
3608 // If this can't be an abstract-declarator, this *must* be a grouping
3609 // paren, because we haven't seen the identifier yet.
3610 isGrouping = true;
3611 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Argyrios Kyrtzidise25d2702008-10-06 00:07:55 +00003612 (getLang().CPlusPlus && Tok.is(tok::ellipsis)) || // C++ int(...)
Chris Lattneref4715c2008-04-06 05:45:57 +00003613 isDeclarationSpecifier()) { // 'int(int)' is a function.
3614 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
3615 // considered to be a type, not a K&R identifier-list.
3616 isGrouping = false;
3617 } else {
3618 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
3619 isGrouping = true;
3620 }
Mike Stump1eb44332009-09-09 15:08:12 +00003621
Chris Lattneref4715c2008-04-06 05:45:57 +00003622 // If this is a grouping paren, handle:
3623 // direct-declarator: '(' declarator ')'
3624 // direct-declarator: '(' attributes declarator ')'
3625 if (isGrouping) {
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00003626 bool hadGroupingParens = D.hasGroupingParens();
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00003627 D.setGroupingParens(true);
3628
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003629 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattneref4715c2008-04-06 05:45:57 +00003630 // Match the ')'.
Abramo Bagnara075f8f12010-12-10 16:29:40 +00003631 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_paren, StartLoc);
John McCall0b7e6782011-03-24 11:26:52 +00003632 D.AddTypeInfo(DeclaratorChunk::getParen(StartLoc, EndLoc),
3633 attrs, EndLoc);
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00003634
3635 D.setGroupingParens(hadGroupingParens);
Chris Lattneref4715c2008-04-06 05:45:57 +00003636 return;
3637 }
Mike Stump1eb44332009-09-09 15:08:12 +00003638
Chris Lattneref4715c2008-04-06 05:45:57 +00003639 // Okay, if this wasn't a grouping paren, it must be the start of a function
3640 // argument list. Recognize that this declarator will never have an
Chris Lattner7399ee02008-10-20 02:05:46 +00003641 // identifier (and remember where it would have been), then call into
3642 // ParseFunctionDeclarator to handle of argument list.
Chris Lattneref4715c2008-04-06 05:45:57 +00003643 D.SetIdentifier(0, Tok.getLocation());
3644
John McCall7f040a92010-12-24 02:08:15 +00003645 ParseFunctionDeclarator(StartLoc, D, attrs, RequiresArg);
Chris Lattneref4715c2008-04-06 05:45:57 +00003646}
3647
3648/// ParseFunctionDeclarator - We are after the identifier and have parsed the
3649/// declarator D up to a paren, which indicates that we are parsing function
3650/// arguments.
Reid Spencer5f016e22007-07-11 17:01:13 +00003651///
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00003652/// If attrs is non-null, then the caller parsed those arguments immediately
Chris Lattner7399ee02008-10-20 02:05:46 +00003653/// after the open paren - they should be considered to be the first argument of
3654/// a parameter. If RequiresArg is true, then the first argument of the
3655/// function is required to be present and required to not be an identifier
3656/// list.
3657///
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00003658/// For C++, after the parameter-list, it also parses cv-qualifier-seq[opt],
3659/// (C++0x) ref-qualifier[opt], exception-specification[opt], and
3660/// (C++0x) trailing-return-type[opt].
3661///
3662/// [C++0x] exception-specification:
3663/// dynamic-exception-specification
3664/// noexcept-specification
3665///
3666void Parser::ParseFunctionDeclarator(SourceLocation LParenLoc, Declarator &D,
3667 ParsedAttributes &attrs,
3668 bool RequiresArg) {
3669 // lparen is already consumed!
3670 assert(D.isPastIdentifier() && "Should not call before identifier!");
3671
3672 // This should be true when the function has typed arguments.
3673 // Otherwise, it is treated as a K&R-style function.
3674 bool HasProto = false;
3675 // Build up an array of information about the parsed arguments.
3676 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
3677 // Remember where we see an ellipsis, if any.
3678 SourceLocation EllipsisLoc;
3679
3680 DeclSpec DS(AttrFactory);
3681 bool RefQualifierIsLValueRef = true;
3682 SourceLocation RefQualifierLoc;
3683 ExceptionSpecificationType ESpecType = EST_None;
3684 SourceRange ESpecRange;
3685 llvm::SmallVector<ParsedType, 2> DynamicExceptions;
3686 llvm::SmallVector<SourceRange, 2> DynamicExceptionRanges;
3687 ExprResult NoexceptExpr;
3688 ParsedType TrailingReturnType;
3689
3690 SourceLocation EndLoc;
3691
3692 if (isFunctionDeclaratorIdentifierList()) {
3693 if (RequiresArg)
3694 Diag(Tok, diag::err_argument_required_after_attribute);
3695
3696 ParseFunctionDeclaratorIdentifierList(D, ParamInfo);
3697
3698 EndLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
3699 } else {
3700 // Enter function-declaration scope, limiting any declarators to the
3701 // function prototype scope, including parameter declarators.
3702 ParseScope PrototypeScope(this,
3703 Scope::FunctionPrototypeScope|Scope::DeclScope);
3704
3705 if (Tok.isNot(tok::r_paren))
3706 ParseParameterDeclarationClause(D, attrs, ParamInfo, EllipsisLoc);
3707 else if (RequiresArg)
3708 Diag(Tok, diag::err_argument_required_after_attribute);
3709
3710 HasProto = ParamInfo.size() || getLang().CPlusPlus;
3711
3712 // If we have the closing ')', eat it.
3713 EndLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
3714
3715 if (getLang().CPlusPlus) {
3716 MaybeParseCXX0XAttributes(attrs);
3717
3718 // Parse cv-qualifier-seq[opt].
3719 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
3720 if (!DS.getSourceRange().getEnd().isInvalid())
3721 EndLoc = DS.getSourceRange().getEnd();
3722
3723 // Parse ref-qualifier[opt].
3724 if (Tok.is(tok::amp) || Tok.is(tok::ampamp)) {
3725 if (!getLang().CPlusPlus0x)
3726 Diag(Tok, diag::ext_ref_qualifier);
3727
3728 RefQualifierIsLValueRef = Tok.is(tok::amp);
3729 RefQualifierLoc = ConsumeToken();
3730 EndLoc = RefQualifierLoc;
3731 }
3732
3733 // Parse exception-specification[opt].
3734 ESpecType = MaybeParseExceptionSpecification(ESpecRange,
3735 DynamicExceptions,
3736 DynamicExceptionRanges,
3737 NoexceptExpr);
3738 if (ESpecType != EST_None)
3739 EndLoc = ESpecRange.getEnd();
3740
3741 // Parse trailing-return-type[opt].
3742 if (getLang().CPlusPlus0x && Tok.is(tok::arrow)) {
3743 TrailingReturnType = ParseTrailingReturnType().get();
3744 }
3745 }
3746
3747 // Leave prototype scope.
3748 PrototypeScope.Exit();
3749 }
3750
3751 // Remember that we parsed a function type, and remember the attributes.
3752 D.AddTypeInfo(DeclaratorChunk::getFunction(HasProto,
3753 /*isVariadic=*/EllipsisLoc.isValid(),
3754 EllipsisLoc,
3755 ParamInfo.data(), ParamInfo.size(),
3756 DS.getTypeQualifiers(),
3757 RefQualifierIsLValueRef,
3758 RefQualifierLoc,
3759 ESpecType, ESpecRange.getBegin(),
3760 DynamicExceptions.data(),
3761 DynamicExceptionRanges.data(),
3762 DynamicExceptions.size(),
3763 NoexceptExpr.isUsable() ?
3764 NoexceptExpr.get() : 0,
3765 LParenLoc, EndLoc, D,
3766 TrailingReturnType),
3767 attrs, EndLoc);
3768}
3769
3770/// isFunctionDeclaratorIdentifierList - This parameter list may have an
3771/// identifier list form for a K&R-style function: void foo(a,b,c)
3772///
3773/// Note that identifier-lists are only allowed for normal declarators, not for
3774/// abstract-declarators.
3775bool Parser::isFunctionDeclaratorIdentifierList() {
3776 return !getLang().CPlusPlus
3777 && Tok.is(tok::identifier)
3778 && !TryAltiVecVectorToken()
3779 // K&R identifier lists can't have typedefs as identifiers, per C99
3780 // 6.7.5.3p11.
3781 && (TryAnnotateTypeOrScopeToken() || !Tok.is(tok::annot_typename))
3782 // Identifier lists follow a really simple grammar: the identifiers can
3783 // be followed *only* by a ", identifier" or ")". However, K&R
3784 // identifier lists are really rare in the brave new modern world, and
3785 // it is very common for someone to typo a type in a non-K&R style
3786 // list. If we are presented with something like: "void foo(intptr x,
3787 // float y)", we don't want to start parsing the function declarator as
3788 // though it is a K&R style declarator just because intptr is an
3789 // invalid type.
3790 //
3791 // To handle this, we check to see if the token after the first
3792 // identifier is a "," or ")". Only then do we parse it as an
3793 // identifier list.
3794 && (NextToken().is(tok::comma) || NextToken().is(tok::r_paren));
3795}
3796
3797/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
3798/// we found a K&R-style identifier list instead of a typed parameter list.
3799///
3800/// After returning, ParamInfo will hold the parsed parameters.
3801///
3802/// identifier-list: [C99 6.7.5]
3803/// identifier
3804/// identifier-list ',' identifier
3805///
3806void Parser::ParseFunctionDeclaratorIdentifierList(
3807 Declarator &D,
3808 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> &ParamInfo) {
3809 // If there was no identifier specified for the declarator, either we are in
3810 // an abstract-declarator, or we are in a parameter declarator which was found
3811 // to be abstract. In abstract-declarators, identifier lists are not valid:
3812 // diagnose this.
3813 if (!D.getIdentifier())
3814 Diag(Tok, diag::ext_ident_list_in_param);
3815
3816 // Maintain an efficient lookup of params we have seen so far.
3817 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
3818
3819 while (1) {
3820 // If this isn't an identifier, report the error and skip until ')'.
3821 if (Tok.isNot(tok::identifier)) {
3822 Diag(Tok, diag::err_expected_ident);
3823 SkipUntil(tok::r_paren, /*StopAtSemi=*/true, /*DontConsume=*/true);
3824 // Forget we parsed anything.
3825 ParamInfo.clear();
3826 return;
3827 }
3828
3829 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
3830
3831 // Reject 'typedef int y; int test(x, y)', but continue parsing.
3832 if (Actions.getTypeName(*ParmII, Tok.getLocation(), getCurScope()))
3833 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
3834
3835 // Verify that the argument identifier has not already been mentioned.
3836 if (!ParamsSoFar.insert(ParmII)) {
3837 Diag(Tok, diag::err_param_redefinition) << ParmII;
3838 } else {
3839 // Remember this identifier in ParamInfo.
3840 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
3841 Tok.getLocation(),
3842 0));
3843 }
3844
3845 // Eat the identifier.
3846 ConsumeToken();
3847
3848 // The list continues if we see a comma.
3849 if (Tok.isNot(tok::comma))
3850 break;
3851 ConsumeToken();
3852 }
3853}
3854
3855/// ParseParameterDeclarationClause - Parse a (possibly empty) parameter-list
3856/// after the opening parenthesis. This function will not parse a K&R-style
3857/// identifier list.
3858///
3859/// D is the declarator being parsed. If attrs is non-null, then the caller
3860/// parsed those arguments immediately after the open paren - they should be
3861/// considered to be the first argument of a parameter.
3862///
3863/// After returning, ParamInfo will hold the parsed parameters. EllipsisLoc will
3864/// be the location of the ellipsis, if any was parsed.
3865///
Reid Spencer5f016e22007-07-11 17:01:13 +00003866/// parameter-type-list: [C99 6.7.5]
3867/// parameter-list
3868/// parameter-list ',' '...'
Douglas Gregored5d6512009-09-22 21:41:40 +00003869/// [C++] parameter-list '...'
Reid Spencer5f016e22007-07-11 17:01:13 +00003870///
3871/// parameter-list: [C99 6.7.5]
3872/// parameter-declaration
3873/// parameter-list ',' parameter-declaration
3874///
3875/// parameter-declaration: [C99 6.7.5]
3876/// declaration-specifiers declarator
Chris Lattner04421082008-04-08 04:40:51 +00003877/// [C++] declaration-specifiers declarator '=' assignment-expression
Reid Spencer5f016e22007-07-11 17:01:13 +00003878/// [GNU] declaration-specifiers declarator attributes
Sebastian Redl50de12f2009-03-24 22:27:57 +00003879/// declaration-specifiers abstract-declarator[opt]
3880/// [C++] declaration-specifiers abstract-declarator[opt]
Chris Lattner8123a952008-04-10 02:22:51 +00003881/// '=' assignment-expression
Reid Spencer5f016e22007-07-11 17:01:13 +00003882/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
3883///
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00003884void Parser::ParseParameterDeclarationClause(
3885 Declarator &D,
3886 ParsedAttributes &attrs,
3887 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> &ParamInfo,
3888 SourceLocation &EllipsisLoc) {
Mike Stump1eb44332009-09-09 15:08:12 +00003889
Chris Lattnerf97409f2008-04-06 06:57:35 +00003890 while (1) {
3891 if (Tok.is(tok::ellipsis)) {
Douglas Gregor965acbb2009-02-18 07:07:28 +00003892 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chris Lattnerf97409f2008-04-06 06:57:35 +00003893 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00003894 }
Mike Stump1eb44332009-09-09 15:08:12 +00003895
Chris Lattnerf97409f2008-04-06 06:57:35 +00003896 // Parse the declaration-specifiers.
John McCall54abf7d2009-11-04 02:18:39 +00003897 // Just use the ParsingDeclaration "scope" of the declarator.
John McCall0b7e6782011-03-24 11:26:52 +00003898 DeclSpec DS(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00003899
3900 // Skip any Microsoft attributes before a param.
3901 if (getLang().Microsoft && Tok.is(tok::l_square))
3902 ParseMicrosoftAttributes(DS.getAttributes());
3903
3904 SourceLocation DSStart = Tok.getLocation();
Chris Lattner7399ee02008-10-20 02:05:46 +00003905
3906 // If the caller parsed attributes for the first argument, add them now.
John McCall7f040a92010-12-24 02:08:15 +00003907 // Take them so that we only apply the attributes to the first parameter.
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00003908 // FIXME: If we saw an ellipsis first, this code is not reached. Are the
3909 // attributes lost? Should they even be allowed?
3910 // FIXME: If we can leave the attributes in the token stream somehow, we can
3911 // get rid of a parameter (attrs) and this statement. It might be too much
3912 // hassle.
John McCall7f040a92010-12-24 02:08:15 +00003913 DS.takeAttributesFrom(attrs);
3914
Chris Lattnere64c5492009-02-27 18:38:20 +00003915 ParseDeclarationSpecifiers(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00003916
Chris Lattnerf97409f2008-04-06 06:57:35 +00003917 // Parse the declarator. This is "PrototypeContext", because we must
3918 // accept either 'declarator' or 'abstract-declarator' here.
3919 Declarator ParmDecl(DS, Declarator::PrototypeContext);
3920 ParseDeclarator(ParmDecl);
3921
3922 // Parse GNU attributes, if present.
John McCall7f040a92010-12-24 02:08:15 +00003923 MaybeParseGNUAttributes(ParmDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00003924
Chris Lattnerf97409f2008-04-06 06:57:35 +00003925 // Remember this parsed parameter in ParamInfo.
3926 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
Mike Stump1eb44332009-09-09 15:08:12 +00003927
Douglas Gregor72b505b2008-12-16 21:30:33 +00003928 // DefArgToks is used when the parsing of default arguments needs
3929 // to be delayed.
3930 CachedTokens *DefArgToks = 0;
3931
Chris Lattnerf97409f2008-04-06 06:57:35 +00003932 // If no parameter was specified, verify that *something* was specified,
3933 // otherwise we have a missing type and identifier.
Chris Lattnere64c5492009-02-27 18:38:20 +00003934 if (DS.isEmpty() && ParmDecl.getIdentifier() == 0 &&
3935 ParmDecl.getNumTypeObjects() == 0) {
Chris Lattnerf97409f2008-04-06 06:57:35 +00003936 // Completely missing, emit error.
3937 Diag(DSStart, diag::err_missing_param);
3938 } else {
3939 // Otherwise, we have something. Add it and let semantic analysis try
3940 // to grok it and add the result to the ParamInfo we are building.
Mike Stump1eb44332009-09-09 15:08:12 +00003941
Chris Lattnerf97409f2008-04-06 06:57:35 +00003942 // Inform the actions module about the parameter declarator, so it gets
3943 // added to the current scope.
John McCalld226f652010-08-21 09:40:31 +00003944 Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl);
Chris Lattner04421082008-04-08 04:40:51 +00003945
3946 // Parse the default argument, if any. We parse the default
3947 // arguments in all dialects; the semantic analysis in
3948 // ActOnParamDefaultArgument will reject the default argument in
3949 // C.
3950 if (Tok.is(tok::equal)) {
Douglas Gregor61366e92008-12-24 00:01:03 +00003951 SourceLocation EqualLoc = Tok.getLocation();
3952
Chris Lattner04421082008-04-08 04:40:51 +00003953 // Parse the default argument
Douglas Gregor72b505b2008-12-16 21:30:33 +00003954 if (D.getContext() == Declarator::MemberContext) {
3955 // If we're inside a class definition, cache the tokens
3956 // corresponding to the default argument. We'll actually parse
3957 // them when we see the end of the class definition.
3958 // FIXME: Templates will require something similar.
3959 // FIXME: Can we use a smart pointer for Toks?
3960 DefArgToks = new CachedTokens;
3961
Mike Stump1eb44332009-09-09 15:08:12 +00003962 if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks,
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +00003963 /*StopAtSemi=*/true,
3964 /*ConsumeFinalToken=*/false)) {
Douglas Gregor72b505b2008-12-16 21:30:33 +00003965 delete DefArgToks;
3966 DefArgToks = 0;
Douglas Gregor61366e92008-12-24 00:01:03 +00003967 Actions.ActOnParamDefaultArgumentError(Param);
Argyrios Kyrtzidis2b602ad2010-08-06 09:47:24 +00003968 } else {
3969 // Mark the end of the default argument so that we know when to
3970 // stop when we parse it later on.
3971 Token DefArgEnd;
3972 DefArgEnd.startToken();
3973 DefArgEnd.setKind(tok::cxx_defaultarg_end);
3974 DefArgEnd.setLocation(Tok.getLocation());
3975 DefArgToks->push_back(DefArgEnd);
Mike Stump1eb44332009-09-09 15:08:12 +00003976 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc,
Anders Carlsson5e300d12009-06-12 16:51:40 +00003977 (*DefArgToks)[1].getLocation());
Argyrios Kyrtzidis2b602ad2010-08-06 09:47:24 +00003978 }
Chris Lattner04421082008-04-08 04:40:51 +00003979 } else {
Douglas Gregor72b505b2008-12-16 21:30:33 +00003980 // Consume the '='.
Douglas Gregor61366e92008-12-24 00:01:03 +00003981 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00003982
Douglas Gregorbe0f7bd2010-09-11 20:24:53 +00003983 // The argument isn't actually potentially evaluated unless it is
3984 // used.
3985 EnterExpressionEvaluationContext Eval(Actions,
3986 Sema::PotentiallyEvaluatedIfUsed);
3987
John McCall60d7b3a2010-08-24 06:29:42 +00003988 ExprResult DefArgResult(ParseAssignmentExpression());
Douglas Gregor72b505b2008-12-16 21:30:33 +00003989 if (DefArgResult.isInvalid()) {
3990 Actions.ActOnParamDefaultArgumentError(Param);
3991 SkipUntil(tok::comma, tok::r_paren, true, true);
3992 } else {
3993 // Inform the actions module about the default argument
3994 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
John McCall9ae2f072010-08-23 23:25:46 +00003995 DefArgResult.take());
Douglas Gregor72b505b2008-12-16 21:30:33 +00003996 }
Chris Lattner04421082008-04-08 04:40:51 +00003997 }
3998 }
Mike Stump1eb44332009-09-09 15:08:12 +00003999
4000 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
4001 ParmDecl.getIdentifierLoc(), Param,
Douglas Gregor72b505b2008-12-16 21:30:33 +00004002 DefArgToks));
Chris Lattnerf97409f2008-04-06 06:57:35 +00004003 }
4004
4005 // If the next token is a comma, consume it and keep reading arguments.
Douglas Gregored5d6512009-09-22 21:41:40 +00004006 if (Tok.isNot(tok::comma)) {
4007 if (Tok.is(tok::ellipsis)) {
Douglas Gregored5d6512009-09-22 21:41:40 +00004008 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
4009
4010 if (!getLang().CPlusPlus) {
4011 // We have ellipsis without a preceding ',', which is ill-formed
4012 // in C. Complain and provide the fix.
4013 Diag(EllipsisLoc, diag::err_missing_comma_before_ellipsis)
Douglas Gregor849b2432010-03-31 17:46:05 +00004014 << FixItHint::CreateInsertion(EllipsisLoc, ", ");
Douglas Gregored5d6512009-09-22 21:41:40 +00004015 }
4016 }
4017
4018 break;
4019 }
Mike Stump1eb44332009-09-09 15:08:12 +00004020
Chris Lattnerf97409f2008-04-06 06:57:35 +00004021 // Consume the comma.
4022 ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00004023 }
Mike Stump1eb44332009-09-09 15:08:12 +00004024
Chris Lattner66d28652008-04-06 06:34:08 +00004025}
Chris Lattneref4715c2008-04-06 05:45:57 +00004026
Reid Spencer5f016e22007-07-11 17:01:13 +00004027/// [C90] direct-declarator '[' constant-expression[opt] ']'
4028/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
4029/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
4030/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
4031/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
4032void Parser::ParseBracketDeclarator(Declarator &D) {
4033 SourceLocation StartLoc = ConsumeBracket();
Mike Stump1eb44332009-09-09 15:08:12 +00004034
Chris Lattner378c7e42008-12-18 07:27:21 +00004035 // C array syntax has many features, but by-far the most common is [] and [4].
4036 // This code does a fast path to handle some of the most obvious cases.
4037 if (Tok.getKind() == tok::r_square) {
Sebastian Redlab197ba2009-02-09 18:23:29 +00004038 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
John McCall0b7e6782011-03-24 11:26:52 +00004039 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00004040 MaybeParseCXX0XAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00004041
Chris Lattner378c7e42008-12-18 07:27:21 +00004042 // Remember that we parsed the empty array type.
John McCall60d7b3a2010-08-24 06:29:42 +00004043 ExprResult NumElements;
John McCall0b7e6782011-03-24 11:26:52 +00004044 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00004045 StartLoc, EndLoc),
John McCall0b7e6782011-03-24 11:26:52 +00004046 attrs, EndLoc);
Chris Lattner378c7e42008-12-18 07:27:21 +00004047 return;
4048 } else if (Tok.getKind() == tok::numeric_constant &&
4049 GetLookAheadToken(1).is(tok::r_square)) {
4050 // [4] is very common. Parse the numeric constant expression.
John McCall60d7b3a2010-08-24 06:29:42 +00004051 ExprResult ExprRes(Actions.ActOnNumericConstant(Tok));
Chris Lattner378c7e42008-12-18 07:27:21 +00004052 ConsumeToken();
4053
Sebastian Redlab197ba2009-02-09 18:23:29 +00004054 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
John McCall0b7e6782011-03-24 11:26:52 +00004055 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00004056 MaybeParseCXX0XAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00004057
Chris Lattner378c7e42008-12-18 07:27:21 +00004058 // Remember that we parsed a array type, and remember its features.
John McCall0b7e6782011-03-24 11:26:52 +00004059 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, 0,
John McCall7f040a92010-12-24 02:08:15 +00004060 ExprRes.release(),
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00004061 StartLoc, EndLoc),
John McCall0b7e6782011-03-24 11:26:52 +00004062 attrs, EndLoc);
Chris Lattner378c7e42008-12-18 07:27:21 +00004063 return;
4064 }
Mike Stump1eb44332009-09-09 15:08:12 +00004065
Reid Spencer5f016e22007-07-11 17:01:13 +00004066 // If valid, this location is the position where we read the 'static' keyword.
4067 SourceLocation StaticLoc;
Chris Lattner04d66662007-10-09 17:33:22 +00004068 if (Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00004069 StaticLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00004070
Reid Spencer5f016e22007-07-11 17:01:13 +00004071 // If there is a type-qualifier-list, read it now.
Chris Lattnera1fcbad2008-12-18 06:50:14 +00004072 // Type qualifiers in an array subscript are a C99 feature.
John McCall0b7e6782011-03-24 11:26:52 +00004073 DeclSpec DS(AttrFactory);
Chris Lattner5a69d1c2008-12-18 07:02:59 +00004074 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Mike Stump1eb44332009-09-09 15:08:12 +00004075
Reid Spencer5f016e22007-07-11 17:01:13 +00004076 // If we haven't already read 'static', check to see if there is one after the
4077 // type-qualifier-list.
Chris Lattner04d66662007-10-09 17:33:22 +00004078 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00004079 StaticLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00004080
Reid Spencer5f016e22007-07-11 17:01:13 +00004081 // Handle "direct-declarator [ type-qual-list[opt] * ]".
4082 bool isStar = false;
John McCall60d7b3a2010-08-24 06:29:42 +00004083 ExprResult NumElements;
Mike Stump1eb44332009-09-09 15:08:12 +00004084
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00004085 // Handle the case where we have '[*]' as the array size. However, a leading
4086 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
4087 // the the token after the star is a ']'. Since stars in arrays are
4088 // infrequent, use of lookahead is not costly here.
4089 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattnera711dd02008-04-06 05:27:21 +00004090 ConsumeToken(); // Eat the '*'.
Reid Spencer5f016e22007-07-11 17:01:13 +00004091
Chris Lattnera1fcbad2008-12-18 06:50:14 +00004092 if (StaticLoc.isValid()) {
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00004093 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattnera1fcbad2008-12-18 06:50:14 +00004094 StaticLoc = SourceLocation(); // Drop the static.
4095 }
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00004096 isStar = true;
Chris Lattner04d66662007-10-09 17:33:22 +00004097 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner378c7e42008-12-18 07:27:21 +00004098 // Note, in C89, this production uses the constant-expr production instead
4099 // of assignment-expr. The only difference is that assignment-expr allows
4100 // things like '=' and '*='. Sema rejects these in C89 mode because they
4101 // are not i-c-e's, so we don't need to distinguish between the two here.
Mike Stump1eb44332009-09-09 15:08:12 +00004102
Douglas Gregore0762c92009-06-19 23:52:42 +00004103 // Parse the constant-expression or assignment-expression now (depending
4104 // on dialect).
4105 if (getLang().CPlusPlus)
4106 NumElements = ParseConstantExpression();
4107 else
4108 NumElements = ParseAssignmentExpression();
Reid Spencer5f016e22007-07-11 17:01:13 +00004109 }
Mike Stump1eb44332009-09-09 15:08:12 +00004110
Reid Spencer5f016e22007-07-11 17:01:13 +00004111 // If there was an error parsing the assignment-expression, recover.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00004112 if (NumElements.isInvalid()) {
Chris Lattner5cb10d32009-04-24 22:30:50 +00004113 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00004114 // If the expression was invalid, skip it.
4115 SkipUntil(tok::r_square);
4116 return;
4117 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00004118
4119 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
4120
John McCall0b7e6782011-03-24 11:26:52 +00004121 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00004122 MaybeParseCXX0XAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00004123
Chris Lattner378c7e42008-12-18 07:27:21 +00004124 // Remember that we parsed a array type, and remember its features.
John McCall0b7e6782011-03-24 11:26:52 +00004125 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
Reid Spencer5f016e22007-07-11 17:01:13 +00004126 StaticLoc.isValid(), isStar,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00004127 NumElements.release(),
4128 StartLoc, EndLoc),
John McCall0b7e6782011-03-24 11:26:52 +00004129 attrs, EndLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00004130}
4131
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004132/// [GNU] typeof-specifier:
4133/// typeof ( expressions )
4134/// typeof ( type-name )
4135/// [GNU/C++] typeof unary-expression
Steve Naroffd1861fd2007-07-31 12:34:36 +00004136///
4137void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner04d66662007-10-09 17:33:22 +00004138 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004139 Token OpTok = Tok;
Steve Naroffd1861fd2007-07-31 12:34:36 +00004140 SourceLocation StartLoc = ConsumeToken();
4141
John McCallcfb708c2010-01-13 20:03:27 +00004142 const bool hasParens = Tok.is(tok::l_paren);
4143
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004144 bool isCastExpr;
John McCallb3d87482010-08-24 05:47:05 +00004145 ParsedType CastTy;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004146 SourceRange CastRange;
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00004147 ExprResult Operand = ParseExprAfterUnaryExprOrTypeTrait(OpTok, isCastExpr,
4148 CastTy, CastRange);
John McCallcfb708c2010-01-13 20:03:27 +00004149 if (hasParens)
4150 DS.setTypeofParensRange(CastRange);
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004151
4152 if (CastRange.getEnd().isInvalid())
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004153 // FIXME: Not accurate, the range gets one token more than it should.
4154 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004155 else
4156 DS.SetRangeEnd(CastRange.getEnd());
Mike Stump1eb44332009-09-09 15:08:12 +00004157
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004158 if (isCastExpr) {
4159 if (!CastTy) {
4160 DS.SetTypeSpecError();
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004161 return;
Douglas Gregor809070a2009-02-18 17:45:20 +00004162 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004163
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004164 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00004165 unsigned DiagID;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004166 // Check for duplicate type specifiers (e.g. "int typeof(int)").
4167 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00004168 DiagID, CastTy))
4169 Diag(StartLoc, DiagID) << PrevSpec;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004170 return;
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004171 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004172
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004173 // If we get here, the operand to the typeof was an expresion.
4174 if (Operand.isInvalid()) {
4175 DS.SetTypeSpecError();
Steve Naroff9dfa7b42007-08-02 02:53:48 +00004176 return;
Steve Naroffd1861fd2007-07-31 12:34:36 +00004177 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004178
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004179 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00004180 unsigned DiagID;
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004181 // Check for duplicate type specifiers (e.g. "int typeof(int)").
4182 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
John McCallb3d87482010-08-24 05:47:05 +00004183 DiagID, Operand.get()))
John McCallfec54012009-08-03 20:12:06 +00004184 Diag(StartLoc, DiagID) << PrevSpec;
Steve Naroffd1861fd2007-07-31 12:34:36 +00004185}
Chris Lattner1b492422010-02-28 18:33:55 +00004186
4187
4188/// TryAltiVecVectorTokenOutOfLine - Out of line body that should only be called
4189/// from TryAltiVecVectorToken.
4190bool Parser::TryAltiVecVectorTokenOutOfLine() {
4191 Token Next = NextToken();
4192 switch (Next.getKind()) {
4193 default: return false;
4194 case tok::kw_short:
4195 case tok::kw_long:
4196 case tok::kw_signed:
4197 case tok::kw_unsigned:
4198 case tok::kw_void:
4199 case tok::kw_char:
4200 case tok::kw_int:
4201 case tok::kw_float:
4202 case tok::kw_double:
4203 case tok::kw_bool:
4204 case tok::kw___pixel:
4205 Tok.setKind(tok::kw___vector);
4206 return true;
4207 case tok::identifier:
4208 if (Next.getIdentifierInfo() == Ident_pixel) {
4209 Tok.setKind(tok::kw___vector);
4210 return true;
4211 }
4212 return false;
4213 }
4214}
4215
4216bool Parser::TryAltiVecTokenOutOfLine(DeclSpec &DS, SourceLocation Loc,
4217 const char *&PrevSpec, unsigned &DiagID,
4218 bool &isInvalid) {
4219 if (Tok.getIdentifierInfo() == Ident_vector) {
4220 Token Next = NextToken();
4221 switch (Next.getKind()) {
4222 case tok::kw_short:
4223 case tok::kw_long:
4224 case tok::kw_signed:
4225 case tok::kw_unsigned:
4226 case tok::kw_void:
4227 case tok::kw_char:
4228 case tok::kw_int:
4229 case tok::kw_float:
4230 case tok::kw_double:
4231 case tok::kw_bool:
4232 case tok::kw___pixel:
4233 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
4234 return true;
4235 case tok::identifier:
4236 if (Next.getIdentifierInfo() == Ident_pixel) {
4237 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
4238 return true;
4239 }
4240 break;
4241 default:
4242 break;
4243 }
Douglas Gregora8f031f2010-06-16 15:28:57 +00004244 } else if ((Tok.getIdentifierInfo() == Ident_pixel) &&
Chris Lattner1b492422010-02-28 18:33:55 +00004245 DS.isTypeAltiVecVector()) {
4246 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
4247 return true;
4248 }
4249 return false;
4250}