blob: 1f81202f014a3bd40923a7dd995f4fbaf764584c [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
Reid Spencer5f016e22007-07-11 17:01:13 +0000296/// ParseDeclaration - Parse a full 'declaration', which consists of
297/// declaration-specifiers, some number of declarators, and a semicolon.
Chris Lattner97144fc2009-04-02 04:16:50 +0000298/// 'Context' should be a Declarator::TheContext value. This returns the
299/// location of the semicolon in DeclEnd.
Chris Lattner8f08cb72007-08-25 06:57:03 +0000300///
301/// declaration: [C99 6.7]
302/// block-declaration ->
303/// simple-declaration
304/// others [FIXME]
Douglas Gregoradcac882008-12-01 23:54:00 +0000305/// [C++] template-declaration
Chris Lattner8f08cb72007-08-25 06:57:03 +0000306/// [C++] namespace-definition
Douglas Gregorf780abc2008-12-30 03:27:21 +0000307/// [C++] using-directive
Douglas Gregord7f37bf2009-06-22 23:06:13 +0000308/// [C++] using-declaration
Sebastian Redl50de12f2009-03-24 22:27:57 +0000309/// [C++0x] static_assert-declaration
Chris Lattner8f08cb72007-08-25 06:57:03 +0000310/// others... [FIXME]
311///
Chris Lattner97144fc2009-04-02 04:16:50 +0000312Parser::DeclGroupPtrTy Parser::ParseDeclaration(unsigned Context,
Sean Huntbbd37c62009-11-21 08:43:09 +0000313 SourceLocation &DeclEnd,
314 CXX0XAttributeList Attr) {
Argyrios Kyrtzidis36d36802010-06-17 10:52:18 +0000315 ParenBraceBracketBalancer BalancerRAIIObj(*this);
316
John McCalld226f652010-08-21 09:40:31 +0000317 Decl *SingleDecl = 0;
Chris Lattner8f08cb72007-08-25 06:57:03 +0000318 switch (Tok.getKind()) {
Douglas Gregoradcac882008-12-01 23:54:00 +0000319 case tok::kw_template:
Douglas Gregor1426e532009-05-12 21:31:51 +0000320 case tok::kw_export:
Sean Huntbbd37c62009-11-21 08:43:09 +0000321 if (Attr.HasAttr)
322 Diag(Attr.Range.getBegin(), diag::err_attributes_not_allowed)
323 << Attr.Range;
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000324 SingleDecl = ParseDeclarationStartingWithTemplate(Context, DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +0000325 break;
Sebastian Redld078e642010-08-27 23:12:46 +0000326 case tok::kw_inline:
327 // Could be the start of an inline namespace.
328 if (getLang().CPlusPlus0x && NextToken().is(tok::kw_namespace)) {
329 if (Attr.HasAttr)
330 Diag(Attr.Range.getBegin(), diag::err_attributes_not_allowed)
331 << Attr.Range;
332 SourceLocation InlineLoc = ConsumeToken();
333 SingleDecl = ParseNamespace(Context, DeclEnd, InlineLoc);
334 break;
335 }
336 return ParseSimpleDeclaration(Context, DeclEnd, Attr.AttrList, true);
Chris Lattner8f08cb72007-08-25 06:57:03 +0000337 case tok::kw_namespace:
Sean Huntbbd37c62009-11-21 08:43:09 +0000338 if (Attr.HasAttr)
339 Diag(Attr.Range.getBegin(), diag::err_attributes_not_allowed)
340 << Attr.Range;
Chris Lattner97144fc2009-04-02 04:16:50 +0000341 SingleDecl = ParseNamespace(Context, DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +0000342 break;
Douglas Gregorf780abc2008-12-30 03:27:21 +0000343 case tok::kw_using:
Sean Huntbbd37c62009-11-21 08:43:09 +0000344 SingleDecl = ParseUsingDirectiveOrDeclaration(Context, DeclEnd, Attr);
Chris Lattner682bf922009-03-29 16:50:03 +0000345 break;
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000346 case tok::kw_static_assert:
Sean Huntbbd37c62009-11-21 08:43:09 +0000347 if (Attr.HasAttr)
348 Diag(Attr.Range.getBegin(), diag::err_attributes_not_allowed)
349 << Attr.Range;
Chris Lattner97144fc2009-04-02 04:16:50 +0000350 SingleDecl = ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +0000351 break;
Chris Lattner8f08cb72007-08-25 06:57:03 +0000352 default:
Chris Lattner5c5db552010-04-05 18:18:31 +0000353 return ParseSimpleDeclaration(Context, DeclEnd, Attr.AttrList, true);
Chris Lattner8f08cb72007-08-25 06:57:03 +0000354 }
Sean Huntbbd37c62009-11-21 08:43:09 +0000355
Chris Lattner682bf922009-03-29 16:50:03 +0000356 // This routine returns a DeclGroup, if the thing we parsed only contains a
357 // single decl, convert it now.
358 return Actions.ConvertDeclToDeclGroup(SingleDecl);
Chris Lattner8f08cb72007-08-25 06:57:03 +0000359}
360
361/// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl]
362/// declaration-specifiers init-declarator-list[opt] ';'
363///[C90/C++]init-declarator-list ';' [TODO]
364/// [OMP] threadprivate-directive [TODO]
Chris Lattnercd147752009-03-29 17:27:48 +0000365///
366/// If RequireSemi is false, this does not check for a ';' at the end of the
Chris Lattner5c5db552010-04-05 18:18:31 +0000367/// declaration. If it is true, it checks for and eats it.
Chris Lattnercd147752009-03-29 17:27:48 +0000368Parser::DeclGroupPtrTy Parser::ParseSimpleDeclaration(unsigned Context,
Sean Huntbbd37c62009-11-21 08:43:09 +0000369 SourceLocation &DeclEnd,
Chris Lattner5c5db552010-04-05 18:18:31 +0000370 AttributeList *Attr,
371 bool RequireSemi) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000372 // Parse the common declaration-specifiers piece.
John McCall54abf7d2009-11-04 02:18:39 +0000373 ParsingDeclSpec DS(*this);
Sean Huntbbd37c62009-11-21 08:43:09 +0000374 if (Attr)
375 DS.AddAttributes(Attr);
Douglas Gregor0efc2c12010-01-13 17:31:36 +0000376 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS_none,
377 getDeclSpecContextFromDeclaratorContext(Context));
Mike Stump1eb44332009-09-09 15:08:12 +0000378
Reid Spencer5f016e22007-07-11 17:01:13 +0000379 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
380 // declaration-specifiers init-declarator-list[opt] ';'
Chris Lattner04d66662007-10-09 17:33:22 +0000381 if (Tok.is(tok::semi)) {
Chris Lattner5c5db552010-04-05 18:18:31 +0000382 if (RequireSemi) ConsumeToken();
John McCalld226f652010-08-21 09:40:31 +0000383 Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none,
John McCallaec03712010-05-21 20:45:30 +0000384 DS);
John McCall54abf7d2009-11-04 02:18:39 +0000385 DS.complete(TheDecl);
Chris Lattner682bf922009-03-29 16:50:03 +0000386 return Actions.ConvertDeclToDeclGroup(TheDecl);
Reid Spencer5f016e22007-07-11 17:01:13 +0000387 }
Mike Stump1eb44332009-09-09 15:08:12 +0000388
Chris Lattner5c5db552010-04-05 18:18:31 +0000389 return ParseDeclGroup(DS, Context, /*FunctionDefs=*/ false, &DeclEnd);
John McCalld8ac0572009-11-03 19:26:08 +0000390}
Mike Stump1eb44332009-09-09 15:08:12 +0000391
John McCalld8ac0572009-11-03 19:26:08 +0000392/// ParseDeclGroup - Having concluded that this is either a function
393/// definition or a group of object declarations, actually parse the
394/// result.
John McCall54abf7d2009-11-04 02:18:39 +0000395Parser::DeclGroupPtrTy Parser::ParseDeclGroup(ParsingDeclSpec &DS,
396 unsigned Context,
John McCalld8ac0572009-11-03 19:26:08 +0000397 bool AllowFunctionDefinitions,
398 SourceLocation *DeclEnd) {
399 // Parse the first declarator.
John McCall54abf7d2009-11-04 02:18:39 +0000400 ParsingDeclarator D(*this, DS, static_cast<Declarator::TheContext>(Context));
John McCalld8ac0572009-11-03 19:26:08 +0000401 ParseDeclarator(D);
Chris Lattnercd147752009-03-29 17:27:48 +0000402
John McCalld8ac0572009-11-03 19:26:08 +0000403 // Bail out if the first declarator didn't seem well-formed.
404 if (!D.hasName() && !D.mayOmitIdentifier()) {
405 // Skip until ; or }.
406 SkipUntil(tok::r_brace, true, true);
407 if (Tok.is(tok::semi))
408 ConsumeToken();
409 return DeclGroupPtrTy();
Chris Lattner23c4b182009-03-29 17:18:04 +0000410 }
Mike Stump1eb44332009-09-09 15:08:12 +0000411
Chris Lattnerc82daef2010-07-11 22:24:20 +0000412 // Check to see if we have a function *definition* which must have a body.
413 if (AllowFunctionDefinitions && D.isFunctionDeclarator() &&
414 // Look at the next token to make sure that this isn't a function
415 // declaration. We have to check this because __attribute__ might be the
416 // start of a function definition in GCC-extended K&R C.
417 !isDeclarationAfterDeclarator()) {
418
Chris Lattner004659a2010-07-11 22:42:07 +0000419 if (isStartOfFunctionDefinition(D)) {
John McCalld8ac0572009-11-03 19:26:08 +0000420 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
421 Diag(Tok, diag::err_function_declared_typedef);
422
423 // Recover by treating the 'typedef' as spurious.
424 DS.ClearStorageClassSpecs();
425 }
426
John McCalld226f652010-08-21 09:40:31 +0000427 Decl *TheDecl = ParseFunctionDefinition(D);
John McCalld8ac0572009-11-03 19:26:08 +0000428 return Actions.ConvertDeclToDeclGroup(TheDecl);
Chris Lattner004659a2010-07-11 22:42:07 +0000429 }
430
431 if (isDeclarationSpecifier()) {
432 // If there is an invalid declaration specifier right after the function
433 // prototype, then we must be in a missing semicolon case where this isn't
434 // actually a body. Just fall through into the code that handles it as a
435 // prototype, and let the top-level code handle the erroneous declspec
436 // where it would otherwise expect a comma or semicolon.
John McCalld8ac0572009-11-03 19:26:08 +0000437 } else {
438 Diag(Tok, diag::err_expected_fn_body);
439 SkipUntil(tok::semi);
440 return DeclGroupPtrTy();
441 }
442 }
443
John McCalld226f652010-08-21 09:40:31 +0000444 llvm::SmallVector<Decl *, 8> DeclsInGroup;
445 Decl *FirstDecl = ParseDeclarationAfterDeclarator(D);
John McCall54abf7d2009-11-04 02:18:39 +0000446 D.complete(FirstDecl);
John McCalld226f652010-08-21 09:40:31 +0000447 if (FirstDecl)
John McCalld8ac0572009-11-03 19:26:08 +0000448 DeclsInGroup.push_back(FirstDecl);
449
450 // If we don't have a comma, it is either the end of the list (a ';') or an
451 // error, bail out.
452 while (Tok.is(tok::comma)) {
453 // Consume the comma.
Chris Lattner23c4b182009-03-29 17:18:04 +0000454 ConsumeToken();
John McCalld8ac0572009-11-03 19:26:08 +0000455
456 // Parse the next declarator.
457 D.clear();
458
459 // Accept attributes in an init-declarator. In the first declarator in a
460 // declaration, these would be part of the declspec. In subsequent
461 // declarators, they become part of the declarator itself, so that they
462 // don't apply to declarators after *this* one. Examples:
463 // short __attribute__((common)) var; -> declspec
464 // short var __attribute__((common)); -> declarator
465 // short x, __attribute__((common)) var; -> declarator
466 if (Tok.is(tok::kw___attribute)) {
467 SourceLocation Loc;
Sean Huntbbd37c62009-11-21 08:43:09 +0000468 AttributeList *AttrList = ParseGNUAttributes(&Loc);
John McCalld8ac0572009-11-03 19:26:08 +0000469 D.AddAttributes(AttrList, Loc);
470 }
471
472 ParseDeclarator(D);
473
John McCalld226f652010-08-21 09:40:31 +0000474 Decl *ThisDecl = ParseDeclarationAfterDeclarator(D);
John McCall54abf7d2009-11-04 02:18:39 +0000475 D.complete(ThisDecl);
John McCalld226f652010-08-21 09:40:31 +0000476 if (ThisDecl)
John McCalld8ac0572009-11-03 19:26:08 +0000477 DeclsInGroup.push_back(ThisDecl);
478 }
479
480 if (DeclEnd)
481 *DeclEnd = Tok.getLocation();
482
483 if (Context != Declarator::ForContext &&
484 ExpectAndConsume(tok::semi,
485 Context == Declarator::FileContext
486 ? diag::err_invalid_token_after_toplevel_declarator
487 : diag::err_expected_semi_declaration)) {
Chris Lattner004659a2010-07-11 22:42:07 +0000488 // Okay, there was no semicolon and one was expected. If we see a
489 // declaration specifier, just assume it was missing and continue parsing.
490 // Otherwise things are very confused and we skip to recover.
491 if (!isDeclarationSpecifier()) {
492 SkipUntil(tok::r_brace, true, true);
493 if (Tok.is(tok::semi))
494 ConsumeToken();
495 }
John McCalld8ac0572009-11-03 19:26:08 +0000496 }
497
Douglas Gregor23c94db2010-07-02 17:43:08 +0000498 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS,
John McCalld8ac0572009-11-03 19:26:08 +0000499 DeclsInGroup.data(),
500 DeclsInGroup.size());
Reid Spencer5f016e22007-07-11 17:01:13 +0000501}
502
Douglas Gregor1426e532009-05-12 21:31:51 +0000503/// \brief Parse 'declaration' after parsing 'declaration-specifiers
504/// declarator'. This method parses the remainder of the declaration
505/// (including any attributes or initializer, among other things) and
506/// finalizes the declaration.
Reid Spencer5f016e22007-07-11 17:01:13 +0000507///
Reid Spencer5f016e22007-07-11 17:01:13 +0000508/// init-declarator: [C99 6.7]
509/// declarator
510/// declarator '=' initializer
511/// [GNU] declarator simple-asm-expr[opt] attributes[opt]
512/// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +0000513/// [C++] declarator initializer[opt]
514///
515/// [C++] initializer:
516/// [C++] '=' initializer-clause
517/// [C++] '(' expression-list ')'
Sebastian Redl50de12f2009-03-24 22:27:57 +0000518/// [C++0x] '=' 'default' [TODO]
519/// [C++0x] '=' 'delete'
520///
521/// According to the standard grammar, =default and =delete are function
522/// definitions, but that definitely doesn't fit with the parser here.
Reid Spencer5f016e22007-07-11 17:01:13 +0000523///
John McCalld226f652010-08-21 09:40:31 +0000524Decl *Parser::ParseDeclarationAfterDeclarator(Declarator &D,
Douglas Gregore542c862009-06-23 23:11:28 +0000525 const ParsedTemplateInfo &TemplateInfo) {
Douglas Gregor1426e532009-05-12 21:31:51 +0000526 // If a simple-asm-expr is present, parse it.
527 if (Tok.is(tok::kw_asm)) {
528 SourceLocation Loc;
John McCall60d7b3a2010-08-24 06:29:42 +0000529 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
Douglas Gregor1426e532009-05-12 21:31:51 +0000530 if (AsmLabel.isInvalid()) {
531 SkipUntil(tok::semi, true, true);
John McCalld226f652010-08-21 09:40:31 +0000532 return 0;
Douglas Gregor1426e532009-05-12 21:31:51 +0000533 }
Mike Stump1eb44332009-09-09 15:08:12 +0000534
Douglas Gregor1426e532009-05-12 21:31:51 +0000535 D.setAsmLabel(AsmLabel.release());
536 D.SetRangeEnd(Loc);
537 }
Mike Stump1eb44332009-09-09 15:08:12 +0000538
Douglas Gregor1426e532009-05-12 21:31:51 +0000539 // If attributes are present, parse them.
540 if (Tok.is(tok::kw___attribute)) {
541 SourceLocation Loc;
Sean Huntbbd37c62009-11-21 08:43:09 +0000542 AttributeList *AttrList = ParseGNUAttributes(&Loc);
Douglas Gregor1426e532009-05-12 21:31:51 +0000543 D.AddAttributes(AttrList, Loc);
544 }
Mike Stump1eb44332009-09-09 15:08:12 +0000545
Douglas Gregor1426e532009-05-12 21:31:51 +0000546 // Inform the current actions module that we just parsed this declarator.
John McCalld226f652010-08-21 09:40:31 +0000547 Decl *ThisDecl = 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +0000548 switch (TemplateInfo.Kind) {
549 case ParsedTemplateInfo::NonTemplate:
Douglas Gregor23c94db2010-07-02 17:43:08 +0000550 ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
Douglas Gregord5a423b2009-09-25 18:43:00 +0000551 break;
552
553 case ParsedTemplateInfo::Template:
554 case ParsedTemplateInfo::ExplicitSpecialization:
Douglas Gregor23c94db2010-07-02 17:43:08 +0000555 ThisDecl = Actions.ActOnTemplateDeclarator(getCurScope(),
John McCallf312b1e2010-08-26 23:41:50 +0000556 MultiTemplateParamsArg(Actions,
Douglas Gregore542c862009-06-23 23:11:28 +0000557 TemplateInfo.TemplateParams->data(),
558 TemplateInfo.TemplateParams->size()),
Douglas Gregord5a423b2009-09-25 18:43:00 +0000559 D);
560 break;
561
562 case ParsedTemplateInfo::ExplicitInstantiation: {
John McCalld226f652010-08-21 09:40:31 +0000563 DeclResult ThisRes
Douglas Gregor23c94db2010-07-02 17:43:08 +0000564 = Actions.ActOnExplicitInstantiation(getCurScope(),
Douglas Gregord5a423b2009-09-25 18:43:00 +0000565 TemplateInfo.ExternLoc,
566 TemplateInfo.TemplateLoc,
567 D);
568 if (ThisRes.isInvalid()) {
569 SkipUntil(tok::semi, true, true);
John McCalld226f652010-08-21 09:40:31 +0000570 return 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +0000571 }
572
573 ThisDecl = ThisRes.get();
574 break;
575 }
576 }
Mike Stump1eb44332009-09-09 15:08:12 +0000577
Douglas Gregor1426e532009-05-12 21:31:51 +0000578 // Parse declarator '=' initializer.
579 if (Tok.is(tok::equal)) {
580 ConsumeToken();
581 if (getLang().CPlusPlus0x && Tok.is(tok::kw_delete)) {
582 SourceLocation DelLoc = ConsumeToken();
583 Actions.SetDeclDeleted(ThisDecl, DelLoc);
584 } else {
John McCall731ad842009-12-19 09:28:58 +0000585 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
586 EnterScope(0);
Douglas Gregor23c94db2010-07-02 17:43:08 +0000587 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
John McCall731ad842009-12-19 09:28:58 +0000588 }
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +0000589
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +0000590 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000591 Actions.CodeCompleteInitializer(getCurScope(), ThisDecl);
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +0000592 ConsumeCodeCompletionToken();
593 SkipUntil(tok::comma, true, true);
594 return ThisDecl;
595 }
596
John McCall60d7b3a2010-08-24 06:29:42 +0000597 ExprResult Init(ParseInitializer());
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +0000598
John McCall731ad842009-12-19 09:28:58 +0000599 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000600 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
John McCall731ad842009-12-19 09:28:58 +0000601 ExitScope();
602 }
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +0000603
Douglas Gregor1426e532009-05-12 21:31:51 +0000604 if (Init.isInvalid()) {
Douglas Gregor00225542010-03-01 18:27:54 +0000605 SkipUntil(tok::comma, true, true);
606 Actions.ActOnInitializerError(ThisDecl);
607 } else
John McCall9ae2f072010-08-23 23:25:46 +0000608 Actions.AddInitializerToDecl(ThisDecl, Init.take());
Douglas Gregor1426e532009-05-12 21:31:51 +0000609 }
610 } else if (Tok.is(tok::l_paren)) {
611 // Parse C++ direct initializer: '(' expression-list ')'
612 SourceLocation LParenLoc = ConsumeParen();
613 ExprVector Exprs(Actions);
614 CommaLocsTy CommaLocs;
615
Douglas Gregorb4debae2009-12-22 17:47:17 +0000616 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
617 EnterScope(0);
Douglas Gregor23c94db2010-07-02 17:43:08 +0000618 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +0000619 }
620
Douglas Gregor1426e532009-05-12 21:31:51 +0000621 if (ParseExpressionList(Exprs, CommaLocs)) {
622 SkipUntil(tok::r_paren);
Douglas Gregorb4debae2009-12-22 17:47:17 +0000623
624 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000625 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +0000626 ExitScope();
627 }
Douglas Gregor1426e532009-05-12 21:31:51 +0000628 } else {
629 // Match the ')'.
630 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
631
632 assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() &&
633 "Unexpected number of commas!");
Douglas Gregorb4debae2009-12-22 17:47:17 +0000634
635 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000636 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +0000637 ExitScope();
638 }
639
Douglas Gregor1426e532009-05-12 21:31:51 +0000640 Actions.AddCXXDirectInitializerToDecl(ThisDecl, LParenLoc,
641 move_arg(Exprs),
Jay Foadbeaaccd2009-05-21 09:52:38 +0000642 CommaLocs.data(), RParenLoc);
Douglas Gregor1426e532009-05-12 21:31:51 +0000643 }
644 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000645 bool TypeContainsUndeducedAuto =
Anders Carlsson6a75cd92009-07-11 00:34:39 +0000646 D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto;
647 Actions.ActOnUninitializedDecl(ThisDecl, TypeContainsUndeducedAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +0000648 }
649
650 return ThisDecl;
651}
652
Reid Spencer5f016e22007-07-11 17:01:13 +0000653/// ParseSpecifierQualifierList
654/// specifier-qualifier-list:
655/// type-specifier specifier-qualifier-list[opt]
656/// type-qualifier specifier-qualifier-list[opt]
657/// [GNU] attributes specifier-qualifier-list[opt]
658///
659void Parser::ParseSpecifierQualifierList(DeclSpec &DS) {
660 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
661 /// parse declaration-specifiers and complain about extra stuff.
Reid Spencer5f016e22007-07-11 17:01:13 +0000662 ParseDeclarationSpecifiers(DS);
Mike Stump1eb44332009-09-09 15:08:12 +0000663
Reid Spencer5f016e22007-07-11 17:01:13 +0000664 // Validate declspec for type-name.
665 unsigned Specs = DS.getParsedSpecifiers();
Chris Lattnerb6645dd2009-04-14 21:16:09 +0000666 if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers() &&
667 !DS.getAttributes())
Reid Spencer5f016e22007-07-11 17:01:13 +0000668 Diag(Tok, diag::err_typename_requires_specqual);
Mike Stump1eb44332009-09-09 15:08:12 +0000669
Reid Spencer5f016e22007-07-11 17:01:13 +0000670 // Issue diagnostic and remove storage class if present.
671 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
672 if (DS.getStorageClassSpecLoc().isValid())
673 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
674 else
675 Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass);
676 DS.ClearStorageClassSpecs();
677 }
Mike Stump1eb44332009-09-09 15:08:12 +0000678
Reid Spencer5f016e22007-07-11 17:01:13 +0000679 // Issue diagnostic and remove function specfier if present.
680 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Douglas Gregorb48fe382008-10-31 09:07:45 +0000681 if (DS.isInlineSpecified())
682 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
683 if (DS.isVirtualSpecified())
684 Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec);
685 if (DS.isExplicitSpecified())
686 Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec);
Reid Spencer5f016e22007-07-11 17:01:13 +0000687 DS.ClearFunctionSpecs();
688 }
689}
690
Chris Lattnerc199ab32009-04-12 20:42:31 +0000691/// isValidAfterIdentifierInDeclaratorAfterDeclSpec - Return true if the
692/// specified token is valid after the identifier in a declarator which
693/// immediately follows the declspec. For example, these things are valid:
694///
695/// int x [ 4]; // direct-declarator
696/// int x ( int y); // direct-declarator
697/// int(int x ) // direct-declarator
698/// int x ; // simple-declaration
699/// int x = 17; // init-declarator-list
700/// int x , y; // init-declarator-list
701/// int x __asm__ ("foo"); // init-declarator-list
Chris Lattnerb6645dd2009-04-14 21:16:09 +0000702/// int x : 4; // struct-declarator
Chris Lattnerc83c27a2009-04-12 22:29:43 +0000703/// int x { 5}; // C++'0x unified initializers
Chris Lattnerc199ab32009-04-12 20:42:31 +0000704///
705/// This is not, because 'x' does not immediately follow the declspec (though
706/// ')' happens to be valid anyway).
707/// int (x)
708///
709static bool isValidAfterIdentifierInDeclarator(const Token &T) {
710 return T.is(tok::l_square) || T.is(tok::l_paren) || T.is(tok::r_paren) ||
711 T.is(tok::semi) || T.is(tok::comma) || T.is(tok::equal) ||
Chris Lattnerb6645dd2009-04-14 21:16:09 +0000712 T.is(tok::kw_asm) || T.is(tok::l_brace) || T.is(tok::colon);
Chris Lattnerc199ab32009-04-12 20:42:31 +0000713}
714
Chris Lattnere40c2952009-04-14 21:34:55 +0000715
716/// ParseImplicitInt - This method is called when we have an non-typename
717/// identifier in a declspec (which normally terminates the decl spec) when
718/// the declspec has no type specifier. In this case, the declspec is either
719/// malformed or is "implicit int" (in K&R and C89).
720///
721/// This method handles diagnosing this prettily and returns false if the
722/// declspec is done being processed. If it recovers and thinks there may be
723/// other pieces of declspec after it, it returns true.
724///
Chris Lattnerf4382f52009-04-14 22:17:06 +0000725bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000726 const ParsedTemplateInfo &TemplateInfo,
Chris Lattnere40c2952009-04-14 21:34:55 +0000727 AccessSpecifier AS) {
Chris Lattnerf4382f52009-04-14 22:17:06 +0000728 assert(Tok.is(tok::identifier) && "should have identifier");
Mike Stump1eb44332009-09-09 15:08:12 +0000729
Chris Lattnere40c2952009-04-14 21:34:55 +0000730 SourceLocation Loc = Tok.getLocation();
731 // If we see an identifier that is not a type name, we normally would
732 // parse it as the identifer being declared. However, when a typename
733 // is typo'd or the definition is not included, this will incorrectly
734 // parse the typename as the identifier name and fall over misparsing
735 // later parts of the diagnostic.
736 //
737 // As such, we try to do some look-ahead in cases where this would
738 // otherwise be an "implicit-int" case to see if this is invalid. For
739 // example: "static foo_t x = 4;" In this case, if we parsed foo_t as
740 // an identifier with implicit int, we'd get a parse error because the
741 // next token is obviously invalid for a type. Parse these as a case
742 // with an invalid type specifier.
743 assert(!DS.hasTypeSpecifier() && "Type specifier checked above");
Mike Stump1eb44332009-09-09 15:08:12 +0000744
Chris Lattnere40c2952009-04-14 21:34:55 +0000745 // Since we know that this either implicit int (which is rare) or an
746 // error, we'd do lookahead to try to do better recovery.
747 if (isValidAfterIdentifierInDeclarator(NextToken())) {
748 // If this token is valid for implicit int, e.g. "static x = 4", then
749 // we just avoid eating the identifier, so it will be parsed as the
750 // identifier in the declarator.
751 return false;
752 }
Mike Stump1eb44332009-09-09 15:08:12 +0000753
Chris Lattnere40c2952009-04-14 21:34:55 +0000754 // Otherwise, if we don't consume this token, we are going to emit an
755 // error anyway. Try to recover from various common problems. Check
756 // to see if this was a reference to a tag name without a tag specified.
757 // This is a common problem in C (saying 'foo' instead of 'struct foo').
Chris Lattnerf4382f52009-04-14 22:17:06 +0000758 //
759 // C++ doesn't need this, and isTagName doesn't take SS.
760 if (SS == 0) {
761 const char *TagName = 0;
762 tok::TokenKind TagKind = tok::unknown;
Mike Stump1eb44332009-09-09 15:08:12 +0000763
Douglas Gregor23c94db2010-07-02 17:43:08 +0000764 switch (Actions.isTagName(*Tok.getIdentifierInfo(), getCurScope())) {
Chris Lattnere40c2952009-04-14 21:34:55 +0000765 default: break;
766 case DeclSpec::TST_enum: TagName="enum" ;TagKind=tok::kw_enum ;break;
767 case DeclSpec::TST_union: TagName="union" ;TagKind=tok::kw_union ;break;
768 case DeclSpec::TST_struct:TagName="struct";TagKind=tok::kw_struct;break;
769 case DeclSpec::TST_class: TagName="class" ;TagKind=tok::kw_class ;break;
770 }
Mike Stump1eb44332009-09-09 15:08:12 +0000771
Chris Lattnerf4382f52009-04-14 22:17:06 +0000772 if (TagName) {
773 Diag(Loc, diag::err_use_of_tag_name_without_tag)
John McCall23e907a2010-02-14 01:03:10 +0000774 << Tok.getIdentifierInfo() << TagName << getLang().CPlusPlus
Douglas Gregor849b2432010-03-31 17:46:05 +0000775 << FixItHint::CreateInsertion(Tok.getLocation(),TagName);
Mike Stump1eb44332009-09-09 15:08:12 +0000776
Chris Lattnerf4382f52009-04-14 22:17:06 +0000777 // Parse this as a tag as if the missing tag were present.
778 if (TagKind == tok::kw_enum)
Douglas Gregor9b9edd62010-03-02 17:53:14 +0000779 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS);
Chris Lattnerf4382f52009-04-14 22:17:06 +0000780 else
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000781 ParseClassSpecifier(TagKind, Loc, DS, TemplateInfo, AS);
Chris Lattnerf4382f52009-04-14 22:17:06 +0000782 return true;
783 }
Chris Lattnere40c2952009-04-14 21:34:55 +0000784 }
Mike Stump1eb44332009-09-09 15:08:12 +0000785
Douglas Gregora786fdb2009-10-13 23:27:22 +0000786 // This is almost certainly an invalid type name. Let the action emit a
787 // diagnostic and attempt to recover.
John McCallb3d87482010-08-24 05:47:05 +0000788 ParsedType T;
Douglas Gregora786fdb2009-10-13 23:27:22 +0000789 if (Actions.DiagnoseUnknownTypeName(*Tok.getIdentifierInfo(), Loc,
Douglas Gregor23c94db2010-07-02 17:43:08 +0000790 getCurScope(), SS, T)) {
Douglas Gregora786fdb2009-10-13 23:27:22 +0000791 // The action emitted a diagnostic, so we don't have to.
792 if (T) {
793 // The action has suggested that the type T could be used. Set that as
794 // the type in the declaration specifiers, consume the would-be type
795 // name token, and we're done.
796 const char *PrevSpec;
797 unsigned DiagID;
John McCallb3d87482010-08-24 05:47:05 +0000798 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID, T);
Douglas Gregora786fdb2009-10-13 23:27:22 +0000799 DS.SetRangeEnd(Tok.getLocation());
800 ConsumeToken();
801
802 // There may be other declaration specifiers after this.
803 return true;
804 }
805
806 // Fall through; the action had no suggestion for us.
807 } else {
808 // The action did not emit a diagnostic, so emit one now.
809 SourceRange R;
810 if (SS) R = SS->getRange();
811 Diag(Loc, diag::err_unknown_typename) << Tok.getIdentifierInfo() << R;
812 }
Mike Stump1eb44332009-09-09 15:08:12 +0000813
Douglas Gregora786fdb2009-10-13 23:27:22 +0000814 // Mark this as an error.
Chris Lattnere40c2952009-04-14 21:34:55 +0000815 const char *PrevSpec;
John McCallfec54012009-08-03 20:12:06 +0000816 unsigned DiagID;
817 DS.SetTypeSpecType(DeclSpec::TST_error, Loc, PrevSpec, DiagID);
Chris Lattnere40c2952009-04-14 21:34:55 +0000818 DS.SetRangeEnd(Tok.getLocation());
819 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000820
Chris Lattnere40c2952009-04-14 21:34:55 +0000821 // TODO: Could inject an invalid typedef decl in an enclosing scope to
822 // avoid rippling error messages on subsequent uses of the same type,
823 // could be useful if #include was forgotten.
824 return false;
825}
826
Douglas Gregor0efc2c12010-01-13 17:31:36 +0000827/// \brief Determine the declaration specifier context from the declarator
828/// context.
829///
830/// \param Context the declarator context, which is one of the
831/// Declarator::TheContext enumerator values.
832Parser::DeclSpecContext
833Parser::getDeclSpecContextFromDeclaratorContext(unsigned Context) {
834 if (Context == Declarator::MemberContext)
835 return DSC_class;
836 if (Context == Declarator::FileContext)
837 return DSC_top_level;
838 return DSC_normal;
839}
840
Reid Spencer5f016e22007-07-11 17:01:13 +0000841/// ParseDeclarationSpecifiers
842/// declaration-specifiers: [C99 6.7]
843/// storage-class-specifier declaration-specifiers[opt]
844/// type-specifier declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +0000845/// [C99] function-specifier declaration-specifiers[opt]
846/// [GNU] attributes declaration-specifiers[opt]
847///
848/// storage-class-specifier: [C99 6.7.1]
849/// 'typedef'
850/// 'extern'
851/// 'static'
852/// 'auto'
853/// 'register'
Sebastian Redl669d5d72008-11-14 23:42:31 +0000854/// [C++] 'mutable'
Reid Spencer5f016e22007-07-11 17:01:13 +0000855/// [GNU] '__thread'
Reid Spencer5f016e22007-07-11 17:01:13 +0000856/// function-specifier: [C99 6.7.4]
857/// [C99] 'inline'
Douglas Gregorb48fe382008-10-31 09:07:45 +0000858/// [C++] 'virtual'
859/// [C++] 'explicit'
Anders Carlssonf47f7a12009-05-06 04:46:28 +0000860/// 'friend': [C++ dcl.friend]
Sebastian Redl2ac67232009-11-05 15:47:02 +0000861/// 'constexpr': [C++0x dcl.constexpr]
Anders Carlssonf47f7a12009-05-06 04:46:28 +0000862
Reid Spencer5f016e22007-07-11 17:01:13 +0000863///
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000864void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000865 const ParsedTemplateInfo &TemplateInfo,
John McCall67d1a672009-08-06 02:15:43 +0000866 AccessSpecifier AS,
Douglas Gregor2ccccb32010-08-23 18:23:48 +0000867 DeclSpecContext DSContext) {
Chris Lattner81c018d2008-03-13 06:29:04 +0000868 DS.SetRangeStart(Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +0000869 while (1) {
John McCallfec54012009-08-03 20:12:06 +0000870 bool isInvalid = false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000871 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +0000872 unsigned DiagID = 0;
873
Reid Spencer5f016e22007-07-11 17:01:13 +0000874 SourceLocation Loc = Tok.getLocation();
Douglas Gregor12e083c2008-11-07 15:42:26 +0000875
Reid Spencer5f016e22007-07-11 17:01:13 +0000876 switch (Tok.getKind()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000877 default:
Chris Lattnerbce61352008-07-26 00:20:22 +0000878 DoneWithDeclSpec:
Reid Spencer5f016e22007-07-11 17:01:13 +0000879 // If this is not a declaration specifier token, we're done reading decl
880 // specifiers. First verify that DeclSpec's are consistent.
Douglas Gregor9b3064b2009-04-01 22:41:11 +0000881 DS.Finish(Diags, PP);
Reid Spencer5f016e22007-07-11 17:01:13 +0000882 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000883
Douglas Gregor2ccccb32010-08-23 18:23:48 +0000884 case tok::code_completion: {
John McCallf312b1e2010-08-26 23:41:50 +0000885 Sema::ParserCompletionContext CCC = Sema::PCC_Namespace;
Douglas Gregor2ccccb32010-08-23 18:23:48 +0000886 if (DS.hasTypeSpecifier()) {
887 bool AllowNonIdentifiers
888 = (getCurScope()->getFlags() & (Scope::ControlScope |
889 Scope::BlockScope |
890 Scope::TemplateParamScope |
891 Scope::FunctionPrototypeScope |
892 Scope::AtCatchScope)) == 0;
893 bool AllowNestedNameSpecifiers
894 = DSContext == DSC_top_level ||
895 (DSContext == DSC_class && DS.isFriendSpecified());
896
897 Actions.CodeCompleteDeclarator(getCurScope(), AllowNonIdentifiers,
898 AllowNestedNameSpecifiers);
899 ConsumeCodeCompletionToken();
900 return;
901 }
902
903 if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate)
John McCallf312b1e2010-08-26 23:41:50 +0000904 CCC = DSContext == DSC_class? Sema::PCC_MemberTemplate
905 : Sema::PCC_Template;
Douglas Gregor2ccccb32010-08-23 18:23:48 +0000906 else if (DSContext == DSC_class)
John McCallf312b1e2010-08-26 23:41:50 +0000907 CCC = Sema::PCC_Class;
Douglas Gregor2ccccb32010-08-23 18:23:48 +0000908 else if (ObjCImpDecl)
John McCallf312b1e2010-08-26 23:41:50 +0000909 CCC = Sema::PCC_ObjCImplementation;
Douglas Gregor2ccccb32010-08-23 18:23:48 +0000910
911 Actions.CodeCompleteOrdinaryName(getCurScope(), CCC);
912 ConsumeCodeCompletionToken();
913 return;
914 }
915
Chris Lattner5e02c472009-01-05 00:07:25 +0000916 case tok::coloncolon: // ::foo::bar
John McCall9ba61662010-02-26 08:45:28 +0000917 // C++ scope specifier. Annotate and loop, or bail out on error.
918 if (TryAnnotateCXXScopeToken(true)) {
919 if (!DS.hasTypeSpecifier())
920 DS.SetTypeSpecError();
921 goto DoneWithDeclSpec;
922 }
John McCall2e0a7152010-03-01 18:20:46 +0000923 if (Tok.is(tok::coloncolon)) // ::new or ::delete
924 goto DoneWithDeclSpec;
John McCall9ba61662010-02-26 08:45:28 +0000925 continue;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000926
927 case tok::annot_cxxscope: {
928 if (DS.hasTypeSpecifier())
929 goto DoneWithDeclSpec;
930
John McCallaa87d332009-12-12 11:40:51 +0000931 CXXScopeSpec SS;
John McCallca0408f2010-08-23 06:44:23 +0000932 SS.setScopeRep((NestedNameSpecifier*) Tok.getAnnotationValue());
John McCallaa87d332009-12-12 11:40:51 +0000933 SS.setRange(Tok.getAnnotationRange());
934
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000935 // We are looking for a qualified typename.
Douglas Gregor9135c722009-03-25 15:40:00 +0000936 Token Next = NextToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000937 if (Next.is(tok::annot_template_id) &&
Douglas Gregor9135c722009-03-25 15:40:00 +0000938 static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue())
Douglas Gregorc45c2322009-03-31 00:43:58 +0000939 ->Kind == TNK_Type_template) {
Douglas Gregor9135c722009-03-25 15:40:00 +0000940 // We have a qualified template-id, e.g., N::A<int>
Douglas Gregor0efc2c12010-01-13 17:31:36 +0000941
942 // C++ [class.qual]p2:
943 // In a lookup in which the constructor is an acceptable lookup
944 // result and the nested-name-specifier nominates a class C:
945 //
946 // - if the name specified after the
947 // nested-name-specifier, when looked up in C, is the
948 // injected-class-name of C (Clause 9), or
949 //
950 // - if the name specified after the nested-name-specifier
951 // is the same as the identifier or the
952 // simple-template-id's template-name in the last
953 // component of the nested-name-specifier,
954 //
955 // the name is instead considered to name the constructor of
956 // class C.
957 //
958 // Thus, if the template-name is actually the constructor
959 // name, then the code is ill-formed; this interpretation is
960 // reinforced by the NAD status of core issue 635.
961 TemplateIdAnnotation *TemplateId
962 = static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue());
John McCallba9d8532010-04-13 06:39:49 +0000963 if ((DSContext == DSC_top_level ||
964 (DSContext == DSC_class && DS.isFriendSpecified())) &&
965 TemplateId->Name &&
Douglas Gregor23c94db2010-07-02 17:43:08 +0000966 Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) {
Douglas Gregor0efc2c12010-01-13 17:31:36 +0000967 if (isConstructorDeclarator()) {
968 // The user meant this to be an out-of-line constructor
969 // definition, but template arguments are not allowed
970 // there. Just allow this as a constructor; we'll
971 // complain about it later.
972 goto DoneWithDeclSpec;
973 }
974
975 // The user meant this to name a type, but it actually names
976 // a constructor with some extraneous template
977 // arguments. Complain, then parse it as a type as the user
978 // intended.
979 Diag(TemplateId->TemplateNameLoc,
980 diag::err_out_of_line_template_id_names_constructor)
981 << TemplateId->Name;
982 }
983
John McCallaa87d332009-12-12 11:40:51 +0000984 DS.getTypeSpecScope() = SS;
985 ConsumeToken(); // The C++ scope.
Mike Stump1eb44332009-09-09 15:08:12 +0000986 assert(Tok.is(tok::annot_template_id) &&
Douglas Gregor9135c722009-03-25 15:40:00 +0000987 "ParseOptionalCXXScopeSpecifier not working");
988 AnnotateTemplateIdTokenAsType(&SS);
989 continue;
990 }
991
Douglas Gregor9d7b3532009-09-28 07:26:33 +0000992 if (Next.is(tok::annot_typename)) {
John McCallaa87d332009-12-12 11:40:51 +0000993 DS.getTypeSpecScope() = SS;
994 ConsumeToken(); // The C++ scope.
John McCallb3d87482010-08-24 05:47:05 +0000995 if (Tok.getAnnotationValue()) {
996 ParsedType T = getTypeAnnotation(Tok);
Douglas Gregor9d7b3532009-09-28 07:26:33 +0000997 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc,
John McCallb3d87482010-08-24 05:47:05 +0000998 PrevSpec, DiagID, T);
999 }
Douglas Gregor9d7b3532009-09-28 07:26:33 +00001000 else
1001 DS.SetTypeSpecError();
1002 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1003 ConsumeToken(); // The typename
1004 }
1005
Douglas Gregor9135c722009-03-25 15:40:00 +00001006 if (Next.isNot(tok::identifier))
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001007 goto DoneWithDeclSpec;
1008
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001009 // If we're in a context where the identifier could be a class name,
1010 // check whether this is a constructor declaration.
John McCallba9d8532010-04-13 06:39:49 +00001011 if ((DSContext == DSC_top_level ||
1012 (DSContext == DSC_class && DS.isFriendSpecified())) &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00001013 Actions.isCurrentClassName(*Next.getIdentifierInfo(), getCurScope(),
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001014 &SS)) {
1015 if (isConstructorDeclarator())
1016 goto DoneWithDeclSpec;
1017
1018 // As noted in C++ [class.qual]p2 (cited above), when the name
1019 // of the class is qualified in a context where it could name
1020 // a constructor, its a constructor name. However, we've
1021 // looked at the declarator, and the user probably meant this
1022 // to be a type. Complain that it isn't supposed to be treated
1023 // as a type, then proceed to parse it as a type.
1024 Diag(Next.getLocation(), diag::err_out_of_line_type_names_constructor)
1025 << Next.getIdentifierInfo();
1026 }
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001027
John McCallb3d87482010-08-24 05:47:05 +00001028 ParsedType TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(),
1029 Next.getLocation(),
1030 getCurScope(), &SS);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001031
Chris Lattnerf4382f52009-04-14 22:17:06 +00001032 // If the referenced identifier is not a type, then this declspec is
1033 // erroneous: We already checked about that it has no type specifier, and
1034 // C++ doesn't have implicit int. Diagnose it as a typo w.r.t. to the
Mike Stump1eb44332009-09-09 15:08:12 +00001035 // typename.
Chris Lattnerf4382f52009-04-14 22:17:06 +00001036 if (TypeRep == 0) {
1037 ConsumeToken(); // Eat the scope spec so the identifier is current.
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001038 if (ParseImplicitInt(DS, &SS, TemplateInfo, AS)) continue;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001039 goto DoneWithDeclSpec;
Chris Lattnerf4382f52009-04-14 22:17:06 +00001040 }
Mike Stump1eb44332009-09-09 15:08:12 +00001041
John McCallaa87d332009-12-12 11:40:51 +00001042 DS.getTypeSpecScope() = SS;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001043 ConsumeToken(); // The C++ scope.
1044
Douglas Gregor1a51b4a2009-02-09 15:09:02 +00001045 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001046 DiagID, TypeRep);
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001047 if (isInvalid)
1048 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001049
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001050 DS.SetRangeEnd(Tok.getLocation());
1051 ConsumeToken(); // The typename.
1052
1053 continue;
1054 }
Mike Stump1eb44332009-09-09 15:08:12 +00001055
Chris Lattner80d0c892009-01-21 19:48:37 +00001056 case tok::annot_typename: {
John McCallb3d87482010-08-24 05:47:05 +00001057 if (Tok.getAnnotationValue()) {
1058 ParsedType T = getTypeAnnotation(Tok);
Douglas Gregor31a19b62009-04-01 21:51:26 +00001059 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallb3d87482010-08-24 05:47:05 +00001060 DiagID, T);
1061 } else
Douglas Gregor31a19b62009-04-01 21:51:26 +00001062 DS.SetTypeSpecError();
Chris Lattner5c5db552010-04-05 18:18:31 +00001063
1064 if (isInvalid)
1065 break;
1066
Chris Lattner80d0c892009-01-21 19:48:37 +00001067 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1068 ConsumeToken(); // The typename
Mike Stump1eb44332009-09-09 15:08:12 +00001069
Chris Lattner80d0c892009-01-21 19:48:37 +00001070 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1071 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
1072 // Objective-C interface. If we don't have Objective-C or a '<', this is
1073 // just a normal reference to a typedef name.
1074 if (!Tok.is(tok::less) || !getLang().ObjC1)
1075 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001076
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001077 SourceLocation LAngleLoc, EndProtoLoc;
John McCalld226f652010-08-21 09:40:31 +00001078 llvm::SmallVector<Decl *, 8> ProtocolDecl;
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001079 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
1080 ParseObjCProtocolReferences(ProtocolDecl, ProtocolLocs, false,
1081 LAngleLoc, EndProtoLoc);
1082 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size(),
1083 ProtocolLocs.data(), LAngleLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001084
Chris Lattner80d0c892009-01-21 19:48:37 +00001085 DS.SetRangeEnd(EndProtoLoc);
1086 continue;
1087 }
Mike Stump1eb44332009-09-09 15:08:12 +00001088
Chris Lattner3bd934a2008-07-26 01:18:38 +00001089 // typedef-name
1090 case tok::identifier: {
Chris Lattner5e02c472009-01-05 00:07:25 +00001091 // In C++, check to see if this is a scope specifier like foo::bar::, if
1092 // so handle it as such. This is important for ctor parsing.
John McCall9ba61662010-02-26 08:45:28 +00001093 if (getLang().CPlusPlus) {
1094 if (TryAnnotateCXXScopeToken(true)) {
1095 if (!DS.hasTypeSpecifier())
1096 DS.SetTypeSpecError();
1097 goto DoneWithDeclSpec;
1098 }
1099 if (!Tok.is(tok::identifier))
1100 continue;
1101 }
Mike Stump1eb44332009-09-09 15:08:12 +00001102
Chris Lattner3bd934a2008-07-26 01:18:38 +00001103 // This identifier can only be a typedef name if we haven't already seen
1104 // a type-specifier. Without this check we misparse:
1105 // typedef int X; struct Y { short X; }; as 'short int'.
1106 if (DS.hasTypeSpecifier())
1107 goto DoneWithDeclSpec;
Mike Stump1eb44332009-09-09 15:08:12 +00001108
John Thompson82287d12010-02-05 00:12:22 +00001109 // Check for need to substitute AltiVec keyword tokens.
1110 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
1111 break;
1112
Chris Lattner3bd934a2008-07-26 01:18:38 +00001113 // It has to be available as a typedef too!
John McCallb3d87482010-08-24 05:47:05 +00001114 ParsedType TypeRep =
1115 Actions.getTypeName(*Tok.getIdentifierInfo(),
1116 Tok.getLocation(), getCurScope());
Douglas Gregor55f6b142009-02-09 18:46:07 +00001117
Chris Lattnerc199ab32009-04-12 20:42:31 +00001118 // If this is not a typedef name, don't parse it as part of the declspec,
1119 // it must be an implicit int or an error.
John McCallb3d87482010-08-24 05:47:05 +00001120 if (!TypeRep) {
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001121 if (ParseImplicitInt(DS, 0, TemplateInfo, AS)) continue;
Chris Lattner3bd934a2008-07-26 01:18:38 +00001122 goto DoneWithDeclSpec;
Chris Lattnerc199ab32009-04-12 20:42:31 +00001123 }
Douglas Gregor55f6b142009-02-09 18:46:07 +00001124
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001125 // If we're in a context where the identifier could be a class name,
1126 // check whether this is a constructor declaration.
1127 if (getLang().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00001128 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), getCurScope()) &&
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001129 isConstructorDeclarator())
Douglas Gregorb48fe382008-10-31 09:07:45 +00001130 goto DoneWithDeclSpec;
1131
Douglas Gregor1a51b4a2009-02-09 15:09:02 +00001132 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001133 DiagID, TypeRep);
Chris Lattner3bd934a2008-07-26 01:18:38 +00001134 if (isInvalid)
1135 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001136
Chris Lattner3bd934a2008-07-26 01:18:38 +00001137 DS.SetRangeEnd(Tok.getLocation());
1138 ConsumeToken(); // The identifier
1139
1140 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1141 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
1142 // Objective-C interface. If we don't have Objective-C or a '<', this is
1143 // just a normal reference to a typedef name.
1144 if (!Tok.is(tok::less) || !getLang().ObjC1)
1145 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001146
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001147 SourceLocation LAngleLoc, EndProtoLoc;
John McCalld226f652010-08-21 09:40:31 +00001148 llvm::SmallVector<Decl *, 8> ProtocolDecl;
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001149 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
1150 ParseObjCProtocolReferences(ProtocolDecl, ProtocolLocs, false,
1151 LAngleLoc, EndProtoLoc);
1152 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size(),
1153 ProtocolLocs.data(), LAngleLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001154
Chris Lattner3bd934a2008-07-26 01:18:38 +00001155 DS.SetRangeEnd(EndProtoLoc);
1156
Steve Naroff4f9b9f12008-09-22 10:28:57 +00001157 // Need to support trailing type qualifiers (e.g. "id<p> const").
1158 // If a type specifier follows, it will be diagnosed elsewhere.
1159 continue;
Chris Lattner3bd934a2008-07-26 01:18:38 +00001160 }
Douglas Gregor39a8de12009-02-25 19:37:18 +00001161
1162 // type-name
1163 case tok::annot_template_id: {
Mike Stump1eb44332009-09-09 15:08:12 +00001164 TemplateIdAnnotation *TemplateId
Douglas Gregor39a8de12009-02-25 19:37:18 +00001165 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
Douglas Gregorc45c2322009-03-31 00:43:58 +00001166 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor39a8de12009-02-25 19:37:18 +00001167 // This template-id does not refer to a type name, so we're
1168 // done with the type-specifiers.
1169 goto DoneWithDeclSpec;
1170 }
1171
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001172 // If we're in a context where the template-id could be a
1173 // constructor name or specialization, check whether this is a
1174 // constructor declaration.
1175 if (getLang().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00001176 Actions.isCurrentClassName(*TemplateId->Name, getCurScope()) &&
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001177 isConstructorDeclarator())
1178 goto DoneWithDeclSpec;
1179
Douglas Gregor39a8de12009-02-25 19:37:18 +00001180 // Turn the template-id annotation token into a type annotation
1181 // token, then try again to parse it as a type-specifier.
Douglas Gregor31a19b62009-04-01 21:51:26 +00001182 AnnotateTemplateIdTokenAsType();
Douglas Gregor39a8de12009-02-25 19:37:18 +00001183 continue;
1184 }
1185
Reid Spencer5f016e22007-07-11 17:01:13 +00001186 // GNU attributes support.
1187 case tok::kw___attribute:
Sean Huntbbd37c62009-11-21 08:43:09 +00001188 DS.AddAttributes(ParseGNUAttributes());
Reid Spencer5f016e22007-07-11 17:01:13 +00001189 continue;
Steve Narofff59e17e2008-12-24 20:59:21 +00001190
1191 // Microsoft declspec support.
1192 case tok::kw___declspec:
Eli Friedmana23b4852009-06-08 07:21:15 +00001193 DS.AddAttributes(ParseMicrosoftDeclSpec());
Steve Narofff59e17e2008-12-24 20:59:21 +00001194 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001195
Steve Naroff239f0732008-12-25 14:16:32 +00001196 // Microsoft single token adornments.
Steve Naroff86bc6cf2008-12-25 14:41:26 +00001197 case tok::kw___forceinline:
Eli Friedman290eeb02009-06-08 23:27:34 +00001198 // FIXME: Add handling here!
1199 break;
1200
1201 case tok::kw___ptr64:
Steve Naroff86bc6cf2008-12-25 14:41:26 +00001202 case tok::kw___w64:
Steve Naroff239f0732008-12-25 14:16:32 +00001203 case tok::kw___cdecl:
1204 case tok::kw___stdcall:
1205 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00001206 case tok::kw___thiscall:
Eli Friedman290eeb02009-06-08 23:27:34 +00001207 DS.AddAttributes(ParseMicrosoftTypeAttributes());
1208 continue;
1209
Reid Spencer5f016e22007-07-11 17:01:13 +00001210 // storage-class-specifier
1211 case tok::kw_typedef:
John McCallfec54012009-08-03 20:12:06 +00001212 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_typedef, Loc, PrevSpec,
1213 DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001214 break;
1215 case tok::kw_extern:
1216 if (DS.isThreadSpecified())
Chris Lattner1ab3b962008-11-18 07:48:38 +00001217 Diag(Tok, diag::ext_thread_before) << "extern";
John McCallfec54012009-08-03 20:12:06 +00001218 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_extern, Loc, PrevSpec,
1219 DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001220 break;
Steve Naroff8d54bf22007-12-18 00:16:02 +00001221 case tok::kw___private_extern__:
Chris Lattnerf97409f2008-04-06 06:57:35 +00001222 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_private_extern, Loc,
John McCallfec54012009-08-03 20:12:06 +00001223 PrevSpec, DiagID);
Steve Naroff8d54bf22007-12-18 00:16:02 +00001224 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00001225 case tok::kw_static:
1226 if (DS.isThreadSpecified())
Chris Lattner1ab3b962008-11-18 07:48:38 +00001227 Diag(Tok, diag::ext_thread_before) << "static";
John McCallfec54012009-08-03 20:12:06 +00001228 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_static, Loc, PrevSpec,
1229 DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001230 break;
1231 case tok::kw_auto:
Anders Carlssone89d1592009-06-26 18:41:36 +00001232 if (getLang().CPlusPlus0x)
John McCallfec54012009-08-03 20:12:06 +00001233 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec,
1234 DiagID);
Anders Carlssone89d1592009-06-26 18:41:36 +00001235 else
John McCallfec54012009-08-03 20:12:06 +00001236 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, Loc, PrevSpec,
1237 DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001238 break;
1239 case tok::kw_register:
John McCallfec54012009-08-03 20:12:06 +00001240 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_register, Loc, PrevSpec,
1241 DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001242 break;
Sebastian Redl669d5d72008-11-14 23:42:31 +00001243 case tok::kw_mutable:
John McCallfec54012009-08-03 20:12:06 +00001244 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_mutable, Loc, PrevSpec,
1245 DiagID);
Sebastian Redl669d5d72008-11-14 23:42:31 +00001246 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00001247 case tok::kw___thread:
John McCallfec54012009-08-03 20:12:06 +00001248 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001249 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001250
Reid Spencer5f016e22007-07-11 17:01:13 +00001251 // function-specifier
1252 case tok::kw_inline:
John McCallfec54012009-08-03 20:12:06 +00001253 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001254 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +00001255 case tok::kw_virtual:
John McCallfec54012009-08-03 20:12:06 +00001256 isInvalid = DS.SetFunctionSpecVirtual(Loc, PrevSpec, DiagID);
Douglas Gregorb48fe382008-10-31 09:07:45 +00001257 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +00001258 case tok::kw_explicit:
John McCallfec54012009-08-03 20:12:06 +00001259 isInvalid = DS.SetFunctionSpecExplicit(Loc, PrevSpec, DiagID);
Douglas Gregorb48fe382008-10-31 09:07:45 +00001260 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00001261
Anders Carlssonf47f7a12009-05-06 04:46:28 +00001262 // friend
1263 case tok::kw_friend:
John McCall67d1a672009-08-06 02:15:43 +00001264 if (DSContext == DSC_class)
1265 isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID);
1266 else {
1267 PrevSpec = ""; // not actually used by the diagnostic
1268 DiagID = diag::err_friend_invalid_in_context;
1269 isInvalid = true;
1270 }
Anders Carlssonf47f7a12009-05-06 04:46:28 +00001271 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001272
Sebastian Redl2ac67232009-11-05 15:47:02 +00001273 // constexpr
1274 case tok::kw_constexpr:
1275 isInvalid = DS.SetConstexprSpec(Loc, PrevSpec, DiagID);
1276 break;
1277
Chris Lattner80d0c892009-01-21 19:48:37 +00001278 // type-specifier
1279 case tok::kw_short:
John McCallfec54012009-08-03 20:12:06 +00001280 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec,
1281 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001282 break;
1283 case tok::kw_long:
1284 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCallfec54012009-08-03 20:12:06 +00001285 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
1286 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001287 else
John McCallfec54012009-08-03 20:12:06 +00001288 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
1289 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001290 break;
1291 case tok::kw_signed:
John McCallfec54012009-08-03 20:12:06 +00001292 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec,
1293 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001294 break;
1295 case tok::kw_unsigned:
John McCallfec54012009-08-03 20:12:06 +00001296 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
1297 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001298 break;
1299 case tok::kw__Complex:
John McCallfec54012009-08-03 20:12:06 +00001300 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
1301 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001302 break;
1303 case tok::kw__Imaginary:
John McCallfec54012009-08-03 20:12:06 +00001304 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
1305 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001306 break;
1307 case tok::kw_void:
John McCallfec54012009-08-03 20:12:06 +00001308 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec,
1309 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001310 break;
1311 case tok::kw_char:
John McCallfec54012009-08-03 20:12:06 +00001312 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec,
1313 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001314 break;
1315 case tok::kw_int:
John McCallfec54012009-08-03 20:12:06 +00001316 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec,
1317 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001318 break;
1319 case tok::kw_float:
John McCallfec54012009-08-03 20:12:06 +00001320 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec,
1321 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001322 break;
1323 case tok::kw_double:
John McCallfec54012009-08-03 20:12:06 +00001324 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec,
1325 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001326 break;
1327 case tok::kw_wchar_t:
John McCallfec54012009-08-03 20:12:06 +00001328 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec,
1329 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001330 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001331 case tok::kw_char16_t:
John McCallfec54012009-08-03 20:12:06 +00001332 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec,
1333 DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001334 break;
1335 case tok::kw_char32_t:
John McCallfec54012009-08-03 20:12:06 +00001336 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec,
1337 DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001338 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00001339 case tok::kw_bool:
1340 case tok::kw__Bool:
John McCallfec54012009-08-03 20:12:06 +00001341 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec,
1342 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001343 break;
1344 case tok::kw__Decimal32:
John McCallfec54012009-08-03 20:12:06 +00001345 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
1346 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001347 break;
1348 case tok::kw__Decimal64:
John McCallfec54012009-08-03 20:12:06 +00001349 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
1350 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001351 break;
1352 case tok::kw__Decimal128:
John McCallfec54012009-08-03 20:12:06 +00001353 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
1354 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001355 break;
John Thompson82287d12010-02-05 00:12:22 +00001356 case tok::kw___vector:
1357 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
1358 break;
1359 case tok::kw___pixel:
1360 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
1361 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00001362
1363 // class-specifier:
1364 case tok::kw_class:
1365 case tok::kw_struct:
Chris Lattner4c97d762009-04-12 21:49:30 +00001366 case tok::kw_union: {
1367 tok::TokenKind Kind = Tok.getKind();
1368 ConsumeToken();
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001369 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS);
Chris Lattner80d0c892009-01-21 19:48:37 +00001370 continue;
Chris Lattner4c97d762009-04-12 21:49:30 +00001371 }
Chris Lattner80d0c892009-01-21 19:48:37 +00001372
1373 // enum-specifier:
1374 case tok::kw_enum:
Chris Lattner4c97d762009-04-12 21:49:30 +00001375 ConsumeToken();
Douglas Gregor9b9edd62010-03-02 17:53:14 +00001376 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS);
Chris Lattner80d0c892009-01-21 19:48:37 +00001377 continue;
1378
1379 // cv-qualifier:
1380 case tok::kw_const:
John McCallfec54012009-08-03 20:12:06 +00001381 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec, DiagID,
1382 getLang());
Chris Lattner80d0c892009-01-21 19:48:37 +00001383 break;
1384 case tok::kw_volatile:
John McCallfec54012009-08-03 20:12:06 +00001385 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
1386 getLang());
Chris Lattner80d0c892009-01-21 19:48:37 +00001387 break;
1388 case tok::kw_restrict:
John McCallfec54012009-08-03 20:12:06 +00001389 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
1390 getLang());
Chris Lattner80d0c892009-01-21 19:48:37 +00001391 break;
1392
Douglas Gregord57959a2009-03-27 23:10:48 +00001393 // C++ typename-specifier:
1394 case tok::kw_typename:
John McCall9ba61662010-02-26 08:45:28 +00001395 if (TryAnnotateTypeOrScopeToken()) {
1396 DS.SetTypeSpecError();
1397 goto DoneWithDeclSpec;
1398 }
1399 if (!Tok.is(tok::kw_typename))
Douglas Gregord57959a2009-03-27 23:10:48 +00001400 continue;
1401 break;
1402
Chris Lattner80d0c892009-01-21 19:48:37 +00001403 // GNU typeof support.
1404 case tok::kw_typeof:
1405 ParseTypeofSpecifier(DS);
1406 continue;
1407
Anders Carlsson6fd634f2009-06-24 17:47:40 +00001408 case tok::kw_decltype:
1409 ParseDecltypeSpecifier(DS);
1410 continue;
1411
Steve Naroffd3ded1f2008-06-05 00:02:44 +00001412 case tok::less:
Chris Lattner3bd934a2008-07-26 01:18:38 +00001413 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattnerbce61352008-07-26 00:20:22 +00001414 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
1415 // but we support it.
Chris Lattner3bd934a2008-07-26 01:18:38 +00001416 if (DS.hasTypeSpecifier() || !getLang().ObjC1)
Chris Lattnerbce61352008-07-26 00:20:22 +00001417 goto DoneWithDeclSpec;
Mike Stump1eb44332009-09-09 15:08:12 +00001418
Chris Lattnerbce61352008-07-26 00:20:22 +00001419 {
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001420 SourceLocation LAngleLoc, EndProtoLoc;
John McCalld226f652010-08-21 09:40:31 +00001421 llvm::SmallVector<Decl *, 8> ProtocolDecl;
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001422 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
1423 ParseObjCProtocolReferences(ProtocolDecl, ProtocolLocs, false,
1424 LAngleLoc, EndProtoLoc);
1425 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size(),
1426 ProtocolLocs.data(), LAngleLoc);
Chris Lattner3bd934a2008-07-26 01:18:38 +00001427 DS.SetRangeEnd(EndProtoLoc);
1428
Chris Lattner1ab3b962008-11-18 07:48:38 +00001429 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
Douglas Gregor849b2432010-03-31 17:46:05 +00001430 << FixItHint::CreateInsertion(Loc, "id")
Chris Lattner1ab3b962008-11-18 07:48:38 +00001431 << SourceRange(Loc, EndProtoLoc);
Steve Naroff4f9b9f12008-09-22 10:28:57 +00001432 // Need to support trailing type qualifiers (e.g. "id<p> const").
1433 // If a type specifier follows, it will be diagnosed elsewhere.
1434 continue;
Steve Naroffd3ded1f2008-06-05 00:02:44 +00001435 }
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.
1550 if (!Tok.is(tok::less) || !getLang().ObjC1)
1551 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00001552
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001553 SourceLocation LAngleLoc, EndProtoLoc;
John McCalld226f652010-08-21 09:40:31 +00001554 llvm::SmallVector<Decl *, 8> ProtocolDecl;
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001555 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
1556 ParseObjCProtocolReferences(ProtocolDecl, ProtocolLocs, false,
1557 LAngleLoc, EndProtoLoc);
1558 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size(),
1559 ProtocolLocs.data(), LAngleLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001560
Douglas Gregor12e083c2008-11-07 15:42:26 +00001561 DS.SetRangeEnd(EndProtoLoc);
1562 return true;
1563 }
1564
1565 case tok::kw_short:
John McCallfec54012009-08-03 20:12:06 +00001566 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001567 break;
1568 case tok::kw_long:
1569 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCallfec54012009-08-03 20:12:06 +00001570 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
1571 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001572 else
John McCallfec54012009-08-03 20:12:06 +00001573 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
1574 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001575 break;
1576 case tok::kw_signed:
John McCallfec54012009-08-03 20:12:06 +00001577 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001578 break;
1579 case tok::kw_unsigned:
John McCallfec54012009-08-03 20:12:06 +00001580 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
1581 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001582 break;
1583 case tok::kw__Complex:
John McCallfec54012009-08-03 20:12:06 +00001584 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
1585 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001586 break;
1587 case tok::kw__Imaginary:
John McCallfec54012009-08-03 20:12:06 +00001588 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
1589 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001590 break;
1591 case tok::kw_void:
John McCallfec54012009-08-03 20:12:06 +00001592 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001593 break;
1594 case tok::kw_char:
John McCallfec54012009-08-03 20:12:06 +00001595 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001596 break;
1597 case tok::kw_int:
John McCallfec54012009-08-03 20:12:06 +00001598 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001599 break;
1600 case tok::kw_float:
John McCallfec54012009-08-03 20:12:06 +00001601 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001602 break;
1603 case tok::kw_double:
John McCallfec54012009-08-03 20:12:06 +00001604 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001605 break;
1606 case tok::kw_wchar_t:
John McCallfec54012009-08-03 20:12:06 +00001607 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001608 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001609 case tok::kw_char16_t:
John McCallfec54012009-08-03 20:12:06 +00001610 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001611 break;
1612 case tok::kw_char32_t:
John McCallfec54012009-08-03 20:12:06 +00001613 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001614 break;
Douglas Gregor12e083c2008-11-07 15:42:26 +00001615 case tok::kw_bool:
1616 case tok::kw__Bool:
John McCallfec54012009-08-03 20:12:06 +00001617 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001618 break;
1619 case tok::kw__Decimal32:
John McCallfec54012009-08-03 20:12:06 +00001620 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
1621 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001622 break;
1623 case tok::kw__Decimal64:
John McCallfec54012009-08-03 20:12:06 +00001624 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
1625 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001626 break;
1627 case tok::kw__Decimal128:
John McCallfec54012009-08-03 20:12:06 +00001628 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
1629 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001630 break;
John Thompson82287d12010-02-05 00:12:22 +00001631 case tok::kw___vector:
1632 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
1633 break;
1634 case tok::kw___pixel:
1635 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
1636 break;
1637
Douglas Gregor12e083c2008-11-07 15:42:26 +00001638 // class-specifier:
1639 case tok::kw_class:
1640 case tok::kw_struct:
Chris Lattner4c97d762009-04-12 21:49:30 +00001641 case tok::kw_union: {
1642 tok::TokenKind Kind = Tok.getKind();
1643 ConsumeToken();
Sebastian Redld9bafa72010-02-03 21:21:43 +00001644 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS_none,
1645 SuppressDeclarations);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001646 return true;
Chris Lattner4c97d762009-04-12 21:49:30 +00001647 }
Douglas Gregor12e083c2008-11-07 15:42:26 +00001648
1649 // enum-specifier:
1650 case tok::kw_enum:
Chris Lattner4c97d762009-04-12 21:49:30 +00001651 ConsumeToken();
Douglas Gregor9b9edd62010-03-02 17:53:14 +00001652 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS_none);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001653 return true;
1654
1655 // cv-qualifier:
1656 case tok::kw_const:
1657 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001658 DiagID, getLang());
Douglas Gregor12e083c2008-11-07 15:42:26 +00001659 break;
1660 case tok::kw_volatile:
1661 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001662 DiagID, getLang());
Douglas Gregor12e083c2008-11-07 15:42:26 +00001663 break;
1664 case tok::kw_restrict:
1665 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001666 DiagID, getLang());
Douglas Gregor12e083c2008-11-07 15:42:26 +00001667 break;
1668
1669 // GNU typeof support.
1670 case tok::kw_typeof:
1671 ParseTypeofSpecifier(DS);
1672 return true;
1673
Anders Carlsson6fd634f2009-06-24 17:47:40 +00001674 // C++0x decltype support.
1675 case tok::kw_decltype:
1676 ParseDecltypeSpecifier(DS);
1677 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00001678
Anders Carlsson0b7f7892009-06-26 23:44:14 +00001679 // C++0x auto support.
1680 case tok::kw_auto:
1681 if (!getLang().CPlusPlus0x)
1682 return false;
1683
John McCallfec54012009-08-03 20:12:06 +00001684 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec, DiagID);
Anders Carlsson0b7f7892009-06-26 23:44:14 +00001685 break;
Eli Friedman290eeb02009-06-08 23:27:34 +00001686 case tok::kw___ptr64:
1687 case tok::kw___w64:
Steve Naroff239f0732008-12-25 14:16:32 +00001688 case tok::kw___cdecl:
1689 case tok::kw___stdcall:
1690 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00001691 case tok::kw___thiscall:
Eli Friedman290eeb02009-06-08 23:27:34 +00001692 DS.AddAttributes(ParseMicrosoftTypeAttributes());
Chris Lattner837acd02009-01-21 19:19:26 +00001693 return true;
Steve Naroff239f0732008-12-25 14:16:32 +00001694
Douglas Gregor12e083c2008-11-07 15:42:26 +00001695 default:
1696 // Not a type-specifier; do nothing.
1697 return false;
1698 }
1699
1700 // If the specifier combination wasn't legal, issue a diagnostic.
1701 if (isInvalid) {
1702 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner1ab3b962008-11-18 07:48:38 +00001703 // Pick between error or extwarn.
Chris Lattner1ab3b962008-11-18 07:48:38 +00001704 Diag(Tok, DiagID) << PrevSpec;
Douglas Gregor12e083c2008-11-07 15:42:26 +00001705 }
1706 DS.SetRangeEnd(Tok.getLocation());
1707 ConsumeToken(); // whatever we parsed above.
1708 return true;
1709}
Reid Spencer5f016e22007-07-11 17:01:13 +00001710
Chris Lattnercd4b83c2007-10-29 04:42:53 +00001711/// ParseStructDeclaration - Parse a struct declaration without the terminating
1712/// semicolon.
1713///
Reid Spencer5f016e22007-07-11 17:01:13 +00001714/// struct-declaration:
Chris Lattnercd4b83c2007-10-29 04:42:53 +00001715/// specifier-qualifier-list struct-declarator-list
Reid Spencer5f016e22007-07-11 17:01:13 +00001716/// [GNU] __extension__ struct-declaration
Chris Lattnercd4b83c2007-10-29 04:42:53 +00001717/// [GNU] specifier-qualifier-list
Reid Spencer5f016e22007-07-11 17:01:13 +00001718/// struct-declarator-list:
1719/// struct-declarator
1720/// struct-declarator-list ',' struct-declarator
1721/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
1722/// struct-declarator:
1723/// declarator
1724/// [GNU] declarator attributes[opt]
1725/// declarator[opt] ':' constant-expression
1726/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
1727///
Chris Lattnere1359422008-04-10 06:46:29 +00001728void Parser::
John McCallbdd563e2009-11-03 02:38:08 +00001729ParseStructDeclaration(DeclSpec &DS, FieldCallback &Fields) {
Chris Lattnerc46d1a12008-10-20 06:45:43 +00001730 if (Tok.is(tok::kw___extension__)) {
1731 // __extension__ silences extension warnings in the subexpression.
1732 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroff28a7ca82007-08-20 22:28:22 +00001733 ConsumeToken();
Chris Lattnerc46d1a12008-10-20 06:45:43 +00001734 return ParseStructDeclaration(DS, Fields);
1735 }
Mike Stump1eb44332009-09-09 15:08:12 +00001736
Steve Naroff28a7ca82007-08-20 22:28:22 +00001737 // Parse the common specifier-qualifiers-list piece.
Chris Lattner60b1e3e2008-04-10 06:15:14 +00001738 SourceLocation DSStart = Tok.getLocation();
Steve Naroff28a7ca82007-08-20 22:28:22 +00001739 ParseSpecifierQualifierList(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00001740
Douglas Gregor4920f1f2009-01-12 22:49:06 +00001741 // If there are no declarators, this is a free-standing declaration
1742 // specifier. Let the actions module cope with it.
Chris Lattner04d66662007-10-09 17:33:22 +00001743 if (Tok.is(tok::semi)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001744 Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none, DS);
Steve Naroff28a7ca82007-08-20 22:28:22 +00001745 return;
1746 }
1747
1748 // Read struct-declarators until we find the semicolon.
John McCallbdd563e2009-11-03 02:38:08 +00001749 bool FirstDeclarator = true;
Steve Naroff28a7ca82007-08-20 22:28:22 +00001750 while (1) {
John McCall54abf7d2009-11-04 02:18:39 +00001751 ParsingDeclRAIIObject PD(*this);
John McCallbdd563e2009-11-03 02:38:08 +00001752 FieldDeclarator DeclaratorInfo(DS);
1753
1754 // Attributes are only allowed here on successive declarators.
1755 if (!FirstDeclarator && Tok.is(tok::kw___attribute)) {
1756 SourceLocation Loc;
Sean Huntbbd37c62009-11-21 08:43:09 +00001757 AttributeList *AttrList = ParseGNUAttributes(&Loc);
John McCallbdd563e2009-11-03 02:38:08 +00001758 DeclaratorInfo.D.AddAttributes(AttrList, Loc);
1759 }
Mike Stump1eb44332009-09-09 15:08:12 +00001760
Steve Naroff28a7ca82007-08-20 22:28:22 +00001761 /// struct-declarator: declarator
1762 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattnera1efc8c2009-12-10 01:59:24 +00001763 if (Tok.isNot(tok::colon)) {
1764 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
1765 ColonProtectionRAIIObject X(*this);
Chris Lattnere1359422008-04-10 06:46:29 +00001766 ParseDeclarator(DeclaratorInfo.D);
Chris Lattnera1efc8c2009-12-10 01:59:24 +00001767 }
Mike Stump1eb44332009-09-09 15:08:12 +00001768
Chris Lattner04d66662007-10-09 17:33:22 +00001769 if (Tok.is(tok::colon)) {
Steve Naroff28a7ca82007-08-20 22:28:22 +00001770 ConsumeToken();
John McCall60d7b3a2010-08-24 06:29:42 +00001771 ExprResult Res(ParseConstantExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001772 if (Res.isInvalid())
Steve Naroff28a7ca82007-08-20 22:28:22 +00001773 SkipUntil(tok::semi, true, true);
Chris Lattner60b1e3e2008-04-10 06:15:14 +00001774 else
Sebastian Redleffa8d12008-12-10 00:02:53 +00001775 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroff28a7ca82007-08-20 22:28:22 +00001776 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00001777
Steve Naroff28a7ca82007-08-20 22:28:22 +00001778 // If attributes exist after the declarator, parse them.
Sebastian Redlab197ba2009-02-09 18:23:29 +00001779 if (Tok.is(tok::kw___attribute)) {
1780 SourceLocation Loc;
Sean Huntbbd37c62009-11-21 08:43:09 +00001781 AttributeList *AttrList = ParseGNUAttributes(&Loc);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001782 DeclaratorInfo.D.AddAttributes(AttrList, Loc);
1783 }
1784
John McCallbdd563e2009-11-03 02:38:08 +00001785 // We're done with this declarator; invoke the callback.
John McCalld226f652010-08-21 09:40:31 +00001786 Decl *D = Fields.invoke(DeclaratorInfo);
John McCall54abf7d2009-11-04 02:18:39 +00001787 PD.complete(D);
John McCallbdd563e2009-11-03 02:38:08 +00001788
Steve Naroff28a7ca82007-08-20 22:28:22 +00001789 // If we don't have a comma, it is either the end of the list (a ';')
1790 // or an error, bail out.
Chris Lattner04d66662007-10-09 17:33:22 +00001791 if (Tok.isNot(tok::comma))
Chris Lattnercd4b83c2007-10-29 04:42:53 +00001792 return;
Sebastian Redlab197ba2009-02-09 18:23:29 +00001793
Steve Naroff28a7ca82007-08-20 22:28:22 +00001794 // Consume the comma.
1795 ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00001796
John McCallbdd563e2009-11-03 02:38:08 +00001797 FirstDeclarator = false;
Steve Naroff28a7ca82007-08-20 22:28:22 +00001798 }
Steve Naroff28a7ca82007-08-20 22:28:22 +00001799}
1800
1801/// ParseStructUnionBody
1802/// struct-contents:
1803/// struct-declaration-list
1804/// [EXT] empty
1805/// [GNU] "struct-declaration-list" without terminatoring ';'
1806/// struct-declaration-list:
1807/// struct-declaration
1808/// struct-declaration-list struct-declaration
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00001809/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroff28a7ca82007-08-20 22:28:22 +00001810///
Reid Spencer5f016e22007-07-11 17:01:13 +00001811void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
John McCalld226f652010-08-21 09:40:31 +00001812 unsigned TagType, Decl *TagDecl) {
John McCallf312b1e2010-08-26 23:41:50 +00001813 PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc,
1814 "parsing struct/union body");
Mike Stump1eb44332009-09-09 15:08:12 +00001815
Reid Spencer5f016e22007-07-11 17:01:13 +00001816 SourceLocation LBraceLoc = ConsumeBrace();
Mike Stump1eb44332009-09-09 15:08:12 +00001817
Douglas Gregor3218c4b2009-01-09 22:42:13 +00001818 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
Douglas Gregor23c94db2010-07-02 17:43:08 +00001819 Actions.ActOnTagStartDefinition(getCurScope(), TagDecl);
Douglas Gregor72de6672009-01-08 20:45:30 +00001820
Reid Spencer5f016e22007-07-11 17:01:13 +00001821 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
1822 // C++.
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001823 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Douglas Gregor03332962010-07-29 14:29:34 +00001824 Diag(Tok, diag::ext_empty_struct_union)
1825 << (TagType == TST_union);
Reid Spencer5f016e22007-07-11 17:01:13 +00001826
John McCalld226f652010-08-21 09:40:31 +00001827 llvm::SmallVector<Decl *, 32> FieldDecls;
Chris Lattnere1359422008-04-10 06:46:29 +00001828
Reid Spencer5f016e22007-07-11 17:01:13 +00001829 // While we still have something to read, read the declarations in the struct.
Chris Lattner04d66662007-10-09 17:33:22 +00001830 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001831 // Each iteration of this loop reads one struct-declaration.
Mike Stump1eb44332009-09-09 15:08:12 +00001832
Reid Spencer5f016e22007-07-11 17:01:13 +00001833 // Check for extraneous top-level semicolon.
Chris Lattner04d66662007-10-09 17:33:22 +00001834 if (Tok.is(tok::semi)) {
Douglas Gregor9b3064b2009-04-01 22:41:11 +00001835 Diag(Tok, diag::ext_extra_struct_semi)
Douglas Gregorf13ca062010-06-16 23:08:59 +00001836 << DeclSpec::getSpecifierName((DeclSpec::TST)TagType)
Douglas Gregor849b2432010-03-31 17:46:05 +00001837 << FixItHint::CreateRemoval(Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00001838 ConsumeToken();
1839 continue;
1840 }
Chris Lattnere1359422008-04-10 06:46:29 +00001841
1842 // Parse all the comma separated declarators.
1843 DeclSpec DS;
Mike Stump1eb44332009-09-09 15:08:12 +00001844
John McCallbdd563e2009-11-03 02:38:08 +00001845 if (!Tok.is(tok::at)) {
1846 struct CFieldCallback : FieldCallback {
1847 Parser &P;
John McCalld226f652010-08-21 09:40:31 +00001848 Decl *TagDecl;
1849 llvm::SmallVectorImpl<Decl *> &FieldDecls;
John McCallbdd563e2009-11-03 02:38:08 +00001850
John McCalld226f652010-08-21 09:40:31 +00001851 CFieldCallback(Parser &P, Decl *TagDecl,
1852 llvm::SmallVectorImpl<Decl *> &FieldDecls) :
John McCallbdd563e2009-11-03 02:38:08 +00001853 P(P), TagDecl(TagDecl), FieldDecls(FieldDecls) {}
1854
John McCalld226f652010-08-21 09:40:31 +00001855 virtual Decl *invoke(FieldDeclarator &FD) {
John McCallbdd563e2009-11-03 02:38:08 +00001856 // Install the declarator into the current TagDecl.
John McCalld226f652010-08-21 09:40:31 +00001857 Decl *Field = P.Actions.ActOnField(P.getCurScope(), TagDecl,
John McCall4ba39712009-11-03 21:13:47 +00001858 FD.D.getDeclSpec().getSourceRange().getBegin(),
1859 FD.D, FD.BitfieldSize);
John McCallbdd563e2009-11-03 02:38:08 +00001860 FieldDecls.push_back(Field);
1861 return Field;
Douglas Gregor91a28862009-08-26 14:27:30 +00001862 }
John McCallbdd563e2009-11-03 02:38:08 +00001863 } Callback(*this, TagDecl, FieldDecls);
1864
1865 ParseStructDeclaration(DS, Callback);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00001866 } else { // Handle @defs
1867 ConsumeToken();
1868 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
1869 Diag(Tok, diag::err_unexpected_at);
Chris Lattner3e156ad2010-02-02 00:37:27 +00001870 SkipUntil(tok::semi, true);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00001871 continue;
1872 }
1873 ConsumeToken();
1874 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
1875 if (!Tok.is(tok::identifier)) {
1876 Diag(Tok, diag::err_expected_ident);
Chris Lattner3e156ad2010-02-02 00:37:27 +00001877 SkipUntil(tok::semi, true);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00001878 continue;
1879 }
John McCalld226f652010-08-21 09:40:31 +00001880 llvm::SmallVector<Decl *, 16> Fields;
Douglas Gregor23c94db2010-07-02 17:43:08 +00001881 Actions.ActOnDefs(getCurScope(), TagDecl, Tok.getLocation(),
Douglas Gregor44b43212008-12-11 16:49:14 +00001882 Tok.getIdentifierInfo(), Fields);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00001883 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
1884 ConsumeToken();
1885 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
Mike Stump1eb44332009-09-09 15:08:12 +00001886 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001887
Chris Lattner04d66662007-10-09 17:33:22 +00001888 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001889 ConsumeToken();
Chris Lattner04d66662007-10-09 17:33:22 +00001890 } else if (Tok.is(tok::r_brace)) {
Chris Lattner3e156ad2010-02-02 00:37:27 +00001891 ExpectAndConsume(tok::semi, diag::ext_expected_semi_decl_list);
Reid Spencer5f016e22007-07-11 17:01:13 +00001892 break;
1893 } else {
Chris Lattner3e156ad2010-02-02 00:37:27 +00001894 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
1895 // Skip to end of block or statement to avoid ext-warning on extra ';'.
Reid Spencer5f016e22007-07-11 17:01:13 +00001896 SkipUntil(tok::r_brace, true, true);
Chris Lattner3e156ad2010-02-02 00:37:27 +00001897 // If we stopped at a ';', eat it.
1898 if (Tok.is(tok::semi)) ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00001899 }
1900 }
Mike Stump1eb44332009-09-09 15:08:12 +00001901
Steve Naroff60fccee2007-10-29 21:38:07 +00001902 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001903
Ted Kremenek1e377652010-02-11 02:19:13 +00001904 llvm::OwningPtr<AttributeList> AttrList;
Reid Spencer5f016e22007-07-11 17:01:13 +00001905 // If attributes exist after struct contents, parse them.
Chris Lattner04d66662007-10-09 17:33:22 +00001906 if (Tok.is(tok::kw___attribute))
Ted Kremenek1e377652010-02-11 02:19:13 +00001907 AttrList.reset(ParseGNUAttributes());
Daniel Dunbar1bfe1c22008-10-03 02:03:53 +00001908
Douglas Gregor23c94db2010-07-02 17:43:08 +00001909 Actions.ActOnFields(getCurScope(),
Jay Foadbeaaccd2009-05-21 09:52:38 +00001910 RecordLoc, TagDecl, FieldDecls.data(), FieldDecls.size(),
Daniel Dunbar1bfe1c22008-10-03 02:03:53 +00001911 LBraceLoc, RBraceLoc,
Ted Kremenek1e377652010-02-11 02:19:13 +00001912 AttrList.get());
Douglas Gregor72de6672009-01-08 20:45:30 +00001913 StructScope.Exit();
Douglas Gregor23c94db2010-07-02 17:43:08 +00001914 Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl, RBraceLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001915}
1916
1917
1918/// ParseEnumSpecifier
1919/// enum-specifier: [C99 6.7.2.2]
1920/// 'enum' identifier[opt] '{' enumerator-list '}'
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001921///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Reid Spencer5f016e22007-07-11 17:01:13 +00001922/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
1923/// '}' attributes[opt]
1924/// 'enum' identifier
1925/// [GNU] 'enum' attributes[opt] identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001926///
1927/// [C++] elaborated-type-specifier:
1928/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
1929///
Chris Lattner4c97d762009-04-12 21:49:30 +00001930void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregor9b9edd62010-03-02 17:53:14 +00001931 const ParsedTemplateInfo &TemplateInfo,
Chris Lattner4c97d762009-04-12 21:49:30 +00001932 AccessSpecifier AS) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001933 // Parse the tag portion of this.
Douglas Gregor374929f2009-09-18 15:37:17 +00001934 if (Tok.is(tok::code_completion)) {
1935 // Code completion for an enum name.
Douglas Gregor23c94db2010-07-02 17:43:08 +00001936 Actions.CodeCompleteTag(getCurScope(), DeclSpec::TST_enum);
Douglas Gregordc845342010-05-25 05:58:43 +00001937 ConsumeCodeCompletionToken();
Douglas Gregor374929f2009-09-18 15:37:17 +00001938 }
1939
Ted Kremenek1e377652010-02-11 02:19:13 +00001940 llvm::OwningPtr<AttributeList> Attr;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001941 // If attributes exist after tag, parse them.
1942 if (Tok.is(tok::kw___attribute))
Ted Kremenek1e377652010-02-11 02:19:13 +00001943 Attr.reset(ParseGNUAttributes());
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001944
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00001945 CXXScopeSpec &SS = DS.getTypeSpecScope();
John McCall9ba61662010-02-26 08:45:28 +00001946 if (getLang().CPlusPlus) {
John McCallb3d87482010-08-24 05:47:05 +00001947 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), false))
John McCall9ba61662010-02-26 08:45:28 +00001948 return;
1949
1950 if (SS.isSet() && Tok.isNot(tok::identifier)) {
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001951 Diag(Tok, diag::err_expected_ident);
1952 if (Tok.isNot(tok::l_brace)) {
1953 // Has no name and is not a definition.
1954 // Skip the rest of this declarator, up until the comma or semicolon.
1955 SkipUntil(tok::comma, true);
1956 return;
1957 }
1958 }
1959 }
Mike Stump1eb44332009-09-09 15:08:12 +00001960
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001961 // Must have either 'enum name' or 'enum {...}'.
1962 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace)) {
1963 Diag(Tok, diag::err_expected_ident_lbrace);
Mike Stump1eb44332009-09-09 15:08:12 +00001964
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001965 // Skip the rest of this declarator, up until the comma or semicolon.
1966 SkipUntil(tok::comma, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00001967 return;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001968 }
Mike Stump1eb44332009-09-09 15:08:12 +00001969
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001970 // If an identifier is present, consume and remember it.
1971 IdentifierInfo *Name = 0;
1972 SourceLocation NameLoc;
1973 if (Tok.is(tok::identifier)) {
1974 Name = Tok.getIdentifierInfo();
1975 NameLoc = ConsumeToken();
1976 }
Mike Stump1eb44332009-09-09 15:08:12 +00001977
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001978 // There are three options here. If we have 'enum foo;', then this is a
1979 // forward declaration. If we have 'enum foo {...' then this is a
1980 // definition. Otherwise we have something like 'enum foo xyz', a reference.
1981 //
1982 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
1983 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
1984 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
1985 //
John McCallf312b1e2010-08-26 23:41:50 +00001986 Sema::TagUseKind TUK;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001987 if (Tok.is(tok::l_brace))
John McCallf312b1e2010-08-26 23:41:50 +00001988 TUK = Sema::TUK_Definition;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001989 else if (Tok.is(tok::semi))
John McCallf312b1e2010-08-26 23:41:50 +00001990 TUK = Sema::TUK_Declaration;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001991 else
John McCallf312b1e2010-08-26 23:41:50 +00001992 TUK = Sema::TUK_Reference;
Douglas Gregor8fc6d232010-05-03 17:48:54 +00001993
1994 // enums cannot be templates, although they can be referenced from a
1995 // template.
1996 if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate &&
John McCallf312b1e2010-08-26 23:41:50 +00001997 TUK != Sema::TUK_Reference) {
Douglas Gregor8fc6d232010-05-03 17:48:54 +00001998 Diag(Tok, diag::err_enum_template);
1999
2000 // Skip the rest of this declarator, up until the comma or semicolon.
2001 SkipUntil(tok::comma, true);
2002 return;
2003 }
2004
Douglas Gregor402abb52009-05-28 23:31:59 +00002005 bool Owned = false;
John McCallc4e70192009-09-11 04:59:25 +00002006 bool IsDependent = false;
Douglas Gregor48c89f42010-04-24 16:38:41 +00002007 SourceLocation TSTLoc = NameLoc.isValid()? NameLoc : StartLoc;
2008 const char *PrevSpec = 0;
2009 unsigned DiagID;
John McCalld226f652010-08-21 09:40:31 +00002010 Decl *TagDecl = Actions.ActOnTag(getCurScope(), DeclSpec::TST_enum, TUK,
2011 StartLoc, SS, Name, NameLoc, Attr.get(),
2012 AS,
John McCallf312b1e2010-08-26 23:41:50 +00002013 MultiTemplateParamsArg(Actions),
John McCalld226f652010-08-21 09:40:31 +00002014 Owned, IsDependent);
Douglas Gregor48c89f42010-04-24 16:38:41 +00002015 if (IsDependent) {
2016 // This enum has a dependent nested-name-specifier. Handle it as a
2017 // dependent tag.
2018 if (!Name) {
2019 DS.SetTypeSpecError();
2020 Diag(Tok, diag::err_expected_type_name_after_typename);
2021 return;
2022 }
2023
Douglas Gregor23c94db2010-07-02 17:43:08 +00002024 TypeResult Type = Actions.ActOnDependentTag(getCurScope(), DeclSpec::TST_enum,
Douglas Gregor48c89f42010-04-24 16:38:41 +00002025 TUK, SS, Name, StartLoc,
2026 NameLoc);
2027 if (Type.isInvalid()) {
2028 DS.SetTypeSpecError();
2029 return;
2030 }
2031
2032 if (DS.SetTypeSpecType(DeclSpec::TST_typename, TSTLoc, PrevSpec, DiagID,
John McCallb3d87482010-08-24 05:47:05 +00002033 Type.get()))
Douglas Gregor48c89f42010-04-24 16:38:41 +00002034 Diag(StartLoc, DiagID) << PrevSpec;
2035
2036 return;
2037 }
Mike Stump1eb44332009-09-09 15:08:12 +00002038
John McCalld226f652010-08-21 09:40:31 +00002039 if (!TagDecl) {
Douglas Gregor48c89f42010-04-24 16:38:41 +00002040 // The action failed to produce an enumeration tag. If this is a
2041 // definition, consume the entire definition.
2042 if (Tok.is(tok::l_brace)) {
2043 ConsumeBrace();
2044 SkipUntil(tok::r_brace);
2045 }
2046
2047 DS.SetTypeSpecError();
2048 return;
2049 }
2050
Chris Lattner04d66662007-10-09 17:33:22 +00002051 if (Tok.is(tok::l_brace))
Reid Spencer5f016e22007-07-11 17:01:13 +00002052 ParseEnumBody(StartLoc, TagDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00002053
John McCallb3d87482010-08-24 05:47:05 +00002054 // FIXME: The DeclSpec should keep the locations of both the keyword
2055 // and the name (if there is one).
Douglas Gregorb988f9c2010-01-25 16:33:23 +00002056 if (DS.SetTypeSpecType(DeclSpec::TST_enum, TSTLoc, PrevSpec, DiagID,
John McCalld226f652010-08-21 09:40:31 +00002057 TagDecl, Owned))
John McCallfec54012009-08-03 20:12:06 +00002058 Diag(StartLoc, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00002059}
2060
2061/// ParseEnumBody - Parse a {} enclosed enumerator-list.
2062/// enumerator-list:
2063/// enumerator
2064/// enumerator-list ',' enumerator
2065/// enumerator:
2066/// enumeration-constant
2067/// enumeration-constant '=' constant-expression
2068/// enumeration-constant:
2069/// identifier
2070///
John McCalld226f652010-08-21 09:40:31 +00002071void Parser::ParseEnumBody(SourceLocation StartLoc, Decl *EnumDecl) {
Douglas Gregor074149e2009-01-05 19:45:36 +00002072 // Enter the scope of the enum body and start the definition.
2073 ParseScope EnumScope(this, Scope::DeclScope);
Douglas Gregor23c94db2010-07-02 17:43:08 +00002074 Actions.ActOnTagStartDefinition(getCurScope(), EnumDecl);
Douglas Gregor074149e2009-01-05 19:45:36 +00002075
Reid Spencer5f016e22007-07-11 17:01:13 +00002076 SourceLocation LBraceLoc = ConsumeBrace();
Mike Stump1eb44332009-09-09 15:08:12 +00002077
Chris Lattner7946dd32007-08-27 17:24:30 +00002078 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
Chris Lattner04d66662007-10-09 17:33:22 +00002079 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Fariborz Jahanian05115522010-05-28 22:23:22 +00002080 Diag(Tok, diag::error_empty_enum);
Mike Stump1eb44332009-09-09 15:08:12 +00002081
John McCalld226f652010-08-21 09:40:31 +00002082 llvm::SmallVector<Decl *, 32> EnumConstantDecls;
Reid Spencer5f016e22007-07-11 17:01:13 +00002083
John McCalld226f652010-08-21 09:40:31 +00002084 Decl *LastEnumConstDecl = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00002085
Reid Spencer5f016e22007-07-11 17:01:13 +00002086 // Parse the enumerator-list.
Chris Lattner04d66662007-10-09 17:33:22 +00002087 while (Tok.is(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002088 IdentifierInfo *Ident = Tok.getIdentifierInfo();
2089 SourceLocation IdentLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00002090
Reid Spencer5f016e22007-07-11 17:01:13 +00002091 SourceLocation EqualLoc;
John McCall60d7b3a2010-08-24 06:29:42 +00002092 ExprResult AssignedVal;
Chris Lattner04d66662007-10-09 17:33:22 +00002093 if (Tok.is(tok::equal)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002094 EqualLoc = ConsumeToken();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002095 AssignedVal = ParseConstantExpression();
2096 if (AssignedVal.isInvalid())
Reid Spencer5f016e22007-07-11 17:01:13 +00002097 SkipUntil(tok::comma, tok::r_brace, true, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00002098 }
Mike Stump1eb44332009-09-09 15:08:12 +00002099
Reid Spencer5f016e22007-07-11 17:01:13 +00002100 // Install the enumerator constant into EnumDecl.
John McCalld226f652010-08-21 09:40:31 +00002101 Decl *EnumConstDecl = Actions.ActOnEnumConstant(getCurScope(), EnumDecl,
2102 LastEnumConstDecl,
2103 IdentLoc, Ident,
2104 EqualLoc,
2105 AssignedVal.release());
Reid Spencer5f016e22007-07-11 17:01:13 +00002106 EnumConstantDecls.push_back(EnumConstDecl);
2107 LastEnumConstDecl = EnumConstDecl;
Mike Stump1eb44332009-09-09 15:08:12 +00002108
Chris Lattner04d66662007-10-09 17:33:22 +00002109 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +00002110 break;
2111 SourceLocation CommaLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00002112
2113 if (Tok.isNot(tok::identifier) &&
Douglas Gregor9b3064b2009-04-01 22:41:11 +00002114 !(getLang().C99 || getLang().CPlusPlus0x))
2115 Diag(CommaLoc, diag::ext_enumerator_list_comma)
2116 << getLang().CPlusPlus
Douglas Gregor849b2432010-03-31 17:46:05 +00002117 << FixItHint::CreateRemoval(CommaLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00002118 }
Mike Stump1eb44332009-09-09 15:08:12 +00002119
Reid Spencer5f016e22007-07-11 17:01:13 +00002120 // Eat the }.
Mike Stumpc6e35aa2009-05-16 07:06:02 +00002121 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00002122
Ted Kremenek1e377652010-02-11 02:19:13 +00002123 llvm::OwningPtr<AttributeList> Attr;
Reid Spencer5f016e22007-07-11 17:01:13 +00002124 // If attributes exist after the identifier list, parse them.
Chris Lattner04d66662007-10-09 17:33:22 +00002125 if (Tok.is(tok::kw___attribute))
Ted Kremenek1e377652010-02-11 02:19:13 +00002126 Attr.reset(ParseGNUAttributes()); // FIXME: where do they do?
Douglas Gregor72de6672009-01-08 20:45:30 +00002127
Edward O'Callaghanfee13812009-08-08 14:36:57 +00002128 Actions.ActOnEnumBody(StartLoc, LBraceLoc, RBraceLoc, EnumDecl,
2129 EnumConstantDecls.data(), EnumConstantDecls.size(),
Douglas Gregor23c94db2010-07-02 17:43:08 +00002130 getCurScope(), Attr.get());
Mike Stump1eb44332009-09-09 15:08:12 +00002131
Douglas Gregor72de6672009-01-08 20:45:30 +00002132 EnumScope.Exit();
Douglas Gregor23c94db2010-07-02 17:43:08 +00002133 Actions.ActOnTagFinishDefinition(getCurScope(), EnumDecl, RBraceLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00002134}
2135
2136/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff5f8aa692008-02-11 23:15:56 +00002137/// start of a type-qualifier-list.
2138bool Parser::isTypeQualifier() const {
2139 switch (Tok.getKind()) {
2140 default: return false;
2141 // type-qualifier
2142 case tok::kw_const:
2143 case tok::kw_volatile:
2144 case tok::kw_restrict:
2145 return true;
2146 }
2147}
2148
Chris Lattnerb3a4e432010-02-28 18:18:36 +00002149/// isKnownToBeTypeSpecifier - Return true if we know that the specified token
2150/// is definitely a type-specifier. Return false if it isn't part of a type
2151/// specifier or if we're not sure.
2152bool Parser::isKnownToBeTypeSpecifier(const Token &Tok) const {
2153 switch (Tok.getKind()) {
2154 default: return false;
2155 // type-specifiers
2156 case tok::kw_short:
2157 case tok::kw_long:
2158 case tok::kw_signed:
2159 case tok::kw_unsigned:
2160 case tok::kw__Complex:
2161 case tok::kw__Imaginary:
2162 case tok::kw_void:
2163 case tok::kw_char:
2164 case tok::kw_wchar_t:
2165 case tok::kw_char16_t:
2166 case tok::kw_char32_t:
2167 case tok::kw_int:
2168 case tok::kw_float:
2169 case tok::kw_double:
2170 case tok::kw_bool:
2171 case tok::kw__Bool:
2172 case tok::kw__Decimal32:
2173 case tok::kw__Decimal64:
2174 case tok::kw__Decimal128:
2175 case tok::kw___vector:
2176
2177 // struct-or-union-specifier (C99) or class-specifier (C++)
2178 case tok::kw_class:
2179 case tok::kw_struct:
2180 case tok::kw_union:
2181 // enum-specifier
2182 case tok::kw_enum:
2183
2184 // typedef-name
2185 case tok::annot_typename:
2186 return true;
2187 }
2188}
2189
Steve Naroff5f8aa692008-02-11 23:15:56 +00002190/// isTypeSpecifierQualifier - Return true if the current token could be the
Reid Spencer5f016e22007-07-11 17:01:13 +00002191/// start of a specifier-qualifier-list.
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002192bool Parser::isTypeSpecifierQualifier() {
Reid Spencer5f016e22007-07-11 17:01:13 +00002193 switch (Tok.getKind()) {
2194 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002195
Chris Lattner166a8fc2009-01-04 23:41:41 +00002196 case tok::identifier: // foo::bar
John Thompson82287d12010-02-05 00:12:22 +00002197 if (TryAltiVecVectorToken())
2198 return true;
2199 // Fall through.
Douglas Gregord57959a2009-03-27 23:10:48 +00002200 case tok::kw_typename: // typename T::type
Chris Lattner166a8fc2009-01-04 23:41:41 +00002201 // Annotate typenames and C++ scope specifiers. If we get one, just
2202 // recurse to handle whatever we get.
2203 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00002204 return true;
2205 if (Tok.is(tok::identifier))
2206 return false;
2207 return isTypeSpecifierQualifier();
Douglas Gregord57959a2009-03-27 23:10:48 +00002208
Chris Lattner166a8fc2009-01-04 23:41:41 +00002209 case tok::coloncolon: // ::foo::bar
2210 if (NextToken().is(tok::kw_new) || // ::new
2211 NextToken().is(tok::kw_delete)) // ::delete
2212 return false;
2213
Chris Lattner166a8fc2009-01-04 23:41:41 +00002214 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00002215 return true;
2216 return isTypeSpecifierQualifier();
Mike Stump1eb44332009-09-09 15:08:12 +00002217
Reid Spencer5f016e22007-07-11 17:01:13 +00002218 // GNU attributes support.
2219 case tok::kw___attribute:
Steve Naroffd1861fd2007-07-31 12:34:36 +00002220 // GNU typeof support.
2221 case tok::kw_typeof:
Mike Stump1eb44332009-09-09 15:08:12 +00002222
Reid Spencer5f016e22007-07-11 17:01:13 +00002223 // type-specifiers
2224 case tok::kw_short:
2225 case tok::kw_long:
2226 case tok::kw_signed:
2227 case tok::kw_unsigned:
2228 case tok::kw__Complex:
2229 case tok::kw__Imaginary:
2230 case tok::kw_void:
2231 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00002232 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002233 case tok::kw_char16_t:
2234 case tok::kw_char32_t:
Reid Spencer5f016e22007-07-11 17:01:13 +00002235 case tok::kw_int:
2236 case tok::kw_float:
2237 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00002238 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00002239 case tok::kw__Bool:
2240 case tok::kw__Decimal32:
2241 case tok::kw__Decimal64:
2242 case tok::kw__Decimal128:
John Thompson82287d12010-02-05 00:12:22 +00002243 case tok::kw___vector:
Mike Stump1eb44332009-09-09 15:08:12 +00002244
Chris Lattner99dc9142008-04-13 18:59:07 +00002245 // struct-or-union-specifier (C99) or class-specifier (C++)
2246 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00002247 case tok::kw_struct:
2248 case tok::kw_union:
2249 // enum-specifier
2250 case tok::kw_enum:
Mike Stump1eb44332009-09-09 15:08:12 +00002251
Reid Spencer5f016e22007-07-11 17:01:13 +00002252 // type-qualifier
2253 case tok::kw_const:
2254 case tok::kw_volatile:
2255 case tok::kw_restrict:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002256
2257 // typedef-name
Chris Lattnerb31757b2009-01-06 05:06:21 +00002258 case tok::annot_typename:
Reid Spencer5f016e22007-07-11 17:01:13 +00002259 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00002260
Chris Lattner7c186be2008-10-20 00:25:30 +00002261 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
2262 case tok::less:
2263 return getLang().ObjC1;
Mike Stump1eb44332009-09-09 15:08:12 +00002264
Steve Naroff239f0732008-12-25 14:16:32 +00002265 case tok::kw___cdecl:
2266 case tok::kw___stdcall:
2267 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00002268 case tok::kw___thiscall:
Eli Friedman290eeb02009-06-08 23:27:34 +00002269 case tok::kw___w64:
2270 case tok::kw___ptr64:
2271 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00002272 }
2273}
2274
2275/// isDeclarationSpecifier() - Return true if the current token is part of a
2276/// declaration specifier.
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002277bool Parser::isDeclarationSpecifier() {
Reid Spencer5f016e22007-07-11 17:01:13 +00002278 switch (Tok.getKind()) {
2279 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002280
Chris Lattner166a8fc2009-01-04 23:41:41 +00002281 case tok::identifier: // foo::bar
Steve Naroff61f72cb2009-03-09 21:12:44 +00002282 // Unfortunate hack to support "Class.factoryMethod" notation.
2283 if (getLang().ObjC1 && NextToken().is(tok::period))
2284 return false;
John Thompson82287d12010-02-05 00:12:22 +00002285 if (TryAltiVecVectorToken())
2286 return true;
2287 // Fall through.
Douglas Gregord57959a2009-03-27 23:10:48 +00002288 case tok::kw_typename: // typename T::type
Chris Lattner166a8fc2009-01-04 23:41:41 +00002289 // Annotate typenames and C++ scope specifiers. If we get one, just
2290 // recurse to handle whatever we get.
2291 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00002292 return true;
2293 if (Tok.is(tok::identifier))
2294 return false;
2295 return isDeclarationSpecifier();
2296
Chris Lattner166a8fc2009-01-04 23:41:41 +00002297 case tok::coloncolon: // ::foo::bar
2298 if (NextToken().is(tok::kw_new) || // ::new
2299 NextToken().is(tok::kw_delete)) // ::delete
2300 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002301
Chris Lattner166a8fc2009-01-04 23:41:41 +00002302 // Annotate typenames and C++ scope specifiers. If we get one, just
2303 // recurse to handle whatever we get.
2304 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00002305 return true;
2306 return isDeclarationSpecifier();
Mike Stump1eb44332009-09-09 15:08:12 +00002307
Reid Spencer5f016e22007-07-11 17:01:13 +00002308 // storage-class-specifier
2309 case tok::kw_typedef:
2310 case tok::kw_extern:
Steve Naroff8d54bf22007-12-18 00:16:02 +00002311 case tok::kw___private_extern__:
Reid Spencer5f016e22007-07-11 17:01:13 +00002312 case tok::kw_static:
2313 case tok::kw_auto:
2314 case tok::kw_register:
2315 case tok::kw___thread:
Mike Stump1eb44332009-09-09 15:08:12 +00002316
Reid Spencer5f016e22007-07-11 17:01:13 +00002317 // type-specifiers
2318 case tok::kw_short:
2319 case tok::kw_long:
2320 case tok::kw_signed:
2321 case tok::kw_unsigned:
2322 case tok::kw__Complex:
2323 case tok::kw__Imaginary:
2324 case tok::kw_void:
2325 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00002326 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002327 case tok::kw_char16_t:
2328 case tok::kw_char32_t:
2329
Reid Spencer5f016e22007-07-11 17:01:13 +00002330 case tok::kw_int:
2331 case tok::kw_float:
2332 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00002333 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00002334 case tok::kw__Bool:
2335 case tok::kw__Decimal32:
2336 case tok::kw__Decimal64:
2337 case tok::kw__Decimal128:
John Thompson82287d12010-02-05 00:12:22 +00002338 case tok::kw___vector:
Mike Stump1eb44332009-09-09 15:08:12 +00002339
Chris Lattner99dc9142008-04-13 18:59:07 +00002340 // struct-or-union-specifier (C99) or class-specifier (C++)
2341 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00002342 case tok::kw_struct:
2343 case tok::kw_union:
2344 // enum-specifier
2345 case tok::kw_enum:
Mike Stump1eb44332009-09-09 15:08:12 +00002346
Reid Spencer5f016e22007-07-11 17:01:13 +00002347 // type-qualifier
2348 case tok::kw_const:
2349 case tok::kw_volatile:
2350 case tok::kw_restrict:
Steve Naroffd1861fd2007-07-31 12:34:36 +00002351
Reid Spencer5f016e22007-07-11 17:01:13 +00002352 // function-specifier
2353 case tok::kw_inline:
Douglas Gregorb48fe382008-10-31 09:07:45 +00002354 case tok::kw_virtual:
2355 case tok::kw_explicit:
Chris Lattnerd6c7c182007-08-09 16:40:21 +00002356
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002357 // typedef-name
Chris Lattnerb31757b2009-01-06 05:06:21 +00002358 case tok::annot_typename:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002359
Chris Lattner1ef08762007-08-09 17:01:07 +00002360 // GNU typeof support.
2361 case tok::kw_typeof:
Mike Stump1eb44332009-09-09 15:08:12 +00002362
Chris Lattner1ef08762007-08-09 17:01:07 +00002363 // GNU attributes.
Chris Lattnerd6c7c182007-08-09 16:40:21 +00002364 case tok::kw___attribute:
Reid Spencer5f016e22007-07-11 17:01:13 +00002365 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00002366
Chris Lattnerf3948c42008-07-26 03:38:44 +00002367 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
2368 case tok::less:
2369 return getLang().ObjC1;
Mike Stump1eb44332009-09-09 15:08:12 +00002370
Steve Naroff47f52092009-01-06 19:34:12 +00002371 case tok::kw___declspec:
Steve Naroff239f0732008-12-25 14:16:32 +00002372 case tok::kw___cdecl:
2373 case tok::kw___stdcall:
2374 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00002375 case tok::kw___thiscall:
Eli Friedman290eeb02009-06-08 23:27:34 +00002376 case tok::kw___w64:
2377 case tok::kw___ptr64:
2378 case tok::kw___forceinline:
2379 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00002380 }
2381}
2382
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002383bool Parser::isConstructorDeclarator() {
2384 TentativeParsingAction TPA(*this);
2385
2386 // Parse the C++ scope specifier.
2387 CXXScopeSpec SS;
John McCallb3d87482010-08-24 05:47:05 +00002388 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), true)) {
John McCall9ba61662010-02-26 08:45:28 +00002389 TPA.Revert();
2390 return false;
2391 }
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002392
2393 // Parse the constructor name.
2394 if (Tok.is(tok::identifier) || Tok.is(tok::annot_template_id)) {
2395 // We already know that we have a constructor name; just consume
2396 // the token.
2397 ConsumeToken();
2398 } else {
2399 TPA.Revert();
2400 return false;
2401 }
2402
2403 // Current class name must be followed by a left parentheses.
2404 if (Tok.isNot(tok::l_paren)) {
2405 TPA.Revert();
2406 return false;
2407 }
2408 ConsumeParen();
2409
2410 // A right parentheses or ellipsis signals that we have a constructor.
2411 if (Tok.is(tok::r_paren) || Tok.is(tok::ellipsis)) {
2412 TPA.Revert();
2413 return true;
2414 }
2415
2416 // If we need to, enter the specified scope.
2417 DeclaratorScopeObj DeclScopeObj(*this, SS);
Douglas Gregor23c94db2010-07-02 17:43:08 +00002418 if (SS.isSet() && Actions.ShouldEnterDeclaratorScope(getCurScope(), SS))
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002419 DeclScopeObj.EnterDeclaratorScope();
2420
2421 // Check whether the next token(s) are part of a declaration
2422 // specifier, in which case we have the start of a parameter and,
2423 // therefore, we know that this is a constructor.
2424 bool IsConstructor = isDeclarationSpecifier();
2425 TPA.Revert();
2426 return IsConstructor;
2427}
Reid Spencer5f016e22007-07-11 17:01:13 +00002428
2429/// ParseTypeQualifierListOpt
2430/// type-qualifier-list: [C99 6.7.5]
2431/// type-qualifier
Chris Lattner5a69d1c2008-12-18 07:02:59 +00002432/// [GNU] attributes [ only if AttributesAllowed=true ]
Reid Spencer5f016e22007-07-11 17:01:13 +00002433/// type-qualifier-list type-qualifier
Chris Lattner5a69d1c2008-12-18 07:02:59 +00002434/// [GNU] type-qualifier-list attributes [ only if AttributesAllowed=true ]
Sean Huntbbd37c62009-11-21 08:43:09 +00002435/// [C++0x] attribute-specifier[opt] is allowed before cv-qualifier-seq
2436/// if CXX0XAttributesAllowed = true
Reid Spencer5f016e22007-07-11 17:01:13 +00002437///
Sean Huntbbd37c62009-11-21 08:43:09 +00002438void Parser::ParseTypeQualifierListOpt(DeclSpec &DS, bool GNUAttributesAllowed,
2439 bool CXX0XAttributesAllowed) {
2440 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier()) {
2441 SourceLocation Loc = Tok.getLocation();
2442 CXX0XAttributeList Attr = ParseCXX0XAttributes();
2443 if (CXX0XAttributesAllowed)
2444 DS.AddAttributes(Attr.AttrList);
2445 else
2446 Diag(Loc, diag::err_attributes_not_allowed);
2447 }
2448
Reid Spencer5f016e22007-07-11 17:01:13 +00002449 while (1) {
John McCallfec54012009-08-03 20:12:06 +00002450 bool isInvalid = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00002451 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00002452 unsigned DiagID = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00002453 SourceLocation Loc = Tok.getLocation();
2454
2455 switch (Tok.getKind()) {
Douglas Gregor1a480c42010-08-27 17:35:51 +00002456 case tok::code_completion:
2457 Actions.CodeCompleteTypeQualifiers(DS);
2458 ConsumeCodeCompletionToken();
2459 break;
2460
Reid Spencer5f016e22007-07-11 17:01:13 +00002461 case tok::kw_const:
John McCallfec54012009-08-03 20:12:06 +00002462 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, DiagID,
2463 getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00002464 break;
2465 case tok::kw_volatile:
John McCallfec54012009-08-03 20:12:06 +00002466 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
2467 getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00002468 break;
2469 case tok::kw_restrict:
John McCallfec54012009-08-03 20:12:06 +00002470 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
2471 getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00002472 break;
Eli Friedman290eeb02009-06-08 23:27:34 +00002473 case tok::kw___w64:
Steve Naroff86bc6cf2008-12-25 14:41:26 +00002474 case tok::kw___ptr64:
Steve Naroff239f0732008-12-25 14:16:32 +00002475 case tok::kw___cdecl:
2476 case tok::kw___stdcall:
2477 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00002478 case tok::kw___thiscall:
Sean Huntbbd37c62009-11-21 08:43:09 +00002479 if (GNUAttributesAllowed) {
Eli Friedman290eeb02009-06-08 23:27:34 +00002480 DS.AddAttributes(ParseMicrosoftTypeAttributes());
2481 continue;
2482 }
2483 goto DoneWithTypeQuals;
Reid Spencer5f016e22007-07-11 17:01:13 +00002484 case tok::kw___attribute:
Sean Huntbbd37c62009-11-21 08:43:09 +00002485 if (GNUAttributesAllowed) {
2486 DS.AddAttributes(ParseGNUAttributes());
Chris Lattner5a69d1c2008-12-18 07:02:59 +00002487 continue; // do *not* consume the next token!
2488 }
2489 // otherwise, FALL THROUGH!
2490 default:
Steve Naroff239f0732008-12-25 14:16:32 +00002491 DoneWithTypeQuals:
Chris Lattner5a69d1c2008-12-18 07:02:59 +00002492 // If this is not a type-qualifier token, we're done reading type
2493 // qualifiers. First verify that DeclSpec's are consistent.
Douglas Gregor9b3064b2009-04-01 22:41:11 +00002494 DS.Finish(Diags, PP);
Chris Lattner5a69d1c2008-12-18 07:02:59 +00002495 return;
Reid Spencer5f016e22007-07-11 17:01:13 +00002496 }
Chris Lattnera1fcbad2008-12-18 06:50:14 +00002497
Reid Spencer5f016e22007-07-11 17:01:13 +00002498 // If the specifier combination wasn't legal, issue a diagnostic.
2499 if (isInvalid) {
2500 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner1ab3b962008-11-18 07:48:38 +00002501 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00002502 }
2503 ConsumeToken();
2504 }
2505}
2506
2507
2508/// ParseDeclarator - Parse and verify a newly-initialized declarator.
2509///
2510void Parser::ParseDeclarator(Declarator &D) {
2511 /// This implements the 'declarator' production in the C grammar, then checks
2512 /// for well-formedness and issues diagnostics.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002513 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Reid Spencer5f016e22007-07-11 17:01:13 +00002514}
2515
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002516/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
2517/// is parsed by the function passed to it. Pass null, and the direct-declarator
2518/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregor2f1bc522008-11-07 20:08:42 +00002519/// ptr-operator production.
2520///
Sebastian Redlf30208a2009-01-24 21:16:55 +00002521/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
2522/// [C] pointer[opt] direct-declarator
2523/// [C++] direct-declarator
2524/// [C++] ptr-operator declarator
Reid Spencer5f016e22007-07-11 17:01:13 +00002525///
2526/// pointer: [C99 6.7.5]
2527/// '*' type-qualifier-list[opt]
2528/// '*' type-qualifier-list[opt] pointer
2529///
Douglas Gregor2f1bc522008-11-07 20:08:42 +00002530/// ptr-operator:
2531/// '*' cv-qualifier-seq[opt]
2532/// '&'
Sebastian Redl05532f22009-03-15 22:02:01 +00002533/// [C++0x] '&&'
Douglas Gregor2f1bc522008-11-07 20:08:42 +00002534/// [GNU] '&' restrict[opt] attributes[opt]
Sebastian Redl05532f22009-03-15 22:02:01 +00002535/// [GNU?] '&&' restrict[opt] attributes[opt]
Sebastian Redlf30208a2009-01-24 21:16:55 +00002536/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002537void Parser::ParseDeclaratorInternal(Declarator &D,
2538 DirectDeclParseFunction DirectDeclParser) {
Douglas Gregor91a28862009-08-26 14:27:30 +00002539 if (Diags.hasAllExtensionsSilenced())
2540 D.setExtension();
Douglas Gregor2ccccb32010-08-23 18:23:48 +00002541
Sebastian Redlf30208a2009-01-24 21:16:55 +00002542 // C++ member pointers start with a '::' or a nested-name.
2543 // Member pointers get special handling, since there's no place for the
2544 // scope spec in the generic path below.
Chris Lattnerf919bfe2009-03-24 17:04:48 +00002545 if (getLang().CPlusPlus &&
2546 (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) ||
2547 Tok.is(tok::annot_cxxscope))) {
Sebastian Redlf30208a2009-01-24 21:16:55 +00002548 CXXScopeSpec SS;
John McCallb3d87482010-08-24 05:47:05 +00002549 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), true); // ignore fail
John McCall9ba61662010-02-26 08:45:28 +00002550
Jeffrey Yasskinedc28772010-04-07 23:29:58 +00002551 if (SS.isNotEmpty()) {
Mike Stump1eb44332009-09-09 15:08:12 +00002552 if (Tok.isNot(tok::star)) {
Sebastian Redlf30208a2009-01-24 21:16:55 +00002553 // The scope spec really belongs to the direct-declarator.
2554 D.getCXXScopeSpec() = SS;
2555 if (DirectDeclParser)
2556 (this->*DirectDeclParser)(D);
2557 return;
2558 }
2559
2560 SourceLocation Loc = ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00002561 D.SetRangeEnd(Loc);
Sebastian Redlf30208a2009-01-24 21:16:55 +00002562 DeclSpec DS;
2563 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002564 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00002565
2566 // Recurse to parse whatever is left.
2567 ParseDeclaratorInternal(D, DirectDeclParser);
2568
2569 // Sema will have to catch (syntactically invalid) pointers into global
2570 // scope. It has to catch pointers into namespace scope anyway.
2571 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00002572 Loc, DS.TakeAttributes()),
2573 /* Don't replace range end. */SourceLocation());
Sebastian Redlf30208a2009-01-24 21:16:55 +00002574 return;
2575 }
2576 }
2577
2578 tok::TokenKind Kind = Tok.getKind();
Steve Naroff5618bd42008-08-27 16:04:49 +00002579 // Not a pointer, C++ reference, or block.
Chris Lattner9af55002009-03-27 04:18:06 +00002580 if (Kind != tok::star && Kind != tok::caret &&
Chris Lattnerf919bfe2009-03-24 17:04:48 +00002581 (Kind != tok::amp || !getLang().CPlusPlus) &&
Sebastian Redl743de1f2009-03-23 00:00:23 +00002582 // We parse rvalue refs in C++03, because otherwise the errors are scary.
Chris Lattner9af55002009-03-27 04:18:06 +00002583 (Kind != tok::ampamp || !getLang().CPlusPlus)) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002584 if (DirectDeclParser)
2585 (this->*DirectDeclParser)(D);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00002586 return;
2587 }
Sebastian Redlf30208a2009-01-24 21:16:55 +00002588
Sebastian Redl05532f22009-03-15 22:02:01 +00002589 // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference,
2590 // '&&' -> rvalue reference
Sebastian Redl743de1f2009-03-23 00:00:23 +00002591 SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&.
Sebastian Redlab197ba2009-02-09 18:23:29 +00002592 D.SetRangeEnd(Loc);
Reid Spencer5f016e22007-07-11 17:01:13 +00002593
Chris Lattner9af55002009-03-27 04:18:06 +00002594 if (Kind == tok::star || Kind == tok::caret) {
Chris Lattner76549142008-02-21 01:32:26 +00002595 // Is a pointer.
Reid Spencer5f016e22007-07-11 17:01:13 +00002596 DeclSpec DS;
Sebastian Redlf30208a2009-01-24 21:16:55 +00002597
Reid Spencer5f016e22007-07-11 17:01:13 +00002598 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002599 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00002600
Reid Spencer5f016e22007-07-11 17:01:13 +00002601 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002602 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroff5618bd42008-08-27 16:04:49 +00002603 if (Kind == tok::star)
2604 // Remember that we parsed a pointer type, and remember the type-quals.
2605 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
Sebastian Redlab197ba2009-02-09 18:23:29 +00002606 DS.TakeAttributes()),
2607 SourceLocation());
Steve Naroff5618bd42008-08-27 16:04:49 +00002608 else
2609 // Remember that we parsed a Block type, and remember the type-quals.
Mike Stump1eb44332009-09-09 15:08:12 +00002610 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
Mike Stump75b163f2009-04-21 00:51:43 +00002611 Loc, DS.TakeAttributes()),
Sebastian Redlab197ba2009-02-09 18:23:29 +00002612 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00002613 } else {
2614 // Is a reference
2615 DeclSpec DS;
2616
Sebastian Redl743de1f2009-03-23 00:00:23 +00002617 // Complain about rvalue references in C++03, but then go on and build
2618 // the declarator.
2619 if (Kind == tok::ampamp && !getLang().CPlusPlus0x)
2620 Diag(Loc, diag::err_rvalue_reference);
2621
Reid Spencer5f016e22007-07-11 17:01:13 +00002622 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
2623 // cv-qualifiers are introduced through the use of a typedef or of a
2624 // template type argument, in which case the cv-qualifiers are ignored.
2625 //
2626 // [GNU] Retricted references are allowed.
2627 // [GNU] Attributes on references are allowed.
Sean Huntbbd37c62009-11-21 08:43:09 +00002628 // [C++0x] Attributes on references are not allowed.
2629 ParseTypeQualifierListOpt(DS, true, false);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002630 D.ExtendWithDeclSpec(DS);
Reid Spencer5f016e22007-07-11 17:01:13 +00002631
2632 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
2633 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
2634 Diag(DS.getConstSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00002635 diag::err_invalid_reference_qualifier_application) << "const";
Reid Spencer5f016e22007-07-11 17:01:13 +00002636 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
2637 Diag(DS.getVolatileSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00002638 diag::err_invalid_reference_qualifier_application) << "volatile";
Reid Spencer5f016e22007-07-11 17:01:13 +00002639 }
2640
2641 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002642 ParseDeclaratorInternal(D, DirectDeclParser);
Reid Spencer5f016e22007-07-11 17:01:13 +00002643
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00002644 if (D.getNumTypeObjects() > 0) {
2645 // C++ [dcl.ref]p4: There shall be no references to references.
2646 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
2647 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattnerda83bac2008-11-19 07:37:42 +00002648 if (const IdentifierInfo *II = D.getIdentifier())
2649 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
2650 << II;
2651 else
2652 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
2653 << "type name";
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00002654
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002655 // Once we've complained about the reference-to-reference, we
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00002656 // can go ahead and build the (technically ill-formed)
2657 // declarator: reference collapsing will take care of it.
2658 }
2659 }
2660
Reid Spencer5f016e22007-07-11 17:01:13 +00002661 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner76549142008-02-21 01:32:26 +00002662 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
Sebastian Redl05532f22009-03-15 22:02:01 +00002663 DS.TakeAttributes(),
2664 Kind == tok::amp),
Sebastian Redlab197ba2009-02-09 18:23:29 +00002665 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00002666 }
2667}
2668
2669/// ParseDirectDeclarator
2670/// direct-declarator: [C99 6.7.5]
Douglas Gregor42a552f2008-11-05 20:51:48 +00002671/// [C99] identifier
Reid Spencer5f016e22007-07-11 17:01:13 +00002672/// '(' declarator ')'
2673/// [GNU] '(' attributes declarator ')'
2674/// [C90] direct-declarator '[' constant-expression[opt] ']'
2675/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
2676/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
2677/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
2678/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
2679/// direct-declarator '(' parameter-type-list ')'
2680/// direct-declarator '(' identifier-list[opt] ')'
2681/// [GNU] direct-declarator '(' parameter-forward-declarations
2682/// parameter-type-list[opt] ')'
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002683/// [C++] direct-declarator '(' parameter-declaration-clause ')'
2684/// cv-qualifier-seq[opt] exception-specification[opt]
Douglas Gregorb48fe382008-10-31 09:07:45 +00002685/// [C++] declarator-id
Douglas Gregor42a552f2008-11-05 20:51:48 +00002686///
2687/// declarator-id: [C++ 8]
2688/// id-expression
2689/// '::'[opt] nested-name-specifier[opt] type-name
2690///
2691/// id-expression: [C++ 5.1]
2692/// unqualified-id
Douglas Gregordb422df2009-09-25 21:45:23 +00002693/// qualified-id
Douglas Gregor42a552f2008-11-05 20:51:48 +00002694///
2695/// unqualified-id: [C++ 5.1]
Mike Stump1eb44332009-09-09 15:08:12 +00002696/// identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002697/// operator-function-id
Douglas Gregordb422df2009-09-25 21:45:23 +00002698/// conversion-function-id
Mike Stump1eb44332009-09-09 15:08:12 +00002699/// '~' class-name
Douglas Gregor39a8de12009-02-25 19:37:18 +00002700/// template-id
Argyrios Kyrtzidisc7ed9c62008-11-07 22:02:30 +00002701///
Reid Spencer5f016e22007-07-11 17:01:13 +00002702void Parser::ParseDirectDeclarator(Declarator &D) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002703 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002704
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002705 if (getLang().CPlusPlus && D.mayHaveIdentifier()) {
2706 // ParseDeclaratorInternal might already have parsed the scope.
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00002707 if (D.getCXXScopeSpec().isEmpty()) {
John McCallb3d87482010-08-24 05:47:05 +00002708 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec(), ParsedType(), true);
John McCall9ba61662010-02-26 08:45:28 +00002709 }
2710
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00002711 if (D.getCXXScopeSpec().isValid()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00002712 if (Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
John McCalle7e278b2009-12-11 20:04:54 +00002713 // Change the declaration context for name lookup, until this function
2714 // is exited (and the declarator has been parsed).
2715 DeclScopeObj.EnterDeclaratorScope();
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00002716 }
2717
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002718 if (Tok.is(tok::identifier) || Tok.is(tok::kw_operator) ||
2719 Tok.is(tok::annot_template_id) || Tok.is(tok::tilde)) {
2720 // We found something that indicates the start of an unqualified-id.
2721 // Parse that unqualified-id.
John McCallba9d8532010-04-13 06:39:49 +00002722 bool AllowConstructorName;
2723 if (D.getDeclSpec().hasTypeSpecifier())
2724 AllowConstructorName = false;
2725 else if (D.getCXXScopeSpec().isSet())
2726 AllowConstructorName =
2727 (D.getContext() == Declarator::FileContext ||
2728 (D.getContext() == Declarator::MemberContext &&
2729 D.getDeclSpec().isFriendSpecified()));
2730 else
2731 AllowConstructorName = (D.getContext() == Declarator::MemberContext);
2732
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002733 if (ParseUnqualifiedId(D.getCXXScopeSpec(),
2734 /*EnteringContext=*/true,
2735 /*AllowDestructorName=*/true,
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002736 AllowConstructorName,
John McCallb3d87482010-08-24 05:47:05 +00002737 ParsedType(),
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00002738 D.getName()) ||
2739 // Once we're past the identifier, if the scope was bad, mark the
2740 // whole declarator bad.
2741 D.getCXXScopeSpec().isInvalid()) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002742 D.SetIdentifier(0, Tok.getLocation());
2743 D.setInvalidType(true);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002744 } else {
2745 // Parsed the unqualified-id; update range information and move along.
2746 if (D.getSourceRange().getBegin().isInvalid())
2747 D.SetRangeBegin(D.getName().getSourceRange().getBegin());
2748 D.SetRangeEnd(D.getName().getSourceRange().getEnd());
Douglas Gregor2f1bc522008-11-07 20:08:42 +00002749 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002750 goto PastIdentifier;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00002751 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002752 } else if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002753 assert(!getLang().CPlusPlus &&
2754 "There's a C++-specific check for tok::identifier above");
2755 assert(Tok.getIdentifierInfo() && "Not an identifier?");
2756 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
2757 ConsumeToken();
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002758 goto PastIdentifier;
2759 }
2760
2761 if (Tok.is(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002762 // direct-declarator: '(' declarator ')'
2763 // direct-declarator: '(' attributes declarator ')'
2764 // Example: 'char (*X)' or 'int (*XX)(void)'
2765 ParseParenDeclarator(D);
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002766
2767 // If the declarator was parenthesized, we entered the declarator
2768 // scope when parsing the parenthesized declarator, then exited
2769 // the scope already. Re-enter the scope, if we need to.
2770 if (D.getCXXScopeSpec().isSet()) {
Fariborz Jahanian46877cd2010-08-17 23:50:37 +00002771 // If there was an error parsing parenthesized declarator, declarator
2772 // scope may have been enterred before. Don't do it again.
2773 if (!D.isInvalidType() &&
2774 Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002775 // Change the declaration context for name lookup, until this function
2776 // is exited (and the declarator has been parsed).
Fariborz Jahanian46877cd2010-08-17 23:50:37 +00002777 DeclScopeObj.EnterDeclaratorScope();
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002778 }
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002779 } else if (D.mayOmitIdentifier()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002780 // This could be something simple like "int" (in which case the declarator
2781 // portion is empty), if an abstract-declarator is allowed.
2782 D.SetIdentifier(0, Tok.getLocation());
2783 } else {
Douglas Gregore950d4b2009-03-06 23:28:18 +00002784 if (D.getContext() == Declarator::MemberContext)
2785 Diag(Tok, diag::err_expected_member_name_or_semi)
2786 << D.getDeclSpec().getSourceRange();
2787 else if (getLang().CPlusPlus)
Douglas Gregor2d1c2142009-11-03 19:44:04 +00002788 Diag(Tok, diag::err_expected_unqualified_id) << getLang().CPlusPlus;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002789 else
Chris Lattner1ab3b962008-11-18 07:48:38 +00002790 Diag(Tok, diag::err_expected_ident_lparen);
Reid Spencer5f016e22007-07-11 17:01:13 +00002791 D.SetIdentifier(0, Tok.getLocation());
Chris Lattner1f6f54b2008-11-11 06:13:16 +00002792 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00002793 }
Mike Stump1eb44332009-09-09 15:08:12 +00002794
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002795 PastIdentifier:
Reid Spencer5f016e22007-07-11 17:01:13 +00002796 assert(D.isPastIdentifier() &&
2797 "Haven't past the location of the identifier yet?");
Mike Stump1eb44332009-09-09 15:08:12 +00002798
Sean Huntbbd37c62009-11-21 08:43:09 +00002799 // Don't parse attributes unless we have an identifier.
Douglas Gregor3c3aaf92010-02-19 16:47:56 +00002800 if (D.getIdentifier() && getLang().CPlusPlus0x
Sean Huntbbd37c62009-11-21 08:43:09 +00002801 && isCXX0XAttributeSpecifier(true)) {
2802 SourceLocation AttrEndLoc;
2803 CXX0XAttributeList Attr = ParseCXX0XAttributes();
2804 D.AddAttributes(Attr.AttrList, AttrEndLoc);
2805 }
2806
Reid Spencer5f016e22007-07-11 17:01:13 +00002807 while (1) {
Chris Lattner04d66662007-10-09 17:33:22 +00002808 if (Tok.is(tok::l_paren)) {
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00002809 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
2810 // In such a case, check if we actually have a function declarator; if it
2811 // is not, the declarator has been fully parsed.
Chris Lattner7399ee02008-10-20 02:05:46 +00002812 if (getLang().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
2813 // When not in file scope, warn for ambiguous function declarators, just
2814 // in case the author intended it as a variable definition.
2815 bool warnIfAmbiguous = D.getContext() != Declarator::FileContext;
2816 if (!isCXXFunctionDeclarator(warnIfAmbiguous))
2817 break;
2818 }
Chris Lattneref4715c2008-04-06 05:45:57 +00002819 ParseFunctionDeclarator(ConsumeParen(), D);
Chris Lattner04d66662007-10-09 17:33:22 +00002820 } else if (Tok.is(tok::l_square)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002821 ParseBracketDeclarator(D);
2822 } else {
2823 break;
2824 }
2825 }
2826}
2827
Chris Lattneref4715c2008-04-06 05:45:57 +00002828/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
2829/// only called before the identifier, so these are most likely just grouping
Mike Stump1eb44332009-09-09 15:08:12 +00002830/// parens for precedence. If we find that these are actually function
Chris Lattneref4715c2008-04-06 05:45:57 +00002831/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
2832///
2833/// direct-declarator:
2834/// '(' declarator ')'
2835/// [GNU] '(' attributes declarator ')'
Chris Lattner7399ee02008-10-20 02:05:46 +00002836/// direct-declarator '(' parameter-type-list ')'
2837/// direct-declarator '(' identifier-list[opt] ')'
2838/// [GNU] direct-declarator '(' parameter-forward-declarations
2839/// parameter-type-list[opt] ')'
Chris Lattneref4715c2008-04-06 05:45:57 +00002840///
2841void Parser::ParseParenDeclarator(Declarator &D) {
2842 SourceLocation StartLoc = ConsumeParen();
2843 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
Mike Stump1eb44332009-09-09 15:08:12 +00002844
Chris Lattner7399ee02008-10-20 02:05:46 +00002845 // Eat any attributes before we look at whether this is a grouping or function
2846 // declarator paren. If this is a grouping paren, the attribute applies to
2847 // the type being built up, for example:
2848 // int (__attribute__(()) *x)(long y)
2849 // If this ends up not being a grouping paren, the attribute applies to the
2850 // first argument, for example:
2851 // int (__attribute__(()) int x)
2852 // In either case, we need to eat any attributes to be able to determine what
2853 // sort of paren this is.
2854 //
Ted Kremenek1e377652010-02-11 02:19:13 +00002855 llvm::OwningPtr<AttributeList> AttrList;
Chris Lattner7399ee02008-10-20 02:05:46 +00002856 bool RequiresArg = false;
2857 if (Tok.is(tok::kw___attribute)) {
Ted Kremenek1e377652010-02-11 02:19:13 +00002858 AttrList.reset(ParseGNUAttributes());
Mike Stump1eb44332009-09-09 15:08:12 +00002859
Chris Lattner7399ee02008-10-20 02:05:46 +00002860 // We require that the argument list (if this is a non-grouping paren) be
2861 // present even if the attribute list was empty.
2862 RequiresArg = true;
2863 }
Steve Naroff239f0732008-12-25 14:16:32 +00002864 // Eat any Microsoft extensions.
Eli Friedman290eeb02009-06-08 23:27:34 +00002865 if (Tok.is(tok::kw___cdecl) || Tok.is(tok::kw___stdcall) ||
Douglas Gregorf813a2c2010-05-18 16:57:00 +00002866 Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___fastcall) ||
2867 Tok.is(tok::kw___w64) || Tok.is(tok::kw___ptr64)) {
Ted Kremenek1e377652010-02-11 02:19:13 +00002868 AttrList.reset(ParseMicrosoftTypeAttributes(AttrList.take()));
Eli Friedman290eeb02009-06-08 23:27:34 +00002869 }
Mike Stump1eb44332009-09-09 15:08:12 +00002870
Chris Lattneref4715c2008-04-06 05:45:57 +00002871 // If we haven't past the identifier yet (or where the identifier would be
2872 // stored, if this is an abstract declarator), then this is probably just
2873 // grouping parens. However, if this could be an abstract-declarator, then
2874 // this could also be the start of function arguments (consider 'void()').
2875 bool isGrouping;
Mike Stump1eb44332009-09-09 15:08:12 +00002876
Chris Lattneref4715c2008-04-06 05:45:57 +00002877 if (!D.mayOmitIdentifier()) {
2878 // If this can't be an abstract-declarator, this *must* be a grouping
2879 // paren, because we haven't seen the identifier yet.
2880 isGrouping = true;
2881 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Argyrios Kyrtzidise25d2702008-10-06 00:07:55 +00002882 (getLang().CPlusPlus && Tok.is(tok::ellipsis)) || // C++ int(...)
Chris Lattneref4715c2008-04-06 05:45:57 +00002883 isDeclarationSpecifier()) { // 'int(int)' is a function.
2884 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
2885 // considered to be a type, not a K&R identifier-list.
2886 isGrouping = false;
2887 } else {
2888 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
2889 isGrouping = true;
2890 }
Mike Stump1eb44332009-09-09 15:08:12 +00002891
Chris Lattneref4715c2008-04-06 05:45:57 +00002892 // If this is a grouping paren, handle:
2893 // direct-declarator: '(' declarator ')'
2894 // direct-declarator: '(' attributes declarator ')'
2895 if (isGrouping) {
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00002896 bool hadGroupingParens = D.hasGroupingParens();
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00002897 D.setGroupingParens(true);
Chris Lattner7399ee02008-10-20 02:05:46 +00002898 if (AttrList)
Ted Kremenek1e377652010-02-11 02:19:13 +00002899 D.AddAttributes(AttrList.take(), SourceLocation());
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00002900
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002901 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattneref4715c2008-04-06 05:45:57 +00002902 // Match the ')'.
Sebastian Redlab197ba2009-02-09 18:23:29 +00002903 SourceLocation Loc = MatchRHSPunctuation(tok::r_paren, StartLoc);
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00002904
2905 D.setGroupingParens(hadGroupingParens);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002906 D.SetRangeEnd(Loc);
Chris Lattneref4715c2008-04-06 05:45:57 +00002907 return;
2908 }
Mike Stump1eb44332009-09-09 15:08:12 +00002909
Chris Lattneref4715c2008-04-06 05:45:57 +00002910 // Okay, if this wasn't a grouping paren, it must be the start of a function
2911 // argument list. Recognize that this declarator will never have an
Chris Lattner7399ee02008-10-20 02:05:46 +00002912 // identifier (and remember where it would have been), then call into
2913 // ParseFunctionDeclarator to handle of argument list.
Chris Lattneref4715c2008-04-06 05:45:57 +00002914 D.SetIdentifier(0, Tok.getLocation());
2915
Ted Kremenek1e377652010-02-11 02:19:13 +00002916 ParseFunctionDeclarator(StartLoc, D, AttrList.take(), RequiresArg);
Chris Lattneref4715c2008-04-06 05:45:57 +00002917}
2918
2919/// ParseFunctionDeclarator - We are after the identifier and have parsed the
2920/// declarator D up to a paren, which indicates that we are parsing function
2921/// arguments.
Reid Spencer5f016e22007-07-11 17:01:13 +00002922///
Chris Lattner7399ee02008-10-20 02:05:46 +00002923/// If AttrList is non-null, then the caller parsed those arguments immediately
2924/// after the open paren - they should be considered to be the first argument of
2925/// a parameter. If RequiresArg is true, then the first argument of the
2926/// function is required to be present and required to not be an identifier
2927/// list.
2928///
Reid Spencer5f016e22007-07-11 17:01:13 +00002929/// This method also handles this portion of the grammar:
2930/// parameter-type-list: [C99 6.7.5]
2931/// parameter-list
2932/// parameter-list ',' '...'
Douglas Gregored5d6512009-09-22 21:41:40 +00002933/// [C++] parameter-list '...'
Reid Spencer5f016e22007-07-11 17:01:13 +00002934///
2935/// parameter-list: [C99 6.7.5]
2936/// parameter-declaration
2937/// parameter-list ',' parameter-declaration
2938///
2939/// parameter-declaration: [C99 6.7.5]
2940/// declaration-specifiers declarator
Chris Lattner04421082008-04-08 04:40:51 +00002941/// [C++] declaration-specifiers declarator '=' assignment-expression
Reid Spencer5f016e22007-07-11 17:01:13 +00002942/// [GNU] declaration-specifiers declarator attributes
Sebastian Redl50de12f2009-03-24 22:27:57 +00002943/// declaration-specifiers abstract-declarator[opt]
2944/// [C++] declaration-specifiers abstract-declarator[opt]
Chris Lattner8123a952008-04-10 02:22:51 +00002945/// '=' assignment-expression
Reid Spencer5f016e22007-07-11 17:01:13 +00002946/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
2947///
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002948/// For C++, after the parameter-list, it also parses "cv-qualifier-seq[opt]"
Sebastian Redl50de12f2009-03-24 22:27:57 +00002949/// and "exception-specification[opt]".
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002950///
Chris Lattner7399ee02008-10-20 02:05:46 +00002951void Parser::ParseFunctionDeclarator(SourceLocation LParenLoc, Declarator &D,
2952 AttributeList *AttrList,
2953 bool RequiresArg) {
Chris Lattneref4715c2008-04-06 05:45:57 +00002954 // lparen is already consumed!
2955 assert(D.isPastIdentifier() && "Should not call before identifier!");
Mike Stump1eb44332009-09-09 15:08:12 +00002956
Chris Lattner7399ee02008-10-20 02:05:46 +00002957 // This parameter list may be empty.
Chris Lattner04d66662007-10-09 17:33:22 +00002958 if (Tok.is(tok::r_paren)) {
Chris Lattner7399ee02008-10-20 02:05:46 +00002959 if (RequiresArg) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00002960 Diag(Tok, diag::err_argument_required_after_attribute);
Chris Lattner7399ee02008-10-20 02:05:46 +00002961 delete AttrList;
2962 }
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002963
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00002964 SourceLocation RParenLoc = ConsumeParen(); // Eat the closing ')'.
2965 SourceLocation EndLoc = RParenLoc;
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002966
2967 // cv-qualifier-seq[opt].
2968 DeclSpec DS;
Sebastian Redl7dc81342009-04-29 17:30:04 +00002969 bool hasExceptionSpec = false;
Sebastian Redl3cc97262009-05-31 11:47:27 +00002970 SourceLocation ThrowLoc;
Sebastian Redl7dc81342009-04-29 17:30:04 +00002971 bool hasAnyExceptionSpec = false;
John McCallb3d87482010-08-24 05:47:05 +00002972 llvm::SmallVector<ParsedType, 2> Exceptions;
Sebastian Redlef65f062009-05-29 18:02:33 +00002973 llvm::SmallVector<SourceRange, 2> ExceptionRanges;
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002974 if (getLang().CPlusPlus) {
Chris Lattner5a69d1c2008-12-18 07:02:59 +00002975 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002976 if (!DS.getSourceRange().getEnd().isInvalid())
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00002977 EndLoc = DS.getSourceRange().getEnd();
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002978
2979 // Parse exception-specification[opt].
Sebastian Redl7dc81342009-04-29 17:30:04 +00002980 if (Tok.is(tok::kw_throw)) {
2981 hasExceptionSpec = true;
Sebastian Redl3cc97262009-05-31 11:47:27 +00002982 ThrowLoc = Tok.getLocation();
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00002983 ParseExceptionSpecification(EndLoc, Exceptions, ExceptionRanges,
Sebastian Redlef65f062009-05-29 18:02:33 +00002984 hasAnyExceptionSpec);
2985 assert(Exceptions.size() == ExceptionRanges.size() &&
2986 "Produced different number of exception types and ranges.");
Sebastian Redl7dc81342009-04-29 17:30:04 +00002987 }
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002988 }
2989
Chris Lattnerf97409f2008-04-06 06:57:35 +00002990 // Remember that we parsed a function type, and remember the attributes.
Reid Spencer5f016e22007-07-11 17:01:13 +00002991 // int() -> no prototype, no '...'.
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002992 D.AddTypeInfo(DeclaratorChunk::getFunction(/*prototype*/getLang().CPlusPlus,
Chris Lattnerf97409f2008-04-06 06:57:35 +00002993 /*variadic*/ false,
Douglas Gregor965acbb2009-02-18 07:07:28 +00002994 SourceLocation(),
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002995 /*arglist*/ 0, 0,
2996 DS.getTypeQualifiers(),
Sebastian Redl3cc97262009-05-31 11:47:27 +00002997 hasExceptionSpec, ThrowLoc,
Sebastian Redl7dc81342009-04-29 17:30:04 +00002998 hasAnyExceptionSpec,
Sebastian Redlef65f062009-05-29 18:02:33 +00002999 Exceptions.data(),
3000 ExceptionRanges.data(),
Sebastian Redl7dc81342009-04-29 17:30:04 +00003001 Exceptions.size(),
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00003002 LParenLoc, RParenLoc, D),
3003 EndLoc);
Chris Lattnerf97409f2008-04-06 06:57:35 +00003004 return;
Sebastian Redlef65f062009-05-29 18:02:33 +00003005 }
3006
Chris Lattner7399ee02008-10-20 02:05:46 +00003007 // Alternatively, this parameter list may be an identifier list form for a
3008 // K&R-style function: void foo(a,b,c)
John Thompson82287d12010-02-05 00:12:22 +00003009 if (!getLang().CPlusPlus && Tok.is(tok::identifier)
3010 && !TryAltiVecVectorToken()) {
John McCall9ba61662010-02-26 08:45:28 +00003011 if (TryAnnotateTypeOrScopeToken() || !Tok.is(tok::annot_typename)) {
Chris Lattner7399ee02008-10-20 02:05:46 +00003012 // K&R identifier lists can't have typedefs as identifiers, per
3013 // C99 6.7.5.3p11.
Steve Naroff2d081c42009-01-28 19:16:40 +00003014 if (RequiresArg) {
3015 Diag(Tok, diag::err_argument_required_after_attribute);
3016 delete AttrList;
3017 }
Chris Lattner83a94472010-05-14 17:23:36 +00003018
Steve Naroff2d081c42009-01-28 19:16:40 +00003019 // Identifier list. Note that '(' identifier-list ')' is only allowed for
Chris Lattner83a94472010-05-14 17:23:36 +00003020 // normal declarators, not for abstract-declarators. Get the first
3021 // identifier.
Chris Lattner9a65b812010-05-14 17:44:56 +00003022 Token FirstTok = Tok;
Chris Lattner83a94472010-05-14 17:23:36 +00003023 ConsumeToken(); // eat the first identifier.
Chris Lattner9a65b812010-05-14 17:44:56 +00003024
3025 // Identifier lists follow a really simple grammar: the identifiers can
3026 // be followed *only* by a ", moreidentifiers" or ")". However, K&R
3027 // identifier lists are really rare in the brave new modern world, and it
3028 // is very common for someone to typo a type in a non-k&r style list. If
3029 // we are presented with something like: "void foo(intptr x, float y)",
3030 // we don't want to start parsing the function declarator as though it is
3031 // a K&R style declarator just because intptr is an invalid type.
3032 //
3033 // To handle this, we check to see if the token after the first identifier
3034 // is a "," or ")". Only if so, do we parse it as an identifier list.
3035 if (Tok.is(tok::comma) || Tok.is(tok::r_paren))
3036 return ParseFunctionDeclaratorIdentifierList(LParenLoc,
3037 FirstTok.getIdentifierInfo(),
3038 FirstTok.getLocation(), D);
3039
3040 // If we get here, the code is invalid. Push the first identifier back
3041 // into the token stream and parse the first argument as an (invalid)
3042 // normal argument declarator.
3043 PP.EnterToken(Tok);
3044 Tok = FirstTok;
Chris Lattner7399ee02008-10-20 02:05:46 +00003045 }
Chris Lattnerf97409f2008-04-06 06:57:35 +00003046 }
Mike Stump1eb44332009-09-09 15:08:12 +00003047
Chris Lattnerf97409f2008-04-06 06:57:35 +00003048 // Finally, a normal, non-empty parameter type list.
Mike Stump1eb44332009-09-09 15:08:12 +00003049
Chris Lattnerf97409f2008-04-06 06:57:35 +00003050 // Build up an array of information about the parsed arguments.
3051 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Chris Lattner04421082008-04-08 04:40:51 +00003052
3053 // Enter function-declaration scope, limiting any declarators to the
3054 // function prototype scope, including parameter declarators.
Chris Lattnerae50fa02009-03-05 00:00:31 +00003055 ParseScope PrototypeScope(this,
3056 Scope::FunctionPrototypeScope|Scope::DeclScope);
Mike Stump1eb44332009-09-09 15:08:12 +00003057
Chris Lattnerf97409f2008-04-06 06:57:35 +00003058 bool IsVariadic = false;
Douglas Gregor965acbb2009-02-18 07:07:28 +00003059 SourceLocation EllipsisLoc;
Chris Lattnerf97409f2008-04-06 06:57:35 +00003060 while (1) {
3061 if (Tok.is(tok::ellipsis)) {
3062 IsVariadic = true;
Douglas Gregor965acbb2009-02-18 07:07:28 +00003063 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chris Lattnerf97409f2008-04-06 06:57:35 +00003064 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00003065 }
Mike Stump1eb44332009-09-09 15:08:12 +00003066
Chris Lattnerf97409f2008-04-06 06:57:35 +00003067 SourceLocation DSStart = Tok.getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +00003068
Chris Lattnerf97409f2008-04-06 06:57:35 +00003069 // Parse the declaration-specifiers.
John McCall54abf7d2009-11-04 02:18:39 +00003070 // Just use the ParsingDeclaration "scope" of the declarator.
Chris Lattnerf97409f2008-04-06 06:57:35 +00003071 DeclSpec DS;
Chris Lattner7399ee02008-10-20 02:05:46 +00003072
3073 // If the caller parsed attributes for the first argument, add them now.
3074 if (AttrList) {
3075 DS.AddAttributes(AttrList);
3076 AttrList = 0; // Only apply the attributes to the first parameter.
3077 }
Chris Lattnere64c5492009-02-27 18:38:20 +00003078 ParseDeclarationSpecifiers(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00003079
Chris Lattnerf97409f2008-04-06 06:57:35 +00003080 // Parse the declarator. This is "PrototypeContext", because we must
3081 // accept either 'declarator' or 'abstract-declarator' here.
3082 Declarator ParmDecl(DS, Declarator::PrototypeContext);
3083 ParseDeclarator(ParmDecl);
3084
3085 // Parse GNU attributes, if present.
Sebastian Redlab197ba2009-02-09 18:23:29 +00003086 if (Tok.is(tok::kw___attribute)) {
3087 SourceLocation Loc;
Sean Huntbbd37c62009-11-21 08:43:09 +00003088 AttributeList *AttrList = ParseGNUAttributes(&Loc);
Sebastian Redlab197ba2009-02-09 18:23:29 +00003089 ParmDecl.AddAttributes(AttrList, Loc);
3090 }
Mike Stump1eb44332009-09-09 15:08:12 +00003091
Chris Lattnerf97409f2008-04-06 06:57:35 +00003092 // Remember this parsed parameter in ParamInfo.
3093 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
Mike Stump1eb44332009-09-09 15:08:12 +00003094
Douglas Gregor72b505b2008-12-16 21:30:33 +00003095 // DefArgToks is used when the parsing of default arguments needs
3096 // to be delayed.
3097 CachedTokens *DefArgToks = 0;
3098
Chris Lattnerf97409f2008-04-06 06:57:35 +00003099 // If no parameter was specified, verify that *something* was specified,
3100 // otherwise we have a missing type and identifier.
Chris Lattnere64c5492009-02-27 18:38:20 +00003101 if (DS.isEmpty() && ParmDecl.getIdentifier() == 0 &&
3102 ParmDecl.getNumTypeObjects() == 0) {
Chris Lattnerf97409f2008-04-06 06:57:35 +00003103 // Completely missing, emit error.
3104 Diag(DSStart, diag::err_missing_param);
3105 } else {
3106 // Otherwise, we have something. Add it and let semantic analysis try
3107 // to grok it and add the result to the ParamInfo we are building.
Mike Stump1eb44332009-09-09 15:08:12 +00003108
Chris Lattnerf97409f2008-04-06 06:57:35 +00003109 // Inform the actions module about the parameter declarator, so it gets
3110 // added to the current scope.
John McCalld226f652010-08-21 09:40:31 +00003111 Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl);
Chris Lattner04421082008-04-08 04:40:51 +00003112
3113 // Parse the default argument, if any. We parse the default
3114 // arguments in all dialects; the semantic analysis in
3115 // ActOnParamDefaultArgument will reject the default argument in
3116 // C.
3117 if (Tok.is(tok::equal)) {
Douglas Gregor61366e92008-12-24 00:01:03 +00003118 SourceLocation EqualLoc = Tok.getLocation();
3119
Chris Lattner04421082008-04-08 04:40:51 +00003120 // Parse the default argument
Douglas Gregor72b505b2008-12-16 21:30:33 +00003121 if (D.getContext() == Declarator::MemberContext) {
3122 // If we're inside a class definition, cache the tokens
3123 // corresponding to the default argument. We'll actually parse
3124 // them when we see the end of the class definition.
3125 // FIXME: Templates will require something similar.
3126 // FIXME: Can we use a smart pointer for Toks?
3127 DefArgToks = new CachedTokens;
3128
Mike Stump1eb44332009-09-09 15:08:12 +00003129 if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks,
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +00003130 /*StopAtSemi=*/true,
3131 /*ConsumeFinalToken=*/false)) {
Douglas Gregor72b505b2008-12-16 21:30:33 +00003132 delete DefArgToks;
3133 DefArgToks = 0;
Douglas Gregor61366e92008-12-24 00:01:03 +00003134 Actions.ActOnParamDefaultArgumentError(Param);
Argyrios Kyrtzidis2b602ad2010-08-06 09:47:24 +00003135 } else {
3136 // Mark the end of the default argument so that we know when to
3137 // stop when we parse it later on.
3138 Token DefArgEnd;
3139 DefArgEnd.startToken();
3140 DefArgEnd.setKind(tok::cxx_defaultarg_end);
3141 DefArgEnd.setLocation(Tok.getLocation());
3142 DefArgToks->push_back(DefArgEnd);
Mike Stump1eb44332009-09-09 15:08:12 +00003143 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc,
Anders Carlsson5e300d12009-06-12 16:51:40 +00003144 (*DefArgToks)[1].getLocation());
Argyrios Kyrtzidis2b602ad2010-08-06 09:47:24 +00003145 }
Chris Lattner04421082008-04-08 04:40:51 +00003146 } else {
Douglas Gregor72b505b2008-12-16 21:30:33 +00003147 // Consume the '='.
Douglas Gregor61366e92008-12-24 00:01:03 +00003148 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00003149
John McCall60d7b3a2010-08-24 06:29:42 +00003150 ExprResult DefArgResult(ParseAssignmentExpression());
Douglas Gregor72b505b2008-12-16 21:30:33 +00003151 if (DefArgResult.isInvalid()) {
3152 Actions.ActOnParamDefaultArgumentError(Param);
3153 SkipUntil(tok::comma, tok::r_paren, true, true);
3154 } else {
3155 // Inform the actions module about the default argument
3156 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
John McCall9ae2f072010-08-23 23:25:46 +00003157 DefArgResult.take());
Douglas Gregor72b505b2008-12-16 21:30:33 +00003158 }
Chris Lattner04421082008-04-08 04:40:51 +00003159 }
3160 }
Mike Stump1eb44332009-09-09 15:08:12 +00003161
3162 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
3163 ParmDecl.getIdentifierLoc(), Param,
Douglas Gregor72b505b2008-12-16 21:30:33 +00003164 DefArgToks));
Chris Lattnerf97409f2008-04-06 06:57:35 +00003165 }
3166
3167 // If the next token is a comma, consume it and keep reading arguments.
Douglas Gregored5d6512009-09-22 21:41:40 +00003168 if (Tok.isNot(tok::comma)) {
3169 if (Tok.is(tok::ellipsis)) {
3170 IsVariadic = true;
3171 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
3172
3173 if (!getLang().CPlusPlus) {
3174 // We have ellipsis without a preceding ',', which is ill-formed
3175 // in C. Complain and provide the fix.
3176 Diag(EllipsisLoc, diag::err_missing_comma_before_ellipsis)
Douglas Gregor849b2432010-03-31 17:46:05 +00003177 << FixItHint::CreateInsertion(EllipsisLoc, ", ");
Douglas Gregored5d6512009-09-22 21:41:40 +00003178 }
3179 }
3180
3181 break;
3182 }
Mike Stump1eb44332009-09-09 15:08:12 +00003183
Chris Lattnerf97409f2008-04-06 06:57:35 +00003184 // Consume the comma.
3185 ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00003186 }
Mike Stump1eb44332009-09-09 15:08:12 +00003187
Chris Lattnerf97409f2008-04-06 06:57:35 +00003188 // Leave prototype scope.
Douglas Gregor8935b8b2008-12-10 06:34:36 +00003189 PrototypeScope.Exit();
Mike Stump1eb44332009-09-09 15:08:12 +00003190
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003191 // If we have the closing ')', eat it.
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00003192 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
3193 SourceLocation EndLoc = RParenLoc;
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003194
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003195 DeclSpec DS;
Sebastian Redl7dc81342009-04-29 17:30:04 +00003196 bool hasExceptionSpec = false;
Sebastian Redl3cc97262009-05-31 11:47:27 +00003197 SourceLocation ThrowLoc;
Sebastian Redl7dc81342009-04-29 17:30:04 +00003198 bool hasAnyExceptionSpec = false;
John McCallb3d87482010-08-24 05:47:05 +00003199 llvm::SmallVector<ParsedType, 2> Exceptions;
Sebastian Redlef65f062009-05-29 18:02:33 +00003200 llvm::SmallVector<SourceRange, 2> ExceptionRanges;
Sean Huntbbd37c62009-11-21 08:43:09 +00003201
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003202 if (getLang().CPlusPlus) {
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00003203 // Parse cv-qualifier-seq[opt].
Chris Lattner5a69d1c2008-12-18 07:02:59 +00003204 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Sebastian Redlab197ba2009-02-09 18:23:29 +00003205 if (!DS.getSourceRange().getEnd().isInvalid())
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00003206 EndLoc = DS.getSourceRange().getEnd();
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00003207
3208 // Parse exception-specification[opt].
Sebastian Redl7dc81342009-04-29 17:30:04 +00003209 if (Tok.is(tok::kw_throw)) {
3210 hasExceptionSpec = true;
Sebastian Redl3cc97262009-05-31 11:47:27 +00003211 ThrowLoc = Tok.getLocation();
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00003212 ParseExceptionSpecification(EndLoc, Exceptions, ExceptionRanges,
Sebastian Redlef65f062009-05-29 18:02:33 +00003213 hasAnyExceptionSpec);
3214 assert(Exceptions.size() == ExceptionRanges.size() &&
3215 "Produced different number of exception types and ranges.");
Sebastian Redl7dc81342009-04-29 17:30:04 +00003216 }
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003217 }
3218
Reid Spencer5f016e22007-07-11 17:01:13 +00003219 // Remember that we parsed a function type, and remember the attributes.
Chris Lattnerf97409f2008-04-06 06:57:35 +00003220 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/true, IsVariadic,
Douglas Gregor965acbb2009-02-18 07:07:28 +00003221 EllipsisLoc,
Jay Foadbeaaccd2009-05-21 09:52:38 +00003222 ParamInfo.data(), ParamInfo.size(),
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003223 DS.getTypeQualifiers(),
Sebastian Redl3cc97262009-05-31 11:47:27 +00003224 hasExceptionSpec, ThrowLoc,
Sebastian Redl7dc81342009-04-29 17:30:04 +00003225 hasAnyExceptionSpec,
Sebastian Redlef65f062009-05-29 18:02:33 +00003226 Exceptions.data(),
3227 ExceptionRanges.data(),
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00003228 Exceptions.size(),
3229 LParenLoc, RParenLoc, D),
3230 EndLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00003231}
3232
Chris Lattner66d28652008-04-06 06:34:08 +00003233/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
3234/// we found a K&R-style identifier list instead of a type argument list. The
Chris Lattner83a94472010-05-14 17:23:36 +00003235/// first identifier has already been consumed, and the current token is the
3236/// token right after it.
Chris Lattner66d28652008-04-06 06:34:08 +00003237///
3238/// identifier-list: [C99 6.7.5]
3239/// identifier
3240/// identifier-list ',' identifier
3241///
3242void Parser::ParseFunctionDeclaratorIdentifierList(SourceLocation LParenLoc,
Chris Lattner83a94472010-05-14 17:23:36 +00003243 IdentifierInfo *FirstIdent,
3244 SourceLocation FirstIdentLoc,
Chris Lattner66d28652008-04-06 06:34:08 +00003245 Declarator &D) {
3246 // Build up an array of information about the parsed arguments.
3247 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
3248 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
Mike Stump1eb44332009-09-09 15:08:12 +00003249
Chris Lattner66d28652008-04-06 06:34:08 +00003250 // If there was no identifier specified for the declarator, either we are in
3251 // an abstract-declarator, or we are in a parameter declarator which was found
3252 // to be abstract. In abstract-declarators, identifier lists are not valid:
3253 // diagnose this.
3254 if (!D.getIdentifier())
Chris Lattner83a94472010-05-14 17:23:36 +00003255 Diag(FirstIdentLoc, diag::ext_ident_list_in_param);
Chris Lattner66d28652008-04-06 06:34:08 +00003256
Chris Lattner83a94472010-05-14 17:23:36 +00003257 // The first identifier was already read, and is known to be the first
3258 // identifier in the list. Remember this identifier in ParamInfo.
3259 ParamsSoFar.insert(FirstIdent);
John McCalld226f652010-08-21 09:40:31 +00003260 ParamInfo.push_back(DeclaratorChunk::ParamInfo(FirstIdent, FirstIdentLoc, 0));
Mike Stump1eb44332009-09-09 15:08:12 +00003261
Chris Lattner66d28652008-04-06 06:34:08 +00003262 while (Tok.is(tok::comma)) {
3263 // Eat the comma.
3264 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00003265
Chris Lattner50c64772008-04-06 06:39:19 +00003266 // If this isn't an identifier, report the error and skip until ')'.
Chris Lattner66d28652008-04-06 06:34:08 +00003267 if (Tok.isNot(tok::identifier)) {
3268 Diag(Tok, diag::err_expected_ident);
Chris Lattner50c64772008-04-06 06:39:19 +00003269 SkipUntil(tok::r_paren);
3270 return;
Chris Lattner66d28652008-04-06 06:34:08 +00003271 }
Chris Lattneraaf9ddb2008-04-06 06:47:48 +00003272
Chris Lattner66d28652008-04-06 06:34:08 +00003273 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
Chris Lattneraaf9ddb2008-04-06 06:47:48 +00003274
3275 // Reject 'typedef int y; int test(x, y)', but continue parsing.
Douglas Gregor23c94db2010-07-02 17:43:08 +00003276 if (Actions.getTypeName(*ParmII, Tok.getLocation(), getCurScope()))
Chris Lattnerda83bac2008-11-19 07:37:42 +00003277 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
Mike Stump1eb44332009-09-09 15:08:12 +00003278
Chris Lattner66d28652008-04-06 06:34:08 +00003279 // Verify that the argument identifier has not already been mentioned.
3280 if (!ParamsSoFar.insert(ParmII)) {
Chris Lattnerda83bac2008-11-19 07:37:42 +00003281 Diag(Tok, diag::err_param_redefinition) << ParmII;
Chris Lattner50c64772008-04-06 06:39:19 +00003282 } else {
3283 // Remember this identifier in ParamInfo.
Chris Lattner66d28652008-04-06 06:34:08 +00003284 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Chris Lattnerb28317a2009-03-28 19:18:32 +00003285 Tok.getLocation(),
John McCalld226f652010-08-21 09:40:31 +00003286 0));
Chris Lattner50c64772008-04-06 06:39:19 +00003287 }
Mike Stump1eb44332009-09-09 15:08:12 +00003288
Chris Lattner66d28652008-04-06 06:34:08 +00003289 // Eat the identifier.
3290 ConsumeToken();
3291 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00003292
3293 // If we have the closing ')', eat it and we're done.
3294 SourceLocation RLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
3295
Chris Lattner50c64772008-04-06 06:39:19 +00003296 // Remember that we parsed a function type, and remember the attributes. This
3297 // function type is always a K&R style function type, which is not varargs and
3298 // has no prototype.
3299 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/false, /*varargs*/false,
Douglas Gregor965acbb2009-02-18 07:07:28 +00003300 SourceLocation(),
Chris Lattner50c64772008-04-06 06:39:19 +00003301 &ParamInfo[0], ParamInfo.size(),
Sebastian Redl7dc81342009-04-29 17:30:04 +00003302 /*TypeQuals*/0,
Sebastian Redl3cc97262009-05-31 11:47:27 +00003303 /*exception*/false,
3304 SourceLocation(), false, 0, 0, 0,
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00003305 LParenLoc, RLoc, D),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003306 RLoc);
Chris Lattner66d28652008-04-06 06:34:08 +00003307}
Chris Lattneref4715c2008-04-06 05:45:57 +00003308
Reid Spencer5f016e22007-07-11 17:01:13 +00003309/// [C90] direct-declarator '[' constant-expression[opt] ']'
3310/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
3311/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
3312/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
3313/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
3314void Parser::ParseBracketDeclarator(Declarator &D) {
3315 SourceLocation StartLoc = ConsumeBracket();
Mike Stump1eb44332009-09-09 15:08:12 +00003316
Chris Lattner378c7e42008-12-18 07:27:21 +00003317 // C array syntax has many features, but by-far the most common is [] and [4].
3318 // This code does a fast path to handle some of the most obvious cases.
3319 if (Tok.getKind() == tok::r_square) {
Sebastian Redlab197ba2009-02-09 18:23:29 +00003320 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
Sean Huntbbd37c62009-11-21 08:43:09 +00003321 //FIXME: Use these
3322 CXX0XAttributeList Attr;
3323 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier(true)) {
3324 Attr = ParseCXX0XAttributes();
3325 }
3326
Chris Lattner378c7e42008-12-18 07:27:21 +00003327 // Remember that we parsed the empty array type.
John McCall60d7b3a2010-08-24 06:29:42 +00003328 ExprResult NumElements;
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00003329 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0,
3330 StartLoc, EndLoc),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003331 EndLoc);
Chris Lattner378c7e42008-12-18 07:27:21 +00003332 return;
3333 } else if (Tok.getKind() == tok::numeric_constant &&
3334 GetLookAheadToken(1).is(tok::r_square)) {
3335 // [4] is very common. Parse the numeric constant expression.
John McCall60d7b3a2010-08-24 06:29:42 +00003336 ExprResult ExprRes(Actions.ActOnNumericConstant(Tok));
Chris Lattner378c7e42008-12-18 07:27:21 +00003337 ConsumeToken();
3338
Sebastian Redlab197ba2009-02-09 18:23:29 +00003339 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
Sean Huntbbd37c62009-11-21 08:43:09 +00003340 //FIXME: Use these
3341 CXX0XAttributeList Attr;
3342 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier()) {
3343 Attr = ParseCXX0XAttributes();
3344 }
Chris Lattner378c7e42008-12-18 07:27:21 +00003345
3346 // If there was an error parsing the assignment-expression, recover.
3347 if (ExprRes.isInvalid())
3348 ExprRes.release(); // Deallocate expr, just use [].
Mike Stump1eb44332009-09-09 15:08:12 +00003349
Chris Lattner378c7e42008-12-18 07:27:21 +00003350 // Remember that we parsed a array type, and remember its features.
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00003351 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, 0, ExprRes.release(),
3352 StartLoc, EndLoc),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003353 EndLoc);
Chris Lattner378c7e42008-12-18 07:27:21 +00003354 return;
3355 }
Mike Stump1eb44332009-09-09 15:08:12 +00003356
Reid Spencer5f016e22007-07-11 17:01:13 +00003357 // If valid, this location is the position where we read the 'static' keyword.
3358 SourceLocation StaticLoc;
Chris Lattner04d66662007-10-09 17:33:22 +00003359 if (Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00003360 StaticLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00003361
Reid Spencer5f016e22007-07-11 17:01:13 +00003362 // If there is a type-qualifier-list, read it now.
Chris Lattnera1fcbad2008-12-18 06:50:14 +00003363 // Type qualifiers in an array subscript are a C99 feature.
Reid Spencer5f016e22007-07-11 17:01:13 +00003364 DeclSpec DS;
Chris Lattner5a69d1c2008-12-18 07:02:59 +00003365 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Mike Stump1eb44332009-09-09 15:08:12 +00003366
Reid Spencer5f016e22007-07-11 17:01:13 +00003367 // If we haven't already read 'static', check to see if there is one after the
3368 // type-qualifier-list.
Chris Lattner04d66662007-10-09 17:33:22 +00003369 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00003370 StaticLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00003371
Reid Spencer5f016e22007-07-11 17:01:13 +00003372 // Handle "direct-declarator [ type-qual-list[opt] * ]".
3373 bool isStar = false;
John McCall60d7b3a2010-08-24 06:29:42 +00003374 ExprResult NumElements;
Mike Stump1eb44332009-09-09 15:08:12 +00003375
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00003376 // Handle the case where we have '[*]' as the array size. However, a leading
3377 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
3378 // the the token after the star is a ']'. Since stars in arrays are
3379 // infrequent, use of lookahead is not costly here.
3380 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattnera711dd02008-04-06 05:27:21 +00003381 ConsumeToken(); // Eat the '*'.
Reid Spencer5f016e22007-07-11 17:01:13 +00003382
Chris Lattnera1fcbad2008-12-18 06:50:14 +00003383 if (StaticLoc.isValid()) {
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00003384 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattnera1fcbad2008-12-18 06:50:14 +00003385 StaticLoc = SourceLocation(); // Drop the static.
3386 }
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00003387 isStar = true;
Chris Lattner04d66662007-10-09 17:33:22 +00003388 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner378c7e42008-12-18 07:27:21 +00003389 // Note, in C89, this production uses the constant-expr production instead
3390 // of assignment-expr. The only difference is that assignment-expr allows
3391 // things like '=' and '*='. Sema rejects these in C89 mode because they
3392 // are not i-c-e's, so we don't need to distinguish between the two here.
Mike Stump1eb44332009-09-09 15:08:12 +00003393
Douglas Gregore0762c92009-06-19 23:52:42 +00003394 // Parse the constant-expression or assignment-expression now (depending
3395 // on dialect).
3396 if (getLang().CPlusPlus)
3397 NumElements = ParseConstantExpression();
3398 else
3399 NumElements = ParseAssignmentExpression();
Reid Spencer5f016e22007-07-11 17:01:13 +00003400 }
Mike Stump1eb44332009-09-09 15:08:12 +00003401
Reid Spencer5f016e22007-07-11 17:01:13 +00003402 // If there was an error parsing the assignment-expression, recover.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00003403 if (NumElements.isInvalid()) {
Chris Lattner5cb10d32009-04-24 22:30:50 +00003404 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00003405 // If the expression was invalid, skip it.
3406 SkipUntil(tok::r_square);
3407 return;
3408 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00003409
3410 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
3411
Sean Huntbbd37c62009-11-21 08:43:09 +00003412 //FIXME: Use these
3413 CXX0XAttributeList Attr;
3414 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier()) {
3415 Attr = ParseCXX0XAttributes();
3416 }
3417
Chris Lattner378c7e42008-12-18 07:27:21 +00003418 // Remember that we parsed a array type, and remember its features.
Reid Spencer5f016e22007-07-11 17:01:13 +00003419 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
3420 StaticLoc.isValid(), isStar,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00003421 NumElements.release(),
3422 StartLoc, EndLoc),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003423 EndLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00003424}
3425
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00003426/// [GNU] typeof-specifier:
3427/// typeof ( expressions )
3428/// typeof ( type-name )
3429/// [GNU/C++] typeof unary-expression
Steve Naroffd1861fd2007-07-31 12:34:36 +00003430///
3431void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner04d66662007-10-09 17:33:22 +00003432 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00003433 Token OpTok = Tok;
Steve Naroffd1861fd2007-07-31 12:34:36 +00003434 SourceLocation StartLoc = ConsumeToken();
3435
John McCallcfb708c2010-01-13 20:03:27 +00003436 const bool hasParens = Tok.is(tok::l_paren);
3437
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00003438 bool isCastExpr;
John McCallb3d87482010-08-24 05:47:05 +00003439 ParsedType CastTy;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00003440 SourceRange CastRange;
John McCall60d7b3a2010-08-24 06:29:42 +00003441 ExprResult Operand = ParseExprAfterTypeofSizeofAlignof(OpTok,
John McCall911093e2010-08-25 02:45:51 +00003442 isCastExpr,
3443 CastTy,
3444 CastRange);
John McCallcfb708c2010-01-13 20:03:27 +00003445 if (hasParens)
3446 DS.setTypeofParensRange(CastRange);
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00003447
3448 if (CastRange.getEnd().isInvalid())
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00003449 // FIXME: Not accurate, the range gets one token more than it should.
3450 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00003451 else
3452 DS.SetRangeEnd(CastRange.getEnd());
Mike Stump1eb44332009-09-09 15:08:12 +00003453
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00003454 if (isCastExpr) {
3455 if (!CastTy) {
3456 DS.SetTypeSpecError();
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00003457 return;
Douglas Gregor809070a2009-02-18 17:45:20 +00003458 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00003459
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00003460 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00003461 unsigned DiagID;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00003462 // Check for duplicate type specifiers (e.g. "int typeof(int)").
3463 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00003464 DiagID, CastTy))
3465 Diag(StartLoc, DiagID) << PrevSpec;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00003466 return;
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00003467 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00003468
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00003469 // If we get here, the operand to the typeof was an expresion.
3470 if (Operand.isInvalid()) {
3471 DS.SetTypeSpecError();
Steve Naroff9dfa7b42007-08-02 02:53:48 +00003472 return;
Steve Naroffd1861fd2007-07-31 12:34:36 +00003473 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00003474
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00003475 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00003476 unsigned DiagID;
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00003477 // Check for duplicate type specifiers (e.g. "int typeof(int)").
3478 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
John McCallb3d87482010-08-24 05:47:05 +00003479 DiagID, Operand.get()))
John McCallfec54012009-08-03 20:12:06 +00003480 Diag(StartLoc, DiagID) << PrevSpec;
Steve Naroffd1861fd2007-07-31 12:34:36 +00003481}
Chris Lattner1b492422010-02-28 18:33:55 +00003482
3483
3484/// TryAltiVecVectorTokenOutOfLine - Out of line body that should only be called
3485/// from TryAltiVecVectorToken.
3486bool Parser::TryAltiVecVectorTokenOutOfLine() {
3487 Token Next = NextToken();
3488 switch (Next.getKind()) {
3489 default: return false;
3490 case tok::kw_short:
3491 case tok::kw_long:
3492 case tok::kw_signed:
3493 case tok::kw_unsigned:
3494 case tok::kw_void:
3495 case tok::kw_char:
3496 case tok::kw_int:
3497 case tok::kw_float:
3498 case tok::kw_double:
3499 case tok::kw_bool:
3500 case tok::kw___pixel:
3501 Tok.setKind(tok::kw___vector);
3502 return true;
3503 case tok::identifier:
3504 if (Next.getIdentifierInfo() == Ident_pixel) {
3505 Tok.setKind(tok::kw___vector);
3506 return true;
3507 }
3508 return false;
3509 }
3510}
3511
3512bool Parser::TryAltiVecTokenOutOfLine(DeclSpec &DS, SourceLocation Loc,
3513 const char *&PrevSpec, unsigned &DiagID,
3514 bool &isInvalid) {
3515 if (Tok.getIdentifierInfo() == Ident_vector) {
3516 Token Next = NextToken();
3517 switch (Next.getKind()) {
3518 case tok::kw_short:
3519 case tok::kw_long:
3520 case tok::kw_signed:
3521 case tok::kw_unsigned:
3522 case tok::kw_void:
3523 case tok::kw_char:
3524 case tok::kw_int:
3525 case tok::kw_float:
3526 case tok::kw_double:
3527 case tok::kw_bool:
3528 case tok::kw___pixel:
3529 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
3530 return true;
3531 case tok::identifier:
3532 if (Next.getIdentifierInfo() == Ident_pixel) {
3533 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
3534 return true;
3535 }
3536 break;
3537 default:
3538 break;
3539 }
Douglas Gregora8f031f2010-06-16 15:28:57 +00003540 } else if ((Tok.getIdentifierInfo() == Ident_pixel) &&
Chris Lattner1b492422010-02-28 18:33:55 +00003541 DS.isTypeAltiVecVector()) {
3542 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
3543 return true;
3544 }
3545 return false;
3546}
3547