blob: 5ed22e275377ecab78e405c4c375ed428cf2674d [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- ParseDecl.cpp - Declaration Parsing ------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Declaration portions of the Parser interfaces.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Parse/Parser.h"
Chris Lattner500d3292009-01-29 05:15:15 +000015#include "clang/Parse/ParseDiagnostic.h"
John McCall19510852010-08-20 18:27:03 +000016#include "clang/Sema/Scope.h"
17#include "clang/Sema/ParsedTemplate.h"
John McCallf312b1e2010-08-26 23:41:50 +000018#include "clang/Sema/PrettyDeclStackTrace.h"
Chris Lattnerd167ca02009-12-10 00:21:05 +000019#include "RAIIObjectsForParser.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000020#include "llvm/ADT/SmallSet.h"
21using namespace clang;
22
23//===----------------------------------------------------------------------===//
24// C99 6.7: Declarations.
25//===----------------------------------------------------------------------===//
26
27/// ParseTypeName
28/// type-name: [C99 6.7.6]
29/// specifier-qualifier-list abstract-declarator[opt]
Sebastian Redl4c5d3202008-11-21 19:14:01 +000030///
31/// Called type-id in C++.
John McCallf312b1e2010-08-26 23:41:50 +000032TypeResult Parser::ParseTypeName(SourceRange *Range) {
Reid Spencer5f016e22007-07-11 17:01:13 +000033 // Parse the common declaration-specifiers piece.
34 DeclSpec DS;
35 ParseSpecifierQualifierList(DS);
Sebastian Redlef65f062009-05-29 18:02:33 +000036
Reid Spencer5f016e22007-07-11 17:01:13 +000037 // Parse the abstract-declarator, if present.
38 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
39 ParseDeclarator(DeclaratorInfo);
Sebastian Redlef65f062009-05-29 18:02:33 +000040 if (Range)
41 *Range = DeclaratorInfo.getSourceRange();
42
Chris Lattnereaaebc72009-04-25 08:06:05 +000043 if (DeclaratorInfo.isInvalidType())
Douglas Gregor809070a2009-02-18 17:45:20 +000044 return true;
45
Douglas Gregor23c94db2010-07-02 17:43:08 +000046 return Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
Reid Spencer5f016e22007-07-11 17:01:13 +000047}
48
Sean Huntbbd37c62009-11-21 08:43:09 +000049/// ParseGNUAttributes - Parse a non-empty attributes list.
Reid Spencer5f016e22007-07-11 17:01:13 +000050///
51/// [GNU] attributes:
52/// attribute
53/// attributes attribute
54///
55/// [GNU] attribute:
56/// '__attribute__' '(' '(' attribute-list ')' ')'
57///
58/// [GNU] attribute-list:
59/// attrib
60/// attribute_list ',' attrib
61///
62/// [GNU] attrib:
63/// empty
64/// attrib-name
65/// attrib-name '(' identifier ')'
66/// attrib-name '(' identifier ',' nonempty-expr-list ')'
67/// attrib-name '(' argument-expression-list [C99 6.5.2] ')'
68///
69/// [GNU] attrib-name:
70/// identifier
71/// typespec
72/// typequal
73/// storageclass
Mike Stump1eb44332009-09-09 15:08:12 +000074///
Reid Spencer5f016e22007-07-11 17:01:13 +000075/// FIXME: The GCC grammar/code for this construct implies we need two
Mike Stump1eb44332009-09-09 15:08:12 +000076/// token lookahead. Comment from gcc: "If they start with an identifier
77/// which is followed by a comma or close parenthesis, then the arguments
Reid Spencer5f016e22007-07-11 17:01:13 +000078/// start with that identifier; otherwise they are an expression list."
79///
80/// At the moment, I am not doing 2 token lookahead. I am also unaware of
81/// any attributes that don't work (based on my limited testing). Most
82/// attributes are very simple in practice. Until we find a bug, I don't see
83/// a pressing need to implement the 2 token lookahead.
84
Sean Huntbbd37c62009-11-21 08:43:09 +000085AttributeList *Parser::ParseGNUAttributes(SourceLocation *EndLoc) {
86 assert(Tok.is(tok::kw___attribute) && "Not a GNU attribute list!");
Mike Stump1eb44332009-09-09 15:08:12 +000087
Reid Spencer5f016e22007-07-11 17:01:13 +000088 AttributeList *CurrAttr = 0;
Mike Stump1eb44332009-09-09 15:08:12 +000089
Chris Lattner04d66662007-10-09 17:33:22 +000090 while (Tok.is(tok::kw___attribute)) {
Reid Spencer5f016e22007-07-11 17:01:13 +000091 ConsumeToken();
92 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
93 "attribute")) {
94 SkipUntil(tok::r_paren, true); // skip until ) or ;
95 return CurrAttr;
96 }
97 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, "(")) {
98 SkipUntil(tok::r_paren, true); // skip until ) or ;
99 return CurrAttr;
100 }
101 // Parse the attribute-list. e.g. __attribute__(( weak, alias("__f") ))
Chris Lattner04d66662007-10-09 17:33:22 +0000102 while (Tok.is(tok::identifier) || isDeclarationSpecifier() ||
103 Tok.is(tok::comma)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000104
105 if (Tok.is(tok::comma)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000106 // allows for empty/non-empty attributes. ((__vector_size__(16),,,,))
107 ConsumeToken();
108 continue;
109 }
110 // we have an identifier or declaration specifier (const, int, etc.)
111 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
112 SourceLocation AttrNameLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000113
Douglas Gregorec1afbf2010-03-16 19:09:18 +0000114 // check if we have a "parameterized" attribute
Chris Lattner04d66662007-10-09 17:33:22 +0000115 if (Tok.is(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000116 ConsumeParen(); // ignore the left paren loc for now
Mike Stump1eb44332009-09-09 15:08:12 +0000117
Chris Lattner04d66662007-10-09 17:33:22 +0000118 if (Tok.is(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000119 IdentifierInfo *ParmName = Tok.getIdentifierInfo();
120 SourceLocation ParmLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000121
122 if (Tok.is(tok::r_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000123 // __attribute__(( mode(byte) ))
124 ConsumeParen(); // ignore the right paren loc for now
Sean Huntbbd37c62009-11-21 08:43:09 +0000125 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0, AttrNameLoc,
Reid Spencer5f016e22007-07-11 17:01:13 +0000126 ParmName, ParmLoc, 0, 0, CurrAttr);
Chris Lattner04d66662007-10-09 17:33:22 +0000127 } else if (Tok.is(tok::comma)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000128 ConsumeToken();
129 // __attribute__(( format(printf, 1, 2) ))
Sebastian Redla55e52c2008-11-25 22:21:31 +0000130 ExprVector ArgExprs(Actions);
Reid Spencer5f016e22007-07-11 17:01:13 +0000131 bool ArgExprsOk = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000132
Reid Spencer5f016e22007-07-11 17:01:13 +0000133 // now parse the non-empty comma separated list of expressions
134 while (1) {
John McCall60d7b3a2010-08-24 06:29:42 +0000135 ExprResult ArgExpr(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000136 if (ArgExpr.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000137 ArgExprsOk = false;
138 SkipUntil(tok::r_paren);
139 break;
140 } else {
Sebastian Redleffa8d12008-12-10 00:02:53 +0000141 ArgExprs.push_back(ArgExpr.release());
Reid Spencer5f016e22007-07-11 17:01:13 +0000142 }
Chris Lattner04d66662007-10-09 17:33:22 +0000143 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +0000144 break;
145 ConsumeToken(); // Eat the comma, move to the next argument
146 }
Chris Lattner04d66662007-10-09 17:33:22 +0000147 if (ArgExprsOk && Tok.is(tok::r_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000148 ConsumeParen(); // ignore the right paren loc for now
Sean Huntbbd37c62009-11-21 08:43:09 +0000149 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0,
150 AttrNameLoc, ParmName, ParmLoc,
151 ArgExprs.take(), ArgExprs.size(),
152 CurrAttr);
Reid Spencer5f016e22007-07-11 17:01:13 +0000153 }
154 }
155 } else { // not an identifier
Nate Begeman6f3d8382009-06-26 06:32:41 +0000156 switch (Tok.getKind()) {
157 case tok::r_paren:
Reid Spencer5f016e22007-07-11 17:01:13 +0000158 // parse a possibly empty comma separated list of expressions
Reid Spencer5f016e22007-07-11 17:01:13 +0000159 // __attribute__(( nonnull() ))
160 ConsumeParen(); // ignore the right paren loc for now
Sean Huntbbd37c62009-11-21 08:43:09 +0000161 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0, AttrNameLoc,
Reid Spencer5f016e22007-07-11 17:01:13 +0000162 0, SourceLocation(), 0, 0, CurrAttr);
Nate Begeman6f3d8382009-06-26 06:32:41 +0000163 break;
164 case tok::kw_char:
165 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000166 case tok::kw_char16_t:
167 case tok::kw_char32_t:
Nate Begeman6f3d8382009-06-26 06:32:41 +0000168 case tok::kw_bool:
169 case tok::kw_short:
170 case tok::kw_int:
171 case tok::kw_long:
172 case tok::kw_signed:
173 case tok::kw_unsigned:
174 case tok::kw_float:
175 case tok::kw_double:
176 case tok::kw_void:
177 case tok::kw_typeof:
Fariborz Jahanian1b72fa72010-08-17 23:19:16 +0000178 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0, AttrNameLoc,
179 0, SourceLocation(), 0, 0, CurrAttr);
180 if (CurrAttr->getKind() == AttributeList::AT_IBOutletCollection)
181 Diag(Tok, diag::err_iboutletcollection_builtintype);
Nate Begeman6f3d8382009-06-26 06:32:41 +0000182 // If it's a builtin type name, eat it and expect a rparen
183 // __attribute__(( vec_type_hint(char) ))
184 ConsumeToken();
Nate Begeman6f3d8382009-06-26 06:32:41 +0000185 if (Tok.is(tok::r_paren))
186 ConsumeParen();
187 break;
188 default:
Reid Spencer5f016e22007-07-11 17:01:13 +0000189 // __attribute__(( aligned(16) ))
Sebastian Redla55e52c2008-11-25 22:21:31 +0000190 ExprVector ArgExprs(Actions);
Reid Spencer5f016e22007-07-11 17:01:13 +0000191 bool ArgExprsOk = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000192
Reid Spencer5f016e22007-07-11 17:01:13 +0000193 // now parse the list of expressions
194 while (1) {
John McCall60d7b3a2010-08-24 06:29:42 +0000195 ExprResult ArgExpr(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000196 if (ArgExpr.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000197 ArgExprsOk = false;
198 SkipUntil(tok::r_paren);
199 break;
200 } else {
Sebastian Redleffa8d12008-12-10 00:02:53 +0000201 ArgExprs.push_back(ArgExpr.release());
Reid Spencer5f016e22007-07-11 17:01:13 +0000202 }
Chris Lattner04d66662007-10-09 17:33:22 +0000203 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +0000204 break;
205 ConsumeToken(); // Eat the comma, move to the next argument
206 }
207 // Match the ')'.
Chris Lattner04d66662007-10-09 17:33:22 +0000208 if (ArgExprsOk && Tok.is(tok::r_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000209 ConsumeParen(); // ignore the right paren loc for now
Sebastian Redla55e52c2008-11-25 22:21:31 +0000210 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0,
Sean Huntbbd37c62009-11-21 08:43:09 +0000211 AttrNameLoc, 0, SourceLocation(), ArgExprs.take(),
212 ArgExprs.size(),
Reid Spencer5f016e22007-07-11 17:01:13 +0000213 CurrAttr);
214 }
Nate Begeman6f3d8382009-06-26 06:32:41 +0000215 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000216 }
217 }
218 } else {
Sean Huntbbd37c62009-11-21 08:43:09 +0000219 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0, AttrNameLoc,
Reid Spencer5f016e22007-07-11 17:01:13 +0000220 0, SourceLocation(), 0, 0, CurrAttr);
221 }
222 }
223 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
Reid Spencer5f016e22007-07-11 17:01:13 +0000224 SkipUntil(tok::r_paren, false);
Sean Huntbbd37c62009-11-21 08:43:09 +0000225 SourceLocation Loc = Tok.getLocation();
Sebastian Redlab197ba2009-02-09 18:23:29 +0000226 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) {
227 SkipUntil(tok::r_paren, false);
228 }
229 if (EndLoc)
230 *EndLoc = Loc;
Reid Spencer5f016e22007-07-11 17:01:13 +0000231 }
232 return CurrAttr;
233}
234
Eli Friedmana23b4852009-06-08 07:21:15 +0000235/// ParseMicrosoftDeclSpec - Parse an __declspec construct
236///
237/// [MS] decl-specifier:
238/// __declspec ( extended-decl-modifier-seq )
239///
240/// [MS] extended-decl-modifier-seq:
241/// extended-decl-modifier[opt]
242/// extended-decl-modifier extended-decl-modifier-seq
243
Eli Friedman290eeb02009-06-08 23:27:34 +0000244AttributeList* Parser::ParseMicrosoftDeclSpec(AttributeList *CurrAttr) {
Steve Narofff59e17e2008-12-24 20:59:21 +0000245 assert(Tok.is(tok::kw___declspec) && "Not a declspec!");
Eli Friedmana23b4852009-06-08 07:21:15 +0000246
Steve Narofff59e17e2008-12-24 20:59:21 +0000247 ConsumeToken();
Eli Friedmana23b4852009-06-08 07:21:15 +0000248 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
249 "declspec")) {
250 SkipUntil(tok::r_paren, true); // skip until ) or ;
251 return CurrAttr;
252 }
Eli Friedman290eeb02009-06-08 23:27:34 +0000253 while (Tok.getIdentifierInfo()) {
Eli Friedmana23b4852009-06-08 07:21:15 +0000254 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
255 SourceLocation AttrNameLoc = ConsumeToken();
256 if (Tok.is(tok::l_paren)) {
257 ConsumeParen();
258 // FIXME: This doesn't parse __declspec(property(get=get_func_name))
259 // correctly.
John McCall60d7b3a2010-08-24 06:29:42 +0000260 ExprResult ArgExpr(ParseAssignmentExpression());
Eli Friedmana23b4852009-06-08 07:21:15 +0000261 if (!ArgExpr.isInvalid()) {
John McCallca0408f2010-08-23 06:44:23 +0000262 Expr *ExprList = ArgExpr.take();
Sean Huntbbd37c62009-11-21 08:43:09 +0000263 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
Eli Friedmana23b4852009-06-08 07:21:15 +0000264 SourceLocation(), &ExprList, 1,
265 CurrAttr, true);
266 }
267 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
268 SkipUntil(tok::r_paren, false);
269 } else {
Sean Huntbbd37c62009-11-21 08:43:09 +0000270 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0, AttrNameLoc,
271 0, SourceLocation(), 0, 0, CurrAttr, true);
Eli Friedmana23b4852009-06-08 07:21:15 +0000272 }
273 }
274 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
275 SkipUntil(tok::r_paren, false);
Eli Friedman290eeb02009-06-08 23:27:34 +0000276 return CurrAttr;
277}
278
279AttributeList* Parser::ParseMicrosoftTypeAttributes(AttributeList *CurrAttr) {
280 // Treat these like attributes
281 // FIXME: Allow Sema to distinguish between these and real attributes!
282 while (Tok.is(tok::kw___fastcall) || Tok.is(tok::kw___stdcall) ||
Douglas Gregorf813a2c2010-05-18 16:57:00 +0000283 Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___cdecl) ||
284 Tok.is(tok::kw___ptr64) || Tok.is(tok::kw___w64)) {
Eli Friedman290eeb02009-06-08 23:27:34 +0000285 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
286 SourceLocation AttrNameLoc = ConsumeToken();
287 if (Tok.is(tok::kw___ptr64) || Tok.is(tok::kw___w64))
288 // FIXME: Support these properly!
289 continue;
Sean Huntbbd37c62009-11-21 08:43:09 +0000290 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
Eli Friedman290eeb02009-06-08 23:27:34 +0000291 SourceLocation(), 0, 0, CurrAttr, true);
292 }
293 return CurrAttr;
Steve Narofff59e17e2008-12-24 20:59:21 +0000294}
295
Dawn Perchik52fc3142010-09-03 01:29:35 +0000296AttributeList* Parser::ParseBorlandTypeAttributes(AttributeList *CurrAttr) {
297 // Treat these like attributes
298 while (Tok.is(tok::kw___pascal)) {
299 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
300 SourceLocation AttrNameLoc = ConsumeToken();
301 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
302 SourceLocation(), 0, 0, CurrAttr, true);
303 }
304 return CurrAttr;
305}
306
Reid Spencer5f016e22007-07-11 17:01:13 +0000307/// ParseDeclaration - Parse a full 'declaration', which consists of
308/// declaration-specifiers, some number of declarators, and a semicolon.
Chris Lattner97144fc2009-04-02 04:16:50 +0000309/// 'Context' should be a Declarator::TheContext value. This returns the
310/// location of the semicolon in DeclEnd.
Chris Lattner8f08cb72007-08-25 06:57:03 +0000311///
312/// declaration: [C99 6.7]
313/// block-declaration ->
314/// simple-declaration
315/// others [FIXME]
Douglas Gregoradcac882008-12-01 23:54:00 +0000316/// [C++] template-declaration
Chris Lattner8f08cb72007-08-25 06:57:03 +0000317/// [C++] namespace-definition
Douglas Gregorf780abc2008-12-30 03:27:21 +0000318/// [C++] using-directive
Douglas Gregord7f37bf2009-06-22 23:06:13 +0000319/// [C++] using-declaration
Sebastian Redl50de12f2009-03-24 22:27:57 +0000320/// [C++0x] static_assert-declaration
Chris Lattner8f08cb72007-08-25 06:57:03 +0000321/// others... [FIXME]
322///
Chris Lattner97144fc2009-04-02 04:16:50 +0000323Parser::DeclGroupPtrTy Parser::ParseDeclaration(unsigned Context,
Sean Huntbbd37c62009-11-21 08:43:09 +0000324 SourceLocation &DeclEnd,
325 CXX0XAttributeList Attr) {
Argyrios Kyrtzidis36d36802010-06-17 10:52:18 +0000326 ParenBraceBracketBalancer BalancerRAIIObj(*this);
327
John McCalld226f652010-08-21 09:40:31 +0000328 Decl *SingleDecl = 0;
Chris Lattner8f08cb72007-08-25 06:57:03 +0000329 switch (Tok.getKind()) {
Douglas Gregoradcac882008-12-01 23:54:00 +0000330 case tok::kw_template:
Douglas Gregor1426e532009-05-12 21:31:51 +0000331 case tok::kw_export:
Sean Huntbbd37c62009-11-21 08:43:09 +0000332 if (Attr.HasAttr)
333 Diag(Attr.Range.getBegin(), diag::err_attributes_not_allowed)
334 << Attr.Range;
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000335 SingleDecl = ParseDeclarationStartingWithTemplate(Context, DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +0000336 break;
Sebastian Redld078e642010-08-27 23:12:46 +0000337 case tok::kw_inline:
Sebastian Redl88e64ca2010-08-31 00:36:45 +0000338 // Could be the start of an inline namespace. Allowed as an ext in C++03.
339 if (getLang().CPlusPlus && NextToken().is(tok::kw_namespace)) {
Sebastian Redld078e642010-08-27 23:12:46 +0000340 if (Attr.HasAttr)
341 Diag(Attr.Range.getBegin(), diag::err_attributes_not_allowed)
342 << Attr.Range;
343 SourceLocation InlineLoc = ConsumeToken();
344 SingleDecl = ParseNamespace(Context, DeclEnd, InlineLoc);
345 break;
346 }
347 return ParseSimpleDeclaration(Context, DeclEnd, Attr.AttrList, true);
Chris Lattner8f08cb72007-08-25 06:57:03 +0000348 case tok::kw_namespace:
Sean Huntbbd37c62009-11-21 08:43:09 +0000349 if (Attr.HasAttr)
350 Diag(Attr.Range.getBegin(), diag::err_attributes_not_allowed)
351 << Attr.Range;
Chris Lattner97144fc2009-04-02 04:16:50 +0000352 SingleDecl = ParseNamespace(Context, DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +0000353 break;
Douglas Gregorf780abc2008-12-30 03:27:21 +0000354 case tok::kw_using:
Sean Huntbbd37c62009-11-21 08:43:09 +0000355 SingleDecl = ParseUsingDirectiveOrDeclaration(Context, DeclEnd, Attr);
Chris Lattner682bf922009-03-29 16:50:03 +0000356 break;
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000357 case tok::kw_static_assert:
Sean Huntbbd37c62009-11-21 08:43:09 +0000358 if (Attr.HasAttr)
359 Diag(Attr.Range.getBegin(), diag::err_attributes_not_allowed)
360 << Attr.Range;
Chris Lattner97144fc2009-04-02 04:16:50 +0000361 SingleDecl = ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +0000362 break;
Chris Lattner8f08cb72007-08-25 06:57:03 +0000363 default:
Chris Lattner5c5db552010-04-05 18:18:31 +0000364 return ParseSimpleDeclaration(Context, DeclEnd, Attr.AttrList, true);
Chris Lattner8f08cb72007-08-25 06:57:03 +0000365 }
Sean Huntbbd37c62009-11-21 08:43:09 +0000366
Chris Lattner682bf922009-03-29 16:50:03 +0000367 // This routine returns a DeclGroup, if the thing we parsed only contains a
368 // single decl, convert it now.
369 return Actions.ConvertDeclToDeclGroup(SingleDecl);
Chris Lattner8f08cb72007-08-25 06:57:03 +0000370}
371
372/// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl]
373/// declaration-specifiers init-declarator-list[opt] ';'
374///[C90/C++]init-declarator-list ';' [TODO]
375/// [OMP] threadprivate-directive [TODO]
Chris Lattnercd147752009-03-29 17:27:48 +0000376///
377/// If RequireSemi is false, this does not check for a ';' at the end of the
Chris Lattner5c5db552010-04-05 18:18:31 +0000378/// declaration. If it is true, it checks for and eats it.
Chris Lattnercd147752009-03-29 17:27:48 +0000379Parser::DeclGroupPtrTy Parser::ParseSimpleDeclaration(unsigned Context,
Sean Huntbbd37c62009-11-21 08:43:09 +0000380 SourceLocation &DeclEnd,
Chris Lattner5c5db552010-04-05 18:18:31 +0000381 AttributeList *Attr,
382 bool RequireSemi) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000383 // Parse the common declaration-specifiers piece.
John McCall54abf7d2009-11-04 02:18:39 +0000384 ParsingDeclSpec DS(*this);
Sean Huntbbd37c62009-11-21 08:43:09 +0000385 if (Attr)
386 DS.AddAttributes(Attr);
Douglas Gregor0efc2c12010-01-13 17:31:36 +0000387 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS_none,
388 getDeclSpecContextFromDeclaratorContext(Context));
Mike Stump1eb44332009-09-09 15:08:12 +0000389
Reid Spencer5f016e22007-07-11 17:01:13 +0000390 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
391 // declaration-specifiers init-declarator-list[opt] ';'
Chris Lattner04d66662007-10-09 17:33:22 +0000392 if (Tok.is(tok::semi)) {
Chris Lattner5c5db552010-04-05 18:18:31 +0000393 if (RequireSemi) ConsumeToken();
John McCalld226f652010-08-21 09:40:31 +0000394 Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none,
John McCallaec03712010-05-21 20:45:30 +0000395 DS);
John McCall54abf7d2009-11-04 02:18:39 +0000396 DS.complete(TheDecl);
Chris Lattner682bf922009-03-29 16:50:03 +0000397 return Actions.ConvertDeclToDeclGroup(TheDecl);
Reid Spencer5f016e22007-07-11 17:01:13 +0000398 }
Mike Stump1eb44332009-09-09 15:08:12 +0000399
Chris Lattner5c5db552010-04-05 18:18:31 +0000400 return ParseDeclGroup(DS, Context, /*FunctionDefs=*/ false, &DeclEnd);
John McCalld8ac0572009-11-03 19:26:08 +0000401}
Mike Stump1eb44332009-09-09 15:08:12 +0000402
John McCalld8ac0572009-11-03 19:26:08 +0000403/// ParseDeclGroup - Having concluded that this is either a function
404/// definition or a group of object declarations, actually parse the
405/// result.
John McCall54abf7d2009-11-04 02:18:39 +0000406Parser::DeclGroupPtrTy Parser::ParseDeclGroup(ParsingDeclSpec &DS,
407 unsigned Context,
John McCalld8ac0572009-11-03 19:26:08 +0000408 bool AllowFunctionDefinitions,
409 SourceLocation *DeclEnd) {
410 // Parse the first declarator.
John McCall54abf7d2009-11-04 02:18:39 +0000411 ParsingDeclarator D(*this, DS, static_cast<Declarator::TheContext>(Context));
John McCalld8ac0572009-11-03 19:26:08 +0000412 ParseDeclarator(D);
Chris Lattnercd147752009-03-29 17:27:48 +0000413
John McCalld8ac0572009-11-03 19:26:08 +0000414 // Bail out if the first declarator didn't seem well-formed.
415 if (!D.hasName() && !D.mayOmitIdentifier()) {
416 // Skip until ; or }.
417 SkipUntil(tok::r_brace, true, true);
418 if (Tok.is(tok::semi))
419 ConsumeToken();
420 return DeclGroupPtrTy();
Chris Lattner23c4b182009-03-29 17:18:04 +0000421 }
Mike Stump1eb44332009-09-09 15:08:12 +0000422
Chris Lattnerc82daef2010-07-11 22:24:20 +0000423 // Check to see if we have a function *definition* which must have a body.
424 if (AllowFunctionDefinitions && D.isFunctionDeclarator() &&
425 // Look at the next token to make sure that this isn't a function
426 // declaration. We have to check this because __attribute__ might be the
427 // start of a function definition in GCC-extended K&R C.
428 !isDeclarationAfterDeclarator()) {
429
Chris Lattner004659a2010-07-11 22:42:07 +0000430 if (isStartOfFunctionDefinition(D)) {
John McCalld8ac0572009-11-03 19:26:08 +0000431 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
432 Diag(Tok, diag::err_function_declared_typedef);
433
434 // Recover by treating the 'typedef' as spurious.
435 DS.ClearStorageClassSpecs();
436 }
437
John McCalld226f652010-08-21 09:40:31 +0000438 Decl *TheDecl = ParseFunctionDefinition(D);
John McCalld8ac0572009-11-03 19:26:08 +0000439 return Actions.ConvertDeclToDeclGroup(TheDecl);
Chris Lattner004659a2010-07-11 22:42:07 +0000440 }
441
442 if (isDeclarationSpecifier()) {
443 // If there is an invalid declaration specifier right after the function
444 // prototype, then we must be in a missing semicolon case where this isn't
445 // actually a body. Just fall through into the code that handles it as a
446 // prototype, and let the top-level code handle the erroneous declspec
447 // where it would otherwise expect a comma or semicolon.
John McCalld8ac0572009-11-03 19:26:08 +0000448 } else {
449 Diag(Tok, diag::err_expected_fn_body);
450 SkipUntil(tok::semi);
451 return DeclGroupPtrTy();
452 }
453 }
454
John McCalld226f652010-08-21 09:40:31 +0000455 llvm::SmallVector<Decl *, 8> DeclsInGroup;
456 Decl *FirstDecl = ParseDeclarationAfterDeclarator(D);
John McCall54abf7d2009-11-04 02:18:39 +0000457 D.complete(FirstDecl);
John McCalld226f652010-08-21 09:40:31 +0000458 if (FirstDecl)
John McCalld8ac0572009-11-03 19:26:08 +0000459 DeclsInGroup.push_back(FirstDecl);
460
461 // If we don't have a comma, it is either the end of the list (a ';') or an
462 // error, bail out.
463 while (Tok.is(tok::comma)) {
464 // Consume the comma.
Chris Lattner23c4b182009-03-29 17:18:04 +0000465 ConsumeToken();
John McCalld8ac0572009-11-03 19:26:08 +0000466
467 // Parse the next declarator.
468 D.clear();
469
470 // Accept attributes in an init-declarator. In the first declarator in a
471 // declaration, these would be part of the declspec. In subsequent
472 // declarators, they become part of the declarator itself, so that they
473 // don't apply to declarators after *this* one. Examples:
474 // short __attribute__((common)) var; -> declspec
475 // short var __attribute__((common)); -> declarator
476 // short x, __attribute__((common)) var; -> declarator
477 if (Tok.is(tok::kw___attribute)) {
478 SourceLocation Loc;
Sean Huntbbd37c62009-11-21 08:43:09 +0000479 AttributeList *AttrList = ParseGNUAttributes(&Loc);
John McCalld8ac0572009-11-03 19:26:08 +0000480 D.AddAttributes(AttrList, Loc);
481 }
482
483 ParseDeclarator(D);
484
John McCalld226f652010-08-21 09:40:31 +0000485 Decl *ThisDecl = ParseDeclarationAfterDeclarator(D);
John McCall54abf7d2009-11-04 02:18:39 +0000486 D.complete(ThisDecl);
John McCalld226f652010-08-21 09:40:31 +0000487 if (ThisDecl)
John McCalld8ac0572009-11-03 19:26:08 +0000488 DeclsInGroup.push_back(ThisDecl);
489 }
490
491 if (DeclEnd)
492 *DeclEnd = Tok.getLocation();
493
494 if (Context != Declarator::ForContext &&
495 ExpectAndConsume(tok::semi,
496 Context == Declarator::FileContext
497 ? diag::err_invalid_token_after_toplevel_declarator
498 : diag::err_expected_semi_declaration)) {
Chris Lattner004659a2010-07-11 22:42:07 +0000499 // Okay, there was no semicolon and one was expected. If we see a
500 // declaration specifier, just assume it was missing and continue parsing.
501 // Otherwise things are very confused and we skip to recover.
502 if (!isDeclarationSpecifier()) {
503 SkipUntil(tok::r_brace, true, true);
504 if (Tok.is(tok::semi))
505 ConsumeToken();
506 }
John McCalld8ac0572009-11-03 19:26:08 +0000507 }
508
Douglas Gregor23c94db2010-07-02 17:43:08 +0000509 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS,
John McCalld8ac0572009-11-03 19:26:08 +0000510 DeclsInGroup.data(),
511 DeclsInGroup.size());
Reid Spencer5f016e22007-07-11 17:01:13 +0000512}
513
Douglas Gregor1426e532009-05-12 21:31:51 +0000514/// \brief Parse 'declaration' after parsing 'declaration-specifiers
515/// declarator'. This method parses the remainder of the declaration
516/// (including any attributes or initializer, among other things) and
517/// finalizes the declaration.
Reid Spencer5f016e22007-07-11 17:01:13 +0000518///
Reid Spencer5f016e22007-07-11 17:01:13 +0000519/// init-declarator: [C99 6.7]
520/// declarator
521/// declarator '=' initializer
522/// [GNU] declarator simple-asm-expr[opt] attributes[opt]
523/// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +0000524/// [C++] declarator initializer[opt]
525///
526/// [C++] initializer:
527/// [C++] '=' initializer-clause
528/// [C++] '(' expression-list ')'
Sebastian Redl50de12f2009-03-24 22:27:57 +0000529/// [C++0x] '=' 'default' [TODO]
530/// [C++0x] '=' 'delete'
531///
532/// According to the standard grammar, =default and =delete are function
533/// definitions, but that definitely doesn't fit with the parser here.
Reid Spencer5f016e22007-07-11 17:01:13 +0000534///
John McCalld226f652010-08-21 09:40:31 +0000535Decl *Parser::ParseDeclarationAfterDeclarator(Declarator &D,
Douglas Gregore542c862009-06-23 23:11:28 +0000536 const ParsedTemplateInfo &TemplateInfo) {
Douglas Gregor1426e532009-05-12 21:31:51 +0000537 // If a simple-asm-expr is present, parse it.
538 if (Tok.is(tok::kw_asm)) {
539 SourceLocation Loc;
John McCall60d7b3a2010-08-24 06:29:42 +0000540 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
Douglas Gregor1426e532009-05-12 21:31:51 +0000541 if (AsmLabel.isInvalid()) {
542 SkipUntil(tok::semi, true, true);
John McCalld226f652010-08-21 09:40:31 +0000543 return 0;
Douglas Gregor1426e532009-05-12 21:31:51 +0000544 }
Mike Stump1eb44332009-09-09 15:08:12 +0000545
Douglas Gregor1426e532009-05-12 21:31:51 +0000546 D.setAsmLabel(AsmLabel.release());
547 D.SetRangeEnd(Loc);
548 }
Mike Stump1eb44332009-09-09 15:08:12 +0000549
Douglas Gregor1426e532009-05-12 21:31:51 +0000550 // If attributes are present, parse them.
551 if (Tok.is(tok::kw___attribute)) {
552 SourceLocation Loc;
Sean Huntbbd37c62009-11-21 08:43:09 +0000553 AttributeList *AttrList = ParseGNUAttributes(&Loc);
Douglas Gregor1426e532009-05-12 21:31:51 +0000554 D.AddAttributes(AttrList, Loc);
555 }
Mike Stump1eb44332009-09-09 15:08:12 +0000556
Douglas Gregor1426e532009-05-12 21:31:51 +0000557 // Inform the current actions module that we just parsed this declarator.
John McCalld226f652010-08-21 09:40:31 +0000558 Decl *ThisDecl = 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +0000559 switch (TemplateInfo.Kind) {
560 case ParsedTemplateInfo::NonTemplate:
Douglas Gregor23c94db2010-07-02 17:43:08 +0000561 ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
Douglas Gregord5a423b2009-09-25 18:43:00 +0000562 break;
563
564 case ParsedTemplateInfo::Template:
565 case ParsedTemplateInfo::ExplicitSpecialization:
Douglas Gregor23c94db2010-07-02 17:43:08 +0000566 ThisDecl = Actions.ActOnTemplateDeclarator(getCurScope(),
John McCallf312b1e2010-08-26 23:41:50 +0000567 MultiTemplateParamsArg(Actions,
Douglas Gregore542c862009-06-23 23:11:28 +0000568 TemplateInfo.TemplateParams->data(),
569 TemplateInfo.TemplateParams->size()),
Douglas Gregord5a423b2009-09-25 18:43:00 +0000570 D);
571 break;
572
573 case ParsedTemplateInfo::ExplicitInstantiation: {
John McCalld226f652010-08-21 09:40:31 +0000574 DeclResult ThisRes
Douglas Gregor23c94db2010-07-02 17:43:08 +0000575 = Actions.ActOnExplicitInstantiation(getCurScope(),
Douglas Gregord5a423b2009-09-25 18:43:00 +0000576 TemplateInfo.ExternLoc,
577 TemplateInfo.TemplateLoc,
578 D);
579 if (ThisRes.isInvalid()) {
580 SkipUntil(tok::semi, true, true);
John McCalld226f652010-08-21 09:40:31 +0000581 return 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +0000582 }
583
584 ThisDecl = ThisRes.get();
585 break;
586 }
587 }
Mike Stump1eb44332009-09-09 15:08:12 +0000588
Douglas Gregor1426e532009-05-12 21:31:51 +0000589 // Parse declarator '=' initializer.
590 if (Tok.is(tok::equal)) {
591 ConsumeToken();
Anders Carlsson37bf9d22010-09-24 21:25:25 +0000592 if (Tok.is(tok::kw_delete)) {
Douglas Gregor1426e532009-05-12 21:31:51 +0000593 SourceLocation DelLoc = ConsumeToken();
Anders Carlsson37bf9d22010-09-24 21:25:25 +0000594
595 if (!getLang().CPlusPlus0x)
596 Diag(DelLoc, diag::warn_deleted_function_accepted_as_extension);
597
Douglas Gregor1426e532009-05-12 21:31:51 +0000598 Actions.SetDeclDeleted(ThisDecl, DelLoc);
599 } else {
John McCall731ad842009-12-19 09:28:58 +0000600 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
601 EnterScope(0);
Douglas Gregor23c94db2010-07-02 17:43:08 +0000602 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
John McCall731ad842009-12-19 09:28:58 +0000603 }
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +0000604
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +0000605 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000606 Actions.CodeCompleteInitializer(getCurScope(), ThisDecl);
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +0000607 ConsumeCodeCompletionToken();
608 SkipUntil(tok::comma, true, true);
609 return ThisDecl;
610 }
611
John McCall60d7b3a2010-08-24 06:29:42 +0000612 ExprResult Init(ParseInitializer());
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +0000613
John McCall731ad842009-12-19 09:28:58 +0000614 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000615 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
John McCall731ad842009-12-19 09:28:58 +0000616 ExitScope();
617 }
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +0000618
Douglas Gregor1426e532009-05-12 21:31:51 +0000619 if (Init.isInvalid()) {
Douglas Gregor00225542010-03-01 18:27:54 +0000620 SkipUntil(tok::comma, true, true);
621 Actions.ActOnInitializerError(ThisDecl);
622 } else
John McCall9ae2f072010-08-23 23:25:46 +0000623 Actions.AddInitializerToDecl(ThisDecl, Init.take());
Douglas Gregor1426e532009-05-12 21:31:51 +0000624 }
625 } else if (Tok.is(tok::l_paren)) {
626 // Parse C++ direct initializer: '(' expression-list ')'
627 SourceLocation LParenLoc = ConsumeParen();
628 ExprVector Exprs(Actions);
629 CommaLocsTy CommaLocs;
630
Douglas Gregorb4debae2009-12-22 17:47:17 +0000631 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
632 EnterScope(0);
Douglas Gregor23c94db2010-07-02 17:43:08 +0000633 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +0000634 }
635
Douglas Gregor1426e532009-05-12 21:31:51 +0000636 if (ParseExpressionList(Exprs, CommaLocs)) {
637 SkipUntil(tok::r_paren);
Douglas Gregorb4debae2009-12-22 17:47:17 +0000638
639 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000640 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +0000641 ExitScope();
642 }
Douglas Gregor1426e532009-05-12 21:31:51 +0000643 } else {
644 // Match the ')'.
645 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
646
647 assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() &&
648 "Unexpected number of commas!");
Douglas Gregorb4debae2009-12-22 17:47:17 +0000649
650 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000651 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +0000652 ExitScope();
653 }
654
Douglas Gregor1426e532009-05-12 21:31:51 +0000655 Actions.AddCXXDirectInitializerToDecl(ThisDecl, LParenLoc,
656 move_arg(Exprs),
Douglas Gregora1a04782010-09-09 16:33:13 +0000657 RParenLoc);
Douglas Gregor1426e532009-05-12 21:31:51 +0000658 }
659 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000660 bool TypeContainsUndeducedAuto =
Anders Carlsson6a75cd92009-07-11 00:34:39 +0000661 D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto;
662 Actions.ActOnUninitializedDecl(ThisDecl, TypeContainsUndeducedAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +0000663 }
664
665 return ThisDecl;
666}
667
Reid Spencer5f016e22007-07-11 17:01:13 +0000668/// ParseSpecifierQualifierList
669/// specifier-qualifier-list:
670/// type-specifier specifier-qualifier-list[opt]
671/// type-qualifier specifier-qualifier-list[opt]
672/// [GNU] attributes specifier-qualifier-list[opt]
673///
674void Parser::ParseSpecifierQualifierList(DeclSpec &DS) {
675 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
676 /// parse declaration-specifiers and complain about extra stuff.
Reid Spencer5f016e22007-07-11 17:01:13 +0000677 ParseDeclarationSpecifiers(DS);
Mike Stump1eb44332009-09-09 15:08:12 +0000678
Reid Spencer5f016e22007-07-11 17:01:13 +0000679 // Validate declspec for type-name.
680 unsigned Specs = DS.getParsedSpecifiers();
Chris Lattnerb6645dd2009-04-14 21:16:09 +0000681 if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers() &&
682 !DS.getAttributes())
Reid Spencer5f016e22007-07-11 17:01:13 +0000683 Diag(Tok, diag::err_typename_requires_specqual);
Mike Stump1eb44332009-09-09 15:08:12 +0000684
Reid Spencer5f016e22007-07-11 17:01:13 +0000685 // Issue diagnostic and remove storage class if present.
686 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
687 if (DS.getStorageClassSpecLoc().isValid())
688 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
689 else
690 Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass);
691 DS.ClearStorageClassSpecs();
692 }
Mike Stump1eb44332009-09-09 15:08:12 +0000693
Reid Spencer5f016e22007-07-11 17:01:13 +0000694 // Issue diagnostic and remove function specfier if present.
695 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Douglas Gregorb48fe382008-10-31 09:07:45 +0000696 if (DS.isInlineSpecified())
697 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
698 if (DS.isVirtualSpecified())
699 Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec);
700 if (DS.isExplicitSpecified())
701 Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec);
Reid Spencer5f016e22007-07-11 17:01:13 +0000702 DS.ClearFunctionSpecs();
703 }
704}
705
Chris Lattnerc199ab32009-04-12 20:42:31 +0000706/// isValidAfterIdentifierInDeclaratorAfterDeclSpec - Return true if the
707/// specified token is valid after the identifier in a declarator which
708/// immediately follows the declspec. For example, these things are valid:
709///
710/// int x [ 4]; // direct-declarator
711/// int x ( int y); // direct-declarator
712/// int(int x ) // direct-declarator
713/// int x ; // simple-declaration
714/// int x = 17; // init-declarator-list
715/// int x , y; // init-declarator-list
716/// int x __asm__ ("foo"); // init-declarator-list
Chris Lattnerb6645dd2009-04-14 21:16:09 +0000717/// int x : 4; // struct-declarator
Chris Lattnerc83c27a2009-04-12 22:29:43 +0000718/// int x { 5}; // C++'0x unified initializers
Chris Lattnerc199ab32009-04-12 20:42:31 +0000719///
720/// This is not, because 'x' does not immediately follow the declspec (though
721/// ')' happens to be valid anyway).
722/// int (x)
723///
724static bool isValidAfterIdentifierInDeclarator(const Token &T) {
725 return T.is(tok::l_square) || T.is(tok::l_paren) || T.is(tok::r_paren) ||
726 T.is(tok::semi) || T.is(tok::comma) || T.is(tok::equal) ||
Chris Lattnerb6645dd2009-04-14 21:16:09 +0000727 T.is(tok::kw_asm) || T.is(tok::l_brace) || T.is(tok::colon);
Chris Lattnerc199ab32009-04-12 20:42:31 +0000728}
729
Chris Lattnere40c2952009-04-14 21:34:55 +0000730
731/// ParseImplicitInt - This method is called when we have an non-typename
732/// identifier in a declspec (which normally terminates the decl spec) when
733/// the declspec has no type specifier. In this case, the declspec is either
734/// malformed or is "implicit int" (in K&R and C89).
735///
736/// This method handles diagnosing this prettily and returns false if the
737/// declspec is done being processed. If it recovers and thinks there may be
738/// other pieces of declspec after it, it returns true.
739///
Chris Lattnerf4382f52009-04-14 22:17:06 +0000740bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000741 const ParsedTemplateInfo &TemplateInfo,
Chris Lattnere40c2952009-04-14 21:34:55 +0000742 AccessSpecifier AS) {
Chris Lattnerf4382f52009-04-14 22:17:06 +0000743 assert(Tok.is(tok::identifier) && "should have identifier");
Mike Stump1eb44332009-09-09 15:08:12 +0000744
Chris Lattnere40c2952009-04-14 21:34:55 +0000745 SourceLocation Loc = Tok.getLocation();
746 // If we see an identifier that is not a type name, we normally would
747 // parse it as the identifer being declared. However, when a typename
748 // is typo'd or the definition is not included, this will incorrectly
749 // parse the typename as the identifier name and fall over misparsing
750 // later parts of the diagnostic.
751 //
752 // As such, we try to do some look-ahead in cases where this would
753 // otherwise be an "implicit-int" case to see if this is invalid. For
754 // example: "static foo_t x = 4;" In this case, if we parsed foo_t as
755 // an identifier with implicit int, we'd get a parse error because the
756 // next token is obviously invalid for a type. Parse these as a case
757 // with an invalid type specifier.
758 assert(!DS.hasTypeSpecifier() && "Type specifier checked above");
Mike Stump1eb44332009-09-09 15:08:12 +0000759
Chris Lattnere40c2952009-04-14 21:34:55 +0000760 // Since we know that this either implicit int (which is rare) or an
761 // error, we'd do lookahead to try to do better recovery.
762 if (isValidAfterIdentifierInDeclarator(NextToken())) {
763 // If this token is valid for implicit int, e.g. "static x = 4", then
764 // we just avoid eating the identifier, so it will be parsed as the
765 // identifier in the declarator.
766 return false;
767 }
Mike Stump1eb44332009-09-09 15:08:12 +0000768
Chris Lattnere40c2952009-04-14 21:34:55 +0000769 // Otherwise, if we don't consume this token, we are going to emit an
770 // error anyway. Try to recover from various common problems. Check
771 // to see if this was a reference to a tag name without a tag specified.
772 // This is a common problem in C (saying 'foo' instead of 'struct foo').
Chris Lattnerf4382f52009-04-14 22:17:06 +0000773 //
774 // C++ doesn't need this, and isTagName doesn't take SS.
775 if (SS == 0) {
776 const char *TagName = 0;
777 tok::TokenKind TagKind = tok::unknown;
Mike Stump1eb44332009-09-09 15:08:12 +0000778
Douglas Gregor23c94db2010-07-02 17:43:08 +0000779 switch (Actions.isTagName(*Tok.getIdentifierInfo(), getCurScope())) {
Chris Lattnere40c2952009-04-14 21:34:55 +0000780 default: break;
781 case DeclSpec::TST_enum: TagName="enum" ;TagKind=tok::kw_enum ;break;
782 case DeclSpec::TST_union: TagName="union" ;TagKind=tok::kw_union ;break;
783 case DeclSpec::TST_struct:TagName="struct";TagKind=tok::kw_struct;break;
784 case DeclSpec::TST_class: TagName="class" ;TagKind=tok::kw_class ;break;
785 }
Mike Stump1eb44332009-09-09 15:08:12 +0000786
Chris Lattnerf4382f52009-04-14 22:17:06 +0000787 if (TagName) {
788 Diag(Loc, diag::err_use_of_tag_name_without_tag)
John McCall23e907a2010-02-14 01:03:10 +0000789 << Tok.getIdentifierInfo() << TagName << getLang().CPlusPlus
Douglas Gregor849b2432010-03-31 17:46:05 +0000790 << FixItHint::CreateInsertion(Tok.getLocation(),TagName);
Mike Stump1eb44332009-09-09 15:08:12 +0000791
Chris Lattnerf4382f52009-04-14 22:17:06 +0000792 // Parse this as a tag as if the missing tag were present.
793 if (TagKind == tok::kw_enum)
Douglas Gregor9b9edd62010-03-02 17:53:14 +0000794 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS);
Chris Lattnerf4382f52009-04-14 22:17:06 +0000795 else
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000796 ParseClassSpecifier(TagKind, Loc, DS, TemplateInfo, AS);
Chris Lattnerf4382f52009-04-14 22:17:06 +0000797 return true;
798 }
Chris Lattnere40c2952009-04-14 21:34:55 +0000799 }
Mike Stump1eb44332009-09-09 15:08:12 +0000800
Douglas Gregora786fdb2009-10-13 23:27:22 +0000801 // This is almost certainly an invalid type name. Let the action emit a
802 // diagnostic and attempt to recover.
John McCallb3d87482010-08-24 05:47:05 +0000803 ParsedType T;
Douglas Gregora786fdb2009-10-13 23:27:22 +0000804 if (Actions.DiagnoseUnknownTypeName(*Tok.getIdentifierInfo(), Loc,
Douglas Gregor23c94db2010-07-02 17:43:08 +0000805 getCurScope(), SS, T)) {
Douglas Gregora786fdb2009-10-13 23:27:22 +0000806 // The action emitted a diagnostic, so we don't have to.
807 if (T) {
808 // The action has suggested that the type T could be used. Set that as
809 // the type in the declaration specifiers, consume the would-be type
810 // name token, and we're done.
811 const char *PrevSpec;
812 unsigned DiagID;
John McCallb3d87482010-08-24 05:47:05 +0000813 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID, T);
Douglas Gregora786fdb2009-10-13 23:27:22 +0000814 DS.SetRangeEnd(Tok.getLocation());
815 ConsumeToken();
816
817 // There may be other declaration specifiers after this.
818 return true;
819 }
820
821 // Fall through; the action had no suggestion for us.
822 } else {
823 // The action did not emit a diagnostic, so emit one now.
824 SourceRange R;
825 if (SS) R = SS->getRange();
826 Diag(Loc, diag::err_unknown_typename) << Tok.getIdentifierInfo() << R;
827 }
Mike Stump1eb44332009-09-09 15:08:12 +0000828
Douglas Gregora786fdb2009-10-13 23:27:22 +0000829 // Mark this as an error.
Chris Lattnere40c2952009-04-14 21:34:55 +0000830 const char *PrevSpec;
John McCallfec54012009-08-03 20:12:06 +0000831 unsigned DiagID;
832 DS.SetTypeSpecType(DeclSpec::TST_error, Loc, PrevSpec, DiagID);
Chris Lattnere40c2952009-04-14 21:34:55 +0000833 DS.SetRangeEnd(Tok.getLocation());
834 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000835
Chris Lattnere40c2952009-04-14 21:34:55 +0000836 // TODO: Could inject an invalid typedef decl in an enclosing scope to
837 // avoid rippling error messages on subsequent uses of the same type,
838 // could be useful if #include was forgotten.
839 return false;
840}
841
Douglas Gregor0efc2c12010-01-13 17:31:36 +0000842/// \brief Determine the declaration specifier context from the declarator
843/// context.
844///
845/// \param Context the declarator context, which is one of the
846/// Declarator::TheContext enumerator values.
847Parser::DeclSpecContext
848Parser::getDeclSpecContextFromDeclaratorContext(unsigned Context) {
849 if (Context == Declarator::MemberContext)
850 return DSC_class;
851 if (Context == Declarator::FileContext)
852 return DSC_top_level;
853 return DSC_normal;
854}
855
Reid Spencer5f016e22007-07-11 17:01:13 +0000856/// ParseDeclarationSpecifiers
857/// declaration-specifiers: [C99 6.7]
858/// storage-class-specifier declaration-specifiers[opt]
859/// type-specifier declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +0000860/// [C99] function-specifier declaration-specifiers[opt]
861/// [GNU] attributes declaration-specifiers[opt]
862///
863/// storage-class-specifier: [C99 6.7.1]
864/// 'typedef'
865/// 'extern'
866/// 'static'
867/// 'auto'
868/// 'register'
Sebastian Redl669d5d72008-11-14 23:42:31 +0000869/// [C++] 'mutable'
Reid Spencer5f016e22007-07-11 17:01:13 +0000870/// [GNU] '__thread'
Reid Spencer5f016e22007-07-11 17:01:13 +0000871/// function-specifier: [C99 6.7.4]
872/// [C99] 'inline'
Douglas Gregorb48fe382008-10-31 09:07:45 +0000873/// [C++] 'virtual'
874/// [C++] 'explicit'
Anders Carlssonf47f7a12009-05-06 04:46:28 +0000875/// 'friend': [C++ dcl.friend]
Sebastian Redl2ac67232009-11-05 15:47:02 +0000876/// 'constexpr': [C++0x dcl.constexpr]
Anders Carlssonf47f7a12009-05-06 04:46:28 +0000877
Reid Spencer5f016e22007-07-11 17:01:13 +0000878///
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000879void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000880 const ParsedTemplateInfo &TemplateInfo,
John McCall67d1a672009-08-06 02:15:43 +0000881 AccessSpecifier AS,
Douglas Gregor2ccccb32010-08-23 18:23:48 +0000882 DeclSpecContext DSContext) {
Chris Lattner81c018d2008-03-13 06:29:04 +0000883 DS.SetRangeStart(Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +0000884 while (1) {
John McCallfec54012009-08-03 20:12:06 +0000885 bool isInvalid = false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000886 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +0000887 unsigned DiagID = 0;
888
Reid Spencer5f016e22007-07-11 17:01:13 +0000889 SourceLocation Loc = Tok.getLocation();
Douglas Gregor12e083c2008-11-07 15:42:26 +0000890
Reid Spencer5f016e22007-07-11 17:01:13 +0000891 switch (Tok.getKind()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000892 default:
Chris Lattnerbce61352008-07-26 00:20:22 +0000893 DoneWithDeclSpec:
Reid Spencer5f016e22007-07-11 17:01:13 +0000894 // If this is not a declaration specifier token, we're done reading decl
895 // specifiers. First verify that DeclSpec's are consistent.
Douglas Gregor9b3064b2009-04-01 22:41:11 +0000896 DS.Finish(Diags, PP);
Reid Spencer5f016e22007-07-11 17:01:13 +0000897 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000898
Douglas Gregor2ccccb32010-08-23 18:23:48 +0000899 case tok::code_completion: {
John McCallf312b1e2010-08-26 23:41:50 +0000900 Sema::ParserCompletionContext CCC = Sema::PCC_Namespace;
Douglas Gregor2ccccb32010-08-23 18:23:48 +0000901 if (DS.hasTypeSpecifier()) {
902 bool AllowNonIdentifiers
903 = (getCurScope()->getFlags() & (Scope::ControlScope |
904 Scope::BlockScope |
905 Scope::TemplateParamScope |
906 Scope::FunctionPrototypeScope |
907 Scope::AtCatchScope)) == 0;
908 bool AllowNestedNameSpecifiers
909 = DSContext == DSC_top_level ||
910 (DSContext == DSC_class && DS.isFriendSpecified());
911
Douglas Gregorc7b6d882010-09-16 15:14:18 +0000912 Actions.CodeCompleteDeclSpec(getCurScope(), DS,
913 AllowNonIdentifiers,
914 AllowNestedNameSpecifiers);
Douglas Gregor2ccccb32010-08-23 18:23:48 +0000915 ConsumeCodeCompletionToken();
916 return;
917 }
918
919 if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate)
John McCallf312b1e2010-08-26 23:41:50 +0000920 CCC = DSContext == DSC_class? Sema::PCC_MemberTemplate
921 : Sema::PCC_Template;
Douglas Gregor2ccccb32010-08-23 18:23:48 +0000922 else if (DSContext == DSC_class)
John McCallf312b1e2010-08-26 23:41:50 +0000923 CCC = Sema::PCC_Class;
Douglas Gregor2ccccb32010-08-23 18:23:48 +0000924 else if (ObjCImpDecl)
John McCallf312b1e2010-08-26 23:41:50 +0000925 CCC = Sema::PCC_ObjCImplementation;
Douglas Gregor2ccccb32010-08-23 18:23:48 +0000926
927 Actions.CodeCompleteOrdinaryName(getCurScope(), CCC);
928 ConsumeCodeCompletionToken();
929 return;
930 }
931
Chris Lattner5e02c472009-01-05 00:07:25 +0000932 case tok::coloncolon: // ::foo::bar
John McCall9ba61662010-02-26 08:45:28 +0000933 // C++ scope specifier. Annotate and loop, or bail out on error.
934 if (TryAnnotateCXXScopeToken(true)) {
935 if (!DS.hasTypeSpecifier())
936 DS.SetTypeSpecError();
937 goto DoneWithDeclSpec;
938 }
John McCall2e0a7152010-03-01 18:20:46 +0000939 if (Tok.is(tok::coloncolon)) // ::new or ::delete
940 goto DoneWithDeclSpec;
John McCall9ba61662010-02-26 08:45:28 +0000941 continue;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000942
943 case tok::annot_cxxscope: {
944 if (DS.hasTypeSpecifier())
945 goto DoneWithDeclSpec;
946
John McCallaa87d332009-12-12 11:40:51 +0000947 CXXScopeSpec SS;
John McCallca0408f2010-08-23 06:44:23 +0000948 SS.setScopeRep((NestedNameSpecifier*) Tok.getAnnotationValue());
John McCallaa87d332009-12-12 11:40:51 +0000949 SS.setRange(Tok.getAnnotationRange());
950
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000951 // We are looking for a qualified typename.
Douglas Gregor9135c722009-03-25 15:40:00 +0000952 Token Next = NextToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000953 if (Next.is(tok::annot_template_id) &&
Douglas Gregor9135c722009-03-25 15:40:00 +0000954 static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue())
Douglas Gregorc45c2322009-03-31 00:43:58 +0000955 ->Kind == TNK_Type_template) {
Douglas Gregor9135c722009-03-25 15:40:00 +0000956 // We have a qualified template-id, e.g., N::A<int>
Douglas Gregor0efc2c12010-01-13 17:31:36 +0000957
958 // C++ [class.qual]p2:
959 // In a lookup in which the constructor is an acceptable lookup
960 // result and the nested-name-specifier nominates a class C:
961 //
962 // - if the name specified after the
963 // nested-name-specifier, when looked up in C, is the
964 // injected-class-name of C (Clause 9), or
965 //
966 // - if the name specified after the nested-name-specifier
967 // is the same as the identifier or the
968 // simple-template-id's template-name in the last
969 // component of the nested-name-specifier,
970 //
971 // the name is instead considered to name the constructor of
972 // class C.
973 //
974 // Thus, if the template-name is actually the constructor
975 // name, then the code is ill-formed; this interpretation is
976 // reinforced by the NAD status of core issue 635.
977 TemplateIdAnnotation *TemplateId
978 = static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue());
John McCallba9d8532010-04-13 06:39:49 +0000979 if ((DSContext == DSC_top_level ||
980 (DSContext == DSC_class && DS.isFriendSpecified())) &&
981 TemplateId->Name &&
Douglas Gregor23c94db2010-07-02 17:43:08 +0000982 Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) {
Douglas Gregor0efc2c12010-01-13 17:31:36 +0000983 if (isConstructorDeclarator()) {
984 // The user meant this to be an out-of-line constructor
985 // definition, but template arguments are not allowed
986 // there. Just allow this as a constructor; we'll
987 // complain about it later.
988 goto DoneWithDeclSpec;
989 }
990
991 // The user meant this to name a type, but it actually names
992 // a constructor with some extraneous template
993 // arguments. Complain, then parse it as a type as the user
994 // intended.
995 Diag(TemplateId->TemplateNameLoc,
996 diag::err_out_of_line_template_id_names_constructor)
997 << TemplateId->Name;
998 }
999
John McCallaa87d332009-12-12 11:40:51 +00001000 DS.getTypeSpecScope() = SS;
1001 ConsumeToken(); // The C++ scope.
Mike Stump1eb44332009-09-09 15:08:12 +00001002 assert(Tok.is(tok::annot_template_id) &&
Douglas Gregor9135c722009-03-25 15:40:00 +00001003 "ParseOptionalCXXScopeSpecifier not working");
1004 AnnotateTemplateIdTokenAsType(&SS);
1005 continue;
1006 }
1007
Douglas Gregor9d7b3532009-09-28 07:26:33 +00001008 if (Next.is(tok::annot_typename)) {
John McCallaa87d332009-12-12 11:40:51 +00001009 DS.getTypeSpecScope() = SS;
1010 ConsumeToken(); // The C++ scope.
John McCallb3d87482010-08-24 05:47:05 +00001011 if (Tok.getAnnotationValue()) {
1012 ParsedType T = getTypeAnnotation(Tok);
Douglas Gregor9d7b3532009-09-28 07:26:33 +00001013 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc,
John McCallb3d87482010-08-24 05:47:05 +00001014 PrevSpec, DiagID, T);
1015 }
Douglas Gregor9d7b3532009-09-28 07:26:33 +00001016 else
1017 DS.SetTypeSpecError();
1018 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1019 ConsumeToken(); // The typename
1020 }
1021
Douglas Gregor9135c722009-03-25 15:40:00 +00001022 if (Next.isNot(tok::identifier))
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001023 goto DoneWithDeclSpec;
1024
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001025 // If we're in a context where the identifier could be a class name,
1026 // check whether this is a constructor declaration.
John McCallba9d8532010-04-13 06:39:49 +00001027 if ((DSContext == DSC_top_level ||
1028 (DSContext == DSC_class && DS.isFriendSpecified())) &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00001029 Actions.isCurrentClassName(*Next.getIdentifierInfo(), getCurScope(),
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001030 &SS)) {
1031 if (isConstructorDeclarator())
1032 goto DoneWithDeclSpec;
1033
1034 // As noted in C++ [class.qual]p2 (cited above), when the name
1035 // of the class is qualified in a context where it could name
1036 // a constructor, its a constructor name. However, we've
1037 // looked at the declarator, and the user probably meant this
1038 // to be a type. Complain that it isn't supposed to be treated
1039 // as a type, then proceed to parse it as a type.
1040 Diag(Next.getLocation(), diag::err_out_of_line_type_names_constructor)
1041 << Next.getIdentifierInfo();
1042 }
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001043
John McCallb3d87482010-08-24 05:47:05 +00001044 ParsedType TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(),
1045 Next.getLocation(),
1046 getCurScope(), &SS);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001047
Chris Lattnerf4382f52009-04-14 22:17:06 +00001048 // If the referenced identifier is not a type, then this declspec is
1049 // erroneous: We already checked about that it has no type specifier, and
1050 // C++ doesn't have implicit int. Diagnose it as a typo w.r.t. to the
Mike Stump1eb44332009-09-09 15:08:12 +00001051 // typename.
Chris Lattnerf4382f52009-04-14 22:17:06 +00001052 if (TypeRep == 0) {
1053 ConsumeToken(); // Eat the scope spec so the identifier is current.
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001054 if (ParseImplicitInt(DS, &SS, TemplateInfo, AS)) continue;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001055 goto DoneWithDeclSpec;
Chris Lattnerf4382f52009-04-14 22:17:06 +00001056 }
Mike Stump1eb44332009-09-09 15:08:12 +00001057
John McCallaa87d332009-12-12 11:40:51 +00001058 DS.getTypeSpecScope() = SS;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001059 ConsumeToken(); // The C++ scope.
1060
Douglas Gregor1a51b4a2009-02-09 15:09:02 +00001061 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001062 DiagID, TypeRep);
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001063 if (isInvalid)
1064 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001065
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001066 DS.SetRangeEnd(Tok.getLocation());
1067 ConsumeToken(); // The typename.
1068
1069 continue;
1070 }
Mike Stump1eb44332009-09-09 15:08:12 +00001071
Chris Lattner80d0c892009-01-21 19:48:37 +00001072 case tok::annot_typename: {
John McCallb3d87482010-08-24 05:47:05 +00001073 if (Tok.getAnnotationValue()) {
1074 ParsedType T = getTypeAnnotation(Tok);
Douglas Gregor31a19b62009-04-01 21:51:26 +00001075 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallb3d87482010-08-24 05:47:05 +00001076 DiagID, T);
1077 } else
Douglas Gregor31a19b62009-04-01 21:51:26 +00001078 DS.SetTypeSpecError();
Chris Lattner5c5db552010-04-05 18:18:31 +00001079
1080 if (isInvalid)
1081 break;
1082
Chris Lattner80d0c892009-01-21 19:48:37 +00001083 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1084 ConsumeToken(); // The typename
Mike Stump1eb44332009-09-09 15:08:12 +00001085
Chris Lattner80d0c892009-01-21 19:48:37 +00001086 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1087 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
1088 // Objective-C interface. If we don't have Objective-C or a '<', this is
1089 // just a normal reference to a typedef name.
1090 if (!Tok.is(tok::less) || !getLang().ObjC1)
1091 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001092
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001093 SourceLocation LAngleLoc, EndProtoLoc;
John McCalld226f652010-08-21 09:40:31 +00001094 llvm::SmallVector<Decl *, 8> ProtocolDecl;
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001095 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
1096 ParseObjCProtocolReferences(ProtocolDecl, ProtocolLocs, false,
1097 LAngleLoc, EndProtoLoc);
1098 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size(),
1099 ProtocolLocs.data(), LAngleLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001100
Chris Lattner80d0c892009-01-21 19:48:37 +00001101 DS.SetRangeEnd(EndProtoLoc);
1102 continue;
1103 }
Mike Stump1eb44332009-09-09 15:08:12 +00001104
Chris Lattner3bd934a2008-07-26 01:18:38 +00001105 // typedef-name
1106 case tok::identifier: {
Chris Lattner5e02c472009-01-05 00:07:25 +00001107 // In C++, check to see if this is a scope specifier like foo::bar::, if
1108 // so handle it as such. This is important for ctor parsing.
John McCall9ba61662010-02-26 08:45:28 +00001109 if (getLang().CPlusPlus) {
1110 if (TryAnnotateCXXScopeToken(true)) {
1111 if (!DS.hasTypeSpecifier())
1112 DS.SetTypeSpecError();
1113 goto DoneWithDeclSpec;
1114 }
1115 if (!Tok.is(tok::identifier))
1116 continue;
1117 }
Mike Stump1eb44332009-09-09 15:08:12 +00001118
Chris Lattner3bd934a2008-07-26 01:18:38 +00001119 // This identifier can only be a typedef name if we haven't already seen
1120 // a type-specifier. Without this check we misparse:
1121 // typedef int X; struct Y { short X; }; as 'short int'.
1122 if (DS.hasTypeSpecifier())
1123 goto DoneWithDeclSpec;
Mike Stump1eb44332009-09-09 15:08:12 +00001124
John Thompson82287d12010-02-05 00:12:22 +00001125 // Check for need to substitute AltiVec keyword tokens.
1126 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
1127 break;
1128
Chris Lattner3bd934a2008-07-26 01:18:38 +00001129 // It has to be available as a typedef too!
John McCallb3d87482010-08-24 05:47:05 +00001130 ParsedType TypeRep =
1131 Actions.getTypeName(*Tok.getIdentifierInfo(),
1132 Tok.getLocation(), getCurScope());
Douglas Gregor55f6b142009-02-09 18:46:07 +00001133
Chris Lattnerc199ab32009-04-12 20:42:31 +00001134 // If this is not a typedef name, don't parse it as part of the declspec,
1135 // it must be an implicit int or an error.
John McCallb3d87482010-08-24 05:47:05 +00001136 if (!TypeRep) {
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001137 if (ParseImplicitInt(DS, 0, TemplateInfo, AS)) continue;
Chris Lattner3bd934a2008-07-26 01:18:38 +00001138 goto DoneWithDeclSpec;
Chris Lattnerc199ab32009-04-12 20:42:31 +00001139 }
Douglas Gregor55f6b142009-02-09 18:46:07 +00001140
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001141 // If we're in a context where the identifier could be a class name,
1142 // check whether this is a constructor declaration.
1143 if (getLang().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00001144 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), getCurScope()) &&
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001145 isConstructorDeclarator())
Douglas Gregorb48fe382008-10-31 09:07:45 +00001146 goto DoneWithDeclSpec;
1147
Douglas Gregor1a51b4a2009-02-09 15:09:02 +00001148 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001149 DiagID, TypeRep);
Chris Lattner3bd934a2008-07-26 01:18:38 +00001150 if (isInvalid)
1151 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001152
Chris Lattner3bd934a2008-07-26 01:18:38 +00001153 DS.SetRangeEnd(Tok.getLocation());
1154 ConsumeToken(); // The identifier
1155
1156 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1157 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
1158 // Objective-C interface. If we don't have Objective-C or a '<', this is
1159 // just a normal reference to a typedef name.
1160 if (!Tok.is(tok::less) || !getLang().ObjC1)
1161 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001162
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001163 SourceLocation LAngleLoc, EndProtoLoc;
John McCalld226f652010-08-21 09:40:31 +00001164 llvm::SmallVector<Decl *, 8> ProtocolDecl;
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001165 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
1166 ParseObjCProtocolReferences(ProtocolDecl, ProtocolLocs, false,
1167 LAngleLoc, EndProtoLoc);
1168 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size(),
1169 ProtocolLocs.data(), LAngleLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001170
Chris Lattner3bd934a2008-07-26 01:18:38 +00001171 DS.SetRangeEnd(EndProtoLoc);
1172
Steve Naroff4f9b9f12008-09-22 10:28:57 +00001173 // Need to support trailing type qualifiers (e.g. "id<p> const").
1174 // If a type specifier follows, it will be diagnosed elsewhere.
1175 continue;
Chris Lattner3bd934a2008-07-26 01:18:38 +00001176 }
Douglas Gregor39a8de12009-02-25 19:37:18 +00001177
1178 // type-name
1179 case tok::annot_template_id: {
Mike Stump1eb44332009-09-09 15:08:12 +00001180 TemplateIdAnnotation *TemplateId
Douglas Gregor39a8de12009-02-25 19:37:18 +00001181 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
Douglas Gregorc45c2322009-03-31 00:43:58 +00001182 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor39a8de12009-02-25 19:37:18 +00001183 // This template-id does not refer to a type name, so we're
1184 // done with the type-specifiers.
1185 goto DoneWithDeclSpec;
1186 }
1187
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001188 // If we're in a context where the template-id could be a
1189 // constructor name or specialization, check whether this is a
1190 // constructor declaration.
1191 if (getLang().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00001192 Actions.isCurrentClassName(*TemplateId->Name, getCurScope()) &&
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001193 isConstructorDeclarator())
1194 goto DoneWithDeclSpec;
1195
Douglas Gregor39a8de12009-02-25 19:37:18 +00001196 // Turn the template-id annotation token into a type annotation
1197 // token, then try again to parse it as a type-specifier.
Douglas Gregor31a19b62009-04-01 21:51:26 +00001198 AnnotateTemplateIdTokenAsType();
Douglas Gregor39a8de12009-02-25 19:37:18 +00001199 continue;
1200 }
1201
Reid Spencer5f016e22007-07-11 17:01:13 +00001202 // GNU attributes support.
1203 case tok::kw___attribute:
Sean Huntbbd37c62009-11-21 08:43:09 +00001204 DS.AddAttributes(ParseGNUAttributes());
Reid Spencer5f016e22007-07-11 17:01:13 +00001205 continue;
Steve Narofff59e17e2008-12-24 20:59:21 +00001206
1207 // Microsoft declspec support.
1208 case tok::kw___declspec:
Eli Friedmana23b4852009-06-08 07:21:15 +00001209 DS.AddAttributes(ParseMicrosoftDeclSpec());
Steve Narofff59e17e2008-12-24 20:59:21 +00001210 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001211
Steve Naroff239f0732008-12-25 14:16:32 +00001212 // Microsoft single token adornments.
Steve Naroff86bc6cf2008-12-25 14:41:26 +00001213 case tok::kw___forceinline:
Eli Friedman290eeb02009-06-08 23:27:34 +00001214 // FIXME: Add handling here!
1215 break;
1216
1217 case tok::kw___ptr64:
Steve Naroff86bc6cf2008-12-25 14:41:26 +00001218 case tok::kw___w64:
Steve Naroff239f0732008-12-25 14:16:32 +00001219 case tok::kw___cdecl:
1220 case tok::kw___stdcall:
1221 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00001222 case tok::kw___thiscall:
Eli Friedman290eeb02009-06-08 23:27:34 +00001223 DS.AddAttributes(ParseMicrosoftTypeAttributes());
1224 continue;
1225
Dawn Perchik52fc3142010-09-03 01:29:35 +00001226 // Borland single token adornments.
1227 case tok::kw___pascal:
1228 DS.AddAttributes(ParseBorlandTypeAttributes());
1229 continue;
1230
Reid Spencer5f016e22007-07-11 17:01:13 +00001231 // storage-class-specifier
1232 case tok::kw_typedef:
John McCallfec54012009-08-03 20:12:06 +00001233 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_typedef, Loc, PrevSpec,
1234 DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001235 break;
1236 case tok::kw_extern:
1237 if (DS.isThreadSpecified())
Chris Lattner1ab3b962008-11-18 07:48:38 +00001238 Diag(Tok, diag::ext_thread_before) << "extern";
John McCallfec54012009-08-03 20:12:06 +00001239 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_extern, Loc, PrevSpec,
1240 DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001241 break;
Steve Naroff8d54bf22007-12-18 00:16:02 +00001242 case tok::kw___private_extern__:
Chris Lattnerf97409f2008-04-06 06:57:35 +00001243 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_private_extern, Loc,
John McCallfec54012009-08-03 20:12:06 +00001244 PrevSpec, DiagID);
Steve Naroff8d54bf22007-12-18 00:16:02 +00001245 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00001246 case tok::kw_static:
1247 if (DS.isThreadSpecified())
Chris Lattner1ab3b962008-11-18 07:48:38 +00001248 Diag(Tok, diag::ext_thread_before) << "static";
John McCallfec54012009-08-03 20:12:06 +00001249 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_static, Loc, PrevSpec,
1250 DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001251 break;
1252 case tok::kw_auto:
Anders Carlssone89d1592009-06-26 18:41:36 +00001253 if (getLang().CPlusPlus0x)
John McCallfec54012009-08-03 20:12:06 +00001254 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec,
1255 DiagID);
Anders Carlssone89d1592009-06-26 18:41:36 +00001256 else
John McCallfec54012009-08-03 20:12:06 +00001257 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, Loc, PrevSpec,
1258 DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001259 break;
1260 case tok::kw_register:
John McCallfec54012009-08-03 20:12:06 +00001261 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_register, Loc, PrevSpec,
1262 DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001263 break;
Sebastian Redl669d5d72008-11-14 23:42:31 +00001264 case tok::kw_mutable:
John McCallfec54012009-08-03 20:12:06 +00001265 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_mutable, Loc, PrevSpec,
1266 DiagID);
Sebastian Redl669d5d72008-11-14 23:42:31 +00001267 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00001268 case tok::kw___thread:
John McCallfec54012009-08-03 20:12:06 +00001269 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001270 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001271
Reid Spencer5f016e22007-07-11 17:01:13 +00001272 // function-specifier
1273 case tok::kw_inline:
John McCallfec54012009-08-03 20:12:06 +00001274 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001275 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +00001276 case tok::kw_virtual:
John McCallfec54012009-08-03 20:12:06 +00001277 isInvalid = DS.SetFunctionSpecVirtual(Loc, PrevSpec, DiagID);
Douglas Gregorb48fe382008-10-31 09:07:45 +00001278 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +00001279 case tok::kw_explicit:
John McCallfec54012009-08-03 20:12:06 +00001280 isInvalid = DS.SetFunctionSpecExplicit(Loc, PrevSpec, DiagID);
Douglas Gregorb48fe382008-10-31 09:07:45 +00001281 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00001282
Anders Carlssonf47f7a12009-05-06 04:46:28 +00001283 // friend
1284 case tok::kw_friend:
John McCall67d1a672009-08-06 02:15:43 +00001285 if (DSContext == DSC_class)
1286 isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID);
1287 else {
1288 PrevSpec = ""; // not actually used by the diagnostic
1289 DiagID = diag::err_friend_invalid_in_context;
1290 isInvalid = true;
1291 }
Anders Carlssonf47f7a12009-05-06 04:46:28 +00001292 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001293
Sebastian Redl2ac67232009-11-05 15:47:02 +00001294 // constexpr
1295 case tok::kw_constexpr:
1296 isInvalid = DS.SetConstexprSpec(Loc, PrevSpec, DiagID);
1297 break;
1298
Chris Lattner80d0c892009-01-21 19:48:37 +00001299 // type-specifier
1300 case tok::kw_short:
John McCallfec54012009-08-03 20:12:06 +00001301 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec,
1302 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001303 break;
1304 case tok::kw_long:
1305 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCallfec54012009-08-03 20:12:06 +00001306 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
1307 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001308 else
John McCallfec54012009-08-03 20:12:06 +00001309 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
1310 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001311 break;
1312 case tok::kw_signed:
John McCallfec54012009-08-03 20:12:06 +00001313 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec,
1314 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001315 break;
1316 case tok::kw_unsigned:
John McCallfec54012009-08-03 20:12:06 +00001317 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
1318 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001319 break;
1320 case tok::kw__Complex:
John McCallfec54012009-08-03 20:12:06 +00001321 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
1322 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001323 break;
1324 case tok::kw__Imaginary:
John McCallfec54012009-08-03 20:12:06 +00001325 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
1326 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001327 break;
1328 case tok::kw_void:
John McCallfec54012009-08-03 20:12:06 +00001329 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec,
1330 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001331 break;
1332 case tok::kw_char:
John McCallfec54012009-08-03 20:12:06 +00001333 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec,
1334 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001335 break;
1336 case tok::kw_int:
John McCallfec54012009-08-03 20:12:06 +00001337 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec,
1338 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001339 break;
1340 case tok::kw_float:
John McCallfec54012009-08-03 20:12:06 +00001341 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec,
1342 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001343 break;
1344 case tok::kw_double:
John McCallfec54012009-08-03 20:12:06 +00001345 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec,
1346 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001347 break;
1348 case tok::kw_wchar_t:
John McCallfec54012009-08-03 20:12:06 +00001349 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec,
1350 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001351 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001352 case tok::kw_char16_t:
John McCallfec54012009-08-03 20:12:06 +00001353 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec,
1354 DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001355 break;
1356 case tok::kw_char32_t:
John McCallfec54012009-08-03 20:12:06 +00001357 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec,
1358 DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001359 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00001360 case tok::kw_bool:
1361 case tok::kw__Bool:
John McCallfec54012009-08-03 20:12:06 +00001362 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec,
1363 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001364 break;
1365 case tok::kw__Decimal32:
John McCallfec54012009-08-03 20:12:06 +00001366 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
1367 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001368 break;
1369 case tok::kw__Decimal64:
John McCallfec54012009-08-03 20:12:06 +00001370 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
1371 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001372 break;
1373 case tok::kw__Decimal128:
John McCallfec54012009-08-03 20:12:06 +00001374 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
1375 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001376 break;
John Thompson82287d12010-02-05 00:12:22 +00001377 case tok::kw___vector:
1378 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
1379 break;
1380 case tok::kw___pixel:
1381 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
1382 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00001383
1384 // class-specifier:
1385 case tok::kw_class:
1386 case tok::kw_struct:
Chris Lattner4c97d762009-04-12 21:49:30 +00001387 case tok::kw_union: {
1388 tok::TokenKind Kind = Tok.getKind();
1389 ConsumeToken();
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001390 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS);
Chris Lattner80d0c892009-01-21 19:48:37 +00001391 continue;
Chris Lattner4c97d762009-04-12 21:49:30 +00001392 }
Chris Lattner80d0c892009-01-21 19:48:37 +00001393
1394 // enum-specifier:
1395 case tok::kw_enum:
Chris Lattner4c97d762009-04-12 21:49:30 +00001396 ConsumeToken();
Douglas Gregor9b9edd62010-03-02 17:53:14 +00001397 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS);
Chris Lattner80d0c892009-01-21 19:48:37 +00001398 continue;
1399
1400 // cv-qualifier:
1401 case tok::kw_const:
John McCallfec54012009-08-03 20:12:06 +00001402 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec, DiagID,
1403 getLang());
Chris Lattner80d0c892009-01-21 19:48:37 +00001404 break;
1405 case tok::kw_volatile:
John McCallfec54012009-08-03 20:12:06 +00001406 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
1407 getLang());
Chris Lattner80d0c892009-01-21 19:48:37 +00001408 break;
1409 case tok::kw_restrict:
John McCallfec54012009-08-03 20:12:06 +00001410 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
1411 getLang());
Chris Lattner80d0c892009-01-21 19:48:37 +00001412 break;
1413
Douglas Gregord57959a2009-03-27 23:10:48 +00001414 // C++ typename-specifier:
1415 case tok::kw_typename:
John McCall9ba61662010-02-26 08:45:28 +00001416 if (TryAnnotateTypeOrScopeToken()) {
1417 DS.SetTypeSpecError();
1418 goto DoneWithDeclSpec;
1419 }
1420 if (!Tok.is(tok::kw_typename))
Douglas Gregord57959a2009-03-27 23:10:48 +00001421 continue;
1422 break;
1423
Chris Lattner80d0c892009-01-21 19:48:37 +00001424 // GNU typeof support.
1425 case tok::kw_typeof:
1426 ParseTypeofSpecifier(DS);
1427 continue;
1428
Anders Carlsson6fd634f2009-06-24 17:47:40 +00001429 case tok::kw_decltype:
1430 ParseDecltypeSpecifier(DS);
1431 continue;
1432
Steve Naroffd3ded1f2008-06-05 00:02:44 +00001433 case tok::less:
Chris Lattner3bd934a2008-07-26 01:18:38 +00001434 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattnerbce61352008-07-26 00:20:22 +00001435 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
1436 // but we support it.
Chris Lattner3bd934a2008-07-26 01:18:38 +00001437 if (DS.hasTypeSpecifier() || !getLang().ObjC1)
Chris Lattnerbce61352008-07-26 00:20:22 +00001438 goto DoneWithDeclSpec;
Mike Stump1eb44332009-09-09 15:08:12 +00001439
Chris Lattnerbce61352008-07-26 00:20:22 +00001440 {
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001441 SourceLocation LAngleLoc, EndProtoLoc;
John McCalld226f652010-08-21 09:40:31 +00001442 llvm::SmallVector<Decl *, 8> ProtocolDecl;
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001443 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
1444 ParseObjCProtocolReferences(ProtocolDecl, ProtocolLocs, false,
1445 LAngleLoc, EndProtoLoc);
1446 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size(),
1447 ProtocolLocs.data(), LAngleLoc);
Chris Lattner3bd934a2008-07-26 01:18:38 +00001448 DS.SetRangeEnd(EndProtoLoc);
1449
Chris Lattner1ab3b962008-11-18 07:48:38 +00001450 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
Douglas Gregor849b2432010-03-31 17:46:05 +00001451 << FixItHint::CreateInsertion(Loc, "id")
Chris Lattner1ab3b962008-11-18 07:48:38 +00001452 << SourceRange(Loc, EndProtoLoc);
Steve Naroff4f9b9f12008-09-22 10:28:57 +00001453 // Need to support trailing type qualifiers (e.g. "id<p> const").
1454 // If a type specifier follows, it will be diagnosed elsewhere.
1455 continue;
Steve Naroffd3ded1f2008-06-05 00:02:44 +00001456 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001457 }
John McCallfec54012009-08-03 20:12:06 +00001458 // If the specifier wasn't legal, issue a diagnostic.
Reid Spencer5f016e22007-07-11 17:01:13 +00001459 if (isInvalid) {
1460 assert(PrevSpec && "Method did not return previous specifier!");
John McCallfec54012009-08-03 20:12:06 +00001461 assert(DiagID);
Douglas Gregorae2fb142010-08-23 14:34:43 +00001462
1463 if (DiagID == diag::ext_duplicate_declspec)
1464 Diag(Tok, DiagID)
1465 << PrevSpec << FixItHint::CreateRemoval(Tok.getLocation());
1466 else
1467 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00001468 }
Chris Lattner81c018d2008-03-13 06:29:04 +00001469 DS.SetRangeEnd(Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00001470 ConsumeToken();
1471 }
1472}
Douglas Gregoradcac882008-12-01 23:54:00 +00001473
Chris Lattner7a0ab5f2009-01-06 06:59:53 +00001474/// ParseOptionalTypeSpecifier - Try to parse a single type-specifier. We
Douglas Gregor12e083c2008-11-07 15:42:26 +00001475/// primarily follow the C++ grammar with additions for C99 and GNU,
1476/// which together subsume the C grammar. Note that the C++
1477/// type-specifier also includes the C type-qualifier (for const,
1478/// volatile, and C99 restrict). Returns true if a type-specifier was
1479/// found (and parsed), false otherwise.
1480///
1481/// type-specifier: [C++ 7.1.5]
1482/// simple-type-specifier
1483/// class-specifier
1484/// enum-specifier
1485/// elaborated-type-specifier [TODO]
1486/// cv-qualifier
1487///
1488/// cv-qualifier: [C++ 7.1.5.1]
1489/// 'const'
1490/// 'volatile'
1491/// [C99] 'restrict'
1492///
1493/// simple-type-specifier: [ C++ 7.1.5.2]
1494/// '::'[opt] nested-name-specifier[opt] type-name [TODO]
1495/// '::'[opt] nested-name-specifier 'template' template-id [TODO]
1496/// 'char'
1497/// 'wchar_t'
1498/// 'bool'
1499/// 'short'
1500/// 'int'
1501/// 'long'
1502/// 'signed'
1503/// 'unsigned'
1504/// 'float'
1505/// 'double'
1506/// 'void'
1507/// [C99] '_Bool'
1508/// [C99] '_Complex'
1509/// [C99] '_Imaginary' // Removed in TC2?
1510/// [GNU] '_Decimal32'
1511/// [GNU] '_Decimal64'
1512/// [GNU] '_Decimal128'
1513/// [GNU] typeof-specifier
1514/// [OBJC] class-name objc-protocol-refs[opt] [TODO]
1515/// [OBJC] typedef-name objc-protocol-refs[opt] [TODO]
Anders Carlsson6fd634f2009-06-24 17:47:40 +00001516/// [C++0x] 'decltype' ( expression )
John Thompson82287d12010-02-05 00:12:22 +00001517/// [AltiVec] '__vector'
John McCallfec54012009-08-03 20:12:06 +00001518bool Parser::ParseOptionalTypeSpecifier(DeclSpec &DS, bool& isInvalid,
Chris Lattner7a0ab5f2009-01-06 06:59:53 +00001519 const char *&PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001520 unsigned &DiagID,
Sebastian Redld9bafa72010-02-03 21:21:43 +00001521 const ParsedTemplateInfo &TemplateInfo,
1522 bool SuppressDeclarations) {
Douglas Gregor12e083c2008-11-07 15:42:26 +00001523 SourceLocation Loc = Tok.getLocation();
1524
1525 switch (Tok.getKind()) {
Chris Lattner166a8fc2009-01-04 23:41:41 +00001526 case tok::identifier: // foo::bar
Douglas Gregorc0b39642010-04-15 23:40:53 +00001527 // If we already have a type specifier, this identifier is not a type.
1528 if (DS.getTypeSpecType() != DeclSpec::TST_unspecified ||
1529 DS.getTypeSpecWidth() != DeclSpec::TSW_unspecified ||
1530 DS.getTypeSpecSign() != DeclSpec::TSS_unspecified)
1531 return false;
John Thompson82287d12010-02-05 00:12:22 +00001532 // Check for need to substitute AltiVec keyword tokens.
1533 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
1534 break;
1535 // Fall through.
Douglas Gregord57959a2009-03-27 23:10:48 +00001536 case tok::kw_typename: // typename foo::bar
Chris Lattner166a8fc2009-01-04 23:41:41 +00001537 // Annotate typenames and C++ scope specifiers. If we get one, just
1538 // recurse to handle whatever we get.
1539 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00001540 return true;
1541 if (Tok.is(tok::identifier))
1542 return false;
1543 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
1544 TemplateInfo, SuppressDeclarations);
Chris Lattner166a8fc2009-01-04 23:41:41 +00001545 case tok::coloncolon: // ::foo::bar
1546 if (NextToken().is(tok::kw_new) || // ::new
1547 NextToken().is(tok::kw_delete)) // ::delete
1548 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001549
Chris Lattner166a8fc2009-01-04 23:41:41 +00001550 // Annotate typenames and C++ scope specifiers. If we get one, just
1551 // recurse to handle whatever we get.
1552 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00001553 return true;
1554 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
1555 TemplateInfo, SuppressDeclarations);
Mike Stump1eb44332009-09-09 15:08:12 +00001556
Douglas Gregor12e083c2008-11-07 15:42:26 +00001557 // simple-type-specifier:
Chris Lattnerb31757b2009-01-06 05:06:21 +00001558 case tok::annot_typename: {
John McCallb3d87482010-08-24 05:47:05 +00001559 if (ParsedType T = getTypeAnnotation(Tok)) {
Douglas Gregor31a19b62009-04-01 21:51:26 +00001560 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallb3d87482010-08-24 05:47:05 +00001561 DiagID, T);
1562 } else
Douglas Gregor31a19b62009-04-01 21:51:26 +00001563 DS.SetTypeSpecError();
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001564 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1565 ConsumeToken(); // The typename
Mike Stump1eb44332009-09-09 15:08:12 +00001566
Douglas Gregor12e083c2008-11-07 15:42:26 +00001567 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1568 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
1569 // Objective-C interface. If we don't have Objective-C or a '<', this is
1570 // just a normal reference to a typedef name.
1571 if (!Tok.is(tok::less) || !getLang().ObjC1)
1572 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00001573
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001574 SourceLocation LAngleLoc, EndProtoLoc;
John McCalld226f652010-08-21 09:40:31 +00001575 llvm::SmallVector<Decl *, 8> ProtocolDecl;
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001576 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
1577 ParseObjCProtocolReferences(ProtocolDecl, ProtocolLocs, false,
1578 LAngleLoc, EndProtoLoc);
1579 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size(),
1580 ProtocolLocs.data(), LAngleLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001581
Douglas Gregor12e083c2008-11-07 15:42:26 +00001582 DS.SetRangeEnd(EndProtoLoc);
1583 return true;
1584 }
1585
1586 case tok::kw_short:
John McCallfec54012009-08-03 20:12:06 +00001587 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001588 break;
1589 case tok::kw_long:
1590 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCallfec54012009-08-03 20:12:06 +00001591 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
1592 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001593 else
John McCallfec54012009-08-03 20:12:06 +00001594 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
1595 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001596 break;
1597 case tok::kw_signed:
John McCallfec54012009-08-03 20:12:06 +00001598 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001599 break;
1600 case tok::kw_unsigned:
John McCallfec54012009-08-03 20:12:06 +00001601 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
1602 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001603 break;
1604 case tok::kw__Complex:
John McCallfec54012009-08-03 20:12:06 +00001605 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
1606 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001607 break;
1608 case tok::kw__Imaginary:
John McCallfec54012009-08-03 20:12:06 +00001609 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
1610 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001611 break;
1612 case tok::kw_void:
John McCallfec54012009-08-03 20:12:06 +00001613 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001614 break;
1615 case tok::kw_char:
John McCallfec54012009-08-03 20:12:06 +00001616 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001617 break;
1618 case tok::kw_int:
John McCallfec54012009-08-03 20:12:06 +00001619 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001620 break;
1621 case tok::kw_float:
John McCallfec54012009-08-03 20:12:06 +00001622 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001623 break;
1624 case tok::kw_double:
John McCallfec54012009-08-03 20:12:06 +00001625 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001626 break;
1627 case tok::kw_wchar_t:
John McCallfec54012009-08-03 20:12:06 +00001628 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001629 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001630 case tok::kw_char16_t:
John McCallfec54012009-08-03 20:12:06 +00001631 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001632 break;
1633 case tok::kw_char32_t:
John McCallfec54012009-08-03 20:12:06 +00001634 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001635 break;
Douglas Gregor12e083c2008-11-07 15:42:26 +00001636 case tok::kw_bool:
1637 case tok::kw__Bool:
John McCallfec54012009-08-03 20:12:06 +00001638 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001639 break;
1640 case tok::kw__Decimal32:
John McCallfec54012009-08-03 20:12:06 +00001641 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
1642 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001643 break;
1644 case tok::kw__Decimal64:
John McCallfec54012009-08-03 20:12:06 +00001645 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
1646 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001647 break;
1648 case tok::kw__Decimal128:
John McCallfec54012009-08-03 20:12:06 +00001649 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
1650 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001651 break;
John Thompson82287d12010-02-05 00:12:22 +00001652 case tok::kw___vector:
1653 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
1654 break;
1655 case tok::kw___pixel:
1656 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
1657 break;
1658
Douglas Gregor12e083c2008-11-07 15:42:26 +00001659 // class-specifier:
1660 case tok::kw_class:
1661 case tok::kw_struct:
Chris Lattner4c97d762009-04-12 21:49:30 +00001662 case tok::kw_union: {
1663 tok::TokenKind Kind = Tok.getKind();
1664 ConsumeToken();
Sebastian Redld9bafa72010-02-03 21:21:43 +00001665 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS_none,
1666 SuppressDeclarations);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001667 return true;
Chris Lattner4c97d762009-04-12 21:49:30 +00001668 }
Douglas Gregor12e083c2008-11-07 15:42:26 +00001669
1670 // enum-specifier:
1671 case tok::kw_enum:
Chris Lattner4c97d762009-04-12 21:49:30 +00001672 ConsumeToken();
Douglas Gregor9b9edd62010-03-02 17:53:14 +00001673 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS_none);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001674 return true;
1675
1676 // cv-qualifier:
1677 case tok::kw_const:
1678 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001679 DiagID, getLang());
Douglas Gregor12e083c2008-11-07 15:42:26 +00001680 break;
1681 case tok::kw_volatile:
1682 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001683 DiagID, getLang());
Douglas Gregor12e083c2008-11-07 15:42:26 +00001684 break;
1685 case tok::kw_restrict:
1686 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001687 DiagID, getLang());
Douglas Gregor12e083c2008-11-07 15:42:26 +00001688 break;
1689
1690 // GNU typeof support.
1691 case tok::kw_typeof:
1692 ParseTypeofSpecifier(DS);
1693 return true;
1694
Anders Carlsson6fd634f2009-06-24 17:47:40 +00001695 // C++0x decltype support.
1696 case tok::kw_decltype:
1697 ParseDecltypeSpecifier(DS);
1698 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00001699
Anders Carlsson0b7f7892009-06-26 23:44:14 +00001700 // C++0x auto support.
1701 case tok::kw_auto:
1702 if (!getLang().CPlusPlus0x)
1703 return false;
1704
John McCallfec54012009-08-03 20:12:06 +00001705 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec, DiagID);
Anders Carlsson0b7f7892009-06-26 23:44:14 +00001706 break;
Dawn Perchik52fc3142010-09-03 01:29:35 +00001707
Eli Friedman290eeb02009-06-08 23:27:34 +00001708 case tok::kw___ptr64:
1709 case tok::kw___w64:
Steve Naroff239f0732008-12-25 14:16:32 +00001710 case tok::kw___cdecl:
1711 case tok::kw___stdcall:
1712 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00001713 case tok::kw___thiscall:
Eli Friedman290eeb02009-06-08 23:27:34 +00001714 DS.AddAttributes(ParseMicrosoftTypeAttributes());
Chris Lattner837acd02009-01-21 19:19:26 +00001715 return true;
Steve Naroff239f0732008-12-25 14:16:32 +00001716
Dawn Perchik52fc3142010-09-03 01:29:35 +00001717 case tok::kw___pascal:
1718 DS.AddAttributes(ParseBorlandTypeAttributes());
1719 return true;
1720
Douglas Gregor12e083c2008-11-07 15:42:26 +00001721 default:
1722 // Not a type-specifier; do nothing.
1723 return false;
1724 }
1725
1726 // If the specifier combination wasn't legal, issue a diagnostic.
1727 if (isInvalid) {
1728 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner1ab3b962008-11-18 07:48:38 +00001729 // Pick between error or extwarn.
Chris Lattner1ab3b962008-11-18 07:48:38 +00001730 Diag(Tok, DiagID) << PrevSpec;
Douglas Gregor12e083c2008-11-07 15:42:26 +00001731 }
1732 DS.SetRangeEnd(Tok.getLocation());
1733 ConsumeToken(); // whatever we parsed above.
1734 return true;
1735}
Reid Spencer5f016e22007-07-11 17:01:13 +00001736
Chris Lattnercd4b83c2007-10-29 04:42:53 +00001737/// ParseStructDeclaration - Parse a struct declaration without the terminating
1738/// semicolon.
1739///
Reid Spencer5f016e22007-07-11 17:01:13 +00001740/// struct-declaration:
Chris Lattnercd4b83c2007-10-29 04:42:53 +00001741/// specifier-qualifier-list struct-declarator-list
Reid Spencer5f016e22007-07-11 17:01:13 +00001742/// [GNU] __extension__ struct-declaration
Chris Lattnercd4b83c2007-10-29 04:42:53 +00001743/// [GNU] specifier-qualifier-list
Reid Spencer5f016e22007-07-11 17:01:13 +00001744/// struct-declarator-list:
1745/// struct-declarator
1746/// struct-declarator-list ',' struct-declarator
1747/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
1748/// struct-declarator:
1749/// declarator
1750/// [GNU] declarator attributes[opt]
1751/// declarator[opt] ':' constant-expression
1752/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
1753///
Chris Lattnere1359422008-04-10 06:46:29 +00001754void Parser::
John McCallbdd563e2009-11-03 02:38:08 +00001755ParseStructDeclaration(DeclSpec &DS, FieldCallback &Fields) {
Chris Lattnerc46d1a12008-10-20 06:45:43 +00001756 if (Tok.is(tok::kw___extension__)) {
1757 // __extension__ silences extension warnings in the subexpression.
1758 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroff28a7ca82007-08-20 22:28:22 +00001759 ConsumeToken();
Chris Lattnerc46d1a12008-10-20 06:45:43 +00001760 return ParseStructDeclaration(DS, Fields);
1761 }
Mike Stump1eb44332009-09-09 15:08:12 +00001762
Steve Naroff28a7ca82007-08-20 22:28:22 +00001763 // Parse the common specifier-qualifiers-list piece.
Chris Lattner60b1e3e2008-04-10 06:15:14 +00001764 SourceLocation DSStart = Tok.getLocation();
Steve Naroff28a7ca82007-08-20 22:28:22 +00001765 ParseSpecifierQualifierList(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00001766
Douglas Gregor4920f1f2009-01-12 22:49:06 +00001767 // If there are no declarators, this is a free-standing declaration
1768 // specifier. Let the actions module cope with it.
Chris Lattner04d66662007-10-09 17:33:22 +00001769 if (Tok.is(tok::semi)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001770 Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none, DS);
Steve Naroff28a7ca82007-08-20 22:28:22 +00001771 return;
1772 }
1773
1774 // Read struct-declarators until we find the semicolon.
John McCallbdd563e2009-11-03 02:38:08 +00001775 bool FirstDeclarator = true;
Steve Naroff28a7ca82007-08-20 22:28:22 +00001776 while (1) {
John McCall54abf7d2009-11-04 02:18:39 +00001777 ParsingDeclRAIIObject PD(*this);
John McCallbdd563e2009-11-03 02:38:08 +00001778 FieldDeclarator DeclaratorInfo(DS);
1779
1780 // Attributes are only allowed here on successive declarators.
1781 if (!FirstDeclarator && Tok.is(tok::kw___attribute)) {
1782 SourceLocation Loc;
Sean Huntbbd37c62009-11-21 08:43:09 +00001783 AttributeList *AttrList = ParseGNUAttributes(&Loc);
John McCallbdd563e2009-11-03 02:38:08 +00001784 DeclaratorInfo.D.AddAttributes(AttrList, Loc);
1785 }
Mike Stump1eb44332009-09-09 15:08:12 +00001786
Steve Naroff28a7ca82007-08-20 22:28:22 +00001787 /// struct-declarator: declarator
1788 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattnera1efc8c2009-12-10 01:59:24 +00001789 if (Tok.isNot(tok::colon)) {
1790 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
1791 ColonProtectionRAIIObject X(*this);
Chris Lattnere1359422008-04-10 06:46:29 +00001792 ParseDeclarator(DeclaratorInfo.D);
Chris Lattnera1efc8c2009-12-10 01:59:24 +00001793 }
Mike Stump1eb44332009-09-09 15:08:12 +00001794
Chris Lattner04d66662007-10-09 17:33:22 +00001795 if (Tok.is(tok::colon)) {
Steve Naroff28a7ca82007-08-20 22:28:22 +00001796 ConsumeToken();
John McCall60d7b3a2010-08-24 06:29:42 +00001797 ExprResult Res(ParseConstantExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001798 if (Res.isInvalid())
Steve Naroff28a7ca82007-08-20 22:28:22 +00001799 SkipUntil(tok::semi, true, true);
Chris Lattner60b1e3e2008-04-10 06:15:14 +00001800 else
Sebastian Redleffa8d12008-12-10 00:02:53 +00001801 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroff28a7ca82007-08-20 22:28:22 +00001802 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00001803
Steve Naroff28a7ca82007-08-20 22:28:22 +00001804 // If attributes exist after the declarator, parse them.
Sebastian Redlab197ba2009-02-09 18:23:29 +00001805 if (Tok.is(tok::kw___attribute)) {
1806 SourceLocation Loc;
Sean Huntbbd37c62009-11-21 08:43:09 +00001807 AttributeList *AttrList = ParseGNUAttributes(&Loc);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001808 DeclaratorInfo.D.AddAttributes(AttrList, Loc);
1809 }
1810
John McCallbdd563e2009-11-03 02:38:08 +00001811 // We're done with this declarator; invoke the callback.
John McCalld226f652010-08-21 09:40:31 +00001812 Decl *D = Fields.invoke(DeclaratorInfo);
John McCall54abf7d2009-11-04 02:18:39 +00001813 PD.complete(D);
John McCallbdd563e2009-11-03 02:38:08 +00001814
Steve Naroff28a7ca82007-08-20 22:28:22 +00001815 // If we don't have a comma, it is either the end of the list (a ';')
1816 // or an error, bail out.
Chris Lattner04d66662007-10-09 17:33:22 +00001817 if (Tok.isNot(tok::comma))
Chris Lattnercd4b83c2007-10-29 04:42:53 +00001818 return;
Sebastian Redlab197ba2009-02-09 18:23:29 +00001819
Steve Naroff28a7ca82007-08-20 22:28:22 +00001820 // Consume the comma.
1821 ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00001822
John McCallbdd563e2009-11-03 02:38:08 +00001823 FirstDeclarator = false;
Steve Naroff28a7ca82007-08-20 22:28:22 +00001824 }
Steve Naroff28a7ca82007-08-20 22:28:22 +00001825}
1826
1827/// ParseStructUnionBody
1828/// struct-contents:
1829/// struct-declaration-list
1830/// [EXT] empty
1831/// [GNU] "struct-declaration-list" without terminatoring ';'
1832/// struct-declaration-list:
1833/// struct-declaration
1834/// struct-declaration-list struct-declaration
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00001835/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroff28a7ca82007-08-20 22:28:22 +00001836///
Reid Spencer5f016e22007-07-11 17:01:13 +00001837void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
John McCalld226f652010-08-21 09:40:31 +00001838 unsigned TagType, Decl *TagDecl) {
John McCallf312b1e2010-08-26 23:41:50 +00001839 PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc,
1840 "parsing struct/union body");
Mike Stump1eb44332009-09-09 15:08:12 +00001841
Reid Spencer5f016e22007-07-11 17:01:13 +00001842 SourceLocation LBraceLoc = ConsumeBrace();
Mike Stump1eb44332009-09-09 15:08:12 +00001843
Douglas Gregor3218c4b2009-01-09 22:42:13 +00001844 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
Douglas Gregor23c94db2010-07-02 17:43:08 +00001845 Actions.ActOnTagStartDefinition(getCurScope(), TagDecl);
Douglas Gregor72de6672009-01-08 20:45:30 +00001846
Reid Spencer5f016e22007-07-11 17:01:13 +00001847 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
1848 // C++.
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001849 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Douglas Gregor03332962010-07-29 14:29:34 +00001850 Diag(Tok, diag::ext_empty_struct_union)
1851 << (TagType == TST_union);
Reid Spencer5f016e22007-07-11 17:01:13 +00001852
John McCalld226f652010-08-21 09:40:31 +00001853 llvm::SmallVector<Decl *, 32> FieldDecls;
Chris Lattnere1359422008-04-10 06:46:29 +00001854
Reid Spencer5f016e22007-07-11 17:01:13 +00001855 // While we still have something to read, read the declarations in the struct.
Chris Lattner04d66662007-10-09 17:33:22 +00001856 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001857 // Each iteration of this loop reads one struct-declaration.
Mike Stump1eb44332009-09-09 15:08:12 +00001858
Reid Spencer5f016e22007-07-11 17:01:13 +00001859 // Check for extraneous top-level semicolon.
Chris Lattner04d66662007-10-09 17:33:22 +00001860 if (Tok.is(tok::semi)) {
Douglas Gregor9b3064b2009-04-01 22:41:11 +00001861 Diag(Tok, diag::ext_extra_struct_semi)
Douglas Gregorf13ca062010-06-16 23:08:59 +00001862 << DeclSpec::getSpecifierName((DeclSpec::TST)TagType)
Douglas Gregor849b2432010-03-31 17:46:05 +00001863 << FixItHint::CreateRemoval(Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00001864 ConsumeToken();
1865 continue;
1866 }
Chris Lattnere1359422008-04-10 06:46:29 +00001867
1868 // Parse all the comma separated declarators.
1869 DeclSpec DS;
Mike Stump1eb44332009-09-09 15:08:12 +00001870
John McCallbdd563e2009-11-03 02:38:08 +00001871 if (!Tok.is(tok::at)) {
1872 struct CFieldCallback : FieldCallback {
1873 Parser &P;
John McCalld226f652010-08-21 09:40:31 +00001874 Decl *TagDecl;
1875 llvm::SmallVectorImpl<Decl *> &FieldDecls;
John McCallbdd563e2009-11-03 02:38:08 +00001876
John McCalld226f652010-08-21 09:40:31 +00001877 CFieldCallback(Parser &P, Decl *TagDecl,
1878 llvm::SmallVectorImpl<Decl *> &FieldDecls) :
John McCallbdd563e2009-11-03 02:38:08 +00001879 P(P), TagDecl(TagDecl), FieldDecls(FieldDecls) {}
1880
John McCalld226f652010-08-21 09:40:31 +00001881 virtual Decl *invoke(FieldDeclarator &FD) {
John McCallbdd563e2009-11-03 02:38:08 +00001882 // Install the declarator into the current TagDecl.
John McCalld226f652010-08-21 09:40:31 +00001883 Decl *Field = P.Actions.ActOnField(P.getCurScope(), TagDecl,
John McCall4ba39712009-11-03 21:13:47 +00001884 FD.D.getDeclSpec().getSourceRange().getBegin(),
1885 FD.D, FD.BitfieldSize);
John McCallbdd563e2009-11-03 02:38:08 +00001886 FieldDecls.push_back(Field);
1887 return Field;
Douglas Gregor91a28862009-08-26 14:27:30 +00001888 }
John McCallbdd563e2009-11-03 02:38:08 +00001889 } Callback(*this, TagDecl, FieldDecls);
1890
1891 ParseStructDeclaration(DS, Callback);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00001892 } else { // Handle @defs
1893 ConsumeToken();
1894 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
1895 Diag(Tok, diag::err_unexpected_at);
Chris Lattner3e156ad2010-02-02 00:37:27 +00001896 SkipUntil(tok::semi, true);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00001897 continue;
1898 }
1899 ConsumeToken();
1900 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
1901 if (!Tok.is(tok::identifier)) {
1902 Diag(Tok, diag::err_expected_ident);
Chris Lattner3e156ad2010-02-02 00:37:27 +00001903 SkipUntil(tok::semi, true);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00001904 continue;
1905 }
John McCalld226f652010-08-21 09:40:31 +00001906 llvm::SmallVector<Decl *, 16> Fields;
Douglas Gregor23c94db2010-07-02 17:43:08 +00001907 Actions.ActOnDefs(getCurScope(), TagDecl, Tok.getLocation(),
Douglas Gregor44b43212008-12-11 16:49:14 +00001908 Tok.getIdentifierInfo(), Fields);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00001909 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
1910 ConsumeToken();
1911 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
Mike Stump1eb44332009-09-09 15:08:12 +00001912 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001913
Chris Lattner04d66662007-10-09 17:33:22 +00001914 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001915 ConsumeToken();
Chris Lattner04d66662007-10-09 17:33:22 +00001916 } else if (Tok.is(tok::r_brace)) {
Chris Lattner3e156ad2010-02-02 00:37:27 +00001917 ExpectAndConsume(tok::semi, diag::ext_expected_semi_decl_list);
Reid Spencer5f016e22007-07-11 17:01:13 +00001918 break;
1919 } else {
Chris Lattner3e156ad2010-02-02 00:37:27 +00001920 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
1921 // Skip to end of block or statement to avoid ext-warning on extra ';'.
Reid Spencer5f016e22007-07-11 17:01:13 +00001922 SkipUntil(tok::r_brace, true, true);
Chris Lattner3e156ad2010-02-02 00:37:27 +00001923 // If we stopped at a ';', eat it.
1924 if (Tok.is(tok::semi)) ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00001925 }
1926 }
Mike Stump1eb44332009-09-09 15:08:12 +00001927
Steve Naroff60fccee2007-10-29 21:38:07 +00001928 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001929
Ted Kremenek1e377652010-02-11 02:19:13 +00001930 llvm::OwningPtr<AttributeList> AttrList;
Reid Spencer5f016e22007-07-11 17:01:13 +00001931 // If attributes exist after struct contents, parse them.
Chris Lattner04d66662007-10-09 17:33:22 +00001932 if (Tok.is(tok::kw___attribute))
Ted Kremenek1e377652010-02-11 02:19:13 +00001933 AttrList.reset(ParseGNUAttributes());
Daniel Dunbar1bfe1c22008-10-03 02:03:53 +00001934
Douglas Gregor23c94db2010-07-02 17:43:08 +00001935 Actions.ActOnFields(getCurScope(),
Jay Foadbeaaccd2009-05-21 09:52:38 +00001936 RecordLoc, TagDecl, FieldDecls.data(), FieldDecls.size(),
Daniel Dunbar1bfe1c22008-10-03 02:03:53 +00001937 LBraceLoc, RBraceLoc,
Ted Kremenek1e377652010-02-11 02:19:13 +00001938 AttrList.get());
Douglas Gregor72de6672009-01-08 20:45:30 +00001939 StructScope.Exit();
Douglas Gregor23c94db2010-07-02 17:43:08 +00001940 Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl, RBraceLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001941}
1942
1943
1944/// ParseEnumSpecifier
1945/// enum-specifier: [C99 6.7.2.2]
1946/// 'enum' identifier[opt] '{' enumerator-list '}'
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001947///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Reid Spencer5f016e22007-07-11 17:01:13 +00001948/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
1949/// '}' attributes[opt]
1950/// 'enum' identifier
1951/// [GNU] 'enum' attributes[opt] identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001952///
1953/// [C++] elaborated-type-specifier:
1954/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
1955///
Chris Lattner4c97d762009-04-12 21:49:30 +00001956void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregor9b9edd62010-03-02 17:53:14 +00001957 const ParsedTemplateInfo &TemplateInfo,
Chris Lattner4c97d762009-04-12 21:49:30 +00001958 AccessSpecifier AS) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001959 // Parse the tag portion of this.
Douglas Gregor374929f2009-09-18 15:37:17 +00001960 if (Tok.is(tok::code_completion)) {
1961 // Code completion for an enum name.
Douglas Gregor23c94db2010-07-02 17:43:08 +00001962 Actions.CodeCompleteTag(getCurScope(), DeclSpec::TST_enum);
Douglas Gregordc845342010-05-25 05:58:43 +00001963 ConsumeCodeCompletionToken();
Douglas Gregor374929f2009-09-18 15:37:17 +00001964 }
1965
Ted Kremenek1e377652010-02-11 02:19:13 +00001966 llvm::OwningPtr<AttributeList> Attr;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001967 // If attributes exist after tag, parse them.
1968 if (Tok.is(tok::kw___attribute))
Ted Kremenek1e377652010-02-11 02:19:13 +00001969 Attr.reset(ParseGNUAttributes());
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001970
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00001971 CXXScopeSpec &SS = DS.getTypeSpecScope();
John McCall9ba61662010-02-26 08:45:28 +00001972 if (getLang().CPlusPlus) {
John McCallb3d87482010-08-24 05:47:05 +00001973 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), false))
John McCall9ba61662010-02-26 08:45:28 +00001974 return;
1975
1976 if (SS.isSet() && Tok.isNot(tok::identifier)) {
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001977 Diag(Tok, diag::err_expected_ident);
1978 if (Tok.isNot(tok::l_brace)) {
1979 // Has no name and is not a definition.
1980 // Skip the rest of this declarator, up until the comma or semicolon.
1981 SkipUntil(tok::comma, true);
1982 return;
1983 }
1984 }
1985 }
Mike Stump1eb44332009-09-09 15:08:12 +00001986
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001987 // Must have either 'enum name' or 'enum {...}'.
1988 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace)) {
1989 Diag(Tok, diag::err_expected_ident_lbrace);
Mike Stump1eb44332009-09-09 15:08:12 +00001990
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001991 // Skip the rest of this declarator, up until the comma or semicolon.
1992 SkipUntil(tok::comma, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00001993 return;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001994 }
Mike Stump1eb44332009-09-09 15:08:12 +00001995
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001996 // If an identifier is present, consume and remember it.
1997 IdentifierInfo *Name = 0;
1998 SourceLocation NameLoc;
1999 if (Tok.is(tok::identifier)) {
2000 Name = Tok.getIdentifierInfo();
2001 NameLoc = ConsumeToken();
2002 }
Mike Stump1eb44332009-09-09 15:08:12 +00002003
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002004 // There are three options here. If we have 'enum foo;', then this is a
2005 // forward declaration. If we have 'enum foo {...' then this is a
2006 // definition. Otherwise we have something like 'enum foo xyz', a reference.
2007 //
2008 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
2009 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
2010 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
2011 //
John McCallf312b1e2010-08-26 23:41:50 +00002012 Sema::TagUseKind TUK;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002013 if (Tok.is(tok::l_brace))
John McCallf312b1e2010-08-26 23:41:50 +00002014 TUK = Sema::TUK_Definition;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002015 else if (Tok.is(tok::semi))
John McCallf312b1e2010-08-26 23:41:50 +00002016 TUK = Sema::TUK_Declaration;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002017 else
John McCallf312b1e2010-08-26 23:41:50 +00002018 TUK = Sema::TUK_Reference;
Douglas Gregor8fc6d232010-05-03 17:48:54 +00002019
2020 // enums cannot be templates, although they can be referenced from a
2021 // template.
2022 if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate &&
John McCallf312b1e2010-08-26 23:41:50 +00002023 TUK != Sema::TUK_Reference) {
Douglas Gregor8fc6d232010-05-03 17:48:54 +00002024 Diag(Tok, diag::err_enum_template);
2025
2026 // Skip the rest of this declarator, up until the comma or semicolon.
2027 SkipUntil(tok::comma, true);
2028 return;
2029 }
2030
Douglas Gregor402abb52009-05-28 23:31:59 +00002031 bool Owned = false;
John McCallc4e70192009-09-11 04:59:25 +00002032 bool IsDependent = false;
Douglas Gregor48c89f42010-04-24 16:38:41 +00002033 SourceLocation TSTLoc = NameLoc.isValid()? NameLoc : StartLoc;
2034 const char *PrevSpec = 0;
2035 unsigned DiagID;
John McCalld226f652010-08-21 09:40:31 +00002036 Decl *TagDecl = Actions.ActOnTag(getCurScope(), DeclSpec::TST_enum, TUK,
2037 StartLoc, SS, Name, NameLoc, Attr.get(),
2038 AS,
John McCallf312b1e2010-08-26 23:41:50 +00002039 MultiTemplateParamsArg(Actions),
John McCalld226f652010-08-21 09:40:31 +00002040 Owned, IsDependent);
Douglas Gregor48c89f42010-04-24 16:38:41 +00002041 if (IsDependent) {
2042 // This enum has a dependent nested-name-specifier. Handle it as a
2043 // dependent tag.
2044 if (!Name) {
2045 DS.SetTypeSpecError();
2046 Diag(Tok, diag::err_expected_type_name_after_typename);
2047 return;
2048 }
2049
Douglas Gregor23c94db2010-07-02 17:43:08 +00002050 TypeResult Type = Actions.ActOnDependentTag(getCurScope(), DeclSpec::TST_enum,
Douglas Gregor48c89f42010-04-24 16:38:41 +00002051 TUK, SS, Name, StartLoc,
2052 NameLoc);
2053 if (Type.isInvalid()) {
2054 DS.SetTypeSpecError();
2055 return;
2056 }
2057
2058 if (DS.SetTypeSpecType(DeclSpec::TST_typename, TSTLoc, PrevSpec, DiagID,
John McCallb3d87482010-08-24 05:47:05 +00002059 Type.get()))
Douglas Gregor48c89f42010-04-24 16:38:41 +00002060 Diag(StartLoc, DiagID) << PrevSpec;
2061
2062 return;
2063 }
Mike Stump1eb44332009-09-09 15:08:12 +00002064
John McCalld226f652010-08-21 09:40:31 +00002065 if (!TagDecl) {
Douglas Gregor48c89f42010-04-24 16:38:41 +00002066 // The action failed to produce an enumeration tag. If this is a
2067 // definition, consume the entire definition.
2068 if (Tok.is(tok::l_brace)) {
2069 ConsumeBrace();
2070 SkipUntil(tok::r_brace);
2071 }
2072
2073 DS.SetTypeSpecError();
2074 return;
2075 }
2076
Chris Lattner04d66662007-10-09 17:33:22 +00002077 if (Tok.is(tok::l_brace))
Reid Spencer5f016e22007-07-11 17:01:13 +00002078 ParseEnumBody(StartLoc, TagDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00002079
John McCallb3d87482010-08-24 05:47:05 +00002080 // FIXME: The DeclSpec should keep the locations of both the keyword
2081 // and the name (if there is one).
Douglas Gregorb988f9c2010-01-25 16:33:23 +00002082 if (DS.SetTypeSpecType(DeclSpec::TST_enum, TSTLoc, PrevSpec, DiagID,
John McCalld226f652010-08-21 09:40:31 +00002083 TagDecl, Owned))
John McCallfec54012009-08-03 20:12:06 +00002084 Diag(StartLoc, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00002085}
2086
2087/// ParseEnumBody - Parse a {} enclosed enumerator-list.
2088/// enumerator-list:
2089/// enumerator
2090/// enumerator-list ',' enumerator
2091/// enumerator:
2092/// enumeration-constant
2093/// enumeration-constant '=' constant-expression
2094/// enumeration-constant:
2095/// identifier
2096///
John McCalld226f652010-08-21 09:40:31 +00002097void Parser::ParseEnumBody(SourceLocation StartLoc, Decl *EnumDecl) {
Douglas Gregor074149e2009-01-05 19:45:36 +00002098 // Enter the scope of the enum body and start the definition.
2099 ParseScope EnumScope(this, Scope::DeclScope);
Douglas Gregor23c94db2010-07-02 17:43:08 +00002100 Actions.ActOnTagStartDefinition(getCurScope(), EnumDecl);
Douglas Gregor074149e2009-01-05 19:45:36 +00002101
Reid Spencer5f016e22007-07-11 17:01:13 +00002102 SourceLocation LBraceLoc = ConsumeBrace();
Mike Stump1eb44332009-09-09 15:08:12 +00002103
Chris Lattner7946dd32007-08-27 17:24:30 +00002104 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
Chris Lattner04d66662007-10-09 17:33:22 +00002105 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Fariborz Jahanian05115522010-05-28 22:23:22 +00002106 Diag(Tok, diag::error_empty_enum);
Mike Stump1eb44332009-09-09 15:08:12 +00002107
John McCalld226f652010-08-21 09:40:31 +00002108 llvm::SmallVector<Decl *, 32> EnumConstantDecls;
Reid Spencer5f016e22007-07-11 17:01:13 +00002109
John McCalld226f652010-08-21 09:40:31 +00002110 Decl *LastEnumConstDecl = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00002111
Reid Spencer5f016e22007-07-11 17:01:13 +00002112 // Parse the enumerator-list.
Chris Lattner04d66662007-10-09 17:33:22 +00002113 while (Tok.is(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002114 IdentifierInfo *Ident = Tok.getIdentifierInfo();
2115 SourceLocation IdentLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00002116
Reid Spencer5f016e22007-07-11 17:01:13 +00002117 SourceLocation EqualLoc;
John McCall60d7b3a2010-08-24 06:29:42 +00002118 ExprResult AssignedVal;
Chris Lattner04d66662007-10-09 17:33:22 +00002119 if (Tok.is(tok::equal)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002120 EqualLoc = ConsumeToken();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002121 AssignedVal = ParseConstantExpression();
2122 if (AssignedVal.isInvalid())
Reid Spencer5f016e22007-07-11 17:01:13 +00002123 SkipUntil(tok::comma, tok::r_brace, true, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00002124 }
Mike Stump1eb44332009-09-09 15:08:12 +00002125
Reid Spencer5f016e22007-07-11 17:01:13 +00002126 // Install the enumerator constant into EnumDecl.
John McCalld226f652010-08-21 09:40:31 +00002127 Decl *EnumConstDecl = Actions.ActOnEnumConstant(getCurScope(), EnumDecl,
2128 LastEnumConstDecl,
2129 IdentLoc, Ident,
2130 EqualLoc,
2131 AssignedVal.release());
Reid Spencer5f016e22007-07-11 17:01:13 +00002132 EnumConstantDecls.push_back(EnumConstDecl);
2133 LastEnumConstDecl = EnumConstDecl;
Mike Stump1eb44332009-09-09 15:08:12 +00002134
Douglas Gregor751f6922010-09-07 14:51:08 +00002135 if (Tok.is(tok::identifier)) {
2136 // We're missing a comma between enumerators.
2137 SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation);
2138 Diag(Loc, diag::err_enumerator_list_missing_comma)
2139 << FixItHint::CreateInsertion(Loc, ", ");
2140 continue;
2141 }
2142
Chris Lattner04d66662007-10-09 17:33:22 +00002143 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +00002144 break;
2145 SourceLocation CommaLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00002146
2147 if (Tok.isNot(tok::identifier) &&
Douglas Gregor9b3064b2009-04-01 22:41:11 +00002148 !(getLang().C99 || getLang().CPlusPlus0x))
2149 Diag(CommaLoc, diag::ext_enumerator_list_comma)
2150 << getLang().CPlusPlus
Douglas Gregor849b2432010-03-31 17:46:05 +00002151 << FixItHint::CreateRemoval(CommaLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00002152 }
Mike Stump1eb44332009-09-09 15:08:12 +00002153
Reid Spencer5f016e22007-07-11 17:01:13 +00002154 // Eat the }.
Mike Stumpc6e35aa2009-05-16 07:06:02 +00002155 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00002156
Ted Kremenek1e377652010-02-11 02:19:13 +00002157 llvm::OwningPtr<AttributeList> Attr;
Reid Spencer5f016e22007-07-11 17:01:13 +00002158 // If attributes exist after the identifier list, parse them.
Chris Lattner04d66662007-10-09 17:33:22 +00002159 if (Tok.is(tok::kw___attribute))
Ted Kremenek1e377652010-02-11 02:19:13 +00002160 Attr.reset(ParseGNUAttributes()); // FIXME: where do they do?
Douglas Gregor72de6672009-01-08 20:45:30 +00002161
Edward O'Callaghanfee13812009-08-08 14:36:57 +00002162 Actions.ActOnEnumBody(StartLoc, LBraceLoc, RBraceLoc, EnumDecl,
2163 EnumConstantDecls.data(), EnumConstantDecls.size(),
Douglas Gregor23c94db2010-07-02 17:43:08 +00002164 getCurScope(), Attr.get());
Mike Stump1eb44332009-09-09 15:08:12 +00002165
Douglas Gregor72de6672009-01-08 20:45:30 +00002166 EnumScope.Exit();
Douglas Gregor23c94db2010-07-02 17:43:08 +00002167 Actions.ActOnTagFinishDefinition(getCurScope(), EnumDecl, RBraceLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00002168}
2169
2170/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff5f8aa692008-02-11 23:15:56 +00002171/// start of a type-qualifier-list.
2172bool Parser::isTypeQualifier() const {
2173 switch (Tok.getKind()) {
2174 default: return false;
2175 // type-qualifier
2176 case tok::kw_const:
2177 case tok::kw_volatile:
2178 case tok::kw_restrict:
2179 return true;
2180 }
2181}
2182
Chris Lattnerb3a4e432010-02-28 18:18:36 +00002183/// isKnownToBeTypeSpecifier - Return true if we know that the specified token
2184/// is definitely a type-specifier. Return false if it isn't part of a type
2185/// specifier or if we're not sure.
2186bool Parser::isKnownToBeTypeSpecifier(const Token &Tok) const {
2187 switch (Tok.getKind()) {
2188 default: return false;
2189 // type-specifiers
2190 case tok::kw_short:
2191 case tok::kw_long:
2192 case tok::kw_signed:
2193 case tok::kw_unsigned:
2194 case tok::kw__Complex:
2195 case tok::kw__Imaginary:
2196 case tok::kw_void:
2197 case tok::kw_char:
2198 case tok::kw_wchar_t:
2199 case tok::kw_char16_t:
2200 case tok::kw_char32_t:
2201 case tok::kw_int:
2202 case tok::kw_float:
2203 case tok::kw_double:
2204 case tok::kw_bool:
2205 case tok::kw__Bool:
2206 case tok::kw__Decimal32:
2207 case tok::kw__Decimal64:
2208 case tok::kw__Decimal128:
2209 case tok::kw___vector:
2210
2211 // struct-or-union-specifier (C99) or class-specifier (C++)
2212 case tok::kw_class:
2213 case tok::kw_struct:
2214 case tok::kw_union:
2215 // enum-specifier
2216 case tok::kw_enum:
2217
2218 // typedef-name
2219 case tok::annot_typename:
2220 return true;
2221 }
2222}
2223
Steve Naroff5f8aa692008-02-11 23:15:56 +00002224/// isTypeSpecifierQualifier - Return true if the current token could be the
Reid Spencer5f016e22007-07-11 17:01:13 +00002225/// start of a specifier-qualifier-list.
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002226bool Parser::isTypeSpecifierQualifier() {
Reid Spencer5f016e22007-07-11 17:01:13 +00002227 switch (Tok.getKind()) {
2228 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002229
Chris Lattner166a8fc2009-01-04 23:41:41 +00002230 case tok::identifier: // foo::bar
John Thompson82287d12010-02-05 00:12:22 +00002231 if (TryAltiVecVectorToken())
2232 return true;
2233 // Fall through.
Douglas Gregord57959a2009-03-27 23:10:48 +00002234 case tok::kw_typename: // typename T::type
Chris Lattner166a8fc2009-01-04 23:41:41 +00002235 // Annotate typenames and C++ scope specifiers. If we get one, just
2236 // recurse to handle whatever we get.
2237 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00002238 return true;
2239 if (Tok.is(tok::identifier))
2240 return false;
2241 return isTypeSpecifierQualifier();
Douglas Gregord57959a2009-03-27 23:10:48 +00002242
Chris Lattner166a8fc2009-01-04 23:41:41 +00002243 case tok::coloncolon: // ::foo::bar
2244 if (NextToken().is(tok::kw_new) || // ::new
2245 NextToken().is(tok::kw_delete)) // ::delete
2246 return false;
2247
Chris Lattner166a8fc2009-01-04 23:41:41 +00002248 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00002249 return true;
2250 return isTypeSpecifierQualifier();
Mike Stump1eb44332009-09-09 15:08:12 +00002251
Reid Spencer5f016e22007-07-11 17:01:13 +00002252 // GNU attributes support.
2253 case tok::kw___attribute:
Steve Naroffd1861fd2007-07-31 12:34:36 +00002254 // GNU typeof support.
2255 case tok::kw_typeof:
Mike Stump1eb44332009-09-09 15:08:12 +00002256
Reid Spencer5f016e22007-07-11 17:01:13 +00002257 // type-specifiers
2258 case tok::kw_short:
2259 case tok::kw_long:
2260 case tok::kw_signed:
2261 case tok::kw_unsigned:
2262 case tok::kw__Complex:
2263 case tok::kw__Imaginary:
2264 case tok::kw_void:
2265 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00002266 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002267 case tok::kw_char16_t:
2268 case tok::kw_char32_t:
Reid Spencer5f016e22007-07-11 17:01:13 +00002269 case tok::kw_int:
2270 case tok::kw_float:
2271 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00002272 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00002273 case tok::kw__Bool:
2274 case tok::kw__Decimal32:
2275 case tok::kw__Decimal64:
2276 case tok::kw__Decimal128:
John Thompson82287d12010-02-05 00:12:22 +00002277 case tok::kw___vector:
Mike Stump1eb44332009-09-09 15:08:12 +00002278
Chris Lattner99dc9142008-04-13 18:59:07 +00002279 // struct-or-union-specifier (C99) or class-specifier (C++)
2280 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00002281 case tok::kw_struct:
2282 case tok::kw_union:
2283 // enum-specifier
2284 case tok::kw_enum:
Mike Stump1eb44332009-09-09 15:08:12 +00002285
Reid Spencer5f016e22007-07-11 17:01:13 +00002286 // type-qualifier
2287 case tok::kw_const:
2288 case tok::kw_volatile:
2289 case tok::kw_restrict:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002290
2291 // typedef-name
Chris Lattnerb31757b2009-01-06 05:06:21 +00002292 case tok::annot_typename:
Reid Spencer5f016e22007-07-11 17:01:13 +00002293 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00002294
Chris Lattner7c186be2008-10-20 00:25:30 +00002295 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
2296 case tok::less:
2297 return getLang().ObjC1;
Mike Stump1eb44332009-09-09 15:08:12 +00002298
Steve Naroff239f0732008-12-25 14:16:32 +00002299 case tok::kw___cdecl:
2300 case tok::kw___stdcall:
2301 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00002302 case tok::kw___thiscall:
Eli Friedman290eeb02009-06-08 23:27:34 +00002303 case tok::kw___w64:
2304 case tok::kw___ptr64:
Dawn Perchik52fc3142010-09-03 01:29:35 +00002305 case tok::kw___pascal:
Eli Friedman290eeb02009-06-08 23:27:34 +00002306 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00002307 }
2308}
2309
2310/// isDeclarationSpecifier() - Return true if the current token is part of a
2311/// declaration specifier.
Douglas Gregor9497a732010-09-16 01:51:54 +00002312///
2313/// \param DisambiguatingWithExpression True to indicate that the purpose of
2314/// this check is to disambiguate between an expression and a declaration.
2315bool Parser::isDeclarationSpecifier(bool DisambiguatingWithExpression) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002316 switch (Tok.getKind()) {
2317 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002318
Chris Lattner166a8fc2009-01-04 23:41:41 +00002319 case tok::identifier: // foo::bar
Steve Naroff61f72cb2009-03-09 21:12:44 +00002320 // Unfortunate hack to support "Class.factoryMethod" notation.
2321 if (getLang().ObjC1 && NextToken().is(tok::period))
2322 return false;
John Thompson82287d12010-02-05 00:12:22 +00002323 if (TryAltiVecVectorToken())
2324 return true;
2325 // Fall through.
Douglas Gregord57959a2009-03-27 23:10:48 +00002326 case tok::kw_typename: // typename T::type
Chris Lattner166a8fc2009-01-04 23:41:41 +00002327 // Annotate typenames and C++ scope specifiers. If we get one, just
2328 // recurse to handle whatever we get.
2329 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00002330 return true;
2331 if (Tok.is(tok::identifier))
2332 return false;
Douglas Gregor9497a732010-09-16 01:51:54 +00002333
2334 // If we're in Objective-C and we have an Objective-C class type followed
2335 // by an identifier and then either ':' or ']', in a place where an
2336 // expression is permitted, then this is probably a class message send
2337 // missing the initial '['. In this case, we won't consider this to be
2338 // the start of a declaration.
2339 if (DisambiguatingWithExpression &&
2340 isStartOfObjCClassMessageMissingOpenBracket())
2341 return false;
2342
John McCall9ba61662010-02-26 08:45:28 +00002343 return isDeclarationSpecifier();
2344
Chris Lattner166a8fc2009-01-04 23:41:41 +00002345 case tok::coloncolon: // ::foo::bar
2346 if (NextToken().is(tok::kw_new) || // ::new
2347 NextToken().is(tok::kw_delete)) // ::delete
2348 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002349
Chris Lattner166a8fc2009-01-04 23:41:41 +00002350 // Annotate typenames and C++ scope specifiers. If we get one, just
2351 // recurse to handle whatever we get.
2352 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00002353 return true;
2354 return isDeclarationSpecifier();
Mike Stump1eb44332009-09-09 15:08:12 +00002355
Reid Spencer5f016e22007-07-11 17:01:13 +00002356 // storage-class-specifier
2357 case tok::kw_typedef:
2358 case tok::kw_extern:
Steve Naroff8d54bf22007-12-18 00:16:02 +00002359 case tok::kw___private_extern__:
Reid Spencer5f016e22007-07-11 17:01:13 +00002360 case tok::kw_static:
2361 case tok::kw_auto:
2362 case tok::kw_register:
2363 case tok::kw___thread:
Mike Stump1eb44332009-09-09 15:08:12 +00002364
Reid Spencer5f016e22007-07-11 17:01:13 +00002365 // type-specifiers
2366 case tok::kw_short:
2367 case tok::kw_long:
2368 case tok::kw_signed:
2369 case tok::kw_unsigned:
2370 case tok::kw__Complex:
2371 case tok::kw__Imaginary:
2372 case tok::kw_void:
2373 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00002374 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002375 case tok::kw_char16_t:
2376 case tok::kw_char32_t:
2377
Reid Spencer5f016e22007-07-11 17:01:13 +00002378 case tok::kw_int:
2379 case tok::kw_float:
2380 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00002381 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00002382 case tok::kw__Bool:
2383 case tok::kw__Decimal32:
2384 case tok::kw__Decimal64:
2385 case tok::kw__Decimal128:
John Thompson82287d12010-02-05 00:12:22 +00002386 case tok::kw___vector:
Mike Stump1eb44332009-09-09 15:08:12 +00002387
Chris Lattner99dc9142008-04-13 18:59:07 +00002388 // struct-or-union-specifier (C99) or class-specifier (C++)
2389 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00002390 case tok::kw_struct:
2391 case tok::kw_union:
2392 // enum-specifier
2393 case tok::kw_enum:
Mike Stump1eb44332009-09-09 15:08:12 +00002394
Reid Spencer5f016e22007-07-11 17:01:13 +00002395 // type-qualifier
2396 case tok::kw_const:
2397 case tok::kw_volatile:
2398 case tok::kw_restrict:
Steve Naroffd1861fd2007-07-31 12:34:36 +00002399
Reid Spencer5f016e22007-07-11 17:01:13 +00002400 // function-specifier
2401 case tok::kw_inline:
Douglas Gregorb48fe382008-10-31 09:07:45 +00002402 case tok::kw_virtual:
2403 case tok::kw_explicit:
Chris Lattnerd6c7c182007-08-09 16:40:21 +00002404
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002405 // typedef-name
Chris Lattnerb31757b2009-01-06 05:06:21 +00002406 case tok::annot_typename:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002407
Chris Lattner1ef08762007-08-09 17:01:07 +00002408 // GNU typeof support.
2409 case tok::kw_typeof:
Mike Stump1eb44332009-09-09 15:08:12 +00002410
Chris Lattner1ef08762007-08-09 17:01:07 +00002411 // GNU attributes.
Chris Lattnerd6c7c182007-08-09 16:40:21 +00002412 case tok::kw___attribute:
Reid Spencer5f016e22007-07-11 17:01:13 +00002413 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00002414
Chris Lattnerf3948c42008-07-26 03:38:44 +00002415 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
2416 case tok::less:
2417 return getLang().ObjC1;
Mike Stump1eb44332009-09-09 15:08:12 +00002418
Steve Naroff47f52092009-01-06 19:34:12 +00002419 case tok::kw___declspec:
Steve Naroff239f0732008-12-25 14:16:32 +00002420 case tok::kw___cdecl:
2421 case tok::kw___stdcall:
2422 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00002423 case tok::kw___thiscall:
Eli Friedman290eeb02009-06-08 23:27:34 +00002424 case tok::kw___w64:
2425 case tok::kw___ptr64:
2426 case tok::kw___forceinline:
Dawn Perchik52fc3142010-09-03 01:29:35 +00002427 case tok::kw___pascal:
Eli Friedman290eeb02009-06-08 23:27:34 +00002428 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00002429 }
2430}
2431
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002432bool Parser::isConstructorDeclarator() {
2433 TentativeParsingAction TPA(*this);
2434
2435 // Parse the C++ scope specifier.
2436 CXXScopeSpec SS;
John McCallb3d87482010-08-24 05:47:05 +00002437 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), true)) {
John McCall9ba61662010-02-26 08:45:28 +00002438 TPA.Revert();
2439 return false;
2440 }
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002441
2442 // Parse the constructor name.
2443 if (Tok.is(tok::identifier) || Tok.is(tok::annot_template_id)) {
2444 // We already know that we have a constructor name; just consume
2445 // the token.
2446 ConsumeToken();
2447 } else {
2448 TPA.Revert();
2449 return false;
2450 }
2451
2452 // Current class name must be followed by a left parentheses.
2453 if (Tok.isNot(tok::l_paren)) {
2454 TPA.Revert();
2455 return false;
2456 }
2457 ConsumeParen();
2458
2459 // A right parentheses or ellipsis signals that we have a constructor.
2460 if (Tok.is(tok::r_paren) || Tok.is(tok::ellipsis)) {
2461 TPA.Revert();
2462 return true;
2463 }
2464
2465 // If we need to, enter the specified scope.
2466 DeclaratorScopeObj DeclScopeObj(*this, SS);
Douglas Gregor23c94db2010-07-02 17:43:08 +00002467 if (SS.isSet() && Actions.ShouldEnterDeclaratorScope(getCurScope(), SS))
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002468 DeclScopeObj.EnterDeclaratorScope();
2469
2470 // Check whether the next token(s) are part of a declaration
2471 // specifier, in which case we have the start of a parameter and,
2472 // therefore, we know that this is a constructor.
2473 bool IsConstructor = isDeclarationSpecifier();
2474 TPA.Revert();
2475 return IsConstructor;
2476}
Reid Spencer5f016e22007-07-11 17:01:13 +00002477
2478/// ParseTypeQualifierListOpt
Dawn Perchik52fc3142010-09-03 01:29:35 +00002479/// type-qualifier-list: [C99 6.7.5]
2480/// type-qualifier
2481/// [vendor] attributes
2482/// [ only if VendorAttributesAllowed=true ]
2483/// type-qualifier-list type-qualifier
2484/// [vendor] type-qualifier-list attributes
2485/// [ only if VendorAttributesAllowed=true ]
2486/// [C++0x] attribute-specifier[opt] is allowed before cv-qualifier-seq
2487/// [ only if CXX0XAttributesAllowed=true ]
2488/// Note: vendor can be GNU, MS, etc.
Reid Spencer5f016e22007-07-11 17:01:13 +00002489///
Dawn Perchik52fc3142010-09-03 01:29:35 +00002490void Parser::ParseTypeQualifierListOpt(DeclSpec &DS,
2491 bool VendorAttributesAllowed,
Sean Huntbbd37c62009-11-21 08:43:09 +00002492 bool CXX0XAttributesAllowed) {
2493 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier()) {
2494 SourceLocation Loc = Tok.getLocation();
2495 CXX0XAttributeList Attr = ParseCXX0XAttributes();
2496 if (CXX0XAttributesAllowed)
2497 DS.AddAttributes(Attr.AttrList);
2498 else
2499 Diag(Loc, diag::err_attributes_not_allowed);
2500 }
2501
Reid Spencer5f016e22007-07-11 17:01:13 +00002502 while (1) {
John McCallfec54012009-08-03 20:12:06 +00002503 bool isInvalid = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00002504 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00002505 unsigned DiagID = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00002506 SourceLocation Loc = Tok.getLocation();
2507
2508 switch (Tok.getKind()) {
Douglas Gregor1a480c42010-08-27 17:35:51 +00002509 case tok::code_completion:
2510 Actions.CodeCompleteTypeQualifiers(DS);
2511 ConsumeCodeCompletionToken();
2512 break;
2513
Reid Spencer5f016e22007-07-11 17:01:13 +00002514 case tok::kw_const:
John McCallfec54012009-08-03 20:12:06 +00002515 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, DiagID,
2516 getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00002517 break;
2518 case tok::kw_volatile:
John McCallfec54012009-08-03 20:12:06 +00002519 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
2520 getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00002521 break;
2522 case tok::kw_restrict:
John McCallfec54012009-08-03 20:12:06 +00002523 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
2524 getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00002525 break;
Eli Friedman290eeb02009-06-08 23:27:34 +00002526 case tok::kw___w64:
Steve Naroff86bc6cf2008-12-25 14:41:26 +00002527 case tok::kw___ptr64:
Steve Naroff239f0732008-12-25 14:16:32 +00002528 case tok::kw___cdecl:
2529 case tok::kw___stdcall:
2530 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00002531 case tok::kw___thiscall:
Dawn Perchik52fc3142010-09-03 01:29:35 +00002532 if (VendorAttributesAllowed) {
Eli Friedman290eeb02009-06-08 23:27:34 +00002533 DS.AddAttributes(ParseMicrosoftTypeAttributes());
2534 continue;
2535 }
2536 goto DoneWithTypeQuals;
Dawn Perchik52fc3142010-09-03 01:29:35 +00002537 case tok::kw___pascal:
2538 if (VendorAttributesAllowed) {
2539 DS.AddAttributes(ParseBorlandTypeAttributes());
2540 continue;
2541 }
2542 goto DoneWithTypeQuals;
Reid Spencer5f016e22007-07-11 17:01:13 +00002543 case tok::kw___attribute:
Dawn Perchik52fc3142010-09-03 01:29:35 +00002544 if (VendorAttributesAllowed) {
Sean Huntbbd37c62009-11-21 08:43:09 +00002545 DS.AddAttributes(ParseGNUAttributes());
Chris Lattner5a69d1c2008-12-18 07:02:59 +00002546 continue; // do *not* consume the next token!
2547 }
2548 // otherwise, FALL THROUGH!
2549 default:
Steve Naroff239f0732008-12-25 14:16:32 +00002550 DoneWithTypeQuals:
Chris Lattner5a69d1c2008-12-18 07:02:59 +00002551 // If this is not a type-qualifier token, we're done reading type
2552 // qualifiers. First verify that DeclSpec's are consistent.
Douglas Gregor9b3064b2009-04-01 22:41:11 +00002553 DS.Finish(Diags, PP);
Chris Lattner5a69d1c2008-12-18 07:02:59 +00002554 return;
Reid Spencer5f016e22007-07-11 17:01:13 +00002555 }
Chris Lattnera1fcbad2008-12-18 06:50:14 +00002556
Reid Spencer5f016e22007-07-11 17:01:13 +00002557 // If the specifier combination wasn't legal, issue a diagnostic.
2558 if (isInvalid) {
2559 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner1ab3b962008-11-18 07:48:38 +00002560 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00002561 }
2562 ConsumeToken();
2563 }
2564}
2565
2566
2567/// ParseDeclarator - Parse and verify a newly-initialized declarator.
2568///
2569void Parser::ParseDeclarator(Declarator &D) {
2570 /// This implements the 'declarator' production in the C grammar, then checks
2571 /// for well-formedness and issues diagnostics.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002572 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Reid Spencer5f016e22007-07-11 17:01:13 +00002573}
2574
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002575/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
2576/// is parsed by the function passed to it. Pass null, and the direct-declarator
2577/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregor2f1bc522008-11-07 20:08:42 +00002578/// ptr-operator production.
2579///
Sebastian Redlf30208a2009-01-24 21:16:55 +00002580/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
2581/// [C] pointer[opt] direct-declarator
2582/// [C++] direct-declarator
2583/// [C++] ptr-operator declarator
Reid Spencer5f016e22007-07-11 17:01:13 +00002584///
2585/// pointer: [C99 6.7.5]
2586/// '*' type-qualifier-list[opt]
2587/// '*' type-qualifier-list[opt] pointer
2588///
Douglas Gregor2f1bc522008-11-07 20:08:42 +00002589/// ptr-operator:
2590/// '*' cv-qualifier-seq[opt]
2591/// '&'
Sebastian Redl05532f22009-03-15 22:02:01 +00002592/// [C++0x] '&&'
Douglas Gregor2f1bc522008-11-07 20:08:42 +00002593/// [GNU] '&' restrict[opt] attributes[opt]
Sebastian Redl05532f22009-03-15 22:02:01 +00002594/// [GNU?] '&&' restrict[opt] attributes[opt]
Sebastian Redlf30208a2009-01-24 21:16:55 +00002595/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002596void Parser::ParseDeclaratorInternal(Declarator &D,
2597 DirectDeclParseFunction DirectDeclParser) {
Douglas Gregor91a28862009-08-26 14:27:30 +00002598 if (Diags.hasAllExtensionsSilenced())
2599 D.setExtension();
Douglas Gregor2ccccb32010-08-23 18:23:48 +00002600
Sebastian Redlf30208a2009-01-24 21:16:55 +00002601 // C++ member pointers start with a '::' or a nested-name.
2602 // Member pointers get special handling, since there's no place for the
2603 // scope spec in the generic path below.
Chris Lattnerf919bfe2009-03-24 17:04:48 +00002604 if (getLang().CPlusPlus &&
2605 (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) ||
2606 Tok.is(tok::annot_cxxscope))) {
Sebastian Redlf30208a2009-01-24 21:16:55 +00002607 CXXScopeSpec SS;
John McCallb3d87482010-08-24 05:47:05 +00002608 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), true); // ignore fail
John McCall9ba61662010-02-26 08:45:28 +00002609
Jeffrey Yasskinedc28772010-04-07 23:29:58 +00002610 if (SS.isNotEmpty()) {
Mike Stump1eb44332009-09-09 15:08:12 +00002611 if (Tok.isNot(tok::star)) {
Sebastian Redlf30208a2009-01-24 21:16:55 +00002612 // The scope spec really belongs to the direct-declarator.
2613 D.getCXXScopeSpec() = SS;
2614 if (DirectDeclParser)
2615 (this->*DirectDeclParser)(D);
2616 return;
2617 }
2618
2619 SourceLocation Loc = ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00002620 D.SetRangeEnd(Loc);
Sebastian Redlf30208a2009-01-24 21:16:55 +00002621 DeclSpec DS;
2622 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002623 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00002624
2625 // Recurse to parse whatever is left.
2626 ParseDeclaratorInternal(D, DirectDeclParser);
2627
2628 // Sema will have to catch (syntactically invalid) pointers into global
2629 // scope. It has to catch pointers into namespace scope anyway.
2630 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00002631 Loc, DS.TakeAttributes()),
2632 /* Don't replace range end. */SourceLocation());
Sebastian Redlf30208a2009-01-24 21:16:55 +00002633 return;
2634 }
2635 }
2636
2637 tok::TokenKind Kind = Tok.getKind();
Steve Naroff5618bd42008-08-27 16:04:49 +00002638 // Not a pointer, C++ reference, or block.
Chris Lattner9af55002009-03-27 04:18:06 +00002639 if (Kind != tok::star && Kind != tok::caret &&
Chris Lattnerf919bfe2009-03-24 17:04:48 +00002640 (Kind != tok::amp || !getLang().CPlusPlus) &&
Sebastian Redl743de1f2009-03-23 00:00:23 +00002641 // We parse rvalue refs in C++03, because otherwise the errors are scary.
Chris Lattner9af55002009-03-27 04:18:06 +00002642 (Kind != tok::ampamp || !getLang().CPlusPlus)) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002643 if (DirectDeclParser)
2644 (this->*DirectDeclParser)(D);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00002645 return;
2646 }
Sebastian Redlf30208a2009-01-24 21:16:55 +00002647
Sebastian Redl05532f22009-03-15 22:02:01 +00002648 // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference,
2649 // '&&' -> rvalue reference
Sebastian Redl743de1f2009-03-23 00:00:23 +00002650 SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&.
Sebastian Redlab197ba2009-02-09 18:23:29 +00002651 D.SetRangeEnd(Loc);
Reid Spencer5f016e22007-07-11 17:01:13 +00002652
Chris Lattner9af55002009-03-27 04:18:06 +00002653 if (Kind == tok::star || Kind == tok::caret) {
Chris Lattner76549142008-02-21 01:32:26 +00002654 // Is a pointer.
Reid Spencer5f016e22007-07-11 17:01:13 +00002655 DeclSpec DS;
Sebastian Redlf30208a2009-01-24 21:16:55 +00002656
Reid Spencer5f016e22007-07-11 17:01:13 +00002657 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002658 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00002659
Reid Spencer5f016e22007-07-11 17:01:13 +00002660 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002661 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroff5618bd42008-08-27 16:04:49 +00002662 if (Kind == tok::star)
2663 // Remember that we parsed a pointer type, and remember the type-quals.
2664 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
Sebastian Redlab197ba2009-02-09 18:23:29 +00002665 DS.TakeAttributes()),
2666 SourceLocation());
Steve Naroff5618bd42008-08-27 16:04:49 +00002667 else
2668 // Remember that we parsed a Block type, and remember the type-quals.
Mike Stump1eb44332009-09-09 15:08:12 +00002669 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
Mike Stump75b163f2009-04-21 00:51:43 +00002670 Loc, DS.TakeAttributes()),
Sebastian Redlab197ba2009-02-09 18:23:29 +00002671 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00002672 } else {
2673 // Is a reference
2674 DeclSpec DS;
2675
Sebastian Redl743de1f2009-03-23 00:00:23 +00002676 // Complain about rvalue references in C++03, but then go on and build
2677 // the declarator.
2678 if (Kind == tok::ampamp && !getLang().CPlusPlus0x)
2679 Diag(Loc, diag::err_rvalue_reference);
2680
Reid Spencer5f016e22007-07-11 17:01:13 +00002681 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
2682 // cv-qualifiers are introduced through the use of a typedef or of a
2683 // template type argument, in which case the cv-qualifiers are ignored.
2684 //
2685 // [GNU] Retricted references are allowed.
2686 // [GNU] Attributes on references are allowed.
Sean Huntbbd37c62009-11-21 08:43:09 +00002687 // [C++0x] Attributes on references are not allowed.
2688 ParseTypeQualifierListOpt(DS, true, false);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002689 D.ExtendWithDeclSpec(DS);
Reid Spencer5f016e22007-07-11 17:01:13 +00002690
2691 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
2692 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
2693 Diag(DS.getConstSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00002694 diag::err_invalid_reference_qualifier_application) << "const";
Reid Spencer5f016e22007-07-11 17:01:13 +00002695 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
2696 Diag(DS.getVolatileSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00002697 diag::err_invalid_reference_qualifier_application) << "volatile";
Reid Spencer5f016e22007-07-11 17:01:13 +00002698 }
2699
2700 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002701 ParseDeclaratorInternal(D, DirectDeclParser);
Reid Spencer5f016e22007-07-11 17:01:13 +00002702
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00002703 if (D.getNumTypeObjects() > 0) {
2704 // C++ [dcl.ref]p4: There shall be no references to references.
2705 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
2706 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattnerda83bac2008-11-19 07:37:42 +00002707 if (const IdentifierInfo *II = D.getIdentifier())
2708 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
2709 << II;
2710 else
2711 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
2712 << "type name";
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00002713
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002714 // Once we've complained about the reference-to-reference, we
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00002715 // can go ahead and build the (technically ill-formed)
2716 // declarator: reference collapsing will take care of it.
2717 }
2718 }
2719
Reid Spencer5f016e22007-07-11 17:01:13 +00002720 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner76549142008-02-21 01:32:26 +00002721 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
Sebastian Redl05532f22009-03-15 22:02:01 +00002722 DS.TakeAttributes(),
2723 Kind == tok::amp),
Sebastian Redlab197ba2009-02-09 18:23:29 +00002724 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00002725 }
2726}
2727
2728/// ParseDirectDeclarator
2729/// direct-declarator: [C99 6.7.5]
Douglas Gregor42a552f2008-11-05 20:51:48 +00002730/// [C99] identifier
Reid Spencer5f016e22007-07-11 17:01:13 +00002731/// '(' declarator ')'
2732/// [GNU] '(' attributes declarator ')'
2733/// [C90] direct-declarator '[' constant-expression[opt] ']'
2734/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
2735/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
2736/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
2737/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
2738/// direct-declarator '(' parameter-type-list ')'
2739/// direct-declarator '(' identifier-list[opt] ')'
2740/// [GNU] direct-declarator '(' parameter-forward-declarations
2741/// parameter-type-list[opt] ')'
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002742/// [C++] direct-declarator '(' parameter-declaration-clause ')'
2743/// cv-qualifier-seq[opt] exception-specification[opt]
Douglas Gregorb48fe382008-10-31 09:07:45 +00002744/// [C++] declarator-id
Douglas Gregor42a552f2008-11-05 20:51:48 +00002745///
2746/// declarator-id: [C++ 8]
2747/// id-expression
2748/// '::'[opt] nested-name-specifier[opt] type-name
2749///
2750/// id-expression: [C++ 5.1]
2751/// unqualified-id
Douglas Gregordb422df2009-09-25 21:45:23 +00002752/// qualified-id
Douglas Gregor42a552f2008-11-05 20:51:48 +00002753///
2754/// unqualified-id: [C++ 5.1]
Mike Stump1eb44332009-09-09 15:08:12 +00002755/// identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002756/// operator-function-id
Douglas Gregordb422df2009-09-25 21:45:23 +00002757/// conversion-function-id
Mike Stump1eb44332009-09-09 15:08:12 +00002758/// '~' class-name
Douglas Gregor39a8de12009-02-25 19:37:18 +00002759/// template-id
Argyrios Kyrtzidisc7ed9c62008-11-07 22:02:30 +00002760///
Reid Spencer5f016e22007-07-11 17:01:13 +00002761void Parser::ParseDirectDeclarator(Declarator &D) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002762 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002763
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002764 if (getLang().CPlusPlus && D.mayHaveIdentifier()) {
2765 // ParseDeclaratorInternal might already have parsed the scope.
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00002766 if (D.getCXXScopeSpec().isEmpty()) {
John McCallb3d87482010-08-24 05:47:05 +00002767 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec(), ParsedType(), true);
John McCall9ba61662010-02-26 08:45:28 +00002768 }
2769
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00002770 if (D.getCXXScopeSpec().isValid()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00002771 if (Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
John McCalle7e278b2009-12-11 20:04:54 +00002772 // Change the declaration context for name lookup, until this function
2773 // is exited (and the declarator has been parsed).
2774 DeclScopeObj.EnterDeclaratorScope();
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00002775 }
2776
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002777 if (Tok.is(tok::identifier) || Tok.is(tok::kw_operator) ||
2778 Tok.is(tok::annot_template_id) || Tok.is(tok::tilde)) {
2779 // We found something that indicates the start of an unqualified-id.
2780 // Parse that unqualified-id.
John McCallba9d8532010-04-13 06:39:49 +00002781 bool AllowConstructorName;
2782 if (D.getDeclSpec().hasTypeSpecifier())
2783 AllowConstructorName = false;
2784 else if (D.getCXXScopeSpec().isSet())
2785 AllowConstructorName =
2786 (D.getContext() == Declarator::FileContext ||
2787 (D.getContext() == Declarator::MemberContext &&
2788 D.getDeclSpec().isFriendSpecified()));
2789 else
2790 AllowConstructorName = (D.getContext() == Declarator::MemberContext);
2791
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002792 if (ParseUnqualifiedId(D.getCXXScopeSpec(),
2793 /*EnteringContext=*/true,
2794 /*AllowDestructorName=*/true,
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002795 AllowConstructorName,
John McCallb3d87482010-08-24 05:47:05 +00002796 ParsedType(),
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00002797 D.getName()) ||
2798 // Once we're past the identifier, if the scope was bad, mark the
2799 // whole declarator bad.
2800 D.getCXXScopeSpec().isInvalid()) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002801 D.SetIdentifier(0, Tok.getLocation());
2802 D.setInvalidType(true);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002803 } else {
2804 // Parsed the unqualified-id; update range information and move along.
2805 if (D.getSourceRange().getBegin().isInvalid())
2806 D.SetRangeBegin(D.getName().getSourceRange().getBegin());
2807 D.SetRangeEnd(D.getName().getSourceRange().getEnd());
Douglas Gregor2f1bc522008-11-07 20:08:42 +00002808 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002809 goto PastIdentifier;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00002810 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002811 } else if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002812 assert(!getLang().CPlusPlus &&
2813 "There's a C++-specific check for tok::identifier above");
2814 assert(Tok.getIdentifierInfo() && "Not an identifier?");
2815 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
2816 ConsumeToken();
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002817 goto PastIdentifier;
2818 }
2819
2820 if (Tok.is(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002821 // direct-declarator: '(' declarator ')'
2822 // direct-declarator: '(' attributes declarator ')'
2823 // Example: 'char (*X)' or 'int (*XX)(void)'
2824 ParseParenDeclarator(D);
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002825
2826 // If the declarator was parenthesized, we entered the declarator
2827 // scope when parsing the parenthesized declarator, then exited
2828 // the scope already. Re-enter the scope, if we need to.
2829 if (D.getCXXScopeSpec().isSet()) {
Fariborz Jahanian46877cd2010-08-17 23:50:37 +00002830 // If there was an error parsing parenthesized declarator, declarator
2831 // scope may have been enterred before. Don't do it again.
2832 if (!D.isInvalidType() &&
2833 Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002834 // Change the declaration context for name lookup, until this function
2835 // is exited (and the declarator has been parsed).
Fariborz Jahanian46877cd2010-08-17 23:50:37 +00002836 DeclScopeObj.EnterDeclaratorScope();
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002837 }
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002838 } else if (D.mayOmitIdentifier()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002839 // This could be something simple like "int" (in which case the declarator
2840 // portion is empty), if an abstract-declarator is allowed.
2841 D.SetIdentifier(0, Tok.getLocation());
2842 } else {
Douglas Gregore950d4b2009-03-06 23:28:18 +00002843 if (D.getContext() == Declarator::MemberContext)
2844 Diag(Tok, diag::err_expected_member_name_or_semi)
2845 << D.getDeclSpec().getSourceRange();
2846 else if (getLang().CPlusPlus)
Douglas Gregor2d1c2142009-11-03 19:44:04 +00002847 Diag(Tok, diag::err_expected_unqualified_id) << getLang().CPlusPlus;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002848 else
Chris Lattner1ab3b962008-11-18 07:48:38 +00002849 Diag(Tok, diag::err_expected_ident_lparen);
Reid Spencer5f016e22007-07-11 17:01:13 +00002850 D.SetIdentifier(0, Tok.getLocation());
Chris Lattner1f6f54b2008-11-11 06:13:16 +00002851 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00002852 }
Mike Stump1eb44332009-09-09 15:08:12 +00002853
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002854 PastIdentifier:
Reid Spencer5f016e22007-07-11 17:01:13 +00002855 assert(D.isPastIdentifier() &&
2856 "Haven't past the location of the identifier yet?");
Mike Stump1eb44332009-09-09 15:08:12 +00002857
Sean Huntbbd37c62009-11-21 08:43:09 +00002858 // Don't parse attributes unless we have an identifier.
Douglas Gregor3c3aaf92010-02-19 16:47:56 +00002859 if (D.getIdentifier() && getLang().CPlusPlus0x
Sean Huntbbd37c62009-11-21 08:43:09 +00002860 && isCXX0XAttributeSpecifier(true)) {
2861 SourceLocation AttrEndLoc;
2862 CXX0XAttributeList Attr = ParseCXX0XAttributes();
2863 D.AddAttributes(Attr.AttrList, AttrEndLoc);
2864 }
2865
Reid Spencer5f016e22007-07-11 17:01:13 +00002866 while (1) {
Chris Lattner04d66662007-10-09 17:33:22 +00002867 if (Tok.is(tok::l_paren)) {
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00002868 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
2869 // In such a case, check if we actually have a function declarator; if it
2870 // is not, the declarator has been fully parsed.
Chris Lattner7399ee02008-10-20 02:05:46 +00002871 if (getLang().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
2872 // When not in file scope, warn for ambiguous function declarators, just
2873 // in case the author intended it as a variable definition.
2874 bool warnIfAmbiguous = D.getContext() != Declarator::FileContext;
2875 if (!isCXXFunctionDeclarator(warnIfAmbiguous))
2876 break;
2877 }
Chris Lattneref4715c2008-04-06 05:45:57 +00002878 ParseFunctionDeclarator(ConsumeParen(), D);
Chris Lattner04d66662007-10-09 17:33:22 +00002879 } else if (Tok.is(tok::l_square)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002880 ParseBracketDeclarator(D);
2881 } else {
2882 break;
2883 }
2884 }
2885}
2886
Chris Lattneref4715c2008-04-06 05:45:57 +00002887/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
2888/// only called before the identifier, so these are most likely just grouping
Mike Stump1eb44332009-09-09 15:08:12 +00002889/// parens for precedence. If we find that these are actually function
Chris Lattneref4715c2008-04-06 05:45:57 +00002890/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
2891///
2892/// direct-declarator:
2893/// '(' declarator ')'
2894/// [GNU] '(' attributes declarator ')'
Chris Lattner7399ee02008-10-20 02:05:46 +00002895/// direct-declarator '(' parameter-type-list ')'
2896/// direct-declarator '(' identifier-list[opt] ')'
2897/// [GNU] direct-declarator '(' parameter-forward-declarations
2898/// parameter-type-list[opt] ')'
Chris Lattneref4715c2008-04-06 05:45:57 +00002899///
2900void Parser::ParseParenDeclarator(Declarator &D) {
2901 SourceLocation StartLoc = ConsumeParen();
2902 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
Mike Stump1eb44332009-09-09 15:08:12 +00002903
Chris Lattner7399ee02008-10-20 02:05:46 +00002904 // Eat any attributes before we look at whether this is a grouping or function
2905 // declarator paren. If this is a grouping paren, the attribute applies to
2906 // the type being built up, for example:
2907 // int (__attribute__(()) *x)(long y)
2908 // If this ends up not being a grouping paren, the attribute applies to the
2909 // first argument, for example:
2910 // int (__attribute__(()) int x)
2911 // In either case, we need to eat any attributes to be able to determine what
2912 // sort of paren this is.
2913 //
Ted Kremenek1e377652010-02-11 02:19:13 +00002914 llvm::OwningPtr<AttributeList> AttrList;
Chris Lattner7399ee02008-10-20 02:05:46 +00002915 bool RequiresArg = false;
2916 if (Tok.is(tok::kw___attribute)) {
Ted Kremenek1e377652010-02-11 02:19:13 +00002917 AttrList.reset(ParseGNUAttributes());
Mike Stump1eb44332009-09-09 15:08:12 +00002918
Chris Lattner7399ee02008-10-20 02:05:46 +00002919 // We require that the argument list (if this is a non-grouping paren) be
2920 // present even if the attribute list was empty.
2921 RequiresArg = true;
2922 }
Steve Naroff239f0732008-12-25 14:16:32 +00002923 // Eat any Microsoft extensions.
Eli Friedman290eeb02009-06-08 23:27:34 +00002924 if (Tok.is(tok::kw___cdecl) || Tok.is(tok::kw___stdcall) ||
Douglas Gregorf813a2c2010-05-18 16:57:00 +00002925 Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___fastcall) ||
2926 Tok.is(tok::kw___w64) || Tok.is(tok::kw___ptr64)) {
Ted Kremenek1e377652010-02-11 02:19:13 +00002927 AttrList.reset(ParseMicrosoftTypeAttributes(AttrList.take()));
Eli Friedman290eeb02009-06-08 23:27:34 +00002928 }
Dawn Perchik52fc3142010-09-03 01:29:35 +00002929 // Eat any Borland extensions.
2930 if (Tok.is(tok::kw___pascal)) {
2931 AttrList.reset(ParseBorlandTypeAttributes(AttrList.take()));
2932 }
Mike Stump1eb44332009-09-09 15:08:12 +00002933
Chris Lattneref4715c2008-04-06 05:45:57 +00002934 // If we haven't past the identifier yet (or where the identifier would be
2935 // stored, if this is an abstract declarator), then this is probably just
2936 // grouping parens. However, if this could be an abstract-declarator, then
2937 // this could also be the start of function arguments (consider 'void()').
2938 bool isGrouping;
Mike Stump1eb44332009-09-09 15:08:12 +00002939
Chris Lattneref4715c2008-04-06 05:45:57 +00002940 if (!D.mayOmitIdentifier()) {
2941 // If this can't be an abstract-declarator, this *must* be a grouping
2942 // paren, because we haven't seen the identifier yet.
2943 isGrouping = true;
2944 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Argyrios Kyrtzidise25d2702008-10-06 00:07:55 +00002945 (getLang().CPlusPlus && Tok.is(tok::ellipsis)) || // C++ int(...)
Chris Lattneref4715c2008-04-06 05:45:57 +00002946 isDeclarationSpecifier()) { // 'int(int)' is a function.
2947 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
2948 // considered to be a type, not a K&R identifier-list.
2949 isGrouping = false;
2950 } else {
2951 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
2952 isGrouping = true;
2953 }
Mike Stump1eb44332009-09-09 15:08:12 +00002954
Chris Lattneref4715c2008-04-06 05:45:57 +00002955 // If this is a grouping paren, handle:
2956 // direct-declarator: '(' declarator ')'
2957 // direct-declarator: '(' attributes declarator ')'
2958 if (isGrouping) {
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00002959 bool hadGroupingParens = D.hasGroupingParens();
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00002960 D.setGroupingParens(true);
Chris Lattner7399ee02008-10-20 02:05:46 +00002961 if (AttrList)
Ted Kremenek1e377652010-02-11 02:19:13 +00002962 D.AddAttributes(AttrList.take(), SourceLocation());
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00002963
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002964 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattneref4715c2008-04-06 05:45:57 +00002965 // Match the ')'.
Sebastian Redlab197ba2009-02-09 18:23:29 +00002966 SourceLocation Loc = MatchRHSPunctuation(tok::r_paren, StartLoc);
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00002967
2968 D.setGroupingParens(hadGroupingParens);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002969 D.SetRangeEnd(Loc);
Chris Lattneref4715c2008-04-06 05:45:57 +00002970 return;
2971 }
Mike Stump1eb44332009-09-09 15:08:12 +00002972
Chris Lattneref4715c2008-04-06 05:45:57 +00002973 // Okay, if this wasn't a grouping paren, it must be the start of a function
2974 // argument list. Recognize that this declarator will never have an
Chris Lattner7399ee02008-10-20 02:05:46 +00002975 // identifier (and remember where it would have been), then call into
2976 // ParseFunctionDeclarator to handle of argument list.
Chris Lattneref4715c2008-04-06 05:45:57 +00002977 D.SetIdentifier(0, Tok.getLocation());
2978
Ted Kremenek1e377652010-02-11 02:19:13 +00002979 ParseFunctionDeclarator(StartLoc, D, AttrList.take(), RequiresArg);
Chris Lattneref4715c2008-04-06 05:45:57 +00002980}
2981
2982/// ParseFunctionDeclarator - We are after the identifier and have parsed the
2983/// declarator D up to a paren, which indicates that we are parsing function
2984/// arguments.
Reid Spencer5f016e22007-07-11 17:01:13 +00002985///
Chris Lattner7399ee02008-10-20 02:05:46 +00002986/// If AttrList is non-null, then the caller parsed those arguments immediately
2987/// after the open paren - they should be considered to be the first argument of
2988/// a parameter. If RequiresArg is true, then the first argument of the
2989/// function is required to be present and required to not be an identifier
2990/// list.
2991///
Reid Spencer5f016e22007-07-11 17:01:13 +00002992/// This method also handles this portion of the grammar:
2993/// parameter-type-list: [C99 6.7.5]
2994/// parameter-list
2995/// parameter-list ',' '...'
Douglas Gregored5d6512009-09-22 21:41:40 +00002996/// [C++] parameter-list '...'
Reid Spencer5f016e22007-07-11 17:01:13 +00002997///
2998/// parameter-list: [C99 6.7.5]
2999/// parameter-declaration
3000/// parameter-list ',' parameter-declaration
3001///
3002/// parameter-declaration: [C99 6.7.5]
3003/// declaration-specifiers declarator
Chris Lattner04421082008-04-08 04:40:51 +00003004/// [C++] declaration-specifiers declarator '=' assignment-expression
Reid Spencer5f016e22007-07-11 17:01:13 +00003005/// [GNU] declaration-specifiers declarator attributes
Sebastian Redl50de12f2009-03-24 22:27:57 +00003006/// declaration-specifiers abstract-declarator[opt]
3007/// [C++] declaration-specifiers abstract-declarator[opt]
Chris Lattner8123a952008-04-10 02:22:51 +00003008/// '=' assignment-expression
Reid Spencer5f016e22007-07-11 17:01:13 +00003009/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
3010///
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003011/// For C++, after the parameter-list, it also parses "cv-qualifier-seq[opt]"
Sebastian Redl50de12f2009-03-24 22:27:57 +00003012/// and "exception-specification[opt]".
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003013///
Chris Lattner7399ee02008-10-20 02:05:46 +00003014void Parser::ParseFunctionDeclarator(SourceLocation LParenLoc, Declarator &D,
3015 AttributeList *AttrList,
3016 bool RequiresArg) {
Chris Lattneref4715c2008-04-06 05:45:57 +00003017 // lparen is already consumed!
3018 assert(D.isPastIdentifier() && "Should not call before identifier!");
Mike Stump1eb44332009-09-09 15:08:12 +00003019
Chris Lattner7399ee02008-10-20 02:05:46 +00003020 // This parameter list may be empty.
Chris Lattner04d66662007-10-09 17:33:22 +00003021 if (Tok.is(tok::r_paren)) {
Chris Lattner7399ee02008-10-20 02:05:46 +00003022 if (RequiresArg) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00003023 Diag(Tok, diag::err_argument_required_after_attribute);
Chris Lattner7399ee02008-10-20 02:05:46 +00003024 delete AttrList;
3025 }
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003026
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00003027 SourceLocation RParenLoc = ConsumeParen(); // Eat the closing ')'.
3028 SourceLocation EndLoc = RParenLoc;
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003029
3030 // cv-qualifier-seq[opt].
3031 DeclSpec DS;
Sebastian Redl7dc81342009-04-29 17:30:04 +00003032 bool hasExceptionSpec = false;
Sebastian Redl3cc97262009-05-31 11:47:27 +00003033 SourceLocation ThrowLoc;
Sebastian Redl7dc81342009-04-29 17:30:04 +00003034 bool hasAnyExceptionSpec = false;
John McCallb3d87482010-08-24 05:47:05 +00003035 llvm::SmallVector<ParsedType, 2> Exceptions;
Sebastian Redlef65f062009-05-29 18:02:33 +00003036 llvm::SmallVector<SourceRange, 2> ExceptionRanges;
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003037 if (getLang().CPlusPlus) {
Chris Lattner5a69d1c2008-12-18 07:02:59 +00003038 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Sebastian Redlab197ba2009-02-09 18:23:29 +00003039 if (!DS.getSourceRange().getEnd().isInvalid())
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00003040 EndLoc = DS.getSourceRange().getEnd();
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00003041
3042 // Parse exception-specification[opt].
Sebastian Redl7dc81342009-04-29 17:30:04 +00003043 if (Tok.is(tok::kw_throw)) {
3044 hasExceptionSpec = true;
Sebastian Redl3cc97262009-05-31 11:47:27 +00003045 ThrowLoc = Tok.getLocation();
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00003046 ParseExceptionSpecification(EndLoc, Exceptions, ExceptionRanges,
Sebastian Redlef65f062009-05-29 18:02:33 +00003047 hasAnyExceptionSpec);
3048 assert(Exceptions.size() == ExceptionRanges.size() &&
3049 "Produced different number of exception types and ranges.");
Sebastian Redl7dc81342009-04-29 17:30:04 +00003050 }
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003051 }
3052
Chris Lattnerf97409f2008-04-06 06:57:35 +00003053 // Remember that we parsed a function type, and remember the attributes.
Reid Spencer5f016e22007-07-11 17:01:13 +00003054 // int() -> no prototype, no '...'.
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003055 D.AddTypeInfo(DeclaratorChunk::getFunction(/*prototype*/getLang().CPlusPlus,
Chris Lattnerf97409f2008-04-06 06:57:35 +00003056 /*variadic*/ false,
Douglas Gregor965acbb2009-02-18 07:07:28 +00003057 SourceLocation(),
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003058 /*arglist*/ 0, 0,
3059 DS.getTypeQualifiers(),
Sebastian Redl3cc97262009-05-31 11:47:27 +00003060 hasExceptionSpec, ThrowLoc,
Sebastian Redl7dc81342009-04-29 17:30:04 +00003061 hasAnyExceptionSpec,
Sebastian Redlef65f062009-05-29 18:02:33 +00003062 Exceptions.data(),
3063 ExceptionRanges.data(),
Sebastian Redl7dc81342009-04-29 17:30:04 +00003064 Exceptions.size(),
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00003065 LParenLoc, RParenLoc, D),
3066 EndLoc);
Chris Lattnerf97409f2008-04-06 06:57:35 +00003067 return;
Sebastian Redlef65f062009-05-29 18:02:33 +00003068 }
3069
Chris Lattner7399ee02008-10-20 02:05:46 +00003070 // Alternatively, this parameter list may be an identifier list form for a
3071 // K&R-style function: void foo(a,b,c)
John Thompson82287d12010-02-05 00:12:22 +00003072 if (!getLang().CPlusPlus && Tok.is(tok::identifier)
3073 && !TryAltiVecVectorToken()) {
John McCall9ba61662010-02-26 08:45:28 +00003074 if (TryAnnotateTypeOrScopeToken() || !Tok.is(tok::annot_typename)) {
Chris Lattner7399ee02008-10-20 02:05:46 +00003075 // K&R identifier lists can't have typedefs as identifiers, per
3076 // C99 6.7.5.3p11.
Steve Naroff2d081c42009-01-28 19:16:40 +00003077 if (RequiresArg) {
3078 Diag(Tok, diag::err_argument_required_after_attribute);
3079 delete AttrList;
3080 }
Chris Lattner83a94472010-05-14 17:23:36 +00003081
Steve Naroff2d081c42009-01-28 19:16:40 +00003082 // Identifier list. Note that '(' identifier-list ')' is only allowed for
Chris Lattner83a94472010-05-14 17:23:36 +00003083 // normal declarators, not for abstract-declarators. Get the first
3084 // identifier.
Chris Lattner9a65b812010-05-14 17:44:56 +00003085 Token FirstTok = Tok;
Chris Lattner83a94472010-05-14 17:23:36 +00003086 ConsumeToken(); // eat the first identifier.
Chris Lattner9a65b812010-05-14 17:44:56 +00003087
3088 // Identifier lists follow a really simple grammar: the identifiers can
3089 // be followed *only* by a ", moreidentifiers" or ")". However, K&R
3090 // identifier lists are really rare in the brave new modern world, and it
3091 // is very common for someone to typo a type in a non-k&r style list. If
3092 // we are presented with something like: "void foo(intptr x, float y)",
3093 // we don't want to start parsing the function declarator as though it is
3094 // a K&R style declarator just because intptr is an invalid type.
3095 //
3096 // To handle this, we check to see if the token after the first identifier
3097 // is a "," or ")". Only if so, do we parse it as an identifier list.
3098 if (Tok.is(tok::comma) || Tok.is(tok::r_paren))
3099 return ParseFunctionDeclaratorIdentifierList(LParenLoc,
3100 FirstTok.getIdentifierInfo(),
3101 FirstTok.getLocation(), D);
3102
3103 // If we get here, the code is invalid. Push the first identifier back
3104 // into the token stream and parse the first argument as an (invalid)
3105 // normal argument declarator.
3106 PP.EnterToken(Tok);
3107 Tok = FirstTok;
Chris Lattner7399ee02008-10-20 02:05:46 +00003108 }
Chris Lattnerf97409f2008-04-06 06:57:35 +00003109 }
Mike Stump1eb44332009-09-09 15:08:12 +00003110
Chris Lattnerf97409f2008-04-06 06:57:35 +00003111 // Finally, a normal, non-empty parameter type list.
Mike Stump1eb44332009-09-09 15:08:12 +00003112
Chris Lattnerf97409f2008-04-06 06:57:35 +00003113 // Build up an array of information about the parsed arguments.
3114 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Chris Lattner04421082008-04-08 04:40:51 +00003115
3116 // Enter function-declaration scope, limiting any declarators to the
3117 // function prototype scope, including parameter declarators.
Chris Lattnerae50fa02009-03-05 00:00:31 +00003118 ParseScope PrototypeScope(this,
3119 Scope::FunctionPrototypeScope|Scope::DeclScope);
Mike Stump1eb44332009-09-09 15:08:12 +00003120
Chris Lattnerf97409f2008-04-06 06:57:35 +00003121 bool IsVariadic = false;
Douglas Gregor965acbb2009-02-18 07:07:28 +00003122 SourceLocation EllipsisLoc;
Chris Lattnerf97409f2008-04-06 06:57:35 +00003123 while (1) {
3124 if (Tok.is(tok::ellipsis)) {
3125 IsVariadic = true;
Douglas Gregor965acbb2009-02-18 07:07:28 +00003126 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chris Lattnerf97409f2008-04-06 06:57:35 +00003127 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00003128 }
Mike Stump1eb44332009-09-09 15:08:12 +00003129
Chris Lattnerf97409f2008-04-06 06:57:35 +00003130 SourceLocation DSStart = Tok.getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +00003131
Chris Lattnerf97409f2008-04-06 06:57:35 +00003132 // Parse the declaration-specifiers.
John McCall54abf7d2009-11-04 02:18:39 +00003133 // Just use the ParsingDeclaration "scope" of the declarator.
Chris Lattnerf97409f2008-04-06 06:57:35 +00003134 DeclSpec DS;
Chris Lattner7399ee02008-10-20 02:05:46 +00003135
3136 // If the caller parsed attributes for the first argument, add them now.
3137 if (AttrList) {
3138 DS.AddAttributes(AttrList);
3139 AttrList = 0; // Only apply the attributes to the first parameter.
3140 }
Chris Lattnere64c5492009-02-27 18:38:20 +00003141 ParseDeclarationSpecifiers(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00003142
Chris Lattnerf97409f2008-04-06 06:57:35 +00003143 // Parse the declarator. This is "PrototypeContext", because we must
3144 // accept either 'declarator' or 'abstract-declarator' here.
3145 Declarator ParmDecl(DS, Declarator::PrototypeContext);
3146 ParseDeclarator(ParmDecl);
3147
3148 // Parse GNU attributes, if present.
Sebastian Redlab197ba2009-02-09 18:23:29 +00003149 if (Tok.is(tok::kw___attribute)) {
3150 SourceLocation Loc;
Sean Huntbbd37c62009-11-21 08:43:09 +00003151 AttributeList *AttrList = ParseGNUAttributes(&Loc);
Sebastian Redlab197ba2009-02-09 18:23:29 +00003152 ParmDecl.AddAttributes(AttrList, Loc);
3153 }
Mike Stump1eb44332009-09-09 15:08:12 +00003154
Chris Lattnerf97409f2008-04-06 06:57:35 +00003155 // Remember this parsed parameter in ParamInfo.
3156 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
Mike Stump1eb44332009-09-09 15:08:12 +00003157
Douglas Gregor72b505b2008-12-16 21:30:33 +00003158 // DefArgToks is used when the parsing of default arguments needs
3159 // to be delayed.
3160 CachedTokens *DefArgToks = 0;
3161
Chris Lattnerf97409f2008-04-06 06:57:35 +00003162 // If no parameter was specified, verify that *something* was specified,
3163 // otherwise we have a missing type and identifier.
Chris Lattnere64c5492009-02-27 18:38:20 +00003164 if (DS.isEmpty() && ParmDecl.getIdentifier() == 0 &&
3165 ParmDecl.getNumTypeObjects() == 0) {
Chris Lattnerf97409f2008-04-06 06:57:35 +00003166 // Completely missing, emit error.
3167 Diag(DSStart, diag::err_missing_param);
3168 } else {
3169 // Otherwise, we have something. Add it and let semantic analysis try
3170 // to grok it and add the result to the ParamInfo we are building.
Mike Stump1eb44332009-09-09 15:08:12 +00003171
Chris Lattnerf97409f2008-04-06 06:57:35 +00003172 // Inform the actions module about the parameter declarator, so it gets
3173 // added to the current scope.
John McCalld226f652010-08-21 09:40:31 +00003174 Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl);
Chris Lattner04421082008-04-08 04:40:51 +00003175
3176 // Parse the default argument, if any. We parse the default
3177 // arguments in all dialects; the semantic analysis in
3178 // ActOnParamDefaultArgument will reject the default argument in
3179 // C.
3180 if (Tok.is(tok::equal)) {
Douglas Gregor61366e92008-12-24 00:01:03 +00003181 SourceLocation EqualLoc = Tok.getLocation();
3182
Chris Lattner04421082008-04-08 04:40:51 +00003183 // Parse the default argument
Douglas Gregor72b505b2008-12-16 21:30:33 +00003184 if (D.getContext() == Declarator::MemberContext) {
3185 // If we're inside a class definition, cache the tokens
3186 // corresponding to the default argument. We'll actually parse
3187 // them when we see the end of the class definition.
3188 // FIXME: Templates will require something similar.
3189 // FIXME: Can we use a smart pointer for Toks?
3190 DefArgToks = new CachedTokens;
3191
Mike Stump1eb44332009-09-09 15:08:12 +00003192 if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks,
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +00003193 /*StopAtSemi=*/true,
3194 /*ConsumeFinalToken=*/false)) {
Douglas Gregor72b505b2008-12-16 21:30:33 +00003195 delete DefArgToks;
3196 DefArgToks = 0;
Douglas Gregor61366e92008-12-24 00:01:03 +00003197 Actions.ActOnParamDefaultArgumentError(Param);
Argyrios Kyrtzidis2b602ad2010-08-06 09:47:24 +00003198 } else {
3199 // Mark the end of the default argument so that we know when to
3200 // stop when we parse it later on.
3201 Token DefArgEnd;
3202 DefArgEnd.startToken();
3203 DefArgEnd.setKind(tok::cxx_defaultarg_end);
3204 DefArgEnd.setLocation(Tok.getLocation());
3205 DefArgToks->push_back(DefArgEnd);
Mike Stump1eb44332009-09-09 15:08:12 +00003206 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc,
Anders Carlsson5e300d12009-06-12 16:51:40 +00003207 (*DefArgToks)[1].getLocation());
Argyrios Kyrtzidis2b602ad2010-08-06 09:47:24 +00003208 }
Chris Lattner04421082008-04-08 04:40:51 +00003209 } else {
Douglas Gregor72b505b2008-12-16 21:30:33 +00003210 // Consume the '='.
Douglas Gregor61366e92008-12-24 00:01:03 +00003211 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00003212
Douglas Gregorbe0f7bd2010-09-11 20:24:53 +00003213 // The argument isn't actually potentially evaluated unless it is
3214 // used.
3215 EnterExpressionEvaluationContext Eval(Actions,
3216 Sema::PotentiallyEvaluatedIfUsed);
3217
John McCall60d7b3a2010-08-24 06:29:42 +00003218 ExprResult DefArgResult(ParseAssignmentExpression());
Douglas Gregor72b505b2008-12-16 21:30:33 +00003219 if (DefArgResult.isInvalid()) {
3220 Actions.ActOnParamDefaultArgumentError(Param);
3221 SkipUntil(tok::comma, tok::r_paren, true, true);
3222 } else {
3223 // Inform the actions module about the default argument
3224 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
John McCall9ae2f072010-08-23 23:25:46 +00003225 DefArgResult.take());
Douglas Gregor72b505b2008-12-16 21:30:33 +00003226 }
Chris Lattner04421082008-04-08 04:40:51 +00003227 }
3228 }
Mike Stump1eb44332009-09-09 15:08:12 +00003229
3230 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
3231 ParmDecl.getIdentifierLoc(), Param,
Douglas Gregor72b505b2008-12-16 21:30:33 +00003232 DefArgToks));
Chris Lattnerf97409f2008-04-06 06:57:35 +00003233 }
3234
3235 // If the next token is a comma, consume it and keep reading arguments.
Douglas Gregored5d6512009-09-22 21:41:40 +00003236 if (Tok.isNot(tok::comma)) {
3237 if (Tok.is(tok::ellipsis)) {
3238 IsVariadic = true;
3239 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
3240
3241 if (!getLang().CPlusPlus) {
3242 // We have ellipsis without a preceding ',', which is ill-formed
3243 // in C. Complain and provide the fix.
3244 Diag(EllipsisLoc, diag::err_missing_comma_before_ellipsis)
Douglas Gregor849b2432010-03-31 17:46:05 +00003245 << FixItHint::CreateInsertion(EllipsisLoc, ", ");
Douglas Gregored5d6512009-09-22 21:41:40 +00003246 }
3247 }
3248
3249 break;
3250 }
Mike Stump1eb44332009-09-09 15:08:12 +00003251
Chris Lattnerf97409f2008-04-06 06:57:35 +00003252 // Consume the comma.
3253 ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00003254 }
Mike Stump1eb44332009-09-09 15:08:12 +00003255
Chris Lattnerf97409f2008-04-06 06:57:35 +00003256 // Leave prototype scope.
Douglas Gregor8935b8b2008-12-10 06:34:36 +00003257 PrototypeScope.Exit();
Mike Stump1eb44332009-09-09 15:08:12 +00003258
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003259 // If we have the closing ')', eat it.
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00003260 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
3261 SourceLocation EndLoc = RParenLoc;
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003262
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003263 DeclSpec DS;
Sebastian Redl7dc81342009-04-29 17:30:04 +00003264 bool hasExceptionSpec = false;
Sebastian Redl3cc97262009-05-31 11:47:27 +00003265 SourceLocation ThrowLoc;
Sebastian Redl7dc81342009-04-29 17:30:04 +00003266 bool hasAnyExceptionSpec = false;
John McCallb3d87482010-08-24 05:47:05 +00003267 llvm::SmallVector<ParsedType, 2> Exceptions;
Sebastian Redlef65f062009-05-29 18:02:33 +00003268 llvm::SmallVector<SourceRange, 2> ExceptionRanges;
Sean Huntbbd37c62009-11-21 08:43:09 +00003269
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003270 if (getLang().CPlusPlus) {
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00003271 // Parse cv-qualifier-seq[opt].
Chris Lattner5a69d1c2008-12-18 07:02:59 +00003272 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Sebastian Redlab197ba2009-02-09 18:23:29 +00003273 if (!DS.getSourceRange().getEnd().isInvalid())
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00003274 EndLoc = DS.getSourceRange().getEnd();
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00003275
3276 // Parse exception-specification[opt].
Sebastian Redl7dc81342009-04-29 17:30:04 +00003277 if (Tok.is(tok::kw_throw)) {
3278 hasExceptionSpec = true;
Sebastian Redl3cc97262009-05-31 11:47:27 +00003279 ThrowLoc = Tok.getLocation();
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00003280 ParseExceptionSpecification(EndLoc, Exceptions, ExceptionRanges,
Sebastian Redlef65f062009-05-29 18:02:33 +00003281 hasAnyExceptionSpec);
3282 assert(Exceptions.size() == ExceptionRanges.size() &&
3283 "Produced different number of exception types and ranges.");
Sebastian Redl7dc81342009-04-29 17:30:04 +00003284 }
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003285 }
3286
Reid Spencer5f016e22007-07-11 17:01:13 +00003287 // Remember that we parsed a function type, and remember the attributes.
Chris Lattnerf97409f2008-04-06 06:57:35 +00003288 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/true, IsVariadic,
Douglas Gregor965acbb2009-02-18 07:07:28 +00003289 EllipsisLoc,
Jay Foadbeaaccd2009-05-21 09:52:38 +00003290 ParamInfo.data(), ParamInfo.size(),
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003291 DS.getTypeQualifiers(),
Sebastian Redl3cc97262009-05-31 11:47:27 +00003292 hasExceptionSpec, ThrowLoc,
Sebastian Redl7dc81342009-04-29 17:30:04 +00003293 hasAnyExceptionSpec,
Sebastian Redlef65f062009-05-29 18:02:33 +00003294 Exceptions.data(),
3295 ExceptionRanges.data(),
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00003296 Exceptions.size(),
3297 LParenLoc, RParenLoc, D),
3298 EndLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00003299}
3300
Chris Lattner66d28652008-04-06 06:34:08 +00003301/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
3302/// we found a K&R-style identifier list instead of a type argument list. The
Chris Lattner83a94472010-05-14 17:23:36 +00003303/// first identifier has already been consumed, and the current token is the
3304/// token right after it.
Chris Lattner66d28652008-04-06 06:34:08 +00003305///
3306/// identifier-list: [C99 6.7.5]
3307/// identifier
3308/// identifier-list ',' identifier
3309///
3310void Parser::ParseFunctionDeclaratorIdentifierList(SourceLocation LParenLoc,
Chris Lattner83a94472010-05-14 17:23:36 +00003311 IdentifierInfo *FirstIdent,
3312 SourceLocation FirstIdentLoc,
Chris Lattner66d28652008-04-06 06:34:08 +00003313 Declarator &D) {
3314 // Build up an array of information about the parsed arguments.
3315 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
3316 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
Mike Stump1eb44332009-09-09 15:08:12 +00003317
Chris Lattner66d28652008-04-06 06:34:08 +00003318 // If there was no identifier specified for the declarator, either we are in
3319 // an abstract-declarator, or we are in a parameter declarator which was found
3320 // to be abstract. In abstract-declarators, identifier lists are not valid:
3321 // diagnose this.
3322 if (!D.getIdentifier())
Chris Lattner83a94472010-05-14 17:23:36 +00003323 Diag(FirstIdentLoc, diag::ext_ident_list_in_param);
Chris Lattner66d28652008-04-06 06:34:08 +00003324
Chris Lattner83a94472010-05-14 17:23:36 +00003325 // The first identifier was already read, and is known to be the first
3326 // identifier in the list. Remember this identifier in ParamInfo.
3327 ParamsSoFar.insert(FirstIdent);
John McCalld226f652010-08-21 09:40:31 +00003328 ParamInfo.push_back(DeclaratorChunk::ParamInfo(FirstIdent, FirstIdentLoc, 0));
Mike Stump1eb44332009-09-09 15:08:12 +00003329
Chris Lattner66d28652008-04-06 06:34:08 +00003330 while (Tok.is(tok::comma)) {
3331 // Eat the comma.
3332 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00003333
Chris Lattner50c64772008-04-06 06:39:19 +00003334 // If this isn't an identifier, report the error and skip until ')'.
Chris Lattner66d28652008-04-06 06:34:08 +00003335 if (Tok.isNot(tok::identifier)) {
3336 Diag(Tok, diag::err_expected_ident);
Chris Lattner50c64772008-04-06 06:39:19 +00003337 SkipUntil(tok::r_paren);
3338 return;
Chris Lattner66d28652008-04-06 06:34:08 +00003339 }
Chris Lattneraaf9ddb2008-04-06 06:47:48 +00003340
Chris Lattner66d28652008-04-06 06:34:08 +00003341 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
Chris Lattneraaf9ddb2008-04-06 06:47:48 +00003342
3343 // Reject 'typedef int y; int test(x, y)', but continue parsing.
Douglas Gregor23c94db2010-07-02 17:43:08 +00003344 if (Actions.getTypeName(*ParmII, Tok.getLocation(), getCurScope()))
Chris Lattnerda83bac2008-11-19 07:37:42 +00003345 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
Mike Stump1eb44332009-09-09 15:08:12 +00003346
Chris Lattner66d28652008-04-06 06:34:08 +00003347 // Verify that the argument identifier has not already been mentioned.
3348 if (!ParamsSoFar.insert(ParmII)) {
Chris Lattnerda83bac2008-11-19 07:37:42 +00003349 Diag(Tok, diag::err_param_redefinition) << ParmII;
Chris Lattner50c64772008-04-06 06:39:19 +00003350 } else {
3351 // Remember this identifier in ParamInfo.
Chris Lattner66d28652008-04-06 06:34:08 +00003352 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Chris Lattnerb28317a2009-03-28 19:18:32 +00003353 Tok.getLocation(),
John McCalld226f652010-08-21 09:40:31 +00003354 0));
Chris Lattner50c64772008-04-06 06:39:19 +00003355 }
Mike Stump1eb44332009-09-09 15:08:12 +00003356
Chris Lattner66d28652008-04-06 06:34:08 +00003357 // Eat the identifier.
3358 ConsumeToken();
3359 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00003360
3361 // If we have the closing ')', eat it and we're done.
3362 SourceLocation RLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
3363
Chris Lattner50c64772008-04-06 06:39:19 +00003364 // Remember that we parsed a function type, and remember the attributes. This
3365 // function type is always a K&R style function type, which is not varargs and
3366 // has no prototype.
3367 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/false, /*varargs*/false,
Douglas Gregor965acbb2009-02-18 07:07:28 +00003368 SourceLocation(),
Chris Lattner50c64772008-04-06 06:39:19 +00003369 &ParamInfo[0], ParamInfo.size(),
Sebastian Redl7dc81342009-04-29 17:30:04 +00003370 /*TypeQuals*/0,
Sebastian Redl3cc97262009-05-31 11:47:27 +00003371 /*exception*/false,
3372 SourceLocation(), false, 0, 0, 0,
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00003373 LParenLoc, RLoc, D),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003374 RLoc);
Chris Lattner66d28652008-04-06 06:34:08 +00003375}
Chris Lattneref4715c2008-04-06 05:45:57 +00003376
Reid Spencer5f016e22007-07-11 17:01:13 +00003377/// [C90] direct-declarator '[' constant-expression[opt] ']'
3378/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
3379/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
3380/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
3381/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
3382void Parser::ParseBracketDeclarator(Declarator &D) {
3383 SourceLocation StartLoc = ConsumeBracket();
Mike Stump1eb44332009-09-09 15:08:12 +00003384
Chris Lattner378c7e42008-12-18 07:27:21 +00003385 // C array syntax has many features, but by-far the most common is [] and [4].
3386 // This code does a fast path to handle some of the most obvious cases.
3387 if (Tok.getKind() == tok::r_square) {
Sebastian Redlab197ba2009-02-09 18:23:29 +00003388 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
Sean Huntbbd37c62009-11-21 08:43:09 +00003389 //FIXME: Use these
3390 CXX0XAttributeList Attr;
3391 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier(true)) {
3392 Attr = ParseCXX0XAttributes();
3393 }
3394
Chris Lattner378c7e42008-12-18 07:27:21 +00003395 // Remember that we parsed the empty array type.
John McCall60d7b3a2010-08-24 06:29:42 +00003396 ExprResult NumElements;
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00003397 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0,
3398 StartLoc, EndLoc),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003399 EndLoc);
Chris Lattner378c7e42008-12-18 07:27:21 +00003400 return;
3401 } else if (Tok.getKind() == tok::numeric_constant &&
3402 GetLookAheadToken(1).is(tok::r_square)) {
3403 // [4] is very common. Parse the numeric constant expression.
John McCall60d7b3a2010-08-24 06:29:42 +00003404 ExprResult ExprRes(Actions.ActOnNumericConstant(Tok));
Chris Lattner378c7e42008-12-18 07:27:21 +00003405 ConsumeToken();
3406
Sebastian Redlab197ba2009-02-09 18:23:29 +00003407 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
Sean Huntbbd37c62009-11-21 08:43:09 +00003408 //FIXME: Use these
3409 CXX0XAttributeList Attr;
3410 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier()) {
3411 Attr = ParseCXX0XAttributes();
3412 }
Chris Lattner378c7e42008-12-18 07:27:21 +00003413
3414 // If there was an error parsing the assignment-expression, recover.
3415 if (ExprRes.isInvalid())
3416 ExprRes.release(); // Deallocate expr, just use [].
Mike Stump1eb44332009-09-09 15:08:12 +00003417
Chris Lattner378c7e42008-12-18 07:27:21 +00003418 // Remember that we parsed a array type, and remember its features.
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00003419 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, 0, ExprRes.release(),
3420 StartLoc, EndLoc),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003421 EndLoc);
Chris Lattner378c7e42008-12-18 07:27:21 +00003422 return;
3423 }
Mike Stump1eb44332009-09-09 15:08:12 +00003424
Reid Spencer5f016e22007-07-11 17:01:13 +00003425 // If valid, this location is the position where we read the 'static' keyword.
3426 SourceLocation StaticLoc;
Chris Lattner04d66662007-10-09 17:33:22 +00003427 if (Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00003428 StaticLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00003429
Reid Spencer5f016e22007-07-11 17:01:13 +00003430 // If there is a type-qualifier-list, read it now.
Chris Lattnera1fcbad2008-12-18 06:50:14 +00003431 // Type qualifiers in an array subscript are a C99 feature.
Reid Spencer5f016e22007-07-11 17:01:13 +00003432 DeclSpec DS;
Chris Lattner5a69d1c2008-12-18 07:02:59 +00003433 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Mike Stump1eb44332009-09-09 15:08:12 +00003434
Reid Spencer5f016e22007-07-11 17:01:13 +00003435 // If we haven't already read 'static', check to see if there is one after the
3436 // type-qualifier-list.
Chris Lattner04d66662007-10-09 17:33:22 +00003437 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00003438 StaticLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00003439
Reid Spencer5f016e22007-07-11 17:01:13 +00003440 // Handle "direct-declarator [ type-qual-list[opt] * ]".
3441 bool isStar = false;
John McCall60d7b3a2010-08-24 06:29:42 +00003442 ExprResult NumElements;
Mike Stump1eb44332009-09-09 15:08:12 +00003443
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00003444 // Handle the case where we have '[*]' as the array size. However, a leading
3445 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
3446 // the the token after the star is a ']'. Since stars in arrays are
3447 // infrequent, use of lookahead is not costly here.
3448 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattnera711dd02008-04-06 05:27:21 +00003449 ConsumeToken(); // Eat the '*'.
Reid Spencer5f016e22007-07-11 17:01:13 +00003450
Chris Lattnera1fcbad2008-12-18 06:50:14 +00003451 if (StaticLoc.isValid()) {
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00003452 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattnera1fcbad2008-12-18 06:50:14 +00003453 StaticLoc = SourceLocation(); // Drop the static.
3454 }
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00003455 isStar = true;
Chris Lattner04d66662007-10-09 17:33:22 +00003456 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner378c7e42008-12-18 07:27:21 +00003457 // Note, in C89, this production uses the constant-expr production instead
3458 // of assignment-expr. The only difference is that assignment-expr allows
3459 // things like '=' and '*='. Sema rejects these in C89 mode because they
3460 // are not i-c-e's, so we don't need to distinguish between the two here.
Mike Stump1eb44332009-09-09 15:08:12 +00003461
Douglas Gregore0762c92009-06-19 23:52:42 +00003462 // Parse the constant-expression or assignment-expression now (depending
3463 // on dialect).
3464 if (getLang().CPlusPlus)
3465 NumElements = ParseConstantExpression();
3466 else
3467 NumElements = ParseAssignmentExpression();
Reid Spencer5f016e22007-07-11 17:01:13 +00003468 }
Mike Stump1eb44332009-09-09 15:08:12 +00003469
Reid Spencer5f016e22007-07-11 17:01:13 +00003470 // If there was an error parsing the assignment-expression, recover.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00003471 if (NumElements.isInvalid()) {
Chris Lattner5cb10d32009-04-24 22:30:50 +00003472 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00003473 // If the expression was invalid, skip it.
3474 SkipUntil(tok::r_square);
3475 return;
3476 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00003477
3478 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
3479
Sean Huntbbd37c62009-11-21 08:43:09 +00003480 //FIXME: Use these
3481 CXX0XAttributeList Attr;
3482 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier()) {
3483 Attr = ParseCXX0XAttributes();
3484 }
3485
Chris Lattner378c7e42008-12-18 07:27:21 +00003486 // Remember that we parsed a array type, and remember its features.
Reid Spencer5f016e22007-07-11 17:01:13 +00003487 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
3488 StaticLoc.isValid(), isStar,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00003489 NumElements.release(),
3490 StartLoc, EndLoc),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003491 EndLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00003492}
3493
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00003494/// [GNU] typeof-specifier:
3495/// typeof ( expressions )
3496/// typeof ( type-name )
3497/// [GNU/C++] typeof unary-expression
Steve Naroffd1861fd2007-07-31 12:34:36 +00003498///
3499void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner04d66662007-10-09 17:33:22 +00003500 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00003501 Token OpTok = Tok;
Steve Naroffd1861fd2007-07-31 12:34:36 +00003502 SourceLocation StartLoc = ConsumeToken();
3503
John McCallcfb708c2010-01-13 20:03:27 +00003504 const bool hasParens = Tok.is(tok::l_paren);
3505
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00003506 bool isCastExpr;
John McCallb3d87482010-08-24 05:47:05 +00003507 ParsedType CastTy;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00003508 SourceRange CastRange;
John McCall60d7b3a2010-08-24 06:29:42 +00003509 ExprResult Operand = ParseExprAfterTypeofSizeofAlignof(OpTok,
John McCall911093e2010-08-25 02:45:51 +00003510 isCastExpr,
3511 CastTy,
3512 CastRange);
John McCallcfb708c2010-01-13 20:03:27 +00003513 if (hasParens)
3514 DS.setTypeofParensRange(CastRange);
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00003515
3516 if (CastRange.getEnd().isInvalid())
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00003517 // FIXME: Not accurate, the range gets one token more than it should.
3518 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00003519 else
3520 DS.SetRangeEnd(CastRange.getEnd());
Mike Stump1eb44332009-09-09 15:08:12 +00003521
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00003522 if (isCastExpr) {
3523 if (!CastTy) {
3524 DS.SetTypeSpecError();
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00003525 return;
Douglas Gregor809070a2009-02-18 17:45:20 +00003526 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00003527
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00003528 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00003529 unsigned DiagID;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00003530 // Check for duplicate type specifiers (e.g. "int typeof(int)").
3531 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00003532 DiagID, CastTy))
3533 Diag(StartLoc, DiagID) << PrevSpec;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00003534 return;
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00003535 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00003536
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00003537 // If we get here, the operand to the typeof was an expresion.
3538 if (Operand.isInvalid()) {
3539 DS.SetTypeSpecError();
Steve Naroff9dfa7b42007-08-02 02:53:48 +00003540 return;
Steve Naroffd1861fd2007-07-31 12:34:36 +00003541 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00003542
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00003543 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00003544 unsigned DiagID;
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00003545 // Check for duplicate type specifiers (e.g. "int typeof(int)").
3546 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
John McCallb3d87482010-08-24 05:47:05 +00003547 DiagID, Operand.get()))
John McCallfec54012009-08-03 20:12:06 +00003548 Diag(StartLoc, DiagID) << PrevSpec;
Steve Naroffd1861fd2007-07-31 12:34:36 +00003549}
Chris Lattner1b492422010-02-28 18:33:55 +00003550
3551
3552/// TryAltiVecVectorTokenOutOfLine - Out of line body that should only be called
3553/// from TryAltiVecVectorToken.
3554bool Parser::TryAltiVecVectorTokenOutOfLine() {
3555 Token Next = NextToken();
3556 switch (Next.getKind()) {
3557 default: return false;
3558 case tok::kw_short:
3559 case tok::kw_long:
3560 case tok::kw_signed:
3561 case tok::kw_unsigned:
3562 case tok::kw_void:
3563 case tok::kw_char:
3564 case tok::kw_int:
3565 case tok::kw_float:
3566 case tok::kw_double:
3567 case tok::kw_bool:
3568 case tok::kw___pixel:
3569 Tok.setKind(tok::kw___vector);
3570 return true;
3571 case tok::identifier:
3572 if (Next.getIdentifierInfo() == Ident_pixel) {
3573 Tok.setKind(tok::kw___vector);
3574 return true;
3575 }
3576 return false;
3577 }
3578}
3579
3580bool Parser::TryAltiVecTokenOutOfLine(DeclSpec &DS, SourceLocation Loc,
3581 const char *&PrevSpec, unsigned &DiagID,
3582 bool &isInvalid) {
3583 if (Tok.getIdentifierInfo() == Ident_vector) {
3584 Token Next = NextToken();
3585 switch (Next.getKind()) {
3586 case tok::kw_short:
3587 case tok::kw_long:
3588 case tok::kw_signed:
3589 case tok::kw_unsigned:
3590 case tok::kw_void:
3591 case tok::kw_char:
3592 case tok::kw_int:
3593 case tok::kw_float:
3594 case tok::kw_double:
3595 case tok::kw_bool:
3596 case tok::kw___pixel:
3597 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
3598 return true;
3599 case tok::identifier:
3600 if (Next.getIdentifierInfo() == Ident_pixel) {
3601 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
3602 return true;
3603 }
3604 break;
3605 default:
3606 break;
3607 }
Douglas Gregora8f031f2010-06-16 15:28:57 +00003608 } else if ((Tok.getIdentifierInfo() == Ident_pixel) &&
Chris Lattner1b492422010-02-28 18:33:55 +00003609 DS.isTypeAltiVecVector()) {
3610 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
3611 return true;
3612 }
3613 return false;
3614}
3615