blob: 24e41d073799fa775d8557e692b804bd1a2f4d2c [file] [log] [blame]
Chris Lattner7ad0fbe2006-11-05 07:46:30 +00001//===--- ParseDecl.cpp - Declaration Parsing ------------------------------===//
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Declaration portions of the Parser interfaces.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Parse/Parser.h"
Chris Lattner60f36222009-01-29 05:15:15 +000015#include "clang/Parse/ParseDiagnostic.h"
John McCall8b0666c2010-08-20 18:27:03 +000016#include "clang/Sema/Scope.h"
17#include "clang/Sema/ParsedTemplate.h"
John McCallfaf5fb42010-08-26 23:41:50 +000018#include "clang/Sema/PrettyDeclStackTrace.h"
Chris Lattner8a9a97a2009-12-10 00:21:05 +000019#include "RAIIObjectsForParser.h"
Chris Lattnerad9ac942007-01-23 01:14:52 +000020#include "llvm/ADT/SmallSet.h"
Chris Lattnerc0acd3d2006-07-31 05:13:43 +000021using namespace clang;
22
23//===----------------------------------------------------------------------===//
24// C99 6.7: Declarations.
25//===----------------------------------------------------------------------===//
26
Chris Lattnerf5fbd792006-08-10 23:56:11 +000027/// ParseTypeName
28/// type-name: [C99 6.7.6]
29/// specifier-qualifier-list abstract-declarator[opt]
Sebastian Redlbd150f42008-11-21 19:14:01 +000030///
31/// Called type-id in C++.
John McCallfaf5fb42010-08-26 23:41:50 +000032TypeResult Parser::ParseTypeName(SourceRange *Range) {
Chris Lattnerf5fbd792006-08-10 23:56:11 +000033 // Parse the common declaration-specifiers piece.
34 DeclSpec DS;
Chris Lattner1890ac82006-08-13 01:16:23 +000035 ParseSpecifierQualifierList(DS);
Sebastian Redld6434562009-05-29 18:02:33 +000036
Chris Lattnerf5fbd792006-08-10 23:56:11 +000037 // Parse the abstract-declarator, if present.
38 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
39 ParseDeclarator(DeclaratorInfo);
Sebastian Redld6434562009-05-29 18:02:33 +000040 if (Range)
41 *Range = DeclaratorInfo.getSourceRange();
42
Chris Lattnerf6d1c9c2009-04-25 08:06:05 +000043 if (DeclaratorInfo.isInvalidType())
Douglas Gregor220cac52009-02-18 17:45:20 +000044 return true;
45
Douglas Gregor0be31a22010-07-02 17:43:08 +000046 return Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
Chris Lattnerf5fbd792006-08-10 23:56:11 +000047}
48
Alexis Hunt96d5c762009-11-21 08:43:09 +000049/// ParseGNUAttributes - Parse a non-empty attributes list.
Chris Lattnerb8cd5c22006-08-15 04:10:46 +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
Steve Naroff0f2fe172007-06-01 17:11:19 +000064/// attrib-name
65/// attrib-name '(' identifier ')'
66/// attrib-name '(' identifier ',' nonempty-expr-list ')'
67/// attrib-name '(' argument-expression-list [C99 6.5.2] ')'
Chris Lattnerb8cd5c22006-08-15 04:10:46 +000068///
Steve Naroff0f2fe172007-06-01 17:11:19 +000069/// [GNU] attrib-name:
70/// identifier
71/// typespec
72/// typequal
73/// storageclass
Mike Stump11289f42009-09-09 15:08:12 +000074///
Steve Naroff0f2fe172007-06-01 17:11:19 +000075/// FIXME: The GCC grammar/code for this construct implies we need two
Mike Stump11289f42009-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
Steve Naroff0f2fe172007-06-01 17:11:19 +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.
Chris Lattnerb8cd5c22006-08-15 04:10:46 +000084
John McCall53fa7142010-12-24 02:08:15 +000085void Parser::ParseGNUAttributes(ParsedAttributes &attrs,
86 SourceLocation *endLoc) {
Alexis Hunt96d5c762009-11-21 08:43:09 +000087 assert(Tok.is(tok::kw___attribute) && "Not a GNU attribute list!");
Mike Stump11289f42009-09-09 15:08:12 +000088
Chris Lattner76c72282007-10-09 17:33:22 +000089 while (Tok.is(tok::kw___attribute)) {
Steve Naroff0f2fe172007-06-01 17:11:19 +000090 ConsumeToken();
91 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
92 "attribute")) {
93 SkipUntil(tok::r_paren, true); // skip until ) or ;
John McCall53fa7142010-12-24 02:08:15 +000094 return;
Steve Naroff0f2fe172007-06-01 17:11:19 +000095 }
96 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, "(")) {
97 SkipUntil(tok::r_paren, true); // skip until ) or ;
John McCall53fa7142010-12-24 02:08:15 +000098 return;
Steve Naroff0f2fe172007-06-01 17:11:19 +000099 }
100 // Parse the attribute-list. e.g. __attribute__(( weak, alias("__f") ))
Chris Lattner76c72282007-10-09 17:33:22 +0000101 while (Tok.is(tok::identifier) || isDeclarationSpecifier() ||
102 Tok.is(tok::comma)) {
Mike Stump11289f42009-09-09 15:08:12 +0000103
104 if (Tok.is(tok::comma)) {
Steve Naroff0f2fe172007-06-01 17:11:19 +0000105 // allows for empty/non-empty attributes. ((__vector_size__(16),,,,))
106 ConsumeToken();
107 continue;
108 }
109 // we have an identifier or declaration specifier (const, int, etc.)
110 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
111 SourceLocation AttrNameLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000112
Douglas Gregora2f49452010-03-16 19:09:18 +0000113 // check if we have a "parameterized" attribute
Chris Lattner76c72282007-10-09 17:33:22 +0000114 if (Tok.is(tok::l_paren)) {
Steve Naroffb8371e12007-06-09 03:39:29 +0000115 ConsumeParen(); // ignore the left paren loc for now
Mike Stump11289f42009-09-09 15:08:12 +0000116
Chris Lattner76c72282007-10-09 17:33:22 +0000117 if (Tok.is(tok::identifier)) {
Steve Naroff0f2fe172007-06-01 17:11:19 +0000118 IdentifierInfo *ParmName = Tok.getIdentifierInfo();
119 SourceLocation ParmLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000120
121 if (Tok.is(tok::r_paren)) {
Steve Naroff0f2fe172007-06-01 17:11:19 +0000122 // __attribute__(( mode(byte) ))
Steve Naroffb8371e12007-06-09 03:39:29 +0000123 ConsumeParen(); // ignore the right paren loc for now
John McCall53fa7142010-12-24 02:08:15 +0000124 attrs.add(AttrFactory.Create(AttrName, AttrNameLoc, 0, AttrNameLoc,
125 ParmName, ParmLoc, 0, 0));
Chris Lattner76c72282007-10-09 17:33:22 +0000126 } else if (Tok.is(tok::comma)) {
Steve Naroff0f2fe172007-06-01 17:11:19 +0000127 ConsumeToken();
128 // __attribute__(( format(printf, 1, 2) ))
Sebastian Redl511ed552008-11-25 22:21:31 +0000129 ExprVector ArgExprs(Actions);
Steve Naroff0f2fe172007-06-01 17:11:19 +0000130 bool ArgExprsOk = true;
Mike Stump11289f42009-09-09 15:08:12 +0000131
Steve Naroff0f2fe172007-06-01 17:11:19 +0000132 // now parse the non-empty comma separated list of expressions
133 while (1) {
John McCalldadc5752010-08-24 06:29:42 +0000134 ExprResult ArgExpr(ParseAssignmentExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000135 if (ArgExpr.isInvalid()) {
Steve Naroff0f2fe172007-06-01 17:11:19 +0000136 ArgExprsOk = false;
137 SkipUntil(tok::r_paren);
138 break;
139 } else {
Sebastian Redld9f7b1c2008-12-10 00:02:53 +0000140 ArgExprs.push_back(ArgExpr.release());
Steve Naroff0f2fe172007-06-01 17:11:19 +0000141 }
Chris Lattner76c72282007-10-09 17:33:22 +0000142 if (Tok.isNot(tok::comma))
Steve Naroff0f2fe172007-06-01 17:11:19 +0000143 break;
144 ConsumeToken(); // Eat the comma, move to the next argument
145 }
Chris Lattner76c72282007-10-09 17:33:22 +0000146 if (ArgExprsOk && Tok.is(tok::r_paren)) {
Steve Naroffb8371e12007-06-09 03:39:29 +0000147 ConsumeParen(); // ignore the right paren loc for now
John McCall53fa7142010-12-24 02:08:15 +0000148 attrs.add(AttrFactory.Create(AttrName, AttrNameLoc, 0,
149 AttrNameLoc, ParmName, ParmLoc,
150 ArgExprs.take(), ArgExprs.size()));
Steve Naroff0f2fe172007-06-01 17:11:19 +0000151 }
152 }
153 } else { // not an identifier
Nate Begemanf2758702009-06-26 06:32:41 +0000154 switch (Tok.getKind()) {
155 case tok::r_paren:
Steve Naroff0f2fe172007-06-01 17:11:19 +0000156 // parse a possibly empty comma separated list of expressions
Steve Naroff0f2fe172007-06-01 17:11:19 +0000157 // __attribute__(( nonnull() ))
Steve Naroffb8371e12007-06-09 03:39:29 +0000158 ConsumeParen(); // ignore the right paren loc for now
John McCall53fa7142010-12-24 02:08:15 +0000159 attrs.add(AttrFactory.Create(AttrName, AttrNameLoc, 0, AttrNameLoc,
160 0, SourceLocation(), 0, 0));
Nate Begemanf2758702009-06-26 06:32:41 +0000161 break;
162 case tok::kw_char:
163 case tok::kw_wchar_t:
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +0000164 case tok::kw_char16_t:
165 case tok::kw_char32_t:
Nate Begemanf2758702009-06-26 06:32:41 +0000166 case tok::kw_bool:
167 case tok::kw_short:
168 case tok::kw_int:
169 case tok::kw_long:
170 case tok::kw_signed:
171 case tok::kw_unsigned:
172 case tok::kw_float:
173 case tok::kw_double:
174 case tok::kw_void:
John McCall53fa7142010-12-24 02:08:15 +0000175 case tok::kw_typeof: {
176 AttributeList *attr
177 = AttrFactory.Create(AttrName, AttrNameLoc, 0, AttrNameLoc,
178 0, SourceLocation(), 0, 0);
179 attrs.add(attr);
180 if (attr->getKind() == AttributeList::AT_IBOutletCollection)
Fariborz Jahanian9d7d3d82010-08-17 23:19:16 +0000181 Diag(Tok, diag::err_iboutletcollection_builtintype);
Nate Begemanf2758702009-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 Begemanf2758702009-06-26 06:32:41 +0000185 if (Tok.is(tok::r_paren))
186 ConsumeParen();
187 break;
John McCall53fa7142010-12-24 02:08:15 +0000188 }
Nate Begemanf2758702009-06-26 06:32:41 +0000189 default:
Steve Naroff0f2fe172007-06-01 17:11:19 +0000190 // __attribute__(( aligned(16) ))
Sebastian Redl511ed552008-11-25 22:21:31 +0000191 ExprVector ArgExprs(Actions);
Steve Naroff0f2fe172007-06-01 17:11:19 +0000192 bool ArgExprsOk = true;
Mike Stump11289f42009-09-09 15:08:12 +0000193
Steve Naroff0f2fe172007-06-01 17:11:19 +0000194 // now parse the list of expressions
195 while (1) {
John McCalldadc5752010-08-24 06:29:42 +0000196 ExprResult ArgExpr(ParseAssignmentExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000197 if (ArgExpr.isInvalid()) {
Steve Naroff0f2fe172007-06-01 17:11:19 +0000198 ArgExprsOk = false;
199 SkipUntil(tok::r_paren);
200 break;
201 } else {
Sebastian Redld9f7b1c2008-12-10 00:02:53 +0000202 ArgExprs.push_back(ArgExpr.release());
Steve Naroff0f2fe172007-06-01 17:11:19 +0000203 }
Chris Lattner76c72282007-10-09 17:33:22 +0000204 if (Tok.isNot(tok::comma))
Steve Naroff0f2fe172007-06-01 17:11:19 +0000205 break;
206 ConsumeToken(); // Eat the comma, move to the next argument
207 }
208 // Match the ')'.
Chris Lattner76c72282007-10-09 17:33:22 +0000209 if (ArgExprsOk && Tok.is(tok::r_paren)) {
Steve Naroffb8371e12007-06-09 03:39:29 +0000210 ConsumeParen(); // ignore the right paren loc for now
John McCall53fa7142010-12-24 02:08:15 +0000211 attrs.add(AttrFactory.Create(AttrName, AttrNameLoc, 0,
212 AttrNameLoc, 0, SourceLocation(),
213 ArgExprs.take(), ArgExprs.size()));
Steve Naroff0f2fe172007-06-01 17:11:19 +0000214 }
Nate Begemanf2758702009-06-26 06:32:41 +0000215 break;
Steve Naroff0f2fe172007-06-01 17:11:19 +0000216 }
217 }
218 } else {
John McCall53fa7142010-12-24 02:08:15 +0000219 attrs.add(AttrFactory.Create(AttrName, AttrNameLoc, 0, AttrNameLoc,
220 0, SourceLocation(), 0, 0));
Steve Naroff0f2fe172007-06-01 17:11:19 +0000221 }
222 }
Steve Naroff98d153c2007-06-06 23:19:11 +0000223 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
Steve Naroff98d153c2007-06-06 23:19:11 +0000224 SkipUntil(tok::r_paren, false);
Alexis Hunt96d5c762009-11-21 08:43:09 +0000225 SourceLocation Loc = Tok.getLocation();
Sebastian Redlf6591ca2009-02-09 18:23:29 +0000226 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) {
227 SkipUntil(tok::r_paren, false);
228 }
John McCall53fa7142010-12-24 02:08:15 +0000229 if (endLoc)
230 *endLoc = Loc;
Steve Naroff0f2fe172007-06-01 17:11:19 +0000231 }
Steve Naroff0f2fe172007-06-01 17:11:19 +0000232}
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000233
Eli Friedman06de2b52009-06-08 07:21:15 +0000234/// ParseMicrosoftDeclSpec - Parse an __declspec construct
235///
236/// [MS] decl-specifier:
237/// __declspec ( extended-decl-modifier-seq )
238///
239/// [MS] extended-decl-modifier-seq:
240/// extended-decl-modifier[opt]
241/// extended-decl-modifier extended-decl-modifier-seq
242
John McCall53fa7142010-12-24 02:08:15 +0000243void Parser::ParseMicrosoftDeclSpec(ParsedAttributes &attrs) {
Steve Naroff3a9b7e02008-12-24 20:59:21 +0000244 assert(Tok.is(tok::kw___declspec) && "Not a declspec!");
Eli Friedman06de2b52009-06-08 07:21:15 +0000245
Steve Naroff3a9b7e02008-12-24 20:59:21 +0000246 ConsumeToken();
Eli Friedman06de2b52009-06-08 07:21:15 +0000247 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
248 "declspec")) {
249 SkipUntil(tok::r_paren, true); // skip until ) or ;
John McCall53fa7142010-12-24 02:08:15 +0000250 return;
Eli Friedman06de2b52009-06-08 07:21:15 +0000251 }
Eli Friedman53339e02009-06-08 23:27:34 +0000252 while (Tok.getIdentifierInfo()) {
Eli Friedman06de2b52009-06-08 07:21:15 +0000253 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
254 SourceLocation AttrNameLoc = ConsumeToken();
255 if (Tok.is(tok::l_paren)) {
256 ConsumeParen();
257 // FIXME: This doesn't parse __declspec(property(get=get_func_name))
258 // correctly.
John McCalldadc5752010-08-24 06:29:42 +0000259 ExprResult ArgExpr(ParseAssignmentExpression());
Eli Friedman06de2b52009-06-08 07:21:15 +0000260 if (!ArgExpr.isInvalid()) {
John McCall37ad5512010-08-23 06:44:23 +0000261 Expr *ExprList = ArgExpr.take();
John McCall53fa7142010-12-24 02:08:15 +0000262 attrs.add(AttrFactory.Create(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
263 SourceLocation(), &ExprList, 1, true));
Eli Friedman06de2b52009-06-08 07:21:15 +0000264 }
265 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
266 SkipUntil(tok::r_paren, false);
267 } else {
John McCall53fa7142010-12-24 02:08:15 +0000268 attrs.add(AttrFactory.Create(AttrName, AttrNameLoc, 0, AttrNameLoc,
269 0, SourceLocation(), 0, 0, true));
Eli Friedman06de2b52009-06-08 07:21:15 +0000270 }
271 }
272 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
273 SkipUntil(tok::r_paren, false);
John McCall53fa7142010-12-24 02:08:15 +0000274 return;
Eli Friedman53339e02009-06-08 23:27:34 +0000275}
276
John McCall53fa7142010-12-24 02:08:15 +0000277void Parser::ParseMicrosoftTypeAttributes(ParsedAttributes &attrs) {
Eli Friedman53339e02009-06-08 23:27:34 +0000278 // Treat these like attributes
279 // FIXME: Allow Sema to distinguish between these and real attributes!
280 while (Tok.is(tok::kw___fastcall) || Tok.is(tok::kw___stdcall) ||
Douglas Gregora941dca2010-05-18 16:57:00 +0000281 Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___cdecl) ||
282 Tok.is(tok::kw___ptr64) || Tok.is(tok::kw___w64)) {
Eli Friedman53339e02009-06-08 23:27:34 +0000283 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
284 SourceLocation AttrNameLoc = ConsumeToken();
285 if (Tok.is(tok::kw___ptr64) || Tok.is(tok::kw___w64))
286 // FIXME: Support these properly!
287 continue;
John McCall53fa7142010-12-24 02:08:15 +0000288 attrs.add(AttrFactory.Create(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
289 SourceLocation(), 0, 0, true));
Eli Friedman53339e02009-06-08 23:27:34 +0000290 }
Steve Naroff3a9b7e02008-12-24 20:59:21 +0000291}
292
John McCall53fa7142010-12-24 02:08:15 +0000293void Parser::ParseBorlandTypeAttributes(ParsedAttributes &attrs) {
Dawn Perchik335e16b2010-09-03 01:29:35 +0000294 // Treat these like attributes
295 while (Tok.is(tok::kw___pascal)) {
296 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
297 SourceLocation AttrNameLoc = ConsumeToken();
John McCall53fa7142010-12-24 02:08:15 +0000298 attrs.add(AttrFactory.Create(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
299 SourceLocation(), 0, 0, true));
Dawn Perchik335e16b2010-09-03 01:29:35 +0000300 }
John McCall53fa7142010-12-24 02:08:15 +0000301}
302
303void Parser::DiagnoseProhibitedAttributes(ParsedAttributesWithRange &attrs) {
304 Diag(attrs.Range.getBegin(), diag::err_attributes_not_allowed)
305 << attrs.Range;
Dawn Perchik335e16b2010-09-03 01:29:35 +0000306}
307
Chris Lattner53361ac2006-08-10 05:19:57 +0000308/// ParseDeclaration - Parse a full 'declaration', which consists of
309/// declaration-specifiers, some number of declarators, and a semicolon.
Chris Lattner49836b42009-04-02 04:16:50 +0000310/// 'Context' should be a Declarator::TheContext value. This returns the
311/// location of the semicolon in DeclEnd.
Chris Lattnera5235172007-08-25 06:57:03 +0000312///
313/// declaration: [C99 6.7]
314/// block-declaration ->
315/// simple-declaration
316/// others [FIXME]
Douglas Gregoreb31f392008-12-01 23:54:00 +0000317/// [C++] template-declaration
Chris Lattnera5235172007-08-25 06:57:03 +0000318/// [C++] namespace-definition
Douglas Gregord7c4d982008-12-30 03:27:21 +0000319/// [C++] using-directive
Douglas Gregor77b50e12009-06-22 23:06:13 +0000320/// [C++] using-declaration
Sebastian Redlf769df52009-03-24 22:27:57 +0000321/// [C++0x] static_assert-declaration
Chris Lattnera5235172007-08-25 06:57:03 +0000322/// others... [FIXME]
323///
Fariborz Jahanian1db5c942010-09-28 20:42:35 +0000324Parser::DeclGroupPtrTy Parser::ParseDeclaration(StmtVector &Stmts,
325 unsigned Context,
Alexis Hunt96d5c762009-11-21 08:43:09 +0000326 SourceLocation &DeclEnd,
John McCall53fa7142010-12-24 02:08:15 +0000327 ParsedAttributesWithRange &attrs) {
Argyrios Kyrtzidis355094e2010-06-17 10:52:18 +0000328 ParenBraceBracketBalancer BalancerRAIIObj(*this);
329
John McCall48871652010-08-21 09:40:31 +0000330 Decl *SingleDecl = 0;
Chris Lattnera5235172007-08-25 06:57:03 +0000331 switch (Tok.getKind()) {
Douglas Gregoreb31f392008-12-01 23:54:00 +0000332 case tok::kw_template:
Douglas Gregor23996282009-05-12 21:31:51 +0000333 case tok::kw_export:
John McCall53fa7142010-12-24 02:08:15 +0000334 ProhibitAttributes(attrs);
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000335 SingleDecl = ParseDeclarationStartingWithTemplate(Context, DeclEnd);
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000336 break;
Sebastian Redl67667942010-08-27 23:12:46 +0000337 case tok::kw_inline:
Sebastian Redl5a5f2c72010-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)) {
John McCall53fa7142010-12-24 02:08:15 +0000340 ProhibitAttributes(attrs);
Sebastian Redl67667942010-08-27 23:12:46 +0000341 SourceLocation InlineLoc = ConsumeToken();
342 SingleDecl = ParseNamespace(Context, DeclEnd, InlineLoc);
343 break;
344 }
John McCall53fa7142010-12-24 02:08:15 +0000345 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs,
Fariborz Jahanian1db5c942010-09-28 20:42:35 +0000346 true);
Chris Lattnera5235172007-08-25 06:57:03 +0000347 case tok::kw_namespace:
John McCall53fa7142010-12-24 02:08:15 +0000348 ProhibitAttributes(attrs);
Chris Lattner49836b42009-04-02 04:16:50 +0000349 SingleDecl = ParseNamespace(Context, DeclEnd);
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000350 break;
Douglas Gregord7c4d982008-12-30 03:27:21 +0000351 case tok::kw_using:
John McCall9b72f892010-11-10 02:40:36 +0000352 SingleDecl = ParseUsingDirectiveOrDeclaration(Context, ParsedTemplateInfo(),
John McCall53fa7142010-12-24 02:08:15 +0000353 DeclEnd, attrs);
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000354 break;
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000355 case tok::kw_static_assert:
John McCall53fa7142010-12-24 02:08:15 +0000356 ProhibitAttributes(attrs);
Chris Lattner49836b42009-04-02 04:16:50 +0000357 SingleDecl = ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000358 break;
Chris Lattnera5235172007-08-25 06:57:03 +0000359 default:
John McCall53fa7142010-12-24 02:08:15 +0000360 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs, true);
Chris Lattnera5235172007-08-25 06:57:03 +0000361 }
Alexis Hunt96d5c762009-11-21 08:43:09 +0000362
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000363 // This routine returns a DeclGroup, if the thing we parsed only contains a
364 // single decl, convert it now.
365 return Actions.ConvertDeclToDeclGroup(SingleDecl);
Chris Lattnera5235172007-08-25 06:57:03 +0000366}
367
368/// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl]
369/// declaration-specifiers init-declarator-list[opt] ';'
370///[C90/C++]init-declarator-list ';' [TODO]
371/// [OMP] threadprivate-directive [TODO]
Chris Lattner32dc41c2009-03-29 17:27:48 +0000372///
373/// If RequireSemi is false, this does not check for a ';' at the end of the
Chris Lattner005fc1b2010-04-05 18:18:31 +0000374/// declaration. If it is true, it checks for and eats it.
Fariborz Jahanian1db5c942010-09-28 20:42:35 +0000375Parser::DeclGroupPtrTy Parser::ParseSimpleDeclaration(StmtVector &Stmts,
376 unsigned Context,
Alexis Hunt96d5c762009-11-21 08:43:09 +0000377 SourceLocation &DeclEnd,
John McCall53fa7142010-12-24 02:08:15 +0000378 ParsedAttributes &attrs,
Chris Lattner005fc1b2010-04-05 18:18:31 +0000379 bool RequireSemi) {
Chris Lattner53361ac2006-08-10 05:19:57 +0000380 // Parse the common declaration-specifiers piece.
John McCall28a6aea2009-11-04 02:18:39 +0000381 ParsingDeclSpec DS(*this);
John McCall53fa7142010-12-24 02:08:15 +0000382 DS.takeAttributesFrom(attrs);
Douglas Gregor9de54ea2010-01-13 17:31:36 +0000383 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS_none,
384 getDeclSpecContextFromDeclaratorContext(Context));
Fariborz Jahanian1db5c942010-09-28 20:42:35 +0000385 StmtResult R = Actions.ActOnVlaStmt(DS);
386 if (R.isUsable())
387 Stmts.push_back(R.release());
Mike Stump11289f42009-09-09 15:08:12 +0000388
Chris Lattner0e894622006-08-13 19:58:17 +0000389 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
390 // declaration-specifiers init-declarator-list[opt] ';'
Chris Lattner76c72282007-10-09 17:33:22 +0000391 if (Tok.is(tok::semi)) {
Chris Lattner005fc1b2010-04-05 18:18:31 +0000392 if (RequireSemi) ConsumeToken();
John McCall48871652010-08-21 09:40:31 +0000393 Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none,
John McCallb54367d2010-05-21 20:45:30 +0000394 DS);
John McCall28a6aea2009-11-04 02:18:39 +0000395 DS.complete(TheDecl);
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000396 return Actions.ConvertDeclToDeclGroup(TheDecl);
Chris Lattner0e894622006-08-13 19:58:17 +0000397 }
Mike Stump11289f42009-09-09 15:08:12 +0000398
Chris Lattner005fc1b2010-04-05 18:18:31 +0000399 return ParseDeclGroup(DS, Context, /*FunctionDefs=*/ false, &DeclEnd);
John McCalld5a36322009-11-03 19:26:08 +0000400}
Mike Stump11289f42009-09-09 15:08:12 +0000401
John McCalld5a36322009-11-03 19:26:08 +0000402/// ParseDeclGroup - Having concluded that this is either a function
403/// definition or a group of object declarations, actually parse the
404/// result.
John McCall28a6aea2009-11-04 02:18:39 +0000405Parser::DeclGroupPtrTy Parser::ParseDeclGroup(ParsingDeclSpec &DS,
406 unsigned Context,
John McCalld5a36322009-11-03 19:26:08 +0000407 bool AllowFunctionDefinitions,
408 SourceLocation *DeclEnd) {
409 // Parse the first declarator.
John McCall28a6aea2009-11-04 02:18:39 +0000410 ParsingDeclarator D(*this, DS, static_cast<Declarator::TheContext>(Context));
John McCalld5a36322009-11-03 19:26:08 +0000411 ParseDeclarator(D);
Chris Lattner32dc41c2009-03-29 17:27:48 +0000412
John McCalld5a36322009-11-03 19:26:08 +0000413 // Bail out if the first declarator didn't seem well-formed.
414 if (!D.hasName() && !D.mayOmitIdentifier()) {
415 // Skip until ; or }.
416 SkipUntil(tok::r_brace, true, true);
417 if (Tok.is(tok::semi))
418 ConsumeToken();
419 return DeclGroupPtrTy();
Chris Lattnerefb0f112009-03-29 17:18:04 +0000420 }
Mike Stump11289f42009-09-09 15:08:12 +0000421
Chris Lattnerdbb1e932010-07-11 22:24:20 +0000422 // Check to see if we have a function *definition* which must have a body.
423 if (AllowFunctionDefinitions && D.isFunctionDeclarator() &&
424 // Look at the next token to make sure that this isn't a function
425 // declaration. We have to check this because __attribute__ might be the
426 // start of a function definition in GCC-extended K&R C.
427 !isDeclarationAfterDeclarator()) {
428
Chris Lattner13901342010-07-11 22:42:07 +0000429 if (isStartOfFunctionDefinition(D)) {
John McCalld5a36322009-11-03 19:26:08 +0000430 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
431 Diag(Tok, diag::err_function_declared_typedef);
432
433 // Recover by treating the 'typedef' as spurious.
434 DS.ClearStorageClassSpecs();
435 }
436
John McCall48871652010-08-21 09:40:31 +0000437 Decl *TheDecl = ParseFunctionDefinition(D);
John McCalld5a36322009-11-03 19:26:08 +0000438 return Actions.ConvertDeclToDeclGroup(TheDecl);
Chris Lattner13901342010-07-11 22:42:07 +0000439 }
440
441 if (isDeclarationSpecifier()) {
442 // If there is an invalid declaration specifier right after the function
443 // prototype, then we must be in a missing semicolon case where this isn't
444 // actually a body. Just fall through into the code that handles it as a
445 // prototype, and let the top-level code handle the erroneous declspec
446 // where it would otherwise expect a comma or semicolon.
John McCalld5a36322009-11-03 19:26:08 +0000447 } else {
448 Diag(Tok, diag::err_expected_fn_body);
449 SkipUntil(tok::semi);
450 return DeclGroupPtrTy();
451 }
452 }
453
John McCall48871652010-08-21 09:40:31 +0000454 llvm::SmallVector<Decl *, 8> DeclsInGroup;
455 Decl *FirstDecl = ParseDeclarationAfterDeclarator(D);
John McCall28a6aea2009-11-04 02:18:39 +0000456 D.complete(FirstDecl);
John McCall48871652010-08-21 09:40:31 +0000457 if (FirstDecl)
John McCalld5a36322009-11-03 19:26:08 +0000458 DeclsInGroup.push_back(FirstDecl);
459
460 // If we don't have a comma, it is either the end of the list (a ';') or an
461 // error, bail out.
462 while (Tok.is(tok::comma)) {
463 // Consume the comma.
Chris Lattnerefb0f112009-03-29 17:18:04 +0000464 ConsumeToken();
John McCalld5a36322009-11-03 19:26:08 +0000465
466 // Parse the next declarator.
467 D.clear();
468
469 // Accept attributes in an init-declarator. In the first declarator in a
470 // declaration, these would be part of the declspec. In subsequent
471 // declarators, they become part of the declarator itself, so that they
472 // don't apply to declarators after *this* one. Examples:
473 // short __attribute__((common)) var; -> declspec
474 // short var __attribute__((common)); -> declarator
475 // short x, __attribute__((common)) var; -> declarator
John McCall53fa7142010-12-24 02:08:15 +0000476 MaybeParseGNUAttributes(D);
John McCalld5a36322009-11-03 19:26:08 +0000477
478 ParseDeclarator(D);
479
John McCall48871652010-08-21 09:40:31 +0000480 Decl *ThisDecl = ParseDeclarationAfterDeclarator(D);
John McCall28a6aea2009-11-04 02:18:39 +0000481 D.complete(ThisDecl);
John McCall48871652010-08-21 09:40:31 +0000482 if (ThisDecl)
John McCalld5a36322009-11-03 19:26:08 +0000483 DeclsInGroup.push_back(ThisDecl);
484 }
485
486 if (DeclEnd)
487 *DeclEnd = Tok.getLocation();
488
489 if (Context != Declarator::ForContext &&
490 ExpectAndConsume(tok::semi,
491 Context == Declarator::FileContext
492 ? diag::err_invalid_token_after_toplevel_declarator
493 : diag::err_expected_semi_declaration)) {
Chris Lattner13901342010-07-11 22:42:07 +0000494 // Okay, there was no semicolon and one was expected. If we see a
495 // declaration specifier, just assume it was missing and continue parsing.
496 // Otherwise things are very confused and we skip to recover.
497 if (!isDeclarationSpecifier()) {
498 SkipUntil(tok::r_brace, true, true);
499 if (Tok.is(tok::semi))
500 ConsumeToken();
501 }
John McCalld5a36322009-11-03 19:26:08 +0000502 }
503
Douglas Gregor0be31a22010-07-02 17:43:08 +0000504 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS,
John McCalld5a36322009-11-03 19:26:08 +0000505 DeclsInGroup.data(),
506 DeclsInGroup.size());
Chris Lattner53361ac2006-08-10 05:19:57 +0000507}
508
Douglas Gregor23996282009-05-12 21:31:51 +0000509/// \brief Parse 'declaration' after parsing 'declaration-specifiers
510/// declarator'. This method parses the remainder of the declaration
511/// (including any attributes or initializer, among other things) and
512/// finalizes the declaration.
Chris Lattnerf0f3baa2006-08-14 00:15:20 +0000513///
Chris Lattnerf0f3baa2006-08-14 00:15:20 +0000514/// init-declarator: [C99 6.7]
515/// declarator
516/// declarator '=' initializer
Chris Lattner6d7e6342006-08-15 03:41:14 +0000517/// [GNU] declarator simple-asm-expr[opt] attributes[opt]
518/// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +0000519/// [C++] declarator initializer[opt]
520///
521/// [C++] initializer:
522/// [C++] '=' initializer-clause
523/// [C++] '(' expression-list ')'
Sebastian Redlf769df52009-03-24 22:27:57 +0000524/// [C++0x] '=' 'default' [TODO]
525/// [C++0x] '=' 'delete'
526///
527/// According to the standard grammar, =default and =delete are function
528/// definitions, but that definitely doesn't fit with the parser here.
Chris Lattnerf0f3baa2006-08-14 00:15:20 +0000529///
John McCall48871652010-08-21 09:40:31 +0000530Decl *Parser::ParseDeclarationAfterDeclarator(Declarator &D,
Douglas Gregorb52fabb2009-06-23 23:11:28 +0000531 const ParsedTemplateInfo &TemplateInfo) {
Douglas Gregor23996282009-05-12 21:31:51 +0000532 // If a simple-asm-expr is present, parse it.
533 if (Tok.is(tok::kw_asm)) {
534 SourceLocation Loc;
John McCalldadc5752010-08-24 06:29:42 +0000535 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
Douglas Gregor23996282009-05-12 21:31:51 +0000536 if (AsmLabel.isInvalid()) {
537 SkipUntil(tok::semi, true, true);
John McCall48871652010-08-21 09:40:31 +0000538 return 0;
Douglas Gregor23996282009-05-12 21:31:51 +0000539 }
Mike Stump11289f42009-09-09 15:08:12 +0000540
Douglas Gregor23996282009-05-12 21:31:51 +0000541 D.setAsmLabel(AsmLabel.release());
542 D.SetRangeEnd(Loc);
543 }
Mike Stump11289f42009-09-09 15:08:12 +0000544
John McCall53fa7142010-12-24 02:08:15 +0000545 MaybeParseGNUAttributes(D);
Mike Stump11289f42009-09-09 15:08:12 +0000546
Douglas Gregor23996282009-05-12 21:31:51 +0000547 // Inform the current actions module that we just parsed this declarator.
John McCall48871652010-08-21 09:40:31 +0000548 Decl *ThisDecl = 0;
Douglas Gregor450f00842009-09-25 18:43:00 +0000549 switch (TemplateInfo.Kind) {
550 case ParsedTemplateInfo::NonTemplate:
Douglas Gregor0be31a22010-07-02 17:43:08 +0000551 ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
Douglas Gregor450f00842009-09-25 18:43:00 +0000552 break;
553
554 case ParsedTemplateInfo::Template:
555 case ParsedTemplateInfo::ExplicitSpecialization:
Douglas Gregor0be31a22010-07-02 17:43:08 +0000556 ThisDecl = Actions.ActOnTemplateDeclarator(getCurScope(),
John McCallfaf5fb42010-08-26 23:41:50 +0000557 MultiTemplateParamsArg(Actions,
Douglas Gregorb52fabb2009-06-23 23:11:28 +0000558 TemplateInfo.TemplateParams->data(),
559 TemplateInfo.TemplateParams->size()),
Douglas Gregor450f00842009-09-25 18:43:00 +0000560 D);
561 break;
562
563 case ParsedTemplateInfo::ExplicitInstantiation: {
John McCall48871652010-08-21 09:40:31 +0000564 DeclResult ThisRes
Douglas Gregor0be31a22010-07-02 17:43:08 +0000565 = Actions.ActOnExplicitInstantiation(getCurScope(),
Douglas Gregor450f00842009-09-25 18:43:00 +0000566 TemplateInfo.ExternLoc,
567 TemplateInfo.TemplateLoc,
568 D);
569 if (ThisRes.isInvalid()) {
570 SkipUntil(tok::semi, true, true);
John McCall48871652010-08-21 09:40:31 +0000571 return 0;
Douglas Gregor450f00842009-09-25 18:43:00 +0000572 }
573
574 ThisDecl = ThisRes.get();
575 break;
576 }
577 }
Mike Stump11289f42009-09-09 15:08:12 +0000578
Douglas Gregor23996282009-05-12 21:31:51 +0000579 // Parse declarator '=' initializer.
Argyrios Kyrtzidisb5c7c512010-10-08 02:39:23 +0000580 if (isTokenEqualOrMistypedEqualEqual(
581 diag::err_invalid_equalequal_after_declarator)) {
Douglas Gregor23996282009-05-12 21:31:51 +0000582 ConsumeToken();
Anders Carlsson991285e2010-09-24 21:25:25 +0000583 if (Tok.is(tok::kw_delete)) {
Douglas Gregor23996282009-05-12 21:31:51 +0000584 SourceLocation DelLoc = ConsumeToken();
Anders Carlsson991285e2010-09-24 21:25:25 +0000585
586 if (!getLang().CPlusPlus0x)
587 Diag(DelLoc, diag::warn_deleted_function_accepted_as_extension);
588
Douglas Gregor23996282009-05-12 21:31:51 +0000589 Actions.SetDeclDeleted(ThisDecl, DelLoc);
590 } else {
John McCall1f4ee7b2009-12-19 09:28:58 +0000591 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
592 EnterScope(0);
Douglas Gregor0be31a22010-07-02 17:43:08 +0000593 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
John McCall1f4ee7b2009-12-19 09:28:58 +0000594 }
Argyrios Kyrtzidis3df19782009-06-17 22:50:06 +0000595
Douglas Gregor7aa6b222010-05-30 01:49:25 +0000596 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000597 Actions.CodeCompleteInitializer(getCurScope(), ThisDecl);
Douglas Gregor7aa6b222010-05-30 01:49:25 +0000598 ConsumeCodeCompletionToken();
599 SkipUntil(tok::comma, true, true);
600 return ThisDecl;
601 }
602
John McCalldadc5752010-08-24 06:29:42 +0000603 ExprResult Init(ParseInitializer());
Argyrios Kyrtzidis3df19782009-06-17 22:50:06 +0000604
John McCall1f4ee7b2009-12-19 09:28:58 +0000605 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000606 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
John McCall1f4ee7b2009-12-19 09:28:58 +0000607 ExitScope();
608 }
Argyrios Kyrtzidis3df19782009-06-17 22:50:06 +0000609
Douglas Gregor23996282009-05-12 21:31:51 +0000610 if (Init.isInvalid()) {
Douglas Gregor604c3022010-03-01 18:27:54 +0000611 SkipUntil(tok::comma, true, true);
612 Actions.ActOnInitializerError(ThisDecl);
613 } else
John McCallb268a282010-08-23 23:25:46 +0000614 Actions.AddInitializerToDecl(ThisDecl, Init.take());
Douglas Gregor23996282009-05-12 21:31:51 +0000615 }
616 } else if (Tok.is(tok::l_paren)) {
617 // Parse C++ direct initializer: '(' expression-list ')'
618 SourceLocation LParenLoc = ConsumeParen();
619 ExprVector Exprs(Actions);
620 CommaLocsTy CommaLocs;
621
Douglas Gregor613bf102009-12-22 17:47:17 +0000622 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
623 EnterScope(0);
Douglas Gregor0be31a22010-07-02 17:43:08 +0000624 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregor613bf102009-12-22 17:47:17 +0000625 }
626
Douglas Gregor23996282009-05-12 21:31:51 +0000627 if (ParseExpressionList(Exprs, CommaLocs)) {
628 SkipUntil(tok::r_paren);
Douglas Gregor613bf102009-12-22 17:47:17 +0000629
630 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000631 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregor613bf102009-12-22 17:47:17 +0000632 ExitScope();
633 }
Douglas Gregor23996282009-05-12 21:31:51 +0000634 } else {
635 // Match the ')'.
636 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
637
638 assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() &&
639 "Unexpected number of commas!");
Douglas Gregor613bf102009-12-22 17:47:17 +0000640
641 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000642 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregor613bf102009-12-22 17:47:17 +0000643 ExitScope();
644 }
645
Douglas Gregor23996282009-05-12 21:31:51 +0000646 Actions.AddCXXDirectInitializerToDecl(ThisDecl, LParenLoc,
647 move_arg(Exprs),
Douglas Gregorce5aa332010-09-09 16:33:13 +0000648 RParenLoc);
Douglas Gregor23996282009-05-12 21:31:51 +0000649 }
650 } else {
Mike Stump11289f42009-09-09 15:08:12 +0000651 bool TypeContainsUndeducedAuto =
Anders Carlssonae019932009-07-11 00:34:39 +0000652 D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto;
653 Actions.ActOnUninitializedDecl(ThisDecl, TypeContainsUndeducedAuto);
Douglas Gregor23996282009-05-12 21:31:51 +0000654 }
655
656 return ThisDecl;
657}
658
Chris Lattner1890ac82006-08-13 01:16:23 +0000659/// ParseSpecifierQualifierList
660/// specifier-qualifier-list:
661/// type-specifier specifier-qualifier-list[opt]
662/// type-qualifier specifier-qualifier-list[opt]
Chris Lattnere37e2332006-08-15 04:50:22 +0000663/// [GNU] attributes specifier-qualifier-list[opt]
Chris Lattner1890ac82006-08-13 01:16:23 +0000664///
665void Parser::ParseSpecifierQualifierList(DeclSpec &DS) {
666 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
667 /// parse declaration-specifiers and complain about extra stuff.
Chris Lattner1890ac82006-08-13 01:16:23 +0000668 ParseDeclarationSpecifiers(DS);
Mike Stump11289f42009-09-09 15:08:12 +0000669
Chris Lattner1890ac82006-08-13 01:16:23 +0000670 // Validate declspec for type-name.
671 unsigned Specs = DS.getParsedSpecifiers();
Chris Lattnera723ba92009-04-14 21:16:09 +0000672 if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers() &&
John McCall53fa7142010-12-24 02:08:15 +0000673 !DS.hasAttributes())
Chris Lattner1890ac82006-08-13 01:16:23 +0000674 Diag(Tok, diag::err_typename_requires_specqual);
Mike Stump11289f42009-09-09 15:08:12 +0000675
Chris Lattner1b22eed2006-11-28 05:12:07 +0000676 // Issue diagnostic and remove storage class if present.
Chris Lattner1890ac82006-08-13 01:16:23 +0000677 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
Chris Lattner1b22eed2006-11-28 05:12:07 +0000678 if (DS.getStorageClassSpecLoc().isValid())
679 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
680 else
681 Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass);
Chris Lattnera925dc62006-11-28 04:33:46 +0000682 DS.ClearStorageClassSpecs();
Chris Lattner1890ac82006-08-13 01:16:23 +0000683 }
Mike Stump11289f42009-09-09 15:08:12 +0000684
Chris Lattner1b22eed2006-11-28 05:12:07 +0000685 // Issue diagnostic and remove function specfier if present.
Chris Lattner1890ac82006-08-13 01:16:23 +0000686 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Douglas Gregor61956c42008-10-31 09:07:45 +0000687 if (DS.isInlineSpecified())
688 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
689 if (DS.isVirtualSpecified())
690 Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec);
691 if (DS.isExplicitSpecified())
692 Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec);
Chris Lattnera925dc62006-11-28 04:33:46 +0000693 DS.ClearFunctionSpecs();
Chris Lattner1890ac82006-08-13 01:16:23 +0000694 }
695}
Chris Lattner53361ac2006-08-10 05:19:57 +0000696
Chris Lattner6cc055a2009-04-12 20:42:31 +0000697/// isValidAfterIdentifierInDeclaratorAfterDeclSpec - Return true if the
698/// specified token is valid after the identifier in a declarator which
699/// immediately follows the declspec. For example, these things are valid:
700///
701/// int x [ 4]; // direct-declarator
702/// int x ( int y); // direct-declarator
703/// int(int x ) // direct-declarator
704/// int x ; // simple-declaration
705/// int x = 17; // init-declarator-list
706/// int x , y; // init-declarator-list
707/// int x __asm__ ("foo"); // init-declarator-list
Chris Lattnera723ba92009-04-14 21:16:09 +0000708/// int x : 4; // struct-declarator
Chris Lattner2b988c12009-04-12 22:29:43 +0000709/// int x { 5}; // C++'0x unified initializers
Chris Lattner6cc055a2009-04-12 20:42:31 +0000710///
711/// This is not, because 'x' does not immediately follow the declspec (though
712/// ')' happens to be valid anyway).
713/// int (x)
714///
715static bool isValidAfterIdentifierInDeclarator(const Token &T) {
716 return T.is(tok::l_square) || T.is(tok::l_paren) || T.is(tok::r_paren) ||
717 T.is(tok::semi) || T.is(tok::comma) || T.is(tok::equal) ||
Chris Lattnera723ba92009-04-14 21:16:09 +0000718 T.is(tok::kw_asm) || T.is(tok::l_brace) || T.is(tok::colon);
Chris Lattner6cc055a2009-04-12 20:42:31 +0000719}
720
Chris Lattner20a0c612009-04-14 21:34:55 +0000721
722/// ParseImplicitInt - This method is called when we have an non-typename
723/// identifier in a declspec (which normally terminates the decl spec) when
724/// the declspec has no type specifier. In this case, the declspec is either
725/// malformed or is "implicit int" (in K&R and C89).
726///
727/// This method handles diagnosing this prettily and returns false if the
728/// declspec is done being processed. If it recovers and thinks there may be
729/// other pieces of declspec after it, it returns true.
730///
Chris Lattnerb4a8fe82009-04-14 22:17:06 +0000731bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS,
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000732 const ParsedTemplateInfo &TemplateInfo,
Chris Lattner20a0c612009-04-14 21:34:55 +0000733 AccessSpecifier AS) {
Chris Lattnerb4a8fe82009-04-14 22:17:06 +0000734 assert(Tok.is(tok::identifier) && "should have identifier");
Mike Stump11289f42009-09-09 15:08:12 +0000735
Chris Lattner20a0c612009-04-14 21:34:55 +0000736 SourceLocation Loc = Tok.getLocation();
737 // If we see an identifier that is not a type name, we normally would
738 // parse it as the identifer being declared. However, when a typename
739 // is typo'd or the definition is not included, this will incorrectly
740 // parse the typename as the identifier name and fall over misparsing
741 // later parts of the diagnostic.
742 //
743 // As such, we try to do some look-ahead in cases where this would
744 // otherwise be an "implicit-int" case to see if this is invalid. For
745 // example: "static foo_t x = 4;" In this case, if we parsed foo_t as
746 // an identifier with implicit int, we'd get a parse error because the
747 // next token is obviously invalid for a type. Parse these as a case
748 // with an invalid type specifier.
749 assert(!DS.hasTypeSpecifier() && "Type specifier checked above");
Mike Stump11289f42009-09-09 15:08:12 +0000750
Chris Lattner20a0c612009-04-14 21:34:55 +0000751 // Since we know that this either implicit int (which is rare) or an
752 // error, we'd do lookahead to try to do better recovery.
753 if (isValidAfterIdentifierInDeclarator(NextToken())) {
754 // If this token is valid for implicit int, e.g. "static x = 4", then
755 // we just avoid eating the identifier, so it will be parsed as the
756 // identifier in the declarator.
757 return false;
758 }
Mike Stump11289f42009-09-09 15:08:12 +0000759
Chris Lattner20a0c612009-04-14 21:34:55 +0000760 // Otherwise, if we don't consume this token, we are going to emit an
761 // error anyway. Try to recover from various common problems. Check
762 // to see if this was a reference to a tag name without a tag specified.
763 // This is a common problem in C (saying 'foo' instead of 'struct foo').
Chris Lattnerb4a8fe82009-04-14 22:17:06 +0000764 //
765 // C++ doesn't need this, and isTagName doesn't take SS.
766 if (SS == 0) {
767 const char *TagName = 0;
768 tok::TokenKind TagKind = tok::unknown;
Mike Stump11289f42009-09-09 15:08:12 +0000769
Douglas Gregor0be31a22010-07-02 17:43:08 +0000770 switch (Actions.isTagName(*Tok.getIdentifierInfo(), getCurScope())) {
Chris Lattner20a0c612009-04-14 21:34:55 +0000771 default: break;
772 case DeclSpec::TST_enum: TagName="enum" ;TagKind=tok::kw_enum ;break;
773 case DeclSpec::TST_union: TagName="union" ;TagKind=tok::kw_union ;break;
774 case DeclSpec::TST_struct:TagName="struct";TagKind=tok::kw_struct;break;
775 case DeclSpec::TST_class: TagName="class" ;TagKind=tok::kw_class ;break;
776 }
Mike Stump11289f42009-09-09 15:08:12 +0000777
Chris Lattnerb4a8fe82009-04-14 22:17:06 +0000778 if (TagName) {
779 Diag(Loc, diag::err_use_of_tag_name_without_tag)
John McCall38200b02010-02-14 01:03:10 +0000780 << Tok.getIdentifierInfo() << TagName << getLang().CPlusPlus
Douglas Gregora771f462010-03-31 17:46:05 +0000781 << FixItHint::CreateInsertion(Tok.getLocation(),TagName);
Mike Stump11289f42009-09-09 15:08:12 +0000782
Chris Lattnerb4a8fe82009-04-14 22:17:06 +0000783 // Parse this as a tag as if the missing tag were present.
784 if (TagKind == tok::kw_enum)
Douglas Gregordc70c3a2010-03-02 17:53:14 +0000785 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS);
Chris Lattnerb4a8fe82009-04-14 22:17:06 +0000786 else
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000787 ParseClassSpecifier(TagKind, Loc, DS, TemplateInfo, AS);
Chris Lattnerb4a8fe82009-04-14 22:17:06 +0000788 return true;
789 }
Chris Lattner20a0c612009-04-14 21:34:55 +0000790 }
Mike Stump11289f42009-09-09 15:08:12 +0000791
Douglas Gregor15e56022009-10-13 23:27:22 +0000792 // This is almost certainly an invalid type name. Let the action emit a
793 // diagnostic and attempt to recover.
John McCallba7bf592010-08-24 05:47:05 +0000794 ParsedType T;
Douglas Gregor15e56022009-10-13 23:27:22 +0000795 if (Actions.DiagnoseUnknownTypeName(*Tok.getIdentifierInfo(), Loc,
Douglas Gregor0be31a22010-07-02 17:43:08 +0000796 getCurScope(), SS, T)) {
Douglas Gregor15e56022009-10-13 23:27:22 +0000797 // The action emitted a diagnostic, so we don't have to.
798 if (T) {
799 // The action has suggested that the type T could be used. Set that as
800 // the type in the declaration specifiers, consume the would-be type
801 // name token, and we're done.
802 const char *PrevSpec;
803 unsigned DiagID;
John McCallba7bf592010-08-24 05:47:05 +0000804 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID, T);
Douglas Gregor15e56022009-10-13 23:27:22 +0000805 DS.SetRangeEnd(Tok.getLocation());
806 ConsumeToken();
807
808 // There may be other declaration specifiers after this.
809 return true;
810 }
811
812 // Fall through; the action had no suggestion for us.
813 } else {
814 // The action did not emit a diagnostic, so emit one now.
815 SourceRange R;
816 if (SS) R = SS->getRange();
817 Diag(Loc, diag::err_unknown_typename) << Tok.getIdentifierInfo() << R;
818 }
Mike Stump11289f42009-09-09 15:08:12 +0000819
Douglas Gregor15e56022009-10-13 23:27:22 +0000820 // Mark this as an error.
Chris Lattner20a0c612009-04-14 21:34:55 +0000821 const char *PrevSpec;
John McCall49bfce42009-08-03 20:12:06 +0000822 unsigned DiagID;
823 DS.SetTypeSpecType(DeclSpec::TST_error, Loc, PrevSpec, DiagID);
Chris Lattner20a0c612009-04-14 21:34:55 +0000824 DS.SetRangeEnd(Tok.getLocation());
825 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000826
Chris Lattner20a0c612009-04-14 21:34:55 +0000827 // TODO: Could inject an invalid typedef decl in an enclosing scope to
828 // avoid rippling error messages on subsequent uses of the same type,
829 // could be useful if #include was forgotten.
830 return false;
831}
832
Douglas Gregor9de54ea2010-01-13 17:31:36 +0000833/// \brief Determine the declaration specifier context from the declarator
834/// context.
835///
836/// \param Context the declarator context, which is one of the
837/// Declarator::TheContext enumerator values.
838Parser::DeclSpecContext
839Parser::getDeclSpecContextFromDeclaratorContext(unsigned Context) {
840 if (Context == Declarator::MemberContext)
841 return DSC_class;
842 if (Context == Declarator::FileContext)
843 return DSC_top_level;
844 return DSC_normal;
845}
846
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000847/// ParseDeclarationSpecifiers
848/// declaration-specifiers: [C99 6.7]
Chris Lattner3b561a32006-08-13 00:12:11 +0000849/// storage-class-specifier declaration-specifiers[opt]
850/// type-specifier declaration-specifiers[opt]
Chris Lattner3b561a32006-08-13 00:12:11 +0000851/// [C99] function-specifier declaration-specifiers[opt]
Chris Lattnere37e2332006-08-15 04:50:22 +0000852/// [GNU] attributes declaration-specifiers[opt]
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000853///
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000854/// storage-class-specifier: [C99 6.7.1]
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000855/// 'typedef'
856/// 'extern'
857/// 'static'
858/// 'auto'
859/// 'register'
Sebastian Redlccdfaba2008-11-14 23:42:31 +0000860/// [C++] 'mutable'
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000861/// [GNU] '__thread'
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000862/// function-specifier: [C99 6.7.4]
Chris Lattner3b561a32006-08-13 00:12:11 +0000863/// [C99] 'inline'
Douglas Gregor61956c42008-10-31 09:07:45 +0000864/// [C++] 'virtual'
865/// [C++] 'explicit'
Anders Carlssoncd8db412009-05-06 04:46:28 +0000866/// 'friend': [C++ dcl.friend]
Sebastian Redl39c2a8b2009-11-05 15:47:02 +0000867/// 'constexpr': [C++0x dcl.constexpr]
Anders Carlssoncd8db412009-05-06 04:46:28 +0000868
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000869///
Douglas Gregorb9bd8a92008-12-24 02:52:09 +0000870void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000871 const ParsedTemplateInfo &TemplateInfo,
John McCall07e91c02009-08-06 02:15:43 +0000872 AccessSpecifier AS,
Douglas Gregorc49f5b22010-08-23 18:23:48 +0000873 DeclSpecContext DSContext) {
Chris Lattner2e232092008-03-13 06:29:04 +0000874 DS.SetRangeStart(Tok.getLocation());
Chris Lattner07865442010-11-09 20:14:26 +0000875 DS.SetRangeEnd(Tok.getLocation());
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000876 while (1) {
John McCall49bfce42009-08-03 20:12:06 +0000877 bool isInvalid = false;
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000878 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +0000879 unsigned DiagID = 0;
880
Chris Lattner4d8f8732006-11-28 05:05:08 +0000881 SourceLocation Loc = Tok.getLocation();
Douglas Gregor450c75a2008-11-07 15:42:26 +0000882
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000883 switch (Tok.getKind()) {
Mike Stump11289f42009-09-09 15:08:12 +0000884 default:
Chris Lattner0974b232008-07-26 00:20:22 +0000885 DoneWithDeclSpec:
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000886 // If this is not a declaration specifier token, we're done reading decl
887 // specifiers. First verify that DeclSpec's are consistent.
Douglas Gregore3e01a22009-04-01 22:41:11 +0000888 DS.Finish(Diags, PP);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000889 return;
Mike Stump11289f42009-09-09 15:08:12 +0000890
Douglas Gregorc49f5b22010-08-23 18:23:48 +0000891 case tok::code_completion: {
John McCallfaf5fb42010-08-26 23:41:50 +0000892 Sema::ParserCompletionContext CCC = Sema::PCC_Namespace;
Douglas Gregorc49f5b22010-08-23 18:23:48 +0000893 if (DS.hasTypeSpecifier()) {
894 bool AllowNonIdentifiers
895 = (getCurScope()->getFlags() & (Scope::ControlScope |
896 Scope::BlockScope |
897 Scope::TemplateParamScope |
898 Scope::FunctionPrototypeScope |
899 Scope::AtCatchScope)) == 0;
900 bool AllowNestedNameSpecifiers
901 = DSContext == DSC_top_level ||
902 (DSContext == DSC_class && DS.isFriendSpecified());
903
Douglas Gregorbfcea8b2010-09-16 15:14:18 +0000904 Actions.CodeCompleteDeclSpec(getCurScope(), DS,
905 AllowNonIdentifiers,
906 AllowNestedNameSpecifiers);
Douglas Gregorc49f5b22010-08-23 18:23:48 +0000907 ConsumeCodeCompletionToken();
908 return;
909 }
910
911 if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate)
John McCallfaf5fb42010-08-26 23:41:50 +0000912 CCC = DSContext == DSC_class? Sema::PCC_MemberTemplate
913 : Sema::PCC_Template;
Douglas Gregorc49f5b22010-08-23 18:23:48 +0000914 else if (DSContext == DSC_class)
John McCallfaf5fb42010-08-26 23:41:50 +0000915 CCC = Sema::PCC_Class;
Douglas Gregorc49f5b22010-08-23 18:23:48 +0000916 else if (ObjCImpDecl)
John McCallfaf5fb42010-08-26 23:41:50 +0000917 CCC = Sema::PCC_ObjCImplementation;
Douglas Gregorc49f5b22010-08-23 18:23:48 +0000918
919 Actions.CodeCompleteOrdinaryName(getCurScope(), CCC);
920 ConsumeCodeCompletionToken();
921 return;
922 }
923
Chris Lattnerbd31aa32009-01-05 00:07:25 +0000924 case tok::coloncolon: // ::foo::bar
John McCall1f476a12010-02-26 08:45:28 +0000925 // C++ scope specifier. Annotate and loop, or bail out on error.
926 if (TryAnnotateCXXScopeToken(true)) {
927 if (!DS.hasTypeSpecifier())
928 DS.SetTypeSpecError();
929 goto DoneWithDeclSpec;
930 }
John McCall8bc2a702010-03-01 18:20:46 +0000931 if (Tok.is(tok::coloncolon)) // ::new or ::delete
932 goto DoneWithDeclSpec;
John McCall1f476a12010-02-26 08:45:28 +0000933 continue;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000934
935 case tok::annot_cxxscope: {
936 if (DS.hasTypeSpecifier())
937 goto DoneWithDeclSpec;
938
John McCall9dab4e62009-12-12 11:40:51 +0000939 CXXScopeSpec SS;
John McCall37ad5512010-08-23 06:44:23 +0000940 SS.setScopeRep((NestedNameSpecifier*) Tok.getAnnotationValue());
John McCall9dab4e62009-12-12 11:40:51 +0000941 SS.setRange(Tok.getAnnotationRange());
942
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000943 // We are looking for a qualified typename.
Douglas Gregor167fa622009-03-25 15:40:00 +0000944 Token Next = NextToken();
Mike Stump11289f42009-09-09 15:08:12 +0000945 if (Next.is(tok::annot_template_id) &&
Douglas Gregor167fa622009-03-25 15:40:00 +0000946 static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue())
Douglas Gregorb67535d2009-03-31 00:43:58 +0000947 ->Kind == TNK_Type_template) {
Douglas Gregor167fa622009-03-25 15:40:00 +0000948 // We have a qualified template-id, e.g., N::A<int>
Douglas Gregor9de54ea2010-01-13 17:31:36 +0000949
950 // C++ [class.qual]p2:
951 // In a lookup in which the constructor is an acceptable lookup
952 // result and the nested-name-specifier nominates a class C:
953 //
954 // - if the name specified after the
955 // nested-name-specifier, when looked up in C, is the
956 // injected-class-name of C (Clause 9), or
957 //
958 // - if the name specified after the nested-name-specifier
959 // is the same as the identifier or the
960 // simple-template-id's template-name in the last
961 // component of the nested-name-specifier,
962 //
963 // the name is instead considered to name the constructor of
964 // class C.
965 //
966 // Thus, if the template-name is actually the constructor
967 // name, then the code is ill-formed; this interpretation is
968 // reinforced by the NAD status of core issue 635.
969 TemplateIdAnnotation *TemplateId
970 = static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue());
John McCall84821e72010-04-13 06:39:49 +0000971 if ((DSContext == DSC_top_level ||
972 (DSContext == DSC_class && DS.isFriendSpecified())) &&
973 TemplateId->Name &&
Douglas Gregor0be31a22010-07-02 17:43:08 +0000974 Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) {
Douglas Gregor9de54ea2010-01-13 17:31:36 +0000975 if (isConstructorDeclarator()) {
976 // The user meant this to be an out-of-line constructor
977 // definition, but template arguments are not allowed
978 // there. Just allow this as a constructor; we'll
979 // complain about it later.
980 goto DoneWithDeclSpec;
981 }
982
983 // The user meant this to name a type, but it actually names
984 // a constructor with some extraneous template
985 // arguments. Complain, then parse it as a type as the user
986 // intended.
987 Diag(TemplateId->TemplateNameLoc,
988 diag::err_out_of_line_template_id_names_constructor)
989 << TemplateId->Name;
990 }
991
John McCall9dab4e62009-12-12 11:40:51 +0000992 DS.getTypeSpecScope() = SS;
993 ConsumeToken(); // The C++ scope.
Mike Stump11289f42009-09-09 15:08:12 +0000994 assert(Tok.is(tok::annot_template_id) &&
Douglas Gregor167fa622009-03-25 15:40:00 +0000995 "ParseOptionalCXXScopeSpecifier not working");
996 AnnotateTemplateIdTokenAsType(&SS);
997 continue;
998 }
999
Douglas Gregorc5790df2009-09-28 07:26:33 +00001000 if (Next.is(tok::annot_typename)) {
John McCall9dab4e62009-12-12 11:40:51 +00001001 DS.getTypeSpecScope() = SS;
1002 ConsumeToken(); // The C++ scope.
John McCallba7bf592010-08-24 05:47:05 +00001003 if (Tok.getAnnotationValue()) {
1004 ParsedType T = getTypeAnnotation(Tok);
Nico Weber77430342010-11-22 10:30:56 +00001005 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename,
1006 Tok.getAnnotationEndLoc(),
John McCallba7bf592010-08-24 05:47:05 +00001007 PrevSpec, DiagID, T);
1008 }
Douglas Gregorc5790df2009-09-28 07:26:33 +00001009 else
1010 DS.SetTypeSpecError();
1011 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1012 ConsumeToken(); // The typename
1013 }
1014
Douglas Gregor167fa622009-03-25 15:40:00 +00001015 if (Next.isNot(tok::identifier))
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001016 goto DoneWithDeclSpec;
1017
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001018 // If we're in a context where the identifier could be a class name,
1019 // check whether this is a constructor declaration.
John McCall84821e72010-04-13 06:39:49 +00001020 if ((DSContext == DSC_top_level ||
1021 (DSContext == DSC_class && DS.isFriendSpecified())) &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00001022 Actions.isCurrentClassName(*Next.getIdentifierInfo(), getCurScope(),
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001023 &SS)) {
1024 if (isConstructorDeclarator())
1025 goto DoneWithDeclSpec;
1026
1027 // As noted in C++ [class.qual]p2 (cited above), when the name
1028 // of the class is qualified in a context where it could name
1029 // a constructor, its a constructor name. However, we've
1030 // looked at the declarator, and the user probably meant this
1031 // to be a type. Complain that it isn't supposed to be treated
1032 // as a type, then proceed to parse it as a type.
1033 Diag(Next.getLocation(), diag::err_out_of_line_type_names_constructor)
1034 << Next.getIdentifierInfo();
1035 }
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001036
John McCallba7bf592010-08-24 05:47:05 +00001037 ParsedType TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(),
1038 Next.getLocation(),
1039 getCurScope(), &SS);
Douglas Gregor8bf42052009-02-09 18:46:07 +00001040
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001041 // If the referenced identifier is not a type, then this declspec is
1042 // erroneous: We already checked about that it has no type specifier, and
1043 // C++ doesn't have implicit int. Diagnose it as a typo w.r.t. to the
Mike Stump11289f42009-09-09 15:08:12 +00001044 // typename.
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001045 if (TypeRep == 0) {
1046 ConsumeToken(); // Eat the scope spec so the identifier is current.
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001047 if (ParseImplicitInt(DS, &SS, TemplateInfo, AS)) continue;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001048 goto DoneWithDeclSpec;
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001049 }
Mike Stump11289f42009-09-09 15:08:12 +00001050
John McCall9dab4e62009-12-12 11:40:51 +00001051 DS.getTypeSpecScope() = SS;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001052 ConsumeToken(); // The C++ scope.
1053
Douglas Gregor9817f4a2009-02-09 15:09:02 +00001054 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00001055 DiagID, TypeRep);
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001056 if (isInvalid)
1057 break;
Mike Stump11289f42009-09-09 15:08:12 +00001058
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001059 DS.SetRangeEnd(Tok.getLocation());
1060 ConsumeToken(); // The typename.
1061
1062 continue;
1063 }
Mike Stump11289f42009-09-09 15:08:12 +00001064
Chris Lattnere387d9e2009-01-21 19:48:37 +00001065 case tok::annot_typename: {
John McCallba7bf592010-08-24 05:47:05 +00001066 if (Tok.getAnnotationValue()) {
1067 ParsedType T = getTypeAnnotation(Tok);
Nico Weber7f8bb362010-11-22 12:50:03 +00001068 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallba7bf592010-08-24 05:47:05 +00001069 DiagID, T);
1070 } else
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00001071 DS.SetTypeSpecError();
Chris Lattner005fc1b2010-04-05 18:18:31 +00001072
1073 if (isInvalid)
1074 break;
1075
Chris Lattnere387d9e2009-01-21 19:48:37 +00001076 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1077 ConsumeToken(); // The typename
Mike Stump11289f42009-09-09 15:08:12 +00001078
Chris Lattnere387d9e2009-01-21 19:48:37 +00001079 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1080 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001081 // Objective-C interface.
1082 if (Tok.is(tok::less) && getLang().ObjC1)
1083 ParseObjCProtocolQualifiers(DS);
1084
Chris Lattnere387d9e2009-01-21 19:48:37 +00001085 continue;
1086 }
Mike Stump11289f42009-09-09 15:08:12 +00001087
Chris Lattner16fac4f2008-07-26 01:18:38 +00001088 // typedef-name
1089 case tok::identifier: {
Chris Lattnerbd31aa32009-01-05 00:07:25 +00001090 // In C++, check to see if this is a scope specifier like foo::bar::, if
1091 // so handle it as such. This is important for ctor parsing.
John McCall1f476a12010-02-26 08:45:28 +00001092 if (getLang().CPlusPlus) {
1093 if (TryAnnotateCXXScopeToken(true)) {
1094 if (!DS.hasTypeSpecifier())
1095 DS.SetTypeSpecError();
1096 goto DoneWithDeclSpec;
1097 }
1098 if (!Tok.is(tok::identifier))
1099 continue;
1100 }
Mike Stump11289f42009-09-09 15:08:12 +00001101
Chris Lattner16fac4f2008-07-26 01:18:38 +00001102 // This identifier can only be a typedef name if we haven't already seen
1103 // a type-specifier. Without this check we misparse:
1104 // typedef int X; struct Y { short X; }; as 'short int'.
1105 if (DS.hasTypeSpecifier())
1106 goto DoneWithDeclSpec;
Mike Stump11289f42009-09-09 15:08:12 +00001107
John Thompson22334602010-02-05 00:12:22 +00001108 // Check for need to substitute AltiVec keyword tokens.
1109 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
1110 break;
1111
Chris Lattner16fac4f2008-07-26 01:18:38 +00001112 // It has to be available as a typedef too!
John McCallba7bf592010-08-24 05:47:05 +00001113 ParsedType TypeRep =
1114 Actions.getTypeName(*Tok.getIdentifierInfo(),
1115 Tok.getLocation(), getCurScope());
Douglas Gregor8bf42052009-02-09 18:46:07 +00001116
Chris Lattner6cc055a2009-04-12 20:42:31 +00001117 // If this is not a typedef name, don't parse it as part of the declspec,
1118 // it must be an implicit int or an error.
John McCallba7bf592010-08-24 05:47:05 +00001119 if (!TypeRep) {
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001120 if (ParseImplicitInt(DS, 0, TemplateInfo, AS)) continue;
Chris Lattner16fac4f2008-07-26 01:18:38 +00001121 goto DoneWithDeclSpec;
Chris Lattner6cc055a2009-04-12 20:42:31 +00001122 }
Douglas Gregor8bf42052009-02-09 18:46:07 +00001123
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001124 // If we're in a context where the identifier could be a class name,
1125 // check whether this is a constructor declaration.
1126 if (getLang().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00001127 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), getCurScope()) &&
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001128 isConstructorDeclarator())
Douglas Gregor61956c42008-10-31 09:07:45 +00001129 goto DoneWithDeclSpec;
1130
Douglas Gregor9817f4a2009-02-09 15:09:02 +00001131 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00001132 DiagID, TypeRep);
Chris Lattner16fac4f2008-07-26 01:18:38 +00001133 if (isInvalid)
1134 break;
Mike Stump11289f42009-09-09 15:08:12 +00001135
Chris Lattner16fac4f2008-07-26 01:18:38 +00001136 DS.SetRangeEnd(Tok.getLocation());
1137 ConsumeToken(); // The identifier
1138
1139 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1140 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001141 // Objective-C interface.
1142 if (Tok.is(tok::less) && getLang().ObjC1)
1143 ParseObjCProtocolQualifiers(DS);
1144
Steve Naroffcd5e7822008-09-22 10:28:57 +00001145 // Need to support trailing type qualifiers (e.g. "id<p> const").
1146 // If a type specifier follows, it will be diagnosed elsewhere.
1147 continue;
Chris Lattner16fac4f2008-07-26 01:18:38 +00001148 }
Douglas Gregor7f741122009-02-25 19:37:18 +00001149
1150 // type-name
1151 case tok::annot_template_id: {
Mike Stump11289f42009-09-09 15:08:12 +00001152 TemplateIdAnnotation *TemplateId
Douglas Gregor7f741122009-02-25 19:37:18 +00001153 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
Douglas Gregorb67535d2009-03-31 00:43:58 +00001154 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor7f741122009-02-25 19:37:18 +00001155 // This template-id does not refer to a type name, so we're
1156 // done with the type-specifiers.
1157 goto DoneWithDeclSpec;
1158 }
1159
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001160 // If we're in a context where the template-id could be a
1161 // constructor name or specialization, check whether this is a
1162 // constructor declaration.
1163 if (getLang().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00001164 Actions.isCurrentClassName(*TemplateId->Name, getCurScope()) &&
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001165 isConstructorDeclarator())
1166 goto DoneWithDeclSpec;
1167
Douglas Gregor7f741122009-02-25 19:37:18 +00001168 // Turn the template-id annotation token into a type annotation
1169 // token, then try again to parse it as a type-specifier.
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00001170 AnnotateTemplateIdTokenAsType();
Douglas Gregor7f741122009-02-25 19:37:18 +00001171 continue;
1172 }
1173
Chris Lattnere37e2332006-08-15 04:50:22 +00001174 // GNU attributes support.
1175 case tok::kw___attribute:
John McCall53fa7142010-12-24 02:08:15 +00001176 ParseGNUAttributes(DS.getAttributes());
Chris Lattnerb95cca02006-10-17 03:01:08 +00001177 continue;
Steve Naroff3a9b7e02008-12-24 20:59:21 +00001178
1179 // Microsoft declspec support.
1180 case tok::kw___declspec:
John McCall53fa7142010-12-24 02:08:15 +00001181 ParseMicrosoftDeclSpec(DS.getAttributes());
Steve Naroff3a9b7e02008-12-24 20:59:21 +00001182 continue;
Mike Stump11289f42009-09-09 15:08:12 +00001183
Steve Naroff44ac7772008-12-25 14:16:32 +00001184 // Microsoft single token adornments.
Steve Narofff9c29d42008-12-25 14:41:26 +00001185 case tok::kw___forceinline:
Eli Friedman53339e02009-06-08 23:27:34 +00001186 // FIXME: Add handling here!
1187 break;
1188
1189 case tok::kw___ptr64:
Steve Narofff9c29d42008-12-25 14:41:26 +00001190 case tok::kw___w64:
Steve Naroff44ac7772008-12-25 14:16:32 +00001191 case tok::kw___cdecl:
1192 case tok::kw___stdcall:
1193 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00001194 case tok::kw___thiscall:
John McCall53fa7142010-12-24 02:08:15 +00001195 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman53339e02009-06-08 23:27:34 +00001196 continue;
1197
Dawn Perchik335e16b2010-09-03 01:29:35 +00001198 // Borland single token adornments.
1199 case tok::kw___pascal:
John McCall53fa7142010-12-24 02:08:15 +00001200 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik335e16b2010-09-03 01:29:35 +00001201 continue;
1202
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001203 // storage-class-specifier
1204 case tok::kw_typedef:
John McCall49bfce42009-08-03 20:12:06 +00001205 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_typedef, Loc, PrevSpec,
1206 DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001207 break;
1208 case tok::kw_extern:
Chris Lattner353f5742006-11-28 04:50:12 +00001209 if (DS.isThreadSpecified())
Chris Lattner6d29c102008-11-18 07:48:38 +00001210 Diag(Tok, diag::ext_thread_before) << "extern";
John McCall49bfce42009-08-03 20:12:06 +00001211 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_extern, Loc, PrevSpec,
1212 DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001213 break;
Steve Naroff2050b0d2007-12-18 00:16:02 +00001214 case tok::kw___private_extern__:
Chris Lattner371ed4e2008-04-06 06:57:35 +00001215 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_private_extern, Loc,
John McCall49bfce42009-08-03 20:12:06 +00001216 PrevSpec, DiagID);
Steve Naroff2050b0d2007-12-18 00:16:02 +00001217 break;
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001218 case tok::kw_static:
Chris Lattner353f5742006-11-28 04:50:12 +00001219 if (DS.isThreadSpecified())
Chris Lattner6d29c102008-11-18 07:48:38 +00001220 Diag(Tok, diag::ext_thread_before) << "static";
John McCall49bfce42009-08-03 20:12:06 +00001221 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_static, Loc, PrevSpec,
1222 DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001223 break;
1224 case tok::kw_auto:
Anders Carlsson082acde2009-06-26 18:41:36 +00001225 if (getLang().CPlusPlus0x)
John McCall49bfce42009-08-03 20:12:06 +00001226 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec,
1227 DiagID);
Anders Carlsson082acde2009-06-26 18:41:36 +00001228 else
John McCall49bfce42009-08-03 20:12:06 +00001229 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, Loc, PrevSpec,
1230 DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001231 break;
1232 case tok::kw_register:
John McCall49bfce42009-08-03 20:12:06 +00001233 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_register, Loc, PrevSpec,
1234 DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001235 break;
Sebastian Redlccdfaba2008-11-14 23:42:31 +00001236 case tok::kw_mutable:
John McCall49bfce42009-08-03 20:12:06 +00001237 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_mutable, Loc, PrevSpec,
1238 DiagID);
Sebastian Redlccdfaba2008-11-14 23:42:31 +00001239 break;
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001240 case tok::kw___thread:
John McCall49bfce42009-08-03 20:12:06 +00001241 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001242 break;
Mike Stump11289f42009-09-09 15:08:12 +00001243
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001244 // function-specifier
1245 case tok::kw_inline:
John McCall49bfce42009-08-03 20:12:06 +00001246 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec, DiagID);
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001247 break;
Douglas Gregor61956c42008-10-31 09:07:45 +00001248 case tok::kw_virtual:
John McCall49bfce42009-08-03 20:12:06 +00001249 isInvalid = DS.SetFunctionSpecVirtual(Loc, PrevSpec, DiagID);
Douglas Gregor61956c42008-10-31 09:07:45 +00001250 break;
Douglas Gregor61956c42008-10-31 09:07:45 +00001251 case tok::kw_explicit:
John McCall49bfce42009-08-03 20:12:06 +00001252 isInvalid = DS.SetFunctionSpecExplicit(Loc, PrevSpec, DiagID);
Douglas Gregor61956c42008-10-31 09:07:45 +00001253 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00001254
Anders Carlssoncd8db412009-05-06 04:46:28 +00001255 // friend
1256 case tok::kw_friend:
John McCall07e91c02009-08-06 02:15:43 +00001257 if (DSContext == DSC_class)
1258 isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID);
1259 else {
1260 PrevSpec = ""; // not actually used by the diagnostic
1261 DiagID = diag::err_friend_invalid_in_context;
1262 isInvalid = true;
1263 }
Anders Carlssoncd8db412009-05-06 04:46:28 +00001264 break;
Mike Stump11289f42009-09-09 15:08:12 +00001265
Sebastian Redl39c2a8b2009-11-05 15:47:02 +00001266 // constexpr
1267 case tok::kw_constexpr:
1268 isInvalid = DS.SetConstexprSpec(Loc, PrevSpec, DiagID);
1269 break;
1270
Chris Lattnere387d9e2009-01-21 19:48:37 +00001271 // type-specifier
1272 case tok::kw_short:
John McCall49bfce42009-08-03 20:12:06 +00001273 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec,
1274 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001275 break;
1276 case tok::kw_long:
1277 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCall49bfce42009-08-03 20:12:06 +00001278 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
1279 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001280 else
John McCall49bfce42009-08-03 20:12:06 +00001281 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
1282 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001283 break;
1284 case tok::kw_signed:
John McCall49bfce42009-08-03 20:12:06 +00001285 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec,
1286 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001287 break;
1288 case tok::kw_unsigned:
John McCall49bfce42009-08-03 20:12:06 +00001289 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
1290 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001291 break;
1292 case tok::kw__Complex:
John McCall49bfce42009-08-03 20:12:06 +00001293 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
1294 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001295 break;
1296 case tok::kw__Imaginary:
John McCall49bfce42009-08-03 20:12:06 +00001297 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
1298 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001299 break;
1300 case tok::kw_void:
John McCall49bfce42009-08-03 20:12:06 +00001301 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec,
1302 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001303 break;
1304 case tok::kw_char:
John McCall49bfce42009-08-03 20:12:06 +00001305 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec,
1306 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001307 break;
1308 case tok::kw_int:
John McCall49bfce42009-08-03 20:12:06 +00001309 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec,
1310 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001311 break;
1312 case tok::kw_float:
John McCall49bfce42009-08-03 20:12:06 +00001313 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec,
1314 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001315 break;
1316 case tok::kw_double:
John McCall49bfce42009-08-03 20:12:06 +00001317 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec,
1318 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001319 break;
1320 case tok::kw_wchar_t:
John McCall49bfce42009-08-03 20:12:06 +00001321 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec,
1322 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001323 break;
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001324 case tok::kw_char16_t:
John McCall49bfce42009-08-03 20:12:06 +00001325 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec,
1326 DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001327 break;
1328 case tok::kw_char32_t:
John McCall49bfce42009-08-03 20:12:06 +00001329 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec,
1330 DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001331 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00001332 case tok::kw_bool:
1333 case tok::kw__Bool:
Argyrios Kyrtzidis20ee5ae2010-11-16 18:18:13 +00001334 if (Tok.is(tok::kw_bool) &&
1335 DS.getTypeSpecType() != DeclSpec::TST_unspecified &&
1336 DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
1337 PrevSpec = ""; // Not used by the diagnostic.
1338 DiagID = diag::err_bool_redeclaration;
1339 isInvalid = true;
1340 } else {
1341 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec,
1342 DiagID);
1343 }
Chris Lattnere387d9e2009-01-21 19:48:37 +00001344 break;
1345 case tok::kw__Decimal32:
John McCall49bfce42009-08-03 20:12:06 +00001346 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
1347 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001348 break;
1349 case tok::kw__Decimal64:
John McCall49bfce42009-08-03 20:12:06 +00001350 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
1351 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001352 break;
1353 case tok::kw__Decimal128:
John McCall49bfce42009-08-03 20:12:06 +00001354 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
1355 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001356 break;
John Thompson22334602010-02-05 00:12:22 +00001357 case tok::kw___vector:
1358 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
1359 break;
1360 case tok::kw___pixel:
1361 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
1362 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00001363
1364 // class-specifier:
1365 case tok::kw_class:
1366 case tok::kw_struct:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001367 case tok::kw_union: {
1368 tok::TokenKind Kind = Tok.getKind();
1369 ConsumeToken();
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001370 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001371 continue;
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001372 }
Chris Lattnere387d9e2009-01-21 19:48:37 +00001373
1374 // enum-specifier:
1375 case tok::kw_enum:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001376 ConsumeToken();
Douglas Gregordc70c3a2010-03-02 17:53:14 +00001377 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001378 continue;
1379
1380 // cv-qualifier:
1381 case tok::kw_const:
John McCall49bfce42009-08-03 20:12:06 +00001382 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec, DiagID,
1383 getLang());
Chris Lattnere387d9e2009-01-21 19:48:37 +00001384 break;
1385 case tok::kw_volatile:
John McCall49bfce42009-08-03 20:12:06 +00001386 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
1387 getLang());
Chris Lattnere387d9e2009-01-21 19:48:37 +00001388 break;
1389 case tok::kw_restrict:
John McCall49bfce42009-08-03 20:12:06 +00001390 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
1391 getLang());
Chris Lattnere387d9e2009-01-21 19:48:37 +00001392 break;
1393
Douglas Gregor333489b2009-03-27 23:10:48 +00001394 // C++ typename-specifier:
1395 case tok::kw_typename:
John McCall1f476a12010-02-26 08:45:28 +00001396 if (TryAnnotateTypeOrScopeToken()) {
1397 DS.SetTypeSpecError();
1398 goto DoneWithDeclSpec;
1399 }
1400 if (!Tok.is(tok::kw_typename))
Douglas Gregor333489b2009-03-27 23:10:48 +00001401 continue;
1402 break;
1403
Chris Lattnere387d9e2009-01-21 19:48:37 +00001404 // GNU typeof support.
1405 case tok::kw_typeof:
1406 ParseTypeofSpecifier(DS);
1407 continue;
1408
Anders Carlsson74948d02009-06-24 17:47:40 +00001409 case tok::kw_decltype:
1410 ParseDecltypeSpecifier(DS);
1411 continue;
1412
Steve Naroffcfdf6162008-06-05 00:02:44 +00001413 case tok::less:
Chris Lattner16fac4f2008-07-26 01:18:38 +00001414 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattner0974b232008-07-26 00:20:22 +00001415 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
1416 // but we support it.
Chris Lattner16fac4f2008-07-26 01:18:38 +00001417 if (DS.hasTypeSpecifier() || !getLang().ObjC1)
Chris Lattner0974b232008-07-26 00:20:22 +00001418 goto DoneWithDeclSpec;
Mike Stump11289f42009-09-09 15:08:12 +00001419
Douglas Gregor3a001f42010-11-19 17:10:50 +00001420 if (!ParseObjCProtocolQualifiers(DS))
1421 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
1422 << FixItHint::CreateInsertion(Loc, "id")
1423 << SourceRange(Loc, DS.getSourceRange().getEnd());
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001424
1425 // Need to support trailing type qualifiers (e.g. "id<p> const").
1426 // If a type specifier follows, it will be diagnosed elsewhere.
1427 continue;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001428 }
John McCall49bfce42009-08-03 20:12:06 +00001429 // If the specifier wasn't legal, issue a diagnostic.
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001430 if (isInvalid) {
1431 assert(PrevSpec && "Method did not return previous specifier!");
John McCall49bfce42009-08-03 20:12:06 +00001432 assert(DiagID);
Douglas Gregora05f5ab2010-08-23 14:34:43 +00001433
1434 if (DiagID == diag::ext_duplicate_declspec)
1435 Diag(Tok, DiagID)
1436 << PrevSpec << FixItHint::CreateRemoval(Tok.getLocation());
1437 else
1438 Diag(Tok, DiagID) << PrevSpec;
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001439 }
Chris Lattner2e232092008-03-13 06:29:04 +00001440 DS.SetRangeEnd(Tok.getLocation());
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001441 ConsumeToken();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001442 }
1443}
Douglas Gregoreb31f392008-12-01 23:54:00 +00001444
Chris Lattnera448d752009-01-06 06:59:53 +00001445/// ParseOptionalTypeSpecifier - Try to parse a single type-specifier. We
Douglas Gregor450c75a2008-11-07 15:42:26 +00001446/// primarily follow the C++ grammar with additions for C99 and GNU,
1447/// which together subsume the C grammar. Note that the C++
1448/// type-specifier also includes the C type-qualifier (for const,
1449/// volatile, and C99 restrict). Returns true if a type-specifier was
1450/// found (and parsed), false otherwise.
1451///
1452/// type-specifier: [C++ 7.1.5]
1453/// simple-type-specifier
1454/// class-specifier
1455/// enum-specifier
1456/// elaborated-type-specifier [TODO]
1457/// cv-qualifier
1458///
1459/// cv-qualifier: [C++ 7.1.5.1]
1460/// 'const'
1461/// 'volatile'
1462/// [C99] 'restrict'
1463///
1464/// simple-type-specifier: [ C++ 7.1.5.2]
1465/// '::'[opt] nested-name-specifier[opt] type-name [TODO]
1466/// '::'[opt] nested-name-specifier 'template' template-id [TODO]
1467/// 'char'
1468/// 'wchar_t'
1469/// 'bool'
1470/// 'short'
1471/// 'int'
1472/// 'long'
1473/// 'signed'
1474/// 'unsigned'
1475/// 'float'
1476/// 'double'
1477/// 'void'
1478/// [C99] '_Bool'
1479/// [C99] '_Complex'
1480/// [C99] '_Imaginary' // Removed in TC2?
1481/// [GNU] '_Decimal32'
1482/// [GNU] '_Decimal64'
1483/// [GNU] '_Decimal128'
1484/// [GNU] typeof-specifier
1485/// [OBJC] class-name objc-protocol-refs[opt] [TODO]
1486/// [OBJC] typedef-name objc-protocol-refs[opt] [TODO]
Anders Carlsson74948d02009-06-24 17:47:40 +00001487/// [C++0x] 'decltype' ( expression )
John Thompson22334602010-02-05 00:12:22 +00001488/// [AltiVec] '__vector'
John McCall49bfce42009-08-03 20:12:06 +00001489bool Parser::ParseOptionalTypeSpecifier(DeclSpec &DS, bool& isInvalid,
Chris Lattnera448d752009-01-06 06:59:53 +00001490 const char *&PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00001491 unsigned &DiagID,
Sebastian Redl2b372722010-02-03 21:21:43 +00001492 const ParsedTemplateInfo &TemplateInfo,
1493 bool SuppressDeclarations) {
Douglas Gregor450c75a2008-11-07 15:42:26 +00001494 SourceLocation Loc = Tok.getLocation();
1495
1496 switch (Tok.getKind()) {
Chris Lattner020bab92009-01-04 23:41:41 +00001497 case tok::identifier: // foo::bar
Douglas Gregorb8eaf292010-04-15 23:40:53 +00001498 // If we already have a type specifier, this identifier is not a type.
1499 if (DS.getTypeSpecType() != DeclSpec::TST_unspecified ||
1500 DS.getTypeSpecWidth() != DeclSpec::TSW_unspecified ||
1501 DS.getTypeSpecSign() != DeclSpec::TSS_unspecified)
1502 return false;
John Thompson22334602010-02-05 00:12:22 +00001503 // Check for need to substitute AltiVec keyword tokens.
1504 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
1505 break;
1506 // Fall through.
Douglas Gregor333489b2009-03-27 23:10:48 +00001507 case tok::kw_typename: // typename foo::bar
Chris Lattner020bab92009-01-04 23:41:41 +00001508 // Annotate typenames and C++ scope specifiers. If we get one, just
1509 // recurse to handle whatever we get.
1510 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00001511 return true;
1512 if (Tok.is(tok::identifier))
1513 return false;
1514 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
1515 TemplateInfo, SuppressDeclarations);
Chris Lattner020bab92009-01-04 23:41:41 +00001516 case tok::coloncolon: // ::foo::bar
1517 if (NextToken().is(tok::kw_new) || // ::new
1518 NextToken().is(tok::kw_delete)) // ::delete
1519 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001520
Chris Lattner020bab92009-01-04 23:41:41 +00001521 // Annotate typenames and C++ scope specifiers. If we get one, just
1522 // recurse to handle whatever we get.
1523 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00001524 return true;
1525 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
1526 TemplateInfo, SuppressDeclarations);
Mike Stump11289f42009-09-09 15:08:12 +00001527
Douglas Gregor450c75a2008-11-07 15:42:26 +00001528 // simple-type-specifier:
Chris Lattnera8a3f732009-01-06 05:06:21 +00001529 case tok::annot_typename: {
John McCallba7bf592010-08-24 05:47:05 +00001530 if (ParsedType T = getTypeAnnotation(Tok)) {
Nico Weber77430342010-11-22 10:30:56 +00001531 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename,
1532 Tok.getAnnotationEndLoc(), PrevSpec,
John McCallba7bf592010-08-24 05:47:05 +00001533 DiagID, T);
1534 } else
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00001535 DS.SetTypeSpecError();
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001536 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1537 ConsumeToken(); // The typename
Mike Stump11289f42009-09-09 15:08:12 +00001538
Douglas Gregor450c75a2008-11-07 15:42:26 +00001539 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1540 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
1541 // Objective-C interface. If we don't have Objective-C or a '<', this is
1542 // just a normal reference to a typedef name.
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001543 if (Tok.is(tok::less) && getLang().ObjC1)
1544 ParseObjCProtocolQualifiers(DS);
1545
Douglas Gregor450c75a2008-11-07 15:42:26 +00001546 return true;
1547 }
1548
1549 case tok::kw_short:
John McCall49bfce42009-08-03 20:12:06 +00001550 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001551 break;
1552 case tok::kw_long:
1553 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCall49bfce42009-08-03 20:12:06 +00001554 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
1555 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001556 else
John McCall49bfce42009-08-03 20:12:06 +00001557 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
1558 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001559 break;
1560 case tok::kw_signed:
John McCall49bfce42009-08-03 20:12:06 +00001561 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001562 break;
1563 case tok::kw_unsigned:
John McCall49bfce42009-08-03 20:12:06 +00001564 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
1565 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001566 break;
1567 case tok::kw__Complex:
John McCall49bfce42009-08-03 20:12:06 +00001568 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
1569 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001570 break;
1571 case tok::kw__Imaginary:
John McCall49bfce42009-08-03 20:12:06 +00001572 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
1573 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001574 break;
1575 case tok::kw_void:
John McCall49bfce42009-08-03 20:12:06 +00001576 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001577 break;
1578 case tok::kw_char:
John McCall49bfce42009-08-03 20:12:06 +00001579 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001580 break;
1581 case tok::kw_int:
John McCall49bfce42009-08-03 20:12:06 +00001582 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001583 break;
1584 case tok::kw_float:
John McCall49bfce42009-08-03 20:12:06 +00001585 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001586 break;
1587 case tok::kw_double:
John McCall49bfce42009-08-03 20:12:06 +00001588 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001589 break;
1590 case tok::kw_wchar_t:
John McCall49bfce42009-08-03 20:12:06 +00001591 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001592 break;
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001593 case tok::kw_char16_t:
John McCall49bfce42009-08-03 20:12:06 +00001594 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001595 break;
1596 case tok::kw_char32_t:
John McCall49bfce42009-08-03 20:12:06 +00001597 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001598 break;
Douglas Gregor450c75a2008-11-07 15:42:26 +00001599 case tok::kw_bool:
1600 case tok::kw__Bool:
John McCall49bfce42009-08-03 20:12:06 +00001601 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001602 break;
1603 case tok::kw__Decimal32:
John McCall49bfce42009-08-03 20:12:06 +00001604 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
1605 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001606 break;
1607 case tok::kw__Decimal64:
John McCall49bfce42009-08-03 20:12:06 +00001608 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
1609 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001610 break;
1611 case tok::kw__Decimal128:
John McCall49bfce42009-08-03 20:12:06 +00001612 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
1613 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001614 break;
John Thompson22334602010-02-05 00:12:22 +00001615 case tok::kw___vector:
1616 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
1617 break;
1618 case tok::kw___pixel:
1619 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
1620 break;
1621
Douglas Gregor450c75a2008-11-07 15:42:26 +00001622 // class-specifier:
1623 case tok::kw_class:
1624 case tok::kw_struct:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001625 case tok::kw_union: {
1626 tok::TokenKind Kind = Tok.getKind();
1627 ConsumeToken();
Sebastian Redl2b372722010-02-03 21:21:43 +00001628 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS_none,
1629 SuppressDeclarations);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001630 return true;
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001631 }
Douglas Gregor450c75a2008-11-07 15:42:26 +00001632
1633 // enum-specifier:
1634 case tok::kw_enum:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001635 ConsumeToken();
Douglas Gregordc70c3a2010-03-02 17:53:14 +00001636 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS_none);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001637 return true;
1638
1639 // cv-qualifier:
1640 case tok::kw_const:
1641 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00001642 DiagID, getLang());
Douglas Gregor450c75a2008-11-07 15:42:26 +00001643 break;
1644 case tok::kw_volatile:
1645 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00001646 DiagID, getLang());
Douglas Gregor450c75a2008-11-07 15:42:26 +00001647 break;
1648 case tok::kw_restrict:
1649 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00001650 DiagID, getLang());
Douglas Gregor450c75a2008-11-07 15:42:26 +00001651 break;
1652
1653 // GNU typeof support.
1654 case tok::kw_typeof:
1655 ParseTypeofSpecifier(DS);
1656 return true;
1657
Anders Carlsson74948d02009-06-24 17:47:40 +00001658 // C++0x decltype support.
1659 case tok::kw_decltype:
1660 ParseDecltypeSpecifier(DS);
1661 return true;
Mike Stump11289f42009-09-09 15:08:12 +00001662
Anders Carlssonbae27372009-06-26 23:44:14 +00001663 // C++0x auto support.
1664 case tok::kw_auto:
1665 if (!getLang().CPlusPlus0x)
1666 return false;
1667
John McCall49bfce42009-08-03 20:12:06 +00001668 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec, DiagID);
Anders Carlssonbae27372009-06-26 23:44:14 +00001669 break;
Dawn Perchik335e16b2010-09-03 01:29:35 +00001670
Eli Friedman53339e02009-06-08 23:27:34 +00001671 case tok::kw___ptr64:
1672 case tok::kw___w64:
Steve Naroff44ac7772008-12-25 14:16:32 +00001673 case tok::kw___cdecl:
1674 case tok::kw___stdcall:
1675 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00001676 case tok::kw___thiscall:
John McCall53fa7142010-12-24 02:08:15 +00001677 ParseMicrosoftTypeAttributes(DS.getAttributes());
Chris Lattner78ecd4f2009-01-21 19:19:26 +00001678 return true;
Steve Naroff44ac7772008-12-25 14:16:32 +00001679
Dawn Perchik335e16b2010-09-03 01:29:35 +00001680 case tok::kw___pascal:
John McCall53fa7142010-12-24 02:08:15 +00001681 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik335e16b2010-09-03 01:29:35 +00001682 return true;
1683
Douglas Gregor450c75a2008-11-07 15:42:26 +00001684 default:
1685 // Not a type-specifier; do nothing.
1686 return false;
1687 }
1688
1689 // If the specifier combination wasn't legal, issue a diagnostic.
1690 if (isInvalid) {
1691 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner6d29c102008-11-18 07:48:38 +00001692 // Pick between error or extwarn.
Chris Lattner6d29c102008-11-18 07:48:38 +00001693 Diag(Tok, DiagID) << PrevSpec;
Douglas Gregor450c75a2008-11-07 15:42:26 +00001694 }
1695 DS.SetRangeEnd(Tok.getLocation());
1696 ConsumeToken(); // whatever we parsed above.
1697 return true;
1698}
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001699
Chris Lattner70ae4912007-10-29 04:42:53 +00001700/// ParseStructDeclaration - Parse a struct declaration without the terminating
1701/// semicolon.
1702///
Chris Lattner90a26b02007-01-23 04:38:16 +00001703/// struct-declaration:
Chris Lattner70ae4912007-10-29 04:42:53 +00001704/// specifier-qualifier-list struct-declarator-list
Chris Lattner736ed5d2007-06-09 05:59:07 +00001705/// [GNU] __extension__ struct-declaration
Chris Lattner70ae4912007-10-29 04:42:53 +00001706/// [GNU] specifier-qualifier-list
Chris Lattner90a26b02007-01-23 04:38:16 +00001707/// struct-declarator-list:
1708/// struct-declarator
1709/// struct-declarator-list ',' struct-declarator
1710/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
1711/// struct-declarator:
1712/// declarator
1713/// [GNU] declarator attributes[opt]
1714/// declarator[opt] ':' constant-expression
1715/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
1716///
Chris Lattnera12405b2008-04-10 06:46:29 +00001717void Parser::
John McCallcfefb6d2009-11-03 02:38:08 +00001718ParseStructDeclaration(DeclSpec &DS, FieldCallback &Fields) {
Chris Lattnerf02ef3e2008-10-20 06:45:43 +00001719 if (Tok.is(tok::kw___extension__)) {
1720 // __extension__ silences extension warnings in the subexpression.
1721 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroff97170802007-08-20 22:28:22 +00001722 ConsumeToken();
Chris Lattnerf02ef3e2008-10-20 06:45:43 +00001723 return ParseStructDeclaration(DS, Fields);
1724 }
Mike Stump11289f42009-09-09 15:08:12 +00001725
Steve Naroff97170802007-08-20 22:28:22 +00001726 // Parse the common specifier-qualifiers-list piece.
Steve Naroff97170802007-08-20 22:28:22 +00001727 ParseSpecifierQualifierList(DS);
Mike Stump11289f42009-09-09 15:08:12 +00001728
Douglas Gregorc6f58fe2009-01-12 22:49:06 +00001729 // If there are no declarators, this is a free-standing declaration
1730 // specifier. Let the actions module cope with it.
Chris Lattner76c72282007-10-09 17:33:22 +00001731 if (Tok.is(tok::semi)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001732 Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none, DS);
Steve Naroff97170802007-08-20 22:28:22 +00001733 return;
1734 }
1735
1736 // Read struct-declarators until we find the semicolon.
John McCallcfefb6d2009-11-03 02:38:08 +00001737 bool FirstDeclarator = true;
Steve Naroff97170802007-08-20 22:28:22 +00001738 while (1) {
John McCall28a6aea2009-11-04 02:18:39 +00001739 ParsingDeclRAIIObject PD(*this);
John McCallcfefb6d2009-11-03 02:38:08 +00001740 FieldDeclarator DeclaratorInfo(DS);
1741
1742 // Attributes are only allowed here on successive declarators.
John McCall53fa7142010-12-24 02:08:15 +00001743 if (!FirstDeclarator)
1744 MaybeParseGNUAttributes(DeclaratorInfo.D);
Mike Stump11289f42009-09-09 15:08:12 +00001745
Steve Naroff97170802007-08-20 22:28:22 +00001746 /// struct-declarator: declarator
1747 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattner17c3b1f2009-12-10 01:59:24 +00001748 if (Tok.isNot(tok::colon)) {
1749 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
1750 ColonProtectionRAIIObject X(*this);
Chris Lattnera12405b2008-04-10 06:46:29 +00001751 ParseDeclarator(DeclaratorInfo.D);
Chris Lattner17c3b1f2009-12-10 01:59:24 +00001752 }
Mike Stump11289f42009-09-09 15:08:12 +00001753
Chris Lattner76c72282007-10-09 17:33:22 +00001754 if (Tok.is(tok::colon)) {
Steve Naroff97170802007-08-20 22:28:22 +00001755 ConsumeToken();
John McCalldadc5752010-08-24 06:29:42 +00001756 ExprResult Res(ParseConstantExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001757 if (Res.isInvalid())
Steve Naroff97170802007-08-20 22:28:22 +00001758 SkipUntil(tok::semi, true, true);
Chris Lattner32295d32008-04-10 06:15:14 +00001759 else
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001760 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroff97170802007-08-20 22:28:22 +00001761 }
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001762
Steve Naroff97170802007-08-20 22:28:22 +00001763 // If attributes exist after the declarator, parse them.
John McCall53fa7142010-12-24 02:08:15 +00001764 MaybeParseGNUAttributes(DeclaratorInfo.D);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001765
John McCallcfefb6d2009-11-03 02:38:08 +00001766 // We're done with this declarator; invoke the callback.
John McCall48871652010-08-21 09:40:31 +00001767 Decl *D = Fields.invoke(DeclaratorInfo);
John McCall28a6aea2009-11-04 02:18:39 +00001768 PD.complete(D);
John McCallcfefb6d2009-11-03 02:38:08 +00001769
Steve Naroff97170802007-08-20 22:28:22 +00001770 // If we don't have a comma, it is either the end of the list (a ';')
1771 // or an error, bail out.
Chris Lattner76c72282007-10-09 17:33:22 +00001772 if (Tok.isNot(tok::comma))
Chris Lattner70ae4912007-10-29 04:42:53 +00001773 return;
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001774
Steve Naroff97170802007-08-20 22:28:22 +00001775 // Consume the comma.
1776 ConsumeToken();
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001777
John McCallcfefb6d2009-11-03 02:38:08 +00001778 FirstDeclarator = false;
Steve Naroff97170802007-08-20 22:28:22 +00001779 }
Steve Naroff97170802007-08-20 22:28:22 +00001780}
1781
1782/// ParseStructUnionBody
1783/// struct-contents:
1784/// struct-declaration-list
1785/// [EXT] empty
1786/// [GNU] "struct-declaration-list" without terminatoring ';'
1787/// struct-declaration-list:
1788/// struct-declaration
1789/// struct-declaration-list struct-declaration
Chris Lattner535b8302008-06-21 19:39:06 +00001790/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroff97170802007-08-20 22:28:22 +00001791///
Chris Lattner1300fb92007-01-23 23:42:53 +00001792void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
John McCall48871652010-08-21 09:40:31 +00001793 unsigned TagType, Decl *TagDecl) {
John McCallfaf5fb42010-08-26 23:41:50 +00001794 PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc,
1795 "parsing struct/union body");
Mike Stump11289f42009-09-09 15:08:12 +00001796
Chris Lattner90a26b02007-01-23 04:38:16 +00001797 SourceLocation LBraceLoc = ConsumeBrace();
Mike Stump11289f42009-09-09 15:08:12 +00001798
Douglas Gregor658b9552009-01-09 22:42:13 +00001799 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
Douglas Gregor0be31a22010-07-02 17:43:08 +00001800 Actions.ActOnTagStartDefinition(getCurScope(), TagDecl);
Douglas Gregor82ac25e2009-01-08 20:45:30 +00001801
Chris Lattner7b9ace62007-01-23 20:11:08 +00001802 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
1803 // C++.
Douglas Gregor556877c2008-04-13 21:30:24 +00001804 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Douglas Gregorda2955e2010-07-29 14:29:34 +00001805 Diag(Tok, diag::ext_empty_struct_union)
1806 << (TagType == TST_union);
Chris Lattner7b9ace62007-01-23 20:11:08 +00001807
John McCall48871652010-08-21 09:40:31 +00001808 llvm::SmallVector<Decl *, 32> FieldDecls;
Chris Lattnera12405b2008-04-10 06:46:29 +00001809
Chris Lattner7b9ace62007-01-23 20:11:08 +00001810 // While we still have something to read, read the declarations in the struct.
Chris Lattner76c72282007-10-09 17:33:22 +00001811 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Chris Lattner90a26b02007-01-23 04:38:16 +00001812 // Each iteration of this loop reads one struct-declaration.
Mike Stump11289f42009-09-09 15:08:12 +00001813
Chris Lattner736ed5d2007-06-09 05:59:07 +00001814 // Check for extraneous top-level semicolon.
Chris Lattner76c72282007-10-09 17:33:22 +00001815 if (Tok.is(tok::semi)) {
Douglas Gregore3e01a22009-04-01 22:41:11 +00001816 Diag(Tok, diag::ext_extra_struct_semi)
Douglas Gregor13d05682010-06-16 23:08:59 +00001817 << DeclSpec::getSpecifierName((DeclSpec::TST)TagType)
Douglas Gregora771f462010-03-31 17:46:05 +00001818 << FixItHint::CreateRemoval(Tok.getLocation());
Chris Lattner36e46a22007-06-09 05:49:55 +00001819 ConsumeToken();
1820 continue;
1821 }
Chris Lattnera12405b2008-04-10 06:46:29 +00001822
1823 // Parse all the comma separated declarators.
1824 DeclSpec DS;
Mike Stump11289f42009-09-09 15:08:12 +00001825
John McCallcfefb6d2009-11-03 02:38:08 +00001826 if (!Tok.is(tok::at)) {
1827 struct CFieldCallback : FieldCallback {
1828 Parser &P;
John McCall48871652010-08-21 09:40:31 +00001829 Decl *TagDecl;
1830 llvm::SmallVectorImpl<Decl *> &FieldDecls;
John McCallcfefb6d2009-11-03 02:38:08 +00001831
John McCall48871652010-08-21 09:40:31 +00001832 CFieldCallback(Parser &P, Decl *TagDecl,
1833 llvm::SmallVectorImpl<Decl *> &FieldDecls) :
John McCallcfefb6d2009-11-03 02:38:08 +00001834 P(P), TagDecl(TagDecl), FieldDecls(FieldDecls) {}
1835
John McCall48871652010-08-21 09:40:31 +00001836 virtual Decl *invoke(FieldDeclarator &FD) {
John McCallcfefb6d2009-11-03 02:38:08 +00001837 // Install the declarator into the current TagDecl.
John McCall48871652010-08-21 09:40:31 +00001838 Decl *Field = P.Actions.ActOnField(P.getCurScope(), TagDecl,
John McCall5e6253b2009-11-03 21:13:47 +00001839 FD.D.getDeclSpec().getSourceRange().getBegin(),
1840 FD.D, FD.BitfieldSize);
John McCallcfefb6d2009-11-03 02:38:08 +00001841 FieldDecls.push_back(Field);
1842 return Field;
Douglas Gregor66a985d2009-08-26 14:27:30 +00001843 }
John McCallcfefb6d2009-11-03 02:38:08 +00001844 } Callback(*this, TagDecl, FieldDecls);
1845
1846 ParseStructDeclaration(DS, Callback);
Chris Lattner535b8302008-06-21 19:39:06 +00001847 } else { // Handle @defs
1848 ConsumeToken();
1849 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
1850 Diag(Tok, diag::err_unexpected_at);
Chris Lattner245c5332010-02-02 00:37:27 +00001851 SkipUntil(tok::semi, true);
Chris Lattner535b8302008-06-21 19:39:06 +00001852 continue;
1853 }
1854 ConsumeToken();
1855 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
1856 if (!Tok.is(tok::identifier)) {
1857 Diag(Tok, diag::err_expected_ident);
Chris Lattner245c5332010-02-02 00:37:27 +00001858 SkipUntil(tok::semi, true);
Chris Lattner535b8302008-06-21 19:39:06 +00001859 continue;
1860 }
John McCall48871652010-08-21 09:40:31 +00001861 llvm::SmallVector<Decl *, 16> Fields;
Douglas Gregor0be31a22010-07-02 17:43:08 +00001862 Actions.ActOnDefs(getCurScope(), TagDecl, Tok.getLocation(),
Douglas Gregor91f84212008-12-11 16:49:14 +00001863 Tok.getIdentifierInfo(), Fields);
Chris Lattner535b8302008-06-21 19:39:06 +00001864 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
1865 ConsumeToken();
1866 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
Mike Stump11289f42009-09-09 15:08:12 +00001867 }
Chris Lattner736ed5d2007-06-09 05:59:07 +00001868
Chris Lattner76c72282007-10-09 17:33:22 +00001869 if (Tok.is(tok::semi)) {
Chris Lattner90a26b02007-01-23 04:38:16 +00001870 ConsumeToken();
Chris Lattner76c72282007-10-09 17:33:22 +00001871 } else if (Tok.is(tok::r_brace)) {
Chris Lattner245c5332010-02-02 00:37:27 +00001872 ExpectAndConsume(tok::semi, diag::ext_expected_semi_decl_list);
Chris Lattner0c7e82d2007-06-09 05:54:40 +00001873 break;
Chris Lattner90a26b02007-01-23 04:38:16 +00001874 } else {
Chris Lattner245c5332010-02-02 00:37:27 +00001875 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
1876 // Skip to end of block or statement to avoid ext-warning on extra ';'.
Chris Lattner90a26b02007-01-23 04:38:16 +00001877 SkipUntil(tok::r_brace, true, true);
Chris Lattner245c5332010-02-02 00:37:27 +00001878 // If we stopped at a ';', eat it.
1879 if (Tok.is(tok::semi)) ConsumeToken();
Chris Lattner90a26b02007-01-23 04:38:16 +00001880 }
1881 }
Mike Stump11289f42009-09-09 15:08:12 +00001882
Steve Naroff33a1e802007-10-29 21:38:07 +00001883 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Mike Stump11289f42009-09-09 15:08:12 +00001884
John McCall53fa7142010-12-24 02:08:15 +00001885 ParsedAttributes attrs;
Chris Lattner90a26b02007-01-23 04:38:16 +00001886 // If attributes exist after struct contents, parse them.
John McCall53fa7142010-12-24 02:08:15 +00001887 MaybeParseGNUAttributes(attrs);
Daniel Dunbar15619c72008-10-03 02:03:53 +00001888
Douglas Gregor0be31a22010-07-02 17:43:08 +00001889 Actions.ActOnFields(getCurScope(),
Jay Foad7d0479f2009-05-21 09:52:38 +00001890 RecordLoc, TagDecl, FieldDecls.data(), FieldDecls.size(),
Daniel Dunbar15619c72008-10-03 02:03:53 +00001891 LBraceLoc, RBraceLoc,
John McCall53fa7142010-12-24 02:08:15 +00001892 attrs.getList());
Douglas Gregor82ac25e2009-01-08 20:45:30 +00001893 StructScope.Exit();
Douglas Gregor0be31a22010-07-02 17:43:08 +00001894 Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl, RBraceLoc);
Chris Lattner90a26b02007-01-23 04:38:16 +00001895}
1896
Chris Lattner3b561a32006-08-13 00:12:11 +00001897/// ParseEnumSpecifier
Chris Lattner1890ac82006-08-13 01:16:23 +00001898/// enum-specifier: [C99 6.7.2.2]
Chris Lattner3b561a32006-08-13 00:12:11 +00001899/// 'enum' identifier[opt] '{' enumerator-list '}'
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001900///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Chris Lattnere37e2332006-08-15 04:50:22 +00001901/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
1902/// '}' attributes[opt]
Chris Lattner3b561a32006-08-13 00:12:11 +00001903/// 'enum' identifier
Chris Lattnere37e2332006-08-15 04:50:22 +00001904/// [GNU] 'enum' attributes[opt] identifier
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001905///
Douglas Gregor0bf31402010-10-08 23:50:27 +00001906/// [C++0x] enum-head '{' enumerator-list[opt] '}'
1907/// [C++0x] enum-head '{' enumerator-list ',' '}'
1908///
1909/// enum-head: [C++0x]
1910/// enum-key attributes[opt] identifier[opt] enum-base[opt]
1911/// enum-key attributes[opt] nested-name-specifier identifier enum-base[opt]
1912///
1913/// enum-key: [C++0x]
1914/// 'enum'
1915/// 'enum' 'class'
1916/// 'enum' 'struct'
1917///
1918/// enum-base: [C++0x]
1919/// ':' type-specifier-seq
1920///
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001921/// [C++] elaborated-type-specifier:
1922/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
1923///
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001924void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregordc70c3a2010-03-02 17:53:14 +00001925 const ParsedTemplateInfo &TemplateInfo,
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001926 AccessSpecifier AS) {
Chris Lattnerffbc2712007-01-25 06:05:38 +00001927 // Parse the tag portion of this.
Douglas Gregorf45b0cf2009-09-18 15:37:17 +00001928 if (Tok.is(tok::code_completion)) {
1929 // Code completion for an enum name.
Douglas Gregor0be31a22010-07-02 17:43:08 +00001930 Actions.CodeCompleteTag(getCurScope(), DeclSpec::TST_enum);
Douglas Gregor6da3db42010-05-25 05:58:43 +00001931 ConsumeCodeCompletionToken();
Douglas Gregorf45b0cf2009-09-18 15:37:17 +00001932 }
1933
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00001934 // If attributes exist after tag, parse them.
John McCall53fa7142010-12-24 02:08:15 +00001935 ParsedAttributes attrs;
1936 MaybeParseGNUAttributes(attrs);
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001937
Abramo Bagnarad7548482010-05-19 21:37:53 +00001938 CXXScopeSpec &SS = DS.getTypeSpecScope();
John McCall1f476a12010-02-26 08:45:28 +00001939 if (getLang().CPlusPlus) {
John McCallba7bf592010-08-24 05:47:05 +00001940 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), false))
John McCall1f476a12010-02-26 08:45:28 +00001941 return;
1942
1943 if (SS.isSet() && Tok.isNot(tok::identifier)) {
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001944 Diag(Tok, diag::err_expected_ident);
1945 if (Tok.isNot(tok::l_brace)) {
1946 // Has no name and is not a definition.
1947 // Skip the rest of this declarator, up until the comma or semicolon.
1948 SkipUntil(tok::comma, true);
1949 return;
1950 }
1951 }
1952 }
Mike Stump11289f42009-09-09 15:08:12 +00001953
Douglas Gregor0bf31402010-10-08 23:50:27 +00001954 bool IsScopedEnum = false;
Abramo Bagnara0e05e242010-12-03 18:54:17 +00001955 bool IsScopedUsingClassTag = false;
Douglas Gregor0bf31402010-10-08 23:50:27 +00001956
Abramo Bagnara0e05e242010-12-03 18:54:17 +00001957 if (getLang().CPlusPlus0x &&
1958 (Tok.is(tok::kw_class) || Tok.is(tok::kw_struct))) {
Douglas Gregor0bf31402010-10-08 23:50:27 +00001959 IsScopedEnum = true;
Abramo Bagnara0e05e242010-12-03 18:54:17 +00001960 IsScopedUsingClassTag = Tok.is(tok::kw_class);
1961 ConsumeToken();
Douglas Gregor0bf31402010-10-08 23:50:27 +00001962 }
1963
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00001964 // Must have either 'enum name' or 'enum {...}'.
1965 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace)) {
1966 Diag(Tok, diag::err_expected_ident_lbrace);
Mike Stump11289f42009-09-09 15:08:12 +00001967
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00001968 // Skip the rest of this declarator, up until the comma or semicolon.
1969 SkipUntil(tok::comma, true);
Chris Lattner3b561a32006-08-13 00:12:11 +00001970 return;
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00001971 }
Mike Stump11289f42009-09-09 15:08:12 +00001972
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00001973 // If an identifier is present, consume and remember it.
1974 IdentifierInfo *Name = 0;
1975 SourceLocation NameLoc;
1976 if (Tok.is(tok::identifier)) {
1977 Name = Tok.getIdentifierInfo();
1978 NameLoc = ConsumeToken();
1979 }
Mike Stump11289f42009-09-09 15:08:12 +00001980
Douglas Gregor0bf31402010-10-08 23:50:27 +00001981 if (!Name && IsScopedEnum) {
1982 // C++0x 7.2p2: The optional identifier shall not be omitted in the
1983 // declaration of a scoped enumeration.
1984 Diag(Tok, diag::err_scoped_enum_missing_identifier);
1985 IsScopedEnum = false;
Abramo Bagnara0e05e242010-12-03 18:54:17 +00001986 IsScopedUsingClassTag = false;
Douglas Gregor0bf31402010-10-08 23:50:27 +00001987 }
1988
1989 TypeResult BaseType;
1990
Douglas Gregord1f69f62010-12-01 17:42:47 +00001991 // Parse the fixed underlying type.
Douglas Gregor0bf31402010-10-08 23:50:27 +00001992 if (getLang().CPlusPlus0x && Tok.is(tok::colon)) {
Douglas Gregord1f69f62010-12-01 17:42:47 +00001993 bool PossibleBitfield = false;
1994 if (getCurScope()->getFlags() & Scope::ClassScope) {
1995 // If we're in class scope, this can either be an enum declaration with
1996 // an underlying type, or a declaration of a bitfield member. We try to
1997 // use a simple disambiguation scheme first to catch the common cases
1998 // (integer literal, sizeof); if it's still ambiguous, we then consider
1999 // anything that's a simple-type-specifier followed by '(' as an
2000 // expression. This suffices because function types are not valid
2001 // underlying types anyway.
2002 TPResult TPR = isExpressionOrTypeSpecifierSimple(NextToken().getKind());
2003 // If the next token starts an expression, we know we're parsing a
2004 // bit-field. This is the common case.
2005 if (TPR == TPResult::True())
2006 PossibleBitfield = true;
2007 // If the next token starts a type-specifier-seq, it may be either a
2008 // a fixed underlying type or the start of a function-style cast in C++;
2009 // lookahead one more token to see if it's obvious that we have a
2010 // fixed underlying type.
2011 else if (TPR == TPResult::False() &&
2012 GetLookAheadToken(2).getKind() == tok::semi) {
2013 // Consume the ':'.
2014 ConsumeToken();
2015 } else {
2016 // We have the start of a type-specifier-seq, so we have to perform
2017 // tentative parsing to determine whether we have an expression or a
2018 // type.
2019 TentativeParsingAction TPA(*this);
2020
2021 // Consume the ':'.
2022 ConsumeToken();
2023
2024 if (isCXXDeclarationSpecifier() != TPResult::True()) {
2025 // We'll parse this as a bitfield later.
2026 PossibleBitfield = true;
2027 TPA.Revert();
2028 } else {
2029 // We have a type-specifier-seq.
2030 TPA.Commit();
2031 }
2032 }
2033 } else {
2034 // Consume the ':'.
2035 ConsumeToken();
2036 }
2037
2038 if (!PossibleBitfield) {
2039 SourceRange Range;
2040 BaseType = ParseTypeName(&Range);
2041 }
Douglas Gregor0bf31402010-10-08 23:50:27 +00002042 }
2043
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00002044 // There are three options here. If we have 'enum foo;', then this is a
2045 // forward declaration. If we have 'enum foo {...' then this is a
2046 // definition. Otherwise we have something like 'enum foo xyz', a reference.
2047 //
2048 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
2049 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
2050 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
2051 //
John McCallfaf5fb42010-08-26 23:41:50 +00002052 Sema::TagUseKind TUK;
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00002053 if (Tok.is(tok::l_brace))
John McCallfaf5fb42010-08-26 23:41:50 +00002054 TUK = Sema::TUK_Definition;
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00002055 else if (Tok.is(tok::semi))
John McCallfaf5fb42010-08-26 23:41:50 +00002056 TUK = Sema::TUK_Declaration;
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00002057 else
John McCallfaf5fb42010-08-26 23:41:50 +00002058 TUK = Sema::TUK_Reference;
Douglas Gregorcbbf3e32010-05-03 17:48:54 +00002059
2060 // enums cannot be templates, although they can be referenced from a
2061 // template.
2062 if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate &&
John McCallfaf5fb42010-08-26 23:41:50 +00002063 TUK != Sema::TUK_Reference) {
Douglas Gregorcbbf3e32010-05-03 17:48:54 +00002064 Diag(Tok, diag::err_enum_template);
2065
2066 // Skip the rest of this declarator, up until the comma or semicolon.
2067 SkipUntil(tok::comma, true);
2068 return;
2069 }
2070
Douglas Gregord6ab8742009-05-28 23:31:59 +00002071 bool Owned = false;
John McCall7f41d982009-09-11 04:59:25 +00002072 bool IsDependent = false;
Douglas Gregorba41d012010-04-24 16:38:41 +00002073 SourceLocation TSTLoc = NameLoc.isValid()? NameLoc : StartLoc;
2074 const char *PrevSpec = 0;
2075 unsigned DiagID;
John McCall48871652010-08-21 09:40:31 +00002076 Decl *TagDecl = Actions.ActOnTag(getCurScope(), DeclSpec::TST_enum, TUK,
John McCall53fa7142010-12-24 02:08:15 +00002077 StartLoc, SS, Name, NameLoc, attrs.getList(),
John McCall48871652010-08-21 09:40:31 +00002078 AS,
John McCallfaf5fb42010-08-26 23:41:50 +00002079 MultiTemplateParamsArg(Actions),
Douglas Gregor0bf31402010-10-08 23:50:27 +00002080 Owned, IsDependent, IsScopedEnum,
Abramo Bagnara0e05e242010-12-03 18:54:17 +00002081 IsScopedUsingClassTag, BaseType);
Douglas Gregor0bf31402010-10-08 23:50:27 +00002082
Douglas Gregorba41d012010-04-24 16:38:41 +00002083 if (IsDependent) {
2084 // This enum has a dependent nested-name-specifier. Handle it as a
2085 // dependent tag.
2086 if (!Name) {
2087 DS.SetTypeSpecError();
2088 Diag(Tok, diag::err_expected_type_name_after_typename);
2089 return;
2090 }
2091
Douglas Gregor0be31a22010-07-02 17:43:08 +00002092 TypeResult Type = Actions.ActOnDependentTag(getCurScope(), DeclSpec::TST_enum,
Douglas Gregorba41d012010-04-24 16:38:41 +00002093 TUK, SS, Name, StartLoc,
2094 NameLoc);
2095 if (Type.isInvalid()) {
2096 DS.SetTypeSpecError();
2097 return;
2098 }
2099
2100 if (DS.SetTypeSpecType(DeclSpec::TST_typename, TSTLoc, PrevSpec, DiagID,
John McCallba7bf592010-08-24 05:47:05 +00002101 Type.get()))
Douglas Gregorba41d012010-04-24 16:38:41 +00002102 Diag(StartLoc, DiagID) << PrevSpec;
2103
2104 return;
2105 }
Mike Stump11289f42009-09-09 15:08:12 +00002106
John McCall48871652010-08-21 09:40:31 +00002107 if (!TagDecl) {
Douglas Gregorba41d012010-04-24 16:38:41 +00002108 // The action failed to produce an enumeration tag. If this is a
2109 // definition, consume the entire definition.
2110 if (Tok.is(tok::l_brace)) {
2111 ConsumeBrace();
2112 SkipUntil(tok::r_brace);
2113 }
2114
2115 DS.SetTypeSpecError();
2116 return;
2117 }
2118
Chris Lattner76c72282007-10-09 17:33:22 +00002119 if (Tok.is(tok::l_brace))
Chris Lattnerc1915e22007-01-25 07:29:02 +00002120 ParseEnumBody(StartLoc, TagDecl);
Mike Stump11289f42009-09-09 15:08:12 +00002121
John McCallba7bf592010-08-24 05:47:05 +00002122 // FIXME: The DeclSpec should keep the locations of both the keyword
2123 // and the name (if there is one).
Douglas Gregor72100632010-01-25 16:33:23 +00002124 if (DS.SetTypeSpecType(DeclSpec::TST_enum, TSTLoc, PrevSpec, DiagID,
John McCall48871652010-08-21 09:40:31 +00002125 TagDecl, Owned))
John McCall49bfce42009-08-03 20:12:06 +00002126 Diag(StartLoc, DiagID) << PrevSpec;
Chris Lattner3b561a32006-08-13 00:12:11 +00002127}
2128
Chris Lattnerc1915e22007-01-25 07:29:02 +00002129/// ParseEnumBody - Parse a {} enclosed enumerator-list.
2130/// enumerator-list:
2131/// enumerator
2132/// enumerator-list ',' enumerator
2133/// enumerator:
2134/// enumeration-constant
2135/// enumeration-constant '=' constant-expression
2136/// enumeration-constant:
2137/// identifier
2138///
John McCall48871652010-08-21 09:40:31 +00002139void Parser::ParseEnumBody(SourceLocation StartLoc, Decl *EnumDecl) {
Douglas Gregor07665a62009-01-05 19:45:36 +00002140 // Enter the scope of the enum body and start the definition.
2141 ParseScope EnumScope(this, Scope::DeclScope);
Douglas Gregor0be31a22010-07-02 17:43:08 +00002142 Actions.ActOnTagStartDefinition(getCurScope(), EnumDecl);
Douglas Gregor07665a62009-01-05 19:45:36 +00002143
Chris Lattnerc1915e22007-01-25 07:29:02 +00002144 SourceLocation LBraceLoc = ConsumeBrace();
Mike Stump11289f42009-09-09 15:08:12 +00002145
Chris Lattner37256fb2007-08-27 17:24:30 +00002146 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
Chris Lattner76c72282007-10-09 17:33:22 +00002147 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Fariborz Jahanian6e814922010-05-28 22:23:22 +00002148 Diag(Tok, diag::error_empty_enum);
Mike Stump11289f42009-09-09 15:08:12 +00002149
John McCall48871652010-08-21 09:40:31 +00002150 llvm::SmallVector<Decl *, 32> EnumConstantDecls;
Chris Lattnerc1915e22007-01-25 07:29:02 +00002151
John McCall48871652010-08-21 09:40:31 +00002152 Decl *LastEnumConstDecl = 0;
Mike Stump11289f42009-09-09 15:08:12 +00002153
Chris Lattnerc1915e22007-01-25 07:29:02 +00002154 // Parse the enumerator-list.
Chris Lattner76c72282007-10-09 17:33:22 +00002155 while (Tok.is(tok::identifier)) {
Chris Lattnerc1915e22007-01-25 07:29:02 +00002156 IdentifierInfo *Ident = Tok.getIdentifierInfo();
2157 SourceLocation IdentLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00002158
John McCall811a0f52010-10-22 23:36:17 +00002159 // If attributes exist after the enumerator, parse them.
John McCall53fa7142010-12-24 02:08:15 +00002160 ParsedAttributes attrs;
2161 MaybeParseGNUAttributes(attrs);
John McCall811a0f52010-10-22 23:36:17 +00002162
Chris Lattnerc1915e22007-01-25 07:29:02 +00002163 SourceLocation EqualLoc;
John McCalldadc5752010-08-24 06:29:42 +00002164 ExprResult AssignedVal;
Chris Lattner76c72282007-10-09 17:33:22 +00002165 if (Tok.is(tok::equal)) {
Chris Lattnerc1915e22007-01-25 07:29:02 +00002166 EqualLoc = ConsumeToken();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002167 AssignedVal = ParseConstantExpression();
2168 if (AssignedVal.isInvalid())
Chris Lattnerda6c2ce2007-04-27 19:13:15 +00002169 SkipUntil(tok::comma, tok::r_brace, true, true);
Chris Lattnerc1915e22007-01-25 07:29:02 +00002170 }
Mike Stump11289f42009-09-09 15:08:12 +00002171
Chris Lattnerc1915e22007-01-25 07:29:02 +00002172 // Install the enumerator constant into EnumDecl.
John McCall48871652010-08-21 09:40:31 +00002173 Decl *EnumConstDecl = Actions.ActOnEnumConstant(getCurScope(), EnumDecl,
2174 LastEnumConstDecl,
2175 IdentLoc, Ident,
John McCall53fa7142010-12-24 02:08:15 +00002176 attrs.getList(), EqualLoc,
John McCall48871652010-08-21 09:40:31 +00002177 AssignedVal.release());
Chris Lattner4ef40012007-06-11 01:28:17 +00002178 EnumConstantDecls.push_back(EnumConstDecl);
2179 LastEnumConstDecl = EnumConstDecl;
Mike Stump11289f42009-09-09 15:08:12 +00002180
Douglas Gregorce66d022010-09-07 14:51:08 +00002181 if (Tok.is(tok::identifier)) {
2182 // We're missing a comma between enumerators.
2183 SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation);
2184 Diag(Loc, diag::err_enumerator_list_missing_comma)
2185 << FixItHint::CreateInsertion(Loc, ", ");
2186 continue;
2187 }
2188
Chris Lattner76c72282007-10-09 17:33:22 +00002189 if (Tok.isNot(tok::comma))
Chris Lattnerc1915e22007-01-25 07:29:02 +00002190 break;
2191 SourceLocation CommaLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00002192
2193 if (Tok.isNot(tok::identifier) &&
Douglas Gregore3e01a22009-04-01 22:41:11 +00002194 !(getLang().C99 || getLang().CPlusPlus0x))
2195 Diag(CommaLoc, diag::ext_enumerator_list_comma)
2196 << getLang().CPlusPlus
Douglas Gregora771f462010-03-31 17:46:05 +00002197 << FixItHint::CreateRemoval(CommaLoc);
Chris Lattnerc1915e22007-01-25 07:29:02 +00002198 }
Mike Stump11289f42009-09-09 15:08:12 +00002199
Chris Lattnerc1915e22007-01-25 07:29:02 +00002200 // Eat the }.
Mike Stump6814d1c2009-05-16 07:06:02 +00002201 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Chris Lattnerc1915e22007-01-25 07:29:02 +00002202
Chris Lattnerc1915e22007-01-25 07:29:02 +00002203 // If attributes exist after the identifier list, parse them.
John McCall53fa7142010-12-24 02:08:15 +00002204 ParsedAttributes attrs;
2205 MaybeParseGNUAttributes(attrs);
Douglas Gregor82ac25e2009-01-08 20:45:30 +00002206
Edward O'Callaghanc69169d2009-08-08 14:36:57 +00002207 Actions.ActOnEnumBody(StartLoc, LBraceLoc, RBraceLoc, EnumDecl,
2208 EnumConstantDecls.data(), EnumConstantDecls.size(),
John McCall53fa7142010-12-24 02:08:15 +00002209 getCurScope(), attrs.getList());
Mike Stump11289f42009-09-09 15:08:12 +00002210
Douglas Gregor82ac25e2009-01-08 20:45:30 +00002211 EnumScope.Exit();
Douglas Gregor0be31a22010-07-02 17:43:08 +00002212 Actions.ActOnTagFinishDefinition(getCurScope(), EnumDecl, RBraceLoc);
Chris Lattnerc1915e22007-01-25 07:29:02 +00002213}
Chris Lattner3b561a32006-08-13 00:12:11 +00002214
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002215/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff69e8f9e2008-02-11 23:15:56 +00002216/// start of a type-qualifier-list.
2217bool Parser::isTypeQualifier() const {
2218 switch (Tok.getKind()) {
2219 default: return false;
2220 // type-qualifier
2221 case tok::kw_const:
2222 case tok::kw_volatile:
2223 case tok::kw_restrict:
2224 return true;
2225 }
2226}
2227
Chris Lattnerfd48afe2010-02-28 18:18:36 +00002228/// isKnownToBeTypeSpecifier - Return true if we know that the specified token
2229/// is definitely a type-specifier. Return false if it isn't part of a type
2230/// specifier or if we're not sure.
2231bool Parser::isKnownToBeTypeSpecifier(const Token &Tok) const {
2232 switch (Tok.getKind()) {
2233 default: return false;
2234 // type-specifiers
2235 case tok::kw_short:
2236 case tok::kw_long:
2237 case tok::kw_signed:
2238 case tok::kw_unsigned:
2239 case tok::kw__Complex:
2240 case tok::kw__Imaginary:
2241 case tok::kw_void:
2242 case tok::kw_char:
2243 case tok::kw_wchar_t:
2244 case tok::kw_char16_t:
2245 case tok::kw_char32_t:
2246 case tok::kw_int:
2247 case tok::kw_float:
2248 case tok::kw_double:
2249 case tok::kw_bool:
2250 case tok::kw__Bool:
2251 case tok::kw__Decimal32:
2252 case tok::kw__Decimal64:
2253 case tok::kw__Decimal128:
2254 case tok::kw___vector:
2255
2256 // struct-or-union-specifier (C99) or class-specifier (C++)
2257 case tok::kw_class:
2258 case tok::kw_struct:
2259 case tok::kw_union:
2260 // enum-specifier
2261 case tok::kw_enum:
2262
2263 // typedef-name
2264 case tok::annot_typename:
2265 return true;
2266 }
2267}
2268
Steve Naroff69e8f9e2008-02-11 23:15:56 +00002269/// isTypeSpecifierQualifier - Return true if the current token could be the
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002270/// start of a specifier-qualifier-list.
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002271bool Parser::isTypeSpecifierQualifier() {
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002272 switch (Tok.getKind()) {
2273 default: return false;
Mike Stump11289f42009-09-09 15:08:12 +00002274
Chris Lattner020bab92009-01-04 23:41:41 +00002275 case tok::identifier: // foo::bar
John Thompson22334602010-02-05 00:12:22 +00002276 if (TryAltiVecVectorToken())
2277 return true;
2278 // Fall through.
Douglas Gregor333489b2009-03-27 23:10:48 +00002279 case tok::kw_typename: // typename T::type
Chris Lattner020bab92009-01-04 23:41:41 +00002280 // Annotate typenames and C++ scope specifiers. If we get one, just
2281 // recurse to handle whatever we get.
2282 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00002283 return true;
2284 if (Tok.is(tok::identifier))
2285 return false;
2286 return isTypeSpecifierQualifier();
Douglas Gregor333489b2009-03-27 23:10:48 +00002287
Chris Lattner020bab92009-01-04 23:41:41 +00002288 case tok::coloncolon: // ::foo::bar
2289 if (NextToken().is(tok::kw_new) || // ::new
2290 NextToken().is(tok::kw_delete)) // ::delete
2291 return false;
2292
Chris Lattner020bab92009-01-04 23:41:41 +00002293 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00002294 return true;
2295 return isTypeSpecifierQualifier();
Mike Stump11289f42009-09-09 15:08:12 +00002296
Chris Lattnere37e2332006-08-15 04:50:22 +00002297 // GNU attributes support.
2298 case tok::kw___attribute:
Steve Naroffad373bd2007-07-31 12:34:36 +00002299 // GNU typeof support.
2300 case tok::kw_typeof:
Mike Stump11289f42009-09-09 15:08:12 +00002301
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002302 // type-specifiers
2303 case tok::kw_short:
2304 case tok::kw_long:
2305 case tok::kw_signed:
2306 case tok::kw_unsigned:
2307 case tok::kw__Complex:
2308 case tok::kw__Imaginary:
2309 case tok::kw_void:
2310 case tok::kw_char:
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +00002311 case tok::kw_wchar_t:
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00002312 case tok::kw_char16_t:
2313 case tok::kw_char32_t:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002314 case tok::kw_int:
2315 case tok::kw_float:
2316 case tok::kw_double:
Chris Lattnerbb31a422007-11-15 05:25:19 +00002317 case tok::kw_bool:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002318 case tok::kw__Bool:
2319 case tok::kw__Decimal32:
2320 case tok::kw__Decimal64:
2321 case tok::kw__Decimal128:
John Thompson22334602010-02-05 00:12:22 +00002322 case tok::kw___vector:
Mike Stump11289f42009-09-09 15:08:12 +00002323
Chris Lattner861a2262008-04-13 18:59:07 +00002324 // struct-or-union-specifier (C99) or class-specifier (C++)
2325 case tok::kw_class:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002326 case tok::kw_struct:
2327 case tok::kw_union:
2328 // enum-specifier
2329 case tok::kw_enum:
Mike Stump11289f42009-09-09 15:08:12 +00002330
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002331 // type-qualifier
2332 case tok::kw_const:
2333 case tok::kw_volatile:
2334 case tok::kw_restrict:
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002335
2336 // typedef-name
Chris Lattnera8a3f732009-01-06 05:06:21 +00002337 case tok::annot_typename:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002338 return true;
Mike Stump11289f42009-09-09 15:08:12 +00002339
Chris Lattner409bf7d2008-10-20 00:25:30 +00002340 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
2341 case tok::less:
2342 return getLang().ObjC1;
Mike Stump11289f42009-09-09 15:08:12 +00002343
Steve Naroff44ac7772008-12-25 14:16:32 +00002344 case tok::kw___cdecl:
2345 case tok::kw___stdcall:
2346 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00002347 case tok::kw___thiscall:
Eli Friedman53339e02009-06-08 23:27:34 +00002348 case tok::kw___w64:
2349 case tok::kw___ptr64:
Dawn Perchik335e16b2010-09-03 01:29:35 +00002350 case tok::kw___pascal:
Eli Friedman53339e02009-06-08 23:27:34 +00002351 return true;
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002352 }
2353}
2354
Chris Lattneracd58a32006-08-06 17:24:14 +00002355/// isDeclarationSpecifier() - Return true if the current token is part of a
2356/// declaration specifier.
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00002357///
2358/// \param DisambiguatingWithExpression True to indicate that the purpose of
2359/// this check is to disambiguate between an expression and a declaration.
2360bool Parser::isDeclarationSpecifier(bool DisambiguatingWithExpression) {
Chris Lattneracd58a32006-08-06 17:24:14 +00002361 switch (Tok.getKind()) {
2362 default: return false;
Mike Stump11289f42009-09-09 15:08:12 +00002363
Chris Lattner020bab92009-01-04 23:41:41 +00002364 case tok::identifier: // foo::bar
Steve Naroff9527bbf2009-03-09 21:12:44 +00002365 // Unfortunate hack to support "Class.factoryMethod" notation.
2366 if (getLang().ObjC1 && NextToken().is(tok::period))
2367 return false;
John Thompson22334602010-02-05 00:12:22 +00002368 if (TryAltiVecVectorToken())
2369 return true;
2370 // Fall through.
Douglas Gregor333489b2009-03-27 23:10:48 +00002371 case tok::kw_typename: // typename T::type
Chris Lattner020bab92009-01-04 23:41:41 +00002372 // Annotate typenames and C++ scope specifiers. If we get one, just
2373 // recurse to handle whatever we get.
2374 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00002375 return true;
2376 if (Tok.is(tok::identifier))
2377 return false;
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00002378
2379 // If we're in Objective-C and we have an Objective-C class type followed
2380 // by an identifier and then either ':' or ']', in a place where an
2381 // expression is permitted, then this is probably a class message send
2382 // missing the initial '['. In this case, we won't consider this to be
2383 // the start of a declaration.
2384 if (DisambiguatingWithExpression &&
2385 isStartOfObjCClassMessageMissingOpenBracket())
2386 return false;
2387
John McCall1f476a12010-02-26 08:45:28 +00002388 return isDeclarationSpecifier();
2389
Chris Lattner020bab92009-01-04 23:41:41 +00002390 case tok::coloncolon: // ::foo::bar
2391 if (NextToken().is(tok::kw_new) || // ::new
2392 NextToken().is(tok::kw_delete)) // ::delete
2393 return false;
Mike Stump11289f42009-09-09 15:08:12 +00002394
Chris Lattner020bab92009-01-04 23:41:41 +00002395 // Annotate typenames and C++ scope specifiers. If we get one, just
2396 // recurse to handle whatever we get.
2397 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00002398 return true;
2399 return isDeclarationSpecifier();
Mike Stump11289f42009-09-09 15:08:12 +00002400
Chris Lattneracd58a32006-08-06 17:24:14 +00002401 // storage-class-specifier
2402 case tok::kw_typedef:
2403 case tok::kw_extern:
Steve Naroff2050b0d2007-12-18 00:16:02 +00002404 case tok::kw___private_extern__:
Chris Lattneracd58a32006-08-06 17:24:14 +00002405 case tok::kw_static:
2406 case tok::kw_auto:
2407 case tok::kw_register:
2408 case tok::kw___thread:
Mike Stump11289f42009-09-09 15:08:12 +00002409
Chris Lattneracd58a32006-08-06 17:24:14 +00002410 // type-specifiers
2411 case tok::kw_short:
2412 case tok::kw_long:
2413 case tok::kw_signed:
2414 case tok::kw_unsigned:
2415 case tok::kw__Complex:
2416 case tok::kw__Imaginary:
2417 case tok::kw_void:
2418 case tok::kw_char:
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +00002419 case tok::kw_wchar_t:
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00002420 case tok::kw_char16_t:
2421 case tok::kw_char32_t:
2422
Chris Lattneracd58a32006-08-06 17:24:14 +00002423 case tok::kw_int:
2424 case tok::kw_float:
2425 case tok::kw_double:
Chris Lattnerbb31a422007-11-15 05:25:19 +00002426 case tok::kw_bool:
Chris Lattneracd58a32006-08-06 17:24:14 +00002427 case tok::kw__Bool:
2428 case tok::kw__Decimal32:
2429 case tok::kw__Decimal64:
2430 case tok::kw__Decimal128:
John Thompson22334602010-02-05 00:12:22 +00002431 case tok::kw___vector:
Mike Stump11289f42009-09-09 15:08:12 +00002432
Chris Lattner861a2262008-04-13 18:59:07 +00002433 // struct-or-union-specifier (C99) or class-specifier (C++)
2434 case tok::kw_class:
Chris Lattneracd58a32006-08-06 17:24:14 +00002435 case tok::kw_struct:
2436 case tok::kw_union:
2437 // enum-specifier
2438 case tok::kw_enum:
Mike Stump11289f42009-09-09 15:08:12 +00002439
Chris Lattneracd58a32006-08-06 17:24:14 +00002440 // type-qualifier
2441 case tok::kw_const:
2442 case tok::kw_volatile:
2443 case tok::kw_restrict:
Steve Naroffad373bd2007-07-31 12:34:36 +00002444
Chris Lattneracd58a32006-08-06 17:24:14 +00002445 // function-specifier
2446 case tok::kw_inline:
Douglas Gregor61956c42008-10-31 09:07:45 +00002447 case tok::kw_virtual:
2448 case tok::kw_explicit:
Chris Lattner7b20dc72007-08-09 16:40:21 +00002449
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002450 // typedef-name
Chris Lattnera8a3f732009-01-06 05:06:21 +00002451 case tok::annot_typename:
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002452
Chris Lattner599e47e2007-08-09 17:01:07 +00002453 // GNU typeof support.
2454 case tok::kw_typeof:
Mike Stump11289f42009-09-09 15:08:12 +00002455
Chris Lattner599e47e2007-08-09 17:01:07 +00002456 // GNU attributes.
Chris Lattner7b20dc72007-08-09 16:40:21 +00002457 case tok::kw___attribute:
Chris Lattneracd58a32006-08-06 17:24:14 +00002458 return true;
Mike Stump11289f42009-09-09 15:08:12 +00002459
Chris Lattner8b2ec162008-07-26 03:38:44 +00002460 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
2461 case tok::less:
2462 return getLang().ObjC1;
Mike Stump11289f42009-09-09 15:08:12 +00002463
Steve Narofff192fab2009-01-06 19:34:12 +00002464 case tok::kw___declspec:
Steve Naroff44ac7772008-12-25 14:16:32 +00002465 case tok::kw___cdecl:
2466 case tok::kw___stdcall:
2467 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00002468 case tok::kw___thiscall:
Eli Friedman53339e02009-06-08 23:27:34 +00002469 case tok::kw___w64:
2470 case tok::kw___ptr64:
2471 case tok::kw___forceinline:
Dawn Perchik335e16b2010-09-03 01:29:35 +00002472 case tok::kw___pascal:
Eli Friedman53339e02009-06-08 23:27:34 +00002473 return true;
Chris Lattneracd58a32006-08-06 17:24:14 +00002474 }
2475}
2476
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002477bool Parser::isConstructorDeclarator() {
2478 TentativeParsingAction TPA(*this);
2479
2480 // Parse the C++ scope specifier.
2481 CXXScopeSpec SS;
John McCallba7bf592010-08-24 05:47:05 +00002482 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), true)) {
John McCall1f476a12010-02-26 08:45:28 +00002483 TPA.Revert();
2484 return false;
2485 }
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002486
2487 // Parse the constructor name.
2488 if (Tok.is(tok::identifier) || Tok.is(tok::annot_template_id)) {
2489 // We already know that we have a constructor name; just consume
2490 // the token.
2491 ConsumeToken();
2492 } else {
2493 TPA.Revert();
2494 return false;
2495 }
2496
2497 // Current class name must be followed by a left parentheses.
2498 if (Tok.isNot(tok::l_paren)) {
2499 TPA.Revert();
2500 return false;
2501 }
2502 ConsumeParen();
2503
2504 // A right parentheses or ellipsis signals that we have a constructor.
2505 if (Tok.is(tok::r_paren) || Tok.is(tok::ellipsis)) {
2506 TPA.Revert();
2507 return true;
2508 }
2509
2510 // If we need to, enter the specified scope.
2511 DeclaratorScopeObj DeclScopeObj(*this, SS);
Douglas Gregor0be31a22010-07-02 17:43:08 +00002512 if (SS.isSet() && Actions.ShouldEnterDeclaratorScope(getCurScope(), SS))
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002513 DeclScopeObj.EnterDeclaratorScope();
2514
Francois Pichet79f3a872011-01-31 04:54:32 +00002515 // Optionally skip Microsoft attributes.
2516 ParsedAttributes Attrs;
2517 MaybeParseMicrosoftAttributes(Attrs);
2518
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002519 // Check whether the next token(s) are part of a declaration
2520 // specifier, in which case we have the start of a parameter and,
2521 // therefore, we know that this is a constructor.
2522 bool IsConstructor = isDeclarationSpecifier();
2523 TPA.Revert();
2524 return IsConstructor;
2525}
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002526
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002527/// ParseTypeQualifierListOpt
Dawn Perchik335e16b2010-09-03 01:29:35 +00002528/// type-qualifier-list: [C99 6.7.5]
2529/// type-qualifier
2530/// [vendor] attributes
2531/// [ only if VendorAttributesAllowed=true ]
2532/// type-qualifier-list type-qualifier
2533/// [vendor] type-qualifier-list attributes
2534/// [ only if VendorAttributesAllowed=true ]
2535/// [C++0x] attribute-specifier[opt] is allowed before cv-qualifier-seq
2536/// [ only if CXX0XAttributesAllowed=true ]
2537/// Note: vendor can be GNU, MS, etc.
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002538///
Dawn Perchik335e16b2010-09-03 01:29:35 +00002539void Parser::ParseTypeQualifierListOpt(DeclSpec &DS,
2540 bool VendorAttributesAllowed,
Alexis Hunt96d5c762009-11-21 08:43:09 +00002541 bool CXX0XAttributesAllowed) {
2542 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier()) {
2543 SourceLocation Loc = Tok.getLocation();
John McCall53fa7142010-12-24 02:08:15 +00002544 ParsedAttributesWithRange attrs;
2545 ParseCXX0XAttributes(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +00002546 if (CXX0XAttributesAllowed)
John McCall53fa7142010-12-24 02:08:15 +00002547 DS.takeAttributesFrom(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +00002548 else
2549 Diag(Loc, diag::err_attributes_not_allowed);
2550 }
2551
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002552 while (1) {
John McCall49bfce42009-08-03 20:12:06 +00002553 bool isInvalid = false;
Chris Lattnerd9c3c592006-08-05 06:26:47 +00002554 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00002555 unsigned DiagID = 0;
Chris Lattner60809f52006-11-28 05:18:46 +00002556 SourceLocation Loc = Tok.getLocation();
Chris Lattnerd9c3c592006-08-05 06:26:47 +00002557
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002558 switch (Tok.getKind()) {
Douglas Gregor28c78432010-08-27 17:35:51 +00002559 case tok::code_completion:
2560 Actions.CodeCompleteTypeQualifiers(DS);
2561 ConsumeCodeCompletionToken();
2562 break;
2563
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002564 case tok::kw_const:
John McCall49bfce42009-08-03 20:12:06 +00002565 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, DiagID,
2566 getLang());
Chris Lattnerd9c3c592006-08-05 06:26:47 +00002567 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002568 case tok::kw_volatile:
John McCall49bfce42009-08-03 20:12:06 +00002569 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
2570 getLang());
Chris Lattnerd9c3c592006-08-05 06:26:47 +00002571 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002572 case tok::kw_restrict:
John McCall49bfce42009-08-03 20:12:06 +00002573 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
2574 getLang());
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002575 break;
Eli Friedman53339e02009-06-08 23:27:34 +00002576 case tok::kw___w64:
Steve Narofff9c29d42008-12-25 14:41:26 +00002577 case tok::kw___ptr64:
Steve Naroff44ac7772008-12-25 14:16:32 +00002578 case tok::kw___cdecl:
2579 case tok::kw___stdcall:
2580 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00002581 case tok::kw___thiscall:
Dawn Perchik335e16b2010-09-03 01:29:35 +00002582 if (VendorAttributesAllowed) {
John McCall53fa7142010-12-24 02:08:15 +00002583 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman53339e02009-06-08 23:27:34 +00002584 continue;
2585 }
2586 goto DoneWithTypeQuals;
Dawn Perchik335e16b2010-09-03 01:29:35 +00002587 case tok::kw___pascal:
2588 if (VendorAttributesAllowed) {
John McCall53fa7142010-12-24 02:08:15 +00002589 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik335e16b2010-09-03 01:29:35 +00002590 continue;
2591 }
2592 goto DoneWithTypeQuals;
Chris Lattnere37e2332006-08-15 04:50:22 +00002593 case tok::kw___attribute:
Dawn Perchik335e16b2010-09-03 01:29:35 +00002594 if (VendorAttributesAllowed) {
John McCall53fa7142010-12-24 02:08:15 +00002595 ParseGNUAttributes(DS.getAttributes());
Chris Lattnercf0bab22008-12-18 07:02:59 +00002596 continue; // do *not* consume the next token!
2597 }
2598 // otherwise, FALL THROUGH!
2599 default:
Steve Naroff44ac7772008-12-25 14:16:32 +00002600 DoneWithTypeQuals:
Chris Lattnercf0bab22008-12-18 07:02:59 +00002601 // If this is not a type-qualifier token, we're done reading type
2602 // qualifiers. First verify that DeclSpec's are consistent.
Douglas Gregore3e01a22009-04-01 22:41:11 +00002603 DS.Finish(Diags, PP);
Chris Lattnercf0bab22008-12-18 07:02:59 +00002604 return;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002605 }
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00002606
Chris Lattnerd9c3c592006-08-05 06:26:47 +00002607 // If the specifier combination wasn't legal, issue a diagnostic.
2608 if (isInvalid) {
2609 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner6d29c102008-11-18 07:48:38 +00002610 Diag(Tok, DiagID) << PrevSpec;
Chris Lattnerd9c3c592006-08-05 06:26:47 +00002611 }
2612 ConsumeToken();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002613 }
2614}
2615
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00002616
2617/// ParseDeclarator - Parse and verify a newly-initialized declarator.
2618///
2619void Parser::ParseDeclarator(Declarator &D) {
2620 /// This implements the 'declarator' production in the C grammar, then checks
2621 /// for well-formedness and issues diagnostics.
Sebastian Redlbd150f42008-11-21 19:14:01 +00002622 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00002623}
2624
Sebastian Redlbd150f42008-11-21 19:14:01 +00002625/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
2626/// is parsed by the function passed to it. Pass null, and the direct-declarator
2627/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregordbc5daf2008-11-07 20:08:42 +00002628/// ptr-operator production.
2629///
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002630/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
2631/// [C] pointer[opt] direct-declarator
2632/// [C++] direct-declarator
2633/// [C++] ptr-operator declarator
Chris Lattner6c7416c2006-08-07 00:19:33 +00002634///
2635/// pointer: [C99 6.7.5]
2636/// '*' type-qualifier-list[opt]
2637/// '*' type-qualifier-list[opt] pointer
2638///
Douglas Gregordbc5daf2008-11-07 20:08:42 +00002639/// ptr-operator:
2640/// '*' cv-qualifier-seq[opt]
2641/// '&'
Sebastian Redled0f3b02009-03-15 22:02:01 +00002642/// [C++0x] '&&'
Douglas Gregordbc5daf2008-11-07 20:08:42 +00002643/// [GNU] '&' restrict[opt] attributes[opt]
Sebastian Redled0f3b02009-03-15 22:02:01 +00002644/// [GNU?] '&&' restrict[opt] attributes[opt]
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002645/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Sebastian Redlbd150f42008-11-21 19:14:01 +00002646void Parser::ParseDeclaratorInternal(Declarator &D,
2647 DirectDeclParseFunction DirectDeclParser) {
Douglas Gregor66a985d2009-08-26 14:27:30 +00002648 if (Diags.hasAllExtensionsSilenced())
2649 D.setExtension();
Douglas Gregorc49f5b22010-08-23 18:23:48 +00002650
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002651 // C++ member pointers start with a '::' or a nested-name.
2652 // Member pointers get special handling, since there's no place for the
2653 // scope spec in the generic path below.
Chris Lattner803802d2009-03-24 17:04:48 +00002654 if (getLang().CPlusPlus &&
2655 (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) ||
2656 Tok.is(tok::annot_cxxscope))) {
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002657 CXXScopeSpec SS;
John McCallba7bf592010-08-24 05:47:05 +00002658 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), true); // ignore fail
John McCall1f476a12010-02-26 08:45:28 +00002659
Jeffrey Yasskin4e150f82010-04-07 23:29:58 +00002660 if (SS.isNotEmpty()) {
Mike Stump11289f42009-09-09 15:08:12 +00002661 if (Tok.isNot(tok::star)) {
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002662 // The scope spec really belongs to the direct-declarator.
2663 D.getCXXScopeSpec() = SS;
2664 if (DirectDeclParser)
2665 (this->*DirectDeclParser)(D);
2666 return;
2667 }
2668
2669 SourceLocation Loc = ConsumeToken();
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002670 D.SetRangeEnd(Loc);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002671 DeclSpec DS;
2672 ParseTypeQualifierListOpt(DS);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002673 D.ExtendWithDeclSpec(DS);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002674
2675 // Recurse to parse whatever is left.
2676 ParseDeclaratorInternal(D, DirectDeclParser);
2677
2678 // Sema will have to catch (syntactically invalid) pointers into global
2679 // scope. It has to catch pointers into namespace scope anyway.
2680 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
John McCall53fa7142010-12-24 02:08:15 +00002681 Loc, DS.takeAttributes()),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002682 /* Don't replace range end. */SourceLocation());
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002683 return;
2684 }
2685 }
2686
2687 tok::TokenKind Kind = Tok.getKind();
Steve Naroffec33ed92008-08-27 16:04:49 +00002688 // Not a pointer, C++ reference, or block.
Chris Lattner9eac9312009-03-27 04:18:06 +00002689 if (Kind != tok::star && Kind != tok::caret &&
Chris Lattner803802d2009-03-24 17:04:48 +00002690 (Kind != tok::amp || !getLang().CPlusPlus) &&
Sebastian Redl3b27be62009-03-23 00:00:23 +00002691 // We parse rvalue refs in C++03, because otherwise the errors are scary.
Chris Lattner9eac9312009-03-27 04:18:06 +00002692 (Kind != tok::ampamp || !getLang().CPlusPlus)) {
Sebastian Redlbd150f42008-11-21 19:14:01 +00002693 if (DirectDeclParser)
2694 (this->*DirectDeclParser)(D);
Douglas Gregordbc5daf2008-11-07 20:08:42 +00002695 return;
2696 }
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002697
Sebastian Redled0f3b02009-03-15 22:02:01 +00002698 // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference,
2699 // '&&' -> rvalue reference
Sebastian Redl3b27be62009-03-23 00:00:23 +00002700 SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&.
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002701 D.SetRangeEnd(Loc);
Bill Wendling3708c182007-05-27 10:15:43 +00002702
Chris Lattner9eac9312009-03-27 04:18:06 +00002703 if (Kind == tok::star || Kind == tok::caret) {
Chris Lattner788404f2008-02-21 01:32:26 +00002704 // Is a pointer.
Bill Wendling3708c182007-05-27 10:15:43 +00002705 DeclSpec DS;
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002706
Bill Wendling3708c182007-05-27 10:15:43 +00002707 ParseTypeQualifierListOpt(DS);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002708 D.ExtendWithDeclSpec(DS);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002709
Bill Wendling3708c182007-05-27 10:15:43 +00002710 // Recursively parse the declarator.
Sebastian Redlbd150f42008-11-21 19:14:01 +00002711 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroffec33ed92008-08-27 16:04:49 +00002712 if (Kind == tok::star)
2713 // Remember that we parsed a pointer type, and remember the type-quals.
2714 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
John McCall53fa7142010-12-24 02:08:15 +00002715 DS.takeAttributes()),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002716 SourceLocation());
Steve Naroffec33ed92008-08-27 16:04:49 +00002717 else
2718 // Remember that we parsed a Block type, and remember the type-quals.
Mike Stump11289f42009-09-09 15:08:12 +00002719 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
John McCall53fa7142010-12-24 02:08:15 +00002720 Loc, DS.takeAttributes()),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002721 SourceLocation());
Bill Wendling3708c182007-05-27 10:15:43 +00002722 } else {
2723 // Is a reference
Bill Wendling93efb222007-06-02 23:28:54 +00002724 DeclSpec DS;
2725
Sebastian Redl3b27be62009-03-23 00:00:23 +00002726 // Complain about rvalue references in C++03, but then go on and build
2727 // the declarator.
2728 if (Kind == tok::ampamp && !getLang().CPlusPlus0x)
Douglas Gregor00984992011-01-25 02:17:32 +00002729 Diag(Loc, diag::ext_rvalue_reference);
Sebastian Redl3b27be62009-03-23 00:00:23 +00002730
Bill Wendling93efb222007-06-02 23:28:54 +00002731 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
2732 // cv-qualifiers are introduced through the use of a typedef or of a
2733 // template type argument, in which case the cv-qualifiers are ignored.
2734 //
2735 // [GNU] Retricted references are allowed.
2736 // [GNU] Attributes on references are allowed.
Alexis Hunt96d5c762009-11-21 08:43:09 +00002737 // [C++0x] Attributes on references are not allowed.
2738 ParseTypeQualifierListOpt(DS, true, false);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002739 D.ExtendWithDeclSpec(DS);
Bill Wendling93efb222007-06-02 23:28:54 +00002740
2741 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
2742 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
2743 Diag(DS.getConstSpecLoc(),
Chris Lattner6d29c102008-11-18 07:48:38 +00002744 diag::err_invalid_reference_qualifier_application) << "const";
Bill Wendling93efb222007-06-02 23:28:54 +00002745 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
2746 Diag(DS.getVolatileSpecLoc(),
Chris Lattner6d29c102008-11-18 07:48:38 +00002747 diag::err_invalid_reference_qualifier_application) << "volatile";
Bill Wendling93efb222007-06-02 23:28:54 +00002748 }
Bill Wendling3708c182007-05-27 10:15:43 +00002749
2750 // Recursively parse the declarator.
Sebastian Redlbd150f42008-11-21 19:14:01 +00002751 ParseDeclaratorInternal(D, DirectDeclParser);
Bill Wendling3708c182007-05-27 10:15:43 +00002752
Douglas Gregor66583c52008-11-03 15:51:28 +00002753 if (D.getNumTypeObjects() > 0) {
2754 // C++ [dcl.ref]p4: There shall be no references to references.
2755 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
2756 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattnerebad6a22008-11-19 07:37:42 +00002757 if (const IdentifierInfo *II = D.getIdentifier())
2758 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
2759 << II;
2760 else
2761 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
2762 << "type name";
Douglas Gregor66583c52008-11-03 15:51:28 +00002763
Sebastian Redlbd150f42008-11-21 19:14:01 +00002764 // Once we've complained about the reference-to-reference, we
Douglas Gregor66583c52008-11-03 15:51:28 +00002765 // can go ahead and build the (technically ill-formed)
2766 // declarator: reference collapsing will take care of it.
2767 }
2768 }
2769
Bill Wendling3708c182007-05-27 10:15:43 +00002770 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner788404f2008-02-21 01:32:26 +00002771 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
John McCall53fa7142010-12-24 02:08:15 +00002772 DS.takeAttributes(),
Sebastian Redled0f3b02009-03-15 22:02:01 +00002773 Kind == tok::amp),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002774 SourceLocation());
Bill Wendling3708c182007-05-27 10:15:43 +00002775 }
Chris Lattner6c7416c2006-08-07 00:19:33 +00002776}
2777
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002778/// ParseDirectDeclarator
2779/// direct-declarator: [C99 6.7.5]
Douglas Gregor831c93f2008-11-05 20:51:48 +00002780/// [C99] identifier
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002781/// '(' declarator ')'
2782/// [GNU] '(' attributes declarator ')'
Chris Lattnere8074e62006-08-06 18:30:15 +00002783/// [C90] direct-declarator '[' constant-expression[opt] ']'
2784/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
2785/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
2786/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
2787/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002788/// direct-declarator '(' parameter-type-list ')'
2789/// direct-declarator '(' identifier-list[opt] ')'
2790/// [GNU] direct-declarator '(' parameter-forward-declarations
2791/// parameter-type-list[opt] ')'
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00002792/// [C++] direct-declarator '(' parameter-declaration-clause ')'
2793/// cv-qualifier-seq[opt] exception-specification[opt]
Douglas Gregor61956c42008-10-31 09:07:45 +00002794/// [C++] declarator-id
Douglas Gregor831c93f2008-11-05 20:51:48 +00002795///
2796/// declarator-id: [C++ 8]
Douglas Gregor27b4c162010-12-23 22:44:42 +00002797/// '...'[opt] id-expression
Douglas Gregor831c93f2008-11-05 20:51:48 +00002798/// '::'[opt] nested-name-specifier[opt] type-name
2799///
2800/// id-expression: [C++ 5.1]
2801/// unqualified-id
Douglas Gregord90fd522009-09-25 21:45:23 +00002802/// qualified-id
Douglas Gregor831c93f2008-11-05 20:51:48 +00002803///
2804/// unqualified-id: [C++ 5.1]
Mike Stump11289f42009-09-09 15:08:12 +00002805/// identifier
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002806/// operator-function-id
Douglas Gregord90fd522009-09-25 21:45:23 +00002807/// conversion-function-id
Mike Stump11289f42009-09-09 15:08:12 +00002808/// '~' class-name
Douglas Gregor7f741122009-02-25 19:37:18 +00002809/// template-id
Argyrios Kyrtzidise4426352008-11-07 22:02:30 +00002810///
Chris Lattneracd58a32006-08-06 17:24:14 +00002811void Parser::ParseDirectDeclarator(Declarator &D) {
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00002812 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002813
Douglas Gregor7861a802009-11-03 01:35:08 +00002814 if (getLang().CPlusPlus && D.mayHaveIdentifier()) {
2815 // ParseDeclaratorInternal might already have parsed the scope.
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00002816 if (D.getCXXScopeSpec().isEmpty()) {
John McCallba7bf592010-08-24 05:47:05 +00002817 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec(), ParsedType(), true);
John McCall1f476a12010-02-26 08:45:28 +00002818 }
2819
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00002820 if (D.getCXXScopeSpec().isValid()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00002821 if (Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
John McCall2b058ef2009-12-11 20:04:54 +00002822 // Change the declaration context for name lookup, until this function
2823 // is exited (and the declarator has been parsed).
2824 DeclScopeObj.EnterDeclaratorScope();
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00002825 }
2826
Douglas Gregor27b4c162010-12-23 22:44:42 +00002827 // C++0x [dcl.fct]p14:
2828 // There is a syntactic ambiguity when an ellipsis occurs at the end
2829 // of a parameter-declaration-clause without a preceding comma. In
2830 // this case, the ellipsis is parsed as part of the
2831 // abstract-declarator if the type of the parameter names a template
2832 // parameter pack that has not been expanded; otherwise, it is parsed
2833 // as part of the parameter-declaration-clause.
2834 if (Tok.is(tok::ellipsis) &&
2835 !((D.getContext() == Declarator::PrototypeContext ||
2836 D.getContext() == Declarator::BlockLiteralContext) &&
Douglas Gregor27b4c162010-12-23 22:44:42 +00002837 NextToken().is(tok::r_paren) &&
2838 !Actions.containsUnexpandedParameterPacks(D)))
2839 D.setEllipsisLoc(ConsumeToken());
2840
Douglas Gregor7861a802009-11-03 01:35:08 +00002841 if (Tok.is(tok::identifier) || Tok.is(tok::kw_operator) ||
2842 Tok.is(tok::annot_template_id) || Tok.is(tok::tilde)) {
2843 // We found something that indicates the start of an unqualified-id.
2844 // Parse that unqualified-id.
John McCall84821e72010-04-13 06:39:49 +00002845 bool AllowConstructorName;
2846 if (D.getDeclSpec().hasTypeSpecifier())
2847 AllowConstructorName = false;
2848 else if (D.getCXXScopeSpec().isSet())
2849 AllowConstructorName =
2850 (D.getContext() == Declarator::FileContext ||
2851 (D.getContext() == Declarator::MemberContext &&
2852 D.getDeclSpec().isFriendSpecified()));
2853 else
2854 AllowConstructorName = (D.getContext() == Declarator::MemberContext);
2855
Douglas Gregor7861a802009-11-03 01:35:08 +00002856 if (ParseUnqualifiedId(D.getCXXScopeSpec(),
2857 /*EnteringContext=*/true,
2858 /*AllowDestructorName=*/true,
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002859 AllowConstructorName,
John McCallba7bf592010-08-24 05:47:05 +00002860 ParsedType(),
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00002861 D.getName()) ||
2862 // Once we're past the identifier, if the scope was bad, mark the
2863 // whole declarator bad.
2864 D.getCXXScopeSpec().isInvalid()) {
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00002865 D.SetIdentifier(0, Tok.getLocation());
2866 D.setInvalidType(true);
Douglas Gregor7861a802009-11-03 01:35:08 +00002867 } else {
2868 // Parsed the unqualified-id; update range information and move along.
2869 if (D.getSourceRange().getBegin().isInvalid())
2870 D.SetRangeBegin(D.getName().getSourceRange().getBegin());
2871 D.SetRangeEnd(D.getName().getSourceRange().getEnd());
Douglas Gregordbc5daf2008-11-07 20:08:42 +00002872 }
Douglas Gregor7861a802009-11-03 01:35:08 +00002873 goto PastIdentifier;
Douglas Gregor11d0c4c2008-11-06 22:13:31 +00002874 }
Douglas Gregor7861a802009-11-03 01:35:08 +00002875 } else if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00002876 assert(!getLang().CPlusPlus &&
2877 "There's a C++-specific check for tok::identifier above");
2878 assert(Tok.getIdentifierInfo() && "Not an identifier?");
2879 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
2880 ConsumeToken();
Douglas Gregor7861a802009-11-03 01:35:08 +00002881 goto PastIdentifier;
2882 }
2883
2884 if (Tok.is(tok::l_paren)) {
Chris Lattneracd58a32006-08-06 17:24:14 +00002885 // direct-declarator: '(' declarator ')'
Chris Lattnere37e2332006-08-15 04:50:22 +00002886 // direct-declarator: '(' attributes declarator ')'
Chris Lattneracd58a32006-08-06 17:24:14 +00002887 // Example: 'char (*X)' or 'int (*XX)(void)'
2888 ParseParenDeclarator(D);
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002889
2890 // If the declarator was parenthesized, we entered the declarator
2891 // scope when parsing the parenthesized declarator, then exited
2892 // the scope already. Re-enter the scope, if we need to.
2893 if (D.getCXXScopeSpec().isSet()) {
Fariborz Jahanian358acd52010-08-17 23:50:37 +00002894 // If there was an error parsing parenthesized declarator, declarator
2895 // scope may have been enterred before. Don't do it again.
2896 if (!D.isInvalidType() &&
2897 Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002898 // Change the declaration context for name lookup, until this function
2899 // is exited (and the declarator has been parsed).
Fariborz Jahanian358acd52010-08-17 23:50:37 +00002900 DeclScopeObj.EnterDeclaratorScope();
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002901 }
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00002902 } else if (D.mayOmitIdentifier()) {
Chris Lattneracd58a32006-08-06 17:24:14 +00002903 // This could be something simple like "int" (in which case the declarator
2904 // portion is empty), if an abstract-declarator is allowed.
2905 D.SetIdentifier(0, Tok.getLocation());
2906 } else {
Douglas Gregord9f92e22009-03-06 23:28:18 +00002907 if (D.getContext() == Declarator::MemberContext)
2908 Diag(Tok, diag::err_expected_member_name_or_semi)
2909 << D.getDeclSpec().getSourceRange();
2910 else if (getLang().CPlusPlus)
Douglas Gregor30d60cb2009-11-03 19:44:04 +00002911 Diag(Tok, diag::err_expected_unqualified_id) << getLang().CPlusPlus;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002912 else
Chris Lattner6d29c102008-11-18 07:48:38 +00002913 Diag(Tok, diag::err_expected_ident_lparen);
Chris Lattnereec40f92006-08-06 21:55:29 +00002914 D.SetIdentifier(0, Tok.getLocation());
Chris Lattner8c5dd732008-11-11 06:13:16 +00002915 D.setInvalidType(true);
Chris Lattneracd58a32006-08-06 17:24:14 +00002916 }
Mike Stump11289f42009-09-09 15:08:12 +00002917
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00002918 PastIdentifier:
Chris Lattneracd58a32006-08-06 17:24:14 +00002919 assert(D.isPastIdentifier() &&
2920 "Haven't past the location of the identifier yet?");
Mike Stump11289f42009-09-09 15:08:12 +00002921
Alexis Hunt96d5c762009-11-21 08:43:09 +00002922 // Don't parse attributes unless we have an identifier.
John McCall53fa7142010-12-24 02:08:15 +00002923 if (D.getIdentifier())
2924 MaybeParseCXX0XAttributes(D);
Alexis Hunt96d5c762009-11-21 08:43:09 +00002925
Chris Lattneracd58a32006-08-06 17:24:14 +00002926 while (1) {
Chris Lattner76c72282007-10-09 17:33:22 +00002927 if (Tok.is(tok::l_paren)) {
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00002928 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
2929 // In such a case, check if we actually have a function declarator; if it
2930 // is not, the declarator has been fully parsed.
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002931 if (getLang().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
2932 // When not in file scope, warn for ambiguous function declarators, just
2933 // in case the author intended it as a variable definition.
2934 bool warnIfAmbiguous = D.getContext() != Declarator::FileContext;
2935 if (!isCXXFunctionDeclarator(warnIfAmbiguous))
2936 break;
2937 }
John McCall53fa7142010-12-24 02:08:15 +00002938 ParsedAttributes attrs;
2939 ParseFunctionDeclarator(ConsumeParen(), D, attrs);
Chris Lattner76c72282007-10-09 17:33:22 +00002940 } else if (Tok.is(tok::l_square)) {
Chris Lattnere8074e62006-08-06 18:30:15 +00002941 ParseBracketDeclarator(D);
Chris Lattneracd58a32006-08-06 17:24:14 +00002942 } else {
2943 break;
2944 }
2945 }
2946}
2947
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002948/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
2949/// only called before the identifier, so these are most likely just grouping
Mike Stump11289f42009-09-09 15:08:12 +00002950/// parens for precedence. If we find that these are actually function
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002951/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
2952///
2953/// direct-declarator:
2954/// '(' declarator ')'
2955/// [GNU] '(' attributes declarator ')'
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002956/// direct-declarator '(' parameter-type-list ')'
2957/// direct-declarator '(' identifier-list[opt] ')'
2958/// [GNU] direct-declarator '(' parameter-forward-declarations
2959/// parameter-type-list[opt] ')'
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002960///
2961void Parser::ParseParenDeclarator(Declarator &D) {
2962 SourceLocation StartLoc = ConsumeParen();
2963 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
Mike Stump11289f42009-09-09 15:08:12 +00002964
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002965 // Eat any attributes before we look at whether this is a grouping or function
2966 // declarator paren. If this is a grouping paren, the attribute applies to
2967 // the type being built up, for example:
2968 // int (__attribute__(()) *x)(long y)
2969 // If this ends up not being a grouping paren, the attribute applies to the
2970 // first argument, for example:
2971 // int (__attribute__(()) int x)
2972 // In either case, we need to eat any attributes to be able to determine what
2973 // sort of paren this is.
2974 //
John McCall53fa7142010-12-24 02:08:15 +00002975 ParsedAttributes attrs;
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002976 bool RequiresArg = false;
2977 if (Tok.is(tok::kw___attribute)) {
John McCall53fa7142010-12-24 02:08:15 +00002978 ParseGNUAttributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +00002979
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002980 // We require that the argument list (if this is a non-grouping paren) be
2981 // present even if the attribute list was empty.
2982 RequiresArg = true;
2983 }
Steve Naroff44ac7772008-12-25 14:16:32 +00002984 // Eat any Microsoft extensions.
Eli Friedman53339e02009-06-08 23:27:34 +00002985 if (Tok.is(tok::kw___cdecl) || Tok.is(tok::kw___stdcall) ||
Douglas Gregora941dca2010-05-18 16:57:00 +00002986 Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___fastcall) ||
2987 Tok.is(tok::kw___w64) || Tok.is(tok::kw___ptr64)) {
John McCall53fa7142010-12-24 02:08:15 +00002988 ParseMicrosoftTypeAttributes(attrs);
Eli Friedman53339e02009-06-08 23:27:34 +00002989 }
Dawn Perchik335e16b2010-09-03 01:29:35 +00002990 // Eat any Borland extensions.
Ted Kremenek5eec2b02010-11-10 05:59:39 +00002991 if (Tok.is(tok::kw___pascal))
John McCall53fa7142010-12-24 02:08:15 +00002992 ParseBorlandTypeAttributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +00002993
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002994 // If we haven't past the identifier yet (or where the identifier would be
2995 // stored, if this is an abstract declarator), then this is probably just
2996 // grouping parens. However, if this could be an abstract-declarator, then
2997 // this could also be the start of function arguments (consider 'void()').
2998 bool isGrouping;
Mike Stump11289f42009-09-09 15:08:12 +00002999
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003000 if (!D.mayOmitIdentifier()) {
3001 // If this can't be an abstract-declarator, this *must* be a grouping
3002 // paren, because we haven't seen the identifier yet.
3003 isGrouping = true;
3004 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Argyrios Kyrtzidise8addf52008-10-06 00:07:55 +00003005 (getLang().CPlusPlus && Tok.is(tok::ellipsis)) || // C++ int(...)
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003006 isDeclarationSpecifier()) { // 'int(int)' is a function.
3007 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
3008 // considered to be a type, not a K&R identifier-list.
3009 isGrouping = false;
3010 } else {
3011 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
3012 isGrouping = true;
3013 }
Mike Stump11289f42009-09-09 15:08:12 +00003014
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003015 // If this is a grouping paren, handle:
3016 // direct-declarator: '(' declarator ')'
3017 // direct-declarator: '(' attributes declarator ')'
3018 if (isGrouping) {
Argyrios Kyrtzidis8ae36842008-10-07 10:21:57 +00003019 bool hadGroupingParens = D.hasGroupingParens();
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00003020 D.setGroupingParens(true);
John McCall53fa7142010-12-24 02:08:15 +00003021 if (!attrs.empty())
3022 D.addAttributes(attrs.getList(), SourceLocation());
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00003023
Sebastian Redlbd150f42008-11-21 19:14:01 +00003024 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003025 // Match the ')'.
Abramo Bagnara924a8f32010-12-10 16:29:40 +00003026 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_paren, StartLoc);
3027 D.AddTypeInfo(DeclaratorChunk::getParen(StartLoc, EndLoc), EndLoc);
Argyrios Kyrtzidis8ae36842008-10-07 10:21:57 +00003028
3029 D.setGroupingParens(hadGroupingParens);
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003030 return;
3031 }
Mike Stump11289f42009-09-09 15:08:12 +00003032
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003033 // Okay, if this wasn't a grouping paren, it must be the start of a function
3034 // argument list. Recognize that this declarator will never have an
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003035 // identifier (and remember where it would have been), then call into
3036 // ParseFunctionDeclarator to handle of argument list.
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003037 D.SetIdentifier(0, Tok.getLocation());
3038
John McCall53fa7142010-12-24 02:08:15 +00003039 ParseFunctionDeclarator(StartLoc, D, attrs, RequiresArg);
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003040}
3041
3042/// ParseFunctionDeclarator - We are after the identifier and have parsed the
3043/// declarator D up to a paren, which indicates that we are parsing function
3044/// arguments.
Chris Lattneracd58a32006-08-06 17:24:14 +00003045///
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003046/// If AttrList is non-null, then the caller parsed those arguments immediately
3047/// after the open paren - they should be considered to be the first argument of
3048/// a parameter. If RequiresArg is true, then the first argument of the
3049/// function is required to be present and required to not be an identifier
3050/// list.
3051///
Chris Lattneracd58a32006-08-06 17:24:14 +00003052/// This method also handles this portion of the grammar:
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003053/// parameter-type-list: [C99 6.7.5]
3054/// parameter-list
3055/// parameter-list ',' '...'
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00003056/// [C++] parameter-list '...'
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003057///
3058/// parameter-list: [C99 6.7.5]
3059/// parameter-declaration
3060/// parameter-list ',' parameter-declaration
3061///
3062/// parameter-declaration: [C99 6.7.5]
3063/// declaration-specifiers declarator
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00003064/// [C++] declaration-specifiers declarator '=' assignment-expression
Chris Lattnere37e2332006-08-15 04:50:22 +00003065/// [GNU] declaration-specifiers declarator attributes
Sebastian Redlf769df52009-03-24 22:27:57 +00003066/// declaration-specifiers abstract-declarator[opt]
3067/// [C++] declaration-specifiers abstract-declarator[opt]
Chris Lattner58258242008-04-10 02:22:51 +00003068/// '=' assignment-expression
Chris Lattnere37e2332006-08-15 04:50:22 +00003069/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003070///
Douglas Gregor54992352011-01-26 03:43:54 +00003071/// For C++, after the parameter-list, it also parses "cv-qualifier-seq[opt]",
3072/// C++0x "ref-qualifier[opt]" and "exception-specification[opt]".
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003073///
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003074void Parser::ParseFunctionDeclarator(SourceLocation LParenLoc, Declarator &D,
John McCall53fa7142010-12-24 02:08:15 +00003075 ParsedAttributes &attrs,
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003076 bool RequiresArg) {
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003077 // lparen is already consumed!
3078 assert(D.isPastIdentifier() && "Should not call before identifier!");
Mike Stump11289f42009-09-09 15:08:12 +00003079
Douglas Gregor7fb25412010-10-01 18:44:50 +00003080 ParsedType TrailingReturnType;
3081
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003082 // This parameter list may be empty.
Chris Lattner76c72282007-10-09 17:33:22 +00003083 if (Tok.is(tok::r_paren)) {
Ted Kremenek5eec2b02010-11-10 05:59:39 +00003084 if (RequiresArg)
Chris Lattner6d29c102008-11-18 07:48:38 +00003085 Diag(Tok, diag::err_argument_required_after_attribute);
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003086
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00003087 SourceLocation RParenLoc = ConsumeParen(); // Eat the closing ')'.
3088 SourceLocation EndLoc = RParenLoc;
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003089
3090 // cv-qualifier-seq[opt].
3091 DeclSpec DS;
Douglas Gregor54992352011-01-26 03:43:54 +00003092 SourceLocation RefQualifierLoc;
3093 bool RefQualifierIsLValueRef = true;
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00003094 bool hasExceptionSpec = false;
Sebastian Redlfb3f1792009-05-31 11:47:27 +00003095 SourceLocation ThrowLoc;
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00003096 bool hasAnyExceptionSpec = false;
John McCallba7bf592010-08-24 05:47:05 +00003097 llvm::SmallVector<ParsedType, 2> Exceptions;
Sebastian Redld6434562009-05-29 18:02:33 +00003098 llvm::SmallVector<SourceRange, 2> ExceptionRanges;
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003099 if (getLang().CPlusPlus) {
John McCall53fa7142010-12-24 02:08:15 +00003100 MaybeParseCXX0XAttributes(attrs);
3101
Chris Lattnercf0bab22008-12-18 07:02:59 +00003102 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003103 if (!DS.getSourceRange().getEnd().isInvalid())
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00003104 EndLoc = DS.getSourceRange().getEnd();
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003105
Douglas Gregor54992352011-01-26 03:43:54 +00003106 // Parse ref-qualifier[opt]
3107 if (Tok.is(tok::amp) || Tok.is(tok::ampamp)) {
3108 if (!getLang().CPlusPlus0x)
Douglas Gregora5271302011-01-26 20:35:32 +00003109 Diag(Tok, diag::ext_ref_qualifier);
Douglas Gregor54992352011-01-26 03:43:54 +00003110
3111 RefQualifierIsLValueRef = Tok.is(tok::amp);
3112 RefQualifierLoc = ConsumeToken();
3113 EndLoc = RefQualifierLoc;
3114 }
3115
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003116 // Parse exception-specification[opt].
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00003117 if (Tok.is(tok::kw_throw)) {
3118 hasExceptionSpec = true;
Sebastian Redlfb3f1792009-05-31 11:47:27 +00003119 ThrowLoc = Tok.getLocation();
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00003120 ParseExceptionSpecification(EndLoc, Exceptions, ExceptionRanges,
Sebastian Redld6434562009-05-29 18:02:33 +00003121 hasAnyExceptionSpec);
3122 assert(Exceptions.size() == ExceptionRanges.size() &&
3123 "Produced different number of exception types and ranges.");
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00003124 }
Douglas Gregor7fb25412010-10-01 18:44:50 +00003125
3126 // Parse trailing-return-type.
3127 if (getLang().CPlusPlus0x && Tok.is(tok::arrow)) {
3128 TrailingReturnType = ParseTrailingReturnType().get();
3129 }
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003130 }
3131
Chris Lattner371ed4e2008-04-06 06:57:35 +00003132 // Remember that we parsed a function type, and remember the attributes.
Chris Lattneracd58a32006-08-06 17:24:14 +00003133 // int() -> no prototype, no '...'.
John McCall53fa7142010-12-24 02:08:15 +00003134 D.AddTypeInfo(DeclaratorChunk::getFunction(attrs,
3135 /*prototype*/getLang().CPlusPlus,
Chris Lattner371ed4e2008-04-06 06:57:35 +00003136 /*variadic*/ false,
Douglas Gregor94349fd2009-02-18 07:07:28 +00003137 SourceLocation(),
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003138 /*arglist*/ 0, 0,
3139 DS.getTypeQualifiers(),
Douglas Gregor54992352011-01-26 03:43:54 +00003140 RefQualifierIsLValueRef,
3141 RefQualifierLoc,
Sebastian Redlfb3f1792009-05-31 11:47:27 +00003142 hasExceptionSpec, ThrowLoc,
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00003143 hasAnyExceptionSpec,
Sebastian Redld6434562009-05-29 18:02:33 +00003144 Exceptions.data(),
3145 ExceptionRanges.data(),
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00003146 Exceptions.size(),
Douglas Gregor7fb25412010-10-01 18:44:50 +00003147 LParenLoc, RParenLoc, D,
3148 TrailingReturnType),
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00003149 EndLoc);
Chris Lattner371ed4e2008-04-06 06:57:35 +00003150 return;
Sebastian Redld6434562009-05-29 18:02:33 +00003151 }
3152
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003153 // Alternatively, this parameter list may be an identifier list form for a
3154 // K&R-style function: void foo(a,b,c)
John Thompson22334602010-02-05 00:12:22 +00003155 if (!getLang().CPlusPlus && Tok.is(tok::identifier)
3156 && !TryAltiVecVectorToken()) {
John McCall1f476a12010-02-26 08:45:28 +00003157 if (TryAnnotateTypeOrScopeToken() || !Tok.is(tok::annot_typename)) {
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003158 // K&R identifier lists can't have typedefs as identifiers, per
3159 // C99 6.7.5.3p11.
Ted Kremenek5eec2b02010-11-10 05:59:39 +00003160 if (RequiresArg)
Steve Naroffb0486722009-01-28 19:16:40 +00003161 Diag(Tok, diag::err_argument_required_after_attribute);
Chris Lattner9453ab82010-05-14 17:23:36 +00003162
Steve Naroffb0486722009-01-28 19:16:40 +00003163 // Identifier list. Note that '(' identifier-list ')' is only allowed for
Chris Lattner9453ab82010-05-14 17:23:36 +00003164 // normal declarators, not for abstract-declarators. Get the first
3165 // identifier.
Chris Lattnerff895c12010-05-14 17:44:56 +00003166 Token FirstTok = Tok;
Chris Lattner9453ab82010-05-14 17:23:36 +00003167 ConsumeToken(); // eat the first identifier.
Chris Lattnerff895c12010-05-14 17:44:56 +00003168
3169 // Identifier lists follow a really simple grammar: the identifiers can
3170 // be followed *only* by a ", moreidentifiers" or ")". However, K&R
3171 // identifier lists are really rare in the brave new modern world, and it
3172 // is very common for someone to typo a type in a non-k&r style list. If
3173 // we are presented with something like: "void foo(intptr x, float y)",
3174 // we don't want to start parsing the function declarator as though it is
3175 // a K&R style declarator just because intptr is an invalid type.
3176 //
3177 // To handle this, we check to see if the token after the first identifier
3178 // is a "," or ")". Only if so, do we parse it as an identifier list.
3179 if (Tok.is(tok::comma) || Tok.is(tok::r_paren))
3180 return ParseFunctionDeclaratorIdentifierList(LParenLoc,
3181 FirstTok.getIdentifierInfo(),
3182 FirstTok.getLocation(), D);
3183
3184 // If we get here, the code is invalid. Push the first identifier back
3185 // into the token stream and parse the first argument as an (invalid)
3186 // normal argument declarator.
3187 PP.EnterToken(Tok);
3188 Tok = FirstTok;
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003189 }
Chris Lattner371ed4e2008-04-06 06:57:35 +00003190 }
Mike Stump11289f42009-09-09 15:08:12 +00003191
Chris Lattner371ed4e2008-04-06 06:57:35 +00003192 // Finally, a normal, non-empty parameter type list.
Mike Stump11289f42009-09-09 15:08:12 +00003193
Chris Lattner371ed4e2008-04-06 06:57:35 +00003194 // Build up an array of information about the parsed arguments.
3195 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00003196
3197 // Enter function-declaration scope, limiting any declarators to the
3198 // function prototype scope, including parameter declarators.
Chris Lattnerbd61a952009-03-05 00:00:31 +00003199 ParseScope PrototypeScope(this,
3200 Scope::FunctionPrototypeScope|Scope::DeclScope);
Mike Stump11289f42009-09-09 15:08:12 +00003201
Chris Lattner371ed4e2008-04-06 06:57:35 +00003202 bool IsVariadic = false;
Douglas Gregor94349fd2009-02-18 07:07:28 +00003203 SourceLocation EllipsisLoc;
Chris Lattner371ed4e2008-04-06 06:57:35 +00003204 while (1) {
3205 if (Tok.is(tok::ellipsis)) {
3206 IsVariadic = true;
Douglas Gregor94349fd2009-02-18 07:07:28 +00003207 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chris Lattner371ed4e2008-04-06 06:57:35 +00003208 break;
Chris Lattneracd58a32006-08-06 17:24:14 +00003209 }
Mike Stump11289f42009-09-09 15:08:12 +00003210
Chris Lattner371ed4e2008-04-06 06:57:35 +00003211 // Parse the declaration-specifiers.
John McCall28a6aea2009-11-04 02:18:39 +00003212 // Just use the ParsingDeclaration "scope" of the declarator.
Chris Lattner371ed4e2008-04-06 06:57:35 +00003213 DeclSpec DS;
John McCall53fa7142010-12-24 02:08:15 +00003214
3215 // Skip any Microsoft attributes before a param.
3216 if (getLang().Microsoft && Tok.is(tok::l_square))
3217 ParseMicrosoftAttributes(DS.getAttributes());
3218
3219 SourceLocation DSStart = Tok.getLocation();
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003220
3221 // If the caller parsed attributes for the first argument, add them now.
John McCall53fa7142010-12-24 02:08:15 +00003222 // Take them so that we only apply the attributes to the first parameter.
3223 DS.takeAttributesFrom(attrs);
3224
Chris Lattnerde39c3e2009-02-27 18:38:20 +00003225 ParseDeclarationSpecifiers(DS);
Mike Stump11289f42009-09-09 15:08:12 +00003226
Chris Lattner371ed4e2008-04-06 06:57:35 +00003227 // Parse the declarator. This is "PrototypeContext", because we must
3228 // accept either 'declarator' or 'abstract-declarator' here.
3229 Declarator ParmDecl(DS, Declarator::PrototypeContext);
3230 ParseDeclarator(ParmDecl);
3231
3232 // Parse GNU attributes, if present.
John McCall53fa7142010-12-24 02:08:15 +00003233 MaybeParseGNUAttributes(ParmDecl);
Mike Stump11289f42009-09-09 15:08:12 +00003234
Chris Lattner371ed4e2008-04-06 06:57:35 +00003235 // Remember this parsed parameter in ParamInfo.
3236 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
Mike Stump11289f42009-09-09 15:08:12 +00003237
Douglas Gregor4d87df52008-12-16 21:30:33 +00003238 // DefArgToks is used when the parsing of default arguments needs
3239 // to be delayed.
3240 CachedTokens *DefArgToks = 0;
3241
Chris Lattner371ed4e2008-04-06 06:57:35 +00003242 // If no parameter was specified, verify that *something* was specified,
3243 // otherwise we have a missing type and identifier.
Chris Lattnerde39c3e2009-02-27 18:38:20 +00003244 if (DS.isEmpty() && ParmDecl.getIdentifier() == 0 &&
3245 ParmDecl.getNumTypeObjects() == 0) {
Chris Lattner371ed4e2008-04-06 06:57:35 +00003246 // Completely missing, emit error.
3247 Diag(DSStart, diag::err_missing_param);
3248 } else {
3249 // Otherwise, we have something. Add it and let semantic analysis try
3250 // to grok it and add the result to the ParamInfo we are building.
Mike Stump11289f42009-09-09 15:08:12 +00003251
Chris Lattner371ed4e2008-04-06 06:57:35 +00003252 // Inform the actions module about the parameter declarator, so it gets
3253 // added to the current scope.
John McCall48871652010-08-21 09:40:31 +00003254 Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl);
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00003255
3256 // Parse the default argument, if any. We parse the default
3257 // arguments in all dialects; the semantic analysis in
3258 // ActOnParamDefaultArgument will reject the default argument in
3259 // C.
3260 if (Tok.is(tok::equal)) {
Douglas Gregor58354032008-12-24 00:01:03 +00003261 SourceLocation EqualLoc = Tok.getLocation();
3262
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00003263 // Parse the default argument
Douglas Gregor4d87df52008-12-16 21:30:33 +00003264 if (D.getContext() == Declarator::MemberContext) {
3265 // If we're inside a class definition, cache the tokens
3266 // corresponding to the default argument. We'll actually parse
3267 // them when we see the end of the class definition.
3268 // FIXME: Templates will require something similar.
3269 // FIXME: Can we use a smart pointer for Toks?
3270 DefArgToks = new CachedTokens;
3271
Mike Stump11289f42009-09-09 15:08:12 +00003272 if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks,
Argyrios Kyrtzidis8d7bdba2010-04-23 21:20:12 +00003273 /*StopAtSemi=*/true,
3274 /*ConsumeFinalToken=*/false)) {
Douglas Gregor4d87df52008-12-16 21:30:33 +00003275 delete DefArgToks;
3276 DefArgToks = 0;
Douglas Gregor58354032008-12-24 00:01:03 +00003277 Actions.ActOnParamDefaultArgumentError(Param);
Argyrios Kyrtzidis249179c2010-08-06 09:47:24 +00003278 } else {
3279 // Mark the end of the default argument so that we know when to
3280 // stop when we parse it later on.
3281 Token DefArgEnd;
3282 DefArgEnd.startToken();
3283 DefArgEnd.setKind(tok::cxx_defaultarg_end);
3284 DefArgEnd.setLocation(Tok.getLocation());
3285 DefArgToks->push_back(DefArgEnd);
Mike Stump11289f42009-09-09 15:08:12 +00003286 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc,
Anders Carlsson84613c42009-06-12 16:51:40 +00003287 (*DefArgToks)[1].getLocation());
Argyrios Kyrtzidis249179c2010-08-06 09:47:24 +00003288 }
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00003289 } else {
Douglas Gregor4d87df52008-12-16 21:30:33 +00003290 // Consume the '='.
Douglas Gregor58354032008-12-24 00:01:03 +00003291 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00003292
Douglas Gregor8a01b2a2010-09-11 20:24:53 +00003293 // The argument isn't actually potentially evaluated unless it is
3294 // used.
3295 EnterExpressionEvaluationContext Eval(Actions,
3296 Sema::PotentiallyEvaluatedIfUsed);
3297
John McCalldadc5752010-08-24 06:29:42 +00003298 ExprResult DefArgResult(ParseAssignmentExpression());
Douglas Gregor4d87df52008-12-16 21:30:33 +00003299 if (DefArgResult.isInvalid()) {
3300 Actions.ActOnParamDefaultArgumentError(Param);
3301 SkipUntil(tok::comma, tok::r_paren, true, true);
3302 } else {
3303 // Inform the actions module about the default argument
3304 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
John McCallb268a282010-08-23 23:25:46 +00003305 DefArgResult.take());
Douglas Gregor4d87df52008-12-16 21:30:33 +00003306 }
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00003307 }
3308 }
Mike Stump11289f42009-09-09 15:08:12 +00003309
3310 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
3311 ParmDecl.getIdentifierLoc(), Param,
Douglas Gregor4d87df52008-12-16 21:30:33 +00003312 DefArgToks));
Chris Lattner371ed4e2008-04-06 06:57:35 +00003313 }
3314
3315 // If the next token is a comma, consume it and keep reading arguments.
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00003316 if (Tok.isNot(tok::comma)) {
3317 if (Tok.is(tok::ellipsis)) {
3318 IsVariadic = true;
3319 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
3320
3321 if (!getLang().CPlusPlus) {
3322 // We have ellipsis without a preceding ',', which is ill-formed
3323 // in C. Complain and provide the fix.
3324 Diag(EllipsisLoc, diag::err_missing_comma_before_ellipsis)
Douglas Gregora771f462010-03-31 17:46:05 +00003325 << FixItHint::CreateInsertion(EllipsisLoc, ", ");
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00003326 }
3327 }
3328
3329 break;
3330 }
Mike Stump11289f42009-09-09 15:08:12 +00003331
Chris Lattner371ed4e2008-04-06 06:57:35 +00003332 // Consume the comma.
3333 ConsumeToken();
Chris Lattneracd58a32006-08-06 17:24:14 +00003334 }
Mike Stump11289f42009-09-09 15:08:12 +00003335
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003336 // If we have the closing ')', eat it.
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00003337 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
3338 SourceLocation EndLoc = RParenLoc;
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003339
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003340 DeclSpec DS;
Douglas Gregor54992352011-01-26 03:43:54 +00003341 SourceLocation RefQualifierLoc;
3342 bool RefQualifierIsLValueRef = true;
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00003343 bool hasExceptionSpec = false;
Sebastian Redlfb3f1792009-05-31 11:47:27 +00003344 SourceLocation ThrowLoc;
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00003345 bool hasAnyExceptionSpec = false;
John McCallba7bf592010-08-24 05:47:05 +00003346 llvm::SmallVector<ParsedType, 2> Exceptions;
Sebastian Redld6434562009-05-29 18:02:33 +00003347 llvm::SmallVector<SourceRange, 2> ExceptionRanges;
Alexis Hunt96d5c762009-11-21 08:43:09 +00003348
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003349 if (getLang().CPlusPlus) {
John McCall53fa7142010-12-24 02:08:15 +00003350 MaybeParseCXX0XAttributes(attrs);
3351
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003352 // Parse cv-qualifier-seq[opt].
Chris Lattnercf0bab22008-12-18 07:02:59 +00003353 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003354 if (!DS.getSourceRange().getEnd().isInvalid())
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00003355 EndLoc = DS.getSourceRange().getEnd();
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003356
Douglas Gregor54992352011-01-26 03:43:54 +00003357 // Parse ref-qualifier[opt]
3358 if (Tok.is(tok::amp) || Tok.is(tok::ampamp)) {
3359 if (!getLang().CPlusPlus0x)
Douglas Gregora5271302011-01-26 20:35:32 +00003360 Diag(Tok, diag::ext_ref_qualifier);
Douglas Gregor54992352011-01-26 03:43:54 +00003361
3362 RefQualifierIsLValueRef = Tok.is(tok::amp);
3363 RefQualifierLoc = ConsumeToken();
3364 EndLoc = RefQualifierLoc;
3365 }
3366
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003367 // Parse exception-specification[opt].
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00003368 if (Tok.is(tok::kw_throw)) {
3369 hasExceptionSpec = true;
Sebastian Redlfb3f1792009-05-31 11:47:27 +00003370 ThrowLoc = Tok.getLocation();
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00003371 ParseExceptionSpecification(EndLoc, Exceptions, ExceptionRanges,
Sebastian Redld6434562009-05-29 18:02:33 +00003372 hasAnyExceptionSpec);
3373 assert(Exceptions.size() == ExceptionRanges.size() &&
3374 "Produced different number of exception types and ranges.");
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00003375 }
Douglas Gregor7fb25412010-10-01 18:44:50 +00003376
3377 // Parse trailing-return-type.
3378 if (getLang().CPlusPlus0x && Tok.is(tok::arrow)) {
3379 TrailingReturnType = ParseTrailingReturnType().get();
3380 }
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003381 }
3382
Douglas Gregor7fb25412010-10-01 18:44:50 +00003383 // FIXME: We should leave the prototype scope before parsing the exception
3384 // specification, and then reenter it when parsing the trailing return type.
3385
3386 // Leave prototype scope.
3387 PrototypeScope.Exit();
3388
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00003389 // Remember that we parsed a function type, and remember the attributes.
John McCall53fa7142010-12-24 02:08:15 +00003390 D.AddTypeInfo(DeclaratorChunk::getFunction(attrs,
3391 /*proto*/true, IsVariadic,
Douglas Gregor94349fd2009-02-18 07:07:28 +00003392 EllipsisLoc,
Jay Foad7d0479f2009-05-21 09:52:38 +00003393 ParamInfo.data(), ParamInfo.size(),
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003394 DS.getTypeQualifiers(),
Douglas Gregor54992352011-01-26 03:43:54 +00003395 RefQualifierIsLValueRef,
3396 RefQualifierLoc,
Sebastian Redlfb3f1792009-05-31 11:47:27 +00003397 hasExceptionSpec, ThrowLoc,
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00003398 hasAnyExceptionSpec,
Sebastian Redld6434562009-05-29 18:02:33 +00003399 Exceptions.data(),
3400 ExceptionRanges.data(),
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00003401 Exceptions.size(),
Douglas Gregor7fb25412010-10-01 18:44:50 +00003402 LParenLoc, RParenLoc, D,
3403 TrailingReturnType),
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00003404 EndLoc);
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003405}
Chris Lattneracd58a32006-08-06 17:24:14 +00003406
Chris Lattner6c940e62008-04-06 06:34:08 +00003407/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
3408/// we found a K&R-style identifier list instead of a type argument list. The
Chris Lattner9453ab82010-05-14 17:23:36 +00003409/// first identifier has already been consumed, and the current token is the
3410/// token right after it.
Chris Lattner6c940e62008-04-06 06:34:08 +00003411///
3412/// identifier-list: [C99 6.7.5]
3413/// identifier
3414/// identifier-list ',' identifier
3415///
3416void Parser::ParseFunctionDeclaratorIdentifierList(SourceLocation LParenLoc,
Chris Lattner9453ab82010-05-14 17:23:36 +00003417 IdentifierInfo *FirstIdent,
3418 SourceLocation FirstIdentLoc,
Chris Lattner6c940e62008-04-06 06:34:08 +00003419 Declarator &D) {
3420 // Build up an array of information about the parsed arguments.
3421 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
3422 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
Mike Stump11289f42009-09-09 15:08:12 +00003423
Chris Lattner6c940e62008-04-06 06:34:08 +00003424 // If there was no identifier specified for the declarator, either we are in
3425 // an abstract-declarator, or we are in a parameter declarator which was found
3426 // to be abstract. In abstract-declarators, identifier lists are not valid:
3427 // diagnose this.
3428 if (!D.getIdentifier())
Chris Lattner9453ab82010-05-14 17:23:36 +00003429 Diag(FirstIdentLoc, diag::ext_ident_list_in_param);
Chris Lattner6c940e62008-04-06 06:34:08 +00003430
Chris Lattner9453ab82010-05-14 17:23:36 +00003431 // The first identifier was already read, and is known to be the first
3432 // identifier in the list. Remember this identifier in ParamInfo.
3433 ParamsSoFar.insert(FirstIdent);
John McCall48871652010-08-21 09:40:31 +00003434 ParamInfo.push_back(DeclaratorChunk::ParamInfo(FirstIdent, FirstIdentLoc, 0));
Mike Stump11289f42009-09-09 15:08:12 +00003435
Chris Lattner6c940e62008-04-06 06:34:08 +00003436 while (Tok.is(tok::comma)) {
3437 // Eat the comma.
3438 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00003439
Chris Lattner9186f552008-04-06 06:39:19 +00003440 // If this isn't an identifier, report the error and skip until ')'.
Chris Lattner6c940e62008-04-06 06:34:08 +00003441 if (Tok.isNot(tok::identifier)) {
3442 Diag(Tok, diag::err_expected_ident);
Chris Lattner9186f552008-04-06 06:39:19 +00003443 SkipUntil(tok::r_paren);
3444 return;
Chris Lattner6c940e62008-04-06 06:34:08 +00003445 }
Chris Lattner67b450c2008-04-06 06:47:48 +00003446
Chris Lattner6c940e62008-04-06 06:34:08 +00003447 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
Chris Lattner67b450c2008-04-06 06:47:48 +00003448
3449 // Reject 'typedef int y; int test(x, y)', but continue parsing.
Douglas Gregor0be31a22010-07-02 17:43:08 +00003450 if (Actions.getTypeName(*ParmII, Tok.getLocation(), getCurScope()))
Chris Lattnerebad6a22008-11-19 07:37:42 +00003451 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
Mike Stump11289f42009-09-09 15:08:12 +00003452
Chris Lattner6c940e62008-04-06 06:34:08 +00003453 // Verify that the argument identifier has not already been mentioned.
3454 if (!ParamsSoFar.insert(ParmII)) {
Chris Lattnerebad6a22008-11-19 07:37:42 +00003455 Diag(Tok, diag::err_param_redefinition) << ParmII;
Chris Lattner9186f552008-04-06 06:39:19 +00003456 } else {
3457 // Remember this identifier in ParamInfo.
Chris Lattner6c940e62008-04-06 06:34:08 +00003458 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Chris Lattner83f095c2009-03-28 19:18:32 +00003459 Tok.getLocation(),
John McCall48871652010-08-21 09:40:31 +00003460 0));
Chris Lattner9186f552008-04-06 06:39:19 +00003461 }
Mike Stump11289f42009-09-09 15:08:12 +00003462
Chris Lattner6c940e62008-04-06 06:34:08 +00003463 // Eat the identifier.
3464 ConsumeToken();
3465 }
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003466
3467 // If we have the closing ')', eat it and we're done.
3468 SourceLocation RLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
3469
Chris Lattner9186f552008-04-06 06:39:19 +00003470 // Remember that we parsed a function type, and remember the attributes. This
3471 // function type is always a K&R style function type, which is not varargs and
3472 // has no prototype.
John McCall53fa7142010-12-24 02:08:15 +00003473 D.AddTypeInfo(DeclaratorChunk::getFunction(ParsedAttributes(),
3474 /*proto*/false, /*varargs*/false,
Douglas Gregor94349fd2009-02-18 07:07:28 +00003475 SourceLocation(),
Chris Lattner9186f552008-04-06 06:39:19 +00003476 &ParamInfo[0], ParamInfo.size(),
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00003477 /*TypeQuals*/0,
Douglas Gregor54992352011-01-26 03:43:54 +00003478 true, SourceLocation(),
Sebastian Redlfb3f1792009-05-31 11:47:27 +00003479 /*exception*/false,
3480 SourceLocation(), false, 0, 0, 0,
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00003481 LParenLoc, RLoc, D),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003482 RLoc);
Chris Lattner6c940e62008-04-06 06:34:08 +00003483}
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003484
Chris Lattnere8074e62006-08-06 18:30:15 +00003485/// [C90] direct-declarator '[' constant-expression[opt] ']'
3486/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
3487/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
3488/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
3489/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
3490void Parser::ParseBracketDeclarator(Declarator &D) {
Chris Lattner04132372006-10-16 06:12:55 +00003491 SourceLocation StartLoc = ConsumeBracket();
Mike Stump11289f42009-09-09 15:08:12 +00003492
Chris Lattner84a11622008-12-18 07:27:21 +00003493 // C array syntax has many features, but by-far the most common is [] and [4].
3494 // This code does a fast path to handle some of the most obvious cases.
3495 if (Tok.getKind() == tok::r_square) {
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003496 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
John McCall53fa7142010-12-24 02:08:15 +00003497 ParsedAttributes attrs;
3498 MaybeParseCXX0XAttributes(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +00003499
Chris Lattner84a11622008-12-18 07:27:21 +00003500 // Remember that we parsed the empty array type.
John McCalldadc5752010-08-24 06:29:42 +00003501 ExprResult NumElements;
John McCall53fa7142010-12-24 02:08:15 +00003502 D.AddTypeInfo(DeclaratorChunk::getArray(0, attrs, false, false, 0,
Douglas Gregor04318252009-07-06 15:59:29 +00003503 StartLoc, EndLoc),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003504 EndLoc);
Chris Lattner84a11622008-12-18 07:27:21 +00003505 return;
3506 } else if (Tok.getKind() == tok::numeric_constant &&
3507 GetLookAheadToken(1).is(tok::r_square)) {
3508 // [4] is very common. Parse the numeric constant expression.
John McCalldadc5752010-08-24 06:29:42 +00003509 ExprResult ExprRes(Actions.ActOnNumericConstant(Tok));
Chris Lattner84a11622008-12-18 07:27:21 +00003510 ConsumeToken();
3511
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003512 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
John McCall53fa7142010-12-24 02:08:15 +00003513 ParsedAttributes attrs;
3514 MaybeParseCXX0XAttributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +00003515
Chris Lattner84a11622008-12-18 07:27:21 +00003516 // Remember that we parsed a array type, and remember its features.
John McCall53fa7142010-12-24 02:08:15 +00003517 D.AddTypeInfo(DeclaratorChunk::getArray(0, attrs, false, 0,
3518 ExprRes.release(),
Douglas Gregor04318252009-07-06 15:59:29 +00003519 StartLoc, EndLoc),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003520 EndLoc);
Chris Lattner84a11622008-12-18 07:27:21 +00003521 return;
3522 }
Mike Stump11289f42009-09-09 15:08:12 +00003523
Chris Lattnere8074e62006-08-06 18:30:15 +00003524 // If valid, this location is the position where we read the 'static' keyword.
3525 SourceLocation StaticLoc;
Chris Lattner76c72282007-10-09 17:33:22 +00003526 if (Tok.is(tok::kw_static))
Chris Lattneraf635312006-10-16 06:06:51 +00003527 StaticLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00003528
Chris Lattnere8074e62006-08-06 18:30:15 +00003529 // If there is a type-qualifier-list, read it now.
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00003530 // Type qualifiers in an array subscript are a C99 feature.
Chris Lattnere8074e62006-08-06 18:30:15 +00003531 DeclSpec DS;
Chris Lattnercf0bab22008-12-18 07:02:59 +00003532 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Mike Stump11289f42009-09-09 15:08:12 +00003533
Chris Lattnere8074e62006-08-06 18:30:15 +00003534 // If we haven't already read 'static', check to see if there is one after the
3535 // type-qualifier-list.
Chris Lattner76c72282007-10-09 17:33:22 +00003536 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Chris Lattneraf635312006-10-16 06:06:51 +00003537 StaticLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00003538
Chris Lattnere8074e62006-08-06 18:30:15 +00003539 // Handle "direct-declarator [ type-qual-list[opt] * ]".
Chris Lattnere8074e62006-08-06 18:30:15 +00003540 bool isStar = false;
John McCalldadc5752010-08-24 06:29:42 +00003541 ExprResult NumElements;
Mike Stump11289f42009-09-09 15:08:12 +00003542
Chris Lattner521ff2b2008-04-06 05:26:30 +00003543 // Handle the case where we have '[*]' as the array size. However, a leading
3544 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
3545 // the the token after the star is a ']'. Since stars in arrays are
3546 // infrequent, use of lookahead is not costly here.
3547 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattnerc439f0d2008-04-06 05:27:21 +00003548 ConsumeToken(); // Eat the '*'.
Chris Lattner1906f802006-08-06 19:14:46 +00003549
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00003550 if (StaticLoc.isValid()) {
Chris Lattner521ff2b2008-04-06 05:26:30 +00003551 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00003552 StaticLoc = SourceLocation(); // Drop the static.
3553 }
Chris Lattner521ff2b2008-04-06 05:26:30 +00003554 isStar = true;
Chris Lattner76c72282007-10-09 17:33:22 +00003555 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner84a11622008-12-18 07:27:21 +00003556 // Note, in C89, this production uses the constant-expr production instead
3557 // of assignment-expr. The only difference is that assignment-expr allows
3558 // things like '=' and '*='. Sema rejects these in C89 mode because they
3559 // are not i-c-e's, so we don't need to distinguish between the two here.
Mike Stump11289f42009-09-09 15:08:12 +00003560
Douglas Gregorc9c02ed2009-06-19 23:52:42 +00003561 // Parse the constant-expression or assignment-expression now (depending
3562 // on dialect).
3563 if (getLang().CPlusPlus)
3564 NumElements = ParseConstantExpression();
3565 else
3566 NumElements = ParseAssignmentExpression();
Chris Lattner62591722006-08-12 18:40:58 +00003567 }
Mike Stump11289f42009-09-09 15:08:12 +00003568
Chris Lattner62591722006-08-12 18:40:58 +00003569 // If there was an error parsing the assignment-expression, recover.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00003570 if (NumElements.isInvalid()) {
Chris Lattnercd2a8c52009-04-24 22:30:50 +00003571 D.setInvalidType(true);
Chris Lattner62591722006-08-12 18:40:58 +00003572 // If the expression was invalid, skip it.
3573 SkipUntil(tok::r_square);
3574 return;
Chris Lattnere8074e62006-08-06 18:30:15 +00003575 }
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003576
3577 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
3578
John McCall53fa7142010-12-24 02:08:15 +00003579 ParsedAttributes attrs;
3580 MaybeParseCXX0XAttributes(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +00003581
Chris Lattner84a11622008-12-18 07:27:21 +00003582 // Remember that we parsed a array type, and remember its features.
John McCall53fa7142010-12-24 02:08:15 +00003583 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(), attrs,
Chris Lattnercbc426d2006-12-02 06:43:02 +00003584 StaticLoc.isValid(), isStar,
Douglas Gregor04318252009-07-06 15:59:29 +00003585 NumElements.release(),
3586 StartLoc, EndLoc),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003587 EndLoc);
Chris Lattnere8074e62006-08-06 18:30:15 +00003588}
3589
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00003590/// [GNU] typeof-specifier:
3591/// typeof ( expressions )
3592/// typeof ( type-name )
3593/// [GNU/C++] typeof unary-expression
Steve Naroffad373bd2007-07-31 12:34:36 +00003594///
3595void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner76c72282007-10-09 17:33:22 +00003596 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00003597 Token OpTok = Tok;
Steve Naroffad373bd2007-07-31 12:34:36 +00003598 SourceLocation StartLoc = ConsumeToken();
3599
John McCalle8595032010-01-13 20:03:27 +00003600 const bool hasParens = Tok.is(tok::l_paren);
3601
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00003602 bool isCastExpr;
John McCallba7bf592010-08-24 05:47:05 +00003603 ParsedType CastTy;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00003604 SourceRange CastRange;
John McCalldadc5752010-08-24 06:29:42 +00003605 ExprResult Operand = ParseExprAfterTypeofSizeofAlignof(OpTok,
John McCall6caebb12010-08-25 02:45:51 +00003606 isCastExpr,
3607 CastTy,
3608 CastRange);
John McCalle8595032010-01-13 20:03:27 +00003609 if (hasParens)
3610 DS.setTypeofParensRange(CastRange);
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00003611
3612 if (CastRange.getEnd().isInvalid())
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00003613 // FIXME: Not accurate, the range gets one token more than it should.
3614 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00003615 else
3616 DS.SetRangeEnd(CastRange.getEnd());
Mike Stump11289f42009-09-09 15:08:12 +00003617
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00003618 if (isCastExpr) {
3619 if (!CastTy) {
3620 DS.SetTypeSpecError();
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00003621 return;
Douglas Gregor220cac52009-02-18 17:45:20 +00003622 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00003623
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00003624 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00003625 unsigned DiagID;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00003626 // Check for duplicate type specifiers (e.g. "int typeof(int)").
3627 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00003628 DiagID, CastTy))
3629 Diag(StartLoc, DiagID) << PrevSpec;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00003630 return;
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00003631 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00003632
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00003633 // If we get here, the operand to the typeof was an expresion.
3634 if (Operand.isInvalid()) {
3635 DS.SetTypeSpecError();
Steve Naroff4bd2f712007-08-02 02:53:48 +00003636 return;
Steve Naroffad373bd2007-07-31 12:34:36 +00003637 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00003638
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00003639 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00003640 unsigned DiagID;
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00003641 // Check for duplicate type specifiers (e.g. "int typeof(int)").
3642 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
John McCallba7bf592010-08-24 05:47:05 +00003643 DiagID, Operand.get()))
John McCall49bfce42009-08-03 20:12:06 +00003644 Diag(StartLoc, DiagID) << PrevSpec;
Steve Naroffad373bd2007-07-31 12:34:36 +00003645}
Chris Lattner73a9c7d2010-02-28 18:33:55 +00003646
3647
3648/// TryAltiVecVectorTokenOutOfLine - Out of line body that should only be called
3649/// from TryAltiVecVectorToken.
3650bool Parser::TryAltiVecVectorTokenOutOfLine() {
3651 Token Next = NextToken();
3652 switch (Next.getKind()) {
3653 default: return false;
3654 case tok::kw_short:
3655 case tok::kw_long:
3656 case tok::kw_signed:
3657 case tok::kw_unsigned:
3658 case tok::kw_void:
3659 case tok::kw_char:
3660 case tok::kw_int:
3661 case tok::kw_float:
3662 case tok::kw_double:
3663 case tok::kw_bool:
3664 case tok::kw___pixel:
3665 Tok.setKind(tok::kw___vector);
3666 return true;
3667 case tok::identifier:
3668 if (Next.getIdentifierInfo() == Ident_pixel) {
3669 Tok.setKind(tok::kw___vector);
3670 return true;
3671 }
3672 return false;
3673 }
3674}
3675
3676bool Parser::TryAltiVecTokenOutOfLine(DeclSpec &DS, SourceLocation Loc,
3677 const char *&PrevSpec, unsigned &DiagID,
3678 bool &isInvalid) {
3679 if (Tok.getIdentifierInfo() == Ident_vector) {
3680 Token Next = NextToken();
3681 switch (Next.getKind()) {
3682 case tok::kw_short:
3683 case tok::kw_long:
3684 case tok::kw_signed:
3685 case tok::kw_unsigned:
3686 case tok::kw_void:
3687 case tok::kw_char:
3688 case tok::kw_int:
3689 case tok::kw_float:
3690 case tok::kw_double:
3691 case tok::kw_bool:
3692 case tok::kw___pixel:
3693 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
3694 return true;
3695 case tok::identifier:
3696 if (Next.getIdentifierInfo() == Ident_pixel) {
3697 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
3698 return true;
3699 }
3700 break;
3701 default:
3702 break;
3703 }
Douglas Gregor9938e3b2010-06-16 15:28:57 +00003704 } else if ((Tok.getIdentifierInfo() == Ident_pixel) &&
Chris Lattner73a9c7d2010-02-28 18:33:55 +00003705 DS.isTypeAltiVecVector()) {
3706 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
3707 return true;
3708 }
3709 return false;
3710}
3711