blob: ad3fcfe0d36945f8e5ae8ce56a6a7e35b105aa9a [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- ParseDecl.cpp - Declaration Parsing ------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Declaration portions of the Parser interfaces.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Parse/Parser.h"
Chris Lattner500d3292009-01-29 05:15:15 +000015#include "clang/Parse/ParseDiagnostic.h"
Peter Collingbourne207f4d82011-03-18 22:38:29 +000016#include "clang/Basic/OpenCL.h"
John McCall19510852010-08-20 18:27:03 +000017#include "clang/Sema/Scope.h"
18#include "clang/Sema/ParsedTemplate.h"
John McCallf312b1e2010-08-26 23:41:50 +000019#include "clang/Sema/PrettyDeclStackTrace.h"
Chris Lattnerd167ca02009-12-10 00:21:05 +000020#include "RAIIObjectsForParser.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000021#include "llvm/ADT/SmallSet.h"
22using namespace clang;
23
24//===----------------------------------------------------------------------===//
25// C99 6.7: Declarations.
26//===----------------------------------------------------------------------===//
27
28/// ParseTypeName
29/// type-name: [C99 6.7.6]
30/// specifier-qualifier-list abstract-declarator[opt]
Sebastian Redl4c5d3202008-11-21 19:14:01 +000031///
32/// Called type-id in C++.
Douglas Gregor683a81f2011-01-31 16:09:46 +000033TypeResult Parser::ParseTypeName(SourceRange *Range,
34 Declarator::TheContext Context) {
Reid Spencer5f016e22007-07-11 17:01:13 +000035 // Parse the common declaration-specifiers piece.
John McCall0b7e6782011-03-24 11:26:52 +000036 DeclSpec DS(AttrFactory);
Reid Spencer5f016e22007-07-11 17:01:13 +000037 ParseSpecifierQualifierList(DS);
Sebastian Redlef65f062009-05-29 18:02:33 +000038
Reid Spencer5f016e22007-07-11 17:01:13 +000039 // Parse the abstract-declarator, if present.
Douglas Gregor683a81f2011-01-31 16:09:46 +000040 Declarator DeclaratorInfo(DS, Context);
Reid Spencer5f016e22007-07-11 17:01:13 +000041 ParseDeclarator(DeclaratorInfo);
Sebastian Redlef65f062009-05-29 18:02:33 +000042 if (Range)
43 *Range = DeclaratorInfo.getSourceRange();
44
Chris Lattnereaaebc72009-04-25 08:06:05 +000045 if (DeclaratorInfo.isInvalidType())
Douglas Gregor809070a2009-02-18 17:45:20 +000046 return true;
47
Douglas Gregor23c94db2010-07-02 17:43:08 +000048 return Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
Reid Spencer5f016e22007-07-11 17:01:13 +000049}
50
Sean Huntbbd37c62009-11-21 08:43:09 +000051/// ParseGNUAttributes - Parse a non-empty attributes list.
Reid Spencer5f016e22007-07-11 17:01:13 +000052///
53/// [GNU] attributes:
54/// attribute
55/// attributes attribute
56///
57/// [GNU] attribute:
58/// '__attribute__' '(' '(' attribute-list ')' ')'
59///
60/// [GNU] attribute-list:
61/// attrib
62/// attribute_list ',' attrib
63///
64/// [GNU] attrib:
65/// empty
66/// attrib-name
67/// attrib-name '(' identifier ')'
68/// attrib-name '(' identifier ',' nonempty-expr-list ')'
69/// attrib-name '(' argument-expression-list [C99 6.5.2] ')'
70///
71/// [GNU] attrib-name:
72/// identifier
73/// typespec
74/// typequal
75/// storageclass
Mike Stump1eb44332009-09-09 15:08:12 +000076///
Reid Spencer5f016e22007-07-11 17:01:13 +000077/// FIXME: The GCC grammar/code for this construct implies we need two
Mike Stump1eb44332009-09-09 15:08:12 +000078/// token lookahead. Comment from gcc: "If they start with an identifier
79/// which is followed by a comma or close parenthesis, then the arguments
Reid Spencer5f016e22007-07-11 17:01:13 +000080/// start with that identifier; otherwise they are an expression list."
81///
82/// At the moment, I am not doing 2 token lookahead. I am also unaware of
83/// any attributes that don't work (based on my limited testing). Most
84/// attributes are very simple in practice. Until we find a bug, I don't see
85/// a pressing need to implement the 2 token lookahead.
86
John McCall7f040a92010-12-24 02:08:15 +000087void Parser::ParseGNUAttributes(ParsedAttributes &attrs,
88 SourceLocation *endLoc) {
Sean Huntbbd37c62009-11-21 08:43:09 +000089 assert(Tok.is(tok::kw___attribute) && "Not a GNU attribute list!");
Mike Stump1eb44332009-09-09 15:08:12 +000090
Chris Lattner04d66662007-10-09 17:33:22 +000091 while (Tok.is(tok::kw___attribute)) {
Reid Spencer5f016e22007-07-11 17:01:13 +000092 ConsumeToken();
93 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
94 "attribute")) {
95 SkipUntil(tok::r_paren, true); // skip until ) or ;
John McCall7f040a92010-12-24 02:08:15 +000096 return;
Reid Spencer5f016e22007-07-11 17:01:13 +000097 }
98 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, "(")) {
99 SkipUntil(tok::r_paren, true); // skip until ) or ;
John McCall7f040a92010-12-24 02:08:15 +0000100 return;
Reid Spencer5f016e22007-07-11 17:01:13 +0000101 }
102 // Parse the attribute-list. e.g. __attribute__(( weak, alias("__f") ))
Chris Lattner04d66662007-10-09 17:33:22 +0000103 while (Tok.is(tok::identifier) || isDeclarationSpecifier() ||
104 Tok.is(tok::comma)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000105
106 if (Tok.is(tok::comma)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000107 // allows for empty/non-empty attributes. ((__vector_size__(16),,,,))
108 ConsumeToken();
109 continue;
110 }
111 // we have an identifier or declaration specifier (const, int, etc.)
112 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
113 SourceLocation AttrNameLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000114
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000115 // Availability attributes have their own grammar.
116 if (AttrName->isStr("availability"))
117 ParseAvailabilityAttribute(*AttrName, AttrNameLoc, attrs, endLoc);
Douglas Gregorec1afbf2010-03-16 19:09:18 +0000118 // check if we have a "parameterized" attribute
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000119 else if (Tok.is(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000120 ConsumeParen(); // ignore the left paren loc for now
Mike Stump1eb44332009-09-09 15:08:12 +0000121
Chris Lattner04d66662007-10-09 17:33:22 +0000122 if (Tok.is(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000123 IdentifierInfo *ParmName = Tok.getIdentifierInfo();
124 SourceLocation ParmLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000125
126 if (Tok.is(tok::r_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000127 // __attribute__(( mode(byte) ))
128 ConsumeParen(); // ignore the right paren loc for now
John McCall0b7e6782011-03-24 11:26:52 +0000129 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc,
130 ParmName, ParmLoc, 0, 0);
Chris Lattner04d66662007-10-09 17:33:22 +0000131 } else if (Tok.is(tok::comma)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000132 ConsumeToken();
133 // __attribute__(( format(printf, 1, 2) ))
Sebastian Redla55e52c2008-11-25 22:21:31 +0000134 ExprVector ArgExprs(Actions);
Reid Spencer5f016e22007-07-11 17:01:13 +0000135 bool ArgExprsOk = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000136
Reid Spencer5f016e22007-07-11 17:01:13 +0000137 // now parse the non-empty comma separated list of expressions
138 while (1) {
John McCall60d7b3a2010-08-24 06:29:42 +0000139 ExprResult ArgExpr(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000140 if (ArgExpr.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000141 ArgExprsOk = false;
142 SkipUntil(tok::r_paren);
143 break;
144 } else {
Sebastian Redleffa8d12008-12-10 00:02:53 +0000145 ArgExprs.push_back(ArgExpr.release());
Reid Spencer5f016e22007-07-11 17:01:13 +0000146 }
Chris Lattner04d66662007-10-09 17:33:22 +0000147 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +0000148 break;
149 ConsumeToken(); // Eat the comma, move to the next argument
150 }
Chris Lattner04d66662007-10-09 17:33:22 +0000151 if (ArgExprsOk && Tok.is(tok::r_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000152 ConsumeParen(); // ignore the right paren loc for now
John McCall0b7e6782011-03-24 11:26:52 +0000153 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc,
154 ParmName, ParmLoc, ArgExprs.take(), ArgExprs.size());
Reid Spencer5f016e22007-07-11 17:01:13 +0000155 }
156 }
157 } else { // not an identifier
Nate Begeman6f3d8382009-06-26 06:32:41 +0000158 switch (Tok.getKind()) {
159 case tok::r_paren:
Reid Spencer5f016e22007-07-11 17:01:13 +0000160 // parse a possibly empty comma separated list of expressions
Reid Spencer5f016e22007-07-11 17:01:13 +0000161 // __attribute__(( nonnull() ))
162 ConsumeParen(); // ignore the right paren loc for now
John McCall0b7e6782011-03-24 11:26:52 +0000163 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc,
164 0, SourceLocation(), 0, 0);
Nate Begeman6f3d8382009-06-26 06:32:41 +0000165 break;
166 case tok::kw_char:
167 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000168 case tok::kw_char16_t:
169 case tok::kw_char32_t:
Nate Begeman6f3d8382009-06-26 06:32:41 +0000170 case tok::kw_bool:
171 case tok::kw_short:
172 case tok::kw_int:
173 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +0000174 case tok::kw___int64:
Nate Begeman6f3d8382009-06-26 06:32:41 +0000175 case tok::kw_signed:
176 case tok::kw_unsigned:
177 case tok::kw_float:
178 case tok::kw_double:
179 case tok::kw_void:
John McCall7f040a92010-12-24 02:08:15 +0000180 case tok::kw_typeof: {
181 AttributeList *attr
John McCall0b7e6782011-03-24 11:26:52 +0000182 = attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc,
183 0, SourceLocation(), 0, 0);
John McCall7f040a92010-12-24 02:08:15 +0000184 if (attr->getKind() == AttributeList::AT_IBOutletCollection)
Fariborz Jahanian1b72fa72010-08-17 23:19:16 +0000185 Diag(Tok, diag::err_iboutletcollection_builtintype);
Nate Begeman6f3d8382009-06-26 06:32:41 +0000186 // If it's a builtin type name, eat it and expect a rparen
187 // __attribute__(( vec_type_hint(char) ))
188 ConsumeToken();
Nate Begeman6f3d8382009-06-26 06:32:41 +0000189 if (Tok.is(tok::r_paren))
190 ConsumeParen();
191 break;
John McCall7f040a92010-12-24 02:08:15 +0000192 }
Nate Begeman6f3d8382009-06-26 06:32:41 +0000193 default:
Reid Spencer5f016e22007-07-11 17:01:13 +0000194 // __attribute__(( aligned(16) ))
Sebastian Redla55e52c2008-11-25 22:21:31 +0000195 ExprVector ArgExprs(Actions);
Reid Spencer5f016e22007-07-11 17:01:13 +0000196 bool ArgExprsOk = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000197
Reid Spencer5f016e22007-07-11 17:01:13 +0000198 // now parse the list of expressions
199 while (1) {
John McCall60d7b3a2010-08-24 06:29:42 +0000200 ExprResult ArgExpr(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000201 if (ArgExpr.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000202 ArgExprsOk = false;
203 SkipUntil(tok::r_paren);
204 break;
205 } else {
Sebastian Redleffa8d12008-12-10 00:02:53 +0000206 ArgExprs.push_back(ArgExpr.release());
Reid Spencer5f016e22007-07-11 17:01:13 +0000207 }
Chris Lattner04d66662007-10-09 17:33:22 +0000208 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +0000209 break;
210 ConsumeToken(); // Eat the comma, move to the next argument
211 }
212 // Match the ')'.
Chris Lattner04d66662007-10-09 17:33:22 +0000213 if (ArgExprsOk && Tok.is(tok::r_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000214 ConsumeParen(); // ignore the right paren loc for now
John McCall0b7e6782011-03-24 11:26:52 +0000215 attrs.addNew(AttrName, AttrNameLoc, 0,
216 AttrNameLoc, 0, SourceLocation(),
217 ArgExprs.take(), ArgExprs.size());
Reid Spencer5f016e22007-07-11 17:01:13 +0000218 }
Nate Begeman6f3d8382009-06-26 06:32:41 +0000219 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000220 }
221 }
222 } else {
John McCall0b7e6782011-03-24 11:26:52 +0000223 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc,
224 0, SourceLocation(), 0, 0);
Reid Spencer5f016e22007-07-11 17:01:13 +0000225 }
226 }
227 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
Reid Spencer5f016e22007-07-11 17:01:13 +0000228 SkipUntil(tok::r_paren, false);
Sean Huntbbd37c62009-11-21 08:43:09 +0000229 SourceLocation Loc = Tok.getLocation();
Sebastian Redlab197ba2009-02-09 18:23:29 +0000230 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) {
231 SkipUntil(tok::r_paren, false);
232 }
John McCall7f040a92010-12-24 02:08:15 +0000233 if (endLoc)
234 *endLoc = Loc;
Reid Spencer5f016e22007-07-11 17:01:13 +0000235 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000236}
237
Eli Friedmana23b4852009-06-08 07:21:15 +0000238/// ParseMicrosoftDeclSpec - Parse an __declspec construct
239///
240/// [MS] decl-specifier:
241/// __declspec ( extended-decl-modifier-seq )
242///
243/// [MS] extended-decl-modifier-seq:
244/// extended-decl-modifier[opt]
245/// extended-decl-modifier extended-decl-modifier-seq
246
John McCall7f040a92010-12-24 02:08:15 +0000247void Parser::ParseMicrosoftDeclSpec(ParsedAttributes &attrs) {
Steve Narofff59e17e2008-12-24 20:59:21 +0000248 assert(Tok.is(tok::kw___declspec) && "Not a declspec!");
Eli Friedmana23b4852009-06-08 07:21:15 +0000249
Steve Narofff59e17e2008-12-24 20:59:21 +0000250 ConsumeToken();
Eli Friedmana23b4852009-06-08 07:21:15 +0000251 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
252 "declspec")) {
253 SkipUntil(tok::r_paren, true); // skip until ) or ;
John McCall7f040a92010-12-24 02:08:15 +0000254 return;
Eli Friedmana23b4852009-06-08 07:21:15 +0000255 }
Francois Pichet373197b2011-05-07 19:04:49 +0000256
Eli Friedman290eeb02009-06-08 23:27:34 +0000257 while (Tok.getIdentifierInfo()) {
Eli Friedmana23b4852009-06-08 07:21:15 +0000258 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
259 SourceLocation AttrNameLoc = ConsumeToken();
Francois Pichet373197b2011-05-07 19:04:49 +0000260
261 // FIXME: Remove this when we have proper __declspec(property()) support.
262 // Just skip everything inside property().
263 if (AttrName->getName() == "property") {
264 ConsumeParen();
265 SkipUntil(tok::r_paren);
266 }
Eli Friedmana23b4852009-06-08 07:21:15 +0000267 if (Tok.is(tok::l_paren)) {
268 ConsumeParen();
269 // FIXME: This doesn't parse __declspec(property(get=get_func_name))
270 // correctly.
John McCall60d7b3a2010-08-24 06:29:42 +0000271 ExprResult ArgExpr(ParseAssignmentExpression());
Eli Friedmana23b4852009-06-08 07:21:15 +0000272 if (!ArgExpr.isInvalid()) {
John McCallca0408f2010-08-23 06:44:23 +0000273 Expr *ExprList = ArgExpr.take();
John McCall0b7e6782011-03-24 11:26:52 +0000274 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
275 SourceLocation(), &ExprList, 1, true);
Eli Friedmana23b4852009-06-08 07:21:15 +0000276 }
277 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
278 SkipUntil(tok::r_paren, false);
279 } else {
John McCall0b7e6782011-03-24 11:26:52 +0000280 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc,
281 0, SourceLocation(), 0, 0, true);
Eli Friedmana23b4852009-06-08 07:21:15 +0000282 }
283 }
284 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
285 SkipUntil(tok::r_paren, false);
John McCall7f040a92010-12-24 02:08:15 +0000286 return;
Eli Friedman290eeb02009-06-08 23:27:34 +0000287}
288
John McCall7f040a92010-12-24 02:08:15 +0000289void Parser::ParseMicrosoftTypeAttributes(ParsedAttributes &attrs) {
Eli Friedman290eeb02009-06-08 23:27:34 +0000290 // Treat these like attributes
291 // FIXME: Allow Sema to distinguish between these and real attributes!
292 while (Tok.is(tok::kw___fastcall) || Tok.is(tok::kw___stdcall) ||
Douglas Gregorf813a2c2010-05-18 16:57:00 +0000293 Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___cdecl) ||
294 Tok.is(tok::kw___ptr64) || Tok.is(tok::kw___w64)) {
Eli Friedman290eeb02009-06-08 23:27:34 +0000295 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
296 SourceLocation AttrNameLoc = ConsumeToken();
297 if (Tok.is(tok::kw___ptr64) || Tok.is(tok::kw___w64))
298 // FIXME: Support these properly!
299 continue;
John McCall0b7e6782011-03-24 11:26:52 +0000300 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
301 SourceLocation(), 0, 0, true);
Eli Friedman290eeb02009-06-08 23:27:34 +0000302 }
Steve Narofff59e17e2008-12-24 20:59:21 +0000303}
304
John McCall7f040a92010-12-24 02:08:15 +0000305void Parser::ParseBorlandTypeAttributes(ParsedAttributes &attrs) {
Dawn Perchik52fc3142010-09-03 01:29:35 +0000306 // Treat these like attributes
307 while (Tok.is(tok::kw___pascal)) {
308 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
309 SourceLocation AttrNameLoc = ConsumeToken();
John McCall0b7e6782011-03-24 11:26:52 +0000310 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
311 SourceLocation(), 0, 0, true);
Dawn Perchik52fc3142010-09-03 01:29:35 +0000312 }
John McCall7f040a92010-12-24 02:08:15 +0000313}
314
Peter Collingbournef315fa82011-02-14 01:42:53 +0000315void Parser::ParseOpenCLAttributes(ParsedAttributes &attrs) {
316 // Treat these like attributes
317 while (Tok.is(tok::kw___kernel)) {
318 SourceLocation AttrNameLoc = ConsumeToken();
John McCall0b7e6782011-03-24 11:26:52 +0000319 attrs.addNew(PP.getIdentifierInfo("opencl_kernel_function"),
320 AttrNameLoc, 0, AttrNameLoc, 0,
321 SourceLocation(), 0, 0, false);
Peter Collingbournef315fa82011-02-14 01:42:53 +0000322 }
323}
324
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000325void Parser::ParseOpenCLQualifiers(DeclSpec &DS) {
326 SourceLocation Loc = Tok.getLocation();
327 switch(Tok.getKind()) {
328 // OpenCL qualifiers:
329 case tok::kw___private:
330 case tok::kw_private:
John McCall0b7e6782011-03-24 11:26:52 +0000331 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000332 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000333 PP.getIdentifierInfo("address_space"), Loc, 0);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000334 break;
335
336 case tok::kw___global:
John McCall0b7e6782011-03-24 11:26:52 +0000337 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000338 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000339 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_global);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000340 break;
341
342 case tok::kw___local:
John McCall0b7e6782011-03-24 11:26:52 +0000343 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000344 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000345 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_local);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000346 break;
347
348 case tok::kw___constant:
John McCall0b7e6782011-03-24 11:26:52 +0000349 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000350 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000351 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_constant);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000352 break;
353
354 case tok::kw___read_only:
John McCall0b7e6782011-03-24 11:26:52 +0000355 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000356 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000357 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_read_only);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000358 break;
359
360 case tok::kw___write_only:
John McCall0b7e6782011-03-24 11:26:52 +0000361 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000362 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000363 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_write_only);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000364 break;
365
366 case tok::kw___read_write:
John McCall0b7e6782011-03-24 11:26:52 +0000367 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000368 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000369 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_read_write);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000370 break;
371 default: break;
372 }
373}
374
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000375/// \brief Parse a version number.
376///
377/// version:
378/// simple-integer
379/// simple-integer ',' simple-integer
380/// simple-integer ',' simple-integer ',' simple-integer
381VersionTuple Parser::ParseVersionTuple(SourceRange &Range) {
382 Range = Tok.getLocation();
383
384 if (!Tok.is(tok::numeric_constant)) {
385 Diag(Tok, diag::err_expected_version);
386 SkipUntil(tok::comma, tok::r_paren, true, true, true);
387 return VersionTuple();
388 }
389
390 // Parse the major (and possibly minor and subminor) versions, which
391 // are stored in the numeric constant. We utilize a quirk of the
392 // lexer, which is that it handles something like 1.2.3 as a single
393 // numeric constant, rather than two separate tokens.
394 llvm::SmallString<512> Buffer;
395 Buffer.resize(Tok.getLength()+1);
396 const char *ThisTokBegin = &Buffer[0];
397
398 // Get the spelling of the token, which eliminates trigraphs, etc.
399 bool Invalid = false;
400 unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin, &Invalid);
401 if (Invalid)
402 return VersionTuple();
403
404 // Parse the major version.
405 unsigned AfterMajor = 0;
406 unsigned Major = 0;
407 while (AfterMajor < ActualLength && isdigit(ThisTokBegin[AfterMajor])) {
408 Major = Major * 10 + ThisTokBegin[AfterMajor] - '0';
409 ++AfterMajor;
410 }
411
412 if (AfterMajor == 0) {
413 Diag(Tok, diag::err_expected_version);
414 SkipUntil(tok::comma, tok::r_paren, true, true, true);
415 return VersionTuple();
416 }
417
418 if (AfterMajor == ActualLength) {
419 ConsumeToken();
420
421 // We only had a single version component.
422 if (Major == 0) {
423 Diag(Tok, diag::err_zero_version);
424 return VersionTuple();
425 }
426
427 return VersionTuple(Major);
428 }
429
430 if (ThisTokBegin[AfterMajor] != '.' || (AfterMajor + 1 == ActualLength)) {
431 Diag(Tok, diag::err_expected_version);
432 SkipUntil(tok::comma, tok::r_paren, true, true, true);
433 return VersionTuple();
434 }
435
436 // Parse the minor version.
437 unsigned AfterMinor = AfterMajor + 1;
438 unsigned Minor = 0;
439 while (AfterMinor < ActualLength && isdigit(ThisTokBegin[AfterMinor])) {
440 Minor = Minor * 10 + ThisTokBegin[AfterMinor] - '0';
441 ++AfterMinor;
442 }
443
444 if (AfterMinor == ActualLength) {
445 ConsumeToken();
446
447 // We had major.minor.
448 if (Major == 0 && Minor == 0) {
449 Diag(Tok, diag::err_zero_version);
450 return VersionTuple();
451 }
452
453 return VersionTuple(Major, Minor);
454 }
455
456 // If what follows is not a '.', we have a problem.
457 if (ThisTokBegin[AfterMinor] != '.') {
458 Diag(Tok, diag::err_expected_version);
459 SkipUntil(tok::comma, tok::r_paren, true, true, true);
460 return VersionTuple();
461 }
462
463 // Parse the subminor version.
464 unsigned AfterSubminor = AfterMinor + 1;
465 unsigned Subminor = 0;
466 while (AfterSubminor < ActualLength && isdigit(ThisTokBegin[AfterSubminor])) {
467 Subminor = Subminor * 10 + ThisTokBegin[AfterSubminor] - '0';
468 ++AfterSubminor;
469 }
470
471 if (AfterSubminor != ActualLength) {
472 Diag(Tok, diag::err_expected_version);
473 SkipUntil(tok::comma, tok::r_paren, true, true, true);
474 return VersionTuple();
475 }
476 ConsumeToken();
477 return VersionTuple(Major, Minor, Subminor);
478}
479
480/// \brief Parse the contents of the "availability" attribute.
481///
482/// availability-attribute:
483/// 'availability' '(' platform ',' version-arg-list ')'
484///
485/// platform:
486/// identifier
487///
488/// version-arg-list:
489/// version-arg
490/// version-arg ',' version-arg-list
491///
492/// version-arg:
493/// 'introduced' '=' version
494/// 'deprecated' '=' version
495/// 'removed' = version
Douglas Gregorb53e4172011-03-26 03:35:55 +0000496/// 'unavailable'
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000497void Parser::ParseAvailabilityAttribute(IdentifierInfo &Availability,
498 SourceLocation AvailabilityLoc,
499 ParsedAttributes &attrs,
500 SourceLocation *endLoc) {
501 SourceLocation PlatformLoc;
502 IdentifierInfo *Platform = 0;
503
504 enum { Introduced, Deprecated, Obsoleted, Unknown };
505 AvailabilityChange Changes[Unknown];
506
507 // Opening '('.
508 SourceLocation LParenLoc;
509 if (!Tok.is(tok::l_paren)) {
510 Diag(Tok, diag::err_expected_lparen);
511 return;
512 }
513 LParenLoc = ConsumeParen();
514
515 // Parse the platform name,
516 if (Tok.isNot(tok::identifier)) {
517 Diag(Tok, diag::err_availability_expected_platform);
518 SkipUntil(tok::r_paren);
519 return;
520 }
521 Platform = Tok.getIdentifierInfo();
522 PlatformLoc = ConsumeToken();
523
524 // Parse the ',' following the platform name.
525 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "", tok::r_paren))
526 return;
527
528 // If we haven't grabbed the pointers for the identifiers
529 // "introduced", "deprecated", and "obsoleted", do so now.
530 if (!Ident_introduced) {
531 Ident_introduced = PP.getIdentifierInfo("introduced");
532 Ident_deprecated = PP.getIdentifierInfo("deprecated");
533 Ident_obsoleted = PP.getIdentifierInfo("obsoleted");
Douglas Gregorb53e4172011-03-26 03:35:55 +0000534 Ident_unavailable = PP.getIdentifierInfo("unavailable");
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000535 }
536
537 // Parse the set of introductions/deprecations/removals.
Douglas Gregorb53e4172011-03-26 03:35:55 +0000538 SourceLocation UnavailableLoc;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000539 do {
540 if (Tok.isNot(tok::identifier)) {
541 Diag(Tok, diag::err_availability_expected_change);
542 SkipUntil(tok::r_paren);
543 return;
544 }
545 IdentifierInfo *Keyword = Tok.getIdentifierInfo();
546 SourceLocation KeywordLoc = ConsumeToken();
547
Douglas Gregorb53e4172011-03-26 03:35:55 +0000548 if (Keyword == Ident_unavailable) {
549 if (UnavailableLoc.isValid()) {
550 Diag(KeywordLoc, diag::err_availability_redundant)
551 << Keyword << SourceRange(UnavailableLoc);
552 }
553 UnavailableLoc = KeywordLoc;
554
555 if (Tok.isNot(tok::comma))
556 break;
557
558 ConsumeToken();
559 continue;
560 }
561
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000562 if (Tok.isNot(tok::equal)) {
563 Diag(Tok, diag::err_expected_equal_after)
564 << Keyword;
565 SkipUntil(tok::r_paren);
566 return;
567 }
568 ConsumeToken();
569
570 SourceRange VersionRange;
571 VersionTuple Version = ParseVersionTuple(VersionRange);
572
573 if (Version.empty()) {
574 SkipUntil(tok::r_paren);
575 return;
576 }
577
578 unsigned Index;
579 if (Keyword == Ident_introduced)
580 Index = Introduced;
581 else if (Keyword == Ident_deprecated)
582 Index = Deprecated;
583 else if (Keyword == Ident_obsoleted)
584 Index = Obsoleted;
585 else
586 Index = Unknown;
587
588 if (Index < Unknown) {
589 if (!Changes[Index].KeywordLoc.isInvalid()) {
590 Diag(KeywordLoc, diag::err_availability_redundant)
591 << Keyword
592 << SourceRange(Changes[Index].KeywordLoc,
593 Changes[Index].VersionRange.getEnd());
594 }
595
596 Changes[Index].KeywordLoc = KeywordLoc;
597 Changes[Index].Version = Version;
598 Changes[Index].VersionRange = VersionRange;
599 } else {
600 Diag(KeywordLoc, diag::err_availability_unknown_change)
601 << Keyword << VersionRange;
602 }
603
604 if (Tok.isNot(tok::comma))
605 break;
606
607 ConsumeToken();
608 } while (true);
609
610 // Closing ')'.
611 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
612 if (RParenLoc.isInvalid())
613 return;
614
615 if (endLoc)
616 *endLoc = RParenLoc;
617
Douglas Gregorb53e4172011-03-26 03:35:55 +0000618 // The 'unavailable' availability cannot be combined with any other
619 // availability changes. Make sure that hasn't happened.
620 if (UnavailableLoc.isValid()) {
621 bool Complained = false;
622 for (unsigned Index = Introduced; Index != Unknown; ++Index) {
623 if (Changes[Index].KeywordLoc.isValid()) {
624 if (!Complained) {
625 Diag(UnavailableLoc, diag::warn_availability_and_unavailable)
626 << SourceRange(Changes[Index].KeywordLoc,
627 Changes[Index].VersionRange.getEnd());
628 Complained = true;
629 }
630
631 // Clear out the availability.
632 Changes[Index] = AvailabilityChange();
633 }
634 }
635 }
636
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000637 // Record this attribute
Douglas Gregorb53e4172011-03-26 03:35:55 +0000638 attrs.addNew(&Availability, AvailabilityLoc,
John McCall0b7e6782011-03-24 11:26:52 +0000639 0, SourceLocation(),
640 Platform, PlatformLoc,
641 Changes[Introduced],
642 Changes[Deprecated],
Douglas Gregorb53e4172011-03-26 03:35:55 +0000643 Changes[Obsoleted],
644 UnavailableLoc, false, false);
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000645}
646
John McCall7f040a92010-12-24 02:08:15 +0000647void Parser::DiagnoseProhibitedAttributes(ParsedAttributesWithRange &attrs) {
648 Diag(attrs.Range.getBegin(), diag::err_attributes_not_allowed)
649 << attrs.Range;
Dawn Perchik52fc3142010-09-03 01:29:35 +0000650}
651
Reid Spencer5f016e22007-07-11 17:01:13 +0000652/// ParseDeclaration - Parse a full 'declaration', which consists of
653/// declaration-specifiers, some number of declarators, and a semicolon.
Chris Lattner97144fc2009-04-02 04:16:50 +0000654/// 'Context' should be a Declarator::TheContext value. This returns the
655/// location of the semicolon in DeclEnd.
Chris Lattner8f08cb72007-08-25 06:57:03 +0000656///
657/// declaration: [C99 6.7]
658/// block-declaration ->
659/// simple-declaration
660/// others [FIXME]
Douglas Gregoradcac882008-12-01 23:54:00 +0000661/// [C++] template-declaration
Chris Lattner8f08cb72007-08-25 06:57:03 +0000662/// [C++] namespace-definition
Douglas Gregorf780abc2008-12-30 03:27:21 +0000663/// [C++] using-directive
Douglas Gregord7f37bf2009-06-22 23:06:13 +0000664/// [C++] using-declaration
Peter Collingbournec6eb44b2011-04-15 00:35:57 +0000665/// [C++0x/C1X] static_assert-declaration
Chris Lattner8f08cb72007-08-25 06:57:03 +0000666/// others... [FIXME]
667///
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +0000668Parser::DeclGroupPtrTy Parser::ParseDeclaration(StmtVector &Stmts,
669 unsigned Context,
Sean Huntbbd37c62009-11-21 08:43:09 +0000670 SourceLocation &DeclEnd,
John McCall7f040a92010-12-24 02:08:15 +0000671 ParsedAttributesWithRange &attrs) {
Argyrios Kyrtzidis36d36802010-06-17 10:52:18 +0000672 ParenBraceBracketBalancer BalancerRAIIObj(*this);
673
John McCalld226f652010-08-21 09:40:31 +0000674 Decl *SingleDecl = 0;
Chris Lattner8f08cb72007-08-25 06:57:03 +0000675 switch (Tok.getKind()) {
Douglas Gregoradcac882008-12-01 23:54:00 +0000676 case tok::kw_template:
Douglas Gregor1426e532009-05-12 21:31:51 +0000677 case tok::kw_export:
John McCall7f040a92010-12-24 02:08:15 +0000678 ProhibitAttributes(attrs);
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000679 SingleDecl = ParseDeclarationStartingWithTemplate(Context, DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +0000680 break;
Sebastian Redld078e642010-08-27 23:12:46 +0000681 case tok::kw_inline:
Sebastian Redl88e64ca2010-08-31 00:36:45 +0000682 // Could be the start of an inline namespace. Allowed as an ext in C++03.
683 if (getLang().CPlusPlus && NextToken().is(tok::kw_namespace)) {
John McCall7f040a92010-12-24 02:08:15 +0000684 ProhibitAttributes(attrs);
Sebastian Redld078e642010-08-27 23:12:46 +0000685 SourceLocation InlineLoc = ConsumeToken();
686 SingleDecl = ParseNamespace(Context, DeclEnd, InlineLoc);
687 break;
688 }
John McCall7f040a92010-12-24 02:08:15 +0000689 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs,
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +0000690 true);
Chris Lattner8f08cb72007-08-25 06:57:03 +0000691 case tok::kw_namespace:
John McCall7f040a92010-12-24 02:08:15 +0000692 ProhibitAttributes(attrs);
Chris Lattner97144fc2009-04-02 04:16:50 +0000693 SingleDecl = ParseNamespace(Context, DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +0000694 break;
Douglas Gregorf780abc2008-12-30 03:27:21 +0000695 case tok::kw_using:
John McCall78b81052010-11-10 02:40:36 +0000696 SingleDecl = ParseUsingDirectiveOrDeclaration(Context, ParsedTemplateInfo(),
John McCall7f040a92010-12-24 02:08:15 +0000697 DeclEnd, attrs);
Chris Lattner682bf922009-03-29 16:50:03 +0000698 break;
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000699 case tok::kw_static_assert:
Peter Collingbournec6eb44b2011-04-15 00:35:57 +0000700 case tok::kw__Static_assert:
John McCall7f040a92010-12-24 02:08:15 +0000701 ProhibitAttributes(attrs);
Chris Lattner97144fc2009-04-02 04:16:50 +0000702 SingleDecl = ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +0000703 break;
Chris Lattner8f08cb72007-08-25 06:57:03 +0000704 default:
John McCall7f040a92010-12-24 02:08:15 +0000705 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs, true);
Chris Lattner8f08cb72007-08-25 06:57:03 +0000706 }
Sean Huntbbd37c62009-11-21 08:43:09 +0000707
Chris Lattner682bf922009-03-29 16:50:03 +0000708 // This routine returns a DeclGroup, if the thing we parsed only contains a
709 // single decl, convert it now.
710 return Actions.ConvertDeclToDeclGroup(SingleDecl);
Chris Lattner8f08cb72007-08-25 06:57:03 +0000711}
712
713/// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl]
714/// declaration-specifiers init-declarator-list[opt] ';'
715///[C90/C++]init-declarator-list ';' [TODO]
716/// [OMP] threadprivate-directive [TODO]
Chris Lattnercd147752009-03-29 17:27:48 +0000717///
Richard Smithad762fc2011-04-14 22:09:26 +0000718/// for-range-declaration: [C++0x 6.5p1: stmt.ranged]
719/// attribute-specifier-seq[opt] type-specifier-seq declarator
720///
Chris Lattnercd147752009-03-29 17:27:48 +0000721/// If RequireSemi is false, this does not check for a ';' at the end of the
Chris Lattner5c5db552010-04-05 18:18:31 +0000722/// declaration. If it is true, it checks for and eats it.
Richard Smithad762fc2011-04-14 22:09:26 +0000723///
724/// If FRI is non-null, we might be parsing a for-range-declaration instead
725/// of a simple-declaration. If we find that we are, we also parse the
726/// for-range-initializer, and place it here.
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +0000727Parser::DeclGroupPtrTy Parser::ParseSimpleDeclaration(StmtVector &Stmts,
728 unsigned Context,
Sean Huntbbd37c62009-11-21 08:43:09 +0000729 SourceLocation &DeclEnd,
John McCall7f040a92010-12-24 02:08:15 +0000730 ParsedAttributes &attrs,
Richard Smithad762fc2011-04-14 22:09:26 +0000731 bool RequireSemi,
732 ForRangeInit *FRI) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000733 // Parse the common declaration-specifiers piece.
John McCall54abf7d2009-11-04 02:18:39 +0000734 ParsingDeclSpec DS(*this);
John McCall7f040a92010-12-24 02:08:15 +0000735 DS.takeAttributesFrom(attrs);
Douglas Gregor312eadb2011-04-24 05:37:28 +0000736
Douglas Gregor0efc2c12010-01-13 17:31:36 +0000737 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS_none,
Richard Smith34b41d92011-02-20 03:19:35 +0000738 getDeclSpecContextFromDeclaratorContext(Context));
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +0000739 StmtResult R = Actions.ActOnVlaStmt(DS);
740 if (R.isUsable())
741 Stmts.push_back(R.release());
Douglas Gregor312eadb2011-04-24 05:37:28 +0000742
Reid Spencer5f016e22007-07-11 17:01:13 +0000743 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
744 // declaration-specifiers init-declarator-list[opt] ';'
Chris Lattner04d66662007-10-09 17:33:22 +0000745 if (Tok.is(tok::semi)) {
Chris Lattner5c5db552010-04-05 18:18:31 +0000746 if (RequireSemi) ConsumeToken();
John McCalld226f652010-08-21 09:40:31 +0000747 Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none,
Douglas Gregor312eadb2011-04-24 05:37:28 +0000748 DS);
John McCall54abf7d2009-11-04 02:18:39 +0000749 DS.complete(TheDecl);
Chris Lattner682bf922009-03-29 16:50:03 +0000750 return Actions.ConvertDeclToDeclGroup(TheDecl);
Reid Spencer5f016e22007-07-11 17:01:13 +0000751 }
Douglas Gregor312eadb2011-04-24 05:37:28 +0000752
753 return ParseDeclGroup(DS, Context, /*FunctionDefs=*/ false, &DeclEnd, FRI);
John McCalld8ac0572009-11-03 19:26:08 +0000754}
Mike Stump1eb44332009-09-09 15:08:12 +0000755
John McCalld8ac0572009-11-03 19:26:08 +0000756/// ParseDeclGroup - Having concluded that this is either a function
757/// definition or a group of object declarations, actually parse the
758/// result.
John McCall54abf7d2009-11-04 02:18:39 +0000759Parser::DeclGroupPtrTy Parser::ParseDeclGroup(ParsingDeclSpec &DS,
760 unsigned Context,
John McCalld8ac0572009-11-03 19:26:08 +0000761 bool AllowFunctionDefinitions,
Richard Smithad762fc2011-04-14 22:09:26 +0000762 SourceLocation *DeclEnd,
763 ForRangeInit *FRI) {
John McCalld8ac0572009-11-03 19:26:08 +0000764 // Parse the first declarator.
John McCall54abf7d2009-11-04 02:18:39 +0000765 ParsingDeclarator D(*this, DS, static_cast<Declarator::TheContext>(Context));
John McCalld8ac0572009-11-03 19:26:08 +0000766 ParseDeclarator(D);
Chris Lattnercd147752009-03-29 17:27:48 +0000767
John McCalld8ac0572009-11-03 19:26:08 +0000768 // Bail out if the first declarator didn't seem well-formed.
769 if (!D.hasName() && !D.mayOmitIdentifier()) {
770 // Skip until ; or }.
771 SkipUntil(tok::r_brace, true, true);
772 if (Tok.is(tok::semi))
773 ConsumeToken();
774 return DeclGroupPtrTy();
Chris Lattner23c4b182009-03-29 17:18:04 +0000775 }
Mike Stump1eb44332009-09-09 15:08:12 +0000776
Chris Lattnerc82daef2010-07-11 22:24:20 +0000777 // Check to see if we have a function *definition* which must have a body.
778 if (AllowFunctionDefinitions && D.isFunctionDeclarator() &&
779 // Look at the next token to make sure that this isn't a function
780 // declaration. We have to check this because __attribute__ might be the
781 // start of a function definition in GCC-extended K&R C.
782 !isDeclarationAfterDeclarator()) {
783
Chris Lattner004659a2010-07-11 22:42:07 +0000784 if (isStartOfFunctionDefinition(D)) {
John McCalld8ac0572009-11-03 19:26:08 +0000785 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
786 Diag(Tok, diag::err_function_declared_typedef);
787
788 // Recover by treating the 'typedef' as spurious.
789 DS.ClearStorageClassSpecs();
790 }
791
John McCalld226f652010-08-21 09:40:31 +0000792 Decl *TheDecl = ParseFunctionDefinition(D);
John McCalld8ac0572009-11-03 19:26:08 +0000793 return Actions.ConvertDeclToDeclGroup(TheDecl);
Chris Lattner004659a2010-07-11 22:42:07 +0000794 }
795
796 if (isDeclarationSpecifier()) {
797 // If there is an invalid declaration specifier right after the function
798 // prototype, then we must be in a missing semicolon case where this isn't
799 // actually a body. Just fall through into the code that handles it as a
800 // prototype, and let the top-level code handle the erroneous declspec
801 // where it would otherwise expect a comma or semicolon.
John McCalld8ac0572009-11-03 19:26:08 +0000802 } else {
803 Diag(Tok, diag::err_expected_fn_body);
804 SkipUntil(tok::semi);
805 return DeclGroupPtrTy();
806 }
807 }
808
Richard Smithad762fc2011-04-14 22:09:26 +0000809 if (ParseAttributesAfterDeclarator(D))
810 return DeclGroupPtrTy();
811
812 // C++0x [stmt.iter]p1: Check if we have a for-range-declarator. If so, we
813 // must parse and analyze the for-range-initializer before the declaration is
814 // analyzed.
815 if (FRI && Tok.is(tok::colon)) {
816 FRI->ColonLoc = ConsumeToken();
Sebastian Redldbef1bb2011-06-05 12:23:16 +0000817 if (Tok.is(tok::l_brace))
818 FRI->RangeExpr = ParseBraceInitializer();
819 else
820 FRI->RangeExpr = ParseExpression();
Richard Smithad762fc2011-04-14 22:09:26 +0000821 Decl *ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
822 Actions.ActOnCXXForRangeDecl(ThisDecl);
823 Actions.FinalizeDeclaration(ThisDecl);
824 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS, &ThisDecl, 1);
825 }
826
John McCalld226f652010-08-21 09:40:31 +0000827 llvm::SmallVector<Decl *, 8> DeclsInGroup;
Richard Smithad762fc2011-04-14 22:09:26 +0000828 Decl *FirstDecl = ParseDeclarationAfterDeclaratorAndAttributes(D);
John McCall54abf7d2009-11-04 02:18:39 +0000829 D.complete(FirstDecl);
John McCalld226f652010-08-21 09:40:31 +0000830 if (FirstDecl)
John McCalld8ac0572009-11-03 19:26:08 +0000831 DeclsInGroup.push_back(FirstDecl);
832
833 // If we don't have a comma, it is either the end of the list (a ';') or an
834 // error, bail out.
835 while (Tok.is(tok::comma)) {
836 // Consume the comma.
Chris Lattner23c4b182009-03-29 17:18:04 +0000837 ConsumeToken();
John McCalld8ac0572009-11-03 19:26:08 +0000838
839 // Parse the next declarator.
840 D.clear();
841
842 // Accept attributes in an init-declarator. In the first declarator in a
843 // declaration, these would be part of the declspec. In subsequent
844 // declarators, they become part of the declarator itself, so that they
845 // don't apply to declarators after *this* one. Examples:
846 // short __attribute__((common)) var; -> declspec
847 // short var __attribute__((common)); -> declarator
848 // short x, __attribute__((common)) var; -> declarator
John McCall7f040a92010-12-24 02:08:15 +0000849 MaybeParseGNUAttributes(D);
John McCalld8ac0572009-11-03 19:26:08 +0000850
851 ParseDeclarator(D);
852
John McCalld226f652010-08-21 09:40:31 +0000853 Decl *ThisDecl = ParseDeclarationAfterDeclarator(D);
John McCall54abf7d2009-11-04 02:18:39 +0000854 D.complete(ThisDecl);
John McCalld226f652010-08-21 09:40:31 +0000855 if (ThisDecl)
John McCalld8ac0572009-11-03 19:26:08 +0000856 DeclsInGroup.push_back(ThisDecl);
857 }
858
859 if (DeclEnd)
860 *DeclEnd = Tok.getLocation();
861
862 if (Context != Declarator::ForContext &&
863 ExpectAndConsume(tok::semi,
864 Context == Declarator::FileContext
865 ? diag::err_invalid_token_after_toplevel_declarator
866 : diag::err_expected_semi_declaration)) {
Chris Lattner004659a2010-07-11 22:42:07 +0000867 // Okay, there was no semicolon and one was expected. If we see a
868 // declaration specifier, just assume it was missing and continue parsing.
869 // Otherwise things are very confused and we skip to recover.
870 if (!isDeclarationSpecifier()) {
871 SkipUntil(tok::r_brace, true, true);
872 if (Tok.is(tok::semi))
873 ConsumeToken();
874 }
John McCalld8ac0572009-11-03 19:26:08 +0000875 }
876
Douglas Gregor23c94db2010-07-02 17:43:08 +0000877 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS,
John McCalld8ac0572009-11-03 19:26:08 +0000878 DeclsInGroup.data(),
879 DeclsInGroup.size());
Reid Spencer5f016e22007-07-11 17:01:13 +0000880}
881
Richard Smithad762fc2011-04-14 22:09:26 +0000882/// Parse an optional simple-asm-expr and attributes, and attach them to a
883/// declarator. Returns true on an error.
884bool Parser::ParseAttributesAfterDeclarator(Declarator &D) {
885 // If a simple-asm-expr is present, parse it.
886 if (Tok.is(tok::kw_asm)) {
887 SourceLocation Loc;
888 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
889 if (AsmLabel.isInvalid()) {
890 SkipUntil(tok::semi, true, true);
891 return true;
892 }
893
894 D.setAsmLabel(AsmLabel.release());
895 D.SetRangeEnd(Loc);
896 }
897
898 MaybeParseGNUAttributes(D);
899 return false;
900}
901
Douglas Gregor1426e532009-05-12 21:31:51 +0000902/// \brief Parse 'declaration' after parsing 'declaration-specifiers
903/// declarator'. This method parses the remainder of the declaration
904/// (including any attributes or initializer, among other things) and
905/// finalizes the declaration.
Reid Spencer5f016e22007-07-11 17:01:13 +0000906///
Reid Spencer5f016e22007-07-11 17:01:13 +0000907/// init-declarator: [C99 6.7]
908/// declarator
909/// declarator '=' initializer
910/// [GNU] declarator simple-asm-expr[opt] attributes[opt]
911/// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +0000912/// [C++] declarator initializer[opt]
913///
914/// [C++] initializer:
915/// [C++] '=' initializer-clause
916/// [C++] '(' expression-list ')'
Sebastian Redl50de12f2009-03-24 22:27:57 +0000917/// [C++0x] '=' 'default' [TODO]
918/// [C++0x] '=' 'delete'
Sebastian Redldbef1bb2011-06-05 12:23:16 +0000919/// [C++0x] braced-init-list
Sebastian Redl50de12f2009-03-24 22:27:57 +0000920///
921/// According to the standard grammar, =default and =delete are function
922/// definitions, but that definitely doesn't fit with the parser here.
Reid Spencer5f016e22007-07-11 17:01:13 +0000923///
John McCalld226f652010-08-21 09:40:31 +0000924Decl *Parser::ParseDeclarationAfterDeclarator(Declarator &D,
Douglas Gregore542c862009-06-23 23:11:28 +0000925 const ParsedTemplateInfo &TemplateInfo) {
Richard Smithad762fc2011-04-14 22:09:26 +0000926 if (ParseAttributesAfterDeclarator(D))
927 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000928
Richard Smithad762fc2011-04-14 22:09:26 +0000929 return ParseDeclarationAfterDeclaratorAndAttributes(D, TemplateInfo);
930}
Mike Stump1eb44332009-09-09 15:08:12 +0000931
Richard Smithad762fc2011-04-14 22:09:26 +0000932Decl *Parser::ParseDeclarationAfterDeclaratorAndAttributes(Declarator &D,
933 const ParsedTemplateInfo &TemplateInfo) {
Douglas Gregor1426e532009-05-12 21:31:51 +0000934 // Inform the current actions module that we just parsed this declarator.
John McCalld226f652010-08-21 09:40:31 +0000935 Decl *ThisDecl = 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +0000936 switch (TemplateInfo.Kind) {
937 case ParsedTemplateInfo::NonTemplate:
Douglas Gregor23c94db2010-07-02 17:43:08 +0000938 ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
Douglas Gregord5a423b2009-09-25 18:43:00 +0000939 break;
940
941 case ParsedTemplateInfo::Template:
942 case ParsedTemplateInfo::ExplicitSpecialization:
Douglas Gregor23c94db2010-07-02 17:43:08 +0000943 ThisDecl = Actions.ActOnTemplateDeclarator(getCurScope(),
John McCallf312b1e2010-08-26 23:41:50 +0000944 MultiTemplateParamsArg(Actions,
Douglas Gregore542c862009-06-23 23:11:28 +0000945 TemplateInfo.TemplateParams->data(),
946 TemplateInfo.TemplateParams->size()),
Douglas Gregord5a423b2009-09-25 18:43:00 +0000947 D);
948 break;
949
950 case ParsedTemplateInfo::ExplicitInstantiation: {
John McCalld226f652010-08-21 09:40:31 +0000951 DeclResult ThisRes
Douglas Gregor23c94db2010-07-02 17:43:08 +0000952 = Actions.ActOnExplicitInstantiation(getCurScope(),
Douglas Gregord5a423b2009-09-25 18:43:00 +0000953 TemplateInfo.ExternLoc,
954 TemplateInfo.TemplateLoc,
955 D);
956 if (ThisRes.isInvalid()) {
957 SkipUntil(tok::semi, true, true);
John McCalld226f652010-08-21 09:40:31 +0000958 return 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +0000959 }
960
961 ThisDecl = ThisRes.get();
962 break;
963 }
964 }
Mike Stump1eb44332009-09-09 15:08:12 +0000965
Richard Smith34b41d92011-02-20 03:19:35 +0000966 bool TypeContainsAuto =
967 D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto;
968
Douglas Gregor1426e532009-05-12 21:31:51 +0000969 // Parse declarator '=' initializer.
Argyrios Kyrtzidisa6eb5f82010-10-08 02:39:23 +0000970 if (isTokenEqualOrMistypedEqualEqual(
971 diag::err_invalid_equalequal_after_declarator)) {
Douglas Gregor1426e532009-05-12 21:31:51 +0000972 ConsumeToken();
Anders Carlsson37bf9d22010-09-24 21:25:25 +0000973 if (Tok.is(tok::kw_delete)) {
Sean Hunte4246a62011-05-12 06:15:49 +0000974 if (D.isFunctionDeclarator())
975 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
976 << 1 /* delete */;
977 else
978 Diag(ConsumeToken(), diag::err_deleted_non_function);
Sean Huntfe2695e2011-05-06 01:42:00 +0000979 } else if (Tok.is(tok::kw_default)) {
Sean Hunte4246a62011-05-12 06:15:49 +0000980 if (D.isFunctionDeclarator())
981 Diag(Tok, diag::err_default_delete_in_multiple_declaration)
982 << 1 /* delete */;
983 else
984 Diag(ConsumeToken(), diag::err_default_special_members);
Douglas Gregor1426e532009-05-12 21:31:51 +0000985 } else {
John McCall731ad842009-12-19 09:28:58 +0000986 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
987 EnterScope(0);
Douglas Gregor23c94db2010-07-02 17:43:08 +0000988 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
John McCall731ad842009-12-19 09:28:58 +0000989 }
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +0000990
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +0000991 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000992 Actions.CodeCompleteInitializer(getCurScope(), ThisDecl);
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +0000993 ConsumeCodeCompletionToken();
994 SkipUntil(tok::comma, true, true);
995 return ThisDecl;
996 }
997
John McCall60d7b3a2010-08-24 06:29:42 +0000998 ExprResult Init(ParseInitializer());
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +0000999
John McCall731ad842009-12-19 09:28:58 +00001000 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001001 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
John McCall731ad842009-12-19 09:28:58 +00001002 ExitScope();
1003 }
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +00001004
Douglas Gregor1426e532009-05-12 21:31:51 +00001005 if (Init.isInvalid()) {
Douglas Gregor00225542010-03-01 18:27:54 +00001006 SkipUntil(tok::comma, true, true);
1007 Actions.ActOnInitializerError(ThisDecl);
1008 } else
Richard Smith34b41d92011-02-20 03:19:35 +00001009 Actions.AddInitializerToDecl(ThisDecl, Init.take(),
1010 /*DirectInit=*/false, TypeContainsAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +00001011 }
1012 } else if (Tok.is(tok::l_paren)) {
1013 // Parse C++ direct initializer: '(' expression-list ')'
1014 SourceLocation LParenLoc = ConsumeParen();
1015 ExprVector Exprs(Actions);
1016 CommaLocsTy CommaLocs;
1017
Douglas Gregorb4debae2009-12-22 17:47:17 +00001018 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
1019 EnterScope(0);
Douglas Gregor23c94db2010-07-02 17:43:08 +00001020 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001021 }
1022
Douglas Gregor1426e532009-05-12 21:31:51 +00001023 if (ParseExpressionList(Exprs, CommaLocs)) {
1024 SkipUntil(tok::r_paren);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001025
1026 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001027 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001028 ExitScope();
1029 }
Douglas Gregor1426e532009-05-12 21:31:51 +00001030 } else {
1031 // Match the ')'.
1032 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
1033
1034 assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() &&
1035 "Unexpected number of commas!");
Douglas Gregorb4debae2009-12-22 17:47:17 +00001036
1037 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001038 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001039 ExitScope();
1040 }
1041
Douglas Gregor1426e532009-05-12 21:31:51 +00001042 Actions.AddCXXDirectInitializerToDecl(ThisDecl, LParenLoc,
1043 move_arg(Exprs),
Richard Smith34b41d92011-02-20 03:19:35 +00001044 RParenLoc,
1045 TypeContainsAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +00001046 }
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001047 } else if (getLang().CPlusPlus0x && Tok.is(tok::l_brace)) {
1048 // Parse C++0x braced-init-list.
1049 if (D.getCXXScopeSpec().isSet()) {
1050 EnterScope(0);
1051 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
1052 }
1053
1054 ExprResult Init(ParseBraceInitializer());
1055
1056 if (D.getCXXScopeSpec().isSet()) {
1057 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
1058 ExitScope();
1059 }
1060
1061 if (Init.isInvalid()) {
1062 Actions.ActOnInitializerError(ThisDecl);
1063 } else
1064 Actions.AddInitializerToDecl(ThisDecl, Init.take(),
1065 /*DirectInit=*/true, TypeContainsAuto);
1066
Douglas Gregor1426e532009-05-12 21:31:51 +00001067 } else {
Richard Smith34b41d92011-02-20 03:19:35 +00001068 Actions.ActOnUninitializedDecl(ThisDecl, TypeContainsAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +00001069 }
1070
Richard Smith483b9f32011-02-21 20:05:19 +00001071 Actions.FinalizeDeclaration(ThisDecl);
1072
Douglas Gregor1426e532009-05-12 21:31:51 +00001073 return ThisDecl;
1074}
1075
Reid Spencer5f016e22007-07-11 17:01:13 +00001076/// ParseSpecifierQualifierList
1077/// specifier-qualifier-list:
1078/// type-specifier specifier-qualifier-list[opt]
1079/// type-qualifier specifier-qualifier-list[opt]
1080/// [GNU] attributes specifier-qualifier-list[opt]
1081///
1082void Parser::ParseSpecifierQualifierList(DeclSpec &DS) {
1083 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
1084 /// parse declaration-specifiers and complain about extra stuff.
Reid Spencer5f016e22007-07-11 17:01:13 +00001085 ParseDeclarationSpecifiers(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00001086
Reid Spencer5f016e22007-07-11 17:01:13 +00001087 // Validate declspec for type-name.
1088 unsigned Specs = DS.getParsedSpecifiers();
Chris Lattnerb6645dd2009-04-14 21:16:09 +00001089 if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers() &&
John McCall7f040a92010-12-24 02:08:15 +00001090 !DS.hasAttributes())
Reid Spencer5f016e22007-07-11 17:01:13 +00001091 Diag(Tok, diag::err_typename_requires_specqual);
Mike Stump1eb44332009-09-09 15:08:12 +00001092
Reid Spencer5f016e22007-07-11 17:01:13 +00001093 // Issue diagnostic and remove storage class if present.
1094 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
1095 if (DS.getStorageClassSpecLoc().isValid())
1096 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
1097 else
1098 Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass);
1099 DS.ClearStorageClassSpecs();
1100 }
Mike Stump1eb44332009-09-09 15:08:12 +00001101
Reid Spencer5f016e22007-07-11 17:01:13 +00001102 // Issue diagnostic and remove function specfier if present.
1103 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Douglas Gregorb48fe382008-10-31 09:07:45 +00001104 if (DS.isInlineSpecified())
1105 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
1106 if (DS.isVirtualSpecified())
1107 Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec);
1108 if (DS.isExplicitSpecified())
1109 Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec);
Reid Spencer5f016e22007-07-11 17:01:13 +00001110 DS.ClearFunctionSpecs();
1111 }
1112}
1113
Chris Lattnerc199ab32009-04-12 20:42:31 +00001114/// isValidAfterIdentifierInDeclaratorAfterDeclSpec - Return true if the
1115/// specified token is valid after the identifier in a declarator which
1116/// immediately follows the declspec. For example, these things are valid:
1117///
1118/// int x [ 4]; // direct-declarator
1119/// int x ( int y); // direct-declarator
1120/// int(int x ) // direct-declarator
1121/// int x ; // simple-declaration
1122/// int x = 17; // init-declarator-list
1123/// int x , y; // init-declarator-list
1124/// int x __asm__ ("foo"); // init-declarator-list
Chris Lattnerb6645dd2009-04-14 21:16:09 +00001125/// int x : 4; // struct-declarator
Chris Lattnerc83c27a2009-04-12 22:29:43 +00001126/// int x { 5}; // C++'0x unified initializers
Chris Lattnerc199ab32009-04-12 20:42:31 +00001127///
1128/// This is not, because 'x' does not immediately follow the declspec (though
1129/// ')' happens to be valid anyway).
1130/// int (x)
1131///
1132static bool isValidAfterIdentifierInDeclarator(const Token &T) {
1133 return T.is(tok::l_square) || T.is(tok::l_paren) || T.is(tok::r_paren) ||
1134 T.is(tok::semi) || T.is(tok::comma) || T.is(tok::equal) ||
Chris Lattnerb6645dd2009-04-14 21:16:09 +00001135 T.is(tok::kw_asm) || T.is(tok::l_brace) || T.is(tok::colon);
Chris Lattnerc199ab32009-04-12 20:42:31 +00001136}
1137
Chris Lattnere40c2952009-04-14 21:34:55 +00001138
1139/// ParseImplicitInt - This method is called when we have an non-typename
1140/// identifier in a declspec (which normally terminates the decl spec) when
1141/// the declspec has no type specifier. In this case, the declspec is either
1142/// malformed or is "implicit int" (in K&R and C89).
1143///
1144/// This method handles diagnosing this prettily and returns false if the
1145/// declspec is done being processed. If it recovers and thinks there may be
1146/// other pieces of declspec after it, it returns true.
1147///
Chris Lattnerf4382f52009-04-14 22:17:06 +00001148bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001149 const ParsedTemplateInfo &TemplateInfo,
Chris Lattnere40c2952009-04-14 21:34:55 +00001150 AccessSpecifier AS) {
Chris Lattnerf4382f52009-04-14 22:17:06 +00001151 assert(Tok.is(tok::identifier) && "should have identifier");
Mike Stump1eb44332009-09-09 15:08:12 +00001152
Chris Lattnere40c2952009-04-14 21:34:55 +00001153 SourceLocation Loc = Tok.getLocation();
1154 // If we see an identifier that is not a type name, we normally would
1155 // parse it as the identifer being declared. However, when a typename
1156 // is typo'd or the definition is not included, this will incorrectly
1157 // parse the typename as the identifier name and fall over misparsing
1158 // later parts of the diagnostic.
1159 //
1160 // As such, we try to do some look-ahead in cases where this would
1161 // otherwise be an "implicit-int" case to see if this is invalid. For
1162 // example: "static foo_t x = 4;" In this case, if we parsed foo_t as
1163 // an identifier with implicit int, we'd get a parse error because the
1164 // next token is obviously invalid for a type. Parse these as a case
1165 // with an invalid type specifier.
1166 assert(!DS.hasTypeSpecifier() && "Type specifier checked above");
Mike Stump1eb44332009-09-09 15:08:12 +00001167
Chris Lattnere40c2952009-04-14 21:34:55 +00001168 // Since we know that this either implicit int (which is rare) or an
1169 // error, we'd do lookahead to try to do better recovery.
1170 if (isValidAfterIdentifierInDeclarator(NextToken())) {
1171 // If this token is valid for implicit int, e.g. "static x = 4", then
1172 // we just avoid eating the identifier, so it will be parsed as the
1173 // identifier in the declarator.
1174 return false;
1175 }
Mike Stump1eb44332009-09-09 15:08:12 +00001176
Chris Lattnere40c2952009-04-14 21:34:55 +00001177 // Otherwise, if we don't consume this token, we are going to emit an
1178 // error anyway. Try to recover from various common problems. Check
1179 // to see if this was a reference to a tag name without a tag specified.
1180 // This is a common problem in C (saying 'foo' instead of 'struct foo').
Chris Lattnerf4382f52009-04-14 22:17:06 +00001181 //
1182 // C++ doesn't need this, and isTagName doesn't take SS.
1183 if (SS == 0) {
Argyrios Kyrtzidisb8a9d3b2011-04-21 17:29:47 +00001184 const char *TagName = 0, *FixitTagName = 0;
Chris Lattnerf4382f52009-04-14 22:17:06 +00001185 tok::TokenKind TagKind = tok::unknown;
Mike Stump1eb44332009-09-09 15:08:12 +00001186
Douglas Gregor23c94db2010-07-02 17:43:08 +00001187 switch (Actions.isTagName(*Tok.getIdentifierInfo(), getCurScope())) {
Chris Lattnere40c2952009-04-14 21:34:55 +00001188 default: break;
Argyrios Kyrtzidisb8a9d3b2011-04-21 17:29:47 +00001189 case DeclSpec::TST_enum:
1190 TagName="enum" ; FixitTagName = "enum " ; TagKind=tok::kw_enum ;break;
1191 case DeclSpec::TST_union:
1192 TagName="union" ; FixitTagName = "union " ;TagKind=tok::kw_union ;break;
1193 case DeclSpec::TST_struct:
1194 TagName="struct"; FixitTagName = "struct ";TagKind=tok::kw_struct;break;
1195 case DeclSpec::TST_class:
1196 TagName="class" ; FixitTagName = "class " ;TagKind=tok::kw_class ;break;
Chris Lattnere40c2952009-04-14 21:34:55 +00001197 }
Mike Stump1eb44332009-09-09 15:08:12 +00001198
Chris Lattnerf4382f52009-04-14 22:17:06 +00001199 if (TagName) {
1200 Diag(Loc, diag::err_use_of_tag_name_without_tag)
John McCall23e907a2010-02-14 01:03:10 +00001201 << Tok.getIdentifierInfo() << TagName << getLang().CPlusPlus
Argyrios Kyrtzidisb8a9d3b2011-04-21 17:29:47 +00001202 << FixItHint::CreateInsertion(Tok.getLocation(),FixitTagName);
Mike Stump1eb44332009-09-09 15:08:12 +00001203
Chris Lattnerf4382f52009-04-14 22:17:06 +00001204 // Parse this as a tag as if the missing tag were present.
1205 if (TagKind == tok::kw_enum)
Douglas Gregor9b9edd62010-03-02 17:53:14 +00001206 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS);
Chris Lattnerf4382f52009-04-14 22:17:06 +00001207 else
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001208 ParseClassSpecifier(TagKind, Loc, DS, TemplateInfo, AS);
Chris Lattnerf4382f52009-04-14 22:17:06 +00001209 return true;
1210 }
Chris Lattnere40c2952009-04-14 21:34:55 +00001211 }
Mike Stump1eb44332009-09-09 15:08:12 +00001212
Douglas Gregora786fdb2009-10-13 23:27:22 +00001213 // This is almost certainly an invalid type name. Let the action emit a
1214 // diagnostic and attempt to recover.
John McCallb3d87482010-08-24 05:47:05 +00001215 ParsedType T;
Douglas Gregora786fdb2009-10-13 23:27:22 +00001216 if (Actions.DiagnoseUnknownTypeName(*Tok.getIdentifierInfo(), Loc,
Douglas Gregor23c94db2010-07-02 17:43:08 +00001217 getCurScope(), SS, T)) {
Douglas Gregora786fdb2009-10-13 23:27:22 +00001218 // The action emitted a diagnostic, so we don't have to.
1219 if (T) {
1220 // The action has suggested that the type T could be used. Set that as
1221 // the type in the declaration specifiers, consume the would-be type
1222 // name token, and we're done.
1223 const char *PrevSpec;
1224 unsigned DiagID;
John McCallb3d87482010-08-24 05:47:05 +00001225 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID, T);
Douglas Gregora786fdb2009-10-13 23:27:22 +00001226 DS.SetRangeEnd(Tok.getLocation());
1227 ConsumeToken();
1228
1229 // There may be other declaration specifiers after this.
1230 return true;
1231 }
1232
1233 // Fall through; the action had no suggestion for us.
1234 } else {
1235 // The action did not emit a diagnostic, so emit one now.
1236 SourceRange R;
1237 if (SS) R = SS->getRange();
1238 Diag(Loc, diag::err_unknown_typename) << Tok.getIdentifierInfo() << R;
1239 }
Mike Stump1eb44332009-09-09 15:08:12 +00001240
Douglas Gregora786fdb2009-10-13 23:27:22 +00001241 // Mark this as an error.
Chris Lattnere40c2952009-04-14 21:34:55 +00001242 const char *PrevSpec;
John McCallfec54012009-08-03 20:12:06 +00001243 unsigned DiagID;
1244 DS.SetTypeSpecType(DeclSpec::TST_error, Loc, PrevSpec, DiagID);
Chris Lattnere40c2952009-04-14 21:34:55 +00001245 DS.SetRangeEnd(Tok.getLocation());
1246 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001247
Chris Lattnere40c2952009-04-14 21:34:55 +00001248 // TODO: Could inject an invalid typedef decl in an enclosing scope to
1249 // avoid rippling error messages on subsequent uses of the same type,
1250 // could be useful if #include was forgotten.
1251 return false;
1252}
1253
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001254/// \brief Determine the declaration specifier context from the declarator
1255/// context.
1256///
1257/// \param Context the declarator context, which is one of the
1258/// Declarator::TheContext enumerator values.
1259Parser::DeclSpecContext
1260Parser::getDeclSpecContextFromDeclaratorContext(unsigned Context) {
1261 if (Context == Declarator::MemberContext)
1262 return DSC_class;
1263 if (Context == Declarator::FileContext)
1264 return DSC_top_level;
1265 return DSC_normal;
1266}
1267
Reid Spencer5f016e22007-07-11 17:01:13 +00001268/// ParseDeclarationSpecifiers
1269/// declaration-specifiers: [C99 6.7]
1270/// storage-class-specifier declaration-specifiers[opt]
1271/// type-specifier declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00001272/// [C99] function-specifier declaration-specifiers[opt]
1273/// [GNU] attributes declaration-specifiers[opt]
1274///
1275/// storage-class-specifier: [C99 6.7.1]
1276/// 'typedef'
1277/// 'extern'
1278/// 'static'
1279/// 'auto'
1280/// 'register'
Sebastian Redl669d5d72008-11-14 23:42:31 +00001281/// [C++] 'mutable'
Reid Spencer5f016e22007-07-11 17:01:13 +00001282/// [GNU] '__thread'
Reid Spencer5f016e22007-07-11 17:01:13 +00001283/// function-specifier: [C99 6.7.4]
1284/// [C99] 'inline'
Douglas Gregorb48fe382008-10-31 09:07:45 +00001285/// [C++] 'virtual'
1286/// [C++] 'explicit'
Peter Collingbournef315fa82011-02-14 01:42:53 +00001287/// [OpenCL] '__kernel'
Anders Carlssonf47f7a12009-05-06 04:46:28 +00001288/// 'friend': [C++ dcl.friend]
Sebastian Redl2ac67232009-11-05 15:47:02 +00001289/// 'constexpr': [C++0x dcl.constexpr]
Anders Carlssonf47f7a12009-05-06 04:46:28 +00001290
Reid Spencer5f016e22007-07-11 17:01:13 +00001291///
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +00001292void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001293 const ParsedTemplateInfo &TemplateInfo,
John McCall67d1a672009-08-06 02:15:43 +00001294 AccessSpecifier AS,
Douglas Gregor312eadb2011-04-24 05:37:28 +00001295 DeclSpecContext DSContext) {
1296 if (DS.getSourceRange().isInvalid()) {
1297 DS.SetRangeStart(Tok.getLocation());
1298 DS.SetRangeEnd(Tok.getLocation());
1299 }
1300
Reid Spencer5f016e22007-07-11 17:01:13 +00001301 while (1) {
John McCallfec54012009-08-03 20:12:06 +00001302 bool isInvalid = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001303 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00001304 unsigned DiagID = 0;
1305
Reid Spencer5f016e22007-07-11 17:01:13 +00001306 SourceLocation Loc = Tok.getLocation();
Douglas Gregor12e083c2008-11-07 15:42:26 +00001307
Reid Spencer5f016e22007-07-11 17:01:13 +00001308 switch (Tok.getKind()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001309 default:
Chris Lattnerbce61352008-07-26 00:20:22 +00001310 DoneWithDeclSpec:
Reid Spencer5f016e22007-07-11 17:01:13 +00001311 // If this is not a declaration specifier token, we're done reading decl
1312 // specifiers. First verify that DeclSpec's are consistent.
Douglas Gregor9b3064b2009-04-01 22:41:11 +00001313 DS.Finish(Diags, PP);
Reid Spencer5f016e22007-07-11 17:01:13 +00001314 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001315
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001316 case tok::code_completion: {
John McCallf312b1e2010-08-26 23:41:50 +00001317 Sema::ParserCompletionContext CCC = Sema::PCC_Namespace;
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001318 if (DS.hasTypeSpecifier()) {
1319 bool AllowNonIdentifiers
1320 = (getCurScope()->getFlags() & (Scope::ControlScope |
1321 Scope::BlockScope |
1322 Scope::TemplateParamScope |
1323 Scope::FunctionPrototypeScope |
1324 Scope::AtCatchScope)) == 0;
1325 bool AllowNestedNameSpecifiers
1326 = DSContext == DSC_top_level ||
1327 (DSContext == DSC_class && DS.isFriendSpecified());
1328
Douglas Gregorc7b6d882010-09-16 15:14:18 +00001329 Actions.CodeCompleteDeclSpec(getCurScope(), DS,
1330 AllowNonIdentifiers,
1331 AllowNestedNameSpecifiers);
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001332 ConsumeCodeCompletionToken();
1333 return;
1334 }
1335
Douglas Gregor68e3c2e2011-02-15 20:33:25 +00001336 if (getCurScope()->getFnParent() || getCurScope()->getBlockParent())
1337 CCC = Sema::PCC_LocalDeclarationSpecifiers;
1338 else if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate)
John McCallf312b1e2010-08-26 23:41:50 +00001339 CCC = DSContext == DSC_class? Sema::PCC_MemberTemplate
1340 : Sema::PCC_Template;
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001341 else if (DSContext == DSC_class)
John McCallf312b1e2010-08-26 23:41:50 +00001342 CCC = Sema::PCC_Class;
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001343 else if (ObjCImpDecl)
John McCallf312b1e2010-08-26 23:41:50 +00001344 CCC = Sema::PCC_ObjCImplementation;
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001345
1346 Actions.CodeCompleteOrdinaryName(getCurScope(), CCC);
1347 ConsumeCodeCompletionToken();
1348 return;
1349 }
1350
Chris Lattner5e02c472009-01-05 00:07:25 +00001351 case tok::coloncolon: // ::foo::bar
John McCall9ba61662010-02-26 08:45:28 +00001352 // C++ scope specifier. Annotate and loop, or bail out on error.
1353 if (TryAnnotateCXXScopeToken(true)) {
1354 if (!DS.hasTypeSpecifier())
1355 DS.SetTypeSpecError();
1356 goto DoneWithDeclSpec;
1357 }
John McCall2e0a7152010-03-01 18:20:46 +00001358 if (Tok.is(tok::coloncolon)) // ::new or ::delete
1359 goto DoneWithDeclSpec;
John McCall9ba61662010-02-26 08:45:28 +00001360 continue;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001361
1362 case tok::annot_cxxscope: {
1363 if (DS.hasTypeSpecifier())
1364 goto DoneWithDeclSpec;
1365
John McCallaa87d332009-12-12 11:40:51 +00001366 CXXScopeSpec SS;
Douglas Gregorc34348a2011-02-24 17:54:50 +00001367 Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(),
1368 Tok.getAnnotationRange(),
1369 SS);
John McCallaa87d332009-12-12 11:40:51 +00001370
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001371 // We are looking for a qualified typename.
Douglas Gregor9135c722009-03-25 15:40:00 +00001372 Token Next = NextToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001373 if (Next.is(tok::annot_template_id) &&
Douglas Gregor9135c722009-03-25 15:40:00 +00001374 static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue())
Douglas Gregorc45c2322009-03-31 00:43:58 +00001375 ->Kind == TNK_Type_template) {
Douglas Gregor9135c722009-03-25 15:40:00 +00001376 // We have a qualified template-id, e.g., N::A<int>
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001377
1378 // C++ [class.qual]p2:
1379 // In a lookup in which the constructor is an acceptable lookup
1380 // result and the nested-name-specifier nominates a class C:
1381 //
1382 // - if the name specified after the
1383 // nested-name-specifier, when looked up in C, is the
1384 // injected-class-name of C (Clause 9), or
1385 //
1386 // - if the name specified after the nested-name-specifier
1387 // is the same as the identifier or the
1388 // simple-template-id's template-name in the last
1389 // component of the nested-name-specifier,
1390 //
1391 // the name is instead considered to name the constructor of
1392 // class C.
1393 //
1394 // Thus, if the template-name is actually the constructor
1395 // name, then the code is ill-formed; this interpretation is
1396 // reinforced by the NAD status of core issue 635.
1397 TemplateIdAnnotation *TemplateId
1398 = static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue());
John McCallba9d8532010-04-13 06:39:49 +00001399 if ((DSContext == DSC_top_level ||
1400 (DSContext == DSC_class && DS.isFriendSpecified())) &&
1401 TemplateId->Name &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00001402 Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) {
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001403 if (isConstructorDeclarator()) {
1404 // The user meant this to be an out-of-line constructor
1405 // definition, but template arguments are not allowed
1406 // there. Just allow this as a constructor; we'll
1407 // complain about it later.
1408 goto DoneWithDeclSpec;
1409 }
1410
1411 // The user meant this to name a type, but it actually names
1412 // a constructor with some extraneous template
1413 // arguments. Complain, then parse it as a type as the user
1414 // intended.
1415 Diag(TemplateId->TemplateNameLoc,
1416 diag::err_out_of_line_template_id_names_constructor)
1417 << TemplateId->Name;
1418 }
1419
John McCallaa87d332009-12-12 11:40:51 +00001420 DS.getTypeSpecScope() = SS;
1421 ConsumeToken(); // The C++ scope.
Mike Stump1eb44332009-09-09 15:08:12 +00001422 assert(Tok.is(tok::annot_template_id) &&
Douglas Gregor9135c722009-03-25 15:40:00 +00001423 "ParseOptionalCXXScopeSpecifier not working");
Douglas Gregor059101f2011-03-02 00:47:37 +00001424 AnnotateTemplateIdTokenAsType();
Douglas Gregor9135c722009-03-25 15:40:00 +00001425 continue;
1426 }
1427
Douglas Gregor9d7b3532009-09-28 07:26:33 +00001428 if (Next.is(tok::annot_typename)) {
John McCallaa87d332009-12-12 11:40:51 +00001429 DS.getTypeSpecScope() = SS;
1430 ConsumeToken(); // The C++ scope.
John McCallb3d87482010-08-24 05:47:05 +00001431 if (Tok.getAnnotationValue()) {
1432 ParsedType T = getTypeAnnotation(Tok);
Nico Weber253e80b2010-11-22 10:30:56 +00001433 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename,
1434 Tok.getAnnotationEndLoc(),
John McCallb3d87482010-08-24 05:47:05 +00001435 PrevSpec, DiagID, T);
1436 }
Douglas Gregor9d7b3532009-09-28 07:26:33 +00001437 else
1438 DS.SetTypeSpecError();
1439 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1440 ConsumeToken(); // The typename
1441 }
1442
Douglas Gregor9135c722009-03-25 15:40:00 +00001443 if (Next.isNot(tok::identifier))
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001444 goto DoneWithDeclSpec;
1445
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001446 // If we're in a context where the identifier could be a class name,
1447 // check whether this is a constructor declaration.
John McCallba9d8532010-04-13 06:39:49 +00001448 if ((DSContext == DSC_top_level ||
1449 (DSContext == DSC_class && DS.isFriendSpecified())) &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00001450 Actions.isCurrentClassName(*Next.getIdentifierInfo(), getCurScope(),
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001451 &SS)) {
1452 if (isConstructorDeclarator())
1453 goto DoneWithDeclSpec;
1454
1455 // As noted in C++ [class.qual]p2 (cited above), when the name
1456 // of the class is qualified in a context where it could name
1457 // a constructor, its a constructor name. However, we've
1458 // looked at the declarator, and the user probably meant this
1459 // to be a type. Complain that it isn't supposed to be treated
1460 // as a type, then proceed to parse it as a type.
1461 Diag(Next.getLocation(), diag::err_out_of_line_type_names_constructor)
1462 << Next.getIdentifierInfo();
1463 }
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001464
John McCallb3d87482010-08-24 05:47:05 +00001465 ParsedType TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(),
1466 Next.getLocation(),
Douglas Gregor9e876872011-03-01 18:12:44 +00001467 getCurScope(), &SS,
1468 false, false, ParsedType(),
1469 /*NonTrivialSourceInfo=*/true);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001470
Chris Lattnerf4382f52009-04-14 22:17:06 +00001471 // If the referenced identifier is not a type, then this declspec is
1472 // erroneous: We already checked about that it has no type specifier, and
1473 // C++ doesn't have implicit int. Diagnose it as a typo w.r.t. to the
Mike Stump1eb44332009-09-09 15:08:12 +00001474 // typename.
Chris Lattnerf4382f52009-04-14 22:17:06 +00001475 if (TypeRep == 0) {
1476 ConsumeToken(); // Eat the scope spec so the identifier is current.
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001477 if (ParseImplicitInt(DS, &SS, TemplateInfo, AS)) continue;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001478 goto DoneWithDeclSpec;
Chris Lattnerf4382f52009-04-14 22:17:06 +00001479 }
Mike Stump1eb44332009-09-09 15:08:12 +00001480
John McCallaa87d332009-12-12 11:40:51 +00001481 DS.getTypeSpecScope() = SS;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001482 ConsumeToken(); // The C++ scope.
1483
Douglas Gregor1a51b4a2009-02-09 15:09:02 +00001484 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001485 DiagID, TypeRep);
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001486 if (isInvalid)
1487 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001488
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001489 DS.SetRangeEnd(Tok.getLocation());
1490 ConsumeToken(); // The typename.
1491
1492 continue;
1493 }
Mike Stump1eb44332009-09-09 15:08:12 +00001494
Chris Lattner80d0c892009-01-21 19:48:37 +00001495 case tok::annot_typename: {
John McCallb3d87482010-08-24 05:47:05 +00001496 if (Tok.getAnnotationValue()) {
1497 ParsedType T = getTypeAnnotation(Tok);
Nico Weberc43271e2010-11-22 12:50:03 +00001498 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallb3d87482010-08-24 05:47:05 +00001499 DiagID, T);
1500 } else
Douglas Gregor31a19b62009-04-01 21:51:26 +00001501 DS.SetTypeSpecError();
Chris Lattner5c5db552010-04-05 18:18:31 +00001502
1503 if (isInvalid)
1504 break;
1505
Chris Lattner80d0c892009-01-21 19:48:37 +00001506 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1507 ConsumeToken(); // The typename
Mike Stump1eb44332009-09-09 15:08:12 +00001508
Chris Lattner80d0c892009-01-21 19:48:37 +00001509 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1510 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001511 // Objective-C interface.
1512 if (Tok.is(tok::less) && getLang().ObjC1)
1513 ParseObjCProtocolQualifiers(DS);
1514
Chris Lattner80d0c892009-01-21 19:48:37 +00001515 continue;
1516 }
Mike Stump1eb44332009-09-09 15:08:12 +00001517
Douglas Gregorbfad9152011-04-28 15:48:45 +00001518 case tok::kw___is_signed:
1519 // GNU libstdc++ 4.4 uses __is_signed as an identifier, but Clang
1520 // typically treats it as a trait. If we see __is_signed as it appears
1521 // in libstdc++, e.g.,
1522 //
1523 // static const bool __is_signed;
1524 //
1525 // then treat __is_signed as an identifier rather than as a keyword.
1526 if (DS.getTypeSpecType() == TST_bool &&
1527 DS.getTypeQualifiers() == DeclSpec::TQ_const &&
1528 DS.getStorageClassSpec() == DeclSpec::SCS_static) {
1529 Tok.getIdentifierInfo()->RevertTokenIDToIdentifier();
1530 Tok.setKind(tok::identifier);
1531 }
1532
1533 // We're done with the declaration-specifiers.
1534 goto DoneWithDeclSpec;
1535
Chris Lattner3bd934a2008-07-26 01:18:38 +00001536 // typedef-name
1537 case tok::identifier: {
Chris Lattner5e02c472009-01-05 00:07:25 +00001538 // In C++, check to see if this is a scope specifier like foo::bar::, if
1539 // so handle it as such. This is important for ctor parsing.
John McCall9ba61662010-02-26 08:45:28 +00001540 if (getLang().CPlusPlus) {
1541 if (TryAnnotateCXXScopeToken(true)) {
1542 if (!DS.hasTypeSpecifier())
1543 DS.SetTypeSpecError();
1544 goto DoneWithDeclSpec;
1545 }
1546 if (!Tok.is(tok::identifier))
1547 continue;
1548 }
Mike Stump1eb44332009-09-09 15:08:12 +00001549
Chris Lattner3bd934a2008-07-26 01:18:38 +00001550 // This identifier can only be a typedef name if we haven't already seen
1551 // a type-specifier. Without this check we misparse:
1552 // typedef int X; struct Y { short X; }; as 'short int'.
1553 if (DS.hasTypeSpecifier())
1554 goto DoneWithDeclSpec;
Mike Stump1eb44332009-09-09 15:08:12 +00001555
John Thompson82287d12010-02-05 00:12:22 +00001556 // Check for need to substitute AltiVec keyword tokens.
1557 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
1558 break;
1559
Chris Lattner3bd934a2008-07-26 01:18:38 +00001560 // It has to be available as a typedef too!
John McCallb3d87482010-08-24 05:47:05 +00001561 ParsedType TypeRep =
1562 Actions.getTypeName(*Tok.getIdentifierInfo(),
1563 Tok.getLocation(), getCurScope());
Douglas Gregor55f6b142009-02-09 18:46:07 +00001564
Chris Lattnerc199ab32009-04-12 20:42:31 +00001565 // If this is not a typedef name, don't parse it as part of the declspec,
1566 // it must be an implicit int or an error.
John McCallb3d87482010-08-24 05:47:05 +00001567 if (!TypeRep) {
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001568 if (ParseImplicitInt(DS, 0, TemplateInfo, AS)) continue;
Chris Lattner3bd934a2008-07-26 01:18:38 +00001569 goto DoneWithDeclSpec;
Chris Lattnerc199ab32009-04-12 20:42:31 +00001570 }
Douglas Gregor55f6b142009-02-09 18:46:07 +00001571
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001572 // If we're in a context where the identifier could be a class name,
1573 // check whether this is a constructor declaration.
1574 if (getLang().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00001575 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), getCurScope()) &&
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001576 isConstructorDeclarator())
Douglas Gregorb48fe382008-10-31 09:07:45 +00001577 goto DoneWithDeclSpec;
1578
Douglas Gregor1a51b4a2009-02-09 15:09:02 +00001579 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001580 DiagID, TypeRep);
Chris Lattner3bd934a2008-07-26 01:18:38 +00001581 if (isInvalid)
1582 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001583
Chris Lattner3bd934a2008-07-26 01:18:38 +00001584 DS.SetRangeEnd(Tok.getLocation());
1585 ConsumeToken(); // The identifier
1586
1587 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1588 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001589 // Objective-C interface.
1590 if (Tok.is(tok::less) && getLang().ObjC1)
1591 ParseObjCProtocolQualifiers(DS);
1592
Steve Naroff4f9b9f12008-09-22 10:28:57 +00001593 // Need to support trailing type qualifiers (e.g. "id<p> const").
1594 // If a type specifier follows, it will be diagnosed elsewhere.
1595 continue;
Chris Lattner3bd934a2008-07-26 01:18:38 +00001596 }
Douglas Gregor39a8de12009-02-25 19:37:18 +00001597
1598 // type-name
1599 case tok::annot_template_id: {
Mike Stump1eb44332009-09-09 15:08:12 +00001600 TemplateIdAnnotation *TemplateId
Douglas Gregor39a8de12009-02-25 19:37:18 +00001601 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
Douglas Gregorc45c2322009-03-31 00:43:58 +00001602 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor39a8de12009-02-25 19:37:18 +00001603 // This template-id does not refer to a type name, so we're
1604 // done with the type-specifiers.
1605 goto DoneWithDeclSpec;
1606 }
1607
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001608 // If we're in a context where the template-id could be a
1609 // constructor name or specialization, check whether this is a
1610 // constructor declaration.
1611 if (getLang().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00001612 Actions.isCurrentClassName(*TemplateId->Name, getCurScope()) &&
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001613 isConstructorDeclarator())
1614 goto DoneWithDeclSpec;
1615
Douglas Gregor39a8de12009-02-25 19:37:18 +00001616 // Turn the template-id annotation token into a type annotation
1617 // token, then try again to parse it as a type-specifier.
Douglas Gregor31a19b62009-04-01 21:51:26 +00001618 AnnotateTemplateIdTokenAsType();
Douglas Gregor39a8de12009-02-25 19:37:18 +00001619 continue;
1620 }
1621
Reid Spencer5f016e22007-07-11 17:01:13 +00001622 // GNU attributes support.
1623 case tok::kw___attribute:
John McCall7f040a92010-12-24 02:08:15 +00001624 ParseGNUAttributes(DS.getAttributes());
Reid Spencer5f016e22007-07-11 17:01:13 +00001625 continue;
Steve Narofff59e17e2008-12-24 20:59:21 +00001626
1627 // Microsoft declspec support.
1628 case tok::kw___declspec:
John McCall7f040a92010-12-24 02:08:15 +00001629 ParseMicrosoftDeclSpec(DS.getAttributes());
Steve Narofff59e17e2008-12-24 20:59:21 +00001630 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001631
Steve Naroff239f0732008-12-25 14:16:32 +00001632 // Microsoft single token adornments.
Steve Naroff86bc6cf2008-12-25 14:41:26 +00001633 case tok::kw___forceinline:
Eli Friedman290eeb02009-06-08 23:27:34 +00001634 // FIXME: Add handling here!
1635 break;
1636
1637 case tok::kw___ptr64:
Steve Naroff86bc6cf2008-12-25 14:41:26 +00001638 case tok::kw___w64:
Steve Naroff239f0732008-12-25 14:16:32 +00001639 case tok::kw___cdecl:
1640 case tok::kw___stdcall:
1641 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00001642 case tok::kw___thiscall:
John McCall7f040a92010-12-24 02:08:15 +00001643 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman290eeb02009-06-08 23:27:34 +00001644 continue;
1645
Dawn Perchik52fc3142010-09-03 01:29:35 +00001646 // Borland single token adornments.
1647 case tok::kw___pascal:
John McCall7f040a92010-12-24 02:08:15 +00001648 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik52fc3142010-09-03 01:29:35 +00001649 continue;
1650
Peter Collingbournef315fa82011-02-14 01:42:53 +00001651 // OpenCL single token adornments.
1652 case tok::kw___kernel:
1653 ParseOpenCLAttributes(DS.getAttributes());
1654 continue;
1655
Reid Spencer5f016e22007-07-11 17:01:13 +00001656 // storage-class-specifier
1657 case tok::kw_typedef:
John McCallfec54012009-08-03 20:12:06 +00001658 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_typedef, Loc, PrevSpec,
Peter Collingbournee2f82f72011-02-11 19:59:54 +00001659 DiagID, getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00001660 break;
1661 case tok::kw_extern:
1662 if (DS.isThreadSpecified())
Chris Lattner1ab3b962008-11-18 07:48:38 +00001663 Diag(Tok, diag::ext_thread_before) << "extern";
John McCallfec54012009-08-03 20:12:06 +00001664 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_extern, Loc, PrevSpec,
Peter Collingbournee2f82f72011-02-11 19:59:54 +00001665 DiagID, getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00001666 break;
Steve Naroff8d54bf22007-12-18 00:16:02 +00001667 case tok::kw___private_extern__:
Chris Lattnerf97409f2008-04-06 06:57:35 +00001668 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_private_extern, Loc,
Peter Collingbournee2f82f72011-02-11 19:59:54 +00001669 PrevSpec, DiagID, getLang());
Steve Naroff8d54bf22007-12-18 00:16:02 +00001670 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00001671 case tok::kw_static:
1672 if (DS.isThreadSpecified())
Chris Lattner1ab3b962008-11-18 07:48:38 +00001673 Diag(Tok, diag::ext_thread_before) << "static";
John McCallfec54012009-08-03 20:12:06 +00001674 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_static, Loc, PrevSpec,
Peter Collingbournee2f82f72011-02-11 19:59:54 +00001675 DiagID, getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00001676 break;
1677 case tok::kw_auto:
Douglas Gregor18d8b792011-03-14 21:43:30 +00001678 if (getLang().CPlusPlus0x) {
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00001679 if (isKnownToBeTypeSpecifier(GetLookAheadToken(1))) {
1680 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, Loc, PrevSpec,
1681 DiagID, getLang());
1682 if (!isInvalid)
1683 Diag(Tok, diag::auto_storage_class)
1684 << FixItHint::CreateRemoval(DS.getStorageClassSpecLoc());
1685 }
1686 else
1687 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec,
1688 DiagID);
1689 }
Anders Carlssone89d1592009-06-26 18:41:36 +00001690 else
John McCallfec54012009-08-03 20:12:06 +00001691 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, Loc, PrevSpec,
Peter Collingbournee2f82f72011-02-11 19:59:54 +00001692 DiagID, getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00001693 break;
1694 case tok::kw_register:
John McCallfec54012009-08-03 20:12:06 +00001695 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_register, Loc, PrevSpec,
Peter Collingbournee2f82f72011-02-11 19:59:54 +00001696 DiagID, getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00001697 break;
Sebastian Redl669d5d72008-11-14 23:42:31 +00001698 case tok::kw_mutable:
John McCallfec54012009-08-03 20:12:06 +00001699 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_mutable, Loc, PrevSpec,
Peter Collingbournee2f82f72011-02-11 19:59:54 +00001700 DiagID, getLang());
Sebastian Redl669d5d72008-11-14 23:42:31 +00001701 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00001702 case tok::kw___thread:
John McCallfec54012009-08-03 20:12:06 +00001703 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001704 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001705
Reid Spencer5f016e22007-07-11 17:01:13 +00001706 // function-specifier
1707 case tok::kw_inline:
John McCallfec54012009-08-03 20:12:06 +00001708 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001709 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +00001710 case tok::kw_virtual:
John McCallfec54012009-08-03 20:12:06 +00001711 isInvalid = DS.SetFunctionSpecVirtual(Loc, PrevSpec, DiagID);
Douglas Gregorb48fe382008-10-31 09:07:45 +00001712 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +00001713 case tok::kw_explicit:
John McCallfec54012009-08-03 20:12:06 +00001714 isInvalid = DS.SetFunctionSpecExplicit(Loc, PrevSpec, DiagID);
Douglas Gregorb48fe382008-10-31 09:07:45 +00001715 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00001716
Anders Carlssonf47f7a12009-05-06 04:46:28 +00001717 // friend
1718 case tok::kw_friend:
John McCall67d1a672009-08-06 02:15:43 +00001719 if (DSContext == DSC_class)
1720 isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID);
1721 else {
1722 PrevSpec = ""; // not actually used by the diagnostic
1723 DiagID = diag::err_friend_invalid_in_context;
1724 isInvalid = true;
1725 }
Anders Carlssonf47f7a12009-05-06 04:46:28 +00001726 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001727
Sebastian Redl2ac67232009-11-05 15:47:02 +00001728 // constexpr
1729 case tok::kw_constexpr:
1730 isInvalid = DS.SetConstexprSpec(Loc, PrevSpec, DiagID);
1731 break;
1732
Chris Lattner80d0c892009-01-21 19:48:37 +00001733 // type-specifier
1734 case tok::kw_short:
John McCallfec54012009-08-03 20:12:06 +00001735 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec,
1736 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001737 break;
1738 case tok::kw_long:
1739 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCallfec54012009-08-03 20:12:06 +00001740 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
1741 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001742 else
John McCallfec54012009-08-03 20:12:06 +00001743 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
1744 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001745 break;
Francois Pichet338d7f72011-04-28 01:59:37 +00001746 case tok::kw___int64:
1747 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
1748 DiagID);
1749 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00001750 case tok::kw_signed:
John McCallfec54012009-08-03 20:12:06 +00001751 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec,
1752 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001753 break;
1754 case tok::kw_unsigned:
John McCallfec54012009-08-03 20:12:06 +00001755 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
1756 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001757 break;
1758 case tok::kw__Complex:
John McCallfec54012009-08-03 20:12:06 +00001759 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
1760 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001761 break;
1762 case tok::kw__Imaginary:
John McCallfec54012009-08-03 20:12:06 +00001763 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
1764 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001765 break;
1766 case tok::kw_void:
John McCallfec54012009-08-03 20:12:06 +00001767 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec,
1768 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001769 break;
1770 case tok::kw_char:
John McCallfec54012009-08-03 20:12:06 +00001771 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec,
1772 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001773 break;
1774 case tok::kw_int:
John McCallfec54012009-08-03 20:12:06 +00001775 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec,
1776 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001777 break;
1778 case tok::kw_float:
John McCallfec54012009-08-03 20:12:06 +00001779 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec,
1780 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001781 break;
1782 case tok::kw_double:
John McCallfec54012009-08-03 20:12:06 +00001783 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec,
1784 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001785 break;
1786 case tok::kw_wchar_t:
John McCallfec54012009-08-03 20:12:06 +00001787 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec,
1788 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001789 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001790 case tok::kw_char16_t:
John McCallfec54012009-08-03 20:12:06 +00001791 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec,
1792 DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001793 break;
1794 case tok::kw_char32_t:
John McCallfec54012009-08-03 20:12:06 +00001795 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec,
1796 DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001797 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00001798 case tok::kw_bool:
1799 case tok::kw__Bool:
Argyrios Kyrtzidis4383e182010-11-16 18:18:13 +00001800 if (Tok.is(tok::kw_bool) &&
1801 DS.getTypeSpecType() != DeclSpec::TST_unspecified &&
1802 DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
1803 PrevSpec = ""; // Not used by the diagnostic.
1804 DiagID = diag::err_bool_redeclaration;
Fariborz Jahaniane106a0b2011-04-19 21:42:37 +00001805 // For better error recovery.
1806 Tok.setKind(tok::identifier);
Argyrios Kyrtzidis4383e182010-11-16 18:18:13 +00001807 isInvalid = true;
1808 } else {
1809 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec,
1810 DiagID);
1811 }
Chris Lattner80d0c892009-01-21 19:48:37 +00001812 break;
1813 case tok::kw__Decimal32:
John McCallfec54012009-08-03 20:12:06 +00001814 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
1815 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001816 break;
1817 case tok::kw__Decimal64:
John McCallfec54012009-08-03 20:12:06 +00001818 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
1819 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001820 break;
1821 case tok::kw__Decimal128:
John McCallfec54012009-08-03 20:12:06 +00001822 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
1823 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001824 break;
John Thompson82287d12010-02-05 00:12:22 +00001825 case tok::kw___vector:
1826 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
1827 break;
1828 case tok::kw___pixel:
1829 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
1830 break;
John McCalla5fc4722011-04-09 22:50:59 +00001831 case tok::kw___unknown_anytype:
1832 isInvalid = DS.SetTypeSpecType(TST_unknown_anytype, Loc,
1833 PrevSpec, DiagID);
1834 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00001835
1836 // class-specifier:
1837 case tok::kw_class:
1838 case tok::kw_struct:
Chris Lattner4c97d762009-04-12 21:49:30 +00001839 case tok::kw_union: {
1840 tok::TokenKind Kind = Tok.getKind();
1841 ConsumeToken();
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001842 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS);
Chris Lattner80d0c892009-01-21 19:48:37 +00001843 continue;
Chris Lattner4c97d762009-04-12 21:49:30 +00001844 }
Chris Lattner80d0c892009-01-21 19:48:37 +00001845
1846 // enum-specifier:
1847 case tok::kw_enum:
Chris Lattner4c97d762009-04-12 21:49:30 +00001848 ConsumeToken();
Douglas Gregor9b9edd62010-03-02 17:53:14 +00001849 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS);
Chris Lattner80d0c892009-01-21 19:48:37 +00001850 continue;
1851
1852 // cv-qualifier:
1853 case tok::kw_const:
John McCallfec54012009-08-03 20:12:06 +00001854 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec, DiagID,
1855 getLang());
Chris Lattner80d0c892009-01-21 19:48:37 +00001856 break;
1857 case tok::kw_volatile:
John McCallfec54012009-08-03 20:12:06 +00001858 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
1859 getLang());
Chris Lattner80d0c892009-01-21 19:48:37 +00001860 break;
1861 case tok::kw_restrict:
John McCallfec54012009-08-03 20:12:06 +00001862 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
1863 getLang());
Chris Lattner80d0c892009-01-21 19:48:37 +00001864 break;
1865
Douglas Gregord57959a2009-03-27 23:10:48 +00001866 // C++ typename-specifier:
1867 case tok::kw_typename:
John McCall9ba61662010-02-26 08:45:28 +00001868 if (TryAnnotateTypeOrScopeToken()) {
1869 DS.SetTypeSpecError();
1870 goto DoneWithDeclSpec;
1871 }
1872 if (!Tok.is(tok::kw_typename))
Douglas Gregord57959a2009-03-27 23:10:48 +00001873 continue;
1874 break;
1875
Chris Lattner80d0c892009-01-21 19:48:37 +00001876 // GNU typeof support.
1877 case tok::kw_typeof:
1878 ParseTypeofSpecifier(DS);
1879 continue;
1880
Anders Carlsson6fd634f2009-06-24 17:47:40 +00001881 case tok::kw_decltype:
1882 ParseDecltypeSpecifier(DS);
1883 continue;
1884
Sean Huntdb5d44b2011-05-19 05:37:45 +00001885 case tok::kw___underlying_type:
1886 ParseUnderlyingTypeSpecifier(DS);
1887
Peter Collingbourne207f4d82011-03-18 22:38:29 +00001888 // OpenCL qualifiers:
1889 case tok::kw_private:
1890 if (!getLang().OpenCL)
1891 goto DoneWithDeclSpec;
1892 case tok::kw___private:
1893 case tok::kw___global:
1894 case tok::kw___local:
1895 case tok::kw___constant:
1896 case tok::kw___read_only:
1897 case tok::kw___write_only:
1898 case tok::kw___read_write:
1899 ParseOpenCLQualifiers(DS);
1900 break;
1901
Steve Naroffd3ded1f2008-06-05 00:02:44 +00001902 case tok::less:
Chris Lattner3bd934a2008-07-26 01:18:38 +00001903 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattnerbce61352008-07-26 00:20:22 +00001904 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
1905 // but we support it.
Chris Lattner3bd934a2008-07-26 01:18:38 +00001906 if (DS.hasTypeSpecifier() || !getLang().ObjC1)
Chris Lattnerbce61352008-07-26 00:20:22 +00001907 goto DoneWithDeclSpec;
Mike Stump1eb44332009-09-09 15:08:12 +00001908
Douglas Gregor46f936e2010-11-19 17:10:50 +00001909 if (!ParseObjCProtocolQualifiers(DS))
1910 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
1911 << FixItHint::CreateInsertion(Loc, "id")
1912 << SourceRange(Loc, DS.getSourceRange().getEnd());
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001913
1914 // Need to support trailing type qualifiers (e.g. "id<p> const").
1915 // If a type specifier follows, it will be diagnosed elsewhere.
1916 continue;
Reid Spencer5f016e22007-07-11 17:01:13 +00001917 }
John McCallfec54012009-08-03 20:12:06 +00001918 // If the specifier wasn't legal, issue a diagnostic.
Reid Spencer5f016e22007-07-11 17:01:13 +00001919 if (isInvalid) {
1920 assert(PrevSpec && "Method did not return previous specifier!");
John McCallfec54012009-08-03 20:12:06 +00001921 assert(DiagID);
Douglas Gregorae2fb142010-08-23 14:34:43 +00001922
1923 if (DiagID == diag::ext_duplicate_declspec)
1924 Diag(Tok, DiagID)
1925 << PrevSpec << FixItHint::CreateRemoval(Tok.getLocation());
1926 else
1927 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00001928 }
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00001929
Chris Lattner81c018d2008-03-13 06:29:04 +00001930 DS.SetRangeEnd(Tok.getLocation());
Fariborz Jahaniane106a0b2011-04-19 21:42:37 +00001931 if (DiagID != diag::err_bool_redeclaration)
1932 ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00001933 }
1934}
Douglas Gregoradcac882008-12-01 23:54:00 +00001935
Chris Lattner7a0ab5f2009-01-06 06:59:53 +00001936/// ParseOptionalTypeSpecifier - Try to parse a single type-specifier. We
Douglas Gregor12e083c2008-11-07 15:42:26 +00001937/// primarily follow the C++ grammar with additions for C99 and GNU,
1938/// which together subsume the C grammar. Note that the C++
1939/// type-specifier also includes the C type-qualifier (for const,
1940/// volatile, and C99 restrict). Returns true if a type-specifier was
1941/// found (and parsed), false otherwise.
1942///
1943/// type-specifier: [C++ 7.1.5]
1944/// simple-type-specifier
1945/// class-specifier
1946/// enum-specifier
1947/// elaborated-type-specifier [TODO]
1948/// cv-qualifier
1949///
1950/// cv-qualifier: [C++ 7.1.5.1]
1951/// 'const'
1952/// 'volatile'
1953/// [C99] 'restrict'
1954///
1955/// simple-type-specifier: [ C++ 7.1.5.2]
1956/// '::'[opt] nested-name-specifier[opt] type-name [TODO]
1957/// '::'[opt] nested-name-specifier 'template' template-id [TODO]
1958/// 'char'
1959/// 'wchar_t'
1960/// 'bool'
1961/// 'short'
1962/// 'int'
1963/// 'long'
1964/// 'signed'
1965/// 'unsigned'
1966/// 'float'
1967/// 'double'
1968/// 'void'
1969/// [C99] '_Bool'
1970/// [C99] '_Complex'
1971/// [C99] '_Imaginary' // Removed in TC2?
1972/// [GNU] '_Decimal32'
1973/// [GNU] '_Decimal64'
1974/// [GNU] '_Decimal128'
1975/// [GNU] typeof-specifier
1976/// [OBJC] class-name objc-protocol-refs[opt] [TODO]
1977/// [OBJC] typedef-name objc-protocol-refs[opt] [TODO]
Anders Carlsson6fd634f2009-06-24 17:47:40 +00001978/// [C++0x] 'decltype' ( expression )
John Thompson82287d12010-02-05 00:12:22 +00001979/// [AltiVec] '__vector'
John McCallfec54012009-08-03 20:12:06 +00001980bool Parser::ParseOptionalTypeSpecifier(DeclSpec &DS, bool& isInvalid,
Chris Lattner7a0ab5f2009-01-06 06:59:53 +00001981 const char *&PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001982 unsigned &DiagID,
Sebastian Redld9bafa72010-02-03 21:21:43 +00001983 const ParsedTemplateInfo &TemplateInfo,
1984 bool SuppressDeclarations) {
Douglas Gregor12e083c2008-11-07 15:42:26 +00001985 SourceLocation Loc = Tok.getLocation();
1986
1987 switch (Tok.getKind()) {
Chris Lattner166a8fc2009-01-04 23:41:41 +00001988 case tok::identifier: // foo::bar
Douglas Gregorc0b39642010-04-15 23:40:53 +00001989 // If we already have a type specifier, this identifier is not a type.
1990 if (DS.getTypeSpecType() != DeclSpec::TST_unspecified ||
1991 DS.getTypeSpecWidth() != DeclSpec::TSW_unspecified ||
1992 DS.getTypeSpecSign() != DeclSpec::TSS_unspecified)
1993 return false;
John Thompson82287d12010-02-05 00:12:22 +00001994 // Check for need to substitute AltiVec keyword tokens.
1995 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
1996 break;
1997 // Fall through.
Douglas Gregord57959a2009-03-27 23:10:48 +00001998 case tok::kw_typename: // typename foo::bar
Chris Lattner166a8fc2009-01-04 23:41:41 +00001999 // Annotate typenames and C++ scope specifiers. If we get one, just
2000 // recurse to handle whatever we get.
2001 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00002002 return true;
2003 if (Tok.is(tok::identifier))
2004 return false;
2005 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
2006 TemplateInfo, SuppressDeclarations);
Chris Lattner166a8fc2009-01-04 23:41:41 +00002007 case tok::coloncolon: // ::foo::bar
2008 if (NextToken().is(tok::kw_new) || // ::new
2009 NextToken().is(tok::kw_delete)) // ::delete
2010 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002011
Chris Lattner166a8fc2009-01-04 23:41:41 +00002012 // Annotate typenames and C++ scope specifiers. If we get one, just
2013 // recurse to handle whatever we get.
2014 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00002015 return true;
2016 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
2017 TemplateInfo, SuppressDeclarations);
Mike Stump1eb44332009-09-09 15:08:12 +00002018
Douglas Gregor12e083c2008-11-07 15:42:26 +00002019 // simple-type-specifier:
Chris Lattnerb31757b2009-01-06 05:06:21 +00002020 case tok::annot_typename: {
John McCallb3d87482010-08-24 05:47:05 +00002021 if (ParsedType T = getTypeAnnotation(Tok)) {
Nico Weber253e80b2010-11-22 10:30:56 +00002022 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename,
2023 Tok.getAnnotationEndLoc(), PrevSpec,
John McCallb3d87482010-08-24 05:47:05 +00002024 DiagID, T);
2025 } else
Douglas Gregor31a19b62009-04-01 21:51:26 +00002026 DS.SetTypeSpecError();
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002027 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
2028 ConsumeToken(); // The typename
Mike Stump1eb44332009-09-09 15:08:12 +00002029
Douglas Gregor12e083c2008-11-07 15:42:26 +00002030 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
2031 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
2032 // Objective-C interface. If we don't have Objective-C or a '<', this is
2033 // just a normal reference to a typedef name.
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00002034 if (Tok.is(tok::less) && getLang().ObjC1)
2035 ParseObjCProtocolQualifiers(DS);
2036
Douglas Gregor12e083c2008-11-07 15:42:26 +00002037 return true;
2038 }
2039
2040 case tok::kw_short:
John McCallfec54012009-08-03 20:12:06 +00002041 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002042 break;
2043 case tok::kw_long:
2044 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCallfec54012009-08-03 20:12:06 +00002045 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
2046 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002047 else
John McCallfec54012009-08-03 20:12:06 +00002048 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2049 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002050 break;
Francois Pichet338d7f72011-04-28 01:59:37 +00002051 case tok::kw___int64:
2052 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2053 DiagID);
2054 break;
Douglas Gregor12e083c2008-11-07 15:42:26 +00002055 case tok::kw_signed:
John McCallfec54012009-08-03 20:12:06 +00002056 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002057 break;
2058 case tok::kw_unsigned:
John McCallfec54012009-08-03 20:12:06 +00002059 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
2060 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002061 break;
2062 case tok::kw__Complex:
John McCallfec54012009-08-03 20:12:06 +00002063 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
2064 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002065 break;
2066 case tok::kw__Imaginary:
John McCallfec54012009-08-03 20:12:06 +00002067 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
2068 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002069 break;
2070 case tok::kw_void:
John McCallfec54012009-08-03 20:12:06 +00002071 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002072 break;
2073 case tok::kw_char:
John McCallfec54012009-08-03 20:12:06 +00002074 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002075 break;
2076 case tok::kw_int:
John McCallfec54012009-08-03 20:12:06 +00002077 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002078 break;
2079 case tok::kw_float:
John McCallfec54012009-08-03 20:12:06 +00002080 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002081 break;
2082 case tok::kw_double:
John McCallfec54012009-08-03 20:12:06 +00002083 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002084 break;
2085 case tok::kw_wchar_t:
John McCallfec54012009-08-03 20:12:06 +00002086 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002087 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002088 case tok::kw_char16_t:
John McCallfec54012009-08-03 20:12:06 +00002089 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002090 break;
2091 case tok::kw_char32_t:
John McCallfec54012009-08-03 20:12:06 +00002092 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002093 break;
Douglas Gregor12e083c2008-11-07 15:42:26 +00002094 case tok::kw_bool:
2095 case tok::kw__Bool:
John McCallfec54012009-08-03 20:12:06 +00002096 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002097 break;
2098 case tok::kw__Decimal32:
John McCallfec54012009-08-03 20:12:06 +00002099 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
2100 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002101 break;
2102 case tok::kw__Decimal64:
John McCallfec54012009-08-03 20:12:06 +00002103 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
2104 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002105 break;
2106 case tok::kw__Decimal128:
John McCallfec54012009-08-03 20:12:06 +00002107 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
2108 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002109 break;
John Thompson82287d12010-02-05 00:12:22 +00002110 case tok::kw___vector:
2111 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
2112 break;
2113 case tok::kw___pixel:
2114 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
2115 break;
2116
Douglas Gregor12e083c2008-11-07 15:42:26 +00002117 // class-specifier:
2118 case tok::kw_class:
2119 case tok::kw_struct:
Chris Lattner4c97d762009-04-12 21:49:30 +00002120 case tok::kw_union: {
2121 tok::TokenKind Kind = Tok.getKind();
2122 ConsumeToken();
Sebastian Redld9bafa72010-02-03 21:21:43 +00002123 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS_none,
2124 SuppressDeclarations);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002125 return true;
Chris Lattner4c97d762009-04-12 21:49:30 +00002126 }
Douglas Gregor12e083c2008-11-07 15:42:26 +00002127
2128 // enum-specifier:
2129 case tok::kw_enum:
Chris Lattner4c97d762009-04-12 21:49:30 +00002130 ConsumeToken();
Douglas Gregor9b9edd62010-03-02 17:53:14 +00002131 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS_none);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002132 return true;
2133
2134 // cv-qualifier:
2135 case tok::kw_const:
2136 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00002137 DiagID, getLang());
Douglas Gregor12e083c2008-11-07 15:42:26 +00002138 break;
2139 case tok::kw_volatile:
2140 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00002141 DiagID, getLang());
Douglas Gregor12e083c2008-11-07 15:42:26 +00002142 break;
2143 case tok::kw_restrict:
2144 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00002145 DiagID, getLang());
Douglas Gregor12e083c2008-11-07 15:42:26 +00002146 break;
2147
2148 // GNU typeof support.
2149 case tok::kw_typeof:
2150 ParseTypeofSpecifier(DS);
2151 return true;
2152
Anders Carlsson6fd634f2009-06-24 17:47:40 +00002153 // C++0x decltype support.
2154 case tok::kw_decltype:
2155 ParseDecltypeSpecifier(DS);
2156 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00002157
Sean Huntdb5d44b2011-05-19 05:37:45 +00002158 // C++0x type traits support.
2159 case tok::kw___underlying_type:
2160 ParseUnderlyingTypeSpecifier(DS);
2161 return true;
2162
Peter Collingbourne207f4d82011-03-18 22:38:29 +00002163 // OpenCL qualifiers:
2164 case tok::kw_private:
2165 if (!getLang().OpenCL)
2166 return false;
2167 case tok::kw___private:
2168 case tok::kw___global:
2169 case tok::kw___local:
2170 case tok::kw___constant:
2171 case tok::kw___read_only:
2172 case tok::kw___write_only:
2173 case tok::kw___read_write:
2174 ParseOpenCLQualifiers(DS);
2175 break;
2176
Anders Carlsson0b7f7892009-06-26 23:44:14 +00002177 // C++0x auto support.
2178 case tok::kw_auto:
2179 if (!getLang().CPlusPlus0x)
2180 return false;
2181
John McCallfec54012009-08-03 20:12:06 +00002182 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec, DiagID);
Anders Carlsson0b7f7892009-06-26 23:44:14 +00002183 break;
Dawn Perchik52fc3142010-09-03 01:29:35 +00002184
Eli Friedman290eeb02009-06-08 23:27:34 +00002185 case tok::kw___ptr64:
2186 case tok::kw___w64:
Steve Naroff239f0732008-12-25 14:16:32 +00002187 case tok::kw___cdecl:
2188 case tok::kw___stdcall:
2189 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00002190 case tok::kw___thiscall:
John McCall7f040a92010-12-24 02:08:15 +00002191 ParseMicrosoftTypeAttributes(DS.getAttributes());
Chris Lattner837acd02009-01-21 19:19:26 +00002192 return true;
Steve Naroff239f0732008-12-25 14:16:32 +00002193
Dawn Perchik52fc3142010-09-03 01:29:35 +00002194 case tok::kw___pascal:
John McCall7f040a92010-12-24 02:08:15 +00002195 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik52fc3142010-09-03 01:29:35 +00002196 return true;
2197
Douglas Gregor12e083c2008-11-07 15:42:26 +00002198 default:
2199 // Not a type-specifier; do nothing.
2200 return false;
2201 }
2202
2203 // If the specifier combination wasn't legal, issue a diagnostic.
2204 if (isInvalid) {
2205 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner1ab3b962008-11-18 07:48:38 +00002206 // Pick between error or extwarn.
Chris Lattner1ab3b962008-11-18 07:48:38 +00002207 Diag(Tok, DiagID) << PrevSpec;
Douglas Gregor12e083c2008-11-07 15:42:26 +00002208 }
2209 DS.SetRangeEnd(Tok.getLocation());
2210 ConsumeToken(); // whatever we parsed above.
2211 return true;
2212}
Reid Spencer5f016e22007-07-11 17:01:13 +00002213
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002214/// ParseStructDeclaration - Parse a struct declaration without the terminating
2215/// semicolon.
2216///
Reid Spencer5f016e22007-07-11 17:01:13 +00002217/// struct-declaration:
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002218/// specifier-qualifier-list struct-declarator-list
Reid Spencer5f016e22007-07-11 17:01:13 +00002219/// [GNU] __extension__ struct-declaration
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002220/// [GNU] specifier-qualifier-list
Reid Spencer5f016e22007-07-11 17:01:13 +00002221/// struct-declarator-list:
2222/// struct-declarator
2223/// struct-declarator-list ',' struct-declarator
2224/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
2225/// struct-declarator:
2226/// declarator
2227/// [GNU] declarator attributes[opt]
2228/// declarator[opt] ':' constant-expression
2229/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
2230///
Chris Lattnere1359422008-04-10 06:46:29 +00002231void Parser::
John McCallbdd563e2009-11-03 02:38:08 +00002232ParseStructDeclaration(DeclSpec &DS, FieldCallback &Fields) {
Chris Lattnerc46d1a12008-10-20 06:45:43 +00002233 if (Tok.is(tok::kw___extension__)) {
2234 // __extension__ silences extension warnings in the subexpression.
2235 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroff28a7ca82007-08-20 22:28:22 +00002236 ConsumeToken();
Chris Lattnerc46d1a12008-10-20 06:45:43 +00002237 return ParseStructDeclaration(DS, Fields);
2238 }
Mike Stump1eb44332009-09-09 15:08:12 +00002239
Steve Naroff28a7ca82007-08-20 22:28:22 +00002240 // Parse the common specifier-qualifiers-list piece.
Steve Naroff28a7ca82007-08-20 22:28:22 +00002241 ParseSpecifierQualifierList(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00002242
Douglas Gregor4920f1f2009-01-12 22:49:06 +00002243 // If there are no declarators, this is a free-standing declaration
2244 // specifier. Let the actions module cope with it.
Chris Lattner04d66662007-10-09 17:33:22 +00002245 if (Tok.is(tok::semi)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00002246 Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none, DS);
Steve Naroff28a7ca82007-08-20 22:28:22 +00002247 return;
2248 }
2249
2250 // Read struct-declarators until we find the semicolon.
John McCallbdd563e2009-11-03 02:38:08 +00002251 bool FirstDeclarator = true;
Steve Naroff28a7ca82007-08-20 22:28:22 +00002252 while (1) {
John McCall54abf7d2009-11-04 02:18:39 +00002253 ParsingDeclRAIIObject PD(*this);
John McCallbdd563e2009-11-03 02:38:08 +00002254 FieldDeclarator DeclaratorInfo(DS);
2255
2256 // Attributes are only allowed here on successive declarators.
John McCall7f040a92010-12-24 02:08:15 +00002257 if (!FirstDeclarator)
2258 MaybeParseGNUAttributes(DeclaratorInfo.D);
Mike Stump1eb44332009-09-09 15:08:12 +00002259
Steve Naroff28a7ca82007-08-20 22:28:22 +00002260 /// struct-declarator: declarator
2261 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattnera1efc8c2009-12-10 01:59:24 +00002262 if (Tok.isNot(tok::colon)) {
2263 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
2264 ColonProtectionRAIIObject X(*this);
Chris Lattnere1359422008-04-10 06:46:29 +00002265 ParseDeclarator(DeclaratorInfo.D);
Chris Lattnera1efc8c2009-12-10 01:59:24 +00002266 }
Mike Stump1eb44332009-09-09 15:08:12 +00002267
Chris Lattner04d66662007-10-09 17:33:22 +00002268 if (Tok.is(tok::colon)) {
Steve Naroff28a7ca82007-08-20 22:28:22 +00002269 ConsumeToken();
John McCall60d7b3a2010-08-24 06:29:42 +00002270 ExprResult Res(ParseConstantExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002271 if (Res.isInvalid())
Steve Naroff28a7ca82007-08-20 22:28:22 +00002272 SkipUntil(tok::semi, true, true);
Chris Lattner60b1e3e2008-04-10 06:15:14 +00002273 else
Sebastian Redleffa8d12008-12-10 00:02:53 +00002274 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroff28a7ca82007-08-20 22:28:22 +00002275 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00002276
Steve Naroff28a7ca82007-08-20 22:28:22 +00002277 // If attributes exist after the declarator, parse them.
John McCall7f040a92010-12-24 02:08:15 +00002278 MaybeParseGNUAttributes(DeclaratorInfo.D);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002279
John McCallbdd563e2009-11-03 02:38:08 +00002280 // We're done with this declarator; invoke the callback.
John McCalld226f652010-08-21 09:40:31 +00002281 Decl *D = Fields.invoke(DeclaratorInfo);
John McCall54abf7d2009-11-04 02:18:39 +00002282 PD.complete(D);
John McCallbdd563e2009-11-03 02:38:08 +00002283
Steve Naroff28a7ca82007-08-20 22:28:22 +00002284 // If we don't have a comma, it is either the end of the list (a ';')
2285 // or an error, bail out.
Chris Lattner04d66662007-10-09 17:33:22 +00002286 if (Tok.isNot(tok::comma))
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002287 return;
Sebastian Redlab197ba2009-02-09 18:23:29 +00002288
Steve Naroff28a7ca82007-08-20 22:28:22 +00002289 // Consume the comma.
2290 ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00002291
John McCallbdd563e2009-11-03 02:38:08 +00002292 FirstDeclarator = false;
Steve Naroff28a7ca82007-08-20 22:28:22 +00002293 }
Steve Naroff28a7ca82007-08-20 22:28:22 +00002294}
2295
2296/// ParseStructUnionBody
2297/// struct-contents:
2298/// struct-declaration-list
2299/// [EXT] empty
2300/// [GNU] "struct-declaration-list" without terminatoring ';'
2301/// struct-declaration-list:
2302/// struct-declaration
2303/// struct-declaration-list struct-declaration
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002304/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroff28a7ca82007-08-20 22:28:22 +00002305///
Reid Spencer5f016e22007-07-11 17:01:13 +00002306void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
John McCalld226f652010-08-21 09:40:31 +00002307 unsigned TagType, Decl *TagDecl) {
John McCallf312b1e2010-08-26 23:41:50 +00002308 PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc,
2309 "parsing struct/union body");
Mike Stump1eb44332009-09-09 15:08:12 +00002310
Reid Spencer5f016e22007-07-11 17:01:13 +00002311 SourceLocation LBraceLoc = ConsumeBrace();
Mike Stump1eb44332009-09-09 15:08:12 +00002312
Douglas Gregor3218c4b2009-01-09 22:42:13 +00002313 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
Douglas Gregor23c94db2010-07-02 17:43:08 +00002314 Actions.ActOnTagStartDefinition(getCurScope(), TagDecl);
Douglas Gregor72de6672009-01-08 20:45:30 +00002315
Reid Spencer5f016e22007-07-11 17:01:13 +00002316 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
2317 // C++.
Douglas Gregore37ac4f2008-04-13 21:30:24 +00002318 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Douglas Gregor03332962010-07-29 14:29:34 +00002319 Diag(Tok, diag::ext_empty_struct_union)
2320 << (TagType == TST_union);
Reid Spencer5f016e22007-07-11 17:01:13 +00002321
John McCalld226f652010-08-21 09:40:31 +00002322 llvm::SmallVector<Decl *, 32> FieldDecls;
Chris Lattnere1359422008-04-10 06:46:29 +00002323
Reid Spencer5f016e22007-07-11 17:01:13 +00002324 // While we still have something to read, read the declarations in the struct.
Chris Lattner04d66662007-10-09 17:33:22 +00002325 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002326 // Each iteration of this loop reads one struct-declaration.
Mike Stump1eb44332009-09-09 15:08:12 +00002327
Reid Spencer5f016e22007-07-11 17:01:13 +00002328 // Check for extraneous top-level semicolon.
Chris Lattner04d66662007-10-09 17:33:22 +00002329 if (Tok.is(tok::semi)) {
Douglas Gregor9b3064b2009-04-01 22:41:11 +00002330 Diag(Tok, diag::ext_extra_struct_semi)
Douglas Gregorf13ca062010-06-16 23:08:59 +00002331 << DeclSpec::getSpecifierName((DeclSpec::TST)TagType)
Douglas Gregor849b2432010-03-31 17:46:05 +00002332 << FixItHint::CreateRemoval(Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00002333 ConsumeToken();
2334 continue;
2335 }
Chris Lattnere1359422008-04-10 06:46:29 +00002336
2337 // Parse all the comma separated declarators.
John McCall0b7e6782011-03-24 11:26:52 +00002338 DeclSpec DS(AttrFactory);
Mike Stump1eb44332009-09-09 15:08:12 +00002339
John McCallbdd563e2009-11-03 02:38:08 +00002340 if (!Tok.is(tok::at)) {
2341 struct CFieldCallback : FieldCallback {
2342 Parser &P;
John McCalld226f652010-08-21 09:40:31 +00002343 Decl *TagDecl;
2344 llvm::SmallVectorImpl<Decl *> &FieldDecls;
John McCallbdd563e2009-11-03 02:38:08 +00002345
John McCalld226f652010-08-21 09:40:31 +00002346 CFieldCallback(Parser &P, Decl *TagDecl,
2347 llvm::SmallVectorImpl<Decl *> &FieldDecls) :
John McCallbdd563e2009-11-03 02:38:08 +00002348 P(P), TagDecl(TagDecl), FieldDecls(FieldDecls) {}
2349
John McCalld226f652010-08-21 09:40:31 +00002350 virtual Decl *invoke(FieldDeclarator &FD) {
John McCallbdd563e2009-11-03 02:38:08 +00002351 // Install the declarator into the current TagDecl.
John McCalld226f652010-08-21 09:40:31 +00002352 Decl *Field = P.Actions.ActOnField(P.getCurScope(), TagDecl,
John McCall4ba39712009-11-03 21:13:47 +00002353 FD.D.getDeclSpec().getSourceRange().getBegin(),
2354 FD.D, FD.BitfieldSize);
John McCallbdd563e2009-11-03 02:38:08 +00002355 FieldDecls.push_back(Field);
2356 return Field;
Douglas Gregor91a28862009-08-26 14:27:30 +00002357 }
John McCallbdd563e2009-11-03 02:38:08 +00002358 } Callback(*this, TagDecl, FieldDecls);
2359
2360 ParseStructDeclaration(DS, Callback);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002361 } else { // Handle @defs
2362 ConsumeToken();
2363 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
2364 Diag(Tok, diag::err_unexpected_at);
Chris Lattner3e156ad2010-02-02 00:37:27 +00002365 SkipUntil(tok::semi, true);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002366 continue;
2367 }
2368 ConsumeToken();
2369 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
2370 if (!Tok.is(tok::identifier)) {
2371 Diag(Tok, diag::err_expected_ident);
Chris Lattner3e156ad2010-02-02 00:37:27 +00002372 SkipUntil(tok::semi, true);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002373 continue;
2374 }
John McCalld226f652010-08-21 09:40:31 +00002375 llvm::SmallVector<Decl *, 16> Fields;
Douglas Gregor23c94db2010-07-02 17:43:08 +00002376 Actions.ActOnDefs(getCurScope(), TagDecl, Tok.getLocation(),
Douglas Gregor44b43212008-12-11 16:49:14 +00002377 Tok.getIdentifierInfo(), Fields);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002378 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
2379 ConsumeToken();
2380 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
Mike Stump1eb44332009-09-09 15:08:12 +00002381 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002382
Chris Lattner04d66662007-10-09 17:33:22 +00002383 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002384 ConsumeToken();
Chris Lattner04d66662007-10-09 17:33:22 +00002385 } else if (Tok.is(tok::r_brace)) {
Chris Lattner3e156ad2010-02-02 00:37:27 +00002386 ExpectAndConsume(tok::semi, diag::ext_expected_semi_decl_list);
Reid Spencer5f016e22007-07-11 17:01:13 +00002387 break;
2388 } else {
Chris Lattner3e156ad2010-02-02 00:37:27 +00002389 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
2390 // Skip to end of block or statement to avoid ext-warning on extra ';'.
Reid Spencer5f016e22007-07-11 17:01:13 +00002391 SkipUntil(tok::r_brace, true, true);
Chris Lattner3e156ad2010-02-02 00:37:27 +00002392 // If we stopped at a ';', eat it.
2393 if (Tok.is(tok::semi)) ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00002394 }
2395 }
Mike Stump1eb44332009-09-09 15:08:12 +00002396
Steve Naroff60fccee2007-10-29 21:38:07 +00002397 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00002398
John McCall0b7e6782011-03-24 11:26:52 +00002399 ParsedAttributes attrs(AttrFactory);
Reid Spencer5f016e22007-07-11 17:01:13 +00002400 // If attributes exist after struct contents, parse them.
John McCall7f040a92010-12-24 02:08:15 +00002401 MaybeParseGNUAttributes(attrs);
Daniel Dunbar1bfe1c22008-10-03 02:03:53 +00002402
Douglas Gregor23c94db2010-07-02 17:43:08 +00002403 Actions.ActOnFields(getCurScope(),
Jay Foadbeaaccd2009-05-21 09:52:38 +00002404 RecordLoc, TagDecl, FieldDecls.data(), FieldDecls.size(),
Daniel Dunbar1bfe1c22008-10-03 02:03:53 +00002405 LBraceLoc, RBraceLoc,
John McCall7f040a92010-12-24 02:08:15 +00002406 attrs.getList());
Douglas Gregor72de6672009-01-08 20:45:30 +00002407 StructScope.Exit();
Douglas Gregor23c94db2010-07-02 17:43:08 +00002408 Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl, RBraceLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00002409}
2410
Reid Spencer5f016e22007-07-11 17:01:13 +00002411/// ParseEnumSpecifier
2412/// enum-specifier: [C99 6.7.2.2]
2413/// 'enum' identifier[opt] '{' enumerator-list '}'
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002414///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Reid Spencer5f016e22007-07-11 17:01:13 +00002415/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
2416/// '}' attributes[opt]
2417/// 'enum' identifier
2418/// [GNU] 'enum' attributes[opt] identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002419///
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002420/// [C++0x] enum-head '{' enumerator-list[opt] '}'
2421/// [C++0x] enum-head '{' enumerator-list ',' '}'
2422///
2423/// enum-head: [C++0x]
2424/// enum-key attributes[opt] identifier[opt] enum-base[opt]
2425/// enum-key attributes[opt] nested-name-specifier identifier enum-base[opt]
2426///
2427/// enum-key: [C++0x]
2428/// 'enum'
2429/// 'enum' 'class'
2430/// 'enum' 'struct'
2431///
2432/// enum-base: [C++0x]
2433/// ':' type-specifier-seq
2434///
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002435/// [C++] elaborated-type-specifier:
2436/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
2437///
Chris Lattner4c97d762009-04-12 21:49:30 +00002438void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregor9b9edd62010-03-02 17:53:14 +00002439 const ParsedTemplateInfo &TemplateInfo,
Chris Lattner4c97d762009-04-12 21:49:30 +00002440 AccessSpecifier AS) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002441 // Parse the tag portion of this.
Douglas Gregor374929f2009-09-18 15:37:17 +00002442 if (Tok.is(tok::code_completion)) {
2443 // Code completion for an enum name.
Douglas Gregor23c94db2010-07-02 17:43:08 +00002444 Actions.CodeCompleteTag(getCurScope(), DeclSpec::TST_enum);
Douglas Gregordc845342010-05-25 05:58:43 +00002445 ConsumeCodeCompletionToken();
Douglas Gregor374929f2009-09-18 15:37:17 +00002446 }
2447
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002448 // If attributes exist after tag, parse them.
John McCall0b7e6782011-03-24 11:26:52 +00002449 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00002450 MaybeParseGNUAttributes(attrs);
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002451
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00002452 CXXScopeSpec &SS = DS.getTypeSpecScope();
John McCall9ba61662010-02-26 08:45:28 +00002453 if (getLang().CPlusPlus) {
John McCallb3d87482010-08-24 05:47:05 +00002454 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), false))
John McCall9ba61662010-02-26 08:45:28 +00002455 return;
2456
2457 if (SS.isSet() && Tok.isNot(tok::identifier)) {
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002458 Diag(Tok, diag::err_expected_ident);
2459 if (Tok.isNot(tok::l_brace)) {
2460 // Has no name and is not a definition.
2461 // Skip the rest of this declarator, up until the comma or semicolon.
2462 SkipUntil(tok::comma, true);
2463 return;
2464 }
2465 }
2466 }
Mike Stump1eb44332009-09-09 15:08:12 +00002467
Douglas Gregor86f208c2011-02-22 20:32:04 +00002468 bool AllowFixedUnderlyingType = getLang().CPlusPlus0x || getLang().Microsoft;
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002469 bool IsScopedEnum = false;
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00002470 bool IsScopedUsingClassTag = false;
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002471
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00002472 if (getLang().CPlusPlus0x &&
2473 (Tok.is(tok::kw_class) || Tok.is(tok::kw_struct))) {
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002474 IsScopedEnum = true;
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00002475 IsScopedUsingClassTag = Tok.is(tok::kw_class);
2476 ConsumeToken();
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002477 }
2478
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002479 // Must have either 'enum name' or 'enum {...}'.
Douglas Gregorb9075602011-02-22 02:55:24 +00002480 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace) &&
2481 (AllowFixedUnderlyingType && Tok.isNot(tok::colon))) {
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002482 Diag(Tok, diag::err_expected_ident_lbrace);
Mike Stump1eb44332009-09-09 15:08:12 +00002483
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002484 // Skip the rest of this declarator, up until the comma or semicolon.
2485 SkipUntil(tok::comma, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00002486 return;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002487 }
Mike Stump1eb44332009-09-09 15:08:12 +00002488
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002489 // If an identifier is present, consume and remember it.
2490 IdentifierInfo *Name = 0;
2491 SourceLocation NameLoc;
2492 if (Tok.is(tok::identifier)) {
2493 Name = Tok.getIdentifierInfo();
2494 NameLoc = ConsumeToken();
2495 }
Mike Stump1eb44332009-09-09 15:08:12 +00002496
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002497 if (!Name && IsScopedEnum) {
2498 // C++0x 7.2p2: The optional identifier shall not be omitted in the
2499 // declaration of a scoped enumeration.
2500 Diag(Tok, diag::err_scoped_enum_missing_identifier);
2501 IsScopedEnum = false;
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00002502 IsScopedUsingClassTag = false;
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002503 }
2504
2505 TypeResult BaseType;
2506
Douglas Gregora61b3e72010-12-01 17:42:47 +00002507 // Parse the fixed underlying type.
Douglas Gregorb9075602011-02-22 02:55:24 +00002508 if (AllowFixedUnderlyingType && Tok.is(tok::colon)) {
Douglas Gregora61b3e72010-12-01 17:42:47 +00002509 bool PossibleBitfield = false;
2510 if (getCurScope()->getFlags() & Scope::ClassScope) {
2511 // If we're in class scope, this can either be an enum declaration with
2512 // an underlying type, or a declaration of a bitfield member. We try to
2513 // use a simple disambiguation scheme first to catch the common cases
2514 // (integer literal, sizeof); if it's still ambiguous, we then consider
2515 // anything that's a simple-type-specifier followed by '(' as an
2516 // expression. This suffices because function types are not valid
2517 // underlying types anyway.
2518 TPResult TPR = isExpressionOrTypeSpecifierSimple(NextToken().getKind());
2519 // If the next token starts an expression, we know we're parsing a
2520 // bit-field. This is the common case.
2521 if (TPR == TPResult::True())
2522 PossibleBitfield = true;
2523 // If the next token starts a type-specifier-seq, it may be either a
2524 // a fixed underlying type or the start of a function-style cast in C++;
2525 // lookahead one more token to see if it's obvious that we have a
2526 // fixed underlying type.
2527 else if (TPR == TPResult::False() &&
2528 GetLookAheadToken(2).getKind() == tok::semi) {
2529 // Consume the ':'.
2530 ConsumeToken();
2531 } else {
2532 // We have the start of a type-specifier-seq, so we have to perform
2533 // tentative parsing to determine whether we have an expression or a
2534 // type.
2535 TentativeParsingAction TPA(*this);
2536
2537 // Consume the ':'.
2538 ConsumeToken();
2539
Douglas Gregor86f208c2011-02-22 20:32:04 +00002540 if ((getLang().CPlusPlus &&
2541 isCXXDeclarationSpecifier() != TPResult::True()) ||
2542 (!getLang().CPlusPlus && !isDeclarationSpecifier(true))) {
Douglas Gregora61b3e72010-12-01 17:42:47 +00002543 // We'll parse this as a bitfield later.
2544 PossibleBitfield = true;
2545 TPA.Revert();
2546 } else {
2547 // We have a type-specifier-seq.
2548 TPA.Commit();
2549 }
2550 }
2551 } else {
2552 // Consume the ':'.
2553 ConsumeToken();
2554 }
2555
2556 if (!PossibleBitfield) {
2557 SourceRange Range;
2558 BaseType = ParseTypeName(&Range);
Douglas Gregor86f208c2011-02-22 20:32:04 +00002559
2560 if (!getLang().CPlusPlus0x)
2561 Diag(StartLoc, diag::ext_ms_enum_fixed_underlying_type)
2562 << Range;
Douglas Gregora61b3e72010-12-01 17:42:47 +00002563 }
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002564 }
2565
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002566 // There are three options here. If we have 'enum foo;', then this is a
2567 // forward declaration. If we have 'enum foo {...' then this is a
2568 // definition. Otherwise we have something like 'enum foo xyz', a reference.
2569 //
2570 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
2571 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
2572 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
2573 //
John McCallf312b1e2010-08-26 23:41:50 +00002574 Sema::TagUseKind TUK;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002575 if (Tok.is(tok::l_brace))
John McCallf312b1e2010-08-26 23:41:50 +00002576 TUK = Sema::TUK_Definition;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002577 else if (Tok.is(tok::semi))
John McCallf312b1e2010-08-26 23:41:50 +00002578 TUK = Sema::TUK_Declaration;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002579 else
John McCallf312b1e2010-08-26 23:41:50 +00002580 TUK = Sema::TUK_Reference;
Douglas Gregor8fc6d232010-05-03 17:48:54 +00002581
2582 // enums cannot be templates, although they can be referenced from a
2583 // template.
2584 if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate &&
John McCallf312b1e2010-08-26 23:41:50 +00002585 TUK != Sema::TUK_Reference) {
Douglas Gregor8fc6d232010-05-03 17:48:54 +00002586 Diag(Tok, diag::err_enum_template);
2587
2588 // Skip the rest of this declarator, up until the comma or semicolon.
2589 SkipUntil(tok::comma, true);
2590 return;
2591 }
2592
Douglas Gregorb9075602011-02-22 02:55:24 +00002593 if (!Name && TUK != Sema::TUK_Definition) {
2594 Diag(Tok, diag::err_enumerator_unnamed_no_def);
2595
2596 // Skip the rest of this declarator, up until the comma or semicolon.
2597 SkipUntil(tok::comma, true);
2598 return;
2599 }
2600
Douglas Gregor402abb52009-05-28 23:31:59 +00002601 bool Owned = false;
John McCallc4e70192009-09-11 04:59:25 +00002602 bool IsDependent = false;
Douglas Gregor48c89f42010-04-24 16:38:41 +00002603 const char *PrevSpec = 0;
2604 unsigned DiagID;
John McCalld226f652010-08-21 09:40:31 +00002605 Decl *TagDecl = Actions.ActOnTag(getCurScope(), DeclSpec::TST_enum, TUK,
John McCall7f040a92010-12-24 02:08:15 +00002606 StartLoc, SS, Name, NameLoc, attrs.getList(),
John McCalld226f652010-08-21 09:40:31 +00002607 AS,
John McCallf312b1e2010-08-26 23:41:50 +00002608 MultiTemplateParamsArg(Actions),
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002609 Owned, IsDependent, IsScopedEnum,
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00002610 IsScopedUsingClassTag, BaseType);
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002611
Douglas Gregor48c89f42010-04-24 16:38:41 +00002612 if (IsDependent) {
2613 // This enum has a dependent nested-name-specifier. Handle it as a
2614 // dependent tag.
2615 if (!Name) {
2616 DS.SetTypeSpecError();
2617 Diag(Tok, diag::err_expected_type_name_after_typename);
2618 return;
2619 }
2620
Douglas Gregor23c94db2010-07-02 17:43:08 +00002621 TypeResult Type = Actions.ActOnDependentTag(getCurScope(), DeclSpec::TST_enum,
Douglas Gregor48c89f42010-04-24 16:38:41 +00002622 TUK, SS, Name, StartLoc,
2623 NameLoc);
2624 if (Type.isInvalid()) {
2625 DS.SetTypeSpecError();
2626 return;
2627 }
2628
Abramo Bagnara0daaf322011-03-16 20:16:18 +00002629 if (DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc,
2630 NameLoc.isValid() ? NameLoc : StartLoc,
2631 PrevSpec, DiagID, Type.get()))
Douglas Gregor48c89f42010-04-24 16:38:41 +00002632 Diag(StartLoc, DiagID) << PrevSpec;
2633
2634 return;
2635 }
Mike Stump1eb44332009-09-09 15:08:12 +00002636
John McCalld226f652010-08-21 09:40:31 +00002637 if (!TagDecl) {
Douglas Gregor48c89f42010-04-24 16:38:41 +00002638 // The action failed to produce an enumeration tag. If this is a
2639 // definition, consume the entire definition.
2640 if (Tok.is(tok::l_brace)) {
2641 ConsumeBrace();
2642 SkipUntil(tok::r_brace);
2643 }
2644
2645 DS.SetTypeSpecError();
2646 return;
2647 }
2648
Chris Lattner04d66662007-10-09 17:33:22 +00002649 if (Tok.is(tok::l_brace))
Reid Spencer5f016e22007-07-11 17:01:13 +00002650 ParseEnumBody(StartLoc, TagDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00002651
Abramo Bagnara0daaf322011-03-16 20:16:18 +00002652 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc,
2653 NameLoc.isValid() ? NameLoc : StartLoc,
2654 PrevSpec, DiagID, TagDecl, Owned))
John McCallfec54012009-08-03 20:12:06 +00002655 Diag(StartLoc, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00002656}
2657
2658/// ParseEnumBody - Parse a {} enclosed enumerator-list.
2659/// enumerator-list:
2660/// enumerator
2661/// enumerator-list ',' enumerator
2662/// enumerator:
2663/// enumeration-constant
2664/// enumeration-constant '=' constant-expression
2665/// enumeration-constant:
2666/// identifier
2667///
John McCalld226f652010-08-21 09:40:31 +00002668void Parser::ParseEnumBody(SourceLocation StartLoc, Decl *EnumDecl) {
Douglas Gregor074149e2009-01-05 19:45:36 +00002669 // Enter the scope of the enum body and start the definition.
2670 ParseScope EnumScope(this, Scope::DeclScope);
Douglas Gregor23c94db2010-07-02 17:43:08 +00002671 Actions.ActOnTagStartDefinition(getCurScope(), EnumDecl);
Douglas Gregor074149e2009-01-05 19:45:36 +00002672
Reid Spencer5f016e22007-07-11 17:01:13 +00002673 SourceLocation LBraceLoc = ConsumeBrace();
Mike Stump1eb44332009-09-09 15:08:12 +00002674
Chris Lattner7946dd32007-08-27 17:24:30 +00002675 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
Chris Lattner04d66662007-10-09 17:33:22 +00002676 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Fariborz Jahanian05115522010-05-28 22:23:22 +00002677 Diag(Tok, diag::error_empty_enum);
Mike Stump1eb44332009-09-09 15:08:12 +00002678
John McCalld226f652010-08-21 09:40:31 +00002679 llvm::SmallVector<Decl *, 32> EnumConstantDecls;
Reid Spencer5f016e22007-07-11 17:01:13 +00002680
John McCalld226f652010-08-21 09:40:31 +00002681 Decl *LastEnumConstDecl = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00002682
Reid Spencer5f016e22007-07-11 17:01:13 +00002683 // Parse the enumerator-list.
Chris Lattner04d66662007-10-09 17:33:22 +00002684 while (Tok.is(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002685 IdentifierInfo *Ident = Tok.getIdentifierInfo();
2686 SourceLocation IdentLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00002687
John McCall5b629aa2010-10-22 23:36:17 +00002688 // If attributes exist after the enumerator, parse them.
John McCall0b7e6782011-03-24 11:26:52 +00002689 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00002690 MaybeParseGNUAttributes(attrs);
John McCall5b629aa2010-10-22 23:36:17 +00002691
Reid Spencer5f016e22007-07-11 17:01:13 +00002692 SourceLocation EqualLoc;
John McCall60d7b3a2010-08-24 06:29:42 +00002693 ExprResult AssignedVal;
Chris Lattner04d66662007-10-09 17:33:22 +00002694 if (Tok.is(tok::equal)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002695 EqualLoc = ConsumeToken();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002696 AssignedVal = ParseConstantExpression();
2697 if (AssignedVal.isInvalid())
Reid Spencer5f016e22007-07-11 17:01:13 +00002698 SkipUntil(tok::comma, tok::r_brace, true, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00002699 }
Mike Stump1eb44332009-09-09 15:08:12 +00002700
Reid Spencer5f016e22007-07-11 17:01:13 +00002701 // Install the enumerator constant into EnumDecl.
John McCalld226f652010-08-21 09:40:31 +00002702 Decl *EnumConstDecl = Actions.ActOnEnumConstant(getCurScope(), EnumDecl,
2703 LastEnumConstDecl,
2704 IdentLoc, Ident,
John McCall7f040a92010-12-24 02:08:15 +00002705 attrs.getList(), EqualLoc,
John McCalld226f652010-08-21 09:40:31 +00002706 AssignedVal.release());
Reid Spencer5f016e22007-07-11 17:01:13 +00002707 EnumConstantDecls.push_back(EnumConstDecl);
2708 LastEnumConstDecl = EnumConstDecl;
Mike Stump1eb44332009-09-09 15:08:12 +00002709
Douglas Gregor751f6922010-09-07 14:51:08 +00002710 if (Tok.is(tok::identifier)) {
2711 // We're missing a comma between enumerators.
2712 SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation);
2713 Diag(Loc, diag::err_enumerator_list_missing_comma)
2714 << FixItHint::CreateInsertion(Loc, ", ");
2715 continue;
2716 }
2717
Chris Lattner04d66662007-10-09 17:33:22 +00002718 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +00002719 break;
2720 SourceLocation CommaLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00002721
2722 if (Tok.isNot(tok::identifier) &&
Douglas Gregor9b3064b2009-04-01 22:41:11 +00002723 !(getLang().C99 || getLang().CPlusPlus0x))
2724 Diag(CommaLoc, diag::ext_enumerator_list_comma)
2725 << getLang().CPlusPlus
Douglas Gregor849b2432010-03-31 17:46:05 +00002726 << FixItHint::CreateRemoval(CommaLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00002727 }
Mike Stump1eb44332009-09-09 15:08:12 +00002728
Reid Spencer5f016e22007-07-11 17:01:13 +00002729 // Eat the }.
Mike Stumpc6e35aa2009-05-16 07:06:02 +00002730 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00002731
Reid Spencer5f016e22007-07-11 17:01:13 +00002732 // If attributes exist after the identifier list, parse them.
John McCall0b7e6782011-03-24 11:26:52 +00002733 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00002734 MaybeParseGNUAttributes(attrs);
Douglas Gregor72de6672009-01-08 20:45:30 +00002735
Edward O'Callaghanfee13812009-08-08 14:36:57 +00002736 Actions.ActOnEnumBody(StartLoc, LBraceLoc, RBraceLoc, EnumDecl,
2737 EnumConstantDecls.data(), EnumConstantDecls.size(),
John McCall7f040a92010-12-24 02:08:15 +00002738 getCurScope(), attrs.getList());
Mike Stump1eb44332009-09-09 15:08:12 +00002739
Douglas Gregor72de6672009-01-08 20:45:30 +00002740 EnumScope.Exit();
Douglas Gregor23c94db2010-07-02 17:43:08 +00002741 Actions.ActOnTagFinishDefinition(getCurScope(), EnumDecl, RBraceLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00002742}
2743
2744/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff5f8aa692008-02-11 23:15:56 +00002745/// start of a type-qualifier-list.
2746bool Parser::isTypeQualifier() const {
2747 switch (Tok.getKind()) {
2748 default: return false;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00002749
2750 // type-qualifier only in OpenCL
2751 case tok::kw_private:
2752 return getLang().OpenCL;
2753
Steve Naroff5f8aa692008-02-11 23:15:56 +00002754 // type-qualifier
2755 case tok::kw_const:
2756 case tok::kw_volatile:
2757 case tok::kw_restrict:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00002758 case tok::kw___private:
2759 case tok::kw___local:
2760 case tok::kw___global:
2761 case tok::kw___constant:
2762 case tok::kw___read_only:
2763 case tok::kw___read_write:
2764 case tok::kw___write_only:
Steve Naroff5f8aa692008-02-11 23:15:56 +00002765 return true;
2766 }
2767}
2768
Chris Lattnerb3a4e432010-02-28 18:18:36 +00002769/// isKnownToBeTypeSpecifier - Return true if we know that the specified token
2770/// is definitely a type-specifier. Return false if it isn't part of a type
2771/// specifier or if we're not sure.
2772bool Parser::isKnownToBeTypeSpecifier(const Token &Tok) const {
2773 switch (Tok.getKind()) {
2774 default: return false;
2775 // type-specifiers
2776 case tok::kw_short:
2777 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00002778 case tok::kw___int64:
Chris Lattnerb3a4e432010-02-28 18:18:36 +00002779 case tok::kw_signed:
2780 case tok::kw_unsigned:
2781 case tok::kw__Complex:
2782 case tok::kw__Imaginary:
2783 case tok::kw_void:
2784 case tok::kw_char:
2785 case tok::kw_wchar_t:
2786 case tok::kw_char16_t:
2787 case tok::kw_char32_t:
2788 case tok::kw_int:
2789 case tok::kw_float:
2790 case tok::kw_double:
2791 case tok::kw_bool:
2792 case tok::kw__Bool:
2793 case tok::kw__Decimal32:
2794 case tok::kw__Decimal64:
2795 case tok::kw__Decimal128:
2796 case tok::kw___vector:
2797
2798 // struct-or-union-specifier (C99) or class-specifier (C++)
2799 case tok::kw_class:
2800 case tok::kw_struct:
2801 case tok::kw_union:
2802 // enum-specifier
2803 case tok::kw_enum:
2804
2805 // typedef-name
2806 case tok::annot_typename:
2807 return true;
2808 }
2809}
2810
Steve Naroff5f8aa692008-02-11 23:15:56 +00002811/// isTypeSpecifierQualifier - Return true if the current token could be the
Reid Spencer5f016e22007-07-11 17:01:13 +00002812/// start of a specifier-qualifier-list.
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002813bool Parser::isTypeSpecifierQualifier() {
Reid Spencer5f016e22007-07-11 17:01:13 +00002814 switch (Tok.getKind()) {
2815 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002816
Chris Lattner166a8fc2009-01-04 23:41:41 +00002817 case tok::identifier: // foo::bar
John Thompson82287d12010-02-05 00:12:22 +00002818 if (TryAltiVecVectorToken())
2819 return true;
2820 // Fall through.
Douglas Gregord57959a2009-03-27 23:10:48 +00002821 case tok::kw_typename: // typename T::type
Chris Lattner166a8fc2009-01-04 23:41:41 +00002822 // Annotate typenames and C++ scope specifiers. If we get one, just
2823 // recurse to handle whatever we get.
2824 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00002825 return true;
2826 if (Tok.is(tok::identifier))
2827 return false;
2828 return isTypeSpecifierQualifier();
Douglas Gregord57959a2009-03-27 23:10:48 +00002829
Chris Lattner166a8fc2009-01-04 23:41:41 +00002830 case tok::coloncolon: // ::foo::bar
2831 if (NextToken().is(tok::kw_new) || // ::new
2832 NextToken().is(tok::kw_delete)) // ::delete
2833 return false;
2834
Chris Lattner166a8fc2009-01-04 23:41:41 +00002835 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00002836 return true;
2837 return isTypeSpecifierQualifier();
Mike Stump1eb44332009-09-09 15:08:12 +00002838
Reid Spencer5f016e22007-07-11 17:01:13 +00002839 // GNU attributes support.
2840 case tok::kw___attribute:
Steve Naroffd1861fd2007-07-31 12:34:36 +00002841 // GNU typeof support.
2842 case tok::kw_typeof:
Mike Stump1eb44332009-09-09 15:08:12 +00002843
Reid Spencer5f016e22007-07-11 17:01:13 +00002844 // type-specifiers
2845 case tok::kw_short:
2846 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00002847 case tok::kw___int64:
Reid Spencer5f016e22007-07-11 17:01:13 +00002848 case tok::kw_signed:
2849 case tok::kw_unsigned:
2850 case tok::kw__Complex:
2851 case tok::kw__Imaginary:
2852 case tok::kw_void:
2853 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00002854 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002855 case tok::kw_char16_t:
2856 case tok::kw_char32_t:
Reid Spencer5f016e22007-07-11 17:01:13 +00002857 case tok::kw_int:
2858 case tok::kw_float:
2859 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00002860 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00002861 case tok::kw__Bool:
2862 case tok::kw__Decimal32:
2863 case tok::kw__Decimal64:
2864 case tok::kw__Decimal128:
John Thompson82287d12010-02-05 00:12:22 +00002865 case tok::kw___vector:
Mike Stump1eb44332009-09-09 15:08:12 +00002866
Chris Lattner99dc9142008-04-13 18:59:07 +00002867 // struct-or-union-specifier (C99) or class-specifier (C++)
2868 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00002869 case tok::kw_struct:
2870 case tok::kw_union:
2871 // enum-specifier
2872 case tok::kw_enum:
Mike Stump1eb44332009-09-09 15:08:12 +00002873
Reid Spencer5f016e22007-07-11 17:01:13 +00002874 // type-qualifier
2875 case tok::kw_const:
2876 case tok::kw_volatile:
2877 case tok::kw_restrict:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002878
2879 // typedef-name
Chris Lattnerb31757b2009-01-06 05:06:21 +00002880 case tok::annot_typename:
Reid Spencer5f016e22007-07-11 17:01:13 +00002881 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00002882
Chris Lattner7c186be2008-10-20 00:25:30 +00002883 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
2884 case tok::less:
2885 return getLang().ObjC1;
Mike Stump1eb44332009-09-09 15:08:12 +00002886
Steve Naroff239f0732008-12-25 14:16:32 +00002887 case tok::kw___cdecl:
2888 case tok::kw___stdcall:
2889 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00002890 case tok::kw___thiscall:
Eli Friedman290eeb02009-06-08 23:27:34 +00002891 case tok::kw___w64:
2892 case tok::kw___ptr64:
Dawn Perchik52fc3142010-09-03 01:29:35 +00002893 case tok::kw___pascal:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00002894
2895 case tok::kw___private:
2896 case tok::kw___local:
2897 case tok::kw___global:
2898 case tok::kw___constant:
2899 case tok::kw___read_only:
2900 case tok::kw___read_write:
2901 case tok::kw___write_only:
2902
Eli Friedman290eeb02009-06-08 23:27:34 +00002903 return true;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00002904
2905 case tok::kw_private:
2906 return getLang().OpenCL;
Reid Spencer5f016e22007-07-11 17:01:13 +00002907 }
2908}
2909
2910/// isDeclarationSpecifier() - Return true if the current token is part of a
2911/// declaration specifier.
Douglas Gregor9497a732010-09-16 01:51:54 +00002912///
2913/// \param DisambiguatingWithExpression True to indicate that the purpose of
2914/// this check is to disambiguate between an expression and a declaration.
2915bool Parser::isDeclarationSpecifier(bool DisambiguatingWithExpression) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002916 switch (Tok.getKind()) {
2917 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002918
Peter Collingbourne207f4d82011-03-18 22:38:29 +00002919 case tok::kw_private:
2920 return getLang().OpenCL;
2921
Chris Lattner166a8fc2009-01-04 23:41:41 +00002922 case tok::identifier: // foo::bar
Steve Naroff61f72cb2009-03-09 21:12:44 +00002923 // Unfortunate hack to support "Class.factoryMethod" notation.
2924 if (getLang().ObjC1 && NextToken().is(tok::period))
2925 return false;
John Thompson82287d12010-02-05 00:12:22 +00002926 if (TryAltiVecVectorToken())
2927 return true;
2928 // Fall through.
Douglas Gregord57959a2009-03-27 23:10:48 +00002929 case tok::kw_typename: // typename T::type
Chris Lattner166a8fc2009-01-04 23:41:41 +00002930 // Annotate typenames and C++ scope specifiers. If we get one, just
2931 // recurse to handle whatever we get.
2932 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00002933 return true;
2934 if (Tok.is(tok::identifier))
2935 return false;
Douglas Gregor9497a732010-09-16 01:51:54 +00002936
2937 // If we're in Objective-C and we have an Objective-C class type followed
2938 // by an identifier and then either ':' or ']', in a place where an
2939 // expression is permitted, then this is probably a class message send
2940 // missing the initial '['. In this case, we won't consider this to be
2941 // the start of a declaration.
2942 if (DisambiguatingWithExpression &&
2943 isStartOfObjCClassMessageMissingOpenBracket())
2944 return false;
2945
John McCall9ba61662010-02-26 08:45:28 +00002946 return isDeclarationSpecifier();
2947
Chris Lattner166a8fc2009-01-04 23:41:41 +00002948 case tok::coloncolon: // ::foo::bar
2949 if (NextToken().is(tok::kw_new) || // ::new
2950 NextToken().is(tok::kw_delete)) // ::delete
2951 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002952
Chris Lattner166a8fc2009-01-04 23:41:41 +00002953 // Annotate typenames and C++ scope specifiers. If we get one, just
2954 // recurse to handle whatever we get.
2955 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00002956 return true;
2957 return isDeclarationSpecifier();
Mike Stump1eb44332009-09-09 15:08:12 +00002958
Reid Spencer5f016e22007-07-11 17:01:13 +00002959 // storage-class-specifier
2960 case tok::kw_typedef:
2961 case tok::kw_extern:
Steve Naroff8d54bf22007-12-18 00:16:02 +00002962 case tok::kw___private_extern__:
Reid Spencer5f016e22007-07-11 17:01:13 +00002963 case tok::kw_static:
2964 case tok::kw_auto:
2965 case tok::kw_register:
2966 case tok::kw___thread:
Mike Stump1eb44332009-09-09 15:08:12 +00002967
Reid Spencer5f016e22007-07-11 17:01:13 +00002968 // type-specifiers
2969 case tok::kw_short:
2970 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00002971 case tok::kw___int64:
Reid Spencer5f016e22007-07-11 17:01:13 +00002972 case tok::kw_signed:
2973 case tok::kw_unsigned:
2974 case tok::kw__Complex:
2975 case tok::kw__Imaginary:
2976 case tok::kw_void:
2977 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00002978 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002979 case tok::kw_char16_t:
2980 case tok::kw_char32_t:
2981
Reid Spencer5f016e22007-07-11 17:01:13 +00002982 case tok::kw_int:
2983 case tok::kw_float:
2984 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00002985 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00002986 case tok::kw__Bool:
2987 case tok::kw__Decimal32:
2988 case tok::kw__Decimal64:
2989 case tok::kw__Decimal128:
John Thompson82287d12010-02-05 00:12:22 +00002990 case tok::kw___vector:
Mike Stump1eb44332009-09-09 15:08:12 +00002991
Chris Lattner99dc9142008-04-13 18:59:07 +00002992 // struct-or-union-specifier (C99) or class-specifier (C++)
2993 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00002994 case tok::kw_struct:
2995 case tok::kw_union:
2996 // enum-specifier
2997 case tok::kw_enum:
Mike Stump1eb44332009-09-09 15:08:12 +00002998
Reid Spencer5f016e22007-07-11 17:01:13 +00002999 // type-qualifier
3000 case tok::kw_const:
3001 case tok::kw_volatile:
3002 case tok::kw_restrict:
Steve Naroffd1861fd2007-07-31 12:34:36 +00003003
Reid Spencer5f016e22007-07-11 17:01:13 +00003004 // function-specifier
3005 case tok::kw_inline:
Douglas Gregorb48fe382008-10-31 09:07:45 +00003006 case tok::kw_virtual:
3007 case tok::kw_explicit:
Chris Lattnerd6c7c182007-08-09 16:40:21 +00003008
Peter Collingbournec6eb44b2011-04-15 00:35:57 +00003009 // static_assert-declaration
3010 case tok::kw__Static_assert:
3011
Chris Lattner1ef08762007-08-09 17:01:07 +00003012 // GNU typeof support.
3013 case tok::kw_typeof:
Mike Stump1eb44332009-09-09 15:08:12 +00003014
Chris Lattner1ef08762007-08-09 17:01:07 +00003015 // GNU attributes.
Chris Lattnerd6c7c182007-08-09 16:40:21 +00003016 case tok::kw___attribute:
Reid Spencer5f016e22007-07-11 17:01:13 +00003017 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00003018
Chris Lattnerf3948c42008-07-26 03:38:44 +00003019 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
3020 case tok::less:
3021 return getLang().ObjC1;
Mike Stump1eb44332009-09-09 15:08:12 +00003022
Douglas Gregord9d75e52011-04-27 05:41:15 +00003023 // typedef-name
3024 case tok::annot_typename:
3025 return !DisambiguatingWithExpression ||
3026 !isStartOfObjCClassMessageMissingOpenBracket();
3027
Steve Naroff47f52092009-01-06 19:34:12 +00003028 case tok::kw___declspec:
Steve Naroff239f0732008-12-25 14:16:32 +00003029 case tok::kw___cdecl:
3030 case tok::kw___stdcall:
3031 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00003032 case tok::kw___thiscall:
Eli Friedman290eeb02009-06-08 23:27:34 +00003033 case tok::kw___w64:
3034 case tok::kw___ptr64:
3035 case tok::kw___forceinline:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003036 case tok::kw___pascal:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003037
3038 case tok::kw___private:
3039 case tok::kw___local:
3040 case tok::kw___global:
3041 case tok::kw___constant:
3042 case tok::kw___read_only:
3043 case tok::kw___read_write:
3044 case tok::kw___write_only:
3045
Eli Friedman290eeb02009-06-08 23:27:34 +00003046 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00003047 }
3048}
3049
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003050bool Parser::isConstructorDeclarator() {
3051 TentativeParsingAction TPA(*this);
3052
3053 // Parse the C++ scope specifier.
3054 CXXScopeSpec SS;
John McCallb3d87482010-08-24 05:47:05 +00003055 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), true)) {
John McCall9ba61662010-02-26 08:45:28 +00003056 TPA.Revert();
3057 return false;
3058 }
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003059
3060 // Parse the constructor name.
3061 if (Tok.is(tok::identifier) || Tok.is(tok::annot_template_id)) {
3062 // We already know that we have a constructor name; just consume
3063 // the token.
3064 ConsumeToken();
3065 } else {
3066 TPA.Revert();
3067 return false;
3068 }
3069
3070 // Current class name must be followed by a left parentheses.
3071 if (Tok.isNot(tok::l_paren)) {
3072 TPA.Revert();
3073 return false;
3074 }
3075 ConsumeParen();
3076
3077 // A right parentheses or ellipsis signals that we have a constructor.
3078 if (Tok.is(tok::r_paren) || Tok.is(tok::ellipsis)) {
3079 TPA.Revert();
3080 return true;
3081 }
3082
3083 // If we need to, enter the specified scope.
3084 DeclaratorScopeObj DeclScopeObj(*this, SS);
Douglas Gregor23c94db2010-07-02 17:43:08 +00003085 if (SS.isSet() && Actions.ShouldEnterDeclaratorScope(getCurScope(), SS))
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003086 DeclScopeObj.EnterDeclaratorScope();
3087
Francois Pichetdfaa5fb2011-01-31 04:54:32 +00003088 // Optionally skip Microsoft attributes.
John McCall0b7e6782011-03-24 11:26:52 +00003089 ParsedAttributes Attrs(AttrFactory);
Francois Pichetdfaa5fb2011-01-31 04:54:32 +00003090 MaybeParseMicrosoftAttributes(Attrs);
3091
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003092 // Check whether the next token(s) are part of a declaration
3093 // specifier, in which case we have the start of a parameter and,
3094 // therefore, we know that this is a constructor.
3095 bool IsConstructor = isDeclarationSpecifier();
3096 TPA.Revert();
3097 return IsConstructor;
3098}
Reid Spencer5f016e22007-07-11 17:01:13 +00003099
3100/// ParseTypeQualifierListOpt
Dawn Perchik52fc3142010-09-03 01:29:35 +00003101/// type-qualifier-list: [C99 6.7.5]
3102/// type-qualifier
3103/// [vendor] attributes
3104/// [ only if VendorAttributesAllowed=true ]
3105/// type-qualifier-list type-qualifier
3106/// [vendor] type-qualifier-list attributes
3107/// [ only if VendorAttributesAllowed=true ]
3108/// [C++0x] attribute-specifier[opt] is allowed before cv-qualifier-seq
3109/// [ only if CXX0XAttributesAllowed=true ]
3110/// Note: vendor can be GNU, MS, etc.
Reid Spencer5f016e22007-07-11 17:01:13 +00003111///
Dawn Perchik52fc3142010-09-03 01:29:35 +00003112void Parser::ParseTypeQualifierListOpt(DeclSpec &DS,
3113 bool VendorAttributesAllowed,
Sean Huntbbd37c62009-11-21 08:43:09 +00003114 bool CXX0XAttributesAllowed) {
3115 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier()) {
3116 SourceLocation Loc = Tok.getLocation();
John McCall0b7e6782011-03-24 11:26:52 +00003117 ParsedAttributesWithRange attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00003118 ParseCXX0XAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00003119 if (CXX0XAttributesAllowed)
John McCall7f040a92010-12-24 02:08:15 +00003120 DS.takeAttributesFrom(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00003121 else
3122 Diag(Loc, diag::err_attributes_not_allowed);
3123 }
Abramo Bagnara796aa442011-03-12 11:17:06 +00003124
3125 SourceLocation EndLoc;
3126
Reid Spencer5f016e22007-07-11 17:01:13 +00003127 while (1) {
John McCallfec54012009-08-03 20:12:06 +00003128 bool isInvalid = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00003129 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00003130 unsigned DiagID = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00003131 SourceLocation Loc = Tok.getLocation();
3132
3133 switch (Tok.getKind()) {
Douglas Gregor1a480c42010-08-27 17:35:51 +00003134 case tok::code_completion:
3135 Actions.CodeCompleteTypeQualifiers(DS);
3136 ConsumeCodeCompletionToken();
3137 break;
3138
Reid Spencer5f016e22007-07-11 17:01:13 +00003139 case tok::kw_const:
John McCallfec54012009-08-03 20:12:06 +00003140 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, DiagID,
3141 getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00003142 break;
3143 case tok::kw_volatile:
John McCallfec54012009-08-03 20:12:06 +00003144 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
3145 getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00003146 break;
3147 case tok::kw_restrict:
John McCallfec54012009-08-03 20:12:06 +00003148 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
3149 getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00003150 break;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003151
3152 // OpenCL qualifiers:
3153 case tok::kw_private:
3154 if (!getLang().OpenCL)
3155 goto DoneWithTypeQuals;
3156 case tok::kw___private:
3157 case tok::kw___global:
3158 case tok::kw___local:
3159 case tok::kw___constant:
3160 case tok::kw___read_only:
3161 case tok::kw___write_only:
3162 case tok::kw___read_write:
3163 ParseOpenCLQualifiers(DS);
3164 break;
3165
Eli Friedman290eeb02009-06-08 23:27:34 +00003166 case tok::kw___w64:
Steve Naroff86bc6cf2008-12-25 14:41:26 +00003167 case tok::kw___ptr64:
Steve Naroff239f0732008-12-25 14:16:32 +00003168 case tok::kw___cdecl:
3169 case tok::kw___stdcall:
3170 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00003171 case tok::kw___thiscall:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003172 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00003173 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman290eeb02009-06-08 23:27:34 +00003174 continue;
3175 }
3176 goto DoneWithTypeQuals;
Dawn Perchik52fc3142010-09-03 01:29:35 +00003177 case tok::kw___pascal:
3178 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00003179 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik52fc3142010-09-03 01:29:35 +00003180 continue;
3181 }
3182 goto DoneWithTypeQuals;
Reid Spencer5f016e22007-07-11 17:01:13 +00003183 case tok::kw___attribute:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003184 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00003185 ParseGNUAttributes(DS.getAttributes());
Chris Lattner5a69d1c2008-12-18 07:02:59 +00003186 continue; // do *not* consume the next token!
3187 }
3188 // otherwise, FALL THROUGH!
3189 default:
Steve Naroff239f0732008-12-25 14:16:32 +00003190 DoneWithTypeQuals:
Chris Lattner5a69d1c2008-12-18 07:02:59 +00003191 // If this is not a type-qualifier token, we're done reading type
3192 // qualifiers. First verify that DeclSpec's are consistent.
Douglas Gregor9b3064b2009-04-01 22:41:11 +00003193 DS.Finish(Diags, PP);
Abramo Bagnara796aa442011-03-12 11:17:06 +00003194 if (EndLoc.isValid())
3195 DS.SetRangeEnd(EndLoc);
Chris Lattner5a69d1c2008-12-18 07:02:59 +00003196 return;
Reid Spencer5f016e22007-07-11 17:01:13 +00003197 }
Chris Lattnera1fcbad2008-12-18 06:50:14 +00003198
Reid Spencer5f016e22007-07-11 17:01:13 +00003199 // If the specifier combination wasn't legal, issue a diagnostic.
3200 if (isInvalid) {
3201 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner1ab3b962008-11-18 07:48:38 +00003202 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00003203 }
Abramo Bagnara796aa442011-03-12 11:17:06 +00003204 EndLoc = ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00003205 }
3206}
3207
3208
3209/// ParseDeclarator - Parse and verify a newly-initialized declarator.
3210///
3211void Parser::ParseDeclarator(Declarator &D) {
3212 /// This implements the 'declarator' production in the C grammar, then checks
3213 /// for well-formedness and issues diagnostics.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003214 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Reid Spencer5f016e22007-07-11 17:01:13 +00003215}
3216
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003217/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
3218/// is parsed by the function passed to it. Pass null, and the direct-declarator
3219/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003220/// ptr-operator production.
3221///
Sebastian Redlf30208a2009-01-24 21:16:55 +00003222/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
3223/// [C] pointer[opt] direct-declarator
3224/// [C++] direct-declarator
3225/// [C++] ptr-operator declarator
Reid Spencer5f016e22007-07-11 17:01:13 +00003226///
3227/// pointer: [C99 6.7.5]
3228/// '*' type-qualifier-list[opt]
3229/// '*' type-qualifier-list[opt] pointer
3230///
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003231/// ptr-operator:
3232/// '*' cv-qualifier-seq[opt]
3233/// '&'
Sebastian Redl05532f22009-03-15 22:02:01 +00003234/// [C++0x] '&&'
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003235/// [GNU] '&' restrict[opt] attributes[opt]
Sebastian Redl05532f22009-03-15 22:02:01 +00003236/// [GNU?] '&&' restrict[opt] attributes[opt]
Sebastian Redlf30208a2009-01-24 21:16:55 +00003237/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003238void Parser::ParseDeclaratorInternal(Declarator &D,
3239 DirectDeclParseFunction DirectDeclParser) {
Douglas Gregor91a28862009-08-26 14:27:30 +00003240 if (Diags.hasAllExtensionsSilenced())
3241 D.setExtension();
Douglas Gregor2ccccb32010-08-23 18:23:48 +00003242
Sebastian Redlf30208a2009-01-24 21:16:55 +00003243 // C++ member pointers start with a '::' or a nested-name.
3244 // Member pointers get special handling, since there's no place for the
3245 // scope spec in the generic path below.
Chris Lattnerf919bfe2009-03-24 17:04:48 +00003246 if (getLang().CPlusPlus &&
3247 (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) ||
3248 Tok.is(tok::annot_cxxscope))) {
Sebastian Redlf30208a2009-01-24 21:16:55 +00003249 CXXScopeSpec SS;
John McCallb3d87482010-08-24 05:47:05 +00003250 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), true); // ignore fail
John McCall9ba61662010-02-26 08:45:28 +00003251
Jeffrey Yasskinedc28772010-04-07 23:29:58 +00003252 if (SS.isNotEmpty()) {
Mike Stump1eb44332009-09-09 15:08:12 +00003253 if (Tok.isNot(tok::star)) {
Sebastian Redlf30208a2009-01-24 21:16:55 +00003254 // The scope spec really belongs to the direct-declarator.
3255 D.getCXXScopeSpec() = SS;
3256 if (DirectDeclParser)
3257 (this->*DirectDeclParser)(D);
3258 return;
3259 }
3260
3261 SourceLocation Loc = ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00003262 D.SetRangeEnd(Loc);
John McCall0b7e6782011-03-24 11:26:52 +00003263 DeclSpec DS(AttrFactory);
Sebastian Redlf30208a2009-01-24 21:16:55 +00003264 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00003265 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00003266
3267 // Recurse to parse whatever is left.
3268 ParseDeclaratorInternal(D, DirectDeclParser);
3269
3270 // Sema will have to catch (syntactically invalid) pointers into global
3271 // scope. It has to catch pointers into namespace scope anyway.
3272 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
John McCall0b7e6782011-03-24 11:26:52 +00003273 Loc),
3274 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003275 /* Don't replace range end. */SourceLocation());
Sebastian Redlf30208a2009-01-24 21:16:55 +00003276 return;
3277 }
3278 }
3279
3280 tok::TokenKind Kind = Tok.getKind();
Steve Naroff5618bd42008-08-27 16:04:49 +00003281 // Not a pointer, C++ reference, or block.
Chris Lattner9af55002009-03-27 04:18:06 +00003282 if (Kind != tok::star && Kind != tok::caret &&
Chris Lattnerf919bfe2009-03-24 17:04:48 +00003283 (Kind != tok::amp || !getLang().CPlusPlus) &&
Sebastian Redl743de1f2009-03-23 00:00:23 +00003284 // We parse rvalue refs in C++03, because otherwise the errors are scary.
Chris Lattner9af55002009-03-27 04:18:06 +00003285 (Kind != tok::ampamp || !getLang().CPlusPlus)) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003286 if (DirectDeclParser)
3287 (this->*DirectDeclParser)(D);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003288 return;
3289 }
Sebastian Redlf30208a2009-01-24 21:16:55 +00003290
Sebastian Redl05532f22009-03-15 22:02:01 +00003291 // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference,
3292 // '&&' -> rvalue reference
Sebastian Redl743de1f2009-03-23 00:00:23 +00003293 SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&.
Sebastian Redlab197ba2009-02-09 18:23:29 +00003294 D.SetRangeEnd(Loc);
Reid Spencer5f016e22007-07-11 17:01:13 +00003295
Chris Lattner9af55002009-03-27 04:18:06 +00003296 if (Kind == tok::star || Kind == tok::caret) {
Chris Lattner76549142008-02-21 01:32:26 +00003297 // Is a pointer.
John McCall0b7e6782011-03-24 11:26:52 +00003298 DeclSpec DS(AttrFactory);
Sebastian Redlf30208a2009-01-24 21:16:55 +00003299
Reid Spencer5f016e22007-07-11 17:01:13 +00003300 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00003301 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00003302
Reid Spencer5f016e22007-07-11 17:01:13 +00003303 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003304 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroff5618bd42008-08-27 16:04:49 +00003305 if (Kind == tok::star)
3306 // Remember that we parsed a pointer type, and remember the type-quals.
3307 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
Chandler Carruthd067c072011-02-23 18:51:59 +00003308 DS.getConstSpecLoc(),
3309 DS.getVolatileSpecLoc(),
John McCall0b7e6782011-03-24 11:26:52 +00003310 DS.getRestrictSpecLoc()),
3311 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003312 SourceLocation());
Steve Naroff5618bd42008-08-27 16:04:49 +00003313 else
3314 // Remember that we parsed a Block type, and remember the type-quals.
Mike Stump1eb44332009-09-09 15:08:12 +00003315 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
John McCall0b7e6782011-03-24 11:26:52 +00003316 Loc),
3317 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003318 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00003319 } else {
3320 // Is a reference
John McCall0b7e6782011-03-24 11:26:52 +00003321 DeclSpec DS(AttrFactory);
Reid Spencer5f016e22007-07-11 17:01:13 +00003322
Sebastian Redl743de1f2009-03-23 00:00:23 +00003323 // Complain about rvalue references in C++03, but then go on and build
3324 // the declarator.
3325 if (Kind == tok::ampamp && !getLang().CPlusPlus0x)
Douglas Gregor16cf8f52011-01-25 02:17:32 +00003326 Diag(Loc, diag::ext_rvalue_reference);
Sebastian Redl743de1f2009-03-23 00:00:23 +00003327
Reid Spencer5f016e22007-07-11 17:01:13 +00003328 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
3329 // cv-qualifiers are introduced through the use of a typedef or of a
3330 // template type argument, in which case the cv-qualifiers are ignored.
3331 //
3332 // [GNU] Retricted references are allowed.
3333 // [GNU] Attributes on references are allowed.
Sean Huntbbd37c62009-11-21 08:43:09 +00003334 // [C++0x] Attributes on references are not allowed.
3335 ParseTypeQualifierListOpt(DS, true, false);
Sebastian Redlab197ba2009-02-09 18:23:29 +00003336 D.ExtendWithDeclSpec(DS);
Reid Spencer5f016e22007-07-11 17:01:13 +00003337
3338 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
3339 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
3340 Diag(DS.getConstSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00003341 diag::err_invalid_reference_qualifier_application) << "const";
Reid Spencer5f016e22007-07-11 17:01:13 +00003342 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
3343 Diag(DS.getVolatileSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00003344 diag::err_invalid_reference_qualifier_application) << "volatile";
Reid Spencer5f016e22007-07-11 17:01:13 +00003345 }
3346
3347 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003348 ParseDeclaratorInternal(D, DirectDeclParser);
Reid Spencer5f016e22007-07-11 17:01:13 +00003349
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00003350 if (D.getNumTypeObjects() > 0) {
3351 // C++ [dcl.ref]p4: There shall be no references to references.
3352 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
3353 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattnerda83bac2008-11-19 07:37:42 +00003354 if (const IdentifierInfo *II = D.getIdentifier())
3355 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
3356 << II;
3357 else
3358 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
3359 << "type name";
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00003360
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003361 // Once we've complained about the reference-to-reference, we
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00003362 // can go ahead and build the (technically ill-formed)
3363 // declarator: reference collapsing will take care of it.
3364 }
3365 }
3366
Reid Spencer5f016e22007-07-11 17:01:13 +00003367 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner76549142008-02-21 01:32:26 +00003368 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
Sebastian Redl05532f22009-03-15 22:02:01 +00003369 Kind == tok::amp),
John McCall0b7e6782011-03-24 11:26:52 +00003370 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003371 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00003372 }
3373}
3374
3375/// ParseDirectDeclarator
3376/// direct-declarator: [C99 6.7.5]
Douglas Gregor42a552f2008-11-05 20:51:48 +00003377/// [C99] identifier
Reid Spencer5f016e22007-07-11 17:01:13 +00003378/// '(' declarator ')'
3379/// [GNU] '(' attributes declarator ')'
3380/// [C90] direct-declarator '[' constant-expression[opt] ']'
3381/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
3382/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
3383/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
3384/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
3385/// direct-declarator '(' parameter-type-list ')'
3386/// direct-declarator '(' identifier-list[opt] ')'
3387/// [GNU] direct-declarator '(' parameter-forward-declarations
3388/// parameter-type-list[opt] ')'
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003389/// [C++] direct-declarator '(' parameter-declaration-clause ')'
3390/// cv-qualifier-seq[opt] exception-specification[opt]
Douglas Gregorb48fe382008-10-31 09:07:45 +00003391/// [C++] declarator-id
Douglas Gregor42a552f2008-11-05 20:51:48 +00003392///
3393/// declarator-id: [C++ 8]
Douglas Gregora8bc8c92010-12-23 22:44:42 +00003394/// '...'[opt] id-expression
Douglas Gregor42a552f2008-11-05 20:51:48 +00003395/// '::'[opt] nested-name-specifier[opt] type-name
3396///
3397/// id-expression: [C++ 5.1]
3398/// unqualified-id
Douglas Gregordb422df2009-09-25 21:45:23 +00003399/// qualified-id
Douglas Gregor42a552f2008-11-05 20:51:48 +00003400///
3401/// unqualified-id: [C++ 5.1]
Mike Stump1eb44332009-09-09 15:08:12 +00003402/// identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003403/// operator-function-id
Douglas Gregordb422df2009-09-25 21:45:23 +00003404/// conversion-function-id
Mike Stump1eb44332009-09-09 15:08:12 +00003405/// '~' class-name
Douglas Gregor39a8de12009-02-25 19:37:18 +00003406/// template-id
Argyrios Kyrtzidisc7ed9c62008-11-07 22:02:30 +00003407///
Reid Spencer5f016e22007-07-11 17:01:13 +00003408void Parser::ParseDirectDeclarator(Declarator &D) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00003409 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003410
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003411 if (getLang().CPlusPlus && D.mayHaveIdentifier()) {
3412 // ParseDeclaratorInternal might already have parsed the scope.
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00003413 if (D.getCXXScopeSpec().isEmpty()) {
John McCallb3d87482010-08-24 05:47:05 +00003414 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec(), ParsedType(), true);
John McCall9ba61662010-02-26 08:45:28 +00003415 }
3416
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00003417 if (D.getCXXScopeSpec().isValid()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00003418 if (Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
John McCalle7e278b2009-12-11 20:04:54 +00003419 // Change the declaration context for name lookup, until this function
3420 // is exited (and the declarator has been parsed).
3421 DeclScopeObj.EnterDeclaratorScope();
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00003422 }
3423
Douglas Gregora8bc8c92010-12-23 22:44:42 +00003424 // C++0x [dcl.fct]p14:
3425 // There is a syntactic ambiguity when an ellipsis occurs at the end
3426 // of a parameter-declaration-clause without a preceding comma. In
3427 // this case, the ellipsis is parsed as part of the
3428 // abstract-declarator if the type of the parameter names a template
3429 // parameter pack that has not been expanded; otherwise, it is parsed
3430 // as part of the parameter-declaration-clause.
3431 if (Tok.is(tok::ellipsis) &&
3432 !((D.getContext() == Declarator::PrototypeContext ||
3433 D.getContext() == Declarator::BlockLiteralContext) &&
Douglas Gregora8bc8c92010-12-23 22:44:42 +00003434 NextToken().is(tok::r_paren) &&
3435 !Actions.containsUnexpandedParameterPacks(D)))
3436 D.setEllipsisLoc(ConsumeToken());
3437
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003438 if (Tok.is(tok::identifier) || Tok.is(tok::kw_operator) ||
3439 Tok.is(tok::annot_template_id) || Tok.is(tok::tilde)) {
3440 // We found something that indicates the start of an unqualified-id.
3441 // Parse that unqualified-id.
John McCallba9d8532010-04-13 06:39:49 +00003442 bool AllowConstructorName;
3443 if (D.getDeclSpec().hasTypeSpecifier())
3444 AllowConstructorName = false;
3445 else if (D.getCXXScopeSpec().isSet())
3446 AllowConstructorName =
3447 (D.getContext() == Declarator::FileContext ||
3448 (D.getContext() == Declarator::MemberContext &&
3449 D.getDeclSpec().isFriendSpecified()));
3450 else
3451 AllowConstructorName = (D.getContext() == Declarator::MemberContext);
3452
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003453 if (ParseUnqualifiedId(D.getCXXScopeSpec(),
3454 /*EnteringContext=*/true,
3455 /*AllowDestructorName=*/true,
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003456 AllowConstructorName,
John McCallb3d87482010-08-24 05:47:05 +00003457 ParsedType(),
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00003458 D.getName()) ||
3459 // Once we're past the identifier, if the scope was bad, mark the
3460 // whole declarator bad.
3461 D.getCXXScopeSpec().isInvalid()) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00003462 D.SetIdentifier(0, Tok.getLocation());
3463 D.setInvalidType(true);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003464 } else {
3465 // Parsed the unqualified-id; update range information and move along.
3466 if (D.getSourceRange().getBegin().isInvalid())
3467 D.SetRangeBegin(D.getName().getSourceRange().getBegin());
3468 D.SetRangeEnd(D.getName().getSourceRange().getEnd());
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003469 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003470 goto PastIdentifier;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00003471 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003472 } else if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00003473 assert(!getLang().CPlusPlus &&
3474 "There's a C++-specific check for tok::identifier above");
3475 assert(Tok.getIdentifierInfo() && "Not an identifier?");
3476 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
3477 ConsumeToken();
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003478 goto PastIdentifier;
3479 }
3480
3481 if (Tok.is(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003482 // direct-declarator: '(' declarator ')'
3483 // direct-declarator: '(' attributes declarator ')'
3484 // Example: 'char (*X)' or 'int (*XX)(void)'
3485 ParseParenDeclarator(D);
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003486
3487 // If the declarator was parenthesized, we entered the declarator
3488 // scope when parsing the parenthesized declarator, then exited
3489 // the scope already. Re-enter the scope, if we need to.
3490 if (D.getCXXScopeSpec().isSet()) {
Fariborz Jahanian46877cd2010-08-17 23:50:37 +00003491 // If there was an error parsing parenthesized declarator, declarator
3492 // scope may have been enterred before. Don't do it again.
3493 if (!D.isInvalidType() &&
3494 Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003495 // Change the declaration context for name lookup, until this function
3496 // is exited (and the declarator has been parsed).
Fariborz Jahanian46877cd2010-08-17 23:50:37 +00003497 DeclScopeObj.EnterDeclaratorScope();
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003498 }
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00003499 } else if (D.mayOmitIdentifier()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003500 // This could be something simple like "int" (in which case the declarator
3501 // portion is empty), if an abstract-declarator is allowed.
3502 D.SetIdentifier(0, Tok.getLocation());
3503 } else {
Douglas Gregore950d4b2009-03-06 23:28:18 +00003504 if (D.getContext() == Declarator::MemberContext)
3505 Diag(Tok, diag::err_expected_member_name_or_semi)
3506 << D.getDeclSpec().getSourceRange();
3507 else if (getLang().CPlusPlus)
Douglas Gregor2d1c2142009-11-03 19:44:04 +00003508 Diag(Tok, diag::err_expected_unqualified_id) << getLang().CPlusPlus;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003509 else
Chris Lattner1ab3b962008-11-18 07:48:38 +00003510 Diag(Tok, diag::err_expected_ident_lparen);
Reid Spencer5f016e22007-07-11 17:01:13 +00003511 D.SetIdentifier(0, Tok.getLocation());
Chris Lattner1f6f54b2008-11-11 06:13:16 +00003512 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00003513 }
Mike Stump1eb44332009-09-09 15:08:12 +00003514
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00003515 PastIdentifier:
Reid Spencer5f016e22007-07-11 17:01:13 +00003516 assert(D.isPastIdentifier() &&
3517 "Haven't past the location of the identifier yet?");
Mike Stump1eb44332009-09-09 15:08:12 +00003518
Sean Huntbbd37c62009-11-21 08:43:09 +00003519 // Don't parse attributes unless we have an identifier.
John McCall7f040a92010-12-24 02:08:15 +00003520 if (D.getIdentifier())
3521 MaybeParseCXX0XAttributes(D);
Sean Huntbbd37c62009-11-21 08:43:09 +00003522
Reid Spencer5f016e22007-07-11 17:01:13 +00003523 while (1) {
Chris Lattner04d66662007-10-09 17:33:22 +00003524 if (Tok.is(tok::l_paren)) {
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00003525 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
3526 // In such a case, check if we actually have a function declarator; if it
3527 // is not, the declarator has been fully parsed.
Chris Lattner7399ee02008-10-20 02:05:46 +00003528 if (getLang().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
3529 // When not in file scope, warn for ambiguous function declarators, just
3530 // in case the author intended it as a variable definition.
3531 bool warnIfAmbiguous = D.getContext() != Declarator::FileContext;
3532 if (!isCXXFunctionDeclarator(warnIfAmbiguous))
3533 break;
3534 }
John McCall0b7e6782011-03-24 11:26:52 +00003535 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00003536 ParseFunctionDeclarator(ConsumeParen(), D, attrs);
Chris Lattner04d66662007-10-09 17:33:22 +00003537 } else if (Tok.is(tok::l_square)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003538 ParseBracketDeclarator(D);
3539 } else {
3540 break;
3541 }
3542 }
3543}
3544
Chris Lattneref4715c2008-04-06 05:45:57 +00003545/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
3546/// only called before the identifier, so these are most likely just grouping
Mike Stump1eb44332009-09-09 15:08:12 +00003547/// parens for precedence. If we find that these are actually function
Chris Lattneref4715c2008-04-06 05:45:57 +00003548/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
3549///
3550/// direct-declarator:
3551/// '(' declarator ')'
3552/// [GNU] '(' attributes declarator ')'
Chris Lattner7399ee02008-10-20 02:05:46 +00003553/// direct-declarator '(' parameter-type-list ')'
3554/// direct-declarator '(' identifier-list[opt] ')'
3555/// [GNU] direct-declarator '(' parameter-forward-declarations
3556/// parameter-type-list[opt] ')'
Chris Lattneref4715c2008-04-06 05:45:57 +00003557///
3558void Parser::ParseParenDeclarator(Declarator &D) {
3559 SourceLocation StartLoc = ConsumeParen();
3560 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
Mike Stump1eb44332009-09-09 15:08:12 +00003561
Chris Lattner7399ee02008-10-20 02:05:46 +00003562 // Eat any attributes before we look at whether this is a grouping or function
3563 // declarator paren. If this is a grouping paren, the attribute applies to
3564 // the type being built up, for example:
3565 // int (__attribute__(()) *x)(long y)
3566 // If this ends up not being a grouping paren, the attribute applies to the
3567 // first argument, for example:
3568 // int (__attribute__(()) int x)
3569 // In either case, we need to eat any attributes to be able to determine what
3570 // sort of paren this is.
3571 //
John McCall0b7e6782011-03-24 11:26:52 +00003572 ParsedAttributes attrs(AttrFactory);
Chris Lattner7399ee02008-10-20 02:05:46 +00003573 bool RequiresArg = false;
3574 if (Tok.is(tok::kw___attribute)) {
John McCall7f040a92010-12-24 02:08:15 +00003575 ParseGNUAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00003576
Chris Lattner7399ee02008-10-20 02:05:46 +00003577 // We require that the argument list (if this is a non-grouping paren) be
3578 // present even if the attribute list was empty.
3579 RequiresArg = true;
3580 }
Steve Naroff239f0732008-12-25 14:16:32 +00003581 // Eat any Microsoft extensions.
Eli Friedman290eeb02009-06-08 23:27:34 +00003582 if (Tok.is(tok::kw___cdecl) || Tok.is(tok::kw___stdcall) ||
Douglas Gregorf813a2c2010-05-18 16:57:00 +00003583 Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___fastcall) ||
3584 Tok.is(tok::kw___w64) || Tok.is(tok::kw___ptr64)) {
John McCall7f040a92010-12-24 02:08:15 +00003585 ParseMicrosoftTypeAttributes(attrs);
Eli Friedman290eeb02009-06-08 23:27:34 +00003586 }
Dawn Perchik52fc3142010-09-03 01:29:35 +00003587 // Eat any Borland extensions.
Ted Kremenek8113ecf2010-11-10 05:59:39 +00003588 if (Tok.is(tok::kw___pascal))
John McCall7f040a92010-12-24 02:08:15 +00003589 ParseBorlandTypeAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00003590
Chris Lattneref4715c2008-04-06 05:45:57 +00003591 // If we haven't past the identifier yet (or where the identifier would be
3592 // stored, if this is an abstract declarator), then this is probably just
3593 // grouping parens. However, if this could be an abstract-declarator, then
3594 // this could also be the start of function arguments (consider 'void()').
3595 bool isGrouping;
Mike Stump1eb44332009-09-09 15:08:12 +00003596
Chris Lattneref4715c2008-04-06 05:45:57 +00003597 if (!D.mayOmitIdentifier()) {
3598 // If this can't be an abstract-declarator, this *must* be a grouping
3599 // paren, because we haven't seen the identifier yet.
3600 isGrouping = true;
3601 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Argyrios Kyrtzidise25d2702008-10-06 00:07:55 +00003602 (getLang().CPlusPlus && Tok.is(tok::ellipsis)) || // C++ int(...)
Chris Lattneref4715c2008-04-06 05:45:57 +00003603 isDeclarationSpecifier()) { // 'int(int)' is a function.
3604 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
3605 // considered to be a type, not a K&R identifier-list.
3606 isGrouping = false;
3607 } else {
3608 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
3609 isGrouping = true;
3610 }
Mike Stump1eb44332009-09-09 15:08:12 +00003611
Chris Lattneref4715c2008-04-06 05:45:57 +00003612 // If this is a grouping paren, handle:
3613 // direct-declarator: '(' declarator ')'
3614 // direct-declarator: '(' attributes declarator ')'
3615 if (isGrouping) {
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00003616 bool hadGroupingParens = D.hasGroupingParens();
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00003617 D.setGroupingParens(true);
3618
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003619 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattneref4715c2008-04-06 05:45:57 +00003620 // Match the ')'.
Abramo Bagnara075f8f12010-12-10 16:29:40 +00003621 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_paren, StartLoc);
John McCall0b7e6782011-03-24 11:26:52 +00003622 D.AddTypeInfo(DeclaratorChunk::getParen(StartLoc, EndLoc),
3623 attrs, EndLoc);
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00003624
3625 D.setGroupingParens(hadGroupingParens);
Chris Lattneref4715c2008-04-06 05:45:57 +00003626 return;
3627 }
Mike Stump1eb44332009-09-09 15:08:12 +00003628
Chris Lattneref4715c2008-04-06 05:45:57 +00003629 // Okay, if this wasn't a grouping paren, it must be the start of a function
3630 // argument list. Recognize that this declarator will never have an
Chris Lattner7399ee02008-10-20 02:05:46 +00003631 // identifier (and remember where it would have been), then call into
3632 // ParseFunctionDeclarator to handle of argument list.
Chris Lattneref4715c2008-04-06 05:45:57 +00003633 D.SetIdentifier(0, Tok.getLocation());
3634
John McCall7f040a92010-12-24 02:08:15 +00003635 ParseFunctionDeclarator(StartLoc, D, attrs, RequiresArg);
Chris Lattneref4715c2008-04-06 05:45:57 +00003636}
3637
3638/// ParseFunctionDeclarator - We are after the identifier and have parsed the
3639/// declarator D up to a paren, which indicates that we are parsing function
3640/// arguments.
Reid Spencer5f016e22007-07-11 17:01:13 +00003641///
Chris Lattner7399ee02008-10-20 02:05:46 +00003642/// If AttrList is non-null, then the caller parsed those arguments immediately
3643/// after the open paren - they should be considered to be the first argument of
3644/// a parameter. If RequiresArg is true, then the first argument of the
3645/// function is required to be present and required to not be an identifier
3646/// list.
3647///
Reid Spencer5f016e22007-07-11 17:01:13 +00003648/// This method also handles this portion of the grammar:
3649/// parameter-type-list: [C99 6.7.5]
3650/// parameter-list
3651/// parameter-list ',' '...'
Douglas Gregored5d6512009-09-22 21:41:40 +00003652/// [C++] parameter-list '...'
Reid Spencer5f016e22007-07-11 17:01:13 +00003653///
3654/// parameter-list: [C99 6.7.5]
3655/// parameter-declaration
3656/// parameter-list ',' parameter-declaration
3657///
3658/// parameter-declaration: [C99 6.7.5]
3659/// declaration-specifiers declarator
Chris Lattner04421082008-04-08 04:40:51 +00003660/// [C++] declaration-specifiers declarator '=' assignment-expression
Reid Spencer5f016e22007-07-11 17:01:13 +00003661/// [GNU] declaration-specifiers declarator attributes
Sebastian Redl50de12f2009-03-24 22:27:57 +00003662/// declaration-specifiers abstract-declarator[opt]
3663/// [C++] declaration-specifiers abstract-declarator[opt]
Chris Lattner8123a952008-04-10 02:22:51 +00003664/// '=' assignment-expression
Reid Spencer5f016e22007-07-11 17:01:13 +00003665/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
3666///
Douglas Gregor83f51722011-01-26 03:43:54 +00003667/// For C++, after the parameter-list, it also parses "cv-qualifier-seq[opt]",
3668/// C++0x "ref-qualifier[opt]" and "exception-specification[opt]".
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003669///
Sebastian Redl7acafd02011-03-05 14:45:16 +00003670/// [C++0x] exception-specification:
3671/// dynamic-exception-specification
3672/// noexcept-specification
3673///
Chris Lattner7399ee02008-10-20 02:05:46 +00003674void Parser::ParseFunctionDeclarator(SourceLocation LParenLoc, Declarator &D,
John McCall7f040a92010-12-24 02:08:15 +00003675 ParsedAttributes &attrs,
Chris Lattner7399ee02008-10-20 02:05:46 +00003676 bool RequiresArg) {
Chris Lattneref4715c2008-04-06 05:45:57 +00003677 // lparen is already consumed!
3678 assert(D.isPastIdentifier() && "Should not call before identifier!");
Mike Stump1eb44332009-09-09 15:08:12 +00003679
Douglas Gregordab60ad2010-10-01 18:44:50 +00003680 ParsedType TrailingReturnType;
3681
Chris Lattner7399ee02008-10-20 02:05:46 +00003682 // This parameter list may be empty.
Chris Lattner04d66662007-10-09 17:33:22 +00003683 if (Tok.is(tok::r_paren)) {
Ted Kremenek8113ecf2010-11-10 05:59:39 +00003684 if (RequiresArg)
Chris Lattner1ab3b962008-11-18 07:48:38 +00003685 Diag(Tok, diag::err_argument_required_after_attribute);
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003686
Abramo Bagnara796aa442011-03-12 11:17:06 +00003687 SourceLocation EndLoc = ConsumeParen(); // Eat the closing ')'.
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003688
3689 // cv-qualifier-seq[opt].
John McCall0b7e6782011-03-24 11:26:52 +00003690 DeclSpec DS(AttrFactory);
Douglas Gregor83f51722011-01-26 03:43:54 +00003691 SourceLocation RefQualifierLoc;
3692 bool RefQualifierIsLValueRef = true;
Sebastian Redl7acafd02011-03-05 14:45:16 +00003693 ExceptionSpecificationType ESpecType = EST_None;
3694 SourceRange ESpecRange;
3695 llvm::SmallVector<ParsedType, 2> DynamicExceptions;
3696 llvm::SmallVector<SourceRange, 2> DynamicExceptionRanges;
3697 ExprResult NoexceptExpr;
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003698 if (getLang().CPlusPlus) {
John McCall7f040a92010-12-24 02:08:15 +00003699 MaybeParseCXX0XAttributes(attrs);
3700
Chris Lattner5a69d1c2008-12-18 07:02:59 +00003701 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Sebastian Redlab197ba2009-02-09 18:23:29 +00003702 if (!DS.getSourceRange().getEnd().isInvalid())
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00003703 EndLoc = DS.getSourceRange().getEnd();
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00003704
Douglas Gregor83f51722011-01-26 03:43:54 +00003705 // Parse ref-qualifier[opt]
3706 if (Tok.is(tok::amp) || Tok.is(tok::ampamp)) {
3707 if (!getLang().CPlusPlus0x)
Douglas Gregor1f381062011-01-26 20:35:32 +00003708 Diag(Tok, diag::ext_ref_qualifier);
Abramo Bagnara796aa442011-03-12 11:17:06 +00003709
Douglas Gregor83f51722011-01-26 03:43:54 +00003710 RefQualifierIsLValueRef = Tok.is(tok::amp);
3711 RefQualifierLoc = ConsumeToken();
3712 EndLoc = RefQualifierLoc;
3713 }
Sebastian Redl7acafd02011-03-05 14:45:16 +00003714
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00003715 // Parse exception-specification[opt].
Sebastian Redl7acafd02011-03-05 14:45:16 +00003716 ESpecType = MaybeParseExceptionSpecification(ESpecRange,
3717 DynamicExceptions,
3718 DynamicExceptionRanges,
3719 NoexceptExpr);
3720 if (ESpecType != EST_None)
3721 EndLoc = ESpecRange.getEnd();
Douglas Gregordab60ad2010-10-01 18:44:50 +00003722
3723 // Parse trailing-return-type.
3724 if (getLang().CPlusPlus0x && Tok.is(tok::arrow)) {
3725 TrailingReturnType = ParseTrailingReturnType().get();
3726 }
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003727 }
3728
Chris Lattnerf97409f2008-04-06 06:57:35 +00003729 // Remember that we parsed a function type, and remember the attributes.
Reid Spencer5f016e22007-07-11 17:01:13 +00003730 // int() -> no prototype, no '...'.
John McCall0b7e6782011-03-24 11:26:52 +00003731 D.AddTypeInfo(DeclaratorChunk::getFunction(/*prototype*/getLang().CPlusPlus,
Chris Lattnerf97409f2008-04-06 06:57:35 +00003732 /*variadic*/ false,
Douglas Gregor965acbb2009-02-18 07:07:28 +00003733 SourceLocation(),
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003734 /*arglist*/ 0, 0,
3735 DS.getTypeQualifiers(),
Douglas Gregor83f51722011-01-26 03:43:54 +00003736 RefQualifierIsLValueRef,
3737 RefQualifierLoc,
Sebastian Redl6e5d3192011-03-05 22:42:13 +00003738 ESpecType, ESpecRange.getBegin(),
Sebastian Redl7acafd02011-03-05 14:45:16 +00003739 DynamicExceptions.data(),
3740 DynamicExceptionRanges.data(),
3741 DynamicExceptions.size(),
Sebastian Redl6e5d3192011-03-05 22:42:13 +00003742 NoexceptExpr.isUsable() ?
3743 NoexceptExpr.get() : 0,
Abramo Bagnara796aa442011-03-12 11:17:06 +00003744 LParenLoc, EndLoc, D,
Douglas Gregordab60ad2010-10-01 18:44:50 +00003745 TrailingReturnType),
John McCall0b7e6782011-03-24 11:26:52 +00003746 attrs, EndLoc);
Chris Lattnerf97409f2008-04-06 06:57:35 +00003747 return;
Sebastian Redlef65f062009-05-29 18:02:33 +00003748 }
3749
Chris Lattner7399ee02008-10-20 02:05:46 +00003750 // Alternatively, this parameter list may be an identifier list form for a
3751 // K&R-style function: void foo(a,b,c)
John Thompson82287d12010-02-05 00:12:22 +00003752 if (!getLang().CPlusPlus && Tok.is(tok::identifier)
3753 && !TryAltiVecVectorToken()) {
John McCall9ba61662010-02-26 08:45:28 +00003754 if (TryAnnotateTypeOrScopeToken() || !Tok.is(tok::annot_typename)) {
Chris Lattner7399ee02008-10-20 02:05:46 +00003755 // K&R identifier lists can't have typedefs as identifiers, per
3756 // C99 6.7.5.3p11.
Ted Kremenek8113ecf2010-11-10 05:59:39 +00003757 if (RequiresArg)
Steve Naroff2d081c42009-01-28 19:16:40 +00003758 Diag(Tok, diag::err_argument_required_after_attribute);
Chris Lattner83a94472010-05-14 17:23:36 +00003759
Steve Naroff2d081c42009-01-28 19:16:40 +00003760 // Identifier list. Note that '(' identifier-list ')' is only allowed for
Chris Lattner83a94472010-05-14 17:23:36 +00003761 // normal declarators, not for abstract-declarators. Get the first
3762 // identifier.
Chris Lattner9a65b812010-05-14 17:44:56 +00003763 Token FirstTok = Tok;
Chris Lattner83a94472010-05-14 17:23:36 +00003764 ConsumeToken(); // eat the first identifier.
Chris Lattner9a65b812010-05-14 17:44:56 +00003765
3766 // Identifier lists follow a really simple grammar: the identifiers can
3767 // be followed *only* by a ", moreidentifiers" or ")". However, K&R
3768 // identifier lists are really rare in the brave new modern world, and it
3769 // is very common for someone to typo a type in a non-k&r style list. If
3770 // we are presented with something like: "void foo(intptr x, float y)",
3771 // we don't want to start parsing the function declarator as though it is
3772 // a K&R style declarator just because intptr is an invalid type.
3773 //
3774 // To handle this, we check to see if the token after the first identifier
3775 // is a "," or ")". Only if so, do we parse it as an identifier list.
3776 if (Tok.is(tok::comma) || Tok.is(tok::r_paren))
3777 return ParseFunctionDeclaratorIdentifierList(LParenLoc,
3778 FirstTok.getIdentifierInfo(),
3779 FirstTok.getLocation(), D);
3780
3781 // If we get here, the code is invalid. Push the first identifier back
3782 // into the token stream and parse the first argument as an (invalid)
3783 // normal argument declarator.
3784 PP.EnterToken(Tok);
3785 Tok = FirstTok;
Chris Lattner7399ee02008-10-20 02:05:46 +00003786 }
Chris Lattnerf97409f2008-04-06 06:57:35 +00003787 }
Mike Stump1eb44332009-09-09 15:08:12 +00003788
Chris Lattnerf97409f2008-04-06 06:57:35 +00003789 // Finally, a normal, non-empty parameter type list.
Mike Stump1eb44332009-09-09 15:08:12 +00003790
Chris Lattnerf97409f2008-04-06 06:57:35 +00003791 // Build up an array of information about the parsed arguments.
3792 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Chris Lattner04421082008-04-08 04:40:51 +00003793
3794 // Enter function-declaration scope, limiting any declarators to the
3795 // function prototype scope, including parameter declarators.
Chris Lattnerae50fa02009-03-05 00:00:31 +00003796 ParseScope PrototypeScope(this,
3797 Scope::FunctionPrototypeScope|Scope::DeclScope);
Mike Stump1eb44332009-09-09 15:08:12 +00003798
Chris Lattnerf97409f2008-04-06 06:57:35 +00003799 bool IsVariadic = false;
Douglas Gregor965acbb2009-02-18 07:07:28 +00003800 SourceLocation EllipsisLoc;
Chris Lattnerf97409f2008-04-06 06:57:35 +00003801 while (1) {
3802 if (Tok.is(tok::ellipsis)) {
3803 IsVariadic = true;
Douglas Gregor965acbb2009-02-18 07:07:28 +00003804 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chris Lattnerf97409f2008-04-06 06:57:35 +00003805 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00003806 }
Mike Stump1eb44332009-09-09 15:08:12 +00003807
Chris Lattnerf97409f2008-04-06 06:57:35 +00003808 // Parse the declaration-specifiers.
John McCall54abf7d2009-11-04 02:18:39 +00003809 // Just use the ParsingDeclaration "scope" of the declarator.
John McCall0b7e6782011-03-24 11:26:52 +00003810 DeclSpec DS(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00003811
3812 // Skip any Microsoft attributes before a param.
3813 if (getLang().Microsoft && Tok.is(tok::l_square))
3814 ParseMicrosoftAttributes(DS.getAttributes());
3815
3816 SourceLocation DSStart = Tok.getLocation();
Chris Lattner7399ee02008-10-20 02:05:46 +00003817
3818 // If the caller parsed attributes for the first argument, add them now.
John McCall7f040a92010-12-24 02:08:15 +00003819 // Take them so that we only apply the attributes to the first parameter.
3820 DS.takeAttributesFrom(attrs);
3821
Chris Lattnere64c5492009-02-27 18:38:20 +00003822 ParseDeclarationSpecifiers(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00003823
Chris Lattnerf97409f2008-04-06 06:57:35 +00003824 // Parse the declarator. This is "PrototypeContext", because we must
3825 // accept either 'declarator' or 'abstract-declarator' here.
3826 Declarator ParmDecl(DS, Declarator::PrototypeContext);
3827 ParseDeclarator(ParmDecl);
3828
3829 // Parse GNU attributes, if present.
John McCall7f040a92010-12-24 02:08:15 +00003830 MaybeParseGNUAttributes(ParmDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00003831
Chris Lattnerf97409f2008-04-06 06:57:35 +00003832 // Remember this parsed parameter in ParamInfo.
3833 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
Mike Stump1eb44332009-09-09 15:08:12 +00003834
Douglas Gregor72b505b2008-12-16 21:30:33 +00003835 // DefArgToks is used when the parsing of default arguments needs
3836 // to be delayed.
3837 CachedTokens *DefArgToks = 0;
3838
Chris Lattnerf97409f2008-04-06 06:57:35 +00003839 // If no parameter was specified, verify that *something* was specified,
3840 // otherwise we have a missing type and identifier.
Chris Lattnere64c5492009-02-27 18:38:20 +00003841 if (DS.isEmpty() && ParmDecl.getIdentifier() == 0 &&
3842 ParmDecl.getNumTypeObjects() == 0) {
Chris Lattnerf97409f2008-04-06 06:57:35 +00003843 // Completely missing, emit error.
3844 Diag(DSStart, diag::err_missing_param);
3845 } else {
3846 // Otherwise, we have something. Add it and let semantic analysis try
3847 // to grok it and add the result to the ParamInfo we are building.
Mike Stump1eb44332009-09-09 15:08:12 +00003848
Chris Lattnerf97409f2008-04-06 06:57:35 +00003849 // Inform the actions module about the parameter declarator, so it gets
3850 // added to the current scope.
John McCalld226f652010-08-21 09:40:31 +00003851 Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl);
Chris Lattner04421082008-04-08 04:40:51 +00003852
3853 // Parse the default argument, if any. We parse the default
3854 // arguments in all dialects; the semantic analysis in
3855 // ActOnParamDefaultArgument will reject the default argument in
3856 // C.
3857 if (Tok.is(tok::equal)) {
Douglas Gregor61366e92008-12-24 00:01:03 +00003858 SourceLocation EqualLoc = Tok.getLocation();
3859
Chris Lattner04421082008-04-08 04:40:51 +00003860 // Parse the default argument
Douglas Gregor72b505b2008-12-16 21:30:33 +00003861 if (D.getContext() == Declarator::MemberContext) {
3862 // If we're inside a class definition, cache the tokens
3863 // corresponding to the default argument. We'll actually parse
3864 // them when we see the end of the class definition.
3865 // FIXME: Templates will require something similar.
3866 // FIXME: Can we use a smart pointer for Toks?
3867 DefArgToks = new CachedTokens;
3868
Mike Stump1eb44332009-09-09 15:08:12 +00003869 if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks,
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +00003870 /*StopAtSemi=*/true,
3871 /*ConsumeFinalToken=*/false)) {
Douglas Gregor72b505b2008-12-16 21:30:33 +00003872 delete DefArgToks;
3873 DefArgToks = 0;
Douglas Gregor61366e92008-12-24 00:01:03 +00003874 Actions.ActOnParamDefaultArgumentError(Param);
Argyrios Kyrtzidis2b602ad2010-08-06 09:47:24 +00003875 } else {
3876 // Mark the end of the default argument so that we know when to
3877 // stop when we parse it later on.
3878 Token DefArgEnd;
3879 DefArgEnd.startToken();
3880 DefArgEnd.setKind(tok::cxx_defaultarg_end);
3881 DefArgEnd.setLocation(Tok.getLocation());
3882 DefArgToks->push_back(DefArgEnd);
Mike Stump1eb44332009-09-09 15:08:12 +00003883 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc,
Anders Carlsson5e300d12009-06-12 16:51:40 +00003884 (*DefArgToks)[1].getLocation());
Argyrios Kyrtzidis2b602ad2010-08-06 09:47:24 +00003885 }
Chris Lattner04421082008-04-08 04:40:51 +00003886 } else {
Douglas Gregor72b505b2008-12-16 21:30:33 +00003887 // Consume the '='.
Douglas Gregor61366e92008-12-24 00:01:03 +00003888 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00003889
Douglas Gregorbe0f7bd2010-09-11 20:24:53 +00003890 // The argument isn't actually potentially evaluated unless it is
3891 // used.
3892 EnterExpressionEvaluationContext Eval(Actions,
3893 Sema::PotentiallyEvaluatedIfUsed);
3894
John McCall60d7b3a2010-08-24 06:29:42 +00003895 ExprResult DefArgResult(ParseAssignmentExpression());
Douglas Gregor72b505b2008-12-16 21:30:33 +00003896 if (DefArgResult.isInvalid()) {
3897 Actions.ActOnParamDefaultArgumentError(Param);
3898 SkipUntil(tok::comma, tok::r_paren, true, true);
3899 } else {
3900 // Inform the actions module about the default argument
3901 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
John McCall9ae2f072010-08-23 23:25:46 +00003902 DefArgResult.take());
Douglas Gregor72b505b2008-12-16 21:30:33 +00003903 }
Chris Lattner04421082008-04-08 04:40:51 +00003904 }
3905 }
Mike Stump1eb44332009-09-09 15:08:12 +00003906
3907 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
3908 ParmDecl.getIdentifierLoc(), Param,
Douglas Gregor72b505b2008-12-16 21:30:33 +00003909 DefArgToks));
Chris Lattnerf97409f2008-04-06 06:57:35 +00003910 }
3911
3912 // If the next token is a comma, consume it and keep reading arguments.
Douglas Gregored5d6512009-09-22 21:41:40 +00003913 if (Tok.isNot(tok::comma)) {
3914 if (Tok.is(tok::ellipsis)) {
3915 IsVariadic = true;
3916 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
3917
3918 if (!getLang().CPlusPlus) {
3919 // We have ellipsis without a preceding ',', which is ill-formed
3920 // in C. Complain and provide the fix.
3921 Diag(EllipsisLoc, diag::err_missing_comma_before_ellipsis)
Douglas Gregor849b2432010-03-31 17:46:05 +00003922 << FixItHint::CreateInsertion(EllipsisLoc, ", ");
Douglas Gregored5d6512009-09-22 21:41:40 +00003923 }
3924 }
3925
3926 break;
3927 }
Mike Stump1eb44332009-09-09 15:08:12 +00003928
Chris Lattnerf97409f2008-04-06 06:57:35 +00003929 // Consume the comma.
3930 ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00003931 }
Mike Stump1eb44332009-09-09 15:08:12 +00003932
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003933 // If we have the closing ')', eat it.
Abramo Bagnara796aa442011-03-12 11:17:06 +00003934 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003935
John McCall0b7e6782011-03-24 11:26:52 +00003936 DeclSpec DS(AttrFactory);
Douglas Gregor83f51722011-01-26 03:43:54 +00003937 SourceLocation RefQualifierLoc;
3938 bool RefQualifierIsLValueRef = true;
Sebastian Redl7acafd02011-03-05 14:45:16 +00003939 ExceptionSpecificationType ESpecType = EST_None;
3940 SourceRange ESpecRange;
3941 llvm::SmallVector<ParsedType, 2> DynamicExceptions;
3942 llvm::SmallVector<SourceRange, 2> DynamicExceptionRanges;
3943 ExprResult NoexceptExpr;
Sean Huntbbd37c62009-11-21 08:43:09 +00003944
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003945 if (getLang().CPlusPlus) {
John McCall7f040a92010-12-24 02:08:15 +00003946 MaybeParseCXX0XAttributes(attrs);
3947
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00003948 // Parse cv-qualifier-seq[opt].
Chris Lattner5a69d1c2008-12-18 07:02:59 +00003949 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Sebastian Redlab197ba2009-02-09 18:23:29 +00003950 if (!DS.getSourceRange().getEnd().isInvalid())
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00003951 EndLoc = DS.getSourceRange().getEnd();
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00003952
Douglas Gregor83f51722011-01-26 03:43:54 +00003953 // Parse ref-qualifier[opt]
3954 if (Tok.is(tok::amp) || Tok.is(tok::ampamp)) {
3955 if (!getLang().CPlusPlus0x)
Douglas Gregor1f381062011-01-26 20:35:32 +00003956 Diag(Tok, diag::ext_ref_qualifier);
Douglas Gregor83f51722011-01-26 03:43:54 +00003957
3958 RefQualifierIsLValueRef = Tok.is(tok::amp);
3959 RefQualifierLoc = ConsumeToken();
3960 EndLoc = RefQualifierLoc;
3961 }
3962
Sebastian Redl7acafd02011-03-05 14:45:16 +00003963 // FIXME: We should leave the prototype scope before parsing the exception
3964 // specification, and then reenter it when parsing the trailing return type.
3965 // FIXMEFIXME: Why? That wouldn't be right for the noexcept clause.
3966
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00003967 // Parse exception-specification[opt].
Sebastian Redl7acafd02011-03-05 14:45:16 +00003968 ESpecType = MaybeParseExceptionSpecification(ESpecRange,
3969 DynamicExceptions,
3970 DynamicExceptionRanges,
3971 NoexceptExpr);
3972 if (ESpecType != EST_None)
3973 EndLoc = ESpecRange.getEnd();
Douglas Gregordab60ad2010-10-01 18:44:50 +00003974
3975 // Parse trailing-return-type.
3976 if (getLang().CPlusPlus0x && Tok.is(tok::arrow)) {
3977 TrailingReturnType = ParseTrailingReturnType().get();
3978 }
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003979 }
3980
Douglas Gregordab60ad2010-10-01 18:44:50 +00003981 // Leave prototype scope.
3982 PrototypeScope.Exit();
3983
Reid Spencer5f016e22007-07-11 17:01:13 +00003984 // Remember that we parsed a function type, and remember the attributes.
John McCall0b7e6782011-03-24 11:26:52 +00003985 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/true, IsVariadic,
Douglas Gregor965acbb2009-02-18 07:07:28 +00003986 EllipsisLoc,
Jay Foadbeaaccd2009-05-21 09:52:38 +00003987 ParamInfo.data(), ParamInfo.size(),
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003988 DS.getTypeQualifiers(),
Douglas Gregor83f51722011-01-26 03:43:54 +00003989 RefQualifierIsLValueRef,
3990 RefQualifierLoc,
Sebastian Redl6e5d3192011-03-05 22:42:13 +00003991 ESpecType, ESpecRange.getBegin(),
Sebastian Redl7acafd02011-03-05 14:45:16 +00003992 DynamicExceptions.data(),
3993 DynamicExceptionRanges.data(),
3994 DynamicExceptions.size(),
Sebastian Redl6e5d3192011-03-05 22:42:13 +00003995 NoexceptExpr.isUsable() ?
3996 NoexceptExpr.get() : 0,
Abramo Bagnara796aa442011-03-12 11:17:06 +00003997 LParenLoc, EndLoc, D,
Douglas Gregordab60ad2010-10-01 18:44:50 +00003998 TrailingReturnType),
John McCall0b7e6782011-03-24 11:26:52 +00003999 attrs, EndLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00004000}
4001
Chris Lattner66d28652008-04-06 06:34:08 +00004002/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
4003/// we found a K&R-style identifier list instead of a type argument list. The
Chris Lattner83a94472010-05-14 17:23:36 +00004004/// first identifier has already been consumed, and the current token is the
4005/// token right after it.
Chris Lattner66d28652008-04-06 06:34:08 +00004006///
4007/// identifier-list: [C99 6.7.5]
4008/// identifier
4009/// identifier-list ',' identifier
4010///
4011void Parser::ParseFunctionDeclaratorIdentifierList(SourceLocation LParenLoc,
Chris Lattner83a94472010-05-14 17:23:36 +00004012 IdentifierInfo *FirstIdent,
4013 SourceLocation FirstIdentLoc,
Chris Lattner66d28652008-04-06 06:34:08 +00004014 Declarator &D) {
4015 // Build up an array of information about the parsed arguments.
4016 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
4017 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
Mike Stump1eb44332009-09-09 15:08:12 +00004018
Chris Lattner66d28652008-04-06 06:34:08 +00004019 // If there was no identifier specified for the declarator, either we are in
4020 // an abstract-declarator, or we are in a parameter declarator which was found
4021 // to be abstract. In abstract-declarators, identifier lists are not valid:
4022 // diagnose this.
4023 if (!D.getIdentifier())
Chris Lattner83a94472010-05-14 17:23:36 +00004024 Diag(FirstIdentLoc, diag::ext_ident_list_in_param);
Chris Lattner66d28652008-04-06 06:34:08 +00004025
Chris Lattner83a94472010-05-14 17:23:36 +00004026 // The first identifier was already read, and is known to be the first
4027 // identifier in the list. Remember this identifier in ParamInfo.
4028 ParamsSoFar.insert(FirstIdent);
John McCalld226f652010-08-21 09:40:31 +00004029 ParamInfo.push_back(DeclaratorChunk::ParamInfo(FirstIdent, FirstIdentLoc, 0));
Mike Stump1eb44332009-09-09 15:08:12 +00004030
Chris Lattner66d28652008-04-06 06:34:08 +00004031 while (Tok.is(tok::comma)) {
4032 // Eat the comma.
4033 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00004034
Chris Lattner50c64772008-04-06 06:39:19 +00004035 // If this isn't an identifier, report the error and skip until ')'.
Chris Lattner66d28652008-04-06 06:34:08 +00004036 if (Tok.isNot(tok::identifier)) {
4037 Diag(Tok, diag::err_expected_ident);
Chris Lattner50c64772008-04-06 06:39:19 +00004038 SkipUntil(tok::r_paren);
4039 return;
Chris Lattner66d28652008-04-06 06:34:08 +00004040 }
Chris Lattneraaf9ddb2008-04-06 06:47:48 +00004041
Chris Lattner66d28652008-04-06 06:34:08 +00004042 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
Chris Lattneraaf9ddb2008-04-06 06:47:48 +00004043
4044 // Reject 'typedef int y; int test(x, y)', but continue parsing.
Douglas Gregor23c94db2010-07-02 17:43:08 +00004045 if (Actions.getTypeName(*ParmII, Tok.getLocation(), getCurScope()))
Chris Lattnerda83bac2008-11-19 07:37:42 +00004046 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
Mike Stump1eb44332009-09-09 15:08:12 +00004047
Chris Lattner66d28652008-04-06 06:34:08 +00004048 // Verify that the argument identifier has not already been mentioned.
4049 if (!ParamsSoFar.insert(ParmII)) {
Chris Lattnerda83bac2008-11-19 07:37:42 +00004050 Diag(Tok, diag::err_param_redefinition) << ParmII;
Chris Lattner50c64772008-04-06 06:39:19 +00004051 } else {
4052 // Remember this identifier in ParamInfo.
Chris Lattner66d28652008-04-06 06:34:08 +00004053 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Chris Lattnerb28317a2009-03-28 19:18:32 +00004054 Tok.getLocation(),
John McCalld226f652010-08-21 09:40:31 +00004055 0));
Chris Lattner50c64772008-04-06 06:39:19 +00004056 }
Mike Stump1eb44332009-09-09 15:08:12 +00004057
Chris Lattner66d28652008-04-06 06:34:08 +00004058 // Eat the identifier.
4059 ConsumeToken();
4060 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00004061
4062 // If we have the closing ')', eat it and we're done.
4063 SourceLocation RLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
4064
Chris Lattner50c64772008-04-06 06:39:19 +00004065 // Remember that we parsed a function type, and remember the attributes. This
4066 // function type is always a K&R style function type, which is not varargs and
4067 // has no prototype.
John McCall0b7e6782011-03-24 11:26:52 +00004068 ParsedAttributes attrs(AttrFactory);
4069 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/false, /*varargs*/false,
Douglas Gregor965acbb2009-02-18 07:07:28 +00004070 SourceLocation(),
Chris Lattner50c64772008-04-06 06:39:19 +00004071 &ParamInfo[0], ParamInfo.size(),
Sebastian Redl7dc81342009-04-29 17:30:04 +00004072 /*TypeQuals*/0,
Douglas Gregor83f51722011-01-26 03:43:54 +00004073 true, SourceLocation(),
Sebastian Redl6e5d3192011-03-05 22:42:13 +00004074 EST_None, SourceLocation(), 0, 0,
4075 0, 0, LParenLoc, RLoc, D),
John McCall0b7e6782011-03-24 11:26:52 +00004076 attrs, RLoc);
Chris Lattner66d28652008-04-06 06:34:08 +00004077}
Chris Lattneref4715c2008-04-06 05:45:57 +00004078
Reid Spencer5f016e22007-07-11 17:01:13 +00004079/// [C90] direct-declarator '[' constant-expression[opt] ']'
4080/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
4081/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
4082/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
4083/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
4084void Parser::ParseBracketDeclarator(Declarator &D) {
4085 SourceLocation StartLoc = ConsumeBracket();
Mike Stump1eb44332009-09-09 15:08:12 +00004086
Chris Lattner378c7e42008-12-18 07:27:21 +00004087 // C array syntax has many features, but by-far the most common is [] and [4].
4088 // This code does a fast path to handle some of the most obvious cases.
4089 if (Tok.getKind() == tok::r_square) {
Sebastian Redlab197ba2009-02-09 18:23:29 +00004090 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
John McCall0b7e6782011-03-24 11:26:52 +00004091 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00004092 MaybeParseCXX0XAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00004093
Chris Lattner378c7e42008-12-18 07:27:21 +00004094 // Remember that we parsed the empty array type.
John McCall60d7b3a2010-08-24 06:29:42 +00004095 ExprResult NumElements;
John McCall0b7e6782011-03-24 11:26:52 +00004096 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00004097 StartLoc, EndLoc),
John McCall0b7e6782011-03-24 11:26:52 +00004098 attrs, EndLoc);
Chris Lattner378c7e42008-12-18 07:27:21 +00004099 return;
4100 } else if (Tok.getKind() == tok::numeric_constant &&
4101 GetLookAheadToken(1).is(tok::r_square)) {
4102 // [4] is very common. Parse the numeric constant expression.
John McCall60d7b3a2010-08-24 06:29:42 +00004103 ExprResult ExprRes(Actions.ActOnNumericConstant(Tok));
Chris Lattner378c7e42008-12-18 07:27:21 +00004104 ConsumeToken();
4105
Sebastian Redlab197ba2009-02-09 18:23:29 +00004106 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
John McCall0b7e6782011-03-24 11:26:52 +00004107 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00004108 MaybeParseCXX0XAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00004109
Chris Lattner378c7e42008-12-18 07:27:21 +00004110 // Remember that we parsed a array type, and remember its features.
John McCall0b7e6782011-03-24 11:26:52 +00004111 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, 0,
John McCall7f040a92010-12-24 02:08:15 +00004112 ExprRes.release(),
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00004113 StartLoc, EndLoc),
John McCall0b7e6782011-03-24 11:26:52 +00004114 attrs, EndLoc);
Chris Lattner378c7e42008-12-18 07:27:21 +00004115 return;
4116 }
Mike Stump1eb44332009-09-09 15:08:12 +00004117
Reid Spencer5f016e22007-07-11 17:01:13 +00004118 // If valid, this location is the position where we read the 'static' keyword.
4119 SourceLocation StaticLoc;
Chris Lattner04d66662007-10-09 17:33:22 +00004120 if (Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00004121 StaticLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00004122
Reid Spencer5f016e22007-07-11 17:01:13 +00004123 // If there is a type-qualifier-list, read it now.
Chris Lattnera1fcbad2008-12-18 06:50:14 +00004124 // Type qualifiers in an array subscript are a C99 feature.
John McCall0b7e6782011-03-24 11:26:52 +00004125 DeclSpec DS(AttrFactory);
Chris Lattner5a69d1c2008-12-18 07:02:59 +00004126 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Mike Stump1eb44332009-09-09 15:08:12 +00004127
Reid Spencer5f016e22007-07-11 17:01:13 +00004128 // If we haven't already read 'static', check to see if there is one after the
4129 // type-qualifier-list.
Chris Lattner04d66662007-10-09 17:33:22 +00004130 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00004131 StaticLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00004132
Reid Spencer5f016e22007-07-11 17:01:13 +00004133 // Handle "direct-declarator [ type-qual-list[opt] * ]".
4134 bool isStar = false;
John McCall60d7b3a2010-08-24 06:29:42 +00004135 ExprResult NumElements;
Mike Stump1eb44332009-09-09 15:08:12 +00004136
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00004137 // Handle the case where we have '[*]' as the array size. However, a leading
4138 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
4139 // the the token after the star is a ']'. Since stars in arrays are
4140 // infrequent, use of lookahead is not costly here.
4141 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattnera711dd02008-04-06 05:27:21 +00004142 ConsumeToken(); // Eat the '*'.
Reid Spencer5f016e22007-07-11 17:01:13 +00004143
Chris Lattnera1fcbad2008-12-18 06:50:14 +00004144 if (StaticLoc.isValid()) {
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00004145 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattnera1fcbad2008-12-18 06:50:14 +00004146 StaticLoc = SourceLocation(); // Drop the static.
4147 }
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00004148 isStar = true;
Chris Lattner04d66662007-10-09 17:33:22 +00004149 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner378c7e42008-12-18 07:27:21 +00004150 // Note, in C89, this production uses the constant-expr production instead
4151 // of assignment-expr. The only difference is that assignment-expr allows
4152 // things like '=' and '*='. Sema rejects these in C89 mode because they
4153 // are not i-c-e's, so we don't need to distinguish between the two here.
Mike Stump1eb44332009-09-09 15:08:12 +00004154
Douglas Gregore0762c92009-06-19 23:52:42 +00004155 // Parse the constant-expression or assignment-expression now (depending
4156 // on dialect).
4157 if (getLang().CPlusPlus)
4158 NumElements = ParseConstantExpression();
4159 else
4160 NumElements = ParseAssignmentExpression();
Reid Spencer5f016e22007-07-11 17:01:13 +00004161 }
Mike Stump1eb44332009-09-09 15:08:12 +00004162
Reid Spencer5f016e22007-07-11 17:01:13 +00004163 // If there was an error parsing the assignment-expression, recover.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00004164 if (NumElements.isInvalid()) {
Chris Lattner5cb10d32009-04-24 22:30:50 +00004165 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00004166 // If the expression was invalid, skip it.
4167 SkipUntil(tok::r_square);
4168 return;
4169 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00004170
4171 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
4172
John McCall0b7e6782011-03-24 11:26:52 +00004173 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00004174 MaybeParseCXX0XAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00004175
Chris Lattner378c7e42008-12-18 07:27:21 +00004176 // Remember that we parsed a array type, and remember its features.
John McCall0b7e6782011-03-24 11:26:52 +00004177 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
Reid Spencer5f016e22007-07-11 17:01:13 +00004178 StaticLoc.isValid(), isStar,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00004179 NumElements.release(),
4180 StartLoc, EndLoc),
John McCall0b7e6782011-03-24 11:26:52 +00004181 attrs, EndLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00004182}
4183
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004184/// [GNU] typeof-specifier:
4185/// typeof ( expressions )
4186/// typeof ( type-name )
4187/// [GNU/C++] typeof unary-expression
Steve Naroffd1861fd2007-07-31 12:34:36 +00004188///
4189void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner04d66662007-10-09 17:33:22 +00004190 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004191 Token OpTok = Tok;
Steve Naroffd1861fd2007-07-31 12:34:36 +00004192 SourceLocation StartLoc = ConsumeToken();
4193
John McCallcfb708c2010-01-13 20:03:27 +00004194 const bool hasParens = Tok.is(tok::l_paren);
4195
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004196 bool isCastExpr;
John McCallb3d87482010-08-24 05:47:05 +00004197 ParsedType CastTy;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004198 SourceRange CastRange;
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00004199 ExprResult Operand = ParseExprAfterUnaryExprOrTypeTrait(OpTok, isCastExpr,
4200 CastTy, CastRange);
John McCallcfb708c2010-01-13 20:03:27 +00004201 if (hasParens)
4202 DS.setTypeofParensRange(CastRange);
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004203
4204 if (CastRange.getEnd().isInvalid())
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004205 // FIXME: Not accurate, the range gets one token more than it should.
4206 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004207 else
4208 DS.SetRangeEnd(CastRange.getEnd());
Mike Stump1eb44332009-09-09 15:08:12 +00004209
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004210 if (isCastExpr) {
4211 if (!CastTy) {
4212 DS.SetTypeSpecError();
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004213 return;
Douglas Gregor809070a2009-02-18 17:45:20 +00004214 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004215
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004216 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00004217 unsigned DiagID;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004218 // Check for duplicate type specifiers (e.g. "int typeof(int)").
4219 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00004220 DiagID, CastTy))
4221 Diag(StartLoc, DiagID) << PrevSpec;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004222 return;
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004223 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004224
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004225 // If we get here, the operand to the typeof was an expresion.
4226 if (Operand.isInvalid()) {
4227 DS.SetTypeSpecError();
Steve Naroff9dfa7b42007-08-02 02:53:48 +00004228 return;
Steve Naroffd1861fd2007-07-31 12:34:36 +00004229 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004230
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004231 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00004232 unsigned DiagID;
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004233 // Check for duplicate type specifiers (e.g. "int typeof(int)").
4234 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
John McCallb3d87482010-08-24 05:47:05 +00004235 DiagID, Operand.get()))
John McCallfec54012009-08-03 20:12:06 +00004236 Diag(StartLoc, DiagID) << PrevSpec;
Steve Naroffd1861fd2007-07-31 12:34:36 +00004237}
Chris Lattner1b492422010-02-28 18:33:55 +00004238
4239
4240/// TryAltiVecVectorTokenOutOfLine - Out of line body that should only be called
4241/// from TryAltiVecVectorToken.
4242bool Parser::TryAltiVecVectorTokenOutOfLine() {
4243 Token Next = NextToken();
4244 switch (Next.getKind()) {
4245 default: return false;
4246 case tok::kw_short:
4247 case tok::kw_long:
4248 case tok::kw_signed:
4249 case tok::kw_unsigned:
4250 case tok::kw_void:
4251 case tok::kw_char:
4252 case tok::kw_int:
4253 case tok::kw_float:
4254 case tok::kw_double:
4255 case tok::kw_bool:
4256 case tok::kw___pixel:
4257 Tok.setKind(tok::kw___vector);
4258 return true;
4259 case tok::identifier:
4260 if (Next.getIdentifierInfo() == Ident_pixel) {
4261 Tok.setKind(tok::kw___vector);
4262 return true;
4263 }
4264 return false;
4265 }
4266}
4267
4268bool Parser::TryAltiVecTokenOutOfLine(DeclSpec &DS, SourceLocation Loc,
4269 const char *&PrevSpec, unsigned &DiagID,
4270 bool &isInvalid) {
4271 if (Tok.getIdentifierInfo() == Ident_vector) {
4272 Token Next = NextToken();
4273 switch (Next.getKind()) {
4274 case tok::kw_short:
4275 case tok::kw_long:
4276 case tok::kw_signed:
4277 case tok::kw_unsigned:
4278 case tok::kw_void:
4279 case tok::kw_char:
4280 case tok::kw_int:
4281 case tok::kw_float:
4282 case tok::kw_double:
4283 case tok::kw_bool:
4284 case tok::kw___pixel:
4285 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
4286 return true;
4287 case tok::identifier:
4288 if (Next.getIdentifierInfo() == Ident_pixel) {
4289 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
4290 return true;
4291 }
4292 break;
4293 default:
4294 break;
4295 }
Douglas Gregora8f031f2010-06-16 15:28:57 +00004296 } else if ((Tok.getIdentifierInfo() == Ident_pixel) &&
Chris Lattner1b492422010-02-28 18:33:55 +00004297 DS.isTypeAltiVecVector()) {
4298 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
4299 return true;
4300 }
4301 return false;
4302}