blob: ee76cc73a525ae80ad6d3c379f26fdbaf6a313fe [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
Peter Collingbournec6eb44b2011-04-15 00:35:57 +0000656/// [C++0x/C1X] 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:
Peter Collingbournec6eb44b2011-04-15 00:35:57 +0000691 case tok::kw__Static_assert:
John McCall7f040a92010-12-24 02:08:15 +0000692 ProhibitAttributes(attrs);
Chris Lattner97144fc2009-04-02 04:16:50 +0000693 SingleDecl = ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +0000694 break;
Chris Lattner8f08cb72007-08-25 06:57:03 +0000695 default:
John McCall7f040a92010-12-24 02:08:15 +0000696 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs, true);
Chris Lattner8f08cb72007-08-25 06:57:03 +0000697 }
Sean Huntbbd37c62009-11-21 08:43:09 +0000698
Chris Lattner682bf922009-03-29 16:50:03 +0000699 // This routine returns a DeclGroup, if the thing we parsed only contains a
700 // single decl, convert it now.
701 return Actions.ConvertDeclToDeclGroup(SingleDecl);
Chris Lattner8f08cb72007-08-25 06:57:03 +0000702}
703
704/// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl]
705/// declaration-specifiers init-declarator-list[opt] ';'
706///[C90/C++]init-declarator-list ';' [TODO]
707/// [OMP] threadprivate-directive [TODO]
Chris Lattnercd147752009-03-29 17:27:48 +0000708///
Richard Smithad762fc2011-04-14 22:09:26 +0000709/// for-range-declaration: [C++0x 6.5p1: stmt.ranged]
710/// attribute-specifier-seq[opt] type-specifier-seq declarator
711///
Chris Lattnercd147752009-03-29 17:27:48 +0000712/// If RequireSemi is false, this does not check for a ';' at the end of the
Chris Lattner5c5db552010-04-05 18:18:31 +0000713/// declaration. If it is true, it checks for and eats it.
Richard Smithad762fc2011-04-14 22:09:26 +0000714///
715/// If FRI is non-null, we might be parsing a for-range-declaration instead
716/// of a simple-declaration. If we find that we are, we also parse the
717/// for-range-initializer, and place it here.
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +0000718Parser::DeclGroupPtrTy Parser::ParseSimpleDeclaration(StmtVector &Stmts,
719 unsigned Context,
Sean Huntbbd37c62009-11-21 08:43:09 +0000720 SourceLocation &DeclEnd,
John McCall7f040a92010-12-24 02:08:15 +0000721 ParsedAttributes &attrs,
Richard Smithad762fc2011-04-14 22:09:26 +0000722 bool RequireSemi,
723 ForRangeInit *FRI) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000724 // Parse the common declaration-specifiers piece.
John McCall54abf7d2009-11-04 02:18:39 +0000725 ParsingDeclSpec DS(*this);
John McCall7f040a92010-12-24 02:08:15 +0000726 DS.takeAttributesFrom(attrs);
Douglas Gregor0efc2c12010-01-13 17:31:36 +0000727 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS_none,
Richard Smith34b41d92011-02-20 03:19:35 +0000728 getDeclSpecContextFromDeclaratorContext(Context));
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +0000729 StmtResult R = Actions.ActOnVlaStmt(DS);
730 if (R.isUsable())
731 Stmts.push_back(R.release());
Mike Stump1eb44332009-09-09 15:08:12 +0000732
Reid Spencer5f016e22007-07-11 17:01:13 +0000733 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
734 // declaration-specifiers init-declarator-list[opt] ';'
Chris Lattner04d66662007-10-09 17:33:22 +0000735 if (Tok.is(tok::semi)) {
Chris Lattner5c5db552010-04-05 18:18:31 +0000736 if (RequireSemi) ConsumeToken();
John McCalld226f652010-08-21 09:40:31 +0000737 Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none,
John McCallaec03712010-05-21 20:45:30 +0000738 DS);
John McCall54abf7d2009-11-04 02:18:39 +0000739 DS.complete(TheDecl);
Chris Lattner682bf922009-03-29 16:50:03 +0000740 return Actions.ConvertDeclToDeclGroup(TheDecl);
Reid Spencer5f016e22007-07-11 17:01:13 +0000741 }
Mike Stump1eb44332009-09-09 15:08:12 +0000742
Richard Smithad762fc2011-04-14 22:09:26 +0000743 return ParseDeclGroup(DS, Context, /*FunctionDefs=*/ false, &DeclEnd, FRI);
John McCalld8ac0572009-11-03 19:26:08 +0000744}
Mike Stump1eb44332009-09-09 15:08:12 +0000745
John McCalld8ac0572009-11-03 19:26:08 +0000746/// ParseDeclGroup - Having concluded that this is either a function
747/// definition or a group of object declarations, actually parse the
748/// result.
John McCall54abf7d2009-11-04 02:18:39 +0000749Parser::DeclGroupPtrTy Parser::ParseDeclGroup(ParsingDeclSpec &DS,
750 unsigned Context,
John McCalld8ac0572009-11-03 19:26:08 +0000751 bool AllowFunctionDefinitions,
Richard Smithad762fc2011-04-14 22:09:26 +0000752 SourceLocation *DeclEnd,
753 ForRangeInit *FRI) {
John McCalld8ac0572009-11-03 19:26:08 +0000754 // Parse the first declarator.
John McCall54abf7d2009-11-04 02:18:39 +0000755 ParsingDeclarator D(*this, DS, static_cast<Declarator::TheContext>(Context));
John McCalld8ac0572009-11-03 19:26:08 +0000756 ParseDeclarator(D);
Chris Lattnercd147752009-03-29 17:27:48 +0000757
John McCalld8ac0572009-11-03 19:26:08 +0000758 // Bail out if the first declarator didn't seem well-formed.
759 if (!D.hasName() && !D.mayOmitIdentifier()) {
760 // Skip until ; or }.
761 SkipUntil(tok::r_brace, true, true);
762 if (Tok.is(tok::semi))
763 ConsumeToken();
764 return DeclGroupPtrTy();
Chris Lattner23c4b182009-03-29 17:18:04 +0000765 }
Mike Stump1eb44332009-09-09 15:08:12 +0000766
Chris Lattnerc82daef2010-07-11 22:24:20 +0000767 // Check to see if we have a function *definition* which must have a body.
768 if (AllowFunctionDefinitions && D.isFunctionDeclarator() &&
769 // Look at the next token to make sure that this isn't a function
770 // declaration. We have to check this because __attribute__ might be the
771 // start of a function definition in GCC-extended K&R C.
772 !isDeclarationAfterDeclarator()) {
773
Chris Lattner004659a2010-07-11 22:42:07 +0000774 if (isStartOfFunctionDefinition(D)) {
John McCalld8ac0572009-11-03 19:26:08 +0000775 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
776 Diag(Tok, diag::err_function_declared_typedef);
777
778 // Recover by treating the 'typedef' as spurious.
779 DS.ClearStorageClassSpecs();
780 }
781
John McCalld226f652010-08-21 09:40:31 +0000782 Decl *TheDecl = ParseFunctionDefinition(D);
John McCalld8ac0572009-11-03 19:26:08 +0000783 return Actions.ConvertDeclToDeclGroup(TheDecl);
Chris Lattner004659a2010-07-11 22:42:07 +0000784 }
785
786 if (isDeclarationSpecifier()) {
787 // If there is an invalid declaration specifier right after the function
788 // prototype, then we must be in a missing semicolon case where this isn't
789 // actually a body. Just fall through into the code that handles it as a
790 // prototype, and let the top-level code handle the erroneous declspec
791 // where it would otherwise expect a comma or semicolon.
John McCalld8ac0572009-11-03 19:26:08 +0000792 } else {
793 Diag(Tok, diag::err_expected_fn_body);
794 SkipUntil(tok::semi);
795 return DeclGroupPtrTy();
796 }
797 }
798
Richard Smithad762fc2011-04-14 22:09:26 +0000799 if (ParseAttributesAfterDeclarator(D))
800 return DeclGroupPtrTy();
801
802 // C++0x [stmt.iter]p1: Check if we have a for-range-declarator. If so, we
803 // must parse and analyze the for-range-initializer before the declaration is
804 // analyzed.
805 if (FRI && Tok.is(tok::colon)) {
806 FRI->ColonLoc = ConsumeToken();
807 // FIXME: handle braced-init-list here.
808 FRI->RangeExpr = ParseExpression();
809 Decl *ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
810 Actions.ActOnCXXForRangeDecl(ThisDecl);
811 Actions.FinalizeDeclaration(ThisDecl);
812 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS, &ThisDecl, 1);
813 }
814
John McCalld226f652010-08-21 09:40:31 +0000815 llvm::SmallVector<Decl *, 8> DeclsInGroup;
Richard Smithad762fc2011-04-14 22:09:26 +0000816 Decl *FirstDecl = ParseDeclarationAfterDeclaratorAndAttributes(D);
John McCall54abf7d2009-11-04 02:18:39 +0000817 D.complete(FirstDecl);
John McCalld226f652010-08-21 09:40:31 +0000818 if (FirstDecl)
John McCalld8ac0572009-11-03 19:26:08 +0000819 DeclsInGroup.push_back(FirstDecl);
820
821 // If we don't have a comma, it is either the end of the list (a ';') or an
822 // error, bail out.
823 while (Tok.is(tok::comma)) {
824 // Consume the comma.
Chris Lattner23c4b182009-03-29 17:18:04 +0000825 ConsumeToken();
John McCalld8ac0572009-11-03 19:26:08 +0000826
827 // Parse the next declarator.
828 D.clear();
829
830 // Accept attributes in an init-declarator. In the first declarator in a
831 // declaration, these would be part of the declspec. In subsequent
832 // declarators, they become part of the declarator itself, so that they
833 // don't apply to declarators after *this* one. Examples:
834 // short __attribute__((common)) var; -> declspec
835 // short var __attribute__((common)); -> declarator
836 // short x, __attribute__((common)) var; -> declarator
John McCall7f040a92010-12-24 02:08:15 +0000837 MaybeParseGNUAttributes(D);
John McCalld8ac0572009-11-03 19:26:08 +0000838
839 ParseDeclarator(D);
840
John McCalld226f652010-08-21 09:40:31 +0000841 Decl *ThisDecl = ParseDeclarationAfterDeclarator(D);
John McCall54abf7d2009-11-04 02:18:39 +0000842 D.complete(ThisDecl);
John McCalld226f652010-08-21 09:40:31 +0000843 if (ThisDecl)
John McCalld8ac0572009-11-03 19:26:08 +0000844 DeclsInGroup.push_back(ThisDecl);
845 }
846
847 if (DeclEnd)
848 *DeclEnd = Tok.getLocation();
849
850 if (Context != Declarator::ForContext &&
851 ExpectAndConsume(tok::semi,
852 Context == Declarator::FileContext
853 ? diag::err_invalid_token_after_toplevel_declarator
854 : diag::err_expected_semi_declaration)) {
Chris Lattner004659a2010-07-11 22:42:07 +0000855 // Okay, there was no semicolon and one was expected. If we see a
856 // declaration specifier, just assume it was missing and continue parsing.
857 // Otherwise things are very confused and we skip to recover.
858 if (!isDeclarationSpecifier()) {
859 SkipUntil(tok::r_brace, true, true);
860 if (Tok.is(tok::semi))
861 ConsumeToken();
862 }
John McCalld8ac0572009-11-03 19:26:08 +0000863 }
864
Douglas Gregor23c94db2010-07-02 17:43:08 +0000865 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS,
John McCalld8ac0572009-11-03 19:26:08 +0000866 DeclsInGroup.data(),
867 DeclsInGroup.size());
Reid Spencer5f016e22007-07-11 17:01:13 +0000868}
869
Richard Smithad762fc2011-04-14 22:09:26 +0000870/// Parse an optional simple-asm-expr and attributes, and attach them to a
871/// declarator. Returns true on an error.
872bool Parser::ParseAttributesAfterDeclarator(Declarator &D) {
873 // If a simple-asm-expr is present, parse it.
874 if (Tok.is(tok::kw_asm)) {
875 SourceLocation Loc;
876 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
877 if (AsmLabel.isInvalid()) {
878 SkipUntil(tok::semi, true, true);
879 return true;
880 }
881
882 D.setAsmLabel(AsmLabel.release());
883 D.SetRangeEnd(Loc);
884 }
885
886 MaybeParseGNUAttributes(D);
887 return false;
888}
889
Douglas Gregor1426e532009-05-12 21:31:51 +0000890/// \brief Parse 'declaration' after parsing 'declaration-specifiers
891/// declarator'. This method parses the remainder of the declaration
892/// (including any attributes or initializer, among other things) and
893/// finalizes the declaration.
Reid Spencer5f016e22007-07-11 17:01:13 +0000894///
Reid Spencer5f016e22007-07-11 17:01:13 +0000895/// init-declarator: [C99 6.7]
896/// declarator
897/// declarator '=' initializer
898/// [GNU] declarator simple-asm-expr[opt] attributes[opt]
899/// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +0000900/// [C++] declarator initializer[opt]
901///
902/// [C++] initializer:
903/// [C++] '=' initializer-clause
904/// [C++] '(' expression-list ')'
Sebastian Redl50de12f2009-03-24 22:27:57 +0000905/// [C++0x] '=' 'default' [TODO]
906/// [C++0x] '=' 'delete'
907///
908/// According to the standard grammar, =default and =delete are function
909/// definitions, but that definitely doesn't fit with the parser here.
Reid Spencer5f016e22007-07-11 17:01:13 +0000910///
John McCalld226f652010-08-21 09:40:31 +0000911Decl *Parser::ParseDeclarationAfterDeclarator(Declarator &D,
Douglas Gregore542c862009-06-23 23:11:28 +0000912 const ParsedTemplateInfo &TemplateInfo) {
Richard Smithad762fc2011-04-14 22:09:26 +0000913 if (ParseAttributesAfterDeclarator(D))
914 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000915
Richard Smithad762fc2011-04-14 22:09:26 +0000916 return ParseDeclarationAfterDeclaratorAndAttributes(D, TemplateInfo);
917}
Mike Stump1eb44332009-09-09 15:08:12 +0000918
Richard Smithad762fc2011-04-14 22:09:26 +0000919Decl *Parser::ParseDeclarationAfterDeclaratorAndAttributes(Declarator &D,
920 const ParsedTemplateInfo &TemplateInfo) {
Douglas Gregor1426e532009-05-12 21:31:51 +0000921 // Inform the current actions module that we just parsed this declarator.
John McCalld226f652010-08-21 09:40:31 +0000922 Decl *ThisDecl = 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +0000923 switch (TemplateInfo.Kind) {
924 case ParsedTemplateInfo::NonTemplate:
Douglas Gregor23c94db2010-07-02 17:43:08 +0000925 ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
Douglas Gregord5a423b2009-09-25 18:43:00 +0000926 break;
927
928 case ParsedTemplateInfo::Template:
929 case ParsedTemplateInfo::ExplicitSpecialization:
Douglas Gregor23c94db2010-07-02 17:43:08 +0000930 ThisDecl = Actions.ActOnTemplateDeclarator(getCurScope(),
John McCallf312b1e2010-08-26 23:41:50 +0000931 MultiTemplateParamsArg(Actions,
Douglas Gregore542c862009-06-23 23:11:28 +0000932 TemplateInfo.TemplateParams->data(),
933 TemplateInfo.TemplateParams->size()),
Douglas Gregord5a423b2009-09-25 18:43:00 +0000934 D);
935 break;
936
937 case ParsedTemplateInfo::ExplicitInstantiation: {
John McCalld226f652010-08-21 09:40:31 +0000938 DeclResult ThisRes
Douglas Gregor23c94db2010-07-02 17:43:08 +0000939 = Actions.ActOnExplicitInstantiation(getCurScope(),
Douglas Gregord5a423b2009-09-25 18:43:00 +0000940 TemplateInfo.ExternLoc,
941 TemplateInfo.TemplateLoc,
942 D);
943 if (ThisRes.isInvalid()) {
944 SkipUntil(tok::semi, true, true);
John McCalld226f652010-08-21 09:40:31 +0000945 return 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +0000946 }
947
948 ThisDecl = ThisRes.get();
949 break;
950 }
951 }
Mike Stump1eb44332009-09-09 15:08:12 +0000952
Richard Smith34b41d92011-02-20 03:19:35 +0000953 bool TypeContainsAuto =
954 D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto;
955
Douglas Gregor1426e532009-05-12 21:31:51 +0000956 // Parse declarator '=' initializer.
Argyrios Kyrtzidisa6eb5f82010-10-08 02:39:23 +0000957 if (isTokenEqualOrMistypedEqualEqual(
958 diag::err_invalid_equalequal_after_declarator)) {
Douglas Gregor1426e532009-05-12 21:31:51 +0000959 ConsumeToken();
Anders Carlsson37bf9d22010-09-24 21:25:25 +0000960 if (Tok.is(tok::kw_delete)) {
Douglas Gregor1426e532009-05-12 21:31:51 +0000961 SourceLocation DelLoc = ConsumeToken();
Anders Carlsson37bf9d22010-09-24 21:25:25 +0000962
963 if (!getLang().CPlusPlus0x)
964 Diag(DelLoc, diag::warn_deleted_function_accepted_as_extension);
965
Douglas Gregor1426e532009-05-12 21:31:51 +0000966 Actions.SetDeclDeleted(ThisDecl, DelLoc);
967 } else {
John McCall731ad842009-12-19 09:28:58 +0000968 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
969 EnterScope(0);
Douglas Gregor23c94db2010-07-02 17:43:08 +0000970 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
John McCall731ad842009-12-19 09:28:58 +0000971 }
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +0000972
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +0000973 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000974 Actions.CodeCompleteInitializer(getCurScope(), ThisDecl);
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +0000975 ConsumeCodeCompletionToken();
976 SkipUntil(tok::comma, true, true);
977 return ThisDecl;
978 }
979
John McCall60d7b3a2010-08-24 06:29:42 +0000980 ExprResult Init(ParseInitializer());
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +0000981
John McCall731ad842009-12-19 09:28:58 +0000982 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000983 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
John McCall731ad842009-12-19 09:28:58 +0000984 ExitScope();
985 }
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +0000986
Douglas Gregor1426e532009-05-12 21:31:51 +0000987 if (Init.isInvalid()) {
Douglas Gregor00225542010-03-01 18:27:54 +0000988 SkipUntil(tok::comma, true, true);
989 Actions.ActOnInitializerError(ThisDecl);
990 } else
Richard Smith34b41d92011-02-20 03:19:35 +0000991 Actions.AddInitializerToDecl(ThisDecl, Init.take(),
992 /*DirectInit=*/false, TypeContainsAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +0000993 }
994 } else if (Tok.is(tok::l_paren)) {
995 // Parse C++ direct initializer: '(' expression-list ')'
996 SourceLocation LParenLoc = ConsumeParen();
997 ExprVector Exprs(Actions);
998 CommaLocsTy CommaLocs;
999
Douglas Gregorb4debae2009-12-22 17:47:17 +00001000 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
1001 EnterScope(0);
Douglas Gregor23c94db2010-07-02 17:43:08 +00001002 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001003 }
1004
Douglas Gregor1426e532009-05-12 21:31:51 +00001005 if (ParseExpressionList(Exprs, CommaLocs)) {
1006 SkipUntil(tok::r_paren);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001007
1008 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001009 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001010 ExitScope();
1011 }
Douglas Gregor1426e532009-05-12 21:31:51 +00001012 } else {
1013 // Match the ')'.
1014 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
1015
1016 assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() &&
1017 "Unexpected number of commas!");
Douglas Gregorb4debae2009-12-22 17:47:17 +00001018
1019 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001020 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001021 ExitScope();
1022 }
1023
Douglas Gregor1426e532009-05-12 21:31:51 +00001024 Actions.AddCXXDirectInitializerToDecl(ThisDecl, LParenLoc,
1025 move_arg(Exprs),
Richard Smith34b41d92011-02-20 03:19:35 +00001026 RParenLoc,
1027 TypeContainsAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +00001028 }
1029 } else {
Richard Smith34b41d92011-02-20 03:19:35 +00001030 Actions.ActOnUninitializedDecl(ThisDecl, TypeContainsAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +00001031 }
1032
Richard Smith483b9f32011-02-21 20:05:19 +00001033 Actions.FinalizeDeclaration(ThisDecl);
1034
Douglas Gregor1426e532009-05-12 21:31:51 +00001035 return ThisDecl;
1036}
1037
Reid Spencer5f016e22007-07-11 17:01:13 +00001038/// ParseSpecifierQualifierList
1039/// specifier-qualifier-list:
1040/// type-specifier specifier-qualifier-list[opt]
1041/// type-qualifier specifier-qualifier-list[opt]
1042/// [GNU] attributes specifier-qualifier-list[opt]
1043///
1044void Parser::ParseSpecifierQualifierList(DeclSpec &DS) {
1045 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
1046 /// parse declaration-specifiers and complain about extra stuff.
Reid Spencer5f016e22007-07-11 17:01:13 +00001047 ParseDeclarationSpecifiers(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00001048
Reid Spencer5f016e22007-07-11 17:01:13 +00001049 // Validate declspec for type-name.
1050 unsigned Specs = DS.getParsedSpecifiers();
Chris Lattnerb6645dd2009-04-14 21:16:09 +00001051 if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers() &&
John McCall7f040a92010-12-24 02:08:15 +00001052 !DS.hasAttributes())
Reid Spencer5f016e22007-07-11 17:01:13 +00001053 Diag(Tok, diag::err_typename_requires_specqual);
Mike Stump1eb44332009-09-09 15:08:12 +00001054
Reid Spencer5f016e22007-07-11 17:01:13 +00001055 // Issue diagnostic and remove storage class if present.
1056 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
1057 if (DS.getStorageClassSpecLoc().isValid())
1058 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
1059 else
1060 Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass);
1061 DS.ClearStorageClassSpecs();
1062 }
Mike Stump1eb44332009-09-09 15:08:12 +00001063
Reid Spencer5f016e22007-07-11 17:01:13 +00001064 // Issue diagnostic and remove function specfier if present.
1065 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Douglas Gregorb48fe382008-10-31 09:07:45 +00001066 if (DS.isInlineSpecified())
1067 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
1068 if (DS.isVirtualSpecified())
1069 Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec);
1070 if (DS.isExplicitSpecified())
1071 Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec);
Reid Spencer5f016e22007-07-11 17:01:13 +00001072 DS.ClearFunctionSpecs();
1073 }
1074}
1075
Chris Lattnerc199ab32009-04-12 20:42:31 +00001076/// isValidAfterIdentifierInDeclaratorAfterDeclSpec - Return true if the
1077/// specified token is valid after the identifier in a declarator which
1078/// immediately follows the declspec. For example, these things are valid:
1079///
1080/// int x [ 4]; // direct-declarator
1081/// int x ( int y); // direct-declarator
1082/// int(int x ) // direct-declarator
1083/// int x ; // simple-declaration
1084/// int x = 17; // init-declarator-list
1085/// int x , y; // init-declarator-list
1086/// int x __asm__ ("foo"); // init-declarator-list
Chris Lattnerb6645dd2009-04-14 21:16:09 +00001087/// int x : 4; // struct-declarator
Chris Lattnerc83c27a2009-04-12 22:29:43 +00001088/// int x { 5}; // C++'0x unified initializers
Chris Lattnerc199ab32009-04-12 20:42:31 +00001089///
1090/// This is not, because 'x' does not immediately follow the declspec (though
1091/// ')' happens to be valid anyway).
1092/// int (x)
1093///
1094static bool isValidAfterIdentifierInDeclarator(const Token &T) {
1095 return T.is(tok::l_square) || T.is(tok::l_paren) || T.is(tok::r_paren) ||
1096 T.is(tok::semi) || T.is(tok::comma) || T.is(tok::equal) ||
Chris Lattnerb6645dd2009-04-14 21:16:09 +00001097 T.is(tok::kw_asm) || T.is(tok::l_brace) || T.is(tok::colon);
Chris Lattnerc199ab32009-04-12 20:42:31 +00001098}
1099
Chris Lattnere40c2952009-04-14 21:34:55 +00001100
1101/// ParseImplicitInt - This method is called when we have an non-typename
1102/// identifier in a declspec (which normally terminates the decl spec) when
1103/// the declspec has no type specifier. In this case, the declspec is either
1104/// malformed or is "implicit int" (in K&R and C89).
1105///
1106/// This method handles diagnosing this prettily and returns false if the
1107/// declspec is done being processed. If it recovers and thinks there may be
1108/// other pieces of declspec after it, it returns true.
1109///
Chris Lattnerf4382f52009-04-14 22:17:06 +00001110bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001111 const ParsedTemplateInfo &TemplateInfo,
Chris Lattnere40c2952009-04-14 21:34:55 +00001112 AccessSpecifier AS) {
Chris Lattnerf4382f52009-04-14 22:17:06 +00001113 assert(Tok.is(tok::identifier) && "should have identifier");
Mike Stump1eb44332009-09-09 15:08:12 +00001114
Chris Lattnere40c2952009-04-14 21:34:55 +00001115 SourceLocation Loc = Tok.getLocation();
1116 // If we see an identifier that is not a type name, we normally would
1117 // parse it as the identifer being declared. However, when a typename
1118 // is typo'd or the definition is not included, this will incorrectly
1119 // parse the typename as the identifier name and fall over misparsing
1120 // later parts of the diagnostic.
1121 //
1122 // As such, we try to do some look-ahead in cases where this would
1123 // otherwise be an "implicit-int" case to see if this is invalid. For
1124 // example: "static foo_t x = 4;" In this case, if we parsed foo_t as
1125 // an identifier with implicit int, we'd get a parse error because the
1126 // next token is obviously invalid for a type. Parse these as a case
1127 // with an invalid type specifier.
1128 assert(!DS.hasTypeSpecifier() && "Type specifier checked above");
Mike Stump1eb44332009-09-09 15:08:12 +00001129
Chris Lattnere40c2952009-04-14 21:34:55 +00001130 // Since we know that this either implicit int (which is rare) or an
1131 // error, we'd do lookahead to try to do better recovery.
1132 if (isValidAfterIdentifierInDeclarator(NextToken())) {
1133 // If this token is valid for implicit int, e.g. "static x = 4", then
1134 // we just avoid eating the identifier, so it will be parsed as the
1135 // identifier in the declarator.
1136 return false;
1137 }
Mike Stump1eb44332009-09-09 15:08:12 +00001138
Chris Lattnere40c2952009-04-14 21:34:55 +00001139 // Otherwise, if we don't consume this token, we are going to emit an
1140 // error anyway. Try to recover from various common problems. Check
1141 // to see if this was a reference to a tag name without a tag specified.
1142 // This is a common problem in C (saying 'foo' instead of 'struct foo').
Chris Lattnerf4382f52009-04-14 22:17:06 +00001143 //
1144 // C++ doesn't need this, and isTagName doesn't take SS.
1145 if (SS == 0) {
1146 const char *TagName = 0;
1147 tok::TokenKind TagKind = tok::unknown;
Mike Stump1eb44332009-09-09 15:08:12 +00001148
Douglas Gregor23c94db2010-07-02 17:43:08 +00001149 switch (Actions.isTagName(*Tok.getIdentifierInfo(), getCurScope())) {
Chris Lattnere40c2952009-04-14 21:34:55 +00001150 default: break;
1151 case DeclSpec::TST_enum: TagName="enum" ;TagKind=tok::kw_enum ;break;
1152 case DeclSpec::TST_union: TagName="union" ;TagKind=tok::kw_union ;break;
1153 case DeclSpec::TST_struct:TagName="struct";TagKind=tok::kw_struct;break;
1154 case DeclSpec::TST_class: TagName="class" ;TagKind=tok::kw_class ;break;
1155 }
Mike Stump1eb44332009-09-09 15:08:12 +00001156
Chris Lattnerf4382f52009-04-14 22:17:06 +00001157 if (TagName) {
1158 Diag(Loc, diag::err_use_of_tag_name_without_tag)
John McCall23e907a2010-02-14 01:03:10 +00001159 << Tok.getIdentifierInfo() << TagName << getLang().CPlusPlus
Douglas Gregor849b2432010-03-31 17:46:05 +00001160 << FixItHint::CreateInsertion(Tok.getLocation(),TagName);
Mike Stump1eb44332009-09-09 15:08:12 +00001161
Chris Lattnerf4382f52009-04-14 22:17:06 +00001162 // Parse this as a tag as if the missing tag were present.
1163 if (TagKind == tok::kw_enum)
Douglas Gregor9b9edd62010-03-02 17:53:14 +00001164 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS);
Chris Lattnerf4382f52009-04-14 22:17:06 +00001165 else
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001166 ParseClassSpecifier(TagKind, Loc, DS, TemplateInfo, AS);
Chris Lattnerf4382f52009-04-14 22:17:06 +00001167 return true;
1168 }
Chris Lattnere40c2952009-04-14 21:34:55 +00001169 }
Mike Stump1eb44332009-09-09 15:08:12 +00001170
Douglas Gregora786fdb2009-10-13 23:27:22 +00001171 // This is almost certainly an invalid type name. Let the action emit a
1172 // diagnostic and attempt to recover.
John McCallb3d87482010-08-24 05:47:05 +00001173 ParsedType T;
Douglas Gregora786fdb2009-10-13 23:27:22 +00001174 if (Actions.DiagnoseUnknownTypeName(*Tok.getIdentifierInfo(), Loc,
Douglas Gregor23c94db2010-07-02 17:43:08 +00001175 getCurScope(), SS, T)) {
Douglas Gregora786fdb2009-10-13 23:27:22 +00001176 // The action emitted a diagnostic, so we don't have to.
1177 if (T) {
1178 // The action has suggested that the type T could be used. Set that as
1179 // the type in the declaration specifiers, consume the would-be type
1180 // name token, and we're done.
1181 const char *PrevSpec;
1182 unsigned DiagID;
John McCallb3d87482010-08-24 05:47:05 +00001183 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID, T);
Douglas Gregora786fdb2009-10-13 23:27:22 +00001184 DS.SetRangeEnd(Tok.getLocation());
1185 ConsumeToken();
1186
1187 // There may be other declaration specifiers after this.
1188 return true;
1189 }
1190
1191 // Fall through; the action had no suggestion for us.
1192 } else {
1193 // The action did not emit a diagnostic, so emit one now.
1194 SourceRange R;
1195 if (SS) R = SS->getRange();
1196 Diag(Loc, diag::err_unknown_typename) << Tok.getIdentifierInfo() << R;
1197 }
Mike Stump1eb44332009-09-09 15:08:12 +00001198
Douglas Gregora786fdb2009-10-13 23:27:22 +00001199 // Mark this as an error.
Chris Lattnere40c2952009-04-14 21:34:55 +00001200 const char *PrevSpec;
John McCallfec54012009-08-03 20:12:06 +00001201 unsigned DiagID;
1202 DS.SetTypeSpecType(DeclSpec::TST_error, Loc, PrevSpec, DiagID);
Chris Lattnere40c2952009-04-14 21:34:55 +00001203 DS.SetRangeEnd(Tok.getLocation());
1204 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001205
Chris Lattnere40c2952009-04-14 21:34:55 +00001206 // TODO: Could inject an invalid typedef decl in an enclosing scope to
1207 // avoid rippling error messages on subsequent uses of the same type,
1208 // could be useful if #include was forgotten.
1209 return false;
1210}
1211
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001212/// \brief Determine the declaration specifier context from the declarator
1213/// context.
1214///
1215/// \param Context the declarator context, which is one of the
1216/// Declarator::TheContext enumerator values.
1217Parser::DeclSpecContext
1218Parser::getDeclSpecContextFromDeclaratorContext(unsigned Context) {
1219 if (Context == Declarator::MemberContext)
1220 return DSC_class;
1221 if (Context == Declarator::FileContext)
1222 return DSC_top_level;
1223 return DSC_normal;
1224}
1225
Reid Spencer5f016e22007-07-11 17:01:13 +00001226/// ParseDeclarationSpecifiers
1227/// declaration-specifiers: [C99 6.7]
1228/// storage-class-specifier declaration-specifiers[opt]
1229/// type-specifier declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00001230/// [C99] function-specifier declaration-specifiers[opt]
1231/// [GNU] attributes declaration-specifiers[opt]
1232///
1233/// storage-class-specifier: [C99 6.7.1]
1234/// 'typedef'
1235/// 'extern'
1236/// 'static'
1237/// 'auto'
1238/// 'register'
Sebastian Redl669d5d72008-11-14 23:42:31 +00001239/// [C++] 'mutable'
Reid Spencer5f016e22007-07-11 17:01:13 +00001240/// [GNU] '__thread'
Reid Spencer5f016e22007-07-11 17:01:13 +00001241/// function-specifier: [C99 6.7.4]
1242/// [C99] 'inline'
Douglas Gregorb48fe382008-10-31 09:07:45 +00001243/// [C++] 'virtual'
1244/// [C++] 'explicit'
Peter Collingbournef315fa82011-02-14 01:42:53 +00001245/// [OpenCL] '__kernel'
Anders Carlssonf47f7a12009-05-06 04:46:28 +00001246/// 'friend': [C++ dcl.friend]
Sebastian Redl2ac67232009-11-05 15:47:02 +00001247/// 'constexpr': [C++0x dcl.constexpr]
Anders Carlssonf47f7a12009-05-06 04:46:28 +00001248
Reid Spencer5f016e22007-07-11 17:01:13 +00001249///
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +00001250void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001251 const ParsedTemplateInfo &TemplateInfo,
John McCall67d1a672009-08-06 02:15:43 +00001252 AccessSpecifier AS,
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001253 DeclSpecContext DSContext) {
Chris Lattner81c018d2008-03-13 06:29:04 +00001254 DS.SetRangeStart(Tok.getLocation());
Chris Lattner729ad832010-11-09 20:14:26 +00001255 DS.SetRangeEnd(Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00001256 while (1) {
John McCallfec54012009-08-03 20:12:06 +00001257 bool isInvalid = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001258 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00001259 unsigned DiagID = 0;
1260
Reid Spencer5f016e22007-07-11 17:01:13 +00001261 SourceLocation Loc = Tok.getLocation();
Douglas Gregor12e083c2008-11-07 15:42:26 +00001262
Reid Spencer5f016e22007-07-11 17:01:13 +00001263 switch (Tok.getKind()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001264 default:
Chris Lattnerbce61352008-07-26 00:20:22 +00001265 DoneWithDeclSpec:
Reid Spencer5f016e22007-07-11 17:01:13 +00001266 // If this is not a declaration specifier token, we're done reading decl
1267 // specifiers. First verify that DeclSpec's are consistent.
Douglas Gregor9b3064b2009-04-01 22:41:11 +00001268 DS.Finish(Diags, PP);
Reid Spencer5f016e22007-07-11 17:01:13 +00001269 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001270
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001271 case tok::code_completion: {
John McCallf312b1e2010-08-26 23:41:50 +00001272 Sema::ParserCompletionContext CCC = Sema::PCC_Namespace;
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001273 if (DS.hasTypeSpecifier()) {
1274 bool AllowNonIdentifiers
1275 = (getCurScope()->getFlags() & (Scope::ControlScope |
1276 Scope::BlockScope |
1277 Scope::TemplateParamScope |
1278 Scope::FunctionPrototypeScope |
1279 Scope::AtCatchScope)) == 0;
1280 bool AllowNestedNameSpecifiers
1281 = DSContext == DSC_top_level ||
1282 (DSContext == DSC_class && DS.isFriendSpecified());
1283
Douglas Gregorc7b6d882010-09-16 15:14:18 +00001284 Actions.CodeCompleteDeclSpec(getCurScope(), DS,
1285 AllowNonIdentifiers,
1286 AllowNestedNameSpecifiers);
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001287 ConsumeCodeCompletionToken();
1288 return;
1289 }
1290
Douglas Gregor68e3c2e2011-02-15 20:33:25 +00001291 if (getCurScope()->getFnParent() || getCurScope()->getBlockParent())
1292 CCC = Sema::PCC_LocalDeclarationSpecifiers;
1293 else if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate)
John McCallf312b1e2010-08-26 23:41:50 +00001294 CCC = DSContext == DSC_class? Sema::PCC_MemberTemplate
1295 : Sema::PCC_Template;
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001296 else if (DSContext == DSC_class)
John McCallf312b1e2010-08-26 23:41:50 +00001297 CCC = Sema::PCC_Class;
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001298 else if (ObjCImpDecl)
John McCallf312b1e2010-08-26 23:41:50 +00001299 CCC = Sema::PCC_ObjCImplementation;
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001300
1301 Actions.CodeCompleteOrdinaryName(getCurScope(), CCC);
1302 ConsumeCodeCompletionToken();
1303 return;
1304 }
1305
Chris Lattner5e02c472009-01-05 00:07:25 +00001306 case tok::coloncolon: // ::foo::bar
John McCall9ba61662010-02-26 08:45:28 +00001307 // C++ scope specifier. Annotate and loop, or bail out on error.
1308 if (TryAnnotateCXXScopeToken(true)) {
1309 if (!DS.hasTypeSpecifier())
1310 DS.SetTypeSpecError();
1311 goto DoneWithDeclSpec;
1312 }
John McCall2e0a7152010-03-01 18:20:46 +00001313 if (Tok.is(tok::coloncolon)) // ::new or ::delete
1314 goto DoneWithDeclSpec;
John McCall9ba61662010-02-26 08:45:28 +00001315 continue;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001316
1317 case tok::annot_cxxscope: {
1318 if (DS.hasTypeSpecifier())
1319 goto DoneWithDeclSpec;
1320
John McCallaa87d332009-12-12 11:40:51 +00001321 CXXScopeSpec SS;
Douglas Gregorc34348a2011-02-24 17:54:50 +00001322 Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(),
1323 Tok.getAnnotationRange(),
1324 SS);
John McCallaa87d332009-12-12 11:40:51 +00001325
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001326 // We are looking for a qualified typename.
Douglas Gregor9135c722009-03-25 15:40:00 +00001327 Token Next = NextToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001328 if (Next.is(tok::annot_template_id) &&
Douglas Gregor9135c722009-03-25 15:40:00 +00001329 static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue())
Douglas Gregorc45c2322009-03-31 00:43:58 +00001330 ->Kind == TNK_Type_template) {
Douglas Gregor9135c722009-03-25 15:40:00 +00001331 // We have a qualified template-id, e.g., N::A<int>
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001332
1333 // C++ [class.qual]p2:
1334 // In a lookup in which the constructor is an acceptable lookup
1335 // result and the nested-name-specifier nominates a class C:
1336 //
1337 // - if the name specified after the
1338 // nested-name-specifier, when looked up in C, is the
1339 // injected-class-name of C (Clause 9), or
1340 //
1341 // - if the name specified after the nested-name-specifier
1342 // is the same as the identifier or the
1343 // simple-template-id's template-name in the last
1344 // component of the nested-name-specifier,
1345 //
1346 // the name is instead considered to name the constructor of
1347 // class C.
1348 //
1349 // Thus, if the template-name is actually the constructor
1350 // name, then the code is ill-formed; this interpretation is
1351 // reinforced by the NAD status of core issue 635.
1352 TemplateIdAnnotation *TemplateId
1353 = static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue());
John McCallba9d8532010-04-13 06:39:49 +00001354 if ((DSContext == DSC_top_level ||
1355 (DSContext == DSC_class && DS.isFriendSpecified())) &&
1356 TemplateId->Name &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00001357 Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) {
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001358 if (isConstructorDeclarator()) {
1359 // The user meant this to be an out-of-line constructor
1360 // definition, but template arguments are not allowed
1361 // there. Just allow this as a constructor; we'll
1362 // complain about it later.
1363 goto DoneWithDeclSpec;
1364 }
1365
1366 // The user meant this to name a type, but it actually names
1367 // a constructor with some extraneous template
1368 // arguments. Complain, then parse it as a type as the user
1369 // intended.
1370 Diag(TemplateId->TemplateNameLoc,
1371 diag::err_out_of_line_template_id_names_constructor)
1372 << TemplateId->Name;
1373 }
1374
John McCallaa87d332009-12-12 11:40:51 +00001375 DS.getTypeSpecScope() = SS;
1376 ConsumeToken(); // The C++ scope.
Mike Stump1eb44332009-09-09 15:08:12 +00001377 assert(Tok.is(tok::annot_template_id) &&
Douglas Gregor9135c722009-03-25 15:40:00 +00001378 "ParseOptionalCXXScopeSpecifier not working");
Douglas Gregor059101f2011-03-02 00:47:37 +00001379 AnnotateTemplateIdTokenAsType();
Douglas Gregor9135c722009-03-25 15:40:00 +00001380 continue;
1381 }
1382
Douglas Gregor9d7b3532009-09-28 07:26:33 +00001383 if (Next.is(tok::annot_typename)) {
John McCallaa87d332009-12-12 11:40:51 +00001384 DS.getTypeSpecScope() = SS;
1385 ConsumeToken(); // The C++ scope.
John McCallb3d87482010-08-24 05:47:05 +00001386 if (Tok.getAnnotationValue()) {
1387 ParsedType T = getTypeAnnotation(Tok);
Nico Weber253e80b2010-11-22 10:30:56 +00001388 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename,
1389 Tok.getAnnotationEndLoc(),
John McCallb3d87482010-08-24 05:47:05 +00001390 PrevSpec, DiagID, T);
1391 }
Douglas Gregor9d7b3532009-09-28 07:26:33 +00001392 else
1393 DS.SetTypeSpecError();
1394 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1395 ConsumeToken(); // The typename
1396 }
1397
Douglas Gregor9135c722009-03-25 15:40:00 +00001398 if (Next.isNot(tok::identifier))
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001399 goto DoneWithDeclSpec;
1400
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001401 // If we're in a context where the identifier could be a class name,
1402 // check whether this is a constructor declaration.
John McCallba9d8532010-04-13 06:39:49 +00001403 if ((DSContext == DSC_top_level ||
1404 (DSContext == DSC_class && DS.isFriendSpecified())) &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00001405 Actions.isCurrentClassName(*Next.getIdentifierInfo(), getCurScope(),
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001406 &SS)) {
1407 if (isConstructorDeclarator())
1408 goto DoneWithDeclSpec;
1409
1410 // As noted in C++ [class.qual]p2 (cited above), when the name
1411 // of the class is qualified in a context where it could name
1412 // a constructor, its a constructor name. However, we've
1413 // looked at the declarator, and the user probably meant this
1414 // to be a type. Complain that it isn't supposed to be treated
1415 // as a type, then proceed to parse it as a type.
1416 Diag(Next.getLocation(), diag::err_out_of_line_type_names_constructor)
1417 << Next.getIdentifierInfo();
1418 }
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001419
John McCallb3d87482010-08-24 05:47:05 +00001420 ParsedType TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(),
1421 Next.getLocation(),
Douglas Gregor9e876872011-03-01 18:12:44 +00001422 getCurScope(), &SS,
1423 false, false, ParsedType(),
1424 /*NonTrivialSourceInfo=*/true);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001425
Chris Lattnerf4382f52009-04-14 22:17:06 +00001426 // If the referenced identifier is not a type, then this declspec is
1427 // erroneous: We already checked about that it has no type specifier, and
1428 // C++ doesn't have implicit int. Diagnose it as a typo w.r.t. to the
Mike Stump1eb44332009-09-09 15:08:12 +00001429 // typename.
Chris Lattnerf4382f52009-04-14 22:17:06 +00001430 if (TypeRep == 0) {
1431 ConsumeToken(); // Eat the scope spec so the identifier is current.
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001432 if (ParseImplicitInt(DS, &SS, TemplateInfo, AS)) continue;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001433 goto DoneWithDeclSpec;
Chris Lattnerf4382f52009-04-14 22:17:06 +00001434 }
Mike Stump1eb44332009-09-09 15:08:12 +00001435
John McCallaa87d332009-12-12 11:40:51 +00001436 DS.getTypeSpecScope() = SS;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001437 ConsumeToken(); // The C++ scope.
1438
Douglas Gregor1a51b4a2009-02-09 15:09:02 +00001439 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001440 DiagID, TypeRep);
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001441 if (isInvalid)
1442 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001443
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001444 DS.SetRangeEnd(Tok.getLocation());
1445 ConsumeToken(); // The typename.
1446
1447 continue;
1448 }
Mike Stump1eb44332009-09-09 15:08:12 +00001449
Chris Lattner80d0c892009-01-21 19:48:37 +00001450 case tok::annot_typename: {
John McCallb3d87482010-08-24 05:47:05 +00001451 if (Tok.getAnnotationValue()) {
1452 ParsedType T = getTypeAnnotation(Tok);
Nico Weberc43271e2010-11-22 12:50:03 +00001453 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallb3d87482010-08-24 05:47:05 +00001454 DiagID, T);
1455 } else
Douglas Gregor31a19b62009-04-01 21:51:26 +00001456 DS.SetTypeSpecError();
Chris Lattner5c5db552010-04-05 18:18:31 +00001457
1458 if (isInvalid)
1459 break;
1460
Chris Lattner80d0c892009-01-21 19:48:37 +00001461 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1462 ConsumeToken(); // The typename
Mike Stump1eb44332009-09-09 15:08:12 +00001463
Chris Lattner80d0c892009-01-21 19:48:37 +00001464 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1465 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001466 // Objective-C interface.
1467 if (Tok.is(tok::less) && getLang().ObjC1)
1468 ParseObjCProtocolQualifiers(DS);
1469
Chris Lattner80d0c892009-01-21 19:48:37 +00001470 continue;
1471 }
Mike Stump1eb44332009-09-09 15:08:12 +00001472
Chris Lattner3bd934a2008-07-26 01:18:38 +00001473 // typedef-name
1474 case tok::identifier: {
Chris Lattner5e02c472009-01-05 00:07:25 +00001475 // In C++, check to see if this is a scope specifier like foo::bar::, if
1476 // so handle it as such. This is important for ctor parsing.
John McCall9ba61662010-02-26 08:45:28 +00001477 if (getLang().CPlusPlus) {
1478 if (TryAnnotateCXXScopeToken(true)) {
1479 if (!DS.hasTypeSpecifier())
1480 DS.SetTypeSpecError();
1481 goto DoneWithDeclSpec;
1482 }
1483 if (!Tok.is(tok::identifier))
1484 continue;
1485 }
Mike Stump1eb44332009-09-09 15:08:12 +00001486
Chris Lattner3bd934a2008-07-26 01:18:38 +00001487 // This identifier can only be a typedef name if we haven't already seen
1488 // a type-specifier. Without this check we misparse:
1489 // typedef int X; struct Y { short X; }; as 'short int'.
1490 if (DS.hasTypeSpecifier())
1491 goto DoneWithDeclSpec;
Mike Stump1eb44332009-09-09 15:08:12 +00001492
John Thompson82287d12010-02-05 00:12:22 +00001493 // Check for need to substitute AltiVec keyword tokens.
1494 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
1495 break;
1496
Chris Lattner3bd934a2008-07-26 01:18:38 +00001497 // It has to be available as a typedef too!
John McCallb3d87482010-08-24 05:47:05 +00001498 ParsedType TypeRep =
1499 Actions.getTypeName(*Tok.getIdentifierInfo(),
1500 Tok.getLocation(), getCurScope());
Douglas Gregor55f6b142009-02-09 18:46:07 +00001501
Chris Lattnerc199ab32009-04-12 20:42:31 +00001502 // If this is not a typedef name, don't parse it as part of the declspec,
1503 // it must be an implicit int or an error.
John McCallb3d87482010-08-24 05:47:05 +00001504 if (!TypeRep) {
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001505 if (ParseImplicitInt(DS, 0, TemplateInfo, AS)) continue;
Chris Lattner3bd934a2008-07-26 01:18:38 +00001506 goto DoneWithDeclSpec;
Chris Lattnerc199ab32009-04-12 20:42:31 +00001507 }
Douglas Gregor55f6b142009-02-09 18:46:07 +00001508
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001509 // If we're in a context where the identifier could be a class name,
1510 // check whether this is a constructor declaration.
1511 if (getLang().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00001512 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), getCurScope()) &&
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001513 isConstructorDeclarator())
Douglas Gregorb48fe382008-10-31 09:07:45 +00001514 goto DoneWithDeclSpec;
1515
Douglas Gregor1a51b4a2009-02-09 15:09:02 +00001516 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001517 DiagID, TypeRep);
Chris Lattner3bd934a2008-07-26 01:18:38 +00001518 if (isInvalid)
1519 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001520
Chris Lattner3bd934a2008-07-26 01:18:38 +00001521 DS.SetRangeEnd(Tok.getLocation());
1522 ConsumeToken(); // The identifier
1523
1524 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1525 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001526 // Objective-C interface.
1527 if (Tok.is(tok::less) && getLang().ObjC1)
1528 ParseObjCProtocolQualifiers(DS);
1529
Steve Naroff4f9b9f12008-09-22 10:28:57 +00001530 // Need to support trailing type qualifiers (e.g. "id<p> const").
1531 // If a type specifier follows, it will be diagnosed elsewhere.
1532 continue;
Chris Lattner3bd934a2008-07-26 01:18:38 +00001533 }
Douglas Gregor39a8de12009-02-25 19:37:18 +00001534
1535 // type-name
1536 case tok::annot_template_id: {
Mike Stump1eb44332009-09-09 15:08:12 +00001537 TemplateIdAnnotation *TemplateId
Douglas Gregor39a8de12009-02-25 19:37:18 +00001538 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
Douglas Gregorc45c2322009-03-31 00:43:58 +00001539 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor39a8de12009-02-25 19:37:18 +00001540 // This template-id does not refer to a type name, so we're
1541 // done with the type-specifiers.
1542 goto DoneWithDeclSpec;
1543 }
1544
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001545 // If we're in a context where the template-id could be a
1546 // constructor name or specialization, check whether this is a
1547 // constructor declaration.
1548 if (getLang().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00001549 Actions.isCurrentClassName(*TemplateId->Name, getCurScope()) &&
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001550 isConstructorDeclarator())
1551 goto DoneWithDeclSpec;
1552
Douglas Gregor39a8de12009-02-25 19:37:18 +00001553 // Turn the template-id annotation token into a type annotation
1554 // token, then try again to parse it as a type-specifier.
Douglas Gregor31a19b62009-04-01 21:51:26 +00001555 AnnotateTemplateIdTokenAsType();
Douglas Gregor39a8de12009-02-25 19:37:18 +00001556 continue;
1557 }
1558
Reid Spencer5f016e22007-07-11 17:01:13 +00001559 // GNU attributes support.
1560 case tok::kw___attribute:
John McCall7f040a92010-12-24 02:08:15 +00001561 ParseGNUAttributes(DS.getAttributes());
Reid Spencer5f016e22007-07-11 17:01:13 +00001562 continue;
Steve Narofff59e17e2008-12-24 20:59:21 +00001563
1564 // Microsoft declspec support.
1565 case tok::kw___declspec:
John McCall7f040a92010-12-24 02:08:15 +00001566 ParseMicrosoftDeclSpec(DS.getAttributes());
Steve Narofff59e17e2008-12-24 20:59:21 +00001567 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001568
Steve Naroff239f0732008-12-25 14:16:32 +00001569 // Microsoft single token adornments.
Steve Naroff86bc6cf2008-12-25 14:41:26 +00001570 case tok::kw___forceinline:
Eli Friedman290eeb02009-06-08 23:27:34 +00001571 // FIXME: Add handling here!
1572 break;
1573
1574 case tok::kw___ptr64:
Steve Naroff86bc6cf2008-12-25 14:41:26 +00001575 case tok::kw___w64:
Steve Naroff239f0732008-12-25 14:16:32 +00001576 case tok::kw___cdecl:
1577 case tok::kw___stdcall:
1578 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00001579 case tok::kw___thiscall:
John McCall7f040a92010-12-24 02:08:15 +00001580 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman290eeb02009-06-08 23:27:34 +00001581 continue;
1582
Dawn Perchik52fc3142010-09-03 01:29:35 +00001583 // Borland single token adornments.
1584 case tok::kw___pascal:
John McCall7f040a92010-12-24 02:08:15 +00001585 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik52fc3142010-09-03 01:29:35 +00001586 continue;
1587
Peter Collingbournef315fa82011-02-14 01:42:53 +00001588 // OpenCL single token adornments.
1589 case tok::kw___kernel:
1590 ParseOpenCLAttributes(DS.getAttributes());
1591 continue;
1592
Reid Spencer5f016e22007-07-11 17:01:13 +00001593 // storage-class-specifier
1594 case tok::kw_typedef:
John McCallfec54012009-08-03 20:12:06 +00001595 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_typedef, Loc, PrevSpec,
Peter Collingbournee2f82f72011-02-11 19:59:54 +00001596 DiagID, getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00001597 break;
1598 case tok::kw_extern:
1599 if (DS.isThreadSpecified())
Chris Lattner1ab3b962008-11-18 07:48:38 +00001600 Diag(Tok, diag::ext_thread_before) << "extern";
John McCallfec54012009-08-03 20:12:06 +00001601 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_extern, Loc, PrevSpec,
Peter Collingbournee2f82f72011-02-11 19:59:54 +00001602 DiagID, getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00001603 break;
Steve Naroff8d54bf22007-12-18 00:16:02 +00001604 case tok::kw___private_extern__:
Chris Lattnerf97409f2008-04-06 06:57:35 +00001605 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_private_extern, Loc,
Peter Collingbournee2f82f72011-02-11 19:59:54 +00001606 PrevSpec, DiagID, getLang());
Steve Naroff8d54bf22007-12-18 00:16:02 +00001607 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00001608 case tok::kw_static:
1609 if (DS.isThreadSpecified())
Chris Lattner1ab3b962008-11-18 07:48:38 +00001610 Diag(Tok, diag::ext_thread_before) << "static";
John McCallfec54012009-08-03 20:12:06 +00001611 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_static, Loc, PrevSpec,
Peter Collingbournee2f82f72011-02-11 19:59:54 +00001612 DiagID, getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00001613 break;
1614 case tok::kw_auto:
Douglas Gregor18d8b792011-03-14 21:43:30 +00001615 if (getLang().CPlusPlus0x) {
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00001616 if (isKnownToBeTypeSpecifier(GetLookAheadToken(1))) {
1617 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, Loc, PrevSpec,
1618 DiagID, getLang());
1619 if (!isInvalid)
1620 Diag(Tok, diag::auto_storage_class)
1621 << FixItHint::CreateRemoval(DS.getStorageClassSpecLoc());
1622 }
1623 else
1624 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec,
1625 DiagID);
1626 }
Anders Carlssone89d1592009-06-26 18:41:36 +00001627 else
John McCallfec54012009-08-03 20:12:06 +00001628 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, Loc, PrevSpec,
Peter Collingbournee2f82f72011-02-11 19:59:54 +00001629 DiagID, getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00001630 break;
1631 case tok::kw_register:
John McCallfec54012009-08-03 20:12:06 +00001632 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_register, Loc, PrevSpec,
Peter Collingbournee2f82f72011-02-11 19:59:54 +00001633 DiagID, getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00001634 break;
Sebastian Redl669d5d72008-11-14 23:42:31 +00001635 case tok::kw_mutable:
John McCallfec54012009-08-03 20:12:06 +00001636 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_mutable, Loc, PrevSpec,
Peter Collingbournee2f82f72011-02-11 19:59:54 +00001637 DiagID, getLang());
Sebastian Redl669d5d72008-11-14 23:42:31 +00001638 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00001639 case tok::kw___thread:
John McCallfec54012009-08-03 20:12:06 +00001640 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001641 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001642
Reid Spencer5f016e22007-07-11 17:01:13 +00001643 // function-specifier
1644 case tok::kw_inline:
John McCallfec54012009-08-03 20:12:06 +00001645 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001646 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +00001647 case tok::kw_virtual:
John McCallfec54012009-08-03 20:12:06 +00001648 isInvalid = DS.SetFunctionSpecVirtual(Loc, PrevSpec, DiagID);
Douglas Gregorb48fe382008-10-31 09:07:45 +00001649 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +00001650 case tok::kw_explicit:
John McCallfec54012009-08-03 20:12:06 +00001651 isInvalid = DS.SetFunctionSpecExplicit(Loc, PrevSpec, DiagID);
Douglas Gregorb48fe382008-10-31 09:07:45 +00001652 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00001653
Anders Carlssonf47f7a12009-05-06 04:46:28 +00001654 // friend
1655 case tok::kw_friend:
John McCall67d1a672009-08-06 02:15:43 +00001656 if (DSContext == DSC_class)
1657 isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID);
1658 else {
1659 PrevSpec = ""; // not actually used by the diagnostic
1660 DiagID = diag::err_friend_invalid_in_context;
1661 isInvalid = true;
1662 }
Anders Carlssonf47f7a12009-05-06 04:46:28 +00001663 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001664
Sebastian Redl2ac67232009-11-05 15:47:02 +00001665 // constexpr
1666 case tok::kw_constexpr:
1667 isInvalid = DS.SetConstexprSpec(Loc, PrevSpec, DiagID);
1668 break;
1669
Chris Lattner80d0c892009-01-21 19:48:37 +00001670 // type-specifier
1671 case tok::kw_short:
John McCallfec54012009-08-03 20:12:06 +00001672 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec,
1673 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001674 break;
1675 case tok::kw_long:
1676 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCallfec54012009-08-03 20:12:06 +00001677 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
1678 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001679 else
John McCallfec54012009-08-03 20:12:06 +00001680 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
1681 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001682 break;
1683 case tok::kw_signed:
John McCallfec54012009-08-03 20:12:06 +00001684 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec,
1685 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001686 break;
1687 case tok::kw_unsigned:
John McCallfec54012009-08-03 20:12:06 +00001688 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
1689 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001690 break;
1691 case tok::kw__Complex:
John McCallfec54012009-08-03 20:12:06 +00001692 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
1693 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001694 break;
1695 case tok::kw__Imaginary:
John McCallfec54012009-08-03 20:12:06 +00001696 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
1697 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001698 break;
1699 case tok::kw_void:
John McCallfec54012009-08-03 20:12:06 +00001700 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec,
1701 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001702 break;
1703 case tok::kw_char:
John McCallfec54012009-08-03 20:12:06 +00001704 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec,
1705 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001706 break;
1707 case tok::kw_int:
John McCallfec54012009-08-03 20:12:06 +00001708 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec,
1709 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001710 break;
1711 case tok::kw_float:
John McCallfec54012009-08-03 20:12:06 +00001712 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec,
1713 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001714 break;
1715 case tok::kw_double:
John McCallfec54012009-08-03 20:12:06 +00001716 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec,
1717 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001718 break;
1719 case tok::kw_wchar_t:
John McCallfec54012009-08-03 20:12:06 +00001720 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec,
1721 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001722 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001723 case tok::kw_char16_t:
John McCallfec54012009-08-03 20:12:06 +00001724 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec,
1725 DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001726 break;
1727 case tok::kw_char32_t:
John McCallfec54012009-08-03 20:12:06 +00001728 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec,
1729 DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001730 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00001731 case tok::kw_bool:
1732 case tok::kw__Bool:
Argyrios Kyrtzidis4383e182010-11-16 18:18:13 +00001733 if (Tok.is(tok::kw_bool) &&
1734 DS.getTypeSpecType() != DeclSpec::TST_unspecified &&
1735 DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
1736 PrevSpec = ""; // Not used by the diagnostic.
1737 DiagID = diag::err_bool_redeclaration;
1738 isInvalid = true;
1739 } else {
1740 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec,
1741 DiagID);
1742 }
Chris Lattner80d0c892009-01-21 19:48:37 +00001743 break;
1744 case tok::kw__Decimal32:
John McCallfec54012009-08-03 20:12:06 +00001745 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
1746 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001747 break;
1748 case tok::kw__Decimal64:
John McCallfec54012009-08-03 20:12:06 +00001749 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
1750 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001751 break;
1752 case tok::kw__Decimal128:
John McCallfec54012009-08-03 20:12:06 +00001753 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
1754 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001755 break;
John Thompson82287d12010-02-05 00:12:22 +00001756 case tok::kw___vector:
1757 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
1758 break;
1759 case tok::kw___pixel:
1760 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
1761 break;
John McCalla5fc4722011-04-09 22:50:59 +00001762 case tok::kw___unknown_anytype:
1763 isInvalid = DS.SetTypeSpecType(TST_unknown_anytype, Loc,
1764 PrevSpec, DiagID);
1765 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00001766
1767 // class-specifier:
1768 case tok::kw_class:
1769 case tok::kw_struct:
Chris Lattner4c97d762009-04-12 21:49:30 +00001770 case tok::kw_union: {
1771 tok::TokenKind Kind = Tok.getKind();
1772 ConsumeToken();
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001773 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS);
Chris Lattner80d0c892009-01-21 19:48:37 +00001774 continue;
Chris Lattner4c97d762009-04-12 21:49:30 +00001775 }
Chris Lattner80d0c892009-01-21 19:48:37 +00001776
1777 // enum-specifier:
1778 case tok::kw_enum:
Chris Lattner4c97d762009-04-12 21:49:30 +00001779 ConsumeToken();
Douglas Gregor9b9edd62010-03-02 17:53:14 +00001780 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS);
Chris Lattner80d0c892009-01-21 19:48:37 +00001781 continue;
1782
1783 // cv-qualifier:
1784 case tok::kw_const:
John McCallfec54012009-08-03 20:12:06 +00001785 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec, DiagID,
1786 getLang());
Chris Lattner80d0c892009-01-21 19:48:37 +00001787 break;
1788 case tok::kw_volatile:
John McCallfec54012009-08-03 20:12:06 +00001789 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
1790 getLang());
Chris Lattner80d0c892009-01-21 19:48:37 +00001791 break;
1792 case tok::kw_restrict:
John McCallfec54012009-08-03 20:12:06 +00001793 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
1794 getLang());
Chris Lattner80d0c892009-01-21 19:48:37 +00001795 break;
1796
Douglas Gregord57959a2009-03-27 23:10:48 +00001797 // C++ typename-specifier:
1798 case tok::kw_typename:
John McCall9ba61662010-02-26 08:45:28 +00001799 if (TryAnnotateTypeOrScopeToken()) {
1800 DS.SetTypeSpecError();
1801 goto DoneWithDeclSpec;
1802 }
1803 if (!Tok.is(tok::kw_typename))
Douglas Gregord57959a2009-03-27 23:10:48 +00001804 continue;
1805 break;
1806
Chris Lattner80d0c892009-01-21 19:48:37 +00001807 // GNU typeof support.
1808 case tok::kw_typeof:
1809 ParseTypeofSpecifier(DS);
1810 continue;
1811
Anders Carlsson6fd634f2009-06-24 17:47:40 +00001812 case tok::kw_decltype:
1813 ParseDecltypeSpecifier(DS);
1814 continue;
1815
Peter Collingbourne207f4d82011-03-18 22:38:29 +00001816 // OpenCL qualifiers:
1817 case tok::kw_private:
1818 if (!getLang().OpenCL)
1819 goto DoneWithDeclSpec;
1820 case tok::kw___private:
1821 case tok::kw___global:
1822 case tok::kw___local:
1823 case tok::kw___constant:
1824 case tok::kw___read_only:
1825 case tok::kw___write_only:
1826 case tok::kw___read_write:
1827 ParseOpenCLQualifiers(DS);
1828 break;
1829
Steve Naroffd3ded1f2008-06-05 00:02:44 +00001830 case tok::less:
Chris Lattner3bd934a2008-07-26 01:18:38 +00001831 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattnerbce61352008-07-26 00:20:22 +00001832 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
1833 // but we support it.
Chris Lattner3bd934a2008-07-26 01:18:38 +00001834 if (DS.hasTypeSpecifier() || !getLang().ObjC1)
Chris Lattnerbce61352008-07-26 00:20:22 +00001835 goto DoneWithDeclSpec;
Mike Stump1eb44332009-09-09 15:08:12 +00001836
Douglas Gregor46f936e2010-11-19 17:10:50 +00001837 if (!ParseObjCProtocolQualifiers(DS))
1838 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
1839 << FixItHint::CreateInsertion(Loc, "id")
1840 << SourceRange(Loc, DS.getSourceRange().getEnd());
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001841
1842 // Need to support trailing type qualifiers (e.g. "id<p> const").
1843 // If a type specifier follows, it will be diagnosed elsewhere.
1844 continue;
Reid Spencer5f016e22007-07-11 17:01:13 +00001845 }
John McCallfec54012009-08-03 20:12:06 +00001846 // If the specifier wasn't legal, issue a diagnostic.
Reid Spencer5f016e22007-07-11 17:01:13 +00001847 if (isInvalid) {
1848 assert(PrevSpec && "Method did not return previous specifier!");
John McCallfec54012009-08-03 20:12:06 +00001849 assert(DiagID);
Douglas Gregorae2fb142010-08-23 14:34:43 +00001850
1851 if (DiagID == diag::ext_duplicate_declspec)
1852 Diag(Tok, DiagID)
1853 << PrevSpec << FixItHint::CreateRemoval(Tok.getLocation());
1854 else
1855 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00001856 }
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00001857
Chris Lattner81c018d2008-03-13 06:29:04 +00001858 DS.SetRangeEnd(Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00001859 ConsumeToken();
1860 }
1861}
Douglas Gregoradcac882008-12-01 23:54:00 +00001862
Chris Lattner7a0ab5f2009-01-06 06:59:53 +00001863/// ParseOptionalTypeSpecifier - Try to parse a single type-specifier. We
Douglas Gregor12e083c2008-11-07 15:42:26 +00001864/// primarily follow the C++ grammar with additions for C99 and GNU,
1865/// which together subsume the C grammar. Note that the C++
1866/// type-specifier also includes the C type-qualifier (for const,
1867/// volatile, and C99 restrict). Returns true if a type-specifier was
1868/// found (and parsed), false otherwise.
1869///
1870/// type-specifier: [C++ 7.1.5]
1871/// simple-type-specifier
1872/// class-specifier
1873/// enum-specifier
1874/// elaborated-type-specifier [TODO]
1875/// cv-qualifier
1876///
1877/// cv-qualifier: [C++ 7.1.5.1]
1878/// 'const'
1879/// 'volatile'
1880/// [C99] 'restrict'
1881///
1882/// simple-type-specifier: [ C++ 7.1.5.2]
1883/// '::'[opt] nested-name-specifier[opt] type-name [TODO]
1884/// '::'[opt] nested-name-specifier 'template' template-id [TODO]
1885/// 'char'
1886/// 'wchar_t'
1887/// 'bool'
1888/// 'short'
1889/// 'int'
1890/// 'long'
1891/// 'signed'
1892/// 'unsigned'
1893/// 'float'
1894/// 'double'
1895/// 'void'
1896/// [C99] '_Bool'
1897/// [C99] '_Complex'
1898/// [C99] '_Imaginary' // Removed in TC2?
1899/// [GNU] '_Decimal32'
1900/// [GNU] '_Decimal64'
1901/// [GNU] '_Decimal128'
1902/// [GNU] typeof-specifier
1903/// [OBJC] class-name objc-protocol-refs[opt] [TODO]
1904/// [OBJC] typedef-name objc-protocol-refs[opt] [TODO]
Anders Carlsson6fd634f2009-06-24 17:47:40 +00001905/// [C++0x] 'decltype' ( expression )
John Thompson82287d12010-02-05 00:12:22 +00001906/// [AltiVec] '__vector'
John McCallfec54012009-08-03 20:12:06 +00001907bool Parser::ParseOptionalTypeSpecifier(DeclSpec &DS, bool& isInvalid,
Chris Lattner7a0ab5f2009-01-06 06:59:53 +00001908 const char *&PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001909 unsigned &DiagID,
Sebastian Redld9bafa72010-02-03 21:21:43 +00001910 const ParsedTemplateInfo &TemplateInfo,
1911 bool SuppressDeclarations) {
Douglas Gregor12e083c2008-11-07 15:42:26 +00001912 SourceLocation Loc = Tok.getLocation();
1913
1914 switch (Tok.getKind()) {
Chris Lattner166a8fc2009-01-04 23:41:41 +00001915 case tok::identifier: // foo::bar
Douglas Gregorc0b39642010-04-15 23:40:53 +00001916 // If we already have a type specifier, this identifier is not a type.
1917 if (DS.getTypeSpecType() != DeclSpec::TST_unspecified ||
1918 DS.getTypeSpecWidth() != DeclSpec::TSW_unspecified ||
1919 DS.getTypeSpecSign() != DeclSpec::TSS_unspecified)
1920 return false;
John Thompson82287d12010-02-05 00:12:22 +00001921 // Check for need to substitute AltiVec keyword tokens.
1922 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
1923 break;
1924 // Fall through.
Douglas Gregord57959a2009-03-27 23:10:48 +00001925 case tok::kw_typename: // typename foo::bar
Chris Lattner166a8fc2009-01-04 23:41:41 +00001926 // Annotate typenames and C++ scope specifiers. If we get one, just
1927 // recurse to handle whatever we get.
1928 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00001929 return true;
1930 if (Tok.is(tok::identifier))
1931 return false;
1932 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
1933 TemplateInfo, SuppressDeclarations);
Chris Lattner166a8fc2009-01-04 23:41:41 +00001934 case tok::coloncolon: // ::foo::bar
1935 if (NextToken().is(tok::kw_new) || // ::new
1936 NextToken().is(tok::kw_delete)) // ::delete
1937 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001938
Chris Lattner166a8fc2009-01-04 23:41:41 +00001939 // Annotate typenames and C++ scope specifiers. If we get one, just
1940 // recurse to handle whatever we get.
1941 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00001942 return true;
1943 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
1944 TemplateInfo, SuppressDeclarations);
Mike Stump1eb44332009-09-09 15:08:12 +00001945
Douglas Gregor12e083c2008-11-07 15:42:26 +00001946 // simple-type-specifier:
Chris Lattnerb31757b2009-01-06 05:06:21 +00001947 case tok::annot_typename: {
John McCallb3d87482010-08-24 05:47:05 +00001948 if (ParsedType T = getTypeAnnotation(Tok)) {
Nico Weber253e80b2010-11-22 10:30:56 +00001949 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename,
1950 Tok.getAnnotationEndLoc(), PrevSpec,
John McCallb3d87482010-08-24 05:47:05 +00001951 DiagID, T);
1952 } else
Douglas Gregor31a19b62009-04-01 21:51:26 +00001953 DS.SetTypeSpecError();
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001954 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1955 ConsumeToken(); // The typename
Mike Stump1eb44332009-09-09 15:08:12 +00001956
Douglas Gregor12e083c2008-11-07 15:42:26 +00001957 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1958 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
1959 // Objective-C interface. If we don't have Objective-C or a '<', this is
1960 // just a normal reference to a typedef name.
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001961 if (Tok.is(tok::less) && getLang().ObjC1)
1962 ParseObjCProtocolQualifiers(DS);
1963
Douglas Gregor12e083c2008-11-07 15:42:26 +00001964 return true;
1965 }
1966
1967 case tok::kw_short:
John McCallfec54012009-08-03 20:12:06 +00001968 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001969 break;
1970 case tok::kw_long:
1971 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCallfec54012009-08-03 20:12:06 +00001972 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
1973 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001974 else
John McCallfec54012009-08-03 20:12:06 +00001975 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
1976 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001977 break;
1978 case tok::kw_signed:
John McCallfec54012009-08-03 20:12:06 +00001979 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001980 break;
1981 case tok::kw_unsigned:
John McCallfec54012009-08-03 20:12:06 +00001982 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
1983 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001984 break;
1985 case tok::kw__Complex:
John McCallfec54012009-08-03 20:12:06 +00001986 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
1987 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001988 break;
1989 case tok::kw__Imaginary:
John McCallfec54012009-08-03 20:12:06 +00001990 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
1991 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001992 break;
1993 case tok::kw_void:
John McCallfec54012009-08-03 20:12:06 +00001994 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001995 break;
1996 case tok::kw_char:
John McCallfec54012009-08-03 20:12:06 +00001997 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001998 break;
1999 case tok::kw_int:
John McCallfec54012009-08-03 20:12:06 +00002000 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002001 break;
2002 case tok::kw_float:
John McCallfec54012009-08-03 20:12:06 +00002003 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002004 break;
2005 case tok::kw_double:
John McCallfec54012009-08-03 20:12:06 +00002006 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002007 break;
2008 case tok::kw_wchar_t:
John McCallfec54012009-08-03 20:12:06 +00002009 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002010 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002011 case tok::kw_char16_t:
John McCallfec54012009-08-03 20:12:06 +00002012 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002013 break;
2014 case tok::kw_char32_t:
John McCallfec54012009-08-03 20:12:06 +00002015 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002016 break;
Douglas Gregor12e083c2008-11-07 15:42:26 +00002017 case tok::kw_bool:
2018 case tok::kw__Bool:
John McCallfec54012009-08-03 20:12:06 +00002019 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002020 break;
2021 case tok::kw__Decimal32:
John McCallfec54012009-08-03 20:12:06 +00002022 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
2023 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002024 break;
2025 case tok::kw__Decimal64:
John McCallfec54012009-08-03 20:12:06 +00002026 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
2027 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002028 break;
2029 case tok::kw__Decimal128:
John McCallfec54012009-08-03 20:12:06 +00002030 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
2031 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002032 break;
John Thompson82287d12010-02-05 00:12:22 +00002033 case tok::kw___vector:
2034 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
2035 break;
2036 case tok::kw___pixel:
2037 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
2038 break;
2039
Douglas Gregor12e083c2008-11-07 15:42:26 +00002040 // class-specifier:
2041 case tok::kw_class:
2042 case tok::kw_struct:
Chris Lattner4c97d762009-04-12 21:49:30 +00002043 case tok::kw_union: {
2044 tok::TokenKind Kind = Tok.getKind();
2045 ConsumeToken();
Sebastian Redld9bafa72010-02-03 21:21:43 +00002046 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS_none,
2047 SuppressDeclarations);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002048 return true;
Chris Lattner4c97d762009-04-12 21:49:30 +00002049 }
Douglas Gregor12e083c2008-11-07 15:42:26 +00002050
2051 // enum-specifier:
2052 case tok::kw_enum:
Chris Lattner4c97d762009-04-12 21:49:30 +00002053 ConsumeToken();
Douglas Gregor9b9edd62010-03-02 17:53:14 +00002054 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS_none);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002055 return true;
2056
2057 // cv-qualifier:
2058 case tok::kw_const:
2059 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00002060 DiagID, getLang());
Douglas Gregor12e083c2008-11-07 15:42:26 +00002061 break;
2062 case tok::kw_volatile:
2063 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00002064 DiagID, getLang());
Douglas Gregor12e083c2008-11-07 15:42:26 +00002065 break;
2066 case tok::kw_restrict:
2067 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00002068 DiagID, getLang());
Douglas Gregor12e083c2008-11-07 15:42:26 +00002069 break;
2070
2071 // GNU typeof support.
2072 case tok::kw_typeof:
2073 ParseTypeofSpecifier(DS);
2074 return true;
2075
Anders Carlsson6fd634f2009-06-24 17:47:40 +00002076 // C++0x decltype support.
2077 case tok::kw_decltype:
2078 ParseDecltypeSpecifier(DS);
2079 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00002080
Peter Collingbourne207f4d82011-03-18 22:38:29 +00002081 // OpenCL qualifiers:
2082 case tok::kw_private:
2083 if (!getLang().OpenCL)
2084 return false;
2085 case tok::kw___private:
2086 case tok::kw___global:
2087 case tok::kw___local:
2088 case tok::kw___constant:
2089 case tok::kw___read_only:
2090 case tok::kw___write_only:
2091 case tok::kw___read_write:
2092 ParseOpenCLQualifiers(DS);
2093 break;
2094
Anders Carlsson0b7f7892009-06-26 23:44:14 +00002095 // C++0x auto support.
2096 case tok::kw_auto:
2097 if (!getLang().CPlusPlus0x)
2098 return false;
2099
John McCallfec54012009-08-03 20:12:06 +00002100 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec, DiagID);
Anders Carlsson0b7f7892009-06-26 23:44:14 +00002101 break;
Dawn Perchik52fc3142010-09-03 01:29:35 +00002102
Eli Friedman290eeb02009-06-08 23:27:34 +00002103 case tok::kw___ptr64:
2104 case tok::kw___w64:
Steve Naroff239f0732008-12-25 14:16:32 +00002105 case tok::kw___cdecl:
2106 case tok::kw___stdcall:
2107 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00002108 case tok::kw___thiscall:
John McCall7f040a92010-12-24 02:08:15 +00002109 ParseMicrosoftTypeAttributes(DS.getAttributes());
Chris Lattner837acd02009-01-21 19:19:26 +00002110 return true;
Steve Naroff239f0732008-12-25 14:16:32 +00002111
Dawn Perchik52fc3142010-09-03 01:29:35 +00002112 case tok::kw___pascal:
John McCall7f040a92010-12-24 02:08:15 +00002113 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik52fc3142010-09-03 01:29:35 +00002114 return true;
2115
Douglas Gregor12e083c2008-11-07 15:42:26 +00002116 default:
2117 // Not a type-specifier; do nothing.
2118 return false;
2119 }
2120
2121 // If the specifier combination wasn't legal, issue a diagnostic.
2122 if (isInvalid) {
2123 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner1ab3b962008-11-18 07:48:38 +00002124 // Pick between error or extwarn.
Chris Lattner1ab3b962008-11-18 07:48:38 +00002125 Diag(Tok, DiagID) << PrevSpec;
Douglas Gregor12e083c2008-11-07 15:42:26 +00002126 }
2127 DS.SetRangeEnd(Tok.getLocation());
2128 ConsumeToken(); // whatever we parsed above.
2129 return true;
2130}
Reid Spencer5f016e22007-07-11 17:01:13 +00002131
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002132/// ParseStructDeclaration - Parse a struct declaration without the terminating
2133/// semicolon.
2134///
Reid Spencer5f016e22007-07-11 17:01:13 +00002135/// struct-declaration:
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002136/// specifier-qualifier-list struct-declarator-list
Reid Spencer5f016e22007-07-11 17:01:13 +00002137/// [GNU] __extension__ struct-declaration
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002138/// [GNU] specifier-qualifier-list
Reid Spencer5f016e22007-07-11 17:01:13 +00002139/// struct-declarator-list:
2140/// struct-declarator
2141/// struct-declarator-list ',' struct-declarator
2142/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
2143/// struct-declarator:
2144/// declarator
2145/// [GNU] declarator attributes[opt]
2146/// declarator[opt] ':' constant-expression
2147/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
2148///
Chris Lattnere1359422008-04-10 06:46:29 +00002149void Parser::
John McCallbdd563e2009-11-03 02:38:08 +00002150ParseStructDeclaration(DeclSpec &DS, FieldCallback &Fields) {
Chris Lattnerc46d1a12008-10-20 06:45:43 +00002151 if (Tok.is(tok::kw___extension__)) {
2152 // __extension__ silences extension warnings in the subexpression.
2153 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroff28a7ca82007-08-20 22:28:22 +00002154 ConsumeToken();
Chris Lattnerc46d1a12008-10-20 06:45:43 +00002155 return ParseStructDeclaration(DS, Fields);
2156 }
Mike Stump1eb44332009-09-09 15:08:12 +00002157
Steve Naroff28a7ca82007-08-20 22:28:22 +00002158 // Parse the common specifier-qualifiers-list piece.
Steve Naroff28a7ca82007-08-20 22:28:22 +00002159 ParseSpecifierQualifierList(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00002160
Douglas Gregor4920f1f2009-01-12 22:49:06 +00002161 // If there are no declarators, this is a free-standing declaration
2162 // specifier. Let the actions module cope with it.
Chris Lattner04d66662007-10-09 17:33:22 +00002163 if (Tok.is(tok::semi)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00002164 Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none, DS);
Steve Naroff28a7ca82007-08-20 22:28:22 +00002165 return;
2166 }
2167
2168 // Read struct-declarators until we find the semicolon.
John McCallbdd563e2009-11-03 02:38:08 +00002169 bool FirstDeclarator = true;
Steve Naroff28a7ca82007-08-20 22:28:22 +00002170 while (1) {
John McCall54abf7d2009-11-04 02:18:39 +00002171 ParsingDeclRAIIObject PD(*this);
John McCallbdd563e2009-11-03 02:38:08 +00002172 FieldDeclarator DeclaratorInfo(DS);
2173
2174 // Attributes are only allowed here on successive declarators.
John McCall7f040a92010-12-24 02:08:15 +00002175 if (!FirstDeclarator)
2176 MaybeParseGNUAttributes(DeclaratorInfo.D);
Mike Stump1eb44332009-09-09 15:08:12 +00002177
Steve Naroff28a7ca82007-08-20 22:28:22 +00002178 /// struct-declarator: declarator
2179 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattnera1efc8c2009-12-10 01:59:24 +00002180 if (Tok.isNot(tok::colon)) {
2181 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
2182 ColonProtectionRAIIObject X(*this);
Chris Lattnere1359422008-04-10 06:46:29 +00002183 ParseDeclarator(DeclaratorInfo.D);
Chris Lattnera1efc8c2009-12-10 01:59:24 +00002184 }
Mike Stump1eb44332009-09-09 15:08:12 +00002185
Chris Lattner04d66662007-10-09 17:33:22 +00002186 if (Tok.is(tok::colon)) {
Steve Naroff28a7ca82007-08-20 22:28:22 +00002187 ConsumeToken();
John McCall60d7b3a2010-08-24 06:29:42 +00002188 ExprResult Res(ParseConstantExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002189 if (Res.isInvalid())
Steve Naroff28a7ca82007-08-20 22:28:22 +00002190 SkipUntil(tok::semi, true, true);
Chris Lattner60b1e3e2008-04-10 06:15:14 +00002191 else
Sebastian Redleffa8d12008-12-10 00:02:53 +00002192 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroff28a7ca82007-08-20 22:28:22 +00002193 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00002194
Steve Naroff28a7ca82007-08-20 22:28:22 +00002195 // If attributes exist after the declarator, parse them.
John McCall7f040a92010-12-24 02:08:15 +00002196 MaybeParseGNUAttributes(DeclaratorInfo.D);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002197
John McCallbdd563e2009-11-03 02:38:08 +00002198 // We're done with this declarator; invoke the callback.
John McCalld226f652010-08-21 09:40:31 +00002199 Decl *D = Fields.invoke(DeclaratorInfo);
John McCall54abf7d2009-11-04 02:18:39 +00002200 PD.complete(D);
John McCallbdd563e2009-11-03 02:38:08 +00002201
Steve Naroff28a7ca82007-08-20 22:28:22 +00002202 // If we don't have a comma, it is either the end of the list (a ';')
2203 // or an error, bail out.
Chris Lattner04d66662007-10-09 17:33:22 +00002204 if (Tok.isNot(tok::comma))
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002205 return;
Sebastian Redlab197ba2009-02-09 18:23:29 +00002206
Steve Naroff28a7ca82007-08-20 22:28:22 +00002207 // Consume the comma.
2208 ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00002209
John McCallbdd563e2009-11-03 02:38:08 +00002210 FirstDeclarator = false;
Steve Naroff28a7ca82007-08-20 22:28:22 +00002211 }
Steve Naroff28a7ca82007-08-20 22:28:22 +00002212}
2213
2214/// ParseStructUnionBody
2215/// struct-contents:
2216/// struct-declaration-list
2217/// [EXT] empty
2218/// [GNU] "struct-declaration-list" without terminatoring ';'
2219/// struct-declaration-list:
2220/// struct-declaration
2221/// struct-declaration-list struct-declaration
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002222/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroff28a7ca82007-08-20 22:28:22 +00002223///
Reid Spencer5f016e22007-07-11 17:01:13 +00002224void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
John McCalld226f652010-08-21 09:40:31 +00002225 unsigned TagType, Decl *TagDecl) {
John McCallf312b1e2010-08-26 23:41:50 +00002226 PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc,
2227 "parsing struct/union body");
Mike Stump1eb44332009-09-09 15:08:12 +00002228
Reid Spencer5f016e22007-07-11 17:01:13 +00002229 SourceLocation LBraceLoc = ConsumeBrace();
Mike Stump1eb44332009-09-09 15:08:12 +00002230
Douglas Gregor3218c4b2009-01-09 22:42:13 +00002231 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
Douglas Gregor23c94db2010-07-02 17:43:08 +00002232 Actions.ActOnTagStartDefinition(getCurScope(), TagDecl);
Douglas Gregor72de6672009-01-08 20:45:30 +00002233
Reid Spencer5f016e22007-07-11 17:01:13 +00002234 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
2235 // C++.
Douglas Gregore37ac4f2008-04-13 21:30:24 +00002236 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Douglas Gregor03332962010-07-29 14:29:34 +00002237 Diag(Tok, diag::ext_empty_struct_union)
2238 << (TagType == TST_union);
Reid Spencer5f016e22007-07-11 17:01:13 +00002239
John McCalld226f652010-08-21 09:40:31 +00002240 llvm::SmallVector<Decl *, 32> FieldDecls;
Chris Lattnere1359422008-04-10 06:46:29 +00002241
Reid Spencer5f016e22007-07-11 17:01:13 +00002242 // While we still have something to read, read the declarations in the struct.
Chris Lattner04d66662007-10-09 17:33:22 +00002243 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002244 // Each iteration of this loop reads one struct-declaration.
Mike Stump1eb44332009-09-09 15:08:12 +00002245
Reid Spencer5f016e22007-07-11 17:01:13 +00002246 // Check for extraneous top-level semicolon.
Chris Lattner04d66662007-10-09 17:33:22 +00002247 if (Tok.is(tok::semi)) {
Douglas Gregor9b3064b2009-04-01 22:41:11 +00002248 Diag(Tok, diag::ext_extra_struct_semi)
Douglas Gregorf13ca062010-06-16 23:08:59 +00002249 << DeclSpec::getSpecifierName((DeclSpec::TST)TagType)
Douglas Gregor849b2432010-03-31 17:46:05 +00002250 << FixItHint::CreateRemoval(Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00002251 ConsumeToken();
2252 continue;
2253 }
Chris Lattnere1359422008-04-10 06:46:29 +00002254
2255 // Parse all the comma separated declarators.
John McCall0b7e6782011-03-24 11:26:52 +00002256 DeclSpec DS(AttrFactory);
Mike Stump1eb44332009-09-09 15:08:12 +00002257
John McCallbdd563e2009-11-03 02:38:08 +00002258 if (!Tok.is(tok::at)) {
2259 struct CFieldCallback : FieldCallback {
2260 Parser &P;
John McCalld226f652010-08-21 09:40:31 +00002261 Decl *TagDecl;
2262 llvm::SmallVectorImpl<Decl *> &FieldDecls;
John McCallbdd563e2009-11-03 02:38:08 +00002263
John McCalld226f652010-08-21 09:40:31 +00002264 CFieldCallback(Parser &P, Decl *TagDecl,
2265 llvm::SmallVectorImpl<Decl *> &FieldDecls) :
John McCallbdd563e2009-11-03 02:38:08 +00002266 P(P), TagDecl(TagDecl), FieldDecls(FieldDecls) {}
2267
John McCalld226f652010-08-21 09:40:31 +00002268 virtual Decl *invoke(FieldDeclarator &FD) {
John McCallbdd563e2009-11-03 02:38:08 +00002269 // Install the declarator into the current TagDecl.
John McCalld226f652010-08-21 09:40:31 +00002270 Decl *Field = P.Actions.ActOnField(P.getCurScope(), TagDecl,
John McCall4ba39712009-11-03 21:13:47 +00002271 FD.D.getDeclSpec().getSourceRange().getBegin(),
2272 FD.D, FD.BitfieldSize);
John McCallbdd563e2009-11-03 02:38:08 +00002273 FieldDecls.push_back(Field);
2274 return Field;
Douglas Gregor91a28862009-08-26 14:27:30 +00002275 }
John McCallbdd563e2009-11-03 02:38:08 +00002276 } Callback(*this, TagDecl, FieldDecls);
2277
2278 ParseStructDeclaration(DS, Callback);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002279 } else { // Handle @defs
2280 ConsumeToken();
2281 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
2282 Diag(Tok, diag::err_unexpected_at);
Chris Lattner3e156ad2010-02-02 00:37:27 +00002283 SkipUntil(tok::semi, true);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002284 continue;
2285 }
2286 ConsumeToken();
2287 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
2288 if (!Tok.is(tok::identifier)) {
2289 Diag(Tok, diag::err_expected_ident);
Chris Lattner3e156ad2010-02-02 00:37:27 +00002290 SkipUntil(tok::semi, true);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002291 continue;
2292 }
John McCalld226f652010-08-21 09:40:31 +00002293 llvm::SmallVector<Decl *, 16> Fields;
Douglas Gregor23c94db2010-07-02 17:43:08 +00002294 Actions.ActOnDefs(getCurScope(), TagDecl, Tok.getLocation(),
Douglas Gregor44b43212008-12-11 16:49:14 +00002295 Tok.getIdentifierInfo(), Fields);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002296 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
2297 ConsumeToken();
2298 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
Mike Stump1eb44332009-09-09 15:08:12 +00002299 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002300
Chris Lattner04d66662007-10-09 17:33:22 +00002301 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002302 ConsumeToken();
Chris Lattner04d66662007-10-09 17:33:22 +00002303 } else if (Tok.is(tok::r_brace)) {
Chris Lattner3e156ad2010-02-02 00:37:27 +00002304 ExpectAndConsume(tok::semi, diag::ext_expected_semi_decl_list);
Reid Spencer5f016e22007-07-11 17:01:13 +00002305 break;
2306 } else {
Chris Lattner3e156ad2010-02-02 00:37:27 +00002307 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
2308 // Skip to end of block or statement to avoid ext-warning on extra ';'.
Reid Spencer5f016e22007-07-11 17:01:13 +00002309 SkipUntil(tok::r_brace, true, true);
Chris Lattner3e156ad2010-02-02 00:37:27 +00002310 // If we stopped at a ';', eat it.
2311 if (Tok.is(tok::semi)) ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00002312 }
2313 }
Mike Stump1eb44332009-09-09 15:08:12 +00002314
Steve Naroff60fccee2007-10-29 21:38:07 +00002315 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00002316
John McCall0b7e6782011-03-24 11:26:52 +00002317 ParsedAttributes attrs(AttrFactory);
Reid Spencer5f016e22007-07-11 17:01:13 +00002318 // If attributes exist after struct contents, parse them.
John McCall7f040a92010-12-24 02:08:15 +00002319 MaybeParseGNUAttributes(attrs);
Daniel Dunbar1bfe1c22008-10-03 02:03:53 +00002320
Douglas Gregor23c94db2010-07-02 17:43:08 +00002321 Actions.ActOnFields(getCurScope(),
Jay Foadbeaaccd2009-05-21 09:52:38 +00002322 RecordLoc, TagDecl, FieldDecls.data(), FieldDecls.size(),
Daniel Dunbar1bfe1c22008-10-03 02:03:53 +00002323 LBraceLoc, RBraceLoc,
John McCall7f040a92010-12-24 02:08:15 +00002324 attrs.getList());
Douglas Gregor72de6672009-01-08 20:45:30 +00002325 StructScope.Exit();
Douglas Gregor23c94db2010-07-02 17:43:08 +00002326 Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl, RBraceLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00002327}
2328
Reid Spencer5f016e22007-07-11 17:01:13 +00002329/// ParseEnumSpecifier
2330/// enum-specifier: [C99 6.7.2.2]
2331/// 'enum' identifier[opt] '{' enumerator-list '}'
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002332///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Reid Spencer5f016e22007-07-11 17:01:13 +00002333/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
2334/// '}' attributes[opt]
2335/// 'enum' identifier
2336/// [GNU] 'enum' attributes[opt] identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002337///
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002338/// [C++0x] enum-head '{' enumerator-list[opt] '}'
2339/// [C++0x] enum-head '{' enumerator-list ',' '}'
2340///
2341/// enum-head: [C++0x]
2342/// enum-key attributes[opt] identifier[opt] enum-base[opt]
2343/// enum-key attributes[opt] nested-name-specifier identifier enum-base[opt]
2344///
2345/// enum-key: [C++0x]
2346/// 'enum'
2347/// 'enum' 'class'
2348/// 'enum' 'struct'
2349///
2350/// enum-base: [C++0x]
2351/// ':' type-specifier-seq
2352///
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002353/// [C++] elaborated-type-specifier:
2354/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
2355///
Chris Lattner4c97d762009-04-12 21:49:30 +00002356void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregor9b9edd62010-03-02 17:53:14 +00002357 const ParsedTemplateInfo &TemplateInfo,
Chris Lattner4c97d762009-04-12 21:49:30 +00002358 AccessSpecifier AS) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002359 // Parse the tag portion of this.
Douglas Gregor374929f2009-09-18 15:37:17 +00002360 if (Tok.is(tok::code_completion)) {
2361 // Code completion for an enum name.
Douglas Gregor23c94db2010-07-02 17:43:08 +00002362 Actions.CodeCompleteTag(getCurScope(), DeclSpec::TST_enum);
Douglas Gregordc845342010-05-25 05:58:43 +00002363 ConsumeCodeCompletionToken();
Douglas Gregor374929f2009-09-18 15:37:17 +00002364 }
2365
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002366 // If attributes exist after tag, parse them.
John McCall0b7e6782011-03-24 11:26:52 +00002367 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00002368 MaybeParseGNUAttributes(attrs);
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002369
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00002370 CXXScopeSpec &SS = DS.getTypeSpecScope();
John McCall9ba61662010-02-26 08:45:28 +00002371 if (getLang().CPlusPlus) {
John McCallb3d87482010-08-24 05:47:05 +00002372 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), false))
John McCall9ba61662010-02-26 08:45:28 +00002373 return;
2374
2375 if (SS.isSet() && Tok.isNot(tok::identifier)) {
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002376 Diag(Tok, diag::err_expected_ident);
2377 if (Tok.isNot(tok::l_brace)) {
2378 // Has no name and is not a definition.
2379 // Skip the rest of this declarator, up until the comma or semicolon.
2380 SkipUntil(tok::comma, true);
2381 return;
2382 }
2383 }
2384 }
Mike Stump1eb44332009-09-09 15:08:12 +00002385
Douglas Gregor86f208c2011-02-22 20:32:04 +00002386 bool AllowFixedUnderlyingType = getLang().CPlusPlus0x || getLang().Microsoft;
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002387 bool IsScopedEnum = false;
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00002388 bool IsScopedUsingClassTag = false;
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002389
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00002390 if (getLang().CPlusPlus0x &&
2391 (Tok.is(tok::kw_class) || Tok.is(tok::kw_struct))) {
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002392 IsScopedEnum = true;
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00002393 IsScopedUsingClassTag = Tok.is(tok::kw_class);
2394 ConsumeToken();
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002395 }
2396
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002397 // Must have either 'enum name' or 'enum {...}'.
Douglas Gregorb9075602011-02-22 02:55:24 +00002398 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace) &&
2399 (AllowFixedUnderlyingType && Tok.isNot(tok::colon))) {
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002400 Diag(Tok, diag::err_expected_ident_lbrace);
Mike Stump1eb44332009-09-09 15:08:12 +00002401
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002402 // Skip the rest of this declarator, up until the comma or semicolon.
2403 SkipUntil(tok::comma, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00002404 return;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002405 }
Mike Stump1eb44332009-09-09 15:08:12 +00002406
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002407 // If an identifier is present, consume and remember it.
2408 IdentifierInfo *Name = 0;
2409 SourceLocation NameLoc;
2410 if (Tok.is(tok::identifier)) {
2411 Name = Tok.getIdentifierInfo();
2412 NameLoc = ConsumeToken();
2413 }
Mike Stump1eb44332009-09-09 15:08:12 +00002414
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002415 if (!Name && IsScopedEnum) {
2416 // C++0x 7.2p2: The optional identifier shall not be omitted in the
2417 // declaration of a scoped enumeration.
2418 Diag(Tok, diag::err_scoped_enum_missing_identifier);
2419 IsScopedEnum = false;
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00002420 IsScopedUsingClassTag = false;
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002421 }
2422
2423 TypeResult BaseType;
2424
Douglas Gregora61b3e72010-12-01 17:42:47 +00002425 // Parse the fixed underlying type.
Douglas Gregorb9075602011-02-22 02:55:24 +00002426 if (AllowFixedUnderlyingType && Tok.is(tok::colon)) {
Douglas Gregora61b3e72010-12-01 17:42:47 +00002427 bool PossibleBitfield = false;
2428 if (getCurScope()->getFlags() & Scope::ClassScope) {
2429 // If we're in class scope, this can either be an enum declaration with
2430 // an underlying type, or a declaration of a bitfield member. We try to
2431 // use a simple disambiguation scheme first to catch the common cases
2432 // (integer literal, sizeof); if it's still ambiguous, we then consider
2433 // anything that's a simple-type-specifier followed by '(' as an
2434 // expression. This suffices because function types are not valid
2435 // underlying types anyway.
2436 TPResult TPR = isExpressionOrTypeSpecifierSimple(NextToken().getKind());
2437 // If the next token starts an expression, we know we're parsing a
2438 // bit-field. This is the common case.
2439 if (TPR == TPResult::True())
2440 PossibleBitfield = true;
2441 // If the next token starts a type-specifier-seq, it may be either a
2442 // a fixed underlying type or the start of a function-style cast in C++;
2443 // lookahead one more token to see if it's obvious that we have a
2444 // fixed underlying type.
2445 else if (TPR == TPResult::False() &&
2446 GetLookAheadToken(2).getKind() == tok::semi) {
2447 // Consume the ':'.
2448 ConsumeToken();
2449 } else {
2450 // We have the start of a type-specifier-seq, so we have to perform
2451 // tentative parsing to determine whether we have an expression or a
2452 // type.
2453 TentativeParsingAction TPA(*this);
2454
2455 // Consume the ':'.
2456 ConsumeToken();
2457
Douglas Gregor86f208c2011-02-22 20:32:04 +00002458 if ((getLang().CPlusPlus &&
2459 isCXXDeclarationSpecifier() != TPResult::True()) ||
2460 (!getLang().CPlusPlus && !isDeclarationSpecifier(true))) {
Douglas Gregora61b3e72010-12-01 17:42:47 +00002461 // We'll parse this as a bitfield later.
2462 PossibleBitfield = true;
2463 TPA.Revert();
2464 } else {
2465 // We have a type-specifier-seq.
2466 TPA.Commit();
2467 }
2468 }
2469 } else {
2470 // Consume the ':'.
2471 ConsumeToken();
2472 }
2473
2474 if (!PossibleBitfield) {
2475 SourceRange Range;
2476 BaseType = ParseTypeName(&Range);
Douglas Gregor86f208c2011-02-22 20:32:04 +00002477
2478 if (!getLang().CPlusPlus0x)
2479 Diag(StartLoc, diag::ext_ms_enum_fixed_underlying_type)
2480 << Range;
Douglas Gregora61b3e72010-12-01 17:42:47 +00002481 }
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002482 }
2483
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002484 // There are three options here. If we have 'enum foo;', then this is a
2485 // forward declaration. If we have 'enum foo {...' then this is a
2486 // definition. Otherwise we have something like 'enum foo xyz', a reference.
2487 //
2488 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
2489 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
2490 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
2491 //
John McCallf312b1e2010-08-26 23:41:50 +00002492 Sema::TagUseKind TUK;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002493 if (Tok.is(tok::l_brace))
John McCallf312b1e2010-08-26 23:41:50 +00002494 TUK = Sema::TUK_Definition;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002495 else if (Tok.is(tok::semi))
John McCallf312b1e2010-08-26 23:41:50 +00002496 TUK = Sema::TUK_Declaration;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002497 else
John McCallf312b1e2010-08-26 23:41:50 +00002498 TUK = Sema::TUK_Reference;
Douglas Gregor8fc6d232010-05-03 17:48:54 +00002499
2500 // enums cannot be templates, although they can be referenced from a
2501 // template.
2502 if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate &&
John McCallf312b1e2010-08-26 23:41:50 +00002503 TUK != Sema::TUK_Reference) {
Douglas Gregor8fc6d232010-05-03 17:48:54 +00002504 Diag(Tok, diag::err_enum_template);
2505
2506 // Skip the rest of this declarator, up until the comma or semicolon.
2507 SkipUntil(tok::comma, true);
2508 return;
2509 }
2510
Douglas Gregorb9075602011-02-22 02:55:24 +00002511 if (!Name && TUK != Sema::TUK_Definition) {
2512 Diag(Tok, diag::err_enumerator_unnamed_no_def);
2513
2514 // Skip the rest of this declarator, up until the comma or semicolon.
2515 SkipUntil(tok::comma, true);
2516 return;
2517 }
2518
Douglas Gregor402abb52009-05-28 23:31:59 +00002519 bool Owned = false;
John McCallc4e70192009-09-11 04:59:25 +00002520 bool IsDependent = false;
Douglas Gregor48c89f42010-04-24 16:38:41 +00002521 const char *PrevSpec = 0;
2522 unsigned DiagID;
John McCalld226f652010-08-21 09:40:31 +00002523 Decl *TagDecl = Actions.ActOnTag(getCurScope(), DeclSpec::TST_enum, TUK,
John McCall7f040a92010-12-24 02:08:15 +00002524 StartLoc, SS, Name, NameLoc, attrs.getList(),
John McCalld226f652010-08-21 09:40:31 +00002525 AS,
John McCallf312b1e2010-08-26 23:41:50 +00002526 MultiTemplateParamsArg(Actions),
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002527 Owned, IsDependent, IsScopedEnum,
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00002528 IsScopedUsingClassTag, BaseType);
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002529
Douglas Gregor48c89f42010-04-24 16:38:41 +00002530 if (IsDependent) {
2531 // This enum has a dependent nested-name-specifier. Handle it as a
2532 // dependent tag.
2533 if (!Name) {
2534 DS.SetTypeSpecError();
2535 Diag(Tok, diag::err_expected_type_name_after_typename);
2536 return;
2537 }
2538
Douglas Gregor23c94db2010-07-02 17:43:08 +00002539 TypeResult Type = Actions.ActOnDependentTag(getCurScope(), DeclSpec::TST_enum,
Douglas Gregor48c89f42010-04-24 16:38:41 +00002540 TUK, SS, Name, StartLoc,
2541 NameLoc);
2542 if (Type.isInvalid()) {
2543 DS.SetTypeSpecError();
2544 return;
2545 }
2546
Abramo Bagnara0daaf322011-03-16 20:16:18 +00002547 if (DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc,
2548 NameLoc.isValid() ? NameLoc : StartLoc,
2549 PrevSpec, DiagID, Type.get()))
Douglas Gregor48c89f42010-04-24 16:38:41 +00002550 Diag(StartLoc, DiagID) << PrevSpec;
2551
2552 return;
2553 }
Mike Stump1eb44332009-09-09 15:08:12 +00002554
John McCalld226f652010-08-21 09:40:31 +00002555 if (!TagDecl) {
Douglas Gregor48c89f42010-04-24 16:38:41 +00002556 // The action failed to produce an enumeration tag. If this is a
2557 // definition, consume the entire definition.
2558 if (Tok.is(tok::l_brace)) {
2559 ConsumeBrace();
2560 SkipUntil(tok::r_brace);
2561 }
2562
2563 DS.SetTypeSpecError();
2564 return;
2565 }
2566
Chris Lattner04d66662007-10-09 17:33:22 +00002567 if (Tok.is(tok::l_brace))
Reid Spencer5f016e22007-07-11 17:01:13 +00002568 ParseEnumBody(StartLoc, TagDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00002569
Abramo Bagnara0daaf322011-03-16 20:16:18 +00002570 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc,
2571 NameLoc.isValid() ? NameLoc : StartLoc,
2572 PrevSpec, DiagID, TagDecl, Owned))
John McCallfec54012009-08-03 20:12:06 +00002573 Diag(StartLoc, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00002574}
2575
2576/// ParseEnumBody - Parse a {} enclosed enumerator-list.
2577/// enumerator-list:
2578/// enumerator
2579/// enumerator-list ',' enumerator
2580/// enumerator:
2581/// enumeration-constant
2582/// enumeration-constant '=' constant-expression
2583/// enumeration-constant:
2584/// identifier
2585///
John McCalld226f652010-08-21 09:40:31 +00002586void Parser::ParseEnumBody(SourceLocation StartLoc, Decl *EnumDecl) {
Douglas Gregor074149e2009-01-05 19:45:36 +00002587 // Enter the scope of the enum body and start the definition.
2588 ParseScope EnumScope(this, Scope::DeclScope);
Douglas Gregor23c94db2010-07-02 17:43:08 +00002589 Actions.ActOnTagStartDefinition(getCurScope(), EnumDecl);
Douglas Gregor074149e2009-01-05 19:45:36 +00002590
Reid Spencer5f016e22007-07-11 17:01:13 +00002591 SourceLocation LBraceLoc = ConsumeBrace();
Mike Stump1eb44332009-09-09 15:08:12 +00002592
Chris Lattner7946dd32007-08-27 17:24:30 +00002593 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
Chris Lattner04d66662007-10-09 17:33:22 +00002594 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Fariborz Jahanian05115522010-05-28 22:23:22 +00002595 Diag(Tok, diag::error_empty_enum);
Mike Stump1eb44332009-09-09 15:08:12 +00002596
John McCalld226f652010-08-21 09:40:31 +00002597 llvm::SmallVector<Decl *, 32> EnumConstantDecls;
Reid Spencer5f016e22007-07-11 17:01:13 +00002598
John McCalld226f652010-08-21 09:40:31 +00002599 Decl *LastEnumConstDecl = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00002600
Reid Spencer5f016e22007-07-11 17:01:13 +00002601 // Parse the enumerator-list.
Chris Lattner04d66662007-10-09 17:33:22 +00002602 while (Tok.is(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002603 IdentifierInfo *Ident = Tok.getIdentifierInfo();
2604 SourceLocation IdentLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00002605
John McCall5b629aa2010-10-22 23:36:17 +00002606 // If attributes exist after the enumerator, parse them.
John McCall0b7e6782011-03-24 11:26:52 +00002607 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00002608 MaybeParseGNUAttributes(attrs);
John McCall5b629aa2010-10-22 23:36:17 +00002609
Reid Spencer5f016e22007-07-11 17:01:13 +00002610 SourceLocation EqualLoc;
John McCall60d7b3a2010-08-24 06:29:42 +00002611 ExprResult AssignedVal;
Chris Lattner04d66662007-10-09 17:33:22 +00002612 if (Tok.is(tok::equal)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002613 EqualLoc = ConsumeToken();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002614 AssignedVal = ParseConstantExpression();
2615 if (AssignedVal.isInvalid())
Reid Spencer5f016e22007-07-11 17:01:13 +00002616 SkipUntil(tok::comma, tok::r_brace, true, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00002617 }
Mike Stump1eb44332009-09-09 15:08:12 +00002618
Reid Spencer5f016e22007-07-11 17:01:13 +00002619 // Install the enumerator constant into EnumDecl.
John McCalld226f652010-08-21 09:40:31 +00002620 Decl *EnumConstDecl = Actions.ActOnEnumConstant(getCurScope(), EnumDecl,
2621 LastEnumConstDecl,
2622 IdentLoc, Ident,
John McCall7f040a92010-12-24 02:08:15 +00002623 attrs.getList(), EqualLoc,
John McCalld226f652010-08-21 09:40:31 +00002624 AssignedVal.release());
Reid Spencer5f016e22007-07-11 17:01:13 +00002625 EnumConstantDecls.push_back(EnumConstDecl);
2626 LastEnumConstDecl = EnumConstDecl;
Mike Stump1eb44332009-09-09 15:08:12 +00002627
Douglas Gregor751f6922010-09-07 14:51:08 +00002628 if (Tok.is(tok::identifier)) {
2629 // We're missing a comma between enumerators.
2630 SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation);
2631 Diag(Loc, diag::err_enumerator_list_missing_comma)
2632 << FixItHint::CreateInsertion(Loc, ", ");
2633 continue;
2634 }
2635
Chris Lattner04d66662007-10-09 17:33:22 +00002636 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +00002637 break;
2638 SourceLocation CommaLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00002639
2640 if (Tok.isNot(tok::identifier) &&
Douglas Gregor9b3064b2009-04-01 22:41:11 +00002641 !(getLang().C99 || getLang().CPlusPlus0x))
2642 Diag(CommaLoc, diag::ext_enumerator_list_comma)
2643 << getLang().CPlusPlus
Douglas Gregor849b2432010-03-31 17:46:05 +00002644 << FixItHint::CreateRemoval(CommaLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00002645 }
Mike Stump1eb44332009-09-09 15:08:12 +00002646
Reid Spencer5f016e22007-07-11 17:01:13 +00002647 // Eat the }.
Mike Stumpc6e35aa2009-05-16 07:06:02 +00002648 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00002649
Reid Spencer5f016e22007-07-11 17:01:13 +00002650 // If attributes exist after the identifier list, parse them.
John McCall0b7e6782011-03-24 11:26:52 +00002651 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00002652 MaybeParseGNUAttributes(attrs);
Douglas Gregor72de6672009-01-08 20:45:30 +00002653
Edward O'Callaghanfee13812009-08-08 14:36:57 +00002654 Actions.ActOnEnumBody(StartLoc, LBraceLoc, RBraceLoc, EnumDecl,
2655 EnumConstantDecls.data(), EnumConstantDecls.size(),
John McCall7f040a92010-12-24 02:08:15 +00002656 getCurScope(), attrs.getList());
Mike Stump1eb44332009-09-09 15:08:12 +00002657
Douglas Gregor72de6672009-01-08 20:45:30 +00002658 EnumScope.Exit();
Douglas Gregor23c94db2010-07-02 17:43:08 +00002659 Actions.ActOnTagFinishDefinition(getCurScope(), EnumDecl, RBraceLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00002660}
2661
2662/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff5f8aa692008-02-11 23:15:56 +00002663/// start of a type-qualifier-list.
2664bool Parser::isTypeQualifier() const {
2665 switch (Tok.getKind()) {
2666 default: return false;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00002667
2668 // type-qualifier only in OpenCL
2669 case tok::kw_private:
2670 return getLang().OpenCL;
2671
Steve Naroff5f8aa692008-02-11 23:15:56 +00002672 // type-qualifier
2673 case tok::kw_const:
2674 case tok::kw_volatile:
2675 case tok::kw_restrict:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00002676 case tok::kw___private:
2677 case tok::kw___local:
2678 case tok::kw___global:
2679 case tok::kw___constant:
2680 case tok::kw___read_only:
2681 case tok::kw___read_write:
2682 case tok::kw___write_only:
Steve Naroff5f8aa692008-02-11 23:15:56 +00002683 return true;
2684 }
2685}
2686
Chris Lattnerb3a4e432010-02-28 18:18:36 +00002687/// isKnownToBeTypeSpecifier - Return true if we know that the specified token
2688/// is definitely a type-specifier. Return false if it isn't part of a type
2689/// specifier or if we're not sure.
2690bool Parser::isKnownToBeTypeSpecifier(const Token &Tok) const {
2691 switch (Tok.getKind()) {
2692 default: return false;
2693 // type-specifiers
2694 case tok::kw_short:
2695 case tok::kw_long:
2696 case tok::kw_signed:
2697 case tok::kw_unsigned:
2698 case tok::kw__Complex:
2699 case tok::kw__Imaginary:
2700 case tok::kw_void:
2701 case tok::kw_char:
2702 case tok::kw_wchar_t:
2703 case tok::kw_char16_t:
2704 case tok::kw_char32_t:
2705 case tok::kw_int:
2706 case tok::kw_float:
2707 case tok::kw_double:
2708 case tok::kw_bool:
2709 case tok::kw__Bool:
2710 case tok::kw__Decimal32:
2711 case tok::kw__Decimal64:
2712 case tok::kw__Decimal128:
2713 case tok::kw___vector:
2714
2715 // struct-or-union-specifier (C99) or class-specifier (C++)
2716 case tok::kw_class:
2717 case tok::kw_struct:
2718 case tok::kw_union:
2719 // enum-specifier
2720 case tok::kw_enum:
2721
2722 // typedef-name
2723 case tok::annot_typename:
2724 return true;
2725 }
2726}
2727
Steve Naroff5f8aa692008-02-11 23:15:56 +00002728/// isTypeSpecifierQualifier - Return true if the current token could be the
Reid Spencer5f016e22007-07-11 17:01:13 +00002729/// start of a specifier-qualifier-list.
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002730bool Parser::isTypeSpecifierQualifier() {
Reid Spencer5f016e22007-07-11 17:01:13 +00002731 switch (Tok.getKind()) {
2732 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002733
Chris Lattner166a8fc2009-01-04 23:41:41 +00002734 case tok::identifier: // foo::bar
John Thompson82287d12010-02-05 00:12:22 +00002735 if (TryAltiVecVectorToken())
2736 return true;
2737 // Fall through.
Douglas Gregord57959a2009-03-27 23:10:48 +00002738 case tok::kw_typename: // typename T::type
Chris Lattner166a8fc2009-01-04 23:41:41 +00002739 // Annotate typenames and C++ scope specifiers. If we get one, just
2740 // recurse to handle whatever we get.
2741 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00002742 return true;
2743 if (Tok.is(tok::identifier))
2744 return false;
2745 return isTypeSpecifierQualifier();
Douglas Gregord57959a2009-03-27 23:10:48 +00002746
Chris Lattner166a8fc2009-01-04 23:41:41 +00002747 case tok::coloncolon: // ::foo::bar
2748 if (NextToken().is(tok::kw_new) || // ::new
2749 NextToken().is(tok::kw_delete)) // ::delete
2750 return false;
2751
Chris Lattner166a8fc2009-01-04 23:41:41 +00002752 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00002753 return true;
2754 return isTypeSpecifierQualifier();
Mike Stump1eb44332009-09-09 15:08:12 +00002755
Reid Spencer5f016e22007-07-11 17:01:13 +00002756 // GNU attributes support.
2757 case tok::kw___attribute:
Steve Naroffd1861fd2007-07-31 12:34:36 +00002758 // GNU typeof support.
2759 case tok::kw_typeof:
Mike Stump1eb44332009-09-09 15:08:12 +00002760
Reid Spencer5f016e22007-07-11 17:01:13 +00002761 // type-specifiers
2762 case tok::kw_short:
2763 case tok::kw_long:
2764 case tok::kw_signed:
2765 case tok::kw_unsigned:
2766 case tok::kw__Complex:
2767 case tok::kw__Imaginary:
2768 case tok::kw_void:
2769 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00002770 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002771 case tok::kw_char16_t:
2772 case tok::kw_char32_t:
Reid Spencer5f016e22007-07-11 17:01:13 +00002773 case tok::kw_int:
2774 case tok::kw_float:
2775 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00002776 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00002777 case tok::kw__Bool:
2778 case tok::kw__Decimal32:
2779 case tok::kw__Decimal64:
2780 case tok::kw__Decimal128:
John Thompson82287d12010-02-05 00:12:22 +00002781 case tok::kw___vector:
Mike Stump1eb44332009-09-09 15:08:12 +00002782
Chris Lattner99dc9142008-04-13 18:59:07 +00002783 // struct-or-union-specifier (C99) or class-specifier (C++)
2784 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00002785 case tok::kw_struct:
2786 case tok::kw_union:
2787 // enum-specifier
2788 case tok::kw_enum:
Mike Stump1eb44332009-09-09 15:08:12 +00002789
Reid Spencer5f016e22007-07-11 17:01:13 +00002790 // type-qualifier
2791 case tok::kw_const:
2792 case tok::kw_volatile:
2793 case tok::kw_restrict:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002794
2795 // typedef-name
Chris Lattnerb31757b2009-01-06 05:06:21 +00002796 case tok::annot_typename:
Reid Spencer5f016e22007-07-11 17:01:13 +00002797 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00002798
Chris Lattner7c186be2008-10-20 00:25:30 +00002799 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
2800 case tok::less:
2801 return getLang().ObjC1;
Mike Stump1eb44332009-09-09 15:08:12 +00002802
Steve Naroff239f0732008-12-25 14:16:32 +00002803 case tok::kw___cdecl:
2804 case tok::kw___stdcall:
2805 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00002806 case tok::kw___thiscall:
Eli Friedman290eeb02009-06-08 23:27:34 +00002807 case tok::kw___w64:
2808 case tok::kw___ptr64:
Dawn Perchik52fc3142010-09-03 01:29:35 +00002809 case tok::kw___pascal:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00002810
2811 case tok::kw___private:
2812 case tok::kw___local:
2813 case tok::kw___global:
2814 case tok::kw___constant:
2815 case tok::kw___read_only:
2816 case tok::kw___read_write:
2817 case tok::kw___write_only:
2818
Eli Friedman290eeb02009-06-08 23:27:34 +00002819 return true;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00002820
2821 case tok::kw_private:
2822 return getLang().OpenCL;
Reid Spencer5f016e22007-07-11 17:01:13 +00002823 }
2824}
2825
2826/// isDeclarationSpecifier() - Return true if the current token is part of a
2827/// declaration specifier.
Douglas Gregor9497a732010-09-16 01:51:54 +00002828///
2829/// \param DisambiguatingWithExpression True to indicate that the purpose of
2830/// this check is to disambiguate between an expression and a declaration.
2831bool Parser::isDeclarationSpecifier(bool DisambiguatingWithExpression) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002832 switch (Tok.getKind()) {
2833 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002834
Peter Collingbourne207f4d82011-03-18 22:38:29 +00002835 case tok::kw_private:
2836 return getLang().OpenCL;
2837
Chris Lattner166a8fc2009-01-04 23:41:41 +00002838 case tok::identifier: // foo::bar
Steve Naroff61f72cb2009-03-09 21:12:44 +00002839 // Unfortunate hack to support "Class.factoryMethod" notation.
2840 if (getLang().ObjC1 && NextToken().is(tok::period))
2841 return false;
John Thompson82287d12010-02-05 00:12:22 +00002842 if (TryAltiVecVectorToken())
2843 return true;
2844 // Fall through.
Douglas Gregord57959a2009-03-27 23:10:48 +00002845 case tok::kw_typename: // typename T::type
Chris Lattner166a8fc2009-01-04 23:41:41 +00002846 // Annotate typenames and C++ scope specifiers. If we get one, just
2847 // recurse to handle whatever we get.
2848 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00002849 return true;
2850 if (Tok.is(tok::identifier))
2851 return false;
Douglas Gregor9497a732010-09-16 01:51:54 +00002852
2853 // If we're in Objective-C and we have an Objective-C class type followed
2854 // by an identifier and then either ':' or ']', in a place where an
2855 // expression is permitted, then this is probably a class message send
2856 // missing the initial '['. In this case, we won't consider this to be
2857 // the start of a declaration.
2858 if (DisambiguatingWithExpression &&
2859 isStartOfObjCClassMessageMissingOpenBracket())
2860 return false;
2861
John McCall9ba61662010-02-26 08:45:28 +00002862 return isDeclarationSpecifier();
2863
Chris Lattner166a8fc2009-01-04 23:41:41 +00002864 case tok::coloncolon: // ::foo::bar
2865 if (NextToken().is(tok::kw_new) || // ::new
2866 NextToken().is(tok::kw_delete)) // ::delete
2867 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002868
Chris Lattner166a8fc2009-01-04 23:41:41 +00002869 // Annotate typenames and C++ scope specifiers. If we get one, just
2870 // recurse to handle whatever we get.
2871 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00002872 return true;
2873 return isDeclarationSpecifier();
Mike Stump1eb44332009-09-09 15:08:12 +00002874
Reid Spencer5f016e22007-07-11 17:01:13 +00002875 // storage-class-specifier
2876 case tok::kw_typedef:
2877 case tok::kw_extern:
Steve Naroff8d54bf22007-12-18 00:16:02 +00002878 case tok::kw___private_extern__:
Reid Spencer5f016e22007-07-11 17:01:13 +00002879 case tok::kw_static:
2880 case tok::kw_auto:
2881 case tok::kw_register:
2882 case tok::kw___thread:
Mike Stump1eb44332009-09-09 15:08:12 +00002883
Reid Spencer5f016e22007-07-11 17:01:13 +00002884 // type-specifiers
2885 case tok::kw_short:
2886 case tok::kw_long:
2887 case tok::kw_signed:
2888 case tok::kw_unsigned:
2889 case tok::kw__Complex:
2890 case tok::kw__Imaginary:
2891 case tok::kw_void:
2892 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00002893 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002894 case tok::kw_char16_t:
2895 case tok::kw_char32_t:
2896
Reid Spencer5f016e22007-07-11 17:01:13 +00002897 case tok::kw_int:
2898 case tok::kw_float:
2899 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00002900 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00002901 case tok::kw__Bool:
2902 case tok::kw__Decimal32:
2903 case tok::kw__Decimal64:
2904 case tok::kw__Decimal128:
John Thompson82287d12010-02-05 00:12:22 +00002905 case tok::kw___vector:
Mike Stump1eb44332009-09-09 15:08:12 +00002906
Chris Lattner99dc9142008-04-13 18:59:07 +00002907 // struct-or-union-specifier (C99) or class-specifier (C++)
2908 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00002909 case tok::kw_struct:
2910 case tok::kw_union:
2911 // enum-specifier
2912 case tok::kw_enum:
Mike Stump1eb44332009-09-09 15:08:12 +00002913
Reid Spencer5f016e22007-07-11 17:01:13 +00002914 // type-qualifier
2915 case tok::kw_const:
2916 case tok::kw_volatile:
2917 case tok::kw_restrict:
Steve Naroffd1861fd2007-07-31 12:34:36 +00002918
Reid Spencer5f016e22007-07-11 17:01:13 +00002919 // function-specifier
2920 case tok::kw_inline:
Douglas Gregorb48fe382008-10-31 09:07:45 +00002921 case tok::kw_virtual:
2922 case tok::kw_explicit:
Chris Lattnerd6c7c182007-08-09 16:40:21 +00002923
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002924 // typedef-name
Chris Lattnerb31757b2009-01-06 05:06:21 +00002925 case tok::annot_typename:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002926
Peter Collingbournec6eb44b2011-04-15 00:35:57 +00002927 // static_assert-declaration
2928 case tok::kw__Static_assert:
2929
Chris Lattner1ef08762007-08-09 17:01:07 +00002930 // GNU typeof support.
2931 case tok::kw_typeof:
Mike Stump1eb44332009-09-09 15:08:12 +00002932
Chris Lattner1ef08762007-08-09 17:01:07 +00002933 // GNU attributes.
Chris Lattnerd6c7c182007-08-09 16:40:21 +00002934 case tok::kw___attribute:
Reid Spencer5f016e22007-07-11 17:01:13 +00002935 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00002936
Chris Lattnerf3948c42008-07-26 03:38:44 +00002937 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
2938 case tok::less:
2939 return getLang().ObjC1;
Mike Stump1eb44332009-09-09 15:08:12 +00002940
Steve Naroff47f52092009-01-06 19:34:12 +00002941 case tok::kw___declspec:
Steve Naroff239f0732008-12-25 14:16:32 +00002942 case tok::kw___cdecl:
2943 case tok::kw___stdcall:
2944 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00002945 case tok::kw___thiscall:
Eli Friedman290eeb02009-06-08 23:27:34 +00002946 case tok::kw___w64:
2947 case tok::kw___ptr64:
2948 case tok::kw___forceinline:
Dawn Perchik52fc3142010-09-03 01:29:35 +00002949 case tok::kw___pascal:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00002950
2951 case tok::kw___private:
2952 case tok::kw___local:
2953 case tok::kw___global:
2954 case tok::kw___constant:
2955 case tok::kw___read_only:
2956 case tok::kw___read_write:
2957 case tok::kw___write_only:
2958
Eli Friedman290eeb02009-06-08 23:27:34 +00002959 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00002960 }
2961}
2962
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002963bool Parser::isConstructorDeclarator() {
2964 TentativeParsingAction TPA(*this);
2965
2966 // Parse the C++ scope specifier.
2967 CXXScopeSpec SS;
John McCallb3d87482010-08-24 05:47:05 +00002968 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), true)) {
John McCall9ba61662010-02-26 08:45:28 +00002969 TPA.Revert();
2970 return false;
2971 }
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002972
2973 // Parse the constructor name.
2974 if (Tok.is(tok::identifier) || Tok.is(tok::annot_template_id)) {
2975 // We already know that we have a constructor name; just consume
2976 // the token.
2977 ConsumeToken();
2978 } else {
2979 TPA.Revert();
2980 return false;
2981 }
2982
2983 // Current class name must be followed by a left parentheses.
2984 if (Tok.isNot(tok::l_paren)) {
2985 TPA.Revert();
2986 return false;
2987 }
2988 ConsumeParen();
2989
2990 // A right parentheses or ellipsis signals that we have a constructor.
2991 if (Tok.is(tok::r_paren) || Tok.is(tok::ellipsis)) {
2992 TPA.Revert();
2993 return true;
2994 }
2995
2996 // If we need to, enter the specified scope.
2997 DeclaratorScopeObj DeclScopeObj(*this, SS);
Douglas Gregor23c94db2010-07-02 17:43:08 +00002998 if (SS.isSet() && Actions.ShouldEnterDeclaratorScope(getCurScope(), SS))
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002999 DeclScopeObj.EnterDeclaratorScope();
3000
Francois Pichetdfaa5fb2011-01-31 04:54:32 +00003001 // Optionally skip Microsoft attributes.
John McCall0b7e6782011-03-24 11:26:52 +00003002 ParsedAttributes Attrs(AttrFactory);
Francois Pichetdfaa5fb2011-01-31 04:54:32 +00003003 MaybeParseMicrosoftAttributes(Attrs);
3004
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003005 // Check whether the next token(s) are part of a declaration
3006 // specifier, in which case we have the start of a parameter and,
3007 // therefore, we know that this is a constructor.
3008 bool IsConstructor = isDeclarationSpecifier();
3009 TPA.Revert();
3010 return IsConstructor;
3011}
Reid Spencer5f016e22007-07-11 17:01:13 +00003012
3013/// ParseTypeQualifierListOpt
Dawn Perchik52fc3142010-09-03 01:29:35 +00003014/// type-qualifier-list: [C99 6.7.5]
3015/// type-qualifier
3016/// [vendor] attributes
3017/// [ only if VendorAttributesAllowed=true ]
3018/// type-qualifier-list type-qualifier
3019/// [vendor] type-qualifier-list attributes
3020/// [ only if VendorAttributesAllowed=true ]
3021/// [C++0x] attribute-specifier[opt] is allowed before cv-qualifier-seq
3022/// [ only if CXX0XAttributesAllowed=true ]
3023/// Note: vendor can be GNU, MS, etc.
Reid Spencer5f016e22007-07-11 17:01:13 +00003024///
Dawn Perchik52fc3142010-09-03 01:29:35 +00003025void Parser::ParseTypeQualifierListOpt(DeclSpec &DS,
3026 bool VendorAttributesAllowed,
Sean Huntbbd37c62009-11-21 08:43:09 +00003027 bool CXX0XAttributesAllowed) {
3028 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier()) {
3029 SourceLocation Loc = Tok.getLocation();
John McCall0b7e6782011-03-24 11:26:52 +00003030 ParsedAttributesWithRange attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00003031 ParseCXX0XAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00003032 if (CXX0XAttributesAllowed)
John McCall7f040a92010-12-24 02:08:15 +00003033 DS.takeAttributesFrom(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00003034 else
3035 Diag(Loc, diag::err_attributes_not_allowed);
3036 }
Abramo Bagnara796aa442011-03-12 11:17:06 +00003037
3038 SourceLocation EndLoc;
3039
Reid Spencer5f016e22007-07-11 17:01:13 +00003040 while (1) {
John McCallfec54012009-08-03 20:12:06 +00003041 bool isInvalid = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00003042 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00003043 unsigned DiagID = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00003044 SourceLocation Loc = Tok.getLocation();
3045
3046 switch (Tok.getKind()) {
Douglas Gregor1a480c42010-08-27 17:35:51 +00003047 case tok::code_completion:
3048 Actions.CodeCompleteTypeQualifiers(DS);
3049 ConsumeCodeCompletionToken();
3050 break;
3051
Reid Spencer5f016e22007-07-11 17:01:13 +00003052 case tok::kw_const:
John McCallfec54012009-08-03 20:12:06 +00003053 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, DiagID,
3054 getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00003055 break;
3056 case tok::kw_volatile:
John McCallfec54012009-08-03 20:12:06 +00003057 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
3058 getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00003059 break;
3060 case tok::kw_restrict:
John McCallfec54012009-08-03 20:12:06 +00003061 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
3062 getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00003063 break;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003064
3065 // OpenCL qualifiers:
3066 case tok::kw_private:
3067 if (!getLang().OpenCL)
3068 goto DoneWithTypeQuals;
3069 case tok::kw___private:
3070 case tok::kw___global:
3071 case tok::kw___local:
3072 case tok::kw___constant:
3073 case tok::kw___read_only:
3074 case tok::kw___write_only:
3075 case tok::kw___read_write:
3076 ParseOpenCLQualifiers(DS);
3077 break;
3078
Eli Friedman290eeb02009-06-08 23:27:34 +00003079 case tok::kw___w64:
Steve Naroff86bc6cf2008-12-25 14:41:26 +00003080 case tok::kw___ptr64:
Steve Naroff239f0732008-12-25 14:16:32 +00003081 case tok::kw___cdecl:
3082 case tok::kw___stdcall:
3083 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00003084 case tok::kw___thiscall:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003085 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00003086 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman290eeb02009-06-08 23:27:34 +00003087 continue;
3088 }
3089 goto DoneWithTypeQuals;
Dawn Perchik52fc3142010-09-03 01:29:35 +00003090 case tok::kw___pascal:
3091 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00003092 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik52fc3142010-09-03 01:29:35 +00003093 continue;
3094 }
3095 goto DoneWithTypeQuals;
Reid Spencer5f016e22007-07-11 17:01:13 +00003096 case tok::kw___attribute:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003097 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00003098 ParseGNUAttributes(DS.getAttributes());
Chris Lattner5a69d1c2008-12-18 07:02:59 +00003099 continue; // do *not* consume the next token!
3100 }
3101 // otherwise, FALL THROUGH!
3102 default:
Steve Naroff239f0732008-12-25 14:16:32 +00003103 DoneWithTypeQuals:
Chris Lattner5a69d1c2008-12-18 07:02:59 +00003104 // If this is not a type-qualifier token, we're done reading type
3105 // qualifiers. First verify that DeclSpec's are consistent.
Douglas Gregor9b3064b2009-04-01 22:41:11 +00003106 DS.Finish(Diags, PP);
Abramo Bagnara796aa442011-03-12 11:17:06 +00003107 if (EndLoc.isValid())
3108 DS.SetRangeEnd(EndLoc);
Chris Lattner5a69d1c2008-12-18 07:02:59 +00003109 return;
Reid Spencer5f016e22007-07-11 17:01:13 +00003110 }
Chris Lattnera1fcbad2008-12-18 06:50:14 +00003111
Reid Spencer5f016e22007-07-11 17:01:13 +00003112 // If the specifier combination wasn't legal, issue a diagnostic.
3113 if (isInvalid) {
3114 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner1ab3b962008-11-18 07:48:38 +00003115 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00003116 }
Abramo Bagnara796aa442011-03-12 11:17:06 +00003117 EndLoc = ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00003118 }
3119}
3120
3121
3122/// ParseDeclarator - Parse and verify a newly-initialized declarator.
3123///
3124void Parser::ParseDeclarator(Declarator &D) {
3125 /// This implements the 'declarator' production in the C grammar, then checks
3126 /// for well-formedness and issues diagnostics.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003127 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Reid Spencer5f016e22007-07-11 17:01:13 +00003128}
3129
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003130/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
3131/// is parsed by the function passed to it. Pass null, and the direct-declarator
3132/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003133/// ptr-operator production.
3134///
Sebastian Redlf30208a2009-01-24 21:16:55 +00003135/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
3136/// [C] pointer[opt] direct-declarator
3137/// [C++] direct-declarator
3138/// [C++] ptr-operator declarator
Reid Spencer5f016e22007-07-11 17:01:13 +00003139///
3140/// pointer: [C99 6.7.5]
3141/// '*' type-qualifier-list[opt]
3142/// '*' type-qualifier-list[opt] pointer
3143///
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003144/// ptr-operator:
3145/// '*' cv-qualifier-seq[opt]
3146/// '&'
Sebastian Redl05532f22009-03-15 22:02:01 +00003147/// [C++0x] '&&'
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003148/// [GNU] '&' restrict[opt] attributes[opt]
Sebastian Redl05532f22009-03-15 22:02:01 +00003149/// [GNU?] '&&' restrict[opt] attributes[opt]
Sebastian Redlf30208a2009-01-24 21:16:55 +00003150/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003151void Parser::ParseDeclaratorInternal(Declarator &D,
3152 DirectDeclParseFunction DirectDeclParser) {
Douglas Gregor91a28862009-08-26 14:27:30 +00003153 if (Diags.hasAllExtensionsSilenced())
3154 D.setExtension();
Douglas Gregor2ccccb32010-08-23 18:23:48 +00003155
Sebastian Redlf30208a2009-01-24 21:16:55 +00003156 // C++ member pointers start with a '::' or a nested-name.
3157 // Member pointers get special handling, since there's no place for the
3158 // scope spec in the generic path below.
Chris Lattnerf919bfe2009-03-24 17:04:48 +00003159 if (getLang().CPlusPlus &&
3160 (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) ||
3161 Tok.is(tok::annot_cxxscope))) {
Sebastian Redlf30208a2009-01-24 21:16:55 +00003162 CXXScopeSpec SS;
John McCallb3d87482010-08-24 05:47:05 +00003163 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), true); // ignore fail
John McCall9ba61662010-02-26 08:45:28 +00003164
Jeffrey Yasskinedc28772010-04-07 23:29:58 +00003165 if (SS.isNotEmpty()) {
Mike Stump1eb44332009-09-09 15:08:12 +00003166 if (Tok.isNot(tok::star)) {
Sebastian Redlf30208a2009-01-24 21:16:55 +00003167 // The scope spec really belongs to the direct-declarator.
3168 D.getCXXScopeSpec() = SS;
3169 if (DirectDeclParser)
3170 (this->*DirectDeclParser)(D);
3171 return;
3172 }
3173
3174 SourceLocation Loc = ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00003175 D.SetRangeEnd(Loc);
John McCall0b7e6782011-03-24 11:26:52 +00003176 DeclSpec DS(AttrFactory);
Sebastian Redlf30208a2009-01-24 21:16:55 +00003177 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00003178 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00003179
3180 // Recurse to parse whatever is left.
3181 ParseDeclaratorInternal(D, DirectDeclParser);
3182
3183 // Sema will have to catch (syntactically invalid) pointers into global
3184 // scope. It has to catch pointers into namespace scope anyway.
3185 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
John McCall0b7e6782011-03-24 11:26:52 +00003186 Loc),
3187 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003188 /* Don't replace range end. */SourceLocation());
Sebastian Redlf30208a2009-01-24 21:16:55 +00003189 return;
3190 }
3191 }
3192
3193 tok::TokenKind Kind = Tok.getKind();
Steve Naroff5618bd42008-08-27 16:04:49 +00003194 // Not a pointer, C++ reference, or block.
Chris Lattner9af55002009-03-27 04:18:06 +00003195 if (Kind != tok::star && Kind != tok::caret &&
Chris Lattnerf919bfe2009-03-24 17:04:48 +00003196 (Kind != tok::amp || !getLang().CPlusPlus) &&
Sebastian Redl743de1f2009-03-23 00:00:23 +00003197 // We parse rvalue refs in C++03, because otherwise the errors are scary.
Chris Lattner9af55002009-03-27 04:18:06 +00003198 (Kind != tok::ampamp || !getLang().CPlusPlus)) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003199 if (DirectDeclParser)
3200 (this->*DirectDeclParser)(D);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003201 return;
3202 }
Sebastian Redlf30208a2009-01-24 21:16:55 +00003203
Sebastian Redl05532f22009-03-15 22:02:01 +00003204 // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference,
3205 // '&&' -> rvalue reference
Sebastian Redl743de1f2009-03-23 00:00:23 +00003206 SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&.
Sebastian Redlab197ba2009-02-09 18:23:29 +00003207 D.SetRangeEnd(Loc);
Reid Spencer5f016e22007-07-11 17:01:13 +00003208
Chris Lattner9af55002009-03-27 04:18:06 +00003209 if (Kind == tok::star || Kind == tok::caret) {
Chris Lattner76549142008-02-21 01:32:26 +00003210 // Is a pointer.
John McCall0b7e6782011-03-24 11:26:52 +00003211 DeclSpec DS(AttrFactory);
Sebastian Redlf30208a2009-01-24 21:16:55 +00003212
Reid Spencer5f016e22007-07-11 17:01:13 +00003213 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00003214 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00003215
Reid Spencer5f016e22007-07-11 17:01:13 +00003216 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003217 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroff5618bd42008-08-27 16:04:49 +00003218 if (Kind == tok::star)
3219 // Remember that we parsed a pointer type, and remember the type-quals.
3220 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
Chandler Carruthd067c072011-02-23 18:51:59 +00003221 DS.getConstSpecLoc(),
3222 DS.getVolatileSpecLoc(),
John McCall0b7e6782011-03-24 11:26:52 +00003223 DS.getRestrictSpecLoc()),
3224 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003225 SourceLocation());
Steve Naroff5618bd42008-08-27 16:04:49 +00003226 else
3227 // Remember that we parsed a Block type, and remember the type-quals.
Mike Stump1eb44332009-09-09 15:08:12 +00003228 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
John McCall0b7e6782011-03-24 11:26:52 +00003229 Loc),
3230 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003231 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00003232 } else {
3233 // Is a reference
John McCall0b7e6782011-03-24 11:26:52 +00003234 DeclSpec DS(AttrFactory);
Reid Spencer5f016e22007-07-11 17:01:13 +00003235
Sebastian Redl743de1f2009-03-23 00:00:23 +00003236 // Complain about rvalue references in C++03, but then go on and build
3237 // the declarator.
3238 if (Kind == tok::ampamp && !getLang().CPlusPlus0x)
Douglas Gregor16cf8f52011-01-25 02:17:32 +00003239 Diag(Loc, diag::ext_rvalue_reference);
Sebastian Redl743de1f2009-03-23 00:00:23 +00003240
Reid Spencer5f016e22007-07-11 17:01:13 +00003241 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
3242 // cv-qualifiers are introduced through the use of a typedef or of a
3243 // template type argument, in which case the cv-qualifiers are ignored.
3244 //
3245 // [GNU] Retricted references are allowed.
3246 // [GNU] Attributes on references are allowed.
Sean Huntbbd37c62009-11-21 08:43:09 +00003247 // [C++0x] Attributes on references are not allowed.
3248 ParseTypeQualifierListOpt(DS, true, false);
Sebastian Redlab197ba2009-02-09 18:23:29 +00003249 D.ExtendWithDeclSpec(DS);
Reid Spencer5f016e22007-07-11 17:01:13 +00003250
3251 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
3252 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
3253 Diag(DS.getConstSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00003254 diag::err_invalid_reference_qualifier_application) << "const";
Reid Spencer5f016e22007-07-11 17:01:13 +00003255 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
3256 Diag(DS.getVolatileSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00003257 diag::err_invalid_reference_qualifier_application) << "volatile";
Reid Spencer5f016e22007-07-11 17:01:13 +00003258 }
3259
3260 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003261 ParseDeclaratorInternal(D, DirectDeclParser);
Reid Spencer5f016e22007-07-11 17:01:13 +00003262
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00003263 if (D.getNumTypeObjects() > 0) {
3264 // C++ [dcl.ref]p4: There shall be no references to references.
3265 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
3266 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattnerda83bac2008-11-19 07:37:42 +00003267 if (const IdentifierInfo *II = D.getIdentifier())
3268 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
3269 << II;
3270 else
3271 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
3272 << "type name";
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00003273
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003274 // Once we've complained about the reference-to-reference, we
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00003275 // can go ahead and build the (technically ill-formed)
3276 // declarator: reference collapsing will take care of it.
3277 }
3278 }
3279
Reid Spencer5f016e22007-07-11 17:01:13 +00003280 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner76549142008-02-21 01:32:26 +00003281 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
Sebastian Redl05532f22009-03-15 22:02:01 +00003282 Kind == tok::amp),
John McCall0b7e6782011-03-24 11:26:52 +00003283 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003284 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00003285 }
3286}
3287
3288/// ParseDirectDeclarator
3289/// direct-declarator: [C99 6.7.5]
Douglas Gregor42a552f2008-11-05 20:51:48 +00003290/// [C99] identifier
Reid Spencer5f016e22007-07-11 17:01:13 +00003291/// '(' declarator ')'
3292/// [GNU] '(' attributes declarator ')'
3293/// [C90] direct-declarator '[' constant-expression[opt] ']'
3294/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
3295/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
3296/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
3297/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
3298/// direct-declarator '(' parameter-type-list ')'
3299/// direct-declarator '(' identifier-list[opt] ')'
3300/// [GNU] direct-declarator '(' parameter-forward-declarations
3301/// parameter-type-list[opt] ')'
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003302/// [C++] direct-declarator '(' parameter-declaration-clause ')'
3303/// cv-qualifier-seq[opt] exception-specification[opt]
Douglas Gregorb48fe382008-10-31 09:07:45 +00003304/// [C++] declarator-id
Douglas Gregor42a552f2008-11-05 20:51:48 +00003305///
3306/// declarator-id: [C++ 8]
Douglas Gregora8bc8c92010-12-23 22:44:42 +00003307/// '...'[opt] id-expression
Douglas Gregor42a552f2008-11-05 20:51:48 +00003308/// '::'[opt] nested-name-specifier[opt] type-name
3309///
3310/// id-expression: [C++ 5.1]
3311/// unqualified-id
Douglas Gregordb422df2009-09-25 21:45:23 +00003312/// qualified-id
Douglas Gregor42a552f2008-11-05 20:51:48 +00003313///
3314/// unqualified-id: [C++ 5.1]
Mike Stump1eb44332009-09-09 15:08:12 +00003315/// identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003316/// operator-function-id
Douglas Gregordb422df2009-09-25 21:45:23 +00003317/// conversion-function-id
Mike Stump1eb44332009-09-09 15:08:12 +00003318/// '~' class-name
Douglas Gregor39a8de12009-02-25 19:37:18 +00003319/// template-id
Argyrios Kyrtzidisc7ed9c62008-11-07 22:02:30 +00003320///
Reid Spencer5f016e22007-07-11 17:01:13 +00003321void Parser::ParseDirectDeclarator(Declarator &D) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00003322 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003323
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003324 if (getLang().CPlusPlus && D.mayHaveIdentifier()) {
3325 // ParseDeclaratorInternal might already have parsed the scope.
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00003326 if (D.getCXXScopeSpec().isEmpty()) {
John McCallb3d87482010-08-24 05:47:05 +00003327 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec(), ParsedType(), true);
John McCall9ba61662010-02-26 08:45:28 +00003328 }
3329
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00003330 if (D.getCXXScopeSpec().isValid()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00003331 if (Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
John McCalle7e278b2009-12-11 20:04:54 +00003332 // Change the declaration context for name lookup, until this function
3333 // is exited (and the declarator has been parsed).
3334 DeclScopeObj.EnterDeclaratorScope();
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00003335 }
3336
Douglas Gregora8bc8c92010-12-23 22:44:42 +00003337 // C++0x [dcl.fct]p14:
3338 // There is a syntactic ambiguity when an ellipsis occurs at the end
3339 // of a parameter-declaration-clause without a preceding comma. In
3340 // this case, the ellipsis is parsed as part of the
3341 // abstract-declarator if the type of the parameter names a template
3342 // parameter pack that has not been expanded; otherwise, it is parsed
3343 // as part of the parameter-declaration-clause.
3344 if (Tok.is(tok::ellipsis) &&
3345 !((D.getContext() == Declarator::PrototypeContext ||
3346 D.getContext() == Declarator::BlockLiteralContext) &&
Douglas Gregora8bc8c92010-12-23 22:44:42 +00003347 NextToken().is(tok::r_paren) &&
3348 !Actions.containsUnexpandedParameterPacks(D)))
3349 D.setEllipsisLoc(ConsumeToken());
3350
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003351 if (Tok.is(tok::identifier) || Tok.is(tok::kw_operator) ||
3352 Tok.is(tok::annot_template_id) || Tok.is(tok::tilde)) {
3353 // We found something that indicates the start of an unqualified-id.
3354 // Parse that unqualified-id.
John McCallba9d8532010-04-13 06:39:49 +00003355 bool AllowConstructorName;
3356 if (D.getDeclSpec().hasTypeSpecifier())
3357 AllowConstructorName = false;
3358 else if (D.getCXXScopeSpec().isSet())
3359 AllowConstructorName =
3360 (D.getContext() == Declarator::FileContext ||
3361 (D.getContext() == Declarator::MemberContext &&
3362 D.getDeclSpec().isFriendSpecified()));
3363 else
3364 AllowConstructorName = (D.getContext() == Declarator::MemberContext);
3365
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003366 if (ParseUnqualifiedId(D.getCXXScopeSpec(),
3367 /*EnteringContext=*/true,
3368 /*AllowDestructorName=*/true,
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003369 AllowConstructorName,
John McCallb3d87482010-08-24 05:47:05 +00003370 ParsedType(),
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00003371 D.getName()) ||
3372 // Once we're past the identifier, if the scope was bad, mark the
3373 // whole declarator bad.
3374 D.getCXXScopeSpec().isInvalid()) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00003375 D.SetIdentifier(0, Tok.getLocation());
3376 D.setInvalidType(true);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003377 } else {
3378 // Parsed the unqualified-id; update range information and move along.
3379 if (D.getSourceRange().getBegin().isInvalid())
3380 D.SetRangeBegin(D.getName().getSourceRange().getBegin());
3381 D.SetRangeEnd(D.getName().getSourceRange().getEnd());
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003382 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003383 goto PastIdentifier;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00003384 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003385 } else if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00003386 assert(!getLang().CPlusPlus &&
3387 "There's a C++-specific check for tok::identifier above");
3388 assert(Tok.getIdentifierInfo() && "Not an identifier?");
3389 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
3390 ConsumeToken();
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003391 goto PastIdentifier;
3392 }
3393
3394 if (Tok.is(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003395 // direct-declarator: '(' declarator ')'
3396 // direct-declarator: '(' attributes declarator ')'
3397 // Example: 'char (*X)' or 'int (*XX)(void)'
3398 ParseParenDeclarator(D);
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003399
3400 // If the declarator was parenthesized, we entered the declarator
3401 // scope when parsing the parenthesized declarator, then exited
3402 // the scope already. Re-enter the scope, if we need to.
3403 if (D.getCXXScopeSpec().isSet()) {
Fariborz Jahanian46877cd2010-08-17 23:50:37 +00003404 // If there was an error parsing parenthesized declarator, declarator
3405 // scope may have been enterred before. Don't do it again.
3406 if (!D.isInvalidType() &&
3407 Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003408 // Change the declaration context for name lookup, until this function
3409 // is exited (and the declarator has been parsed).
Fariborz Jahanian46877cd2010-08-17 23:50:37 +00003410 DeclScopeObj.EnterDeclaratorScope();
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003411 }
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00003412 } else if (D.mayOmitIdentifier()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003413 // This could be something simple like "int" (in which case the declarator
3414 // portion is empty), if an abstract-declarator is allowed.
3415 D.SetIdentifier(0, Tok.getLocation());
3416 } else {
Douglas Gregore950d4b2009-03-06 23:28:18 +00003417 if (D.getContext() == Declarator::MemberContext)
3418 Diag(Tok, diag::err_expected_member_name_or_semi)
3419 << D.getDeclSpec().getSourceRange();
3420 else if (getLang().CPlusPlus)
Douglas Gregor2d1c2142009-11-03 19:44:04 +00003421 Diag(Tok, diag::err_expected_unqualified_id) << getLang().CPlusPlus;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003422 else
Chris Lattner1ab3b962008-11-18 07:48:38 +00003423 Diag(Tok, diag::err_expected_ident_lparen);
Reid Spencer5f016e22007-07-11 17:01:13 +00003424 D.SetIdentifier(0, Tok.getLocation());
Chris Lattner1f6f54b2008-11-11 06:13:16 +00003425 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00003426 }
Mike Stump1eb44332009-09-09 15:08:12 +00003427
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00003428 PastIdentifier:
Reid Spencer5f016e22007-07-11 17:01:13 +00003429 assert(D.isPastIdentifier() &&
3430 "Haven't past the location of the identifier yet?");
Mike Stump1eb44332009-09-09 15:08:12 +00003431
Sean Huntbbd37c62009-11-21 08:43:09 +00003432 // Don't parse attributes unless we have an identifier.
John McCall7f040a92010-12-24 02:08:15 +00003433 if (D.getIdentifier())
3434 MaybeParseCXX0XAttributes(D);
Sean Huntbbd37c62009-11-21 08:43:09 +00003435
Reid Spencer5f016e22007-07-11 17:01:13 +00003436 while (1) {
Chris Lattner04d66662007-10-09 17:33:22 +00003437 if (Tok.is(tok::l_paren)) {
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00003438 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
3439 // In such a case, check if we actually have a function declarator; if it
3440 // is not, the declarator has been fully parsed.
Chris Lattner7399ee02008-10-20 02:05:46 +00003441 if (getLang().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
3442 // When not in file scope, warn for ambiguous function declarators, just
3443 // in case the author intended it as a variable definition.
3444 bool warnIfAmbiguous = D.getContext() != Declarator::FileContext;
3445 if (!isCXXFunctionDeclarator(warnIfAmbiguous))
3446 break;
3447 }
John McCall0b7e6782011-03-24 11:26:52 +00003448 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00003449 ParseFunctionDeclarator(ConsumeParen(), D, attrs);
Chris Lattner04d66662007-10-09 17:33:22 +00003450 } else if (Tok.is(tok::l_square)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003451 ParseBracketDeclarator(D);
3452 } else {
3453 break;
3454 }
3455 }
3456}
3457
Chris Lattneref4715c2008-04-06 05:45:57 +00003458/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
3459/// only called before the identifier, so these are most likely just grouping
Mike Stump1eb44332009-09-09 15:08:12 +00003460/// parens for precedence. If we find that these are actually function
Chris Lattneref4715c2008-04-06 05:45:57 +00003461/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
3462///
3463/// direct-declarator:
3464/// '(' declarator ')'
3465/// [GNU] '(' attributes declarator ')'
Chris Lattner7399ee02008-10-20 02:05:46 +00003466/// direct-declarator '(' parameter-type-list ')'
3467/// direct-declarator '(' identifier-list[opt] ')'
3468/// [GNU] direct-declarator '(' parameter-forward-declarations
3469/// parameter-type-list[opt] ')'
Chris Lattneref4715c2008-04-06 05:45:57 +00003470///
3471void Parser::ParseParenDeclarator(Declarator &D) {
3472 SourceLocation StartLoc = ConsumeParen();
3473 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
Mike Stump1eb44332009-09-09 15:08:12 +00003474
Chris Lattner7399ee02008-10-20 02:05:46 +00003475 // Eat any attributes before we look at whether this is a grouping or function
3476 // declarator paren. If this is a grouping paren, the attribute applies to
3477 // the type being built up, for example:
3478 // int (__attribute__(()) *x)(long y)
3479 // If this ends up not being a grouping paren, the attribute applies to the
3480 // first argument, for example:
3481 // int (__attribute__(()) int x)
3482 // In either case, we need to eat any attributes to be able to determine what
3483 // sort of paren this is.
3484 //
John McCall0b7e6782011-03-24 11:26:52 +00003485 ParsedAttributes attrs(AttrFactory);
Chris Lattner7399ee02008-10-20 02:05:46 +00003486 bool RequiresArg = false;
3487 if (Tok.is(tok::kw___attribute)) {
John McCall7f040a92010-12-24 02:08:15 +00003488 ParseGNUAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00003489
Chris Lattner7399ee02008-10-20 02:05:46 +00003490 // We require that the argument list (if this is a non-grouping paren) be
3491 // present even if the attribute list was empty.
3492 RequiresArg = true;
3493 }
Steve Naroff239f0732008-12-25 14:16:32 +00003494 // Eat any Microsoft extensions.
Eli Friedman290eeb02009-06-08 23:27:34 +00003495 if (Tok.is(tok::kw___cdecl) || Tok.is(tok::kw___stdcall) ||
Douglas Gregorf813a2c2010-05-18 16:57:00 +00003496 Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___fastcall) ||
3497 Tok.is(tok::kw___w64) || Tok.is(tok::kw___ptr64)) {
John McCall7f040a92010-12-24 02:08:15 +00003498 ParseMicrosoftTypeAttributes(attrs);
Eli Friedman290eeb02009-06-08 23:27:34 +00003499 }
Dawn Perchik52fc3142010-09-03 01:29:35 +00003500 // Eat any Borland extensions.
Ted Kremenek8113ecf2010-11-10 05:59:39 +00003501 if (Tok.is(tok::kw___pascal))
John McCall7f040a92010-12-24 02:08:15 +00003502 ParseBorlandTypeAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00003503
Chris Lattneref4715c2008-04-06 05:45:57 +00003504 // If we haven't past the identifier yet (or where the identifier would be
3505 // stored, if this is an abstract declarator), then this is probably just
3506 // grouping parens. However, if this could be an abstract-declarator, then
3507 // this could also be the start of function arguments (consider 'void()').
3508 bool isGrouping;
Mike Stump1eb44332009-09-09 15:08:12 +00003509
Chris Lattneref4715c2008-04-06 05:45:57 +00003510 if (!D.mayOmitIdentifier()) {
3511 // If this can't be an abstract-declarator, this *must* be a grouping
3512 // paren, because we haven't seen the identifier yet.
3513 isGrouping = true;
3514 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Argyrios Kyrtzidise25d2702008-10-06 00:07:55 +00003515 (getLang().CPlusPlus && Tok.is(tok::ellipsis)) || // C++ int(...)
Chris Lattneref4715c2008-04-06 05:45:57 +00003516 isDeclarationSpecifier()) { // 'int(int)' is a function.
3517 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
3518 // considered to be a type, not a K&R identifier-list.
3519 isGrouping = false;
3520 } else {
3521 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
3522 isGrouping = true;
3523 }
Mike Stump1eb44332009-09-09 15:08:12 +00003524
Chris Lattneref4715c2008-04-06 05:45:57 +00003525 // If this is a grouping paren, handle:
3526 // direct-declarator: '(' declarator ')'
3527 // direct-declarator: '(' attributes declarator ')'
3528 if (isGrouping) {
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00003529 bool hadGroupingParens = D.hasGroupingParens();
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00003530 D.setGroupingParens(true);
3531
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003532 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattneref4715c2008-04-06 05:45:57 +00003533 // Match the ')'.
Abramo Bagnara075f8f12010-12-10 16:29:40 +00003534 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_paren, StartLoc);
John McCall0b7e6782011-03-24 11:26:52 +00003535 D.AddTypeInfo(DeclaratorChunk::getParen(StartLoc, EndLoc),
3536 attrs, EndLoc);
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00003537
3538 D.setGroupingParens(hadGroupingParens);
Chris Lattneref4715c2008-04-06 05:45:57 +00003539 return;
3540 }
Mike Stump1eb44332009-09-09 15:08:12 +00003541
Chris Lattneref4715c2008-04-06 05:45:57 +00003542 // Okay, if this wasn't a grouping paren, it must be the start of a function
3543 // argument list. Recognize that this declarator will never have an
Chris Lattner7399ee02008-10-20 02:05:46 +00003544 // identifier (and remember where it would have been), then call into
3545 // ParseFunctionDeclarator to handle of argument list.
Chris Lattneref4715c2008-04-06 05:45:57 +00003546 D.SetIdentifier(0, Tok.getLocation());
3547
John McCall7f040a92010-12-24 02:08:15 +00003548 ParseFunctionDeclarator(StartLoc, D, attrs, RequiresArg);
Chris Lattneref4715c2008-04-06 05:45:57 +00003549}
3550
3551/// ParseFunctionDeclarator - We are after the identifier and have parsed the
3552/// declarator D up to a paren, which indicates that we are parsing function
3553/// arguments.
Reid Spencer5f016e22007-07-11 17:01:13 +00003554///
Chris Lattner7399ee02008-10-20 02:05:46 +00003555/// If AttrList is non-null, then the caller parsed those arguments immediately
3556/// after the open paren - they should be considered to be the first argument of
3557/// a parameter. If RequiresArg is true, then the first argument of the
3558/// function is required to be present and required to not be an identifier
3559/// list.
3560///
Reid Spencer5f016e22007-07-11 17:01:13 +00003561/// This method also handles this portion of the grammar:
3562/// parameter-type-list: [C99 6.7.5]
3563/// parameter-list
3564/// parameter-list ',' '...'
Douglas Gregored5d6512009-09-22 21:41:40 +00003565/// [C++] parameter-list '...'
Reid Spencer5f016e22007-07-11 17:01:13 +00003566///
3567/// parameter-list: [C99 6.7.5]
3568/// parameter-declaration
3569/// parameter-list ',' parameter-declaration
3570///
3571/// parameter-declaration: [C99 6.7.5]
3572/// declaration-specifiers declarator
Chris Lattner04421082008-04-08 04:40:51 +00003573/// [C++] declaration-specifiers declarator '=' assignment-expression
Reid Spencer5f016e22007-07-11 17:01:13 +00003574/// [GNU] declaration-specifiers declarator attributes
Sebastian Redl50de12f2009-03-24 22:27:57 +00003575/// declaration-specifiers abstract-declarator[opt]
3576/// [C++] declaration-specifiers abstract-declarator[opt]
Chris Lattner8123a952008-04-10 02:22:51 +00003577/// '=' assignment-expression
Reid Spencer5f016e22007-07-11 17:01:13 +00003578/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
3579///
Douglas Gregor83f51722011-01-26 03:43:54 +00003580/// For C++, after the parameter-list, it also parses "cv-qualifier-seq[opt]",
3581/// C++0x "ref-qualifier[opt]" and "exception-specification[opt]".
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003582///
Sebastian Redl7acafd02011-03-05 14:45:16 +00003583/// [C++0x] exception-specification:
3584/// dynamic-exception-specification
3585/// noexcept-specification
3586///
Chris Lattner7399ee02008-10-20 02:05:46 +00003587void Parser::ParseFunctionDeclarator(SourceLocation LParenLoc, Declarator &D,
John McCall7f040a92010-12-24 02:08:15 +00003588 ParsedAttributes &attrs,
Chris Lattner7399ee02008-10-20 02:05:46 +00003589 bool RequiresArg) {
Chris Lattneref4715c2008-04-06 05:45:57 +00003590 // lparen is already consumed!
3591 assert(D.isPastIdentifier() && "Should not call before identifier!");
Mike Stump1eb44332009-09-09 15:08:12 +00003592
Douglas Gregordab60ad2010-10-01 18:44:50 +00003593 ParsedType TrailingReturnType;
3594
Chris Lattner7399ee02008-10-20 02:05:46 +00003595 // This parameter list may be empty.
Chris Lattner04d66662007-10-09 17:33:22 +00003596 if (Tok.is(tok::r_paren)) {
Ted Kremenek8113ecf2010-11-10 05:59:39 +00003597 if (RequiresArg)
Chris Lattner1ab3b962008-11-18 07:48:38 +00003598 Diag(Tok, diag::err_argument_required_after_attribute);
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003599
Abramo Bagnara796aa442011-03-12 11:17:06 +00003600 SourceLocation EndLoc = ConsumeParen(); // Eat the closing ')'.
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003601
3602 // cv-qualifier-seq[opt].
John McCall0b7e6782011-03-24 11:26:52 +00003603 DeclSpec DS(AttrFactory);
Douglas Gregor83f51722011-01-26 03:43:54 +00003604 SourceLocation RefQualifierLoc;
3605 bool RefQualifierIsLValueRef = true;
Sebastian Redl7acafd02011-03-05 14:45:16 +00003606 ExceptionSpecificationType ESpecType = EST_None;
3607 SourceRange ESpecRange;
3608 llvm::SmallVector<ParsedType, 2> DynamicExceptions;
3609 llvm::SmallVector<SourceRange, 2> DynamicExceptionRanges;
3610 ExprResult NoexceptExpr;
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003611 if (getLang().CPlusPlus) {
John McCall7f040a92010-12-24 02:08:15 +00003612 MaybeParseCXX0XAttributes(attrs);
3613
Chris Lattner5a69d1c2008-12-18 07:02:59 +00003614 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Sebastian Redlab197ba2009-02-09 18:23:29 +00003615 if (!DS.getSourceRange().getEnd().isInvalid())
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00003616 EndLoc = DS.getSourceRange().getEnd();
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00003617
Douglas Gregor83f51722011-01-26 03:43:54 +00003618 // Parse ref-qualifier[opt]
3619 if (Tok.is(tok::amp) || Tok.is(tok::ampamp)) {
3620 if (!getLang().CPlusPlus0x)
Douglas Gregor1f381062011-01-26 20:35:32 +00003621 Diag(Tok, diag::ext_ref_qualifier);
Abramo Bagnara796aa442011-03-12 11:17:06 +00003622
Douglas Gregor83f51722011-01-26 03:43:54 +00003623 RefQualifierIsLValueRef = Tok.is(tok::amp);
3624 RefQualifierLoc = ConsumeToken();
3625 EndLoc = RefQualifierLoc;
3626 }
Sebastian Redl7acafd02011-03-05 14:45:16 +00003627
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00003628 // Parse exception-specification[opt].
Sebastian Redl7acafd02011-03-05 14:45:16 +00003629 ESpecType = MaybeParseExceptionSpecification(ESpecRange,
3630 DynamicExceptions,
3631 DynamicExceptionRanges,
3632 NoexceptExpr);
3633 if (ESpecType != EST_None)
3634 EndLoc = ESpecRange.getEnd();
Douglas Gregordab60ad2010-10-01 18:44:50 +00003635
3636 // Parse trailing-return-type.
3637 if (getLang().CPlusPlus0x && Tok.is(tok::arrow)) {
3638 TrailingReturnType = ParseTrailingReturnType().get();
3639 }
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003640 }
3641
Chris Lattnerf97409f2008-04-06 06:57:35 +00003642 // Remember that we parsed a function type, and remember the attributes.
Reid Spencer5f016e22007-07-11 17:01:13 +00003643 // int() -> no prototype, no '...'.
John McCall0b7e6782011-03-24 11:26:52 +00003644 D.AddTypeInfo(DeclaratorChunk::getFunction(/*prototype*/getLang().CPlusPlus,
Chris Lattnerf97409f2008-04-06 06:57:35 +00003645 /*variadic*/ false,
Douglas Gregor965acbb2009-02-18 07:07:28 +00003646 SourceLocation(),
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003647 /*arglist*/ 0, 0,
3648 DS.getTypeQualifiers(),
Douglas Gregor83f51722011-01-26 03:43:54 +00003649 RefQualifierIsLValueRef,
3650 RefQualifierLoc,
Sebastian Redl6e5d3192011-03-05 22:42:13 +00003651 ESpecType, ESpecRange.getBegin(),
Sebastian Redl7acafd02011-03-05 14:45:16 +00003652 DynamicExceptions.data(),
3653 DynamicExceptionRanges.data(),
3654 DynamicExceptions.size(),
Sebastian Redl6e5d3192011-03-05 22:42:13 +00003655 NoexceptExpr.isUsable() ?
3656 NoexceptExpr.get() : 0,
Abramo Bagnara796aa442011-03-12 11:17:06 +00003657 LParenLoc, EndLoc, D,
Douglas Gregordab60ad2010-10-01 18:44:50 +00003658 TrailingReturnType),
John McCall0b7e6782011-03-24 11:26:52 +00003659 attrs, EndLoc);
Chris Lattnerf97409f2008-04-06 06:57:35 +00003660 return;
Sebastian Redlef65f062009-05-29 18:02:33 +00003661 }
3662
Chris Lattner7399ee02008-10-20 02:05:46 +00003663 // Alternatively, this parameter list may be an identifier list form for a
3664 // K&R-style function: void foo(a,b,c)
John Thompson82287d12010-02-05 00:12:22 +00003665 if (!getLang().CPlusPlus && Tok.is(tok::identifier)
3666 && !TryAltiVecVectorToken()) {
John McCall9ba61662010-02-26 08:45:28 +00003667 if (TryAnnotateTypeOrScopeToken() || !Tok.is(tok::annot_typename)) {
Chris Lattner7399ee02008-10-20 02:05:46 +00003668 // K&R identifier lists can't have typedefs as identifiers, per
3669 // C99 6.7.5.3p11.
Ted Kremenek8113ecf2010-11-10 05:59:39 +00003670 if (RequiresArg)
Steve Naroff2d081c42009-01-28 19:16:40 +00003671 Diag(Tok, diag::err_argument_required_after_attribute);
Chris Lattner83a94472010-05-14 17:23:36 +00003672
Steve Naroff2d081c42009-01-28 19:16:40 +00003673 // Identifier list. Note that '(' identifier-list ')' is only allowed for
Chris Lattner83a94472010-05-14 17:23:36 +00003674 // normal declarators, not for abstract-declarators. Get the first
3675 // identifier.
Chris Lattner9a65b812010-05-14 17:44:56 +00003676 Token FirstTok = Tok;
Chris Lattner83a94472010-05-14 17:23:36 +00003677 ConsumeToken(); // eat the first identifier.
Chris Lattner9a65b812010-05-14 17:44:56 +00003678
3679 // Identifier lists follow a really simple grammar: the identifiers can
3680 // be followed *only* by a ", moreidentifiers" or ")". However, K&R
3681 // identifier lists are really rare in the brave new modern world, and it
3682 // is very common for someone to typo a type in a non-k&r style list. If
3683 // we are presented with something like: "void foo(intptr x, float y)",
3684 // we don't want to start parsing the function declarator as though it is
3685 // a K&R style declarator just because intptr is an invalid type.
3686 //
3687 // To handle this, we check to see if the token after the first identifier
3688 // is a "," or ")". Only if so, do we parse it as an identifier list.
3689 if (Tok.is(tok::comma) || Tok.is(tok::r_paren))
3690 return ParseFunctionDeclaratorIdentifierList(LParenLoc,
3691 FirstTok.getIdentifierInfo(),
3692 FirstTok.getLocation(), D);
3693
3694 // If we get here, the code is invalid. Push the first identifier back
3695 // into the token stream and parse the first argument as an (invalid)
3696 // normal argument declarator.
3697 PP.EnterToken(Tok);
3698 Tok = FirstTok;
Chris Lattner7399ee02008-10-20 02:05:46 +00003699 }
Chris Lattnerf97409f2008-04-06 06:57:35 +00003700 }
Mike Stump1eb44332009-09-09 15:08:12 +00003701
Chris Lattnerf97409f2008-04-06 06:57:35 +00003702 // Finally, a normal, non-empty parameter type list.
Mike Stump1eb44332009-09-09 15:08:12 +00003703
Chris Lattnerf97409f2008-04-06 06:57:35 +00003704 // Build up an array of information about the parsed arguments.
3705 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Chris Lattner04421082008-04-08 04:40:51 +00003706
3707 // Enter function-declaration scope, limiting any declarators to the
3708 // function prototype scope, including parameter declarators.
Chris Lattnerae50fa02009-03-05 00:00:31 +00003709 ParseScope PrototypeScope(this,
3710 Scope::FunctionPrototypeScope|Scope::DeclScope);
Mike Stump1eb44332009-09-09 15:08:12 +00003711
Chris Lattnerf97409f2008-04-06 06:57:35 +00003712 bool IsVariadic = false;
Douglas Gregor965acbb2009-02-18 07:07:28 +00003713 SourceLocation EllipsisLoc;
Chris Lattnerf97409f2008-04-06 06:57:35 +00003714 while (1) {
3715 if (Tok.is(tok::ellipsis)) {
3716 IsVariadic = true;
Douglas Gregor965acbb2009-02-18 07:07:28 +00003717 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chris Lattnerf97409f2008-04-06 06:57:35 +00003718 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00003719 }
Mike Stump1eb44332009-09-09 15:08:12 +00003720
Chris Lattnerf97409f2008-04-06 06:57:35 +00003721 // Parse the declaration-specifiers.
John McCall54abf7d2009-11-04 02:18:39 +00003722 // Just use the ParsingDeclaration "scope" of the declarator.
John McCall0b7e6782011-03-24 11:26:52 +00003723 DeclSpec DS(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00003724
3725 // Skip any Microsoft attributes before a param.
3726 if (getLang().Microsoft && Tok.is(tok::l_square))
3727 ParseMicrosoftAttributes(DS.getAttributes());
3728
3729 SourceLocation DSStart = Tok.getLocation();
Chris Lattner7399ee02008-10-20 02:05:46 +00003730
3731 // If the caller parsed attributes for the first argument, add them now.
John McCall7f040a92010-12-24 02:08:15 +00003732 // Take them so that we only apply the attributes to the first parameter.
3733 DS.takeAttributesFrom(attrs);
3734
Chris Lattnere64c5492009-02-27 18:38:20 +00003735 ParseDeclarationSpecifiers(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00003736
Chris Lattnerf97409f2008-04-06 06:57:35 +00003737 // Parse the declarator. This is "PrototypeContext", because we must
3738 // accept either 'declarator' or 'abstract-declarator' here.
3739 Declarator ParmDecl(DS, Declarator::PrototypeContext);
3740 ParseDeclarator(ParmDecl);
3741
3742 // Parse GNU attributes, if present.
John McCall7f040a92010-12-24 02:08:15 +00003743 MaybeParseGNUAttributes(ParmDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00003744
Chris Lattnerf97409f2008-04-06 06:57:35 +00003745 // Remember this parsed parameter in ParamInfo.
3746 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
Mike Stump1eb44332009-09-09 15:08:12 +00003747
Douglas Gregor72b505b2008-12-16 21:30:33 +00003748 // DefArgToks is used when the parsing of default arguments needs
3749 // to be delayed.
3750 CachedTokens *DefArgToks = 0;
3751
Chris Lattnerf97409f2008-04-06 06:57:35 +00003752 // If no parameter was specified, verify that *something* was specified,
3753 // otherwise we have a missing type and identifier.
Chris Lattnere64c5492009-02-27 18:38:20 +00003754 if (DS.isEmpty() && ParmDecl.getIdentifier() == 0 &&
3755 ParmDecl.getNumTypeObjects() == 0) {
Chris Lattnerf97409f2008-04-06 06:57:35 +00003756 // Completely missing, emit error.
3757 Diag(DSStart, diag::err_missing_param);
3758 } else {
3759 // Otherwise, we have something. Add it and let semantic analysis try
3760 // to grok it and add the result to the ParamInfo we are building.
Mike Stump1eb44332009-09-09 15:08:12 +00003761
Chris Lattnerf97409f2008-04-06 06:57:35 +00003762 // Inform the actions module about the parameter declarator, so it gets
3763 // added to the current scope.
John McCalld226f652010-08-21 09:40:31 +00003764 Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl);
Chris Lattner04421082008-04-08 04:40:51 +00003765
3766 // Parse the default argument, if any. We parse the default
3767 // arguments in all dialects; the semantic analysis in
3768 // ActOnParamDefaultArgument will reject the default argument in
3769 // C.
3770 if (Tok.is(tok::equal)) {
Douglas Gregor61366e92008-12-24 00:01:03 +00003771 SourceLocation EqualLoc = Tok.getLocation();
3772
Chris Lattner04421082008-04-08 04:40:51 +00003773 // Parse the default argument
Douglas Gregor72b505b2008-12-16 21:30:33 +00003774 if (D.getContext() == Declarator::MemberContext) {
3775 // If we're inside a class definition, cache the tokens
3776 // corresponding to the default argument. We'll actually parse
3777 // them when we see the end of the class definition.
3778 // FIXME: Templates will require something similar.
3779 // FIXME: Can we use a smart pointer for Toks?
3780 DefArgToks = new CachedTokens;
3781
Mike Stump1eb44332009-09-09 15:08:12 +00003782 if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks,
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +00003783 /*StopAtSemi=*/true,
3784 /*ConsumeFinalToken=*/false)) {
Douglas Gregor72b505b2008-12-16 21:30:33 +00003785 delete DefArgToks;
3786 DefArgToks = 0;
Douglas Gregor61366e92008-12-24 00:01:03 +00003787 Actions.ActOnParamDefaultArgumentError(Param);
Argyrios Kyrtzidis2b602ad2010-08-06 09:47:24 +00003788 } else {
3789 // Mark the end of the default argument so that we know when to
3790 // stop when we parse it later on.
3791 Token DefArgEnd;
3792 DefArgEnd.startToken();
3793 DefArgEnd.setKind(tok::cxx_defaultarg_end);
3794 DefArgEnd.setLocation(Tok.getLocation());
3795 DefArgToks->push_back(DefArgEnd);
Mike Stump1eb44332009-09-09 15:08:12 +00003796 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc,
Anders Carlsson5e300d12009-06-12 16:51:40 +00003797 (*DefArgToks)[1].getLocation());
Argyrios Kyrtzidis2b602ad2010-08-06 09:47:24 +00003798 }
Chris Lattner04421082008-04-08 04:40:51 +00003799 } else {
Douglas Gregor72b505b2008-12-16 21:30:33 +00003800 // Consume the '='.
Douglas Gregor61366e92008-12-24 00:01:03 +00003801 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00003802
Douglas Gregorbe0f7bd2010-09-11 20:24:53 +00003803 // The argument isn't actually potentially evaluated unless it is
3804 // used.
3805 EnterExpressionEvaluationContext Eval(Actions,
3806 Sema::PotentiallyEvaluatedIfUsed);
3807
John McCall60d7b3a2010-08-24 06:29:42 +00003808 ExprResult DefArgResult(ParseAssignmentExpression());
Douglas Gregor72b505b2008-12-16 21:30:33 +00003809 if (DefArgResult.isInvalid()) {
3810 Actions.ActOnParamDefaultArgumentError(Param);
3811 SkipUntil(tok::comma, tok::r_paren, true, true);
3812 } else {
3813 // Inform the actions module about the default argument
3814 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
John McCall9ae2f072010-08-23 23:25:46 +00003815 DefArgResult.take());
Douglas Gregor72b505b2008-12-16 21:30:33 +00003816 }
Chris Lattner04421082008-04-08 04:40:51 +00003817 }
3818 }
Mike Stump1eb44332009-09-09 15:08:12 +00003819
3820 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
3821 ParmDecl.getIdentifierLoc(), Param,
Douglas Gregor72b505b2008-12-16 21:30:33 +00003822 DefArgToks));
Chris Lattnerf97409f2008-04-06 06:57:35 +00003823 }
3824
3825 // If the next token is a comma, consume it and keep reading arguments.
Douglas Gregored5d6512009-09-22 21:41:40 +00003826 if (Tok.isNot(tok::comma)) {
3827 if (Tok.is(tok::ellipsis)) {
3828 IsVariadic = true;
3829 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
3830
3831 if (!getLang().CPlusPlus) {
3832 // We have ellipsis without a preceding ',', which is ill-formed
3833 // in C. Complain and provide the fix.
3834 Diag(EllipsisLoc, diag::err_missing_comma_before_ellipsis)
Douglas Gregor849b2432010-03-31 17:46:05 +00003835 << FixItHint::CreateInsertion(EllipsisLoc, ", ");
Douglas Gregored5d6512009-09-22 21:41:40 +00003836 }
3837 }
3838
3839 break;
3840 }
Mike Stump1eb44332009-09-09 15:08:12 +00003841
Chris Lattnerf97409f2008-04-06 06:57:35 +00003842 // Consume the comma.
3843 ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00003844 }
Mike Stump1eb44332009-09-09 15:08:12 +00003845
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003846 // If we have the closing ')', eat it.
Abramo Bagnara796aa442011-03-12 11:17:06 +00003847 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003848
John McCall0b7e6782011-03-24 11:26:52 +00003849 DeclSpec DS(AttrFactory);
Douglas Gregor83f51722011-01-26 03:43:54 +00003850 SourceLocation RefQualifierLoc;
3851 bool RefQualifierIsLValueRef = true;
Sebastian Redl7acafd02011-03-05 14:45:16 +00003852 ExceptionSpecificationType ESpecType = EST_None;
3853 SourceRange ESpecRange;
3854 llvm::SmallVector<ParsedType, 2> DynamicExceptions;
3855 llvm::SmallVector<SourceRange, 2> DynamicExceptionRanges;
3856 ExprResult NoexceptExpr;
Sean Huntbbd37c62009-11-21 08:43:09 +00003857
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003858 if (getLang().CPlusPlus) {
John McCall7f040a92010-12-24 02:08:15 +00003859 MaybeParseCXX0XAttributes(attrs);
3860
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00003861 // Parse cv-qualifier-seq[opt].
Chris Lattner5a69d1c2008-12-18 07:02:59 +00003862 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Sebastian Redlab197ba2009-02-09 18:23:29 +00003863 if (!DS.getSourceRange().getEnd().isInvalid())
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00003864 EndLoc = DS.getSourceRange().getEnd();
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00003865
Douglas Gregor83f51722011-01-26 03:43:54 +00003866 // Parse ref-qualifier[opt]
3867 if (Tok.is(tok::amp) || Tok.is(tok::ampamp)) {
3868 if (!getLang().CPlusPlus0x)
Douglas Gregor1f381062011-01-26 20:35:32 +00003869 Diag(Tok, diag::ext_ref_qualifier);
Douglas Gregor83f51722011-01-26 03:43:54 +00003870
3871 RefQualifierIsLValueRef = Tok.is(tok::amp);
3872 RefQualifierLoc = ConsumeToken();
3873 EndLoc = RefQualifierLoc;
3874 }
3875
Sebastian Redl7acafd02011-03-05 14:45:16 +00003876 // FIXME: We should leave the prototype scope before parsing the exception
3877 // specification, and then reenter it when parsing the trailing return type.
3878 // FIXMEFIXME: Why? That wouldn't be right for the noexcept clause.
3879
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00003880 // Parse exception-specification[opt].
Sebastian Redl7acafd02011-03-05 14:45:16 +00003881 ESpecType = MaybeParseExceptionSpecification(ESpecRange,
3882 DynamicExceptions,
3883 DynamicExceptionRanges,
3884 NoexceptExpr);
3885 if (ESpecType != EST_None)
3886 EndLoc = ESpecRange.getEnd();
Douglas Gregordab60ad2010-10-01 18:44:50 +00003887
3888 // Parse trailing-return-type.
3889 if (getLang().CPlusPlus0x && Tok.is(tok::arrow)) {
3890 TrailingReturnType = ParseTrailingReturnType().get();
3891 }
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003892 }
3893
Douglas Gregordab60ad2010-10-01 18:44:50 +00003894 // Leave prototype scope.
3895 PrototypeScope.Exit();
3896
Reid Spencer5f016e22007-07-11 17:01:13 +00003897 // Remember that we parsed a function type, and remember the attributes.
John McCall0b7e6782011-03-24 11:26:52 +00003898 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/true, IsVariadic,
Douglas Gregor965acbb2009-02-18 07:07:28 +00003899 EllipsisLoc,
Jay Foadbeaaccd2009-05-21 09:52:38 +00003900 ParamInfo.data(), ParamInfo.size(),
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003901 DS.getTypeQualifiers(),
Douglas Gregor83f51722011-01-26 03:43:54 +00003902 RefQualifierIsLValueRef,
3903 RefQualifierLoc,
Sebastian Redl6e5d3192011-03-05 22:42:13 +00003904 ESpecType, ESpecRange.getBegin(),
Sebastian Redl7acafd02011-03-05 14:45:16 +00003905 DynamicExceptions.data(),
3906 DynamicExceptionRanges.data(),
3907 DynamicExceptions.size(),
Sebastian Redl6e5d3192011-03-05 22:42:13 +00003908 NoexceptExpr.isUsable() ?
3909 NoexceptExpr.get() : 0,
Abramo Bagnara796aa442011-03-12 11:17:06 +00003910 LParenLoc, EndLoc, D,
Douglas Gregordab60ad2010-10-01 18:44:50 +00003911 TrailingReturnType),
John McCall0b7e6782011-03-24 11:26:52 +00003912 attrs, EndLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00003913}
3914
Chris Lattner66d28652008-04-06 06:34:08 +00003915/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
3916/// we found a K&R-style identifier list instead of a type argument list. The
Chris Lattner83a94472010-05-14 17:23:36 +00003917/// first identifier has already been consumed, and the current token is the
3918/// token right after it.
Chris Lattner66d28652008-04-06 06:34:08 +00003919///
3920/// identifier-list: [C99 6.7.5]
3921/// identifier
3922/// identifier-list ',' identifier
3923///
3924void Parser::ParseFunctionDeclaratorIdentifierList(SourceLocation LParenLoc,
Chris Lattner83a94472010-05-14 17:23:36 +00003925 IdentifierInfo *FirstIdent,
3926 SourceLocation FirstIdentLoc,
Chris Lattner66d28652008-04-06 06:34:08 +00003927 Declarator &D) {
3928 // Build up an array of information about the parsed arguments.
3929 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
3930 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
Mike Stump1eb44332009-09-09 15:08:12 +00003931
Chris Lattner66d28652008-04-06 06:34:08 +00003932 // If there was no identifier specified for the declarator, either we are in
3933 // an abstract-declarator, or we are in a parameter declarator which was found
3934 // to be abstract. In abstract-declarators, identifier lists are not valid:
3935 // diagnose this.
3936 if (!D.getIdentifier())
Chris Lattner83a94472010-05-14 17:23:36 +00003937 Diag(FirstIdentLoc, diag::ext_ident_list_in_param);
Chris Lattner66d28652008-04-06 06:34:08 +00003938
Chris Lattner83a94472010-05-14 17:23:36 +00003939 // The first identifier was already read, and is known to be the first
3940 // identifier in the list. Remember this identifier in ParamInfo.
3941 ParamsSoFar.insert(FirstIdent);
John McCalld226f652010-08-21 09:40:31 +00003942 ParamInfo.push_back(DeclaratorChunk::ParamInfo(FirstIdent, FirstIdentLoc, 0));
Mike Stump1eb44332009-09-09 15:08:12 +00003943
Chris Lattner66d28652008-04-06 06:34:08 +00003944 while (Tok.is(tok::comma)) {
3945 // Eat the comma.
3946 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00003947
Chris Lattner50c64772008-04-06 06:39:19 +00003948 // If this isn't an identifier, report the error and skip until ')'.
Chris Lattner66d28652008-04-06 06:34:08 +00003949 if (Tok.isNot(tok::identifier)) {
3950 Diag(Tok, diag::err_expected_ident);
Chris Lattner50c64772008-04-06 06:39:19 +00003951 SkipUntil(tok::r_paren);
3952 return;
Chris Lattner66d28652008-04-06 06:34:08 +00003953 }
Chris Lattneraaf9ddb2008-04-06 06:47:48 +00003954
Chris Lattner66d28652008-04-06 06:34:08 +00003955 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
Chris Lattneraaf9ddb2008-04-06 06:47:48 +00003956
3957 // Reject 'typedef int y; int test(x, y)', but continue parsing.
Douglas Gregor23c94db2010-07-02 17:43:08 +00003958 if (Actions.getTypeName(*ParmII, Tok.getLocation(), getCurScope()))
Chris Lattnerda83bac2008-11-19 07:37:42 +00003959 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
Mike Stump1eb44332009-09-09 15:08:12 +00003960
Chris Lattner66d28652008-04-06 06:34:08 +00003961 // Verify that the argument identifier has not already been mentioned.
3962 if (!ParamsSoFar.insert(ParmII)) {
Chris Lattnerda83bac2008-11-19 07:37:42 +00003963 Diag(Tok, diag::err_param_redefinition) << ParmII;
Chris Lattner50c64772008-04-06 06:39:19 +00003964 } else {
3965 // Remember this identifier in ParamInfo.
Chris Lattner66d28652008-04-06 06:34:08 +00003966 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Chris Lattnerb28317a2009-03-28 19:18:32 +00003967 Tok.getLocation(),
John McCalld226f652010-08-21 09:40:31 +00003968 0));
Chris Lattner50c64772008-04-06 06:39:19 +00003969 }
Mike Stump1eb44332009-09-09 15:08:12 +00003970
Chris Lattner66d28652008-04-06 06:34:08 +00003971 // Eat the identifier.
3972 ConsumeToken();
3973 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00003974
3975 // If we have the closing ')', eat it and we're done.
3976 SourceLocation RLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
3977
Chris Lattner50c64772008-04-06 06:39:19 +00003978 // Remember that we parsed a function type, and remember the attributes. This
3979 // function type is always a K&R style function type, which is not varargs and
3980 // has no prototype.
John McCall0b7e6782011-03-24 11:26:52 +00003981 ParsedAttributes attrs(AttrFactory);
3982 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/false, /*varargs*/false,
Douglas Gregor965acbb2009-02-18 07:07:28 +00003983 SourceLocation(),
Chris Lattner50c64772008-04-06 06:39:19 +00003984 &ParamInfo[0], ParamInfo.size(),
Sebastian Redl7dc81342009-04-29 17:30:04 +00003985 /*TypeQuals*/0,
Douglas Gregor83f51722011-01-26 03:43:54 +00003986 true, SourceLocation(),
Sebastian Redl6e5d3192011-03-05 22:42:13 +00003987 EST_None, SourceLocation(), 0, 0,
3988 0, 0, LParenLoc, RLoc, D),
John McCall0b7e6782011-03-24 11:26:52 +00003989 attrs, RLoc);
Chris Lattner66d28652008-04-06 06:34:08 +00003990}
Chris Lattneref4715c2008-04-06 05:45:57 +00003991
Reid Spencer5f016e22007-07-11 17:01:13 +00003992/// [C90] direct-declarator '[' constant-expression[opt] ']'
3993/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
3994/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
3995/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
3996/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
3997void Parser::ParseBracketDeclarator(Declarator &D) {
3998 SourceLocation StartLoc = ConsumeBracket();
Mike Stump1eb44332009-09-09 15:08:12 +00003999
Chris Lattner378c7e42008-12-18 07:27:21 +00004000 // C array syntax has many features, but by-far the most common is [] and [4].
4001 // This code does a fast path to handle some of the most obvious cases.
4002 if (Tok.getKind() == tok::r_square) {
Sebastian Redlab197ba2009-02-09 18:23:29 +00004003 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
John McCall0b7e6782011-03-24 11:26:52 +00004004 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00004005 MaybeParseCXX0XAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00004006
Chris Lattner378c7e42008-12-18 07:27:21 +00004007 // Remember that we parsed the empty array type.
John McCall60d7b3a2010-08-24 06:29:42 +00004008 ExprResult NumElements;
John McCall0b7e6782011-03-24 11:26:52 +00004009 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00004010 StartLoc, EndLoc),
John McCall0b7e6782011-03-24 11:26:52 +00004011 attrs, EndLoc);
Chris Lattner378c7e42008-12-18 07:27:21 +00004012 return;
4013 } else if (Tok.getKind() == tok::numeric_constant &&
4014 GetLookAheadToken(1).is(tok::r_square)) {
4015 // [4] is very common. Parse the numeric constant expression.
John McCall60d7b3a2010-08-24 06:29:42 +00004016 ExprResult ExprRes(Actions.ActOnNumericConstant(Tok));
Chris Lattner378c7e42008-12-18 07:27:21 +00004017 ConsumeToken();
4018
Sebastian Redlab197ba2009-02-09 18:23:29 +00004019 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
John McCall0b7e6782011-03-24 11:26:52 +00004020 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00004021 MaybeParseCXX0XAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00004022
Chris Lattner378c7e42008-12-18 07:27:21 +00004023 // Remember that we parsed a array type, and remember its features.
John McCall0b7e6782011-03-24 11:26:52 +00004024 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, 0,
John McCall7f040a92010-12-24 02:08:15 +00004025 ExprRes.release(),
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00004026 StartLoc, EndLoc),
John McCall0b7e6782011-03-24 11:26:52 +00004027 attrs, EndLoc);
Chris Lattner378c7e42008-12-18 07:27:21 +00004028 return;
4029 }
Mike Stump1eb44332009-09-09 15:08:12 +00004030
Reid Spencer5f016e22007-07-11 17:01:13 +00004031 // If valid, this location is the position where we read the 'static' keyword.
4032 SourceLocation StaticLoc;
Chris Lattner04d66662007-10-09 17:33:22 +00004033 if (Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00004034 StaticLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00004035
Reid Spencer5f016e22007-07-11 17:01:13 +00004036 // If there is a type-qualifier-list, read it now.
Chris Lattnera1fcbad2008-12-18 06:50:14 +00004037 // Type qualifiers in an array subscript are a C99 feature.
John McCall0b7e6782011-03-24 11:26:52 +00004038 DeclSpec DS(AttrFactory);
Chris Lattner5a69d1c2008-12-18 07:02:59 +00004039 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Mike Stump1eb44332009-09-09 15:08:12 +00004040
Reid Spencer5f016e22007-07-11 17:01:13 +00004041 // If we haven't already read 'static', check to see if there is one after the
4042 // type-qualifier-list.
Chris Lattner04d66662007-10-09 17:33:22 +00004043 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00004044 StaticLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00004045
Reid Spencer5f016e22007-07-11 17:01:13 +00004046 // Handle "direct-declarator [ type-qual-list[opt] * ]".
4047 bool isStar = false;
John McCall60d7b3a2010-08-24 06:29:42 +00004048 ExprResult NumElements;
Mike Stump1eb44332009-09-09 15:08:12 +00004049
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00004050 // Handle the case where we have '[*]' as the array size. However, a leading
4051 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
4052 // the the token after the star is a ']'. Since stars in arrays are
4053 // infrequent, use of lookahead is not costly here.
4054 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattnera711dd02008-04-06 05:27:21 +00004055 ConsumeToken(); // Eat the '*'.
Reid Spencer5f016e22007-07-11 17:01:13 +00004056
Chris Lattnera1fcbad2008-12-18 06:50:14 +00004057 if (StaticLoc.isValid()) {
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00004058 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattnera1fcbad2008-12-18 06:50:14 +00004059 StaticLoc = SourceLocation(); // Drop the static.
4060 }
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00004061 isStar = true;
Chris Lattner04d66662007-10-09 17:33:22 +00004062 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner378c7e42008-12-18 07:27:21 +00004063 // Note, in C89, this production uses the constant-expr production instead
4064 // of assignment-expr. The only difference is that assignment-expr allows
4065 // things like '=' and '*='. Sema rejects these in C89 mode because they
4066 // are not i-c-e's, so we don't need to distinguish between the two here.
Mike Stump1eb44332009-09-09 15:08:12 +00004067
Douglas Gregore0762c92009-06-19 23:52:42 +00004068 // Parse the constant-expression or assignment-expression now (depending
4069 // on dialect).
4070 if (getLang().CPlusPlus)
4071 NumElements = ParseConstantExpression();
4072 else
4073 NumElements = ParseAssignmentExpression();
Reid Spencer5f016e22007-07-11 17:01:13 +00004074 }
Mike Stump1eb44332009-09-09 15:08:12 +00004075
Reid Spencer5f016e22007-07-11 17:01:13 +00004076 // If there was an error parsing the assignment-expression, recover.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00004077 if (NumElements.isInvalid()) {
Chris Lattner5cb10d32009-04-24 22:30:50 +00004078 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00004079 // If the expression was invalid, skip it.
4080 SkipUntil(tok::r_square);
4081 return;
4082 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00004083
4084 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
4085
John McCall0b7e6782011-03-24 11:26:52 +00004086 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00004087 MaybeParseCXX0XAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00004088
Chris Lattner378c7e42008-12-18 07:27:21 +00004089 // Remember that we parsed a array type, and remember its features.
John McCall0b7e6782011-03-24 11:26:52 +00004090 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
Reid Spencer5f016e22007-07-11 17:01:13 +00004091 StaticLoc.isValid(), isStar,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00004092 NumElements.release(),
4093 StartLoc, EndLoc),
John McCall0b7e6782011-03-24 11:26:52 +00004094 attrs, EndLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00004095}
4096
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004097/// [GNU] typeof-specifier:
4098/// typeof ( expressions )
4099/// typeof ( type-name )
4100/// [GNU/C++] typeof unary-expression
Steve Naroffd1861fd2007-07-31 12:34:36 +00004101///
4102void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner04d66662007-10-09 17:33:22 +00004103 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004104 Token OpTok = Tok;
Steve Naroffd1861fd2007-07-31 12:34:36 +00004105 SourceLocation StartLoc = ConsumeToken();
4106
John McCallcfb708c2010-01-13 20:03:27 +00004107 const bool hasParens = Tok.is(tok::l_paren);
4108
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004109 bool isCastExpr;
John McCallb3d87482010-08-24 05:47:05 +00004110 ParsedType CastTy;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004111 SourceRange CastRange;
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00004112 ExprResult Operand = ParseExprAfterUnaryExprOrTypeTrait(OpTok, isCastExpr,
4113 CastTy, CastRange);
John McCallcfb708c2010-01-13 20:03:27 +00004114 if (hasParens)
4115 DS.setTypeofParensRange(CastRange);
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004116
4117 if (CastRange.getEnd().isInvalid())
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004118 // FIXME: Not accurate, the range gets one token more than it should.
4119 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004120 else
4121 DS.SetRangeEnd(CastRange.getEnd());
Mike Stump1eb44332009-09-09 15:08:12 +00004122
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004123 if (isCastExpr) {
4124 if (!CastTy) {
4125 DS.SetTypeSpecError();
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004126 return;
Douglas Gregor809070a2009-02-18 17:45:20 +00004127 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004128
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004129 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00004130 unsigned DiagID;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004131 // Check for duplicate type specifiers (e.g. "int typeof(int)").
4132 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00004133 DiagID, CastTy))
4134 Diag(StartLoc, DiagID) << PrevSpec;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004135 return;
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004136 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004137
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004138 // If we get here, the operand to the typeof was an expresion.
4139 if (Operand.isInvalid()) {
4140 DS.SetTypeSpecError();
Steve Naroff9dfa7b42007-08-02 02:53:48 +00004141 return;
Steve Naroffd1861fd2007-07-31 12:34:36 +00004142 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004143
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004144 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00004145 unsigned DiagID;
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004146 // Check for duplicate type specifiers (e.g. "int typeof(int)").
4147 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
John McCallb3d87482010-08-24 05:47:05 +00004148 DiagID, Operand.get()))
John McCallfec54012009-08-03 20:12:06 +00004149 Diag(StartLoc, DiagID) << PrevSpec;
Steve Naroffd1861fd2007-07-31 12:34:36 +00004150}
Chris Lattner1b492422010-02-28 18:33:55 +00004151
4152
4153/// TryAltiVecVectorTokenOutOfLine - Out of line body that should only be called
4154/// from TryAltiVecVectorToken.
4155bool Parser::TryAltiVecVectorTokenOutOfLine() {
4156 Token Next = NextToken();
4157 switch (Next.getKind()) {
4158 default: return false;
4159 case tok::kw_short:
4160 case tok::kw_long:
4161 case tok::kw_signed:
4162 case tok::kw_unsigned:
4163 case tok::kw_void:
4164 case tok::kw_char:
4165 case tok::kw_int:
4166 case tok::kw_float:
4167 case tok::kw_double:
4168 case tok::kw_bool:
4169 case tok::kw___pixel:
4170 Tok.setKind(tok::kw___vector);
4171 return true;
4172 case tok::identifier:
4173 if (Next.getIdentifierInfo() == Ident_pixel) {
4174 Tok.setKind(tok::kw___vector);
4175 return true;
4176 }
4177 return false;
4178 }
4179}
4180
4181bool Parser::TryAltiVecTokenOutOfLine(DeclSpec &DS, SourceLocation Loc,
4182 const char *&PrevSpec, unsigned &DiagID,
4183 bool &isInvalid) {
4184 if (Tok.getIdentifierInfo() == Ident_vector) {
4185 Token Next = NextToken();
4186 switch (Next.getKind()) {
4187 case tok::kw_short:
4188 case tok::kw_long:
4189 case tok::kw_signed:
4190 case tok::kw_unsigned:
4191 case tok::kw_void:
4192 case tok::kw_char:
4193 case tok::kw_int:
4194 case tok::kw_float:
4195 case tok::kw_double:
4196 case tok::kw_bool:
4197 case tok::kw___pixel:
4198 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
4199 return true;
4200 case tok::identifier:
4201 if (Next.getIdentifierInfo() == Ident_pixel) {
4202 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
4203 return true;
4204 }
4205 break;
4206 default:
4207 break;
4208 }
Douglas Gregora8f031f2010-06-16 15:28:57 +00004209 } else if ((Tok.getIdentifierInfo() == Ident_pixel) &&
Chris Lattner1b492422010-02-28 18:33:55 +00004210 DS.isTypeAltiVecVector()) {
4211 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
4212 return true;
4213 }
4214 return false;
4215}