blob: 9e430a42968f4a15b54942787634f0071d5a767f [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"
John McCall19510852010-08-20 18:27:03 +000016#include "clang/Sema/Scope.h"
17#include "clang/Sema/ParsedTemplate.h"
John McCallf312b1e2010-08-26 23:41:50 +000018#include "clang/Sema/PrettyDeclStackTrace.h"
Chris Lattnerd167ca02009-12-10 00:21:05 +000019#include "RAIIObjectsForParser.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000020#include "llvm/ADT/SmallSet.h"
21using namespace clang;
22
23//===----------------------------------------------------------------------===//
24// C99 6.7: Declarations.
25//===----------------------------------------------------------------------===//
26
27/// ParseTypeName
28/// type-name: [C99 6.7.6]
29/// specifier-qualifier-list abstract-declarator[opt]
Sebastian Redl4c5d3202008-11-21 19:14:01 +000030///
31/// Called type-id in C++.
John McCallf312b1e2010-08-26 23:41:50 +000032TypeResult Parser::ParseTypeName(SourceRange *Range) {
Reid Spencer5f016e22007-07-11 17:01:13 +000033 // Parse the common declaration-specifiers piece.
34 DeclSpec DS;
35 ParseSpecifierQualifierList(DS);
Sebastian Redlef65f062009-05-29 18:02:33 +000036
Reid Spencer5f016e22007-07-11 17:01:13 +000037 // Parse the abstract-declarator, if present.
38 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
39 ParseDeclarator(DeclaratorInfo);
Sebastian Redlef65f062009-05-29 18:02:33 +000040 if (Range)
41 *Range = DeclaratorInfo.getSourceRange();
42
Chris Lattnereaaebc72009-04-25 08:06:05 +000043 if (DeclaratorInfo.isInvalidType())
Douglas Gregor809070a2009-02-18 17:45:20 +000044 return true;
45
Douglas Gregor23c94db2010-07-02 17:43:08 +000046 return Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
Reid Spencer5f016e22007-07-11 17:01:13 +000047}
48
Sean Huntbbd37c62009-11-21 08:43:09 +000049/// ParseGNUAttributes - Parse a non-empty attributes list.
Reid Spencer5f016e22007-07-11 17:01:13 +000050///
51/// [GNU] attributes:
52/// attribute
53/// attributes attribute
54///
55/// [GNU] attribute:
56/// '__attribute__' '(' '(' attribute-list ')' ')'
57///
58/// [GNU] attribute-list:
59/// attrib
60/// attribute_list ',' attrib
61///
62/// [GNU] attrib:
63/// empty
64/// attrib-name
65/// attrib-name '(' identifier ')'
66/// attrib-name '(' identifier ',' nonempty-expr-list ')'
67/// attrib-name '(' argument-expression-list [C99 6.5.2] ')'
68///
69/// [GNU] attrib-name:
70/// identifier
71/// typespec
72/// typequal
73/// storageclass
Mike Stump1eb44332009-09-09 15:08:12 +000074///
Reid Spencer5f016e22007-07-11 17:01:13 +000075/// FIXME: The GCC grammar/code for this construct implies we need two
Mike Stump1eb44332009-09-09 15:08:12 +000076/// token lookahead. Comment from gcc: "If they start with an identifier
77/// which is followed by a comma or close parenthesis, then the arguments
Reid Spencer5f016e22007-07-11 17:01:13 +000078/// start with that identifier; otherwise they are an expression list."
79///
80/// At the moment, I am not doing 2 token lookahead. I am also unaware of
81/// any attributes that don't work (based on my limited testing). Most
82/// attributes are very simple in practice. Until we find a bug, I don't see
83/// a pressing need to implement the 2 token lookahead.
84
Sean Huntbbd37c62009-11-21 08:43:09 +000085AttributeList *Parser::ParseGNUAttributes(SourceLocation *EndLoc) {
86 assert(Tok.is(tok::kw___attribute) && "Not a GNU attribute list!");
Mike Stump1eb44332009-09-09 15:08:12 +000087
Reid Spencer5f016e22007-07-11 17:01:13 +000088 AttributeList *CurrAttr = 0;
Mike Stump1eb44332009-09-09 15:08:12 +000089
Chris Lattner04d66662007-10-09 17:33:22 +000090 while (Tok.is(tok::kw___attribute)) {
Reid Spencer5f016e22007-07-11 17:01:13 +000091 ConsumeToken();
92 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
93 "attribute")) {
94 SkipUntil(tok::r_paren, true); // skip until ) or ;
95 return CurrAttr;
96 }
97 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, "(")) {
98 SkipUntil(tok::r_paren, true); // skip until ) or ;
99 return CurrAttr;
100 }
101 // Parse the attribute-list. e.g. __attribute__(( weak, alias("__f") ))
Chris Lattner04d66662007-10-09 17:33:22 +0000102 while (Tok.is(tok::identifier) || isDeclarationSpecifier() ||
103 Tok.is(tok::comma)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000104
105 if (Tok.is(tok::comma)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000106 // allows for empty/non-empty attributes. ((__vector_size__(16),,,,))
107 ConsumeToken();
108 continue;
109 }
110 // we have an identifier or declaration specifier (const, int, etc.)
111 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
112 SourceLocation AttrNameLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000113
Douglas Gregorec1afbf2010-03-16 19:09:18 +0000114 // check if we have a "parameterized" attribute
Chris Lattner04d66662007-10-09 17:33:22 +0000115 if (Tok.is(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000116 ConsumeParen(); // ignore the left paren loc for now
Mike Stump1eb44332009-09-09 15:08:12 +0000117
Chris Lattner04d66662007-10-09 17:33:22 +0000118 if (Tok.is(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000119 IdentifierInfo *ParmName = Tok.getIdentifierInfo();
120 SourceLocation ParmLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000121
122 if (Tok.is(tok::r_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000123 // __attribute__(( mode(byte) ))
124 ConsumeParen(); // ignore the right paren loc for now
Sean Huntbbd37c62009-11-21 08:43:09 +0000125 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0, AttrNameLoc,
Reid Spencer5f016e22007-07-11 17:01:13 +0000126 ParmName, ParmLoc, 0, 0, CurrAttr);
Chris Lattner04d66662007-10-09 17:33:22 +0000127 } else if (Tok.is(tok::comma)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000128 ConsumeToken();
129 // __attribute__(( format(printf, 1, 2) ))
Sebastian Redla55e52c2008-11-25 22:21:31 +0000130 ExprVector ArgExprs(Actions);
Reid Spencer5f016e22007-07-11 17:01:13 +0000131 bool ArgExprsOk = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000132
Reid Spencer5f016e22007-07-11 17:01:13 +0000133 // now parse the non-empty comma separated list of expressions
134 while (1) {
John McCall60d7b3a2010-08-24 06:29:42 +0000135 ExprResult ArgExpr(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000136 if (ArgExpr.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000137 ArgExprsOk = false;
138 SkipUntil(tok::r_paren);
139 break;
140 } else {
Sebastian Redleffa8d12008-12-10 00:02:53 +0000141 ArgExprs.push_back(ArgExpr.release());
Reid Spencer5f016e22007-07-11 17:01:13 +0000142 }
Chris Lattner04d66662007-10-09 17:33:22 +0000143 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +0000144 break;
145 ConsumeToken(); // Eat the comma, move to the next argument
146 }
Chris Lattner04d66662007-10-09 17:33:22 +0000147 if (ArgExprsOk && Tok.is(tok::r_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000148 ConsumeParen(); // ignore the right paren loc for now
Sean Huntbbd37c62009-11-21 08:43:09 +0000149 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0,
150 AttrNameLoc, ParmName, ParmLoc,
151 ArgExprs.take(), ArgExprs.size(),
152 CurrAttr);
Reid Spencer5f016e22007-07-11 17:01:13 +0000153 }
154 }
155 } else { // not an identifier
Nate Begeman6f3d8382009-06-26 06:32:41 +0000156 switch (Tok.getKind()) {
157 case tok::r_paren:
Reid Spencer5f016e22007-07-11 17:01:13 +0000158 // parse a possibly empty comma separated list of expressions
Reid Spencer5f016e22007-07-11 17:01:13 +0000159 // __attribute__(( nonnull() ))
160 ConsumeParen(); // ignore the right paren loc for now
Sean Huntbbd37c62009-11-21 08:43:09 +0000161 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0, AttrNameLoc,
Reid Spencer5f016e22007-07-11 17:01:13 +0000162 0, SourceLocation(), 0, 0, CurrAttr);
Nate Begeman6f3d8382009-06-26 06:32:41 +0000163 break;
164 case tok::kw_char:
165 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000166 case tok::kw_char16_t:
167 case tok::kw_char32_t:
Nate Begeman6f3d8382009-06-26 06:32:41 +0000168 case tok::kw_bool:
169 case tok::kw_short:
170 case tok::kw_int:
171 case tok::kw_long:
172 case tok::kw_signed:
173 case tok::kw_unsigned:
174 case tok::kw_float:
175 case tok::kw_double:
176 case tok::kw_void:
177 case tok::kw_typeof:
Fariborz Jahanian1b72fa72010-08-17 23:19:16 +0000178 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0, AttrNameLoc,
179 0, SourceLocation(), 0, 0, CurrAttr);
180 if (CurrAttr->getKind() == AttributeList::AT_IBOutletCollection)
181 Diag(Tok, diag::err_iboutletcollection_builtintype);
Nate Begeman6f3d8382009-06-26 06:32:41 +0000182 // If it's a builtin type name, eat it and expect a rparen
183 // __attribute__(( vec_type_hint(char) ))
184 ConsumeToken();
Nate Begeman6f3d8382009-06-26 06:32:41 +0000185 if (Tok.is(tok::r_paren))
186 ConsumeParen();
187 break;
188 default:
Reid Spencer5f016e22007-07-11 17:01:13 +0000189 // __attribute__(( aligned(16) ))
Sebastian Redla55e52c2008-11-25 22:21:31 +0000190 ExprVector ArgExprs(Actions);
Reid Spencer5f016e22007-07-11 17:01:13 +0000191 bool ArgExprsOk = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000192
Reid Spencer5f016e22007-07-11 17:01:13 +0000193 // now parse the list of expressions
194 while (1) {
John McCall60d7b3a2010-08-24 06:29:42 +0000195 ExprResult ArgExpr(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000196 if (ArgExpr.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000197 ArgExprsOk = false;
198 SkipUntil(tok::r_paren);
199 break;
200 } else {
Sebastian Redleffa8d12008-12-10 00:02:53 +0000201 ArgExprs.push_back(ArgExpr.release());
Reid Spencer5f016e22007-07-11 17:01:13 +0000202 }
Chris Lattner04d66662007-10-09 17:33:22 +0000203 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +0000204 break;
205 ConsumeToken(); // Eat the comma, move to the next argument
206 }
207 // Match the ')'.
Chris Lattner04d66662007-10-09 17:33:22 +0000208 if (ArgExprsOk && Tok.is(tok::r_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000209 ConsumeParen(); // ignore the right paren loc for now
Sebastian Redla55e52c2008-11-25 22:21:31 +0000210 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0,
Sean Huntbbd37c62009-11-21 08:43:09 +0000211 AttrNameLoc, 0, SourceLocation(), ArgExprs.take(),
212 ArgExprs.size(),
Reid Spencer5f016e22007-07-11 17:01:13 +0000213 CurrAttr);
214 }
Nate Begeman6f3d8382009-06-26 06:32:41 +0000215 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000216 }
217 }
218 } else {
Sean Huntbbd37c62009-11-21 08:43:09 +0000219 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0, AttrNameLoc,
Reid Spencer5f016e22007-07-11 17:01:13 +0000220 0, SourceLocation(), 0, 0, CurrAttr);
221 }
222 }
223 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
Reid Spencer5f016e22007-07-11 17:01:13 +0000224 SkipUntil(tok::r_paren, false);
Sean Huntbbd37c62009-11-21 08:43:09 +0000225 SourceLocation Loc = Tok.getLocation();
Sebastian Redlab197ba2009-02-09 18:23:29 +0000226 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) {
227 SkipUntil(tok::r_paren, false);
228 }
229 if (EndLoc)
230 *EndLoc = Loc;
Reid Spencer5f016e22007-07-11 17:01:13 +0000231 }
232 return CurrAttr;
233}
234
Eli Friedmana23b4852009-06-08 07:21:15 +0000235/// ParseMicrosoftDeclSpec - Parse an __declspec construct
236///
237/// [MS] decl-specifier:
238/// __declspec ( extended-decl-modifier-seq )
239///
240/// [MS] extended-decl-modifier-seq:
241/// extended-decl-modifier[opt]
242/// extended-decl-modifier extended-decl-modifier-seq
243
Eli Friedman290eeb02009-06-08 23:27:34 +0000244AttributeList* Parser::ParseMicrosoftDeclSpec(AttributeList *CurrAttr) {
Steve Narofff59e17e2008-12-24 20:59:21 +0000245 assert(Tok.is(tok::kw___declspec) && "Not a declspec!");
Eli Friedmana23b4852009-06-08 07:21:15 +0000246
Steve Narofff59e17e2008-12-24 20:59:21 +0000247 ConsumeToken();
Eli Friedmana23b4852009-06-08 07:21:15 +0000248 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
249 "declspec")) {
250 SkipUntil(tok::r_paren, true); // skip until ) or ;
251 return CurrAttr;
252 }
Eli Friedman290eeb02009-06-08 23:27:34 +0000253 while (Tok.getIdentifierInfo()) {
Eli Friedmana23b4852009-06-08 07:21:15 +0000254 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
255 SourceLocation AttrNameLoc = ConsumeToken();
256 if (Tok.is(tok::l_paren)) {
257 ConsumeParen();
258 // FIXME: This doesn't parse __declspec(property(get=get_func_name))
259 // correctly.
John McCall60d7b3a2010-08-24 06:29:42 +0000260 ExprResult ArgExpr(ParseAssignmentExpression());
Eli Friedmana23b4852009-06-08 07:21:15 +0000261 if (!ArgExpr.isInvalid()) {
John McCallca0408f2010-08-23 06:44:23 +0000262 Expr *ExprList = ArgExpr.take();
Sean Huntbbd37c62009-11-21 08:43:09 +0000263 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
Eli Friedmana23b4852009-06-08 07:21:15 +0000264 SourceLocation(), &ExprList, 1,
265 CurrAttr, true);
266 }
267 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
268 SkipUntil(tok::r_paren, false);
269 } else {
Sean Huntbbd37c62009-11-21 08:43:09 +0000270 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0, AttrNameLoc,
271 0, SourceLocation(), 0, 0, CurrAttr, true);
Eli Friedmana23b4852009-06-08 07:21:15 +0000272 }
273 }
274 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
275 SkipUntil(tok::r_paren, false);
Eli Friedman290eeb02009-06-08 23:27:34 +0000276 return CurrAttr;
277}
278
279AttributeList* Parser::ParseMicrosoftTypeAttributes(AttributeList *CurrAttr) {
280 // Treat these like attributes
281 // FIXME: Allow Sema to distinguish between these and real attributes!
282 while (Tok.is(tok::kw___fastcall) || Tok.is(tok::kw___stdcall) ||
Douglas Gregorf813a2c2010-05-18 16:57:00 +0000283 Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___cdecl) ||
284 Tok.is(tok::kw___ptr64) || Tok.is(tok::kw___w64)) {
Eli Friedman290eeb02009-06-08 23:27:34 +0000285 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
286 SourceLocation AttrNameLoc = ConsumeToken();
287 if (Tok.is(tok::kw___ptr64) || Tok.is(tok::kw___w64))
288 // FIXME: Support these properly!
289 continue;
Sean Huntbbd37c62009-11-21 08:43:09 +0000290 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
Eli Friedman290eeb02009-06-08 23:27:34 +0000291 SourceLocation(), 0, 0, CurrAttr, true);
292 }
293 return CurrAttr;
Steve Narofff59e17e2008-12-24 20:59:21 +0000294}
295
Dawn Perchik52fc3142010-09-03 01:29:35 +0000296AttributeList* Parser::ParseBorlandTypeAttributes(AttributeList *CurrAttr) {
297 // Treat these like attributes
298 while (Tok.is(tok::kw___pascal)) {
299 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
300 SourceLocation AttrNameLoc = ConsumeToken();
301 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
302 SourceLocation(), 0, 0, CurrAttr, true);
303 }
304 return CurrAttr;
305}
306
Reid Spencer5f016e22007-07-11 17:01:13 +0000307/// ParseDeclaration - Parse a full 'declaration', which consists of
308/// declaration-specifiers, some number of declarators, and a semicolon.
Chris Lattner97144fc2009-04-02 04:16:50 +0000309/// 'Context' should be a Declarator::TheContext value. This returns the
310/// location of the semicolon in DeclEnd.
Chris Lattner8f08cb72007-08-25 06:57:03 +0000311///
312/// declaration: [C99 6.7]
313/// block-declaration ->
314/// simple-declaration
315/// others [FIXME]
Douglas Gregoradcac882008-12-01 23:54:00 +0000316/// [C++] template-declaration
Chris Lattner8f08cb72007-08-25 06:57:03 +0000317/// [C++] namespace-definition
Douglas Gregorf780abc2008-12-30 03:27:21 +0000318/// [C++] using-directive
Douglas Gregord7f37bf2009-06-22 23:06:13 +0000319/// [C++] using-declaration
Sebastian Redl50de12f2009-03-24 22:27:57 +0000320/// [C++0x] static_assert-declaration
Chris Lattner8f08cb72007-08-25 06:57:03 +0000321/// others... [FIXME]
322///
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +0000323Parser::DeclGroupPtrTy Parser::ParseDeclaration(StmtVector &Stmts,
324 unsigned Context,
Sean Huntbbd37c62009-11-21 08:43:09 +0000325 SourceLocation &DeclEnd,
326 CXX0XAttributeList Attr) {
Argyrios Kyrtzidis36d36802010-06-17 10:52:18 +0000327 ParenBraceBracketBalancer BalancerRAIIObj(*this);
328
John McCalld226f652010-08-21 09:40:31 +0000329 Decl *SingleDecl = 0;
Chris Lattner8f08cb72007-08-25 06:57:03 +0000330 switch (Tok.getKind()) {
Douglas Gregoradcac882008-12-01 23:54:00 +0000331 case tok::kw_template:
Douglas Gregor1426e532009-05-12 21:31:51 +0000332 case tok::kw_export:
Sean Huntbbd37c62009-11-21 08:43:09 +0000333 if (Attr.HasAttr)
334 Diag(Attr.Range.getBegin(), diag::err_attributes_not_allowed)
335 << Attr.Range;
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000336 SingleDecl = ParseDeclarationStartingWithTemplate(Context, DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +0000337 break;
Sebastian Redld078e642010-08-27 23:12:46 +0000338 case tok::kw_inline:
Sebastian Redl88e64ca2010-08-31 00:36:45 +0000339 // Could be the start of an inline namespace. Allowed as an ext in C++03.
340 if (getLang().CPlusPlus && NextToken().is(tok::kw_namespace)) {
Sebastian Redld078e642010-08-27 23:12:46 +0000341 if (Attr.HasAttr)
342 Diag(Attr.Range.getBegin(), diag::err_attributes_not_allowed)
343 << Attr.Range;
344 SourceLocation InlineLoc = ConsumeToken();
345 SingleDecl = ParseNamespace(Context, DeclEnd, InlineLoc);
346 break;
347 }
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +0000348 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, Attr.AttrList,
349 true);
Chris Lattner8f08cb72007-08-25 06:57:03 +0000350 case tok::kw_namespace:
Sean Huntbbd37c62009-11-21 08:43:09 +0000351 if (Attr.HasAttr)
352 Diag(Attr.Range.getBegin(), diag::err_attributes_not_allowed)
353 << Attr.Range;
Chris Lattner97144fc2009-04-02 04:16:50 +0000354 SingleDecl = ParseNamespace(Context, DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +0000355 break;
Douglas Gregorf780abc2008-12-30 03:27:21 +0000356 case tok::kw_using:
Sean Huntbbd37c62009-11-21 08:43:09 +0000357 SingleDecl = ParseUsingDirectiveOrDeclaration(Context, DeclEnd, Attr);
Chris Lattner682bf922009-03-29 16:50:03 +0000358 break;
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000359 case tok::kw_static_assert:
Sean Huntbbd37c62009-11-21 08:43:09 +0000360 if (Attr.HasAttr)
361 Diag(Attr.Range.getBegin(), diag::err_attributes_not_allowed)
362 << Attr.Range;
Chris Lattner97144fc2009-04-02 04:16:50 +0000363 SingleDecl = ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +0000364 break;
Chris Lattner8f08cb72007-08-25 06:57:03 +0000365 default:
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +0000366 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, Attr.AttrList,
367 true);
Chris Lattner8f08cb72007-08-25 06:57:03 +0000368 }
Sean Huntbbd37c62009-11-21 08:43:09 +0000369
Chris Lattner682bf922009-03-29 16:50:03 +0000370 // This routine returns a DeclGroup, if the thing we parsed only contains a
371 // single decl, convert it now.
372 return Actions.ConvertDeclToDeclGroup(SingleDecl);
Chris Lattner8f08cb72007-08-25 06:57:03 +0000373}
374
375/// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl]
376/// declaration-specifiers init-declarator-list[opt] ';'
377///[C90/C++]init-declarator-list ';' [TODO]
378/// [OMP] threadprivate-directive [TODO]
Chris Lattnercd147752009-03-29 17:27:48 +0000379///
380/// If RequireSemi is false, this does not check for a ';' at the end of the
Chris Lattner5c5db552010-04-05 18:18:31 +0000381/// declaration. If it is true, it checks for and eats it.
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +0000382Parser::DeclGroupPtrTy Parser::ParseSimpleDeclaration(StmtVector &Stmts,
383 unsigned Context,
Sean Huntbbd37c62009-11-21 08:43:09 +0000384 SourceLocation &DeclEnd,
Chris Lattner5c5db552010-04-05 18:18:31 +0000385 AttributeList *Attr,
386 bool RequireSemi) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000387 // Parse the common declaration-specifiers piece.
John McCall54abf7d2009-11-04 02:18:39 +0000388 ParsingDeclSpec DS(*this);
Sean Huntbbd37c62009-11-21 08:43:09 +0000389 if (Attr)
390 DS.AddAttributes(Attr);
Douglas Gregor0efc2c12010-01-13 17:31:36 +0000391 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS_none,
392 getDeclSpecContextFromDeclaratorContext(Context));
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +0000393 StmtResult R = Actions.ActOnVlaStmt(DS);
394 if (R.isUsable())
395 Stmts.push_back(R.release());
Mike Stump1eb44332009-09-09 15:08:12 +0000396
Reid Spencer5f016e22007-07-11 17:01:13 +0000397 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
398 // declaration-specifiers init-declarator-list[opt] ';'
Chris Lattner04d66662007-10-09 17:33:22 +0000399 if (Tok.is(tok::semi)) {
Chris Lattner5c5db552010-04-05 18:18:31 +0000400 if (RequireSemi) ConsumeToken();
John McCalld226f652010-08-21 09:40:31 +0000401 Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none,
John McCallaec03712010-05-21 20:45:30 +0000402 DS);
John McCall54abf7d2009-11-04 02:18:39 +0000403 DS.complete(TheDecl);
Chris Lattner682bf922009-03-29 16:50:03 +0000404 return Actions.ConvertDeclToDeclGroup(TheDecl);
Reid Spencer5f016e22007-07-11 17:01:13 +0000405 }
Mike Stump1eb44332009-09-09 15:08:12 +0000406
Chris Lattner5c5db552010-04-05 18:18:31 +0000407 return ParseDeclGroup(DS, Context, /*FunctionDefs=*/ false, &DeclEnd);
John McCalld8ac0572009-11-03 19:26:08 +0000408}
Mike Stump1eb44332009-09-09 15:08:12 +0000409
John McCalld8ac0572009-11-03 19:26:08 +0000410/// ParseDeclGroup - Having concluded that this is either a function
411/// definition or a group of object declarations, actually parse the
412/// result.
John McCall54abf7d2009-11-04 02:18:39 +0000413Parser::DeclGroupPtrTy Parser::ParseDeclGroup(ParsingDeclSpec &DS,
414 unsigned Context,
John McCalld8ac0572009-11-03 19:26:08 +0000415 bool AllowFunctionDefinitions,
416 SourceLocation *DeclEnd) {
417 // Parse the first declarator.
John McCall54abf7d2009-11-04 02:18:39 +0000418 ParsingDeclarator D(*this, DS, static_cast<Declarator::TheContext>(Context));
John McCalld8ac0572009-11-03 19:26:08 +0000419 ParseDeclarator(D);
Chris Lattnercd147752009-03-29 17:27:48 +0000420
John McCalld8ac0572009-11-03 19:26:08 +0000421 // Bail out if the first declarator didn't seem well-formed.
422 if (!D.hasName() && !D.mayOmitIdentifier()) {
423 // Skip until ; or }.
424 SkipUntil(tok::r_brace, true, true);
425 if (Tok.is(tok::semi))
426 ConsumeToken();
427 return DeclGroupPtrTy();
Chris Lattner23c4b182009-03-29 17:18:04 +0000428 }
Mike Stump1eb44332009-09-09 15:08:12 +0000429
Chris Lattnerc82daef2010-07-11 22:24:20 +0000430 // Check to see if we have a function *definition* which must have a body.
431 if (AllowFunctionDefinitions && D.isFunctionDeclarator() &&
432 // Look at the next token to make sure that this isn't a function
433 // declaration. We have to check this because __attribute__ might be the
434 // start of a function definition in GCC-extended K&R C.
435 !isDeclarationAfterDeclarator()) {
436
Chris Lattner004659a2010-07-11 22:42:07 +0000437 if (isStartOfFunctionDefinition(D)) {
John McCalld8ac0572009-11-03 19:26:08 +0000438 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
439 Diag(Tok, diag::err_function_declared_typedef);
440
441 // Recover by treating the 'typedef' as spurious.
442 DS.ClearStorageClassSpecs();
443 }
444
John McCalld226f652010-08-21 09:40:31 +0000445 Decl *TheDecl = ParseFunctionDefinition(D);
John McCalld8ac0572009-11-03 19:26:08 +0000446 return Actions.ConvertDeclToDeclGroup(TheDecl);
Chris Lattner004659a2010-07-11 22:42:07 +0000447 }
448
449 if (isDeclarationSpecifier()) {
450 // If there is an invalid declaration specifier right after the function
451 // prototype, then we must be in a missing semicolon case where this isn't
452 // actually a body. Just fall through into the code that handles it as a
453 // prototype, and let the top-level code handle the erroneous declspec
454 // where it would otherwise expect a comma or semicolon.
John McCalld8ac0572009-11-03 19:26:08 +0000455 } else {
456 Diag(Tok, diag::err_expected_fn_body);
457 SkipUntil(tok::semi);
458 return DeclGroupPtrTy();
459 }
460 }
461
John McCalld226f652010-08-21 09:40:31 +0000462 llvm::SmallVector<Decl *, 8> DeclsInGroup;
463 Decl *FirstDecl = ParseDeclarationAfterDeclarator(D);
John McCall54abf7d2009-11-04 02:18:39 +0000464 D.complete(FirstDecl);
John McCalld226f652010-08-21 09:40:31 +0000465 if (FirstDecl)
John McCalld8ac0572009-11-03 19:26:08 +0000466 DeclsInGroup.push_back(FirstDecl);
467
468 // If we don't have a comma, it is either the end of the list (a ';') or an
469 // error, bail out.
470 while (Tok.is(tok::comma)) {
471 // Consume the comma.
Chris Lattner23c4b182009-03-29 17:18:04 +0000472 ConsumeToken();
John McCalld8ac0572009-11-03 19:26:08 +0000473
474 // Parse the next declarator.
475 D.clear();
476
477 // Accept attributes in an init-declarator. In the first declarator in a
478 // declaration, these would be part of the declspec. In subsequent
479 // declarators, they become part of the declarator itself, so that they
480 // don't apply to declarators after *this* one. Examples:
481 // short __attribute__((common)) var; -> declspec
482 // short var __attribute__((common)); -> declarator
483 // short x, __attribute__((common)) var; -> declarator
484 if (Tok.is(tok::kw___attribute)) {
485 SourceLocation Loc;
Sean Huntbbd37c62009-11-21 08:43:09 +0000486 AttributeList *AttrList = ParseGNUAttributes(&Loc);
John McCalld8ac0572009-11-03 19:26:08 +0000487 D.AddAttributes(AttrList, Loc);
488 }
489
490 ParseDeclarator(D);
491
John McCalld226f652010-08-21 09:40:31 +0000492 Decl *ThisDecl = ParseDeclarationAfterDeclarator(D);
John McCall54abf7d2009-11-04 02:18:39 +0000493 D.complete(ThisDecl);
John McCalld226f652010-08-21 09:40:31 +0000494 if (ThisDecl)
John McCalld8ac0572009-11-03 19:26:08 +0000495 DeclsInGroup.push_back(ThisDecl);
496 }
497
498 if (DeclEnd)
499 *DeclEnd = Tok.getLocation();
500
501 if (Context != Declarator::ForContext &&
502 ExpectAndConsume(tok::semi,
503 Context == Declarator::FileContext
504 ? diag::err_invalid_token_after_toplevel_declarator
505 : diag::err_expected_semi_declaration)) {
Chris Lattner004659a2010-07-11 22:42:07 +0000506 // Okay, there was no semicolon and one was expected. If we see a
507 // declaration specifier, just assume it was missing and continue parsing.
508 // Otherwise things are very confused and we skip to recover.
509 if (!isDeclarationSpecifier()) {
510 SkipUntil(tok::r_brace, true, true);
511 if (Tok.is(tok::semi))
512 ConsumeToken();
513 }
John McCalld8ac0572009-11-03 19:26:08 +0000514 }
515
Douglas Gregor23c94db2010-07-02 17:43:08 +0000516 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS,
John McCalld8ac0572009-11-03 19:26:08 +0000517 DeclsInGroup.data(),
518 DeclsInGroup.size());
Reid Spencer5f016e22007-07-11 17:01:13 +0000519}
520
Douglas Gregor1426e532009-05-12 21:31:51 +0000521/// \brief Parse 'declaration' after parsing 'declaration-specifiers
522/// declarator'. This method parses the remainder of the declaration
523/// (including any attributes or initializer, among other things) and
524/// finalizes the declaration.
Reid Spencer5f016e22007-07-11 17:01:13 +0000525///
Reid Spencer5f016e22007-07-11 17:01:13 +0000526/// init-declarator: [C99 6.7]
527/// declarator
528/// declarator '=' initializer
529/// [GNU] declarator simple-asm-expr[opt] attributes[opt]
530/// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +0000531/// [C++] declarator initializer[opt]
532///
533/// [C++] initializer:
534/// [C++] '=' initializer-clause
535/// [C++] '(' expression-list ')'
Sebastian Redl50de12f2009-03-24 22:27:57 +0000536/// [C++0x] '=' 'default' [TODO]
537/// [C++0x] '=' 'delete'
538///
539/// According to the standard grammar, =default and =delete are function
540/// definitions, but that definitely doesn't fit with the parser here.
Reid Spencer5f016e22007-07-11 17:01:13 +0000541///
John McCalld226f652010-08-21 09:40:31 +0000542Decl *Parser::ParseDeclarationAfterDeclarator(Declarator &D,
Douglas Gregore542c862009-06-23 23:11:28 +0000543 const ParsedTemplateInfo &TemplateInfo) {
Douglas Gregor1426e532009-05-12 21:31:51 +0000544 // If a simple-asm-expr is present, parse it.
545 if (Tok.is(tok::kw_asm)) {
546 SourceLocation Loc;
John McCall60d7b3a2010-08-24 06:29:42 +0000547 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
Douglas Gregor1426e532009-05-12 21:31:51 +0000548 if (AsmLabel.isInvalid()) {
549 SkipUntil(tok::semi, true, true);
John McCalld226f652010-08-21 09:40:31 +0000550 return 0;
Douglas Gregor1426e532009-05-12 21:31:51 +0000551 }
Mike Stump1eb44332009-09-09 15:08:12 +0000552
Douglas Gregor1426e532009-05-12 21:31:51 +0000553 D.setAsmLabel(AsmLabel.release());
554 D.SetRangeEnd(Loc);
555 }
Mike Stump1eb44332009-09-09 15:08:12 +0000556
Douglas Gregor1426e532009-05-12 21:31:51 +0000557 // If attributes are present, parse them.
558 if (Tok.is(tok::kw___attribute)) {
559 SourceLocation Loc;
Sean Huntbbd37c62009-11-21 08:43:09 +0000560 AttributeList *AttrList = ParseGNUAttributes(&Loc);
Douglas Gregor1426e532009-05-12 21:31:51 +0000561 D.AddAttributes(AttrList, Loc);
562 }
Mike Stump1eb44332009-09-09 15:08:12 +0000563
Douglas Gregor1426e532009-05-12 21:31:51 +0000564 // Inform the current actions module that we just parsed this declarator.
John McCalld226f652010-08-21 09:40:31 +0000565 Decl *ThisDecl = 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +0000566 switch (TemplateInfo.Kind) {
567 case ParsedTemplateInfo::NonTemplate:
Douglas Gregor23c94db2010-07-02 17:43:08 +0000568 ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
Douglas Gregord5a423b2009-09-25 18:43:00 +0000569 break;
570
571 case ParsedTemplateInfo::Template:
572 case ParsedTemplateInfo::ExplicitSpecialization:
Douglas Gregor23c94db2010-07-02 17:43:08 +0000573 ThisDecl = Actions.ActOnTemplateDeclarator(getCurScope(),
John McCallf312b1e2010-08-26 23:41:50 +0000574 MultiTemplateParamsArg(Actions,
Douglas Gregore542c862009-06-23 23:11:28 +0000575 TemplateInfo.TemplateParams->data(),
576 TemplateInfo.TemplateParams->size()),
Douglas Gregord5a423b2009-09-25 18:43:00 +0000577 D);
578 break;
579
580 case ParsedTemplateInfo::ExplicitInstantiation: {
John McCalld226f652010-08-21 09:40:31 +0000581 DeclResult ThisRes
Douglas Gregor23c94db2010-07-02 17:43:08 +0000582 = Actions.ActOnExplicitInstantiation(getCurScope(),
Douglas Gregord5a423b2009-09-25 18:43:00 +0000583 TemplateInfo.ExternLoc,
584 TemplateInfo.TemplateLoc,
585 D);
586 if (ThisRes.isInvalid()) {
587 SkipUntil(tok::semi, true, true);
John McCalld226f652010-08-21 09:40:31 +0000588 return 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +0000589 }
590
591 ThisDecl = ThisRes.get();
592 break;
593 }
594 }
Mike Stump1eb44332009-09-09 15:08:12 +0000595
Douglas Gregor1426e532009-05-12 21:31:51 +0000596 // Parse declarator '=' initializer.
Argyrios Kyrtzidisa6eb5f82010-10-08 02:39:23 +0000597 if (isTokenEqualOrMistypedEqualEqual(
598 diag::err_invalid_equalequal_after_declarator)) {
Douglas Gregor1426e532009-05-12 21:31:51 +0000599 ConsumeToken();
Anders Carlsson37bf9d22010-09-24 21:25:25 +0000600 if (Tok.is(tok::kw_delete)) {
Douglas Gregor1426e532009-05-12 21:31:51 +0000601 SourceLocation DelLoc = ConsumeToken();
Anders Carlsson37bf9d22010-09-24 21:25:25 +0000602
603 if (!getLang().CPlusPlus0x)
604 Diag(DelLoc, diag::warn_deleted_function_accepted_as_extension);
605
Douglas Gregor1426e532009-05-12 21:31:51 +0000606 Actions.SetDeclDeleted(ThisDecl, DelLoc);
607 } else {
John McCall731ad842009-12-19 09:28:58 +0000608 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
609 EnterScope(0);
Douglas Gregor23c94db2010-07-02 17:43:08 +0000610 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
John McCall731ad842009-12-19 09:28:58 +0000611 }
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +0000612
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +0000613 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000614 Actions.CodeCompleteInitializer(getCurScope(), ThisDecl);
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +0000615 ConsumeCodeCompletionToken();
616 SkipUntil(tok::comma, true, true);
617 return ThisDecl;
618 }
619
John McCall60d7b3a2010-08-24 06:29:42 +0000620 ExprResult Init(ParseInitializer());
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +0000621
John McCall731ad842009-12-19 09:28:58 +0000622 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000623 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
John McCall731ad842009-12-19 09:28:58 +0000624 ExitScope();
625 }
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +0000626
Douglas Gregor1426e532009-05-12 21:31:51 +0000627 if (Init.isInvalid()) {
Douglas Gregor00225542010-03-01 18:27:54 +0000628 SkipUntil(tok::comma, true, true);
629 Actions.ActOnInitializerError(ThisDecl);
630 } else
John McCall9ae2f072010-08-23 23:25:46 +0000631 Actions.AddInitializerToDecl(ThisDecl, Init.take());
Douglas Gregor1426e532009-05-12 21:31:51 +0000632 }
633 } else if (Tok.is(tok::l_paren)) {
634 // Parse C++ direct initializer: '(' expression-list ')'
635 SourceLocation LParenLoc = ConsumeParen();
636 ExprVector Exprs(Actions);
637 CommaLocsTy CommaLocs;
638
Douglas Gregorb4debae2009-12-22 17:47:17 +0000639 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
640 EnterScope(0);
Douglas Gregor23c94db2010-07-02 17:43:08 +0000641 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +0000642 }
643
Douglas Gregor1426e532009-05-12 21:31:51 +0000644 if (ParseExpressionList(Exprs, CommaLocs)) {
645 SkipUntil(tok::r_paren);
Douglas Gregorb4debae2009-12-22 17:47:17 +0000646
647 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000648 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +0000649 ExitScope();
650 }
Douglas Gregor1426e532009-05-12 21:31:51 +0000651 } else {
652 // Match the ')'.
653 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
654
655 assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() &&
656 "Unexpected number of commas!");
Douglas Gregorb4debae2009-12-22 17:47:17 +0000657
658 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000659 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +0000660 ExitScope();
661 }
662
Douglas Gregor1426e532009-05-12 21:31:51 +0000663 Actions.AddCXXDirectInitializerToDecl(ThisDecl, LParenLoc,
664 move_arg(Exprs),
Douglas Gregora1a04782010-09-09 16:33:13 +0000665 RParenLoc);
Douglas Gregor1426e532009-05-12 21:31:51 +0000666 }
667 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000668 bool TypeContainsUndeducedAuto =
Anders Carlsson6a75cd92009-07-11 00:34:39 +0000669 D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto;
670 Actions.ActOnUninitializedDecl(ThisDecl, TypeContainsUndeducedAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +0000671 }
672
673 return ThisDecl;
674}
675
Reid Spencer5f016e22007-07-11 17:01:13 +0000676/// ParseSpecifierQualifierList
677/// specifier-qualifier-list:
678/// type-specifier specifier-qualifier-list[opt]
679/// type-qualifier specifier-qualifier-list[opt]
680/// [GNU] attributes specifier-qualifier-list[opt]
681///
682void Parser::ParseSpecifierQualifierList(DeclSpec &DS) {
683 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
684 /// parse declaration-specifiers and complain about extra stuff.
Reid Spencer5f016e22007-07-11 17:01:13 +0000685 ParseDeclarationSpecifiers(DS);
Mike Stump1eb44332009-09-09 15:08:12 +0000686
Reid Spencer5f016e22007-07-11 17:01:13 +0000687 // Validate declspec for type-name.
688 unsigned Specs = DS.getParsedSpecifiers();
Chris Lattnerb6645dd2009-04-14 21:16:09 +0000689 if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers() &&
690 !DS.getAttributes())
Reid Spencer5f016e22007-07-11 17:01:13 +0000691 Diag(Tok, diag::err_typename_requires_specqual);
Mike Stump1eb44332009-09-09 15:08:12 +0000692
Reid Spencer5f016e22007-07-11 17:01:13 +0000693 // Issue diagnostic and remove storage class if present.
694 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
695 if (DS.getStorageClassSpecLoc().isValid())
696 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
697 else
698 Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass);
699 DS.ClearStorageClassSpecs();
700 }
Mike Stump1eb44332009-09-09 15:08:12 +0000701
Reid Spencer5f016e22007-07-11 17:01:13 +0000702 // Issue diagnostic and remove function specfier if present.
703 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Douglas Gregorb48fe382008-10-31 09:07:45 +0000704 if (DS.isInlineSpecified())
705 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
706 if (DS.isVirtualSpecified())
707 Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec);
708 if (DS.isExplicitSpecified())
709 Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec);
Reid Spencer5f016e22007-07-11 17:01:13 +0000710 DS.ClearFunctionSpecs();
711 }
712}
713
Chris Lattnerc199ab32009-04-12 20:42:31 +0000714/// isValidAfterIdentifierInDeclaratorAfterDeclSpec - Return true if the
715/// specified token is valid after the identifier in a declarator which
716/// immediately follows the declspec. For example, these things are valid:
717///
718/// int x [ 4]; // direct-declarator
719/// int x ( int y); // direct-declarator
720/// int(int x ) // direct-declarator
721/// int x ; // simple-declaration
722/// int x = 17; // init-declarator-list
723/// int x , y; // init-declarator-list
724/// int x __asm__ ("foo"); // init-declarator-list
Chris Lattnerb6645dd2009-04-14 21:16:09 +0000725/// int x : 4; // struct-declarator
Chris Lattnerc83c27a2009-04-12 22:29:43 +0000726/// int x { 5}; // C++'0x unified initializers
Chris Lattnerc199ab32009-04-12 20:42:31 +0000727///
728/// This is not, because 'x' does not immediately follow the declspec (though
729/// ')' happens to be valid anyway).
730/// int (x)
731///
732static bool isValidAfterIdentifierInDeclarator(const Token &T) {
733 return T.is(tok::l_square) || T.is(tok::l_paren) || T.is(tok::r_paren) ||
734 T.is(tok::semi) || T.is(tok::comma) || T.is(tok::equal) ||
Chris Lattnerb6645dd2009-04-14 21:16:09 +0000735 T.is(tok::kw_asm) || T.is(tok::l_brace) || T.is(tok::colon);
Chris Lattnerc199ab32009-04-12 20:42:31 +0000736}
737
Chris Lattnere40c2952009-04-14 21:34:55 +0000738
739/// ParseImplicitInt - This method is called when we have an non-typename
740/// identifier in a declspec (which normally terminates the decl spec) when
741/// the declspec has no type specifier. In this case, the declspec is either
742/// malformed or is "implicit int" (in K&R and C89).
743///
744/// This method handles diagnosing this prettily and returns false if the
745/// declspec is done being processed. If it recovers and thinks there may be
746/// other pieces of declspec after it, it returns true.
747///
Chris Lattnerf4382f52009-04-14 22:17:06 +0000748bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000749 const ParsedTemplateInfo &TemplateInfo,
Chris Lattnere40c2952009-04-14 21:34:55 +0000750 AccessSpecifier AS) {
Chris Lattnerf4382f52009-04-14 22:17:06 +0000751 assert(Tok.is(tok::identifier) && "should have identifier");
Mike Stump1eb44332009-09-09 15:08:12 +0000752
Chris Lattnere40c2952009-04-14 21:34:55 +0000753 SourceLocation Loc = Tok.getLocation();
754 // If we see an identifier that is not a type name, we normally would
755 // parse it as the identifer being declared. However, when a typename
756 // is typo'd or the definition is not included, this will incorrectly
757 // parse the typename as the identifier name and fall over misparsing
758 // later parts of the diagnostic.
759 //
760 // As such, we try to do some look-ahead in cases where this would
761 // otherwise be an "implicit-int" case to see if this is invalid. For
762 // example: "static foo_t x = 4;" In this case, if we parsed foo_t as
763 // an identifier with implicit int, we'd get a parse error because the
764 // next token is obviously invalid for a type. Parse these as a case
765 // with an invalid type specifier.
766 assert(!DS.hasTypeSpecifier() && "Type specifier checked above");
Mike Stump1eb44332009-09-09 15:08:12 +0000767
Chris Lattnere40c2952009-04-14 21:34:55 +0000768 // Since we know that this either implicit int (which is rare) or an
769 // error, we'd do lookahead to try to do better recovery.
770 if (isValidAfterIdentifierInDeclarator(NextToken())) {
771 // If this token is valid for implicit int, e.g. "static x = 4", then
772 // we just avoid eating the identifier, so it will be parsed as the
773 // identifier in the declarator.
774 return false;
775 }
Mike Stump1eb44332009-09-09 15:08:12 +0000776
Chris Lattnere40c2952009-04-14 21:34:55 +0000777 // Otherwise, if we don't consume this token, we are going to emit an
778 // error anyway. Try to recover from various common problems. Check
779 // to see if this was a reference to a tag name without a tag specified.
780 // This is a common problem in C (saying 'foo' instead of 'struct foo').
Chris Lattnerf4382f52009-04-14 22:17:06 +0000781 //
782 // C++ doesn't need this, and isTagName doesn't take SS.
783 if (SS == 0) {
784 const char *TagName = 0;
785 tok::TokenKind TagKind = tok::unknown;
Mike Stump1eb44332009-09-09 15:08:12 +0000786
Douglas Gregor23c94db2010-07-02 17:43:08 +0000787 switch (Actions.isTagName(*Tok.getIdentifierInfo(), getCurScope())) {
Chris Lattnere40c2952009-04-14 21:34:55 +0000788 default: break;
789 case DeclSpec::TST_enum: TagName="enum" ;TagKind=tok::kw_enum ;break;
790 case DeclSpec::TST_union: TagName="union" ;TagKind=tok::kw_union ;break;
791 case DeclSpec::TST_struct:TagName="struct";TagKind=tok::kw_struct;break;
792 case DeclSpec::TST_class: TagName="class" ;TagKind=tok::kw_class ;break;
793 }
Mike Stump1eb44332009-09-09 15:08:12 +0000794
Chris Lattnerf4382f52009-04-14 22:17:06 +0000795 if (TagName) {
796 Diag(Loc, diag::err_use_of_tag_name_without_tag)
John McCall23e907a2010-02-14 01:03:10 +0000797 << Tok.getIdentifierInfo() << TagName << getLang().CPlusPlus
Douglas Gregor849b2432010-03-31 17:46:05 +0000798 << FixItHint::CreateInsertion(Tok.getLocation(),TagName);
Mike Stump1eb44332009-09-09 15:08:12 +0000799
Chris Lattnerf4382f52009-04-14 22:17:06 +0000800 // Parse this as a tag as if the missing tag were present.
801 if (TagKind == tok::kw_enum)
Douglas Gregor9b9edd62010-03-02 17:53:14 +0000802 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS);
Chris Lattnerf4382f52009-04-14 22:17:06 +0000803 else
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000804 ParseClassSpecifier(TagKind, Loc, DS, TemplateInfo, AS);
Chris Lattnerf4382f52009-04-14 22:17:06 +0000805 return true;
806 }
Chris Lattnere40c2952009-04-14 21:34:55 +0000807 }
Mike Stump1eb44332009-09-09 15:08:12 +0000808
Douglas Gregora786fdb2009-10-13 23:27:22 +0000809 // This is almost certainly an invalid type name. Let the action emit a
810 // diagnostic and attempt to recover.
John McCallb3d87482010-08-24 05:47:05 +0000811 ParsedType T;
Douglas Gregora786fdb2009-10-13 23:27:22 +0000812 if (Actions.DiagnoseUnknownTypeName(*Tok.getIdentifierInfo(), Loc,
Douglas Gregor23c94db2010-07-02 17:43:08 +0000813 getCurScope(), SS, T)) {
Douglas Gregora786fdb2009-10-13 23:27:22 +0000814 // The action emitted a diagnostic, so we don't have to.
815 if (T) {
816 // The action has suggested that the type T could be used. Set that as
817 // the type in the declaration specifiers, consume the would-be type
818 // name token, and we're done.
819 const char *PrevSpec;
820 unsigned DiagID;
John McCallb3d87482010-08-24 05:47:05 +0000821 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID, T);
Douglas Gregora786fdb2009-10-13 23:27:22 +0000822 DS.SetRangeEnd(Tok.getLocation());
823 ConsumeToken();
824
825 // There may be other declaration specifiers after this.
826 return true;
827 }
828
829 // Fall through; the action had no suggestion for us.
830 } else {
831 // The action did not emit a diagnostic, so emit one now.
832 SourceRange R;
833 if (SS) R = SS->getRange();
834 Diag(Loc, diag::err_unknown_typename) << Tok.getIdentifierInfo() << R;
835 }
Mike Stump1eb44332009-09-09 15:08:12 +0000836
Douglas Gregora786fdb2009-10-13 23:27:22 +0000837 // Mark this as an error.
Chris Lattnere40c2952009-04-14 21:34:55 +0000838 const char *PrevSpec;
John McCallfec54012009-08-03 20:12:06 +0000839 unsigned DiagID;
840 DS.SetTypeSpecType(DeclSpec::TST_error, Loc, PrevSpec, DiagID);
Chris Lattnere40c2952009-04-14 21:34:55 +0000841 DS.SetRangeEnd(Tok.getLocation());
842 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000843
Chris Lattnere40c2952009-04-14 21:34:55 +0000844 // TODO: Could inject an invalid typedef decl in an enclosing scope to
845 // avoid rippling error messages on subsequent uses of the same type,
846 // could be useful if #include was forgotten.
847 return false;
848}
849
Douglas Gregor0efc2c12010-01-13 17:31:36 +0000850/// \brief Determine the declaration specifier context from the declarator
851/// context.
852///
853/// \param Context the declarator context, which is one of the
854/// Declarator::TheContext enumerator values.
855Parser::DeclSpecContext
856Parser::getDeclSpecContextFromDeclaratorContext(unsigned Context) {
857 if (Context == Declarator::MemberContext)
858 return DSC_class;
859 if (Context == Declarator::FileContext)
860 return DSC_top_level;
861 return DSC_normal;
862}
863
Reid Spencer5f016e22007-07-11 17:01:13 +0000864/// ParseDeclarationSpecifiers
865/// declaration-specifiers: [C99 6.7]
866/// storage-class-specifier declaration-specifiers[opt]
867/// type-specifier declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +0000868/// [C99] function-specifier declaration-specifiers[opt]
869/// [GNU] attributes declaration-specifiers[opt]
870///
871/// storage-class-specifier: [C99 6.7.1]
872/// 'typedef'
873/// 'extern'
874/// 'static'
875/// 'auto'
876/// 'register'
Sebastian Redl669d5d72008-11-14 23:42:31 +0000877/// [C++] 'mutable'
Reid Spencer5f016e22007-07-11 17:01:13 +0000878/// [GNU] '__thread'
Reid Spencer5f016e22007-07-11 17:01:13 +0000879/// function-specifier: [C99 6.7.4]
880/// [C99] 'inline'
Douglas Gregorb48fe382008-10-31 09:07:45 +0000881/// [C++] 'virtual'
882/// [C++] 'explicit'
Anders Carlssonf47f7a12009-05-06 04:46:28 +0000883/// 'friend': [C++ dcl.friend]
Sebastian Redl2ac67232009-11-05 15:47:02 +0000884/// 'constexpr': [C++0x dcl.constexpr]
Anders Carlssonf47f7a12009-05-06 04:46:28 +0000885
Reid Spencer5f016e22007-07-11 17:01:13 +0000886///
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000887void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000888 const ParsedTemplateInfo &TemplateInfo,
John McCall67d1a672009-08-06 02:15:43 +0000889 AccessSpecifier AS,
Douglas Gregor2ccccb32010-08-23 18:23:48 +0000890 DeclSpecContext DSContext) {
Chris Lattner81c018d2008-03-13 06:29:04 +0000891 DS.SetRangeStart(Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +0000892 while (1) {
John McCallfec54012009-08-03 20:12:06 +0000893 bool isInvalid = false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000894 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +0000895 unsigned DiagID = 0;
896
Reid Spencer5f016e22007-07-11 17:01:13 +0000897 SourceLocation Loc = Tok.getLocation();
Douglas Gregor12e083c2008-11-07 15:42:26 +0000898
Reid Spencer5f016e22007-07-11 17:01:13 +0000899 switch (Tok.getKind()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000900 default:
Chris Lattnerbce61352008-07-26 00:20:22 +0000901 DoneWithDeclSpec:
Reid Spencer5f016e22007-07-11 17:01:13 +0000902 // If this is not a declaration specifier token, we're done reading decl
903 // specifiers. First verify that DeclSpec's are consistent.
Douglas Gregor9b3064b2009-04-01 22:41:11 +0000904 DS.Finish(Diags, PP);
Reid Spencer5f016e22007-07-11 17:01:13 +0000905 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000906
Douglas Gregor2ccccb32010-08-23 18:23:48 +0000907 case tok::code_completion: {
John McCallf312b1e2010-08-26 23:41:50 +0000908 Sema::ParserCompletionContext CCC = Sema::PCC_Namespace;
Douglas Gregor2ccccb32010-08-23 18:23:48 +0000909 if (DS.hasTypeSpecifier()) {
910 bool AllowNonIdentifiers
911 = (getCurScope()->getFlags() & (Scope::ControlScope |
912 Scope::BlockScope |
913 Scope::TemplateParamScope |
914 Scope::FunctionPrototypeScope |
915 Scope::AtCatchScope)) == 0;
916 bool AllowNestedNameSpecifiers
917 = DSContext == DSC_top_level ||
918 (DSContext == DSC_class && DS.isFriendSpecified());
919
Douglas Gregorc7b6d882010-09-16 15:14:18 +0000920 Actions.CodeCompleteDeclSpec(getCurScope(), DS,
921 AllowNonIdentifiers,
922 AllowNestedNameSpecifiers);
Douglas Gregor2ccccb32010-08-23 18:23:48 +0000923 ConsumeCodeCompletionToken();
924 return;
925 }
926
927 if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate)
John McCallf312b1e2010-08-26 23:41:50 +0000928 CCC = DSContext == DSC_class? Sema::PCC_MemberTemplate
929 : Sema::PCC_Template;
Douglas Gregor2ccccb32010-08-23 18:23:48 +0000930 else if (DSContext == DSC_class)
John McCallf312b1e2010-08-26 23:41:50 +0000931 CCC = Sema::PCC_Class;
Douglas Gregor2ccccb32010-08-23 18:23:48 +0000932 else if (ObjCImpDecl)
John McCallf312b1e2010-08-26 23:41:50 +0000933 CCC = Sema::PCC_ObjCImplementation;
Douglas Gregor2ccccb32010-08-23 18:23:48 +0000934
935 Actions.CodeCompleteOrdinaryName(getCurScope(), CCC);
936 ConsumeCodeCompletionToken();
937 return;
938 }
939
Chris Lattner5e02c472009-01-05 00:07:25 +0000940 case tok::coloncolon: // ::foo::bar
John McCall9ba61662010-02-26 08:45:28 +0000941 // C++ scope specifier. Annotate and loop, or bail out on error.
942 if (TryAnnotateCXXScopeToken(true)) {
943 if (!DS.hasTypeSpecifier())
944 DS.SetTypeSpecError();
945 goto DoneWithDeclSpec;
946 }
John McCall2e0a7152010-03-01 18:20:46 +0000947 if (Tok.is(tok::coloncolon)) // ::new or ::delete
948 goto DoneWithDeclSpec;
John McCall9ba61662010-02-26 08:45:28 +0000949 continue;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000950
951 case tok::annot_cxxscope: {
952 if (DS.hasTypeSpecifier())
953 goto DoneWithDeclSpec;
954
John McCallaa87d332009-12-12 11:40:51 +0000955 CXXScopeSpec SS;
John McCallca0408f2010-08-23 06:44:23 +0000956 SS.setScopeRep((NestedNameSpecifier*) Tok.getAnnotationValue());
John McCallaa87d332009-12-12 11:40:51 +0000957 SS.setRange(Tok.getAnnotationRange());
958
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000959 // We are looking for a qualified typename.
Douglas Gregor9135c722009-03-25 15:40:00 +0000960 Token Next = NextToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000961 if (Next.is(tok::annot_template_id) &&
Douglas Gregor9135c722009-03-25 15:40:00 +0000962 static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue())
Douglas Gregorc45c2322009-03-31 00:43:58 +0000963 ->Kind == TNK_Type_template) {
Douglas Gregor9135c722009-03-25 15:40:00 +0000964 // We have a qualified template-id, e.g., N::A<int>
Douglas Gregor0efc2c12010-01-13 17:31:36 +0000965
966 // C++ [class.qual]p2:
967 // In a lookup in which the constructor is an acceptable lookup
968 // result and the nested-name-specifier nominates a class C:
969 //
970 // - if the name specified after the
971 // nested-name-specifier, when looked up in C, is the
972 // injected-class-name of C (Clause 9), or
973 //
974 // - if the name specified after the nested-name-specifier
975 // is the same as the identifier or the
976 // simple-template-id's template-name in the last
977 // component of the nested-name-specifier,
978 //
979 // the name is instead considered to name the constructor of
980 // class C.
981 //
982 // Thus, if the template-name is actually the constructor
983 // name, then the code is ill-formed; this interpretation is
984 // reinforced by the NAD status of core issue 635.
985 TemplateIdAnnotation *TemplateId
986 = static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue());
John McCallba9d8532010-04-13 06:39:49 +0000987 if ((DSContext == DSC_top_level ||
988 (DSContext == DSC_class && DS.isFriendSpecified())) &&
989 TemplateId->Name &&
Douglas Gregor23c94db2010-07-02 17:43:08 +0000990 Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) {
Douglas Gregor0efc2c12010-01-13 17:31:36 +0000991 if (isConstructorDeclarator()) {
992 // The user meant this to be an out-of-line constructor
993 // definition, but template arguments are not allowed
994 // there. Just allow this as a constructor; we'll
995 // complain about it later.
996 goto DoneWithDeclSpec;
997 }
998
999 // The user meant this to name a type, but it actually names
1000 // a constructor with some extraneous template
1001 // arguments. Complain, then parse it as a type as the user
1002 // intended.
1003 Diag(TemplateId->TemplateNameLoc,
1004 diag::err_out_of_line_template_id_names_constructor)
1005 << TemplateId->Name;
1006 }
1007
John McCallaa87d332009-12-12 11:40:51 +00001008 DS.getTypeSpecScope() = SS;
1009 ConsumeToken(); // The C++ scope.
Mike Stump1eb44332009-09-09 15:08:12 +00001010 assert(Tok.is(tok::annot_template_id) &&
Douglas Gregor9135c722009-03-25 15:40:00 +00001011 "ParseOptionalCXXScopeSpecifier not working");
1012 AnnotateTemplateIdTokenAsType(&SS);
1013 continue;
1014 }
1015
Douglas Gregor9d7b3532009-09-28 07:26:33 +00001016 if (Next.is(tok::annot_typename)) {
John McCallaa87d332009-12-12 11:40:51 +00001017 DS.getTypeSpecScope() = SS;
1018 ConsumeToken(); // The C++ scope.
John McCallb3d87482010-08-24 05:47:05 +00001019 if (Tok.getAnnotationValue()) {
1020 ParsedType T = getTypeAnnotation(Tok);
Douglas Gregor9d7b3532009-09-28 07:26:33 +00001021 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc,
John McCallb3d87482010-08-24 05:47:05 +00001022 PrevSpec, DiagID, T);
1023 }
Douglas Gregor9d7b3532009-09-28 07:26:33 +00001024 else
1025 DS.SetTypeSpecError();
1026 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1027 ConsumeToken(); // The typename
1028 }
1029
Douglas Gregor9135c722009-03-25 15:40:00 +00001030 if (Next.isNot(tok::identifier))
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001031 goto DoneWithDeclSpec;
1032
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001033 // If we're in a context where the identifier could be a class name,
1034 // check whether this is a constructor declaration.
John McCallba9d8532010-04-13 06:39:49 +00001035 if ((DSContext == DSC_top_level ||
1036 (DSContext == DSC_class && DS.isFriendSpecified())) &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00001037 Actions.isCurrentClassName(*Next.getIdentifierInfo(), getCurScope(),
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001038 &SS)) {
1039 if (isConstructorDeclarator())
1040 goto DoneWithDeclSpec;
1041
1042 // As noted in C++ [class.qual]p2 (cited above), when the name
1043 // of the class is qualified in a context where it could name
1044 // a constructor, its a constructor name. However, we've
1045 // looked at the declarator, and the user probably meant this
1046 // to be a type. Complain that it isn't supposed to be treated
1047 // as a type, then proceed to parse it as a type.
1048 Diag(Next.getLocation(), diag::err_out_of_line_type_names_constructor)
1049 << Next.getIdentifierInfo();
1050 }
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001051
John McCallb3d87482010-08-24 05:47:05 +00001052 ParsedType TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(),
1053 Next.getLocation(),
1054 getCurScope(), &SS);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001055
Chris Lattnerf4382f52009-04-14 22:17:06 +00001056 // If the referenced identifier is not a type, then this declspec is
1057 // erroneous: We already checked about that it has no type specifier, and
1058 // C++ doesn't have implicit int. Diagnose it as a typo w.r.t. to the
Mike Stump1eb44332009-09-09 15:08:12 +00001059 // typename.
Chris Lattnerf4382f52009-04-14 22:17:06 +00001060 if (TypeRep == 0) {
1061 ConsumeToken(); // Eat the scope spec so the identifier is current.
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001062 if (ParseImplicitInt(DS, &SS, TemplateInfo, AS)) continue;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001063 goto DoneWithDeclSpec;
Chris Lattnerf4382f52009-04-14 22:17:06 +00001064 }
Mike Stump1eb44332009-09-09 15:08:12 +00001065
John McCallaa87d332009-12-12 11:40:51 +00001066 DS.getTypeSpecScope() = SS;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001067 ConsumeToken(); // The C++ scope.
1068
Douglas Gregor1a51b4a2009-02-09 15:09:02 +00001069 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001070 DiagID, TypeRep);
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001071 if (isInvalid)
1072 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001073
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001074 DS.SetRangeEnd(Tok.getLocation());
1075 ConsumeToken(); // The typename.
1076
1077 continue;
1078 }
Mike Stump1eb44332009-09-09 15:08:12 +00001079
Chris Lattner80d0c892009-01-21 19:48:37 +00001080 case tok::annot_typename: {
John McCallb3d87482010-08-24 05:47:05 +00001081 if (Tok.getAnnotationValue()) {
1082 ParsedType T = getTypeAnnotation(Tok);
Douglas Gregor31a19b62009-04-01 21:51:26 +00001083 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallb3d87482010-08-24 05:47:05 +00001084 DiagID, T);
1085 } else
Douglas Gregor31a19b62009-04-01 21:51:26 +00001086 DS.SetTypeSpecError();
Chris Lattner5c5db552010-04-05 18:18:31 +00001087
1088 if (isInvalid)
1089 break;
1090
Chris Lattner80d0c892009-01-21 19:48:37 +00001091 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1092 ConsumeToken(); // The typename
Mike Stump1eb44332009-09-09 15:08:12 +00001093
Chris Lattner80d0c892009-01-21 19:48:37 +00001094 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1095 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001096 // Objective-C interface.
1097 if (Tok.is(tok::less) && getLang().ObjC1)
1098 ParseObjCProtocolQualifiers(DS);
1099
Chris Lattner80d0c892009-01-21 19:48:37 +00001100 continue;
1101 }
Mike Stump1eb44332009-09-09 15:08:12 +00001102
Chris Lattner3bd934a2008-07-26 01:18:38 +00001103 // typedef-name
1104 case tok::identifier: {
Chris Lattner5e02c472009-01-05 00:07:25 +00001105 // In C++, check to see if this is a scope specifier like foo::bar::, if
1106 // so handle it as such. This is important for ctor parsing.
John McCall9ba61662010-02-26 08:45:28 +00001107 if (getLang().CPlusPlus) {
1108 if (TryAnnotateCXXScopeToken(true)) {
1109 if (!DS.hasTypeSpecifier())
1110 DS.SetTypeSpecError();
1111 goto DoneWithDeclSpec;
1112 }
1113 if (!Tok.is(tok::identifier))
1114 continue;
1115 }
Mike Stump1eb44332009-09-09 15:08:12 +00001116
Chris Lattner3bd934a2008-07-26 01:18:38 +00001117 // This identifier can only be a typedef name if we haven't already seen
1118 // a type-specifier. Without this check we misparse:
1119 // typedef int X; struct Y { short X; }; as 'short int'.
1120 if (DS.hasTypeSpecifier())
1121 goto DoneWithDeclSpec;
Mike Stump1eb44332009-09-09 15:08:12 +00001122
John Thompson82287d12010-02-05 00:12:22 +00001123 // Check for need to substitute AltiVec keyword tokens.
1124 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
1125 break;
1126
Chris Lattner3bd934a2008-07-26 01:18:38 +00001127 // It has to be available as a typedef too!
John McCallb3d87482010-08-24 05:47:05 +00001128 ParsedType TypeRep =
1129 Actions.getTypeName(*Tok.getIdentifierInfo(),
1130 Tok.getLocation(), getCurScope());
Douglas Gregor55f6b142009-02-09 18:46:07 +00001131
Chris Lattnerc199ab32009-04-12 20:42:31 +00001132 // If this is not a typedef name, don't parse it as part of the declspec,
1133 // it must be an implicit int or an error.
John McCallb3d87482010-08-24 05:47:05 +00001134 if (!TypeRep) {
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001135 if (ParseImplicitInt(DS, 0, TemplateInfo, AS)) continue;
Chris Lattner3bd934a2008-07-26 01:18:38 +00001136 goto DoneWithDeclSpec;
Chris Lattnerc199ab32009-04-12 20:42:31 +00001137 }
Douglas Gregor55f6b142009-02-09 18:46:07 +00001138
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001139 // If we're in a context where the identifier could be a class name,
1140 // check whether this is a constructor declaration.
1141 if (getLang().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00001142 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), getCurScope()) &&
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001143 isConstructorDeclarator())
Douglas Gregorb48fe382008-10-31 09:07:45 +00001144 goto DoneWithDeclSpec;
1145
Douglas Gregor1a51b4a2009-02-09 15:09:02 +00001146 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001147 DiagID, TypeRep);
Chris Lattner3bd934a2008-07-26 01:18:38 +00001148 if (isInvalid)
1149 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001150
Chris Lattner3bd934a2008-07-26 01:18:38 +00001151 DS.SetRangeEnd(Tok.getLocation());
1152 ConsumeToken(); // The identifier
1153
1154 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1155 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001156 // Objective-C interface.
1157 if (Tok.is(tok::less) && getLang().ObjC1)
1158 ParseObjCProtocolQualifiers(DS);
1159
Steve Naroff4f9b9f12008-09-22 10:28:57 +00001160 // Need to support trailing type qualifiers (e.g. "id<p> const").
1161 // If a type specifier follows, it will be diagnosed elsewhere.
1162 continue;
Chris Lattner3bd934a2008-07-26 01:18:38 +00001163 }
Douglas Gregor39a8de12009-02-25 19:37:18 +00001164
1165 // type-name
1166 case tok::annot_template_id: {
Mike Stump1eb44332009-09-09 15:08:12 +00001167 TemplateIdAnnotation *TemplateId
Douglas Gregor39a8de12009-02-25 19:37:18 +00001168 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
Douglas Gregorc45c2322009-03-31 00:43:58 +00001169 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor39a8de12009-02-25 19:37:18 +00001170 // This template-id does not refer to a type name, so we're
1171 // done with the type-specifiers.
1172 goto DoneWithDeclSpec;
1173 }
1174
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001175 // If we're in a context where the template-id could be a
1176 // constructor name or specialization, check whether this is a
1177 // constructor declaration.
1178 if (getLang().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00001179 Actions.isCurrentClassName(*TemplateId->Name, getCurScope()) &&
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001180 isConstructorDeclarator())
1181 goto DoneWithDeclSpec;
1182
Douglas Gregor39a8de12009-02-25 19:37:18 +00001183 // Turn the template-id annotation token into a type annotation
1184 // token, then try again to parse it as a type-specifier.
Douglas Gregor31a19b62009-04-01 21:51:26 +00001185 AnnotateTemplateIdTokenAsType();
Douglas Gregor39a8de12009-02-25 19:37:18 +00001186 continue;
1187 }
1188
Reid Spencer5f016e22007-07-11 17:01:13 +00001189 // GNU attributes support.
1190 case tok::kw___attribute:
Sean Huntbbd37c62009-11-21 08:43:09 +00001191 DS.AddAttributes(ParseGNUAttributes());
Reid Spencer5f016e22007-07-11 17:01:13 +00001192 continue;
Steve Narofff59e17e2008-12-24 20:59:21 +00001193
1194 // Microsoft declspec support.
1195 case tok::kw___declspec:
Eli Friedmana23b4852009-06-08 07:21:15 +00001196 DS.AddAttributes(ParseMicrosoftDeclSpec());
Steve Narofff59e17e2008-12-24 20:59:21 +00001197 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001198
Steve Naroff239f0732008-12-25 14:16:32 +00001199 // Microsoft single token adornments.
Steve Naroff86bc6cf2008-12-25 14:41:26 +00001200 case tok::kw___forceinline:
Eli Friedman290eeb02009-06-08 23:27:34 +00001201 // FIXME: Add handling here!
1202 break;
1203
1204 case tok::kw___ptr64:
Steve Naroff86bc6cf2008-12-25 14:41:26 +00001205 case tok::kw___w64:
Steve Naroff239f0732008-12-25 14:16:32 +00001206 case tok::kw___cdecl:
1207 case tok::kw___stdcall:
1208 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00001209 case tok::kw___thiscall:
Eli Friedman290eeb02009-06-08 23:27:34 +00001210 DS.AddAttributes(ParseMicrosoftTypeAttributes());
1211 continue;
1212
Dawn Perchik52fc3142010-09-03 01:29:35 +00001213 // Borland single token adornments.
1214 case tok::kw___pascal:
1215 DS.AddAttributes(ParseBorlandTypeAttributes());
1216 continue;
1217
Reid Spencer5f016e22007-07-11 17:01:13 +00001218 // storage-class-specifier
1219 case tok::kw_typedef:
John McCallfec54012009-08-03 20:12:06 +00001220 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_typedef, Loc, PrevSpec,
1221 DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001222 break;
1223 case tok::kw_extern:
1224 if (DS.isThreadSpecified())
Chris Lattner1ab3b962008-11-18 07:48:38 +00001225 Diag(Tok, diag::ext_thread_before) << "extern";
John McCallfec54012009-08-03 20:12:06 +00001226 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_extern, Loc, PrevSpec,
1227 DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001228 break;
Steve Naroff8d54bf22007-12-18 00:16:02 +00001229 case tok::kw___private_extern__:
Chris Lattnerf97409f2008-04-06 06:57:35 +00001230 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_private_extern, Loc,
John McCallfec54012009-08-03 20:12:06 +00001231 PrevSpec, DiagID);
Steve Naroff8d54bf22007-12-18 00:16:02 +00001232 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00001233 case tok::kw_static:
1234 if (DS.isThreadSpecified())
Chris Lattner1ab3b962008-11-18 07:48:38 +00001235 Diag(Tok, diag::ext_thread_before) << "static";
John McCallfec54012009-08-03 20:12:06 +00001236 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_static, Loc, PrevSpec,
1237 DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001238 break;
1239 case tok::kw_auto:
Anders Carlssone89d1592009-06-26 18:41:36 +00001240 if (getLang().CPlusPlus0x)
John McCallfec54012009-08-03 20:12:06 +00001241 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec,
1242 DiagID);
Anders Carlssone89d1592009-06-26 18:41:36 +00001243 else
John McCallfec54012009-08-03 20:12:06 +00001244 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, Loc, PrevSpec,
1245 DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001246 break;
1247 case tok::kw_register:
John McCallfec54012009-08-03 20:12:06 +00001248 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_register, Loc, PrevSpec,
1249 DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001250 break;
Sebastian Redl669d5d72008-11-14 23:42:31 +00001251 case tok::kw_mutable:
John McCallfec54012009-08-03 20:12:06 +00001252 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_mutable, Loc, PrevSpec,
1253 DiagID);
Sebastian Redl669d5d72008-11-14 23:42:31 +00001254 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00001255 case tok::kw___thread:
John McCallfec54012009-08-03 20:12:06 +00001256 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001257 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001258
Reid Spencer5f016e22007-07-11 17:01:13 +00001259 // function-specifier
1260 case tok::kw_inline:
John McCallfec54012009-08-03 20:12:06 +00001261 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001262 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +00001263 case tok::kw_virtual:
John McCallfec54012009-08-03 20:12:06 +00001264 isInvalid = DS.SetFunctionSpecVirtual(Loc, PrevSpec, DiagID);
Douglas Gregorb48fe382008-10-31 09:07:45 +00001265 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +00001266 case tok::kw_explicit:
John McCallfec54012009-08-03 20:12:06 +00001267 isInvalid = DS.SetFunctionSpecExplicit(Loc, PrevSpec, DiagID);
Douglas Gregorb48fe382008-10-31 09:07:45 +00001268 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00001269
Anders Carlssonf47f7a12009-05-06 04:46:28 +00001270 // friend
1271 case tok::kw_friend:
John McCall67d1a672009-08-06 02:15:43 +00001272 if (DSContext == DSC_class)
1273 isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID);
1274 else {
1275 PrevSpec = ""; // not actually used by the diagnostic
1276 DiagID = diag::err_friend_invalid_in_context;
1277 isInvalid = true;
1278 }
Anders Carlssonf47f7a12009-05-06 04:46:28 +00001279 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001280
Sebastian Redl2ac67232009-11-05 15:47:02 +00001281 // constexpr
1282 case tok::kw_constexpr:
1283 isInvalid = DS.SetConstexprSpec(Loc, PrevSpec, DiagID);
1284 break;
1285
Chris Lattner80d0c892009-01-21 19:48:37 +00001286 // type-specifier
1287 case tok::kw_short:
John McCallfec54012009-08-03 20:12:06 +00001288 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec,
1289 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001290 break;
1291 case tok::kw_long:
1292 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCallfec54012009-08-03 20:12:06 +00001293 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
1294 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001295 else
John McCallfec54012009-08-03 20:12:06 +00001296 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
1297 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001298 break;
1299 case tok::kw_signed:
John McCallfec54012009-08-03 20:12:06 +00001300 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec,
1301 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001302 break;
1303 case tok::kw_unsigned:
John McCallfec54012009-08-03 20:12:06 +00001304 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
1305 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001306 break;
1307 case tok::kw__Complex:
John McCallfec54012009-08-03 20:12:06 +00001308 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
1309 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001310 break;
1311 case tok::kw__Imaginary:
John McCallfec54012009-08-03 20:12:06 +00001312 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
1313 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001314 break;
1315 case tok::kw_void:
John McCallfec54012009-08-03 20:12:06 +00001316 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec,
1317 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001318 break;
1319 case tok::kw_char:
John McCallfec54012009-08-03 20:12:06 +00001320 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec,
1321 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001322 break;
1323 case tok::kw_int:
John McCallfec54012009-08-03 20:12:06 +00001324 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec,
1325 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001326 break;
1327 case tok::kw_float:
John McCallfec54012009-08-03 20:12:06 +00001328 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec,
1329 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001330 break;
1331 case tok::kw_double:
John McCallfec54012009-08-03 20:12:06 +00001332 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec,
1333 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001334 break;
1335 case tok::kw_wchar_t:
John McCallfec54012009-08-03 20:12:06 +00001336 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec,
1337 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001338 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001339 case tok::kw_char16_t:
John McCallfec54012009-08-03 20:12:06 +00001340 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec,
1341 DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001342 break;
1343 case tok::kw_char32_t:
John McCallfec54012009-08-03 20:12:06 +00001344 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec,
1345 DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001346 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00001347 case tok::kw_bool:
1348 case tok::kw__Bool:
John McCallfec54012009-08-03 20:12:06 +00001349 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec,
1350 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001351 break;
1352 case tok::kw__Decimal32:
John McCallfec54012009-08-03 20:12:06 +00001353 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
1354 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001355 break;
1356 case tok::kw__Decimal64:
John McCallfec54012009-08-03 20:12:06 +00001357 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
1358 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001359 break;
1360 case tok::kw__Decimal128:
John McCallfec54012009-08-03 20:12:06 +00001361 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
1362 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001363 break;
John Thompson82287d12010-02-05 00:12:22 +00001364 case tok::kw___vector:
1365 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
1366 break;
1367 case tok::kw___pixel:
1368 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
1369 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00001370
1371 // class-specifier:
1372 case tok::kw_class:
1373 case tok::kw_struct:
Chris Lattner4c97d762009-04-12 21:49:30 +00001374 case tok::kw_union: {
1375 tok::TokenKind Kind = Tok.getKind();
1376 ConsumeToken();
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001377 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS);
Chris Lattner80d0c892009-01-21 19:48:37 +00001378 continue;
Chris Lattner4c97d762009-04-12 21:49:30 +00001379 }
Chris Lattner80d0c892009-01-21 19:48:37 +00001380
1381 // enum-specifier:
1382 case tok::kw_enum:
Chris Lattner4c97d762009-04-12 21:49:30 +00001383 ConsumeToken();
Douglas Gregor9b9edd62010-03-02 17:53:14 +00001384 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS);
Chris Lattner80d0c892009-01-21 19:48:37 +00001385 continue;
1386
1387 // cv-qualifier:
1388 case tok::kw_const:
John McCallfec54012009-08-03 20:12:06 +00001389 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec, DiagID,
1390 getLang());
Chris Lattner80d0c892009-01-21 19:48:37 +00001391 break;
1392 case tok::kw_volatile:
John McCallfec54012009-08-03 20:12:06 +00001393 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
1394 getLang());
Chris Lattner80d0c892009-01-21 19:48:37 +00001395 break;
1396 case tok::kw_restrict:
John McCallfec54012009-08-03 20:12:06 +00001397 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
1398 getLang());
Chris Lattner80d0c892009-01-21 19:48:37 +00001399 break;
1400
Douglas Gregord57959a2009-03-27 23:10:48 +00001401 // C++ typename-specifier:
1402 case tok::kw_typename:
John McCall9ba61662010-02-26 08:45:28 +00001403 if (TryAnnotateTypeOrScopeToken()) {
1404 DS.SetTypeSpecError();
1405 goto DoneWithDeclSpec;
1406 }
1407 if (!Tok.is(tok::kw_typename))
Douglas Gregord57959a2009-03-27 23:10:48 +00001408 continue;
1409 break;
1410
Chris Lattner80d0c892009-01-21 19:48:37 +00001411 // GNU typeof support.
1412 case tok::kw_typeof:
1413 ParseTypeofSpecifier(DS);
1414 continue;
1415
Anders Carlsson6fd634f2009-06-24 17:47:40 +00001416 case tok::kw_decltype:
1417 ParseDecltypeSpecifier(DS);
1418 continue;
1419
Steve Naroffd3ded1f2008-06-05 00:02:44 +00001420 case tok::less:
Chris Lattner3bd934a2008-07-26 01:18:38 +00001421 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattnerbce61352008-07-26 00:20:22 +00001422 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
1423 // but we support it.
Chris Lattner3bd934a2008-07-26 01:18:38 +00001424 if (DS.hasTypeSpecifier() || !getLang().ObjC1)
Chris Lattnerbce61352008-07-26 00:20:22 +00001425 goto DoneWithDeclSpec;
Mike Stump1eb44332009-09-09 15:08:12 +00001426
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001427 ParseObjCProtocolQualifiers(DS);
Chris Lattner3bd934a2008-07-26 01:18:38 +00001428
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001429 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
1430 << FixItHint::CreateInsertion(Loc, "id")
1431 << SourceRange(Loc, DS.getSourceRange().getEnd());
1432
1433 // Need to support trailing type qualifiers (e.g. "id<p> const").
1434 // If a type specifier follows, it will be diagnosed elsewhere.
1435 continue;
Reid Spencer5f016e22007-07-11 17:01:13 +00001436 }
John McCallfec54012009-08-03 20:12:06 +00001437 // If the specifier wasn't legal, issue a diagnostic.
Reid Spencer5f016e22007-07-11 17:01:13 +00001438 if (isInvalid) {
1439 assert(PrevSpec && "Method did not return previous specifier!");
John McCallfec54012009-08-03 20:12:06 +00001440 assert(DiagID);
Douglas Gregorae2fb142010-08-23 14:34:43 +00001441
1442 if (DiagID == diag::ext_duplicate_declspec)
1443 Diag(Tok, DiagID)
1444 << PrevSpec << FixItHint::CreateRemoval(Tok.getLocation());
1445 else
1446 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00001447 }
Chris Lattner81c018d2008-03-13 06:29:04 +00001448 DS.SetRangeEnd(Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00001449 ConsumeToken();
1450 }
1451}
Douglas Gregoradcac882008-12-01 23:54:00 +00001452
Chris Lattner7a0ab5f2009-01-06 06:59:53 +00001453/// ParseOptionalTypeSpecifier - Try to parse a single type-specifier. We
Douglas Gregor12e083c2008-11-07 15:42:26 +00001454/// primarily follow the C++ grammar with additions for C99 and GNU,
1455/// which together subsume the C grammar. Note that the C++
1456/// type-specifier also includes the C type-qualifier (for const,
1457/// volatile, and C99 restrict). Returns true if a type-specifier was
1458/// found (and parsed), false otherwise.
1459///
1460/// type-specifier: [C++ 7.1.5]
1461/// simple-type-specifier
1462/// class-specifier
1463/// enum-specifier
1464/// elaborated-type-specifier [TODO]
1465/// cv-qualifier
1466///
1467/// cv-qualifier: [C++ 7.1.5.1]
1468/// 'const'
1469/// 'volatile'
1470/// [C99] 'restrict'
1471///
1472/// simple-type-specifier: [ C++ 7.1.5.2]
1473/// '::'[opt] nested-name-specifier[opt] type-name [TODO]
1474/// '::'[opt] nested-name-specifier 'template' template-id [TODO]
1475/// 'char'
1476/// 'wchar_t'
1477/// 'bool'
1478/// 'short'
1479/// 'int'
1480/// 'long'
1481/// 'signed'
1482/// 'unsigned'
1483/// 'float'
1484/// 'double'
1485/// 'void'
1486/// [C99] '_Bool'
1487/// [C99] '_Complex'
1488/// [C99] '_Imaginary' // Removed in TC2?
1489/// [GNU] '_Decimal32'
1490/// [GNU] '_Decimal64'
1491/// [GNU] '_Decimal128'
1492/// [GNU] typeof-specifier
1493/// [OBJC] class-name objc-protocol-refs[opt] [TODO]
1494/// [OBJC] typedef-name objc-protocol-refs[opt] [TODO]
Anders Carlsson6fd634f2009-06-24 17:47:40 +00001495/// [C++0x] 'decltype' ( expression )
John Thompson82287d12010-02-05 00:12:22 +00001496/// [AltiVec] '__vector'
John McCallfec54012009-08-03 20:12:06 +00001497bool Parser::ParseOptionalTypeSpecifier(DeclSpec &DS, bool& isInvalid,
Chris Lattner7a0ab5f2009-01-06 06:59:53 +00001498 const char *&PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001499 unsigned &DiagID,
Sebastian Redld9bafa72010-02-03 21:21:43 +00001500 const ParsedTemplateInfo &TemplateInfo,
1501 bool SuppressDeclarations) {
Douglas Gregor12e083c2008-11-07 15:42:26 +00001502 SourceLocation Loc = Tok.getLocation();
1503
1504 switch (Tok.getKind()) {
Chris Lattner166a8fc2009-01-04 23:41:41 +00001505 case tok::identifier: // foo::bar
Douglas Gregorc0b39642010-04-15 23:40:53 +00001506 // If we already have a type specifier, this identifier is not a type.
1507 if (DS.getTypeSpecType() != DeclSpec::TST_unspecified ||
1508 DS.getTypeSpecWidth() != DeclSpec::TSW_unspecified ||
1509 DS.getTypeSpecSign() != DeclSpec::TSS_unspecified)
1510 return false;
John Thompson82287d12010-02-05 00:12:22 +00001511 // Check for need to substitute AltiVec keyword tokens.
1512 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
1513 break;
1514 // Fall through.
Douglas Gregord57959a2009-03-27 23:10:48 +00001515 case tok::kw_typename: // typename foo::bar
Chris Lattner166a8fc2009-01-04 23:41:41 +00001516 // Annotate typenames and C++ scope specifiers. If we get one, just
1517 // recurse to handle whatever we get.
1518 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00001519 return true;
1520 if (Tok.is(tok::identifier))
1521 return false;
1522 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
1523 TemplateInfo, SuppressDeclarations);
Chris Lattner166a8fc2009-01-04 23:41:41 +00001524 case tok::coloncolon: // ::foo::bar
1525 if (NextToken().is(tok::kw_new) || // ::new
1526 NextToken().is(tok::kw_delete)) // ::delete
1527 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001528
Chris Lattner166a8fc2009-01-04 23:41:41 +00001529 // Annotate typenames and C++ scope specifiers. If we get one, just
1530 // recurse to handle whatever we get.
1531 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00001532 return true;
1533 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
1534 TemplateInfo, SuppressDeclarations);
Mike Stump1eb44332009-09-09 15:08:12 +00001535
Douglas Gregor12e083c2008-11-07 15:42:26 +00001536 // simple-type-specifier:
Chris Lattnerb31757b2009-01-06 05:06:21 +00001537 case tok::annot_typename: {
John McCallb3d87482010-08-24 05:47:05 +00001538 if (ParsedType T = getTypeAnnotation(Tok)) {
Douglas Gregor31a19b62009-04-01 21:51:26 +00001539 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallb3d87482010-08-24 05:47:05 +00001540 DiagID, T);
1541 } else
Douglas Gregor31a19b62009-04-01 21:51:26 +00001542 DS.SetTypeSpecError();
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001543 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1544 ConsumeToken(); // The typename
Mike Stump1eb44332009-09-09 15:08:12 +00001545
Douglas Gregor12e083c2008-11-07 15:42:26 +00001546 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1547 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
1548 // Objective-C interface. If we don't have Objective-C or a '<', this is
1549 // just a normal reference to a typedef name.
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001550 if (Tok.is(tok::less) && getLang().ObjC1)
1551 ParseObjCProtocolQualifiers(DS);
1552
Douglas Gregor12e083c2008-11-07 15:42:26 +00001553 return true;
1554 }
1555
1556 case tok::kw_short:
John McCallfec54012009-08-03 20:12:06 +00001557 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001558 break;
1559 case tok::kw_long:
1560 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCallfec54012009-08-03 20:12:06 +00001561 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
1562 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001563 else
John McCallfec54012009-08-03 20:12:06 +00001564 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
1565 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001566 break;
1567 case tok::kw_signed:
John McCallfec54012009-08-03 20:12:06 +00001568 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001569 break;
1570 case tok::kw_unsigned:
John McCallfec54012009-08-03 20:12:06 +00001571 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
1572 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001573 break;
1574 case tok::kw__Complex:
John McCallfec54012009-08-03 20:12:06 +00001575 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
1576 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001577 break;
1578 case tok::kw__Imaginary:
John McCallfec54012009-08-03 20:12:06 +00001579 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
1580 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001581 break;
1582 case tok::kw_void:
John McCallfec54012009-08-03 20:12:06 +00001583 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001584 break;
1585 case tok::kw_char:
John McCallfec54012009-08-03 20:12:06 +00001586 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001587 break;
1588 case tok::kw_int:
John McCallfec54012009-08-03 20:12:06 +00001589 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001590 break;
1591 case tok::kw_float:
John McCallfec54012009-08-03 20:12:06 +00001592 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001593 break;
1594 case tok::kw_double:
John McCallfec54012009-08-03 20:12:06 +00001595 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001596 break;
1597 case tok::kw_wchar_t:
John McCallfec54012009-08-03 20:12:06 +00001598 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001599 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001600 case tok::kw_char16_t:
John McCallfec54012009-08-03 20:12:06 +00001601 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001602 break;
1603 case tok::kw_char32_t:
John McCallfec54012009-08-03 20:12:06 +00001604 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001605 break;
Douglas Gregor12e083c2008-11-07 15:42:26 +00001606 case tok::kw_bool:
1607 case tok::kw__Bool:
John McCallfec54012009-08-03 20:12:06 +00001608 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001609 break;
1610 case tok::kw__Decimal32:
John McCallfec54012009-08-03 20:12:06 +00001611 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
1612 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001613 break;
1614 case tok::kw__Decimal64:
John McCallfec54012009-08-03 20:12:06 +00001615 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
1616 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001617 break;
1618 case tok::kw__Decimal128:
John McCallfec54012009-08-03 20:12:06 +00001619 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
1620 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001621 break;
John Thompson82287d12010-02-05 00:12:22 +00001622 case tok::kw___vector:
1623 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
1624 break;
1625 case tok::kw___pixel:
1626 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
1627 break;
1628
Douglas Gregor12e083c2008-11-07 15:42:26 +00001629 // class-specifier:
1630 case tok::kw_class:
1631 case tok::kw_struct:
Chris Lattner4c97d762009-04-12 21:49:30 +00001632 case tok::kw_union: {
1633 tok::TokenKind Kind = Tok.getKind();
1634 ConsumeToken();
Sebastian Redld9bafa72010-02-03 21:21:43 +00001635 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS_none,
1636 SuppressDeclarations);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001637 return true;
Chris Lattner4c97d762009-04-12 21:49:30 +00001638 }
Douglas Gregor12e083c2008-11-07 15:42:26 +00001639
1640 // enum-specifier:
1641 case tok::kw_enum:
Chris Lattner4c97d762009-04-12 21:49:30 +00001642 ConsumeToken();
Douglas Gregor9b9edd62010-03-02 17:53:14 +00001643 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS_none);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001644 return true;
1645
1646 // cv-qualifier:
1647 case tok::kw_const:
1648 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001649 DiagID, getLang());
Douglas Gregor12e083c2008-11-07 15:42:26 +00001650 break;
1651 case tok::kw_volatile:
1652 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001653 DiagID, getLang());
Douglas Gregor12e083c2008-11-07 15:42:26 +00001654 break;
1655 case tok::kw_restrict:
1656 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001657 DiagID, getLang());
Douglas Gregor12e083c2008-11-07 15:42:26 +00001658 break;
1659
1660 // GNU typeof support.
1661 case tok::kw_typeof:
1662 ParseTypeofSpecifier(DS);
1663 return true;
1664
Anders Carlsson6fd634f2009-06-24 17:47:40 +00001665 // C++0x decltype support.
1666 case tok::kw_decltype:
1667 ParseDecltypeSpecifier(DS);
1668 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00001669
Anders Carlsson0b7f7892009-06-26 23:44:14 +00001670 // C++0x auto support.
1671 case tok::kw_auto:
1672 if (!getLang().CPlusPlus0x)
1673 return false;
1674
John McCallfec54012009-08-03 20:12:06 +00001675 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec, DiagID);
Anders Carlsson0b7f7892009-06-26 23:44:14 +00001676 break;
Dawn Perchik52fc3142010-09-03 01:29:35 +00001677
Eli Friedman290eeb02009-06-08 23:27:34 +00001678 case tok::kw___ptr64:
1679 case tok::kw___w64:
Steve Naroff239f0732008-12-25 14:16:32 +00001680 case tok::kw___cdecl:
1681 case tok::kw___stdcall:
1682 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00001683 case tok::kw___thiscall:
Eli Friedman290eeb02009-06-08 23:27:34 +00001684 DS.AddAttributes(ParseMicrosoftTypeAttributes());
Chris Lattner837acd02009-01-21 19:19:26 +00001685 return true;
Steve Naroff239f0732008-12-25 14:16:32 +00001686
Dawn Perchik52fc3142010-09-03 01:29:35 +00001687 case tok::kw___pascal:
1688 DS.AddAttributes(ParseBorlandTypeAttributes());
1689 return true;
1690
Douglas Gregor12e083c2008-11-07 15:42:26 +00001691 default:
1692 // Not a type-specifier; do nothing.
1693 return false;
1694 }
1695
1696 // If the specifier combination wasn't legal, issue a diagnostic.
1697 if (isInvalid) {
1698 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner1ab3b962008-11-18 07:48:38 +00001699 // Pick between error or extwarn.
Chris Lattner1ab3b962008-11-18 07:48:38 +00001700 Diag(Tok, DiagID) << PrevSpec;
Douglas Gregor12e083c2008-11-07 15:42:26 +00001701 }
1702 DS.SetRangeEnd(Tok.getLocation());
1703 ConsumeToken(); // whatever we parsed above.
1704 return true;
1705}
Reid Spencer5f016e22007-07-11 17:01:13 +00001706
Chris Lattnercd4b83c2007-10-29 04:42:53 +00001707/// ParseStructDeclaration - Parse a struct declaration without the terminating
1708/// semicolon.
1709///
Reid Spencer5f016e22007-07-11 17:01:13 +00001710/// struct-declaration:
Chris Lattnercd4b83c2007-10-29 04:42:53 +00001711/// specifier-qualifier-list struct-declarator-list
Reid Spencer5f016e22007-07-11 17:01:13 +00001712/// [GNU] __extension__ struct-declaration
Chris Lattnercd4b83c2007-10-29 04:42:53 +00001713/// [GNU] specifier-qualifier-list
Reid Spencer5f016e22007-07-11 17:01:13 +00001714/// struct-declarator-list:
1715/// struct-declarator
1716/// struct-declarator-list ',' struct-declarator
1717/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
1718/// struct-declarator:
1719/// declarator
1720/// [GNU] declarator attributes[opt]
1721/// declarator[opt] ':' constant-expression
1722/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
1723///
Chris Lattnere1359422008-04-10 06:46:29 +00001724void Parser::
John McCallbdd563e2009-11-03 02:38:08 +00001725ParseStructDeclaration(DeclSpec &DS, FieldCallback &Fields) {
Chris Lattnerc46d1a12008-10-20 06:45:43 +00001726 if (Tok.is(tok::kw___extension__)) {
1727 // __extension__ silences extension warnings in the subexpression.
1728 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroff28a7ca82007-08-20 22:28:22 +00001729 ConsumeToken();
Chris Lattnerc46d1a12008-10-20 06:45:43 +00001730 return ParseStructDeclaration(DS, Fields);
1731 }
Mike Stump1eb44332009-09-09 15:08:12 +00001732
Steve Naroff28a7ca82007-08-20 22:28:22 +00001733 // Parse the common specifier-qualifiers-list piece.
Chris Lattner60b1e3e2008-04-10 06:15:14 +00001734 SourceLocation DSStart = Tok.getLocation();
Steve Naroff28a7ca82007-08-20 22:28:22 +00001735 ParseSpecifierQualifierList(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00001736
Douglas Gregor4920f1f2009-01-12 22:49:06 +00001737 // If there are no declarators, this is a free-standing declaration
1738 // specifier. Let the actions module cope with it.
Chris Lattner04d66662007-10-09 17:33:22 +00001739 if (Tok.is(tok::semi)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001740 Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none, DS);
Steve Naroff28a7ca82007-08-20 22:28:22 +00001741 return;
1742 }
1743
1744 // Read struct-declarators until we find the semicolon.
John McCallbdd563e2009-11-03 02:38:08 +00001745 bool FirstDeclarator = true;
Steve Naroff28a7ca82007-08-20 22:28:22 +00001746 while (1) {
John McCall54abf7d2009-11-04 02:18:39 +00001747 ParsingDeclRAIIObject PD(*this);
John McCallbdd563e2009-11-03 02:38:08 +00001748 FieldDeclarator DeclaratorInfo(DS);
1749
1750 // Attributes are only allowed here on successive declarators.
1751 if (!FirstDeclarator && Tok.is(tok::kw___attribute)) {
1752 SourceLocation Loc;
Sean Huntbbd37c62009-11-21 08:43:09 +00001753 AttributeList *AttrList = ParseGNUAttributes(&Loc);
John McCallbdd563e2009-11-03 02:38:08 +00001754 DeclaratorInfo.D.AddAttributes(AttrList, Loc);
1755 }
Mike Stump1eb44332009-09-09 15:08:12 +00001756
Steve Naroff28a7ca82007-08-20 22:28:22 +00001757 /// struct-declarator: declarator
1758 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattnera1efc8c2009-12-10 01:59:24 +00001759 if (Tok.isNot(tok::colon)) {
1760 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
1761 ColonProtectionRAIIObject X(*this);
Chris Lattnere1359422008-04-10 06:46:29 +00001762 ParseDeclarator(DeclaratorInfo.D);
Chris Lattnera1efc8c2009-12-10 01:59:24 +00001763 }
Mike Stump1eb44332009-09-09 15:08:12 +00001764
Chris Lattner04d66662007-10-09 17:33:22 +00001765 if (Tok.is(tok::colon)) {
Steve Naroff28a7ca82007-08-20 22:28:22 +00001766 ConsumeToken();
John McCall60d7b3a2010-08-24 06:29:42 +00001767 ExprResult Res(ParseConstantExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001768 if (Res.isInvalid())
Steve Naroff28a7ca82007-08-20 22:28:22 +00001769 SkipUntil(tok::semi, true, true);
Chris Lattner60b1e3e2008-04-10 06:15:14 +00001770 else
Sebastian Redleffa8d12008-12-10 00:02:53 +00001771 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroff28a7ca82007-08-20 22:28:22 +00001772 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00001773
Steve Naroff28a7ca82007-08-20 22:28:22 +00001774 // If attributes exist after the declarator, parse them.
Sebastian Redlab197ba2009-02-09 18:23:29 +00001775 if (Tok.is(tok::kw___attribute)) {
1776 SourceLocation Loc;
Sean Huntbbd37c62009-11-21 08:43:09 +00001777 AttributeList *AttrList = ParseGNUAttributes(&Loc);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001778 DeclaratorInfo.D.AddAttributes(AttrList, Loc);
1779 }
1780
John McCallbdd563e2009-11-03 02:38:08 +00001781 // We're done with this declarator; invoke the callback.
John McCalld226f652010-08-21 09:40:31 +00001782 Decl *D = Fields.invoke(DeclaratorInfo);
John McCall54abf7d2009-11-04 02:18:39 +00001783 PD.complete(D);
John McCallbdd563e2009-11-03 02:38:08 +00001784
Steve Naroff28a7ca82007-08-20 22:28:22 +00001785 // If we don't have a comma, it is either the end of the list (a ';')
1786 // or an error, bail out.
Chris Lattner04d66662007-10-09 17:33:22 +00001787 if (Tok.isNot(tok::comma))
Chris Lattnercd4b83c2007-10-29 04:42:53 +00001788 return;
Sebastian Redlab197ba2009-02-09 18:23:29 +00001789
Steve Naroff28a7ca82007-08-20 22:28:22 +00001790 // Consume the comma.
1791 ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00001792
John McCallbdd563e2009-11-03 02:38:08 +00001793 FirstDeclarator = false;
Steve Naroff28a7ca82007-08-20 22:28:22 +00001794 }
Steve Naroff28a7ca82007-08-20 22:28:22 +00001795}
1796
1797/// ParseStructUnionBody
1798/// struct-contents:
1799/// struct-declaration-list
1800/// [EXT] empty
1801/// [GNU] "struct-declaration-list" without terminatoring ';'
1802/// struct-declaration-list:
1803/// struct-declaration
1804/// struct-declaration-list struct-declaration
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00001805/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroff28a7ca82007-08-20 22:28:22 +00001806///
Reid Spencer5f016e22007-07-11 17:01:13 +00001807void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
John McCalld226f652010-08-21 09:40:31 +00001808 unsigned TagType, Decl *TagDecl) {
John McCallf312b1e2010-08-26 23:41:50 +00001809 PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc,
1810 "parsing struct/union body");
Mike Stump1eb44332009-09-09 15:08:12 +00001811
Reid Spencer5f016e22007-07-11 17:01:13 +00001812 SourceLocation LBraceLoc = ConsumeBrace();
Mike Stump1eb44332009-09-09 15:08:12 +00001813
Douglas Gregor3218c4b2009-01-09 22:42:13 +00001814 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
Douglas Gregor23c94db2010-07-02 17:43:08 +00001815 Actions.ActOnTagStartDefinition(getCurScope(), TagDecl);
Douglas Gregor72de6672009-01-08 20:45:30 +00001816
Reid Spencer5f016e22007-07-11 17:01:13 +00001817 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
1818 // C++.
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001819 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Douglas Gregor03332962010-07-29 14:29:34 +00001820 Diag(Tok, diag::ext_empty_struct_union)
1821 << (TagType == TST_union);
Reid Spencer5f016e22007-07-11 17:01:13 +00001822
John McCalld226f652010-08-21 09:40:31 +00001823 llvm::SmallVector<Decl *, 32> FieldDecls;
Chris Lattnere1359422008-04-10 06:46:29 +00001824
Reid Spencer5f016e22007-07-11 17:01:13 +00001825 // While we still have something to read, read the declarations in the struct.
Chris Lattner04d66662007-10-09 17:33:22 +00001826 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001827 // Each iteration of this loop reads one struct-declaration.
Mike Stump1eb44332009-09-09 15:08:12 +00001828
Reid Spencer5f016e22007-07-11 17:01:13 +00001829 // Check for extraneous top-level semicolon.
Chris Lattner04d66662007-10-09 17:33:22 +00001830 if (Tok.is(tok::semi)) {
Douglas Gregor9b3064b2009-04-01 22:41:11 +00001831 Diag(Tok, diag::ext_extra_struct_semi)
Douglas Gregorf13ca062010-06-16 23:08:59 +00001832 << DeclSpec::getSpecifierName((DeclSpec::TST)TagType)
Douglas Gregor849b2432010-03-31 17:46:05 +00001833 << FixItHint::CreateRemoval(Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00001834 ConsumeToken();
1835 continue;
1836 }
Chris Lattnere1359422008-04-10 06:46:29 +00001837
1838 // Parse all the comma separated declarators.
1839 DeclSpec DS;
Mike Stump1eb44332009-09-09 15:08:12 +00001840
John McCallbdd563e2009-11-03 02:38:08 +00001841 if (!Tok.is(tok::at)) {
1842 struct CFieldCallback : FieldCallback {
1843 Parser &P;
John McCalld226f652010-08-21 09:40:31 +00001844 Decl *TagDecl;
1845 llvm::SmallVectorImpl<Decl *> &FieldDecls;
John McCallbdd563e2009-11-03 02:38:08 +00001846
John McCalld226f652010-08-21 09:40:31 +00001847 CFieldCallback(Parser &P, Decl *TagDecl,
1848 llvm::SmallVectorImpl<Decl *> &FieldDecls) :
John McCallbdd563e2009-11-03 02:38:08 +00001849 P(P), TagDecl(TagDecl), FieldDecls(FieldDecls) {}
1850
John McCalld226f652010-08-21 09:40:31 +00001851 virtual Decl *invoke(FieldDeclarator &FD) {
John McCallbdd563e2009-11-03 02:38:08 +00001852 // Install the declarator into the current TagDecl.
John McCalld226f652010-08-21 09:40:31 +00001853 Decl *Field = P.Actions.ActOnField(P.getCurScope(), TagDecl,
John McCall4ba39712009-11-03 21:13:47 +00001854 FD.D.getDeclSpec().getSourceRange().getBegin(),
1855 FD.D, FD.BitfieldSize);
John McCallbdd563e2009-11-03 02:38:08 +00001856 FieldDecls.push_back(Field);
1857 return Field;
Douglas Gregor91a28862009-08-26 14:27:30 +00001858 }
John McCallbdd563e2009-11-03 02:38:08 +00001859 } Callback(*this, TagDecl, FieldDecls);
1860
1861 ParseStructDeclaration(DS, Callback);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00001862 } else { // Handle @defs
1863 ConsumeToken();
1864 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
1865 Diag(Tok, diag::err_unexpected_at);
Chris Lattner3e156ad2010-02-02 00:37:27 +00001866 SkipUntil(tok::semi, true);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00001867 continue;
1868 }
1869 ConsumeToken();
1870 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
1871 if (!Tok.is(tok::identifier)) {
1872 Diag(Tok, diag::err_expected_ident);
Chris Lattner3e156ad2010-02-02 00:37:27 +00001873 SkipUntil(tok::semi, true);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00001874 continue;
1875 }
John McCalld226f652010-08-21 09:40:31 +00001876 llvm::SmallVector<Decl *, 16> Fields;
Douglas Gregor23c94db2010-07-02 17:43:08 +00001877 Actions.ActOnDefs(getCurScope(), TagDecl, Tok.getLocation(),
Douglas Gregor44b43212008-12-11 16:49:14 +00001878 Tok.getIdentifierInfo(), Fields);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00001879 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
1880 ConsumeToken();
1881 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
Mike Stump1eb44332009-09-09 15:08:12 +00001882 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001883
Chris Lattner04d66662007-10-09 17:33:22 +00001884 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001885 ConsumeToken();
Chris Lattner04d66662007-10-09 17:33:22 +00001886 } else if (Tok.is(tok::r_brace)) {
Chris Lattner3e156ad2010-02-02 00:37:27 +00001887 ExpectAndConsume(tok::semi, diag::ext_expected_semi_decl_list);
Reid Spencer5f016e22007-07-11 17:01:13 +00001888 break;
1889 } else {
Chris Lattner3e156ad2010-02-02 00:37:27 +00001890 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
1891 // Skip to end of block or statement to avoid ext-warning on extra ';'.
Reid Spencer5f016e22007-07-11 17:01:13 +00001892 SkipUntil(tok::r_brace, true, true);
Chris Lattner3e156ad2010-02-02 00:37:27 +00001893 // If we stopped at a ';', eat it.
1894 if (Tok.is(tok::semi)) ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00001895 }
1896 }
Mike Stump1eb44332009-09-09 15:08:12 +00001897
Steve Naroff60fccee2007-10-29 21:38:07 +00001898 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001899
Ted Kremenek1e377652010-02-11 02:19:13 +00001900 llvm::OwningPtr<AttributeList> AttrList;
Reid Spencer5f016e22007-07-11 17:01:13 +00001901 // If attributes exist after struct contents, parse them.
Chris Lattner04d66662007-10-09 17:33:22 +00001902 if (Tok.is(tok::kw___attribute))
Ted Kremenek1e377652010-02-11 02:19:13 +00001903 AttrList.reset(ParseGNUAttributes());
Daniel Dunbar1bfe1c22008-10-03 02:03:53 +00001904
Douglas Gregor23c94db2010-07-02 17:43:08 +00001905 Actions.ActOnFields(getCurScope(),
Jay Foadbeaaccd2009-05-21 09:52:38 +00001906 RecordLoc, TagDecl, FieldDecls.data(), FieldDecls.size(),
Daniel Dunbar1bfe1c22008-10-03 02:03:53 +00001907 LBraceLoc, RBraceLoc,
Ted Kremenek1e377652010-02-11 02:19:13 +00001908 AttrList.get());
Douglas Gregor72de6672009-01-08 20:45:30 +00001909 StructScope.Exit();
Douglas Gregor23c94db2010-07-02 17:43:08 +00001910 Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl, RBraceLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001911}
1912
1913
1914/// ParseEnumSpecifier
1915/// enum-specifier: [C99 6.7.2.2]
1916/// 'enum' identifier[opt] '{' enumerator-list '}'
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001917///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Reid Spencer5f016e22007-07-11 17:01:13 +00001918/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
1919/// '}' attributes[opt]
1920/// 'enum' identifier
1921/// [GNU] 'enum' attributes[opt] identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001922///
Douglas Gregor1274ccd2010-10-08 23:50:27 +00001923/// [C++0x] enum-head '{' enumerator-list[opt] '}'
1924/// [C++0x] enum-head '{' enumerator-list ',' '}'
1925///
1926/// enum-head: [C++0x]
1927/// enum-key attributes[opt] identifier[opt] enum-base[opt]
1928/// enum-key attributes[opt] nested-name-specifier identifier enum-base[opt]
1929///
1930/// enum-key: [C++0x]
1931/// 'enum'
1932/// 'enum' 'class'
1933/// 'enum' 'struct'
1934///
1935/// enum-base: [C++0x]
1936/// ':' type-specifier-seq
1937///
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001938/// [C++] elaborated-type-specifier:
1939/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
1940///
Chris Lattner4c97d762009-04-12 21:49:30 +00001941void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregor9b9edd62010-03-02 17:53:14 +00001942 const ParsedTemplateInfo &TemplateInfo,
Chris Lattner4c97d762009-04-12 21:49:30 +00001943 AccessSpecifier AS) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001944 // Parse the tag portion of this.
Douglas Gregor374929f2009-09-18 15:37:17 +00001945 if (Tok.is(tok::code_completion)) {
1946 // Code completion for an enum name.
Douglas Gregor23c94db2010-07-02 17:43:08 +00001947 Actions.CodeCompleteTag(getCurScope(), DeclSpec::TST_enum);
Douglas Gregordc845342010-05-25 05:58:43 +00001948 ConsumeCodeCompletionToken();
Douglas Gregor374929f2009-09-18 15:37:17 +00001949 }
1950
Ted Kremenek1e377652010-02-11 02:19:13 +00001951 llvm::OwningPtr<AttributeList> Attr;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001952 // If attributes exist after tag, parse them.
1953 if (Tok.is(tok::kw___attribute))
Ted Kremenek1e377652010-02-11 02:19:13 +00001954 Attr.reset(ParseGNUAttributes());
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001955
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00001956 CXXScopeSpec &SS = DS.getTypeSpecScope();
John McCall9ba61662010-02-26 08:45:28 +00001957 if (getLang().CPlusPlus) {
John McCallb3d87482010-08-24 05:47:05 +00001958 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), false))
John McCall9ba61662010-02-26 08:45:28 +00001959 return;
1960
1961 if (SS.isSet() && Tok.isNot(tok::identifier)) {
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001962 Diag(Tok, diag::err_expected_ident);
1963 if (Tok.isNot(tok::l_brace)) {
1964 // Has no name and is not a definition.
1965 // Skip the rest of this declarator, up until the comma or semicolon.
1966 SkipUntil(tok::comma, true);
1967 return;
1968 }
1969 }
1970 }
Mike Stump1eb44332009-09-09 15:08:12 +00001971
Douglas Gregor1274ccd2010-10-08 23:50:27 +00001972 bool IsScopedEnum = false;
1973
1974 if (getLang().CPlusPlus0x && (Tok.is(tok::kw_class)
1975 || Tok.is(tok::kw_struct))) {
1976 ConsumeToken();
1977 IsScopedEnum = true;
1978 }
1979
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001980 // Must have either 'enum name' or 'enum {...}'.
1981 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace)) {
1982 Diag(Tok, diag::err_expected_ident_lbrace);
Mike Stump1eb44332009-09-09 15:08:12 +00001983
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001984 // Skip the rest of this declarator, up until the comma or semicolon.
1985 SkipUntil(tok::comma, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00001986 return;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001987 }
Mike Stump1eb44332009-09-09 15:08:12 +00001988
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001989 // If an identifier is present, consume and remember it.
1990 IdentifierInfo *Name = 0;
1991 SourceLocation NameLoc;
1992 if (Tok.is(tok::identifier)) {
1993 Name = Tok.getIdentifierInfo();
1994 NameLoc = ConsumeToken();
1995 }
Mike Stump1eb44332009-09-09 15:08:12 +00001996
Douglas Gregor1274ccd2010-10-08 23:50:27 +00001997 if (!Name && IsScopedEnum) {
1998 // C++0x 7.2p2: The optional identifier shall not be omitted in the
1999 // declaration of a scoped enumeration.
2000 Diag(Tok, diag::err_scoped_enum_missing_identifier);
2001 IsScopedEnum = false;
2002 }
2003
2004 TypeResult BaseType;
2005
2006 if (getLang().CPlusPlus0x && Tok.is(tok::colon)) {
2007 ConsumeToken();
2008 SourceRange Range;
2009 BaseType = ParseTypeName(&Range);
2010 }
2011
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002012 // There are three options here. If we have 'enum foo;', then this is a
2013 // forward declaration. If we have 'enum foo {...' then this is a
2014 // definition. Otherwise we have something like 'enum foo xyz', a reference.
2015 //
2016 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
2017 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
2018 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
2019 //
John McCallf312b1e2010-08-26 23:41:50 +00002020 Sema::TagUseKind TUK;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002021 if (Tok.is(tok::l_brace))
John McCallf312b1e2010-08-26 23:41:50 +00002022 TUK = Sema::TUK_Definition;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002023 else if (Tok.is(tok::semi))
John McCallf312b1e2010-08-26 23:41:50 +00002024 TUK = Sema::TUK_Declaration;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002025 else
John McCallf312b1e2010-08-26 23:41:50 +00002026 TUK = Sema::TUK_Reference;
Douglas Gregor8fc6d232010-05-03 17:48:54 +00002027
2028 // enums cannot be templates, although they can be referenced from a
2029 // template.
2030 if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate &&
John McCallf312b1e2010-08-26 23:41:50 +00002031 TUK != Sema::TUK_Reference) {
Douglas Gregor8fc6d232010-05-03 17:48:54 +00002032 Diag(Tok, diag::err_enum_template);
2033
2034 // Skip the rest of this declarator, up until the comma or semicolon.
2035 SkipUntil(tok::comma, true);
2036 return;
2037 }
2038
Douglas Gregor402abb52009-05-28 23:31:59 +00002039 bool Owned = false;
John McCallc4e70192009-09-11 04:59:25 +00002040 bool IsDependent = false;
Douglas Gregor48c89f42010-04-24 16:38:41 +00002041 SourceLocation TSTLoc = NameLoc.isValid()? NameLoc : StartLoc;
2042 const char *PrevSpec = 0;
2043 unsigned DiagID;
John McCalld226f652010-08-21 09:40:31 +00002044 Decl *TagDecl = Actions.ActOnTag(getCurScope(), DeclSpec::TST_enum, TUK,
2045 StartLoc, SS, Name, NameLoc, Attr.get(),
2046 AS,
John McCallf312b1e2010-08-26 23:41:50 +00002047 MultiTemplateParamsArg(Actions),
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002048 Owned, IsDependent, IsScopedEnum,
2049 BaseType);
2050
Douglas Gregor48c89f42010-04-24 16:38:41 +00002051 if (IsDependent) {
2052 // This enum has a dependent nested-name-specifier. Handle it as a
2053 // dependent tag.
2054 if (!Name) {
2055 DS.SetTypeSpecError();
2056 Diag(Tok, diag::err_expected_type_name_after_typename);
2057 return;
2058 }
2059
Douglas Gregor23c94db2010-07-02 17:43:08 +00002060 TypeResult Type = Actions.ActOnDependentTag(getCurScope(), DeclSpec::TST_enum,
Douglas Gregor48c89f42010-04-24 16:38:41 +00002061 TUK, SS, Name, StartLoc,
2062 NameLoc);
2063 if (Type.isInvalid()) {
2064 DS.SetTypeSpecError();
2065 return;
2066 }
2067
2068 if (DS.SetTypeSpecType(DeclSpec::TST_typename, TSTLoc, PrevSpec, DiagID,
John McCallb3d87482010-08-24 05:47:05 +00002069 Type.get()))
Douglas Gregor48c89f42010-04-24 16:38:41 +00002070 Diag(StartLoc, DiagID) << PrevSpec;
2071
2072 return;
2073 }
Mike Stump1eb44332009-09-09 15:08:12 +00002074
John McCalld226f652010-08-21 09:40:31 +00002075 if (!TagDecl) {
Douglas Gregor48c89f42010-04-24 16:38:41 +00002076 // The action failed to produce an enumeration tag. If this is a
2077 // definition, consume the entire definition.
2078 if (Tok.is(tok::l_brace)) {
2079 ConsumeBrace();
2080 SkipUntil(tok::r_brace);
2081 }
2082
2083 DS.SetTypeSpecError();
2084 return;
2085 }
2086
Chris Lattner04d66662007-10-09 17:33:22 +00002087 if (Tok.is(tok::l_brace))
Reid Spencer5f016e22007-07-11 17:01:13 +00002088 ParseEnumBody(StartLoc, TagDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00002089
John McCallb3d87482010-08-24 05:47:05 +00002090 // FIXME: The DeclSpec should keep the locations of both the keyword
2091 // and the name (if there is one).
Douglas Gregorb988f9c2010-01-25 16:33:23 +00002092 if (DS.SetTypeSpecType(DeclSpec::TST_enum, TSTLoc, PrevSpec, DiagID,
John McCalld226f652010-08-21 09:40:31 +00002093 TagDecl, Owned))
John McCallfec54012009-08-03 20:12:06 +00002094 Diag(StartLoc, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00002095}
2096
2097/// ParseEnumBody - Parse a {} enclosed enumerator-list.
2098/// enumerator-list:
2099/// enumerator
2100/// enumerator-list ',' enumerator
2101/// enumerator:
2102/// enumeration-constant
2103/// enumeration-constant '=' constant-expression
2104/// enumeration-constant:
2105/// identifier
2106///
John McCalld226f652010-08-21 09:40:31 +00002107void Parser::ParseEnumBody(SourceLocation StartLoc, Decl *EnumDecl) {
Douglas Gregor074149e2009-01-05 19:45:36 +00002108 // Enter the scope of the enum body and start the definition.
2109 ParseScope EnumScope(this, Scope::DeclScope);
Douglas Gregor23c94db2010-07-02 17:43:08 +00002110 Actions.ActOnTagStartDefinition(getCurScope(), EnumDecl);
Douglas Gregor074149e2009-01-05 19:45:36 +00002111
Reid Spencer5f016e22007-07-11 17:01:13 +00002112 SourceLocation LBraceLoc = ConsumeBrace();
Mike Stump1eb44332009-09-09 15:08:12 +00002113
Chris Lattner7946dd32007-08-27 17:24:30 +00002114 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
Chris Lattner04d66662007-10-09 17:33:22 +00002115 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Fariborz Jahanian05115522010-05-28 22:23:22 +00002116 Diag(Tok, diag::error_empty_enum);
Mike Stump1eb44332009-09-09 15:08:12 +00002117
John McCalld226f652010-08-21 09:40:31 +00002118 llvm::SmallVector<Decl *, 32> EnumConstantDecls;
Reid Spencer5f016e22007-07-11 17:01:13 +00002119
John McCalld226f652010-08-21 09:40:31 +00002120 Decl *LastEnumConstDecl = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00002121
Reid Spencer5f016e22007-07-11 17:01:13 +00002122 // Parse the enumerator-list.
Chris Lattner04d66662007-10-09 17:33:22 +00002123 while (Tok.is(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002124 IdentifierInfo *Ident = Tok.getIdentifierInfo();
2125 SourceLocation IdentLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00002126
John McCall5b629aa2010-10-22 23:36:17 +00002127 // If attributes exist after the enumerator, parse them.
2128 llvm::OwningPtr<AttributeList> Attr;
2129 if (Tok.is(tok::kw___attribute))
2130 Attr.reset(ParseGNUAttributes());
2131
Reid Spencer5f016e22007-07-11 17:01:13 +00002132 SourceLocation EqualLoc;
John McCall60d7b3a2010-08-24 06:29:42 +00002133 ExprResult AssignedVal;
Chris Lattner04d66662007-10-09 17:33:22 +00002134 if (Tok.is(tok::equal)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002135 EqualLoc = ConsumeToken();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002136 AssignedVal = ParseConstantExpression();
2137 if (AssignedVal.isInvalid())
Reid Spencer5f016e22007-07-11 17:01:13 +00002138 SkipUntil(tok::comma, tok::r_brace, true, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00002139 }
Mike Stump1eb44332009-09-09 15:08:12 +00002140
Reid Spencer5f016e22007-07-11 17:01:13 +00002141 // Install the enumerator constant into EnumDecl.
John McCalld226f652010-08-21 09:40:31 +00002142 Decl *EnumConstDecl = Actions.ActOnEnumConstant(getCurScope(), EnumDecl,
2143 LastEnumConstDecl,
2144 IdentLoc, Ident,
John McCall5b629aa2010-10-22 23:36:17 +00002145 Attr.get(), EqualLoc,
John McCalld226f652010-08-21 09:40:31 +00002146 AssignedVal.release());
Reid Spencer5f016e22007-07-11 17:01:13 +00002147 EnumConstantDecls.push_back(EnumConstDecl);
2148 LastEnumConstDecl = EnumConstDecl;
Mike Stump1eb44332009-09-09 15:08:12 +00002149
Douglas Gregor751f6922010-09-07 14:51:08 +00002150 if (Tok.is(tok::identifier)) {
2151 // We're missing a comma between enumerators.
2152 SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation);
2153 Diag(Loc, diag::err_enumerator_list_missing_comma)
2154 << FixItHint::CreateInsertion(Loc, ", ");
2155 continue;
2156 }
2157
Chris Lattner04d66662007-10-09 17:33:22 +00002158 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +00002159 break;
2160 SourceLocation CommaLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00002161
2162 if (Tok.isNot(tok::identifier) &&
Douglas Gregor9b3064b2009-04-01 22:41:11 +00002163 !(getLang().C99 || getLang().CPlusPlus0x))
2164 Diag(CommaLoc, diag::ext_enumerator_list_comma)
2165 << getLang().CPlusPlus
Douglas Gregor849b2432010-03-31 17:46:05 +00002166 << FixItHint::CreateRemoval(CommaLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00002167 }
Mike Stump1eb44332009-09-09 15:08:12 +00002168
Reid Spencer5f016e22007-07-11 17:01:13 +00002169 // Eat the }.
Mike Stumpc6e35aa2009-05-16 07:06:02 +00002170 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00002171
Ted Kremenek1e377652010-02-11 02:19:13 +00002172 llvm::OwningPtr<AttributeList> Attr;
Reid Spencer5f016e22007-07-11 17:01:13 +00002173 // If attributes exist after the identifier list, parse them.
Chris Lattner04d66662007-10-09 17:33:22 +00002174 if (Tok.is(tok::kw___attribute))
Ted Kremenek1e377652010-02-11 02:19:13 +00002175 Attr.reset(ParseGNUAttributes()); // FIXME: where do they do?
Douglas Gregor72de6672009-01-08 20:45:30 +00002176
Edward O'Callaghanfee13812009-08-08 14:36:57 +00002177 Actions.ActOnEnumBody(StartLoc, LBraceLoc, RBraceLoc, EnumDecl,
2178 EnumConstantDecls.data(), EnumConstantDecls.size(),
Douglas Gregor23c94db2010-07-02 17:43:08 +00002179 getCurScope(), Attr.get());
Mike Stump1eb44332009-09-09 15:08:12 +00002180
Douglas Gregor72de6672009-01-08 20:45:30 +00002181 EnumScope.Exit();
Douglas Gregor23c94db2010-07-02 17:43:08 +00002182 Actions.ActOnTagFinishDefinition(getCurScope(), EnumDecl, RBraceLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00002183}
2184
2185/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff5f8aa692008-02-11 23:15:56 +00002186/// start of a type-qualifier-list.
2187bool Parser::isTypeQualifier() const {
2188 switch (Tok.getKind()) {
2189 default: return false;
2190 // type-qualifier
2191 case tok::kw_const:
2192 case tok::kw_volatile:
2193 case tok::kw_restrict:
2194 return true;
2195 }
2196}
2197
Chris Lattnerb3a4e432010-02-28 18:18:36 +00002198/// isKnownToBeTypeSpecifier - Return true if we know that the specified token
2199/// is definitely a type-specifier. Return false if it isn't part of a type
2200/// specifier or if we're not sure.
2201bool Parser::isKnownToBeTypeSpecifier(const Token &Tok) const {
2202 switch (Tok.getKind()) {
2203 default: return false;
2204 // type-specifiers
2205 case tok::kw_short:
2206 case tok::kw_long:
2207 case tok::kw_signed:
2208 case tok::kw_unsigned:
2209 case tok::kw__Complex:
2210 case tok::kw__Imaginary:
2211 case tok::kw_void:
2212 case tok::kw_char:
2213 case tok::kw_wchar_t:
2214 case tok::kw_char16_t:
2215 case tok::kw_char32_t:
2216 case tok::kw_int:
2217 case tok::kw_float:
2218 case tok::kw_double:
2219 case tok::kw_bool:
2220 case tok::kw__Bool:
2221 case tok::kw__Decimal32:
2222 case tok::kw__Decimal64:
2223 case tok::kw__Decimal128:
2224 case tok::kw___vector:
2225
2226 // struct-or-union-specifier (C99) or class-specifier (C++)
2227 case tok::kw_class:
2228 case tok::kw_struct:
2229 case tok::kw_union:
2230 // enum-specifier
2231 case tok::kw_enum:
2232
2233 // typedef-name
2234 case tok::annot_typename:
2235 return true;
2236 }
2237}
2238
Steve Naroff5f8aa692008-02-11 23:15:56 +00002239/// isTypeSpecifierQualifier - Return true if the current token could be the
Reid Spencer5f016e22007-07-11 17:01:13 +00002240/// start of a specifier-qualifier-list.
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002241bool Parser::isTypeSpecifierQualifier() {
Reid Spencer5f016e22007-07-11 17:01:13 +00002242 switch (Tok.getKind()) {
2243 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002244
Chris Lattner166a8fc2009-01-04 23:41:41 +00002245 case tok::identifier: // foo::bar
John Thompson82287d12010-02-05 00:12:22 +00002246 if (TryAltiVecVectorToken())
2247 return true;
2248 // Fall through.
Douglas Gregord57959a2009-03-27 23:10:48 +00002249 case tok::kw_typename: // typename T::type
Chris Lattner166a8fc2009-01-04 23:41:41 +00002250 // Annotate typenames and C++ scope specifiers. If we get one, just
2251 // recurse to handle whatever we get.
2252 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00002253 return true;
2254 if (Tok.is(tok::identifier))
2255 return false;
2256 return isTypeSpecifierQualifier();
Douglas Gregord57959a2009-03-27 23:10:48 +00002257
Chris Lattner166a8fc2009-01-04 23:41:41 +00002258 case tok::coloncolon: // ::foo::bar
2259 if (NextToken().is(tok::kw_new) || // ::new
2260 NextToken().is(tok::kw_delete)) // ::delete
2261 return false;
2262
Chris Lattner166a8fc2009-01-04 23:41:41 +00002263 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00002264 return true;
2265 return isTypeSpecifierQualifier();
Mike Stump1eb44332009-09-09 15:08:12 +00002266
Reid Spencer5f016e22007-07-11 17:01:13 +00002267 // GNU attributes support.
2268 case tok::kw___attribute:
Steve Naroffd1861fd2007-07-31 12:34:36 +00002269 // GNU typeof support.
2270 case tok::kw_typeof:
Mike Stump1eb44332009-09-09 15:08:12 +00002271
Reid Spencer5f016e22007-07-11 17:01:13 +00002272 // type-specifiers
2273 case tok::kw_short:
2274 case tok::kw_long:
2275 case tok::kw_signed:
2276 case tok::kw_unsigned:
2277 case tok::kw__Complex:
2278 case tok::kw__Imaginary:
2279 case tok::kw_void:
2280 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00002281 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002282 case tok::kw_char16_t:
2283 case tok::kw_char32_t:
Reid Spencer5f016e22007-07-11 17:01:13 +00002284 case tok::kw_int:
2285 case tok::kw_float:
2286 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00002287 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00002288 case tok::kw__Bool:
2289 case tok::kw__Decimal32:
2290 case tok::kw__Decimal64:
2291 case tok::kw__Decimal128:
John Thompson82287d12010-02-05 00:12:22 +00002292 case tok::kw___vector:
Mike Stump1eb44332009-09-09 15:08:12 +00002293
Chris Lattner99dc9142008-04-13 18:59:07 +00002294 // struct-or-union-specifier (C99) or class-specifier (C++)
2295 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00002296 case tok::kw_struct:
2297 case tok::kw_union:
2298 // enum-specifier
2299 case tok::kw_enum:
Mike Stump1eb44332009-09-09 15:08:12 +00002300
Reid Spencer5f016e22007-07-11 17:01:13 +00002301 // type-qualifier
2302 case tok::kw_const:
2303 case tok::kw_volatile:
2304 case tok::kw_restrict:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002305
2306 // typedef-name
Chris Lattnerb31757b2009-01-06 05:06:21 +00002307 case tok::annot_typename:
Reid Spencer5f016e22007-07-11 17:01:13 +00002308 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00002309
Chris Lattner7c186be2008-10-20 00:25:30 +00002310 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
2311 case tok::less:
2312 return getLang().ObjC1;
Mike Stump1eb44332009-09-09 15:08:12 +00002313
Steve Naroff239f0732008-12-25 14:16:32 +00002314 case tok::kw___cdecl:
2315 case tok::kw___stdcall:
2316 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00002317 case tok::kw___thiscall:
Eli Friedman290eeb02009-06-08 23:27:34 +00002318 case tok::kw___w64:
2319 case tok::kw___ptr64:
Dawn Perchik52fc3142010-09-03 01:29:35 +00002320 case tok::kw___pascal:
Eli Friedman290eeb02009-06-08 23:27:34 +00002321 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00002322 }
2323}
2324
2325/// isDeclarationSpecifier() - Return true if the current token is part of a
2326/// declaration specifier.
Douglas Gregor9497a732010-09-16 01:51:54 +00002327///
2328/// \param DisambiguatingWithExpression True to indicate that the purpose of
2329/// this check is to disambiguate between an expression and a declaration.
2330bool Parser::isDeclarationSpecifier(bool DisambiguatingWithExpression) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002331 switch (Tok.getKind()) {
2332 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002333
Chris Lattner166a8fc2009-01-04 23:41:41 +00002334 case tok::identifier: // foo::bar
Steve Naroff61f72cb2009-03-09 21:12:44 +00002335 // Unfortunate hack to support "Class.factoryMethod" notation.
2336 if (getLang().ObjC1 && NextToken().is(tok::period))
2337 return false;
John Thompson82287d12010-02-05 00:12:22 +00002338 if (TryAltiVecVectorToken())
2339 return true;
2340 // Fall through.
Douglas Gregord57959a2009-03-27 23:10:48 +00002341 case tok::kw_typename: // typename T::type
Chris Lattner166a8fc2009-01-04 23:41:41 +00002342 // Annotate typenames and C++ scope specifiers. If we get one, just
2343 // recurse to handle whatever we get.
2344 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00002345 return true;
2346 if (Tok.is(tok::identifier))
2347 return false;
Douglas Gregor9497a732010-09-16 01:51:54 +00002348
2349 // If we're in Objective-C and we have an Objective-C class type followed
2350 // by an identifier and then either ':' or ']', in a place where an
2351 // expression is permitted, then this is probably a class message send
2352 // missing the initial '['. In this case, we won't consider this to be
2353 // the start of a declaration.
2354 if (DisambiguatingWithExpression &&
2355 isStartOfObjCClassMessageMissingOpenBracket())
2356 return false;
2357
John McCall9ba61662010-02-26 08:45:28 +00002358 return isDeclarationSpecifier();
2359
Chris Lattner166a8fc2009-01-04 23:41:41 +00002360 case tok::coloncolon: // ::foo::bar
2361 if (NextToken().is(tok::kw_new) || // ::new
2362 NextToken().is(tok::kw_delete)) // ::delete
2363 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002364
Chris Lattner166a8fc2009-01-04 23:41:41 +00002365 // Annotate typenames and C++ scope specifiers. If we get one, just
2366 // recurse to handle whatever we get.
2367 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00002368 return true;
2369 return isDeclarationSpecifier();
Mike Stump1eb44332009-09-09 15:08:12 +00002370
Reid Spencer5f016e22007-07-11 17:01:13 +00002371 // storage-class-specifier
2372 case tok::kw_typedef:
2373 case tok::kw_extern:
Steve Naroff8d54bf22007-12-18 00:16:02 +00002374 case tok::kw___private_extern__:
Reid Spencer5f016e22007-07-11 17:01:13 +00002375 case tok::kw_static:
2376 case tok::kw_auto:
2377 case tok::kw_register:
2378 case tok::kw___thread:
Mike Stump1eb44332009-09-09 15:08:12 +00002379
Reid Spencer5f016e22007-07-11 17:01:13 +00002380 // type-specifiers
2381 case tok::kw_short:
2382 case tok::kw_long:
2383 case tok::kw_signed:
2384 case tok::kw_unsigned:
2385 case tok::kw__Complex:
2386 case tok::kw__Imaginary:
2387 case tok::kw_void:
2388 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00002389 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002390 case tok::kw_char16_t:
2391 case tok::kw_char32_t:
2392
Reid Spencer5f016e22007-07-11 17:01:13 +00002393 case tok::kw_int:
2394 case tok::kw_float:
2395 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00002396 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00002397 case tok::kw__Bool:
2398 case tok::kw__Decimal32:
2399 case tok::kw__Decimal64:
2400 case tok::kw__Decimal128:
John Thompson82287d12010-02-05 00:12:22 +00002401 case tok::kw___vector:
Mike Stump1eb44332009-09-09 15:08:12 +00002402
Chris Lattner99dc9142008-04-13 18:59:07 +00002403 // struct-or-union-specifier (C99) or class-specifier (C++)
2404 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00002405 case tok::kw_struct:
2406 case tok::kw_union:
2407 // enum-specifier
2408 case tok::kw_enum:
Mike Stump1eb44332009-09-09 15:08:12 +00002409
Reid Spencer5f016e22007-07-11 17:01:13 +00002410 // type-qualifier
2411 case tok::kw_const:
2412 case tok::kw_volatile:
2413 case tok::kw_restrict:
Steve Naroffd1861fd2007-07-31 12:34:36 +00002414
Reid Spencer5f016e22007-07-11 17:01:13 +00002415 // function-specifier
2416 case tok::kw_inline:
Douglas Gregorb48fe382008-10-31 09:07:45 +00002417 case tok::kw_virtual:
2418 case tok::kw_explicit:
Chris Lattnerd6c7c182007-08-09 16:40:21 +00002419
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002420 // typedef-name
Chris Lattnerb31757b2009-01-06 05:06:21 +00002421 case tok::annot_typename:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002422
Chris Lattner1ef08762007-08-09 17:01:07 +00002423 // GNU typeof support.
2424 case tok::kw_typeof:
Mike Stump1eb44332009-09-09 15:08:12 +00002425
Chris Lattner1ef08762007-08-09 17:01:07 +00002426 // GNU attributes.
Chris Lattnerd6c7c182007-08-09 16:40:21 +00002427 case tok::kw___attribute:
Reid Spencer5f016e22007-07-11 17:01:13 +00002428 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00002429
Chris Lattnerf3948c42008-07-26 03:38:44 +00002430 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
2431 case tok::less:
2432 return getLang().ObjC1;
Mike Stump1eb44332009-09-09 15:08:12 +00002433
Steve Naroff47f52092009-01-06 19:34:12 +00002434 case tok::kw___declspec:
Steve Naroff239f0732008-12-25 14:16:32 +00002435 case tok::kw___cdecl:
2436 case tok::kw___stdcall:
2437 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00002438 case tok::kw___thiscall:
Eli Friedman290eeb02009-06-08 23:27:34 +00002439 case tok::kw___w64:
2440 case tok::kw___ptr64:
2441 case tok::kw___forceinline:
Dawn Perchik52fc3142010-09-03 01:29:35 +00002442 case tok::kw___pascal:
Eli Friedman290eeb02009-06-08 23:27:34 +00002443 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00002444 }
2445}
2446
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002447bool Parser::isConstructorDeclarator() {
2448 TentativeParsingAction TPA(*this);
2449
2450 // Parse the C++ scope specifier.
2451 CXXScopeSpec SS;
John McCallb3d87482010-08-24 05:47:05 +00002452 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), true)) {
John McCall9ba61662010-02-26 08:45:28 +00002453 TPA.Revert();
2454 return false;
2455 }
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002456
2457 // Parse the constructor name.
2458 if (Tok.is(tok::identifier) || Tok.is(tok::annot_template_id)) {
2459 // We already know that we have a constructor name; just consume
2460 // the token.
2461 ConsumeToken();
2462 } else {
2463 TPA.Revert();
2464 return false;
2465 }
2466
2467 // Current class name must be followed by a left parentheses.
2468 if (Tok.isNot(tok::l_paren)) {
2469 TPA.Revert();
2470 return false;
2471 }
2472 ConsumeParen();
2473
2474 // A right parentheses or ellipsis signals that we have a constructor.
2475 if (Tok.is(tok::r_paren) || Tok.is(tok::ellipsis)) {
2476 TPA.Revert();
2477 return true;
2478 }
2479
2480 // If we need to, enter the specified scope.
2481 DeclaratorScopeObj DeclScopeObj(*this, SS);
Douglas Gregor23c94db2010-07-02 17:43:08 +00002482 if (SS.isSet() && Actions.ShouldEnterDeclaratorScope(getCurScope(), SS))
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002483 DeclScopeObj.EnterDeclaratorScope();
2484
2485 // Check whether the next token(s) are part of a declaration
2486 // specifier, in which case we have the start of a parameter and,
2487 // therefore, we know that this is a constructor.
2488 bool IsConstructor = isDeclarationSpecifier();
2489 TPA.Revert();
2490 return IsConstructor;
2491}
Reid Spencer5f016e22007-07-11 17:01:13 +00002492
2493/// ParseTypeQualifierListOpt
Dawn Perchik52fc3142010-09-03 01:29:35 +00002494/// type-qualifier-list: [C99 6.7.5]
2495/// type-qualifier
2496/// [vendor] attributes
2497/// [ only if VendorAttributesAllowed=true ]
2498/// type-qualifier-list type-qualifier
2499/// [vendor] type-qualifier-list attributes
2500/// [ only if VendorAttributesAllowed=true ]
2501/// [C++0x] attribute-specifier[opt] is allowed before cv-qualifier-seq
2502/// [ only if CXX0XAttributesAllowed=true ]
2503/// Note: vendor can be GNU, MS, etc.
Reid Spencer5f016e22007-07-11 17:01:13 +00002504///
Dawn Perchik52fc3142010-09-03 01:29:35 +00002505void Parser::ParseTypeQualifierListOpt(DeclSpec &DS,
2506 bool VendorAttributesAllowed,
Sean Huntbbd37c62009-11-21 08:43:09 +00002507 bool CXX0XAttributesAllowed) {
2508 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier()) {
2509 SourceLocation Loc = Tok.getLocation();
2510 CXX0XAttributeList Attr = ParseCXX0XAttributes();
2511 if (CXX0XAttributesAllowed)
2512 DS.AddAttributes(Attr.AttrList);
2513 else
2514 Diag(Loc, diag::err_attributes_not_allowed);
2515 }
2516
Reid Spencer5f016e22007-07-11 17:01:13 +00002517 while (1) {
John McCallfec54012009-08-03 20:12:06 +00002518 bool isInvalid = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00002519 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00002520 unsigned DiagID = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00002521 SourceLocation Loc = Tok.getLocation();
2522
2523 switch (Tok.getKind()) {
Douglas Gregor1a480c42010-08-27 17:35:51 +00002524 case tok::code_completion:
2525 Actions.CodeCompleteTypeQualifiers(DS);
2526 ConsumeCodeCompletionToken();
2527 break;
2528
Reid Spencer5f016e22007-07-11 17:01:13 +00002529 case tok::kw_const:
John McCallfec54012009-08-03 20:12:06 +00002530 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, DiagID,
2531 getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00002532 break;
2533 case tok::kw_volatile:
John McCallfec54012009-08-03 20:12:06 +00002534 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
2535 getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00002536 break;
2537 case tok::kw_restrict:
John McCallfec54012009-08-03 20:12:06 +00002538 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
2539 getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00002540 break;
Eli Friedman290eeb02009-06-08 23:27:34 +00002541 case tok::kw___w64:
Steve Naroff86bc6cf2008-12-25 14:41:26 +00002542 case tok::kw___ptr64:
Steve Naroff239f0732008-12-25 14:16:32 +00002543 case tok::kw___cdecl:
2544 case tok::kw___stdcall:
2545 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00002546 case tok::kw___thiscall:
Dawn Perchik52fc3142010-09-03 01:29:35 +00002547 if (VendorAttributesAllowed) {
Eli Friedman290eeb02009-06-08 23:27:34 +00002548 DS.AddAttributes(ParseMicrosoftTypeAttributes());
2549 continue;
2550 }
2551 goto DoneWithTypeQuals;
Dawn Perchik52fc3142010-09-03 01:29:35 +00002552 case tok::kw___pascal:
2553 if (VendorAttributesAllowed) {
2554 DS.AddAttributes(ParseBorlandTypeAttributes());
2555 continue;
2556 }
2557 goto DoneWithTypeQuals;
Reid Spencer5f016e22007-07-11 17:01:13 +00002558 case tok::kw___attribute:
Dawn Perchik52fc3142010-09-03 01:29:35 +00002559 if (VendorAttributesAllowed) {
Sean Huntbbd37c62009-11-21 08:43:09 +00002560 DS.AddAttributes(ParseGNUAttributes());
Chris Lattner5a69d1c2008-12-18 07:02:59 +00002561 continue; // do *not* consume the next token!
2562 }
2563 // otherwise, FALL THROUGH!
2564 default:
Steve Naroff239f0732008-12-25 14:16:32 +00002565 DoneWithTypeQuals:
Chris Lattner5a69d1c2008-12-18 07:02:59 +00002566 // If this is not a type-qualifier token, we're done reading type
2567 // qualifiers. First verify that DeclSpec's are consistent.
Douglas Gregor9b3064b2009-04-01 22:41:11 +00002568 DS.Finish(Diags, PP);
Chris Lattner5a69d1c2008-12-18 07:02:59 +00002569 return;
Reid Spencer5f016e22007-07-11 17:01:13 +00002570 }
Chris Lattnera1fcbad2008-12-18 06:50:14 +00002571
Reid Spencer5f016e22007-07-11 17:01:13 +00002572 // If the specifier combination wasn't legal, issue a diagnostic.
2573 if (isInvalid) {
2574 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner1ab3b962008-11-18 07:48:38 +00002575 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00002576 }
2577 ConsumeToken();
2578 }
2579}
2580
2581
2582/// ParseDeclarator - Parse and verify a newly-initialized declarator.
2583///
2584void Parser::ParseDeclarator(Declarator &D) {
2585 /// This implements the 'declarator' production in the C grammar, then checks
2586 /// for well-formedness and issues diagnostics.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002587 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Reid Spencer5f016e22007-07-11 17:01:13 +00002588}
2589
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002590/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
2591/// is parsed by the function passed to it. Pass null, and the direct-declarator
2592/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregor2f1bc522008-11-07 20:08:42 +00002593/// ptr-operator production.
2594///
Sebastian Redlf30208a2009-01-24 21:16:55 +00002595/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
2596/// [C] pointer[opt] direct-declarator
2597/// [C++] direct-declarator
2598/// [C++] ptr-operator declarator
Reid Spencer5f016e22007-07-11 17:01:13 +00002599///
2600/// pointer: [C99 6.7.5]
2601/// '*' type-qualifier-list[opt]
2602/// '*' type-qualifier-list[opt] pointer
2603///
Douglas Gregor2f1bc522008-11-07 20:08:42 +00002604/// ptr-operator:
2605/// '*' cv-qualifier-seq[opt]
2606/// '&'
Sebastian Redl05532f22009-03-15 22:02:01 +00002607/// [C++0x] '&&'
Douglas Gregor2f1bc522008-11-07 20:08:42 +00002608/// [GNU] '&' restrict[opt] attributes[opt]
Sebastian Redl05532f22009-03-15 22:02:01 +00002609/// [GNU?] '&&' restrict[opt] attributes[opt]
Sebastian Redlf30208a2009-01-24 21:16:55 +00002610/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002611void Parser::ParseDeclaratorInternal(Declarator &D,
2612 DirectDeclParseFunction DirectDeclParser) {
Douglas Gregor91a28862009-08-26 14:27:30 +00002613 if (Diags.hasAllExtensionsSilenced())
2614 D.setExtension();
Douglas Gregor2ccccb32010-08-23 18:23:48 +00002615
Sebastian Redlf30208a2009-01-24 21:16:55 +00002616 // C++ member pointers start with a '::' or a nested-name.
2617 // Member pointers get special handling, since there's no place for the
2618 // scope spec in the generic path below.
Chris Lattnerf919bfe2009-03-24 17:04:48 +00002619 if (getLang().CPlusPlus &&
2620 (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) ||
2621 Tok.is(tok::annot_cxxscope))) {
Sebastian Redlf30208a2009-01-24 21:16:55 +00002622 CXXScopeSpec SS;
John McCallb3d87482010-08-24 05:47:05 +00002623 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), true); // ignore fail
John McCall9ba61662010-02-26 08:45:28 +00002624
Jeffrey Yasskinedc28772010-04-07 23:29:58 +00002625 if (SS.isNotEmpty()) {
Mike Stump1eb44332009-09-09 15:08:12 +00002626 if (Tok.isNot(tok::star)) {
Sebastian Redlf30208a2009-01-24 21:16:55 +00002627 // The scope spec really belongs to the direct-declarator.
2628 D.getCXXScopeSpec() = SS;
2629 if (DirectDeclParser)
2630 (this->*DirectDeclParser)(D);
2631 return;
2632 }
2633
2634 SourceLocation Loc = ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00002635 D.SetRangeEnd(Loc);
Sebastian Redlf30208a2009-01-24 21:16:55 +00002636 DeclSpec DS;
2637 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002638 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00002639
2640 // Recurse to parse whatever is left.
2641 ParseDeclaratorInternal(D, DirectDeclParser);
2642
2643 // Sema will have to catch (syntactically invalid) pointers into global
2644 // scope. It has to catch pointers into namespace scope anyway.
2645 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00002646 Loc, DS.TakeAttributes()),
2647 /* Don't replace range end. */SourceLocation());
Sebastian Redlf30208a2009-01-24 21:16:55 +00002648 return;
2649 }
2650 }
2651
2652 tok::TokenKind Kind = Tok.getKind();
Steve Naroff5618bd42008-08-27 16:04:49 +00002653 // Not a pointer, C++ reference, or block.
Chris Lattner9af55002009-03-27 04:18:06 +00002654 if (Kind != tok::star && Kind != tok::caret &&
Chris Lattnerf919bfe2009-03-24 17:04:48 +00002655 (Kind != tok::amp || !getLang().CPlusPlus) &&
Sebastian Redl743de1f2009-03-23 00:00:23 +00002656 // We parse rvalue refs in C++03, because otherwise the errors are scary.
Chris Lattner9af55002009-03-27 04:18:06 +00002657 (Kind != tok::ampamp || !getLang().CPlusPlus)) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002658 if (DirectDeclParser)
2659 (this->*DirectDeclParser)(D);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00002660 return;
2661 }
Sebastian Redlf30208a2009-01-24 21:16:55 +00002662
Sebastian Redl05532f22009-03-15 22:02:01 +00002663 // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference,
2664 // '&&' -> rvalue reference
Sebastian Redl743de1f2009-03-23 00:00:23 +00002665 SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&.
Sebastian Redlab197ba2009-02-09 18:23:29 +00002666 D.SetRangeEnd(Loc);
Reid Spencer5f016e22007-07-11 17:01:13 +00002667
Chris Lattner9af55002009-03-27 04:18:06 +00002668 if (Kind == tok::star || Kind == tok::caret) {
Chris Lattner76549142008-02-21 01:32:26 +00002669 // Is a pointer.
Reid Spencer5f016e22007-07-11 17:01:13 +00002670 DeclSpec DS;
Sebastian Redlf30208a2009-01-24 21:16:55 +00002671
Reid Spencer5f016e22007-07-11 17:01:13 +00002672 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002673 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00002674
Reid Spencer5f016e22007-07-11 17:01:13 +00002675 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002676 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroff5618bd42008-08-27 16:04:49 +00002677 if (Kind == tok::star)
2678 // Remember that we parsed a pointer type, and remember the type-quals.
2679 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
Sebastian Redlab197ba2009-02-09 18:23:29 +00002680 DS.TakeAttributes()),
2681 SourceLocation());
Steve Naroff5618bd42008-08-27 16:04:49 +00002682 else
2683 // Remember that we parsed a Block type, and remember the type-quals.
Mike Stump1eb44332009-09-09 15:08:12 +00002684 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
Mike Stump75b163f2009-04-21 00:51:43 +00002685 Loc, DS.TakeAttributes()),
Sebastian Redlab197ba2009-02-09 18:23:29 +00002686 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00002687 } else {
2688 // Is a reference
2689 DeclSpec DS;
2690
Sebastian Redl743de1f2009-03-23 00:00:23 +00002691 // Complain about rvalue references in C++03, but then go on and build
2692 // the declarator.
2693 if (Kind == tok::ampamp && !getLang().CPlusPlus0x)
2694 Diag(Loc, diag::err_rvalue_reference);
2695
Reid Spencer5f016e22007-07-11 17:01:13 +00002696 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
2697 // cv-qualifiers are introduced through the use of a typedef or of a
2698 // template type argument, in which case the cv-qualifiers are ignored.
2699 //
2700 // [GNU] Retricted references are allowed.
2701 // [GNU] Attributes on references are allowed.
Sean Huntbbd37c62009-11-21 08:43:09 +00002702 // [C++0x] Attributes on references are not allowed.
2703 ParseTypeQualifierListOpt(DS, true, false);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002704 D.ExtendWithDeclSpec(DS);
Reid Spencer5f016e22007-07-11 17:01:13 +00002705
2706 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
2707 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
2708 Diag(DS.getConstSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00002709 diag::err_invalid_reference_qualifier_application) << "const";
Reid Spencer5f016e22007-07-11 17:01:13 +00002710 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
2711 Diag(DS.getVolatileSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00002712 diag::err_invalid_reference_qualifier_application) << "volatile";
Reid Spencer5f016e22007-07-11 17:01:13 +00002713 }
2714
2715 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002716 ParseDeclaratorInternal(D, DirectDeclParser);
Reid Spencer5f016e22007-07-11 17:01:13 +00002717
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00002718 if (D.getNumTypeObjects() > 0) {
2719 // C++ [dcl.ref]p4: There shall be no references to references.
2720 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
2721 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattnerda83bac2008-11-19 07:37:42 +00002722 if (const IdentifierInfo *II = D.getIdentifier())
2723 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
2724 << II;
2725 else
2726 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
2727 << "type name";
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00002728
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002729 // Once we've complained about the reference-to-reference, we
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00002730 // can go ahead and build the (technically ill-formed)
2731 // declarator: reference collapsing will take care of it.
2732 }
2733 }
2734
Reid Spencer5f016e22007-07-11 17:01:13 +00002735 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner76549142008-02-21 01:32:26 +00002736 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
Sebastian Redl05532f22009-03-15 22:02:01 +00002737 DS.TakeAttributes(),
2738 Kind == tok::amp),
Sebastian Redlab197ba2009-02-09 18:23:29 +00002739 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00002740 }
2741}
2742
2743/// ParseDirectDeclarator
2744/// direct-declarator: [C99 6.7.5]
Douglas Gregor42a552f2008-11-05 20:51:48 +00002745/// [C99] identifier
Reid Spencer5f016e22007-07-11 17:01:13 +00002746/// '(' declarator ')'
2747/// [GNU] '(' attributes declarator ')'
2748/// [C90] direct-declarator '[' constant-expression[opt] ']'
2749/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
2750/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
2751/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
2752/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
2753/// direct-declarator '(' parameter-type-list ')'
2754/// direct-declarator '(' identifier-list[opt] ')'
2755/// [GNU] direct-declarator '(' parameter-forward-declarations
2756/// parameter-type-list[opt] ')'
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002757/// [C++] direct-declarator '(' parameter-declaration-clause ')'
2758/// cv-qualifier-seq[opt] exception-specification[opt]
Douglas Gregorb48fe382008-10-31 09:07:45 +00002759/// [C++] declarator-id
Douglas Gregor42a552f2008-11-05 20:51:48 +00002760///
2761/// declarator-id: [C++ 8]
2762/// id-expression
2763/// '::'[opt] nested-name-specifier[opt] type-name
2764///
2765/// id-expression: [C++ 5.1]
2766/// unqualified-id
Douglas Gregordb422df2009-09-25 21:45:23 +00002767/// qualified-id
Douglas Gregor42a552f2008-11-05 20:51:48 +00002768///
2769/// unqualified-id: [C++ 5.1]
Mike Stump1eb44332009-09-09 15:08:12 +00002770/// identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002771/// operator-function-id
Douglas Gregordb422df2009-09-25 21:45:23 +00002772/// conversion-function-id
Mike Stump1eb44332009-09-09 15:08:12 +00002773/// '~' class-name
Douglas Gregor39a8de12009-02-25 19:37:18 +00002774/// template-id
Argyrios Kyrtzidisc7ed9c62008-11-07 22:02:30 +00002775///
Reid Spencer5f016e22007-07-11 17:01:13 +00002776void Parser::ParseDirectDeclarator(Declarator &D) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002777 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002778
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002779 if (getLang().CPlusPlus && D.mayHaveIdentifier()) {
2780 // ParseDeclaratorInternal might already have parsed the scope.
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00002781 if (D.getCXXScopeSpec().isEmpty()) {
John McCallb3d87482010-08-24 05:47:05 +00002782 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec(), ParsedType(), true);
John McCall9ba61662010-02-26 08:45:28 +00002783 }
2784
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00002785 if (D.getCXXScopeSpec().isValid()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00002786 if (Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
John McCalle7e278b2009-12-11 20:04:54 +00002787 // Change the declaration context for name lookup, until this function
2788 // is exited (and the declarator has been parsed).
2789 DeclScopeObj.EnterDeclaratorScope();
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00002790 }
2791
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002792 if (Tok.is(tok::identifier) || Tok.is(tok::kw_operator) ||
2793 Tok.is(tok::annot_template_id) || Tok.is(tok::tilde)) {
2794 // We found something that indicates the start of an unqualified-id.
2795 // Parse that unqualified-id.
John McCallba9d8532010-04-13 06:39:49 +00002796 bool AllowConstructorName;
2797 if (D.getDeclSpec().hasTypeSpecifier())
2798 AllowConstructorName = false;
2799 else if (D.getCXXScopeSpec().isSet())
2800 AllowConstructorName =
2801 (D.getContext() == Declarator::FileContext ||
2802 (D.getContext() == Declarator::MemberContext &&
2803 D.getDeclSpec().isFriendSpecified()));
2804 else
2805 AllowConstructorName = (D.getContext() == Declarator::MemberContext);
2806
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002807 if (ParseUnqualifiedId(D.getCXXScopeSpec(),
2808 /*EnteringContext=*/true,
2809 /*AllowDestructorName=*/true,
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002810 AllowConstructorName,
John McCallb3d87482010-08-24 05:47:05 +00002811 ParsedType(),
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00002812 D.getName()) ||
2813 // Once we're past the identifier, if the scope was bad, mark the
2814 // whole declarator bad.
2815 D.getCXXScopeSpec().isInvalid()) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002816 D.SetIdentifier(0, Tok.getLocation());
2817 D.setInvalidType(true);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002818 } else {
2819 // Parsed the unqualified-id; update range information and move along.
2820 if (D.getSourceRange().getBegin().isInvalid())
2821 D.SetRangeBegin(D.getName().getSourceRange().getBegin());
2822 D.SetRangeEnd(D.getName().getSourceRange().getEnd());
Douglas Gregor2f1bc522008-11-07 20:08:42 +00002823 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002824 goto PastIdentifier;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00002825 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002826 } else if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002827 assert(!getLang().CPlusPlus &&
2828 "There's a C++-specific check for tok::identifier above");
2829 assert(Tok.getIdentifierInfo() && "Not an identifier?");
2830 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
2831 ConsumeToken();
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002832 goto PastIdentifier;
2833 }
2834
2835 if (Tok.is(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002836 // direct-declarator: '(' declarator ')'
2837 // direct-declarator: '(' attributes declarator ')'
2838 // Example: 'char (*X)' or 'int (*XX)(void)'
2839 ParseParenDeclarator(D);
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002840
2841 // If the declarator was parenthesized, we entered the declarator
2842 // scope when parsing the parenthesized declarator, then exited
2843 // the scope already. Re-enter the scope, if we need to.
2844 if (D.getCXXScopeSpec().isSet()) {
Fariborz Jahanian46877cd2010-08-17 23:50:37 +00002845 // If there was an error parsing parenthesized declarator, declarator
2846 // scope may have been enterred before. Don't do it again.
2847 if (!D.isInvalidType() &&
2848 Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002849 // Change the declaration context for name lookup, until this function
2850 // is exited (and the declarator has been parsed).
Fariborz Jahanian46877cd2010-08-17 23:50:37 +00002851 DeclScopeObj.EnterDeclaratorScope();
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002852 }
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002853 } else if (D.mayOmitIdentifier()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002854 // This could be something simple like "int" (in which case the declarator
2855 // portion is empty), if an abstract-declarator is allowed.
2856 D.SetIdentifier(0, Tok.getLocation());
2857 } else {
Douglas Gregore950d4b2009-03-06 23:28:18 +00002858 if (D.getContext() == Declarator::MemberContext)
2859 Diag(Tok, diag::err_expected_member_name_or_semi)
2860 << D.getDeclSpec().getSourceRange();
2861 else if (getLang().CPlusPlus)
Douglas Gregor2d1c2142009-11-03 19:44:04 +00002862 Diag(Tok, diag::err_expected_unqualified_id) << getLang().CPlusPlus;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002863 else
Chris Lattner1ab3b962008-11-18 07:48:38 +00002864 Diag(Tok, diag::err_expected_ident_lparen);
Reid Spencer5f016e22007-07-11 17:01:13 +00002865 D.SetIdentifier(0, Tok.getLocation());
Chris Lattner1f6f54b2008-11-11 06:13:16 +00002866 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00002867 }
Mike Stump1eb44332009-09-09 15:08:12 +00002868
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002869 PastIdentifier:
Reid Spencer5f016e22007-07-11 17:01:13 +00002870 assert(D.isPastIdentifier() &&
2871 "Haven't past the location of the identifier yet?");
Mike Stump1eb44332009-09-09 15:08:12 +00002872
Sean Huntbbd37c62009-11-21 08:43:09 +00002873 // Don't parse attributes unless we have an identifier.
Douglas Gregor3c3aaf92010-02-19 16:47:56 +00002874 if (D.getIdentifier() && getLang().CPlusPlus0x
Sean Huntbbd37c62009-11-21 08:43:09 +00002875 && isCXX0XAttributeSpecifier(true)) {
2876 SourceLocation AttrEndLoc;
2877 CXX0XAttributeList Attr = ParseCXX0XAttributes();
2878 D.AddAttributes(Attr.AttrList, AttrEndLoc);
2879 }
2880
Reid Spencer5f016e22007-07-11 17:01:13 +00002881 while (1) {
Chris Lattner04d66662007-10-09 17:33:22 +00002882 if (Tok.is(tok::l_paren)) {
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00002883 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
2884 // In such a case, check if we actually have a function declarator; if it
2885 // is not, the declarator has been fully parsed.
Chris Lattner7399ee02008-10-20 02:05:46 +00002886 if (getLang().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
2887 // When not in file scope, warn for ambiguous function declarators, just
2888 // in case the author intended it as a variable definition.
2889 bool warnIfAmbiguous = D.getContext() != Declarator::FileContext;
2890 if (!isCXXFunctionDeclarator(warnIfAmbiguous))
2891 break;
2892 }
Chris Lattneref4715c2008-04-06 05:45:57 +00002893 ParseFunctionDeclarator(ConsumeParen(), D);
Chris Lattner04d66662007-10-09 17:33:22 +00002894 } else if (Tok.is(tok::l_square)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002895 ParseBracketDeclarator(D);
2896 } else {
2897 break;
2898 }
2899 }
2900}
2901
Chris Lattneref4715c2008-04-06 05:45:57 +00002902/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
2903/// only called before the identifier, so these are most likely just grouping
Mike Stump1eb44332009-09-09 15:08:12 +00002904/// parens for precedence. If we find that these are actually function
Chris Lattneref4715c2008-04-06 05:45:57 +00002905/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
2906///
2907/// direct-declarator:
2908/// '(' declarator ')'
2909/// [GNU] '(' attributes declarator ')'
Chris Lattner7399ee02008-10-20 02:05:46 +00002910/// direct-declarator '(' parameter-type-list ')'
2911/// direct-declarator '(' identifier-list[opt] ')'
2912/// [GNU] direct-declarator '(' parameter-forward-declarations
2913/// parameter-type-list[opt] ')'
Chris Lattneref4715c2008-04-06 05:45:57 +00002914///
2915void Parser::ParseParenDeclarator(Declarator &D) {
2916 SourceLocation StartLoc = ConsumeParen();
2917 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
Mike Stump1eb44332009-09-09 15:08:12 +00002918
Chris Lattner7399ee02008-10-20 02:05:46 +00002919 // Eat any attributes before we look at whether this is a grouping or function
2920 // declarator paren. If this is a grouping paren, the attribute applies to
2921 // the type being built up, for example:
2922 // int (__attribute__(()) *x)(long y)
2923 // If this ends up not being a grouping paren, the attribute applies to the
2924 // first argument, for example:
2925 // int (__attribute__(()) int x)
2926 // In either case, we need to eat any attributes to be able to determine what
2927 // sort of paren this is.
2928 //
Ted Kremenek1e377652010-02-11 02:19:13 +00002929 llvm::OwningPtr<AttributeList> AttrList;
Chris Lattner7399ee02008-10-20 02:05:46 +00002930 bool RequiresArg = false;
2931 if (Tok.is(tok::kw___attribute)) {
Ted Kremenek1e377652010-02-11 02:19:13 +00002932 AttrList.reset(ParseGNUAttributes());
Mike Stump1eb44332009-09-09 15:08:12 +00002933
Chris Lattner7399ee02008-10-20 02:05:46 +00002934 // We require that the argument list (if this is a non-grouping paren) be
2935 // present even if the attribute list was empty.
2936 RequiresArg = true;
2937 }
Steve Naroff239f0732008-12-25 14:16:32 +00002938 // Eat any Microsoft extensions.
Eli Friedman290eeb02009-06-08 23:27:34 +00002939 if (Tok.is(tok::kw___cdecl) || Tok.is(tok::kw___stdcall) ||
Douglas Gregorf813a2c2010-05-18 16:57:00 +00002940 Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___fastcall) ||
2941 Tok.is(tok::kw___w64) || Tok.is(tok::kw___ptr64)) {
Ted Kremenek1e377652010-02-11 02:19:13 +00002942 AttrList.reset(ParseMicrosoftTypeAttributes(AttrList.take()));
Eli Friedman290eeb02009-06-08 23:27:34 +00002943 }
Dawn Perchik52fc3142010-09-03 01:29:35 +00002944 // Eat any Borland extensions.
2945 if (Tok.is(tok::kw___pascal)) {
2946 AttrList.reset(ParseBorlandTypeAttributes(AttrList.take()));
2947 }
Mike Stump1eb44332009-09-09 15:08:12 +00002948
Chris Lattneref4715c2008-04-06 05:45:57 +00002949 // If we haven't past the identifier yet (or where the identifier would be
2950 // stored, if this is an abstract declarator), then this is probably just
2951 // grouping parens. However, if this could be an abstract-declarator, then
2952 // this could also be the start of function arguments (consider 'void()').
2953 bool isGrouping;
Mike Stump1eb44332009-09-09 15:08:12 +00002954
Chris Lattneref4715c2008-04-06 05:45:57 +00002955 if (!D.mayOmitIdentifier()) {
2956 // If this can't be an abstract-declarator, this *must* be a grouping
2957 // paren, because we haven't seen the identifier yet.
2958 isGrouping = true;
2959 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Argyrios Kyrtzidise25d2702008-10-06 00:07:55 +00002960 (getLang().CPlusPlus && Tok.is(tok::ellipsis)) || // C++ int(...)
Chris Lattneref4715c2008-04-06 05:45:57 +00002961 isDeclarationSpecifier()) { // 'int(int)' is a function.
2962 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
2963 // considered to be a type, not a K&R identifier-list.
2964 isGrouping = false;
2965 } else {
2966 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
2967 isGrouping = true;
2968 }
Mike Stump1eb44332009-09-09 15:08:12 +00002969
Chris Lattneref4715c2008-04-06 05:45:57 +00002970 // If this is a grouping paren, handle:
2971 // direct-declarator: '(' declarator ')'
2972 // direct-declarator: '(' attributes declarator ')'
2973 if (isGrouping) {
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00002974 bool hadGroupingParens = D.hasGroupingParens();
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00002975 D.setGroupingParens(true);
Chris Lattner7399ee02008-10-20 02:05:46 +00002976 if (AttrList)
Ted Kremenek1e377652010-02-11 02:19:13 +00002977 D.AddAttributes(AttrList.take(), SourceLocation());
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00002978
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002979 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattneref4715c2008-04-06 05:45:57 +00002980 // Match the ')'.
Sebastian Redlab197ba2009-02-09 18:23:29 +00002981 SourceLocation Loc = MatchRHSPunctuation(tok::r_paren, StartLoc);
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00002982
2983 D.setGroupingParens(hadGroupingParens);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002984 D.SetRangeEnd(Loc);
Chris Lattneref4715c2008-04-06 05:45:57 +00002985 return;
2986 }
Mike Stump1eb44332009-09-09 15:08:12 +00002987
Chris Lattneref4715c2008-04-06 05:45:57 +00002988 // Okay, if this wasn't a grouping paren, it must be the start of a function
2989 // argument list. Recognize that this declarator will never have an
Chris Lattner7399ee02008-10-20 02:05:46 +00002990 // identifier (and remember where it would have been), then call into
2991 // ParseFunctionDeclarator to handle of argument list.
Chris Lattneref4715c2008-04-06 05:45:57 +00002992 D.SetIdentifier(0, Tok.getLocation());
2993
Ted Kremenek1e377652010-02-11 02:19:13 +00002994 ParseFunctionDeclarator(StartLoc, D, AttrList.take(), RequiresArg);
Chris Lattneref4715c2008-04-06 05:45:57 +00002995}
2996
2997/// ParseFunctionDeclarator - We are after the identifier and have parsed the
2998/// declarator D up to a paren, which indicates that we are parsing function
2999/// arguments.
Reid Spencer5f016e22007-07-11 17:01:13 +00003000///
Chris Lattner7399ee02008-10-20 02:05:46 +00003001/// If AttrList is non-null, then the caller parsed those arguments immediately
3002/// after the open paren - they should be considered to be the first argument of
3003/// a parameter. If RequiresArg is true, then the first argument of the
3004/// function is required to be present and required to not be an identifier
3005/// list.
3006///
Reid Spencer5f016e22007-07-11 17:01:13 +00003007/// This method also handles this portion of the grammar:
3008/// parameter-type-list: [C99 6.7.5]
3009/// parameter-list
3010/// parameter-list ',' '...'
Douglas Gregored5d6512009-09-22 21:41:40 +00003011/// [C++] parameter-list '...'
Reid Spencer5f016e22007-07-11 17:01:13 +00003012///
3013/// parameter-list: [C99 6.7.5]
3014/// parameter-declaration
3015/// parameter-list ',' parameter-declaration
3016///
3017/// parameter-declaration: [C99 6.7.5]
3018/// declaration-specifiers declarator
Chris Lattner04421082008-04-08 04:40:51 +00003019/// [C++] declaration-specifiers declarator '=' assignment-expression
Reid Spencer5f016e22007-07-11 17:01:13 +00003020/// [GNU] declaration-specifiers declarator attributes
Sebastian Redl50de12f2009-03-24 22:27:57 +00003021/// declaration-specifiers abstract-declarator[opt]
3022/// [C++] declaration-specifiers abstract-declarator[opt]
Chris Lattner8123a952008-04-10 02:22:51 +00003023/// '=' assignment-expression
Reid Spencer5f016e22007-07-11 17:01:13 +00003024/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
3025///
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003026/// For C++, after the parameter-list, it also parses "cv-qualifier-seq[opt]"
Sebastian Redl50de12f2009-03-24 22:27:57 +00003027/// and "exception-specification[opt]".
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003028///
Chris Lattner7399ee02008-10-20 02:05:46 +00003029void Parser::ParseFunctionDeclarator(SourceLocation LParenLoc, Declarator &D,
3030 AttributeList *AttrList,
3031 bool RequiresArg) {
Chris Lattneref4715c2008-04-06 05:45:57 +00003032 // lparen is already consumed!
3033 assert(D.isPastIdentifier() && "Should not call before identifier!");
Mike Stump1eb44332009-09-09 15:08:12 +00003034
Douglas Gregordab60ad2010-10-01 18:44:50 +00003035 ParsedType TrailingReturnType;
3036
Chris Lattner7399ee02008-10-20 02:05:46 +00003037 // This parameter list may be empty.
Chris Lattner04d66662007-10-09 17:33:22 +00003038 if (Tok.is(tok::r_paren)) {
Chris Lattner7399ee02008-10-20 02:05:46 +00003039 if (RequiresArg) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00003040 Diag(Tok, diag::err_argument_required_after_attribute);
Chris Lattner7399ee02008-10-20 02:05:46 +00003041 delete AttrList;
3042 }
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003043
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00003044 SourceLocation RParenLoc = ConsumeParen(); // Eat the closing ')'.
3045 SourceLocation EndLoc = RParenLoc;
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003046
3047 // cv-qualifier-seq[opt].
3048 DeclSpec DS;
Sebastian Redl7dc81342009-04-29 17:30:04 +00003049 bool hasExceptionSpec = false;
Sebastian Redl3cc97262009-05-31 11:47:27 +00003050 SourceLocation ThrowLoc;
Sebastian Redl7dc81342009-04-29 17:30:04 +00003051 bool hasAnyExceptionSpec = false;
John McCallb3d87482010-08-24 05:47:05 +00003052 llvm::SmallVector<ParsedType, 2> Exceptions;
Sebastian Redlef65f062009-05-29 18:02:33 +00003053 llvm::SmallVector<SourceRange, 2> ExceptionRanges;
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003054 if (getLang().CPlusPlus) {
Chris Lattner5a69d1c2008-12-18 07:02:59 +00003055 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Sebastian Redlab197ba2009-02-09 18:23:29 +00003056 if (!DS.getSourceRange().getEnd().isInvalid())
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00003057 EndLoc = DS.getSourceRange().getEnd();
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00003058
3059 // Parse exception-specification[opt].
Sebastian Redl7dc81342009-04-29 17:30:04 +00003060 if (Tok.is(tok::kw_throw)) {
3061 hasExceptionSpec = true;
Sebastian Redl3cc97262009-05-31 11:47:27 +00003062 ThrowLoc = Tok.getLocation();
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00003063 ParseExceptionSpecification(EndLoc, Exceptions, ExceptionRanges,
Sebastian Redlef65f062009-05-29 18:02:33 +00003064 hasAnyExceptionSpec);
3065 assert(Exceptions.size() == ExceptionRanges.size() &&
3066 "Produced different number of exception types and ranges.");
Sebastian Redl7dc81342009-04-29 17:30:04 +00003067 }
Douglas Gregordab60ad2010-10-01 18:44:50 +00003068
3069 // Parse trailing-return-type.
3070 if (getLang().CPlusPlus0x && Tok.is(tok::arrow)) {
3071 TrailingReturnType = ParseTrailingReturnType().get();
3072 }
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003073 }
3074
Chris Lattnerf97409f2008-04-06 06:57:35 +00003075 // Remember that we parsed a function type, and remember the attributes.
Reid Spencer5f016e22007-07-11 17:01:13 +00003076 // int() -> no prototype, no '...'.
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003077 D.AddTypeInfo(DeclaratorChunk::getFunction(/*prototype*/getLang().CPlusPlus,
Chris Lattnerf97409f2008-04-06 06:57:35 +00003078 /*variadic*/ false,
Douglas Gregor965acbb2009-02-18 07:07:28 +00003079 SourceLocation(),
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003080 /*arglist*/ 0, 0,
3081 DS.getTypeQualifiers(),
Sebastian Redl3cc97262009-05-31 11:47:27 +00003082 hasExceptionSpec, ThrowLoc,
Sebastian Redl7dc81342009-04-29 17:30:04 +00003083 hasAnyExceptionSpec,
Sebastian Redlef65f062009-05-29 18:02:33 +00003084 Exceptions.data(),
3085 ExceptionRanges.data(),
Sebastian Redl7dc81342009-04-29 17:30:04 +00003086 Exceptions.size(),
Douglas Gregordab60ad2010-10-01 18:44:50 +00003087 LParenLoc, RParenLoc, D,
3088 TrailingReturnType),
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00003089 EndLoc);
Chris Lattnerf97409f2008-04-06 06:57:35 +00003090 return;
Sebastian Redlef65f062009-05-29 18:02:33 +00003091 }
3092
Chris Lattner7399ee02008-10-20 02:05:46 +00003093 // Alternatively, this parameter list may be an identifier list form for a
3094 // K&R-style function: void foo(a,b,c)
John Thompson82287d12010-02-05 00:12:22 +00003095 if (!getLang().CPlusPlus && Tok.is(tok::identifier)
3096 && !TryAltiVecVectorToken()) {
John McCall9ba61662010-02-26 08:45:28 +00003097 if (TryAnnotateTypeOrScopeToken() || !Tok.is(tok::annot_typename)) {
Chris Lattner7399ee02008-10-20 02:05:46 +00003098 // K&R identifier lists can't have typedefs as identifiers, per
3099 // C99 6.7.5.3p11.
Steve Naroff2d081c42009-01-28 19:16:40 +00003100 if (RequiresArg) {
3101 Diag(Tok, diag::err_argument_required_after_attribute);
3102 delete AttrList;
3103 }
Chris Lattner83a94472010-05-14 17:23:36 +00003104
Steve Naroff2d081c42009-01-28 19:16:40 +00003105 // Identifier list. Note that '(' identifier-list ')' is only allowed for
Chris Lattner83a94472010-05-14 17:23:36 +00003106 // normal declarators, not for abstract-declarators. Get the first
3107 // identifier.
Chris Lattner9a65b812010-05-14 17:44:56 +00003108 Token FirstTok = Tok;
Chris Lattner83a94472010-05-14 17:23:36 +00003109 ConsumeToken(); // eat the first identifier.
Chris Lattner9a65b812010-05-14 17:44:56 +00003110
3111 // Identifier lists follow a really simple grammar: the identifiers can
3112 // be followed *only* by a ", moreidentifiers" or ")". However, K&R
3113 // identifier lists are really rare in the brave new modern world, and it
3114 // is very common for someone to typo a type in a non-k&r style list. If
3115 // we are presented with something like: "void foo(intptr x, float y)",
3116 // we don't want to start parsing the function declarator as though it is
3117 // a K&R style declarator just because intptr is an invalid type.
3118 //
3119 // To handle this, we check to see if the token after the first identifier
3120 // is a "," or ")". Only if so, do we parse it as an identifier list.
3121 if (Tok.is(tok::comma) || Tok.is(tok::r_paren))
3122 return ParseFunctionDeclaratorIdentifierList(LParenLoc,
3123 FirstTok.getIdentifierInfo(),
3124 FirstTok.getLocation(), D);
3125
3126 // If we get here, the code is invalid. Push the first identifier back
3127 // into the token stream and parse the first argument as an (invalid)
3128 // normal argument declarator.
3129 PP.EnterToken(Tok);
3130 Tok = FirstTok;
Chris Lattner7399ee02008-10-20 02:05:46 +00003131 }
Chris Lattnerf97409f2008-04-06 06:57:35 +00003132 }
Mike Stump1eb44332009-09-09 15:08:12 +00003133
Chris Lattnerf97409f2008-04-06 06:57:35 +00003134 // Finally, a normal, non-empty parameter type list.
Mike Stump1eb44332009-09-09 15:08:12 +00003135
Chris Lattnerf97409f2008-04-06 06:57:35 +00003136 // Build up an array of information about the parsed arguments.
3137 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Chris Lattner04421082008-04-08 04:40:51 +00003138
3139 // Enter function-declaration scope, limiting any declarators to the
3140 // function prototype scope, including parameter declarators.
Chris Lattnerae50fa02009-03-05 00:00:31 +00003141 ParseScope PrototypeScope(this,
3142 Scope::FunctionPrototypeScope|Scope::DeclScope);
Mike Stump1eb44332009-09-09 15:08:12 +00003143
Chris Lattnerf97409f2008-04-06 06:57:35 +00003144 bool IsVariadic = false;
Douglas Gregor965acbb2009-02-18 07:07:28 +00003145 SourceLocation EllipsisLoc;
Chris Lattnerf97409f2008-04-06 06:57:35 +00003146 while (1) {
3147 if (Tok.is(tok::ellipsis)) {
3148 IsVariadic = true;
Douglas Gregor965acbb2009-02-18 07:07:28 +00003149 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chris Lattnerf97409f2008-04-06 06:57:35 +00003150 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00003151 }
Francois Pichet334d47e2010-10-11 12:59:39 +00003152
3153 // Skip any Microsoft attributes before a param.
3154 if (getLang().Microsoft && Tok.is(tok::l_square))
3155 ParseMicrosoftAttributes();
Mike Stump1eb44332009-09-09 15:08:12 +00003156
Chris Lattnerf97409f2008-04-06 06:57:35 +00003157 SourceLocation DSStart = Tok.getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +00003158
Chris Lattnerf97409f2008-04-06 06:57:35 +00003159 // Parse the declaration-specifiers.
John McCall54abf7d2009-11-04 02:18:39 +00003160 // Just use the ParsingDeclaration "scope" of the declarator.
Chris Lattnerf97409f2008-04-06 06:57:35 +00003161 DeclSpec DS;
Chris Lattner7399ee02008-10-20 02:05:46 +00003162
3163 // If the caller parsed attributes for the first argument, add them now.
3164 if (AttrList) {
3165 DS.AddAttributes(AttrList);
3166 AttrList = 0; // Only apply the attributes to the first parameter.
3167 }
Chris Lattnere64c5492009-02-27 18:38:20 +00003168 ParseDeclarationSpecifiers(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00003169
Chris Lattnerf97409f2008-04-06 06:57:35 +00003170 // Parse the declarator. This is "PrototypeContext", because we must
3171 // accept either 'declarator' or 'abstract-declarator' here.
3172 Declarator ParmDecl(DS, Declarator::PrototypeContext);
3173 ParseDeclarator(ParmDecl);
3174
3175 // Parse GNU attributes, if present.
Sebastian Redlab197ba2009-02-09 18:23:29 +00003176 if (Tok.is(tok::kw___attribute)) {
3177 SourceLocation Loc;
Sean Huntbbd37c62009-11-21 08:43:09 +00003178 AttributeList *AttrList = ParseGNUAttributes(&Loc);
Sebastian Redlab197ba2009-02-09 18:23:29 +00003179 ParmDecl.AddAttributes(AttrList, Loc);
3180 }
Mike Stump1eb44332009-09-09 15:08:12 +00003181
Chris Lattnerf97409f2008-04-06 06:57:35 +00003182 // Remember this parsed parameter in ParamInfo.
3183 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
Mike Stump1eb44332009-09-09 15:08:12 +00003184
Douglas Gregor72b505b2008-12-16 21:30:33 +00003185 // DefArgToks is used when the parsing of default arguments needs
3186 // to be delayed.
3187 CachedTokens *DefArgToks = 0;
3188
Chris Lattnerf97409f2008-04-06 06:57:35 +00003189 // If no parameter was specified, verify that *something* was specified,
3190 // otherwise we have a missing type and identifier.
Chris Lattnere64c5492009-02-27 18:38:20 +00003191 if (DS.isEmpty() && ParmDecl.getIdentifier() == 0 &&
3192 ParmDecl.getNumTypeObjects() == 0) {
Chris Lattnerf97409f2008-04-06 06:57:35 +00003193 // Completely missing, emit error.
3194 Diag(DSStart, diag::err_missing_param);
3195 } else {
3196 // Otherwise, we have something. Add it and let semantic analysis try
3197 // to grok it and add the result to the ParamInfo we are building.
Mike Stump1eb44332009-09-09 15:08:12 +00003198
Chris Lattnerf97409f2008-04-06 06:57:35 +00003199 // Inform the actions module about the parameter declarator, so it gets
3200 // added to the current scope.
John McCalld226f652010-08-21 09:40:31 +00003201 Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl);
Chris Lattner04421082008-04-08 04:40:51 +00003202
3203 // Parse the default argument, if any. We parse the default
3204 // arguments in all dialects; the semantic analysis in
3205 // ActOnParamDefaultArgument will reject the default argument in
3206 // C.
3207 if (Tok.is(tok::equal)) {
Douglas Gregor61366e92008-12-24 00:01:03 +00003208 SourceLocation EqualLoc = Tok.getLocation();
3209
Chris Lattner04421082008-04-08 04:40:51 +00003210 // Parse the default argument
Douglas Gregor72b505b2008-12-16 21:30:33 +00003211 if (D.getContext() == Declarator::MemberContext) {
3212 // If we're inside a class definition, cache the tokens
3213 // corresponding to the default argument. We'll actually parse
3214 // them when we see the end of the class definition.
3215 // FIXME: Templates will require something similar.
3216 // FIXME: Can we use a smart pointer for Toks?
3217 DefArgToks = new CachedTokens;
3218
Mike Stump1eb44332009-09-09 15:08:12 +00003219 if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks,
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +00003220 /*StopAtSemi=*/true,
3221 /*ConsumeFinalToken=*/false)) {
Douglas Gregor72b505b2008-12-16 21:30:33 +00003222 delete DefArgToks;
3223 DefArgToks = 0;
Douglas Gregor61366e92008-12-24 00:01:03 +00003224 Actions.ActOnParamDefaultArgumentError(Param);
Argyrios Kyrtzidis2b602ad2010-08-06 09:47:24 +00003225 } else {
3226 // Mark the end of the default argument so that we know when to
3227 // stop when we parse it later on.
3228 Token DefArgEnd;
3229 DefArgEnd.startToken();
3230 DefArgEnd.setKind(tok::cxx_defaultarg_end);
3231 DefArgEnd.setLocation(Tok.getLocation());
3232 DefArgToks->push_back(DefArgEnd);
Mike Stump1eb44332009-09-09 15:08:12 +00003233 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc,
Anders Carlsson5e300d12009-06-12 16:51:40 +00003234 (*DefArgToks)[1].getLocation());
Argyrios Kyrtzidis2b602ad2010-08-06 09:47:24 +00003235 }
Chris Lattner04421082008-04-08 04:40:51 +00003236 } else {
Douglas Gregor72b505b2008-12-16 21:30:33 +00003237 // Consume the '='.
Douglas Gregor61366e92008-12-24 00:01:03 +00003238 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00003239
Douglas Gregorbe0f7bd2010-09-11 20:24:53 +00003240 // The argument isn't actually potentially evaluated unless it is
3241 // used.
3242 EnterExpressionEvaluationContext Eval(Actions,
3243 Sema::PotentiallyEvaluatedIfUsed);
3244
John McCall60d7b3a2010-08-24 06:29:42 +00003245 ExprResult DefArgResult(ParseAssignmentExpression());
Douglas Gregor72b505b2008-12-16 21:30:33 +00003246 if (DefArgResult.isInvalid()) {
3247 Actions.ActOnParamDefaultArgumentError(Param);
3248 SkipUntil(tok::comma, tok::r_paren, true, true);
3249 } else {
3250 // Inform the actions module about the default argument
3251 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
John McCall9ae2f072010-08-23 23:25:46 +00003252 DefArgResult.take());
Douglas Gregor72b505b2008-12-16 21:30:33 +00003253 }
Chris Lattner04421082008-04-08 04:40:51 +00003254 }
3255 }
Mike Stump1eb44332009-09-09 15:08:12 +00003256
3257 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
3258 ParmDecl.getIdentifierLoc(), Param,
Douglas Gregor72b505b2008-12-16 21:30:33 +00003259 DefArgToks));
Chris Lattnerf97409f2008-04-06 06:57:35 +00003260 }
3261
3262 // If the next token is a comma, consume it and keep reading arguments.
Douglas Gregored5d6512009-09-22 21:41:40 +00003263 if (Tok.isNot(tok::comma)) {
3264 if (Tok.is(tok::ellipsis)) {
3265 IsVariadic = true;
3266 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
3267
3268 if (!getLang().CPlusPlus) {
3269 // We have ellipsis without a preceding ',', which is ill-formed
3270 // in C. Complain and provide the fix.
3271 Diag(EllipsisLoc, diag::err_missing_comma_before_ellipsis)
Douglas Gregor849b2432010-03-31 17:46:05 +00003272 << FixItHint::CreateInsertion(EllipsisLoc, ", ");
Douglas Gregored5d6512009-09-22 21:41:40 +00003273 }
3274 }
3275
3276 break;
3277 }
Mike Stump1eb44332009-09-09 15:08:12 +00003278
Chris Lattnerf97409f2008-04-06 06:57:35 +00003279 // Consume the comma.
3280 ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00003281 }
Mike Stump1eb44332009-09-09 15:08:12 +00003282
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003283 // If we have the closing ')', eat it.
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00003284 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
3285 SourceLocation EndLoc = RParenLoc;
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003286
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003287 DeclSpec DS;
Sebastian Redl7dc81342009-04-29 17:30:04 +00003288 bool hasExceptionSpec = false;
Sebastian Redl3cc97262009-05-31 11:47:27 +00003289 SourceLocation ThrowLoc;
Sebastian Redl7dc81342009-04-29 17:30:04 +00003290 bool hasAnyExceptionSpec = false;
John McCallb3d87482010-08-24 05:47:05 +00003291 llvm::SmallVector<ParsedType, 2> Exceptions;
Sebastian Redlef65f062009-05-29 18:02:33 +00003292 llvm::SmallVector<SourceRange, 2> ExceptionRanges;
Sean Huntbbd37c62009-11-21 08:43:09 +00003293
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003294 if (getLang().CPlusPlus) {
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00003295 // Parse cv-qualifier-seq[opt].
Chris Lattner5a69d1c2008-12-18 07:02:59 +00003296 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Sebastian Redlab197ba2009-02-09 18:23:29 +00003297 if (!DS.getSourceRange().getEnd().isInvalid())
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00003298 EndLoc = DS.getSourceRange().getEnd();
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00003299
3300 // Parse exception-specification[opt].
Sebastian Redl7dc81342009-04-29 17:30:04 +00003301 if (Tok.is(tok::kw_throw)) {
3302 hasExceptionSpec = true;
Sebastian Redl3cc97262009-05-31 11:47:27 +00003303 ThrowLoc = Tok.getLocation();
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00003304 ParseExceptionSpecification(EndLoc, Exceptions, ExceptionRanges,
Sebastian Redlef65f062009-05-29 18:02:33 +00003305 hasAnyExceptionSpec);
3306 assert(Exceptions.size() == ExceptionRanges.size() &&
3307 "Produced different number of exception types and ranges.");
Sebastian Redl7dc81342009-04-29 17:30:04 +00003308 }
Douglas Gregordab60ad2010-10-01 18:44:50 +00003309
3310 // Parse trailing-return-type.
3311 if (getLang().CPlusPlus0x && Tok.is(tok::arrow)) {
3312 TrailingReturnType = ParseTrailingReturnType().get();
3313 }
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003314 }
3315
Douglas Gregordab60ad2010-10-01 18:44:50 +00003316 // FIXME: We should leave the prototype scope before parsing the exception
3317 // specification, and then reenter it when parsing the trailing return type.
3318
3319 // Leave prototype scope.
3320 PrototypeScope.Exit();
3321
Reid Spencer5f016e22007-07-11 17:01:13 +00003322 // Remember that we parsed a function type, and remember the attributes.
Chris Lattnerf97409f2008-04-06 06:57:35 +00003323 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/true, IsVariadic,
Douglas Gregor965acbb2009-02-18 07:07:28 +00003324 EllipsisLoc,
Jay Foadbeaaccd2009-05-21 09:52:38 +00003325 ParamInfo.data(), ParamInfo.size(),
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003326 DS.getTypeQualifiers(),
Sebastian Redl3cc97262009-05-31 11:47:27 +00003327 hasExceptionSpec, ThrowLoc,
Sebastian Redl7dc81342009-04-29 17:30:04 +00003328 hasAnyExceptionSpec,
Sebastian Redlef65f062009-05-29 18:02:33 +00003329 Exceptions.data(),
3330 ExceptionRanges.data(),
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00003331 Exceptions.size(),
Douglas Gregordab60ad2010-10-01 18:44:50 +00003332 LParenLoc, RParenLoc, D,
3333 TrailingReturnType),
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00003334 EndLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00003335}
3336
Chris Lattner66d28652008-04-06 06:34:08 +00003337/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
3338/// we found a K&R-style identifier list instead of a type argument list. The
Chris Lattner83a94472010-05-14 17:23:36 +00003339/// first identifier has already been consumed, and the current token is the
3340/// token right after it.
Chris Lattner66d28652008-04-06 06:34:08 +00003341///
3342/// identifier-list: [C99 6.7.5]
3343/// identifier
3344/// identifier-list ',' identifier
3345///
3346void Parser::ParseFunctionDeclaratorIdentifierList(SourceLocation LParenLoc,
Chris Lattner83a94472010-05-14 17:23:36 +00003347 IdentifierInfo *FirstIdent,
3348 SourceLocation FirstIdentLoc,
Chris Lattner66d28652008-04-06 06:34:08 +00003349 Declarator &D) {
3350 // Build up an array of information about the parsed arguments.
3351 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
3352 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
Mike Stump1eb44332009-09-09 15:08:12 +00003353
Chris Lattner66d28652008-04-06 06:34:08 +00003354 // If there was no identifier specified for the declarator, either we are in
3355 // an abstract-declarator, or we are in a parameter declarator which was found
3356 // to be abstract. In abstract-declarators, identifier lists are not valid:
3357 // diagnose this.
3358 if (!D.getIdentifier())
Chris Lattner83a94472010-05-14 17:23:36 +00003359 Diag(FirstIdentLoc, diag::ext_ident_list_in_param);
Chris Lattner66d28652008-04-06 06:34:08 +00003360
Chris Lattner83a94472010-05-14 17:23:36 +00003361 // The first identifier was already read, and is known to be the first
3362 // identifier in the list. Remember this identifier in ParamInfo.
3363 ParamsSoFar.insert(FirstIdent);
John McCalld226f652010-08-21 09:40:31 +00003364 ParamInfo.push_back(DeclaratorChunk::ParamInfo(FirstIdent, FirstIdentLoc, 0));
Mike Stump1eb44332009-09-09 15:08:12 +00003365
Chris Lattner66d28652008-04-06 06:34:08 +00003366 while (Tok.is(tok::comma)) {
3367 // Eat the comma.
3368 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00003369
Chris Lattner50c64772008-04-06 06:39:19 +00003370 // If this isn't an identifier, report the error and skip until ')'.
Chris Lattner66d28652008-04-06 06:34:08 +00003371 if (Tok.isNot(tok::identifier)) {
3372 Diag(Tok, diag::err_expected_ident);
Chris Lattner50c64772008-04-06 06:39:19 +00003373 SkipUntil(tok::r_paren);
3374 return;
Chris Lattner66d28652008-04-06 06:34:08 +00003375 }
Chris Lattneraaf9ddb2008-04-06 06:47:48 +00003376
Chris Lattner66d28652008-04-06 06:34:08 +00003377 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
Chris Lattneraaf9ddb2008-04-06 06:47:48 +00003378
3379 // Reject 'typedef int y; int test(x, y)', but continue parsing.
Douglas Gregor23c94db2010-07-02 17:43:08 +00003380 if (Actions.getTypeName(*ParmII, Tok.getLocation(), getCurScope()))
Chris Lattnerda83bac2008-11-19 07:37:42 +00003381 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
Mike Stump1eb44332009-09-09 15:08:12 +00003382
Chris Lattner66d28652008-04-06 06:34:08 +00003383 // Verify that the argument identifier has not already been mentioned.
3384 if (!ParamsSoFar.insert(ParmII)) {
Chris Lattnerda83bac2008-11-19 07:37:42 +00003385 Diag(Tok, diag::err_param_redefinition) << ParmII;
Chris Lattner50c64772008-04-06 06:39:19 +00003386 } else {
3387 // Remember this identifier in ParamInfo.
Chris Lattner66d28652008-04-06 06:34:08 +00003388 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Chris Lattnerb28317a2009-03-28 19:18:32 +00003389 Tok.getLocation(),
John McCalld226f652010-08-21 09:40:31 +00003390 0));
Chris Lattner50c64772008-04-06 06:39:19 +00003391 }
Mike Stump1eb44332009-09-09 15:08:12 +00003392
Chris Lattner66d28652008-04-06 06:34:08 +00003393 // Eat the identifier.
3394 ConsumeToken();
3395 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00003396
3397 // If we have the closing ')', eat it and we're done.
3398 SourceLocation RLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
3399
Chris Lattner50c64772008-04-06 06:39:19 +00003400 // Remember that we parsed a function type, and remember the attributes. This
3401 // function type is always a K&R style function type, which is not varargs and
3402 // has no prototype.
3403 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/false, /*varargs*/false,
Douglas Gregor965acbb2009-02-18 07:07:28 +00003404 SourceLocation(),
Chris Lattner50c64772008-04-06 06:39:19 +00003405 &ParamInfo[0], ParamInfo.size(),
Sebastian Redl7dc81342009-04-29 17:30:04 +00003406 /*TypeQuals*/0,
Sebastian Redl3cc97262009-05-31 11:47:27 +00003407 /*exception*/false,
3408 SourceLocation(), false, 0, 0, 0,
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00003409 LParenLoc, RLoc, D),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003410 RLoc);
Chris Lattner66d28652008-04-06 06:34:08 +00003411}
Chris Lattneref4715c2008-04-06 05:45:57 +00003412
Reid Spencer5f016e22007-07-11 17:01:13 +00003413/// [C90] direct-declarator '[' constant-expression[opt] ']'
3414/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
3415/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
3416/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
3417/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
3418void Parser::ParseBracketDeclarator(Declarator &D) {
3419 SourceLocation StartLoc = ConsumeBracket();
Mike Stump1eb44332009-09-09 15:08:12 +00003420
Chris Lattner378c7e42008-12-18 07:27:21 +00003421 // C array syntax has many features, but by-far the most common is [] and [4].
3422 // This code does a fast path to handle some of the most obvious cases.
3423 if (Tok.getKind() == tok::r_square) {
Sebastian Redlab197ba2009-02-09 18:23:29 +00003424 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
Sean Huntbbd37c62009-11-21 08:43:09 +00003425 //FIXME: Use these
3426 CXX0XAttributeList Attr;
3427 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier(true)) {
3428 Attr = ParseCXX0XAttributes();
3429 }
3430
Chris Lattner378c7e42008-12-18 07:27:21 +00003431 // Remember that we parsed the empty array type.
John McCall60d7b3a2010-08-24 06:29:42 +00003432 ExprResult NumElements;
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00003433 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0,
3434 StartLoc, EndLoc),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003435 EndLoc);
Chris Lattner378c7e42008-12-18 07:27:21 +00003436 return;
3437 } else if (Tok.getKind() == tok::numeric_constant &&
3438 GetLookAheadToken(1).is(tok::r_square)) {
3439 // [4] is very common. Parse the numeric constant expression.
John McCall60d7b3a2010-08-24 06:29:42 +00003440 ExprResult ExprRes(Actions.ActOnNumericConstant(Tok));
Chris Lattner378c7e42008-12-18 07:27:21 +00003441 ConsumeToken();
3442
Sebastian Redlab197ba2009-02-09 18:23:29 +00003443 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
Sean Huntbbd37c62009-11-21 08:43:09 +00003444 //FIXME: Use these
3445 CXX0XAttributeList Attr;
3446 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier()) {
3447 Attr = ParseCXX0XAttributes();
3448 }
Chris Lattner378c7e42008-12-18 07:27:21 +00003449
3450 // If there was an error parsing the assignment-expression, recover.
3451 if (ExprRes.isInvalid())
3452 ExprRes.release(); // Deallocate expr, just use [].
Mike Stump1eb44332009-09-09 15:08:12 +00003453
Chris Lattner378c7e42008-12-18 07:27:21 +00003454 // Remember that we parsed a array type, and remember its features.
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00003455 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, 0, ExprRes.release(),
3456 StartLoc, EndLoc),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003457 EndLoc);
Chris Lattner378c7e42008-12-18 07:27:21 +00003458 return;
3459 }
Mike Stump1eb44332009-09-09 15:08:12 +00003460
Reid Spencer5f016e22007-07-11 17:01:13 +00003461 // If valid, this location is the position where we read the 'static' keyword.
3462 SourceLocation StaticLoc;
Chris Lattner04d66662007-10-09 17:33:22 +00003463 if (Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00003464 StaticLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00003465
Reid Spencer5f016e22007-07-11 17:01:13 +00003466 // If there is a type-qualifier-list, read it now.
Chris Lattnera1fcbad2008-12-18 06:50:14 +00003467 // Type qualifiers in an array subscript are a C99 feature.
Reid Spencer5f016e22007-07-11 17:01:13 +00003468 DeclSpec DS;
Chris Lattner5a69d1c2008-12-18 07:02:59 +00003469 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Mike Stump1eb44332009-09-09 15:08:12 +00003470
Reid Spencer5f016e22007-07-11 17:01:13 +00003471 // If we haven't already read 'static', check to see if there is one after the
3472 // type-qualifier-list.
Chris Lattner04d66662007-10-09 17:33:22 +00003473 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00003474 StaticLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00003475
Reid Spencer5f016e22007-07-11 17:01:13 +00003476 // Handle "direct-declarator [ type-qual-list[opt] * ]".
3477 bool isStar = false;
John McCall60d7b3a2010-08-24 06:29:42 +00003478 ExprResult NumElements;
Mike Stump1eb44332009-09-09 15:08:12 +00003479
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00003480 // Handle the case where we have '[*]' as the array size. However, a leading
3481 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
3482 // the the token after the star is a ']'. Since stars in arrays are
3483 // infrequent, use of lookahead is not costly here.
3484 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattnera711dd02008-04-06 05:27:21 +00003485 ConsumeToken(); // Eat the '*'.
Reid Spencer5f016e22007-07-11 17:01:13 +00003486
Chris Lattnera1fcbad2008-12-18 06:50:14 +00003487 if (StaticLoc.isValid()) {
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00003488 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattnera1fcbad2008-12-18 06:50:14 +00003489 StaticLoc = SourceLocation(); // Drop the static.
3490 }
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00003491 isStar = true;
Chris Lattner04d66662007-10-09 17:33:22 +00003492 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner378c7e42008-12-18 07:27:21 +00003493 // Note, in C89, this production uses the constant-expr production instead
3494 // of assignment-expr. The only difference is that assignment-expr allows
3495 // things like '=' and '*='. Sema rejects these in C89 mode because they
3496 // are not i-c-e's, so we don't need to distinguish between the two here.
Mike Stump1eb44332009-09-09 15:08:12 +00003497
Douglas Gregore0762c92009-06-19 23:52:42 +00003498 // Parse the constant-expression or assignment-expression now (depending
3499 // on dialect).
3500 if (getLang().CPlusPlus)
3501 NumElements = ParseConstantExpression();
3502 else
3503 NumElements = ParseAssignmentExpression();
Reid Spencer5f016e22007-07-11 17:01:13 +00003504 }
Mike Stump1eb44332009-09-09 15:08:12 +00003505
Reid Spencer5f016e22007-07-11 17:01:13 +00003506 // If there was an error parsing the assignment-expression, recover.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00003507 if (NumElements.isInvalid()) {
Chris Lattner5cb10d32009-04-24 22:30:50 +00003508 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00003509 // If the expression was invalid, skip it.
3510 SkipUntil(tok::r_square);
3511 return;
3512 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00003513
3514 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
3515
Sean Huntbbd37c62009-11-21 08:43:09 +00003516 //FIXME: Use these
3517 CXX0XAttributeList Attr;
3518 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier()) {
3519 Attr = ParseCXX0XAttributes();
3520 }
3521
Chris Lattner378c7e42008-12-18 07:27:21 +00003522 // Remember that we parsed a array type, and remember its features.
Reid Spencer5f016e22007-07-11 17:01:13 +00003523 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
3524 StaticLoc.isValid(), isStar,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00003525 NumElements.release(),
3526 StartLoc, EndLoc),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003527 EndLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00003528}
3529
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00003530/// [GNU] typeof-specifier:
3531/// typeof ( expressions )
3532/// typeof ( type-name )
3533/// [GNU/C++] typeof unary-expression
Steve Naroffd1861fd2007-07-31 12:34:36 +00003534///
3535void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner04d66662007-10-09 17:33:22 +00003536 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00003537 Token OpTok = Tok;
Steve Naroffd1861fd2007-07-31 12:34:36 +00003538 SourceLocation StartLoc = ConsumeToken();
3539
John McCallcfb708c2010-01-13 20:03:27 +00003540 const bool hasParens = Tok.is(tok::l_paren);
3541
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00003542 bool isCastExpr;
John McCallb3d87482010-08-24 05:47:05 +00003543 ParsedType CastTy;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00003544 SourceRange CastRange;
John McCall60d7b3a2010-08-24 06:29:42 +00003545 ExprResult Operand = ParseExprAfterTypeofSizeofAlignof(OpTok,
John McCall911093e2010-08-25 02:45:51 +00003546 isCastExpr,
3547 CastTy,
3548 CastRange);
John McCallcfb708c2010-01-13 20:03:27 +00003549 if (hasParens)
3550 DS.setTypeofParensRange(CastRange);
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00003551
3552 if (CastRange.getEnd().isInvalid())
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00003553 // FIXME: Not accurate, the range gets one token more than it should.
3554 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00003555 else
3556 DS.SetRangeEnd(CastRange.getEnd());
Mike Stump1eb44332009-09-09 15:08:12 +00003557
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00003558 if (isCastExpr) {
3559 if (!CastTy) {
3560 DS.SetTypeSpecError();
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00003561 return;
Douglas Gregor809070a2009-02-18 17:45:20 +00003562 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00003563
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00003564 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00003565 unsigned DiagID;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00003566 // Check for duplicate type specifiers (e.g. "int typeof(int)").
3567 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00003568 DiagID, CastTy))
3569 Diag(StartLoc, DiagID) << PrevSpec;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00003570 return;
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00003571 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00003572
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00003573 // If we get here, the operand to the typeof was an expresion.
3574 if (Operand.isInvalid()) {
3575 DS.SetTypeSpecError();
Steve Naroff9dfa7b42007-08-02 02:53:48 +00003576 return;
Steve Naroffd1861fd2007-07-31 12:34:36 +00003577 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00003578
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00003579 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00003580 unsigned DiagID;
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00003581 // Check for duplicate type specifiers (e.g. "int typeof(int)").
3582 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
John McCallb3d87482010-08-24 05:47:05 +00003583 DiagID, Operand.get()))
John McCallfec54012009-08-03 20:12:06 +00003584 Diag(StartLoc, DiagID) << PrevSpec;
Steve Naroffd1861fd2007-07-31 12:34:36 +00003585}
Chris Lattner1b492422010-02-28 18:33:55 +00003586
3587
3588/// TryAltiVecVectorTokenOutOfLine - Out of line body that should only be called
3589/// from TryAltiVecVectorToken.
3590bool Parser::TryAltiVecVectorTokenOutOfLine() {
3591 Token Next = NextToken();
3592 switch (Next.getKind()) {
3593 default: return false;
3594 case tok::kw_short:
3595 case tok::kw_long:
3596 case tok::kw_signed:
3597 case tok::kw_unsigned:
3598 case tok::kw_void:
3599 case tok::kw_char:
3600 case tok::kw_int:
3601 case tok::kw_float:
3602 case tok::kw_double:
3603 case tok::kw_bool:
3604 case tok::kw___pixel:
3605 Tok.setKind(tok::kw___vector);
3606 return true;
3607 case tok::identifier:
3608 if (Next.getIdentifierInfo() == Ident_pixel) {
3609 Tok.setKind(tok::kw___vector);
3610 return true;
3611 }
3612 return false;
3613 }
3614}
3615
3616bool Parser::TryAltiVecTokenOutOfLine(DeclSpec &DS, SourceLocation Loc,
3617 const char *&PrevSpec, unsigned &DiagID,
3618 bool &isInvalid) {
3619 if (Tok.getIdentifierInfo() == Ident_vector) {
3620 Token Next = NextToken();
3621 switch (Next.getKind()) {
3622 case tok::kw_short:
3623 case tok::kw_long:
3624 case tok::kw_signed:
3625 case tok::kw_unsigned:
3626 case tok::kw_void:
3627 case tok::kw_char:
3628 case tok::kw_int:
3629 case tok::kw_float:
3630 case tok::kw_double:
3631 case tok::kw_bool:
3632 case tok::kw___pixel:
3633 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
3634 return true;
3635 case tok::identifier:
3636 if (Next.getIdentifierInfo() == Ident_pixel) {
3637 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
3638 return true;
3639 }
3640 break;
3641 default:
3642 break;
3643 }
Douglas Gregora8f031f2010-06-16 15:28:57 +00003644 } else if ((Tok.getIdentifierInfo() == Ident_pixel) &&
Chris Lattner1b492422010-02-28 18:33:55 +00003645 DS.isTypeAltiVecVector()) {
3646 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
3647 return true;
3648 }
3649 return false;
3650}
3651