blob: 2a95ecc7c7234d268e35f51f746056acc3cace43 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- ParseDecl.cpp - Declaration Parsing ------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Declaration portions of the Parser interfaces.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Parse/Parser.h"
Chris Lattner500d3292009-01-29 05:15:15 +000015#include "clang/Parse/ParseDiagnostic.h"
Peter Collingbourne207f4d82011-03-18 22:38:29 +000016#include "clang/Basic/OpenCL.h"
John McCall19510852010-08-20 18:27:03 +000017#include "clang/Sema/Scope.h"
18#include "clang/Sema/ParsedTemplate.h"
John McCallf312b1e2010-08-26 23:41:50 +000019#include "clang/Sema/PrettyDeclStackTrace.h"
Chris Lattnerd167ca02009-12-10 00:21:05 +000020#include "RAIIObjectsForParser.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000021#include "llvm/ADT/SmallSet.h"
Caitlin Sadowskib51e0312011-08-09 17:59:31 +000022#include "llvm/ADT/StringSwitch.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000023using namespace clang;
24
25//===----------------------------------------------------------------------===//
26// C99 6.7: Declarations.
27//===----------------------------------------------------------------------===//
28
29/// ParseTypeName
30/// type-name: [C99 6.7.6]
31/// specifier-qualifier-list abstract-declarator[opt]
Sebastian Redl4c5d3202008-11-21 19:14:01 +000032///
33/// Called type-id in C++.
Douglas Gregor683a81f2011-01-31 16:09:46 +000034TypeResult Parser::ParseTypeName(SourceRange *Range,
John McCallf85e1932011-06-15 23:02:42 +000035 Declarator::TheContext Context,
Richard Smithc89edf52011-07-01 19:46:12 +000036 ObjCDeclSpec *objcQuals,
37 AccessSpecifier AS,
38 Decl **OwnedType) {
Reid Spencer5f016e22007-07-11 17:01:13 +000039 // Parse the common declaration-specifiers piece.
John McCall0b7e6782011-03-24 11:26:52 +000040 DeclSpec DS(AttrFactory);
John McCallf85e1932011-06-15 23:02:42 +000041 DS.setObjCQualifiers(objcQuals);
Richard Smithc89edf52011-07-01 19:46:12 +000042 ParseSpecifierQualifierList(DS, AS);
43 if (OwnedType)
44 *OwnedType = DS.isTypeSpecOwned() ? DS.getRepAsDecl() : 0;
Sebastian Redlef65f062009-05-29 18:02:33 +000045
Reid Spencer5f016e22007-07-11 17:01:13 +000046 // Parse the abstract-declarator, if present.
Douglas Gregor683a81f2011-01-31 16:09:46 +000047 Declarator DeclaratorInfo(DS, Context);
Reid Spencer5f016e22007-07-11 17:01:13 +000048 ParseDeclarator(DeclaratorInfo);
Sebastian Redlef65f062009-05-29 18:02:33 +000049 if (Range)
50 *Range = DeclaratorInfo.getSourceRange();
51
Chris Lattnereaaebc72009-04-25 08:06:05 +000052 if (DeclaratorInfo.isInvalidType())
Douglas Gregor809070a2009-02-18 17:45:20 +000053 return true;
54
Douglas Gregor23c94db2010-07-02 17:43:08 +000055 return Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
Reid Spencer5f016e22007-07-11 17:01:13 +000056}
57
Sean Huntbbd37c62009-11-21 08:43:09 +000058/// ParseGNUAttributes - Parse a non-empty attributes list.
Reid Spencer5f016e22007-07-11 17:01:13 +000059///
60/// [GNU] attributes:
61/// attribute
62/// attributes attribute
63///
64/// [GNU] attribute:
65/// '__attribute__' '(' '(' attribute-list ')' ')'
66///
67/// [GNU] attribute-list:
68/// attrib
69/// attribute_list ',' attrib
70///
71/// [GNU] attrib:
72/// empty
73/// attrib-name
74/// attrib-name '(' identifier ')'
75/// attrib-name '(' identifier ',' nonempty-expr-list ')'
76/// attrib-name '(' argument-expression-list [C99 6.5.2] ')'
77///
78/// [GNU] attrib-name:
79/// identifier
80/// typespec
81/// typequal
82/// storageclass
Mike Stump1eb44332009-09-09 15:08:12 +000083///
Reid Spencer5f016e22007-07-11 17:01:13 +000084/// FIXME: The GCC grammar/code for this construct implies we need two
Mike Stump1eb44332009-09-09 15:08:12 +000085/// token lookahead. Comment from gcc: "If they start with an identifier
86/// which is followed by a comma or close parenthesis, then the arguments
Reid Spencer5f016e22007-07-11 17:01:13 +000087/// start with that identifier; otherwise they are an expression list."
88///
89/// At the moment, I am not doing 2 token lookahead. I am also unaware of
90/// any attributes that don't work (based on my limited testing). Most
91/// attributes are very simple in practice. Until we find a bug, I don't see
92/// a pressing need to implement the 2 token lookahead.
93
John McCall7f040a92010-12-24 02:08:15 +000094void Parser::ParseGNUAttributes(ParsedAttributes &attrs,
95 SourceLocation *endLoc) {
Sean Huntbbd37c62009-11-21 08:43:09 +000096 assert(Tok.is(tok::kw___attribute) && "Not a GNU attribute list!");
Mike Stump1eb44332009-09-09 15:08:12 +000097
Chris Lattner04d66662007-10-09 17:33:22 +000098 while (Tok.is(tok::kw___attribute)) {
Reid Spencer5f016e22007-07-11 17:01:13 +000099 ConsumeToken();
100 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
101 "attribute")) {
102 SkipUntil(tok::r_paren, true); // skip until ) or ;
John McCall7f040a92010-12-24 02:08:15 +0000103 return;
Reid Spencer5f016e22007-07-11 17:01:13 +0000104 }
105 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, "(")) {
106 SkipUntil(tok::r_paren, true); // skip until ) or ;
John McCall7f040a92010-12-24 02:08:15 +0000107 return;
Reid Spencer5f016e22007-07-11 17:01:13 +0000108 }
109 // Parse the attribute-list. e.g. __attribute__(( weak, alias("__f") ))
Chris Lattner04d66662007-10-09 17:33:22 +0000110 while (Tok.is(tok::identifier) || isDeclarationSpecifier() ||
111 Tok.is(tok::comma)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000112
113 if (Tok.is(tok::comma)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000114 // allows for empty/non-empty attributes. ((__vector_size__(16),,,,))
115 ConsumeToken();
116 continue;
117 }
118 // we have an identifier or declaration specifier (const, int, etc.)
119 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
120 SourceLocation AttrNameLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000121
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000122 // Availability attributes have their own grammar.
123 if (AttrName->isStr("availability"))
124 ParseAvailabilityAttribute(*AttrName, AttrNameLoc, attrs, endLoc);
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000125 // Thread safety attributes fit into the FIXME case above, so we
126 // just parse the arguments as a list of expressions
127 else if (IsThreadSafetyAttribute(AttrName->getName()))
128 ParseThreadSafetyAttribute(*AttrName, AttrNameLoc, attrs, endLoc);
Douglas Gregorec1afbf2010-03-16 19:09:18 +0000129 // check if we have a "parameterized" attribute
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000130 else if (Tok.is(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000131 ConsumeParen(); // ignore the left paren loc for now
Mike Stump1eb44332009-09-09 15:08:12 +0000132
Chris Lattner04d66662007-10-09 17:33:22 +0000133 if (Tok.is(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000134 IdentifierInfo *ParmName = Tok.getIdentifierInfo();
135 SourceLocation ParmLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000136
137 if (Tok.is(tok::r_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000138 // __attribute__(( mode(byte) ))
139 ConsumeParen(); // ignore the right paren loc for now
John McCall0b7e6782011-03-24 11:26:52 +0000140 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc,
141 ParmName, ParmLoc, 0, 0);
Chris Lattner04d66662007-10-09 17:33:22 +0000142 } else if (Tok.is(tok::comma)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000143 ConsumeToken();
144 // __attribute__(( format(printf, 1, 2) ))
Sebastian Redla55e52c2008-11-25 22:21:31 +0000145 ExprVector ArgExprs(Actions);
Reid Spencer5f016e22007-07-11 17:01:13 +0000146 bool ArgExprsOk = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000147
Reid Spencer5f016e22007-07-11 17:01:13 +0000148 // now parse the non-empty comma separated list of expressions
149 while (1) {
John McCall60d7b3a2010-08-24 06:29:42 +0000150 ExprResult ArgExpr(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000151 if (ArgExpr.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000152 ArgExprsOk = false;
153 SkipUntil(tok::r_paren);
154 break;
155 } else {
Sebastian Redleffa8d12008-12-10 00:02:53 +0000156 ArgExprs.push_back(ArgExpr.release());
Reid Spencer5f016e22007-07-11 17:01:13 +0000157 }
Chris Lattner04d66662007-10-09 17:33:22 +0000158 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +0000159 break;
160 ConsumeToken(); // Eat the comma, move to the next argument
161 }
Chris Lattner04d66662007-10-09 17:33:22 +0000162 if (ArgExprsOk && Tok.is(tok::r_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000163 ConsumeParen(); // ignore the right paren loc for now
John McCall0b7e6782011-03-24 11:26:52 +0000164 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc,
165 ParmName, ParmLoc, ArgExprs.take(), ArgExprs.size());
Reid Spencer5f016e22007-07-11 17:01:13 +0000166 }
167 }
168 } else { // not an identifier
Nate Begeman6f3d8382009-06-26 06:32:41 +0000169 switch (Tok.getKind()) {
170 case tok::r_paren:
Reid Spencer5f016e22007-07-11 17:01:13 +0000171 // parse a possibly empty comma separated list of expressions
Reid Spencer5f016e22007-07-11 17:01:13 +0000172 // __attribute__(( nonnull() ))
173 ConsumeParen(); // ignore the right paren loc for now
John McCall0b7e6782011-03-24 11:26:52 +0000174 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc,
175 0, SourceLocation(), 0, 0);
Nate Begeman6f3d8382009-06-26 06:32:41 +0000176 break;
177 case tok::kw_char:
178 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000179 case tok::kw_char16_t:
180 case tok::kw_char32_t:
Nate Begeman6f3d8382009-06-26 06:32:41 +0000181 case tok::kw_bool:
182 case tok::kw_short:
183 case tok::kw_int:
184 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +0000185 case tok::kw___int64:
Nate Begeman6f3d8382009-06-26 06:32:41 +0000186 case tok::kw_signed:
187 case tok::kw_unsigned:
188 case tok::kw_float:
189 case tok::kw_double:
190 case tok::kw_void:
John McCall7f040a92010-12-24 02:08:15 +0000191 case tok::kw_typeof: {
192 AttributeList *attr
John McCall0b7e6782011-03-24 11:26:52 +0000193 = attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc,
194 0, SourceLocation(), 0, 0);
John McCall7f040a92010-12-24 02:08:15 +0000195 if (attr->getKind() == AttributeList::AT_IBOutletCollection)
Fariborz Jahanian1b72fa72010-08-17 23:19:16 +0000196 Diag(Tok, diag::err_iboutletcollection_builtintype);
Nate Begeman6f3d8382009-06-26 06:32:41 +0000197 // If it's a builtin type name, eat it and expect a rparen
198 // __attribute__(( vec_type_hint(char) ))
199 ConsumeToken();
Nate Begeman6f3d8382009-06-26 06:32:41 +0000200 if (Tok.is(tok::r_paren))
201 ConsumeParen();
202 break;
John McCall7f040a92010-12-24 02:08:15 +0000203 }
Nate Begeman6f3d8382009-06-26 06:32:41 +0000204 default:
Reid Spencer5f016e22007-07-11 17:01:13 +0000205 // __attribute__(( aligned(16) ))
Sebastian Redla55e52c2008-11-25 22:21:31 +0000206 ExprVector ArgExprs(Actions);
Reid Spencer5f016e22007-07-11 17:01:13 +0000207 bool ArgExprsOk = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000208
Reid Spencer5f016e22007-07-11 17:01:13 +0000209 // now parse the list of expressions
210 while (1) {
John McCall60d7b3a2010-08-24 06:29:42 +0000211 ExprResult ArgExpr(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000212 if (ArgExpr.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000213 ArgExprsOk = false;
214 SkipUntil(tok::r_paren);
215 break;
216 } else {
Sebastian Redleffa8d12008-12-10 00:02:53 +0000217 ArgExprs.push_back(ArgExpr.release());
Reid Spencer5f016e22007-07-11 17:01:13 +0000218 }
Chris Lattner04d66662007-10-09 17:33:22 +0000219 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +0000220 break;
221 ConsumeToken(); // Eat the comma, move to the next argument
222 }
223 // Match the ')'.
Chris Lattner04d66662007-10-09 17:33:22 +0000224 if (ArgExprsOk && Tok.is(tok::r_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000225 ConsumeParen(); // ignore the right paren loc for now
John McCall0b7e6782011-03-24 11:26:52 +0000226 attrs.addNew(AttrName, AttrNameLoc, 0,
227 AttrNameLoc, 0, SourceLocation(),
228 ArgExprs.take(), ArgExprs.size());
Reid Spencer5f016e22007-07-11 17:01:13 +0000229 }
Nate Begeman6f3d8382009-06-26 06:32:41 +0000230 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000231 }
232 }
233 } else {
John McCall0b7e6782011-03-24 11:26:52 +0000234 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc,
235 0, SourceLocation(), 0, 0);
Reid Spencer5f016e22007-07-11 17:01:13 +0000236 }
237 }
238 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
Reid Spencer5f016e22007-07-11 17:01:13 +0000239 SkipUntil(tok::r_paren, false);
Sean Huntbbd37c62009-11-21 08:43:09 +0000240 SourceLocation Loc = Tok.getLocation();
Sebastian Redlab197ba2009-02-09 18:23:29 +0000241 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) {
242 SkipUntil(tok::r_paren, false);
243 }
John McCall7f040a92010-12-24 02:08:15 +0000244 if (endLoc)
245 *endLoc = Loc;
Reid Spencer5f016e22007-07-11 17:01:13 +0000246 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000247}
248
Eli Friedmana23b4852009-06-08 07:21:15 +0000249/// ParseMicrosoftDeclSpec - Parse an __declspec construct
250///
251/// [MS] decl-specifier:
252/// __declspec ( extended-decl-modifier-seq )
253///
254/// [MS] extended-decl-modifier-seq:
255/// extended-decl-modifier[opt]
256/// extended-decl-modifier extended-decl-modifier-seq
257
John McCall7f040a92010-12-24 02:08:15 +0000258void Parser::ParseMicrosoftDeclSpec(ParsedAttributes &attrs) {
Steve Narofff59e17e2008-12-24 20:59:21 +0000259 assert(Tok.is(tok::kw___declspec) && "Not a declspec!");
Eli Friedmana23b4852009-06-08 07:21:15 +0000260
Steve Narofff59e17e2008-12-24 20:59:21 +0000261 ConsumeToken();
Eli Friedmana23b4852009-06-08 07:21:15 +0000262 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
263 "declspec")) {
264 SkipUntil(tok::r_paren, true); // skip until ) or ;
John McCall7f040a92010-12-24 02:08:15 +0000265 return;
Eli Friedmana23b4852009-06-08 07:21:15 +0000266 }
Francois Pichet373197b2011-05-07 19:04:49 +0000267
Eli Friedman290eeb02009-06-08 23:27:34 +0000268 while (Tok.getIdentifierInfo()) {
Eli Friedmana23b4852009-06-08 07:21:15 +0000269 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
270 SourceLocation AttrNameLoc = ConsumeToken();
Francois Pichet373197b2011-05-07 19:04:49 +0000271
272 // FIXME: Remove this when we have proper __declspec(property()) support.
273 // Just skip everything inside property().
274 if (AttrName->getName() == "property") {
275 ConsumeParen();
276 SkipUntil(tok::r_paren);
277 }
Eli Friedmana23b4852009-06-08 07:21:15 +0000278 if (Tok.is(tok::l_paren)) {
279 ConsumeParen();
280 // FIXME: This doesn't parse __declspec(property(get=get_func_name))
281 // correctly.
John McCall60d7b3a2010-08-24 06:29:42 +0000282 ExprResult ArgExpr(ParseAssignmentExpression());
Eli Friedmana23b4852009-06-08 07:21:15 +0000283 if (!ArgExpr.isInvalid()) {
John McCallca0408f2010-08-23 06:44:23 +0000284 Expr *ExprList = ArgExpr.take();
John McCall0b7e6782011-03-24 11:26:52 +0000285 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
286 SourceLocation(), &ExprList, 1, true);
Eli Friedmana23b4852009-06-08 07:21:15 +0000287 }
288 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
289 SkipUntil(tok::r_paren, false);
290 } else {
John McCall0b7e6782011-03-24 11:26:52 +0000291 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc,
292 0, SourceLocation(), 0, 0, true);
Eli Friedmana23b4852009-06-08 07:21:15 +0000293 }
294 }
295 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
296 SkipUntil(tok::r_paren, false);
John McCall7f040a92010-12-24 02:08:15 +0000297 return;
Eli Friedman290eeb02009-06-08 23:27:34 +0000298}
299
John McCall7f040a92010-12-24 02:08:15 +0000300void Parser::ParseMicrosoftTypeAttributes(ParsedAttributes &attrs) {
Eli Friedman290eeb02009-06-08 23:27:34 +0000301 // Treat these like attributes
302 // FIXME: Allow Sema to distinguish between these and real attributes!
303 while (Tok.is(tok::kw___fastcall) || Tok.is(tok::kw___stdcall) ||
Douglas Gregorf813a2c2010-05-18 16:57:00 +0000304 Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___cdecl) ||
305 Tok.is(tok::kw___ptr64) || Tok.is(tok::kw___w64)) {
Eli Friedman290eeb02009-06-08 23:27:34 +0000306 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
307 SourceLocation AttrNameLoc = ConsumeToken();
308 if (Tok.is(tok::kw___ptr64) || Tok.is(tok::kw___w64))
309 // FIXME: Support these properly!
310 continue;
John McCall0b7e6782011-03-24 11:26:52 +0000311 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
312 SourceLocation(), 0, 0, true);
Eli Friedman290eeb02009-06-08 23:27:34 +0000313 }
Steve Narofff59e17e2008-12-24 20:59:21 +0000314}
315
John McCall7f040a92010-12-24 02:08:15 +0000316void Parser::ParseBorlandTypeAttributes(ParsedAttributes &attrs) {
Dawn Perchik52fc3142010-09-03 01:29:35 +0000317 // Treat these like attributes
318 while (Tok.is(tok::kw___pascal)) {
319 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
320 SourceLocation AttrNameLoc = ConsumeToken();
John McCall0b7e6782011-03-24 11:26:52 +0000321 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
322 SourceLocation(), 0, 0, true);
Dawn Perchik52fc3142010-09-03 01:29:35 +0000323 }
John McCall7f040a92010-12-24 02:08:15 +0000324}
325
Peter Collingbournef315fa82011-02-14 01:42:53 +0000326void Parser::ParseOpenCLAttributes(ParsedAttributes &attrs) {
327 // Treat these like attributes
328 while (Tok.is(tok::kw___kernel)) {
329 SourceLocation AttrNameLoc = ConsumeToken();
John McCall0b7e6782011-03-24 11:26:52 +0000330 attrs.addNew(PP.getIdentifierInfo("opencl_kernel_function"),
331 AttrNameLoc, 0, AttrNameLoc, 0,
332 SourceLocation(), 0, 0, false);
Peter Collingbournef315fa82011-02-14 01:42:53 +0000333 }
334}
335
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000336void Parser::ParseOpenCLQualifiers(DeclSpec &DS) {
337 SourceLocation Loc = Tok.getLocation();
338 switch(Tok.getKind()) {
339 // OpenCL qualifiers:
340 case tok::kw___private:
341 case tok::kw_private:
John McCall0b7e6782011-03-24 11:26:52 +0000342 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000343 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000344 PP.getIdentifierInfo("address_space"), Loc, 0);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000345 break;
346
347 case tok::kw___global:
John McCall0b7e6782011-03-24 11:26:52 +0000348 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000349 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000350 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_global);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000351 break;
352
353 case tok::kw___local:
John McCall0b7e6782011-03-24 11:26:52 +0000354 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000355 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000356 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_local);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000357 break;
358
359 case tok::kw___constant:
John McCall0b7e6782011-03-24 11:26:52 +0000360 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000361 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000362 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_constant);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000363 break;
364
365 case tok::kw___read_only:
John McCall0b7e6782011-03-24 11:26:52 +0000366 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000367 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000368 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_read_only);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000369 break;
370
371 case tok::kw___write_only:
John McCall0b7e6782011-03-24 11:26:52 +0000372 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000373 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000374 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_write_only);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000375 break;
376
377 case tok::kw___read_write:
John McCall0b7e6782011-03-24 11:26:52 +0000378 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000379 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000380 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_read_write);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000381 break;
382 default: break;
383 }
384}
385
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000386/// \brief Parse a version number.
387///
388/// version:
389/// simple-integer
390/// simple-integer ',' simple-integer
391/// simple-integer ',' simple-integer ',' simple-integer
392VersionTuple Parser::ParseVersionTuple(SourceRange &Range) {
393 Range = Tok.getLocation();
394
395 if (!Tok.is(tok::numeric_constant)) {
396 Diag(Tok, diag::err_expected_version);
397 SkipUntil(tok::comma, tok::r_paren, true, true, true);
398 return VersionTuple();
399 }
400
401 // Parse the major (and possibly minor and subminor) versions, which
402 // are stored in the numeric constant. We utilize a quirk of the
403 // lexer, which is that it handles something like 1.2.3 as a single
404 // numeric constant, rather than two separate tokens.
405 llvm::SmallString<512> Buffer;
406 Buffer.resize(Tok.getLength()+1);
407 const char *ThisTokBegin = &Buffer[0];
408
409 // Get the spelling of the token, which eliminates trigraphs, etc.
410 bool Invalid = false;
411 unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin, &Invalid);
412 if (Invalid)
413 return VersionTuple();
414
415 // Parse the major version.
416 unsigned AfterMajor = 0;
417 unsigned Major = 0;
418 while (AfterMajor < ActualLength && isdigit(ThisTokBegin[AfterMajor])) {
419 Major = Major * 10 + ThisTokBegin[AfterMajor] - '0';
420 ++AfterMajor;
421 }
422
423 if (AfterMajor == 0) {
424 Diag(Tok, diag::err_expected_version);
425 SkipUntil(tok::comma, tok::r_paren, true, true, true);
426 return VersionTuple();
427 }
428
429 if (AfterMajor == ActualLength) {
430 ConsumeToken();
431
432 // We only had a single version component.
433 if (Major == 0) {
434 Diag(Tok, diag::err_zero_version);
435 return VersionTuple();
436 }
437
438 return VersionTuple(Major);
439 }
440
441 if (ThisTokBegin[AfterMajor] != '.' || (AfterMajor + 1 == ActualLength)) {
442 Diag(Tok, diag::err_expected_version);
443 SkipUntil(tok::comma, tok::r_paren, true, true, true);
444 return VersionTuple();
445 }
446
447 // Parse the minor version.
448 unsigned AfterMinor = AfterMajor + 1;
449 unsigned Minor = 0;
450 while (AfterMinor < ActualLength && isdigit(ThisTokBegin[AfterMinor])) {
451 Minor = Minor * 10 + ThisTokBegin[AfterMinor] - '0';
452 ++AfterMinor;
453 }
454
455 if (AfterMinor == ActualLength) {
456 ConsumeToken();
457
458 // We had major.minor.
459 if (Major == 0 && Minor == 0) {
460 Diag(Tok, diag::err_zero_version);
461 return VersionTuple();
462 }
463
464 return VersionTuple(Major, Minor);
465 }
466
467 // If what follows is not a '.', we have a problem.
468 if (ThisTokBegin[AfterMinor] != '.') {
469 Diag(Tok, diag::err_expected_version);
470 SkipUntil(tok::comma, tok::r_paren, true, true, true);
471 return VersionTuple();
472 }
473
474 // Parse the subminor version.
475 unsigned AfterSubminor = AfterMinor + 1;
476 unsigned Subminor = 0;
477 while (AfterSubminor < ActualLength && isdigit(ThisTokBegin[AfterSubminor])) {
478 Subminor = Subminor * 10 + ThisTokBegin[AfterSubminor] - '0';
479 ++AfterSubminor;
480 }
481
482 if (AfterSubminor != ActualLength) {
483 Diag(Tok, diag::err_expected_version);
484 SkipUntil(tok::comma, tok::r_paren, true, true, true);
485 return VersionTuple();
486 }
487 ConsumeToken();
488 return VersionTuple(Major, Minor, Subminor);
489}
490
491/// \brief Parse the contents of the "availability" attribute.
492///
493/// availability-attribute:
494/// 'availability' '(' platform ',' version-arg-list ')'
495///
496/// platform:
497/// identifier
498///
499/// version-arg-list:
500/// version-arg
501/// version-arg ',' version-arg-list
502///
503/// version-arg:
504/// 'introduced' '=' version
505/// 'deprecated' '=' version
506/// 'removed' = version
Douglas Gregorb53e4172011-03-26 03:35:55 +0000507/// 'unavailable'
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000508void Parser::ParseAvailabilityAttribute(IdentifierInfo &Availability,
509 SourceLocation AvailabilityLoc,
510 ParsedAttributes &attrs,
511 SourceLocation *endLoc) {
512 SourceLocation PlatformLoc;
513 IdentifierInfo *Platform = 0;
514
515 enum { Introduced, Deprecated, Obsoleted, Unknown };
516 AvailabilityChange Changes[Unknown];
517
518 // Opening '('.
519 SourceLocation LParenLoc;
520 if (!Tok.is(tok::l_paren)) {
521 Diag(Tok, diag::err_expected_lparen);
522 return;
523 }
524 LParenLoc = ConsumeParen();
525
526 // Parse the platform name,
527 if (Tok.isNot(tok::identifier)) {
528 Diag(Tok, diag::err_availability_expected_platform);
529 SkipUntil(tok::r_paren);
530 return;
531 }
532 Platform = Tok.getIdentifierInfo();
533 PlatformLoc = ConsumeToken();
534
535 // Parse the ',' following the platform name.
536 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "", tok::r_paren))
537 return;
538
539 // If we haven't grabbed the pointers for the identifiers
540 // "introduced", "deprecated", and "obsoleted", do so now.
541 if (!Ident_introduced) {
542 Ident_introduced = PP.getIdentifierInfo("introduced");
543 Ident_deprecated = PP.getIdentifierInfo("deprecated");
544 Ident_obsoleted = PP.getIdentifierInfo("obsoleted");
Douglas Gregorb53e4172011-03-26 03:35:55 +0000545 Ident_unavailable = PP.getIdentifierInfo("unavailable");
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000546 }
547
548 // Parse the set of introductions/deprecations/removals.
Douglas Gregorb53e4172011-03-26 03:35:55 +0000549 SourceLocation UnavailableLoc;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000550 do {
551 if (Tok.isNot(tok::identifier)) {
552 Diag(Tok, diag::err_availability_expected_change);
553 SkipUntil(tok::r_paren);
554 return;
555 }
556 IdentifierInfo *Keyword = Tok.getIdentifierInfo();
557 SourceLocation KeywordLoc = ConsumeToken();
558
Douglas Gregorb53e4172011-03-26 03:35:55 +0000559 if (Keyword == Ident_unavailable) {
560 if (UnavailableLoc.isValid()) {
561 Diag(KeywordLoc, diag::err_availability_redundant)
562 << Keyword << SourceRange(UnavailableLoc);
563 }
564 UnavailableLoc = KeywordLoc;
565
566 if (Tok.isNot(tok::comma))
567 break;
568
569 ConsumeToken();
570 continue;
571 }
572
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000573 if (Tok.isNot(tok::equal)) {
574 Diag(Tok, diag::err_expected_equal_after)
575 << Keyword;
576 SkipUntil(tok::r_paren);
577 return;
578 }
579 ConsumeToken();
580
581 SourceRange VersionRange;
582 VersionTuple Version = ParseVersionTuple(VersionRange);
583
584 if (Version.empty()) {
585 SkipUntil(tok::r_paren);
586 return;
587 }
588
589 unsigned Index;
590 if (Keyword == Ident_introduced)
591 Index = Introduced;
592 else if (Keyword == Ident_deprecated)
593 Index = Deprecated;
594 else if (Keyword == Ident_obsoleted)
595 Index = Obsoleted;
596 else
597 Index = Unknown;
598
599 if (Index < Unknown) {
600 if (!Changes[Index].KeywordLoc.isInvalid()) {
601 Diag(KeywordLoc, diag::err_availability_redundant)
602 << Keyword
603 << SourceRange(Changes[Index].KeywordLoc,
604 Changes[Index].VersionRange.getEnd());
605 }
606
607 Changes[Index].KeywordLoc = KeywordLoc;
608 Changes[Index].Version = Version;
609 Changes[Index].VersionRange = VersionRange;
610 } else {
611 Diag(KeywordLoc, diag::err_availability_unknown_change)
612 << Keyword << VersionRange;
613 }
614
615 if (Tok.isNot(tok::comma))
616 break;
617
618 ConsumeToken();
619 } while (true);
620
621 // Closing ')'.
622 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
623 if (RParenLoc.isInvalid())
624 return;
625
626 if (endLoc)
627 *endLoc = RParenLoc;
628
Douglas Gregorb53e4172011-03-26 03:35:55 +0000629 // The 'unavailable' availability cannot be combined with any other
630 // availability changes. Make sure that hasn't happened.
631 if (UnavailableLoc.isValid()) {
632 bool Complained = false;
633 for (unsigned Index = Introduced; Index != Unknown; ++Index) {
634 if (Changes[Index].KeywordLoc.isValid()) {
635 if (!Complained) {
636 Diag(UnavailableLoc, diag::warn_availability_and_unavailable)
637 << SourceRange(Changes[Index].KeywordLoc,
638 Changes[Index].VersionRange.getEnd());
639 Complained = true;
640 }
641
642 // Clear out the availability.
643 Changes[Index] = AvailabilityChange();
644 }
645 }
646 }
647
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000648 // Record this attribute
Douglas Gregorb53e4172011-03-26 03:35:55 +0000649 attrs.addNew(&Availability, AvailabilityLoc,
John McCall0b7e6782011-03-24 11:26:52 +0000650 0, SourceLocation(),
651 Platform, PlatformLoc,
652 Changes[Introduced],
653 Changes[Deprecated],
Douglas Gregorb53e4172011-03-26 03:35:55 +0000654 Changes[Obsoleted],
655 UnavailableLoc, false, false);
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000656}
657
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000658/// \brief Wrapper around a case statement checking if AttrName is
659/// one of the thread safety attributes
660bool Parser::IsThreadSafetyAttribute(llvm::StringRef AttrName){
661 return llvm::StringSwitch<bool>(AttrName)
662 .Case("guarded_by", true)
663 .Case("guarded_var", true)
664 .Case("pt_guarded_by", true)
665 .Case("pt_guarded_var", true)
666 .Case("lockable", true)
667 .Case("scoped_lockable", true)
668 .Case("no_thread_safety_analysis", true)
669 .Case("acquired_after", true)
670 .Case("acquired_before", true)
671 .Case("exclusive_lock_function", true)
672 .Case("shared_lock_function", true)
673 .Case("exclusive_trylock_function", true)
674 .Case("shared_trylock_function", true)
675 .Case("unlock_function", true)
676 .Case("lock_returned", true)
677 .Case("locks_excluded", true)
678 .Case("exclusive_locks_required", true)
679 .Case("shared_locks_required", true)
680 .Default(false);
681}
682
683/// \brief Parse the contents of thread safety attributes. These
684/// should always be parsed as an expression list.
685///
686/// We need to special case the parsing due to the fact that if the first token
687/// of the first argument is an identifier, the main parse loop will store
688/// that token as a "parameter" and the rest of
689/// the arguments will be added to a list of "arguments". However,
690/// subsequent tokens in the first argument are lost. We instead parse each
691/// argument as an expression and add all arguments to the list of "arguments".
692/// In future, we will take advantage of this special case to also
693/// deal with some argument scoping issues here (for example, referring to a
694/// function parameter in the attribute on that function).
695void Parser::ParseThreadSafetyAttribute(IdentifierInfo &AttrName,
696 SourceLocation AttrNameLoc,
697 ParsedAttributes &Attrs,
698 SourceLocation *EndLoc) {
699
700 if (Tok.is(tok::l_paren)) {
701 SourceLocation LeftParenLoc = Tok.getLocation();
702 ConsumeParen(); // ignore the left paren loc for now
703
704 ExprVector ArgExprs(Actions);
705 bool ArgExprsOk = true;
706
707 // now parse the list of expressions
708 while (1) {
709 ExprResult ArgExpr(ParseAssignmentExpression());
710 if (ArgExpr.isInvalid()) {
711 ArgExprsOk = false;
712 MatchRHSPunctuation(tok::r_paren, LeftParenLoc);
713 break;
714 } else {
715 ArgExprs.push_back(ArgExpr.release());
716 }
717 if (Tok.isNot(tok::comma))
718 break;
719 ConsumeToken(); // Eat the comma, move to the next argument
720 }
721 // Match the ')'.
722 if (ArgExprsOk && Tok.is(tok::r_paren)) {
723 ConsumeParen(); // ignore the right paren loc for now
724 Attrs.addNew(&AttrName, AttrNameLoc, 0, AttrNameLoc, 0, SourceLocation(),
725 ArgExprs.take(), ArgExprs.size());
726 }
727 } else {
728 Attrs.addNew(&AttrName, AttrNameLoc, 0, AttrNameLoc,
729 0, SourceLocation(), 0, 0);
730 }
731}
732
John McCall7f040a92010-12-24 02:08:15 +0000733void Parser::DiagnoseProhibitedAttributes(ParsedAttributesWithRange &attrs) {
734 Diag(attrs.Range.getBegin(), diag::err_attributes_not_allowed)
735 << attrs.Range;
Dawn Perchik52fc3142010-09-03 01:29:35 +0000736}
737
Reid Spencer5f016e22007-07-11 17:01:13 +0000738/// ParseDeclaration - Parse a full 'declaration', which consists of
739/// declaration-specifiers, some number of declarators, and a semicolon.
Chris Lattner97144fc2009-04-02 04:16:50 +0000740/// 'Context' should be a Declarator::TheContext value. This returns the
741/// location of the semicolon in DeclEnd.
Chris Lattner8f08cb72007-08-25 06:57:03 +0000742///
743/// declaration: [C99 6.7]
744/// block-declaration ->
745/// simple-declaration
746/// others [FIXME]
Douglas Gregoradcac882008-12-01 23:54:00 +0000747/// [C++] template-declaration
Chris Lattner8f08cb72007-08-25 06:57:03 +0000748/// [C++] namespace-definition
Douglas Gregorf780abc2008-12-30 03:27:21 +0000749/// [C++] using-directive
Douglas Gregord7f37bf2009-06-22 23:06:13 +0000750/// [C++] using-declaration
Peter Collingbournec6eb44b2011-04-15 00:35:57 +0000751/// [C++0x/C1X] static_assert-declaration
Chris Lattner8f08cb72007-08-25 06:57:03 +0000752/// others... [FIXME]
753///
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +0000754Parser::DeclGroupPtrTy Parser::ParseDeclaration(StmtVector &Stmts,
755 unsigned Context,
Sean Huntbbd37c62009-11-21 08:43:09 +0000756 SourceLocation &DeclEnd,
John McCall7f040a92010-12-24 02:08:15 +0000757 ParsedAttributesWithRange &attrs) {
Argyrios Kyrtzidis36d36802010-06-17 10:52:18 +0000758 ParenBraceBracketBalancer BalancerRAIIObj(*this);
759
John McCalld226f652010-08-21 09:40:31 +0000760 Decl *SingleDecl = 0;
Richard Smithc89edf52011-07-01 19:46:12 +0000761 Decl *OwnedType = 0;
Chris Lattner8f08cb72007-08-25 06:57:03 +0000762 switch (Tok.getKind()) {
Douglas Gregoradcac882008-12-01 23:54:00 +0000763 case tok::kw_template:
Douglas Gregor1426e532009-05-12 21:31:51 +0000764 case tok::kw_export:
John McCall7f040a92010-12-24 02:08:15 +0000765 ProhibitAttributes(attrs);
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000766 SingleDecl = ParseDeclarationStartingWithTemplate(Context, DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +0000767 break;
Sebastian Redld078e642010-08-27 23:12:46 +0000768 case tok::kw_inline:
Sebastian Redl88e64ca2010-08-31 00:36:45 +0000769 // Could be the start of an inline namespace. Allowed as an ext in C++03.
770 if (getLang().CPlusPlus && NextToken().is(tok::kw_namespace)) {
John McCall7f040a92010-12-24 02:08:15 +0000771 ProhibitAttributes(attrs);
Sebastian Redld078e642010-08-27 23:12:46 +0000772 SourceLocation InlineLoc = ConsumeToken();
773 SingleDecl = ParseNamespace(Context, DeclEnd, InlineLoc);
774 break;
775 }
John McCall7f040a92010-12-24 02:08:15 +0000776 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs,
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +0000777 true);
Chris Lattner8f08cb72007-08-25 06:57:03 +0000778 case tok::kw_namespace:
John McCall7f040a92010-12-24 02:08:15 +0000779 ProhibitAttributes(attrs);
Chris Lattner97144fc2009-04-02 04:16:50 +0000780 SingleDecl = ParseNamespace(Context, DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +0000781 break;
Douglas Gregorf780abc2008-12-30 03:27:21 +0000782 case tok::kw_using:
John McCall78b81052010-11-10 02:40:36 +0000783 SingleDecl = ParseUsingDirectiveOrDeclaration(Context, ParsedTemplateInfo(),
Richard Smithc89edf52011-07-01 19:46:12 +0000784 DeclEnd, attrs, &OwnedType);
Chris Lattner682bf922009-03-29 16:50:03 +0000785 break;
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000786 case tok::kw_static_assert:
Peter Collingbournec6eb44b2011-04-15 00:35:57 +0000787 case tok::kw__Static_assert:
John McCall7f040a92010-12-24 02:08:15 +0000788 ProhibitAttributes(attrs);
Chris Lattner97144fc2009-04-02 04:16:50 +0000789 SingleDecl = ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +0000790 break;
Chris Lattner8f08cb72007-08-25 06:57:03 +0000791 default:
John McCall7f040a92010-12-24 02:08:15 +0000792 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs, true);
Chris Lattner8f08cb72007-08-25 06:57:03 +0000793 }
Sean Huntbbd37c62009-11-21 08:43:09 +0000794
Chris Lattner682bf922009-03-29 16:50:03 +0000795 // This routine returns a DeclGroup, if the thing we parsed only contains a
Richard Smithc89edf52011-07-01 19:46:12 +0000796 // single decl, convert it now. Alias declarations can also declare a type;
797 // include that too if it is present.
798 return Actions.ConvertDeclToDeclGroup(SingleDecl, OwnedType);
Chris Lattner8f08cb72007-08-25 06:57:03 +0000799}
800
801/// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl]
802/// declaration-specifiers init-declarator-list[opt] ';'
803///[C90/C++]init-declarator-list ';' [TODO]
804/// [OMP] threadprivate-directive [TODO]
Chris Lattnercd147752009-03-29 17:27:48 +0000805///
Richard Smithad762fc2011-04-14 22:09:26 +0000806/// for-range-declaration: [C++0x 6.5p1: stmt.ranged]
807/// attribute-specifier-seq[opt] type-specifier-seq declarator
808///
Chris Lattnercd147752009-03-29 17:27:48 +0000809/// If RequireSemi is false, this does not check for a ';' at the end of the
Chris Lattner5c5db552010-04-05 18:18:31 +0000810/// declaration. If it is true, it checks for and eats it.
Richard Smithad762fc2011-04-14 22:09:26 +0000811///
812/// If FRI is non-null, we might be parsing a for-range-declaration instead
813/// of a simple-declaration. If we find that we are, we also parse the
814/// for-range-initializer, and place it here.
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +0000815Parser::DeclGroupPtrTy Parser::ParseSimpleDeclaration(StmtVector &Stmts,
816 unsigned Context,
Sean Huntbbd37c62009-11-21 08:43:09 +0000817 SourceLocation &DeclEnd,
John McCall7f040a92010-12-24 02:08:15 +0000818 ParsedAttributes &attrs,
Richard Smithad762fc2011-04-14 22:09:26 +0000819 bool RequireSemi,
820 ForRangeInit *FRI) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000821 // Parse the common declaration-specifiers piece.
John McCall54abf7d2009-11-04 02:18:39 +0000822 ParsingDeclSpec DS(*this);
John McCall7f040a92010-12-24 02:08:15 +0000823 DS.takeAttributesFrom(attrs);
Douglas Gregor312eadb2011-04-24 05:37:28 +0000824
Douglas Gregor0efc2c12010-01-13 17:31:36 +0000825 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS_none,
Richard Smith34b41d92011-02-20 03:19:35 +0000826 getDeclSpecContextFromDeclaratorContext(Context));
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +0000827 StmtResult R = Actions.ActOnVlaStmt(DS);
828 if (R.isUsable())
829 Stmts.push_back(R.release());
Douglas Gregor312eadb2011-04-24 05:37:28 +0000830
Reid Spencer5f016e22007-07-11 17:01:13 +0000831 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
832 // declaration-specifiers init-declarator-list[opt] ';'
Chris Lattner04d66662007-10-09 17:33:22 +0000833 if (Tok.is(tok::semi)) {
Chris Lattner5c5db552010-04-05 18:18:31 +0000834 if (RequireSemi) ConsumeToken();
John McCalld226f652010-08-21 09:40:31 +0000835 Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none,
Douglas Gregor312eadb2011-04-24 05:37:28 +0000836 DS);
John McCall54abf7d2009-11-04 02:18:39 +0000837 DS.complete(TheDecl);
Chris Lattner682bf922009-03-29 16:50:03 +0000838 return Actions.ConvertDeclToDeclGroup(TheDecl);
Reid Spencer5f016e22007-07-11 17:01:13 +0000839 }
Douglas Gregor312eadb2011-04-24 05:37:28 +0000840
841 return ParseDeclGroup(DS, Context, /*FunctionDefs=*/ false, &DeclEnd, FRI);
John McCalld8ac0572009-11-03 19:26:08 +0000842}
Mike Stump1eb44332009-09-09 15:08:12 +0000843
John McCalld8ac0572009-11-03 19:26:08 +0000844/// ParseDeclGroup - Having concluded that this is either a function
845/// definition or a group of object declarations, actually parse the
846/// result.
John McCall54abf7d2009-11-04 02:18:39 +0000847Parser::DeclGroupPtrTy Parser::ParseDeclGroup(ParsingDeclSpec &DS,
848 unsigned Context,
John McCalld8ac0572009-11-03 19:26:08 +0000849 bool AllowFunctionDefinitions,
Richard Smithad762fc2011-04-14 22:09:26 +0000850 SourceLocation *DeclEnd,
851 ForRangeInit *FRI) {
John McCalld8ac0572009-11-03 19:26:08 +0000852 // Parse the first declarator.
John McCall54abf7d2009-11-04 02:18:39 +0000853 ParsingDeclarator D(*this, DS, static_cast<Declarator::TheContext>(Context));
John McCalld8ac0572009-11-03 19:26:08 +0000854 ParseDeclarator(D);
Chris Lattnercd147752009-03-29 17:27:48 +0000855
John McCalld8ac0572009-11-03 19:26:08 +0000856 // Bail out if the first declarator didn't seem well-formed.
857 if (!D.hasName() && !D.mayOmitIdentifier()) {
858 // Skip until ; or }.
859 SkipUntil(tok::r_brace, true, true);
860 if (Tok.is(tok::semi))
861 ConsumeToken();
862 return DeclGroupPtrTy();
Chris Lattner23c4b182009-03-29 17:18:04 +0000863 }
Mike Stump1eb44332009-09-09 15:08:12 +0000864
Chris Lattnerc82daef2010-07-11 22:24:20 +0000865 // Check to see if we have a function *definition* which must have a body.
866 if (AllowFunctionDefinitions && D.isFunctionDeclarator() &&
867 // Look at the next token to make sure that this isn't a function
868 // declaration. We have to check this because __attribute__ might be the
869 // start of a function definition in GCC-extended K&R C.
870 !isDeclarationAfterDeclarator()) {
871
Chris Lattner004659a2010-07-11 22:42:07 +0000872 if (isStartOfFunctionDefinition(D)) {
John McCalld8ac0572009-11-03 19:26:08 +0000873 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
874 Diag(Tok, diag::err_function_declared_typedef);
875
876 // Recover by treating the 'typedef' as spurious.
877 DS.ClearStorageClassSpecs();
878 }
879
John McCalld226f652010-08-21 09:40:31 +0000880 Decl *TheDecl = ParseFunctionDefinition(D);
John McCalld8ac0572009-11-03 19:26:08 +0000881 return Actions.ConvertDeclToDeclGroup(TheDecl);
Chris Lattner004659a2010-07-11 22:42:07 +0000882 }
883
884 if (isDeclarationSpecifier()) {
885 // If there is an invalid declaration specifier right after the function
886 // prototype, then we must be in a missing semicolon case where this isn't
887 // actually a body. Just fall through into the code that handles it as a
888 // prototype, and let the top-level code handle the erroneous declspec
889 // where it would otherwise expect a comma or semicolon.
John McCalld8ac0572009-11-03 19:26:08 +0000890 } else {
891 Diag(Tok, diag::err_expected_fn_body);
892 SkipUntil(tok::semi);
893 return DeclGroupPtrTy();
894 }
895 }
896
Richard Smithad762fc2011-04-14 22:09:26 +0000897 if (ParseAttributesAfterDeclarator(D))
898 return DeclGroupPtrTy();
899
900 // C++0x [stmt.iter]p1: Check if we have a for-range-declarator. If so, we
901 // must parse and analyze the for-range-initializer before the declaration is
902 // analyzed.
903 if (FRI && Tok.is(tok::colon)) {
904 FRI->ColonLoc = ConsumeToken();
Sebastian Redldbef1bb2011-06-05 12:23:16 +0000905 if (Tok.is(tok::l_brace))
906 FRI->RangeExpr = ParseBraceInitializer();
907 else
908 FRI->RangeExpr = ParseExpression();
Richard Smithad762fc2011-04-14 22:09:26 +0000909 Decl *ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
910 Actions.ActOnCXXForRangeDecl(ThisDecl);
911 Actions.FinalizeDeclaration(ThisDecl);
912 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS, &ThisDecl, 1);
913 }
914
Chris Lattner5f9e2722011-07-23 10:55:15 +0000915 SmallVector<Decl *, 8> DeclsInGroup;
Richard Smithad762fc2011-04-14 22:09:26 +0000916 Decl *FirstDecl = ParseDeclarationAfterDeclaratorAndAttributes(D);
John McCall54abf7d2009-11-04 02:18:39 +0000917 D.complete(FirstDecl);
John McCalld226f652010-08-21 09:40:31 +0000918 if (FirstDecl)
John McCalld8ac0572009-11-03 19:26:08 +0000919 DeclsInGroup.push_back(FirstDecl);
920
921 // If we don't have a comma, it is either the end of the list (a ';') or an
922 // error, bail out.
923 while (Tok.is(tok::comma)) {
924 // Consume the comma.
Chris Lattner23c4b182009-03-29 17:18:04 +0000925 ConsumeToken();
John McCalld8ac0572009-11-03 19:26:08 +0000926
927 // Parse the next declarator.
928 D.clear();
929
930 // Accept attributes in an init-declarator. In the first declarator in a
931 // declaration, these would be part of the declspec. In subsequent
932 // declarators, they become part of the declarator itself, so that they
933 // don't apply to declarators after *this* one. Examples:
934 // short __attribute__((common)) var; -> declspec
935 // short var __attribute__((common)); -> declarator
936 // short x, __attribute__((common)) var; -> declarator
John McCall7f040a92010-12-24 02:08:15 +0000937 MaybeParseGNUAttributes(D);
John McCalld8ac0572009-11-03 19:26:08 +0000938
939 ParseDeclarator(D);
940
John McCalld226f652010-08-21 09:40:31 +0000941 Decl *ThisDecl = ParseDeclarationAfterDeclarator(D);
John McCall54abf7d2009-11-04 02:18:39 +0000942 D.complete(ThisDecl);
John McCalld226f652010-08-21 09:40:31 +0000943 if (ThisDecl)
John McCalld8ac0572009-11-03 19:26:08 +0000944 DeclsInGroup.push_back(ThisDecl);
945 }
946
947 if (DeclEnd)
948 *DeclEnd = Tok.getLocation();
949
950 if (Context != Declarator::ForContext &&
951 ExpectAndConsume(tok::semi,
952 Context == Declarator::FileContext
953 ? diag::err_invalid_token_after_toplevel_declarator
954 : diag::err_expected_semi_declaration)) {
Chris Lattner004659a2010-07-11 22:42:07 +0000955 // Okay, there was no semicolon and one was expected. If we see a
956 // declaration specifier, just assume it was missing and continue parsing.
957 // Otherwise things are very confused and we skip to recover.
958 if (!isDeclarationSpecifier()) {
959 SkipUntil(tok::r_brace, true, true);
960 if (Tok.is(tok::semi))
961 ConsumeToken();
962 }
John McCalld8ac0572009-11-03 19:26:08 +0000963 }
964
Douglas Gregor23c94db2010-07-02 17:43:08 +0000965 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS,
John McCalld8ac0572009-11-03 19:26:08 +0000966 DeclsInGroup.data(),
967 DeclsInGroup.size());
Reid Spencer5f016e22007-07-11 17:01:13 +0000968}
969
Richard Smithad762fc2011-04-14 22:09:26 +0000970/// Parse an optional simple-asm-expr and attributes, and attach them to a
971/// declarator. Returns true on an error.
972bool Parser::ParseAttributesAfterDeclarator(Declarator &D) {
973 // If a simple-asm-expr is present, parse it.
974 if (Tok.is(tok::kw_asm)) {
975 SourceLocation Loc;
976 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
977 if (AsmLabel.isInvalid()) {
978 SkipUntil(tok::semi, true, true);
979 return true;
980 }
981
982 D.setAsmLabel(AsmLabel.release());
983 D.SetRangeEnd(Loc);
984 }
985
986 MaybeParseGNUAttributes(D);
987 return false;
988}
989
Douglas Gregor1426e532009-05-12 21:31:51 +0000990/// \brief Parse 'declaration' after parsing 'declaration-specifiers
991/// declarator'. This method parses the remainder of the declaration
992/// (including any attributes or initializer, among other things) and
993/// finalizes the declaration.
Reid Spencer5f016e22007-07-11 17:01:13 +0000994///
Reid Spencer5f016e22007-07-11 17:01:13 +0000995/// init-declarator: [C99 6.7]
996/// declarator
997/// declarator '=' initializer
998/// [GNU] declarator simple-asm-expr[opt] attributes[opt]
999/// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00001000/// [C++] declarator initializer[opt]
1001///
1002/// [C++] initializer:
1003/// [C++] '=' initializer-clause
1004/// [C++] '(' expression-list ')'
Sebastian Redl50de12f2009-03-24 22:27:57 +00001005/// [C++0x] '=' 'default' [TODO]
1006/// [C++0x] '=' 'delete'
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001007/// [C++0x] braced-init-list
Sebastian Redl50de12f2009-03-24 22:27:57 +00001008///
1009/// According to the standard grammar, =default and =delete are function
1010/// definitions, but that definitely doesn't fit with the parser here.
Reid Spencer5f016e22007-07-11 17:01:13 +00001011///
John McCalld226f652010-08-21 09:40:31 +00001012Decl *Parser::ParseDeclarationAfterDeclarator(Declarator &D,
Douglas Gregore542c862009-06-23 23:11:28 +00001013 const ParsedTemplateInfo &TemplateInfo) {
Richard Smithad762fc2011-04-14 22:09:26 +00001014 if (ParseAttributesAfterDeclarator(D))
1015 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001016
Richard Smithad762fc2011-04-14 22:09:26 +00001017 return ParseDeclarationAfterDeclaratorAndAttributes(D, TemplateInfo);
1018}
Mike Stump1eb44332009-09-09 15:08:12 +00001019
Richard Smithad762fc2011-04-14 22:09:26 +00001020Decl *Parser::ParseDeclarationAfterDeclaratorAndAttributes(Declarator &D,
1021 const ParsedTemplateInfo &TemplateInfo) {
Douglas Gregor1426e532009-05-12 21:31:51 +00001022 // Inform the current actions module that we just parsed this declarator.
John McCalld226f652010-08-21 09:40:31 +00001023 Decl *ThisDecl = 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +00001024 switch (TemplateInfo.Kind) {
1025 case ParsedTemplateInfo::NonTemplate:
Douglas Gregor23c94db2010-07-02 17:43:08 +00001026 ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
Douglas Gregord5a423b2009-09-25 18:43:00 +00001027 break;
1028
1029 case ParsedTemplateInfo::Template:
1030 case ParsedTemplateInfo::ExplicitSpecialization:
Douglas Gregor23c94db2010-07-02 17:43:08 +00001031 ThisDecl = Actions.ActOnTemplateDeclarator(getCurScope(),
John McCallf312b1e2010-08-26 23:41:50 +00001032 MultiTemplateParamsArg(Actions,
Douglas Gregore542c862009-06-23 23:11:28 +00001033 TemplateInfo.TemplateParams->data(),
1034 TemplateInfo.TemplateParams->size()),
Douglas Gregord5a423b2009-09-25 18:43:00 +00001035 D);
1036 break;
1037
1038 case ParsedTemplateInfo::ExplicitInstantiation: {
John McCalld226f652010-08-21 09:40:31 +00001039 DeclResult ThisRes
Douglas Gregor23c94db2010-07-02 17:43:08 +00001040 = Actions.ActOnExplicitInstantiation(getCurScope(),
Douglas Gregord5a423b2009-09-25 18:43:00 +00001041 TemplateInfo.ExternLoc,
1042 TemplateInfo.TemplateLoc,
1043 D);
1044 if (ThisRes.isInvalid()) {
1045 SkipUntil(tok::semi, true, true);
John McCalld226f652010-08-21 09:40:31 +00001046 return 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +00001047 }
1048
1049 ThisDecl = ThisRes.get();
1050 break;
1051 }
1052 }
Mike Stump1eb44332009-09-09 15:08:12 +00001053
Richard Smith34b41d92011-02-20 03:19:35 +00001054 bool TypeContainsAuto =
1055 D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto;
1056
Douglas Gregor1426e532009-05-12 21:31:51 +00001057 // Parse declarator '=' initializer.
Argyrios Kyrtzidisa6eb5f82010-10-08 02:39:23 +00001058 if (isTokenEqualOrMistypedEqualEqual(
1059 diag::err_invalid_equalequal_after_declarator)) {
Douglas Gregor1426e532009-05-12 21:31:51 +00001060 ConsumeToken();
Anders Carlsson37bf9d22010-09-24 21:25:25 +00001061 if (Tok.is(tok::kw_delete)) {
Sean Hunte4246a62011-05-12 06:15:49 +00001062 if (D.isFunctionDeclarator())
1063 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
1064 << 1 /* delete */;
1065 else
1066 Diag(ConsumeToken(), diag::err_deleted_non_function);
Sean Huntfe2695e2011-05-06 01:42:00 +00001067 } else if (Tok.is(tok::kw_default)) {
Sean Hunte4246a62011-05-12 06:15:49 +00001068 if (D.isFunctionDeclarator())
1069 Diag(Tok, diag::err_default_delete_in_multiple_declaration)
1070 << 1 /* delete */;
1071 else
1072 Diag(ConsumeToken(), diag::err_default_special_members);
Douglas Gregor1426e532009-05-12 21:31:51 +00001073 } else {
John McCall731ad842009-12-19 09:28:58 +00001074 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
1075 EnterScope(0);
Douglas Gregor23c94db2010-07-02 17:43:08 +00001076 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
John McCall731ad842009-12-19 09:28:58 +00001077 }
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +00001078
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +00001079 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001080 Actions.CodeCompleteInitializer(getCurScope(), ThisDecl);
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +00001081 ConsumeCodeCompletionToken();
1082 SkipUntil(tok::comma, true, true);
1083 return ThisDecl;
1084 }
1085
John McCall60d7b3a2010-08-24 06:29:42 +00001086 ExprResult Init(ParseInitializer());
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +00001087
John McCall731ad842009-12-19 09:28:58 +00001088 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001089 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
John McCall731ad842009-12-19 09:28:58 +00001090 ExitScope();
1091 }
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +00001092
Douglas Gregor1426e532009-05-12 21:31:51 +00001093 if (Init.isInvalid()) {
Douglas Gregor00225542010-03-01 18:27:54 +00001094 SkipUntil(tok::comma, true, true);
1095 Actions.ActOnInitializerError(ThisDecl);
1096 } else
Richard Smith34b41d92011-02-20 03:19:35 +00001097 Actions.AddInitializerToDecl(ThisDecl, Init.take(),
1098 /*DirectInit=*/false, TypeContainsAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +00001099 }
1100 } else if (Tok.is(tok::l_paren)) {
1101 // Parse C++ direct initializer: '(' expression-list ')'
1102 SourceLocation LParenLoc = ConsumeParen();
1103 ExprVector Exprs(Actions);
1104 CommaLocsTy CommaLocs;
1105
Douglas Gregorb4debae2009-12-22 17:47:17 +00001106 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
1107 EnterScope(0);
Douglas Gregor23c94db2010-07-02 17:43:08 +00001108 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001109 }
1110
Douglas Gregor1426e532009-05-12 21:31:51 +00001111 if (ParseExpressionList(Exprs, CommaLocs)) {
1112 SkipUntil(tok::r_paren);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001113
1114 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001115 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001116 ExitScope();
1117 }
Douglas Gregor1426e532009-05-12 21:31:51 +00001118 } else {
1119 // Match the ')'.
1120 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
1121
1122 assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() &&
1123 "Unexpected number of commas!");
Douglas Gregorb4debae2009-12-22 17:47:17 +00001124
1125 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001126 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001127 ExitScope();
1128 }
1129
Douglas Gregor1426e532009-05-12 21:31:51 +00001130 Actions.AddCXXDirectInitializerToDecl(ThisDecl, LParenLoc,
1131 move_arg(Exprs),
Richard Smith34b41d92011-02-20 03:19:35 +00001132 RParenLoc,
1133 TypeContainsAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +00001134 }
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001135 } else if (getLang().CPlusPlus0x && Tok.is(tok::l_brace)) {
1136 // Parse C++0x braced-init-list.
1137 if (D.getCXXScopeSpec().isSet()) {
1138 EnterScope(0);
1139 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
1140 }
1141
1142 ExprResult Init(ParseBraceInitializer());
1143
1144 if (D.getCXXScopeSpec().isSet()) {
1145 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
1146 ExitScope();
1147 }
1148
1149 if (Init.isInvalid()) {
1150 Actions.ActOnInitializerError(ThisDecl);
1151 } else
1152 Actions.AddInitializerToDecl(ThisDecl, Init.take(),
1153 /*DirectInit=*/true, TypeContainsAuto);
1154
Douglas Gregor1426e532009-05-12 21:31:51 +00001155 } else {
Richard Smith34b41d92011-02-20 03:19:35 +00001156 Actions.ActOnUninitializedDecl(ThisDecl, TypeContainsAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +00001157 }
1158
Richard Smith483b9f32011-02-21 20:05:19 +00001159 Actions.FinalizeDeclaration(ThisDecl);
1160
Douglas Gregor1426e532009-05-12 21:31:51 +00001161 return ThisDecl;
1162}
1163
Reid Spencer5f016e22007-07-11 17:01:13 +00001164/// ParseSpecifierQualifierList
1165/// specifier-qualifier-list:
1166/// type-specifier specifier-qualifier-list[opt]
1167/// type-qualifier specifier-qualifier-list[opt]
1168/// [GNU] attributes specifier-qualifier-list[opt]
1169///
Richard Smithc89edf52011-07-01 19:46:12 +00001170void Parser::ParseSpecifierQualifierList(DeclSpec &DS, AccessSpecifier AS) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001171 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
1172 /// parse declaration-specifiers and complain about extra stuff.
Richard Smithc89edf52011-07-01 19:46:12 +00001173 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS);
Mike Stump1eb44332009-09-09 15:08:12 +00001174
Reid Spencer5f016e22007-07-11 17:01:13 +00001175 // Validate declspec for type-name.
1176 unsigned Specs = DS.getParsedSpecifiers();
Chris Lattnerb6645dd2009-04-14 21:16:09 +00001177 if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers() &&
John McCall7f040a92010-12-24 02:08:15 +00001178 !DS.hasAttributes())
Reid Spencer5f016e22007-07-11 17:01:13 +00001179 Diag(Tok, diag::err_typename_requires_specqual);
Mike Stump1eb44332009-09-09 15:08:12 +00001180
Reid Spencer5f016e22007-07-11 17:01:13 +00001181 // Issue diagnostic and remove storage class if present.
1182 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
1183 if (DS.getStorageClassSpecLoc().isValid())
1184 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
1185 else
1186 Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass);
1187 DS.ClearStorageClassSpecs();
1188 }
Mike Stump1eb44332009-09-09 15:08:12 +00001189
Reid Spencer5f016e22007-07-11 17:01:13 +00001190 // Issue diagnostic and remove function specfier if present.
1191 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Douglas Gregorb48fe382008-10-31 09:07:45 +00001192 if (DS.isInlineSpecified())
1193 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
1194 if (DS.isVirtualSpecified())
1195 Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec);
1196 if (DS.isExplicitSpecified())
1197 Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec);
Reid Spencer5f016e22007-07-11 17:01:13 +00001198 DS.ClearFunctionSpecs();
1199 }
1200}
1201
Chris Lattnerc199ab32009-04-12 20:42:31 +00001202/// isValidAfterIdentifierInDeclaratorAfterDeclSpec - Return true if the
1203/// specified token is valid after the identifier in a declarator which
1204/// immediately follows the declspec. For example, these things are valid:
1205///
1206/// int x [ 4]; // direct-declarator
1207/// int x ( int y); // direct-declarator
1208/// int(int x ) // direct-declarator
1209/// int x ; // simple-declaration
1210/// int x = 17; // init-declarator-list
1211/// int x , y; // init-declarator-list
1212/// int x __asm__ ("foo"); // init-declarator-list
Chris Lattnerb6645dd2009-04-14 21:16:09 +00001213/// int x : 4; // struct-declarator
Chris Lattnerc83c27a2009-04-12 22:29:43 +00001214/// int x { 5}; // C++'0x unified initializers
Chris Lattnerc199ab32009-04-12 20:42:31 +00001215///
1216/// This is not, because 'x' does not immediately follow the declspec (though
1217/// ')' happens to be valid anyway).
1218/// int (x)
1219///
1220static bool isValidAfterIdentifierInDeclarator(const Token &T) {
1221 return T.is(tok::l_square) || T.is(tok::l_paren) || T.is(tok::r_paren) ||
1222 T.is(tok::semi) || T.is(tok::comma) || T.is(tok::equal) ||
Chris Lattnerb6645dd2009-04-14 21:16:09 +00001223 T.is(tok::kw_asm) || T.is(tok::l_brace) || T.is(tok::colon);
Chris Lattnerc199ab32009-04-12 20:42:31 +00001224}
1225
Chris Lattnere40c2952009-04-14 21:34:55 +00001226
1227/// ParseImplicitInt - This method is called when we have an non-typename
1228/// identifier in a declspec (which normally terminates the decl spec) when
1229/// the declspec has no type specifier. In this case, the declspec is either
1230/// malformed or is "implicit int" (in K&R and C89).
1231///
1232/// This method handles diagnosing this prettily and returns false if the
1233/// declspec is done being processed. If it recovers and thinks there may be
1234/// other pieces of declspec after it, it returns true.
1235///
Chris Lattnerf4382f52009-04-14 22:17:06 +00001236bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001237 const ParsedTemplateInfo &TemplateInfo,
Chris Lattnere40c2952009-04-14 21:34:55 +00001238 AccessSpecifier AS) {
Chris Lattnerf4382f52009-04-14 22:17:06 +00001239 assert(Tok.is(tok::identifier) && "should have identifier");
Mike Stump1eb44332009-09-09 15:08:12 +00001240
Chris Lattnere40c2952009-04-14 21:34:55 +00001241 SourceLocation Loc = Tok.getLocation();
1242 // If we see an identifier that is not a type name, we normally would
1243 // parse it as the identifer being declared. However, when a typename
1244 // is typo'd or the definition is not included, this will incorrectly
1245 // parse the typename as the identifier name and fall over misparsing
1246 // later parts of the diagnostic.
1247 //
1248 // As such, we try to do some look-ahead in cases where this would
1249 // otherwise be an "implicit-int" case to see if this is invalid. For
1250 // example: "static foo_t x = 4;" In this case, if we parsed foo_t as
1251 // an identifier with implicit int, we'd get a parse error because the
1252 // next token is obviously invalid for a type. Parse these as a case
1253 // with an invalid type specifier.
1254 assert(!DS.hasTypeSpecifier() && "Type specifier checked above");
Mike Stump1eb44332009-09-09 15:08:12 +00001255
Chris Lattnere40c2952009-04-14 21:34:55 +00001256 // Since we know that this either implicit int (which is rare) or an
1257 // error, we'd do lookahead to try to do better recovery.
1258 if (isValidAfterIdentifierInDeclarator(NextToken())) {
1259 // If this token is valid for implicit int, e.g. "static x = 4", then
1260 // we just avoid eating the identifier, so it will be parsed as the
1261 // identifier in the declarator.
1262 return false;
1263 }
Mike Stump1eb44332009-09-09 15:08:12 +00001264
Chris Lattnere40c2952009-04-14 21:34:55 +00001265 // Otherwise, if we don't consume this token, we are going to emit an
1266 // error anyway. Try to recover from various common problems. Check
1267 // to see if this was a reference to a tag name without a tag specified.
1268 // This is a common problem in C (saying 'foo' instead of 'struct foo').
Chris Lattnerf4382f52009-04-14 22:17:06 +00001269 //
1270 // C++ doesn't need this, and isTagName doesn't take SS.
1271 if (SS == 0) {
Argyrios Kyrtzidisb8a9d3b2011-04-21 17:29:47 +00001272 const char *TagName = 0, *FixitTagName = 0;
Chris Lattnerf4382f52009-04-14 22:17:06 +00001273 tok::TokenKind TagKind = tok::unknown;
Mike Stump1eb44332009-09-09 15:08:12 +00001274
Douglas Gregor23c94db2010-07-02 17:43:08 +00001275 switch (Actions.isTagName(*Tok.getIdentifierInfo(), getCurScope())) {
Chris Lattnere40c2952009-04-14 21:34:55 +00001276 default: break;
Argyrios Kyrtzidisb8a9d3b2011-04-21 17:29:47 +00001277 case DeclSpec::TST_enum:
1278 TagName="enum" ; FixitTagName = "enum " ; TagKind=tok::kw_enum ;break;
1279 case DeclSpec::TST_union:
1280 TagName="union" ; FixitTagName = "union " ;TagKind=tok::kw_union ;break;
1281 case DeclSpec::TST_struct:
1282 TagName="struct"; FixitTagName = "struct ";TagKind=tok::kw_struct;break;
1283 case DeclSpec::TST_class:
1284 TagName="class" ; FixitTagName = "class " ;TagKind=tok::kw_class ;break;
Chris Lattnere40c2952009-04-14 21:34:55 +00001285 }
Mike Stump1eb44332009-09-09 15:08:12 +00001286
Chris Lattnerf4382f52009-04-14 22:17:06 +00001287 if (TagName) {
1288 Diag(Loc, diag::err_use_of_tag_name_without_tag)
John McCall23e907a2010-02-14 01:03:10 +00001289 << Tok.getIdentifierInfo() << TagName << getLang().CPlusPlus
Argyrios Kyrtzidisb8a9d3b2011-04-21 17:29:47 +00001290 << FixItHint::CreateInsertion(Tok.getLocation(),FixitTagName);
Mike Stump1eb44332009-09-09 15:08:12 +00001291
Chris Lattnerf4382f52009-04-14 22:17:06 +00001292 // Parse this as a tag as if the missing tag were present.
1293 if (TagKind == tok::kw_enum)
Douglas Gregor9b9edd62010-03-02 17:53:14 +00001294 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS);
Chris Lattnerf4382f52009-04-14 22:17:06 +00001295 else
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001296 ParseClassSpecifier(TagKind, Loc, DS, TemplateInfo, AS);
Chris Lattnerf4382f52009-04-14 22:17:06 +00001297 return true;
1298 }
Chris Lattnere40c2952009-04-14 21:34:55 +00001299 }
Mike Stump1eb44332009-09-09 15:08:12 +00001300
Douglas Gregora786fdb2009-10-13 23:27:22 +00001301 // This is almost certainly an invalid type name. Let the action emit a
1302 // diagnostic and attempt to recover.
John McCallb3d87482010-08-24 05:47:05 +00001303 ParsedType T;
Douglas Gregora786fdb2009-10-13 23:27:22 +00001304 if (Actions.DiagnoseUnknownTypeName(*Tok.getIdentifierInfo(), Loc,
Douglas Gregor23c94db2010-07-02 17:43:08 +00001305 getCurScope(), SS, T)) {
Douglas Gregora786fdb2009-10-13 23:27:22 +00001306 // The action emitted a diagnostic, so we don't have to.
1307 if (T) {
1308 // The action has suggested that the type T could be used. Set that as
1309 // the type in the declaration specifiers, consume the would-be type
1310 // name token, and we're done.
1311 const char *PrevSpec;
1312 unsigned DiagID;
John McCallb3d87482010-08-24 05:47:05 +00001313 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID, T);
Douglas Gregora786fdb2009-10-13 23:27:22 +00001314 DS.SetRangeEnd(Tok.getLocation());
1315 ConsumeToken();
1316
1317 // There may be other declaration specifiers after this.
1318 return true;
1319 }
1320
1321 // Fall through; the action had no suggestion for us.
1322 } else {
1323 // The action did not emit a diagnostic, so emit one now.
1324 SourceRange R;
1325 if (SS) R = SS->getRange();
1326 Diag(Loc, diag::err_unknown_typename) << Tok.getIdentifierInfo() << R;
1327 }
Mike Stump1eb44332009-09-09 15:08:12 +00001328
Douglas Gregora786fdb2009-10-13 23:27:22 +00001329 // Mark this as an error.
Chris Lattnere40c2952009-04-14 21:34:55 +00001330 const char *PrevSpec;
John McCallfec54012009-08-03 20:12:06 +00001331 unsigned DiagID;
1332 DS.SetTypeSpecType(DeclSpec::TST_error, Loc, PrevSpec, DiagID);
Chris Lattnere40c2952009-04-14 21:34:55 +00001333 DS.SetRangeEnd(Tok.getLocation());
1334 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001335
Chris Lattnere40c2952009-04-14 21:34:55 +00001336 // TODO: Could inject an invalid typedef decl in an enclosing scope to
1337 // avoid rippling error messages on subsequent uses of the same type,
1338 // could be useful if #include was forgotten.
1339 return false;
1340}
1341
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001342/// \brief Determine the declaration specifier context from the declarator
1343/// context.
1344///
1345/// \param Context the declarator context, which is one of the
1346/// Declarator::TheContext enumerator values.
1347Parser::DeclSpecContext
1348Parser::getDeclSpecContextFromDeclaratorContext(unsigned Context) {
1349 if (Context == Declarator::MemberContext)
1350 return DSC_class;
1351 if (Context == Declarator::FileContext)
1352 return DSC_top_level;
1353 return DSC_normal;
1354}
1355
Reid Spencer5f016e22007-07-11 17:01:13 +00001356/// ParseDeclarationSpecifiers
1357/// declaration-specifiers: [C99 6.7]
1358/// storage-class-specifier declaration-specifiers[opt]
1359/// type-specifier declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00001360/// [C99] function-specifier declaration-specifiers[opt]
1361/// [GNU] attributes declaration-specifiers[opt]
1362///
1363/// storage-class-specifier: [C99 6.7.1]
1364/// 'typedef'
1365/// 'extern'
1366/// 'static'
1367/// 'auto'
1368/// 'register'
Sebastian Redl669d5d72008-11-14 23:42:31 +00001369/// [C++] 'mutable'
Reid Spencer5f016e22007-07-11 17:01:13 +00001370/// [GNU] '__thread'
Reid Spencer5f016e22007-07-11 17:01:13 +00001371/// function-specifier: [C99 6.7.4]
1372/// [C99] 'inline'
Douglas Gregorb48fe382008-10-31 09:07:45 +00001373/// [C++] 'virtual'
1374/// [C++] 'explicit'
Peter Collingbournef315fa82011-02-14 01:42:53 +00001375/// [OpenCL] '__kernel'
Anders Carlssonf47f7a12009-05-06 04:46:28 +00001376/// 'friend': [C++ dcl.friend]
Sebastian Redl2ac67232009-11-05 15:47:02 +00001377/// 'constexpr': [C++0x dcl.constexpr]
Anders Carlssonf47f7a12009-05-06 04:46:28 +00001378
Reid Spencer5f016e22007-07-11 17:01:13 +00001379///
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +00001380void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001381 const ParsedTemplateInfo &TemplateInfo,
John McCall67d1a672009-08-06 02:15:43 +00001382 AccessSpecifier AS,
Douglas Gregor312eadb2011-04-24 05:37:28 +00001383 DeclSpecContext DSContext) {
1384 if (DS.getSourceRange().isInvalid()) {
1385 DS.SetRangeStart(Tok.getLocation());
1386 DS.SetRangeEnd(Tok.getLocation());
1387 }
1388
Reid Spencer5f016e22007-07-11 17:01:13 +00001389 while (1) {
John McCallfec54012009-08-03 20:12:06 +00001390 bool isInvalid = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001391 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00001392 unsigned DiagID = 0;
1393
Reid Spencer5f016e22007-07-11 17:01:13 +00001394 SourceLocation Loc = Tok.getLocation();
Douglas Gregor12e083c2008-11-07 15:42:26 +00001395
Reid Spencer5f016e22007-07-11 17:01:13 +00001396 switch (Tok.getKind()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001397 default:
Chris Lattnerbce61352008-07-26 00:20:22 +00001398 DoneWithDeclSpec:
Reid Spencer5f016e22007-07-11 17:01:13 +00001399 // If this is not a declaration specifier token, we're done reading decl
1400 // specifiers. First verify that DeclSpec's are consistent.
Douglas Gregor9b3064b2009-04-01 22:41:11 +00001401 DS.Finish(Diags, PP);
Reid Spencer5f016e22007-07-11 17:01:13 +00001402 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001403
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001404 case tok::code_completion: {
John McCallf312b1e2010-08-26 23:41:50 +00001405 Sema::ParserCompletionContext CCC = Sema::PCC_Namespace;
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001406 if (DS.hasTypeSpecifier()) {
1407 bool AllowNonIdentifiers
1408 = (getCurScope()->getFlags() & (Scope::ControlScope |
1409 Scope::BlockScope |
1410 Scope::TemplateParamScope |
1411 Scope::FunctionPrototypeScope |
1412 Scope::AtCatchScope)) == 0;
1413 bool AllowNestedNameSpecifiers
1414 = DSContext == DSC_top_level ||
1415 (DSContext == DSC_class && DS.isFriendSpecified());
1416
Douglas Gregorc7b6d882010-09-16 15:14:18 +00001417 Actions.CodeCompleteDeclSpec(getCurScope(), DS,
1418 AllowNonIdentifiers,
1419 AllowNestedNameSpecifiers);
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001420 ConsumeCodeCompletionToken();
1421 return;
1422 }
1423
Douglas Gregor68e3c2e2011-02-15 20:33:25 +00001424 if (getCurScope()->getFnParent() || getCurScope()->getBlockParent())
1425 CCC = Sema::PCC_LocalDeclarationSpecifiers;
1426 else if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate)
John McCallf312b1e2010-08-26 23:41:50 +00001427 CCC = DSContext == DSC_class? Sema::PCC_MemberTemplate
1428 : Sema::PCC_Template;
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001429 else if (DSContext == DSC_class)
John McCallf312b1e2010-08-26 23:41:50 +00001430 CCC = Sema::PCC_Class;
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001431 else if (ObjCImpDecl)
John McCallf312b1e2010-08-26 23:41:50 +00001432 CCC = Sema::PCC_ObjCImplementation;
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001433
1434 Actions.CodeCompleteOrdinaryName(getCurScope(), CCC);
1435 ConsumeCodeCompletionToken();
1436 return;
1437 }
1438
Chris Lattner5e02c472009-01-05 00:07:25 +00001439 case tok::coloncolon: // ::foo::bar
John McCall9ba61662010-02-26 08:45:28 +00001440 // C++ scope specifier. Annotate and loop, or bail out on error.
1441 if (TryAnnotateCXXScopeToken(true)) {
1442 if (!DS.hasTypeSpecifier())
1443 DS.SetTypeSpecError();
1444 goto DoneWithDeclSpec;
1445 }
John McCall2e0a7152010-03-01 18:20:46 +00001446 if (Tok.is(tok::coloncolon)) // ::new or ::delete
1447 goto DoneWithDeclSpec;
John McCall9ba61662010-02-26 08:45:28 +00001448 continue;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001449
1450 case tok::annot_cxxscope: {
1451 if (DS.hasTypeSpecifier())
1452 goto DoneWithDeclSpec;
1453
John McCallaa87d332009-12-12 11:40:51 +00001454 CXXScopeSpec SS;
Douglas Gregorc34348a2011-02-24 17:54:50 +00001455 Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(),
1456 Tok.getAnnotationRange(),
1457 SS);
John McCallaa87d332009-12-12 11:40:51 +00001458
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001459 // We are looking for a qualified typename.
Douglas Gregor9135c722009-03-25 15:40:00 +00001460 Token Next = NextToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001461 if (Next.is(tok::annot_template_id) &&
Douglas Gregor9135c722009-03-25 15:40:00 +00001462 static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue())
Douglas Gregorc45c2322009-03-31 00:43:58 +00001463 ->Kind == TNK_Type_template) {
Douglas Gregor9135c722009-03-25 15:40:00 +00001464 // We have a qualified template-id, e.g., N::A<int>
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001465
1466 // C++ [class.qual]p2:
1467 // In a lookup in which the constructor is an acceptable lookup
1468 // result and the nested-name-specifier nominates a class C:
1469 //
1470 // - if the name specified after the
1471 // nested-name-specifier, when looked up in C, is the
1472 // injected-class-name of C (Clause 9), or
1473 //
1474 // - if the name specified after the nested-name-specifier
1475 // is the same as the identifier or the
1476 // simple-template-id's template-name in the last
1477 // component of the nested-name-specifier,
1478 //
1479 // the name is instead considered to name the constructor of
1480 // class C.
1481 //
1482 // Thus, if the template-name is actually the constructor
1483 // name, then the code is ill-formed; this interpretation is
1484 // reinforced by the NAD status of core issue 635.
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +00001485 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Next);
John McCallba9d8532010-04-13 06:39:49 +00001486 if ((DSContext == DSC_top_level ||
1487 (DSContext == DSC_class && DS.isFriendSpecified())) &&
1488 TemplateId->Name &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00001489 Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) {
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001490 if (isConstructorDeclarator()) {
1491 // The user meant this to be an out-of-line constructor
1492 // definition, but template arguments are not allowed
1493 // there. Just allow this as a constructor; we'll
1494 // complain about it later.
1495 goto DoneWithDeclSpec;
1496 }
1497
1498 // The user meant this to name a type, but it actually names
1499 // a constructor with some extraneous template
1500 // arguments. Complain, then parse it as a type as the user
1501 // intended.
1502 Diag(TemplateId->TemplateNameLoc,
1503 diag::err_out_of_line_template_id_names_constructor)
1504 << TemplateId->Name;
1505 }
1506
John McCallaa87d332009-12-12 11:40:51 +00001507 DS.getTypeSpecScope() = SS;
1508 ConsumeToken(); // The C++ scope.
Mike Stump1eb44332009-09-09 15:08:12 +00001509 assert(Tok.is(tok::annot_template_id) &&
Douglas Gregor9135c722009-03-25 15:40:00 +00001510 "ParseOptionalCXXScopeSpecifier not working");
Douglas Gregor059101f2011-03-02 00:47:37 +00001511 AnnotateTemplateIdTokenAsType();
Douglas Gregor9135c722009-03-25 15:40:00 +00001512 continue;
1513 }
1514
Douglas Gregor9d7b3532009-09-28 07:26:33 +00001515 if (Next.is(tok::annot_typename)) {
John McCallaa87d332009-12-12 11:40:51 +00001516 DS.getTypeSpecScope() = SS;
1517 ConsumeToken(); // The C++ scope.
John McCallb3d87482010-08-24 05:47:05 +00001518 if (Tok.getAnnotationValue()) {
1519 ParsedType T = getTypeAnnotation(Tok);
Nico Weber253e80b2010-11-22 10:30:56 +00001520 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename,
1521 Tok.getAnnotationEndLoc(),
John McCallb3d87482010-08-24 05:47:05 +00001522 PrevSpec, DiagID, T);
1523 }
Douglas Gregor9d7b3532009-09-28 07:26:33 +00001524 else
1525 DS.SetTypeSpecError();
1526 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1527 ConsumeToken(); // The typename
1528 }
1529
Douglas Gregor9135c722009-03-25 15:40:00 +00001530 if (Next.isNot(tok::identifier))
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001531 goto DoneWithDeclSpec;
1532
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001533 // If we're in a context where the identifier could be a class name,
1534 // check whether this is a constructor declaration.
John McCallba9d8532010-04-13 06:39:49 +00001535 if ((DSContext == DSC_top_level ||
1536 (DSContext == DSC_class && DS.isFriendSpecified())) &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00001537 Actions.isCurrentClassName(*Next.getIdentifierInfo(), getCurScope(),
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001538 &SS)) {
1539 if (isConstructorDeclarator())
1540 goto DoneWithDeclSpec;
1541
1542 // As noted in C++ [class.qual]p2 (cited above), when the name
1543 // of the class is qualified in a context where it could name
1544 // a constructor, its a constructor name. However, we've
1545 // looked at the declarator, and the user probably meant this
1546 // to be a type. Complain that it isn't supposed to be treated
1547 // as a type, then proceed to parse it as a type.
1548 Diag(Next.getLocation(), diag::err_out_of_line_type_names_constructor)
1549 << Next.getIdentifierInfo();
1550 }
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001551
John McCallb3d87482010-08-24 05:47:05 +00001552 ParsedType TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(),
1553 Next.getLocation(),
Douglas Gregor9e876872011-03-01 18:12:44 +00001554 getCurScope(), &SS,
1555 false, false, ParsedType(),
1556 /*NonTrivialSourceInfo=*/true);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001557
Chris Lattnerf4382f52009-04-14 22:17:06 +00001558 // If the referenced identifier is not a type, then this declspec is
1559 // erroneous: We already checked about that it has no type specifier, and
1560 // C++ doesn't have implicit int. Diagnose it as a typo w.r.t. to the
Mike Stump1eb44332009-09-09 15:08:12 +00001561 // typename.
Chris Lattnerf4382f52009-04-14 22:17:06 +00001562 if (TypeRep == 0) {
1563 ConsumeToken(); // Eat the scope spec so the identifier is current.
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001564 if (ParseImplicitInt(DS, &SS, TemplateInfo, AS)) continue;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001565 goto DoneWithDeclSpec;
Chris Lattnerf4382f52009-04-14 22:17:06 +00001566 }
Mike Stump1eb44332009-09-09 15:08:12 +00001567
John McCallaa87d332009-12-12 11:40:51 +00001568 DS.getTypeSpecScope() = SS;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001569 ConsumeToken(); // The C++ scope.
1570
Douglas Gregor1a51b4a2009-02-09 15:09:02 +00001571 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001572 DiagID, TypeRep);
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001573 if (isInvalid)
1574 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001575
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001576 DS.SetRangeEnd(Tok.getLocation());
1577 ConsumeToken(); // The typename.
1578
1579 continue;
1580 }
Mike Stump1eb44332009-09-09 15:08:12 +00001581
Chris Lattner80d0c892009-01-21 19:48:37 +00001582 case tok::annot_typename: {
John McCallb3d87482010-08-24 05:47:05 +00001583 if (Tok.getAnnotationValue()) {
1584 ParsedType T = getTypeAnnotation(Tok);
Nico Weberc43271e2010-11-22 12:50:03 +00001585 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallb3d87482010-08-24 05:47:05 +00001586 DiagID, T);
1587 } else
Douglas Gregor31a19b62009-04-01 21:51:26 +00001588 DS.SetTypeSpecError();
Chris Lattner5c5db552010-04-05 18:18:31 +00001589
1590 if (isInvalid)
1591 break;
1592
Chris Lattner80d0c892009-01-21 19:48:37 +00001593 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1594 ConsumeToken(); // The typename
Mike Stump1eb44332009-09-09 15:08:12 +00001595
Chris Lattner80d0c892009-01-21 19:48:37 +00001596 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1597 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001598 // Objective-C interface.
1599 if (Tok.is(tok::less) && getLang().ObjC1)
1600 ParseObjCProtocolQualifiers(DS);
1601
Chris Lattner80d0c892009-01-21 19:48:37 +00001602 continue;
1603 }
Mike Stump1eb44332009-09-09 15:08:12 +00001604
Douglas Gregorbfad9152011-04-28 15:48:45 +00001605 case tok::kw___is_signed:
1606 // GNU libstdc++ 4.4 uses __is_signed as an identifier, but Clang
1607 // typically treats it as a trait. If we see __is_signed as it appears
1608 // in libstdc++, e.g.,
1609 //
1610 // static const bool __is_signed;
1611 //
1612 // then treat __is_signed as an identifier rather than as a keyword.
1613 if (DS.getTypeSpecType() == TST_bool &&
1614 DS.getTypeQualifiers() == DeclSpec::TQ_const &&
1615 DS.getStorageClassSpec() == DeclSpec::SCS_static) {
1616 Tok.getIdentifierInfo()->RevertTokenIDToIdentifier();
1617 Tok.setKind(tok::identifier);
1618 }
1619
1620 // We're done with the declaration-specifiers.
1621 goto DoneWithDeclSpec;
1622
Chris Lattner3bd934a2008-07-26 01:18:38 +00001623 // typedef-name
1624 case tok::identifier: {
Chris Lattner5e02c472009-01-05 00:07:25 +00001625 // In C++, check to see if this is a scope specifier like foo::bar::, if
1626 // so handle it as such. This is important for ctor parsing.
John McCall9ba61662010-02-26 08:45:28 +00001627 if (getLang().CPlusPlus) {
1628 if (TryAnnotateCXXScopeToken(true)) {
1629 if (!DS.hasTypeSpecifier())
1630 DS.SetTypeSpecError();
1631 goto DoneWithDeclSpec;
1632 }
1633 if (!Tok.is(tok::identifier))
1634 continue;
1635 }
Mike Stump1eb44332009-09-09 15:08:12 +00001636
Chris Lattner3bd934a2008-07-26 01:18:38 +00001637 // This identifier can only be a typedef name if we haven't already seen
1638 // a type-specifier. Without this check we misparse:
1639 // typedef int X; struct Y { short X; }; as 'short int'.
1640 if (DS.hasTypeSpecifier())
1641 goto DoneWithDeclSpec;
Mike Stump1eb44332009-09-09 15:08:12 +00001642
John Thompson82287d12010-02-05 00:12:22 +00001643 // Check for need to substitute AltiVec keyword tokens.
1644 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
1645 break;
1646
Chris Lattner3bd934a2008-07-26 01:18:38 +00001647 // It has to be available as a typedef too!
John McCallb3d87482010-08-24 05:47:05 +00001648 ParsedType TypeRep =
1649 Actions.getTypeName(*Tok.getIdentifierInfo(),
1650 Tok.getLocation(), getCurScope());
Douglas Gregor55f6b142009-02-09 18:46:07 +00001651
Chris Lattnerc199ab32009-04-12 20:42:31 +00001652 // If this is not a typedef name, don't parse it as part of the declspec,
1653 // it must be an implicit int or an error.
John McCallb3d87482010-08-24 05:47:05 +00001654 if (!TypeRep) {
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001655 if (ParseImplicitInt(DS, 0, TemplateInfo, AS)) continue;
Chris Lattner3bd934a2008-07-26 01:18:38 +00001656 goto DoneWithDeclSpec;
Chris Lattnerc199ab32009-04-12 20:42:31 +00001657 }
Douglas Gregor55f6b142009-02-09 18:46:07 +00001658
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001659 // If we're in a context where the identifier could be a class name,
1660 // check whether this is a constructor declaration.
1661 if (getLang().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00001662 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), getCurScope()) &&
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001663 isConstructorDeclarator())
Douglas Gregorb48fe382008-10-31 09:07:45 +00001664 goto DoneWithDeclSpec;
1665
Douglas Gregor1a51b4a2009-02-09 15:09:02 +00001666 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001667 DiagID, TypeRep);
Chris Lattner3bd934a2008-07-26 01:18:38 +00001668 if (isInvalid)
1669 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001670
Chris Lattner3bd934a2008-07-26 01:18:38 +00001671 DS.SetRangeEnd(Tok.getLocation());
1672 ConsumeToken(); // The identifier
1673
1674 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1675 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001676 // Objective-C interface.
1677 if (Tok.is(tok::less) && getLang().ObjC1)
1678 ParseObjCProtocolQualifiers(DS);
1679
Steve Naroff4f9b9f12008-09-22 10:28:57 +00001680 // Need to support trailing type qualifiers (e.g. "id<p> const").
1681 // If a type specifier follows, it will be diagnosed elsewhere.
1682 continue;
Chris Lattner3bd934a2008-07-26 01:18:38 +00001683 }
Douglas Gregor39a8de12009-02-25 19:37:18 +00001684
1685 // type-name
1686 case tok::annot_template_id: {
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +00001687 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregorc45c2322009-03-31 00:43:58 +00001688 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor39a8de12009-02-25 19:37:18 +00001689 // This template-id does not refer to a type name, so we're
1690 // done with the type-specifiers.
1691 goto DoneWithDeclSpec;
1692 }
1693
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001694 // If we're in a context where the template-id could be a
1695 // constructor name or specialization, check whether this is a
1696 // constructor declaration.
1697 if (getLang().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00001698 Actions.isCurrentClassName(*TemplateId->Name, getCurScope()) &&
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001699 isConstructorDeclarator())
1700 goto DoneWithDeclSpec;
1701
Douglas Gregor39a8de12009-02-25 19:37:18 +00001702 // Turn the template-id annotation token into a type annotation
1703 // token, then try again to parse it as a type-specifier.
Douglas Gregor31a19b62009-04-01 21:51:26 +00001704 AnnotateTemplateIdTokenAsType();
Douglas Gregor39a8de12009-02-25 19:37:18 +00001705 continue;
1706 }
1707
Reid Spencer5f016e22007-07-11 17:01:13 +00001708 // GNU attributes support.
1709 case tok::kw___attribute:
John McCall7f040a92010-12-24 02:08:15 +00001710 ParseGNUAttributes(DS.getAttributes());
Reid Spencer5f016e22007-07-11 17:01:13 +00001711 continue;
Steve Narofff59e17e2008-12-24 20:59:21 +00001712
1713 // Microsoft declspec support.
1714 case tok::kw___declspec:
John McCall7f040a92010-12-24 02:08:15 +00001715 ParseMicrosoftDeclSpec(DS.getAttributes());
Steve Narofff59e17e2008-12-24 20:59:21 +00001716 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001717
Steve Naroff239f0732008-12-25 14:16:32 +00001718 // Microsoft single token adornments.
Steve Naroff86bc6cf2008-12-25 14:41:26 +00001719 case tok::kw___forceinline:
Eli Friedman290eeb02009-06-08 23:27:34 +00001720 // FIXME: Add handling here!
1721 break;
1722
1723 case tok::kw___ptr64:
Steve Naroff86bc6cf2008-12-25 14:41:26 +00001724 case tok::kw___w64:
Steve Naroff239f0732008-12-25 14:16:32 +00001725 case tok::kw___cdecl:
1726 case tok::kw___stdcall:
1727 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00001728 case tok::kw___thiscall:
John McCall7f040a92010-12-24 02:08:15 +00001729 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman290eeb02009-06-08 23:27:34 +00001730 continue;
1731
Dawn Perchik52fc3142010-09-03 01:29:35 +00001732 // Borland single token adornments.
1733 case tok::kw___pascal:
John McCall7f040a92010-12-24 02:08:15 +00001734 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik52fc3142010-09-03 01:29:35 +00001735 continue;
1736
Peter Collingbournef315fa82011-02-14 01:42:53 +00001737 // OpenCL single token adornments.
1738 case tok::kw___kernel:
1739 ParseOpenCLAttributes(DS.getAttributes());
1740 continue;
1741
Reid Spencer5f016e22007-07-11 17:01:13 +00001742 // storage-class-specifier
1743 case tok::kw_typedef:
John McCallfec54012009-08-03 20:12:06 +00001744 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_typedef, Loc, PrevSpec,
Peter Collingbournee2f82f72011-02-11 19:59:54 +00001745 DiagID, getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00001746 break;
1747 case tok::kw_extern:
1748 if (DS.isThreadSpecified())
Chris Lattner1ab3b962008-11-18 07:48:38 +00001749 Diag(Tok, diag::ext_thread_before) << "extern";
John McCallfec54012009-08-03 20:12:06 +00001750 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_extern, Loc, PrevSpec,
Peter Collingbournee2f82f72011-02-11 19:59:54 +00001751 DiagID, getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00001752 break;
Steve Naroff8d54bf22007-12-18 00:16:02 +00001753 case tok::kw___private_extern__:
Chris Lattnerf97409f2008-04-06 06:57:35 +00001754 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_private_extern, Loc,
Peter Collingbournee2f82f72011-02-11 19:59:54 +00001755 PrevSpec, DiagID, getLang());
Steve Naroff8d54bf22007-12-18 00:16:02 +00001756 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00001757 case tok::kw_static:
1758 if (DS.isThreadSpecified())
Chris Lattner1ab3b962008-11-18 07:48:38 +00001759 Diag(Tok, diag::ext_thread_before) << "static";
John McCallfec54012009-08-03 20:12:06 +00001760 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_static, Loc, PrevSpec,
Peter Collingbournee2f82f72011-02-11 19:59:54 +00001761 DiagID, getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00001762 break;
1763 case tok::kw_auto:
Douglas Gregor18d8b792011-03-14 21:43:30 +00001764 if (getLang().CPlusPlus0x) {
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00001765 if (isKnownToBeTypeSpecifier(GetLookAheadToken(1))) {
1766 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, Loc, PrevSpec,
1767 DiagID, getLang());
1768 if (!isInvalid)
1769 Diag(Tok, diag::auto_storage_class)
1770 << FixItHint::CreateRemoval(DS.getStorageClassSpecLoc());
1771 }
1772 else
1773 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec,
1774 DiagID);
1775 }
Anders Carlssone89d1592009-06-26 18:41:36 +00001776 else
John McCallfec54012009-08-03 20:12:06 +00001777 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, Loc, PrevSpec,
Peter Collingbournee2f82f72011-02-11 19:59:54 +00001778 DiagID, getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00001779 break;
1780 case tok::kw_register:
John McCallfec54012009-08-03 20:12:06 +00001781 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_register, Loc, PrevSpec,
Peter Collingbournee2f82f72011-02-11 19:59:54 +00001782 DiagID, getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00001783 break;
Sebastian Redl669d5d72008-11-14 23:42:31 +00001784 case tok::kw_mutable:
John McCallfec54012009-08-03 20:12:06 +00001785 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_mutable, Loc, PrevSpec,
Peter Collingbournee2f82f72011-02-11 19:59:54 +00001786 DiagID, getLang());
Sebastian Redl669d5d72008-11-14 23:42:31 +00001787 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00001788 case tok::kw___thread:
John McCallfec54012009-08-03 20:12:06 +00001789 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001790 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001791
Reid Spencer5f016e22007-07-11 17:01:13 +00001792 // function-specifier
1793 case tok::kw_inline:
John McCallfec54012009-08-03 20:12:06 +00001794 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001795 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +00001796 case tok::kw_virtual:
John McCallfec54012009-08-03 20:12:06 +00001797 isInvalid = DS.SetFunctionSpecVirtual(Loc, PrevSpec, DiagID);
Douglas Gregorb48fe382008-10-31 09:07:45 +00001798 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +00001799 case tok::kw_explicit:
John McCallfec54012009-08-03 20:12:06 +00001800 isInvalid = DS.SetFunctionSpecExplicit(Loc, PrevSpec, DiagID);
Douglas Gregorb48fe382008-10-31 09:07:45 +00001801 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00001802
Anders Carlssonf47f7a12009-05-06 04:46:28 +00001803 // friend
1804 case tok::kw_friend:
John McCall67d1a672009-08-06 02:15:43 +00001805 if (DSContext == DSC_class)
1806 isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID);
1807 else {
1808 PrevSpec = ""; // not actually used by the diagnostic
1809 DiagID = diag::err_friend_invalid_in_context;
1810 isInvalid = true;
1811 }
Anders Carlssonf47f7a12009-05-06 04:46:28 +00001812 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001813
Sebastian Redl2ac67232009-11-05 15:47:02 +00001814 // constexpr
1815 case tok::kw_constexpr:
1816 isInvalid = DS.SetConstexprSpec(Loc, PrevSpec, DiagID);
1817 break;
1818
Chris Lattner80d0c892009-01-21 19:48:37 +00001819 // type-specifier
1820 case tok::kw_short:
John McCallfec54012009-08-03 20:12:06 +00001821 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec,
1822 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001823 break;
1824 case tok::kw_long:
1825 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCallfec54012009-08-03 20:12:06 +00001826 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
1827 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001828 else
John McCallfec54012009-08-03 20:12:06 +00001829 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
1830 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001831 break;
Francois Pichet338d7f72011-04-28 01:59:37 +00001832 case tok::kw___int64:
1833 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
1834 DiagID);
1835 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00001836 case tok::kw_signed:
John McCallfec54012009-08-03 20:12:06 +00001837 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec,
1838 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001839 break;
1840 case tok::kw_unsigned:
John McCallfec54012009-08-03 20:12:06 +00001841 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
1842 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001843 break;
1844 case tok::kw__Complex:
John McCallfec54012009-08-03 20:12:06 +00001845 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
1846 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001847 break;
1848 case tok::kw__Imaginary:
John McCallfec54012009-08-03 20:12:06 +00001849 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
1850 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001851 break;
1852 case tok::kw_void:
John McCallfec54012009-08-03 20:12:06 +00001853 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec,
1854 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001855 break;
1856 case tok::kw_char:
John McCallfec54012009-08-03 20:12:06 +00001857 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec,
1858 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001859 break;
1860 case tok::kw_int:
John McCallfec54012009-08-03 20:12:06 +00001861 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec,
1862 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001863 break;
1864 case tok::kw_float:
John McCallfec54012009-08-03 20:12:06 +00001865 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec,
1866 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001867 break;
1868 case tok::kw_double:
John McCallfec54012009-08-03 20:12:06 +00001869 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec,
1870 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001871 break;
1872 case tok::kw_wchar_t:
John McCallfec54012009-08-03 20:12:06 +00001873 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec,
1874 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001875 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001876 case tok::kw_char16_t:
John McCallfec54012009-08-03 20:12:06 +00001877 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec,
1878 DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001879 break;
1880 case tok::kw_char32_t:
John McCallfec54012009-08-03 20:12:06 +00001881 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec,
1882 DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001883 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00001884 case tok::kw_bool:
1885 case tok::kw__Bool:
Argyrios Kyrtzidis4383e182010-11-16 18:18:13 +00001886 if (Tok.is(tok::kw_bool) &&
1887 DS.getTypeSpecType() != DeclSpec::TST_unspecified &&
1888 DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
1889 PrevSpec = ""; // Not used by the diagnostic.
1890 DiagID = diag::err_bool_redeclaration;
Fariborz Jahaniane106a0b2011-04-19 21:42:37 +00001891 // For better error recovery.
1892 Tok.setKind(tok::identifier);
Argyrios Kyrtzidis4383e182010-11-16 18:18:13 +00001893 isInvalid = true;
1894 } else {
1895 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec,
1896 DiagID);
1897 }
Chris Lattner80d0c892009-01-21 19:48:37 +00001898 break;
1899 case tok::kw__Decimal32:
John McCallfec54012009-08-03 20:12:06 +00001900 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
1901 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001902 break;
1903 case tok::kw__Decimal64:
John McCallfec54012009-08-03 20:12:06 +00001904 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
1905 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001906 break;
1907 case tok::kw__Decimal128:
John McCallfec54012009-08-03 20:12:06 +00001908 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
1909 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001910 break;
John Thompson82287d12010-02-05 00:12:22 +00001911 case tok::kw___vector:
1912 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
1913 break;
1914 case tok::kw___pixel:
1915 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
1916 break;
John McCalla5fc4722011-04-09 22:50:59 +00001917 case tok::kw___unknown_anytype:
1918 isInvalid = DS.SetTypeSpecType(TST_unknown_anytype, Loc,
1919 PrevSpec, DiagID);
1920 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00001921
1922 // class-specifier:
1923 case tok::kw_class:
1924 case tok::kw_struct:
Chris Lattner4c97d762009-04-12 21:49:30 +00001925 case tok::kw_union: {
1926 tok::TokenKind Kind = Tok.getKind();
1927 ConsumeToken();
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001928 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS);
Chris Lattner80d0c892009-01-21 19:48:37 +00001929 continue;
Chris Lattner4c97d762009-04-12 21:49:30 +00001930 }
Chris Lattner80d0c892009-01-21 19:48:37 +00001931
1932 // enum-specifier:
1933 case tok::kw_enum:
Chris Lattner4c97d762009-04-12 21:49:30 +00001934 ConsumeToken();
Douglas Gregor9b9edd62010-03-02 17:53:14 +00001935 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS);
Chris Lattner80d0c892009-01-21 19:48:37 +00001936 continue;
1937
1938 // cv-qualifier:
1939 case tok::kw_const:
John McCallfec54012009-08-03 20:12:06 +00001940 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec, DiagID,
1941 getLang());
Chris Lattner80d0c892009-01-21 19:48:37 +00001942 break;
1943 case tok::kw_volatile:
John McCallfec54012009-08-03 20:12:06 +00001944 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
1945 getLang());
Chris Lattner80d0c892009-01-21 19:48:37 +00001946 break;
1947 case tok::kw_restrict:
John McCallfec54012009-08-03 20:12:06 +00001948 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
1949 getLang());
Chris Lattner80d0c892009-01-21 19:48:37 +00001950 break;
1951
Douglas Gregord57959a2009-03-27 23:10:48 +00001952 // C++ typename-specifier:
1953 case tok::kw_typename:
John McCall9ba61662010-02-26 08:45:28 +00001954 if (TryAnnotateTypeOrScopeToken()) {
1955 DS.SetTypeSpecError();
1956 goto DoneWithDeclSpec;
1957 }
1958 if (!Tok.is(tok::kw_typename))
Douglas Gregord57959a2009-03-27 23:10:48 +00001959 continue;
1960 break;
1961
Chris Lattner80d0c892009-01-21 19:48:37 +00001962 // GNU typeof support.
1963 case tok::kw_typeof:
1964 ParseTypeofSpecifier(DS);
1965 continue;
1966
Anders Carlsson6fd634f2009-06-24 17:47:40 +00001967 case tok::kw_decltype:
1968 ParseDecltypeSpecifier(DS);
1969 continue;
1970
Sean Huntdb5d44b2011-05-19 05:37:45 +00001971 case tok::kw___underlying_type:
1972 ParseUnderlyingTypeSpecifier(DS);
1973
Peter Collingbourne207f4d82011-03-18 22:38:29 +00001974 // OpenCL qualifiers:
1975 case tok::kw_private:
1976 if (!getLang().OpenCL)
1977 goto DoneWithDeclSpec;
1978 case tok::kw___private:
1979 case tok::kw___global:
1980 case tok::kw___local:
1981 case tok::kw___constant:
1982 case tok::kw___read_only:
1983 case tok::kw___write_only:
1984 case tok::kw___read_write:
1985 ParseOpenCLQualifiers(DS);
1986 break;
1987
Steve Naroffd3ded1f2008-06-05 00:02:44 +00001988 case tok::less:
Chris Lattner3bd934a2008-07-26 01:18:38 +00001989 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattnerbce61352008-07-26 00:20:22 +00001990 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
1991 // but we support it.
Chris Lattner3bd934a2008-07-26 01:18:38 +00001992 if (DS.hasTypeSpecifier() || !getLang().ObjC1)
Chris Lattnerbce61352008-07-26 00:20:22 +00001993 goto DoneWithDeclSpec;
Mike Stump1eb44332009-09-09 15:08:12 +00001994
Douglas Gregor46f936e2010-11-19 17:10:50 +00001995 if (!ParseObjCProtocolQualifiers(DS))
1996 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
1997 << FixItHint::CreateInsertion(Loc, "id")
1998 << SourceRange(Loc, DS.getSourceRange().getEnd());
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001999
2000 // Need to support trailing type qualifiers (e.g. "id<p> const").
2001 // If a type specifier follows, it will be diagnosed elsewhere.
2002 continue;
Reid Spencer5f016e22007-07-11 17:01:13 +00002003 }
John McCallfec54012009-08-03 20:12:06 +00002004 // If the specifier wasn't legal, issue a diagnostic.
Reid Spencer5f016e22007-07-11 17:01:13 +00002005 if (isInvalid) {
2006 assert(PrevSpec && "Method did not return previous specifier!");
John McCallfec54012009-08-03 20:12:06 +00002007 assert(DiagID);
Douglas Gregorae2fb142010-08-23 14:34:43 +00002008
2009 if (DiagID == diag::ext_duplicate_declspec)
2010 Diag(Tok, DiagID)
2011 << PrevSpec << FixItHint::CreateRemoval(Tok.getLocation());
2012 else
2013 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00002014 }
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002015
Chris Lattner81c018d2008-03-13 06:29:04 +00002016 DS.SetRangeEnd(Tok.getLocation());
Fariborz Jahaniane106a0b2011-04-19 21:42:37 +00002017 if (DiagID != diag::err_bool_redeclaration)
2018 ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00002019 }
2020}
Douglas Gregoradcac882008-12-01 23:54:00 +00002021
Chris Lattner7a0ab5f2009-01-06 06:59:53 +00002022/// ParseOptionalTypeSpecifier - Try to parse a single type-specifier. We
Douglas Gregor12e083c2008-11-07 15:42:26 +00002023/// primarily follow the C++ grammar with additions for C99 and GNU,
2024/// which together subsume the C grammar. Note that the C++
2025/// type-specifier also includes the C type-qualifier (for const,
2026/// volatile, and C99 restrict). Returns true if a type-specifier was
2027/// found (and parsed), false otherwise.
2028///
2029/// type-specifier: [C++ 7.1.5]
2030/// simple-type-specifier
2031/// class-specifier
2032/// enum-specifier
2033/// elaborated-type-specifier [TODO]
2034/// cv-qualifier
2035///
2036/// cv-qualifier: [C++ 7.1.5.1]
2037/// 'const'
2038/// 'volatile'
2039/// [C99] 'restrict'
2040///
2041/// simple-type-specifier: [ C++ 7.1.5.2]
2042/// '::'[opt] nested-name-specifier[opt] type-name [TODO]
2043/// '::'[opt] nested-name-specifier 'template' template-id [TODO]
2044/// 'char'
2045/// 'wchar_t'
2046/// 'bool'
2047/// 'short'
2048/// 'int'
2049/// 'long'
2050/// 'signed'
2051/// 'unsigned'
2052/// 'float'
2053/// 'double'
2054/// 'void'
2055/// [C99] '_Bool'
2056/// [C99] '_Complex'
2057/// [C99] '_Imaginary' // Removed in TC2?
2058/// [GNU] '_Decimal32'
2059/// [GNU] '_Decimal64'
2060/// [GNU] '_Decimal128'
2061/// [GNU] typeof-specifier
2062/// [OBJC] class-name objc-protocol-refs[opt] [TODO]
2063/// [OBJC] typedef-name objc-protocol-refs[opt] [TODO]
Anders Carlsson6fd634f2009-06-24 17:47:40 +00002064/// [C++0x] 'decltype' ( expression )
John Thompson82287d12010-02-05 00:12:22 +00002065/// [AltiVec] '__vector'
John McCallfec54012009-08-03 20:12:06 +00002066bool Parser::ParseOptionalTypeSpecifier(DeclSpec &DS, bool& isInvalid,
Chris Lattner7a0ab5f2009-01-06 06:59:53 +00002067 const char *&PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00002068 unsigned &DiagID,
Sebastian Redld9bafa72010-02-03 21:21:43 +00002069 const ParsedTemplateInfo &TemplateInfo,
2070 bool SuppressDeclarations) {
Douglas Gregor12e083c2008-11-07 15:42:26 +00002071 SourceLocation Loc = Tok.getLocation();
2072
2073 switch (Tok.getKind()) {
Chris Lattner166a8fc2009-01-04 23:41:41 +00002074 case tok::identifier: // foo::bar
Douglas Gregorc0b39642010-04-15 23:40:53 +00002075 // If we already have a type specifier, this identifier is not a type.
2076 if (DS.getTypeSpecType() != DeclSpec::TST_unspecified ||
2077 DS.getTypeSpecWidth() != DeclSpec::TSW_unspecified ||
2078 DS.getTypeSpecSign() != DeclSpec::TSS_unspecified)
2079 return false;
John Thompson82287d12010-02-05 00:12:22 +00002080 // Check for need to substitute AltiVec keyword tokens.
2081 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
2082 break;
2083 // Fall through.
Douglas Gregord57959a2009-03-27 23:10:48 +00002084 case tok::kw_typename: // typename foo::bar
Chris Lattner166a8fc2009-01-04 23:41:41 +00002085 // Annotate typenames and C++ scope specifiers. If we get one, just
2086 // recurse to handle whatever we get.
2087 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00002088 return true;
2089 if (Tok.is(tok::identifier))
2090 return false;
2091 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
2092 TemplateInfo, SuppressDeclarations);
Chris Lattner166a8fc2009-01-04 23:41:41 +00002093 case tok::coloncolon: // ::foo::bar
2094 if (NextToken().is(tok::kw_new) || // ::new
2095 NextToken().is(tok::kw_delete)) // ::delete
2096 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002097
Chris Lattner166a8fc2009-01-04 23:41:41 +00002098 // Annotate typenames and C++ scope specifiers. If we get one, just
2099 // recurse to handle whatever we get.
2100 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00002101 return true;
2102 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
2103 TemplateInfo, SuppressDeclarations);
Mike Stump1eb44332009-09-09 15:08:12 +00002104
Douglas Gregor12e083c2008-11-07 15:42:26 +00002105 // simple-type-specifier:
Chris Lattnerb31757b2009-01-06 05:06:21 +00002106 case tok::annot_typename: {
John McCallb3d87482010-08-24 05:47:05 +00002107 if (ParsedType T = getTypeAnnotation(Tok)) {
Nico Weber253e80b2010-11-22 10:30:56 +00002108 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename,
2109 Tok.getAnnotationEndLoc(), PrevSpec,
John McCallb3d87482010-08-24 05:47:05 +00002110 DiagID, T);
2111 } else
Douglas Gregor31a19b62009-04-01 21:51:26 +00002112 DS.SetTypeSpecError();
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002113 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
2114 ConsumeToken(); // The typename
Mike Stump1eb44332009-09-09 15:08:12 +00002115
Douglas Gregor12e083c2008-11-07 15:42:26 +00002116 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
2117 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
2118 // Objective-C interface. If we don't have Objective-C or a '<', this is
2119 // just a normal reference to a typedef name.
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00002120 if (Tok.is(tok::less) && getLang().ObjC1)
2121 ParseObjCProtocolQualifiers(DS);
2122
Douglas Gregor12e083c2008-11-07 15:42:26 +00002123 return true;
2124 }
2125
2126 case tok::kw_short:
John McCallfec54012009-08-03 20:12:06 +00002127 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002128 break;
2129 case tok::kw_long:
2130 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCallfec54012009-08-03 20:12:06 +00002131 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
2132 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002133 else
John McCallfec54012009-08-03 20:12:06 +00002134 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2135 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002136 break;
Francois Pichet338d7f72011-04-28 01:59:37 +00002137 case tok::kw___int64:
2138 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2139 DiagID);
2140 break;
Douglas Gregor12e083c2008-11-07 15:42:26 +00002141 case tok::kw_signed:
John McCallfec54012009-08-03 20:12:06 +00002142 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002143 break;
2144 case tok::kw_unsigned:
John McCallfec54012009-08-03 20:12:06 +00002145 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
2146 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002147 break;
2148 case tok::kw__Complex:
John McCallfec54012009-08-03 20:12:06 +00002149 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
2150 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002151 break;
2152 case tok::kw__Imaginary:
John McCallfec54012009-08-03 20:12:06 +00002153 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
2154 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002155 break;
2156 case tok::kw_void:
John McCallfec54012009-08-03 20:12:06 +00002157 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002158 break;
2159 case tok::kw_char:
John McCallfec54012009-08-03 20:12:06 +00002160 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002161 break;
2162 case tok::kw_int:
John McCallfec54012009-08-03 20:12:06 +00002163 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002164 break;
2165 case tok::kw_float:
John McCallfec54012009-08-03 20:12:06 +00002166 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002167 break;
2168 case tok::kw_double:
John McCallfec54012009-08-03 20:12:06 +00002169 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002170 break;
2171 case tok::kw_wchar_t:
John McCallfec54012009-08-03 20:12:06 +00002172 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002173 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002174 case tok::kw_char16_t:
John McCallfec54012009-08-03 20:12:06 +00002175 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002176 break;
2177 case tok::kw_char32_t:
John McCallfec54012009-08-03 20:12:06 +00002178 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002179 break;
Douglas Gregor12e083c2008-11-07 15:42:26 +00002180 case tok::kw_bool:
2181 case tok::kw__Bool:
John McCallfec54012009-08-03 20:12:06 +00002182 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002183 break;
2184 case tok::kw__Decimal32:
John McCallfec54012009-08-03 20:12:06 +00002185 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
2186 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002187 break;
2188 case tok::kw__Decimal64:
John McCallfec54012009-08-03 20:12:06 +00002189 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
2190 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002191 break;
2192 case tok::kw__Decimal128:
John McCallfec54012009-08-03 20:12:06 +00002193 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
2194 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002195 break;
John Thompson82287d12010-02-05 00:12:22 +00002196 case tok::kw___vector:
2197 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
2198 break;
2199 case tok::kw___pixel:
2200 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
2201 break;
2202
Douglas Gregor12e083c2008-11-07 15:42:26 +00002203 // class-specifier:
2204 case tok::kw_class:
2205 case tok::kw_struct:
Chris Lattner4c97d762009-04-12 21:49:30 +00002206 case tok::kw_union: {
2207 tok::TokenKind Kind = Tok.getKind();
2208 ConsumeToken();
Sebastian Redld9bafa72010-02-03 21:21:43 +00002209 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS_none,
2210 SuppressDeclarations);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002211 return true;
Chris Lattner4c97d762009-04-12 21:49:30 +00002212 }
Douglas Gregor12e083c2008-11-07 15:42:26 +00002213
2214 // enum-specifier:
2215 case tok::kw_enum:
Chris Lattner4c97d762009-04-12 21:49:30 +00002216 ConsumeToken();
Douglas Gregor9b9edd62010-03-02 17:53:14 +00002217 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS_none);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002218 return true;
2219
2220 // cv-qualifier:
2221 case tok::kw_const:
2222 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00002223 DiagID, getLang());
Douglas Gregor12e083c2008-11-07 15:42:26 +00002224 break;
2225 case tok::kw_volatile:
2226 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00002227 DiagID, getLang());
Douglas Gregor12e083c2008-11-07 15:42:26 +00002228 break;
2229 case tok::kw_restrict:
2230 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00002231 DiagID, getLang());
Douglas Gregor12e083c2008-11-07 15:42:26 +00002232 break;
2233
2234 // GNU typeof support.
2235 case tok::kw_typeof:
2236 ParseTypeofSpecifier(DS);
2237 return true;
2238
Anders Carlsson6fd634f2009-06-24 17:47:40 +00002239 // C++0x decltype support.
2240 case tok::kw_decltype:
2241 ParseDecltypeSpecifier(DS);
2242 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00002243
Sean Huntdb5d44b2011-05-19 05:37:45 +00002244 // C++0x type traits support.
2245 case tok::kw___underlying_type:
2246 ParseUnderlyingTypeSpecifier(DS);
2247 return true;
2248
Peter Collingbourne207f4d82011-03-18 22:38:29 +00002249 // OpenCL qualifiers:
2250 case tok::kw_private:
2251 if (!getLang().OpenCL)
2252 return false;
2253 case tok::kw___private:
2254 case tok::kw___global:
2255 case tok::kw___local:
2256 case tok::kw___constant:
2257 case tok::kw___read_only:
2258 case tok::kw___write_only:
2259 case tok::kw___read_write:
2260 ParseOpenCLQualifiers(DS);
2261 break;
2262
Anders Carlsson0b7f7892009-06-26 23:44:14 +00002263 // C++0x auto support.
2264 case tok::kw_auto:
2265 if (!getLang().CPlusPlus0x)
2266 return false;
2267
John McCallfec54012009-08-03 20:12:06 +00002268 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec, DiagID);
Anders Carlsson0b7f7892009-06-26 23:44:14 +00002269 break;
Dawn Perchik52fc3142010-09-03 01:29:35 +00002270
Eli Friedman290eeb02009-06-08 23:27:34 +00002271 case tok::kw___ptr64:
2272 case tok::kw___w64:
Steve Naroff239f0732008-12-25 14:16:32 +00002273 case tok::kw___cdecl:
2274 case tok::kw___stdcall:
2275 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00002276 case tok::kw___thiscall:
John McCall7f040a92010-12-24 02:08:15 +00002277 ParseMicrosoftTypeAttributes(DS.getAttributes());
Chris Lattner837acd02009-01-21 19:19:26 +00002278 return true;
Steve Naroff239f0732008-12-25 14:16:32 +00002279
Dawn Perchik52fc3142010-09-03 01:29:35 +00002280 case tok::kw___pascal:
John McCall7f040a92010-12-24 02:08:15 +00002281 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik52fc3142010-09-03 01:29:35 +00002282 return true;
2283
Douglas Gregor12e083c2008-11-07 15:42:26 +00002284 default:
2285 // Not a type-specifier; do nothing.
2286 return false;
2287 }
2288
2289 // If the specifier combination wasn't legal, issue a diagnostic.
2290 if (isInvalid) {
2291 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner1ab3b962008-11-18 07:48:38 +00002292 // Pick between error or extwarn.
Chris Lattner1ab3b962008-11-18 07:48:38 +00002293 Diag(Tok, DiagID) << PrevSpec;
Douglas Gregor12e083c2008-11-07 15:42:26 +00002294 }
2295 DS.SetRangeEnd(Tok.getLocation());
2296 ConsumeToken(); // whatever we parsed above.
2297 return true;
2298}
Reid Spencer5f016e22007-07-11 17:01:13 +00002299
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002300/// ParseStructDeclaration - Parse a struct declaration without the terminating
2301/// semicolon.
2302///
Reid Spencer5f016e22007-07-11 17:01:13 +00002303/// struct-declaration:
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002304/// specifier-qualifier-list struct-declarator-list
Reid Spencer5f016e22007-07-11 17:01:13 +00002305/// [GNU] __extension__ struct-declaration
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002306/// [GNU] specifier-qualifier-list
Reid Spencer5f016e22007-07-11 17:01:13 +00002307/// struct-declarator-list:
2308/// struct-declarator
2309/// struct-declarator-list ',' struct-declarator
2310/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
2311/// struct-declarator:
2312/// declarator
2313/// [GNU] declarator attributes[opt]
2314/// declarator[opt] ':' constant-expression
2315/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
2316///
Chris Lattnere1359422008-04-10 06:46:29 +00002317void Parser::
John McCallbdd563e2009-11-03 02:38:08 +00002318ParseStructDeclaration(DeclSpec &DS, FieldCallback &Fields) {
Chris Lattnerc46d1a12008-10-20 06:45:43 +00002319 if (Tok.is(tok::kw___extension__)) {
2320 // __extension__ silences extension warnings in the subexpression.
2321 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroff28a7ca82007-08-20 22:28:22 +00002322 ConsumeToken();
Chris Lattnerc46d1a12008-10-20 06:45:43 +00002323 return ParseStructDeclaration(DS, Fields);
2324 }
Mike Stump1eb44332009-09-09 15:08:12 +00002325
Steve Naroff28a7ca82007-08-20 22:28:22 +00002326 // Parse the common specifier-qualifiers-list piece.
Steve Naroff28a7ca82007-08-20 22:28:22 +00002327 ParseSpecifierQualifierList(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00002328
Douglas Gregor4920f1f2009-01-12 22:49:06 +00002329 // If there are no declarators, this is a free-standing declaration
2330 // specifier. Let the actions module cope with it.
Chris Lattner04d66662007-10-09 17:33:22 +00002331 if (Tok.is(tok::semi)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00002332 Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none, DS);
Steve Naroff28a7ca82007-08-20 22:28:22 +00002333 return;
2334 }
2335
2336 // Read struct-declarators until we find the semicolon.
John McCallbdd563e2009-11-03 02:38:08 +00002337 bool FirstDeclarator = true;
Steve Naroff28a7ca82007-08-20 22:28:22 +00002338 while (1) {
John McCall54abf7d2009-11-04 02:18:39 +00002339 ParsingDeclRAIIObject PD(*this);
John McCallbdd563e2009-11-03 02:38:08 +00002340 FieldDeclarator DeclaratorInfo(DS);
2341
2342 // Attributes are only allowed here on successive declarators.
John McCall7f040a92010-12-24 02:08:15 +00002343 if (!FirstDeclarator)
2344 MaybeParseGNUAttributes(DeclaratorInfo.D);
Mike Stump1eb44332009-09-09 15:08:12 +00002345
Steve Naroff28a7ca82007-08-20 22:28:22 +00002346 /// struct-declarator: declarator
2347 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattnera1efc8c2009-12-10 01:59:24 +00002348 if (Tok.isNot(tok::colon)) {
2349 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
2350 ColonProtectionRAIIObject X(*this);
Chris Lattnere1359422008-04-10 06:46:29 +00002351 ParseDeclarator(DeclaratorInfo.D);
Chris Lattnera1efc8c2009-12-10 01:59:24 +00002352 }
Mike Stump1eb44332009-09-09 15:08:12 +00002353
Chris Lattner04d66662007-10-09 17:33:22 +00002354 if (Tok.is(tok::colon)) {
Steve Naroff28a7ca82007-08-20 22:28:22 +00002355 ConsumeToken();
John McCall60d7b3a2010-08-24 06:29:42 +00002356 ExprResult Res(ParseConstantExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002357 if (Res.isInvalid())
Steve Naroff28a7ca82007-08-20 22:28:22 +00002358 SkipUntil(tok::semi, true, true);
Chris Lattner60b1e3e2008-04-10 06:15:14 +00002359 else
Sebastian Redleffa8d12008-12-10 00:02:53 +00002360 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroff28a7ca82007-08-20 22:28:22 +00002361 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00002362
Steve Naroff28a7ca82007-08-20 22:28:22 +00002363 // If attributes exist after the declarator, parse them.
John McCall7f040a92010-12-24 02:08:15 +00002364 MaybeParseGNUAttributes(DeclaratorInfo.D);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002365
John McCallbdd563e2009-11-03 02:38:08 +00002366 // We're done with this declarator; invoke the callback.
John McCalld226f652010-08-21 09:40:31 +00002367 Decl *D = Fields.invoke(DeclaratorInfo);
John McCall54abf7d2009-11-04 02:18:39 +00002368 PD.complete(D);
John McCallbdd563e2009-11-03 02:38:08 +00002369
Steve Naroff28a7ca82007-08-20 22:28:22 +00002370 // If we don't have a comma, it is either the end of the list (a ';')
2371 // or an error, bail out.
Chris Lattner04d66662007-10-09 17:33:22 +00002372 if (Tok.isNot(tok::comma))
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002373 return;
Sebastian Redlab197ba2009-02-09 18:23:29 +00002374
Steve Naroff28a7ca82007-08-20 22:28:22 +00002375 // Consume the comma.
2376 ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00002377
John McCallbdd563e2009-11-03 02:38:08 +00002378 FirstDeclarator = false;
Steve Naroff28a7ca82007-08-20 22:28:22 +00002379 }
Steve Naroff28a7ca82007-08-20 22:28:22 +00002380}
2381
2382/// ParseStructUnionBody
2383/// struct-contents:
2384/// struct-declaration-list
2385/// [EXT] empty
2386/// [GNU] "struct-declaration-list" without terminatoring ';'
2387/// struct-declaration-list:
2388/// struct-declaration
2389/// struct-declaration-list struct-declaration
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002390/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroff28a7ca82007-08-20 22:28:22 +00002391///
Reid Spencer5f016e22007-07-11 17:01:13 +00002392void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
John McCalld226f652010-08-21 09:40:31 +00002393 unsigned TagType, Decl *TagDecl) {
John McCallf312b1e2010-08-26 23:41:50 +00002394 PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc,
2395 "parsing struct/union body");
Mike Stump1eb44332009-09-09 15:08:12 +00002396
Reid Spencer5f016e22007-07-11 17:01:13 +00002397 SourceLocation LBraceLoc = ConsumeBrace();
Mike Stump1eb44332009-09-09 15:08:12 +00002398
Douglas Gregor3218c4b2009-01-09 22:42:13 +00002399 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
Douglas Gregor23c94db2010-07-02 17:43:08 +00002400 Actions.ActOnTagStartDefinition(getCurScope(), TagDecl);
Douglas Gregor72de6672009-01-08 20:45:30 +00002401
Reid Spencer5f016e22007-07-11 17:01:13 +00002402 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
2403 // C++.
Douglas Gregore37ac4f2008-04-13 21:30:24 +00002404 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Douglas Gregor03332962010-07-29 14:29:34 +00002405 Diag(Tok, diag::ext_empty_struct_union)
2406 << (TagType == TST_union);
Reid Spencer5f016e22007-07-11 17:01:13 +00002407
Chris Lattner5f9e2722011-07-23 10:55:15 +00002408 SmallVector<Decl *, 32> FieldDecls;
Chris Lattnere1359422008-04-10 06:46:29 +00002409
Reid Spencer5f016e22007-07-11 17:01:13 +00002410 // While we still have something to read, read the declarations in the struct.
Chris Lattner04d66662007-10-09 17:33:22 +00002411 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002412 // Each iteration of this loop reads one struct-declaration.
Mike Stump1eb44332009-09-09 15:08:12 +00002413
Reid Spencer5f016e22007-07-11 17:01:13 +00002414 // Check for extraneous top-level semicolon.
Chris Lattner04d66662007-10-09 17:33:22 +00002415 if (Tok.is(tok::semi)) {
Douglas Gregor9b3064b2009-04-01 22:41:11 +00002416 Diag(Tok, diag::ext_extra_struct_semi)
Douglas Gregorf13ca062010-06-16 23:08:59 +00002417 << DeclSpec::getSpecifierName((DeclSpec::TST)TagType)
Douglas Gregor849b2432010-03-31 17:46:05 +00002418 << FixItHint::CreateRemoval(Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00002419 ConsumeToken();
2420 continue;
2421 }
Chris Lattnere1359422008-04-10 06:46:29 +00002422
2423 // Parse all the comma separated declarators.
John McCall0b7e6782011-03-24 11:26:52 +00002424 DeclSpec DS(AttrFactory);
Mike Stump1eb44332009-09-09 15:08:12 +00002425
John McCallbdd563e2009-11-03 02:38:08 +00002426 if (!Tok.is(tok::at)) {
2427 struct CFieldCallback : FieldCallback {
2428 Parser &P;
John McCalld226f652010-08-21 09:40:31 +00002429 Decl *TagDecl;
Chris Lattner5f9e2722011-07-23 10:55:15 +00002430 SmallVectorImpl<Decl *> &FieldDecls;
John McCallbdd563e2009-11-03 02:38:08 +00002431
John McCalld226f652010-08-21 09:40:31 +00002432 CFieldCallback(Parser &P, Decl *TagDecl,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002433 SmallVectorImpl<Decl *> &FieldDecls) :
John McCallbdd563e2009-11-03 02:38:08 +00002434 P(P), TagDecl(TagDecl), FieldDecls(FieldDecls) {}
2435
John McCalld226f652010-08-21 09:40:31 +00002436 virtual Decl *invoke(FieldDeclarator &FD) {
John McCallbdd563e2009-11-03 02:38:08 +00002437 // Install the declarator into the current TagDecl.
John McCalld226f652010-08-21 09:40:31 +00002438 Decl *Field = P.Actions.ActOnField(P.getCurScope(), TagDecl,
John McCall4ba39712009-11-03 21:13:47 +00002439 FD.D.getDeclSpec().getSourceRange().getBegin(),
2440 FD.D, FD.BitfieldSize);
John McCallbdd563e2009-11-03 02:38:08 +00002441 FieldDecls.push_back(Field);
2442 return Field;
Douglas Gregor91a28862009-08-26 14:27:30 +00002443 }
John McCallbdd563e2009-11-03 02:38:08 +00002444 } Callback(*this, TagDecl, FieldDecls);
2445
2446 ParseStructDeclaration(DS, Callback);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002447 } else { // Handle @defs
2448 ConsumeToken();
2449 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
2450 Diag(Tok, diag::err_unexpected_at);
Chris Lattner3e156ad2010-02-02 00:37:27 +00002451 SkipUntil(tok::semi, true);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002452 continue;
2453 }
2454 ConsumeToken();
2455 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
2456 if (!Tok.is(tok::identifier)) {
2457 Diag(Tok, diag::err_expected_ident);
Chris Lattner3e156ad2010-02-02 00:37:27 +00002458 SkipUntil(tok::semi, true);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002459 continue;
2460 }
Chris Lattner5f9e2722011-07-23 10:55:15 +00002461 SmallVector<Decl *, 16> Fields;
Douglas Gregor23c94db2010-07-02 17:43:08 +00002462 Actions.ActOnDefs(getCurScope(), TagDecl, Tok.getLocation(),
Douglas Gregor44b43212008-12-11 16:49:14 +00002463 Tok.getIdentifierInfo(), Fields);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002464 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
2465 ConsumeToken();
2466 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
Mike Stump1eb44332009-09-09 15:08:12 +00002467 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002468
Chris Lattner04d66662007-10-09 17:33:22 +00002469 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002470 ConsumeToken();
Chris Lattner04d66662007-10-09 17:33:22 +00002471 } else if (Tok.is(tok::r_brace)) {
Chris Lattner3e156ad2010-02-02 00:37:27 +00002472 ExpectAndConsume(tok::semi, diag::ext_expected_semi_decl_list);
Reid Spencer5f016e22007-07-11 17:01:13 +00002473 break;
2474 } else {
Chris Lattner3e156ad2010-02-02 00:37:27 +00002475 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
2476 // Skip to end of block or statement to avoid ext-warning on extra ';'.
Reid Spencer5f016e22007-07-11 17:01:13 +00002477 SkipUntil(tok::r_brace, true, true);
Chris Lattner3e156ad2010-02-02 00:37:27 +00002478 // If we stopped at a ';', eat it.
2479 if (Tok.is(tok::semi)) ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00002480 }
2481 }
Mike Stump1eb44332009-09-09 15:08:12 +00002482
Steve Naroff60fccee2007-10-29 21:38:07 +00002483 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00002484
John McCall0b7e6782011-03-24 11:26:52 +00002485 ParsedAttributes attrs(AttrFactory);
Reid Spencer5f016e22007-07-11 17:01:13 +00002486 // If attributes exist after struct contents, parse them.
John McCall7f040a92010-12-24 02:08:15 +00002487 MaybeParseGNUAttributes(attrs);
Daniel Dunbar1bfe1c22008-10-03 02:03:53 +00002488
Douglas Gregor23c94db2010-07-02 17:43:08 +00002489 Actions.ActOnFields(getCurScope(),
Jay Foadbeaaccd2009-05-21 09:52:38 +00002490 RecordLoc, TagDecl, FieldDecls.data(), FieldDecls.size(),
Daniel Dunbar1bfe1c22008-10-03 02:03:53 +00002491 LBraceLoc, RBraceLoc,
John McCall7f040a92010-12-24 02:08:15 +00002492 attrs.getList());
Douglas Gregor72de6672009-01-08 20:45:30 +00002493 StructScope.Exit();
Douglas Gregor23c94db2010-07-02 17:43:08 +00002494 Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl, RBraceLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00002495}
2496
Reid Spencer5f016e22007-07-11 17:01:13 +00002497/// ParseEnumSpecifier
2498/// enum-specifier: [C99 6.7.2.2]
2499/// 'enum' identifier[opt] '{' enumerator-list '}'
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002500///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Reid Spencer5f016e22007-07-11 17:01:13 +00002501/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
2502/// '}' attributes[opt]
2503/// 'enum' identifier
2504/// [GNU] 'enum' attributes[opt] identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002505///
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002506/// [C++0x] enum-head '{' enumerator-list[opt] '}'
2507/// [C++0x] enum-head '{' enumerator-list ',' '}'
2508///
2509/// enum-head: [C++0x]
2510/// enum-key attributes[opt] identifier[opt] enum-base[opt]
2511/// enum-key attributes[opt] nested-name-specifier identifier enum-base[opt]
2512///
2513/// enum-key: [C++0x]
2514/// 'enum'
2515/// 'enum' 'class'
2516/// 'enum' 'struct'
2517///
2518/// enum-base: [C++0x]
2519/// ':' type-specifier-seq
2520///
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002521/// [C++] elaborated-type-specifier:
2522/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
2523///
Chris Lattner4c97d762009-04-12 21:49:30 +00002524void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregor9b9edd62010-03-02 17:53:14 +00002525 const ParsedTemplateInfo &TemplateInfo,
Chris Lattner4c97d762009-04-12 21:49:30 +00002526 AccessSpecifier AS) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002527 // Parse the tag portion of this.
Douglas Gregor374929f2009-09-18 15:37:17 +00002528 if (Tok.is(tok::code_completion)) {
2529 // Code completion for an enum name.
Douglas Gregor23c94db2010-07-02 17:43:08 +00002530 Actions.CodeCompleteTag(getCurScope(), DeclSpec::TST_enum);
Douglas Gregordc845342010-05-25 05:58:43 +00002531 ConsumeCodeCompletionToken();
Douglas Gregor374929f2009-09-18 15:37:17 +00002532 }
John McCall57c13002011-07-06 05:58:41 +00002533
2534 bool IsScopedEnum = false;
2535 bool IsScopedUsingClassTag = false;
2536
2537 if (getLang().CPlusPlus0x &&
2538 (Tok.is(tok::kw_class) || Tok.is(tok::kw_struct))) {
2539 IsScopedEnum = true;
2540 IsScopedUsingClassTag = Tok.is(tok::kw_class);
2541 ConsumeToken();
2542 }
Douglas Gregor374929f2009-09-18 15:37:17 +00002543
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002544 // If attributes exist after tag, parse them.
John McCall0b7e6782011-03-24 11:26:52 +00002545 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00002546 MaybeParseGNUAttributes(attrs);
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002547
John McCall57c13002011-07-06 05:58:41 +00002548 bool AllowFixedUnderlyingType = getLang().CPlusPlus0x || getLang().Microsoft;
2549
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00002550 CXXScopeSpec &SS = DS.getTypeSpecScope();
John McCall9ba61662010-02-26 08:45:28 +00002551 if (getLang().CPlusPlus) {
John McCall57c13002011-07-06 05:58:41 +00002552 // "enum foo : bar;" is not a potential typo for "enum foo::bar;"
2553 // if a fixed underlying type is allowed.
2554 ColonProtectionRAIIObject X(*this, AllowFixedUnderlyingType);
2555
John McCallb3d87482010-08-24 05:47:05 +00002556 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), false))
John McCall9ba61662010-02-26 08:45:28 +00002557 return;
2558
2559 if (SS.isSet() && Tok.isNot(tok::identifier)) {
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002560 Diag(Tok, diag::err_expected_ident);
2561 if (Tok.isNot(tok::l_brace)) {
2562 // Has no name and is not a definition.
2563 // Skip the rest of this declarator, up until the comma or semicolon.
2564 SkipUntil(tok::comma, true);
2565 return;
2566 }
2567 }
2568 }
Mike Stump1eb44332009-09-09 15:08:12 +00002569
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002570 // Must have either 'enum name' or 'enum {...}'.
Douglas Gregorb9075602011-02-22 02:55:24 +00002571 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace) &&
2572 (AllowFixedUnderlyingType && Tok.isNot(tok::colon))) {
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002573 Diag(Tok, diag::err_expected_ident_lbrace);
Mike Stump1eb44332009-09-09 15:08:12 +00002574
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002575 // Skip the rest of this declarator, up until the comma or semicolon.
2576 SkipUntil(tok::comma, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00002577 return;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002578 }
Mike Stump1eb44332009-09-09 15:08:12 +00002579
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002580 // If an identifier is present, consume and remember it.
2581 IdentifierInfo *Name = 0;
2582 SourceLocation NameLoc;
2583 if (Tok.is(tok::identifier)) {
2584 Name = Tok.getIdentifierInfo();
2585 NameLoc = ConsumeToken();
2586 }
Mike Stump1eb44332009-09-09 15:08:12 +00002587
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002588 if (!Name && IsScopedEnum) {
2589 // C++0x 7.2p2: The optional identifier shall not be omitted in the
2590 // declaration of a scoped enumeration.
2591 Diag(Tok, diag::err_scoped_enum_missing_identifier);
2592 IsScopedEnum = false;
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00002593 IsScopedUsingClassTag = false;
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002594 }
2595
2596 TypeResult BaseType;
2597
Douglas Gregora61b3e72010-12-01 17:42:47 +00002598 // Parse the fixed underlying type.
Douglas Gregorb9075602011-02-22 02:55:24 +00002599 if (AllowFixedUnderlyingType && Tok.is(tok::colon)) {
Douglas Gregora61b3e72010-12-01 17:42:47 +00002600 bool PossibleBitfield = false;
2601 if (getCurScope()->getFlags() & Scope::ClassScope) {
2602 // If we're in class scope, this can either be an enum declaration with
2603 // an underlying type, or a declaration of a bitfield member. We try to
2604 // use a simple disambiguation scheme first to catch the common cases
2605 // (integer literal, sizeof); if it's still ambiguous, we then consider
2606 // anything that's a simple-type-specifier followed by '(' as an
2607 // expression. This suffices because function types are not valid
2608 // underlying types anyway.
2609 TPResult TPR = isExpressionOrTypeSpecifierSimple(NextToken().getKind());
2610 // If the next token starts an expression, we know we're parsing a
2611 // bit-field. This is the common case.
2612 if (TPR == TPResult::True())
2613 PossibleBitfield = true;
2614 // If the next token starts a type-specifier-seq, it may be either a
2615 // a fixed underlying type or the start of a function-style cast in C++;
2616 // lookahead one more token to see if it's obvious that we have a
2617 // fixed underlying type.
2618 else if (TPR == TPResult::False() &&
2619 GetLookAheadToken(2).getKind() == tok::semi) {
2620 // Consume the ':'.
2621 ConsumeToken();
2622 } else {
2623 // We have the start of a type-specifier-seq, so we have to perform
2624 // tentative parsing to determine whether we have an expression or a
2625 // type.
2626 TentativeParsingAction TPA(*this);
2627
2628 // Consume the ':'.
2629 ConsumeToken();
2630
Douglas Gregor86f208c2011-02-22 20:32:04 +00002631 if ((getLang().CPlusPlus &&
2632 isCXXDeclarationSpecifier() != TPResult::True()) ||
2633 (!getLang().CPlusPlus && !isDeclarationSpecifier(true))) {
Douglas Gregora61b3e72010-12-01 17:42:47 +00002634 // We'll parse this as a bitfield later.
2635 PossibleBitfield = true;
2636 TPA.Revert();
2637 } else {
2638 // We have a type-specifier-seq.
2639 TPA.Commit();
2640 }
2641 }
2642 } else {
2643 // Consume the ':'.
2644 ConsumeToken();
2645 }
2646
2647 if (!PossibleBitfield) {
2648 SourceRange Range;
2649 BaseType = ParseTypeName(&Range);
Douglas Gregor86f208c2011-02-22 20:32:04 +00002650
2651 if (!getLang().CPlusPlus0x)
2652 Diag(StartLoc, diag::ext_ms_enum_fixed_underlying_type)
2653 << Range;
Douglas Gregora61b3e72010-12-01 17:42:47 +00002654 }
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002655 }
2656
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002657 // There are three options here. If we have 'enum foo;', then this is a
2658 // forward declaration. If we have 'enum foo {...' then this is a
2659 // definition. Otherwise we have something like 'enum foo xyz', a reference.
2660 //
2661 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
2662 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
2663 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
2664 //
John McCallf312b1e2010-08-26 23:41:50 +00002665 Sema::TagUseKind TUK;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002666 if (Tok.is(tok::l_brace))
John McCallf312b1e2010-08-26 23:41:50 +00002667 TUK = Sema::TUK_Definition;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002668 else if (Tok.is(tok::semi))
John McCallf312b1e2010-08-26 23:41:50 +00002669 TUK = Sema::TUK_Declaration;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002670 else
John McCallf312b1e2010-08-26 23:41:50 +00002671 TUK = Sema::TUK_Reference;
Douglas Gregor8fc6d232010-05-03 17:48:54 +00002672
2673 // enums cannot be templates, although they can be referenced from a
2674 // template.
2675 if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate &&
John McCallf312b1e2010-08-26 23:41:50 +00002676 TUK != Sema::TUK_Reference) {
Douglas Gregor8fc6d232010-05-03 17:48:54 +00002677 Diag(Tok, diag::err_enum_template);
2678
2679 // Skip the rest of this declarator, up until the comma or semicolon.
2680 SkipUntil(tok::comma, true);
2681 return;
2682 }
2683
Douglas Gregorb9075602011-02-22 02:55:24 +00002684 if (!Name && TUK != Sema::TUK_Definition) {
2685 Diag(Tok, diag::err_enumerator_unnamed_no_def);
2686
2687 // Skip the rest of this declarator, up until the comma or semicolon.
2688 SkipUntil(tok::comma, true);
2689 return;
2690 }
2691
Douglas Gregor402abb52009-05-28 23:31:59 +00002692 bool Owned = false;
John McCallc4e70192009-09-11 04:59:25 +00002693 bool IsDependent = false;
Douglas Gregor48c89f42010-04-24 16:38:41 +00002694 const char *PrevSpec = 0;
2695 unsigned DiagID;
John McCalld226f652010-08-21 09:40:31 +00002696 Decl *TagDecl = Actions.ActOnTag(getCurScope(), DeclSpec::TST_enum, TUK,
John McCall7f040a92010-12-24 02:08:15 +00002697 StartLoc, SS, Name, NameLoc, attrs.getList(),
John McCalld226f652010-08-21 09:40:31 +00002698 AS,
John McCallf312b1e2010-08-26 23:41:50 +00002699 MultiTemplateParamsArg(Actions),
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002700 Owned, IsDependent, IsScopedEnum,
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00002701 IsScopedUsingClassTag, BaseType);
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002702
Douglas Gregor48c89f42010-04-24 16:38:41 +00002703 if (IsDependent) {
2704 // This enum has a dependent nested-name-specifier. Handle it as a
2705 // dependent tag.
2706 if (!Name) {
2707 DS.SetTypeSpecError();
2708 Diag(Tok, diag::err_expected_type_name_after_typename);
2709 return;
2710 }
2711
Douglas Gregor23c94db2010-07-02 17:43:08 +00002712 TypeResult Type = Actions.ActOnDependentTag(getCurScope(), DeclSpec::TST_enum,
Douglas Gregor48c89f42010-04-24 16:38:41 +00002713 TUK, SS, Name, StartLoc,
2714 NameLoc);
2715 if (Type.isInvalid()) {
2716 DS.SetTypeSpecError();
2717 return;
2718 }
2719
Abramo Bagnara0daaf322011-03-16 20:16:18 +00002720 if (DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc,
2721 NameLoc.isValid() ? NameLoc : StartLoc,
2722 PrevSpec, DiagID, Type.get()))
Douglas Gregor48c89f42010-04-24 16:38:41 +00002723 Diag(StartLoc, DiagID) << PrevSpec;
2724
2725 return;
2726 }
Mike Stump1eb44332009-09-09 15:08:12 +00002727
John McCalld226f652010-08-21 09:40:31 +00002728 if (!TagDecl) {
Douglas Gregor48c89f42010-04-24 16:38:41 +00002729 // The action failed to produce an enumeration tag. If this is a
2730 // definition, consume the entire definition.
2731 if (Tok.is(tok::l_brace)) {
2732 ConsumeBrace();
2733 SkipUntil(tok::r_brace);
2734 }
2735
2736 DS.SetTypeSpecError();
2737 return;
2738 }
2739
Chris Lattner04d66662007-10-09 17:33:22 +00002740 if (Tok.is(tok::l_brace))
Reid Spencer5f016e22007-07-11 17:01:13 +00002741 ParseEnumBody(StartLoc, TagDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00002742
Abramo Bagnara0daaf322011-03-16 20:16:18 +00002743 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc,
2744 NameLoc.isValid() ? NameLoc : StartLoc,
2745 PrevSpec, DiagID, TagDecl, Owned))
John McCallfec54012009-08-03 20:12:06 +00002746 Diag(StartLoc, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00002747}
2748
2749/// ParseEnumBody - Parse a {} enclosed enumerator-list.
2750/// enumerator-list:
2751/// enumerator
2752/// enumerator-list ',' enumerator
2753/// enumerator:
2754/// enumeration-constant
2755/// enumeration-constant '=' constant-expression
2756/// enumeration-constant:
2757/// identifier
2758///
John McCalld226f652010-08-21 09:40:31 +00002759void Parser::ParseEnumBody(SourceLocation StartLoc, Decl *EnumDecl) {
Douglas Gregor074149e2009-01-05 19:45:36 +00002760 // Enter the scope of the enum body and start the definition.
2761 ParseScope EnumScope(this, Scope::DeclScope);
Douglas Gregor23c94db2010-07-02 17:43:08 +00002762 Actions.ActOnTagStartDefinition(getCurScope(), EnumDecl);
Douglas Gregor074149e2009-01-05 19:45:36 +00002763
Reid Spencer5f016e22007-07-11 17:01:13 +00002764 SourceLocation LBraceLoc = ConsumeBrace();
Mike Stump1eb44332009-09-09 15:08:12 +00002765
Chris Lattner7946dd32007-08-27 17:24:30 +00002766 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
Chris Lattner04d66662007-10-09 17:33:22 +00002767 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Fariborz Jahanian05115522010-05-28 22:23:22 +00002768 Diag(Tok, diag::error_empty_enum);
Mike Stump1eb44332009-09-09 15:08:12 +00002769
Chris Lattner5f9e2722011-07-23 10:55:15 +00002770 SmallVector<Decl *, 32> EnumConstantDecls;
Reid Spencer5f016e22007-07-11 17:01:13 +00002771
John McCalld226f652010-08-21 09:40:31 +00002772 Decl *LastEnumConstDecl = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00002773
Reid Spencer5f016e22007-07-11 17:01:13 +00002774 // Parse the enumerator-list.
Chris Lattner04d66662007-10-09 17:33:22 +00002775 while (Tok.is(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002776 IdentifierInfo *Ident = Tok.getIdentifierInfo();
2777 SourceLocation IdentLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00002778
John McCall5b629aa2010-10-22 23:36:17 +00002779 // If attributes exist after the enumerator, parse them.
John McCall0b7e6782011-03-24 11:26:52 +00002780 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00002781 MaybeParseGNUAttributes(attrs);
John McCall5b629aa2010-10-22 23:36:17 +00002782
Reid Spencer5f016e22007-07-11 17:01:13 +00002783 SourceLocation EqualLoc;
John McCall60d7b3a2010-08-24 06:29:42 +00002784 ExprResult AssignedVal;
Chris Lattner04d66662007-10-09 17:33:22 +00002785 if (Tok.is(tok::equal)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002786 EqualLoc = ConsumeToken();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002787 AssignedVal = ParseConstantExpression();
2788 if (AssignedVal.isInvalid())
Reid Spencer5f016e22007-07-11 17:01:13 +00002789 SkipUntil(tok::comma, tok::r_brace, true, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00002790 }
Mike Stump1eb44332009-09-09 15:08:12 +00002791
Reid Spencer5f016e22007-07-11 17:01:13 +00002792 // Install the enumerator constant into EnumDecl.
John McCalld226f652010-08-21 09:40:31 +00002793 Decl *EnumConstDecl = Actions.ActOnEnumConstant(getCurScope(), EnumDecl,
2794 LastEnumConstDecl,
2795 IdentLoc, Ident,
John McCall7f040a92010-12-24 02:08:15 +00002796 attrs.getList(), EqualLoc,
John McCalld226f652010-08-21 09:40:31 +00002797 AssignedVal.release());
Reid Spencer5f016e22007-07-11 17:01:13 +00002798 EnumConstantDecls.push_back(EnumConstDecl);
2799 LastEnumConstDecl = EnumConstDecl;
Mike Stump1eb44332009-09-09 15:08:12 +00002800
Douglas Gregor751f6922010-09-07 14:51:08 +00002801 if (Tok.is(tok::identifier)) {
2802 // We're missing a comma between enumerators.
2803 SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation);
2804 Diag(Loc, diag::err_enumerator_list_missing_comma)
2805 << FixItHint::CreateInsertion(Loc, ", ");
2806 continue;
2807 }
2808
Chris Lattner04d66662007-10-09 17:33:22 +00002809 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +00002810 break;
2811 SourceLocation CommaLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00002812
2813 if (Tok.isNot(tok::identifier) &&
Douglas Gregor9b3064b2009-04-01 22:41:11 +00002814 !(getLang().C99 || getLang().CPlusPlus0x))
2815 Diag(CommaLoc, diag::ext_enumerator_list_comma)
2816 << getLang().CPlusPlus
Douglas Gregor849b2432010-03-31 17:46:05 +00002817 << FixItHint::CreateRemoval(CommaLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00002818 }
Mike Stump1eb44332009-09-09 15:08:12 +00002819
Reid Spencer5f016e22007-07-11 17:01:13 +00002820 // Eat the }.
Mike Stumpc6e35aa2009-05-16 07:06:02 +00002821 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00002822
Reid Spencer5f016e22007-07-11 17:01:13 +00002823 // If attributes exist after the identifier list, parse them.
John McCall0b7e6782011-03-24 11:26:52 +00002824 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00002825 MaybeParseGNUAttributes(attrs);
Douglas Gregor72de6672009-01-08 20:45:30 +00002826
Edward O'Callaghanfee13812009-08-08 14:36:57 +00002827 Actions.ActOnEnumBody(StartLoc, LBraceLoc, RBraceLoc, EnumDecl,
2828 EnumConstantDecls.data(), EnumConstantDecls.size(),
John McCall7f040a92010-12-24 02:08:15 +00002829 getCurScope(), attrs.getList());
Mike Stump1eb44332009-09-09 15:08:12 +00002830
Douglas Gregor72de6672009-01-08 20:45:30 +00002831 EnumScope.Exit();
Douglas Gregor23c94db2010-07-02 17:43:08 +00002832 Actions.ActOnTagFinishDefinition(getCurScope(), EnumDecl, RBraceLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00002833}
2834
2835/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff5f8aa692008-02-11 23:15:56 +00002836/// start of a type-qualifier-list.
2837bool Parser::isTypeQualifier() const {
2838 switch (Tok.getKind()) {
2839 default: return false;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00002840
2841 // type-qualifier only in OpenCL
2842 case tok::kw_private:
2843 return getLang().OpenCL;
2844
Steve Naroff5f8aa692008-02-11 23:15:56 +00002845 // type-qualifier
2846 case tok::kw_const:
2847 case tok::kw_volatile:
2848 case tok::kw_restrict:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00002849 case tok::kw___private:
2850 case tok::kw___local:
2851 case tok::kw___global:
2852 case tok::kw___constant:
2853 case tok::kw___read_only:
2854 case tok::kw___read_write:
2855 case tok::kw___write_only:
Steve Naroff5f8aa692008-02-11 23:15:56 +00002856 return true;
2857 }
2858}
2859
Chris Lattnerb3a4e432010-02-28 18:18:36 +00002860/// isKnownToBeTypeSpecifier - Return true if we know that the specified token
2861/// is definitely a type-specifier. Return false if it isn't part of a type
2862/// specifier or if we're not sure.
2863bool Parser::isKnownToBeTypeSpecifier(const Token &Tok) const {
2864 switch (Tok.getKind()) {
2865 default: return false;
2866 // type-specifiers
2867 case tok::kw_short:
2868 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00002869 case tok::kw___int64:
Chris Lattnerb3a4e432010-02-28 18:18:36 +00002870 case tok::kw_signed:
2871 case tok::kw_unsigned:
2872 case tok::kw__Complex:
2873 case tok::kw__Imaginary:
2874 case tok::kw_void:
2875 case tok::kw_char:
2876 case tok::kw_wchar_t:
2877 case tok::kw_char16_t:
2878 case tok::kw_char32_t:
2879 case tok::kw_int:
2880 case tok::kw_float:
2881 case tok::kw_double:
2882 case tok::kw_bool:
2883 case tok::kw__Bool:
2884 case tok::kw__Decimal32:
2885 case tok::kw__Decimal64:
2886 case tok::kw__Decimal128:
2887 case tok::kw___vector:
2888
2889 // struct-or-union-specifier (C99) or class-specifier (C++)
2890 case tok::kw_class:
2891 case tok::kw_struct:
2892 case tok::kw_union:
2893 // enum-specifier
2894 case tok::kw_enum:
2895
2896 // typedef-name
2897 case tok::annot_typename:
2898 return true;
2899 }
2900}
2901
Steve Naroff5f8aa692008-02-11 23:15:56 +00002902/// isTypeSpecifierQualifier - Return true if the current token could be the
Reid Spencer5f016e22007-07-11 17:01:13 +00002903/// start of a specifier-qualifier-list.
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002904bool Parser::isTypeSpecifierQualifier() {
Reid Spencer5f016e22007-07-11 17:01:13 +00002905 switch (Tok.getKind()) {
2906 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002907
Chris Lattner166a8fc2009-01-04 23:41:41 +00002908 case tok::identifier: // foo::bar
John Thompson82287d12010-02-05 00:12:22 +00002909 if (TryAltiVecVectorToken())
2910 return true;
2911 // Fall through.
Douglas Gregord57959a2009-03-27 23:10:48 +00002912 case tok::kw_typename: // typename T::type
Chris Lattner166a8fc2009-01-04 23:41:41 +00002913 // Annotate typenames and C++ scope specifiers. If we get one, just
2914 // recurse to handle whatever we get.
2915 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00002916 return true;
2917 if (Tok.is(tok::identifier))
2918 return false;
2919 return isTypeSpecifierQualifier();
Douglas Gregord57959a2009-03-27 23:10:48 +00002920
Chris Lattner166a8fc2009-01-04 23:41:41 +00002921 case tok::coloncolon: // ::foo::bar
2922 if (NextToken().is(tok::kw_new) || // ::new
2923 NextToken().is(tok::kw_delete)) // ::delete
2924 return false;
2925
Chris Lattner166a8fc2009-01-04 23:41:41 +00002926 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00002927 return true;
2928 return isTypeSpecifierQualifier();
Mike Stump1eb44332009-09-09 15:08:12 +00002929
Reid Spencer5f016e22007-07-11 17:01:13 +00002930 // GNU attributes support.
2931 case tok::kw___attribute:
Steve Naroffd1861fd2007-07-31 12:34:36 +00002932 // GNU typeof support.
2933 case tok::kw_typeof:
Mike Stump1eb44332009-09-09 15:08:12 +00002934
Reid Spencer5f016e22007-07-11 17:01:13 +00002935 // type-specifiers
2936 case tok::kw_short:
2937 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00002938 case tok::kw___int64:
Reid Spencer5f016e22007-07-11 17:01:13 +00002939 case tok::kw_signed:
2940 case tok::kw_unsigned:
2941 case tok::kw__Complex:
2942 case tok::kw__Imaginary:
2943 case tok::kw_void:
2944 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00002945 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002946 case tok::kw_char16_t:
2947 case tok::kw_char32_t:
Reid Spencer5f016e22007-07-11 17:01:13 +00002948 case tok::kw_int:
2949 case tok::kw_float:
2950 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00002951 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00002952 case tok::kw__Bool:
2953 case tok::kw__Decimal32:
2954 case tok::kw__Decimal64:
2955 case tok::kw__Decimal128:
John Thompson82287d12010-02-05 00:12:22 +00002956 case tok::kw___vector:
Mike Stump1eb44332009-09-09 15:08:12 +00002957
Chris Lattner99dc9142008-04-13 18:59:07 +00002958 // struct-or-union-specifier (C99) or class-specifier (C++)
2959 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00002960 case tok::kw_struct:
2961 case tok::kw_union:
2962 // enum-specifier
2963 case tok::kw_enum:
Mike Stump1eb44332009-09-09 15:08:12 +00002964
Reid Spencer5f016e22007-07-11 17:01:13 +00002965 // type-qualifier
2966 case tok::kw_const:
2967 case tok::kw_volatile:
2968 case tok::kw_restrict:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002969
2970 // typedef-name
Chris Lattnerb31757b2009-01-06 05:06:21 +00002971 case tok::annot_typename:
Reid Spencer5f016e22007-07-11 17:01:13 +00002972 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00002973
Chris Lattner7c186be2008-10-20 00:25:30 +00002974 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
2975 case tok::less:
2976 return getLang().ObjC1;
Mike Stump1eb44332009-09-09 15:08:12 +00002977
Steve Naroff239f0732008-12-25 14:16:32 +00002978 case tok::kw___cdecl:
2979 case tok::kw___stdcall:
2980 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00002981 case tok::kw___thiscall:
Eli Friedman290eeb02009-06-08 23:27:34 +00002982 case tok::kw___w64:
2983 case tok::kw___ptr64:
Dawn Perchik52fc3142010-09-03 01:29:35 +00002984 case tok::kw___pascal:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00002985
2986 case tok::kw___private:
2987 case tok::kw___local:
2988 case tok::kw___global:
2989 case tok::kw___constant:
2990 case tok::kw___read_only:
2991 case tok::kw___read_write:
2992 case tok::kw___write_only:
2993
Eli Friedman290eeb02009-06-08 23:27:34 +00002994 return true;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00002995
2996 case tok::kw_private:
2997 return getLang().OpenCL;
Reid Spencer5f016e22007-07-11 17:01:13 +00002998 }
2999}
3000
3001/// isDeclarationSpecifier() - Return true if the current token is part of a
3002/// declaration specifier.
Douglas Gregor9497a732010-09-16 01:51:54 +00003003///
3004/// \param DisambiguatingWithExpression True to indicate that the purpose of
3005/// this check is to disambiguate between an expression and a declaration.
3006bool Parser::isDeclarationSpecifier(bool DisambiguatingWithExpression) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003007 switch (Tok.getKind()) {
3008 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00003009
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003010 case tok::kw_private:
3011 return getLang().OpenCL;
3012
Chris Lattner166a8fc2009-01-04 23:41:41 +00003013 case tok::identifier: // foo::bar
Steve Naroff61f72cb2009-03-09 21:12:44 +00003014 // Unfortunate hack to support "Class.factoryMethod" notation.
3015 if (getLang().ObjC1 && NextToken().is(tok::period))
3016 return false;
John Thompson82287d12010-02-05 00:12:22 +00003017 if (TryAltiVecVectorToken())
3018 return true;
3019 // Fall through.
Douglas Gregord57959a2009-03-27 23:10:48 +00003020 case tok::kw_typename: // typename T::type
Chris Lattner166a8fc2009-01-04 23:41:41 +00003021 // Annotate typenames and C++ scope specifiers. If we get one, just
3022 // recurse to handle whatever we get.
3023 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003024 return true;
3025 if (Tok.is(tok::identifier))
3026 return false;
Douglas Gregor9497a732010-09-16 01:51:54 +00003027
3028 // If we're in Objective-C and we have an Objective-C class type followed
3029 // by an identifier and then either ':' or ']', in a place where an
3030 // expression is permitted, then this is probably a class message send
3031 // missing the initial '['. In this case, we won't consider this to be
3032 // the start of a declaration.
3033 if (DisambiguatingWithExpression &&
3034 isStartOfObjCClassMessageMissingOpenBracket())
3035 return false;
3036
John McCall9ba61662010-02-26 08:45:28 +00003037 return isDeclarationSpecifier();
3038
Chris Lattner166a8fc2009-01-04 23:41:41 +00003039 case tok::coloncolon: // ::foo::bar
3040 if (NextToken().is(tok::kw_new) || // ::new
3041 NextToken().is(tok::kw_delete)) // ::delete
3042 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00003043
Chris Lattner166a8fc2009-01-04 23:41:41 +00003044 // Annotate typenames and C++ scope specifiers. If we get one, just
3045 // recurse to handle whatever we get.
3046 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003047 return true;
3048 return isDeclarationSpecifier();
Mike Stump1eb44332009-09-09 15:08:12 +00003049
Reid Spencer5f016e22007-07-11 17:01:13 +00003050 // storage-class-specifier
3051 case tok::kw_typedef:
3052 case tok::kw_extern:
Steve Naroff8d54bf22007-12-18 00:16:02 +00003053 case tok::kw___private_extern__:
Reid Spencer5f016e22007-07-11 17:01:13 +00003054 case tok::kw_static:
3055 case tok::kw_auto:
3056 case tok::kw_register:
3057 case tok::kw___thread:
Mike Stump1eb44332009-09-09 15:08:12 +00003058
Reid Spencer5f016e22007-07-11 17:01:13 +00003059 // type-specifiers
3060 case tok::kw_short:
3061 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00003062 case tok::kw___int64:
Reid Spencer5f016e22007-07-11 17:01:13 +00003063 case tok::kw_signed:
3064 case tok::kw_unsigned:
3065 case tok::kw__Complex:
3066 case tok::kw__Imaginary:
3067 case tok::kw_void:
3068 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00003069 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00003070 case tok::kw_char16_t:
3071 case tok::kw_char32_t:
3072
Reid Spencer5f016e22007-07-11 17:01:13 +00003073 case tok::kw_int:
3074 case tok::kw_float:
3075 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00003076 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00003077 case tok::kw__Bool:
3078 case tok::kw__Decimal32:
3079 case tok::kw__Decimal64:
3080 case tok::kw__Decimal128:
John Thompson82287d12010-02-05 00:12:22 +00003081 case tok::kw___vector:
Mike Stump1eb44332009-09-09 15:08:12 +00003082
Chris Lattner99dc9142008-04-13 18:59:07 +00003083 // struct-or-union-specifier (C99) or class-specifier (C++)
3084 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00003085 case tok::kw_struct:
3086 case tok::kw_union:
3087 // enum-specifier
3088 case tok::kw_enum:
Mike Stump1eb44332009-09-09 15:08:12 +00003089
Reid Spencer5f016e22007-07-11 17:01:13 +00003090 // type-qualifier
3091 case tok::kw_const:
3092 case tok::kw_volatile:
3093 case tok::kw_restrict:
Steve Naroffd1861fd2007-07-31 12:34:36 +00003094
Reid Spencer5f016e22007-07-11 17:01:13 +00003095 // function-specifier
3096 case tok::kw_inline:
Douglas Gregorb48fe382008-10-31 09:07:45 +00003097 case tok::kw_virtual:
3098 case tok::kw_explicit:
Chris Lattnerd6c7c182007-08-09 16:40:21 +00003099
Peter Collingbournec6eb44b2011-04-15 00:35:57 +00003100 // static_assert-declaration
3101 case tok::kw__Static_assert:
3102
Chris Lattner1ef08762007-08-09 17:01:07 +00003103 // GNU typeof support.
3104 case tok::kw_typeof:
Mike Stump1eb44332009-09-09 15:08:12 +00003105
Chris Lattner1ef08762007-08-09 17:01:07 +00003106 // GNU attributes.
Chris Lattnerd6c7c182007-08-09 16:40:21 +00003107 case tok::kw___attribute:
Reid Spencer5f016e22007-07-11 17:01:13 +00003108 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00003109
Francois Pichete3d49b42011-06-19 08:02:06 +00003110 // C++0x decltype.
3111 case tok::kw_decltype:
3112 return true;
3113
Chris Lattnerf3948c42008-07-26 03:38:44 +00003114 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
3115 case tok::less:
3116 return getLang().ObjC1;
Mike Stump1eb44332009-09-09 15:08:12 +00003117
Douglas Gregord9d75e52011-04-27 05:41:15 +00003118 // typedef-name
3119 case tok::annot_typename:
3120 return !DisambiguatingWithExpression ||
3121 !isStartOfObjCClassMessageMissingOpenBracket();
3122
Steve Naroff47f52092009-01-06 19:34:12 +00003123 case tok::kw___declspec:
Steve Naroff239f0732008-12-25 14:16:32 +00003124 case tok::kw___cdecl:
3125 case tok::kw___stdcall:
3126 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00003127 case tok::kw___thiscall:
Eli Friedman290eeb02009-06-08 23:27:34 +00003128 case tok::kw___w64:
3129 case tok::kw___ptr64:
3130 case tok::kw___forceinline:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003131 case tok::kw___pascal:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003132
3133 case tok::kw___private:
3134 case tok::kw___local:
3135 case tok::kw___global:
3136 case tok::kw___constant:
3137 case tok::kw___read_only:
3138 case tok::kw___read_write:
3139 case tok::kw___write_only:
3140
Eli Friedman290eeb02009-06-08 23:27:34 +00003141 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00003142 }
3143}
3144
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003145bool Parser::isConstructorDeclarator() {
3146 TentativeParsingAction TPA(*this);
3147
3148 // Parse the C++ scope specifier.
3149 CXXScopeSpec SS;
John McCallb3d87482010-08-24 05:47:05 +00003150 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), true)) {
John McCall9ba61662010-02-26 08:45:28 +00003151 TPA.Revert();
3152 return false;
3153 }
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003154
3155 // Parse the constructor name.
3156 if (Tok.is(tok::identifier) || Tok.is(tok::annot_template_id)) {
3157 // We already know that we have a constructor name; just consume
3158 // the token.
3159 ConsumeToken();
3160 } else {
3161 TPA.Revert();
3162 return false;
3163 }
3164
3165 // Current class name must be followed by a left parentheses.
3166 if (Tok.isNot(tok::l_paren)) {
3167 TPA.Revert();
3168 return false;
3169 }
3170 ConsumeParen();
3171
3172 // A right parentheses or ellipsis signals that we have a constructor.
3173 if (Tok.is(tok::r_paren) || Tok.is(tok::ellipsis)) {
3174 TPA.Revert();
3175 return true;
3176 }
3177
3178 // If we need to, enter the specified scope.
3179 DeclaratorScopeObj DeclScopeObj(*this, SS);
Douglas Gregor23c94db2010-07-02 17:43:08 +00003180 if (SS.isSet() && Actions.ShouldEnterDeclaratorScope(getCurScope(), SS))
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003181 DeclScopeObj.EnterDeclaratorScope();
3182
Francois Pichetdfaa5fb2011-01-31 04:54:32 +00003183 // Optionally skip Microsoft attributes.
John McCall0b7e6782011-03-24 11:26:52 +00003184 ParsedAttributes Attrs(AttrFactory);
Francois Pichetdfaa5fb2011-01-31 04:54:32 +00003185 MaybeParseMicrosoftAttributes(Attrs);
3186
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003187 // Check whether the next token(s) are part of a declaration
3188 // specifier, in which case we have the start of a parameter and,
3189 // therefore, we know that this is a constructor.
3190 bool IsConstructor = isDeclarationSpecifier();
3191 TPA.Revert();
3192 return IsConstructor;
3193}
Reid Spencer5f016e22007-07-11 17:01:13 +00003194
3195/// ParseTypeQualifierListOpt
Dawn Perchik52fc3142010-09-03 01:29:35 +00003196/// type-qualifier-list: [C99 6.7.5]
3197/// type-qualifier
3198/// [vendor] attributes
3199/// [ only if VendorAttributesAllowed=true ]
3200/// type-qualifier-list type-qualifier
3201/// [vendor] type-qualifier-list attributes
3202/// [ only if VendorAttributesAllowed=true ]
3203/// [C++0x] attribute-specifier[opt] is allowed before cv-qualifier-seq
3204/// [ only if CXX0XAttributesAllowed=true ]
3205/// Note: vendor can be GNU, MS, etc.
Reid Spencer5f016e22007-07-11 17:01:13 +00003206///
Dawn Perchik52fc3142010-09-03 01:29:35 +00003207void Parser::ParseTypeQualifierListOpt(DeclSpec &DS,
3208 bool VendorAttributesAllowed,
Sean Huntbbd37c62009-11-21 08:43:09 +00003209 bool CXX0XAttributesAllowed) {
3210 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier()) {
3211 SourceLocation Loc = Tok.getLocation();
John McCall0b7e6782011-03-24 11:26:52 +00003212 ParsedAttributesWithRange attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00003213 ParseCXX0XAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00003214 if (CXX0XAttributesAllowed)
John McCall7f040a92010-12-24 02:08:15 +00003215 DS.takeAttributesFrom(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00003216 else
3217 Diag(Loc, diag::err_attributes_not_allowed);
3218 }
Abramo Bagnara796aa442011-03-12 11:17:06 +00003219
3220 SourceLocation EndLoc;
3221
Reid Spencer5f016e22007-07-11 17:01:13 +00003222 while (1) {
John McCallfec54012009-08-03 20:12:06 +00003223 bool isInvalid = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00003224 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00003225 unsigned DiagID = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00003226 SourceLocation Loc = Tok.getLocation();
3227
3228 switch (Tok.getKind()) {
Douglas Gregor1a480c42010-08-27 17:35:51 +00003229 case tok::code_completion:
3230 Actions.CodeCompleteTypeQualifiers(DS);
3231 ConsumeCodeCompletionToken();
3232 break;
3233
Reid Spencer5f016e22007-07-11 17:01:13 +00003234 case tok::kw_const:
John McCallfec54012009-08-03 20:12:06 +00003235 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, DiagID,
3236 getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00003237 break;
3238 case tok::kw_volatile:
John McCallfec54012009-08-03 20:12:06 +00003239 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
3240 getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00003241 break;
3242 case tok::kw_restrict:
John McCallfec54012009-08-03 20:12:06 +00003243 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
3244 getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00003245 break;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003246
3247 // OpenCL qualifiers:
3248 case tok::kw_private:
3249 if (!getLang().OpenCL)
3250 goto DoneWithTypeQuals;
3251 case tok::kw___private:
3252 case tok::kw___global:
3253 case tok::kw___local:
3254 case tok::kw___constant:
3255 case tok::kw___read_only:
3256 case tok::kw___write_only:
3257 case tok::kw___read_write:
3258 ParseOpenCLQualifiers(DS);
3259 break;
3260
Eli Friedman290eeb02009-06-08 23:27:34 +00003261 case tok::kw___w64:
Steve Naroff86bc6cf2008-12-25 14:41:26 +00003262 case tok::kw___ptr64:
Steve Naroff239f0732008-12-25 14:16:32 +00003263 case tok::kw___cdecl:
3264 case tok::kw___stdcall:
3265 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00003266 case tok::kw___thiscall:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003267 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00003268 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman290eeb02009-06-08 23:27:34 +00003269 continue;
3270 }
3271 goto DoneWithTypeQuals;
Dawn Perchik52fc3142010-09-03 01:29:35 +00003272 case tok::kw___pascal:
3273 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00003274 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik52fc3142010-09-03 01:29:35 +00003275 continue;
3276 }
3277 goto DoneWithTypeQuals;
Reid Spencer5f016e22007-07-11 17:01:13 +00003278 case tok::kw___attribute:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003279 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00003280 ParseGNUAttributes(DS.getAttributes());
Chris Lattner5a69d1c2008-12-18 07:02:59 +00003281 continue; // do *not* consume the next token!
3282 }
3283 // otherwise, FALL THROUGH!
3284 default:
Steve Naroff239f0732008-12-25 14:16:32 +00003285 DoneWithTypeQuals:
Chris Lattner5a69d1c2008-12-18 07:02:59 +00003286 // If this is not a type-qualifier token, we're done reading type
3287 // qualifiers. First verify that DeclSpec's are consistent.
Douglas Gregor9b3064b2009-04-01 22:41:11 +00003288 DS.Finish(Diags, PP);
Abramo Bagnara796aa442011-03-12 11:17:06 +00003289 if (EndLoc.isValid())
3290 DS.SetRangeEnd(EndLoc);
Chris Lattner5a69d1c2008-12-18 07:02:59 +00003291 return;
Reid Spencer5f016e22007-07-11 17:01:13 +00003292 }
Chris Lattnera1fcbad2008-12-18 06:50:14 +00003293
Reid Spencer5f016e22007-07-11 17:01:13 +00003294 // If the specifier combination wasn't legal, issue a diagnostic.
3295 if (isInvalid) {
3296 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner1ab3b962008-11-18 07:48:38 +00003297 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00003298 }
Abramo Bagnara796aa442011-03-12 11:17:06 +00003299 EndLoc = ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00003300 }
3301}
3302
3303
3304/// ParseDeclarator - Parse and verify a newly-initialized declarator.
3305///
3306void Parser::ParseDeclarator(Declarator &D) {
3307 /// This implements the 'declarator' production in the C grammar, then checks
3308 /// for well-formedness and issues diagnostics.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003309 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Reid Spencer5f016e22007-07-11 17:01:13 +00003310}
3311
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003312/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
3313/// is parsed by the function passed to it. Pass null, and the direct-declarator
3314/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003315/// ptr-operator production.
3316///
Sebastian Redlf30208a2009-01-24 21:16:55 +00003317/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
3318/// [C] pointer[opt] direct-declarator
3319/// [C++] direct-declarator
3320/// [C++] ptr-operator declarator
Reid Spencer5f016e22007-07-11 17:01:13 +00003321///
3322/// pointer: [C99 6.7.5]
3323/// '*' type-qualifier-list[opt]
3324/// '*' type-qualifier-list[opt] pointer
3325///
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003326/// ptr-operator:
3327/// '*' cv-qualifier-seq[opt]
3328/// '&'
Sebastian Redl05532f22009-03-15 22:02:01 +00003329/// [C++0x] '&&'
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003330/// [GNU] '&' restrict[opt] attributes[opt]
Sebastian Redl05532f22009-03-15 22:02:01 +00003331/// [GNU?] '&&' restrict[opt] attributes[opt]
Sebastian Redlf30208a2009-01-24 21:16:55 +00003332/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003333void Parser::ParseDeclaratorInternal(Declarator &D,
3334 DirectDeclParseFunction DirectDeclParser) {
Douglas Gregor91a28862009-08-26 14:27:30 +00003335 if (Diags.hasAllExtensionsSilenced())
3336 D.setExtension();
Douglas Gregor2ccccb32010-08-23 18:23:48 +00003337
Sebastian Redlf30208a2009-01-24 21:16:55 +00003338 // C++ member pointers start with a '::' or a nested-name.
3339 // Member pointers get special handling, since there's no place for the
3340 // scope spec in the generic path below.
Chris Lattnerf919bfe2009-03-24 17:04:48 +00003341 if (getLang().CPlusPlus &&
3342 (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) ||
3343 Tok.is(tok::annot_cxxscope))) {
Sebastian Redlf30208a2009-01-24 21:16:55 +00003344 CXXScopeSpec SS;
John McCallb3d87482010-08-24 05:47:05 +00003345 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), true); // ignore fail
John McCall9ba61662010-02-26 08:45:28 +00003346
Jeffrey Yasskinedc28772010-04-07 23:29:58 +00003347 if (SS.isNotEmpty()) {
Mike Stump1eb44332009-09-09 15:08:12 +00003348 if (Tok.isNot(tok::star)) {
Sebastian Redlf30208a2009-01-24 21:16:55 +00003349 // The scope spec really belongs to the direct-declarator.
3350 D.getCXXScopeSpec() = SS;
3351 if (DirectDeclParser)
3352 (this->*DirectDeclParser)(D);
3353 return;
3354 }
3355
3356 SourceLocation Loc = ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00003357 D.SetRangeEnd(Loc);
John McCall0b7e6782011-03-24 11:26:52 +00003358 DeclSpec DS(AttrFactory);
Sebastian Redlf30208a2009-01-24 21:16:55 +00003359 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00003360 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00003361
3362 // Recurse to parse whatever is left.
3363 ParseDeclaratorInternal(D, DirectDeclParser);
3364
3365 // Sema will have to catch (syntactically invalid) pointers into global
3366 // scope. It has to catch pointers into namespace scope anyway.
3367 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
John McCall0b7e6782011-03-24 11:26:52 +00003368 Loc),
3369 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003370 /* Don't replace range end. */SourceLocation());
Sebastian Redlf30208a2009-01-24 21:16:55 +00003371 return;
3372 }
3373 }
3374
3375 tok::TokenKind Kind = Tok.getKind();
Steve Naroff5618bd42008-08-27 16:04:49 +00003376 // Not a pointer, C++ reference, or block.
Chris Lattner9af55002009-03-27 04:18:06 +00003377 if (Kind != tok::star && Kind != tok::caret &&
Chris Lattnerf919bfe2009-03-24 17:04:48 +00003378 (Kind != tok::amp || !getLang().CPlusPlus) &&
Sebastian Redl743de1f2009-03-23 00:00:23 +00003379 // We parse rvalue refs in C++03, because otherwise the errors are scary.
Chris Lattner9af55002009-03-27 04:18:06 +00003380 (Kind != tok::ampamp || !getLang().CPlusPlus)) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003381 if (DirectDeclParser)
3382 (this->*DirectDeclParser)(D);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003383 return;
3384 }
Sebastian Redlf30208a2009-01-24 21:16:55 +00003385
Sebastian Redl05532f22009-03-15 22:02:01 +00003386 // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference,
3387 // '&&' -> rvalue reference
Sebastian Redl743de1f2009-03-23 00:00:23 +00003388 SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&.
Sebastian Redlab197ba2009-02-09 18:23:29 +00003389 D.SetRangeEnd(Loc);
Reid Spencer5f016e22007-07-11 17:01:13 +00003390
Chris Lattner9af55002009-03-27 04:18:06 +00003391 if (Kind == tok::star || Kind == tok::caret) {
Chris Lattner76549142008-02-21 01:32:26 +00003392 // Is a pointer.
John McCall0b7e6782011-03-24 11:26:52 +00003393 DeclSpec DS(AttrFactory);
Sebastian Redlf30208a2009-01-24 21:16:55 +00003394
Reid Spencer5f016e22007-07-11 17:01:13 +00003395 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00003396 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00003397
Reid Spencer5f016e22007-07-11 17:01:13 +00003398 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003399 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroff5618bd42008-08-27 16:04:49 +00003400 if (Kind == tok::star)
3401 // Remember that we parsed a pointer type, and remember the type-quals.
3402 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
Chandler Carruthd067c072011-02-23 18:51:59 +00003403 DS.getConstSpecLoc(),
3404 DS.getVolatileSpecLoc(),
John McCall0b7e6782011-03-24 11:26:52 +00003405 DS.getRestrictSpecLoc()),
3406 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003407 SourceLocation());
Steve Naroff5618bd42008-08-27 16:04:49 +00003408 else
3409 // Remember that we parsed a Block type, and remember the type-quals.
Mike Stump1eb44332009-09-09 15:08:12 +00003410 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
John McCall0b7e6782011-03-24 11:26:52 +00003411 Loc),
3412 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003413 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00003414 } else {
3415 // Is a reference
John McCall0b7e6782011-03-24 11:26:52 +00003416 DeclSpec DS(AttrFactory);
Reid Spencer5f016e22007-07-11 17:01:13 +00003417
Sebastian Redl743de1f2009-03-23 00:00:23 +00003418 // Complain about rvalue references in C++03, but then go on and build
3419 // the declarator.
3420 if (Kind == tok::ampamp && !getLang().CPlusPlus0x)
Douglas Gregor16cf8f52011-01-25 02:17:32 +00003421 Diag(Loc, diag::ext_rvalue_reference);
Sebastian Redl743de1f2009-03-23 00:00:23 +00003422
Reid Spencer5f016e22007-07-11 17:01:13 +00003423 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
3424 // cv-qualifiers are introduced through the use of a typedef or of a
3425 // template type argument, in which case the cv-qualifiers are ignored.
3426 //
3427 // [GNU] Retricted references are allowed.
3428 // [GNU] Attributes on references are allowed.
Sean Huntbbd37c62009-11-21 08:43:09 +00003429 // [C++0x] Attributes on references are not allowed.
3430 ParseTypeQualifierListOpt(DS, true, false);
Sebastian Redlab197ba2009-02-09 18:23:29 +00003431 D.ExtendWithDeclSpec(DS);
Reid Spencer5f016e22007-07-11 17:01:13 +00003432
3433 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
3434 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
3435 Diag(DS.getConstSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00003436 diag::err_invalid_reference_qualifier_application) << "const";
Reid Spencer5f016e22007-07-11 17:01:13 +00003437 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
3438 Diag(DS.getVolatileSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00003439 diag::err_invalid_reference_qualifier_application) << "volatile";
Reid Spencer5f016e22007-07-11 17:01:13 +00003440 }
3441
3442 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003443 ParseDeclaratorInternal(D, DirectDeclParser);
Reid Spencer5f016e22007-07-11 17:01:13 +00003444
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00003445 if (D.getNumTypeObjects() > 0) {
3446 // C++ [dcl.ref]p4: There shall be no references to references.
3447 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
3448 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattnerda83bac2008-11-19 07:37:42 +00003449 if (const IdentifierInfo *II = D.getIdentifier())
3450 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
3451 << II;
3452 else
3453 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
3454 << "type name";
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00003455
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003456 // Once we've complained about the reference-to-reference, we
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00003457 // can go ahead and build the (technically ill-formed)
3458 // declarator: reference collapsing will take care of it.
3459 }
3460 }
3461
Reid Spencer5f016e22007-07-11 17:01:13 +00003462 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner76549142008-02-21 01:32:26 +00003463 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
Sebastian Redl05532f22009-03-15 22:02:01 +00003464 Kind == tok::amp),
John McCall0b7e6782011-03-24 11:26:52 +00003465 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003466 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00003467 }
3468}
3469
3470/// ParseDirectDeclarator
3471/// direct-declarator: [C99 6.7.5]
Douglas Gregor42a552f2008-11-05 20:51:48 +00003472/// [C99] identifier
Reid Spencer5f016e22007-07-11 17:01:13 +00003473/// '(' declarator ')'
3474/// [GNU] '(' attributes declarator ')'
3475/// [C90] direct-declarator '[' constant-expression[opt] ']'
3476/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
3477/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
3478/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
3479/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
3480/// direct-declarator '(' parameter-type-list ')'
3481/// direct-declarator '(' identifier-list[opt] ')'
3482/// [GNU] direct-declarator '(' parameter-forward-declarations
3483/// parameter-type-list[opt] ')'
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003484/// [C++] direct-declarator '(' parameter-declaration-clause ')'
3485/// cv-qualifier-seq[opt] exception-specification[opt]
Douglas Gregorb48fe382008-10-31 09:07:45 +00003486/// [C++] declarator-id
Douglas Gregor42a552f2008-11-05 20:51:48 +00003487///
3488/// declarator-id: [C++ 8]
Douglas Gregora8bc8c92010-12-23 22:44:42 +00003489/// '...'[opt] id-expression
Douglas Gregor42a552f2008-11-05 20:51:48 +00003490/// '::'[opt] nested-name-specifier[opt] type-name
3491///
3492/// id-expression: [C++ 5.1]
3493/// unqualified-id
Douglas Gregordb422df2009-09-25 21:45:23 +00003494/// qualified-id
Douglas Gregor42a552f2008-11-05 20:51:48 +00003495///
3496/// unqualified-id: [C++ 5.1]
Mike Stump1eb44332009-09-09 15:08:12 +00003497/// identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003498/// operator-function-id
Douglas Gregordb422df2009-09-25 21:45:23 +00003499/// conversion-function-id
Mike Stump1eb44332009-09-09 15:08:12 +00003500/// '~' class-name
Douglas Gregor39a8de12009-02-25 19:37:18 +00003501/// template-id
Argyrios Kyrtzidisc7ed9c62008-11-07 22:02:30 +00003502///
Reid Spencer5f016e22007-07-11 17:01:13 +00003503void Parser::ParseDirectDeclarator(Declarator &D) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00003504 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003505
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003506 if (getLang().CPlusPlus && D.mayHaveIdentifier()) {
3507 // ParseDeclaratorInternal might already have parsed the scope.
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00003508 if (D.getCXXScopeSpec().isEmpty()) {
John McCallb3d87482010-08-24 05:47:05 +00003509 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec(), ParsedType(), true);
John McCall9ba61662010-02-26 08:45:28 +00003510 }
3511
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00003512 if (D.getCXXScopeSpec().isValid()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00003513 if (Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
John McCalle7e278b2009-12-11 20:04:54 +00003514 // Change the declaration context for name lookup, until this function
3515 // is exited (and the declarator has been parsed).
3516 DeclScopeObj.EnterDeclaratorScope();
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00003517 }
3518
Douglas Gregora8bc8c92010-12-23 22:44:42 +00003519 // C++0x [dcl.fct]p14:
3520 // There is a syntactic ambiguity when an ellipsis occurs at the end
3521 // of a parameter-declaration-clause without a preceding comma. In
3522 // this case, the ellipsis is parsed as part of the
3523 // abstract-declarator if the type of the parameter names a template
3524 // parameter pack that has not been expanded; otherwise, it is parsed
3525 // as part of the parameter-declaration-clause.
3526 if (Tok.is(tok::ellipsis) &&
3527 !((D.getContext() == Declarator::PrototypeContext ||
3528 D.getContext() == Declarator::BlockLiteralContext) &&
Douglas Gregora8bc8c92010-12-23 22:44:42 +00003529 NextToken().is(tok::r_paren) &&
3530 !Actions.containsUnexpandedParameterPacks(D)))
3531 D.setEllipsisLoc(ConsumeToken());
3532
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003533 if (Tok.is(tok::identifier) || Tok.is(tok::kw_operator) ||
3534 Tok.is(tok::annot_template_id) || Tok.is(tok::tilde)) {
3535 // We found something that indicates the start of an unqualified-id.
3536 // Parse that unqualified-id.
John McCallba9d8532010-04-13 06:39:49 +00003537 bool AllowConstructorName;
3538 if (D.getDeclSpec().hasTypeSpecifier())
3539 AllowConstructorName = false;
3540 else if (D.getCXXScopeSpec().isSet())
3541 AllowConstructorName =
3542 (D.getContext() == Declarator::FileContext ||
3543 (D.getContext() == Declarator::MemberContext &&
3544 D.getDeclSpec().isFriendSpecified()));
3545 else
3546 AllowConstructorName = (D.getContext() == Declarator::MemberContext);
3547
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003548 if (ParseUnqualifiedId(D.getCXXScopeSpec(),
3549 /*EnteringContext=*/true,
3550 /*AllowDestructorName=*/true,
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003551 AllowConstructorName,
John McCallb3d87482010-08-24 05:47:05 +00003552 ParsedType(),
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00003553 D.getName()) ||
3554 // Once we're past the identifier, if the scope was bad, mark the
3555 // whole declarator bad.
3556 D.getCXXScopeSpec().isInvalid()) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00003557 D.SetIdentifier(0, Tok.getLocation());
3558 D.setInvalidType(true);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003559 } else {
3560 // Parsed the unqualified-id; update range information and move along.
3561 if (D.getSourceRange().getBegin().isInvalid())
3562 D.SetRangeBegin(D.getName().getSourceRange().getBegin());
3563 D.SetRangeEnd(D.getName().getSourceRange().getEnd());
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003564 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003565 goto PastIdentifier;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00003566 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003567 } else if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00003568 assert(!getLang().CPlusPlus &&
3569 "There's a C++-specific check for tok::identifier above");
3570 assert(Tok.getIdentifierInfo() && "Not an identifier?");
3571 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
3572 ConsumeToken();
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003573 goto PastIdentifier;
3574 }
3575
3576 if (Tok.is(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003577 // direct-declarator: '(' declarator ')'
3578 // direct-declarator: '(' attributes declarator ')'
3579 // Example: 'char (*X)' or 'int (*XX)(void)'
3580 ParseParenDeclarator(D);
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003581
3582 // If the declarator was parenthesized, we entered the declarator
3583 // scope when parsing the parenthesized declarator, then exited
3584 // the scope already. Re-enter the scope, if we need to.
3585 if (D.getCXXScopeSpec().isSet()) {
Fariborz Jahanian46877cd2010-08-17 23:50:37 +00003586 // If there was an error parsing parenthesized declarator, declarator
3587 // scope may have been enterred before. Don't do it again.
3588 if (!D.isInvalidType() &&
3589 Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003590 // Change the declaration context for name lookup, until this function
3591 // is exited (and the declarator has been parsed).
Fariborz Jahanian46877cd2010-08-17 23:50:37 +00003592 DeclScopeObj.EnterDeclaratorScope();
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003593 }
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00003594 } else if (D.mayOmitIdentifier()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003595 // This could be something simple like "int" (in which case the declarator
3596 // portion is empty), if an abstract-declarator is allowed.
3597 D.SetIdentifier(0, Tok.getLocation());
3598 } else {
Douglas Gregore950d4b2009-03-06 23:28:18 +00003599 if (D.getContext() == Declarator::MemberContext)
3600 Diag(Tok, diag::err_expected_member_name_or_semi)
3601 << D.getDeclSpec().getSourceRange();
3602 else if (getLang().CPlusPlus)
Douglas Gregor2d1c2142009-11-03 19:44:04 +00003603 Diag(Tok, diag::err_expected_unqualified_id) << getLang().CPlusPlus;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003604 else
Chris Lattner1ab3b962008-11-18 07:48:38 +00003605 Diag(Tok, diag::err_expected_ident_lparen);
Reid Spencer5f016e22007-07-11 17:01:13 +00003606 D.SetIdentifier(0, Tok.getLocation());
Chris Lattner1f6f54b2008-11-11 06:13:16 +00003607 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00003608 }
Mike Stump1eb44332009-09-09 15:08:12 +00003609
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00003610 PastIdentifier:
Reid Spencer5f016e22007-07-11 17:01:13 +00003611 assert(D.isPastIdentifier() &&
3612 "Haven't past the location of the identifier yet?");
Mike Stump1eb44332009-09-09 15:08:12 +00003613
Sean Huntbbd37c62009-11-21 08:43:09 +00003614 // Don't parse attributes unless we have an identifier.
John McCall7f040a92010-12-24 02:08:15 +00003615 if (D.getIdentifier())
3616 MaybeParseCXX0XAttributes(D);
Sean Huntbbd37c62009-11-21 08:43:09 +00003617
Reid Spencer5f016e22007-07-11 17:01:13 +00003618 while (1) {
Chris Lattner04d66662007-10-09 17:33:22 +00003619 if (Tok.is(tok::l_paren)) {
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00003620 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
3621 // In such a case, check if we actually have a function declarator; if it
3622 // is not, the declarator has been fully parsed.
Chris Lattner7399ee02008-10-20 02:05:46 +00003623 if (getLang().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
3624 // When not in file scope, warn for ambiguous function declarators, just
3625 // in case the author intended it as a variable definition.
3626 bool warnIfAmbiguous = D.getContext() != Declarator::FileContext;
3627 if (!isCXXFunctionDeclarator(warnIfAmbiguous))
3628 break;
3629 }
John McCall0b7e6782011-03-24 11:26:52 +00003630 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00003631 ParseFunctionDeclarator(ConsumeParen(), D, attrs);
Chris Lattner04d66662007-10-09 17:33:22 +00003632 } else if (Tok.is(tok::l_square)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003633 ParseBracketDeclarator(D);
3634 } else {
3635 break;
3636 }
3637 }
3638}
3639
Chris Lattneref4715c2008-04-06 05:45:57 +00003640/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
3641/// only called before the identifier, so these are most likely just grouping
Mike Stump1eb44332009-09-09 15:08:12 +00003642/// parens for precedence. If we find that these are actually function
Chris Lattneref4715c2008-04-06 05:45:57 +00003643/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
3644///
3645/// direct-declarator:
3646/// '(' declarator ')'
3647/// [GNU] '(' attributes declarator ')'
Chris Lattner7399ee02008-10-20 02:05:46 +00003648/// direct-declarator '(' parameter-type-list ')'
3649/// direct-declarator '(' identifier-list[opt] ')'
3650/// [GNU] direct-declarator '(' parameter-forward-declarations
3651/// parameter-type-list[opt] ')'
Chris Lattneref4715c2008-04-06 05:45:57 +00003652///
3653void Parser::ParseParenDeclarator(Declarator &D) {
3654 SourceLocation StartLoc = ConsumeParen();
3655 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
Mike Stump1eb44332009-09-09 15:08:12 +00003656
Chris Lattner7399ee02008-10-20 02:05:46 +00003657 // Eat any attributes before we look at whether this is a grouping or function
3658 // declarator paren. If this is a grouping paren, the attribute applies to
3659 // the type being built up, for example:
3660 // int (__attribute__(()) *x)(long y)
3661 // If this ends up not being a grouping paren, the attribute applies to the
3662 // first argument, for example:
3663 // int (__attribute__(()) int x)
3664 // In either case, we need to eat any attributes to be able to determine what
3665 // sort of paren this is.
3666 //
John McCall0b7e6782011-03-24 11:26:52 +00003667 ParsedAttributes attrs(AttrFactory);
Chris Lattner7399ee02008-10-20 02:05:46 +00003668 bool RequiresArg = false;
3669 if (Tok.is(tok::kw___attribute)) {
John McCall7f040a92010-12-24 02:08:15 +00003670 ParseGNUAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00003671
Chris Lattner7399ee02008-10-20 02:05:46 +00003672 // We require that the argument list (if this is a non-grouping paren) be
3673 // present even if the attribute list was empty.
3674 RequiresArg = true;
3675 }
Steve Naroff239f0732008-12-25 14:16:32 +00003676 // Eat any Microsoft extensions.
Eli Friedman290eeb02009-06-08 23:27:34 +00003677 if (Tok.is(tok::kw___cdecl) || Tok.is(tok::kw___stdcall) ||
Douglas Gregorf813a2c2010-05-18 16:57:00 +00003678 Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___fastcall) ||
3679 Tok.is(tok::kw___w64) || Tok.is(tok::kw___ptr64)) {
John McCall7f040a92010-12-24 02:08:15 +00003680 ParseMicrosoftTypeAttributes(attrs);
Eli Friedman290eeb02009-06-08 23:27:34 +00003681 }
Dawn Perchik52fc3142010-09-03 01:29:35 +00003682 // Eat any Borland extensions.
Ted Kremenek8113ecf2010-11-10 05:59:39 +00003683 if (Tok.is(tok::kw___pascal))
John McCall7f040a92010-12-24 02:08:15 +00003684 ParseBorlandTypeAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00003685
Chris Lattneref4715c2008-04-06 05:45:57 +00003686 // If we haven't past the identifier yet (or where the identifier would be
3687 // stored, if this is an abstract declarator), then this is probably just
3688 // grouping parens. However, if this could be an abstract-declarator, then
3689 // this could also be the start of function arguments (consider 'void()').
3690 bool isGrouping;
Mike Stump1eb44332009-09-09 15:08:12 +00003691
Chris Lattneref4715c2008-04-06 05:45:57 +00003692 if (!D.mayOmitIdentifier()) {
3693 // If this can't be an abstract-declarator, this *must* be a grouping
3694 // paren, because we haven't seen the identifier yet.
3695 isGrouping = true;
3696 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Argyrios Kyrtzidise25d2702008-10-06 00:07:55 +00003697 (getLang().CPlusPlus && Tok.is(tok::ellipsis)) || // C++ int(...)
Chris Lattneref4715c2008-04-06 05:45:57 +00003698 isDeclarationSpecifier()) { // 'int(int)' is a function.
3699 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
3700 // considered to be a type, not a K&R identifier-list.
3701 isGrouping = false;
3702 } else {
3703 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
3704 isGrouping = true;
3705 }
Mike Stump1eb44332009-09-09 15:08:12 +00003706
Chris Lattneref4715c2008-04-06 05:45:57 +00003707 // If this is a grouping paren, handle:
3708 // direct-declarator: '(' declarator ')'
3709 // direct-declarator: '(' attributes declarator ')'
3710 if (isGrouping) {
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00003711 bool hadGroupingParens = D.hasGroupingParens();
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00003712 D.setGroupingParens(true);
3713
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003714 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattneref4715c2008-04-06 05:45:57 +00003715 // Match the ')'.
Abramo Bagnara075f8f12010-12-10 16:29:40 +00003716 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_paren, StartLoc);
John McCall0b7e6782011-03-24 11:26:52 +00003717 D.AddTypeInfo(DeclaratorChunk::getParen(StartLoc, EndLoc),
3718 attrs, EndLoc);
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00003719
3720 D.setGroupingParens(hadGroupingParens);
Chris Lattneref4715c2008-04-06 05:45:57 +00003721 return;
3722 }
Mike Stump1eb44332009-09-09 15:08:12 +00003723
Chris Lattneref4715c2008-04-06 05:45:57 +00003724 // Okay, if this wasn't a grouping paren, it must be the start of a function
3725 // argument list. Recognize that this declarator will never have an
Chris Lattner7399ee02008-10-20 02:05:46 +00003726 // identifier (and remember where it would have been), then call into
3727 // ParseFunctionDeclarator to handle of argument list.
Chris Lattneref4715c2008-04-06 05:45:57 +00003728 D.SetIdentifier(0, Tok.getLocation());
3729
John McCall7f040a92010-12-24 02:08:15 +00003730 ParseFunctionDeclarator(StartLoc, D, attrs, RequiresArg);
Chris Lattneref4715c2008-04-06 05:45:57 +00003731}
3732
3733/// ParseFunctionDeclarator - We are after the identifier and have parsed the
3734/// declarator D up to a paren, which indicates that we are parsing function
3735/// arguments.
Reid Spencer5f016e22007-07-11 17:01:13 +00003736///
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00003737/// If attrs is non-null, then the caller parsed those arguments immediately
Chris Lattner7399ee02008-10-20 02:05:46 +00003738/// after the open paren - they should be considered to be the first argument of
3739/// a parameter. If RequiresArg is true, then the first argument of the
3740/// function is required to be present and required to not be an identifier
3741/// list.
3742///
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00003743/// For C++, after the parameter-list, it also parses cv-qualifier-seq[opt],
3744/// (C++0x) ref-qualifier[opt], exception-specification[opt], and
3745/// (C++0x) trailing-return-type[opt].
3746///
3747/// [C++0x] exception-specification:
3748/// dynamic-exception-specification
3749/// noexcept-specification
3750///
3751void Parser::ParseFunctionDeclarator(SourceLocation LParenLoc, Declarator &D,
3752 ParsedAttributes &attrs,
3753 bool RequiresArg) {
3754 // lparen is already consumed!
3755 assert(D.isPastIdentifier() && "Should not call before identifier!");
3756
3757 // This should be true when the function has typed arguments.
3758 // Otherwise, it is treated as a K&R-style function.
3759 bool HasProto = false;
3760 // Build up an array of information about the parsed arguments.
Chris Lattner5f9e2722011-07-23 10:55:15 +00003761 SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00003762 // Remember where we see an ellipsis, if any.
3763 SourceLocation EllipsisLoc;
3764
3765 DeclSpec DS(AttrFactory);
3766 bool RefQualifierIsLValueRef = true;
3767 SourceLocation RefQualifierLoc;
3768 ExceptionSpecificationType ESpecType = EST_None;
3769 SourceRange ESpecRange;
Chris Lattner5f9e2722011-07-23 10:55:15 +00003770 SmallVector<ParsedType, 2> DynamicExceptions;
3771 SmallVector<SourceRange, 2> DynamicExceptionRanges;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00003772 ExprResult NoexceptExpr;
3773 ParsedType TrailingReturnType;
3774
3775 SourceLocation EndLoc;
3776
3777 if (isFunctionDeclaratorIdentifierList()) {
3778 if (RequiresArg)
3779 Diag(Tok, diag::err_argument_required_after_attribute);
3780
3781 ParseFunctionDeclaratorIdentifierList(D, ParamInfo);
3782
3783 EndLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
3784 } else {
3785 // Enter function-declaration scope, limiting any declarators to the
3786 // function prototype scope, including parameter declarators.
3787 ParseScope PrototypeScope(this,
3788 Scope::FunctionPrototypeScope|Scope::DeclScope);
3789
3790 if (Tok.isNot(tok::r_paren))
3791 ParseParameterDeclarationClause(D, attrs, ParamInfo, EllipsisLoc);
3792 else if (RequiresArg)
3793 Diag(Tok, diag::err_argument_required_after_attribute);
3794
3795 HasProto = ParamInfo.size() || getLang().CPlusPlus;
3796
3797 // If we have the closing ')', eat it.
3798 EndLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
3799
3800 if (getLang().CPlusPlus) {
3801 MaybeParseCXX0XAttributes(attrs);
3802
3803 // Parse cv-qualifier-seq[opt].
3804 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
3805 if (!DS.getSourceRange().getEnd().isInvalid())
3806 EndLoc = DS.getSourceRange().getEnd();
3807
3808 // Parse ref-qualifier[opt].
3809 if (Tok.is(tok::amp) || Tok.is(tok::ampamp)) {
3810 if (!getLang().CPlusPlus0x)
3811 Diag(Tok, diag::ext_ref_qualifier);
3812
3813 RefQualifierIsLValueRef = Tok.is(tok::amp);
3814 RefQualifierLoc = ConsumeToken();
3815 EndLoc = RefQualifierLoc;
3816 }
3817
3818 // Parse exception-specification[opt].
3819 ESpecType = MaybeParseExceptionSpecification(ESpecRange,
3820 DynamicExceptions,
3821 DynamicExceptionRanges,
3822 NoexceptExpr);
3823 if (ESpecType != EST_None)
3824 EndLoc = ESpecRange.getEnd();
3825
3826 // Parse trailing-return-type[opt].
3827 if (getLang().CPlusPlus0x && Tok.is(tok::arrow)) {
Douglas Gregorae7902c2011-08-04 15:30:47 +00003828 SourceRange Range;
3829 TrailingReturnType = ParseTrailingReturnType(Range).get();
3830 if (Range.getEnd().isValid())
3831 EndLoc = Range.getEnd();
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00003832 }
3833 }
3834
3835 // Leave prototype scope.
3836 PrototypeScope.Exit();
3837 }
3838
3839 // Remember that we parsed a function type, and remember the attributes.
3840 D.AddTypeInfo(DeclaratorChunk::getFunction(HasProto,
3841 /*isVariadic=*/EllipsisLoc.isValid(),
3842 EllipsisLoc,
3843 ParamInfo.data(), ParamInfo.size(),
3844 DS.getTypeQualifiers(),
3845 RefQualifierIsLValueRef,
3846 RefQualifierLoc,
Douglas Gregor90ebed02011-07-13 21:47:47 +00003847 /*MutableLoc=*/SourceLocation(),
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00003848 ESpecType, ESpecRange.getBegin(),
3849 DynamicExceptions.data(),
3850 DynamicExceptionRanges.data(),
3851 DynamicExceptions.size(),
3852 NoexceptExpr.isUsable() ?
3853 NoexceptExpr.get() : 0,
3854 LParenLoc, EndLoc, D,
3855 TrailingReturnType),
3856 attrs, EndLoc);
3857}
3858
3859/// isFunctionDeclaratorIdentifierList - This parameter list may have an
3860/// identifier list form for a K&R-style function: void foo(a,b,c)
3861///
3862/// Note that identifier-lists are only allowed for normal declarators, not for
3863/// abstract-declarators.
3864bool Parser::isFunctionDeclaratorIdentifierList() {
3865 return !getLang().CPlusPlus
3866 && Tok.is(tok::identifier)
3867 && !TryAltiVecVectorToken()
3868 // K&R identifier lists can't have typedefs as identifiers, per C99
3869 // 6.7.5.3p11.
3870 && (TryAnnotateTypeOrScopeToken() || !Tok.is(tok::annot_typename))
3871 // Identifier lists follow a really simple grammar: the identifiers can
3872 // be followed *only* by a ", identifier" or ")". However, K&R
3873 // identifier lists are really rare in the brave new modern world, and
3874 // it is very common for someone to typo a type in a non-K&R style
3875 // list. If we are presented with something like: "void foo(intptr x,
3876 // float y)", we don't want to start parsing the function declarator as
3877 // though it is a K&R style declarator just because intptr is an
3878 // invalid type.
3879 //
3880 // To handle this, we check to see if the token after the first
3881 // identifier is a "," or ")". Only then do we parse it as an
3882 // identifier list.
3883 && (NextToken().is(tok::comma) || NextToken().is(tok::r_paren));
3884}
3885
3886/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
3887/// we found a K&R-style identifier list instead of a typed parameter list.
3888///
3889/// After returning, ParamInfo will hold the parsed parameters.
3890///
3891/// identifier-list: [C99 6.7.5]
3892/// identifier
3893/// identifier-list ',' identifier
3894///
3895void Parser::ParseFunctionDeclaratorIdentifierList(
3896 Declarator &D,
Chris Lattner5f9e2722011-07-23 10:55:15 +00003897 SmallVector<DeclaratorChunk::ParamInfo, 16> &ParamInfo) {
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00003898 // If there was no identifier specified for the declarator, either we are in
3899 // an abstract-declarator, or we are in a parameter declarator which was found
3900 // to be abstract. In abstract-declarators, identifier lists are not valid:
3901 // diagnose this.
3902 if (!D.getIdentifier())
3903 Diag(Tok, diag::ext_ident_list_in_param);
3904
3905 // Maintain an efficient lookup of params we have seen so far.
3906 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
3907
3908 while (1) {
3909 // If this isn't an identifier, report the error and skip until ')'.
3910 if (Tok.isNot(tok::identifier)) {
3911 Diag(Tok, diag::err_expected_ident);
3912 SkipUntil(tok::r_paren, /*StopAtSemi=*/true, /*DontConsume=*/true);
3913 // Forget we parsed anything.
3914 ParamInfo.clear();
3915 return;
3916 }
3917
3918 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
3919
3920 // Reject 'typedef int y; int test(x, y)', but continue parsing.
3921 if (Actions.getTypeName(*ParmII, Tok.getLocation(), getCurScope()))
3922 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
3923
3924 // Verify that the argument identifier has not already been mentioned.
3925 if (!ParamsSoFar.insert(ParmII)) {
3926 Diag(Tok, diag::err_param_redefinition) << ParmII;
3927 } else {
3928 // Remember this identifier in ParamInfo.
3929 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
3930 Tok.getLocation(),
3931 0));
3932 }
3933
3934 // Eat the identifier.
3935 ConsumeToken();
3936
3937 // The list continues if we see a comma.
3938 if (Tok.isNot(tok::comma))
3939 break;
3940 ConsumeToken();
3941 }
3942}
3943
3944/// ParseParameterDeclarationClause - Parse a (possibly empty) parameter-list
3945/// after the opening parenthesis. This function will not parse a K&R-style
3946/// identifier list.
3947///
3948/// D is the declarator being parsed. If attrs is non-null, then the caller
3949/// parsed those arguments immediately after the open paren - they should be
3950/// considered to be the first argument of a parameter.
3951///
3952/// After returning, ParamInfo will hold the parsed parameters. EllipsisLoc will
3953/// be the location of the ellipsis, if any was parsed.
3954///
Reid Spencer5f016e22007-07-11 17:01:13 +00003955/// parameter-type-list: [C99 6.7.5]
3956/// parameter-list
3957/// parameter-list ',' '...'
Douglas Gregored5d6512009-09-22 21:41:40 +00003958/// [C++] parameter-list '...'
Reid Spencer5f016e22007-07-11 17:01:13 +00003959///
3960/// parameter-list: [C99 6.7.5]
3961/// parameter-declaration
3962/// parameter-list ',' parameter-declaration
3963///
3964/// parameter-declaration: [C99 6.7.5]
3965/// declaration-specifiers declarator
Chris Lattner04421082008-04-08 04:40:51 +00003966/// [C++] declaration-specifiers declarator '=' assignment-expression
Reid Spencer5f016e22007-07-11 17:01:13 +00003967/// [GNU] declaration-specifiers declarator attributes
Sebastian Redl50de12f2009-03-24 22:27:57 +00003968/// declaration-specifiers abstract-declarator[opt]
3969/// [C++] declaration-specifiers abstract-declarator[opt]
Chris Lattner8123a952008-04-10 02:22:51 +00003970/// '=' assignment-expression
Reid Spencer5f016e22007-07-11 17:01:13 +00003971/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
3972///
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00003973void Parser::ParseParameterDeclarationClause(
3974 Declarator &D,
3975 ParsedAttributes &attrs,
Chris Lattner5f9e2722011-07-23 10:55:15 +00003976 SmallVector<DeclaratorChunk::ParamInfo, 16> &ParamInfo,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00003977 SourceLocation &EllipsisLoc) {
Mike Stump1eb44332009-09-09 15:08:12 +00003978
Chris Lattnerf97409f2008-04-06 06:57:35 +00003979 while (1) {
3980 if (Tok.is(tok::ellipsis)) {
Douglas Gregor965acbb2009-02-18 07:07:28 +00003981 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chris Lattnerf97409f2008-04-06 06:57:35 +00003982 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00003983 }
Mike Stump1eb44332009-09-09 15:08:12 +00003984
Chris Lattnerf97409f2008-04-06 06:57:35 +00003985 // Parse the declaration-specifiers.
John McCall54abf7d2009-11-04 02:18:39 +00003986 // Just use the ParsingDeclaration "scope" of the declarator.
John McCall0b7e6782011-03-24 11:26:52 +00003987 DeclSpec DS(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00003988
3989 // Skip any Microsoft attributes before a param.
3990 if (getLang().Microsoft && Tok.is(tok::l_square))
3991 ParseMicrosoftAttributes(DS.getAttributes());
3992
3993 SourceLocation DSStart = Tok.getLocation();
Chris Lattner7399ee02008-10-20 02:05:46 +00003994
3995 // If the caller parsed attributes for the first argument, add them now.
John McCall7f040a92010-12-24 02:08:15 +00003996 // Take them so that we only apply the attributes to the first parameter.
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00003997 // FIXME: If we saw an ellipsis first, this code is not reached. Are the
3998 // attributes lost? Should they even be allowed?
3999 // FIXME: If we can leave the attributes in the token stream somehow, we can
4000 // get rid of a parameter (attrs) and this statement. It might be too much
4001 // hassle.
John McCall7f040a92010-12-24 02:08:15 +00004002 DS.takeAttributesFrom(attrs);
4003
Chris Lattnere64c5492009-02-27 18:38:20 +00004004 ParseDeclarationSpecifiers(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00004005
Chris Lattnerf97409f2008-04-06 06:57:35 +00004006 // Parse the declarator. This is "PrototypeContext", because we must
4007 // accept either 'declarator' or 'abstract-declarator' here.
4008 Declarator ParmDecl(DS, Declarator::PrototypeContext);
4009 ParseDeclarator(ParmDecl);
4010
4011 // Parse GNU attributes, if present.
John McCall7f040a92010-12-24 02:08:15 +00004012 MaybeParseGNUAttributes(ParmDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00004013
Chris Lattnerf97409f2008-04-06 06:57:35 +00004014 // Remember this parsed parameter in ParamInfo.
4015 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
Mike Stump1eb44332009-09-09 15:08:12 +00004016
Douglas Gregor72b505b2008-12-16 21:30:33 +00004017 // DefArgToks is used when the parsing of default arguments needs
4018 // to be delayed.
4019 CachedTokens *DefArgToks = 0;
4020
Chris Lattnerf97409f2008-04-06 06:57:35 +00004021 // If no parameter was specified, verify that *something* was specified,
4022 // otherwise we have a missing type and identifier.
Chris Lattnere64c5492009-02-27 18:38:20 +00004023 if (DS.isEmpty() && ParmDecl.getIdentifier() == 0 &&
4024 ParmDecl.getNumTypeObjects() == 0) {
Chris Lattnerf97409f2008-04-06 06:57:35 +00004025 // Completely missing, emit error.
4026 Diag(DSStart, diag::err_missing_param);
4027 } else {
4028 // Otherwise, we have something. Add it and let semantic analysis try
4029 // to grok it and add the result to the ParamInfo we are building.
Mike Stump1eb44332009-09-09 15:08:12 +00004030
Chris Lattnerf97409f2008-04-06 06:57:35 +00004031 // Inform the actions module about the parameter declarator, so it gets
4032 // added to the current scope.
John McCalld226f652010-08-21 09:40:31 +00004033 Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl);
Chris Lattner04421082008-04-08 04:40:51 +00004034
4035 // Parse the default argument, if any. We parse the default
4036 // arguments in all dialects; the semantic analysis in
4037 // ActOnParamDefaultArgument will reject the default argument in
4038 // C.
4039 if (Tok.is(tok::equal)) {
Douglas Gregor61366e92008-12-24 00:01:03 +00004040 SourceLocation EqualLoc = Tok.getLocation();
4041
Chris Lattner04421082008-04-08 04:40:51 +00004042 // Parse the default argument
Douglas Gregor72b505b2008-12-16 21:30:33 +00004043 if (D.getContext() == Declarator::MemberContext) {
4044 // If we're inside a class definition, cache the tokens
4045 // corresponding to the default argument. We'll actually parse
4046 // them when we see the end of the class definition.
4047 // FIXME: Templates will require something similar.
4048 // FIXME: Can we use a smart pointer for Toks?
4049 DefArgToks = new CachedTokens;
4050
Mike Stump1eb44332009-09-09 15:08:12 +00004051 if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks,
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +00004052 /*StopAtSemi=*/true,
4053 /*ConsumeFinalToken=*/false)) {
Douglas Gregor72b505b2008-12-16 21:30:33 +00004054 delete DefArgToks;
4055 DefArgToks = 0;
Douglas Gregor61366e92008-12-24 00:01:03 +00004056 Actions.ActOnParamDefaultArgumentError(Param);
Argyrios Kyrtzidis2b602ad2010-08-06 09:47:24 +00004057 } else {
4058 // Mark the end of the default argument so that we know when to
4059 // stop when we parse it later on.
4060 Token DefArgEnd;
4061 DefArgEnd.startToken();
4062 DefArgEnd.setKind(tok::cxx_defaultarg_end);
4063 DefArgEnd.setLocation(Tok.getLocation());
4064 DefArgToks->push_back(DefArgEnd);
Mike Stump1eb44332009-09-09 15:08:12 +00004065 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc,
Anders Carlsson5e300d12009-06-12 16:51:40 +00004066 (*DefArgToks)[1].getLocation());
Argyrios Kyrtzidis2b602ad2010-08-06 09:47:24 +00004067 }
Chris Lattner04421082008-04-08 04:40:51 +00004068 } else {
Douglas Gregor72b505b2008-12-16 21:30:33 +00004069 // Consume the '='.
Douglas Gregor61366e92008-12-24 00:01:03 +00004070 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00004071
Douglas Gregorbe0f7bd2010-09-11 20:24:53 +00004072 // The argument isn't actually potentially evaluated unless it is
4073 // used.
4074 EnterExpressionEvaluationContext Eval(Actions,
4075 Sema::PotentiallyEvaluatedIfUsed);
4076
John McCall60d7b3a2010-08-24 06:29:42 +00004077 ExprResult DefArgResult(ParseAssignmentExpression());
Douglas Gregor72b505b2008-12-16 21:30:33 +00004078 if (DefArgResult.isInvalid()) {
4079 Actions.ActOnParamDefaultArgumentError(Param);
4080 SkipUntil(tok::comma, tok::r_paren, true, true);
4081 } else {
4082 // Inform the actions module about the default argument
4083 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
John McCall9ae2f072010-08-23 23:25:46 +00004084 DefArgResult.take());
Douglas Gregor72b505b2008-12-16 21:30:33 +00004085 }
Chris Lattner04421082008-04-08 04:40:51 +00004086 }
4087 }
Mike Stump1eb44332009-09-09 15:08:12 +00004088
4089 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
4090 ParmDecl.getIdentifierLoc(), Param,
Douglas Gregor72b505b2008-12-16 21:30:33 +00004091 DefArgToks));
Chris Lattnerf97409f2008-04-06 06:57:35 +00004092 }
4093
4094 // If the next token is a comma, consume it and keep reading arguments.
Douglas Gregored5d6512009-09-22 21:41:40 +00004095 if (Tok.isNot(tok::comma)) {
4096 if (Tok.is(tok::ellipsis)) {
Douglas Gregored5d6512009-09-22 21:41:40 +00004097 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
4098
4099 if (!getLang().CPlusPlus) {
4100 // We have ellipsis without a preceding ',', which is ill-formed
4101 // in C. Complain and provide the fix.
4102 Diag(EllipsisLoc, diag::err_missing_comma_before_ellipsis)
Douglas Gregor849b2432010-03-31 17:46:05 +00004103 << FixItHint::CreateInsertion(EllipsisLoc, ", ");
Douglas Gregored5d6512009-09-22 21:41:40 +00004104 }
4105 }
4106
4107 break;
4108 }
Mike Stump1eb44332009-09-09 15:08:12 +00004109
Chris Lattnerf97409f2008-04-06 06:57:35 +00004110 // Consume the comma.
4111 ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00004112 }
Mike Stump1eb44332009-09-09 15:08:12 +00004113
Chris Lattner66d28652008-04-06 06:34:08 +00004114}
Chris Lattneref4715c2008-04-06 05:45:57 +00004115
Reid Spencer5f016e22007-07-11 17:01:13 +00004116/// [C90] direct-declarator '[' constant-expression[opt] ']'
4117/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
4118/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
4119/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
4120/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
4121void Parser::ParseBracketDeclarator(Declarator &D) {
4122 SourceLocation StartLoc = ConsumeBracket();
Mike Stump1eb44332009-09-09 15:08:12 +00004123
Chris Lattner378c7e42008-12-18 07:27:21 +00004124 // C array syntax has many features, but by-far the most common is [] and [4].
4125 // This code does a fast path to handle some of the most obvious cases.
4126 if (Tok.getKind() == tok::r_square) {
Sebastian Redlab197ba2009-02-09 18:23:29 +00004127 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
John McCall0b7e6782011-03-24 11:26:52 +00004128 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00004129 MaybeParseCXX0XAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00004130
Chris Lattner378c7e42008-12-18 07:27:21 +00004131 // Remember that we parsed the empty array type.
John McCall60d7b3a2010-08-24 06:29:42 +00004132 ExprResult NumElements;
John McCall0b7e6782011-03-24 11:26:52 +00004133 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00004134 StartLoc, EndLoc),
John McCall0b7e6782011-03-24 11:26:52 +00004135 attrs, EndLoc);
Chris Lattner378c7e42008-12-18 07:27:21 +00004136 return;
4137 } else if (Tok.getKind() == tok::numeric_constant &&
4138 GetLookAheadToken(1).is(tok::r_square)) {
4139 // [4] is very common. Parse the numeric constant expression.
John McCall60d7b3a2010-08-24 06:29:42 +00004140 ExprResult ExprRes(Actions.ActOnNumericConstant(Tok));
Chris Lattner378c7e42008-12-18 07:27:21 +00004141 ConsumeToken();
4142
Sebastian Redlab197ba2009-02-09 18:23:29 +00004143 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
John McCall0b7e6782011-03-24 11:26:52 +00004144 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00004145 MaybeParseCXX0XAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00004146
Chris Lattner378c7e42008-12-18 07:27:21 +00004147 // Remember that we parsed a array type, and remember its features.
John McCall0b7e6782011-03-24 11:26:52 +00004148 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, 0,
John McCall7f040a92010-12-24 02:08:15 +00004149 ExprRes.release(),
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00004150 StartLoc, EndLoc),
John McCall0b7e6782011-03-24 11:26:52 +00004151 attrs, EndLoc);
Chris Lattner378c7e42008-12-18 07:27:21 +00004152 return;
4153 }
Mike Stump1eb44332009-09-09 15:08:12 +00004154
Reid Spencer5f016e22007-07-11 17:01:13 +00004155 // If valid, this location is the position where we read the 'static' keyword.
4156 SourceLocation StaticLoc;
Chris Lattner04d66662007-10-09 17:33:22 +00004157 if (Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00004158 StaticLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00004159
Reid Spencer5f016e22007-07-11 17:01:13 +00004160 // If there is a type-qualifier-list, read it now.
Chris Lattnera1fcbad2008-12-18 06:50:14 +00004161 // Type qualifiers in an array subscript are a C99 feature.
John McCall0b7e6782011-03-24 11:26:52 +00004162 DeclSpec DS(AttrFactory);
Chris Lattner5a69d1c2008-12-18 07:02:59 +00004163 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Mike Stump1eb44332009-09-09 15:08:12 +00004164
Reid Spencer5f016e22007-07-11 17:01:13 +00004165 // If we haven't already read 'static', check to see if there is one after the
4166 // type-qualifier-list.
Chris Lattner04d66662007-10-09 17:33:22 +00004167 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00004168 StaticLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00004169
Reid Spencer5f016e22007-07-11 17:01:13 +00004170 // Handle "direct-declarator [ type-qual-list[opt] * ]".
4171 bool isStar = false;
John McCall60d7b3a2010-08-24 06:29:42 +00004172 ExprResult NumElements;
Mike Stump1eb44332009-09-09 15:08:12 +00004173
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00004174 // Handle the case where we have '[*]' as the array size. However, a leading
4175 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
4176 // the the token after the star is a ']'. Since stars in arrays are
4177 // infrequent, use of lookahead is not costly here.
4178 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattnera711dd02008-04-06 05:27:21 +00004179 ConsumeToken(); // Eat the '*'.
Reid Spencer5f016e22007-07-11 17:01:13 +00004180
Chris Lattnera1fcbad2008-12-18 06:50:14 +00004181 if (StaticLoc.isValid()) {
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00004182 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattnera1fcbad2008-12-18 06:50:14 +00004183 StaticLoc = SourceLocation(); // Drop the static.
4184 }
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00004185 isStar = true;
Chris Lattner04d66662007-10-09 17:33:22 +00004186 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner378c7e42008-12-18 07:27:21 +00004187 // Note, in C89, this production uses the constant-expr production instead
4188 // of assignment-expr. The only difference is that assignment-expr allows
4189 // things like '=' and '*='. Sema rejects these in C89 mode because they
4190 // are not i-c-e's, so we don't need to distinguish between the two here.
Mike Stump1eb44332009-09-09 15:08:12 +00004191
Douglas Gregore0762c92009-06-19 23:52:42 +00004192 // Parse the constant-expression or assignment-expression now (depending
4193 // on dialect).
4194 if (getLang().CPlusPlus)
4195 NumElements = ParseConstantExpression();
4196 else
4197 NumElements = ParseAssignmentExpression();
Reid Spencer5f016e22007-07-11 17:01:13 +00004198 }
Mike Stump1eb44332009-09-09 15:08:12 +00004199
Reid Spencer5f016e22007-07-11 17:01:13 +00004200 // If there was an error parsing the assignment-expression, recover.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00004201 if (NumElements.isInvalid()) {
Chris Lattner5cb10d32009-04-24 22:30:50 +00004202 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00004203 // If the expression was invalid, skip it.
4204 SkipUntil(tok::r_square);
4205 return;
4206 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00004207
4208 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
4209
John McCall0b7e6782011-03-24 11:26:52 +00004210 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00004211 MaybeParseCXX0XAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00004212
Chris Lattner378c7e42008-12-18 07:27:21 +00004213 // Remember that we parsed a array type, and remember its features.
John McCall0b7e6782011-03-24 11:26:52 +00004214 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
Reid Spencer5f016e22007-07-11 17:01:13 +00004215 StaticLoc.isValid(), isStar,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00004216 NumElements.release(),
4217 StartLoc, EndLoc),
John McCall0b7e6782011-03-24 11:26:52 +00004218 attrs, EndLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00004219}
4220
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004221/// [GNU] typeof-specifier:
4222/// typeof ( expressions )
4223/// typeof ( type-name )
4224/// [GNU/C++] typeof unary-expression
Steve Naroffd1861fd2007-07-31 12:34:36 +00004225///
4226void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner04d66662007-10-09 17:33:22 +00004227 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004228 Token OpTok = Tok;
Steve Naroffd1861fd2007-07-31 12:34:36 +00004229 SourceLocation StartLoc = ConsumeToken();
4230
John McCallcfb708c2010-01-13 20:03:27 +00004231 const bool hasParens = Tok.is(tok::l_paren);
4232
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004233 bool isCastExpr;
John McCallb3d87482010-08-24 05:47:05 +00004234 ParsedType CastTy;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004235 SourceRange CastRange;
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00004236 ExprResult Operand = ParseExprAfterUnaryExprOrTypeTrait(OpTok, isCastExpr,
4237 CastTy, CastRange);
John McCallcfb708c2010-01-13 20:03:27 +00004238 if (hasParens)
4239 DS.setTypeofParensRange(CastRange);
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004240
4241 if (CastRange.getEnd().isInvalid())
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004242 // FIXME: Not accurate, the range gets one token more than it should.
4243 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004244 else
4245 DS.SetRangeEnd(CastRange.getEnd());
Mike Stump1eb44332009-09-09 15:08:12 +00004246
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004247 if (isCastExpr) {
4248 if (!CastTy) {
4249 DS.SetTypeSpecError();
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004250 return;
Douglas Gregor809070a2009-02-18 17:45:20 +00004251 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004252
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004253 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00004254 unsigned DiagID;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004255 // Check for duplicate type specifiers (e.g. "int typeof(int)").
4256 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00004257 DiagID, CastTy))
4258 Diag(StartLoc, DiagID) << PrevSpec;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004259 return;
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004260 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004261
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004262 // If we get here, the operand to the typeof was an expresion.
4263 if (Operand.isInvalid()) {
4264 DS.SetTypeSpecError();
Steve Naroff9dfa7b42007-08-02 02:53:48 +00004265 return;
Steve Naroffd1861fd2007-07-31 12:34:36 +00004266 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004267
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004268 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00004269 unsigned DiagID;
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004270 // Check for duplicate type specifiers (e.g. "int typeof(int)").
4271 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
John McCallb3d87482010-08-24 05:47:05 +00004272 DiagID, Operand.get()))
John McCallfec54012009-08-03 20:12:06 +00004273 Diag(StartLoc, DiagID) << PrevSpec;
Steve Naroffd1861fd2007-07-31 12:34:36 +00004274}
Chris Lattner1b492422010-02-28 18:33:55 +00004275
4276
4277/// TryAltiVecVectorTokenOutOfLine - Out of line body that should only be called
4278/// from TryAltiVecVectorToken.
4279bool Parser::TryAltiVecVectorTokenOutOfLine() {
4280 Token Next = NextToken();
4281 switch (Next.getKind()) {
4282 default: return false;
4283 case tok::kw_short:
4284 case tok::kw_long:
4285 case tok::kw_signed:
4286 case tok::kw_unsigned:
4287 case tok::kw_void:
4288 case tok::kw_char:
4289 case tok::kw_int:
4290 case tok::kw_float:
4291 case tok::kw_double:
4292 case tok::kw_bool:
4293 case tok::kw___pixel:
4294 Tok.setKind(tok::kw___vector);
4295 return true;
4296 case tok::identifier:
4297 if (Next.getIdentifierInfo() == Ident_pixel) {
4298 Tok.setKind(tok::kw___vector);
4299 return true;
4300 }
4301 return false;
4302 }
4303}
4304
4305bool Parser::TryAltiVecTokenOutOfLine(DeclSpec &DS, SourceLocation Loc,
4306 const char *&PrevSpec, unsigned &DiagID,
4307 bool &isInvalid) {
4308 if (Tok.getIdentifierInfo() == Ident_vector) {
4309 Token Next = NextToken();
4310 switch (Next.getKind()) {
4311 case tok::kw_short:
4312 case tok::kw_long:
4313 case tok::kw_signed:
4314 case tok::kw_unsigned:
4315 case tok::kw_void:
4316 case tok::kw_char:
4317 case tok::kw_int:
4318 case tok::kw_float:
4319 case tok::kw_double:
4320 case tok::kw_bool:
4321 case tok::kw___pixel:
4322 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
4323 return true;
4324 case tok::identifier:
4325 if (Next.getIdentifierInfo() == Ident_pixel) {
4326 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
4327 return true;
4328 }
4329 break;
4330 default:
4331 break;
4332 }
Douglas Gregora8f031f2010-06-16 15:28:57 +00004333 } else if ((Tok.getIdentifierInfo() == Ident_pixel) &&
Chris Lattner1b492422010-02-28 18:33:55 +00004334 DS.isTypeAltiVecVector()) {
4335 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
4336 return true;
4337 }
4338 return false;
4339}