blob: eadd5464f2972cd9915c7fb9d1407a2f7d7d5604 [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();
817 // FIXME: handle braced-init-list here.
818 FRI->RangeExpr = ParseExpression();
819 Decl *ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
820 Actions.ActOnCXXForRangeDecl(ThisDecl);
821 Actions.FinalizeDeclaration(ThisDecl);
822 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS, &ThisDecl, 1);
823 }
824
John McCalld226f652010-08-21 09:40:31 +0000825 llvm::SmallVector<Decl *, 8> DeclsInGroup;
Richard Smithad762fc2011-04-14 22:09:26 +0000826 Decl *FirstDecl = ParseDeclarationAfterDeclaratorAndAttributes(D);
John McCall54abf7d2009-11-04 02:18:39 +0000827 D.complete(FirstDecl);
John McCalld226f652010-08-21 09:40:31 +0000828 if (FirstDecl)
John McCalld8ac0572009-11-03 19:26:08 +0000829 DeclsInGroup.push_back(FirstDecl);
830
831 // If we don't have a comma, it is either the end of the list (a ';') or an
832 // error, bail out.
833 while (Tok.is(tok::comma)) {
834 // Consume the comma.
Chris Lattner23c4b182009-03-29 17:18:04 +0000835 ConsumeToken();
John McCalld8ac0572009-11-03 19:26:08 +0000836
837 // Parse the next declarator.
838 D.clear();
839
840 // Accept attributes in an init-declarator. In the first declarator in a
841 // declaration, these would be part of the declspec. In subsequent
842 // declarators, they become part of the declarator itself, so that they
843 // don't apply to declarators after *this* one. Examples:
844 // short __attribute__((common)) var; -> declspec
845 // short var __attribute__((common)); -> declarator
846 // short x, __attribute__((common)) var; -> declarator
John McCall7f040a92010-12-24 02:08:15 +0000847 MaybeParseGNUAttributes(D);
John McCalld8ac0572009-11-03 19:26:08 +0000848
849 ParseDeclarator(D);
850
John McCalld226f652010-08-21 09:40:31 +0000851 Decl *ThisDecl = ParseDeclarationAfterDeclarator(D);
John McCall54abf7d2009-11-04 02:18:39 +0000852 D.complete(ThisDecl);
John McCalld226f652010-08-21 09:40:31 +0000853 if (ThisDecl)
John McCalld8ac0572009-11-03 19:26:08 +0000854 DeclsInGroup.push_back(ThisDecl);
855 }
856
857 if (DeclEnd)
858 *DeclEnd = Tok.getLocation();
859
860 if (Context != Declarator::ForContext &&
861 ExpectAndConsume(tok::semi,
862 Context == Declarator::FileContext
863 ? diag::err_invalid_token_after_toplevel_declarator
864 : diag::err_expected_semi_declaration)) {
Chris Lattner004659a2010-07-11 22:42:07 +0000865 // Okay, there was no semicolon and one was expected. If we see a
866 // declaration specifier, just assume it was missing and continue parsing.
867 // Otherwise things are very confused and we skip to recover.
868 if (!isDeclarationSpecifier()) {
869 SkipUntil(tok::r_brace, true, true);
870 if (Tok.is(tok::semi))
871 ConsumeToken();
872 }
John McCalld8ac0572009-11-03 19:26:08 +0000873 }
874
Douglas Gregor23c94db2010-07-02 17:43:08 +0000875 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS,
John McCalld8ac0572009-11-03 19:26:08 +0000876 DeclsInGroup.data(),
877 DeclsInGroup.size());
Reid Spencer5f016e22007-07-11 17:01:13 +0000878}
879
Richard Smithad762fc2011-04-14 22:09:26 +0000880/// Parse an optional simple-asm-expr and attributes, and attach them to a
881/// declarator. Returns true on an error.
882bool Parser::ParseAttributesAfterDeclarator(Declarator &D) {
883 // If a simple-asm-expr is present, parse it.
884 if (Tok.is(tok::kw_asm)) {
885 SourceLocation Loc;
886 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
887 if (AsmLabel.isInvalid()) {
888 SkipUntil(tok::semi, true, true);
889 return true;
890 }
891
892 D.setAsmLabel(AsmLabel.release());
893 D.SetRangeEnd(Loc);
894 }
895
896 MaybeParseGNUAttributes(D);
897 return false;
898}
899
Douglas Gregor1426e532009-05-12 21:31:51 +0000900/// \brief Parse 'declaration' after parsing 'declaration-specifiers
901/// declarator'. This method parses the remainder of the declaration
902/// (including any attributes or initializer, among other things) and
903/// finalizes the declaration.
Reid Spencer5f016e22007-07-11 17:01:13 +0000904///
Reid Spencer5f016e22007-07-11 17:01:13 +0000905/// init-declarator: [C99 6.7]
906/// declarator
907/// declarator '=' initializer
908/// [GNU] declarator simple-asm-expr[opt] attributes[opt]
909/// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +0000910/// [C++] declarator initializer[opt]
911///
912/// [C++] initializer:
913/// [C++] '=' initializer-clause
914/// [C++] '(' expression-list ')'
Sebastian Redl50de12f2009-03-24 22:27:57 +0000915/// [C++0x] '=' 'default' [TODO]
916/// [C++0x] '=' 'delete'
917///
918/// According to the standard grammar, =default and =delete are function
919/// definitions, but that definitely doesn't fit with the parser here.
Reid Spencer5f016e22007-07-11 17:01:13 +0000920///
John McCalld226f652010-08-21 09:40:31 +0000921Decl *Parser::ParseDeclarationAfterDeclarator(Declarator &D,
Douglas Gregore542c862009-06-23 23:11:28 +0000922 const ParsedTemplateInfo &TemplateInfo) {
Richard Smithad762fc2011-04-14 22:09:26 +0000923 if (ParseAttributesAfterDeclarator(D))
924 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000925
Richard Smithad762fc2011-04-14 22:09:26 +0000926 return ParseDeclarationAfterDeclaratorAndAttributes(D, TemplateInfo);
927}
Mike Stump1eb44332009-09-09 15:08:12 +0000928
Richard Smithad762fc2011-04-14 22:09:26 +0000929Decl *Parser::ParseDeclarationAfterDeclaratorAndAttributes(Declarator &D,
930 const ParsedTemplateInfo &TemplateInfo) {
Douglas Gregor1426e532009-05-12 21:31:51 +0000931 // Inform the current actions module that we just parsed this declarator.
John McCalld226f652010-08-21 09:40:31 +0000932 Decl *ThisDecl = 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +0000933 switch (TemplateInfo.Kind) {
934 case ParsedTemplateInfo::NonTemplate:
Douglas Gregor23c94db2010-07-02 17:43:08 +0000935 ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
Douglas Gregord5a423b2009-09-25 18:43:00 +0000936 break;
937
938 case ParsedTemplateInfo::Template:
939 case ParsedTemplateInfo::ExplicitSpecialization:
Douglas Gregor23c94db2010-07-02 17:43:08 +0000940 ThisDecl = Actions.ActOnTemplateDeclarator(getCurScope(),
John McCallf312b1e2010-08-26 23:41:50 +0000941 MultiTemplateParamsArg(Actions,
Douglas Gregore542c862009-06-23 23:11:28 +0000942 TemplateInfo.TemplateParams->data(),
943 TemplateInfo.TemplateParams->size()),
Douglas Gregord5a423b2009-09-25 18:43:00 +0000944 D);
945 break;
946
947 case ParsedTemplateInfo::ExplicitInstantiation: {
John McCalld226f652010-08-21 09:40:31 +0000948 DeclResult ThisRes
Douglas Gregor23c94db2010-07-02 17:43:08 +0000949 = Actions.ActOnExplicitInstantiation(getCurScope(),
Douglas Gregord5a423b2009-09-25 18:43:00 +0000950 TemplateInfo.ExternLoc,
951 TemplateInfo.TemplateLoc,
952 D);
953 if (ThisRes.isInvalid()) {
954 SkipUntil(tok::semi, true, true);
John McCalld226f652010-08-21 09:40:31 +0000955 return 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +0000956 }
957
958 ThisDecl = ThisRes.get();
959 break;
960 }
961 }
Mike Stump1eb44332009-09-09 15:08:12 +0000962
Richard Smith34b41d92011-02-20 03:19:35 +0000963 bool TypeContainsAuto =
964 D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto;
965
Douglas Gregor1426e532009-05-12 21:31:51 +0000966 // Parse declarator '=' initializer.
Argyrios Kyrtzidisa6eb5f82010-10-08 02:39:23 +0000967 if (isTokenEqualOrMistypedEqualEqual(
968 diag::err_invalid_equalequal_after_declarator)) {
Douglas Gregor1426e532009-05-12 21:31:51 +0000969 ConsumeToken();
Anders Carlsson37bf9d22010-09-24 21:25:25 +0000970 if (Tok.is(tok::kw_delete)) {
Sean Hunte4246a62011-05-12 06:15:49 +0000971 if (D.isFunctionDeclarator())
972 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
973 << 1 /* delete */;
974 else
975 Diag(ConsumeToken(), diag::err_deleted_non_function);
Sean Huntfe2695e2011-05-06 01:42:00 +0000976 } else if (Tok.is(tok::kw_default)) {
Sean Hunte4246a62011-05-12 06:15:49 +0000977 if (D.isFunctionDeclarator())
978 Diag(Tok, diag::err_default_delete_in_multiple_declaration)
979 << 1 /* delete */;
980 else
981 Diag(ConsumeToken(), diag::err_default_special_members);
Douglas Gregor1426e532009-05-12 21:31:51 +0000982 } else {
John McCall731ad842009-12-19 09:28:58 +0000983 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
984 EnterScope(0);
Douglas Gregor23c94db2010-07-02 17:43:08 +0000985 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
John McCall731ad842009-12-19 09:28:58 +0000986 }
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +0000987
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +0000988 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000989 Actions.CodeCompleteInitializer(getCurScope(), ThisDecl);
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +0000990 ConsumeCodeCompletionToken();
991 SkipUntil(tok::comma, true, true);
992 return ThisDecl;
993 }
994
John McCall60d7b3a2010-08-24 06:29:42 +0000995 ExprResult Init(ParseInitializer());
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +0000996
John McCall731ad842009-12-19 09:28:58 +0000997 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000998 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
John McCall731ad842009-12-19 09:28:58 +0000999 ExitScope();
1000 }
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +00001001
Douglas Gregor1426e532009-05-12 21:31:51 +00001002 if (Init.isInvalid()) {
Douglas Gregor00225542010-03-01 18:27:54 +00001003 SkipUntil(tok::comma, true, true);
1004 Actions.ActOnInitializerError(ThisDecl);
1005 } else
Richard Smith34b41d92011-02-20 03:19:35 +00001006 Actions.AddInitializerToDecl(ThisDecl, Init.take(),
1007 /*DirectInit=*/false, TypeContainsAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +00001008 }
1009 } else if (Tok.is(tok::l_paren)) {
1010 // Parse C++ direct initializer: '(' expression-list ')'
1011 SourceLocation LParenLoc = ConsumeParen();
1012 ExprVector Exprs(Actions);
1013 CommaLocsTy CommaLocs;
1014
Douglas Gregorb4debae2009-12-22 17:47:17 +00001015 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
1016 EnterScope(0);
Douglas Gregor23c94db2010-07-02 17:43:08 +00001017 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001018 }
1019
Douglas Gregor1426e532009-05-12 21:31:51 +00001020 if (ParseExpressionList(Exprs, CommaLocs)) {
1021 SkipUntil(tok::r_paren);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001022
1023 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001024 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001025 ExitScope();
1026 }
Douglas Gregor1426e532009-05-12 21:31:51 +00001027 } else {
1028 // Match the ')'.
1029 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
1030
1031 assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() &&
1032 "Unexpected number of commas!");
Douglas Gregorb4debae2009-12-22 17:47:17 +00001033
1034 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001035 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001036 ExitScope();
1037 }
1038
Douglas Gregor1426e532009-05-12 21:31:51 +00001039 Actions.AddCXXDirectInitializerToDecl(ThisDecl, LParenLoc,
1040 move_arg(Exprs),
Richard Smith34b41d92011-02-20 03:19:35 +00001041 RParenLoc,
1042 TypeContainsAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +00001043 }
1044 } else {
Richard Smith34b41d92011-02-20 03:19:35 +00001045 Actions.ActOnUninitializedDecl(ThisDecl, TypeContainsAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +00001046 }
1047
Richard Smith483b9f32011-02-21 20:05:19 +00001048 Actions.FinalizeDeclaration(ThisDecl);
1049
Douglas Gregor1426e532009-05-12 21:31:51 +00001050 return ThisDecl;
1051}
1052
Reid Spencer5f016e22007-07-11 17:01:13 +00001053/// ParseSpecifierQualifierList
1054/// specifier-qualifier-list:
1055/// type-specifier specifier-qualifier-list[opt]
1056/// type-qualifier specifier-qualifier-list[opt]
1057/// [GNU] attributes specifier-qualifier-list[opt]
1058///
1059void Parser::ParseSpecifierQualifierList(DeclSpec &DS) {
1060 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
1061 /// parse declaration-specifiers and complain about extra stuff.
Reid Spencer5f016e22007-07-11 17:01:13 +00001062 ParseDeclarationSpecifiers(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00001063
Reid Spencer5f016e22007-07-11 17:01:13 +00001064 // Validate declspec for type-name.
1065 unsigned Specs = DS.getParsedSpecifiers();
Chris Lattnerb6645dd2009-04-14 21:16:09 +00001066 if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers() &&
John McCall7f040a92010-12-24 02:08:15 +00001067 !DS.hasAttributes())
Reid Spencer5f016e22007-07-11 17:01:13 +00001068 Diag(Tok, diag::err_typename_requires_specqual);
Mike Stump1eb44332009-09-09 15:08:12 +00001069
Reid Spencer5f016e22007-07-11 17:01:13 +00001070 // Issue diagnostic and remove storage class if present.
1071 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
1072 if (DS.getStorageClassSpecLoc().isValid())
1073 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
1074 else
1075 Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass);
1076 DS.ClearStorageClassSpecs();
1077 }
Mike Stump1eb44332009-09-09 15:08:12 +00001078
Reid Spencer5f016e22007-07-11 17:01:13 +00001079 // Issue diagnostic and remove function specfier if present.
1080 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Douglas Gregorb48fe382008-10-31 09:07:45 +00001081 if (DS.isInlineSpecified())
1082 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
1083 if (DS.isVirtualSpecified())
1084 Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec);
1085 if (DS.isExplicitSpecified())
1086 Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec);
Reid Spencer5f016e22007-07-11 17:01:13 +00001087 DS.ClearFunctionSpecs();
1088 }
1089}
1090
Chris Lattnerc199ab32009-04-12 20:42:31 +00001091/// isValidAfterIdentifierInDeclaratorAfterDeclSpec - Return true if the
1092/// specified token is valid after the identifier in a declarator which
1093/// immediately follows the declspec. For example, these things are valid:
1094///
1095/// int x [ 4]; // direct-declarator
1096/// int x ( int y); // direct-declarator
1097/// int(int x ) // direct-declarator
1098/// int x ; // simple-declaration
1099/// int x = 17; // init-declarator-list
1100/// int x , y; // init-declarator-list
1101/// int x __asm__ ("foo"); // init-declarator-list
Chris Lattnerb6645dd2009-04-14 21:16:09 +00001102/// int x : 4; // struct-declarator
Chris Lattnerc83c27a2009-04-12 22:29:43 +00001103/// int x { 5}; // C++'0x unified initializers
Chris Lattnerc199ab32009-04-12 20:42:31 +00001104///
1105/// This is not, because 'x' does not immediately follow the declspec (though
1106/// ')' happens to be valid anyway).
1107/// int (x)
1108///
1109static bool isValidAfterIdentifierInDeclarator(const Token &T) {
1110 return T.is(tok::l_square) || T.is(tok::l_paren) || T.is(tok::r_paren) ||
1111 T.is(tok::semi) || T.is(tok::comma) || T.is(tok::equal) ||
Chris Lattnerb6645dd2009-04-14 21:16:09 +00001112 T.is(tok::kw_asm) || T.is(tok::l_brace) || T.is(tok::colon);
Chris Lattnerc199ab32009-04-12 20:42:31 +00001113}
1114
Chris Lattnere40c2952009-04-14 21:34:55 +00001115
1116/// ParseImplicitInt - This method is called when we have an non-typename
1117/// identifier in a declspec (which normally terminates the decl spec) when
1118/// the declspec has no type specifier. In this case, the declspec is either
1119/// malformed or is "implicit int" (in K&R and C89).
1120///
1121/// This method handles diagnosing this prettily and returns false if the
1122/// declspec is done being processed. If it recovers and thinks there may be
1123/// other pieces of declspec after it, it returns true.
1124///
Chris Lattnerf4382f52009-04-14 22:17:06 +00001125bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001126 const ParsedTemplateInfo &TemplateInfo,
Chris Lattnere40c2952009-04-14 21:34:55 +00001127 AccessSpecifier AS) {
Chris Lattnerf4382f52009-04-14 22:17:06 +00001128 assert(Tok.is(tok::identifier) && "should have identifier");
Mike Stump1eb44332009-09-09 15:08:12 +00001129
Chris Lattnere40c2952009-04-14 21:34:55 +00001130 SourceLocation Loc = Tok.getLocation();
1131 // If we see an identifier that is not a type name, we normally would
1132 // parse it as the identifer being declared. However, when a typename
1133 // is typo'd or the definition is not included, this will incorrectly
1134 // parse the typename as the identifier name and fall over misparsing
1135 // later parts of the diagnostic.
1136 //
1137 // As such, we try to do some look-ahead in cases where this would
1138 // otherwise be an "implicit-int" case to see if this is invalid. For
1139 // example: "static foo_t x = 4;" In this case, if we parsed foo_t as
1140 // an identifier with implicit int, we'd get a parse error because the
1141 // next token is obviously invalid for a type. Parse these as a case
1142 // with an invalid type specifier.
1143 assert(!DS.hasTypeSpecifier() && "Type specifier checked above");
Mike Stump1eb44332009-09-09 15:08:12 +00001144
Chris Lattnere40c2952009-04-14 21:34:55 +00001145 // Since we know that this either implicit int (which is rare) or an
1146 // error, we'd do lookahead to try to do better recovery.
1147 if (isValidAfterIdentifierInDeclarator(NextToken())) {
1148 // If this token is valid for implicit int, e.g. "static x = 4", then
1149 // we just avoid eating the identifier, so it will be parsed as the
1150 // identifier in the declarator.
1151 return false;
1152 }
Mike Stump1eb44332009-09-09 15:08:12 +00001153
Chris Lattnere40c2952009-04-14 21:34:55 +00001154 // Otherwise, if we don't consume this token, we are going to emit an
1155 // error anyway. Try to recover from various common problems. Check
1156 // to see if this was a reference to a tag name without a tag specified.
1157 // This is a common problem in C (saying 'foo' instead of 'struct foo').
Chris Lattnerf4382f52009-04-14 22:17:06 +00001158 //
1159 // C++ doesn't need this, and isTagName doesn't take SS.
1160 if (SS == 0) {
Argyrios Kyrtzidisb8a9d3b2011-04-21 17:29:47 +00001161 const char *TagName = 0, *FixitTagName = 0;
Chris Lattnerf4382f52009-04-14 22:17:06 +00001162 tok::TokenKind TagKind = tok::unknown;
Mike Stump1eb44332009-09-09 15:08:12 +00001163
Douglas Gregor23c94db2010-07-02 17:43:08 +00001164 switch (Actions.isTagName(*Tok.getIdentifierInfo(), getCurScope())) {
Chris Lattnere40c2952009-04-14 21:34:55 +00001165 default: break;
Argyrios Kyrtzidisb8a9d3b2011-04-21 17:29:47 +00001166 case DeclSpec::TST_enum:
1167 TagName="enum" ; FixitTagName = "enum " ; TagKind=tok::kw_enum ;break;
1168 case DeclSpec::TST_union:
1169 TagName="union" ; FixitTagName = "union " ;TagKind=tok::kw_union ;break;
1170 case DeclSpec::TST_struct:
1171 TagName="struct"; FixitTagName = "struct ";TagKind=tok::kw_struct;break;
1172 case DeclSpec::TST_class:
1173 TagName="class" ; FixitTagName = "class " ;TagKind=tok::kw_class ;break;
Chris Lattnere40c2952009-04-14 21:34:55 +00001174 }
Mike Stump1eb44332009-09-09 15:08:12 +00001175
Chris Lattnerf4382f52009-04-14 22:17:06 +00001176 if (TagName) {
1177 Diag(Loc, diag::err_use_of_tag_name_without_tag)
John McCall23e907a2010-02-14 01:03:10 +00001178 << Tok.getIdentifierInfo() << TagName << getLang().CPlusPlus
Argyrios Kyrtzidisb8a9d3b2011-04-21 17:29:47 +00001179 << FixItHint::CreateInsertion(Tok.getLocation(),FixitTagName);
Mike Stump1eb44332009-09-09 15:08:12 +00001180
Chris Lattnerf4382f52009-04-14 22:17:06 +00001181 // Parse this as a tag as if the missing tag were present.
1182 if (TagKind == tok::kw_enum)
Douglas Gregor9b9edd62010-03-02 17:53:14 +00001183 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS);
Chris Lattnerf4382f52009-04-14 22:17:06 +00001184 else
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001185 ParseClassSpecifier(TagKind, Loc, DS, TemplateInfo, AS);
Chris Lattnerf4382f52009-04-14 22:17:06 +00001186 return true;
1187 }
Chris Lattnere40c2952009-04-14 21:34:55 +00001188 }
Mike Stump1eb44332009-09-09 15:08:12 +00001189
Douglas Gregora786fdb2009-10-13 23:27:22 +00001190 // This is almost certainly an invalid type name. Let the action emit a
1191 // diagnostic and attempt to recover.
John McCallb3d87482010-08-24 05:47:05 +00001192 ParsedType T;
Douglas Gregora786fdb2009-10-13 23:27:22 +00001193 if (Actions.DiagnoseUnknownTypeName(*Tok.getIdentifierInfo(), Loc,
Douglas Gregor23c94db2010-07-02 17:43:08 +00001194 getCurScope(), SS, T)) {
Douglas Gregora786fdb2009-10-13 23:27:22 +00001195 // The action emitted a diagnostic, so we don't have to.
1196 if (T) {
1197 // The action has suggested that the type T could be used. Set that as
1198 // the type in the declaration specifiers, consume the would-be type
1199 // name token, and we're done.
1200 const char *PrevSpec;
1201 unsigned DiagID;
John McCallb3d87482010-08-24 05:47:05 +00001202 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID, T);
Douglas Gregora786fdb2009-10-13 23:27:22 +00001203 DS.SetRangeEnd(Tok.getLocation());
1204 ConsumeToken();
1205
1206 // There may be other declaration specifiers after this.
1207 return true;
1208 }
1209
1210 // Fall through; the action had no suggestion for us.
1211 } else {
1212 // The action did not emit a diagnostic, so emit one now.
1213 SourceRange R;
1214 if (SS) R = SS->getRange();
1215 Diag(Loc, diag::err_unknown_typename) << Tok.getIdentifierInfo() << R;
1216 }
Mike Stump1eb44332009-09-09 15:08:12 +00001217
Douglas Gregora786fdb2009-10-13 23:27:22 +00001218 // Mark this as an error.
Chris Lattnere40c2952009-04-14 21:34:55 +00001219 const char *PrevSpec;
John McCallfec54012009-08-03 20:12:06 +00001220 unsigned DiagID;
1221 DS.SetTypeSpecType(DeclSpec::TST_error, Loc, PrevSpec, DiagID);
Chris Lattnere40c2952009-04-14 21:34:55 +00001222 DS.SetRangeEnd(Tok.getLocation());
1223 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001224
Chris Lattnere40c2952009-04-14 21:34:55 +00001225 // TODO: Could inject an invalid typedef decl in an enclosing scope to
1226 // avoid rippling error messages on subsequent uses of the same type,
1227 // could be useful if #include was forgotten.
1228 return false;
1229}
1230
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001231/// \brief Determine the declaration specifier context from the declarator
1232/// context.
1233///
1234/// \param Context the declarator context, which is one of the
1235/// Declarator::TheContext enumerator values.
1236Parser::DeclSpecContext
1237Parser::getDeclSpecContextFromDeclaratorContext(unsigned Context) {
1238 if (Context == Declarator::MemberContext)
1239 return DSC_class;
1240 if (Context == Declarator::FileContext)
1241 return DSC_top_level;
1242 return DSC_normal;
1243}
1244
Reid Spencer5f016e22007-07-11 17:01:13 +00001245/// ParseDeclarationSpecifiers
1246/// declaration-specifiers: [C99 6.7]
1247/// storage-class-specifier declaration-specifiers[opt]
1248/// type-specifier declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00001249/// [C99] function-specifier declaration-specifiers[opt]
1250/// [GNU] attributes declaration-specifiers[opt]
1251///
1252/// storage-class-specifier: [C99 6.7.1]
1253/// 'typedef'
1254/// 'extern'
1255/// 'static'
1256/// 'auto'
1257/// 'register'
Sebastian Redl669d5d72008-11-14 23:42:31 +00001258/// [C++] 'mutable'
Reid Spencer5f016e22007-07-11 17:01:13 +00001259/// [GNU] '__thread'
Reid Spencer5f016e22007-07-11 17:01:13 +00001260/// function-specifier: [C99 6.7.4]
1261/// [C99] 'inline'
Douglas Gregorb48fe382008-10-31 09:07:45 +00001262/// [C++] 'virtual'
1263/// [C++] 'explicit'
Peter Collingbournef315fa82011-02-14 01:42:53 +00001264/// [OpenCL] '__kernel'
Anders Carlssonf47f7a12009-05-06 04:46:28 +00001265/// 'friend': [C++ dcl.friend]
Sebastian Redl2ac67232009-11-05 15:47:02 +00001266/// 'constexpr': [C++0x dcl.constexpr]
Anders Carlssonf47f7a12009-05-06 04:46:28 +00001267
Reid Spencer5f016e22007-07-11 17:01:13 +00001268///
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +00001269void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001270 const ParsedTemplateInfo &TemplateInfo,
John McCall67d1a672009-08-06 02:15:43 +00001271 AccessSpecifier AS,
Douglas Gregor312eadb2011-04-24 05:37:28 +00001272 DeclSpecContext DSContext) {
1273 if (DS.getSourceRange().isInvalid()) {
1274 DS.SetRangeStart(Tok.getLocation());
1275 DS.SetRangeEnd(Tok.getLocation());
1276 }
1277
Reid Spencer5f016e22007-07-11 17:01:13 +00001278 while (1) {
John McCallfec54012009-08-03 20:12:06 +00001279 bool isInvalid = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001280 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00001281 unsigned DiagID = 0;
1282
Reid Spencer5f016e22007-07-11 17:01:13 +00001283 SourceLocation Loc = Tok.getLocation();
Douglas Gregor12e083c2008-11-07 15:42:26 +00001284
Reid Spencer5f016e22007-07-11 17:01:13 +00001285 switch (Tok.getKind()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001286 default:
Chris Lattnerbce61352008-07-26 00:20:22 +00001287 DoneWithDeclSpec:
Reid Spencer5f016e22007-07-11 17:01:13 +00001288 // If this is not a declaration specifier token, we're done reading decl
1289 // specifiers. First verify that DeclSpec's are consistent.
Douglas Gregor9b3064b2009-04-01 22:41:11 +00001290 DS.Finish(Diags, PP);
Reid Spencer5f016e22007-07-11 17:01:13 +00001291 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001292
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001293 case tok::code_completion: {
John McCallf312b1e2010-08-26 23:41:50 +00001294 Sema::ParserCompletionContext CCC = Sema::PCC_Namespace;
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001295 if (DS.hasTypeSpecifier()) {
1296 bool AllowNonIdentifiers
1297 = (getCurScope()->getFlags() & (Scope::ControlScope |
1298 Scope::BlockScope |
1299 Scope::TemplateParamScope |
1300 Scope::FunctionPrototypeScope |
1301 Scope::AtCatchScope)) == 0;
1302 bool AllowNestedNameSpecifiers
1303 = DSContext == DSC_top_level ||
1304 (DSContext == DSC_class && DS.isFriendSpecified());
1305
Douglas Gregorc7b6d882010-09-16 15:14:18 +00001306 Actions.CodeCompleteDeclSpec(getCurScope(), DS,
1307 AllowNonIdentifiers,
1308 AllowNestedNameSpecifiers);
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001309 ConsumeCodeCompletionToken();
1310 return;
1311 }
1312
Douglas Gregor68e3c2e2011-02-15 20:33:25 +00001313 if (getCurScope()->getFnParent() || getCurScope()->getBlockParent())
1314 CCC = Sema::PCC_LocalDeclarationSpecifiers;
1315 else if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate)
John McCallf312b1e2010-08-26 23:41:50 +00001316 CCC = DSContext == DSC_class? Sema::PCC_MemberTemplate
1317 : Sema::PCC_Template;
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001318 else if (DSContext == DSC_class)
John McCallf312b1e2010-08-26 23:41:50 +00001319 CCC = Sema::PCC_Class;
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001320 else if (ObjCImpDecl)
John McCallf312b1e2010-08-26 23:41:50 +00001321 CCC = Sema::PCC_ObjCImplementation;
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001322
1323 Actions.CodeCompleteOrdinaryName(getCurScope(), CCC);
1324 ConsumeCodeCompletionToken();
1325 return;
1326 }
1327
Chris Lattner5e02c472009-01-05 00:07:25 +00001328 case tok::coloncolon: // ::foo::bar
John McCall9ba61662010-02-26 08:45:28 +00001329 // C++ scope specifier. Annotate and loop, or bail out on error.
1330 if (TryAnnotateCXXScopeToken(true)) {
1331 if (!DS.hasTypeSpecifier())
1332 DS.SetTypeSpecError();
1333 goto DoneWithDeclSpec;
1334 }
John McCall2e0a7152010-03-01 18:20:46 +00001335 if (Tok.is(tok::coloncolon)) // ::new or ::delete
1336 goto DoneWithDeclSpec;
John McCall9ba61662010-02-26 08:45:28 +00001337 continue;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001338
1339 case tok::annot_cxxscope: {
1340 if (DS.hasTypeSpecifier())
1341 goto DoneWithDeclSpec;
1342
John McCallaa87d332009-12-12 11:40:51 +00001343 CXXScopeSpec SS;
Douglas Gregorc34348a2011-02-24 17:54:50 +00001344 Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(),
1345 Tok.getAnnotationRange(),
1346 SS);
John McCallaa87d332009-12-12 11:40:51 +00001347
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001348 // We are looking for a qualified typename.
Douglas Gregor9135c722009-03-25 15:40:00 +00001349 Token Next = NextToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001350 if (Next.is(tok::annot_template_id) &&
Douglas Gregor9135c722009-03-25 15:40:00 +00001351 static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue())
Douglas Gregorc45c2322009-03-31 00:43:58 +00001352 ->Kind == TNK_Type_template) {
Douglas Gregor9135c722009-03-25 15:40:00 +00001353 // We have a qualified template-id, e.g., N::A<int>
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001354
1355 // C++ [class.qual]p2:
1356 // In a lookup in which the constructor is an acceptable lookup
1357 // result and the nested-name-specifier nominates a class C:
1358 //
1359 // - if the name specified after the
1360 // nested-name-specifier, when looked up in C, is the
1361 // injected-class-name of C (Clause 9), or
1362 //
1363 // - if the name specified after the nested-name-specifier
1364 // is the same as the identifier or the
1365 // simple-template-id's template-name in the last
1366 // component of the nested-name-specifier,
1367 //
1368 // the name is instead considered to name the constructor of
1369 // class C.
1370 //
1371 // Thus, if the template-name is actually the constructor
1372 // name, then the code is ill-formed; this interpretation is
1373 // reinforced by the NAD status of core issue 635.
1374 TemplateIdAnnotation *TemplateId
1375 = static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue());
John McCallba9d8532010-04-13 06:39:49 +00001376 if ((DSContext == DSC_top_level ||
1377 (DSContext == DSC_class && DS.isFriendSpecified())) &&
1378 TemplateId->Name &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00001379 Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) {
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001380 if (isConstructorDeclarator()) {
1381 // The user meant this to be an out-of-line constructor
1382 // definition, but template arguments are not allowed
1383 // there. Just allow this as a constructor; we'll
1384 // complain about it later.
1385 goto DoneWithDeclSpec;
1386 }
1387
1388 // The user meant this to name a type, but it actually names
1389 // a constructor with some extraneous template
1390 // arguments. Complain, then parse it as a type as the user
1391 // intended.
1392 Diag(TemplateId->TemplateNameLoc,
1393 diag::err_out_of_line_template_id_names_constructor)
1394 << TemplateId->Name;
1395 }
1396
John McCallaa87d332009-12-12 11:40:51 +00001397 DS.getTypeSpecScope() = SS;
1398 ConsumeToken(); // The C++ scope.
Mike Stump1eb44332009-09-09 15:08:12 +00001399 assert(Tok.is(tok::annot_template_id) &&
Douglas Gregor9135c722009-03-25 15:40:00 +00001400 "ParseOptionalCXXScopeSpecifier not working");
Douglas Gregor059101f2011-03-02 00:47:37 +00001401 AnnotateTemplateIdTokenAsType();
Douglas Gregor9135c722009-03-25 15:40:00 +00001402 continue;
1403 }
1404
Douglas Gregor9d7b3532009-09-28 07:26:33 +00001405 if (Next.is(tok::annot_typename)) {
John McCallaa87d332009-12-12 11:40:51 +00001406 DS.getTypeSpecScope() = SS;
1407 ConsumeToken(); // The C++ scope.
John McCallb3d87482010-08-24 05:47:05 +00001408 if (Tok.getAnnotationValue()) {
1409 ParsedType T = getTypeAnnotation(Tok);
Nico Weber253e80b2010-11-22 10:30:56 +00001410 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename,
1411 Tok.getAnnotationEndLoc(),
John McCallb3d87482010-08-24 05:47:05 +00001412 PrevSpec, DiagID, T);
1413 }
Douglas Gregor9d7b3532009-09-28 07:26:33 +00001414 else
1415 DS.SetTypeSpecError();
1416 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1417 ConsumeToken(); // The typename
1418 }
1419
Douglas Gregor9135c722009-03-25 15:40:00 +00001420 if (Next.isNot(tok::identifier))
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001421 goto DoneWithDeclSpec;
1422
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001423 // If we're in a context where the identifier could be a class name,
1424 // check whether this is a constructor declaration.
John McCallba9d8532010-04-13 06:39:49 +00001425 if ((DSContext == DSC_top_level ||
1426 (DSContext == DSC_class && DS.isFriendSpecified())) &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00001427 Actions.isCurrentClassName(*Next.getIdentifierInfo(), getCurScope(),
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001428 &SS)) {
1429 if (isConstructorDeclarator())
1430 goto DoneWithDeclSpec;
1431
1432 // As noted in C++ [class.qual]p2 (cited above), when the name
1433 // of the class is qualified in a context where it could name
1434 // a constructor, its a constructor name. However, we've
1435 // looked at the declarator, and the user probably meant this
1436 // to be a type. Complain that it isn't supposed to be treated
1437 // as a type, then proceed to parse it as a type.
1438 Diag(Next.getLocation(), diag::err_out_of_line_type_names_constructor)
1439 << Next.getIdentifierInfo();
1440 }
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001441
John McCallb3d87482010-08-24 05:47:05 +00001442 ParsedType TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(),
1443 Next.getLocation(),
Douglas Gregor9e876872011-03-01 18:12:44 +00001444 getCurScope(), &SS,
1445 false, false, ParsedType(),
1446 /*NonTrivialSourceInfo=*/true);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001447
Chris Lattnerf4382f52009-04-14 22:17:06 +00001448 // If the referenced identifier is not a type, then this declspec is
1449 // erroneous: We already checked about that it has no type specifier, and
1450 // C++ doesn't have implicit int. Diagnose it as a typo w.r.t. to the
Mike Stump1eb44332009-09-09 15:08:12 +00001451 // typename.
Chris Lattnerf4382f52009-04-14 22:17:06 +00001452 if (TypeRep == 0) {
1453 ConsumeToken(); // Eat the scope spec so the identifier is current.
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001454 if (ParseImplicitInt(DS, &SS, TemplateInfo, AS)) continue;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001455 goto DoneWithDeclSpec;
Chris Lattnerf4382f52009-04-14 22:17:06 +00001456 }
Mike Stump1eb44332009-09-09 15:08:12 +00001457
John McCallaa87d332009-12-12 11:40:51 +00001458 DS.getTypeSpecScope() = SS;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001459 ConsumeToken(); // The C++ scope.
1460
Douglas Gregor1a51b4a2009-02-09 15:09:02 +00001461 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001462 DiagID, TypeRep);
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001463 if (isInvalid)
1464 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001465
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001466 DS.SetRangeEnd(Tok.getLocation());
1467 ConsumeToken(); // The typename.
1468
1469 continue;
1470 }
Mike Stump1eb44332009-09-09 15:08:12 +00001471
Chris Lattner80d0c892009-01-21 19:48:37 +00001472 case tok::annot_typename: {
John McCallb3d87482010-08-24 05:47:05 +00001473 if (Tok.getAnnotationValue()) {
1474 ParsedType T = getTypeAnnotation(Tok);
Nico Weberc43271e2010-11-22 12:50:03 +00001475 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallb3d87482010-08-24 05:47:05 +00001476 DiagID, T);
1477 } else
Douglas Gregor31a19b62009-04-01 21:51:26 +00001478 DS.SetTypeSpecError();
Chris Lattner5c5db552010-04-05 18:18:31 +00001479
1480 if (isInvalid)
1481 break;
1482
Chris Lattner80d0c892009-01-21 19:48:37 +00001483 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1484 ConsumeToken(); // The typename
Mike Stump1eb44332009-09-09 15:08:12 +00001485
Chris Lattner80d0c892009-01-21 19:48:37 +00001486 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1487 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001488 // Objective-C interface.
1489 if (Tok.is(tok::less) && getLang().ObjC1)
1490 ParseObjCProtocolQualifiers(DS);
1491
Chris Lattner80d0c892009-01-21 19:48:37 +00001492 continue;
1493 }
Mike Stump1eb44332009-09-09 15:08:12 +00001494
Douglas Gregorbfad9152011-04-28 15:48:45 +00001495 case tok::kw___is_signed:
1496 // GNU libstdc++ 4.4 uses __is_signed as an identifier, but Clang
1497 // typically treats it as a trait. If we see __is_signed as it appears
1498 // in libstdc++, e.g.,
1499 //
1500 // static const bool __is_signed;
1501 //
1502 // then treat __is_signed as an identifier rather than as a keyword.
1503 if (DS.getTypeSpecType() == TST_bool &&
1504 DS.getTypeQualifiers() == DeclSpec::TQ_const &&
1505 DS.getStorageClassSpec() == DeclSpec::SCS_static) {
1506 Tok.getIdentifierInfo()->RevertTokenIDToIdentifier();
1507 Tok.setKind(tok::identifier);
1508 }
1509
1510 // We're done with the declaration-specifiers.
1511 goto DoneWithDeclSpec;
1512
Chris Lattner3bd934a2008-07-26 01:18:38 +00001513 // typedef-name
1514 case tok::identifier: {
Chris Lattner5e02c472009-01-05 00:07:25 +00001515 // In C++, check to see if this is a scope specifier like foo::bar::, if
1516 // so handle it as such. This is important for ctor parsing.
John McCall9ba61662010-02-26 08:45:28 +00001517 if (getLang().CPlusPlus) {
1518 if (TryAnnotateCXXScopeToken(true)) {
1519 if (!DS.hasTypeSpecifier())
1520 DS.SetTypeSpecError();
1521 goto DoneWithDeclSpec;
1522 }
1523 if (!Tok.is(tok::identifier))
1524 continue;
1525 }
Mike Stump1eb44332009-09-09 15:08:12 +00001526
Chris Lattner3bd934a2008-07-26 01:18:38 +00001527 // This identifier can only be a typedef name if we haven't already seen
1528 // a type-specifier. Without this check we misparse:
1529 // typedef int X; struct Y { short X; }; as 'short int'.
1530 if (DS.hasTypeSpecifier())
1531 goto DoneWithDeclSpec;
Mike Stump1eb44332009-09-09 15:08:12 +00001532
John Thompson82287d12010-02-05 00:12:22 +00001533 // Check for need to substitute AltiVec keyword tokens.
1534 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
1535 break;
1536
Chris Lattner3bd934a2008-07-26 01:18:38 +00001537 // It has to be available as a typedef too!
John McCallb3d87482010-08-24 05:47:05 +00001538 ParsedType TypeRep =
1539 Actions.getTypeName(*Tok.getIdentifierInfo(),
1540 Tok.getLocation(), getCurScope());
Douglas Gregor55f6b142009-02-09 18:46:07 +00001541
Chris Lattnerc199ab32009-04-12 20:42:31 +00001542 // If this is not a typedef name, don't parse it as part of the declspec,
1543 // it must be an implicit int or an error.
John McCallb3d87482010-08-24 05:47:05 +00001544 if (!TypeRep) {
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001545 if (ParseImplicitInt(DS, 0, TemplateInfo, AS)) continue;
Chris Lattner3bd934a2008-07-26 01:18:38 +00001546 goto DoneWithDeclSpec;
Chris Lattnerc199ab32009-04-12 20:42:31 +00001547 }
Douglas Gregor55f6b142009-02-09 18:46:07 +00001548
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001549 // If we're in a context where the identifier could be a class name,
1550 // check whether this is a constructor declaration.
1551 if (getLang().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00001552 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), getCurScope()) &&
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001553 isConstructorDeclarator())
Douglas Gregorb48fe382008-10-31 09:07:45 +00001554 goto DoneWithDeclSpec;
1555
Douglas Gregor1a51b4a2009-02-09 15:09:02 +00001556 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001557 DiagID, TypeRep);
Chris Lattner3bd934a2008-07-26 01:18:38 +00001558 if (isInvalid)
1559 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001560
Chris Lattner3bd934a2008-07-26 01:18:38 +00001561 DS.SetRangeEnd(Tok.getLocation());
1562 ConsumeToken(); // The identifier
1563
1564 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1565 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001566 // Objective-C interface.
1567 if (Tok.is(tok::less) && getLang().ObjC1)
1568 ParseObjCProtocolQualifiers(DS);
1569
Steve Naroff4f9b9f12008-09-22 10:28:57 +00001570 // Need to support trailing type qualifiers (e.g. "id<p> const").
1571 // If a type specifier follows, it will be diagnosed elsewhere.
1572 continue;
Chris Lattner3bd934a2008-07-26 01:18:38 +00001573 }
Douglas Gregor39a8de12009-02-25 19:37:18 +00001574
1575 // type-name
1576 case tok::annot_template_id: {
Mike Stump1eb44332009-09-09 15:08:12 +00001577 TemplateIdAnnotation *TemplateId
Douglas Gregor39a8de12009-02-25 19:37:18 +00001578 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
Douglas Gregorc45c2322009-03-31 00:43:58 +00001579 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor39a8de12009-02-25 19:37:18 +00001580 // This template-id does not refer to a type name, so we're
1581 // done with the type-specifiers.
1582 goto DoneWithDeclSpec;
1583 }
1584
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001585 // If we're in a context where the template-id could be a
1586 // constructor name or specialization, check whether this is a
1587 // constructor declaration.
1588 if (getLang().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00001589 Actions.isCurrentClassName(*TemplateId->Name, getCurScope()) &&
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001590 isConstructorDeclarator())
1591 goto DoneWithDeclSpec;
1592
Douglas Gregor39a8de12009-02-25 19:37:18 +00001593 // Turn the template-id annotation token into a type annotation
1594 // token, then try again to parse it as a type-specifier.
Douglas Gregor31a19b62009-04-01 21:51:26 +00001595 AnnotateTemplateIdTokenAsType();
Douglas Gregor39a8de12009-02-25 19:37:18 +00001596 continue;
1597 }
1598
Reid Spencer5f016e22007-07-11 17:01:13 +00001599 // GNU attributes support.
1600 case tok::kw___attribute:
John McCall7f040a92010-12-24 02:08:15 +00001601 ParseGNUAttributes(DS.getAttributes());
Reid Spencer5f016e22007-07-11 17:01:13 +00001602 continue;
Steve Narofff59e17e2008-12-24 20:59:21 +00001603
1604 // Microsoft declspec support.
1605 case tok::kw___declspec:
John McCall7f040a92010-12-24 02:08:15 +00001606 ParseMicrosoftDeclSpec(DS.getAttributes());
Steve Narofff59e17e2008-12-24 20:59:21 +00001607 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001608
Steve Naroff239f0732008-12-25 14:16:32 +00001609 // Microsoft single token adornments.
Steve Naroff86bc6cf2008-12-25 14:41:26 +00001610 case tok::kw___forceinline:
Eli Friedman290eeb02009-06-08 23:27:34 +00001611 // FIXME: Add handling here!
1612 break;
1613
1614 case tok::kw___ptr64:
Steve Naroff86bc6cf2008-12-25 14:41:26 +00001615 case tok::kw___w64:
Steve Naroff239f0732008-12-25 14:16:32 +00001616 case tok::kw___cdecl:
1617 case tok::kw___stdcall:
1618 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00001619 case tok::kw___thiscall:
John McCall7f040a92010-12-24 02:08:15 +00001620 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman290eeb02009-06-08 23:27:34 +00001621 continue;
1622
Dawn Perchik52fc3142010-09-03 01:29:35 +00001623 // Borland single token adornments.
1624 case tok::kw___pascal:
John McCall7f040a92010-12-24 02:08:15 +00001625 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik52fc3142010-09-03 01:29:35 +00001626 continue;
1627
Peter Collingbournef315fa82011-02-14 01:42:53 +00001628 // OpenCL single token adornments.
1629 case tok::kw___kernel:
1630 ParseOpenCLAttributes(DS.getAttributes());
1631 continue;
1632
Reid Spencer5f016e22007-07-11 17:01:13 +00001633 // storage-class-specifier
1634 case tok::kw_typedef:
John McCallfec54012009-08-03 20:12:06 +00001635 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_typedef, Loc, PrevSpec,
Peter Collingbournee2f82f72011-02-11 19:59:54 +00001636 DiagID, getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00001637 break;
1638 case tok::kw_extern:
1639 if (DS.isThreadSpecified())
Chris Lattner1ab3b962008-11-18 07:48:38 +00001640 Diag(Tok, diag::ext_thread_before) << "extern";
John McCallfec54012009-08-03 20:12:06 +00001641 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_extern, Loc, PrevSpec,
Peter Collingbournee2f82f72011-02-11 19:59:54 +00001642 DiagID, getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00001643 break;
Steve Naroff8d54bf22007-12-18 00:16:02 +00001644 case tok::kw___private_extern__:
Chris Lattnerf97409f2008-04-06 06:57:35 +00001645 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_private_extern, Loc,
Peter Collingbournee2f82f72011-02-11 19:59:54 +00001646 PrevSpec, DiagID, getLang());
Steve Naroff8d54bf22007-12-18 00:16:02 +00001647 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00001648 case tok::kw_static:
1649 if (DS.isThreadSpecified())
Chris Lattner1ab3b962008-11-18 07:48:38 +00001650 Diag(Tok, diag::ext_thread_before) << "static";
John McCallfec54012009-08-03 20:12:06 +00001651 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_static, Loc, PrevSpec,
Peter Collingbournee2f82f72011-02-11 19:59:54 +00001652 DiagID, getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00001653 break;
1654 case tok::kw_auto:
Douglas Gregor18d8b792011-03-14 21:43:30 +00001655 if (getLang().CPlusPlus0x) {
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00001656 if (isKnownToBeTypeSpecifier(GetLookAheadToken(1))) {
1657 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, Loc, PrevSpec,
1658 DiagID, getLang());
1659 if (!isInvalid)
1660 Diag(Tok, diag::auto_storage_class)
1661 << FixItHint::CreateRemoval(DS.getStorageClassSpecLoc());
1662 }
1663 else
1664 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec,
1665 DiagID);
1666 }
Anders Carlssone89d1592009-06-26 18:41:36 +00001667 else
John McCallfec54012009-08-03 20:12:06 +00001668 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, Loc, PrevSpec,
Peter Collingbournee2f82f72011-02-11 19:59:54 +00001669 DiagID, getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00001670 break;
1671 case tok::kw_register:
John McCallfec54012009-08-03 20:12:06 +00001672 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_register, Loc, PrevSpec,
Peter Collingbournee2f82f72011-02-11 19:59:54 +00001673 DiagID, getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00001674 break;
Sebastian Redl669d5d72008-11-14 23:42:31 +00001675 case tok::kw_mutable:
John McCallfec54012009-08-03 20:12:06 +00001676 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_mutable, Loc, PrevSpec,
Peter Collingbournee2f82f72011-02-11 19:59:54 +00001677 DiagID, getLang());
Sebastian Redl669d5d72008-11-14 23:42:31 +00001678 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00001679 case tok::kw___thread:
John McCallfec54012009-08-03 20:12:06 +00001680 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001681 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001682
Reid Spencer5f016e22007-07-11 17:01:13 +00001683 // function-specifier
1684 case tok::kw_inline:
John McCallfec54012009-08-03 20:12:06 +00001685 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001686 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +00001687 case tok::kw_virtual:
John McCallfec54012009-08-03 20:12:06 +00001688 isInvalid = DS.SetFunctionSpecVirtual(Loc, PrevSpec, DiagID);
Douglas Gregorb48fe382008-10-31 09:07:45 +00001689 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +00001690 case tok::kw_explicit:
John McCallfec54012009-08-03 20:12:06 +00001691 isInvalid = DS.SetFunctionSpecExplicit(Loc, PrevSpec, DiagID);
Douglas Gregorb48fe382008-10-31 09:07:45 +00001692 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00001693
Anders Carlssonf47f7a12009-05-06 04:46:28 +00001694 // friend
1695 case tok::kw_friend:
John McCall67d1a672009-08-06 02:15:43 +00001696 if (DSContext == DSC_class)
1697 isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID);
1698 else {
1699 PrevSpec = ""; // not actually used by the diagnostic
1700 DiagID = diag::err_friend_invalid_in_context;
1701 isInvalid = true;
1702 }
Anders Carlssonf47f7a12009-05-06 04:46:28 +00001703 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001704
Sebastian Redl2ac67232009-11-05 15:47:02 +00001705 // constexpr
1706 case tok::kw_constexpr:
1707 isInvalid = DS.SetConstexprSpec(Loc, PrevSpec, DiagID);
1708 break;
1709
Chris Lattner80d0c892009-01-21 19:48:37 +00001710 // type-specifier
1711 case tok::kw_short:
John McCallfec54012009-08-03 20:12:06 +00001712 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec,
1713 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001714 break;
1715 case tok::kw_long:
1716 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCallfec54012009-08-03 20:12:06 +00001717 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
1718 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001719 else
John McCallfec54012009-08-03 20:12:06 +00001720 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
1721 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001722 break;
Francois Pichet338d7f72011-04-28 01:59:37 +00001723 case tok::kw___int64:
1724 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
1725 DiagID);
1726 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00001727 case tok::kw_signed:
John McCallfec54012009-08-03 20:12:06 +00001728 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec,
1729 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001730 break;
1731 case tok::kw_unsigned:
John McCallfec54012009-08-03 20:12:06 +00001732 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
1733 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001734 break;
1735 case tok::kw__Complex:
John McCallfec54012009-08-03 20:12:06 +00001736 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
1737 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001738 break;
1739 case tok::kw__Imaginary:
John McCallfec54012009-08-03 20:12:06 +00001740 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
1741 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001742 break;
1743 case tok::kw_void:
John McCallfec54012009-08-03 20:12:06 +00001744 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec,
1745 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001746 break;
1747 case tok::kw_char:
John McCallfec54012009-08-03 20:12:06 +00001748 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec,
1749 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001750 break;
1751 case tok::kw_int:
John McCallfec54012009-08-03 20:12:06 +00001752 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec,
1753 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001754 break;
1755 case tok::kw_float:
John McCallfec54012009-08-03 20:12:06 +00001756 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec,
1757 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001758 break;
1759 case tok::kw_double:
John McCallfec54012009-08-03 20:12:06 +00001760 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec,
1761 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001762 break;
1763 case tok::kw_wchar_t:
John McCallfec54012009-08-03 20:12:06 +00001764 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec,
1765 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001766 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001767 case tok::kw_char16_t:
John McCallfec54012009-08-03 20:12:06 +00001768 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec,
1769 DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001770 break;
1771 case tok::kw_char32_t:
John McCallfec54012009-08-03 20:12:06 +00001772 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec,
1773 DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001774 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00001775 case tok::kw_bool:
1776 case tok::kw__Bool:
Argyrios Kyrtzidis4383e182010-11-16 18:18:13 +00001777 if (Tok.is(tok::kw_bool) &&
1778 DS.getTypeSpecType() != DeclSpec::TST_unspecified &&
1779 DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
1780 PrevSpec = ""; // Not used by the diagnostic.
1781 DiagID = diag::err_bool_redeclaration;
Fariborz Jahaniane106a0b2011-04-19 21:42:37 +00001782 // For better error recovery.
1783 Tok.setKind(tok::identifier);
Argyrios Kyrtzidis4383e182010-11-16 18:18:13 +00001784 isInvalid = true;
1785 } else {
1786 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec,
1787 DiagID);
1788 }
Chris Lattner80d0c892009-01-21 19:48:37 +00001789 break;
1790 case tok::kw__Decimal32:
John McCallfec54012009-08-03 20:12:06 +00001791 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
1792 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001793 break;
1794 case tok::kw__Decimal64:
John McCallfec54012009-08-03 20:12:06 +00001795 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
1796 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001797 break;
1798 case tok::kw__Decimal128:
John McCallfec54012009-08-03 20:12:06 +00001799 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
1800 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001801 break;
John Thompson82287d12010-02-05 00:12:22 +00001802 case tok::kw___vector:
1803 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
1804 break;
1805 case tok::kw___pixel:
1806 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
1807 break;
John McCalla5fc4722011-04-09 22:50:59 +00001808 case tok::kw___unknown_anytype:
1809 isInvalid = DS.SetTypeSpecType(TST_unknown_anytype, Loc,
1810 PrevSpec, DiagID);
1811 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00001812
1813 // class-specifier:
1814 case tok::kw_class:
1815 case tok::kw_struct:
Chris Lattner4c97d762009-04-12 21:49:30 +00001816 case tok::kw_union: {
1817 tok::TokenKind Kind = Tok.getKind();
1818 ConsumeToken();
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001819 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS);
Chris Lattner80d0c892009-01-21 19:48:37 +00001820 continue;
Chris Lattner4c97d762009-04-12 21:49:30 +00001821 }
Chris Lattner80d0c892009-01-21 19:48:37 +00001822
1823 // enum-specifier:
1824 case tok::kw_enum:
Chris Lattner4c97d762009-04-12 21:49:30 +00001825 ConsumeToken();
Douglas Gregor9b9edd62010-03-02 17:53:14 +00001826 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS);
Chris Lattner80d0c892009-01-21 19:48:37 +00001827 continue;
1828
1829 // cv-qualifier:
1830 case tok::kw_const:
John McCallfec54012009-08-03 20:12:06 +00001831 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec, DiagID,
1832 getLang());
Chris Lattner80d0c892009-01-21 19:48:37 +00001833 break;
1834 case tok::kw_volatile:
John McCallfec54012009-08-03 20:12:06 +00001835 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
1836 getLang());
Chris Lattner80d0c892009-01-21 19:48:37 +00001837 break;
1838 case tok::kw_restrict:
John McCallfec54012009-08-03 20:12:06 +00001839 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
1840 getLang());
Chris Lattner80d0c892009-01-21 19:48:37 +00001841 break;
1842
Douglas Gregord57959a2009-03-27 23:10:48 +00001843 // C++ typename-specifier:
1844 case tok::kw_typename:
John McCall9ba61662010-02-26 08:45:28 +00001845 if (TryAnnotateTypeOrScopeToken()) {
1846 DS.SetTypeSpecError();
1847 goto DoneWithDeclSpec;
1848 }
1849 if (!Tok.is(tok::kw_typename))
Douglas Gregord57959a2009-03-27 23:10:48 +00001850 continue;
1851 break;
1852
Chris Lattner80d0c892009-01-21 19:48:37 +00001853 // GNU typeof support.
1854 case tok::kw_typeof:
1855 ParseTypeofSpecifier(DS);
1856 continue;
1857
Anders Carlsson6fd634f2009-06-24 17:47:40 +00001858 case tok::kw_decltype:
1859 ParseDecltypeSpecifier(DS);
1860 continue;
1861
Sean Huntdb5d44b2011-05-19 05:37:45 +00001862 case tok::kw___underlying_type:
1863 ParseUnderlyingTypeSpecifier(DS);
1864
Peter Collingbourne207f4d82011-03-18 22:38:29 +00001865 // OpenCL qualifiers:
1866 case tok::kw_private:
1867 if (!getLang().OpenCL)
1868 goto DoneWithDeclSpec;
1869 case tok::kw___private:
1870 case tok::kw___global:
1871 case tok::kw___local:
1872 case tok::kw___constant:
1873 case tok::kw___read_only:
1874 case tok::kw___write_only:
1875 case tok::kw___read_write:
1876 ParseOpenCLQualifiers(DS);
1877 break;
1878
Steve Naroffd3ded1f2008-06-05 00:02:44 +00001879 case tok::less:
Chris Lattner3bd934a2008-07-26 01:18:38 +00001880 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattnerbce61352008-07-26 00:20:22 +00001881 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
1882 // but we support it.
Chris Lattner3bd934a2008-07-26 01:18:38 +00001883 if (DS.hasTypeSpecifier() || !getLang().ObjC1)
Chris Lattnerbce61352008-07-26 00:20:22 +00001884 goto DoneWithDeclSpec;
Mike Stump1eb44332009-09-09 15:08:12 +00001885
Douglas Gregor46f936e2010-11-19 17:10:50 +00001886 if (!ParseObjCProtocolQualifiers(DS))
1887 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
1888 << FixItHint::CreateInsertion(Loc, "id")
1889 << SourceRange(Loc, DS.getSourceRange().getEnd());
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001890
1891 // Need to support trailing type qualifiers (e.g. "id<p> const").
1892 // If a type specifier follows, it will be diagnosed elsewhere.
1893 continue;
Reid Spencer5f016e22007-07-11 17:01:13 +00001894 }
John McCallfec54012009-08-03 20:12:06 +00001895 // If the specifier wasn't legal, issue a diagnostic.
Reid Spencer5f016e22007-07-11 17:01:13 +00001896 if (isInvalid) {
1897 assert(PrevSpec && "Method did not return previous specifier!");
John McCallfec54012009-08-03 20:12:06 +00001898 assert(DiagID);
Douglas Gregorae2fb142010-08-23 14:34:43 +00001899
1900 if (DiagID == diag::ext_duplicate_declspec)
1901 Diag(Tok, DiagID)
1902 << PrevSpec << FixItHint::CreateRemoval(Tok.getLocation());
1903 else
1904 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00001905 }
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00001906
Chris Lattner81c018d2008-03-13 06:29:04 +00001907 DS.SetRangeEnd(Tok.getLocation());
Fariborz Jahaniane106a0b2011-04-19 21:42:37 +00001908 if (DiagID != diag::err_bool_redeclaration)
1909 ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00001910 }
1911}
Douglas Gregoradcac882008-12-01 23:54:00 +00001912
Chris Lattner7a0ab5f2009-01-06 06:59:53 +00001913/// ParseOptionalTypeSpecifier - Try to parse a single type-specifier. We
Douglas Gregor12e083c2008-11-07 15:42:26 +00001914/// primarily follow the C++ grammar with additions for C99 and GNU,
1915/// which together subsume the C grammar. Note that the C++
1916/// type-specifier also includes the C type-qualifier (for const,
1917/// volatile, and C99 restrict). Returns true if a type-specifier was
1918/// found (and parsed), false otherwise.
1919///
1920/// type-specifier: [C++ 7.1.5]
1921/// simple-type-specifier
1922/// class-specifier
1923/// enum-specifier
1924/// elaborated-type-specifier [TODO]
1925/// cv-qualifier
1926///
1927/// cv-qualifier: [C++ 7.1.5.1]
1928/// 'const'
1929/// 'volatile'
1930/// [C99] 'restrict'
1931///
1932/// simple-type-specifier: [ C++ 7.1.5.2]
1933/// '::'[opt] nested-name-specifier[opt] type-name [TODO]
1934/// '::'[opt] nested-name-specifier 'template' template-id [TODO]
1935/// 'char'
1936/// 'wchar_t'
1937/// 'bool'
1938/// 'short'
1939/// 'int'
1940/// 'long'
1941/// 'signed'
1942/// 'unsigned'
1943/// 'float'
1944/// 'double'
1945/// 'void'
1946/// [C99] '_Bool'
1947/// [C99] '_Complex'
1948/// [C99] '_Imaginary' // Removed in TC2?
1949/// [GNU] '_Decimal32'
1950/// [GNU] '_Decimal64'
1951/// [GNU] '_Decimal128'
1952/// [GNU] typeof-specifier
1953/// [OBJC] class-name objc-protocol-refs[opt] [TODO]
1954/// [OBJC] typedef-name objc-protocol-refs[opt] [TODO]
Anders Carlsson6fd634f2009-06-24 17:47:40 +00001955/// [C++0x] 'decltype' ( expression )
John Thompson82287d12010-02-05 00:12:22 +00001956/// [AltiVec] '__vector'
John McCallfec54012009-08-03 20:12:06 +00001957bool Parser::ParseOptionalTypeSpecifier(DeclSpec &DS, bool& isInvalid,
Chris Lattner7a0ab5f2009-01-06 06:59:53 +00001958 const char *&PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001959 unsigned &DiagID,
Sebastian Redld9bafa72010-02-03 21:21:43 +00001960 const ParsedTemplateInfo &TemplateInfo,
1961 bool SuppressDeclarations) {
Douglas Gregor12e083c2008-11-07 15:42:26 +00001962 SourceLocation Loc = Tok.getLocation();
1963
1964 switch (Tok.getKind()) {
Chris Lattner166a8fc2009-01-04 23:41:41 +00001965 case tok::identifier: // foo::bar
Douglas Gregorc0b39642010-04-15 23:40:53 +00001966 // If we already have a type specifier, this identifier is not a type.
1967 if (DS.getTypeSpecType() != DeclSpec::TST_unspecified ||
1968 DS.getTypeSpecWidth() != DeclSpec::TSW_unspecified ||
1969 DS.getTypeSpecSign() != DeclSpec::TSS_unspecified)
1970 return false;
John Thompson82287d12010-02-05 00:12:22 +00001971 // Check for need to substitute AltiVec keyword tokens.
1972 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
1973 break;
1974 // Fall through.
Douglas Gregord57959a2009-03-27 23:10:48 +00001975 case tok::kw_typename: // typename foo::bar
Chris Lattner166a8fc2009-01-04 23:41:41 +00001976 // Annotate typenames and C++ scope specifiers. If we get one, just
1977 // recurse to handle whatever we get.
1978 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00001979 return true;
1980 if (Tok.is(tok::identifier))
1981 return false;
1982 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
1983 TemplateInfo, SuppressDeclarations);
Chris Lattner166a8fc2009-01-04 23:41:41 +00001984 case tok::coloncolon: // ::foo::bar
1985 if (NextToken().is(tok::kw_new) || // ::new
1986 NextToken().is(tok::kw_delete)) // ::delete
1987 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001988
Chris Lattner166a8fc2009-01-04 23:41:41 +00001989 // Annotate typenames and C++ scope specifiers. If we get one, just
1990 // recurse to handle whatever we get.
1991 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00001992 return true;
1993 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
1994 TemplateInfo, SuppressDeclarations);
Mike Stump1eb44332009-09-09 15:08:12 +00001995
Douglas Gregor12e083c2008-11-07 15:42:26 +00001996 // simple-type-specifier:
Chris Lattnerb31757b2009-01-06 05:06:21 +00001997 case tok::annot_typename: {
John McCallb3d87482010-08-24 05:47:05 +00001998 if (ParsedType T = getTypeAnnotation(Tok)) {
Nico Weber253e80b2010-11-22 10:30:56 +00001999 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename,
2000 Tok.getAnnotationEndLoc(), PrevSpec,
John McCallb3d87482010-08-24 05:47:05 +00002001 DiagID, T);
2002 } else
Douglas Gregor31a19b62009-04-01 21:51:26 +00002003 DS.SetTypeSpecError();
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002004 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
2005 ConsumeToken(); // The typename
Mike Stump1eb44332009-09-09 15:08:12 +00002006
Douglas Gregor12e083c2008-11-07 15:42:26 +00002007 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
2008 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
2009 // Objective-C interface. If we don't have Objective-C or a '<', this is
2010 // just a normal reference to a typedef name.
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00002011 if (Tok.is(tok::less) && getLang().ObjC1)
2012 ParseObjCProtocolQualifiers(DS);
2013
Douglas Gregor12e083c2008-11-07 15:42:26 +00002014 return true;
2015 }
2016
2017 case tok::kw_short:
John McCallfec54012009-08-03 20:12:06 +00002018 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002019 break;
2020 case tok::kw_long:
2021 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCallfec54012009-08-03 20:12:06 +00002022 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
2023 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002024 else
John McCallfec54012009-08-03 20:12:06 +00002025 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2026 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002027 break;
Francois Pichet338d7f72011-04-28 01:59:37 +00002028 case tok::kw___int64:
2029 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2030 DiagID);
2031 break;
Douglas Gregor12e083c2008-11-07 15:42:26 +00002032 case tok::kw_signed:
John McCallfec54012009-08-03 20:12:06 +00002033 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002034 break;
2035 case tok::kw_unsigned:
John McCallfec54012009-08-03 20:12:06 +00002036 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
2037 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002038 break;
2039 case tok::kw__Complex:
John McCallfec54012009-08-03 20:12:06 +00002040 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
2041 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002042 break;
2043 case tok::kw__Imaginary:
John McCallfec54012009-08-03 20:12:06 +00002044 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
2045 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002046 break;
2047 case tok::kw_void:
John McCallfec54012009-08-03 20:12:06 +00002048 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002049 break;
2050 case tok::kw_char:
John McCallfec54012009-08-03 20:12:06 +00002051 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002052 break;
2053 case tok::kw_int:
John McCallfec54012009-08-03 20:12:06 +00002054 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002055 break;
2056 case tok::kw_float:
John McCallfec54012009-08-03 20:12:06 +00002057 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002058 break;
2059 case tok::kw_double:
John McCallfec54012009-08-03 20:12:06 +00002060 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002061 break;
2062 case tok::kw_wchar_t:
John McCallfec54012009-08-03 20:12:06 +00002063 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002064 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002065 case tok::kw_char16_t:
John McCallfec54012009-08-03 20:12:06 +00002066 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002067 break;
2068 case tok::kw_char32_t:
John McCallfec54012009-08-03 20:12:06 +00002069 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002070 break;
Douglas Gregor12e083c2008-11-07 15:42:26 +00002071 case tok::kw_bool:
2072 case tok::kw__Bool:
John McCallfec54012009-08-03 20:12:06 +00002073 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002074 break;
2075 case tok::kw__Decimal32:
John McCallfec54012009-08-03 20:12:06 +00002076 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
2077 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002078 break;
2079 case tok::kw__Decimal64:
John McCallfec54012009-08-03 20:12:06 +00002080 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
2081 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002082 break;
2083 case tok::kw__Decimal128:
John McCallfec54012009-08-03 20:12:06 +00002084 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
2085 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002086 break;
John Thompson82287d12010-02-05 00:12:22 +00002087 case tok::kw___vector:
2088 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
2089 break;
2090 case tok::kw___pixel:
2091 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
2092 break;
2093
Douglas Gregor12e083c2008-11-07 15:42:26 +00002094 // class-specifier:
2095 case tok::kw_class:
2096 case tok::kw_struct:
Chris Lattner4c97d762009-04-12 21:49:30 +00002097 case tok::kw_union: {
2098 tok::TokenKind Kind = Tok.getKind();
2099 ConsumeToken();
Sebastian Redld9bafa72010-02-03 21:21:43 +00002100 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS_none,
2101 SuppressDeclarations);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002102 return true;
Chris Lattner4c97d762009-04-12 21:49:30 +00002103 }
Douglas Gregor12e083c2008-11-07 15:42:26 +00002104
2105 // enum-specifier:
2106 case tok::kw_enum:
Chris Lattner4c97d762009-04-12 21:49:30 +00002107 ConsumeToken();
Douglas Gregor9b9edd62010-03-02 17:53:14 +00002108 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS_none);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002109 return true;
2110
2111 // cv-qualifier:
2112 case tok::kw_const:
2113 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00002114 DiagID, getLang());
Douglas Gregor12e083c2008-11-07 15:42:26 +00002115 break;
2116 case tok::kw_volatile:
2117 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00002118 DiagID, getLang());
Douglas Gregor12e083c2008-11-07 15:42:26 +00002119 break;
2120 case tok::kw_restrict:
2121 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00002122 DiagID, getLang());
Douglas Gregor12e083c2008-11-07 15:42:26 +00002123 break;
2124
2125 // GNU typeof support.
2126 case tok::kw_typeof:
2127 ParseTypeofSpecifier(DS);
2128 return true;
2129
Anders Carlsson6fd634f2009-06-24 17:47:40 +00002130 // C++0x decltype support.
2131 case tok::kw_decltype:
2132 ParseDecltypeSpecifier(DS);
2133 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00002134
Sean Huntdb5d44b2011-05-19 05:37:45 +00002135 // C++0x type traits support.
2136 case tok::kw___underlying_type:
2137 ParseUnderlyingTypeSpecifier(DS);
2138 return true;
2139
Peter Collingbourne207f4d82011-03-18 22:38:29 +00002140 // OpenCL qualifiers:
2141 case tok::kw_private:
2142 if (!getLang().OpenCL)
2143 return false;
2144 case tok::kw___private:
2145 case tok::kw___global:
2146 case tok::kw___local:
2147 case tok::kw___constant:
2148 case tok::kw___read_only:
2149 case tok::kw___write_only:
2150 case tok::kw___read_write:
2151 ParseOpenCLQualifiers(DS);
2152 break;
2153
Anders Carlsson0b7f7892009-06-26 23:44:14 +00002154 // C++0x auto support.
2155 case tok::kw_auto:
2156 if (!getLang().CPlusPlus0x)
2157 return false;
2158
John McCallfec54012009-08-03 20:12:06 +00002159 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec, DiagID);
Anders Carlsson0b7f7892009-06-26 23:44:14 +00002160 break;
Dawn Perchik52fc3142010-09-03 01:29:35 +00002161
Eli Friedman290eeb02009-06-08 23:27:34 +00002162 case tok::kw___ptr64:
2163 case tok::kw___w64:
Steve Naroff239f0732008-12-25 14:16:32 +00002164 case tok::kw___cdecl:
2165 case tok::kw___stdcall:
2166 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00002167 case tok::kw___thiscall:
John McCall7f040a92010-12-24 02:08:15 +00002168 ParseMicrosoftTypeAttributes(DS.getAttributes());
Chris Lattner837acd02009-01-21 19:19:26 +00002169 return true;
Steve Naroff239f0732008-12-25 14:16:32 +00002170
Dawn Perchik52fc3142010-09-03 01:29:35 +00002171 case tok::kw___pascal:
John McCall7f040a92010-12-24 02:08:15 +00002172 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik52fc3142010-09-03 01:29:35 +00002173 return true;
2174
Douglas Gregor12e083c2008-11-07 15:42:26 +00002175 default:
2176 // Not a type-specifier; do nothing.
2177 return false;
2178 }
2179
2180 // If the specifier combination wasn't legal, issue a diagnostic.
2181 if (isInvalid) {
2182 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner1ab3b962008-11-18 07:48:38 +00002183 // Pick between error or extwarn.
Chris Lattner1ab3b962008-11-18 07:48:38 +00002184 Diag(Tok, DiagID) << PrevSpec;
Douglas Gregor12e083c2008-11-07 15:42:26 +00002185 }
2186 DS.SetRangeEnd(Tok.getLocation());
2187 ConsumeToken(); // whatever we parsed above.
2188 return true;
2189}
Reid Spencer5f016e22007-07-11 17:01:13 +00002190
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002191/// ParseStructDeclaration - Parse a struct declaration without the terminating
2192/// semicolon.
2193///
Reid Spencer5f016e22007-07-11 17:01:13 +00002194/// struct-declaration:
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002195/// specifier-qualifier-list struct-declarator-list
Reid Spencer5f016e22007-07-11 17:01:13 +00002196/// [GNU] __extension__ struct-declaration
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002197/// [GNU] specifier-qualifier-list
Reid Spencer5f016e22007-07-11 17:01:13 +00002198/// struct-declarator-list:
2199/// struct-declarator
2200/// struct-declarator-list ',' struct-declarator
2201/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
2202/// struct-declarator:
2203/// declarator
2204/// [GNU] declarator attributes[opt]
2205/// declarator[opt] ':' constant-expression
2206/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
2207///
Chris Lattnere1359422008-04-10 06:46:29 +00002208void Parser::
John McCallbdd563e2009-11-03 02:38:08 +00002209ParseStructDeclaration(DeclSpec &DS, FieldCallback &Fields) {
Chris Lattnerc46d1a12008-10-20 06:45:43 +00002210 if (Tok.is(tok::kw___extension__)) {
2211 // __extension__ silences extension warnings in the subexpression.
2212 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroff28a7ca82007-08-20 22:28:22 +00002213 ConsumeToken();
Chris Lattnerc46d1a12008-10-20 06:45:43 +00002214 return ParseStructDeclaration(DS, Fields);
2215 }
Mike Stump1eb44332009-09-09 15:08:12 +00002216
Steve Naroff28a7ca82007-08-20 22:28:22 +00002217 // Parse the common specifier-qualifiers-list piece.
Steve Naroff28a7ca82007-08-20 22:28:22 +00002218 ParseSpecifierQualifierList(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00002219
Douglas Gregor4920f1f2009-01-12 22:49:06 +00002220 // If there are no declarators, this is a free-standing declaration
2221 // specifier. Let the actions module cope with it.
Chris Lattner04d66662007-10-09 17:33:22 +00002222 if (Tok.is(tok::semi)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00002223 Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none, DS);
Steve Naroff28a7ca82007-08-20 22:28:22 +00002224 return;
2225 }
2226
2227 // Read struct-declarators until we find the semicolon.
John McCallbdd563e2009-11-03 02:38:08 +00002228 bool FirstDeclarator = true;
Steve Naroff28a7ca82007-08-20 22:28:22 +00002229 while (1) {
John McCall54abf7d2009-11-04 02:18:39 +00002230 ParsingDeclRAIIObject PD(*this);
John McCallbdd563e2009-11-03 02:38:08 +00002231 FieldDeclarator DeclaratorInfo(DS);
2232
2233 // Attributes are only allowed here on successive declarators.
John McCall7f040a92010-12-24 02:08:15 +00002234 if (!FirstDeclarator)
2235 MaybeParseGNUAttributes(DeclaratorInfo.D);
Mike Stump1eb44332009-09-09 15:08:12 +00002236
Steve Naroff28a7ca82007-08-20 22:28:22 +00002237 /// struct-declarator: declarator
2238 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattnera1efc8c2009-12-10 01:59:24 +00002239 if (Tok.isNot(tok::colon)) {
2240 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
2241 ColonProtectionRAIIObject X(*this);
Chris Lattnere1359422008-04-10 06:46:29 +00002242 ParseDeclarator(DeclaratorInfo.D);
Chris Lattnera1efc8c2009-12-10 01:59:24 +00002243 }
Mike Stump1eb44332009-09-09 15:08:12 +00002244
Chris Lattner04d66662007-10-09 17:33:22 +00002245 if (Tok.is(tok::colon)) {
Steve Naroff28a7ca82007-08-20 22:28:22 +00002246 ConsumeToken();
John McCall60d7b3a2010-08-24 06:29:42 +00002247 ExprResult Res(ParseConstantExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002248 if (Res.isInvalid())
Steve Naroff28a7ca82007-08-20 22:28:22 +00002249 SkipUntil(tok::semi, true, true);
Chris Lattner60b1e3e2008-04-10 06:15:14 +00002250 else
Sebastian Redleffa8d12008-12-10 00:02:53 +00002251 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroff28a7ca82007-08-20 22:28:22 +00002252 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00002253
Steve Naroff28a7ca82007-08-20 22:28:22 +00002254 // If attributes exist after the declarator, parse them.
John McCall7f040a92010-12-24 02:08:15 +00002255 MaybeParseGNUAttributes(DeclaratorInfo.D);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002256
John McCallbdd563e2009-11-03 02:38:08 +00002257 // We're done with this declarator; invoke the callback.
John McCalld226f652010-08-21 09:40:31 +00002258 Decl *D = Fields.invoke(DeclaratorInfo);
John McCall54abf7d2009-11-04 02:18:39 +00002259 PD.complete(D);
John McCallbdd563e2009-11-03 02:38:08 +00002260
Steve Naroff28a7ca82007-08-20 22:28:22 +00002261 // If we don't have a comma, it is either the end of the list (a ';')
2262 // or an error, bail out.
Chris Lattner04d66662007-10-09 17:33:22 +00002263 if (Tok.isNot(tok::comma))
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002264 return;
Sebastian Redlab197ba2009-02-09 18:23:29 +00002265
Steve Naroff28a7ca82007-08-20 22:28:22 +00002266 // Consume the comma.
2267 ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00002268
John McCallbdd563e2009-11-03 02:38:08 +00002269 FirstDeclarator = false;
Steve Naroff28a7ca82007-08-20 22:28:22 +00002270 }
Steve Naroff28a7ca82007-08-20 22:28:22 +00002271}
2272
2273/// ParseStructUnionBody
2274/// struct-contents:
2275/// struct-declaration-list
2276/// [EXT] empty
2277/// [GNU] "struct-declaration-list" without terminatoring ';'
2278/// struct-declaration-list:
2279/// struct-declaration
2280/// struct-declaration-list struct-declaration
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002281/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroff28a7ca82007-08-20 22:28:22 +00002282///
Reid Spencer5f016e22007-07-11 17:01:13 +00002283void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
John McCalld226f652010-08-21 09:40:31 +00002284 unsigned TagType, Decl *TagDecl) {
John McCallf312b1e2010-08-26 23:41:50 +00002285 PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc,
2286 "parsing struct/union body");
Mike Stump1eb44332009-09-09 15:08:12 +00002287
Reid Spencer5f016e22007-07-11 17:01:13 +00002288 SourceLocation LBraceLoc = ConsumeBrace();
Mike Stump1eb44332009-09-09 15:08:12 +00002289
Douglas Gregor3218c4b2009-01-09 22:42:13 +00002290 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
Douglas Gregor23c94db2010-07-02 17:43:08 +00002291 Actions.ActOnTagStartDefinition(getCurScope(), TagDecl);
Douglas Gregor72de6672009-01-08 20:45:30 +00002292
Reid Spencer5f016e22007-07-11 17:01:13 +00002293 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
2294 // C++.
Douglas Gregore37ac4f2008-04-13 21:30:24 +00002295 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Douglas Gregor03332962010-07-29 14:29:34 +00002296 Diag(Tok, diag::ext_empty_struct_union)
2297 << (TagType == TST_union);
Reid Spencer5f016e22007-07-11 17:01:13 +00002298
John McCalld226f652010-08-21 09:40:31 +00002299 llvm::SmallVector<Decl *, 32> FieldDecls;
Chris Lattnere1359422008-04-10 06:46:29 +00002300
Reid Spencer5f016e22007-07-11 17:01:13 +00002301 // While we still have something to read, read the declarations in the struct.
Chris Lattner04d66662007-10-09 17:33:22 +00002302 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002303 // Each iteration of this loop reads one struct-declaration.
Mike Stump1eb44332009-09-09 15:08:12 +00002304
Reid Spencer5f016e22007-07-11 17:01:13 +00002305 // Check for extraneous top-level semicolon.
Chris Lattner04d66662007-10-09 17:33:22 +00002306 if (Tok.is(tok::semi)) {
Douglas Gregor9b3064b2009-04-01 22:41:11 +00002307 Diag(Tok, diag::ext_extra_struct_semi)
Douglas Gregorf13ca062010-06-16 23:08:59 +00002308 << DeclSpec::getSpecifierName((DeclSpec::TST)TagType)
Douglas Gregor849b2432010-03-31 17:46:05 +00002309 << FixItHint::CreateRemoval(Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00002310 ConsumeToken();
2311 continue;
2312 }
Chris Lattnere1359422008-04-10 06:46:29 +00002313
2314 // Parse all the comma separated declarators.
John McCall0b7e6782011-03-24 11:26:52 +00002315 DeclSpec DS(AttrFactory);
Mike Stump1eb44332009-09-09 15:08:12 +00002316
John McCallbdd563e2009-11-03 02:38:08 +00002317 if (!Tok.is(tok::at)) {
2318 struct CFieldCallback : FieldCallback {
2319 Parser &P;
John McCalld226f652010-08-21 09:40:31 +00002320 Decl *TagDecl;
2321 llvm::SmallVectorImpl<Decl *> &FieldDecls;
John McCallbdd563e2009-11-03 02:38:08 +00002322
John McCalld226f652010-08-21 09:40:31 +00002323 CFieldCallback(Parser &P, Decl *TagDecl,
2324 llvm::SmallVectorImpl<Decl *> &FieldDecls) :
John McCallbdd563e2009-11-03 02:38:08 +00002325 P(P), TagDecl(TagDecl), FieldDecls(FieldDecls) {}
2326
John McCalld226f652010-08-21 09:40:31 +00002327 virtual Decl *invoke(FieldDeclarator &FD) {
John McCallbdd563e2009-11-03 02:38:08 +00002328 // Install the declarator into the current TagDecl.
John McCalld226f652010-08-21 09:40:31 +00002329 Decl *Field = P.Actions.ActOnField(P.getCurScope(), TagDecl,
John McCall4ba39712009-11-03 21:13:47 +00002330 FD.D.getDeclSpec().getSourceRange().getBegin(),
2331 FD.D, FD.BitfieldSize);
John McCallbdd563e2009-11-03 02:38:08 +00002332 FieldDecls.push_back(Field);
2333 return Field;
Douglas Gregor91a28862009-08-26 14:27:30 +00002334 }
John McCallbdd563e2009-11-03 02:38:08 +00002335 } Callback(*this, TagDecl, FieldDecls);
2336
2337 ParseStructDeclaration(DS, Callback);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002338 } else { // Handle @defs
2339 ConsumeToken();
2340 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
2341 Diag(Tok, diag::err_unexpected_at);
Chris Lattner3e156ad2010-02-02 00:37:27 +00002342 SkipUntil(tok::semi, true);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002343 continue;
2344 }
2345 ConsumeToken();
2346 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
2347 if (!Tok.is(tok::identifier)) {
2348 Diag(Tok, diag::err_expected_ident);
Chris Lattner3e156ad2010-02-02 00:37:27 +00002349 SkipUntil(tok::semi, true);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002350 continue;
2351 }
John McCalld226f652010-08-21 09:40:31 +00002352 llvm::SmallVector<Decl *, 16> Fields;
Douglas Gregor23c94db2010-07-02 17:43:08 +00002353 Actions.ActOnDefs(getCurScope(), TagDecl, Tok.getLocation(),
Douglas Gregor44b43212008-12-11 16:49:14 +00002354 Tok.getIdentifierInfo(), Fields);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002355 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
2356 ConsumeToken();
2357 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
Mike Stump1eb44332009-09-09 15:08:12 +00002358 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002359
Chris Lattner04d66662007-10-09 17:33:22 +00002360 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002361 ConsumeToken();
Chris Lattner04d66662007-10-09 17:33:22 +00002362 } else if (Tok.is(tok::r_brace)) {
Chris Lattner3e156ad2010-02-02 00:37:27 +00002363 ExpectAndConsume(tok::semi, diag::ext_expected_semi_decl_list);
Reid Spencer5f016e22007-07-11 17:01:13 +00002364 break;
2365 } else {
Chris Lattner3e156ad2010-02-02 00:37:27 +00002366 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
2367 // Skip to end of block or statement to avoid ext-warning on extra ';'.
Reid Spencer5f016e22007-07-11 17:01:13 +00002368 SkipUntil(tok::r_brace, true, true);
Chris Lattner3e156ad2010-02-02 00:37:27 +00002369 // If we stopped at a ';', eat it.
2370 if (Tok.is(tok::semi)) ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00002371 }
2372 }
Mike Stump1eb44332009-09-09 15:08:12 +00002373
Steve Naroff60fccee2007-10-29 21:38:07 +00002374 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00002375
John McCall0b7e6782011-03-24 11:26:52 +00002376 ParsedAttributes attrs(AttrFactory);
Reid Spencer5f016e22007-07-11 17:01:13 +00002377 // If attributes exist after struct contents, parse them.
John McCall7f040a92010-12-24 02:08:15 +00002378 MaybeParseGNUAttributes(attrs);
Daniel Dunbar1bfe1c22008-10-03 02:03:53 +00002379
Douglas Gregor23c94db2010-07-02 17:43:08 +00002380 Actions.ActOnFields(getCurScope(),
Jay Foadbeaaccd2009-05-21 09:52:38 +00002381 RecordLoc, TagDecl, FieldDecls.data(), FieldDecls.size(),
Daniel Dunbar1bfe1c22008-10-03 02:03:53 +00002382 LBraceLoc, RBraceLoc,
John McCall7f040a92010-12-24 02:08:15 +00002383 attrs.getList());
Douglas Gregor72de6672009-01-08 20:45:30 +00002384 StructScope.Exit();
Douglas Gregor23c94db2010-07-02 17:43:08 +00002385 Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl, RBraceLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00002386}
2387
Reid Spencer5f016e22007-07-11 17:01:13 +00002388/// ParseEnumSpecifier
2389/// enum-specifier: [C99 6.7.2.2]
2390/// 'enum' identifier[opt] '{' enumerator-list '}'
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002391///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Reid Spencer5f016e22007-07-11 17:01:13 +00002392/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
2393/// '}' attributes[opt]
2394/// 'enum' identifier
2395/// [GNU] 'enum' attributes[opt] identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002396///
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002397/// [C++0x] enum-head '{' enumerator-list[opt] '}'
2398/// [C++0x] enum-head '{' enumerator-list ',' '}'
2399///
2400/// enum-head: [C++0x]
2401/// enum-key attributes[opt] identifier[opt] enum-base[opt]
2402/// enum-key attributes[opt] nested-name-specifier identifier enum-base[opt]
2403///
2404/// enum-key: [C++0x]
2405/// 'enum'
2406/// 'enum' 'class'
2407/// 'enum' 'struct'
2408///
2409/// enum-base: [C++0x]
2410/// ':' type-specifier-seq
2411///
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002412/// [C++] elaborated-type-specifier:
2413/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
2414///
Chris Lattner4c97d762009-04-12 21:49:30 +00002415void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregor9b9edd62010-03-02 17:53:14 +00002416 const ParsedTemplateInfo &TemplateInfo,
Chris Lattner4c97d762009-04-12 21:49:30 +00002417 AccessSpecifier AS) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002418 // Parse the tag portion of this.
Douglas Gregor374929f2009-09-18 15:37:17 +00002419 if (Tok.is(tok::code_completion)) {
2420 // Code completion for an enum name.
Douglas Gregor23c94db2010-07-02 17:43:08 +00002421 Actions.CodeCompleteTag(getCurScope(), DeclSpec::TST_enum);
Douglas Gregordc845342010-05-25 05:58:43 +00002422 ConsumeCodeCompletionToken();
Douglas Gregor374929f2009-09-18 15:37:17 +00002423 }
2424
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002425 // If attributes exist after tag, parse them.
John McCall0b7e6782011-03-24 11:26:52 +00002426 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00002427 MaybeParseGNUAttributes(attrs);
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002428
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00002429 CXXScopeSpec &SS = DS.getTypeSpecScope();
John McCall9ba61662010-02-26 08:45:28 +00002430 if (getLang().CPlusPlus) {
John McCallb3d87482010-08-24 05:47:05 +00002431 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), false))
John McCall9ba61662010-02-26 08:45:28 +00002432 return;
2433
2434 if (SS.isSet() && Tok.isNot(tok::identifier)) {
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002435 Diag(Tok, diag::err_expected_ident);
2436 if (Tok.isNot(tok::l_brace)) {
2437 // Has no name and is not a definition.
2438 // Skip the rest of this declarator, up until the comma or semicolon.
2439 SkipUntil(tok::comma, true);
2440 return;
2441 }
2442 }
2443 }
Mike Stump1eb44332009-09-09 15:08:12 +00002444
Douglas Gregor86f208c2011-02-22 20:32:04 +00002445 bool AllowFixedUnderlyingType = getLang().CPlusPlus0x || getLang().Microsoft;
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002446 bool IsScopedEnum = false;
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00002447 bool IsScopedUsingClassTag = false;
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002448
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00002449 if (getLang().CPlusPlus0x &&
2450 (Tok.is(tok::kw_class) || Tok.is(tok::kw_struct))) {
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002451 IsScopedEnum = true;
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00002452 IsScopedUsingClassTag = Tok.is(tok::kw_class);
2453 ConsumeToken();
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002454 }
2455
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002456 // Must have either 'enum name' or 'enum {...}'.
Douglas Gregorb9075602011-02-22 02:55:24 +00002457 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace) &&
2458 (AllowFixedUnderlyingType && Tok.isNot(tok::colon))) {
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002459 Diag(Tok, diag::err_expected_ident_lbrace);
Mike Stump1eb44332009-09-09 15:08:12 +00002460
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002461 // Skip the rest of this declarator, up until the comma or semicolon.
2462 SkipUntil(tok::comma, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00002463 return;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002464 }
Mike Stump1eb44332009-09-09 15:08:12 +00002465
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002466 // If an identifier is present, consume and remember it.
2467 IdentifierInfo *Name = 0;
2468 SourceLocation NameLoc;
2469 if (Tok.is(tok::identifier)) {
2470 Name = Tok.getIdentifierInfo();
2471 NameLoc = ConsumeToken();
2472 }
Mike Stump1eb44332009-09-09 15:08:12 +00002473
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002474 if (!Name && IsScopedEnum) {
2475 // C++0x 7.2p2: The optional identifier shall not be omitted in the
2476 // declaration of a scoped enumeration.
2477 Diag(Tok, diag::err_scoped_enum_missing_identifier);
2478 IsScopedEnum = false;
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00002479 IsScopedUsingClassTag = false;
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002480 }
2481
2482 TypeResult BaseType;
2483
Douglas Gregora61b3e72010-12-01 17:42:47 +00002484 // Parse the fixed underlying type.
Douglas Gregorb9075602011-02-22 02:55:24 +00002485 if (AllowFixedUnderlyingType && Tok.is(tok::colon)) {
Douglas Gregora61b3e72010-12-01 17:42:47 +00002486 bool PossibleBitfield = false;
2487 if (getCurScope()->getFlags() & Scope::ClassScope) {
2488 // If we're in class scope, this can either be an enum declaration with
2489 // an underlying type, or a declaration of a bitfield member. We try to
2490 // use a simple disambiguation scheme first to catch the common cases
2491 // (integer literal, sizeof); if it's still ambiguous, we then consider
2492 // anything that's a simple-type-specifier followed by '(' as an
2493 // expression. This suffices because function types are not valid
2494 // underlying types anyway.
2495 TPResult TPR = isExpressionOrTypeSpecifierSimple(NextToken().getKind());
2496 // If the next token starts an expression, we know we're parsing a
2497 // bit-field. This is the common case.
2498 if (TPR == TPResult::True())
2499 PossibleBitfield = true;
2500 // If the next token starts a type-specifier-seq, it may be either a
2501 // a fixed underlying type or the start of a function-style cast in C++;
2502 // lookahead one more token to see if it's obvious that we have a
2503 // fixed underlying type.
2504 else if (TPR == TPResult::False() &&
2505 GetLookAheadToken(2).getKind() == tok::semi) {
2506 // Consume the ':'.
2507 ConsumeToken();
2508 } else {
2509 // We have the start of a type-specifier-seq, so we have to perform
2510 // tentative parsing to determine whether we have an expression or a
2511 // type.
2512 TentativeParsingAction TPA(*this);
2513
2514 // Consume the ':'.
2515 ConsumeToken();
2516
Douglas Gregor86f208c2011-02-22 20:32:04 +00002517 if ((getLang().CPlusPlus &&
2518 isCXXDeclarationSpecifier() != TPResult::True()) ||
2519 (!getLang().CPlusPlus && !isDeclarationSpecifier(true))) {
Douglas Gregora61b3e72010-12-01 17:42:47 +00002520 // We'll parse this as a bitfield later.
2521 PossibleBitfield = true;
2522 TPA.Revert();
2523 } else {
2524 // We have a type-specifier-seq.
2525 TPA.Commit();
2526 }
2527 }
2528 } else {
2529 // Consume the ':'.
2530 ConsumeToken();
2531 }
2532
2533 if (!PossibleBitfield) {
2534 SourceRange Range;
2535 BaseType = ParseTypeName(&Range);
Douglas Gregor86f208c2011-02-22 20:32:04 +00002536
2537 if (!getLang().CPlusPlus0x)
2538 Diag(StartLoc, diag::ext_ms_enum_fixed_underlying_type)
2539 << Range;
Douglas Gregora61b3e72010-12-01 17:42:47 +00002540 }
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002541 }
2542
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002543 // There are three options here. If we have 'enum foo;', then this is a
2544 // forward declaration. If we have 'enum foo {...' then this is a
2545 // definition. Otherwise we have something like 'enum foo xyz', a reference.
2546 //
2547 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
2548 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
2549 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
2550 //
John McCallf312b1e2010-08-26 23:41:50 +00002551 Sema::TagUseKind TUK;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002552 if (Tok.is(tok::l_brace))
John McCallf312b1e2010-08-26 23:41:50 +00002553 TUK = Sema::TUK_Definition;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002554 else if (Tok.is(tok::semi))
John McCallf312b1e2010-08-26 23:41:50 +00002555 TUK = Sema::TUK_Declaration;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002556 else
John McCallf312b1e2010-08-26 23:41:50 +00002557 TUK = Sema::TUK_Reference;
Douglas Gregor8fc6d232010-05-03 17:48:54 +00002558
2559 // enums cannot be templates, although they can be referenced from a
2560 // template.
2561 if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate &&
John McCallf312b1e2010-08-26 23:41:50 +00002562 TUK != Sema::TUK_Reference) {
Douglas Gregor8fc6d232010-05-03 17:48:54 +00002563 Diag(Tok, diag::err_enum_template);
2564
2565 // Skip the rest of this declarator, up until the comma or semicolon.
2566 SkipUntil(tok::comma, true);
2567 return;
2568 }
2569
Douglas Gregorb9075602011-02-22 02:55:24 +00002570 if (!Name && TUK != Sema::TUK_Definition) {
2571 Diag(Tok, diag::err_enumerator_unnamed_no_def);
2572
2573 // Skip the rest of this declarator, up until the comma or semicolon.
2574 SkipUntil(tok::comma, true);
2575 return;
2576 }
2577
Douglas Gregor402abb52009-05-28 23:31:59 +00002578 bool Owned = false;
John McCallc4e70192009-09-11 04:59:25 +00002579 bool IsDependent = false;
Douglas Gregor48c89f42010-04-24 16:38:41 +00002580 const char *PrevSpec = 0;
2581 unsigned DiagID;
John McCalld226f652010-08-21 09:40:31 +00002582 Decl *TagDecl = Actions.ActOnTag(getCurScope(), DeclSpec::TST_enum, TUK,
John McCall7f040a92010-12-24 02:08:15 +00002583 StartLoc, SS, Name, NameLoc, attrs.getList(),
John McCalld226f652010-08-21 09:40:31 +00002584 AS,
John McCallf312b1e2010-08-26 23:41:50 +00002585 MultiTemplateParamsArg(Actions),
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002586 Owned, IsDependent, IsScopedEnum,
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00002587 IsScopedUsingClassTag, BaseType);
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002588
Douglas Gregor48c89f42010-04-24 16:38:41 +00002589 if (IsDependent) {
2590 // This enum has a dependent nested-name-specifier. Handle it as a
2591 // dependent tag.
2592 if (!Name) {
2593 DS.SetTypeSpecError();
2594 Diag(Tok, diag::err_expected_type_name_after_typename);
2595 return;
2596 }
2597
Douglas Gregor23c94db2010-07-02 17:43:08 +00002598 TypeResult Type = Actions.ActOnDependentTag(getCurScope(), DeclSpec::TST_enum,
Douglas Gregor48c89f42010-04-24 16:38:41 +00002599 TUK, SS, Name, StartLoc,
2600 NameLoc);
2601 if (Type.isInvalid()) {
2602 DS.SetTypeSpecError();
2603 return;
2604 }
2605
Abramo Bagnara0daaf322011-03-16 20:16:18 +00002606 if (DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc,
2607 NameLoc.isValid() ? NameLoc : StartLoc,
2608 PrevSpec, DiagID, Type.get()))
Douglas Gregor48c89f42010-04-24 16:38:41 +00002609 Diag(StartLoc, DiagID) << PrevSpec;
2610
2611 return;
2612 }
Mike Stump1eb44332009-09-09 15:08:12 +00002613
John McCalld226f652010-08-21 09:40:31 +00002614 if (!TagDecl) {
Douglas Gregor48c89f42010-04-24 16:38:41 +00002615 // The action failed to produce an enumeration tag. If this is a
2616 // definition, consume the entire definition.
2617 if (Tok.is(tok::l_brace)) {
2618 ConsumeBrace();
2619 SkipUntil(tok::r_brace);
2620 }
2621
2622 DS.SetTypeSpecError();
2623 return;
2624 }
2625
Chris Lattner04d66662007-10-09 17:33:22 +00002626 if (Tok.is(tok::l_brace))
Reid Spencer5f016e22007-07-11 17:01:13 +00002627 ParseEnumBody(StartLoc, TagDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00002628
Abramo Bagnara0daaf322011-03-16 20:16:18 +00002629 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc,
2630 NameLoc.isValid() ? NameLoc : StartLoc,
2631 PrevSpec, DiagID, TagDecl, Owned))
John McCallfec54012009-08-03 20:12:06 +00002632 Diag(StartLoc, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00002633}
2634
2635/// ParseEnumBody - Parse a {} enclosed enumerator-list.
2636/// enumerator-list:
2637/// enumerator
2638/// enumerator-list ',' enumerator
2639/// enumerator:
2640/// enumeration-constant
2641/// enumeration-constant '=' constant-expression
2642/// enumeration-constant:
2643/// identifier
2644///
John McCalld226f652010-08-21 09:40:31 +00002645void Parser::ParseEnumBody(SourceLocation StartLoc, Decl *EnumDecl) {
Douglas Gregor074149e2009-01-05 19:45:36 +00002646 // Enter the scope of the enum body and start the definition.
2647 ParseScope EnumScope(this, Scope::DeclScope);
Douglas Gregor23c94db2010-07-02 17:43:08 +00002648 Actions.ActOnTagStartDefinition(getCurScope(), EnumDecl);
Douglas Gregor074149e2009-01-05 19:45:36 +00002649
Reid Spencer5f016e22007-07-11 17:01:13 +00002650 SourceLocation LBraceLoc = ConsumeBrace();
Mike Stump1eb44332009-09-09 15:08:12 +00002651
Chris Lattner7946dd32007-08-27 17:24:30 +00002652 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
Chris Lattner04d66662007-10-09 17:33:22 +00002653 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Fariborz Jahanian05115522010-05-28 22:23:22 +00002654 Diag(Tok, diag::error_empty_enum);
Mike Stump1eb44332009-09-09 15:08:12 +00002655
John McCalld226f652010-08-21 09:40:31 +00002656 llvm::SmallVector<Decl *, 32> EnumConstantDecls;
Reid Spencer5f016e22007-07-11 17:01:13 +00002657
John McCalld226f652010-08-21 09:40:31 +00002658 Decl *LastEnumConstDecl = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00002659
Reid Spencer5f016e22007-07-11 17:01:13 +00002660 // Parse the enumerator-list.
Chris Lattner04d66662007-10-09 17:33:22 +00002661 while (Tok.is(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002662 IdentifierInfo *Ident = Tok.getIdentifierInfo();
2663 SourceLocation IdentLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00002664
John McCall5b629aa2010-10-22 23:36:17 +00002665 // If attributes exist after the enumerator, parse them.
John McCall0b7e6782011-03-24 11:26:52 +00002666 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00002667 MaybeParseGNUAttributes(attrs);
John McCall5b629aa2010-10-22 23:36:17 +00002668
Reid Spencer5f016e22007-07-11 17:01:13 +00002669 SourceLocation EqualLoc;
John McCall60d7b3a2010-08-24 06:29:42 +00002670 ExprResult AssignedVal;
Chris Lattner04d66662007-10-09 17:33:22 +00002671 if (Tok.is(tok::equal)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002672 EqualLoc = ConsumeToken();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002673 AssignedVal = ParseConstantExpression();
2674 if (AssignedVal.isInvalid())
Reid Spencer5f016e22007-07-11 17:01:13 +00002675 SkipUntil(tok::comma, tok::r_brace, true, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00002676 }
Mike Stump1eb44332009-09-09 15:08:12 +00002677
Reid Spencer5f016e22007-07-11 17:01:13 +00002678 // Install the enumerator constant into EnumDecl.
John McCalld226f652010-08-21 09:40:31 +00002679 Decl *EnumConstDecl = Actions.ActOnEnumConstant(getCurScope(), EnumDecl,
2680 LastEnumConstDecl,
2681 IdentLoc, Ident,
John McCall7f040a92010-12-24 02:08:15 +00002682 attrs.getList(), EqualLoc,
John McCalld226f652010-08-21 09:40:31 +00002683 AssignedVal.release());
Reid Spencer5f016e22007-07-11 17:01:13 +00002684 EnumConstantDecls.push_back(EnumConstDecl);
2685 LastEnumConstDecl = EnumConstDecl;
Mike Stump1eb44332009-09-09 15:08:12 +00002686
Douglas Gregor751f6922010-09-07 14:51:08 +00002687 if (Tok.is(tok::identifier)) {
2688 // We're missing a comma between enumerators.
2689 SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation);
2690 Diag(Loc, diag::err_enumerator_list_missing_comma)
2691 << FixItHint::CreateInsertion(Loc, ", ");
2692 continue;
2693 }
2694
Chris Lattner04d66662007-10-09 17:33:22 +00002695 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +00002696 break;
2697 SourceLocation CommaLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00002698
2699 if (Tok.isNot(tok::identifier) &&
Douglas Gregor9b3064b2009-04-01 22:41:11 +00002700 !(getLang().C99 || getLang().CPlusPlus0x))
2701 Diag(CommaLoc, diag::ext_enumerator_list_comma)
2702 << getLang().CPlusPlus
Douglas Gregor849b2432010-03-31 17:46:05 +00002703 << FixItHint::CreateRemoval(CommaLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00002704 }
Mike Stump1eb44332009-09-09 15:08:12 +00002705
Reid Spencer5f016e22007-07-11 17:01:13 +00002706 // Eat the }.
Mike Stumpc6e35aa2009-05-16 07:06:02 +00002707 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00002708
Reid Spencer5f016e22007-07-11 17:01:13 +00002709 // If attributes exist after the identifier list, parse them.
John McCall0b7e6782011-03-24 11:26:52 +00002710 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00002711 MaybeParseGNUAttributes(attrs);
Douglas Gregor72de6672009-01-08 20:45:30 +00002712
Edward O'Callaghanfee13812009-08-08 14:36:57 +00002713 Actions.ActOnEnumBody(StartLoc, LBraceLoc, RBraceLoc, EnumDecl,
2714 EnumConstantDecls.data(), EnumConstantDecls.size(),
John McCall7f040a92010-12-24 02:08:15 +00002715 getCurScope(), attrs.getList());
Mike Stump1eb44332009-09-09 15:08:12 +00002716
Douglas Gregor72de6672009-01-08 20:45:30 +00002717 EnumScope.Exit();
Douglas Gregor23c94db2010-07-02 17:43:08 +00002718 Actions.ActOnTagFinishDefinition(getCurScope(), EnumDecl, RBraceLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00002719}
2720
2721/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff5f8aa692008-02-11 23:15:56 +00002722/// start of a type-qualifier-list.
2723bool Parser::isTypeQualifier() const {
2724 switch (Tok.getKind()) {
2725 default: return false;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00002726
2727 // type-qualifier only in OpenCL
2728 case tok::kw_private:
2729 return getLang().OpenCL;
2730
Steve Naroff5f8aa692008-02-11 23:15:56 +00002731 // type-qualifier
2732 case tok::kw_const:
2733 case tok::kw_volatile:
2734 case tok::kw_restrict:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00002735 case tok::kw___private:
2736 case tok::kw___local:
2737 case tok::kw___global:
2738 case tok::kw___constant:
2739 case tok::kw___read_only:
2740 case tok::kw___read_write:
2741 case tok::kw___write_only:
Steve Naroff5f8aa692008-02-11 23:15:56 +00002742 return true;
2743 }
2744}
2745
Chris Lattnerb3a4e432010-02-28 18:18:36 +00002746/// isKnownToBeTypeSpecifier - Return true if we know that the specified token
2747/// is definitely a type-specifier. Return false if it isn't part of a type
2748/// specifier or if we're not sure.
2749bool Parser::isKnownToBeTypeSpecifier(const Token &Tok) const {
2750 switch (Tok.getKind()) {
2751 default: return false;
2752 // type-specifiers
2753 case tok::kw_short:
2754 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00002755 case tok::kw___int64:
Chris Lattnerb3a4e432010-02-28 18:18:36 +00002756 case tok::kw_signed:
2757 case tok::kw_unsigned:
2758 case tok::kw__Complex:
2759 case tok::kw__Imaginary:
2760 case tok::kw_void:
2761 case tok::kw_char:
2762 case tok::kw_wchar_t:
2763 case tok::kw_char16_t:
2764 case tok::kw_char32_t:
2765 case tok::kw_int:
2766 case tok::kw_float:
2767 case tok::kw_double:
2768 case tok::kw_bool:
2769 case tok::kw__Bool:
2770 case tok::kw__Decimal32:
2771 case tok::kw__Decimal64:
2772 case tok::kw__Decimal128:
2773 case tok::kw___vector:
2774
2775 // struct-or-union-specifier (C99) or class-specifier (C++)
2776 case tok::kw_class:
2777 case tok::kw_struct:
2778 case tok::kw_union:
2779 // enum-specifier
2780 case tok::kw_enum:
2781
2782 // typedef-name
2783 case tok::annot_typename:
2784 return true;
2785 }
2786}
2787
Steve Naroff5f8aa692008-02-11 23:15:56 +00002788/// isTypeSpecifierQualifier - Return true if the current token could be the
Reid Spencer5f016e22007-07-11 17:01:13 +00002789/// start of a specifier-qualifier-list.
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002790bool Parser::isTypeSpecifierQualifier() {
Reid Spencer5f016e22007-07-11 17:01:13 +00002791 switch (Tok.getKind()) {
2792 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002793
Chris Lattner166a8fc2009-01-04 23:41:41 +00002794 case tok::identifier: // foo::bar
John Thompson82287d12010-02-05 00:12:22 +00002795 if (TryAltiVecVectorToken())
2796 return true;
2797 // Fall through.
Douglas Gregord57959a2009-03-27 23:10:48 +00002798 case tok::kw_typename: // typename T::type
Chris Lattner166a8fc2009-01-04 23:41:41 +00002799 // Annotate typenames and C++ scope specifiers. If we get one, just
2800 // recurse to handle whatever we get.
2801 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00002802 return true;
2803 if (Tok.is(tok::identifier))
2804 return false;
2805 return isTypeSpecifierQualifier();
Douglas Gregord57959a2009-03-27 23:10:48 +00002806
Chris Lattner166a8fc2009-01-04 23:41:41 +00002807 case tok::coloncolon: // ::foo::bar
2808 if (NextToken().is(tok::kw_new) || // ::new
2809 NextToken().is(tok::kw_delete)) // ::delete
2810 return false;
2811
Chris Lattner166a8fc2009-01-04 23:41:41 +00002812 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00002813 return true;
2814 return isTypeSpecifierQualifier();
Mike Stump1eb44332009-09-09 15:08:12 +00002815
Reid Spencer5f016e22007-07-11 17:01:13 +00002816 // GNU attributes support.
2817 case tok::kw___attribute:
Steve Naroffd1861fd2007-07-31 12:34:36 +00002818 // GNU typeof support.
2819 case tok::kw_typeof:
Mike Stump1eb44332009-09-09 15:08:12 +00002820
Reid Spencer5f016e22007-07-11 17:01:13 +00002821 // type-specifiers
2822 case tok::kw_short:
2823 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00002824 case tok::kw___int64:
Reid Spencer5f016e22007-07-11 17:01:13 +00002825 case tok::kw_signed:
2826 case tok::kw_unsigned:
2827 case tok::kw__Complex:
2828 case tok::kw__Imaginary:
2829 case tok::kw_void:
2830 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00002831 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002832 case tok::kw_char16_t:
2833 case tok::kw_char32_t:
Reid Spencer5f016e22007-07-11 17:01:13 +00002834 case tok::kw_int:
2835 case tok::kw_float:
2836 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00002837 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00002838 case tok::kw__Bool:
2839 case tok::kw__Decimal32:
2840 case tok::kw__Decimal64:
2841 case tok::kw__Decimal128:
John Thompson82287d12010-02-05 00:12:22 +00002842 case tok::kw___vector:
Mike Stump1eb44332009-09-09 15:08:12 +00002843
Chris Lattner99dc9142008-04-13 18:59:07 +00002844 // struct-or-union-specifier (C99) or class-specifier (C++)
2845 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00002846 case tok::kw_struct:
2847 case tok::kw_union:
2848 // enum-specifier
2849 case tok::kw_enum:
Mike Stump1eb44332009-09-09 15:08:12 +00002850
Reid Spencer5f016e22007-07-11 17:01:13 +00002851 // type-qualifier
2852 case tok::kw_const:
2853 case tok::kw_volatile:
2854 case tok::kw_restrict:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002855
2856 // typedef-name
Chris Lattnerb31757b2009-01-06 05:06:21 +00002857 case tok::annot_typename:
Reid Spencer5f016e22007-07-11 17:01:13 +00002858 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00002859
Chris Lattner7c186be2008-10-20 00:25:30 +00002860 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
2861 case tok::less:
2862 return getLang().ObjC1;
Mike Stump1eb44332009-09-09 15:08:12 +00002863
Steve Naroff239f0732008-12-25 14:16:32 +00002864 case tok::kw___cdecl:
2865 case tok::kw___stdcall:
2866 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00002867 case tok::kw___thiscall:
Eli Friedman290eeb02009-06-08 23:27:34 +00002868 case tok::kw___w64:
2869 case tok::kw___ptr64:
Dawn Perchik52fc3142010-09-03 01:29:35 +00002870 case tok::kw___pascal:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00002871
2872 case tok::kw___private:
2873 case tok::kw___local:
2874 case tok::kw___global:
2875 case tok::kw___constant:
2876 case tok::kw___read_only:
2877 case tok::kw___read_write:
2878 case tok::kw___write_only:
2879
Eli Friedman290eeb02009-06-08 23:27:34 +00002880 return true;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00002881
2882 case tok::kw_private:
2883 return getLang().OpenCL;
Reid Spencer5f016e22007-07-11 17:01:13 +00002884 }
2885}
2886
2887/// isDeclarationSpecifier() - Return true if the current token is part of a
2888/// declaration specifier.
Douglas Gregor9497a732010-09-16 01:51:54 +00002889///
2890/// \param DisambiguatingWithExpression True to indicate that the purpose of
2891/// this check is to disambiguate between an expression and a declaration.
2892bool Parser::isDeclarationSpecifier(bool DisambiguatingWithExpression) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002893 switch (Tok.getKind()) {
2894 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002895
Peter Collingbourne207f4d82011-03-18 22:38:29 +00002896 case tok::kw_private:
2897 return getLang().OpenCL;
2898
Chris Lattner166a8fc2009-01-04 23:41:41 +00002899 case tok::identifier: // foo::bar
Steve Naroff61f72cb2009-03-09 21:12:44 +00002900 // Unfortunate hack to support "Class.factoryMethod" notation.
2901 if (getLang().ObjC1 && NextToken().is(tok::period))
2902 return false;
John Thompson82287d12010-02-05 00:12:22 +00002903 if (TryAltiVecVectorToken())
2904 return true;
2905 // Fall through.
Douglas Gregord57959a2009-03-27 23:10:48 +00002906 case tok::kw_typename: // typename T::type
Chris Lattner166a8fc2009-01-04 23:41:41 +00002907 // Annotate typenames and C++ scope specifiers. If we get one, just
2908 // recurse to handle whatever we get.
2909 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00002910 return true;
2911 if (Tok.is(tok::identifier))
2912 return false;
Douglas Gregor9497a732010-09-16 01:51:54 +00002913
2914 // If we're in Objective-C and we have an Objective-C class type followed
2915 // by an identifier and then either ':' or ']', in a place where an
2916 // expression is permitted, then this is probably a class message send
2917 // missing the initial '['. In this case, we won't consider this to be
2918 // the start of a declaration.
2919 if (DisambiguatingWithExpression &&
2920 isStartOfObjCClassMessageMissingOpenBracket())
2921 return false;
2922
John McCall9ba61662010-02-26 08:45:28 +00002923 return isDeclarationSpecifier();
2924
Chris Lattner166a8fc2009-01-04 23:41:41 +00002925 case tok::coloncolon: // ::foo::bar
2926 if (NextToken().is(tok::kw_new) || // ::new
2927 NextToken().is(tok::kw_delete)) // ::delete
2928 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002929
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 return isDeclarationSpecifier();
Mike Stump1eb44332009-09-09 15:08:12 +00002935
Reid Spencer5f016e22007-07-11 17:01:13 +00002936 // storage-class-specifier
2937 case tok::kw_typedef:
2938 case tok::kw_extern:
Steve Naroff8d54bf22007-12-18 00:16:02 +00002939 case tok::kw___private_extern__:
Reid Spencer5f016e22007-07-11 17:01:13 +00002940 case tok::kw_static:
2941 case tok::kw_auto:
2942 case tok::kw_register:
2943 case tok::kw___thread:
Mike Stump1eb44332009-09-09 15:08:12 +00002944
Reid Spencer5f016e22007-07-11 17:01:13 +00002945 // type-specifiers
2946 case tok::kw_short:
2947 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00002948 case tok::kw___int64:
Reid Spencer5f016e22007-07-11 17:01:13 +00002949 case tok::kw_signed:
2950 case tok::kw_unsigned:
2951 case tok::kw__Complex:
2952 case tok::kw__Imaginary:
2953 case tok::kw_void:
2954 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00002955 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002956 case tok::kw_char16_t:
2957 case tok::kw_char32_t:
2958
Reid Spencer5f016e22007-07-11 17:01:13 +00002959 case tok::kw_int:
2960 case tok::kw_float:
2961 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00002962 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00002963 case tok::kw__Bool:
2964 case tok::kw__Decimal32:
2965 case tok::kw__Decimal64:
2966 case tok::kw__Decimal128:
John Thompson82287d12010-02-05 00:12:22 +00002967 case tok::kw___vector:
Mike Stump1eb44332009-09-09 15:08:12 +00002968
Chris Lattner99dc9142008-04-13 18:59:07 +00002969 // struct-or-union-specifier (C99) or class-specifier (C++)
2970 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00002971 case tok::kw_struct:
2972 case tok::kw_union:
2973 // enum-specifier
2974 case tok::kw_enum:
Mike Stump1eb44332009-09-09 15:08:12 +00002975
Reid Spencer5f016e22007-07-11 17:01:13 +00002976 // type-qualifier
2977 case tok::kw_const:
2978 case tok::kw_volatile:
2979 case tok::kw_restrict:
Steve Naroffd1861fd2007-07-31 12:34:36 +00002980
Reid Spencer5f016e22007-07-11 17:01:13 +00002981 // function-specifier
2982 case tok::kw_inline:
Douglas Gregorb48fe382008-10-31 09:07:45 +00002983 case tok::kw_virtual:
2984 case tok::kw_explicit:
Chris Lattnerd6c7c182007-08-09 16:40:21 +00002985
Peter Collingbournec6eb44b2011-04-15 00:35:57 +00002986 // static_assert-declaration
2987 case tok::kw__Static_assert:
2988
Chris Lattner1ef08762007-08-09 17:01:07 +00002989 // GNU typeof support.
2990 case tok::kw_typeof:
Mike Stump1eb44332009-09-09 15:08:12 +00002991
Chris Lattner1ef08762007-08-09 17:01:07 +00002992 // GNU attributes.
Chris Lattnerd6c7c182007-08-09 16:40:21 +00002993 case tok::kw___attribute:
Reid Spencer5f016e22007-07-11 17:01:13 +00002994 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00002995
Chris Lattnerf3948c42008-07-26 03:38:44 +00002996 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
2997 case tok::less:
2998 return getLang().ObjC1;
Mike Stump1eb44332009-09-09 15:08:12 +00002999
Douglas Gregord9d75e52011-04-27 05:41:15 +00003000 // typedef-name
3001 case tok::annot_typename:
3002 return !DisambiguatingWithExpression ||
3003 !isStartOfObjCClassMessageMissingOpenBracket();
3004
Steve Naroff47f52092009-01-06 19:34:12 +00003005 case tok::kw___declspec:
Steve Naroff239f0732008-12-25 14:16:32 +00003006 case tok::kw___cdecl:
3007 case tok::kw___stdcall:
3008 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00003009 case tok::kw___thiscall:
Eli Friedman290eeb02009-06-08 23:27:34 +00003010 case tok::kw___w64:
3011 case tok::kw___ptr64:
3012 case tok::kw___forceinline:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003013 case tok::kw___pascal:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003014
3015 case tok::kw___private:
3016 case tok::kw___local:
3017 case tok::kw___global:
3018 case tok::kw___constant:
3019 case tok::kw___read_only:
3020 case tok::kw___read_write:
3021 case tok::kw___write_only:
3022
Eli Friedman290eeb02009-06-08 23:27:34 +00003023 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00003024 }
3025}
3026
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003027bool Parser::isConstructorDeclarator() {
3028 TentativeParsingAction TPA(*this);
3029
3030 // Parse the C++ scope specifier.
3031 CXXScopeSpec SS;
John McCallb3d87482010-08-24 05:47:05 +00003032 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), true)) {
John McCall9ba61662010-02-26 08:45:28 +00003033 TPA.Revert();
3034 return false;
3035 }
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003036
3037 // Parse the constructor name.
3038 if (Tok.is(tok::identifier) || Tok.is(tok::annot_template_id)) {
3039 // We already know that we have a constructor name; just consume
3040 // the token.
3041 ConsumeToken();
3042 } else {
3043 TPA.Revert();
3044 return false;
3045 }
3046
3047 // Current class name must be followed by a left parentheses.
3048 if (Tok.isNot(tok::l_paren)) {
3049 TPA.Revert();
3050 return false;
3051 }
3052 ConsumeParen();
3053
3054 // A right parentheses or ellipsis signals that we have a constructor.
3055 if (Tok.is(tok::r_paren) || Tok.is(tok::ellipsis)) {
3056 TPA.Revert();
3057 return true;
3058 }
3059
3060 // If we need to, enter the specified scope.
3061 DeclaratorScopeObj DeclScopeObj(*this, SS);
Douglas Gregor23c94db2010-07-02 17:43:08 +00003062 if (SS.isSet() && Actions.ShouldEnterDeclaratorScope(getCurScope(), SS))
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003063 DeclScopeObj.EnterDeclaratorScope();
3064
Francois Pichetdfaa5fb2011-01-31 04:54:32 +00003065 // Optionally skip Microsoft attributes.
John McCall0b7e6782011-03-24 11:26:52 +00003066 ParsedAttributes Attrs(AttrFactory);
Francois Pichetdfaa5fb2011-01-31 04:54:32 +00003067 MaybeParseMicrosoftAttributes(Attrs);
3068
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003069 // Check whether the next token(s) are part of a declaration
3070 // specifier, in which case we have the start of a parameter and,
3071 // therefore, we know that this is a constructor.
3072 bool IsConstructor = isDeclarationSpecifier();
3073 TPA.Revert();
3074 return IsConstructor;
3075}
Reid Spencer5f016e22007-07-11 17:01:13 +00003076
3077/// ParseTypeQualifierListOpt
Dawn Perchik52fc3142010-09-03 01:29:35 +00003078/// type-qualifier-list: [C99 6.7.5]
3079/// type-qualifier
3080/// [vendor] attributes
3081/// [ only if VendorAttributesAllowed=true ]
3082/// type-qualifier-list type-qualifier
3083/// [vendor] type-qualifier-list attributes
3084/// [ only if VendorAttributesAllowed=true ]
3085/// [C++0x] attribute-specifier[opt] is allowed before cv-qualifier-seq
3086/// [ only if CXX0XAttributesAllowed=true ]
3087/// Note: vendor can be GNU, MS, etc.
Reid Spencer5f016e22007-07-11 17:01:13 +00003088///
Dawn Perchik52fc3142010-09-03 01:29:35 +00003089void Parser::ParseTypeQualifierListOpt(DeclSpec &DS,
3090 bool VendorAttributesAllowed,
Sean Huntbbd37c62009-11-21 08:43:09 +00003091 bool CXX0XAttributesAllowed) {
3092 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier()) {
3093 SourceLocation Loc = Tok.getLocation();
John McCall0b7e6782011-03-24 11:26:52 +00003094 ParsedAttributesWithRange attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00003095 ParseCXX0XAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00003096 if (CXX0XAttributesAllowed)
John McCall7f040a92010-12-24 02:08:15 +00003097 DS.takeAttributesFrom(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00003098 else
3099 Diag(Loc, diag::err_attributes_not_allowed);
3100 }
Abramo Bagnara796aa442011-03-12 11:17:06 +00003101
3102 SourceLocation EndLoc;
3103
Reid Spencer5f016e22007-07-11 17:01:13 +00003104 while (1) {
John McCallfec54012009-08-03 20:12:06 +00003105 bool isInvalid = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00003106 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00003107 unsigned DiagID = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00003108 SourceLocation Loc = Tok.getLocation();
3109
3110 switch (Tok.getKind()) {
Douglas Gregor1a480c42010-08-27 17:35:51 +00003111 case tok::code_completion:
3112 Actions.CodeCompleteTypeQualifiers(DS);
3113 ConsumeCodeCompletionToken();
3114 break;
3115
Reid Spencer5f016e22007-07-11 17:01:13 +00003116 case tok::kw_const:
John McCallfec54012009-08-03 20:12:06 +00003117 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, DiagID,
3118 getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00003119 break;
3120 case tok::kw_volatile:
John McCallfec54012009-08-03 20:12:06 +00003121 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
3122 getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00003123 break;
3124 case tok::kw_restrict:
John McCallfec54012009-08-03 20:12:06 +00003125 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
3126 getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00003127 break;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003128
3129 // OpenCL qualifiers:
3130 case tok::kw_private:
3131 if (!getLang().OpenCL)
3132 goto DoneWithTypeQuals;
3133 case tok::kw___private:
3134 case tok::kw___global:
3135 case tok::kw___local:
3136 case tok::kw___constant:
3137 case tok::kw___read_only:
3138 case tok::kw___write_only:
3139 case tok::kw___read_write:
3140 ParseOpenCLQualifiers(DS);
3141 break;
3142
Eli Friedman290eeb02009-06-08 23:27:34 +00003143 case tok::kw___w64:
Steve Naroff86bc6cf2008-12-25 14:41:26 +00003144 case tok::kw___ptr64:
Steve Naroff239f0732008-12-25 14:16:32 +00003145 case tok::kw___cdecl:
3146 case tok::kw___stdcall:
3147 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00003148 case tok::kw___thiscall:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003149 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00003150 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman290eeb02009-06-08 23:27:34 +00003151 continue;
3152 }
3153 goto DoneWithTypeQuals;
Dawn Perchik52fc3142010-09-03 01:29:35 +00003154 case tok::kw___pascal:
3155 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00003156 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik52fc3142010-09-03 01:29:35 +00003157 continue;
3158 }
3159 goto DoneWithTypeQuals;
Reid Spencer5f016e22007-07-11 17:01:13 +00003160 case tok::kw___attribute:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003161 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00003162 ParseGNUAttributes(DS.getAttributes());
Chris Lattner5a69d1c2008-12-18 07:02:59 +00003163 continue; // do *not* consume the next token!
3164 }
3165 // otherwise, FALL THROUGH!
3166 default:
Steve Naroff239f0732008-12-25 14:16:32 +00003167 DoneWithTypeQuals:
Chris Lattner5a69d1c2008-12-18 07:02:59 +00003168 // If this is not a type-qualifier token, we're done reading type
3169 // qualifiers. First verify that DeclSpec's are consistent.
Douglas Gregor9b3064b2009-04-01 22:41:11 +00003170 DS.Finish(Diags, PP);
Abramo Bagnara796aa442011-03-12 11:17:06 +00003171 if (EndLoc.isValid())
3172 DS.SetRangeEnd(EndLoc);
Chris Lattner5a69d1c2008-12-18 07:02:59 +00003173 return;
Reid Spencer5f016e22007-07-11 17:01:13 +00003174 }
Chris Lattnera1fcbad2008-12-18 06:50:14 +00003175
Reid Spencer5f016e22007-07-11 17:01:13 +00003176 // If the specifier combination wasn't legal, issue a diagnostic.
3177 if (isInvalid) {
3178 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner1ab3b962008-11-18 07:48:38 +00003179 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00003180 }
Abramo Bagnara796aa442011-03-12 11:17:06 +00003181 EndLoc = ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00003182 }
3183}
3184
3185
3186/// ParseDeclarator - Parse and verify a newly-initialized declarator.
3187///
3188void Parser::ParseDeclarator(Declarator &D) {
3189 /// This implements the 'declarator' production in the C grammar, then checks
3190 /// for well-formedness and issues diagnostics.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003191 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Reid Spencer5f016e22007-07-11 17:01:13 +00003192}
3193
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003194/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
3195/// is parsed by the function passed to it. Pass null, and the direct-declarator
3196/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003197/// ptr-operator production.
3198///
Sebastian Redlf30208a2009-01-24 21:16:55 +00003199/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
3200/// [C] pointer[opt] direct-declarator
3201/// [C++] direct-declarator
3202/// [C++] ptr-operator declarator
Reid Spencer5f016e22007-07-11 17:01:13 +00003203///
3204/// pointer: [C99 6.7.5]
3205/// '*' type-qualifier-list[opt]
3206/// '*' type-qualifier-list[opt] pointer
3207///
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003208/// ptr-operator:
3209/// '*' cv-qualifier-seq[opt]
3210/// '&'
Sebastian Redl05532f22009-03-15 22:02:01 +00003211/// [C++0x] '&&'
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003212/// [GNU] '&' restrict[opt] attributes[opt]
Sebastian Redl05532f22009-03-15 22:02:01 +00003213/// [GNU?] '&&' restrict[opt] attributes[opt]
Sebastian Redlf30208a2009-01-24 21:16:55 +00003214/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003215void Parser::ParseDeclaratorInternal(Declarator &D,
3216 DirectDeclParseFunction DirectDeclParser) {
Douglas Gregor91a28862009-08-26 14:27:30 +00003217 if (Diags.hasAllExtensionsSilenced())
3218 D.setExtension();
Douglas Gregor2ccccb32010-08-23 18:23:48 +00003219
Sebastian Redlf30208a2009-01-24 21:16:55 +00003220 // C++ member pointers start with a '::' or a nested-name.
3221 // Member pointers get special handling, since there's no place for the
3222 // scope spec in the generic path below.
Chris Lattnerf919bfe2009-03-24 17:04:48 +00003223 if (getLang().CPlusPlus &&
3224 (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) ||
3225 Tok.is(tok::annot_cxxscope))) {
Sebastian Redlf30208a2009-01-24 21:16:55 +00003226 CXXScopeSpec SS;
John McCallb3d87482010-08-24 05:47:05 +00003227 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), true); // ignore fail
John McCall9ba61662010-02-26 08:45:28 +00003228
Jeffrey Yasskinedc28772010-04-07 23:29:58 +00003229 if (SS.isNotEmpty()) {
Mike Stump1eb44332009-09-09 15:08:12 +00003230 if (Tok.isNot(tok::star)) {
Sebastian Redlf30208a2009-01-24 21:16:55 +00003231 // The scope spec really belongs to the direct-declarator.
3232 D.getCXXScopeSpec() = SS;
3233 if (DirectDeclParser)
3234 (this->*DirectDeclParser)(D);
3235 return;
3236 }
3237
3238 SourceLocation Loc = ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00003239 D.SetRangeEnd(Loc);
John McCall0b7e6782011-03-24 11:26:52 +00003240 DeclSpec DS(AttrFactory);
Sebastian Redlf30208a2009-01-24 21:16:55 +00003241 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00003242 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00003243
3244 // Recurse to parse whatever is left.
3245 ParseDeclaratorInternal(D, DirectDeclParser);
3246
3247 // Sema will have to catch (syntactically invalid) pointers into global
3248 // scope. It has to catch pointers into namespace scope anyway.
3249 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
John McCall0b7e6782011-03-24 11:26:52 +00003250 Loc),
3251 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003252 /* Don't replace range end. */SourceLocation());
Sebastian Redlf30208a2009-01-24 21:16:55 +00003253 return;
3254 }
3255 }
3256
3257 tok::TokenKind Kind = Tok.getKind();
Steve Naroff5618bd42008-08-27 16:04:49 +00003258 // Not a pointer, C++ reference, or block.
Chris Lattner9af55002009-03-27 04:18:06 +00003259 if (Kind != tok::star && Kind != tok::caret &&
Chris Lattnerf919bfe2009-03-24 17:04:48 +00003260 (Kind != tok::amp || !getLang().CPlusPlus) &&
Sebastian Redl743de1f2009-03-23 00:00:23 +00003261 // We parse rvalue refs in C++03, because otherwise the errors are scary.
Chris Lattner9af55002009-03-27 04:18:06 +00003262 (Kind != tok::ampamp || !getLang().CPlusPlus)) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003263 if (DirectDeclParser)
3264 (this->*DirectDeclParser)(D);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003265 return;
3266 }
Sebastian Redlf30208a2009-01-24 21:16:55 +00003267
Sebastian Redl05532f22009-03-15 22:02:01 +00003268 // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference,
3269 // '&&' -> rvalue reference
Sebastian Redl743de1f2009-03-23 00:00:23 +00003270 SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&.
Sebastian Redlab197ba2009-02-09 18:23:29 +00003271 D.SetRangeEnd(Loc);
Reid Spencer5f016e22007-07-11 17:01:13 +00003272
Chris Lattner9af55002009-03-27 04:18:06 +00003273 if (Kind == tok::star || Kind == tok::caret) {
Chris Lattner76549142008-02-21 01:32:26 +00003274 // Is a pointer.
John McCall0b7e6782011-03-24 11:26:52 +00003275 DeclSpec DS(AttrFactory);
Sebastian Redlf30208a2009-01-24 21:16:55 +00003276
Reid Spencer5f016e22007-07-11 17:01:13 +00003277 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00003278 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00003279
Reid Spencer5f016e22007-07-11 17:01:13 +00003280 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003281 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroff5618bd42008-08-27 16:04:49 +00003282 if (Kind == tok::star)
3283 // Remember that we parsed a pointer type, and remember the type-quals.
3284 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
Chandler Carruthd067c072011-02-23 18:51:59 +00003285 DS.getConstSpecLoc(),
3286 DS.getVolatileSpecLoc(),
John McCall0b7e6782011-03-24 11:26:52 +00003287 DS.getRestrictSpecLoc()),
3288 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003289 SourceLocation());
Steve Naroff5618bd42008-08-27 16:04:49 +00003290 else
3291 // Remember that we parsed a Block type, and remember the type-quals.
Mike Stump1eb44332009-09-09 15:08:12 +00003292 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
John McCall0b7e6782011-03-24 11:26:52 +00003293 Loc),
3294 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003295 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00003296 } else {
3297 // Is a reference
John McCall0b7e6782011-03-24 11:26:52 +00003298 DeclSpec DS(AttrFactory);
Reid Spencer5f016e22007-07-11 17:01:13 +00003299
Sebastian Redl743de1f2009-03-23 00:00:23 +00003300 // Complain about rvalue references in C++03, but then go on and build
3301 // the declarator.
3302 if (Kind == tok::ampamp && !getLang().CPlusPlus0x)
Douglas Gregor16cf8f52011-01-25 02:17:32 +00003303 Diag(Loc, diag::ext_rvalue_reference);
Sebastian Redl743de1f2009-03-23 00:00:23 +00003304
Reid Spencer5f016e22007-07-11 17:01:13 +00003305 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
3306 // cv-qualifiers are introduced through the use of a typedef or of a
3307 // template type argument, in which case the cv-qualifiers are ignored.
3308 //
3309 // [GNU] Retricted references are allowed.
3310 // [GNU] Attributes on references are allowed.
Sean Huntbbd37c62009-11-21 08:43:09 +00003311 // [C++0x] Attributes on references are not allowed.
3312 ParseTypeQualifierListOpt(DS, true, false);
Sebastian Redlab197ba2009-02-09 18:23:29 +00003313 D.ExtendWithDeclSpec(DS);
Reid Spencer5f016e22007-07-11 17:01:13 +00003314
3315 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
3316 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
3317 Diag(DS.getConstSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00003318 diag::err_invalid_reference_qualifier_application) << "const";
Reid Spencer5f016e22007-07-11 17:01:13 +00003319 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
3320 Diag(DS.getVolatileSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00003321 diag::err_invalid_reference_qualifier_application) << "volatile";
Reid Spencer5f016e22007-07-11 17:01:13 +00003322 }
3323
3324 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003325 ParseDeclaratorInternal(D, DirectDeclParser);
Reid Spencer5f016e22007-07-11 17:01:13 +00003326
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00003327 if (D.getNumTypeObjects() > 0) {
3328 // C++ [dcl.ref]p4: There shall be no references to references.
3329 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
3330 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattnerda83bac2008-11-19 07:37:42 +00003331 if (const IdentifierInfo *II = D.getIdentifier())
3332 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
3333 << II;
3334 else
3335 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
3336 << "type name";
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00003337
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003338 // Once we've complained about the reference-to-reference, we
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00003339 // can go ahead and build the (technically ill-formed)
3340 // declarator: reference collapsing will take care of it.
3341 }
3342 }
3343
Reid Spencer5f016e22007-07-11 17:01:13 +00003344 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner76549142008-02-21 01:32:26 +00003345 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
Sebastian Redl05532f22009-03-15 22:02:01 +00003346 Kind == tok::amp),
John McCall0b7e6782011-03-24 11:26:52 +00003347 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003348 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00003349 }
3350}
3351
3352/// ParseDirectDeclarator
3353/// direct-declarator: [C99 6.7.5]
Douglas Gregor42a552f2008-11-05 20:51:48 +00003354/// [C99] identifier
Reid Spencer5f016e22007-07-11 17:01:13 +00003355/// '(' declarator ')'
3356/// [GNU] '(' attributes declarator ')'
3357/// [C90] direct-declarator '[' constant-expression[opt] ']'
3358/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
3359/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
3360/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
3361/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
3362/// direct-declarator '(' parameter-type-list ')'
3363/// direct-declarator '(' identifier-list[opt] ')'
3364/// [GNU] direct-declarator '(' parameter-forward-declarations
3365/// parameter-type-list[opt] ')'
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003366/// [C++] direct-declarator '(' parameter-declaration-clause ')'
3367/// cv-qualifier-seq[opt] exception-specification[opt]
Douglas Gregorb48fe382008-10-31 09:07:45 +00003368/// [C++] declarator-id
Douglas Gregor42a552f2008-11-05 20:51:48 +00003369///
3370/// declarator-id: [C++ 8]
Douglas Gregora8bc8c92010-12-23 22:44:42 +00003371/// '...'[opt] id-expression
Douglas Gregor42a552f2008-11-05 20:51:48 +00003372/// '::'[opt] nested-name-specifier[opt] type-name
3373///
3374/// id-expression: [C++ 5.1]
3375/// unqualified-id
Douglas Gregordb422df2009-09-25 21:45:23 +00003376/// qualified-id
Douglas Gregor42a552f2008-11-05 20:51:48 +00003377///
3378/// unqualified-id: [C++ 5.1]
Mike Stump1eb44332009-09-09 15:08:12 +00003379/// identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003380/// operator-function-id
Douglas Gregordb422df2009-09-25 21:45:23 +00003381/// conversion-function-id
Mike Stump1eb44332009-09-09 15:08:12 +00003382/// '~' class-name
Douglas Gregor39a8de12009-02-25 19:37:18 +00003383/// template-id
Argyrios Kyrtzidisc7ed9c62008-11-07 22:02:30 +00003384///
Reid Spencer5f016e22007-07-11 17:01:13 +00003385void Parser::ParseDirectDeclarator(Declarator &D) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00003386 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003387
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003388 if (getLang().CPlusPlus && D.mayHaveIdentifier()) {
3389 // ParseDeclaratorInternal might already have parsed the scope.
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00003390 if (D.getCXXScopeSpec().isEmpty()) {
John McCallb3d87482010-08-24 05:47:05 +00003391 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec(), ParsedType(), true);
John McCall9ba61662010-02-26 08:45:28 +00003392 }
3393
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00003394 if (D.getCXXScopeSpec().isValid()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00003395 if (Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
John McCalle7e278b2009-12-11 20:04:54 +00003396 // Change the declaration context for name lookup, until this function
3397 // is exited (and the declarator has been parsed).
3398 DeclScopeObj.EnterDeclaratorScope();
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00003399 }
3400
Douglas Gregora8bc8c92010-12-23 22:44:42 +00003401 // C++0x [dcl.fct]p14:
3402 // There is a syntactic ambiguity when an ellipsis occurs at the end
3403 // of a parameter-declaration-clause without a preceding comma. In
3404 // this case, the ellipsis is parsed as part of the
3405 // abstract-declarator if the type of the parameter names a template
3406 // parameter pack that has not been expanded; otherwise, it is parsed
3407 // as part of the parameter-declaration-clause.
3408 if (Tok.is(tok::ellipsis) &&
3409 !((D.getContext() == Declarator::PrototypeContext ||
3410 D.getContext() == Declarator::BlockLiteralContext) &&
Douglas Gregora8bc8c92010-12-23 22:44:42 +00003411 NextToken().is(tok::r_paren) &&
3412 !Actions.containsUnexpandedParameterPacks(D)))
3413 D.setEllipsisLoc(ConsumeToken());
3414
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003415 if (Tok.is(tok::identifier) || Tok.is(tok::kw_operator) ||
3416 Tok.is(tok::annot_template_id) || Tok.is(tok::tilde)) {
3417 // We found something that indicates the start of an unqualified-id.
3418 // Parse that unqualified-id.
John McCallba9d8532010-04-13 06:39:49 +00003419 bool AllowConstructorName;
3420 if (D.getDeclSpec().hasTypeSpecifier())
3421 AllowConstructorName = false;
3422 else if (D.getCXXScopeSpec().isSet())
3423 AllowConstructorName =
3424 (D.getContext() == Declarator::FileContext ||
3425 (D.getContext() == Declarator::MemberContext &&
3426 D.getDeclSpec().isFriendSpecified()));
3427 else
3428 AllowConstructorName = (D.getContext() == Declarator::MemberContext);
3429
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003430 if (ParseUnqualifiedId(D.getCXXScopeSpec(),
3431 /*EnteringContext=*/true,
3432 /*AllowDestructorName=*/true,
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003433 AllowConstructorName,
John McCallb3d87482010-08-24 05:47:05 +00003434 ParsedType(),
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00003435 D.getName()) ||
3436 // Once we're past the identifier, if the scope was bad, mark the
3437 // whole declarator bad.
3438 D.getCXXScopeSpec().isInvalid()) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00003439 D.SetIdentifier(0, Tok.getLocation());
3440 D.setInvalidType(true);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003441 } else {
3442 // Parsed the unqualified-id; update range information and move along.
3443 if (D.getSourceRange().getBegin().isInvalid())
3444 D.SetRangeBegin(D.getName().getSourceRange().getBegin());
3445 D.SetRangeEnd(D.getName().getSourceRange().getEnd());
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003446 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003447 goto PastIdentifier;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00003448 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003449 } else if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00003450 assert(!getLang().CPlusPlus &&
3451 "There's a C++-specific check for tok::identifier above");
3452 assert(Tok.getIdentifierInfo() && "Not an identifier?");
3453 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
3454 ConsumeToken();
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003455 goto PastIdentifier;
3456 }
3457
3458 if (Tok.is(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003459 // direct-declarator: '(' declarator ')'
3460 // direct-declarator: '(' attributes declarator ')'
3461 // Example: 'char (*X)' or 'int (*XX)(void)'
3462 ParseParenDeclarator(D);
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003463
3464 // If the declarator was parenthesized, we entered the declarator
3465 // scope when parsing the parenthesized declarator, then exited
3466 // the scope already. Re-enter the scope, if we need to.
3467 if (D.getCXXScopeSpec().isSet()) {
Fariborz Jahanian46877cd2010-08-17 23:50:37 +00003468 // If there was an error parsing parenthesized declarator, declarator
3469 // scope may have been enterred before. Don't do it again.
3470 if (!D.isInvalidType() &&
3471 Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003472 // Change the declaration context for name lookup, until this function
3473 // is exited (and the declarator has been parsed).
Fariborz Jahanian46877cd2010-08-17 23:50:37 +00003474 DeclScopeObj.EnterDeclaratorScope();
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003475 }
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00003476 } else if (D.mayOmitIdentifier()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003477 // This could be something simple like "int" (in which case the declarator
3478 // portion is empty), if an abstract-declarator is allowed.
3479 D.SetIdentifier(0, Tok.getLocation());
3480 } else {
Douglas Gregore950d4b2009-03-06 23:28:18 +00003481 if (D.getContext() == Declarator::MemberContext)
3482 Diag(Tok, diag::err_expected_member_name_or_semi)
3483 << D.getDeclSpec().getSourceRange();
3484 else if (getLang().CPlusPlus)
Douglas Gregor2d1c2142009-11-03 19:44:04 +00003485 Diag(Tok, diag::err_expected_unqualified_id) << getLang().CPlusPlus;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003486 else
Chris Lattner1ab3b962008-11-18 07:48:38 +00003487 Diag(Tok, diag::err_expected_ident_lparen);
Reid Spencer5f016e22007-07-11 17:01:13 +00003488 D.SetIdentifier(0, Tok.getLocation());
Chris Lattner1f6f54b2008-11-11 06:13:16 +00003489 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00003490 }
Mike Stump1eb44332009-09-09 15:08:12 +00003491
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00003492 PastIdentifier:
Reid Spencer5f016e22007-07-11 17:01:13 +00003493 assert(D.isPastIdentifier() &&
3494 "Haven't past the location of the identifier yet?");
Mike Stump1eb44332009-09-09 15:08:12 +00003495
Sean Huntbbd37c62009-11-21 08:43:09 +00003496 // Don't parse attributes unless we have an identifier.
John McCall7f040a92010-12-24 02:08:15 +00003497 if (D.getIdentifier())
3498 MaybeParseCXX0XAttributes(D);
Sean Huntbbd37c62009-11-21 08:43:09 +00003499
Reid Spencer5f016e22007-07-11 17:01:13 +00003500 while (1) {
Chris Lattner04d66662007-10-09 17:33:22 +00003501 if (Tok.is(tok::l_paren)) {
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00003502 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
3503 // In such a case, check if we actually have a function declarator; if it
3504 // is not, the declarator has been fully parsed.
Chris Lattner7399ee02008-10-20 02:05:46 +00003505 if (getLang().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
3506 // When not in file scope, warn for ambiguous function declarators, just
3507 // in case the author intended it as a variable definition.
3508 bool warnIfAmbiguous = D.getContext() != Declarator::FileContext;
3509 if (!isCXXFunctionDeclarator(warnIfAmbiguous))
3510 break;
3511 }
John McCall0b7e6782011-03-24 11:26:52 +00003512 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00003513 ParseFunctionDeclarator(ConsumeParen(), D, attrs);
Chris Lattner04d66662007-10-09 17:33:22 +00003514 } else if (Tok.is(tok::l_square)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003515 ParseBracketDeclarator(D);
3516 } else {
3517 break;
3518 }
3519 }
3520}
3521
Chris Lattneref4715c2008-04-06 05:45:57 +00003522/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
3523/// only called before the identifier, so these are most likely just grouping
Mike Stump1eb44332009-09-09 15:08:12 +00003524/// parens for precedence. If we find that these are actually function
Chris Lattneref4715c2008-04-06 05:45:57 +00003525/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
3526///
3527/// direct-declarator:
3528/// '(' declarator ')'
3529/// [GNU] '(' attributes declarator ')'
Chris Lattner7399ee02008-10-20 02:05:46 +00003530/// direct-declarator '(' parameter-type-list ')'
3531/// direct-declarator '(' identifier-list[opt] ')'
3532/// [GNU] direct-declarator '(' parameter-forward-declarations
3533/// parameter-type-list[opt] ')'
Chris Lattneref4715c2008-04-06 05:45:57 +00003534///
3535void Parser::ParseParenDeclarator(Declarator &D) {
3536 SourceLocation StartLoc = ConsumeParen();
3537 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
Mike Stump1eb44332009-09-09 15:08:12 +00003538
Chris Lattner7399ee02008-10-20 02:05:46 +00003539 // Eat any attributes before we look at whether this is a grouping or function
3540 // declarator paren. If this is a grouping paren, the attribute applies to
3541 // the type being built up, for example:
3542 // int (__attribute__(()) *x)(long y)
3543 // If this ends up not being a grouping paren, the attribute applies to the
3544 // first argument, for example:
3545 // int (__attribute__(()) int x)
3546 // In either case, we need to eat any attributes to be able to determine what
3547 // sort of paren this is.
3548 //
John McCall0b7e6782011-03-24 11:26:52 +00003549 ParsedAttributes attrs(AttrFactory);
Chris Lattner7399ee02008-10-20 02:05:46 +00003550 bool RequiresArg = false;
3551 if (Tok.is(tok::kw___attribute)) {
John McCall7f040a92010-12-24 02:08:15 +00003552 ParseGNUAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00003553
Chris Lattner7399ee02008-10-20 02:05:46 +00003554 // We require that the argument list (if this is a non-grouping paren) be
3555 // present even if the attribute list was empty.
3556 RequiresArg = true;
3557 }
Steve Naroff239f0732008-12-25 14:16:32 +00003558 // Eat any Microsoft extensions.
Eli Friedman290eeb02009-06-08 23:27:34 +00003559 if (Tok.is(tok::kw___cdecl) || Tok.is(tok::kw___stdcall) ||
Douglas Gregorf813a2c2010-05-18 16:57:00 +00003560 Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___fastcall) ||
3561 Tok.is(tok::kw___w64) || Tok.is(tok::kw___ptr64)) {
John McCall7f040a92010-12-24 02:08:15 +00003562 ParseMicrosoftTypeAttributes(attrs);
Eli Friedman290eeb02009-06-08 23:27:34 +00003563 }
Dawn Perchik52fc3142010-09-03 01:29:35 +00003564 // Eat any Borland extensions.
Ted Kremenek8113ecf2010-11-10 05:59:39 +00003565 if (Tok.is(tok::kw___pascal))
John McCall7f040a92010-12-24 02:08:15 +00003566 ParseBorlandTypeAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00003567
Chris Lattneref4715c2008-04-06 05:45:57 +00003568 // If we haven't past the identifier yet (or where the identifier would be
3569 // stored, if this is an abstract declarator), then this is probably just
3570 // grouping parens. However, if this could be an abstract-declarator, then
3571 // this could also be the start of function arguments (consider 'void()').
3572 bool isGrouping;
Mike Stump1eb44332009-09-09 15:08:12 +00003573
Chris Lattneref4715c2008-04-06 05:45:57 +00003574 if (!D.mayOmitIdentifier()) {
3575 // If this can't be an abstract-declarator, this *must* be a grouping
3576 // paren, because we haven't seen the identifier yet.
3577 isGrouping = true;
3578 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Argyrios Kyrtzidise25d2702008-10-06 00:07:55 +00003579 (getLang().CPlusPlus && Tok.is(tok::ellipsis)) || // C++ int(...)
Chris Lattneref4715c2008-04-06 05:45:57 +00003580 isDeclarationSpecifier()) { // 'int(int)' is a function.
3581 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
3582 // considered to be a type, not a K&R identifier-list.
3583 isGrouping = false;
3584 } else {
3585 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
3586 isGrouping = true;
3587 }
Mike Stump1eb44332009-09-09 15:08:12 +00003588
Chris Lattneref4715c2008-04-06 05:45:57 +00003589 // If this is a grouping paren, handle:
3590 // direct-declarator: '(' declarator ')'
3591 // direct-declarator: '(' attributes declarator ')'
3592 if (isGrouping) {
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00003593 bool hadGroupingParens = D.hasGroupingParens();
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00003594 D.setGroupingParens(true);
3595
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003596 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattneref4715c2008-04-06 05:45:57 +00003597 // Match the ')'.
Abramo Bagnara075f8f12010-12-10 16:29:40 +00003598 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_paren, StartLoc);
John McCall0b7e6782011-03-24 11:26:52 +00003599 D.AddTypeInfo(DeclaratorChunk::getParen(StartLoc, EndLoc),
3600 attrs, EndLoc);
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00003601
3602 D.setGroupingParens(hadGroupingParens);
Chris Lattneref4715c2008-04-06 05:45:57 +00003603 return;
3604 }
Mike Stump1eb44332009-09-09 15:08:12 +00003605
Chris Lattneref4715c2008-04-06 05:45:57 +00003606 // Okay, if this wasn't a grouping paren, it must be the start of a function
3607 // argument list. Recognize that this declarator will never have an
Chris Lattner7399ee02008-10-20 02:05:46 +00003608 // identifier (and remember where it would have been), then call into
3609 // ParseFunctionDeclarator to handle of argument list.
Chris Lattneref4715c2008-04-06 05:45:57 +00003610 D.SetIdentifier(0, Tok.getLocation());
3611
John McCall7f040a92010-12-24 02:08:15 +00003612 ParseFunctionDeclarator(StartLoc, D, attrs, RequiresArg);
Chris Lattneref4715c2008-04-06 05:45:57 +00003613}
3614
3615/// ParseFunctionDeclarator - We are after the identifier and have parsed the
3616/// declarator D up to a paren, which indicates that we are parsing function
3617/// arguments.
Reid Spencer5f016e22007-07-11 17:01:13 +00003618///
Chris Lattner7399ee02008-10-20 02:05:46 +00003619/// If AttrList is non-null, then the caller parsed those arguments immediately
3620/// after the open paren - they should be considered to be the first argument of
3621/// a parameter. If RequiresArg is true, then the first argument of the
3622/// function is required to be present and required to not be an identifier
3623/// list.
3624///
Reid Spencer5f016e22007-07-11 17:01:13 +00003625/// This method also handles this portion of the grammar:
3626/// parameter-type-list: [C99 6.7.5]
3627/// parameter-list
3628/// parameter-list ',' '...'
Douglas Gregored5d6512009-09-22 21:41:40 +00003629/// [C++] parameter-list '...'
Reid Spencer5f016e22007-07-11 17:01:13 +00003630///
3631/// parameter-list: [C99 6.7.5]
3632/// parameter-declaration
3633/// parameter-list ',' parameter-declaration
3634///
3635/// parameter-declaration: [C99 6.7.5]
3636/// declaration-specifiers declarator
Chris Lattner04421082008-04-08 04:40:51 +00003637/// [C++] declaration-specifiers declarator '=' assignment-expression
Reid Spencer5f016e22007-07-11 17:01:13 +00003638/// [GNU] declaration-specifiers declarator attributes
Sebastian Redl50de12f2009-03-24 22:27:57 +00003639/// declaration-specifiers abstract-declarator[opt]
3640/// [C++] declaration-specifiers abstract-declarator[opt]
Chris Lattner8123a952008-04-10 02:22:51 +00003641/// '=' assignment-expression
Reid Spencer5f016e22007-07-11 17:01:13 +00003642/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
3643///
Douglas Gregor83f51722011-01-26 03:43:54 +00003644/// For C++, after the parameter-list, it also parses "cv-qualifier-seq[opt]",
3645/// C++0x "ref-qualifier[opt]" and "exception-specification[opt]".
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003646///
Sebastian Redl7acafd02011-03-05 14:45:16 +00003647/// [C++0x] exception-specification:
3648/// dynamic-exception-specification
3649/// noexcept-specification
3650///
Chris Lattner7399ee02008-10-20 02:05:46 +00003651void Parser::ParseFunctionDeclarator(SourceLocation LParenLoc, Declarator &D,
John McCall7f040a92010-12-24 02:08:15 +00003652 ParsedAttributes &attrs,
Chris Lattner7399ee02008-10-20 02:05:46 +00003653 bool RequiresArg) {
Chris Lattneref4715c2008-04-06 05:45:57 +00003654 // lparen is already consumed!
3655 assert(D.isPastIdentifier() && "Should not call before identifier!");
Mike Stump1eb44332009-09-09 15:08:12 +00003656
Douglas Gregordab60ad2010-10-01 18:44:50 +00003657 ParsedType TrailingReturnType;
3658
Chris Lattner7399ee02008-10-20 02:05:46 +00003659 // This parameter list may be empty.
Chris Lattner04d66662007-10-09 17:33:22 +00003660 if (Tok.is(tok::r_paren)) {
Ted Kremenek8113ecf2010-11-10 05:59:39 +00003661 if (RequiresArg)
Chris Lattner1ab3b962008-11-18 07:48:38 +00003662 Diag(Tok, diag::err_argument_required_after_attribute);
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003663
Abramo Bagnara796aa442011-03-12 11:17:06 +00003664 SourceLocation EndLoc = ConsumeParen(); // Eat the closing ')'.
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003665
3666 // cv-qualifier-seq[opt].
John McCall0b7e6782011-03-24 11:26:52 +00003667 DeclSpec DS(AttrFactory);
Douglas Gregor83f51722011-01-26 03:43:54 +00003668 SourceLocation RefQualifierLoc;
3669 bool RefQualifierIsLValueRef = true;
Sebastian Redl7acafd02011-03-05 14:45:16 +00003670 ExceptionSpecificationType ESpecType = EST_None;
3671 SourceRange ESpecRange;
3672 llvm::SmallVector<ParsedType, 2> DynamicExceptions;
3673 llvm::SmallVector<SourceRange, 2> DynamicExceptionRanges;
3674 ExprResult NoexceptExpr;
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003675 if (getLang().CPlusPlus) {
John McCall7f040a92010-12-24 02:08:15 +00003676 MaybeParseCXX0XAttributes(attrs);
3677
Chris Lattner5a69d1c2008-12-18 07:02:59 +00003678 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Sebastian Redlab197ba2009-02-09 18:23:29 +00003679 if (!DS.getSourceRange().getEnd().isInvalid())
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00003680 EndLoc = DS.getSourceRange().getEnd();
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00003681
Douglas Gregor83f51722011-01-26 03:43:54 +00003682 // Parse ref-qualifier[opt]
3683 if (Tok.is(tok::amp) || Tok.is(tok::ampamp)) {
3684 if (!getLang().CPlusPlus0x)
Douglas Gregor1f381062011-01-26 20:35:32 +00003685 Diag(Tok, diag::ext_ref_qualifier);
Abramo Bagnara796aa442011-03-12 11:17:06 +00003686
Douglas Gregor83f51722011-01-26 03:43:54 +00003687 RefQualifierIsLValueRef = Tok.is(tok::amp);
3688 RefQualifierLoc = ConsumeToken();
3689 EndLoc = RefQualifierLoc;
3690 }
Sebastian Redl7acafd02011-03-05 14:45:16 +00003691
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00003692 // Parse exception-specification[opt].
Sebastian Redl7acafd02011-03-05 14:45:16 +00003693 ESpecType = MaybeParseExceptionSpecification(ESpecRange,
3694 DynamicExceptions,
3695 DynamicExceptionRanges,
3696 NoexceptExpr);
3697 if (ESpecType != EST_None)
3698 EndLoc = ESpecRange.getEnd();
Douglas Gregordab60ad2010-10-01 18:44:50 +00003699
3700 // Parse trailing-return-type.
3701 if (getLang().CPlusPlus0x && Tok.is(tok::arrow)) {
3702 TrailingReturnType = ParseTrailingReturnType().get();
3703 }
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003704 }
3705
Chris Lattnerf97409f2008-04-06 06:57:35 +00003706 // Remember that we parsed a function type, and remember the attributes.
Reid Spencer5f016e22007-07-11 17:01:13 +00003707 // int() -> no prototype, no '...'.
John McCall0b7e6782011-03-24 11:26:52 +00003708 D.AddTypeInfo(DeclaratorChunk::getFunction(/*prototype*/getLang().CPlusPlus,
Chris Lattnerf97409f2008-04-06 06:57:35 +00003709 /*variadic*/ false,
Douglas Gregor965acbb2009-02-18 07:07:28 +00003710 SourceLocation(),
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003711 /*arglist*/ 0, 0,
3712 DS.getTypeQualifiers(),
Douglas Gregor83f51722011-01-26 03:43:54 +00003713 RefQualifierIsLValueRef,
3714 RefQualifierLoc,
Sebastian Redl6e5d3192011-03-05 22:42:13 +00003715 ESpecType, ESpecRange.getBegin(),
Sebastian Redl7acafd02011-03-05 14:45:16 +00003716 DynamicExceptions.data(),
3717 DynamicExceptionRanges.data(),
3718 DynamicExceptions.size(),
Sebastian Redl6e5d3192011-03-05 22:42:13 +00003719 NoexceptExpr.isUsable() ?
3720 NoexceptExpr.get() : 0,
Abramo Bagnara796aa442011-03-12 11:17:06 +00003721 LParenLoc, EndLoc, D,
Douglas Gregordab60ad2010-10-01 18:44:50 +00003722 TrailingReturnType),
John McCall0b7e6782011-03-24 11:26:52 +00003723 attrs, EndLoc);
Chris Lattnerf97409f2008-04-06 06:57:35 +00003724 return;
Sebastian Redlef65f062009-05-29 18:02:33 +00003725 }
3726
Chris Lattner7399ee02008-10-20 02:05:46 +00003727 // Alternatively, this parameter list may be an identifier list form for a
3728 // K&R-style function: void foo(a,b,c)
John Thompson82287d12010-02-05 00:12:22 +00003729 if (!getLang().CPlusPlus && Tok.is(tok::identifier)
3730 && !TryAltiVecVectorToken()) {
John McCall9ba61662010-02-26 08:45:28 +00003731 if (TryAnnotateTypeOrScopeToken() || !Tok.is(tok::annot_typename)) {
Chris Lattner7399ee02008-10-20 02:05:46 +00003732 // K&R identifier lists can't have typedefs as identifiers, per
3733 // C99 6.7.5.3p11.
Ted Kremenek8113ecf2010-11-10 05:59:39 +00003734 if (RequiresArg)
Steve Naroff2d081c42009-01-28 19:16:40 +00003735 Diag(Tok, diag::err_argument_required_after_attribute);
Chris Lattner83a94472010-05-14 17:23:36 +00003736
Steve Naroff2d081c42009-01-28 19:16:40 +00003737 // Identifier list. Note that '(' identifier-list ')' is only allowed for
Chris Lattner83a94472010-05-14 17:23:36 +00003738 // normal declarators, not for abstract-declarators. Get the first
3739 // identifier.
Chris Lattner9a65b812010-05-14 17:44:56 +00003740 Token FirstTok = Tok;
Chris Lattner83a94472010-05-14 17:23:36 +00003741 ConsumeToken(); // eat the first identifier.
Chris Lattner9a65b812010-05-14 17:44:56 +00003742
3743 // Identifier lists follow a really simple grammar: the identifiers can
3744 // be followed *only* by a ", moreidentifiers" or ")". However, K&R
3745 // identifier lists are really rare in the brave new modern world, and it
3746 // is very common for someone to typo a type in a non-k&r style list. If
3747 // we are presented with something like: "void foo(intptr x, float y)",
3748 // we don't want to start parsing the function declarator as though it is
3749 // a K&R style declarator just because intptr is an invalid type.
3750 //
3751 // To handle this, we check to see if the token after the first identifier
3752 // is a "," or ")". Only if so, do we parse it as an identifier list.
3753 if (Tok.is(tok::comma) || Tok.is(tok::r_paren))
3754 return ParseFunctionDeclaratorIdentifierList(LParenLoc,
3755 FirstTok.getIdentifierInfo(),
3756 FirstTok.getLocation(), D);
3757
3758 // If we get here, the code is invalid. Push the first identifier back
3759 // into the token stream and parse the first argument as an (invalid)
3760 // normal argument declarator.
3761 PP.EnterToken(Tok);
3762 Tok = FirstTok;
Chris Lattner7399ee02008-10-20 02:05:46 +00003763 }
Chris Lattnerf97409f2008-04-06 06:57:35 +00003764 }
Mike Stump1eb44332009-09-09 15:08:12 +00003765
Chris Lattnerf97409f2008-04-06 06:57:35 +00003766 // Finally, a normal, non-empty parameter type list.
Mike Stump1eb44332009-09-09 15:08:12 +00003767
Chris Lattnerf97409f2008-04-06 06:57:35 +00003768 // Build up an array of information about the parsed arguments.
3769 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Chris Lattner04421082008-04-08 04:40:51 +00003770
3771 // Enter function-declaration scope, limiting any declarators to the
3772 // function prototype scope, including parameter declarators.
Chris Lattnerae50fa02009-03-05 00:00:31 +00003773 ParseScope PrototypeScope(this,
3774 Scope::FunctionPrototypeScope|Scope::DeclScope);
Mike Stump1eb44332009-09-09 15:08:12 +00003775
Chris Lattnerf97409f2008-04-06 06:57:35 +00003776 bool IsVariadic = false;
Douglas Gregor965acbb2009-02-18 07:07:28 +00003777 SourceLocation EllipsisLoc;
Chris Lattnerf97409f2008-04-06 06:57:35 +00003778 while (1) {
3779 if (Tok.is(tok::ellipsis)) {
3780 IsVariadic = true;
Douglas Gregor965acbb2009-02-18 07:07:28 +00003781 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chris Lattnerf97409f2008-04-06 06:57:35 +00003782 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00003783 }
Mike Stump1eb44332009-09-09 15:08:12 +00003784
Chris Lattnerf97409f2008-04-06 06:57:35 +00003785 // Parse the declaration-specifiers.
John McCall54abf7d2009-11-04 02:18:39 +00003786 // Just use the ParsingDeclaration "scope" of the declarator.
John McCall0b7e6782011-03-24 11:26:52 +00003787 DeclSpec DS(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00003788
3789 // Skip any Microsoft attributes before a param.
3790 if (getLang().Microsoft && Tok.is(tok::l_square))
3791 ParseMicrosoftAttributes(DS.getAttributes());
3792
3793 SourceLocation DSStart = Tok.getLocation();
Chris Lattner7399ee02008-10-20 02:05:46 +00003794
3795 // If the caller parsed attributes for the first argument, add them now.
John McCall7f040a92010-12-24 02:08:15 +00003796 // Take them so that we only apply the attributes to the first parameter.
3797 DS.takeAttributesFrom(attrs);
3798
Chris Lattnere64c5492009-02-27 18:38:20 +00003799 ParseDeclarationSpecifiers(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00003800
Chris Lattnerf97409f2008-04-06 06:57:35 +00003801 // Parse the declarator. This is "PrototypeContext", because we must
3802 // accept either 'declarator' or 'abstract-declarator' here.
3803 Declarator ParmDecl(DS, Declarator::PrototypeContext);
3804 ParseDeclarator(ParmDecl);
3805
3806 // Parse GNU attributes, if present.
John McCall7f040a92010-12-24 02:08:15 +00003807 MaybeParseGNUAttributes(ParmDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00003808
Chris Lattnerf97409f2008-04-06 06:57:35 +00003809 // Remember this parsed parameter in ParamInfo.
3810 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
Mike Stump1eb44332009-09-09 15:08:12 +00003811
Douglas Gregor72b505b2008-12-16 21:30:33 +00003812 // DefArgToks is used when the parsing of default arguments needs
3813 // to be delayed.
3814 CachedTokens *DefArgToks = 0;
3815
Chris Lattnerf97409f2008-04-06 06:57:35 +00003816 // If no parameter was specified, verify that *something* was specified,
3817 // otherwise we have a missing type and identifier.
Chris Lattnere64c5492009-02-27 18:38:20 +00003818 if (DS.isEmpty() && ParmDecl.getIdentifier() == 0 &&
3819 ParmDecl.getNumTypeObjects() == 0) {
Chris Lattnerf97409f2008-04-06 06:57:35 +00003820 // Completely missing, emit error.
3821 Diag(DSStart, diag::err_missing_param);
3822 } else {
3823 // Otherwise, we have something. Add it and let semantic analysis try
3824 // to grok it and add the result to the ParamInfo we are building.
Mike Stump1eb44332009-09-09 15:08:12 +00003825
Chris Lattnerf97409f2008-04-06 06:57:35 +00003826 // Inform the actions module about the parameter declarator, so it gets
3827 // added to the current scope.
John McCalld226f652010-08-21 09:40:31 +00003828 Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl);
Chris Lattner04421082008-04-08 04:40:51 +00003829
3830 // Parse the default argument, if any. We parse the default
3831 // arguments in all dialects; the semantic analysis in
3832 // ActOnParamDefaultArgument will reject the default argument in
3833 // C.
3834 if (Tok.is(tok::equal)) {
Douglas Gregor61366e92008-12-24 00:01:03 +00003835 SourceLocation EqualLoc = Tok.getLocation();
3836
Chris Lattner04421082008-04-08 04:40:51 +00003837 // Parse the default argument
Douglas Gregor72b505b2008-12-16 21:30:33 +00003838 if (D.getContext() == Declarator::MemberContext) {
3839 // If we're inside a class definition, cache the tokens
3840 // corresponding to the default argument. We'll actually parse
3841 // them when we see the end of the class definition.
3842 // FIXME: Templates will require something similar.
3843 // FIXME: Can we use a smart pointer for Toks?
3844 DefArgToks = new CachedTokens;
3845
Mike Stump1eb44332009-09-09 15:08:12 +00003846 if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks,
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +00003847 /*StopAtSemi=*/true,
3848 /*ConsumeFinalToken=*/false)) {
Douglas Gregor72b505b2008-12-16 21:30:33 +00003849 delete DefArgToks;
3850 DefArgToks = 0;
Douglas Gregor61366e92008-12-24 00:01:03 +00003851 Actions.ActOnParamDefaultArgumentError(Param);
Argyrios Kyrtzidis2b602ad2010-08-06 09:47:24 +00003852 } else {
3853 // Mark the end of the default argument so that we know when to
3854 // stop when we parse it later on.
3855 Token DefArgEnd;
3856 DefArgEnd.startToken();
3857 DefArgEnd.setKind(tok::cxx_defaultarg_end);
3858 DefArgEnd.setLocation(Tok.getLocation());
3859 DefArgToks->push_back(DefArgEnd);
Mike Stump1eb44332009-09-09 15:08:12 +00003860 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc,
Anders Carlsson5e300d12009-06-12 16:51:40 +00003861 (*DefArgToks)[1].getLocation());
Argyrios Kyrtzidis2b602ad2010-08-06 09:47:24 +00003862 }
Chris Lattner04421082008-04-08 04:40:51 +00003863 } else {
Douglas Gregor72b505b2008-12-16 21:30:33 +00003864 // Consume the '='.
Douglas Gregor61366e92008-12-24 00:01:03 +00003865 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00003866
Douglas Gregorbe0f7bd2010-09-11 20:24:53 +00003867 // The argument isn't actually potentially evaluated unless it is
3868 // used.
3869 EnterExpressionEvaluationContext Eval(Actions,
3870 Sema::PotentiallyEvaluatedIfUsed);
3871
John McCall60d7b3a2010-08-24 06:29:42 +00003872 ExprResult DefArgResult(ParseAssignmentExpression());
Douglas Gregor72b505b2008-12-16 21:30:33 +00003873 if (DefArgResult.isInvalid()) {
3874 Actions.ActOnParamDefaultArgumentError(Param);
3875 SkipUntil(tok::comma, tok::r_paren, true, true);
3876 } else {
3877 // Inform the actions module about the default argument
3878 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
John McCall9ae2f072010-08-23 23:25:46 +00003879 DefArgResult.take());
Douglas Gregor72b505b2008-12-16 21:30:33 +00003880 }
Chris Lattner04421082008-04-08 04:40:51 +00003881 }
3882 }
Mike Stump1eb44332009-09-09 15:08:12 +00003883
3884 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
3885 ParmDecl.getIdentifierLoc(), Param,
Douglas Gregor72b505b2008-12-16 21:30:33 +00003886 DefArgToks));
Chris Lattnerf97409f2008-04-06 06:57:35 +00003887 }
3888
3889 // If the next token is a comma, consume it and keep reading arguments.
Douglas Gregored5d6512009-09-22 21:41:40 +00003890 if (Tok.isNot(tok::comma)) {
3891 if (Tok.is(tok::ellipsis)) {
3892 IsVariadic = true;
3893 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
3894
3895 if (!getLang().CPlusPlus) {
3896 // We have ellipsis without a preceding ',', which is ill-formed
3897 // in C. Complain and provide the fix.
3898 Diag(EllipsisLoc, diag::err_missing_comma_before_ellipsis)
Douglas Gregor849b2432010-03-31 17:46:05 +00003899 << FixItHint::CreateInsertion(EllipsisLoc, ", ");
Douglas Gregored5d6512009-09-22 21:41:40 +00003900 }
3901 }
3902
3903 break;
3904 }
Mike Stump1eb44332009-09-09 15:08:12 +00003905
Chris Lattnerf97409f2008-04-06 06:57:35 +00003906 // Consume the comma.
3907 ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00003908 }
Mike Stump1eb44332009-09-09 15:08:12 +00003909
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003910 // If we have the closing ')', eat it.
Abramo Bagnara796aa442011-03-12 11:17:06 +00003911 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003912
John McCall0b7e6782011-03-24 11:26:52 +00003913 DeclSpec DS(AttrFactory);
Douglas Gregor83f51722011-01-26 03:43:54 +00003914 SourceLocation RefQualifierLoc;
3915 bool RefQualifierIsLValueRef = true;
Sebastian Redl7acafd02011-03-05 14:45:16 +00003916 ExceptionSpecificationType ESpecType = EST_None;
3917 SourceRange ESpecRange;
3918 llvm::SmallVector<ParsedType, 2> DynamicExceptions;
3919 llvm::SmallVector<SourceRange, 2> DynamicExceptionRanges;
3920 ExprResult NoexceptExpr;
Sean Huntbbd37c62009-11-21 08:43:09 +00003921
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003922 if (getLang().CPlusPlus) {
John McCall7f040a92010-12-24 02:08:15 +00003923 MaybeParseCXX0XAttributes(attrs);
3924
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00003925 // Parse cv-qualifier-seq[opt].
Chris Lattner5a69d1c2008-12-18 07:02:59 +00003926 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Sebastian Redlab197ba2009-02-09 18:23:29 +00003927 if (!DS.getSourceRange().getEnd().isInvalid())
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00003928 EndLoc = DS.getSourceRange().getEnd();
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00003929
Douglas Gregor83f51722011-01-26 03:43:54 +00003930 // Parse ref-qualifier[opt]
3931 if (Tok.is(tok::amp) || Tok.is(tok::ampamp)) {
3932 if (!getLang().CPlusPlus0x)
Douglas Gregor1f381062011-01-26 20:35:32 +00003933 Diag(Tok, diag::ext_ref_qualifier);
Douglas Gregor83f51722011-01-26 03:43:54 +00003934
3935 RefQualifierIsLValueRef = Tok.is(tok::amp);
3936 RefQualifierLoc = ConsumeToken();
3937 EndLoc = RefQualifierLoc;
3938 }
3939
Sebastian Redl7acafd02011-03-05 14:45:16 +00003940 // FIXME: We should leave the prototype scope before parsing the exception
3941 // specification, and then reenter it when parsing the trailing return type.
3942 // FIXMEFIXME: Why? That wouldn't be right for the noexcept clause.
3943
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00003944 // Parse exception-specification[opt].
Sebastian Redl7acafd02011-03-05 14:45:16 +00003945 ESpecType = MaybeParseExceptionSpecification(ESpecRange,
3946 DynamicExceptions,
3947 DynamicExceptionRanges,
3948 NoexceptExpr);
3949 if (ESpecType != EST_None)
3950 EndLoc = ESpecRange.getEnd();
Douglas Gregordab60ad2010-10-01 18:44:50 +00003951
3952 // Parse trailing-return-type.
3953 if (getLang().CPlusPlus0x && Tok.is(tok::arrow)) {
3954 TrailingReturnType = ParseTrailingReturnType().get();
3955 }
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003956 }
3957
Douglas Gregordab60ad2010-10-01 18:44:50 +00003958 // Leave prototype scope.
3959 PrototypeScope.Exit();
3960
Reid Spencer5f016e22007-07-11 17:01:13 +00003961 // Remember that we parsed a function type, and remember the attributes.
John McCall0b7e6782011-03-24 11:26:52 +00003962 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/true, IsVariadic,
Douglas Gregor965acbb2009-02-18 07:07:28 +00003963 EllipsisLoc,
Jay Foadbeaaccd2009-05-21 09:52:38 +00003964 ParamInfo.data(), ParamInfo.size(),
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003965 DS.getTypeQualifiers(),
Douglas Gregor83f51722011-01-26 03:43:54 +00003966 RefQualifierIsLValueRef,
3967 RefQualifierLoc,
Sebastian Redl6e5d3192011-03-05 22:42:13 +00003968 ESpecType, ESpecRange.getBegin(),
Sebastian Redl7acafd02011-03-05 14:45:16 +00003969 DynamicExceptions.data(),
3970 DynamicExceptionRanges.data(),
3971 DynamicExceptions.size(),
Sebastian Redl6e5d3192011-03-05 22:42:13 +00003972 NoexceptExpr.isUsable() ?
3973 NoexceptExpr.get() : 0,
Abramo Bagnara796aa442011-03-12 11:17:06 +00003974 LParenLoc, EndLoc, D,
Douglas Gregordab60ad2010-10-01 18:44:50 +00003975 TrailingReturnType),
John McCall0b7e6782011-03-24 11:26:52 +00003976 attrs, EndLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00003977}
3978
Chris Lattner66d28652008-04-06 06:34:08 +00003979/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
3980/// we found a K&R-style identifier list instead of a type argument list. The
Chris Lattner83a94472010-05-14 17:23:36 +00003981/// first identifier has already been consumed, and the current token is the
3982/// token right after it.
Chris Lattner66d28652008-04-06 06:34:08 +00003983///
3984/// identifier-list: [C99 6.7.5]
3985/// identifier
3986/// identifier-list ',' identifier
3987///
3988void Parser::ParseFunctionDeclaratorIdentifierList(SourceLocation LParenLoc,
Chris Lattner83a94472010-05-14 17:23:36 +00003989 IdentifierInfo *FirstIdent,
3990 SourceLocation FirstIdentLoc,
Chris Lattner66d28652008-04-06 06:34:08 +00003991 Declarator &D) {
3992 // Build up an array of information about the parsed arguments.
3993 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
3994 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
Mike Stump1eb44332009-09-09 15:08:12 +00003995
Chris Lattner66d28652008-04-06 06:34:08 +00003996 // If there was no identifier specified for the declarator, either we are in
3997 // an abstract-declarator, or we are in a parameter declarator which was found
3998 // to be abstract. In abstract-declarators, identifier lists are not valid:
3999 // diagnose this.
4000 if (!D.getIdentifier())
Chris Lattner83a94472010-05-14 17:23:36 +00004001 Diag(FirstIdentLoc, diag::ext_ident_list_in_param);
Chris Lattner66d28652008-04-06 06:34:08 +00004002
Chris Lattner83a94472010-05-14 17:23:36 +00004003 // The first identifier was already read, and is known to be the first
4004 // identifier in the list. Remember this identifier in ParamInfo.
4005 ParamsSoFar.insert(FirstIdent);
John McCalld226f652010-08-21 09:40:31 +00004006 ParamInfo.push_back(DeclaratorChunk::ParamInfo(FirstIdent, FirstIdentLoc, 0));
Mike Stump1eb44332009-09-09 15:08:12 +00004007
Chris Lattner66d28652008-04-06 06:34:08 +00004008 while (Tok.is(tok::comma)) {
4009 // Eat the comma.
4010 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00004011
Chris Lattner50c64772008-04-06 06:39:19 +00004012 // If this isn't an identifier, report the error and skip until ')'.
Chris Lattner66d28652008-04-06 06:34:08 +00004013 if (Tok.isNot(tok::identifier)) {
4014 Diag(Tok, diag::err_expected_ident);
Chris Lattner50c64772008-04-06 06:39:19 +00004015 SkipUntil(tok::r_paren);
4016 return;
Chris Lattner66d28652008-04-06 06:34:08 +00004017 }
Chris Lattneraaf9ddb2008-04-06 06:47:48 +00004018
Chris Lattner66d28652008-04-06 06:34:08 +00004019 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
Chris Lattneraaf9ddb2008-04-06 06:47:48 +00004020
4021 // Reject 'typedef int y; int test(x, y)', but continue parsing.
Douglas Gregor23c94db2010-07-02 17:43:08 +00004022 if (Actions.getTypeName(*ParmII, Tok.getLocation(), getCurScope()))
Chris Lattnerda83bac2008-11-19 07:37:42 +00004023 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
Mike Stump1eb44332009-09-09 15:08:12 +00004024
Chris Lattner66d28652008-04-06 06:34:08 +00004025 // Verify that the argument identifier has not already been mentioned.
4026 if (!ParamsSoFar.insert(ParmII)) {
Chris Lattnerda83bac2008-11-19 07:37:42 +00004027 Diag(Tok, diag::err_param_redefinition) << ParmII;
Chris Lattner50c64772008-04-06 06:39:19 +00004028 } else {
4029 // Remember this identifier in ParamInfo.
Chris Lattner66d28652008-04-06 06:34:08 +00004030 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Chris Lattnerb28317a2009-03-28 19:18:32 +00004031 Tok.getLocation(),
John McCalld226f652010-08-21 09:40:31 +00004032 0));
Chris Lattner50c64772008-04-06 06:39:19 +00004033 }
Mike Stump1eb44332009-09-09 15:08:12 +00004034
Chris Lattner66d28652008-04-06 06:34:08 +00004035 // Eat the identifier.
4036 ConsumeToken();
4037 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00004038
4039 // If we have the closing ')', eat it and we're done.
4040 SourceLocation RLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
4041
Chris Lattner50c64772008-04-06 06:39:19 +00004042 // Remember that we parsed a function type, and remember the attributes. This
4043 // function type is always a K&R style function type, which is not varargs and
4044 // has no prototype.
John McCall0b7e6782011-03-24 11:26:52 +00004045 ParsedAttributes attrs(AttrFactory);
4046 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/false, /*varargs*/false,
Douglas Gregor965acbb2009-02-18 07:07:28 +00004047 SourceLocation(),
Chris Lattner50c64772008-04-06 06:39:19 +00004048 &ParamInfo[0], ParamInfo.size(),
Sebastian Redl7dc81342009-04-29 17:30:04 +00004049 /*TypeQuals*/0,
Douglas Gregor83f51722011-01-26 03:43:54 +00004050 true, SourceLocation(),
Sebastian Redl6e5d3192011-03-05 22:42:13 +00004051 EST_None, SourceLocation(), 0, 0,
4052 0, 0, LParenLoc, RLoc, D),
John McCall0b7e6782011-03-24 11:26:52 +00004053 attrs, RLoc);
Chris Lattner66d28652008-04-06 06:34:08 +00004054}
Chris Lattneref4715c2008-04-06 05:45:57 +00004055
Reid Spencer5f016e22007-07-11 17:01:13 +00004056/// [C90] direct-declarator '[' constant-expression[opt] ']'
4057/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
4058/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
4059/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
4060/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
4061void Parser::ParseBracketDeclarator(Declarator &D) {
4062 SourceLocation StartLoc = ConsumeBracket();
Mike Stump1eb44332009-09-09 15:08:12 +00004063
Chris Lattner378c7e42008-12-18 07:27:21 +00004064 // C array syntax has many features, but by-far the most common is [] and [4].
4065 // This code does a fast path to handle some of the most obvious cases.
4066 if (Tok.getKind() == tok::r_square) {
Sebastian Redlab197ba2009-02-09 18:23:29 +00004067 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
John McCall0b7e6782011-03-24 11:26:52 +00004068 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00004069 MaybeParseCXX0XAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00004070
Chris Lattner378c7e42008-12-18 07:27:21 +00004071 // Remember that we parsed the empty array type.
John McCall60d7b3a2010-08-24 06:29:42 +00004072 ExprResult NumElements;
John McCall0b7e6782011-03-24 11:26:52 +00004073 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00004074 StartLoc, EndLoc),
John McCall0b7e6782011-03-24 11:26:52 +00004075 attrs, EndLoc);
Chris Lattner378c7e42008-12-18 07:27:21 +00004076 return;
4077 } else if (Tok.getKind() == tok::numeric_constant &&
4078 GetLookAheadToken(1).is(tok::r_square)) {
4079 // [4] is very common. Parse the numeric constant expression.
John McCall60d7b3a2010-08-24 06:29:42 +00004080 ExprResult ExprRes(Actions.ActOnNumericConstant(Tok));
Chris Lattner378c7e42008-12-18 07:27:21 +00004081 ConsumeToken();
4082
Sebastian Redlab197ba2009-02-09 18:23:29 +00004083 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
John McCall0b7e6782011-03-24 11:26:52 +00004084 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00004085 MaybeParseCXX0XAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00004086
Chris Lattner378c7e42008-12-18 07:27:21 +00004087 // Remember that we parsed a array type, and remember its features.
John McCall0b7e6782011-03-24 11:26:52 +00004088 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, 0,
John McCall7f040a92010-12-24 02:08:15 +00004089 ExprRes.release(),
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00004090 StartLoc, EndLoc),
John McCall0b7e6782011-03-24 11:26:52 +00004091 attrs, EndLoc);
Chris Lattner378c7e42008-12-18 07:27:21 +00004092 return;
4093 }
Mike Stump1eb44332009-09-09 15:08:12 +00004094
Reid Spencer5f016e22007-07-11 17:01:13 +00004095 // If valid, this location is the position where we read the 'static' keyword.
4096 SourceLocation StaticLoc;
Chris Lattner04d66662007-10-09 17:33:22 +00004097 if (Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00004098 StaticLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00004099
Reid Spencer5f016e22007-07-11 17:01:13 +00004100 // If there is a type-qualifier-list, read it now.
Chris Lattnera1fcbad2008-12-18 06:50:14 +00004101 // Type qualifiers in an array subscript are a C99 feature.
John McCall0b7e6782011-03-24 11:26:52 +00004102 DeclSpec DS(AttrFactory);
Chris Lattner5a69d1c2008-12-18 07:02:59 +00004103 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Mike Stump1eb44332009-09-09 15:08:12 +00004104
Reid Spencer5f016e22007-07-11 17:01:13 +00004105 // If we haven't already read 'static', check to see if there is one after the
4106 // type-qualifier-list.
Chris Lattner04d66662007-10-09 17:33:22 +00004107 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00004108 StaticLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00004109
Reid Spencer5f016e22007-07-11 17:01:13 +00004110 // Handle "direct-declarator [ type-qual-list[opt] * ]".
4111 bool isStar = false;
John McCall60d7b3a2010-08-24 06:29:42 +00004112 ExprResult NumElements;
Mike Stump1eb44332009-09-09 15:08:12 +00004113
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00004114 // Handle the case where we have '[*]' as the array size. However, a leading
4115 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
4116 // the the token after the star is a ']'. Since stars in arrays are
4117 // infrequent, use of lookahead is not costly here.
4118 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattnera711dd02008-04-06 05:27:21 +00004119 ConsumeToken(); // Eat the '*'.
Reid Spencer5f016e22007-07-11 17:01:13 +00004120
Chris Lattnera1fcbad2008-12-18 06:50:14 +00004121 if (StaticLoc.isValid()) {
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00004122 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattnera1fcbad2008-12-18 06:50:14 +00004123 StaticLoc = SourceLocation(); // Drop the static.
4124 }
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00004125 isStar = true;
Chris Lattner04d66662007-10-09 17:33:22 +00004126 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner378c7e42008-12-18 07:27:21 +00004127 // Note, in C89, this production uses the constant-expr production instead
4128 // of assignment-expr. The only difference is that assignment-expr allows
4129 // things like '=' and '*='. Sema rejects these in C89 mode because they
4130 // are not i-c-e's, so we don't need to distinguish between the two here.
Mike Stump1eb44332009-09-09 15:08:12 +00004131
Douglas Gregore0762c92009-06-19 23:52:42 +00004132 // Parse the constant-expression or assignment-expression now (depending
4133 // on dialect).
4134 if (getLang().CPlusPlus)
4135 NumElements = ParseConstantExpression();
4136 else
4137 NumElements = ParseAssignmentExpression();
Reid Spencer5f016e22007-07-11 17:01:13 +00004138 }
Mike Stump1eb44332009-09-09 15:08:12 +00004139
Reid Spencer5f016e22007-07-11 17:01:13 +00004140 // If there was an error parsing the assignment-expression, recover.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00004141 if (NumElements.isInvalid()) {
Chris Lattner5cb10d32009-04-24 22:30:50 +00004142 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00004143 // If the expression was invalid, skip it.
4144 SkipUntil(tok::r_square);
4145 return;
4146 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00004147
4148 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
4149
John McCall0b7e6782011-03-24 11:26:52 +00004150 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00004151 MaybeParseCXX0XAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00004152
Chris Lattner378c7e42008-12-18 07:27:21 +00004153 // Remember that we parsed a array type, and remember its features.
John McCall0b7e6782011-03-24 11:26:52 +00004154 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
Reid Spencer5f016e22007-07-11 17:01:13 +00004155 StaticLoc.isValid(), isStar,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00004156 NumElements.release(),
4157 StartLoc, EndLoc),
John McCall0b7e6782011-03-24 11:26:52 +00004158 attrs, EndLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00004159}
4160
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004161/// [GNU] typeof-specifier:
4162/// typeof ( expressions )
4163/// typeof ( type-name )
4164/// [GNU/C++] typeof unary-expression
Steve Naroffd1861fd2007-07-31 12:34:36 +00004165///
4166void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner04d66662007-10-09 17:33:22 +00004167 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004168 Token OpTok = Tok;
Steve Naroffd1861fd2007-07-31 12:34:36 +00004169 SourceLocation StartLoc = ConsumeToken();
4170
John McCallcfb708c2010-01-13 20:03:27 +00004171 const bool hasParens = Tok.is(tok::l_paren);
4172
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004173 bool isCastExpr;
John McCallb3d87482010-08-24 05:47:05 +00004174 ParsedType CastTy;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004175 SourceRange CastRange;
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00004176 ExprResult Operand = ParseExprAfterUnaryExprOrTypeTrait(OpTok, isCastExpr,
4177 CastTy, CastRange);
John McCallcfb708c2010-01-13 20:03:27 +00004178 if (hasParens)
4179 DS.setTypeofParensRange(CastRange);
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004180
4181 if (CastRange.getEnd().isInvalid())
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004182 // FIXME: Not accurate, the range gets one token more than it should.
4183 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004184 else
4185 DS.SetRangeEnd(CastRange.getEnd());
Mike Stump1eb44332009-09-09 15:08:12 +00004186
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004187 if (isCastExpr) {
4188 if (!CastTy) {
4189 DS.SetTypeSpecError();
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004190 return;
Douglas Gregor809070a2009-02-18 17:45:20 +00004191 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004192
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004193 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00004194 unsigned DiagID;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004195 // Check for duplicate type specifiers (e.g. "int typeof(int)").
4196 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00004197 DiagID, CastTy))
4198 Diag(StartLoc, DiagID) << PrevSpec;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004199 return;
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004200 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004201
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004202 // If we get here, the operand to the typeof was an expresion.
4203 if (Operand.isInvalid()) {
4204 DS.SetTypeSpecError();
Steve Naroff9dfa7b42007-08-02 02:53:48 +00004205 return;
Steve Naroffd1861fd2007-07-31 12:34:36 +00004206 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004207
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004208 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00004209 unsigned DiagID;
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004210 // Check for duplicate type specifiers (e.g. "int typeof(int)").
4211 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
John McCallb3d87482010-08-24 05:47:05 +00004212 DiagID, Operand.get()))
John McCallfec54012009-08-03 20:12:06 +00004213 Diag(StartLoc, DiagID) << PrevSpec;
Steve Naroffd1861fd2007-07-31 12:34:36 +00004214}
Chris Lattner1b492422010-02-28 18:33:55 +00004215
4216
4217/// TryAltiVecVectorTokenOutOfLine - Out of line body that should only be called
4218/// from TryAltiVecVectorToken.
4219bool Parser::TryAltiVecVectorTokenOutOfLine() {
4220 Token Next = NextToken();
4221 switch (Next.getKind()) {
4222 default: return false;
4223 case tok::kw_short:
4224 case tok::kw_long:
4225 case tok::kw_signed:
4226 case tok::kw_unsigned:
4227 case tok::kw_void:
4228 case tok::kw_char:
4229 case tok::kw_int:
4230 case tok::kw_float:
4231 case tok::kw_double:
4232 case tok::kw_bool:
4233 case tok::kw___pixel:
4234 Tok.setKind(tok::kw___vector);
4235 return true;
4236 case tok::identifier:
4237 if (Next.getIdentifierInfo() == Ident_pixel) {
4238 Tok.setKind(tok::kw___vector);
4239 return true;
4240 }
4241 return false;
4242 }
4243}
4244
4245bool Parser::TryAltiVecTokenOutOfLine(DeclSpec &DS, SourceLocation Loc,
4246 const char *&PrevSpec, unsigned &DiagID,
4247 bool &isInvalid) {
4248 if (Tok.getIdentifierInfo() == Ident_vector) {
4249 Token Next = NextToken();
4250 switch (Next.getKind()) {
4251 case tok::kw_short:
4252 case tok::kw_long:
4253 case tok::kw_signed:
4254 case tok::kw_unsigned:
4255 case tok::kw_void:
4256 case tok::kw_char:
4257 case tok::kw_int:
4258 case tok::kw_float:
4259 case tok::kw_double:
4260 case tok::kw_bool:
4261 case tok::kw___pixel:
4262 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
4263 return true;
4264 case tok::identifier:
4265 if (Next.getIdentifierInfo() == Ident_pixel) {
4266 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
4267 return true;
4268 }
4269 break;
4270 default:
4271 break;
4272 }
Douglas Gregora8f031f2010-06-16 15:28:57 +00004273 } else if ((Tok.getIdentifierInfo() == Ident_pixel) &&
Chris Lattner1b492422010-02-28 18:33:55 +00004274 DS.isTypeAltiVecVector()) {
4275 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
4276 return true;
4277 }
4278 return false;
4279}