blob: d553687837e6c547ff594b4e23692d63031e79f4 [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:
174 case tok::kw_signed:
175 case tok::kw_unsigned:
176 case tok::kw_float:
177 case tok::kw_double:
178 case tok::kw_void:
John McCall7f040a92010-12-24 02:08:15 +0000179 case tok::kw_typeof: {
180 AttributeList *attr
John McCall0b7e6782011-03-24 11:26:52 +0000181 = attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc,
182 0, SourceLocation(), 0, 0);
John McCall7f040a92010-12-24 02:08:15 +0000183 if (attr->getKind() == AttributeList::AT_IBOutletCollection)
Fariborz Jahanian1b72fa72010-08-17 23:19:16 +0000184 Diag(Tok, diag::err_iboutletcollection_builtintype);
Nate Begeman6f3d8382009-06-26 06:32:41 +0000185 // If it's a builtin type name, eat it and expect a rparen
186 // __attribute__(( vec_type_hint(char) ))
187 ConsumeToken();
Nate Begeman6f3d8382009-06-26 06:32:41 +0000188 if (Tok.is(tok::r_paren))
189 ConsumeParen();
190 break;
John McCall7f040a92010-12-24 02:08:15 +0000191 }
Nate Begeman6f3d8382009-06-26 06:32:41 +0000192 default:
Reid Spencer5f016e22007-07-11 17:01:13 +0000193 // __attribute__(( aligned(16) ))
Sebastian Redla55e52c2008-11-25 22:21:31 +0000194 ExprVector ArgExprs(Actions);
Reid Spencer5f016e22007-07-11 17:01:13 +0000195 bool ArgExprsOk = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000196
Reid Spencer5f016e22007-07-11 17:01:13 +0000197 // now parse the list of expressions
198 while (1) {
John McCall60d7b3a2010-08-24 06:29:42 +0000199 ExprResult ArgExpr(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000200 if (ArgExpr.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000201 ArgExprsOk = false;
202 SkipUntil(tok::r_paren);
203 break;
204 } else {
Sebastian Redleffa8d12008-12-10 00:02:53 +0000205 ArgExprs.push_back(ArgExpr.release());
Reid Spencer5f016e22007-07-11 17:01:13 +0000206 }
Chris Lattner04d66662007-10-09 17:33:22 +0000207 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +0000208 break;
209 ConsumeToken(); // Eat the comma, move to the next argument
210 }
211 // Match the ')'.
Chris Lattner04d66662007-10-09 17:33:22 +0000212 if (ArgExprsOk && Tok.is(tok::r_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000213 ConsumeParen(); // ignore the right paren loc for now
John McCall0b7e6782011-03-24 11:26:52 +0000214 attrs.addNew(AttrName, AttrNameLoc, 0,
215 AttrNameLoc, 0, SourceLocation(),
216 ArgExprs.take(), ArgExprs.size());
Reid Spencer5f016e22007-07-11 17:01:13 +0000217 }
Nate Begeman6f3d8382009-06-26 06:32:41 +0000218 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000219 }
220 }
221 } else {
John McCall0b7e6782011-03-24 11:26:52 +0000222 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc,
223 0, SourceLocation(), 0, 0);
Reid Spencer5f016e22007-07-11 17:01:13 +0000224 }
225 }
226 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
Reid Spencer5f016e22007-07-11 17:01:13 +0000227 SkipUntil(tok::r_paren, false);
Sean Huntbbd37c62009-11-21 08:43:09 +0000228 SourceLocation Loc = Tok.getLocation();
Sebastian Redlab197ba2009-02-09 18:23:29 +0000229 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) {
230 SkipUntil(tok::r_paren, false);
231 }
John McCall7f040a92010-12-24 02:08:15 +0000232 if (endLoc)
233 *endLoc = Loc;
Reid Spencer5f016e22007-07-11 17:01:13 +0000234 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000235}
236
Eli Friedmana23b4852009-06-08 07:21:15 +0000237/// ParseMicrosoftDeclSpec - Parse an __declspec construct
238///
239/// [MS] decl-specifier:
240/// __declspec ( extended-decl-modifier-seq )
241///
242/// [MS] extended-decl-modifier-seq:
243/// extended-decl-modifier[opt]
244/// extended-decl-modifier extended-decl-modifier-seq
245
John McCall7f040a92010-12-24 02:08:15 +0000246void Parser::ParseMicrosoftDeclSpec(ParsedAttributes &attrs) {
Steve Narofff59e17e2008-12-24 20:59:21 +0000247 assert(Tok.is(tok::kw___declspec) && "Not a declspec!");
Eli Friedmana23b4852009-06-08 07:21:15 +0000248
Steve Narofff59e17e2008-12-24 20:59:21 +0000249 ConsumeToken();
Eli Friedmana23b4852009-06-08 07:21:15 +0000250 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
251 "declspec")) {
252 SkipUntil(tok::r_paren, true); // skip until ) or ;
John McCall7f040a92010-12-24 02:08:15 +0000253 return;
Eli Friedmana23b4852009-06-08 07:21:15 +0000254 }
Eli Friedman290eeb02009-06-08 23:27:34 +0000255 while (Tok.getIdentifierInfo()) {
Eli Friedmana23b4852009-06-08 07:21:15 +0000256 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
257 SourceLocation AttrNameLoc = ConsumeToken();
258 if (Tok.is(tok::l_paren)) {
259 ConsumeParen();
260 // FIXME: This doesn't parse __declspec(property(get=get_func_name))
261 // correctly.
John McCall60d7b3a2010-08-24 06:29:42 +0000262 ExprResult ArgExpr(ParseAssignmentExpression());
Eli Friedmana23b4852009-06-08 07:21:15 +0000263 if (!ArgExpr.isInvalid()) {
John McCallca0408f2010-08-23 06:44:23 +0000264 Expr *ExprList = ArgExpr.take();
John McCall0b7e6782011-03-24 11:26:52 +0000265 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
266 SourceLocation(), &ExprList, 1, true);
Eli Friedmana23b4852009-06-08 07:21:15 +0000267 }
268 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
269 SkipUntil(tok::r_paren, false);
270 } else {
John McCall0b7e6782011-03-24 11:26:52 +0000271 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc,
272 0, SourceLocation(), 0, 0, true);
Eli Friedmana23b4852009-06-08 07:21:15 +0000273 }
274 }
275 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
276 SkipUntil(tok::r_paren, false);
John McCall7f040a92010-12-24 02:08:15 +0000277 return;
Eli Friedman290eeb02009-06-08 23:27:34 +0000278}
279
John McCall7f040a92010-12-24 02:08:15 +0000280void Parser::ParseMicrosoftTypeAttributes(ParsedAttributes &attrs) {
Eli Friedman290eeb02009-06-08 23:27:34 +0000281 // Treat these like attributes
282 // FIXME: Allow Sema to distinguish between these and real attributes!
283 while (Tok.is(tok::kw___fastcall) || Tok.is(tok::kw___stdcall) ||
Douglas Gregorf813a2c2010-05-18 16:57:00 +0000284 Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___cdecl) ||
285 Tok.is(tok::kw___ptr64) || Tok.is(tok::kw___w64)) {
Eli Friedman290eeb02009-06-08 23:27:34 +0000286 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
287 SourceLocation AttrNameLoc = ConsumeToken();
288 if (Tok.is(tok::kw___ptr64) || Tok.is(tok::kw___w64))
289 // FIXME: Support these properly!
290 continue;
John McCall0b7e6782011-03-24 11:26:52 +0000291 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
292 SourceLocation(), 0, 0, true);
Eli Friedman290eeb02009-06-08 23:27:34 +0000293 }
Steve Narofff59e17e2008-12-24 20:59:21 +0000294}
295
John McCall7f040a92010-12-24 02:08:15 +0000296void Parser::ParseBorlandTypeAttributes(ParsedAttributes &attrs) {
Dawn Perchik52fc3142010-09-03 01:29:35 +0000297 // Treat these like attributes
298 while (Tok.is(tok::kw___pascal)) {
299 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
300 SourceLocation AttrNameLoc = ConsumeToken();
John McCall0b7e6782011-03-24 11:26:52 +0000301 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
302 SourceLocation(), 0, 0, true);
Dawn Perchik52fc3142010-09-03 01:29:35 +0000303 }
John McCall7f040a92010-12-24 02:08:15 +0000304}
305
Peter Collingbournef315fa82011-02-14 01:42:53 +0000306void Parser::ParseOpenCLAttributes(ParsedAttributes &attrs) {
307 // Treat these like attributes
308 while (Tok.is(tok::kw___kernel)) {
309 SourceLocation AttrNameLoc = ConsumeToken();
John McCall0b7e6782011-03-24 11:26:52 +0000310 attrs.addNew(PP.getIdentifierInfo("opencl_kernel_function"),
311 AttrNameLoc, 0, AttrNameLoc, 0,
312 SourceLocation(), 0, 0, false);
Peter Collingbournef315fa82011-02-14 01:42:53 +0000313 }
314}
315
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000316void Parser::ParseOpenCLQualifiers(DeclSpec &DS) {
317 SourceLocation Loc = Tok.getLocation();
318 switch(Tok.getKind()) {
319 // OpenCL qualifiers:
320 case tok::kw___private:
321 case tok::kw_private:
John McCall0b7e6782011-03-24 11:26:52 +0000322 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000323 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000324 PP.getIdentifierInfo("address_space"), Loc, 0);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000325 break;
326
327 case tok::kw___global:
John McCall0b7e6782011-03-24 11:26:52 +0000328 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000329 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000330 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_global);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000331 break;
332
333 case tok::kw___local:
John McCall0b7e6782011-03-24 11:26:52 +0000334 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000335 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000336 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_local);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000337 break;
338
339 case tok::kw___constant:
John McCall0b7e6782011-03-24 11:26:52 +0000340 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000341 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000342 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_constant);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000343 break;
344
345 case tok::kw___read_only:
John McCall0b7e6782011-03-24 11:26:52 +0000346 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000347 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000348 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_read_only);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000349 break;
350
351 case tok::kw___write_only:
John McCall0b7e6782011-03-24 11:26:52 +0000352 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000353 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000354 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_write_only);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000355 break;
356
357 case tok::kw___read_write:
John McCall0b7e6782011-03-24 11:26:52 +0000358 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000359 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000360 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_read_write);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000361 break;
362 default: break;
363 }
364}
365
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000366/// \brief Parse a version number.
367///
368/// version:
369/// simple-integer
370/// simple-integer ',' simple-integer
371/// simple-integer ',' simple-integer ',' simple-integer
372VersionTuple Parser::ParseVersionTuple(SourceRange &Range) {
373 Range = Tok.getLocation();
374
375 if (!Tok.is(tok::numeric_constant)) {
376 Diag(Tok, diag::err_expected_version);
377 SkipUntil(tok::comma, tok::r_paren, true, true, true);
378 return VersionTuple();
379 }
380
381 // Parse the major (and possibly minor and subminor) versions, which
382 // are stored in the numeric constant. We utilize a quirk of the
383 // lexer, which is that it handles something like 1.2.3 as a single
384 // numeric constant, rather than two separate tokens.
385 llvm::SmallString<512> Buffer;
386 Buffer.resize(Tok.getLength()+1);
387 const char *ThisTokBegin = &Buffer[0];
388
389 // Get the spelling of the token, which eliminates trigraphs, etc.
390 bool Invalid = false;
391 unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin, &Invalid);
392 if (Invalid)
393 return VersionTuple();
394
395 // Parse the major version.
396 unsigned AfterMajor = 0;
397 unsigned Major = 0;
398 while (AfterMajor < ActualLength && isdigit(ThisTokBegin[AfterMajor])) {
399 Major = Major * 10 + ThisTokBegin[AfterMajor] - '0';
400 ++AfterMajor;
401 }
402
403 if (AfterMajor == 0) {
404 Diag(Tok, diag::err_expected_version);
405 SkipUntil(tok::comma, tok::r_paren, true, true, true);
406 return VersionTuple();
407 }
408
409 if (AfterMajor == ActualLength) {
410 ConsumeToken();
411
412 // We only had a single version component.
413 if (Major == 0) {
414 Diag(Tok, diag::err_zero_version);
415 return VersionTuple();
416 }
417
418 return VersionTuple(Major);
419 }
420
421 if (ThisTokBegin[AfterMajor] != '.' || (AfterMajor + 1 == ActualLength)) {
422 Diag(Tok, diag::err_expected_version);
423 SkipUntil(tok::comma, tok::r_paren, true, true, true);
424 return VersionTuple();
425 }
426
427 // Parse the minor version.
428 unsigned AfterMinor = AfterMajor + 1;
429 unsigned Minor = 0;
430 while (AfterMinor < ActualLength && isdigit(ThisTokBegin[AfterMinor])) {
431 Minor = Minor * 10 + ThisTokBegin[AfterMinor] - '0';
432 ++AfterMinor;
433 }
434
435 if (AfterMinor == ActualLength) {
436 ConsumeToken();
437
438 // We had major.minor.
439 if (Major == 0 && Minor == 0) {
440 Diag(Tok, diag::err_zero_version);
441 return VersionTuple();
442 }
443
444 return VersionTuple(Major, Minor);
445 }
446
447 // If what follows is not a '.', we have a problem.
448 if (ThisTokBegin[AfterMinor] != '.') {
449 Diag(Tok, diag::err_expected_version);
450 SkipUntil(tok::comma, tok::r_paren, true, true, true);
451 return VersionTuple();
452 }
453
454 // Parse the subminor version.
455 unsigned AfterSubminor = AfterMinor + 1;
456 unsigned Subminor = 0;
457 while (AfterSubminor < ActualLength && isdigit(ThisTokBegin[AfterSubminor])) {
458 Subminor = Subminor * 10 + ThisTokBegin[AfterSubminor] - '0';
459 ++AfterSubminor;
460 }
461
462 if (AfterSubminor != ActualLength) {
463 Diag(Tok, diag::err_expected_version);
464 SkipUntil(tok::comma, tok::r_paren, true, true, true);
465 return VersionTuple();
466 }
467 ConsumeToken();
468 return VersionTuple(Major, Minor, Subminor);
469}
470
471/// \brief Parse the contents of the "availability" attribute.
472///
473/// availability-attribute:
474/// 'availability' '(' platform ',' version-arg-list ')'
475///
476/// platform:
477/// identifier
478///
479/// version-arg-list:
480/// version-arg
481/// version-arg ',' version-arg-list
482///
483/// version-arg:
484/// 'introduced' '=' version
485/// 'deprecated' '=' version
486/// 'removed' = version
Douglas Gregorb53e4172011-03-26 03:35:55 +0000487/// 'unavailable'
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000488void Parser::ParseAvailabilityAttribute(IdentifierInfo &Availability,
489 SourceLocation AvailabilityLoc,
490 ParsedAttributes &attrs,
491 SourceLocation *endLoc) {
492 SourceLocation PlatformLoc;
493 IdentifierInfo *Platform = 0;
494
495 enum { Introduced, Deprecated, Obsoleted, Unknown };
496 AvailabilityChange Changes[Unknown];
497
498 // Opening '('.
499 SourceLocation LParenLoc;
500 if (!Tok.is(tok::l_paren)) {
501 Diag(Tok, diag::err_expected_lparen);
502 return;
503 }
504 LParenLoc = ConsumeParen();
505
506 // Parse the platform name,
507 if (Tok.isNot(tok::identifier)) {
508 Diag(Tok, diag::err_availability_expected_platform);
509 SkipUntil(tok::r_paren);
510 return;
511 }
512 Platform = Tok.getIdentifierInfo();
513 PlatformLoc = ConsumeToken();
514
515 // Parse the ',' following the platform name.
516 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "", tok::r_paren))
517 return;
518
519 // If we haven't grabbed the pointers for the identifiers
520 // "introduced", "deprecated", and "obsoleted", do so now.
521 if (!Ident_introduced) {
522 Ident_introduced = PP.getIdentifierInfo("introduced");
523 Ident_deprecated = PP.getIdentifierInfo("deprecated");
524 Ident_obsoleted = PP.getIdentifierInfo("obsoleted");
Douglas Gregorb53e4172011-03-26 03:35:55 +0000525 Ident_unavailable = PP.getIdentifierInfo("unavailable");
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000526 }
527
528 // Parse the set of introductions/deprecations/removals.
Douglas Gregorb53e4172011-03-26 03:35:55 +0000529 SourceLocation UnavailableLoc;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000530 do {
531 if (Tok.isNot(tok::identifier)) {
532 Diag(Tok, diag::err_availability_expected_change);
533 SkipUntil(tok::r_paren);
534 return;
535 }
536 IdentifierInfo *Keyword = Tok.getIdentifierInfo();
537 SourceLocation KeywordLoc = ConsumeToken();
538
Douglas Gregorb53e4172011-03-26 03:35:55 +0000539 if (Keyword == Ident_unavailable) {
540 if (UnavailableLoc.isValid()) {
541 Diag(KeywordLoc, diag::err_availability_redundant)
542 << Keyword << SourceRange(UnavailableLoc);
543 }
544 UnavailableLoc = KeywordLoc;
545
546 if (Tok.isNot(tok::comma))
547 break;
548
549 ConsumeToken();
550 continue;
551 }
552
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000553 if (Tok.isNot(tok::equal)) {
554 Diag(Tok, diag::err_expected_equal_after)
555 << Keyword;
556 SkipUntil(tok::r_paren);
557 return;
558 }
559 ConsumeToken();
560
561 SourceRange VersionRange;
562 VersionTuple Version = ParseVersionTuple(VersionRange);
563
564 if (Version.empty()) {
565 SkipUntil(tok::r_paren);
566 return;
567 }
568
569 unsigned Index;
570 if (Keyword == Ident_introduced)
571 Index = Introduced;
572 else if (Keyword == Ident_deprecated)
573 Index = Deprecated;
574 else if (Keyword == Ident_obsoleted)
575 Index = Obsoleted;
576 else
577 Index = Unknown;
578
579 if (Index < Unknown) {
580 if (!Changes[Index].KeywordLoc.isInvalid()) {
581 Diag(KeywordLoc, diag::err_availability_redundant)
582 << Keyword
583 << SourceRange(Changes[Index].KeywordLoc,
584 Changes[Index].VersionRange.getEnd());
585 }
586
587 Changes[Index].KeywordLoc = KeywordLoc;
588 Changes[Index].Version = Version;
589 Changes[Index].VersionRange = VersionRange;
590 } else {
591 Diag(KeywordLoc, diag::err_availability_unknown_change)
592 << Keyword << VersionRange;
593 }
594
595 if (Tok.isNot(tok::comma))
596 break;
597
598 ConsumeToken();
599 } while (true);
600
601 // Closing ')'.
602 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
603 if (RParenLoc.isInvalid())
604 return;
605
606 if (endLoc)
607 *endLoc = RParenLoc;
608
Douglas Gregorb53e4172011-03-26 03:35:55 +0000609 // The 'unavailable' availability cannot be combined with any other
610 // availability changes. Make sure that hasn't happened.
611 if (UnavailableLoc.isValid()) {
612 bool Complained = false;
613 for (unsigned Index = Introduced; Index != Unknown; ++Index) {
614 if (Changes[Index].KeywordLoc.isValid()) {
615 if (!Complained) {
616 Diag(UnavailableLoc, diag::warn_availability_and_unavailable)
617 << SourceRange(Changes[Index].KeywordLoc,
618 Changes[Index].VersionRange.getEnd());
619 Complained = true;
620 }
621
622 // Clear out the availability.
623 Changes[Index] = AvailabilityChange();
624 }
625 }
626 }
627
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000628 // Record this attribute
Douglas Gregorb53e4172011-03-26 03:35:55 +0000629 attrs.addNew(&Availability, AvailabilityLoc,
John McCall0b7e6782011-03-24 11:26:52 +0000630 0, SourceLocation(),
631 Platform, PlatformLoc,
632 Changes[Introduced],
633 Changes[Deprecated],
Douglas Gregorb53e4172011-03-26 03:35:55 +0000634 Changes[Obsoleted],
635 UnavailableLoc, false, false);
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000636}
637
John McCall7f040a92010-12-24 02:08:15 +0000638void Parser::DiagnoseProhibitedAttributes(ParsedAttributesWithRange &attrs) {
639 Diag(attrs.Range.getBegin(), diag::err_attributes_not_allowed)
640 << attrs.Range;
Dawn Perchik52fc3142010-09-03 01:29:35 +0000641}
642
Reid Spencer5f016e22007-07-11 17:01:13 +0000643/// ParseDeclaration - Parse a full 'declaration', which consists of
644/// declaration-specifiers, some number of declarators, and a semicolon.
Chris Lattner97144fc2009-04-02 04:16:50 +0000645/// 'Context' should be a Declarator::TheContext value. This returns the
646/// location of the semicolon in DeclEnd.
Chris Lattner8f08cb72007-08-25 06:57:03 +0000647///
648/// declaration: [C99 6.7]
649/// block-declaration ->
650/// simple-declaration
651/// others [FIXME]
Douglas Gregoradcac882008-12-01 23:54:00 +0000652/// [C++] template-declaration
Chris Lattner8f08cb72007-08-25 06:57:03 +0000653/// [C++] namespace-definition
Douglas Gregorf780abc2008-12-30 03:27:21 +0000654/// [C++] using-directive
Douglas Gregord7f37bf2009-06-22 23:06:13 +0000655/// [C++] using-declaration
Sebastian Redl50de12f2009-03-24 22:27:57 +0000656/// [C++0x] static_assert-declaration
Chris Lattner8f08cb72007-08-25 06:57:03 +0000657/// others... [FIXME]
658///
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +0000659Parser::DeclGroupPtrTy Parser::ParseDeclaration(StmtVector &Stmts,
660 unsigned Context,
Sean Huntbbd37c62009-11-21 08:43:09 +0000661 SourceLocation &DeclEnd,
John McCall7f040a92010-12-24 02:08:15 +0000662 ParsedAttributesWithRange &attrs) {
Argyrios Kyrtzidis36d36802010-06-17 10:52:18 +0000663 ParenBraceBracketBalancer BalancerRAIIObj(*this);
664
John McCalld226f652010-08-21 09:40:31 +0000665 Decl *SingleDecl = 0;
Chris Lattner8f08cb72007-08-25 06:57:03 +0000666 switch (Tok.getKind()) {
Douglas Gregoradcac882008-12-01 23:54:00 +0000667 case tok::kw_template:
Douglas Gregor1426e532009-05-12 21:31:51 +0000668 case tok::kw_export:
John McCall7f040a92010-12-24 02:08:15 +0000669 ProhibitAttributes(attrs);
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000670 SingleDecl = ParseDeclarationStartingWithTemplate(Context, DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +0000671 break;
Sebastian Redld078e642010-08-27 23:12:46 +0000672 case tok::kw_inline:
Sebastian Redl88e64ca2010-08-31 00:36:45 +0000673 // Could be the start of an inline namespace. Allowed as an ext in C++03.
674 if (getLang().CPlusPlus && NextToken().is(tok::kw_namespace)) {
John McCall7f040a92010-12-24 02:08:15 +0000675 ProhibitAttributes(attrs);
Sebastian Redld078e642010-08-27 23:12:46 +0000676 SourceLocation InlineLoc = ConsumeToken();
677 SingleDecl = ParseNamespace(Context, DeclEnd, InlineLoc);
678 break;
679 }
John McCall7f040a92010-12-24 02:08:15 +0000680 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs,
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +0000681 true);
Chris Lattner8f08cb72007-08-25 06:57:03 +0000682 case tok::kw_namespace:
John McCall7f040a92010-12-24 02:08:15 +0000683 ProhibitAttributes(attrs);
Chris Lattner97144fc2009-04-02 04:16:50 +0000684 SingleDecl = ParseNamespace(Context, DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +0000685 break;
Douglas Gregorf780abc2008-12-30 03:27:21 +0000686 case tok::kw_using:
John McCall78b81052010-11-10 02:40:36 +0000687 SingleDecl = ParseUsingDirectiveOrDeclaration(Context, ParsedTemplateInfo(),
John McCall7f040a92010-12-24 02:08:15 +0000688 DeclEnd, attrs);
Chris Lattner682bf922009-03-29 16:50:03 +0000689 break;
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000690 case tok::kw_static_assert:
John McCall7f040a92010-12-24 02:08:15 +0000691 ProhibitAttributes(attrs);
Chris Lattner97144fc2009-04-02 04:16:50 +0000692 SingleDecl = ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +0000693 break;
Chris Lattner8f08cb72007-08-25 06:57:03 +0000694 default:
John McCall7f040a92010-12-24 02:08:15 +0000695 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs, true);
Chris Lattner8f08cb72007-08-25 06:57:03 +0000696 }
Sean Huntbbd37c62009-11-21 08:43:09 +0000697
Chris Lattner682bf922009-03-29 16:50:03 +0000698 // This routine returns a DeclGroup, if the thing we parsed only contains a
699 // single decl, convert it now.
700 return Actions.ConvertDeclToDeclGroup(SingleDecl);
Chris Lattner8f08cb72007-08-25 06:57:03 +0000701}
702
703/// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl]
704/// declaration-specifiers init-declarator-list[opt] ';'
705///[C90/C++]init-declarator-list ';' [TODO]
706/// [OMP] threadprivate-directive [TODO]
Chris Lattnercd147752009-03-29 17:27:48 +0000707///
708/// If RequireSemi is false, this does not check for a ';' at the end of the
Chris Lattner5c5db552010-04-05 18:18:31 +0000709/// declaration. If it is true, it checks for and eats it.
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +0000710Parser::DeclGroupPtrTy Parser::ParseSimpleDeclaration(StmtVector &Stmts,
711 unsigned Context,
Sean Huntbbd37c62009-11-21 08:43:09 +0000712 SourceLocation &DeclEnd,
John McCall7f040a92010-12-24 02:08:15 +0000713 ParsedAttributes &attrs,
Chris Lattner5c5db552010-04-05 18:18:31 +0000714 bool RequireSemi) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000715 // Parse the common declaration-specifiers piece.
John McCall54abf7d2009-11-04 02:18:39 +0000716 ParsingDeclSpec DS(*this);
John McCall7f040a92010-12-24 02:08:15 +0000717 DS.takeAttributesFrom(attrs);
Douglas Gregor0efc2c12010-01-13 17:31:36 +0000718 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS_none,
Richard Smith34b41d92011-02-20 03:19:35 +0000719 getDeclSpecContextFromDeclaratorContext(Context));
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +0000720 StmtResult R = Actions.ActOnVlaStmt(DS);
721 if (R.isUsable())
722 Stmts.push_back(R.release());
Mike Stump1eb44332009-09-09 15:08:12 +0000723
Reid Spencer5f016e22007-07-11 17:01:13 +0000724 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
725 // declaration-specifiers init-declarator-list[opt] ';'
Chris Lattner04d66662007-10-09 17:33:22 +0000726 if (Tok.is(tok::semi)) {
Chris Lattner5c5db552010-04-05 18:18:31 +0000727 if (RequireSemi) ConsumeToken();
John McCalld226f652010-08-21 09:40:31 +0000728 Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none,
John McCallaec03712010-05-21 20:45:30 +0000729 DS);
John McCall54abf7d2009-11-04 02:18:39 +0000730 DS.complete(TheDecl);
Chris Lattner682bf922009-03-29 16:50:03 +0000731 return Actions.ConvertDeclToDeclGroup(TheDecl);
Reid Spencer5f016e22007-07-11 17:01:13 +0000732 }
Mike Stump1eb44332009-09-09 15:08:12 +0000733
Chris Lattner5c5db552010-04-05 18:18:31 +0000734 return ParseDeclGroup(DS, Context, /*FunctionDefs=*/ false, &DeclEnd);
John McCalld8ac0572009-11-03 19:26:08 +0000735}
Mike Stump1eb44332009-09-09 15:08:12 +0000736
John McCalld8ac0572009-11-03 19:26:08 +0000737/// ParseDeclGroup - Having concluded that this is either a function
738/// definition or a group of object declarations, actually parse the
739/// result.
John McCall54abf7d2009-11-04 02:18:39 +0000740Parser::DeclGroupPtrTy Parser::ParseDeclGroup(ParsingDeclSpec &DS,
741 unsigned Context,
John McCalld8ac0572009-11-03 19:26:08 +0000742 bool AllowFunctionDefinitions,
743 SourceLocation *DeclEnd) {
744 // Parse the first declarator.
John McCall54abf7d2009-11-04 02:18:39 +0000745 ParsingDeclarator D(*this, DS, static_cast<Declarator::TheContext>(Context));
John McCalld8ac0572009-11-03 19:26:08 +0000746 ParseDeclarator(D);
Chris Lattnercd147752009-03-29 17:27:48 +0000747
John McCalld8ac0572009-11-03 19:26:08 +0000748 // Bail out if the first declarator didn't seem well-formed.
749 if (!D.hasName() && !D.mayOmitIdentifier()) {
750 // Skip until ; or }.
751 SkipUntil(tok::r_brace, true, true);
752 if (Tok.is(tok::semi))
753 ConsumeToken();
754 return DeclGroupPtrTy();
Chris Lattner23c4b182009-03-29 17:18:04 +0000755 }
Mike Stump1eb44332009-09-09 15:08:12 +0000756
Chris Lattnerc82daef2010-07-11 22:24:20 +0000757 // Check to see if we have a function *definition* which must have a body.
758 if (AllowFunctionDefinitions && D.isFunctionDeclarator() &&
759 // Look at the next token to make sure that this isn't a function
760 // declaration. We have to check this because __attribute__ might be the
761 // start of a function definition in GCC-extended K&R C.
762 !isDeclarationAfterDeclarator()) {
763
Chris Lattner004659a2010-07-11 22:42:07 +0000764 if (isStartOfFunctionDefinition(D)) {
John McCalld8ac0572009-11-03 19:26:08 +0000765 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
766 Diag(Tok, diag::err_function_declared_typedef);
767
768 // Recover by treating the 'typedef' as spurious.
769 DS.ClearStorageClassSpecs();
770 }
771
John McCalld226f652010-08-21 09:40:31 +0000772 Decl *TheDecl = ParseFunctionDefinition(D);
John McCalld8ac0572009-11-03 19:26:08 +0000773 return Actions.ConvertDeclToDeclGroup(TheDecl);
Chris Lattner004659a2010-07-11 22:42:07 +0000774 }
775
776 if (isDeclarationSpecifier()) {
777 // If there is an invalid declaration specifier right after the function
778 // prototype, then we must be in a missing semicolon case where this isn't
779 // actually a body. Just fall through into the code that handles it as a
780 // prototype, and let the top-level code handle the erroneous declspec
781 // where it would otherwise expect a comma or semicolon.
John McCalld8ac0572009-11-03 19:26:08 +0000782 } else {
783 Diag(Tok, diag::err_expected_fn_body);
784 SkipUntil(tok::semi);
785 return DeclGroupPtrTy();
786 }
787 }
788
John McCalld226f652010-08-21 09:40:31 +0000789 llvm::SmallVector<Decl *, 8> DeclsInGroup;
790 Decl *FirstDecl = ParseDeclarationAfterDeclarator(D);
John McCall54abf7d2009-11-04 02:18:39 +0000791 D.complete(FirstDecl);
John McCalld226f652010-08-21 09:40:31 +0000792 if (FirstDecl)
John McCalld8ac0572009-11-03 19:26:08 +0000793 DeclsInGroup.push_back(FirstDecl);
794
795 // If we don't have a comma, it is either the end of the list (a ';') or an
796 // error, bail out.
797 while (Tok.is(tok::comma)) {
798 // Consume the comma.
Chris Lattner23c4b182009-03-29 17:18:04 +0000799 ConsumeToken();
John McCalld8ac0572009-11-03 19:26:08 +0000800
801 // Parse the next declarator.
802 D.clear();
803
804 // Accept attributes in an init-declarator. In the first declarator in a
805 // declaration, these would be part of the declspec. In subsequent
806 // declarators, they become part of the declarator itself, so that they
807 // don't apply to declarators after *this* one. Examples:
808 // short __attribute__((common)) var; -> declspec
809 // short var __attribute__((common)); -> declarator
810 // short x, __attribute__((common)) var; -> declarator
John McCall7f040a92010-12-24 02:08:15 +0000811 MaybeParseGNUAttributes(D);
John McCalld8ac0572009-11-03 19:26:08 +0000812
813 ParseDeclarator(D);
814
John McCalld226f652010-08-21 09:40:31 +0000815 Decl *ThisDecl = ParseDeclarationAfterDeclarator(D);
John McCall54abf7d2009-11-04 02:18:39 +0000816 D.complete(ThisDecl);
John McCalld226f652010-08-21 09:40:31 +0000817 if (ThisDecl)
John McCalld8ac0572009-11-03 19:26:08 +0000818 DeclsInGroup.push_back(ThisDecl);
819 }
820
821 if (DeclEnd)
822 *DeclEnd = Tok.getLocation();
823
824 if (Context != Declarator::ForContext &&
825 ExpectAndConsume(tok::semi,
826 Context == Declarator::FileContext
827 ? diag::err_invalid_token_after_toplevel_declarator
828 : diag::err_expected_semi_declaration)) {
Chris Lattner004659a2010-07-11 22:42:07 +0000829 // Okay, there was no semicolon and one was expected. If we see a
830 // declaration specifier, just assume it was missing and continue parsing.
831 // Otherwise things are very confused and we skip to recover.
832 if (!isDeclarationSpecifier()) {
833 SkipUntil(tok::r_brace, true, true);
834 if (Tok.is(tok::semi))
835 ConsumeToken();
836 }
John McCalld8ac0572009-11-03 19:26:08 +0000837 }
838
Douglas Gregor23c94db2010-07-02 17:43:08 +0000839 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS,
John McCalld8ac0572009-11-03 19:26:08 +0000840 DeclsInGroup.data(),
841 DeclsInGroup.size());
Reid Spencer5f016e22007-07-11 17:01:13 +0000842}
843
Douglas Gregor1426e532009-05-12 21:31:51 +0000844/// \brief Parse 'declaration' after parsing 'declaration-specifiers
845/// declarator'. This method parses the remainder of the declaration
846/// (including any attributes or initializer, among other things) and
847/// finalizes the declaration.
Reid Spencer5f016e22007-07-11 17:01:13 +0000848///
Reid Spencer5f016e22007-07-11 17:01:13 +0000849/// init-declarator: [C99 6.7]
850/// declarator
851/// declarator '=' initializer
852/// [GNU] declarator simple-asm-expr[opt] attributes[opt]
853/// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +0000854/// [C++] declarator initializer[opt]
855///
856/// [C++] initializer:
857/// [C++] '=' initializer-clause
858/// [C++] '(' expression-list ')'
Sebastian Redl50de12f2009-03-24 22:27:57 +0000859/// [C++0x] '=' 'default' [TODO]
860/// [C++0x] '=' 'delete'
861///
862/// According to the standard grammar, =default and =delete are function
863/// definitions, but that definitely doesn't fit with the parser here.
Reid Spencer5f016e22007-07-11 17:01:13 +0000864///
John McCalld226f652010-08-21 09:40:31 +0000865Decl *Parser::ParseDeclarationAfterDeclarator(Declarator &D,
Douglas Gregore542c862009-06-23 23:11:28 +0000866 const ParsedTemplateInfo &TemplateInfo) {
Douglas Gregor1426e532009-05-12 21:31:51 +0000867 // If a simple-asm-expr is present, parse it.
868 if (Tok.is(tok::kw_asm)) {
869 SourceLocation Loc;
John McCall60d7b3a2010-08-24 06:29:42 +0000870 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
Douglas Gregor1426e532009-05-12 21:31:51 +0000871 if (AsmLabel.isInvalid()) {
872 SkipUntil(tok::semi, true, true);
John McCalld226f652010-08-21 09:40:31 +0000873 return 0;
Douglas Gregor1426e532009-05-12 21:31:51 +0000874 }
Mike Stump1eb44332009-09-09 15:08:12 +0000875
Douglas Gregor1426e532009-05-12 21:31:51 +0000876 D.setAsmLabel(AsmLabel.release());
877 D.SetRangeEnd(Loc);
878 }
Mike Stump1eb44332009-09-09 15:08:12 +0000879
John McCall7f040a92010-12-24 02:08:15 +0000880 MaybeParseGNUAttributes(D);
Mike Stump1eb44332009-09-09 15:08:12 +0000881
Douglas Gregor1426e532009-05-12 21:31:51 +0000882 // Inform the current actions module that we just parsed this declarator.
John McCalld226f652010-08-21 09:40:31 +0000883 Decl *ThisDecl = 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +0000884 switch (TemplateInfo.Kind) {
885 case ParsedTemplateInfo::NonTemplate:
Douglas Gregor23c94db2010-07-02 17:43:08 +0000886 ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
Douglas Gregord5a423b2009-09-25 18:43:00 +0000887 break;
888
889 case ParsedTemplateInfo::Template:
890 case ParsedTemplateInfo::ExplicitSpecialization:
Douglas Gregor23c94db2010-07-02 17:43:08 +0000891 ThisDecl = Actions.ActOnTemplateDeclarator(getCurScope(),
John McCallf312b1e2010-08-26 23:41:50 +0000892 MultiTemplateParamsArg(Actions,
Douglas Gregore542c862009-06-23 23:11:28 +0000893 TemplateInfo.TemplateParams->data(),
894 TemplateInfo.TemplateParams->size()),
Douglas Gregord5a423b2009-09-25 18:43:00 +0000895 D);
896 break;
897
898 case ParsedTemplateInfo::ExplicitInstantiation: {
John McCalld226f652010-08-21 09:40:31 +0000899 DeclResult ThisRes
Douglas Gregor23c94db2010-07-02 17:43:08 +0000900 = Actions.ActOnExplicitInstantiation(getCurScope(),
Douglas Gregord5a423b2009-09-25 18:43:00 +0000901 TemplateInfo.ExternLoc,
902 TemplateInfo.TemplateLoc,
903 D);
904 if (ThisRes.isInvalid()) {
905 SkipUntil(tok::semi, true, true);
John McCalld226f652010-08-21 09:40:31 +0000906 return 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +0000907 }
908
909 ThisDecl = ThisRes.get();
910 break;
911 }
912 }
Mike Stump1eb44332009-09-09 15:08:12 +0000913
Richard Smith34b41d92011-02-20 03:19:35 +0000914 bool TypeContainsAuto =
915 D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto;
916
Douglas Gregor1426e532009-05-12 21:31:51 +0000917 // Parse declarator '=' initializer.
Argyrios Kyrtzidisa6eb5f82010-10-08 02:39:23 +0000918 if (isTokenEqualOrMistypedEqualEqual(
919 diag::err_invalid_equalequal_after_declarator)) {
Douglas Gregor1426e532009-05-12 21:31:51 +0000920 ConsumeToken();
Anders Carlsson37bf9d22010-09-24 21:25:25 +0000921 if (Tok.is(tok::kw_delete)) {
Douglas Gregor1426e532009-05-12 21:31:51 +0000922 SourceLocation DelLoc = ConsumeToken();
Anders Carlsson37bf9d22010-09-24 21:25:25 +0000923
924 if (!getLang().CPlusPlus0x)
925 Diag(DelLoc, diag::warn_deleted_function_accepted_as_extension);
926
Douglas Gregor1426e532009-05-12 21:31:51 +0000927 Actions.SetDeclDeleted(ThisDecl, DelLoc);
928 } else {
John McCall731ad842009-12-19 09:28:58 +0000929 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
930 EnterScope(0);
Douglas Gregor23c94db2010-07-02 17:43:08 +0000931 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
John McCall731ad842009-12-19 09:28:58 +0000932 }
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +0000933
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +0000934 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000935 Actions.CodeCompleteInitializer(getCurScope(), ThisDecl);
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +0000936 ConsumeCodeCompletionToken();
937 SkipUntil(tok::comma, true, true);
938 return ThisDecl;
939 }
940
John McCall60d7b3a2010-08-24 06:29:42 +0000941 ExprResult Init(ParseInitializer());
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +0000942
John McCall731ad842009-12-19 09:28:58 +0000943 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000944 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
John McCall731ad842009-12-19 09:28:58 +0000945 ExitScope();
946 }
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +0000947
Douglas Gregor1426e532009-05-12 21:31:51 +0000948 if (Init.isInvalid()) {
Douglas Gregor00225542010-03-01 18:27:54 +0000949 SkipUntil(tok::comma, true, true);
950 Actions.ActOnInitializerError(ThisDecl);
951 } else
Richard Smith34b41d92011-02-20 03:19:35 +0000952 Actions.AddInitializerToDecl(ThisDecl, Init.take(),
953 /*DirectInit=*/false, TypeContainsAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +0000954 }
955 } else if (Tok.is(tok::l_paren)) {
956 // Parse C++ direct initializer: '(' expression-list ')'
957 SourceLocation LParenLoc = ConsumeParen();
958 ExprVector Exprs(Actions);
959 CommaLocsTy CommaLocs;
960
Douglas Gregorb4debae2009-12-22 17:47:17 +0000961 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
962 EnterScope(0);
Douglas Gregor23c94db2010-07-02 17:43:08 +0000963 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +0000964 }
965
Douglas Gregor1426e532009-05-12 21:31:51 +0000966 if (ParseExpressionList(Exprs, CommaLocs)) {
967 SkipUntil(tok::r_paren);
Douglas Gregorb4debae2009-12-22 17:47:17 +0000968
969 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000970 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +0000971 ExitScope();
972 }
Douglas Gregor1426e532009-05-12 21:31:51 +0000973 } else {
974 // Match the ')'.
975 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
976
977 assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() &&
978 "Unexpected number of commas!");
Douglas Gregorb4debae2009-12-22 17:47:17 +0000979
980 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000981 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +0000982 ExitScope();
983 }
984
Douglas Gregor1426e532009-05-12 21:31:51 +0000985 Actions.AddCXXDirectInitializerToDecl(ThisDecl, LParenLoc,
986 move_arg(Exprs),
Richard Smith34b41d92011-02-20 03:19:35 +0000987 RParenLoc,
988 TypeContainsAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +0000989 }
990 } else {
Richard Smith34b41d92011-02-20 03:19:35 +0000991 Actions.ActOnUninitializedDecl(ThisDecl, TypeContainsAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +0000992 }
993
Richard Smith483b9f32011-02-21 20:05:19 +0000994 Actions.FinalizeDeclaration(ThisDecl);
995
Douglas Gregor1426e532009-05-12 21:31:51 +0000996 return ThisDecl;
997}
998
Reid Spencer5f016e22007-07-11 17:01:13 +0000999/// ParseSpecifierQualifierList
1000/// specifier-qualifier-list:
1001/// type-specifier specifier-qualifier-list[opt]
1002/// type-qualifier specifier-qualifier-list[opt]
1003/// [GNU] attributes specifier-qualifier-list[opt]
1004///
1005void Parser::ParseSpecifierQualifierList(DeclSpec &DS) {
1006 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
1007 /// parse declaration-specifiers and complain about extra stuff.
Reid Spencer5f016e22007-07-11 17:01:13 +00001008 ParseDeclarationSpecifiers(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00001009
Reid Spencer5f016e22007-07-11 17:01:13 +00001010 // Validate declspec for type-name.
1011 unsigned Specs = DS.getParsedSpecifiers();
Chris Lattnerb6645dd2009-04-14 21:16:09 +00001012 if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers() &&
John McCall7f040a92010-12-24 02:08:15 +00001013 !DS.hasAttributes())
Reid Spencer5f016e22007-07-11 17:01:13 +00001014 Diag(Tok, diag::err_typename_requires_specqual);
Mike Stump1eb44332009-09-09 15:08:12 +00001015
Reid Spencer5f016e22007-07-11 17:01:13 +00001016 // Issue diagnostic and remove storage class if present.
1017 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
1018 if (DS.getStorageClassSpecLoc().isValid())
1019 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
1020 else
1021 Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass);
1022 DS.ClearStorageClassSpecs();
1023 }
Mike Stump1eb44332009-09-09 15:08:12 +00001024
Reid Spencer5f016e22007-07-11 17:01:13 +00001025 // Issue diagnostic and remove function specfier if present.
1026 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Douglas Gregorb48fe382008-10-31 09:07:45 +00001027 if (DS.isInlineSpecified())
1028 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
1029 if (DS.isVirtualSpecified())
1030 Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec);
1031 if (DS.isExplicitSpecified())
1032 Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec);
Reid Spencer5f016e22007-07-11 17:01:13 +00001033 DS.ClearFunctionSpecs();
1034 }
1035}
1036
Chris Lattnerc199ab32009-04-12 20:42:31 +00001037/// isValidAfterIdentifierInDeclaratorAfterDeclSpec - Return true if the
1038/// specified token is valid after the identifier in a declarator which
1039/// immediately follows the declspec. For example, these things are valid:
1040///
1041/// int x [ 4]; // direct-declarator
1042/// int x ( int y); // direct-declarator
1043/// int(int x ) // direct-declarator
1044/// int x ; // simple-declaration
1045/// int x = 17; // init-declarator-list
1046/// int x , y; // init-declarator-list
1047/// int x __asm__ ("foo"); // init-declarator-list
Chris Lattnerb6645dd2009-04-14 21:16:09 +00001048/// int x : 4; // struct-declarator
Chris Lattnerc83c27a2009-04-12 22:29:43 +00001049/// int x { 5}; // C++'0x unified initializers
Chris Lattnerc199ab32009-04-12 20:42:31 +00001050///
1051/// This is not, because 'x' does not immediately follow the declspec (though
1052/// ')' happens to be valid anyway).
1053/// int (x)
1054///
1055static bool isValidAfterIdentifierInDeclarator(const Token &T) {
1056 return T.is(tok::l_square) || T.is(tok::l_paren) || T.is(tok::r_paren) ||
1057 T.is(tok::semi) || T.is(tok::comma) || T.is(tok::equal) ||
Chris Lattnerb6645dd2009-04-14 21:16:09 +00001058 T.is(tok::kw_asm) || T.is(tok::l_brace) || T.is(tok::colon);
Chris Lattnerc199ab32009-04-12 20:42:31 +00001059}
1060
Chris Lattnere40c2952009-04-14 21:34:55 +00001061
1062/// ParseImplicitInt - This method is called when we have an non-typename
1063/// identifier in a declspec (which normally terminates the decl spec) when
1064/// the declspec has no type specifier. In this case, the declspec is either
1065/// malformed or is "implicit int" (in K&R and C89).
1066///
1067/// This method handles diagnosing this prettily and returns false if the
1068/// declspec is done being processed. If it recovers and thinks there may be
1069/// other pieces of declspec after it, it returns true.
1070///
Chris Lattnerf4382f52009-04-14 22:17:06 +00001071bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001072 const ParsedTemplateInfo &TemplateInfo,
Chris Lattnere40c2952009-04-14 21:34:55 +00001073 AccessSpecifier AS) {
Chris Lattnerf4382f52009-04-14 22:17:06 +00001074 assert(Tok.is(tok::identifier) && "should have identifier");
Mike Stump1eb44332009-09-09 15:08:12 +00001075
Chris Lattnere40c2952009-04-14 21:34:55 +00001076 SourceLocation Loc = Tok.getLocation();
1077 // If we see an identifier that is not a type name, we normally would
1078 // parse it as the identifer being declared. However, when a typename
1079 // is typo'd or the definition is not included, this will incorrectly
1080 // parse the typename as the identifier name and fall over misparsing
1081 // later parts of the diagnostic.
1082 //
1083 // As such, we try to do some look-ahead in cases where this would
1084 // otherwise be an "implicit-int" case to see if this is invalid. For
1085 // example: "static foo_t x = 4;" In this case, if we parsed foo_t as
1086 // an identifier with implicit int, we'd get a parse error because the
1087 // next token is obviously invalid for a type. Parse these as a case
1088 // with an invalid type specifier.
1089 assert(!DS.hasTypeSpecifier() && "Type specifier checked above");
Mike Stump1eb44332009-09-09 15:08:12 +00001090
Chris Lattnere40c2952009-04-14 21:34:55 +00001091 // Since we know that this either implicit int (which is rare) or an
1092 // error, we'd do lookahead to try to do better recovery.
1093 if (isValidAfterIdentifierInDeclarator(NextToken())) {
1094 // If this token is valid for implicit int, e.g. "static x = 4", then
1095 // we just avoid eating the identifier, so it will be parsed as the
1096 // identifier in the declarator.
1097 return false;
1098 }
Mike Stump1eb44332009-09-09 15:08:12 +00001099
Chris Lattnere40c2952009-04-14 21:34:55 +00001100 // Otherwise, if we don't consume this token, we are going to emit an
1101 // error anyway. Try to recover from various common problems. Check
1102 // to see if this was a reference to a tag name without a tag specified.
1103 // This is a common problem in C (saying 'foo' instead of 'struct foo').
Chris Lattnerf4382f52009-04-14 22:17:06 +00001104 //
1105 // C++ doesn't need this, and isTagName doesn't take SS.
1106 if (SS == 0) {
1107 const char *TagName = 0;
1108 tok::TokenKind TagKind = tok::unknown;
Mike Stump1eb44332009-09-09 15:08:12 +00001109
Douglas Gregor23c94db2010-07-02 17:43:08 +00001110 switch (Actions.isTagName(*Tok.getIdentifierInfo(), getCurScope())) {
Chris Lattnere40c2952009-04-14 21:34:55 +00001111 default: break;
1112 case DeclSpec::TST_enum: TagName="enum" ;TagKind=tok::kw_enum ;break;
1113 case DeclSpec::TST_union: TagName="union" ;TagKind=tok::kw_union ;break;
1114 case DeclSpec::TST_struct:TagName="struct";TagKind=tok::kw_struct;break;
1115 case DeclSpec::TST_class: TagName="class" ;TagKind=tok::kw_class ;break;
1116 }
Mike Stump1eb44332009-09-09 15:08:12 +00001117
Chris Lattnerf4382f52009-04-14 22:17:06 +00001118 if (TagName) {
1119 Diag(Loc, diag::err_use_of_tag_name_without_tag)
John McCall23e907a2010-02-14 01:03:10 +00001120 << Tok.getIdentifierInfo() << TagName << getLang().CPlusPlus
Douglas Gregor849b2432010-03-31 17:46:05 +00001121 << FixItHint::CreateInsertion(Tok.getLocation(),TagName);
Mike Stump1eb44332009-09-09 15:08:12 +00001122
Chris Lattnerf4382f52009-04-14 22:17:06 +00001123 // Parse this as a tag as if the missing tag were present.
1124 if (TagKind == tok::kw_enum)
Douglas Gregor9b9edd62010-03-02 17:53:14 +00001125 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS);
Chris Lattnerf4382f52009-04-14 22:17:06 +00001126 else
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001127 ParseClassSpecifier(TagKind, Loc, DS, TemplateInfo, AS);
Chris Lattnerf4382f52009-04-14 22:17:06 +00001128 return true;
1129 }
Chris Lattnere40c2952009-04-14 21:34:55 +00001130 }
Mike Stump1eb44332009-09-09 15:08:12 +00001131
Douglas Gregora786fdb2009-10-13 23:27:22 +00001132 // This is almost certainly an invalid type name. Let the action emit a
1133 // diagnostic and attempt to recover.
John McCallb3d87482010-08-24 05:47:05 +00001134 ParsedType T;
Douglas Gregora786fdb2009-10-13 23:27:22 +00001135 if (Actions.DiagnoseUnknownTypeName(*Tok.getIdentifierInfo(), Loc,
Douglas Gregor23c94db2010-07-02 17:43:08 +00001136 getCurScope(), SS, T)) {
Douglas Gregora786fdb2009-10-13 23:27:22 +00001137 // The action emitted a diagnostic, so we don't have to.
1138 if (T) {
1139 // The action has suggested that the type T could be used. Set that as
1140 // the type in the declaration specifiers, consume the would-be type
1141 // name token, and we're done.
1142 const char *PrevSpec;
1143 unsigned DiagID;
John McCallb3d87482010-08-24 05:47:05 +00001144 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID, T);
Douglas Gregora786fdb2009-10-13 23:27:22 +00001145 DS.SetRangeEnd(Tok.getLocation());
1146 ConsumeToken();
1147
1148 // There may be other declaration specifiers after this.
1149 return true;
1150 }
1151
1152 // Fall through; the action had no suggestion for us.
1153 } else {
1154 // The action did not emit a diagnostic, so emit one now.
1155 SourceRange R;
1156 if (SS) R = SS->getRange();
1157 Diag(Loc, diag::err_unknown_typename) << Tok.getIdentifierInfo() << R;
1158 }
Mike Stump1eb44332009-09-09 15:08:12 +00001159
Douglas Gregora786fdb2009-10-13 23:27:22 +00001160 // Mark this as an error.
Chris Lattnere40c2952009-04-14 21:34:55 +00001161 const char *PrevSpec;
John McCallfec54012009-08-03 20:12:06 +00001162 unsigned DiagID;
1163 DS.SetTypeSpecType(DeclSpec::TST_error, Loc, PrevSpec, DiagID);
Chris Lattnere40c2952009-04-14 21:34:55 +00001164 DS.SetRangeEnd(Tok.getLocation());
1165 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001166
Chris Lattnere40c2952009-04-14 21:34:55 +00001167 // TODO: Could inject an invalid typedef decl in an enclosing scope to
1168 // avoid rippling error messages on subsequent uses of the same type,
1169 // could be useful if #include was forgotten.
1170 return false;
1171}
1172
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001173/// \brief Determine the declaration specifier context from the declarator
1174/// context.
1175///
1176/// \param Context the declarator context, which is one of the
1177/// Declarator::TheContext enumerator values.
1178Parser::DeclSpecContext
1179Parser::getDeclSpecContextFromDeclaratorContext(unsigned Context) {
1180 if (Context == Declarator::MemberContext)
1181 return DSC_class;
1182 if (Context == Declarator::FileContext)
1183 return DSC_top_level;
1184 return DSC_normal;
1185}
1186
Reid Spencer5f016e22007-07-11 17:01:13 +00001187/// ParseDeclarationSpecifiers
1188/// declaration-specifiers: [C99 6.7]
1189/// storage-class-specifier declaration-specifiers[opt]
1190/// type-specifier declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00001191/// [C99] function-specifier declaration-specifiers[opt]
1192/// [GNU] attributes declaration-specifiers[opt]
1193///
1194/// storage-class-specifier: [C99 6.7.1]
1195/// 'typedef'
1196/// 'extern'
1197/// 'static'
1198/// 'auto'
1199/// 'register'
Sebastian Redl669d5d72008-11-14 23:42:31 +00001200/// [C++] 'mutable'
Reid Spencer5f016e22007-07-11 17:01:13 +00001201/// [GNU] '__thread'
Reid Spencer5f016e22007-07-11 17:01:13 +00001202/// function-specifier: [C99 6.7.4]
1203/// [C99] 'inline'
Douglas Gregorb48fe382008-10-31 09:07:45 +00001204/// [C++] 'virtual'
1205/// [C++] 'explicit'
Peter Collingbournef315fa82011-02-14 01:42:53 +00001206/// [OpenCL] '__kernel'
Anders Carlssonf47f7a12009-05-06 04:46:28 +00001207/// 'friend': [C++ dcl.friend]
Sebastian Redl2ac67232009-11-05 15:47:02 +00001208/// 'constexpr': [C++0x dcl.constexpr]
Anders Carlssonf47f7a12009-05-06 04:46:28 +00001209
Reid Spencer5f016e22007-07-11 17:01:13 +00001210///
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +00001211void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001212 const ParsedTemplateInfo &TemplateInfo,
John McCall67d1a672009-08-06 02:15:43 +00001213 AccessSpecifier AS,
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001214 DeclSpecContext DSContext) {
Chris Lattner81c018d2008-03-13 06:29:04 +00001215 DS.SetRangeStart(Tok.getLocation());
Chris Lattner729ad832010-11-09 20:14:26 +00001216 DS.SetRangeEnd(Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00001217 while (1) {
John McCallfec54012009-08-03 20:12:06 +00001218 bool isInvalid = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001219 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00001220 unsigned DiagID = 0;
1221
Reid Spencer5f016e22007-07-11 17:01:13 +00001222 SourceLocation Loc = Tok.getLocation();
Douglas Gregor12e083c2008-11-07 15:42:26 +00001223
Reid Spencer5f016e22007-07-11 17:01:13 +00001224 switch (Tok.getKind()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001225 default:
Chris Lattnerbce61352008-07-26 00:20:22 +00001226 DoneWithDeclSpec:
Reid Spencer5f016e22007-07-11 17:01:13 +00001227 // If this is not a declaration specifier token, we're done reading decl
1228 // specifiers. First verify that DeclSpec's are consistent.
Douglas Gregor9b3064b2009-04-01 22:41:11 +00001229 DS.Finish(Diags, PP);
Reid Spencer5f016e22007-07-11 17:01:13 +00001230 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001231
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001232 case tok::code_completion: {
John McCallf312b1e2010-08-26 23:41:50 +00001233 Sema::ParserCompletionContext CCC = Sema::PCC_Namespace;
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001234 if (DS.hasTypeSpecifier()) {
1235 bool AllowNonIdentifiers
1236 = (getCurScope()->getFlags() & (Scope::ControlScope |
1237 Scope::BlockScope |
1238 Scope::TemplateParamScope |
1239 Scope::FunctionPrototypeScope |
1240 Scope::AtCatchScope)) == 0;
1241 bool AllowNestedNameSpecifiers
1242 = DSContext == DSC_top_level ||
1243 (DSContext == DSC_class && DS.isFriendSpecified());
1244
Douglas Gregorc7b6d882010-09-16 15:14:18 +00001245 Actions.CodeCompleteDeclSpec(getCurScope(), DS,
1246 AllowNonIdentifiers,
1247 AllowNestedNameSpecifiers);
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001248 ConsumeCodeCompletionToken();
1249 return;
1250 }
1251
Douglas Gregor68e3c2e2011-02-15 20:33:25 +00001252 if (getCurScope()->getFnParent() || getCurScope()->getBlockParent())
1253 CCC = Sema::PCC_LocalDeclarationSpecifiers;
1254 else if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate)
John McCallf312b1e2010-08-26 23:41:50 +00001255 CCC = DSContext == DSC_class? Sema::PCC_MemberTemplate
1256 : Sema::PCC_Template;
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001257 else if (DSContext == DSC_class)
John McCallf312b1e2010-08-26 23:41:50 +00001258 CCC = Sema::PCC_Class;
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001259 else if (ObjCImpDecl)
John McCallf312b1e2010-08-26 23:41:50 +00001260 CCC = Sema::PCC_ObjCImplementation;
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001261
1262 Actions.CodeCompleteOrdinaryName(getCurScope(), CCC);
1263 ConsumeCodeCompletionToken();
1264 return;
1265 }
1266
Chris Lattner5e02c472009-01-05 00:07:25 +00001267 case tok::coloncolon: // ::foo::bar
John McCall9ba61662010-02-26 08:45:28 +00001268 // C++ scope specifier. Annotate and loop, or bail out on error.
1269 if (TryAnnotateCXXScopeToken(true)) {
1270 if (!DS.hasTypeSpecifier())
1271 DS.SetTypeSpecError();
1272 goto DoneWithDeclSpec;
1273 }
John McCall2e0a7152010-03-01 18:20:46 +00001274 if (Tok.is(tok::coloncolon)) // ::new or ::delete
1275 goto DoneWithDeclSpec;
John McCall9ba61662010-02-26 08:45:28 +00001276 continue;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001277
1278 case tok::annot_cxxscope: {
1279 if (DS.hasTypeSpecifier())
1280 goto DoneWithDeclSpec;
1281
John McCallaa87d332009-12-12 11:40:51 +00001282 CXXScopeSpec SS;
Douglas Gregorc34348a2011-02-24 17:54:50 +00001283 Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(),
1284 Tok.getAnnotationRange(),
1285 SS);
John McCallaa87d332009-12-12 11:40:51 +00001286
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001287 // We are looking for a qualified typename.
Douglas Gregor9135c722009-03-25 15:40:00 +00001288 Token Next = NextToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001289 if (Next.is(tok::annot_template_id) &&
Douglas Gregor9135c722009-03-25 15:40:00 +00001290 static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue())
Douglas Gregorc45c2322009-03-31 00:43:58 +00001291 ->Kind == TNK_Type_template) {
Douglas Gregor9135c722009-03-25 15:40:00 +00001292 // We have a qualified template-id, e.g., N::A<int>
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001293
1294 // C++ [class.qual]p2:
1295 // In a lookup in which the constructor is an acceptable lookup
1296 // result and the nested-name-specifier nominates a class C:
1297 //
1298 // - if the name specified after the
1299 // nested-name-specifier, when looked up in C, is the
1300 // injected-class-name of C (Clause 9), or
1301 //
1302 // - if the name specified after the nested-name-specifier
1303 // is the same as the identifier or the
1304 // simple-template-id's template-name in the last
1305 // component of the nested-name-specifier,
1306 //
1307 // the name is instead considered to name the constructor of
1308 // class C.
1309 //
1310 // Thus, if the template-name is actually the constructor
1311 // name, then the code is ill-formed; this interpretation is
1312 // reinforced by the NAD status of core issue 635.
1313 TemplateIdAnnotation *TemplateId
1314 = static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue());
John McCallba9d8532010-04-13 06:39:49 +00001315 if ((DSContext == DSC_top_level ||
1316 (DSContext == DSC_class && DS.isFriendSpecified())) &&
1317 TemplateId->Name &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00001318 Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) {
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001319 if (isConstructorDeclarator()) {
1320 // The user meant this to be an out-of-line constructor
1321 // definition, but template arguments are not allowed
1322 // there. Just allow this as a constructor; we'll
1323 // complain about it later.
1324 goto DoneWithDeclSpec;
1325 }
1326
1327 // The user meant this to name a type, but it actually names
1328 // a constructor with some extraneous template
1329 // arguments. Complain, then parse it as a type as the user
1330 // intended.
1331 Diag(TemplateId->TemplateNameLoc,
1332 diag::err_out_of_line_template_id_names_constructor)
1333 << TemplateId->Name;
1334 }
1335
John McCallaa87d332009-12-12 11:40:51 +00001336 DS.getTypeSpecScope() = SS;
1337 ConsumeToken(); // The C++ scope.
Mike Stump1eb44332009-09-09 15:08:12 +00001338 assert(Tok.is(tok::annot_template_id) &&
Douglas Gregor9135c722009-03-25 15:40:00 +00001339 "ParseOptionalCXXScopeSpecifier not working");
Douglas Gregor059101f2011-03-02 00:47:37 +00001340 AnnotateTemplateIdTokenAsType();
Douglas Gregor9135c722009-03-25 15:40:00 +00001341 continue;
1342 }
1343
Douglas Gregor9d7b3532009-09-28 07:26:33 +00001344 if (Next.is(tok::annot_typename)) {
John McCallaa87d332009-12-12 11:40:51 +00001345 DS.getTypeSpecScope() = SS;
1346 ConsumeToken(); // The C++ scope.
John McCallb3d87482010-08-24 05:47:05 +00001347 if (Tok.getAnnotationValue()) {
1348 ParsedType T = getTypeAnnotation(Tok);
Nico Weber253e80b2010-11-22 10:30:56 +00001349 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename,
1350 Tok.getAnnotationEndLoc(),
John McCallb3d87482010-08-24 05:47:05 +00001351 PrevSpec, DiagID, T);
1352 }
Douglas Gregor9d7b3532009-09-28 07:26:33 +00001353 else
1354 DS.SetTypeSpecError();
1355 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1356 ConsumeToken(); // The typename
1357 }
1358
Douglas Gregor9135c722009-03-25 15:40:00 +00001359 if (Next.isNot(tok::identifier))
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001360 goto DoneWithDeclSpec;
1361
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001362 // If we're in a context where the identifier could be a class name,
1363 // check whether this is a constructor declaration.
John McCallba9d8532010-04-13 06:39:49 +00001364 if ((DSContext == DSC_top_level ||
1365 (DSContext == DSC_class && DS.isFriendSpecified())) &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00001366 Actions.isCurrentClassName(*Next.getIdentifierInfo(), getCurScope(),
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001367 &SS)) {
1368 if (isConstructorDeclarator())
1369 goto DoneWithDeclSpec;
1370
1371 // As noted in C++ [class.qual]p2 (cited above), when the name
1372 // of the class is qualified in a context where it could name
1373 // a constructor, its a constructor name. However, we've
1374 // looked at the declarator, and the user probably meant this
1375 // to be a type. Complain that it isn't supposed to be treated
1376 // as a type, then proceed to parse it as a type.
1377 Diag(Next.getLocation(), diag::err_out_of_line_type_names_constructor)
1378 << Next.getIdentifierInfo();
1379 }
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001380
John McCallb3d87482010-08-24 05:47:05 +00001381 ParsedType TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(),
1382 Next.getLocation(),
Douglas Gregor9e876872011-03-01 18:12:44 +00001383 getCurScope(), &SS,
1384 false, false, ParsedType(),
1385 /*NonTrivialSourceInfo=*/true);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001386
Chris Lattnerf4382f52009-04-14 22:17:06 +00001387 // If the referenced identifier is not a type, then this declspec is
1388 // erroneous: We already checked about that it has no type specifier, and
1389 // C++ doesn't have implicit int. Diagnose it as a typo w.r.t. to the
Mike Stump1eb44332009-09-09 15:08:12 +00001390 // typename.
Chris Lattnerf4382f52009-04-14 22:17:06 +00001391 if (TypeRep == 0) {
1392 ConsumeToken(); // Eat the scope spec so the identifier is current.
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001393 if (ParseImplicitInt(DS, &SS, TemplateInfo, AS)) continue;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001394 goto DoneWithDeclSpec;
Chris Lattnerf4382f52009-04-14 22:17:06 +00001395 }
Mike Stump1eb44332009-09-09 15:08:12 +00001396
John McCallaa87d332009-12-12 11:40:51 +00001397 DS.getTypeSpecScope() = SS;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001398 ConsumeToken(); // The C++ scope.
1399
Douglas Gregor1a51b4a2009-02-09 15:09:02 +00001400 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001401 DiagID, TypeRep);
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001402 if (isInvalid)
1403 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001404
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001405 DS.SetRangeEnd(Tok.getLocation());
1406 ConsumeToken(); // The typename.
1407
1408 continue;
1409 }
Mike Stump1eb44332009-09-09 15:08:12 +00001410
Chris Lattner80d0c892009-01-21 19:48:37 +00001411 case tok::annot_typename: {
John McCallb3d87482010-08-24 05:47:05 +00001412 if (Tok.getAnnotationValue()) {
1413 ParsedType T = getTypeAnnotation(Tok);
Nico Weberc43271e2010-11-22 12:50:03 +00001414 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallb3d87482010-08-24 05:47:05 +00001415 DiagID, T);
1416 } else
Douglas Gregor31a19b62009-04-01 21:51:26 +00001417 DS.SetTypeSpecError();
Chris Lattner5c5db552010-04-05 18:18:31 +00001418
1419 if (isInvalid)
1420 break;
1421
Chris Lattner80d0c892009-01-21 19:48:37 +00001422 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1423 ConsumeToken(); // The typename
Mike Stump1eb44332009-09-09 15:08:12 +00001424
Chris Lattner80d0c892009-01-21 19:48:37 +00001425 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1426 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001427 // Objective-C interface.
1428 if (Tok.is(tok::less) && getLang().ObjC1)
1429 ParseObjCProtocolQualifiers(DS);
1430
Chris Lattner80d0c892009-01-21 19:48:37 +00001431 continue;
1432 }
Mike Stump1eb44332009-09-09 15:08:12 +00001433
Chris Lattner3bd934a2008-07-26 01:18:38 +00001434 // typedef-name
1435 case tok::identifier: {
Chris Lattner5e02c472009-01-05 00:07:25 +00001436 // In C++, check to see if this is a scope specifier like foo::bar::, if
1437 // so handle it as such. This is important for ctor parsing.
John McCall9ba61662010-02-26 08:45:28 +00001438 if (getLang().CPlusPlus) {
1439 if (TryAnnotateCXXScopeToken(true)) {
1440 if (!DS.hasTypeSpecifier())
1441 DS.SetTypeSpecError();
1442 goto DoneWithDeclSpec;
1443 }
1444 if (!Tok.is(tok::identifier))
1445 continue;
1446 }
Mike Stump1eb44332009-09-09 15:08:12 +00001447
Chris Lattner3bd934a2008-07-26 01:18:38 +00001448 // This identifier can only be a typedef name if we haven't already seen
1449 // a type-specifier. Without this check we misparse:
1450 // typedef int X; struct Y { short X; }; as 'short int'.
1451 if (DS.hasTypeSpecifier())
1452 goto DoneWithDeclSpec;
Mike Stump1eb44332009-09-09 15:08:12 +00001453
John Thompson82287d12010-02-05 00:12:22 +00001454 // Check for need to substitute AltiVec keyword tokens.
1455 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
1456 break;
1457
Chris Lattner3bd934a2008-07-26 01:18:38 +00001458 // It has to be available as a typedef too!
John McCallb3d87482010-08-24 05:47:05 +00001459 ParsedType TypeRep =
1460 Actions.getTypeName(*Tok.getIdentifierInfo(),
1461 Tok.getLocation(), getCurScope());
Douglas Gregor55f6b142009-02-09 18:46:07 +00001462
Chris Lattnerc199ab32009-04-12 20:42:31 +00001463 // If this is not a typedef name, don't parse it as part of the declspec,
1464 // it must be an implicit int or an error.
John McCallb3d87482010-08-24 05:47:05 +00001465 if (!TypeRep) {
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001466 if (ParseImplicitInt(DS, 0, TemplateInfo, AS)) continue;
Chris Lattner3bd934a2008-07-26 01:18:38 +00001467 goto DoneWithDeclSpec;
Chris Lattnerc199ab32009-04-12 20:42:31 +00001468 }
Douglas Gregor55f6b142009-02-09 18:46:07 +00001469
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001470 // If we're in a context where the identifier could be a class name,
1471 // check whether this is a constructor declaration.
1472 if (getLang().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00001473 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), getCurScope()) &&
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001474 isConstructorDeclarator())
Douglas Gregorb48fe382008-10-31 09:07:45 +00001475 goto DoneWithDeclSpec;
1476
Douglas Gregor1a51b4a2009-02-09 15:09:02 +00001477 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001478 DiagID, TypeRep);
Chris Lattner3bd934a2008-07-26 01:18:38 +00001479 if (isInvalid)
1480 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001481
Chris Lattner3bd934a2008-07-26 01:18:38 +00001482 DS.SetRangeEnd(Tok.getLocation());
1483 ConsumeToken(); // The identifier
1484
1485 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1486 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001487 // Objective-C interface.
1488 if (Tok.is(tok::less) && getLang().ObjC1)
1489 ParseObjCProtocolQualifiers(DS);
1490
Steve Naroff4f9b9f12008-09-22 10:28:57 +00001491 // Need to support trailing type qualifiers (e.g. "id<p> const").
1492 // If a type specifier follows, it will be diagnosed elsewhere.
1493 continue;
Chris Lattner3bd934a2008-07-26 01:18:38 +00001494 }
Douglas Gregor39a8de12009-02-25 19:37:18 +00001495
1496 // type-name
1497 case tok::annot_template_id: {
Mike Stump1eb44332009-09-09 15:08:12 +00001498 TemplateIdAnnotation *TemplateId
Douglas Gregor39a8de12009-02-25 19:37:18 +00001499 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
Douglas Gregorc45c2322009-03-31 00:43:58 +00001500 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor39a8de12009-02-25 19:37:18 +00001501 // This template-id does not refer to a type name, so we're
1502 // done with the type-specifiers.
1503 goto DoneWithDeclSpec;
1504 }
1505
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001506 // If we're in a context where the template-id could be a
1507 // constructor name or specialization, check whether this is a
1508 // constructor declaration.
1509 if (getLang().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00001510 Actions.isCurrentClassName(*TemplateId->Name, getCurScope()) &&
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001511 isConstructorDeclarator())
1512 goto DoneWithDeclSpec;
1513
Douglas Gregor39a8de12009-02-25 19:37:18 +00001514 // Turn the template-id annotation token into a type annotation
1515 // token, then try again to parse it as a type-specifier.
Douglas Gregor31a19b62009-04-01 21:51:26 +00001516 AnnotateTemplateIdTokenAsType();
Douglas Gregor39a8de12009-02-25 19:37:18 +00001517 continue;
1518 }
1519
Reid Spencer5f016e22007-07-11 17:01:13 +00001520 // GNU attributes support.
1521 case tok::kw___attribute:
John McCall7f040a92010-12-24 02:08:15 +00001522 ParseGNUAttributes(DS.getAttributes());
Reid Spencer5f016e22007-07-11 17:01:13 +00001523 continue;
Steve Narofff59e17e2008-12-24 20:59:21 +00001524
1525 // Microsoft declspec support.
1526 case tok::kw___declspec:
John McCall7f040a92010-12-24 02:08:15 +00001527 ParseMicrosoftDeclSpec(DS.getAttributes());
Steve Narofff59e17e2008-12-24 20:59:21 +00001528 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001529
Steve Naroff239f0732008-12-25 14:16:32 +00001530 // Microsoft single token adornments.
Steve Naroff86bc6cf2008-12-25 14:41:26 +00001531 case tok::kw___forceinline:
Eli Friedman290eeb02009-06-08 23:27:34 +00001532 // FIXME: Add handling here!
1533 break;
1534
1535 case tok::kw___ptr64:
Steve Naroff86bc6cf2008-12-25 14:41:26 +00001536 case tok::kw___w64:
Steve Naroff239f0732008-12-25 14:16:32 +00001537 case tok::kw___cdecl:
1538 case tok::kw___stdcall:
1539 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00001540 case tok::kw___thiscall:
John McCall7f040a92010-12-24 02:08:15 +00001541 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman290eeb02009-06-08 23:27:34 +00001542 continue;
1543
Dawn Perchik52fc3142010-09-03 01:29:35 +00001544 // Borland single token adornments.
1545 case tok::kw___pascal:
John McCall7f040a92010-12-24 02:08:15 +00001546 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik52fc3142010-09-03 01:29:35 +00001547 continue;
1548
Peter Collingbournef315fa82011-02-14 01:42:53 +00001549 // OpenCL single token adornments.
1550 case tok::kw___kernel:
1551 ParseOpenCLAttributes(DS.getAttributes());
1552 continue;
1553
Reid Spencer5f016e22007-07-11 17:01:13 +00001554 // storage-class-specifier
1555 case tok::kw_typedef:
John McCallfec54012009-08-03 20:12:06 +00001556 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_typedef, Loc, PrevSpec,
Peter Collingbournee2f82f72011-02-11 19:59:54 +00001557 DiagID, getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00001558 break;
1559 case tok::kw_extern:
1560 if (DS.isThreadSpecified())
Chris Lattner1ab3b962008-11-18 07:48:38 +00001561 Diag(Tok, diag::ext_thread_before) << "extern";
John McCallfec54012009-08-03 20:12:06 +00001562 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_extern, Loc, PrevSpec,
Peter Collingbournee2f82f72011-02-11 19:59:54 +00001563 DiagID, getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00001564 break;
Steve Naroff8d54bf22007-12-18 00:16:02 +00001565 case tok::kw___private_extern__:
Chris Lattnerf97409f2008-04-06 06:57:35 +00001566 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_private_extern, Loc,
Peter Collingbournee2f82f72011-02-11 19:59:54 +00001567 PrevSpec, DiagID, getLang());
Steve Naroff8d54bf22007-12-18 00:16:02 +00001568 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00001569 case tok::kw_static:
1570 if (DS.isThreadSpecified())
Chris Lattner1ab3b962008-11-18 07:48:38 +00001571 Diag(Tok, diag::ext_thread_before) << "static";
John McCallfec54012009-08-03 20:12:06 +00001572 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_static, Loc, PrevSpec,
Peter Collingbournee2f82f72011-02-11 19:59:54 +00001573 DiagID, getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00001574 break;
1575 case tok::kw_auto:
Douglas Gregor18d8b792011-03-14 21:43:30 +00001576 if (getLang().CPlusPlus0x) {
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00001577 if (isKnownToBeTypeSpecifier(GetLookAheadToken(1))) {
1578 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, Loc, PrevSpec,
1579 DiagID, getLang());
1580 if (!isInvalid)
1581 Diag(Tok, diag::auto_storage_class)
1582 << FixItHint::CreateRemoval(DS.getStorageClassSpecLoc());
1583 }
1584 else
1585 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec,
1586 DiagID);
1587 }
Anders Carlssone89d1592009-06-26 18:41:36 +00001588 else
John McCallfec54012009-08-03 20:12:06 +00001589 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, Loc, PrevSpec,
Peter Collingbournee2f82f72011-02-11 19:59:54 +00001590 DiagID, getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00001591 break;
1592 case tok::kw_register:
John McCallfec54012009-08-03 20:12:06 +00001593 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_register, Loc, PrevSpec,
Peter Collingbournee2f82f72011-02-11 19:59:54 +00001594 DiagID, getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00001595 break;
Sebastian Redl669d5d72008-11-14 23:42:31 +00001596 case tok::kw_mutable:
John McCallfec54012009-08-03 20:12:06 +00001597 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_mutable, Loc, PrevSpec,
Peter Collingbournee2f82f72011-02-11 19:59:54 +00001598 DiagID, getLang());
Sebastian Redl669d5d72008-11-14 23:42:31 +00001599 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00001600 case tok::kw___thread:
John McCallfec54012009-08-03 20:12:06 +00001601 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001602 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001603
Reid Spencer5f016e22007-07-11 17:01:13 +00001604 // function-specifier
1605 case tok::kw_inline:
John McCallfec54012009-08-03 20:12:06 +00001606 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001607 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +00001608 case tok::kw_virtual:
John McCallfec54012009-08-03 20:12:06 +00001609 isInvalid = DS.SetFunctionSpecVirtual(Loc, PrevSpec, DiagID);
Douglas Gregorb48fe382008-10-31 09:07:45 +00001610 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +00001611 case tok::kw_explicit:
John McCallfec54012009-08-03 20:12:06 +00001612 isInvalid = DS.SetFunctionSpecExplicit(Loc, PrevSpec, DiagID);
Douglas Gregorb48fe382008-10-31 09:07:45 +00001613 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00001614
Anders Carlssonf47f7a12009-05-06 04:46:28 +00001615 // friend
1616 case tok::kw_friend:
John McCall67d1a672009-08-06 02:15:43 +00001617 if (DSContext == DSC_class)
1618 isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID);
1619 else {
1620 PrevSpec = ""; // not actually used by the diagnostic
1621 DiagID = diag::err_friend_invalid_in_context;
1622 isInvalid = true;
1623 }
Anders Carlssonf47f7a12009-05-06 04:46:28 +00001624 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001625
Sebastian Redl2ac67232009-11-05 15:47:02 +00001626 // constexpr
1627 case tok::kw_constexpr:
1628 isInvalid = DS.SetConstexprSpec(Loc, PrevSpec, DiagID);
1629 break;
1630
Chris Lattner80d0c892009-01-21 19:48:37 +00001631 // type-specifier
1632 case tok::kw_short:
John McCallfec54012009-08-03 20:12:06 +00001633 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec,
1634 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001635 break;
1636 case tok::kw_long:
1637 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCallfec54012009-08-03 20:12:06 +00001638 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
1639 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001640 else
John McCallfec54012009-08-03 20:12:06 +00001641 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
1642 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001643 break;
1644 case tok::kw_signed:
John McCallfec54012009-08-03 20:12:06 +00001645 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec,
1646 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001647 break;
1648 case tok::kw_unsigned:
John McCallfec54012009-08-03 20:12:06 +00001649 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
1650 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001651 break;
1652 case tok::kw__Complex:
John McCallfec54012009-08-03 20:12:06 +00001653 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
1654 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001655 break;
1656 case tok::kw__Imaginary:
John McCallfec54012009-08-03 20:12:06 +00001657 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
1658 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001659 break;
1660 case tok::kw_void:
John McCallfec54012009-08-03 20:12:06 +00001661 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec,
1662 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001663 break;
1664 case tok::kw_char:
John McCallfec54012009-08-03 20:12:06 +00001665 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec,
1666 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001667 break;
1668 case tok::kw_int:
John McCallfec54012009-08-03 20:12:06 +00001669 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec,
1670 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001671 break;
1672 case tok::kw_float:
John McCallfec54012009-08-03 20:12:06 +00001673 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec,
1674 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001675 break;
1676 case tok::kw_double:
John McCallfec54012009-08-03 20:12:06 +00001677 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec,
1678 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001679 break;
1680 case tok::kw_wchar_t:
John McCallfec54012009-08-03 20:12:06 +00001681 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec,
1682 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001683 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001684 case tok::kw_char16_t:
John McCallfec54012009-08-03 20:12:06 +00001685 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec,
1686 DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001687 break;
1688 case tok::kw_char32_t:
John McCallfec54012009-08-03 20:12:06 +00001689 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec,
1690 DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001691 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00001692 case tok::kw_bool:
1693 case tok::kw__Bool:
Argyrios Kyrtzidis4383e182010-11-16 18:18:13 +00001694 if (Tok.is(tok::kw_bool) &&
1695 DS.getTypeSpecType() != DeclSpec::TST_unspecified &&
1696 DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
1697 PrevSpec = ""; // Not used by the diagnostic.
1698 DiagID = diag::err_bool_redeclaration;
1699 isInvalid = true;
1700 } else {
1701 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec,
1702 DiagID);
1703 }
Chris Lattner80d0c892009-01-21 19:48:37 +00001704 break;
1705 case tok::kw__Decimal32:
John McCallfec54012009-08-03 20:12:06 +00001706 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
1707 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001708 break;
1709 case tok::kw__Decimal64:
John McCallfec54012009-08-03 20:12:06 +00001710 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
1711 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001712 break;
1713 case tok::kw__Decimal128:
John McCallfec54012009-08-03 20:12:06 +00001714 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
1715 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001716 break;
John Thompson82287d12010-02-05 00:12:22 +00001717 case tok::kw___vector:
1718 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
1719 break;
1720 case tok::kw___pixel:
1721 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
1722 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00001723
1724 // class-specifier:
1725 case tok::kw_class:
1726 case tok::kw_struct:
Chris Lattner4c97d762009-04-12 21:49:30 +00001727 case tok::kw_union: {
1728 tok::TokenKind Kind = Tok.getKind();
1729 ConsumeToken();
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001730 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS);
Chris Lattner80d0c892009-01-21 19:48:37 +00001731 continue;
Chris Lattner4c97d762009-04-12 21:49:30 +00001732 }
Chris Lattner80d0c892009-01-21 19:48:37 +00001733
1734 // enum-specifier:
1735 case tok::kw_enum:
Chris Lattner4c97d762009-04-12 21:49:30 +00001736 ConsumeToken();
Douglas Gregor9b9edd62010-03-02 17:53:14 +00001737 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS);
Chris Lattner80d0c892009-01-21 19:48:37 +00001738 continue;
1739
1740 // cv-qualifier:
1741 case tok::kw_const:
John McCallfec54012009-08-03 20:12:06 +00001742 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec, DiagID,
1743 getLang());
Chris Lattner80d0c892009-01-21 19:48:37 +00001744 break;
1745 case tok::kw_volatile:
John McCallfec54012009-08-03 20:12:06 +00001746 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
1747 getLang());
Chris Lattner80d0c892009-01-21 19:48:37 +00001748 break;
1749 case tok::kw_restrict:
John McCallfec54012009-08-03 20:12:06 +00001750 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
1751 getLang());
Chris Lattner80d0c892009-01-21 19:48:37 +00001752 break;
1753
Douglas Gregord57959a2009-03-27 23:10:48 +00001754 // C++ typename-specifier:
1755 case tok::kw_typename:
John McCall9ba61662010-02-26 08:45:28 +00001756 if (TryAnnotateTypeOrScopeToken()) {
1757 DS.SetTypeSpecError();
1758 goto DoneWithDeclSpec;
1759 }
1760 if (!Tok.is(tok::kw_typename))
Douglas Gregord57959a2009-03-27 23:10:48 +00001761 continue;
1762 break;
1763
Chris Lattner80d0c892009-01-21 19:48:37 +00001764 // GNU typeof support.
1765 case tok::kw_typeof:
1766 ParseTypeofSpecifier(DS);
1767 continue;
1768
Anders Carlsson6fd634f2009-06-24 17:47:40 +00001769 case tok::kw_decltype:
1770 ParseDecltypeSpecifier(DS);
1771 continue;
1772
Peter Collingbourne207f4d82011-03-18 22:38:29 +00001773 // OpenCL qualifiers:
1774 case tok::kw_private:
1775 if (!getLang().OpenCL)
1776 goto DoneWithDeclSpec;
1777 case tok::kw___private:
1778 case tok::kw___global:
1779 case tok::kw___local:
1780 case tok::kw___constant:
1781 case tok::kw___read_only:
1782 case tok::kw___write_only:
1783 case tok::kw___read_write:
1784 ParseOpenCLQualifiers(DS);
1785 break;
1786
Steve Naroffd3ded1f2008-06-05 00:02:44 +00001787 case tok::less:
Chris Lattner3bd934a2008-07-26 01:18:38 +00001788 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattnerbce61352008-07-26 00:20:22 +00001789 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
1790 // but we support it.
Chris Lattner3bd934a2008-07-26 01:18:38 +00001791 if (DS.hasTypeSpecifier() || !getLang().ObjC1)
Chris Lattnerbce61352008-07-26 00:20:22 +00001792 goto DoneWithDeclSpec;
Mike Stump1eb44332009-09-09 15:08:12 +00001793
Douglas Gregor46f936e2010-11-19 17:10:50 +00001794 if (!ParseObjCProtocolQualifiers(DS))
1795 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
1796 << FixItHint::CreateInsertion(Loc, "id")
1797 << SourceRange(Loc, DS.getSourceRange().getEnd());
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001798
1799 // Need to support trailing type qualifiers (e.g. "id<p> const").
1800 // If a type specifier follows, it will be diagnosed elsewhere.
1801 continue;
Reid Spencer5f016e22007-07-11 17:01:13 +00001802 }
John McCallfec54012009-08-03 20:12:06 +00001803 // If the specifier wasn't legal, issue a diagnostic.
Reid Spencer5f016e22007-07-11 17:01:13 +00001804 if (isInvalid) {
1805 assert(PrevSpec && "Method did not return previous specifier!");
John McCallfec54012009-08-03 20:12:06 +00001806 assert(DiagID);
Douglas Gregorae2fb142010-08-23 14:34:43 +00001807
1808 if (DiagID == diag::ext_duplicate_declspec)
1809 Diag(Tok, DiagID)
1810 << PrevSpec << FixItHint::CreateRemoval(Tok.getLocation());
1811 else
1812 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00001813 }
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00001814
Chris Lattner81c018d2008-03-13 06:29:04 +00001815 DS.SetRangeEnd(Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00001816 ConsumeToken();
1817 }
1818}
Douglas Gregoradcac882008-12-01 23:54:00 +00001819
Chris Lattner7a0ab5f2009-01-06 06:59:53 +00001820/// ParseOptionalTypeSpecifier - Try to parse a single type-specifier. We
Douglas Gregor12e083c2008-11-07 15:42:26 +00001821/// primarily follow the C++ grammar with additions for C99 and GNU,
1822/// which together subsume the C grammar. Note that the C++
1823/// type-specifier also includes the C type-qualifier (for const,
1824/// volatile, and C99 restrict). Returns true if a type-specifier was
1825/// found (and parsed), false otherwise.
1826///
1827/// type-specifier: [C++ 7.1.5]
1828/// simple-type-specifier
1829/// class-specifier
1830/// enum-specifier
1831/// elaborated-type-specifier [TODO]
1832/// cv-qualifier
1833///
1834/// cv-qualifier: [C++ 7.1.5.1]
1835/// 'const'
1836/// 'volatile'
1837/// [C99] 'restrict'
1838///
1839/// simple-type-specifier: [ C++ 7.1.5.2]
1840/// '::'[opt] nested-name-specifier[opt] type-name [TODO]
1841/// '::'[opt] nested-name-specifier 'template' template-id [TODO]
1842/// 'char'
1843/// 'wchar_t'
1844/// 'bool'
1845/// 'short'
1846/// 'int'
1847/// 'long'
1848/// 'signed'
1849/// 'unsigned'
1850/// 'float'
1851/// 'double'
1852/// 'void'
1853/// [C99] '_Bool'
1854/// [C99] '_Complex'
1855/// [C99] '_Imaginary' // Removed in TC2?
1856/// [GNU] '_Decimal32'
1857/// [GNU] '_Decimal64'
1858/// [GNU] '_Decimal128'
1859/// [GNU] typeof-specifier
1860/// [OBJC] class-name objc-protocol-refs[opt] [TODO]
1861/// [OBJC] typedef-name objc-protocol-refs[opt] [TODO]
Anders Carlsson6fd634f2009-06-24 17:47:40 +00001862/// [C++0x] 'decltype' ( expression )
John Thompson82287d12010-02-05 00:12:22 +00001863/// [AltiVec] '__vector'
John McCallfec54012009-08-03 20:12:06 +00001864bool Parser::ParseOptionalTypeSpecifier(DeclSpec &DS, bool& isInvalid,
Chris Lattner7a0ab5f2009-01-06 06:59:53 +00001865 const char *&PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001866 unsigned &DiagID,
Sebastian Redld9bafa72010-02-03 21:21:43 +00001867 const ParsedTemplateInfo &TemplateInfo,
1868 bool SuppressDeclarations) {
Douglas Gregor12e083c2008-11-07 15:42:26 +00001869 SourceLocation Loc = Tok.getLocation();
1870
1871 switch (Tok.getKind()) {
Chris Lattner166a8fc2009-01-04 23:41:41 +00001872 case tok::identifier: // foo::bar
Douglas Gregorc0b39642010-04-15 23:40:53 +00001873 // If we already have a type specifier, this identifier is not a type.
1874 if (DS.getTypeSpecType() != DeclSpec::TST_unspecified ||
1875 DS.getTypeSpecWidth() != DeclSpec::TSW_unspecified ||
1876 DS.getTypeSpecSign() != DeclSpec::TSS_unspecified)
1877 return false;
John Thompson82287d12010-02-05 00:12:22 +00001878 // Check for need to substitute AltiVec keyword tokens.
1879 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
1880 break;
1881 // Fall through.
Douglas Gregord57959a2009-03-27 23:10:48 +00001882 case tok::kw_typename: // typename foo::bar
Chris Lattner166a8fc2009-01-04 23:41:41 +00001883 // Annotate typenames and C++ scope specifiers. If we get one, just
1884 // recurse to handle whatever we get.
1885 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00001886 return true;
1887 if (Tok.is(tok::identifier))
1888 return false;
1889 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
1890 TemplateInfo, SuppressDeclarations);
Chris Lattner166a8fc2009-01-04 23:41:41 +00001891 case tok::coloncolon: // ::foo::bar
1892 if (NextToken().is(tok::kw_new) || // ::new
1893 NextToken().is(tok::kw_delete)) // ::delete
1894 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001895
Chris Lattner166a8fc2009-01-04 23:41:41 +00001896 // Annotate typenames and C++ scope specifiers. If we get one, just
1897 // recurse to handle whatever we get.
1898 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00001899 return true;
1900 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
1901 TemplateInfo, SuppressDeclarations);
Mike Stump1eb44332009-09-09 15:08:12 +00001902
Douglas Gregor12e083c2008-11-07 15:42:26 +00001903 // simple-type-specifier:
Chris Lattnerb31757b2009-01-06 05:06:21 +00001904 case tok::annot_typename: {
John McCallb3d87482010-08-24 05:47:05 +00001905 if (ParsedType T = getTypeAnnotation(Tok)) {
Nico Weber253e80b2010-11-22 10:30:56 +00001906 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename,
1907 Tok.getAnnotationEndLoc(), PrevSpec,
John McCallb3d87482010-08-24 05:47:05 +00001908 DiagID, T);
1909 } else
Douglas Gregor31a19b62009-04-01 21:51:26 +00001910 DS.SetTypeSpecError();
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001911 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1912 ConsumeToken(); // The typename
Mike Stump1eb44332009-09-09 15:08:12 +00001913
Douglas Gregor12e083c2008-11-07 15:42:26 +00001914 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1915 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
1916 // Objective-C interface. If we don't have Objective-C or a '<', this is
1917 // just a normal reference to a typedef name.
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001918 if (Tok.is(tok::less) && getLang().ObjC1)
1919 ParseObjCProtocolQualifiers(DS);
1920
Douglas Gregor12e083c2008-11-07 15:42:26 +00001921 return true;
1922 }
1923
1924 case tok::kw_short:
John McCallfec54012009-08-03 20:12:06 +00001925 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001926 break;
1927 case tok::kw_long:
1928 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCallfec54012009-08-03 20:12:06 +00001929 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
1930 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001931 else
John McCallfec54012009-08-03 20:12:06 +00001932 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
1933 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001934 break;
1935 case tok::kw_signed:
John McCallfec54012009-08-03 20:12:06 +00001936 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001937 break;
1938 case tok::kw_unsigned:
John McCallfec54012009-08-03 20:12:06 +00001939 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
1940 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001941 break;
1942 case tok::kw__Complex:
John McCallfec54012009-08-03 20:12:06 +00001943 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
1944 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001945 break;
1946 case tok::kw__Imaginary:
John McCallfec54012009-08-03 20:12:06 +00001947 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
1948 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001949 break;
1950 case tok::kw_void:
John McCallfec54012009-08-03 20:12:06 +00001951 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001952 break;
1953 case tok::kw_char:
John McCallfec54012009-08-03 20:12:06 +00001954 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001955 break;
1956 case tok::kw_int:
John McCallfec54012009-08-03 20:12:06 +00001957 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001958 break;
1959 case tok::kw_float:
John McCallfec54012009-08-03 20:12:06 +00001960 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001961 break;
1962 case tok::kw_double:
John McCallfec54012009-08-03 20:12:06 +00001963 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001964 break;
1965 case tok::kw_wchar_t:
John McCallfec54012009-08-03 20:12:06 +00001966 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001967 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001968 case tok::kw_char16_t:
John McCallfec54012009-08-03 20:12:06 +00001969 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001970 break;
1971 case tok::kw_char32_t:
John McCallfec54012009-08-03 20:12:06 +00001972 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001973 break;
Douglas Gregor12e083c2008-11-07 15:42:26 +00001974 case tok::kw_bool:
1975 case tok::kw__Bool:
John McCallfec54012009-08-03 20:12:06 +00001976 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001977 break;
1978 case tok::kw__Decimal32:
John McCallfec54012009-08-03 20:12:06 +00001979 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
1980 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001981 break;
1982 case tok::kw__Decimal64:
John McCallfec54012009-08-03 20:12:06 +00001983 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
1984 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001985 break;
1986 case tok::kw__Decimal128:
John McCallfec54012009-08-03 20:12:06 +00001987 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
1988 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001989 break;
John Thompson82287d12010-02-05 00:12:22 +00001990 case tok::kw___vector:
1991 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
1992 break;
1993 case tok::kw___pixel:
1994 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
1995 break;
1996
Douglas Gregor12e083c2008-11-07 15:42:26 +00001997 // class-specifier:
1998 case tok::kw_class:
1999 case tok::kw_struct:
Chris Lattner4c97d762009-04-12 21:49:30 +00002000 case tok::kw_union: {
2001 tok::TokenKind Kind = Tok.getKind();
2002 ConsumeToken();
Sebastian Redld9bafa72010-02-03 21:21:43 +00002003 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS_none,
2004 SuppressDeclarations);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002005 return true;
Chris Lattner4c97d762009-04-12 21:49:30 +00002006 }
Douglas Gregor12e083c2008-11-07 15:42:26 +00002007
2008 // enum-specifier:
2009 case tok::kw_enum:
Chris Lattner4c97d762009-04-12 21:49:30 +00002010 ConsumeToken();
Douglas Gregor9b9edd62010-03-02 17:53:14 +00002011 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS_none);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002012 return true;
2013
2014 // cv-qualifier:
2015 case tok::kw_const:
2016 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00002017 DiagID, getLang());
Douglas Gregor12e083c2008-11-07 15:42:26 +00002018 break;
2019 case tok::kw_volatile:
2020 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00002021 DiagID, getLang());
Douglas Gregor12e083c2008-11-07 15:42:26 +00002022 break;
2023 case tok::kw_restrict:
2024 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00002025 DiagID, getLang());
Douglas Gregor12e083c2008-11-07 15:42:26 +00002026 break;
2027
2028 // GNU typeof support.
2029 case tok::kw_typeof:
2030 ParseTypeofSpecifier(DS);
2031 return true;
2032
Anders Carlsson6fd634f2009-06-24 17:47:40 +00002033 // C++0x decltype support.
2034 case tok::kw_decltype:
2035 ParseDecltypeSpecifier(DS);
2036 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00002037
Peter Collingbourne207f4d82011-03-18 22:38:29 +00002038 // OpenCL qualifiers:
2039 case tok::kw_private:
2040 if (!getLang().OpenCL)
2041 return false;
2042 case tok::kw___private:
2043 case tok::kw___global:
2044 case tok::kw___local:
2045 case tok::kw___constant:
2046 case tok::kw___read_only:
2047 case tok::kw___write_only:
2048 case tok::kw___read_write:
2049 ParseOpenCLQualifiers(DS);
2050 break;
2051
Anders Carlsson0b7f7892009-06-26 23:44:14 +00002052 // C++0x auto support.
2053 case tok::kw_auto:
2054 if (!getLang().CPlusPlus0x)
2055 return false;
2056
John McCallfec54012009-08-03 20:12:06 +00002057 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec, DiagID);
Anders Carlsson0b7f7892009-06-26 23:44:14 +00002058 break;
Dawn Perchik52fc3142010-09-03 01:29:35 +00002059
Eli Friedman290eeb02009-06-08 23:27:34 +00002060 case tok::kw___ptr64:
2061 case tok::kw___w64:
Steve Naroff239f0732008-12-25 14:16:32 +00002062 case tok::kw___cdecl:
2063 case tok::kw___stdcall:
2064 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00002065 case tok::kw___thiscall:
John McCall7f040a92010-12-24 02:08:15 +00002066 ParseMicrosoftTypeAttributes(DS.getAttributes());
Chris Lattner837acd02009-01-21 19:19:26 +00002067 return true;
Steve Naroff239f0732008-12-25 14:16:32 +00002068
Dawn Perchik52fc3142010-09-03 01:29:35 +00002069 case tok::kw___pascal:
John McCall7f040a92010-12-24 02:08:15 +00002070 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik52fc3142010-09-03 01:29:35 +00002071 return true;
2072
Douglas Gregor12e083c2008-11-07 15:42:26 +00002073 default:
2074 // Not a type-specifier; do nothing.
2075 return false;
2076 }
2077
2078 // If the specifier combination wasn't legal, issue a diagnostic.
2079 if (isInvalid) {
2080 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner1ab3b962008-11-18 07:48:38 +00002081 // Pick between error or extwarn.
Chris Lattner1ab3b962008-11-18 07:48:38 +00002082 Diag(Tok, DiagID) << PrevSpec;
Douglas Gregor12e083c2008-11-07 15:42:26 +00002083 }
2084 DS.SetRangeEnd(Tok.getLocation());
2085 ConsumeToken(); // whatever we parsed above.
2086 return true;
2087}
Reid Spencer5f016e22007-07-11 17:01:13 +00002088
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002089/// ParseStructDeclaration - Parse a struct declaration without the terminating
2090/// semicolon.
2091///
Reid Spencer5f016e22007-07-11 17:01:13 +00002092/// struct-declaration:
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002093/// specifier-qualifier-list struct-declarator-list
Reid Spencer5f016e22007-07-11 17:01:13 +00002094/// [GNU] __extension__ struct-declaration
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002095/// [GNU] specifier-qualifier-list
Reid Spencer5f016e22007-07-11 17:01:13 +00002096/// struct-declarator-list:
2097/// struct-declarator
2098/// struct-declarator-list ',' struct-declarator
2099/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
2100/// struct-declarator:
2101/// declarator
2102/// [GNU] declarator attributes[opt]
2103/// declarator[opt] ':' constant-expression
2104/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
2105///
Chris Lattnere1359422008-04-10 06:46:29 +00002106void Parser::
John McCallbdd563e2009-11-03 02:38:08 +00002107ParseStructDeclaration(DeclSpec &DS, FieldCallback &Fields) {
Chris Lattnerc46d1a12008-10-20 06:45:43 +00002108 if (Tok.is(tok::kw___extension__)) {
2109 // __extension__ silences extension warnings in the subexpression.
2110 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroff28a7ca82007-08-20 22:28:22 +00002111 ConsumeToken();
Chris Lattnerc46d1a12008-10-20 06:45:43 +00002112 return ParseStructDeclaration(DS, Fields);
2113 }
Mike Stump1eb44332009-09-09 15:08:12 +00002114
Steve Naroff28a7ca82007-08-20 22:28:22 +00002115 // Parse the common specifier-qualifiers-list piece.
Steve Naroff28a7ca82007-08-20 22:28:22 +00002116 ParseSpecifierQualifierList(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00002117
Douglas Gregor4920f1f2009-01-12 22:49:06 +00002118 // If there are no declarators, this is a free-standing declaration
2119 // specifier. Let the actions module cope with it.
Chris Lattner04d66662007-10-09 17:33:22 +00002120 if (Tok.is(tok::semi)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00002121 Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none, DS);
Steve Naroff28a7ca82007-08-20 22:28:22 +00002122 return;
2123 }
2124
2125 // Read struct-declarators until we find the semicolon.
John McCallbdd563e2009-11-03 02:38:08 +00002126 bool FirstDeclarator = true;
Steve Naroff28a7ca82007-08-20 22:28:22 +00002127 while (1) {
John McCall54abf7d2009-11-04 02:18:39 +00002128 ParsingDeclRAIIObject PD(*this);
John McCallbdd563e2009-11-03 02:38:08 +00002129 FieldDeclarator DeclaratorInfo(DS);
2130
2131 // Attributes are only allowed here on successive declarators.
John McCall7f040a92010-12-24 02:08:15 +00002132 if (!FirstDeclarator)
2133 MaybeParseGNUAttributes(DeclaratorInfo.D);
Mike Stump1eb44332009-09-09 15:08:12 +00002134
Steve Naroff28a7ca82007-08-20 22:28:22 +00002135 /// struct-declarator: declarator
2136 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattnera1efc8c2009-12-10 01:59:24 +00002137 if (Tok.isNot(tok::colon)) {
2138 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
2139 ColonProtectionRAIIObject X(*this);
Chris Lattnere1359422008-04-10 06:46:29 +00002140 ParseDeclarator(DeclaratorInfo.D);
Chris Lattnera1efc8c2009-12-10 01:59:24 +00002141 }
Mike Stump1eb44332009-09-09 15:08:12 +00002142
Chris Lattner04d66662007-10-09 17:33:22 +00002143 if (Tok.is(tok::colon)) {
Steve Naroff28a7ca82007-08-20 22:28:22 +00002144 ConsumeToken();
John McCall60d7b3a2010-08-24 06:29:42 +00002145 ExprResult Res(ParseConstantExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002146 if (Res.isInvalid())
Steve Naroff28a7ca82007-08-20 22:28:22 +00002147 SkipUntil(tok::semi, true, true);
Chris Lattner60b1e3e2008-04-10 06:15:14 +00002148 else
Sebastian Redleffa8d12008-12-10 00:02:53 +00002149 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroff28a7ca82007-08-20 22:28:22 +00002150 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00002151
Steve Naroff28a7ca82007-08-20 22:28:22 +00002152 // If attributes exist after the declarator, parse them.
John McCall7f040a92010-12-24 02:08:15 +00002153 MaybeParseGNUAttributes(DeclaratorInfo.D);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002154
John McCallbdd563e2009-11-03 02:38:08 +00002155 // We're done with this declarator; invoke the callback.
John McCalld226f652010-08-21 09:40:31 +00002156 Decl *D = Fields.invoke(DeclaratorInfo);
John McCall54abf7d2009-11-04 02:18:39 +00002157 PD.complete(D);
John McCallbdd563e2009-11-03 02:38:08 +00002158
Steve Naroff28a7ca82007-08-20 22:28:22 +00002159 // If we don't have a comma, it is either the end of the list (a ';')
2160 // or an error, bail out.
Chris Lattner04d66662007-10-09 17:33:22 +00002161 if (Tok.isNot(tok::comma))
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002162 return;
Sebastian Redlab197ba2009-02-09 18:23:29 +00002163
Steve Naroff28a7ca82007-08-20 22:28:22 +00002164 // Consume the comma.
2165 ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00002166
John McCallbdd563e2009-11-03 02:38:08 +00002167 FirstDeclarator = false;
Steve Naroff28a7ca82007-08-20 22:28:22 +00002168 }
Steve Naroff28a7ca82007-08-20 22:28:22 +00002169}
2170
2171/// ParseStructUnionBody
2172/// struct-contents:
2173/// struct-declaration-list
2174/// [EXT] empty
2175/// [GNU] "struct-declaration-list" without terminatoring ';'
2176/// struct-declaration-list:
2177/// struct-declaration
2178/// struct-declaration-list struct-declaration
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002179/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroff28a7ca82007-08-20 22:28:22 +00002180///
Reid Spencer5f016e22007-07-11 17:01:13 +00002181void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
John McCalld226f652010-08-21 09:40:31 +00002182 unsigned TagType, Decl *TagDecl) {
John McCallf312b1e2010-08-26 23:41:50 +00002183 PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc,
2184 "parsing struct/union body");
Mike Stump1eb44332009-09-09 15:08:12 +00002185
Reid Spencer5f016e22007-07-11 17:01:13 +00002186 SourceLocation LBraceLoc = ConsumeBrace();
Mike Stump1eb44332009-09-09 15:08:12 +00002187
Douglas Gregor3218c4b2009-01-09 22:42:13 +00002188 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
Douglas Gregor23c94db2010-07-02 17:43:08 +00002189 Actions.ActOnTagStartDefinition(getCurScope(), TagDecl);
Douglas Gregor72de6672009-01-08 20:45:30 +00002190
Reid Spencer5f016e22007-07-11 17:01:13 +00002191 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
2192 // C++.
Douglas Gregore37ac4f2008-04-13 21:30:24 +00002193 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Douglas Gregor03332962010-07-29 14:29:34 +00002194 Diag(Tok, diag::ext_empty_struct_union)
2195 << (TagType == TST_union);
Reid Spencer5f016e22007-07-11 17:01:13 +00002196
John McCalld226f652010-08-21 09:40:31 +00002197 llvm::SmallVector<Decl *, 32> FieldDecls;
Chris Lattnere1359422008-04-10 06:46:29 +00002198
Reid Spencer5f016e22007-07-11 17:01:13 +00002199 // While we still have something to read, read the declarations in the struct.
Chris Lattner04d66662007-10-09 17:33:22 +00002200 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002201 // Each iteration of this loop reads one struct-declaration.
Mike Stump1eb44332009-09-09 15:08:12 +00002202
Reid Spencer5f016e22007-07-11 17:01:13 +00002203 // Check for extraneous top-level semicolon.
Chris Lattner04d66662007-10-09 17:33:22 +00002204 if (Tok.is(tok::semi)) {
Douglas Gregor9b3064b2009-04-01 22:41:11 +00002205 Diag(Tok, diag::ext_extra_struct_semi)
Douglas Gregorf13ca062010-06-16 23:08:59 +00002206 << DeclSpec::getSpecifierName((DeclSpec::TST)TagType)
Douglas Gregor849b2432010-03-31 17:46:05 +00002207 << FixItHint::CreateRemoval(Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00002208 ConsumeToken();
2209 continue;
2210 }
Chris Lattnere1359422008-04-10 06:46:29 +00002211
2212 // Parse all the comma separated declarators.
John McCall0b7e6782011-03-24 11:26:52 +00002213 DeclSpec DS(AttrFactory);
Mike Stump1eb44332009-09-09 15:08:12 +00002214
John McCallbdd563e2009-11-03 02:38:08 +00002215 if (!Tok.is(tok::at)) {
2216 struct CFieldCallback : FieldCallback {
2217 Parser &P;
John McCalld226f652010-08-21 09:40:31 +00002218 Decl *TagDecl;
2219 llvm::SmallVectorImpl<Decl *> &FieldDecls;
John McCallbdd563e2009-11-03 02:38:08 +00002220
John McCalld226f652010-08-21 09:40:31 +00002221 CFieldCallback(Parser &P, Decl *TagDecl,
2222 llvm::SmallVectorImpl<Decl *> &FieldDecls) :
John McCallbdd563e2009-11-03 02:38:08 +00002223 P(P), TagDecl(TagDecl), FieldDecls(FieldDecls) {}
2224
John McCalld226f652010-08-21 09:40:31 +00002225 virtual Decl *invoke(FieldDeclarator &FD) {
John McCallbdd563e2009-11-03 02:38:08 +00002226 // Install the declarator into the current TagDecl.
John McCalld226f652010-08-21 09:40:31 +00002227 Decl *Field = P.Actions.ActOnField(P.getCurScope(), TagDecl,
John McCall4ba39712009-11-03 21:13:47 +00002228 FD.D.getDeclSpec().getSourceRange().getBegin(),
2229 FD.D, FD.BitfieldSize);
John McCallbdd563e2009-11-03 02:38:08 +00002230 FieldDecls.push_back(Field);
2231 return Field;
Douglas Gregor91a28862009-08-26 14:27:30 +00002232 }
John McCallbdd563e2009-11-03 02:38:08 +00002233 } Callback(*this, TagDecl, FieldDecls);
2234
2235 ParseStructDeclaration(DS, Callback);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002236 } else { // Handle @defs
2237 ConsumeToken();
2238 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
2239 Diag(Tok, diag::err_unexpected_at);
Chris Lattner3e156ad2010-02-02 00:37:27 +00002240 SkipUntil(tok::semi, true);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002241 continue;
2242 }
2243 ConsumeToken();
2244 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
2245 if (!Tok.is(tok::identifier)) {
2246 Diag(Tok, diag::err_expected_ident);
Chris Lattner3e156ad2010-02-02 00:37:27 +00002247 SkipUntil(tok::semi, true);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002248 continue;
2249 }
John McCalld226f652010-08-21 09:40:31 +00002250 llvm::SmallVector<Decl *, 16> Fields;
Douglas Gregor23c94db2010-07-02 17:43:08 +00002251 Actions.ActOnDefs(getCurScope(), TagDecl, Tok.getLocation(),
Douglas Gregor44b43212008-12-11 16:49:14 +00002252 Tok.getIdentifierInfo(), Fields);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002253 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
2254 ConsumeToken();
2255 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
Mike Stump1eb44332009-09-09 15:08:12 +00002256 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002257
Chris Lattner04d66662007-10-09 17:33:22 +00002258 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002259 ConsumeToken();
Chris Lattner04d66662007-10-09 17:33:22 +00002260 } else if (Tok.is(tok::r_brace)) {
Chris Lattner3e156ad2010-02-02 00:37:27 +00002261 ExpectAndConsume(tok::semi, diag::ext_expected_semi_decl_list);
Reid Spencer5f016e22007-07-11 17:01:13 +00002262 break;
2263 } else {
Chris Lattner3e156ad2010-02-02 00:37:27 +00002264 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
2265 // Skip to end of block or statement to avoid ext-warning on extra ';'.
Reid Spencer5f016e22007-07-11 17:01:13 +00002266 SkipUntil(tok::r_brace, true, true);
Chris Lattner3e156ad2010-02-02 00:37:27 +00002267 // If we stopped at a ';', eat it.
2268 if (Tok.is(tok::semi)) ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00002269 }
2270 }
Mike Stump1eb44332009-09-09 15:08:12 +00002271
Steve Naroff60fccee2007-10-29 21:38:07 +00002272 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00002273
John McCall0b7e6782011-03-24 11:26:52 +00002274 ParsedAttributes attrs(AttrFactory);
Reid Spencer5f016e22007-07-11 17:01:13 +00002275 // If attributes exist after struct contents, parse them.
John McCall7f040a92010-12-24 02:08:15 +00002276 MaybeParseGNUAttributes(attrs);
Daniel Dunbar1bfe1c22008-10-03 02:03:53 +00002277
Douglas Gregor23c94db2010-07-02 17:43:08 +00002278 Actions.ActOnFields(getCurScope(),
Jay Foadbeaaccd2009-05-21 09:52:38 +00002279 RecordLoc, TagDecl, FieldDecls.data(), FieldDecls.size(),
Daniel Dunbar1bfe1c22008-10-03 02:03:53 +00002280 LBraceLoc, RBraceLoc,
John McCall7f040a92010-12-24 02:08:15 +00002281 attrs.getList());
Douglas Gregor72de6672009-01-08 20:45:30 +00002282 StructScope.Exit();
Douglas Gregor23c94db2010-07-02 17:43:08 +00002283 Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl, RBraceLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00002284}
2285
Reid Spencer5f016e22007-07-11 17:01:13 +00002286/// ParseEnumSpecifier
2287/// enum-specifier: [C99 6.7.2.2]
2288/// 'enum' identifier[opt] '{' enumerator-list '}'
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002289///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Reid Spencer5f016e22007-07-11 17:01:13 +00002290/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
2291/// '}' attributes[opt]
2292/// 'enum' identifier
2293/// [GNU] 'enum' attributes[opt] identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002294///
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002295/// [C++0x] enum-head '{' enumerator-list[opt] '}'
2296/// [C++0x] enum-head '{' enumerator-list ',' '}'
2297///
2298/// enum-head: [C++0x]
2299/// enum-key attributes[opt] identifier[opt] enum-base[opt]
2300/// enum-key attributes[opt] nested-name-specifier identifier enum-base[opt]
2301///
2302/// enum-key: [C++0x]
2303/// 'enum'
2304/// 'enum' 'class'
2305/// 'enum' 'struct'
2306///
2307/// enum-base: [C++0x]
2308/// ':' type-specifier-seq
2309///
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002310/// [C++] elaborated-type-specifier:
2311/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
2312///
Chris Lattner4c97d762009-04-12 21:49:30 +00002313void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregor9b9edd62010-03-02 17:53:14 +00002314 const ParsedTemplateInfo &TemplateInfo,
Chris Lattner4c97d762009-04-12 21:49:30 +00002315 AccessSpecifier AS) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002316 // Parse the tag portion of this.
Douglas Gregor374929f2009-09-18 15:37:17 +00002317 if (Tok.is(tok::code_completion)) {
2318 // Code completion for an enum name.
Douglas Gregor23c94db2010-07-02 17:43:08 +00002319 Actions.CodeCompleteTag(getCurScope(), DeclSpec::TST_enum);
Douglas Gregordc845342010-05-25 05:58:43 +00002320 ConsumeCodeCompletionToken();
Douglas Gregor374929f2009-09-18 15:37:17 +00002321 }
2322
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002323 // If attributes exist after tag, parse them.
John McCall0b7e6782011-03-24 11:26:52 +00002324 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00002325 MaybeParseGNUAttributes(attrs);
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002326
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00002327 CXXScopeSpec &SS = DS.getTypeSpecScope();
John McCall9ba61662010-02-26 08:45:28 +00002328 if (getLang().CPlusPlus) {
John McCallb3d87482010-08-24 05:47:05 +00002329 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), false))
John McCall9ba61662010-02-26 08:45:28 +00002330 return;
2331
2332 if (SS.isSet() && Tok.isNot(tok::identifier)) {
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002333 Diag(Tok, diag::err_expected_ident);
2334 if (Tok.isNot(tok::l_brace)) {
2335 // Has no name and is not a definition.
2336 // Skip the rest of this declarator, up until the comma or semicolon.
2337 SkipUntil(tok::comma, true);
2338 return;
2339 }
2340 }
2341 }
Mike Stump1eb44332009-09-09 15:08:12 +00002342
Douglas Gregor86f208c2011-02-22 20:32:04 +00002343 bool AllowFixedUnderlyingType = getLang().CPlusPlus0x || getLang().Microsoft;
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002344 bool IsScopedEnum = false;
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00002345 bool IsScopedUsingClassTag = false;
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002346
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00002347 if (getLang().CPlusPlus0x &&
2348 (Tok.is(tok::kw_class) || Tok.is(tok::kw_struct))) {
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002349 IsScopedEnum = true;
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00002350 IsScopedUsingClassTag = Tok.is(tok::kw_class);
2351 ConsumeToken();
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002352 }
2353
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002354 // Must have either 'enum name' or 'enum {...}'.
Douglas Gregorb9075602011-02-22 02:55:24 +00002355 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace) &&
2356 (AllowFixedUnderlyingType && Tok.isNot(tok::colon))) {
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002357 Diag(Tok, diag::err_expected_ident_lbrace);
Mike Stump1eb44332009-09-09 15:08:12 +00002358
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002359 // Skip the rest of this declarator, up until the comma or semicolon.
2360 SkipUntil(tok::comma, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00002361 return;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002362 }
Mike Stump1eb44332009-09-09 15:08:12 +00002363
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002364 // If an identifier is present, consume and remember it.
2365 IdentifierInfo *Name = 0;
2366 SourceLocation NameLoc;
2367 if (Tok.is(tok::identifier)) {
2368 Name = Tok.getIdentifierInfo();
2369 NameLoc = ConsumeToken();
2370 }
Mike Stump1eb44332009-09-09 15:08:12 +00002371
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002372 if (!Name && IsScopedEnum) {
2373 // C++0x 7.2p2: The optional identifier shall not be omitted in the
2374 // declaration of a scoped enumeration.
2375 Diag(Tok, diag::err_scoped_enum_missing_identifier);
2376 IsScopedEnum = false;
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00002377 IsScopedUsingClassTag = false;
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002378 }
2379
2380 TypeResult BaseType;
2381
Douglas Gregora61b3e72010-12-01 17:42:47 +00002382 // Parse the fixed underlying type.
Douglas Gregorb9075602011-02-22 02:55:24 +00002383 if (AllowFixedUnderlyingType && Tok.is(tok::colon)) {
Douglas Gregora61b3e72010-12-01 17:42:47 +00002384 bool PossibleBitfield = false;
2385 if (getCurScope()->getFlags() & Scope::ClassScope) {
2386 // If we're in class scope, this can either be an enum declaration with
2387 // an underlying type, or a declaration of a bitfield member. We try to
2388 // use a simple disambiguation scheme first to catch the common cases
2389 // (integer literal, sizeof); if it's still ambiguous, we then consider
2390 // anything that's a simple-type-specifier followed by '(' as an
2391 // expression. This suffices because function types are not valid
2392 // underlying types anyway.
2393 TPResult TPR = isExpressionOrTypeSpecifierSimple(NextToken().getKind());
2394 // If the next token starts an expression, we know we're parsing a
2395 // bit-field. This is the common case.
2396 if (TPR == TPResult::True())
2397 PossibleBitfield = true;
2398 // If the next token starts a type-specifier-seq, it may be either a
2399 // a fixed underlying type or the start of a function-style cast in C++;
2400 // lookahead one more token to see if it's obvious that we have a
2401 // fixed underlying type.
2402 else if (TPR == TPResult::False() &&
2403 GetLookAheadToken(2).getKind() == tok::semi) {
2404 // Consume the ':'.
2405 ConsumeToken();
2406 } else {
2407 // We have the start of a type-specifier-seq, so we have to perform
2408 // tentative parsing to determine whether we have an expression or a
2409 // type.
2410 TentativeParsingAction TPA(*this);
2411
2412 // Consume the ':'.
2413 ConsumeToken();
2414
Douglas Gregor86f208c2011-02-22 20:32:04 +00002415 if ((getLang().CPlusPlus &&
2416 isCXXDeclarationSpecifier() != TPResult::True()) ||
2417 (!getLang().CPlusPlus && !isDeclarationSpecifier(true))) {
Douglas Gregora61b3e72010-12-01 17:42:47 +00002418 // We'll parse this as a bitfield later.
2419 PossibleBitfield = true;
2420 TPA.Revert();
2421 } else {
2422 // We have a type-specifier-seq.
2423 TPA.Commit();
2424 }
2425 }
2426 } else {
2427 // Consume the ':'.
2428 ConsumeToken();
2429 }
2430
2431 if (!PossibleBitfield) {
2432 SourceRange Range;
2433 BaseType = ParseTypeName(&Range);
Douglas Gregor86f208c2011-02-22 20:32:04 +00002434
2435 if (!getLang().CPlusPlus0x)
2436 Diag(StartLoc, diag::ext_ms_enum_fixed_underlying_type)
2437 << Range;
Douglas Gregora61b3e72010-12-01 17:42:47 +00002438 }
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002439 }
2440
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002441 // There are three options here. If we have 'enum foo;', then this is a
2442 // forward declaration. If we have 'enum foo {...' then this is a
2443 // definition. Otherwise we have something like 'enum foo xyz', a reference.
2444 //
2445 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
2446 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
2447 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
2448 //
John McCallf312b1e2010-08-26 23:41:50 +00002449 Sema::TagUseKind TUK;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002450 if (Tok.is(tok::l_brace))
John McCallf312b1e2010-08-26 23:41:50 +00002451 TUK = Sema::TUK_Definition;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002452 else if (Tok.is(tok::semi))
John McCallf312b1e2010-08-26 23:41:50 +00002453 TUK = Sema::TUK_Declaration;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002454 else
John McCallf312b1e2010-08-26 23:41:50 +00002455 TUK = Sema::TUK_Reference;
Douglas Gregor8fc6d232010-05-03 17:48:54 +00002456
2457 // enums cannot be templates, although they can be referenced from a
2458 // template.
2459 if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate &&
John McCallf312b1e2010-08-26 23:41:50 +00002460 TUK != Sema::TUK_Reference) {
Douglas Gregor8fc6d232010-05-03 17:48:54 +00002461 Diag(Tok, diag::err_enum_template);
2462
2463 // Skip the rest of this declarator, up until the comma or semicolon.
2464 SkipUntil(tok::comma, true);
2465 return;
2466 }
2467
Douglas Gregorb9075602011-02-22 02:55:24 +00002468 if (!Name && TUK != Sema::TUK_Definition) {
2469 Diag(Tok, diag::err_enumerator_unnamed_no_def);
2470
2471 // Skip the rest of this declarator, up until the comma or semicolon.
2472 SkipUntil(tok::comma, true);
2473 return;
2474 }
2475
Douglas Gregor402abb52009-05-28 23:31:59 +00002476 bool Owned = false;
John McCallc4e70192009-09-11 04:59:25 +00002477 bool IsDependent = false;
Douglas Gregor48c89f42010-04-24 16:38:41 +00002478 const char *PrevSpec = 0;
2479 unsigned DiagID;
John McCalld226f652010-08-21 09:40:31 +00002480 Decl *TagDecl = Actions.ActOnTag(getCurScope(), DeclSpec::TST_enum, TUK,
John McCall7f040a92010-12-24 02:08:15 +00002481 StartLoc, SS, Name, NameLoc, attrs.getList(),
John McCalld226f652010-08-21 09:40:31 +00002482 AS,
John McCallf312b1e2010-08-26 23:41:50 +00002483 MultiTemplateParamsArg(Actions),
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002484 Owned, IsDependent, IsScopedEnum,
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00002485 IsScopedUsingClassTag, BaseType);
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002486
Douglas Gregor48c89f42010-04-24 16:38:41 +00002487 if (IsDependent) {
2488 // This enum has a dependent nested-name-specifier. Handle it as a
2489 // dependent tag.
2490 if (!Name) {
2491 DS.SetTypeSpecError();
2492 Diag(Tok, diag::err_expected_type_name_after_typename);
2493 return;
2494 }
2495
Douglas Gregor23c94db2010-07-02 17:43:08 +00002496 TypeResult Type = Actions.ActOnDependentTag(getCurScope(), DeclSpec::TST_enum,
Douglas Gregor48c89f42010-04-24 16:38:41 +00002497 TUK, SS, Name, StartLoc,
2498 NameLoc);
2499 if (Type.isInvalid()) {
2500 DS.SetTypeSpecError();
2501 return;
2502 }
2503
Abramo Bagnara0daaf322011-03-16 20:16:18 +00002504 if (DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc,
2505 NameLoc.isValid() ? NameLoc : StartLoc,
2506 PrevSpec, DiagID, Type.get()))
Douglas Gregor48c89f42010-04-24 16:38:41 +00002507 Diag(StartLoc, DiagID) << PrevSpec;
2508
2509 return;
2510 }
Mike Stump1eb44332009-09-09 15:08:12 +00002511
John McCalld226f652010-08-21 09:40:31 +00002512 if (!TagDecl) {
Douglas Gregor48c89f42010-04-24 16:38:41 +00002513 // The action failed to produce an enumeration tag. If this is a
2514 // definition, consume the entire definition.
2515 if (Tok.is(tok::l_brace)) {
2516 ConsumeBrace();
2517 SkipUntil(tok::r_brace);
2518 }
2519
2520 DS.SetTypeSpecError();
2521 return;
2522 }
2523
Chris Lattner04d66662007-10-09 17:33:22 +00002524 if (Tok.is(tok::l_brace))
Reid Spencer5f016e22007-07-11 17:01:13 +00002525 ParseEnumBody(StartLoc, TagDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00002526
Abramo Bagnara0daaf322011-03-16 20:16:18 +00002527 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc,
2528 NameLoc.isValid() ? NameLoc : StartLoc,
2529 PrevSpec, DiagID, TagDecl, Owned))
John McCallfec54012009-08-03 20:12:06 +00002530 Diag(StartLoc, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00002531}
2532
2533/// ParseEnumBody - Parse a {} enclosed enumerator-list.
2534/// enumerator-list:
2535/// enumerator
2536/// enumerator-list ',' enumerator
2537/// enumerator:
2538/// enumeration-constant
2539/// enumeration-constant '=' constant-expression
2540/// enumeration-constant:
2541/// identifier
2542///
John McCalld226f652010-08-21 09:40:31 +00002543void Parser::ParseEnumBody(SourceLocation StartLoc, Decl *EnumDecl) {
Douglas Gregor074149e2009-01-05 19:45:36 +00002544 // Enter the scope of the enum body and start the definition.
2545 ParseScope EnumScope(this, Scope::DeclScope);
Douglas Gregor23c94db2010-07-02 17:43:08 +00002546 Actions.ActOnTagStartDefinition(getCurScope(), EnumDecl);
Douglas Gregor074149e2009-01-05 19:45:36 +00002547
Reid Spencer5f016e22007-07-11 17:01:13 +00002548 SourceLocation LBraceLoc = ConsumeBrace();
Mike Stump1eb44332009-09-09 15:08:12 +00002549
Chris Lattner7946dd32007-08-27 17:24:30 +00002550 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
Chris Lattner04d66662007-10-09 17:33:22 +00002551 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Fariborz Jahanian05115522010-05-28 22:23:22 +00002552 Diag(Tok, diag::error_empty_enum);
Mike Stump1eb44332009-09-09 15:08:12 +00002553
John McCalld226f652010-08-21 09:40:31 +00002554 llvm::SmallVector<Decl *, 32> EnumConstantDecls;
Reid Spencer5f016e22007-07-11 17:01:13 +00002555
John McCalld226f652010-08-21 09:40:31 +00002556 Decl *LastEnumConstDecl = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00002557
Reid Spencer5f016e22007-07-11 17:01:13 +00002558 // Parse the enumerator-list.
Chris Lattner04d66662007-10-09 17:33:22 +00002559 while (Tok.is(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002560 IdentifierInfo *Ident = Tok.getIdentifierInfo();
2561 SourceLocation IdentLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00002562
John McCall5b629aa2010-10-22 23:36:17 +00002563 // If attributes exist after the enumerator, parse them.
John McCall0b7e6782011-03-24 11:26:52 +00002564 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00002565 MaybeParseGNUAttributes(attrs);
John McCall5b629aa2010-10-22 23:36:17 +00002566
Reid Spencer5f016e22007-07-11 17:01:13 +00002567 SourceLocation EqualLoc;
John McCall60d7b3a2010-08-24 06:29:42 +00002568 ExprResult AssignedVal;
Chris Lattner04d66662007-10-09 17:33:22 +00002569 if (Tok.is(tok::equal)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002570 EqualLoc = ConsumeToken();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002571 AssignedVal = ParseConstantExpression();
2572 if (AssignedVal.isInvalid())
Reid Spencer5f016e22007-07-11 17:01:13 +00002573 SkipUntil(tok::comma, tok::r_brace, true, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00002574 }
Mike Stump1eb44332009-09-09 15:08:12 +00002575
Reid Spencer5f016e22007-07-11 17:01:13 +00002576 // Install the enumerator constant into EnumDecl.
John McCalld226f652010-08-21 09:40:31 +00002577 Decl *EnumConstDecl = Actions.ActOnEnumConstant(getCurScope(), EnumDecl,
2578 LastEnumConstDecl,
2579 IdentLoc, Ident,
John McCall7f040a92010-12-24 02:08:15 +00002580 attrs.getList(), EqualLoc,
John McCalld226f652010-08-21 09:40:31 +00002581 AssignedVal.release());
Reid Spencer5f016e22007-07-11 17:01:13 +00002582 EnumConstantDecls.push_back(EnumConstDecl);
2583 LastEnumConstDecl = EnumConstDecl;
Mike Stump1eb44332009-09-09 15:08:12 +00002584
Douglas Gregor751f6922010-09-07 14:51:08 +00002585 if (Tok.is(tok::identifier)) {
2586 // We're missing a comma between enumerators.
2587 SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation);
2588 Diag(Loc, diag::err_enumerator_list_missing_comma)
2589 << FixItHint::CreateInsertion(Loc, ", ");
2590 continue;
2591 }
2592
Chris Lattner04d66662007-10-09 17:33:22 +00002593 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +00002594 break;
2595 SourceLocation CommaLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00002596
2597 if (Tok.isNot(tok::identifier) &&
Douglas Gregor9b3064b2009-04-01 22:41:11 +00002598 !(getLang().C99 || getLang().CPlusPlus0x))
2599 Diag(CommaLoc, diag::ext_enumerator_list_comma)
2600 << getLang().CPlusPlus
Douglas Gregor849b2432010-03-31 17:46:05 +00002601 << FixItHint::CreateRemoval(CommaLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00002602 }
Mike Stump1eb44332009-09-09 15:08:12 +00002603
Reid Spencer5f016e22007-07-11 17:01:13 +00002604 // Eat the }.
Mike Stumpc6e35aa2009-05-16 07:06:02 +00002605 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00002606
Reid Spencer5f016e22007-07-11 17:01:13 +00002607 // If attributes exist after the identifier list, parse them.
John McCall0b7e6782011-03-24 11:26:52 +00002608 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00002609 MaybeParseGNUAttributes(attrs);
Douglas Gregor72de6672009-01-08 20:45:30 +00002610
Edward O'Callaghanfee13812009-08-08 14:36:57 +00002611 Actions.ActOnEnumBody(StartLoc, LBraceLoc, RBraceLoc, EnumDecl,
2612 EnumConstantDecls.data(), EnumConstantDecls.size(),
John McCall7f040a92010-12-24 02:08:15 +00002613 getCurScope(), attrs.getList());
Mike Stump1eb44332009-09-09 15:08:12 +00002614
Douglas Gregor72de6672009-01-08 20:45:30 +00002615 EnumScope.Exit();
Douglas Gregor23c94db2010-07-02 17:43:08 +00002616 Actions.ActOnTagFinishDefinition(getCurScope(), EnumDecl, RBraceLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00002617}
2618
2619/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff5f8aa692008-02-11 23:15:56 +00002620/// start of a type-qualifier-list.
2621bool Parser::isTypeQualifier() const {
2622 switch (Tok.getKind()) {
2623 default: return false;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00002624
2625 // type-qualifier only in OpenCL
2626 case tok::kw_private:
2627 return getLang().OpenCL;
2628
Steve Naroff5f8aa692008-02-11 23:15:56 +00002629 // type-qualifier
2630 case tok::kw_const:
2631 case tok::kw_volatile:
2632 case tok::kw_restrict:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00002633 case tok::kw___private:
2634 case tok::kw___local:
2635 case tok::kw___global:
2636 case tok::kw___constant:
2637 case tok::kw___read_only:
2638 case tok::kw___read_write:
2639 case tok::kw___write_only:
Steve Naroff5f8aa692008-02-11 23:15:56 +00002640 return true;
2641 }
2642}
2643
Chris Lattnerb3a4e432010-02-28 18:18:36 +00002644/// isKnownToBeTypeSpecifier - Return true if we know that the specified token
2645/// is definitely a type-specifier. Return false if it isn't part of a type
2646/// specifier or if we're not sure.
2647bool Parser::isKnownToBeTypeSpecifier(const Token &Tok) const {
2648 switch (Tok.getKind()) {
2649 default: return false;
2650 // type-specifiers
2651 case tok::kw_short:
2652 case tok::kw_long:
2653 case tok::kw_signed:
2654 case tok::kw_unsigned:
2655 case tok::kw__Complex:
2656 case tok::kw__Imaginary:
2657 case tok::kw_void:
2658 case tok::kw_char:
2659 case tok::kw_wchar_t:
2660 case tok::kw_char16_t:
2661 case tok::kw_char32_t:
2662 case tok::kw_int:
2663 case tok::kw_float:
2664 case tok::kw_double:
2665 case tok::kw_bool:
2666 case tok::kw__Bool:
2667 case tok::kw__Decimal32:
2668 case tok::kw__Decimal64:
2669 case tok::kw__Decimal128:
2670 case tok::kw___vector:
2671
2672 // struct-or-union-specifier (C99) or class-specifier (C++)
2673 case tok::kw_class:
2674 case tok::kw_struct:
2675 case tok::kw_union:
2676 // enum-specifier
2677 case tok::kw_enum:
2678
2679 // typedef-name
2680 case tok::annot_typename:
2681 return true;
2682 }
2683}
2684
Steve Naroff5f8aa692008-02-11 23:15:56 +00002685/// isTypeSpecifierQualifier - Return true if the current token could be the
Reid Spencer5f016e22007-07-11 17:01:13 +00002686/// start of a specifier-qualifier-list.
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002687bool Parser::isTypeSpecifierQualifier() {
Reid Spencer5f016e22007-07-11 17:01:13 +00002688 switch (Tok.getKind()) {
2689 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002690
Chris Lattner166a8fc2009-01-04 23:41:41 +00002691 case tok::identifier: // foo::bar
John Thompson82287d12010-02-05 00:12:22 +00002692 if (TryAltiVecVectorToken())
2693 return true;
2694 // Fall through.
Douglas Gregord57959a2009-03-27 23:10:48 +00002695 case tok::kw_typename: // typename T::type
Chris Lattner166a8fc2009-01-04 23:41:41 +00002696 // Annotate typenames and C++ scope specifiers. If we get one, just
2697 // recurse to handle whatever we get.
2698 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00002699 return true;
2700 if (Tok.is(tok::identifier))
2701 return false;
2702 return isTypeSpecifierQualifier();
Douglas Gregord57959a2009-03-27 23:10:48 +00002703
Chris Lattner166a8fc2009-01-04 23:41:41 +00002704 case tok::coloncolon: // ::foo::bar
2705 if (NextToken().is(tok::kw_new) || // ::new
2706 NextToken().is(tok::kw_delete)) // ::delete
2707 return false;
2708
Chris Lattner166a8fc2009-01-04 23:41:41 +00002709 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00002710 return true;
2711 return isTypeSpecifierQualifier();
Mike Stump1eb44332009-09-09 15:08:12 +00002712
Reid Spencer5f016e22007-07-11 17:01:13 +00002713 // GNU attributes support.
2714 case tok::kw___attribute:
Steve Naroffd1861fd2007-07-31 12:34:36 +00002715 // GNU typeof support.
2716 case tok::kw_typeof:
Mike Stump1eb44332009-09-09 15:08:12 +00002717
Reid Spencer5f016e22007-07-11 17:01:13 +00002718 // type-specifiers
2719 case tok::kw_short:
2720 case tok::kw_long:
2721 case tok::kw_signed:
2722 case tok::kw_unsigned:
2723 case tok::kw__Complex:
2724 case tok::kw__Imaginary:
2725 case tok::kw_void:
2726 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00002727 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002728 case tok::kw_char16_t:
2729 case tok::kw_char32_t:
Reid Spencer5f016e22007-07-11 17:01:13 +00002730 case tok::kw_int:
2731 case tok::kw_float:
2732 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00002733 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00002734 case tok::kw__Bool:
2735 case tok::kw__Decimal32:
2736 case tok::kw__Decimal64:
2737 case tok::kw__Decimal128:
John Thompson82287d12010-02-05 00:12:22 +00002738 case tok::kw___vector:
Mike Stump1eb44332009-09-09 15:08:12 +00002739
Chris Lattner99dc9142008-04-13 18:59:07 +00002740 // struct-or-union-specifier (C99) or class-specifier (C++)
2741 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00002742 case tok::kw_struct:
2743 case tok::kw_union:
2744 // enum-specifier
2745 case tok::kw_enum:
Mike Stump1eb44332009-09-09 15:08:12 +00002746
Reid Spencer5f016e22007-07-11 17:01:13 +00002747 // type-qualifier
2748 case tok::kw_const:
2749 case tok::kw_volatile:
2750 case tok::kw_restrict:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002751
2752 // typedef-name
Chris Lattnerb31757b2009-01-06 05:06:21 +00002753 case tok::annot_typename:
Reid Spencer5f016e22007-07-11 17:01:13 +00002754 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00002755
Chris Lattner7c186be2008-10-20 00:25:30 +00002756 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
2757 case tok::less:
2758 return getLang().ObjC1;
Mike Stump1eb44332009-09-09 15:08:12 +00002759
Steve Naroff239f0732008-12-25 14:16:32 +00002760 case tok::kw___cdecl:
2761 case tok::kw___stdcall:
2762 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00002763 case tok::kw___thiscall:
Eli Friedman290eeb02009-06-08 23:27:34 +00002764 case tok::kw___w64:
2765 case tok::kw___ptr64:
Dawn Perchik52fc3142010-09-03 01:29:35 +00002766 case tok::kw___pascal:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00002767
2768 case tok::kw___private:
2769 case tok::kw___local:
2770 case tok::kw___global:
2771 case tok::kw___constant:
2772 case tok::kw___read_only:
2773 case tok::kw___read_write:
2774 case tok::kw___write_only:
2775
Eli Friedman290eeb02009-06-08 23:27:34 +00002776 return true;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00002777
2778 case tok::kw_private:
2779 return getLang().OpenCL;
Reid Spencer5f016e22007-07-11 17:01:13 +00002780 }
2781}
2782
2783/// isDeclarationSpecifier() - Return true if the current token is part of a
2784/// declaration specifier.
Douglas Gregor9497a732010-09-16 01:51:54 +00002785///
2786/// \param DisambiguatingWithExpression True to indicate that the purpose of
2787/// this check is to disambiguate between an expression and a declaration.
2788bool Parser::isDeclarationSpecifier(bool DisambiguatingWithExpression) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002789 switch (Tok.getKind()) {
2790 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002791
Peter Collingbourne207f4d82011-03-18 22:38:29 +00002792 case tok::kw_private:
2793 return getLang().OpenCL;
2794
Chris Lattner166a8fc2009-01-04 23:41:41 +00002795 case tok::identifier: // foo::bar
Steve Naroff61f72cb2009-03-09 21:12:44 +00002796 // Unfortunate hack to support "Class.factoryMethod" notation.
2797 if (getLang().ObjC1 && NextToken().is(tok::period))
2798 return false;
John Thompson82287d12010-02-05 00:12:22 +00002799 if (TryAltiVecVectorToken())
2800 return true;
2801 // Fall through.
Douglas Gregord57959a2009-03-27 23:10:48 +00002802 case tok::kw_typename: // typename T::type
Chris Lattner166a8fc2009-01-04 23:41:41 +00002803 // Annotate typenames and C++ scope specifiers. If we get one, just
2804 // recurse to handle whatever we get.
2805 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00002806 return true;
2807 if (Tok.is(tok::identifier))
2808 return false;
Douglas Gregor9497a732010-09-16 01:51:54 +00002809
2810 // If we're in Objective-C and we have an Objective-C class type followed
2811 // by an identifier and then either ':' or ']', in a place where an
2812 // expression is permitted, then this is probably a class message send
2813 // missing the initial '['. In this case, we won't consider this to be
2814 // the start of a declaration.
2815 if (DisambiguatingWithExpression &&
2816 isStartOfObjCClassMessageMissingOpenBracket())
2817 return false;
2818
John McCall9ba61662010-02-26 08:45:28 +00002819 return isDeclarationSpecifier();
2820
Chris Lattner166a8fc2009-01-04 23:41:41 +00002821 case tok::coloncolon: // ::foo::bar
2822 if (NextToken().is(tok::kw_new) || // ::new
2823 NextToken().is(tok::kw_delete)) // ::delete
2824 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002825
Chris Lattner166a8fc2009-01-04 23:41:41 +00002826 // Annotate typenames and C++ scope specifiers. If we get one, just
2827 // recurse to handle whatever we get.
2828 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00002829 return true;
2830 return isDeclarationSpecifier();
Mike Stump1eb44332009-09-09 15:08:12 +00002831
Reid Spencer5f016e22007-07-11 17:01:13 +00002832 // storage-class-specifier
2833 case tok::kw_typedef:
2834 case tok::kw_extern:
Steve Naroff8d54bf22007-12-18 00:16:02 +00002835 case tok::kw___private_extern__:
Reid Spencer5f016e22007-07-11 17:01:13 +00002836 case tok::kw_static:
2837 case tok::kw_auto:
2838 case tok::kw_register:
2839 case tok::kw___thread:
Mike Stump1eb44332009-09-09 15:08:12 +00002840
Reid Spencer5f016e22007-07-11 17:01:13 +00002841 // type-specifiers
2842 case tok::kw_short:
2843 case tok::kw_long:
2844 case tok::kw_signed:
2845 case tok::kw_unsigned:
2846 case tok::kw__Complex:
2847 case tok::kw__Imaginary:
2848 case tok::kw_void:
2849 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00002850 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002851 case tok::kw_char16_t:
2852 case tok::kw_char32_t:
2853
Reid Spencer5f016e22007-07-11 17:01:13 +00002854 case tok::kw_int:
2855 case tok::kw_float:
2856 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00002857 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00002858 case tok::kw__Bool:
2859 case tok::kw__Decimal32:
2860 case tok::kw__Decimal64:
2861 case tok::kw__Decimal128:
John Thompson82287d12010-02-05 00:12:22 +00002862 case tok::kw___vector:
Mike Stump1eb44332009-09-09 15:08:12 +00002863
Chris Lattner99dc9142008-04-13 18:59:07 +00002864 // struct-or-union-specifier (C99) or class-specifier (C++)
2865 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00002866 case tok::kw_struct:
2867 case tok::kw_union:
2868 // enum-specifier
2869 case tok::kw_enum:
Mike Stump1eb44332009-09-09 15:08:12 +00002870
Reid Spencer5f016e22007-07-11 17:01:13 +00002871 // type-qualifier
2872 case tok::kw_const:
2873 case tok::kw_volatile:
2874 case tok::kw_restrict:
Steve Naroffd1861fd2007-07-31 12:34:36 +00002875
Reid Spencer5f016e22007-07-11 17:01:13 +00002876 // function-specifier
2877 case tok::kw_inline:
Douglas Gregorb48fe382008-10-31 09:07:45 +00002878 case tok::kw_virtual:
2879 case tok::kw_explicit:
Chris Lattnerd6c7c182007-08-09 16:40:21 +00002880
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002881 // typedef-name
Chris Lattnerb31757b2009-01-06 05:06:21 +00002882 case tok::annot_typename:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002883
Chris Lattner1ef08762007-08-09 17:01:07 +00002884 // GNU typeof support.
2885 case tok::kw_typeof:
Mike Stump1eb44332009-09-09 15:08:12 +00002886
Chris Lattner1ef08762007-08-09 17:01:07 +00002887 // GNU attributes.
Chris Lattnerd6c7c182007-08-09 16:40:21 +00002888 case tok::kw___attribute:
Reid Spencer5f016e22007-07-11 17:01:13 +00002889 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00002890
Chris Lattnerf3948c42008-07-26 03:38:44 +00002891 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
2892 case tok::less:
2893 return getLang().ObjC1;
Mike Stump1eb44332009-09-09 15:08:12 +00002894
Steve Naroff47f52092009-01-06 19:34:12 +00002895 case tok::kw___declspec:
Steve Naroff239f0732008-12-25 14:16:32 +00002896 case tok::kw___cdecl:
2897 case tok::kw___stdcall:
2898 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00002899 case tok::kw___thiscall:
Eli Friedman290eeb02009-06-08 23:27:34 +00002900 case tok::kw___w64:
2901 case tok::kw___ptr64:
2902 case tok::kw___forceinline:
Dawn Perchik52fc3142010-09-03 01:29:35 +00002903 case tok::kw___pascal:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00002904
2905 case tok::kw___private:
2906 case tok::kw___local:
2907 case tok::kw___global:
2908 case tok::kw___constant:
2909 case tok::kw___read_only:
2910 case tok::kw___read_write:
2911 case tok::kw___write_only:
2912
Eli Friedman290eeb02009-06-08 23:27:34 +00002913 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00002914 }
2915}
2916
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002917bool Parser::isConstructorDeclarator() {
2918 TentativeParsingAction TPA(*this);
2919
2920 // Parse the C++ scope specifier.
2921 CXXScopeSpec SS;
John McCallb3d87482010-08-24 05:47:05 +00002922 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), true)) {
John McCall9ba61662010-02-26 08:45:28 +00002923 TPA.Revert();
2924 return false;
2925 }
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002926
2927 // Parse the constructor name.
2928 if (Tok.is(tok::identifier) || Tok.is(tok::annot_template_id)) {
2929 // We already know that we have a constructor name; just consume
2930 // the token.
2931 ConsumeToken();
2932 } else {
2933 TPA.Revert();
2934 return false;
2935 }
2936
2937 // Current class name must be followed by a left parentheses.
2938 if (Tok.isNot(tok::l_paren)) {
2939 TPA.Revert();
2940 return false;
2941 }
2942 ConsumeParen();
2943
2944 // A right parentheses or ellipsis signals that we have a constructor.
2945 if (Tok.is(tok::r_paren) || Tok.is(tok::ellipsis)) {
2946 TPA.Revert();
2947 return true;
2948 }
2949
2950 // If we need to, enter the specified scope.
2951 DeclaratorScopeObj DeclScopeObj(*this, SS);
Douglas Gregor23c94db2010-07-02 17:43:08 +00002952 if (SS.isSet() && Actions.ShouldEnterDeclaratorScope(getCurScope(), SS))
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002953 DeclScopeObj.EnterDeclaratorScope();
2954
Francois Pichetdfaa5fb2011-01-31 04:54:32 +00002955 // Optionally skip Microsoft attributes.
John McCall0b7e6782011-03-24 11:26:52 +00002956 ParsedAttributes Attrs(AttrFactory);
Francois Pichetdfaa5fb2011-01-31 04:54:32 +00002957 MaybeParseMicrosoftAttributes(Attrs);
2958
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002959 // Check whether the next token(s) are part of a declaration
2960 // specifier, in which case we have the start of a parameter and,
2961 // therefore, we know that this is a constructor.
2962 bool IsConstructor = isDeclarationSpecifier();
2963 TPA.Revert();
2964 return IsConstructor;
2965}
Reid Spencer5f016e22007-07-11 17:01:13 +00002966
2967/// ParseTypeQualifierListOpt
Dawn Perchik52fc3142010-09-03 01:29:35 +00002968/// type-qualifier-list: [C99 6.7.5]
2969/// type-qualifier
2970/// [vendor] attributes
2971/// [ only if VendorAttributesAllowed=true ]
2972/// type-qualifier-list type-qualifier
2973/// [vendor] type-qualifier-list attributes
2974/// [ only if VendorAttributesAllowed=true ]
2975/// [C++0x] attribute-specifier[opt] is allowed before cv-qualifier-seq
2976/// [ only if CXX0XAttributesAllowed=true ]
2977/// Note: vendor can be GNU, MS, etc.
Reid Spencer5f016e22007-07-11 17:01:13 +00002978///
Dawn Perchik52fc3142010-09-03 01:29:35 +00002979void Parser::ParseTypeQualifierListOpt(DeclSpec &DS,
2980 bool VendorAttributesAllowed,
Sean Huntbbd37c62009-11-21 08:43:09 +00002981 bool CXX0XAttributesAllowed) {
2982 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier()) {
2983 SourceLocation Loc = Tok.getLocation();
John McCall0b7e6782011-03-24 11:26:52 +00002984 ParsedAttributesWithRange attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00002985 ParseCXX0XAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00002986 if (CXX0XAttributesAllowed)
John McCall7f040a92010-12-24 02:08:15 +00002987 DS.takeAttributesFrom(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00002988 else
2989 Diag(Loc, diag::err_attributes_not_allowed);
2990 }
Abramo Bagnara796aa442011-03-12 11:17:06 +00002991
2992 SourceLocation EndLoc;
2993
Reid Spencer5f016e22007-07-11 17:01:13 +00002994 while (1) {
John McCallfec54012009-08-03 20:12:06 +00002995 bool isInvalid = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00002996 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00002997 unsigned DiagID = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00002998 SourceLocation Loc = Tok.getLocation();
2999
3000 switch (Tok.getKind()) {
Douglas Gregor1a480c42010-08-27 17:35:51 +00003001 case tok::code_completion:
3002 Actions.CodeCompleteTypeQualifiers(DS);
3003 ConsumeCodeCompletionToken();
3004 break;
3005
Reid Spencer5f016e22007-07-11 17:01:13 +00003006 case tok::kw_const:
John McCallfec54012009-08-03 20:12:06 +00003007 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, DiagID,
3008 getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00003009 break;
3010 case tok::kw_volatile:
John McCallfec54012009-08-03 20:12:06 +00003011 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
3012 getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00003013 break;
3014 case tok::kw_restrict:
John McCallfec54012009-08-03 20:12:06 +00003015 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
3016 getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00003017 break;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003018
3019 // OpenCL qualifiers:
3020 case tok::kw_private:
3021 if (!getLang().OpenCL)
3022 goto DoneWithTypeQuals;
3023 case tok::kw___private:
3024 case tok::kw___global:
3025 case tok::kw___local:
3026 case tok::kw___constant:
3027 case tok::kw___read_only:
3028 case tok::kw___write_only:
3029 case tok::kw___read_write:
3030 ParseOpenCLQualifiers(DS);
3031 break;
3032
Eli Friedman290eeb02009-06-08 23:27:34 +00003033 case tok::kw___w64:
Steve Naroff86bc6cf2008-12-25 14:41:26 +00003034 case tok::kw___ptr64:
Steve Naroff239f0732008-12-25 14:16:32 +00003035 case tok::kw___cdecl:
3036 case tok::kw___stdcall:
3037 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00003038 case tok::kw___thiscall:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003039 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00003040 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman290eeb02009-06-08 23:27:34 +00003041 continue;
3042 }
3043 goto DoneWithTypeQuals;
Dawn Perchik52fc3142010-09-03 01:29:35 +00003044 case tok::kw___pascal:
3045 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00003046 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik52fc3142010-09-03 01:29:35 +00003047 continue;
3048 }
3049 goto DoneWithTypeQuals;
Reid Spencer5f016e22007-07-11 17:01:13 +00003050 case tok::kw___attribute:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003051 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00003052 ParseGNUAttributes(DS.getAttributes());
Chris Lattner5a69d1c2008-12-18 07:02:59 +00003053 continue; // do *not* consume the next token!
3054 }
3055 // otherwise, FALL THROUGH!
3056 default:
Steve Naroff239f0732008-12-25 14:16:32 +00003057 DoneWithTypeQuals:
Chris Lattner5a69d1c2008-12-18 07:02:59 +00003058 // If this is not a type-qualifier token, we're done reading type
3059 // qualifiers. First verify that DeclSpec's are consistent.
Douglas Gregor9b3064b2009-04-01 22:41:11 +00003060 DS.Finish(Diags, PP);
Abramo Bagnara796aa442011-03-12 11:17:06 +00003061 if (EndLoc.isValid())
3062 DS.SetRangeEnd(EndLoc);
Chris Lattner5a69d1c2008-12-18 07:02:59 +00003063 return;
Reid Spencer5f016e22007-07-11 17:01:13 +00003064 }
Chris Lattnera1fcbad2008-12-18 06:50:14 +00003065
Reid Spencer5f016e22007-07-11 17:01:13 +00003066 // If the specifier combination wasn't legal, issue a diagnostic.
3067 if (isInvalid) {
3068 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner1ab3b962008-11-18 07:48:38 +00003069 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00003070 }
Abramo Bagnara796aa442011-03-12 11:17:06 +00003071 EndLoc = ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00003072 }
3073}
3074
3075
3076/// ParseDeclarator - Parse and verify a newly-initialized declarator.
3077///
3078void Parser::ParseDeclarator(Declarator &D) {
3079 /// This implements the 'declarator' production in the C grammar, then checks
3080 /// for well-formedness and issues diagnostics.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003081 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Reid Spencer5f016e22007-07-11 17:01:13 +00003082}
3083
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003084/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
3085/// is parsed by the function passed to it. Pass null, and the direct-declarator
3086/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003087/// ptr-operator production.
3088///
Sebastian Redlf30208a2009-01-24 21:16:55 +00003089/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
3090/// [C] pointer[opt] direct-declarator
3091/// [C++] direct-declarator
3092/// [C++] ptr-operator declarator
Reid Spencer5f016e22007-07-11 17:01:13 +00003093///
3094/// pointer: [C99 6.7.5]
3095/// '*' type-qualifier-list[opt]
3096/// '*' type-qualifier-list[opt] pointer
3097///
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003098/// ptr-operator:
3099/// '*' cv-qualifier-seq[opt]
3100/// '&'
Sebastian Redl05532f22009-03-15 22:02:01 +00003101/// [C++0x] '&&'
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003102/// [GNU] '&' restrict[opt] attributes[opt]
Sebastian Redl05532f22009-03-15 22:02:01 +00003103/// [GNU?] '&&' restrict[opt] attributes[opt]
Sebastian Redlf30208a2009-01-24 21:16:55 +00003104/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003105void Parser::ParseDeclaratorInternal(Declarator &D,
3106 DirectDeclParseFunction DirectDeclParser) {
Douglas Gregor91a28862009-08-26 14:27:30 +00003107 if (Diags.hasAllExtensionsSilenced())
3108 D.setExtension();
Douglas Gregor2ccccb32010-08-23 18:23:48 +00003109
Sebastian Redlf30208a2009-01-24 21:16:55 +00003110 // C++ member pointers start with a '::' or a nested-name.
3111 // Member pointers get special handling, since there's no place for the
3112 // scope spec in the generic path below.
Chris Lattnerf919bfe2009-03-24 17:04:48 +00003113 if (getLang().CPlusPlus &&
3114 (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) ||
3115 Tok.is(tok::annot_cxxscope))) {
Sebastian Redlf30208a2009-01-24 21:16:55 +00003116 CXXScopeSpec SS;
John McCallb3d87482010-08-24 05:47:05 +00003117 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), true); // ignore fail
John McCall9ba61662010-02-26 08:45:28 +00003118
Jeffrey Yasskinedc28772010-04-07 23:29:58 +00003119 if (SS.isNotEmpty()) {
Mike Stump1eb44332009-09-09 15:08:12 +00003120 if (Tok.isNot(tok::star)) {
Sebastian Redlf30208a2009-01-24 21:16:55 +00003121 // The scope spec really belongs to the direct-declarator.
3122 D.getCXXScopeSpec() = SS;
3123 if (DirectDeclParser)
3124 (this->*DirectDeclParser)(D);
3125 return;
3126 }
3127
3128 SourceLocation Loc = ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00003129 D.SetRangeEnd(Loc);
John McCall0b7e6782011-03-24 11:26:52 +00003130 DeclSpec DS(AttrFactory);
Sebastian Redlf30208a2009-01-24 21:16:55 +00003131 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00003132 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00003133
3134 // Recurse to parse whatever is left.
3135 ParseDeclaratorInternal(D, DirectDeclParser);
3136
3137 // Sema will have to catch (syntactically invalid) pointers into global
3138 // scope. It has to catch pointers into namespace scope anyway.
3139 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
John McCall0b7e6782011-03-24 11:26:52 +00003140 Loc),
3141 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003142 /* Don't replace range end. */SourceLocation());
Sebastian Redlf30208a2009-01-24 21:16:55 +00003143 return;
3144 }
3145 }
3146
3147 tok::TokenKind Kind = Tok.getKind();
Steve Naroff5618bd42008-08-27 16:04:49 +00003148 // Not a pointer, C++ reference, or block.
Chris Lattner9af55002009-03-27 04:18:06 +00003149 if (Kind != tok::star && Kind != tok::caret &&
Chris Lattnerf919bfe2009-03-24 17:04:48 +00003150 (Kind != tok::amp || !getLang().CPlusPlus) &&
Sebastian Redl743de1f2009-03-23 00:00:23 +00003151 // We parse rvalue refs in C++03, because otherwise the errors are scary.
Chris Lattner9af55002009-03-27 04:18:06 +00003152 (Kind != tok::ampamp || !getLang().CPlusPlus)) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003153 if (DirectDeclParser)
3154 (this->*DirectDeclParser)(D);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003155 return;
3156 }
Sebastian Redlf30208a2009-01-24 21:16:55 +00003157
Sebastian Redl05532f22009-03-15 22:02:01 +00003158 // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference,
3159 // '&&' -> rvalue reference
Sebastian Redl743de1f2009-03-23 00:00:23 +00003160 SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&.
Sebastian Redlab197ba2009-02-09 18:23:29 +00003161 D.SetRangeEnd(Loc);
Reid Spencer5f016e22007-07-11 17:01:13 +00003162
Chris Lattner9af55002009-03-27 04:18:06 +00003163 if (Kind == tok::star || Kind == tok::caret) {
Chris Lattner76549142008-02-21 01:32:26 +00003164 // Is a pointer.
John McCall0b7e6782011-03-24 11:26:52 +00003165 DeclSpec DS(AttrFactory);
Sebastian Redlf30208a2009-01-24 21:16:55 +00003166
Reid Spencer5f016e22007-07-11 17:01:13 +00003167 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00003168 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00003169
Reid Spencer5f016e22007-07-11 17:01:13 +00003170 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003171 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroff5618bd42008-08-27 16:04:49 +00003172 if (Kind == tok::star)
3173 // Remember that we parsed a pointer type, and remember the type-quals.
3174 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
Chandler Carruthd067c072011-02-23 18:51:59 +00003175 DS.getConstSpecLoc(),
3176 DS.getVolatileSpecLoc(),
John McCall0b7e6782011-03-24 11:26:52 +00003177 DS.getRestrictSpecLoc()),
3178 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003179 SourceLocation());
Steve Naroff5618bd42008-08-27 16:04:49 +00003180 else
3181 // Remember that we parsed a Block type, and remember the type-quals.
Mike Stump1eb44332009-09-09 15:08:12 +00003182 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
John McCall0b7e6782011-03-24 11:26:52 +00003183 Loc),
3184 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003185 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00003186 } else {
3187 // Is a reference
John McCall0b7e6782011-03-24 11:26:52 +00003188 DeclSpec DS(AttrFactory);
Reid Spencer5f016e22007-07-11 17:01:13 +00003189
Sebastian Redl743de1f2009-03-23 00:00:23 +00003190 // Complain about rvalue references in C++03, but then go on and build
3191 // the declarator.
3192 if (Kind == tok::ampamp && !getLang().CPlusPlus0x)
Douglas Gregor16cf8f52011-01-25 02:17:32 +00003193 Diag(Loc, diag::ext_rvalue_reference);
Sebastian Redl743de1f2009-03-23 00:00:23 +00003194
Reid Spencer5f016e22007-07-11 17:01:13 +00003195 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
3196 // cv-qualifiers are introduced through the use of a typedef or of a
3197 // template type argument, in which case the cv-qualifiers are ignored.
3198 //
3199 // [GNU] Retricted references are allowed.
3200 // [GNU] Attributes on references are allowed.
Sean Huntbbd37c62009-11-21 08:43:09 +00003201 // [C++0x] Attributes on references are not allowed.
3202 ParseTypeQualifierListOpt(DS, true, false);
Sebastian Redlab197ba2009-02-09 18:23:29 +00003203 D.ExtendWithDeclSpec(DS);
Reid Spencer5f016e22007-07-11 17:01:13 +00003204
3205 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
3206 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
3207 Diag(DS.getConstSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00003208 diag::err_invalid_reference_qualifier_application) << "const";
Reid Spencer5f016e22007-07-11 17:01:13 +00003209 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
3210 Diag(DS.getVolatileSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00003211 diag::err_invalid_reference_qualifier_application) << "volatile";
Reid Spencer5f016e22007-07-11 17:01:13 +00003212 }
3213
3214 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003215 ParseDeclaratorInternal(D, DirectDeclParser);
Reid Spencer5f016e22007-07-11 17:01:13 +00003216
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00003217 if (D.getNumTypeObjects() > 0) {
3218 // C++ [dcl.ref]p4: There shall be no references to references.
3219 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
3220 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattnerda83bac2008-11-19 07:37:42 +00003221 if (const IdentifierInfo *II = D.getIdentifier())
3222 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
3223 << II;
3224 else
3225 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
3226 << "type name";
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00003227
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003228 // Once we've complained about the reference-to-reference, we
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00003229 // can go ahead and build the (technically ill-formed)
3230 // declarator: reference collapsing will take care of it.
3231 }
3232 }
3233
Reid Spencer5f016e22007-07-11 17:01:13 +00003234 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner76549142008-02-21 01:32:26 +00003235 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
Sebastian Redl05532f22009-03-15 22:02:01 +00003236 Kind == tok::amp),
John McCall0b7e6782011-03-24 11:26:52 +00003237 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003238 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00003239 }
3240}
3241
3242/// ParseDirectDeclarator
3243/// direct-declarator: [C99 6.7.5]
Douglas Gregor42a552f2008-11-05 20:51:48 +00003244/// [C99] identifier
Reid Spencer5f016e22007-07-11 17:01:13 +00003245/// '(' declarator ')'
3246/// [GNU] '(' attributes declarator ')'
3247/// [C90] direct-declarator '[' constant-expression[opt] ']'
3248/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
3249/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
3250/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
3251/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
3252/// direct-declarator '(' parameter-type-list ')'
3253/// direct-declarator '(' identifier-list[opt] ')'
3254/// [GNU] direct-declarator '(' parameter-forward-declarations
3255/// parameter-type-list[opt] ')'
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003256/// [C++] direct-declarator '(' parameter-declaration-clause ')'
3257/// cv-qualifier-seq[opt] exception-specification[opt]
Douglas Gregorb48fe382008-10-31 09:07:45 +00003258/// [C++] declarator-id
Douglas Gregor42a552f2008-11-05 20:51:48 +00003259///
3260/// declarator-id: [C++ 8]
Douglas Gregora8bc8c92010-12-23 22:44:42 +00003261/// '...'[opt] id-expression
Douglas Gregor42a552f2008-11-05 20:51:48 +00003262/// '::'[opt] nested-name-specifier[opt] type-name
3263///
3264/// id-expression: [C++ 5.1]
3265/// unqualified-id
Douglas Gregordb422df2009-09-25 21:45:23 +00003266/// qualified-id
Douglas Gregor42a552f2008-11-05 20:51:48 +00003267///
3268/// unqualified-id: [C++ 5.1]
Mike Stump1eb44332009-09-09 15:08:12 +00003269/// identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003270/// operator-function-id
Douglas Gregordb422df2009-09-25 21:45:23 +00003271/// conversion-function-id
Mike Stump1eb44332009-09-09 15:08:12 +00003272/// '~' class-name
Douglas Gregor39a8de12009-02-25 19:37:18 +00003273/// template-id
Argyrios Kyrtzidisc7ed9c62008-11-07 22:02:30 +00003274///
Reid Spencer5f016e22007-07-11 17:01:13 +00003275void Parser::ParseDirectDeclarator(Declarator &D) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00003276 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003277
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003278 if (getLang().CPlusPlus && D.mayHaveIdentifier()) {
3279 // ParseDeclaratorInternal might already have parsed the scope.
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00003280 if (D.getCXXScopeSpec().isEmpty()) {
John McCallb3d87482010-08-24 05:47:05 +00003281 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec(), ParsedType(), true);
John McCall9ba61662010-02-26 08:45:28 +00003282 }
3283
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00003284 if (D.getCXXScopeSpec().isValid()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00003285 if (Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
John McCalle7e278b2009-12-11 20:04:54 +00003286 // Change the declaration context for name lookup, until this function
3287 // is exited (and the declarator has been parsed).
3288 DeclScopeObj.EnterDeclaratorScope();
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00003289 }
3290
Douglas Gregora8bc8c92010-12-23 22:44:42 +00003291 // C++0x [dcl.fct]p14:
3292 // There is a syntactic ambiguity when an ellipsis occurs at the end
3293 // of a parameter-declaration-clause without a preceding comma. In
3294 // this case, the ellipsis is parsed as part of the
3295 // abstract-declarator if the type of the parameter names a template
3296 // parameter pack that has not been expanded; otherwise, it is parsed
3297 // as part of the parameter-declaration-clause.
3298 if (Tok.is(tok::ellipsis) &&
3299 !((D.getContext() == Declarator::PrototypeContext ||
3300 D.getContext() == Declarator::BlockLiteralContext) &&
Douglas Gregora8bc8c92010-12-23 22:44:42 +00003301 NextToken().is(tok::r_paren) &&
3302 !Actions.containsUnexpandedParameterPacks(D)))
3303 D.setEllipsisLoc(ConsumeToken());
3304
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003305 if (Tok.is(tok::identifier) || Tok.is(tok::kw_operator) ||
3306 Tok.is(tok::annot_template_id) || Tok.is(tok::tilde)) {
3307 // We found something that indicates the start of an unqualified-id.
3308 // Parse that unqualified-id.
John McCallba9d8532010-04-13 06:39:49 +00003309 bool AllowConstructorName;
3310 if (D.getDeclSpec().hasTypeSpecifier())
3311 AllowConstructorName = false;
3312 else if (D.getCXXScopeSpec().isSet())
3313 AllowConstructorName =
3314 (D.getContext() == Declarator::FileContext ||
3315 (D.getContext() == Declarator::MemberContext &&
3316 D.getDeclSpec().isFriendSpecified()));
3317 else
3318 AllowConstructorName = (D.getContext() == Declarator::MemberContext);
3319
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003320 if (ParseUnqualifiedId(D.getCXXScopeSpec(),
3321 /*EnteringContext=*/true,
3322 /*AllowDestructorName=*/true,
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003323 AllowConstructorName,
John McCallb3d87482010-08-24 05:47:05 +00003324 ParsedType(),
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00003325 D.getName()) ||
3326 // Once we're past the identifier, if the scope was bad, mark the
3327 // whole declarator bad.
3328 D.getCXXScopeSpec().isInvalid()) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00003329 D.SetIdentifier(0, Tok.getLocation());
3330 D.setInvalidType(true);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003331 } else {
3332 // Parsed the unqualified-id; update range information and move along.
3333 if (D.getSourceRange().getBegin().isInvalid())
3334 D.SetRangeBegin(D.getName().getSourceRange().getBegin());
3335 D.SetRangeEnd(D.getName().getSourceRange().getEnd());
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003336 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003337 goto PastIdentifier;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00003338 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003339 } else if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00003340 assert(!getLang().CPlusPlus &&
3341 "There's a C++-specific check for tok::identifier above");
3342 assert(Tok.getIdentifierInfo() && "Not an identifier?");
3343 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
3344 ConsumeToken();
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003345 goto PastIdentifier;
3346 }
3347
3348 if (Tok.is(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003349 // direct-declarator: '(' declarator ')'
3350 // direct-declarator: '(' attributes declarator ')'
3351 // Example: 'char (*X)' or 'int (*XX)(void)'
3352 ParseParenDeclarator(D);
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003353
3354 // If the declarator was parenthesized, we entered the declarator
3355 // scope when parsing the parenthesized declarator, then exited
3356 // the scope already. Re-enter the scope, if we need to.
3357 if (D.getCXXScopeSpec().isSet()) {
Fariborz Jahanian46877cd2010-08-17 23:50:37 +00003358 // If there was an error parsing parenthesized declarator, declarator
3359 // scope may have been enterred before. Don't do it again.
3360 if (!D.isInvalidType() &&
3361 Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003362 // Change the declaration context for name lookup, until this function
3363 // is exited (and the declarator has been parsed).
Fariborz Jahanian46877cd2010-08-17 23:50:37 +00003364 DeclScopeObj.EnterDeclaratorScope();
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003365 }
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00003366 } else if (D.mayOmitIdentifier()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003367 // This could be something simple like "int" (in which case the declarator
3368 // portion is empty), if an abstract-declarator is allowed.
3369 D.SetIdentifier(0, Tok.getLocation());
3370 } else {
Douglas Gregore950d4b2009-03-06 23:28:18 +00003371 if (D.getContext() == Declarator::MemberContext)
3372 Diag(Tok, diag::err_expected_member_name_or_semi)
3373 << D.getDeclSpec().getSourceRange();
3374 else if (getLang().CPlusPlus)
Douglas Gregor2d1c2142009-11-03 19:44:04 +00003375 Diag(Tok, diag::err_expected_unqualified_id) << getLang().CPlusPlus;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003376 else
Chris Lattner1ab3b962008-11-18 07:48:38 +00003377 Diag(Tok, diag::err_expected_ident_lparen);
Reid Spencer5f016e22007-07-11 17:01:13 +00003378 D.SetIdentifier(0, Tok.getLocation());
Chris Lattner1f6f54b2008-11-11 06:13:16 +00003379 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00003380 }
Mike Stump1eb44332009-09-09 15:08:12 +00003381
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00003382 PastIdentifier:
Reid Spencer5f016e22007-07-11 17:01:13 +00003383 assert(D.isPastIdentifier() &&
3384 "Haven't past the location of the identifier yet?");
Mike Stump1eb44332009-09-09 15:08:12 +00003385
Sean Huntbbd37c62009-11-21 08:43:09 +00003386 // Don't parse attributes unless we have an identifier.
John McCall7f040a92010-12-24 02:08:15 +00003387 if (D.getIdentifier())
3388 MaybeParseCXX0XAttributes(D);
Sean Huntbbd37c62009-11-21 08:43:09 +00003389
Reid Spencer5f016e22007-07-11 17:01:13 +00003390 while (1) {
Chris Lattner04d66662007-10-09 17:33:22 +00003391 if (Tok.is(tok::l_paren)) {
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00003392 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
3393 // In such a case, check if we actually have a function declarator; if it
3394 // is not, the declarator has been fully parsed.
Chris Lattner7399ee02008-10-20 02:05:46 +00003395 if (getLang().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
3396 // When not in file scope, warn for ambiguous function declarators, just
3397 // in case the author intended it as a variable definition.
3398 bool warnIfAmbiguous = D.getContext() != Declarator::FileContext;
3399 if (!isCXXFunctionDeclarator(warnIfAmbiguous))
3400 break;
3401 }
John McCall0b7e6782011-03-24 11:26:52 +00003402 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00003403 ParseFunctionDeclarator(ConsumeParen(), D, attrs);
Chris Lattner04d66662007-10-09 17:33:22 +00003404 } else if (Tok.is(tok::l_square)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003405 ParseBracketDeclarator(D);
3406 } else {
3407 break;
3408 }
3409 }
3410}
3411
Chris Lattneref4715c2008-04-06 05:45:57 +00003412/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
3413/// only called before the identifier, so these are most likely just grouping
Mike Stump1eb44332009-09-09 15:08:12 +00003414/// parens for precedence. If we find that these are actually function
Chris Lattneref4715c2008-04-06 05:45:57 +00003415/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
3416///
3417/// direct-declarator:
3418/// '(' declarator ')'
3419/// [GNU] '(' attributes declarator ')'
Chris Lattner7399ee02008-10-20 02:05:46 +00003420/// direct-declarator '(' parameter-type-list ')'
3421/// direct-declarator '(' identifier-list[opt] ')'
3422/// [GNU] direct-declarator '(' parameter-forward-declarations
3423/// parameter-type-list[opt] ')'
Chris Lattneref4715c2008-04-06 05:45:57 +00003424///
3425void Parser::ParseParenDeclarator(Declarator &D) {
3426 SourceLocation StartLoc = ConsumeParen();
3427 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
Mike Stump1eb44332009-09-09 15:08:12 +00003428
Chris Lattner7399ee02008-10-20 02:05:46 +00003429 // Eat any attributes before we look at whether this is a grouping or function
3430 // declarator paren. If this is a grouping paren, the attribute applies to
3431 // the type being built up, for example:
3432 // int (__attribute__(()) *x)(long y)
3433 // If this ends up not being a grouping paren, the attribute applies to the
3434 // first argument, for example:
3435 // int (__attribute__(()) int x)
3436 // In either case, we need to eat any attributes to be able to determine what
3437 // sort of paren this is.
3438 //
John McCall0b7e6782011-03-24 11:26:52 +00003439 ParsedAttributes attrs(AttrFactory);
Chris Lattner7399ee02008-10-20 02:05:46 +00003440 bool RequiresArg = false;
3441 if (Tok.is(tok::kw___attribute)) {
John McCall7f040a92010-12-24 02:08:15 +00003442 ParseGNUAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00003443
Chris Lattner7399ee02008-10-20 02:05:46 +00003444 // We require that the argument list (if this is a non-grouping paren) be
3445 // present even if the attribute list was empty.
3446 RequiresArg = true;
3447 }
Steve Naroff239f0732008-12-25 14:16:32 +00003448 // Eat any Microsoft extensions.
Eli Friedman290eeb02009-06-08 23:27:34 +00003449 if (Tok.is(tok::kw___cdecl) || Tok.is(tok::kw___stdcall) ||
Douglas Gregorf813a2c2010-05-18 16:57:00 +00003450 Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___fastcall) ||
3451 Tok.is(tok::kw___w64) || Tok.is(tok::kw___ptr64)) {
John McCall7f040a92010-12-24 02:08:15 +00003452 ParseMicrosoftTypeAttributes(attrs);
Eli Friedman290eeb02009-06-08 23:27:34 +00003453 }
Dawn Perchik52fc3142010-09-03 01:29:35 +00003454 // Eat any Borland extensions.
Ted Kremenek8113ecf2010-11-10 05:59:39 +00003455 if (Tok.is(tok::kw___pascal))
John McCall7f040a92010-12-24 02:08:15 +00003456 ParseBorlandTypeAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00003457
Chris Lattneref4715c2008-04-06 05:45:57 +00003458 // If we haven't past the identifier yet (or where the identifier would be
3459 // stored, if this is an abstract declarator), then this is probably just
3460 // grouping parens. However, if this could be an abstract-declarator, then
3461 // this could also be the start of function arguments (consider 'void()').
3462 bool isGrouping;
Mike Stump1eb44332009-09-09 15:08:12 +00003463
Chris Lattneref4715c2008-04-06 05:45:57 +00003464 if (!D.mayOmitIdentifier()) {
3465 // If this can't be an abstract-declarator, this *must* be a grouping
3466 // paren, because we haven't seen the identifier yet.
3467 isGrouping = true;
3468 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Argyrios Kyrtzidise25d2702008-10-06 00:07:55 +00003469 (getLang().CPlusPlus && Tok.is(tok::ellipsis)) || // C++ int(...)
Chris Lattneref4715c2008-04-06 05:45:57 +00003470 isDeclarationSpecifier()) { // 'int(int)' is a function.
3471 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
3472 // considered to be a type, not a K&R identifier-list.
3473 isGrouping = false;
3474 } else {
3475 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
3476 isGrouping = true;
3477 }
Mike Stump1eb44332009-09-09 15:08:12 +00003478
Chris Lattneref4715c2008-04-06 05:45:57 +00003479 // If this is a grouping paren, handle:
3480 // direct-declarator: '(' declarator ')'
3481 // direct-declarator: '(' attributes declarator ')'
3482 if (isGrouping) {
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00003483 bool hadGroupingParens = D.hasGroupingParens();
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00003484 D.setGroupingParens(true);
3485
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003486 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattneref4715c2008-04-06 05:45:57 +00003487 // Match the ')'.
Abramo Bagnara075f8f12010-12-10 16:29:40 +00003488 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_paren, StartLoc);
John McCall0b7e6782011-03-24 11:26:52 +00003489 D.AddTypeInfo(DeclaratorChunk::getParen(StartLoc, EndLoc),
3490 attrs, EndLoc);
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00003491
3492 D.setGroupingParens(hadGroupingParens);
Chris Lattneref4715c2008-04-06 05:45:57 +00003493 return;
3494 }
Mike Stump1eb44332009-09-09 15:08:12 +00003495
Chris Lattneref4715c2008-04-06 05:45:57 +00003496 // Okay, if this wasn't a grouping paren, it must be the start of a function
3497 // argument list. Recognize that this declarator will never have an
Chris Lattner7399ee02008-10-20 02:05:46 +00003498 // identifier (and remember where it would have been), then call into
3499 // ParseFunctionDeclarator to handle of argument list.
Chris Lattneref4715c2008-04-06 05:45:57 +00003500 D.SetIdentifier(0, Tok.getLocation());
3501
John McCall7f040a92010-12-24 02:08:15 +00003502 ParseFunctionDeclarator(StartLoc, D, attrs, RequiresArg);
Chris Lattneref4715c2008-04-06 05:45:57 +00003503}
3504
3505/// ParseFunctionDeclarator - We are after the identifier and have parsed the
3506/// declarator D up to a paren, which indicates that we are parsing function
3507/// arguments.
Reid Spencer5f016e22007-07-11 17:01:13 +00003508///
Chris Lattner7399ee02008-10-20 02:05:46 +00003509/// If AttrList is non-null, then the caller parsed those arguments immediately
3510/// after the open paren - they should be considered to be the first argument of
3511/// a parameter. If RequiresArg is true, then the first argument of the
3512/// function is required to be present and required to not be an identifier
3513/// list.
3514///
Reid Spencer5f016e22007-07-11 17:01:13 +00003515/// This method also handles this portion of the grammar:
3516/// parameter-type-list: [C99 6.7.5]
3517/// parameter-list
3518/// parameter-list ',' '...'
Douglas Gregored5d6512009-09-22 21:41:40 +00003519/// [C++] parameter-list '...'
Reid Spencer5f016e22007-07-11 17:01:13 +00003520///
3521/// parameter-list: [C99 6.7.5]
3522/// parameter-declaration
3523/// parameter-list ',' parameter-declaration
3524///
3525/// parameter-declaration: [C99 6.7.5]
3526/// declaration-specifiers declarator
Chris Lattner04421082008-04-08 04:40:51 +00003527/// [C++] declaration-specifiers declarator '=' assignment-expression
Reid Spencer5f016e22007-07-11 17:01:13 +00003528/// [GNU] declaration-specifiers declarator attributes
Sebastian Redl50de12f2009-03-24 22:27:57 +00003529/// declaration-specifiers abstract-declarator[opt]
3530/// [C++] declaration-specifiers abstract-declarator[opt]
Chris Lattner8123a952008-04-10 02:22:51 +00003531/// '=' assignment-expression
Reid Spencer5f016e22007-07-11 17:01:13 +00003532/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
3533///
Douglas Gregor83f51722011-01-26 03:43:54 +00003534/// For C++, after the parameter-list, it also parses "cv-qualifier-seq[opt]",
3535/// C++0x "ref-qualifier[opt]" and "exception-specification[opt]".
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003536///
Sebastian Redl7acafd02011-03-05 14:45:16 +00003537/// [C++0x] exception-specification:
3538/// dynamic-exception-specification
3539/// noexcept-specification
3540///
Chris Lattner7399ee02008-10-20 02:05:46 +00003541void Parser::ParseFunctionDeclarator(SourceLocation LParenLoc, Declarator &D,
John McCall7f040a92010-12-24 02:08:15 +00003542 ParsedAttributes &attrs,
Chris Lattner7399ee02008-10-20 02:05:46 +00003543 bool RequiresArg) {
Chris Lattneref4715c2008-04-06 05:45:57 +00003544 // lparen is already consumed!
3545 assert(D.isPastIdentifier() && "Should not call before identifier!");
Mike Stump1eb44332009-09-09 15:08:12 +00003546
Douglas Gregordab60ad2010-10-01 18:44:50 +00003547 ParsedType TrailingReturnType;
3548
Chris Lattner7399ee02008-10-20 02:05:46 +00003549 // This parameter list may be empty.
Chris Lattner04d66662007-10-09 17:33:22 +00003550 if (Tok.is(tok::r_paren)) {
Ted Kremenek8113ecf2010-11-10 05:59:39 +00003551 if (RequiresArg)
Chris Lattner1ab3b962008-11-18 07:48:38 +00003552 Diag(Tok, diag::err_argument_required_after_attribute);
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003553
Abramo Bagnara796aa442011-03-12 11:17:06 +00003554 SourceLocation EndLoc = ConsumeParen(); // Eat the closing ')'.
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003555
3556 // cv-qualifier-seq[opt].
John McCall0b7e6782011-03-24 11:26:52 +00003557 DeclSpec DS(AttrFactory);
Douglas Gregor83f51722011-01-26 03:43:54 +00003558 SourceLocation RefQualifierLoc;
3559 bool RefQualifierIsLValueRef = true;
Sebastian Redl7acafd02011-03-05 14:45:16 +00003560 ExceptionSpecificationType ESpecType = EST_None;
3561 SourceRange ESpecRange;
3562 llvm::SmallVector<ParsedType, 2> DynamicExceptions;
3563 llvm::SmallVector<SourceRange, 2> DynamicExceptionRanges;
3564 ExprResult NoexceptExpr;
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003565 if (getLang().CPlusPlus) {
John McCall7f040a92010-12-24 02:08:15 +00003566 MaybeParseCXX0XAttributes(attrs);
3567
Chris Lattner5a69d1c2008-12-18 07:02:59 +00003568 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Sebastian Redlab197ba2009-02-09 18:23:29 +00003569 if (!DS.getSourceRange().getEnd().isInvalid())
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00003570 EndLoc = DS.getSourceRange().getEnd();
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00003571
Douglas Gregor83f51722011-01-26 03:43:54 +00003572 // Parse ref-qualifier[opt]
3573 if (Tok.is(tok::amp) || Tok.is(tok::ampamp)) {
3574 if (!getLang().CPlusPlus0x)
Douglas Gregor1f381062011-01-26 20:35:32 +00003575 Diag(Tok, diag::ext_ref_qualifier);
Abramo Bagnara796aa442011-03-12 11:17:06 +00003576
Douglas Gregor83f51722011-01-26 03:43:54 +00003577 RefQualifierIsLValueRef = Tok.is(tok::amp);
3578 RefQualifierLoc = ConsumeToken();
3579 EndLoc = RefQualifierLoc;
3580 }
Sebastian Redl7acafd02011-03-05 14:45:16 +00003581
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00003582 // Parse exception-specification[opt].
Sebastian Redl7acafd02011-03-05 14:45:16 +00003583 ESpecType = MaybeParseExceptionSpecification(ESpecRange,
3584 DynamicExceptions,
3585 DynamicExceptionRanges,
3586 NoexceptExpr);
3587 if (ESpecType != EST_None)
3588 EndLoc = ESpecRange.getEnd();
Douglas Gregordab60ad2010-10-01 18:44:50 +00003589
3590 // Parse trailing-return-type.
3591 if (getLang().CPlusPlus0x && Tok.is(tok::arrow)) {
3592 TrailingReturnType = ParseTrailingReturnType().get();
3593 }
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003594 }
3595
Chris Lattnerf97409f2008-04-06 06:57:35 +00003596 // Remember that we parsed a function type, and remember the attributes.
Reid Spencer5f016e22007-07-11 17:01:13 +00003597 // int() -> no prototype, no '...'.
John McCall0b7e6782011-03-24 11:26:52 +00003598 D.AddTypeInfo(DeclaratorChunk::getFunction(/*prototype*/getLang().CPlusPlus,
Chris Lattnerf97409f2008-04-06 06:57:35 +00003599 /*variadic*/ false,
Douglas Gregor965acbb2009-02-18 07:07:28 +00003600 SourceLocation(),
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003601 /*arglist*/ 0, 0,
3602 DS.getTypeQualifiers(),
Douglas Gregor83f51722011-01-26 03:43:54 +00003603 RefQualifierIsLValueRef,
3604 RefQualifierLoc,
Sebastian Redl6e5d3192011-03-05 22:42:13 +00003605 ESpecType, ESpecRange.getBegin(),
Sebastian Redl7acafd02011-03-05 14:45:16 +00003606 DynamicExceptions.data(),
3607 DynamicExceptionRanges.data(),
3608 DynamicExceptions.size(),
Sebastian Redl6e5d3192011-03-05 22:42:13 +00003609 NoexceptExpr.isUsable() ?
3610 NoexceptExpr.get() : 0,
Abramo Bagnara796aa442011-03-12 11:17:06 +00003611 LParenLoc, EndLoc, D,
Douglas Gregordab60ad2010-10-01 18:44:50 +00003612 TrailingReturnType),
John McCall0b7e6782011-03-24 11:26:52 +00003613 attrs, EndLoc);
Chris Lattnerf97409f2008-04-06 06:57:35 +00003614 return;
Sebastian Redlef65f062009-05-29 18:02:33 +00003615 }
3616
Chris Lattner7399ee02008-10-20 02:05:46 +00003617 // Alternatively, this parameter list may be an identifier list form for a
3618 // K&R-style function: void foo(a,b,c)
John Thompson82287d12010-02-05 00:12:22 +00003619 if (!getLang().CPlusPlus && Tok.is(tok::identifier)
3620 && !TryAltiVecVectorToken()) {
John McCall9ba61662010-02-26 08:45:28 +00003621 if (TryAnnotateTypeOrScopeToken() || !Tok.is(tok::annot_typename)) {
Chris Lattner7399ee02008-10-20 02:05:46 +00003622 // K&R identifier lists can't have typedefs as identifiers, per
3623 // C99 6.7.5.3p11.
Ted Kremenek8113ecf2010-11-10 05:59:39 +00003624 if (RequiresArg)
Steve Naroff2d081c42009-01-28 19:16:40 +00003625 Diag(Tok, diag::err_argument_required_after_attribute);
Chris Lattner83a94472010-05-14 17:23:36 +00003626
Steve Naroff2d081c42009-01-28 19:16:40 +00003627 // Identifier list. Note that '(' identifier-list ')' is only allowed for
Chris Lattner83a94472010-05-14 17:23:36 +00003628 // normal declarators, not for abstract-declarators. Get the first
3629 // identifier.
Chris Lattner9a65b812010-05-14 17:44:56 +00003630 Token FirstTok = Tok;
Chris Lattner83a94472010-05-14 17:23:36 +00003631 ConsumeToken(); // eat the first identifier.
Chris Lattner9a65b812010-05-14 17:44:56 +00003632
3633 // Identifier lists follow a really simple grammar: the identifiers can
3634 // be followed *only* by a ", moreidentifiers" or ")". However, K&R
3635 // identifier lists are really rare in the brave new modern world, and it
3636 // is very common for someone to typo a type in a non-k&r style list. If
3637 // we are presented with something like: "void foo(intptr x, float y)",
3638 // we don't want to start parsing the function declarator as though it is
3639 // a K&R style declarator just because intptr is an invalid type.
3640 //
3641 // To handle this, we check to see if the token after the first identifier
3642 // is a "," or ")". Only if so, do we parse it as an identifier list.
3643 if (Tok.is(tok::comma) || Tok.is(tok::r_paren))
3644 return ParseFunctionDeclaratorIdentifierList(LParenLoc,
3645 FirstTok.getIdentifierInfo(),
3646 FirstTok.getLocation(), D);
3647
3648 // If we get here, the code is invalid. Push the first identifier back
3649 // into the token stream and parse the first argument as an (invalid)
3650 // normal argument declarator.
3651 PP.EnterToken(Tok);
3652 Tok = FirstTok;
Chris Lattner7399ee02008-10-20 02:05:46 +00003653 }
Chris Lattnerf97409f2008-04-06 06:57:35 +00003654 }
Mike Stump1eb44332009-09-09 15:08:12 +00003655
Chris Lattnerf97409f2008-04-06 06:57:35 +00003656 // Finally, a normal, non-empty parameter type list.
Mike Stump1eb44332009-09-09 15:08:12 +00003657
Chris Lattnerf97409f2008-04-06 06:57:35 +00003658 // Build up an array of information about the parsed arguments.
3659 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Chris Lattner04421082008-04-08 04:40:51 +00003660
3661 // Enter function-declaration scope, limiting any declarators to the
3662 // function prototype scope, including parameter declarators.
Chris Lattnerae50fa02009-03-05 00:00:31 +00003663 ParseScope PrototypeScope(this,
3664 Scope::FunctionPrototypeScope|Scope::DeclScope);
Mike Stump1eb44332009-09-09 15:08:12 +00003665
Chris Lattnerf97409f2008-04-06 06:57:35 +00003666 bool IsVariadic = false;
Douglas Gregor965acbb2009-02-18 07:07:28 +00003667 SourceLocation EllipsisLoc;
Chris Lattnerf97409f2008-04-06 06:57:35 +00003668 while (1) {
3669 if (Tok.is(tok::ellipsis)) {
3670 IsVariadic = true;
Douglas Gregor965acbb2009-02-18 07:07:28 +00003671 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chris Lattnerf97409f2008-04-06 06:57:35 +00003672 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00003673 }
Mike Stump1eb44332009-09-09 15:08:12 +00003674
Chris Lattnerf97409f2008-04-06 06:57:35 +00003675 // Parse the declaration-specifiers.
John McCall54abf7d2009-11-04 02:18:39 +00003676 // Just use the ParsingDeclaration "scope" of the declarator.
John McCall0b7e6782011-03-24 11:26:52 +00003677 DeclSpec DS(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00003678
3679 // Skip any Microsoft attributes before a param.
3680 if (getLang().Microsoft && Tok.is(tok::l_square))
3681 ParseMicrosoftAttributes(DS.getAttributes());
3682
3683 SourceLocation DSStart = Tok.getLocation();
Chris Lattner7399ee02008-10-20 02:05:46 +00003684
3685 // If the caller parsed attributes for the first argument, add them now.
John McCall7f040a92010-12-24 02:08:15 +00003686 // Take them so that we only apply the attributes to the first parameter.
3687 DS.takeAttributesFrom(attrs);
3688
Chris Lattnere64c5492009-02-27 18:38:20 +00003689 ParseDeclarationSpecifiers(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00003690
Chris Lattnerf97409f2008-04-06 06:57:35 +00003691 // Parse the declarator. This is "PrototypeContext", because we must
3692 // accept either 'declarator' or 'abstract-declarator' here.
3693 Declarator ParmDecl(DS, Declarator::PrototypeContext);
3694 ParseDeclarator(ParmDecl);
3695
3696 // Parse GNU attributes, if present.
John McCall7f040a92010-12-24 02:08:15 +00003697 MaybeParseGNUAttributes(ParmDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00003698
Chris Lattnerf97409f2008-04-06 06:57:35 +00003699 // Remember this parsed parameter in ParamInfo.
3700 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
Mike Stump1eb44332009-09-09 15:08:12 +00003701
Douglas Gregor72b505b2008-12-16 21:30:33 +00003702 // DefArgToks is used when the parsing of default arguments needs
3703 // to be delayed.
3704 CachedTokens *DefArgToks = 0;
3705
Chris Lattnerf97409f2008-04-06 06:57:35 +00003706 // If no parameter was specified, verify that *something* was specified,
3707 // otherwise we have a missing type and identifier.
Chris Lattnere64c5492009-02-27 18:38:20 +00003708 if (DS.isEmpty() && ParmDecl.getIdentifier() == 0 &&
3709 ParmDecl.getNumTypeObjects() == 0) {
Chris Lattnerf97409f2008-04-06 06:57:35 +00003710 // Completely missing, emit error.
3711 Diag(DSStart, diag::err_missing_param);
3712 } else {
3713 // Otherwise, we have something. Add it and let semantic analysis try
3714 // to grok it and add the result to the ParamInfo we are building.
Mike Stump1eb44332009-09-09 15:08:12 +00003715
Chris Lattnerf97409f2008-04-06 06:57:35 +00003716 // Inform the actions module about the parameter declarator, so it gets
3717 // added to the current scope.
John McCalld226f652010-08-21 09:40:31 +00003718 Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl);
Chris Lattner04421082008-04-08 04:40:51 +00003719
3720 // Parse the default argument, if any. We parse the default
3721 // arguments in all dialects; the semantic analysis in
3722 // ActOnParamDefaultArgument will reject the default argument in
3723 // C.
3724 if (Tok.is(tok::equal)) {
Douglas Gregor61366e92008-12-24 00:01:03 +00003725 SourceLocation EqualLoc = Tok.getLocation();
3726
Chris Lattner04421082008-04-08 04:40:51 +00003727 // Parse the default argument
Douglas Gregor72b505b2008-12-16 21:30:33 +00003728 if (D.getContext() == Declarator::MemberContext) {
3729 // If we're inside a class definition, cache the tokens
3730 // corresponding to the default argument. We'll actually parse
3731 // them when we see the end of the class definition.
3732 // FIXME: Templates will require something similar.
3733 // FIXME: Can we use a smart pointer for Toks?
3734 DefArgToks = new CachedTokens;
3735
Mike Stump1eb44332009-09-09 15:08:12 +00003736 if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks,
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +00003737 /*StopAtSemi=*/true,
3738 /*ConsumeFinalToken=*/false)) {
Douglas Gregor72b505b2008-12-16 21:30:33 +00003739 delete DefArgToks;
3740 DefArgToks = 0;
Douglas Gregor61366e92008-12-24 00:01:03 +00003741 Actions.ActOnParamDefaultArgumentError(Param);
Argyrios Kyrtzidis2b602ad2010-08-06 09:47:24 +00003742 } else {
3743 // Mark the end of the default argument so that we know when to
3744 // stop when we parse it later on.
3745 Token DefArgEnd;
3746 DefArgEnd.startToken();
3747 DefArgEnd.setKind(tok::cxx_defaultarg_end);
3748 DefArgEnd.setLocation(Tok.getLocation());
3749 DefArgToks->push_back(DefArgEnd);
Mike Stump1eb44332009-09-09 15:08:12 +00003750 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc,
Anders Carlsson5e300d12009-06-12 16:51:40 +00003751 (*DefArgToks)[1].getLocation());
Argyrios Kyrtzidis2b602ad2010-08-06 09:47:24 +00003752 }
Chris Lattner04421082008-04-08 04:40:51 +00003753 } else {
Douglas Gregor72b505b2008-12-16 21:30:33 +00003754 // Consume the '='.
Douglas Gregor61366e92008-12-24 00:01:03 +00003755 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00003756
Douglas Gregorbe0f7bd2010-09-11 20:24:53 +00003757 // The argument isn't actually potentially evaluated unless it is
3758 // used.
3759 EnterExpressionEvaluationContext Eval(Actions,
3760 Sema::PotentiallyEvaluatedIfUsed);
3761
John McCall60d7b3a2010-08-24 06:29:42 +00003762 ExprResult DefArgResult(ParseAssignmentExpression());
Douglas Gregor72b505b2008-12-16 21:30:33 +00003763 if (DefArgResult.isInvalid()) {
3764 Actions.ActOnParamDefaultArgumentError(Param);
3765 SkipUntil(tok::comma, tok::r_paren, true, true);
3766 } else {
3767 // Inform the actions module about the default argument
3768 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
John McCall9ae2f072010-08-23 23:25:46 +00003769 DefArgResult.take());
Douglas Gregor72b505b2008-12-16 21:30:33 +00003770 }
Chris Lattner04421082008-04-08 04:40:51 +00003771 }
3772 }
Mike Stump1eb44332009-09-09 15:08:12 +00003773
3774 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
3775 ParmDecl.getIdentifierLoc(), Param,
Douglas Gregor72b505b2008-12-16 21:30:33 +00003776 DefArgToks));
Chris Lattnerf97409f2008-04-06 06:57:35 +00003777 }
3778
3779 // If the next token is a comma, consume it and keep reading arguments.
Douglas Gregored5d6512009-09-22 21:41:40 +00003780 if (Tok.isNot(tok::comma)) {
3781 if (Tok.is(tok::ellipsis)) {
3782 IsVariadic = true;
3783 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
3784
3785 if (!getLang().CPlusPlus) {
3786 // We have ellipsis without a preceding ',', which is ill-formed
3787 // in C. Complain and provide the fix.
3788 Diag(EllipsisLoc, diag::err_missing_comma_before_ellipsis)
Douglas Gregor849b2432010-03-31 17:46:05 +00003789 << FixItHint::CreateInsertion(EllipsisLoc, ", ");
Douglas Gregored5d6512009-09-22 21:41:40 +00003790 }
3791 }
3792
3793 break;
3794 }
Mike Stump1eb44332009-09-09 15:08:12 +00003795
Chris Lattnerf97409f2008-04-06 06:57:35 +00003796 // Consume the comma.
3797 ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00003798 }
Mike Stump1eb44332009-09-09 15:08:12 +00003799
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003800 // If we have the closing ')', eat it.
Abramo Bagnara796aa442011-03-12 11:17:06 +00003801 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003802
John McCall0b7e6782011-03-24 11:26:52 +00003803 DeclSpec DS(AttrFactory);
Douglas Gregor83f51722011-01-26 03:43:54 +00003804 SourceLocation RefQualifierLoc;
3805 bool RefQualifierIsLValueRef = true;
Sebastian Redl7acafd02011-03-05 14:45:16 +00003806 ExceptionSpecificationType ESpecType = EST_None;
3807 SourceRange ESpecRange;
3808 llvm::SmallVector<ParsedType, 2> DynamicExceptions;
3809 llvm::SmallVector<SourceRange, 2> DynamicExceptionRanges;
3810 ExprResult NoexceptExpr;
Sean Huntbbd37c62009-11-21 08:43:09 +00003811
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003812 if (getLang().CPlusPlus) {
John McCall7f040a92010-12-24 02:08:15 +00003813 MaybeParseCXX0XAttributes(attrs);
3814
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00003815 // Parse cv-qualifier-seq[opt].
Chris Lattner5a69d1c2008-12-18 07:02:59 +00003816 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Sebastian Redlab197ba2009-02-09 18:23:29 +00003817 if (!DS.getSourceRange().getEnd().isInvalid())
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00003818 EndLoc = DS.getSourceRange().getEnd();
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00003819
Douglas Gregor83f51722011-01-26 03:43:54 +00003820 // Parse ref-qualifier[opt]
3821 if (Tok.is(tok::amp) || Tok.is(tok::ampamp)) {
3822 if (!getLang().CPlusPlus0x)
Douglas Gregor1f381062011-01-26 20:35:32 +00003823 Diag(Tok, diag::ext_ref_qualifier);
Douglas Gregor83f51722011-01-26 03:43:54 +00003824
3825 RefQualifierIsLValueRef = Tok.is(tok::amp);
3826 RefQualifierLoc = ConsumeToken();
3827 EndLoc = RefQualifierLoc;
3828 }
3829
Sebastian Redl7acafd02011-03-05 14:45:16 +00003830 // FIXME: We should leave the prototype scope before parsing the exception
3831 // specification, and then reenter it when parsing the trailing return type.
3832 // FIXMEFIXME: Why? That wouldn't be right for the noexcept clause.
3833
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00003834 // Parse exception-specification[opt].
Sebastian Redl7acafd02011-03-05 14:45:16 +00003835 ESpecType = MaybeParseExceptionSpecification(ESpecRange,
3836 DynamicExceptions,
3837 DynamicExceptionRanges,
3838 NoexceptExpr);
3839 if (ESpecType != EST_None)
3840 EndLoc = ESpecRange.getEnd();
Douglas Gregordab60ad2010-10-01 18:44:50 +00003841
3842 // Parse trailing-return-type.
3843 if (getLang().CPlusPlus0x && Tok.is(tok::arrow)) {
3844 TrailingReturnType = ParseTrailingReturnType().get();
3845 }
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003846 }
3847
Douglas Gregordab60ad2010-10-01 18:44:50 +00003848 // Leave prototype scope.
3849 PrototypeScope.Exit();
3850
Reid Spencer5f016e22007-07-11 17:01:13 +00003851 // Remember that we parsed a function type, and remember the attributes.
John McCall0b7e6782011-03-24 11:26:52 +00003852 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/true, IsVariadic,
Douglas Gregor965acbb2009-02-18 07:07:28 +00003853 EllipsisLoc,
Jay Foadbeaaccd2009-05-21 09:52:38 +00003854 ParamInfo.data(), ParamInfo.size(),
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003855 DS.getTypeQualifiers(),
Douglas Gregor83f51722011-01-26 03:43:54 +00003856 RefQualifierIsLValueRef,
3857 RefQualifierLoc,
Sebastian Redl6e5d3192011-03-05 22:42:13 +00003858 ESpecType, ESpecRange.getBegin(),
Sebastian Redl7acafd02011-03-05 14:45:16 +00003859 DynamicExceptions.data(),
3860 DynamicExceptionRanges.data(),
3861 DynamicExceptions.size(),
Sebastian Redl6e5d3192011-03-05 22:42:13 +00003862 NoexceptExpr.isUsable() ?
3863 NoexceptExpr.get() : 0,
Abramo Bagnara796aa442011-03-12 11:17:06 +00003864 LParenLoc, EndLoc, D,
Douglas Gregordab60ad2010-10-01 18:44:50 +00003865 TrailingReturnType),
John McCall0b7e6782011-03-24 11:26:52 +00003866 attrs, EndLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00003867}
3868
Chris Lattner66d28652008-04-06 06:34:08 +00003869/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
3870/// we found a K&R-style identifier list instead of a type argument list. The
Chris Lattner83a94472010-05-14 17:23:36 +00003871/// first identifier has already been consumed, and the current token is the
3872/// token right after it.
Chris Lattner66d28652008-04-06 06:34:08 +00003873///
3874/// identifier-list: [C99 6.7.5]
3875/// identifier
3876/// identifier-list ',' identifier
3877///
3878void Parser::ParseFunctionDeclaratorIdentifierList(SourceLocation LParenLoc,
Chris Lattner83a94472010-05-14 17:23:36 +00003879 IdentifierInfo *FirstIdent,
3880 SourceLocation FirstIdentLoc,
Chris Lattner66d28652008-04-06 06:34:08 +00003881 Declarator &D) {
3882 // Build up an array of information about the parsed arguments.
3883 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
3884 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
Mike Stump1eb44332009-09-09 15:08:12 +00003885
Chris Lattner66d28652008-04-06 06:34:08 +00003886 // If there was no identifier specified for the declarator, either we are in
3887 // an abstract-declarator, or we are in a parameter declarator which was found
3888 // to be abstract. In abstract-declarators, identifier lists are not valid:
3889 // diagnose this.
3890 if (!D.getIdentifier())
Chris Lattner83a94472010-05-14 17:23:36 +00003891 Diag(FirstIdentLoc, diag::ext_ident_list_in_param);
Chris Lattner66d28652008-04-06 06:34:08 +00003892
Chris Lattner83a94472010-05-14 17:23:36 +00003893 // The first identifier was already read, and is known to be the first
3894 // identifier in the list. Remember this identifier in ParamInfo.
3895 ParamsSoFar.insert(FirstIdent);
John McCalld226f652010-08-21 09:40:31 +00003896 ParamInfo.push_back(DeclaratorChunk::ParamInfo(FirstIdent, FirstIdentLoc, 0));
Mike Stump1eb44332009-09-09 15:08:12 +00003897
Chris Lattner66d28652008-04-06 06:34:08 +00003898 while (Tok.is(tok::comma)) {
3899 // Eat the comma.
3900 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00003901
Chris Lattner50c64772008-04-06 06:39:19 +00003902 // If this isn't an identifier, report the error and skip until ')'.
Chris Lattner66d28652008-04-06 06:34:08 +00003903 if (Tok.isNot(tok::identifier)) {
3904 Diag(Tok, diag::err_expected_ident);
Chris Lattner50c64772008-04-06 06:39:19 +00003905 SkipUntil(tok::r_paren);
3906 return;
Chris Lattner66d28652008-04-06 06:34:08 +00003907 }
Chris Lattneraaf9ddb2008-04-06 06:47:48 +00003908
Chris Lattner66d28652008-04-06 06:34:08 +00003909 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
Chris Lattneraaf9ddb2008-04-06 06:47:48 +00003910
3911 // Reject 'typedef int y; int test(x, y)', but continue parsing.
Douglas Gregor23c94db2010-07-02 17:43:08 +00003912 if (Actions.getTypeName(*ParmII, Tok.getLocation(), getCurScope()))
Chris Lattnerda83bac2008-11-19 07:37:42 +00003913 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
Mike Stump1eb44332009-09-09 15:08:12 +00003914
Chris Lattner66d28652008-04-06 06:34:08 +00003915 // Verify that the argument identifier has not already been mentioned.
3916 if (!ParamsSoFar.insert(ParmII)) {
Chris Lattnerda83bac2008-11-19 07:37:42 +00003917 Diag(Tok, diag::err_param_redefinition) << ParmII;
Chris Lattner50c64772008-04-06 06:39:19 +00003918 } else {
3919 // Remember this identifier in ParamInfo.
Chris Lattner66d28652008-04-06 06:34:08 +00003920 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Chris Lattnerb28317a2009-03-28 19:18:32 +00003921 Tok.getLocation(),
John McCalld226f652010-08-21 09:40:31 +00003922 0));
Chris Lattner50c64772008-04-06 06:39:19 +00003923 }
Mike Stump1eb44332009-09-09 15:08:12 +00003924
Chris Lattner66d28652008-04-06 06:34:08 +00003925 // Eat the identifier.
3926 ConsumeToken();
3927 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00003928
3929 // If we have the closing ')', eat it and we're done.
3930 SourceLocation RLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
3931
Chris Lattner50c64772008-04-06 06:39:19 +00003932 // Remember that we parsed a function type, and remember the attributes. This
3933 // function type is always a K&R style function type, which is not varargs and
3934 // has no prototype.
John McCall0b7e6782011-03-24 11:26:52 +00003935 ParsedAttributes attrs(AttrFactory);
3936 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/false, /*varargs*/false,
Douglas Gregor965acbb2009-02-18 07:07:28 +00003937 SourceLocation(),
Chris Lattner50c64772008-04-06 06:39:19 +00003938 &ParamInfo[0], ParamInfo.size(),
Sebastian Redl7dc81342009-04-29 17:30:04 +00003939 /*TypeQuals*/0,
Douglas Gregor83f51722011-01-26 03:43:54 +00003940 true, SourceLocation(),
Sebastian Redl6e5d3192011-03-05 22:42:13 +00003941 EST_None, SourceLocation(), 0, 0,
3942 0, 0, LParenLoc, RLoc, D),
John McCall0b7e6782011-03-24 11:26:52 +00003943 attrs, RLoc);
Chris Lattner66d28652008-04-06 06:34:08 +00003944}
Chris Lattneref4715c2008-04-06 05:45:57 +00003945
Reid Spencer5f016e22007-07-11 17:01:13 +00003946/// [C90] direct-declarator '[' constant-expression[opt] ']'
3947/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
3948/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
3949/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
3950/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
3951void Parser::ParseBracketDeclarator(Declarator &D) {
3952 SourceLocation StartLoc = ConsumeBracket();
Mike Stump1eb44332009-09-09 15:08:12 +00003953
Chris Lattner378c7e42008-12-18 07:27:21 +00003954 // C array syntax has many features, but by-far the most common is [] and [4].
3955 // This code does a fast path to handle some of the most obvious cases.
3956 if (Tok.getKind() == tok::r_square) {
Sebastian Redlab197ba2009-02-09 18:23:29 +00003957 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
John McCall0b7e6782011-03-24 11:26:52 +00003958 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00003959 MaybeParseCXX0XAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00003960
Chris Lattner378c7e42008-12-18 07:27:21 +00003961 // Remember that we parsed the empty array type.
John McCall60d7b3a2010-08-24 06:29:42 +00003962 ExprResult NumElements;
John McCall0b7e6782011-03-24 11:26:52 +00003963 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00003964 StartLoc, EndLoc),
John McCall0b7e6782011-03-24 11:26:52 +00003965 attrs, EndLoc);
Chris Lattner378c7e42008-12-18 07:27:21 +00003966 return;
3967 } else if (Tok.getKind() == tok::numeric_constant &&
3968 GetLookAheadToken(1).is(tok::r_square)) {
3969 // [4] is very common. Parse the numeric constant expression.
John McCall60d7b3a2010-08-24 06:29:42 +00003970 ExprResult ExprRes(Actions.ActOnNumericConstant(Tok));
Chris Lattner378c7e42008-12-18 07:27:21 +00003971 ConsumeToken();
3972
Sebastian Redlab197ba2009-02-09 18:23:29 +00003973 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
John McCall0b7e6782011-03-24 11:26:52 +00003974 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00003975 MaybeParseCXX0XAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00003976
Chris Lattner378c7e42008-12-18 07:27:21 +00003977 // Remember that we parsed a array type, and remember its features.
John McCall0b7e6782011-03-24 11:26:52 +00003978 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, 0,
John McCall7f040a92010-12-24 02:08:15 +00003979 ExprRes.release(),
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00003980 StartLoc, EndLoc),
John McCall0b7e6782011-03-24 11:26:52 +00003981 attrs, EndLoc);
Chris Lattner378c7e42008-12-18 07:27:21 +00003982 return;
3983 }
Mike Stump1eb44332009-09-09 15:08:12 +00003984
Reid Spencer5f016e22007-07-11 17:01:13 +00003985 // If valid, this location is the position where we read the 'static' keyword.
3986 SourceLocation StaticLoc;
Chris Lattner04d66662007-10-09 17:33:22 +00003987 if (Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00003988 StaticLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00003989
Reid Spencer5f016e22007-07-11 17:01:13 +00003990 // If there is a type-qualifier-list, read it now.
Chris Lattnera1fcbad2008-12-18 06:50:14 +00003991 // Type qualifiers in an array subscript are a C99 feature.
John McCall0b7e6782011-03-24 11:26:52 +00003992 DeclSpec DS(AttrFactory);
Chris Lattner5a69d1c2008-12-18 07:02:59 +00003993 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Mike Stump1eb44332009-09-09 15:08:12 +00003994
Reid Spencer5f016e22007-07-11 17:01:13 +00003995 // If we haven't already read 'static', check to see if there is one after the
3996 // type-qualifier-list.
Chris Lattner04d66662007-10-09 17:33:22 +00003997 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00003998 StaticLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00003999
Reid Spencer5f016e22007-07-11 17:01:13 +00004000 // Handle "direct-declarator [ type-qual-list[opt] * ]".
4001 bool isStar = false;
John McCall60d7b3a2010-08-24 06:29:42 +00004002 ExprResult NumElements;
Mike Stump1eb44332009-09-09 15:08:12 +00004003
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00004004 // Handle the case where we have '[*]' as the array size. However, a leading
4005 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
4006 // the the token after the star is a ']'. Since stars in arrays are
4007 // infrequent, use of lookahead is not costly here.
4008 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattnera711dd02008-04-06 05:27:21 +00004009 ConsumeToken(); // Eat the '*'.
Reid Spencer5f016e22007-07-11 17:01:13 +00004010
Chris Lattnera1fcbad2008-12-18 06:50:14 +00004011 if (StaticLoc.isValid()) {
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00004012 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattnera1fcbad2008-12-18 06:50:14 +00004013 StaticLoc = SourceLocation(); // Drop the static.
4014 }
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00004015 isStar = true;
Chris Lattner04d66662007-10-09 17:33:22 +00004016 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner378c7e42008-12-18 07:27:21 +00004017 // Note, in C89, this production uses the constant-expr production instead
4018 // of assignment-expr. The only difference is that assignment-expr allows
4019 // things like '=' and '*='. Sema rejects these in C89 mode because they
4020 // are not i-c-e's, so we don't need to distinguish between the two here.
Mike Stump1eb44332009-09-09 15:08:12 +00004021
Douglas Gregore0762c92009-06-19 23:52:42 +00004022 // Parse the constant-expression or assignment-expression now (depending
4023 // on dialect).
4024 if (getLang().CPlusPlus)
4025 NumElements = ParseConstantExpression();
4026 else
4027 NumElements = ParseAssignmentExpression();
Reid Spencer5f016e22007-07-11 17:01:13 +00004028 }
Mike Stump1eb44332009-09-09 15:08:12 +00004029
Reid Spencer5f016e22007-07-11 17:01:13 +00004030 // If there was an error parsing the assignment-expression, recover.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00004031 if (NumElements.isInvalid()) {
Chris Lattner5cb10d32009-04-24 22:30:50 +00004032 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00004033 // If the expression was invalid, skip it.
4034 SkipUntil(tok::r_square);
4035 return;
4036 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00004037
4038 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
4039
John McCall0b7e6782011-03-24 11:26:52 +00004040 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00004041 MaybeParseCXX0XAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00004042
Chris Lattner378c7e42008-12-18 07:27:21 +00004043 // Remember that we parsed a array type, and remember its features.
John McCall0b7e6782011-03-24 11:26:52 +00004044 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
Reid Spencer5f016e22007-07-11 17:01:13 +00004045 StaticLoc.isValid(), isStar,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00004046 NumElements.release(),
4047 StartLoc, EndLoc),
John McCall0b7e6782011-03-24 11:26:52 +00004048 attrs, EndLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00004049}
4050
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004051/// [GNU] typeof-specifier:
4052/// typeof ( expressions )
4053/// typeof ( type-name )
4054/// [GNU/C++] typeof unary-expression
Steve Naroffd1861fd2007-07-31 12:34:36 +00004055///
4056void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner04d66662007-10-09 17:33:22 +00004057 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004058 Token OpTok = Tok;
Steve Naroffd1861fd2007-07-31 12:34:36 +00004059 SourceLocation StartLoc = ConsumeToken();
4060
John McCallcfb708c2010-01-13 20:03:27 +00004061 const bool hasParens = Tok.is(tok::l_paren);
4062
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004063 bool isCastExpr;
John McCallb3d87482010-08-24 05:47:05 +00004064 ParsedType CastTy;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004065 SourceRange CastRange;
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00004066 ExprResult Operand = ParseExprAfterUnaryExprOrTypeTrait(OpTok, isCastExpr,
4067 CastTy, CastRange);
John McCallcfb708c2010-01-13 20:03:27 +00004068 if (hasParens)
4069 DS.setTypeofParensRange(CastRange);
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004070
4071 if (CastRange.getEnd().isInvalid())
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004072 // FIXME: Not accurate, the range gets one token more than it should.
4073 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004074 else
4075 DS.SetRangeEnd(CastRange.getEnd());
Mike Stump1eb44332009-09-09 15:08:12 +00004076
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004077 if (isCastExpr) {
4078 if (!CastTy) {
4079 DS.SetTypeSpecError();
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004080 return;
Douglas Gregor809070a2009-02-18 17:45:20 +00004081 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004082
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004083 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00004084 unsigned DiagID;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004085 // Check for duplicate type specifiers (e.g. "int typeof(int)").
4086 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00004087 DiagID, CastTy))
4088 Diag(StartLoc, DiagID) << PrevSpec;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004089 return;
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004090 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004091
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004092 // If we get here, the operand to the typeof was an expresion.
4093 if (Operand.isInvalid()) {
4094 DS.SetTypeSpecError();
Steve Naroff9dfa7b42007-08-02 02:53:48 +00004095 return;
Steve Naroffd1861fd2007-07-31 12:34:36 +00004096 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004097
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004098 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00004099 unsigned DiagID;
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004100 // Check for duplicate type specifiers (e.g. "int typeof(int)").
4101 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
John McCallb3d87482010-08-24 05:47:05 +00004102 DiagID, Operand.get()))
John McCallfec54012009-08-03 20:12:06 +00004103 Diag(StartLoc, DiagID) << PrevSpec;
Steve Naroffd1861fd2007-07-31 12:34:36 +00004104}
Chris Lattner1b492422010-02-28 18:33:55 +00004105
4106
4107/// TryAltiVecVectorTokenOutOfLine - Out of line body that should only be called
4108/// from TryAltiVecVectorToken.
4109bool Parser::TryAltiVecVectorTokenOutOfLine() {
4110 Token Next = NextToken();
4111 switch (Next.getKind()) {
4112 default: return false;
4113 case tok::kw_short:
4114 case tok::kw_long:
4115 case tok::kw_signed:
4116 case tok::kw_unsigned:
4117 case tok::kw_void:
4118 case tok::kw_char:
4119 case tok::kw_int:
4120 case tok::kw_float:
4121 case tok::kw_double:
4122 case tok::kw_bool:
4123 case tok::kw___pixel:
4124 Tok.setKind(tok::kw___vector);
4125 return true;
4126 case tok::identifier:
4127 if (Next.getIdentifierInfo() == Ident_pixel) {
4128 Tok.setKind(tok::kw___vector);
4129 return true;
4130 }
4131 return false;
4132 }
4133}
4134
4135bool Parser::TryAltiVecTokenOutOfLine(DeclSpec &DS, SourceLocation Loc,
4136 const char *&PrevSpec, unsigned &DiagID,
4137 bool &isInvalid) {
4138 if (Tok.getIdentifierInfo() == Ident_vector) {
4139 Token Next = NextToken();
4140 switch (Next.getKind()) {
4141 case tok::kw_short:
4142 case tok::kw_long:
4143 case tok::kw_signed:
4144 case tok::kw_unsigned:
4145 case tok::kw_void:
4146 case tok::kw_char:
4147 case tok::kw_int:
4148 case tok::kw_float:
4149 case tok::kw_double:
4150 case tok::kw_bool:
4151 case tok::kw___pixel:
4152 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
4153 return true;
4154 case tok::identifier:
4155 if (Next.getIdentifierInfo() == Ident_pixel) {
4156 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
4157 return true;
4158 }
4159 break;
4160 default:
4161 break;
4162 }
Douglas Gregora8f031f2010-06-16 15:28:57 +00004163 } else if ((Tok.getIdentifierInfo() == Ident_pixel) &&
Chris Lattner1b492422010-02-28 18:33:55 +00004164 DS.isTypeAltiVecVector()) {
4165 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
4166 return true;
4167 }
4168 return false;
4169}