blob: c4fce66b65779b513a01c10f34dec6e8ff9caabb [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
2515 // Check whether the next token(s) are part of a declaration
2516 // specifier, in which case we have the start of a parameter and,
2517 // therefore, we know that this is a constructor.
2518 bool IsConstructor = isDeclarationSpecifier();
2519 TPA.Revert();
2520 return IsConstructor;
2521}
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002522
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002523/// ParseTypeQualifierListOpt
Dawn Perchik335e16b2010-09-03 01:29:35 +00002524/// type-qualifier-list: [C99 6.7.5]
2525/// type-qualifier
2526/// [vendor] attributes
2527/// [ only if VendorAttributesAllowed=true ]
2528/// type-qualifier-list type-qualifier
2529/// [vendor] type-qualifier-list attributes
2530/// [ only if VendorAttributesAllowed=true ]
2531/// [C++0x] attribute-specifier[opt] is allowed before cv-qualifier-seq
2532/// [ only if CXX0XAttributesAllowed=true ]
2533/// Note: vendor can be GNU, MS, etc.
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002534///
Dawn Perchik335e16b2010-09-03 01:29:35 +00002535void Parser::ParseTypeQualifierListOpt(DeclSpec &DS,
2536 bool VendorAttributesAllowed,
Alexis Hunt96d5c762009-11-21 08:43:09 +00002537 bool CXX0XAttributesAllowed) {
2538 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier()) {
2539 SourceLocation Loc = Tok.getLocation();
John McCall53fa7142010-12-24 02:08:15 +00002540 ParsedAttributesWithRange attrs;
2541 ParseCXX0XAttributes(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +00002542 if (CXX0XAttributesAllowed)
John McCall53fa7142010-12-24 02:08:15 +00002543 DS.takeAttributesFrom(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +00002544 else
2545 Diag(Loc, diag::err_attributes_not_allowed);
2546 }
2547
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002548 while (1) {
John McCall49bfce42009-08-03 20:12:06 +00002549 bool isInvalid = false;
Chris Lattnerd9c3c592006-08-05 06:26:47 +00002550 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00002551 unsigned DiagID = 0;
Chris Lattner60809f52006-11-28 05:18:46 +00002552 SourceLocation Loc = Tok.getLocation();
Chris Lattnerd9c3c592006-08-05 06:26:47 +00002553
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002554 switch (Tok.getKind()) {
Douglas Gregor28c78432010-08-27 17:35:51 +00002555 case tok::code_completion:
2556 Actions.CodeCompleteTypeQualifiers(DS);
2557 ConsumeCodeCompletionToken();
2558 break;
2559
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002560 case tok::kw_const:
John McCall49bfce42009-08-03 20:12:06 +00002561 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, DiagID,
2562 getLang());
Chris Lattnerd9c3c592006-08-05 06:26:47 +00002563 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002564 case tok::kw_volatile:
John McCall49bfce42009-08-03 20:12:06 +00002565 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, 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_restrict:
John McCall49bfce42009-08-03 20:12:06 +00002569 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
2570 getLang());
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002571 break;
Eli Friedman53339e02009-06-08 23:27:34 +00002572 case tok::kw___w64:
Steve Narofff9c29d42008-12-25 14:41:26 +00002573 case tok::kw___ptr64:
Steve Naroff44ac7772008-12-25 14:16:32 +00002574 case tok::kw___cdecl:
2575 case tok::kw___stdcall:
2576 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00002577 case tok::kw___thiscall:
Dawn Perchik335e16b2010-09-03 01:29:35 +00002578 if (VendorAttributesAllowed) {
John McCall53fa7142010-12-24 02:08:15 +00002579 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman53339e02009-06-08 23:27:34 +00002580 continue;
2581 }
2582 goto DoneWithTypeQuals;
Dawn Perchik335e16b2010-09-03 01:29:35 +00002583 case tok::kw___pascal:
2584 if (VendorAttributesAllowed) {
John McCall53fa7142010-12-24 02:08:15 +00002585 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik335e16b2010-09-03 01:29:35 +00002586 continue;
2587 }
2588 goto DoneWithTypeQuals;
Chris Lattnere37e2332006-08-15 04:50:22 +00002589 case tok::kw___attribute:
Dawn Perchik335e16b2010-09-03 01:29:35 +00002590 if (VendorAttributesAllowed) {
John McCall53fa7142010-12-24 02:08:15 +00002591 ParseGNUAttributes(DS.getAttributes());
Chris Lattnercf0bab22008-12-18 07:02:59 +00002592 continue; // do *not* consume the next token!
2593 }
2594 // otherwise, FALL THROUGH!
2595 default:
Steve Naroff44ac7772008-12-25 14:16:32 +00002596 DoneWithTypeQuals:
Chris Lattnercf0bab22008-12-18 07:02:59 +00002597 // If this is not a type-qualifier token, we're done reading type
2598 // qualifiers. First verify that DeclSpec's are consistent.
Douglas Gregore3e01a22009-04-01 22:41:11 +00002599 DS.Finish(Diags, PP);
Chris Lattnercf0bab22008-12-18 07:02:59 +00002600 return;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002601 }
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00002602
Chris Lattnerd9c3c592006-08-05 06:26:47 +00002603 // If the specifier combination wasn't legal, issue a diagnostic.
2604 if (isInvalid) {
2605 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner6d29c102008-11-18 07:48:38 +00002606 Diag(Tok, DiagID) << PrevSpec;
Chris Lattnerd9c3c592006-08-05 06:26:47 +00002607 }
2608 ConsumeToken();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002609 }
2610}
2611
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00002612
2613/// ParseDeclarator - Parse and verify a newly-initialized declarator.
2614///
2615void Parser::ParseDeclarator(Declarator &D) {
2616 /// This implements the 'declarator' production in the C grammar, then checks
2617 /// for well-formedness and issues diagnostics.
Sebastian Redlbd150f42008-11-21 19:14:01 +00002618 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00002619}
2620
Sebastian Redlbd150f42008-11-21 19:14:01 +00002621/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
2622/// is parsed by the function passed to it. Pass null, and the direct-declarator
2623/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregordbc5daf2008-11-07 20:08:42 +00002624/// ptr-operator production.
2625///
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002626/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
2627/// [C] pointer[opt] direct-declarator
2628/// [C++] direct-declarator
2629/// [C++] ptr-operator declarator
Chris Lattner6c7416c2006-08-07 00:19:33 +00002630///
2631/// pointer: [C99 6.7.5]
2632/// '*' type-qualifier-list[opt]
2633/// '*' type-qualifier-list[opt] pointer
2634///
Douglas Gregordbc5daf2008-11-07 20:08:42 +00002635/// ptr-operator:
2636/// '*' cv-qualifier-seq[opt]
2637/// '&'
Sebastian Redled0f3b02009-03-15 22:02:01 +00002638/// [C++0x] '&&'
Douglas Gregordbc5daf2008-11-07 20:08:42 +00002639/// [GNU] '&' restrict[opt] attributes[opt]
Sebastian Redled0f3b02009-03-15 22:02:01 +00002640/// [GNU?] '&&' restrict[opt] attributes[opt]
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002641/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Sebastian Redlbd150f42008-11-21 19:14:01 +00002642void Parser::ParseDeclaratorInternal(Declarator &D,
2643 DirectDeclParseFunction DirectDeclParser) {
Douglas Gregor66a985d2009-08-26 14:27:30 +00002644 if (Diags.hasAllExtensionsSilenced())
2645 D.setExtension();
Douglas Gregorc49f5b22010-08-23 18:23:48 +00002646
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002647 // C++ member pointers start with a '::' or a nested-name.
2648 // Member pointers get special handling, since there's no place for the
2649 // scope spec in the generic path below.
Chris Lattner803802d2009-03-24 17:04:48 +00002650 if (getLang().CPlusPlus &&
2651 (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) ||
2652 Tok.is(tok::annot_cxxscope))) {
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002653 CXXScopeSpec SS;
John McCallba7bf592010-08-24 05:47:05 +00002654 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), true); // ignore fail
John McCall1f476a12010-02-26 08:45:28 +00002655
Jeffrey Yasskin4e150f82010-04-07 23:29:58 +00002656 if (SS.isNotEmpty()) {
Mike Stump11289f42009-09-09 15:08:12 +00002657 if (Tok.isNot(tok::star)) {
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002658 // The scope spec really belongs to the direct-declarator.
2659 D.getCXXScopeSpec() = SS;
2660 if (DirectDeclParser)
2661 (this->*DirectDeclParser)(D);
2662 return;
2663 }
2664
2665 SourceLocation Loc = ConsumeToken();
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002666 D.SetRangeEnd(Loc);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002667 DeclSpec DS;
2668 ParseTypeQualifierListOpt(DS);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002669 D.ExtendWithDeclSpec(DS);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002670
2671 // Recurse to parse whatever is left.
2672 ParseDeclaratorInternal(D, DirectDeclParser);
2673
2674 // Sema will have to catch (syntactically invalid) pointers into global
2675 // scope. It has to catch pointers into namespace scope anyway.
2676 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
John McCall53fa7142010-12-24 02:08:15 +00002677 Loc, DS.takeAttributes()),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002678 /* Don't replace range end. */SourceLocation());
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002679 return;
2680 }
2681 }
2682
2683 tok::TokenKind Kind = Tok.getKind();
Steve Naroffec33ed92008-08-27 16:04:49 +00002684 // Not a pointer, C++ reference, or block.
Chris Lattner9eac9312009-03-27 04:18:06 +00002685 if (Kind != tok::star && Kind != tok::caret &&
Chris Lattner803802d2009-03-24 17:04:48 +00002686 (Kind != tok::amp || !getLang().CPlusPlus) &&
Sebastian Redl3b27be62009-03-23 00:00:23 +00002687 // We parse rvalue refs in C++03, because otherwise the errors are scary.
Chris Lattner9eac9312009-03-27 04:18:06 +00002688 (Kind != tok::ampamp || !getLang().CPlusPlus)) {
Sebastian Redlbd150f42008-11-21 19:14:01 +00002689 if (DirectDeclParser)
2690 (this->*DirectDeclParser)(D);
Douglas Gregordbc5daf2008-11-07 20:08:42 +00002691 return;
2692 }
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002693
Sebastian Redled0f3b02009-03-15 22:02:01 +00002694 // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference,
2695 // '&&' -> rvalue reference
Sebastian Redl3b27be62009-03-23 00:00:23 +00002696 SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&.
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002697 D.SetRangeEnd(Loc);
Bill Wendling3708c182007-05-27 10:15:43 +00002698
Chris Lattner9eac9312009-03-27 04:18:06 +00002699 if (Kind == tok::star || Kind == tok::caret) {
Chris Lattner788404f2008-02-21 01:32:26 +00002700 // Is a pointer.
Bill Wendling3708c182007-05-27 10:15:43 +00002701 DeclSpec DS;
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002702
Bill Wendling3708c182007-05-27 10:15:43 +00002703 ParseTypeQualifierListOpt(DS);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002704 D.ExtendWithDeclSpec(DS);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002705
Bill Wendling3708c182007-05-27 10:15:43 +00002706 // Recursively parse the declarator.
Sebastian Redlbd150f42008-11-21 19:14:01 +00002707 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroffec33ed92008-08-27 16:04:49 +00002708 if (Kind == tok::star)
2709 // Remember that we parsed a pointer type, and remember the type-quals.
2710 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
John McCall53fa7142010-12-24 02:08:15 +00002711 DS.takeAttributes()),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002712 SourceLocation());
Steve Naroffec33ed92008-08-27 16:04:49 +00002713 else
2714 // Remember that we parsed a Block type, and remember the type-quals.
Mike Stump11289f42009-09-09 15:08:12 +00002715 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
John McCall53fa7142010-12-24 02:08:15 +00002716 Loc, DS.takeAttributes()),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002717 SourceLocation());
Bill Wendling3708c182007-05-27 10:15:43 +00002718 } else {
2719 // Is a reference
Bill Wendling93efb222007-06-02 23:28:54 +00002720 DeclSpec DS;
2721
Sebastian Redl3b27be62009-03-23 00:00:23 +00002722 // Complain about rvalue references in C++03, but then go on and build
2723 // the declarator.
2724 if (Kind == tok::ampamp && !getLang().CPlusPlus0x)
Douglas Gregor00984992011-01-25 02:17:32 +00002725 Diag(Loc, diag::ext_rvalue_reference);
Sebastian Redl3b27be62009-03-23 00:00:23 +00002726
Bill Wendling93efb222007-06-02 23:28:54 +00002727 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
2728 // cv-qualifiers are introduced through the use of a typedef or of a
2729 // template type argument, in which case the cv-qualifiers are ignored.
2730 //
2731 // [GNU] Retricted references are allowed.
2732 // [GNU] Attributes on references are allowed.
Alexis Hunt96d5c762009-11-21 08:43:09 +00002733 // [C++0x] Attributes on references are not allowed.
2734 ParseTypeQualifierListOpt(DS, true, false);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002735 D.ExtendWithDeclSpec(DS);
Bill Wendling93efb222007-06-02 23:28:54 +00002736
2737 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
2738 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
2739 Diag(DS.getConstSpecLoc(),
Chris Lattner6d29c102008-11-18 07:48:38 +00002740 diag::err_invalid_reference_qualifier_application) << "const";
Bill Wendling93efb222007-06-02 23:28:54 +00002741 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
2742 Diag(DS.getVolatileSpecLoc(),
Chris Lattner6d29c102008-11-18 07:48:38 +00002743 diag::err_invalid_reference_qualifier_application) << "volatile";
Bill Wendling93efb222007-06-02 23:28:54 +00002744 }
Bill Wendling3708c182007-05-27 10:15:43 +00002745
2746 // Recursively parse the declarator.
Sebastian Redlbd150f42008-11-21 19:14:01 +00002747 ParseDeclaratorInternal(D, DirectDeclParser);
Bill Wendling3708c182007-05-27 10:15:43 +00002748
Douglas Gregor66583c52008-11-03 15:51:28 +00002749 if (D.getNumTypeObjects() > 0) {
2750 // C++ [dcl.ref]p4: There shall be no references to references.
2751 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
2752 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattnerebad6a22008-11-19 07:37:42 +00002753 if (const IdentifierInfo *II = D.getIdentifier())
2754 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
2755 << II;
2756 else
2757 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
2758 << "type name";
Douglas Gregor66583c52008-11-03 15:51:28 +00002759
Sebastian Redlbd150f42008-11-21 19:14:01 +00002760 // Once we've complained about the reference-to-reference, we
Douglas Gregor66583c52008-11-03 15:51:28 +00002761 // can go ahead and build the (technically ill-formed)
2762 // declarator: reference collapsing will take care of it.
2763 }
2764 }
2765
Bill Wendling3708c182007-05-27 10:15:43 +00002766 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner788404f2008-02-21 01:32:26 +00002767 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
John McCall53fa7142010-12-24 02:08:15 +00002768 DS.takeAttributes(),
Sebastian Redled0f3b02009-03-15 22:02:01 +00002769 Kind == tok::amp),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002770 SourceLocation());
Bill Wendling3708c182007-05-27 10:15:43 +00002771 }
Chris Lattner6c7416c2006-08-07 00:19:33 +00002772}
2773
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002774/// ParseDirectDeclarator
2775/// direct-declarator: [C99 6.7.5]
Douglas Gregor831c93f2008-11-05 20:51:48 +00002776/// [C99] identifier
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002777/// '(' declarator ')'
2778/// [GNU] '(' attributes declarator ')'
Chris Lattnere8074e62006-08-06 18:30:15 +00002779/// [C90] direct-declarator '[' constant-expression[opt] ']'
2780/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
2781/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
2782/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
2783/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002784/// direct-declarator '(' parameter-type-list ')'
2785/// direct-declarator '(' identifier-list[opt] ')'
2786/// [GNU] direct-declarator '(' parameter-forward-declarations
2787/// parameter-type-list[opt] ')'
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00002788/// [C++] direct-declarator '(' parameter-declaration-clause ')'
2789/// cv-qualifier-seq[opt] exception-specification[opt]
Douglas Gregor61956c42008-10-31 09:07:45 +00002790/// [C++] declarator-id
Douglas Gregor831c93f2008-11-05 20:51:48 +00002791///
2792/// declarator-id: [C++ 8]
Douglas Gregor27b4c162010-12-23 22:44:42 +00002793/// '...'[opt] id-expression
Douglas Gregor831c93f2008-11-05 20:51:48 +00002794/// '::'[opt] nested-name-specifier[opt] type-name
2795///
2796/// id-expression: [C++ 5.1]
2797/// unqualified-id
Douglas Gregord90fd522009-09-25 21:45:23 +00002798/// qualified-id
Douglas Gregor831c93f2008-11-05 20:51:48 +00002799///
2800/// unqualified-id: [C++ 5.1]
Mike Stump11289f42009-09-09 15:08:12 +00002801/// identifier
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002802/// operator-function-id
Douglas Gregord90fd522009-09-25 21:45:23 +00002803/// conversion-function-id
Mike Stump11289f42009-09-09 15:08:12 +00002804/// '~' class-name
Douglas Gregor7f741122009-02-25 19:37:18 +00002805/// template-id
Argyrios Kyrtzidise4426352008-11-07 22:02:30 +00002806///
Chris Lattneracd58a32006-08-06 17:24:14 +00002807void Parser::ParseDirectDeclarator(Declarator &D) {
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00002808 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002809
Douglas Gregor7861a802009-11-03 01:35:08 +00002810 if (getLang().CPlusPlus && D.mayHaveIdentifier()) {
2811 // ParseDeclaratorInternal might already have parsed the scope.
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00002812 if (D.getCXXScopeSpec().isEmpty()) {
John McCallba7bf592010-08-24 05:47:05 +00002813 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec(), ParsedType(), true);
John McCall1f476a12010-02-26 08:45:28 +00002814 }
2815
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00002816 if (D.getCXXScopeSpec().isValid()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00002817 if (Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
John McCall2b058ef2009-12-11 20:04:54 +00002818 // Change the declaration context for name lookup, until this function
2819 // is exited (and the declarator has been parsed).
2820 DeclScopeObj.EnterDeclaratorScope();
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00002821 }
2822
Douglas Gregor27b4c162010-12-23 22:44:42 +00002823 // C++0x [dcl.fct]p14:
2824 // There is a syntactic ambiguity when an ellipsis occurs at the end
2825 // of a parameter-declaration-clause without a preceding comma. In
2826 // this case, the ellipsis is parsed as part of the
2827 // abstract-declarator if the type of the parameter names a template
2828 // parameter pack that has not been expanded; otherwise, it is parsed
2829 // as part of the parameter-declaration-clause.
2830 if (Tok.is(tok::ellipsis) &&
2831 !((D.getContext() == Declarator::PrototypeContext ||
2832 D.getContext() == Declarator::BlockLiteralContext) &&
Douglas Gregor27b4c162010-12-23 22:44:42 +00002833 NextToken().is(tok::r_paren) &&
2834 !Actions.containsUnexpandedParameterPacks(D)))
2835 D.setEllipsisLoc(ConsumeToken());
2836
Douglas Gregor7861a802009-11-03 01:35:08 +00002837 if (Tok.is(tok::identifier) || Tok.is(tok::kw_operator) ||
2838 Tok.is(tok::annot_template_id) || Tok.is(tok::tilde)) {
2839 // We found something that indicates the start of an unqualified-id.
2840 // Parse that unqualified-id.
John McCall84821e72010-04-13 06:39:49 +00002841 bool AllowConstructorName;
2842 if (D.getDeclSpec().hasTypeSpecifier())
2843 AllowConstructorName = false;
2844 else if (D.getCXXScopeSpec().isSet())
2845 AllowConstructorName =
2846 (D.getContext() == Declarator::FileContext ||
2847 (D.getContext() == Declarator::MemberContext &&
2848 D.getDeclSpec().isFriendSpecified()));
2849 else
2850 AllowConstructorName = (D.getContext() == Declarator::MemberContext);
2851
Douglas Gregor7861a802009-11-03 01:35:08 +00002852 if (ParseUnqualifiedId(D.getCXXScopeSpec(),
2853 /*EnteringContext=*/true,
2854 /*AllowDestructorName=*/true,
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002855 AllowConstructorName,
John McCallba7bf592010-08-24 05:47:05 +00002856 ParsedType(),
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00002857 D.getName()) ||
2858 // Once we're past the identifier, if the scope was bad, mark the
2859 // whole declarator bad.
2860 D.getCXXScopeSpec().isInvalid()) {
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00002861 D.SetIdentifier(0, Tok.getLocation());
2862 D.setInvalidType(true);
Douglas Gregor7861a802009-11-03 01:35:08 +00002863 } else {
2864 // Parsed the unqualified-id; update range information and move along.
2865 if (D.getSourceRange().getBegin().isInvalid())
2866 D.SetRangeBegin(D.getName().getSourceRange().getBegin());
2867 D.SetRangeEnd(D.getName().getSourceRange().getEnd());
Douglas Gregordbc5daf2008-11-07 20:08:42 +00002868 }
Douglas Gregor7861a802009-11-03 01:35:08 +00002869 goto PastIdentifier;
Douglas Gregor11d0c4c2008-11-06 22:13:31 +00002870 }
Douglas Gregor7861a802009-11-03 01:35:08 +00002871 } else if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00002872 assert(!getLang().CPlusPlus &&
2873 "There's a C++-specific check for tok::identifier above");
2874 assert(Tok.getIdentifierInfo() && "Not an identifier?");
2875 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
2876 ConsumeToken();
Douglas Gregor7861a802009-11-03 01:35:08 +00002877 goto PastIdentifier;
2878 }
2879
2880 if (Tok.is(tok::l_paren)) {
Chris Lattneracd58a32006-08-06 17:24:14 +00002881 // direct-declarator: '(' declarator ')'
Chris Lattnere37e2332006-08-15 04:50:22 +00002882 // direct-declarator: '(' attributes declarator ')'
Chris Lattneracd58a32006-08-06 17:24:14 +00002883 // Example: 'char (*X)' or 'int (*XX)(void)'
2884 ParseParenDeclarator(D);
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002885
2886 // If the declarator was parenthesized, we entered the declarator
2887 // scope when parsing the parenthesized declarator, then exited
2888 // the scope already. Re-enter the scope, if we need to.
2889 if (D.getCXXScopeSpec().isSet()) {
Fariborz Jahanian358acd52010-08-17 23:50:37 +00002890 // If there was an error parsing parenthesized declarator, declarator
2891 // scope may have been enterred before. Don't do it again.
2892 if (!D.isInvalidType() &&
2893 Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002894 // Change the declaration context for name lookup, until this function
2895 // is exited (and the declarator has been parsed).
Fariborz Jahanian358acd52010-08-17 23:50:37 +00002896 DeclScopeObj.EnterDeclaratorScope();
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002897 }
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00002898 } else if (D.mayOmitIdentifier()) {
Chris Lattneracd58a32006-08-06 17:24:14 +00002899 // This could be something simple like "int" (in which case the declarator
2900 // portion is empty), if an abstract-declarator is allowed.
2901 D.SetIdentifier(0, Tok.getLocation());
2902 } else {
Douglas Gregord9f92e22009-03-06 23:28:18 +00002903 if (D.getContext() == Declarator::MemberContext)
2904 Diag(Tok, diag::err_expected_member_name_or_semi)
2905 << D.getDeclSpec().getSourceRange();
2906 else if (getLang().CPlusPlus)
Douglas Gregor30d60cb2009-11-03 19:44:04 +00002907 Diag(Tok, diag::err_expected_unqualified_id) << getLang().CPlusPlus;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002908 else
Chris Lattner6d29c102008-11-18 07:48:38 +00002909 Diag(Tok, diag::err_expected_ident_lparen);
Chris Lattnereec40f92006-08-06 21:55:29 +00002910 D.SetIdentifier(0, Tok.getLocation());
Chris Lattner8c5dd732008-11-11 06:13:16 +00002911 D.setInvalidType(true);
Chris Lattneracd58a32006-08-06 17:24:14 +00002912 }
Mike Stump11289f42009-09-09 15:08:12 +00002913
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00002914 PastIdentifier:
Chris Lattneracd58a32006-08-06 17:24:14 +00002915 assert(D.isPastIdentifier() &&
2916 "Haven't past the location of the identifier yet?");
Mike Stump11289f42009-09-09 15:08:12 +00002917
Alexis Hunt96d5c762009-11-21 08:43:09 +00002918 // Don't parse attributes unless we have an identifier.
John McCall53fa7142010-12-24 02:08:15 +00002919 if (D.getIdentifier())
2920 MaybeParseCXX0XAttributes(D);
Alexis Hunt96d5c762009-11-21 08:43:09 +00002921
Chris Lattneracd58a32006-08-06 17:24:14 +00002922 while (1) {
Chris Lattner76c72282007-10-09 17:33:22 +00002923 if (Tok.is(tok::l_paren)) {
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00002924 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
2925 // In such a case, check if we actually have a function declarator; if it
2926 // is not, the declarator has been fully parsed.
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002927 if (getLang().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
2928 // When not in file scope, warn for ambiguous function declarators, just
2929 // in case the author intended it as a variable definition.
2930 bool warnIfAmbiguous = D.getContext() != Declarator::FileContext;
2931 if (!isCXXFunctionDeclarator(warnIfAmbiguous))
2932 break;
2933 }
John McCall53fa7142010-12-24 02:08:15 +00002934 ParsedAttributes attrs;
2935 ParseFunctionDeclarator(ConsumeParen(), D, attrs);
Chris Lattner76c72282007-10-09 17:33:22 +00002936 } else if (Tok.is(tok::l_square)) {
Chris Lattnere8074e62006-08-06 18:30:15 +00002937 ParseBracketDeclarator(D);
Chris Lattneracd58a32006-08-06 17:24:14 +00002938 } else {
2939 break;
2940 }
2941 }
2942}
2943
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002944/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
2945/// only called before the identifier, so these are most likely just grouping
Mike Stump11289f42009-09-09 15:08:12 +00002946/// parens for precedence. If we find that these are actually function
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002947/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
2948///
2949/// direct-declarator:
2950/// '(' declarator ')'
2951/// [GNU] '(' attributes declarator ')'
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002952/// direct-declarator '(' parameter-type-list ')'
2953/// direct-declarator '(' identifier-list[opt] ')'
2954/// [GNU] direct-declarator '(' parameter-forward-declarations
2955/// parameter-type-list[opt] ')'
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002956///
2957void Parser::ParseParenDeclarator(Declarator &D) {
2958 SourceLocation StartLoc = ConsumeParen();
2959 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
Mike Stump11289f42009-09-09 15:08:12 +00002960
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002961 // Eat any attributes before we look at whether this is a grouping or function
2962 // declarator paren. If this is a grouping paren, the attribute applies to
2963 // the type being built up, for example:
2964 // int (__attribute__(()) *x)(long y)
2965 // If this ends up not being a grouping paren, the attribute applies to the
2966 // first argument, for example:
2967 // int (__attribute__(()) int x)
2968 // In either case, we need to eat any attributes to be able to determine what
2969 // sort of paren this is.
2970 //
John McCall53fa7142010-12-24 02:08:15 +00002971 ParsedAttributes attrs;
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002972 bool RequiresArg = false;
2973 if (Tok.is(tok::kw___attribute)) {
John McCall53fa7142010-12-24 02:08:15 +00002974 ParseGNUAttributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +00002975
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002976 // We require that the argument list (if this is a non-grouping paren) be
2977 // present even if the attribute list was empty.
2978 RequiresArg = true;
2979 }
Steve Naroff44ac7772008-12-25 14:16:32 +00002980 // Eat any Microsoft extensions.
Eli Friedman53339e02009-06-08 23:27:34 +00002981 if (Tok.is(tok::kw___cdecl) || Tok.is(tok::kw___stdcall) ||
Douglas Gregora941dca2010-05-18 16:57:00 +00002982 Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___fastcall) ||
2983 Tok.is(tok::kw___w64) || Tok.is(tok::kw___ptr64)) {
John McCall53fa7142010-12-24 02:08:15 +00002984 ParseMicrosoftTypeAttributes(attrs);
Eli Friedman53339e02009-06-08 23:27:34 +00002985 }
Dawn Perchik335e16b2010-09-03 01:29:35 +00002986 // Eat any Borland extensions.
Ted Kremenek5eec2b02010-11-10 05:59:39 +00002987 if (Tok.is(tok::kw___pascal))
John McCall53fa7142010-12-24 02:08:15 +00002988 ParseBorlandTypeAttributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +00002989
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002990 // If we haven't past the identifier yet (or where the identifier would be
2991 // stored, if this is an abstract declarator), then this is probably just
2992 // grouping parens. However, if this could be an abstract-declarator, then
2993 // this could also be the start of function arguments (consider 'void()').
2994 bool isGrouping;
Mike Stump11289f42009-09-09 15:08:12 +00002995
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002996 if (!D.mayOmitIdentifier()) {
2997 // If this can't be an abstract-declarator, this *must* be a grouping
2998 // paren, because we haven't seen the identifier yet.
2999 isGrouping = true;
3000 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Argyrios Kyrtzidise8addf52008-10-06 00:07:55 +00003001 (getLang().CPlusPlus && Tok.is(tok::ellipsis)) || // C++ int(...)
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003002 isDeclarationSpecifier()) { // 'int(int)' is a function.
3003 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
3004 // considered to be a type, not a K&R identifier-list.
3005 isGrouping = false;
3006 } else {
3007 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
3008 isGrouping = true;
3009 }
Mike Stump11289f42009-09-09 15:08:12 +00003010
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003011 // If this is a grouping paren, handle:
3012 // direct-declarator: '(' declarator ')'
3013 // direct-declarator: '(' attributes declarator ')'
3014 if (isGrouping) {
Argyrios Kyrtzidis8ae36842008-10-07 10:21:57 +00003015 bool hadGroupingParens = D.hasGroupingParens();
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00003016 D.setGroupingParens(true);
John McCall53fa7142010-12-24 02:08:15 +00003017 if (!attrs.empty())
3018 D.addAttributes(attrs.getList(), SourceLocation());
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00003019
Sebastian Redlbd150f42008-11-21 19:14:01 +00003020 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003021 // Match the ')'.
Abramo Bagnara924a8f32010-12-10 16:29:40 +00003022 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_paren, StartLoc);
3023 D.AddTypeInfo(DeclaratorChunk::getParen(StartLoc, EndLoc), EndLoc);
Argyrios Kyrtzidis8ae36842008-10-07 10:21:57 +00003024
3025 D.setGroupingParens(hadGroupingParens);
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003026 return;
3027 }
Mike Stump11289f42009-09-09 15:08:12 +00003028
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003029 // Okay, if this wasn't a grouping paren, it must be the start of a function
3030 // argument list. Recognize that this declarator will never have an
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003031 // identifier (and remember where it would have been), then call into
3032 // ParseFunctionDeclarator to handle of argument list.
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003033 D.SetIdentifier(0, Tok.getLocation());
3034
John McCall53fa7142010-12-24 02:08:15 +00003035 ParseFunctionDeclarator(StartLoc, D, attrs, RequiresArg);
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003036}
3037
3038/// ParseFunctionDeclarator - We are after the identifier and have parsed the
3039/// declarator D up to a paren, which indicates that we are parsing function
3040/// arguments.
Chris Lattneracd58a32006-08-06 17:24:14 +00003041///
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003042/// If AttrList is non-null, then the caller parsed those arguments immediately
3043/// after the open paren - they should be considered to be the first argument of
3044/// a parameter. If RequiresArg is true, then the first argument of the
3045/// function is required to be present and required to not be an identifier
3046/// list.
3047///
Chris Lattneracd58a32006-08-06 17:24:14 +00003048/// This method also handles this portion of the grammar:
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003049/// parameter-type-list: [C99 6.7.5]
3050/// parameter-list
3051/// parameter-list ',' '...'
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00003052/// [C++] parameter-list '...'
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003053///
3054/// parameter-list: [C99 6.7.5]
3055/// parameter-declaration
3056/// parameter-list ',' parameter-declaration
3057///
3058/// parameter-declaration: [C99 6.7.5]
3059/// declaration-specifiers declarator
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00003060/// [C++] declaration-specifiers declarator '=' assignment-expression
Chris Lattnere37e2332006-08-15 04:50:22 +00003061/// [GNU] declaration-specifiers declarator attributes
Sebastian Redlf769df52009-03-24 22:27:57 +00003062/// declaration-specifiers abstract-declarator[opt]
3063/// [C++] declaration-specifiers abstract-declarator[opt]
Chris Lattner58258242008-04-10 02:22:51 +00003064/// '=' assignment-expression
Chris Lattnere37e2332006-08-15 04:50:22 +00003065/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003066///
Douglas Gregor54992352011-01-26 03:43:54 +00003067/// For C++, after the parameter-list, it also parses "cv-qualifier-seq[opt]",
3068/// C++0x "ref-qualifier[opt]" and "exception-specification[opt]".
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003069///
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003070void Parser::ParseFunctionDeclarator(SourceLocation LParenLoc, Declarator &D,
John McCall53fa7142010-12-24 02:08:15 +00003071 ParsedAttributes &attrs,
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003072 bool RequiresArg) {
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003073 // lparen is already consumed!
3074 assert(D.isPastIdentifier() && "Should not call before identifier!");
Mike Stump11289f42009-09-09 15:08:12 +00003075
Douglas Gregor7fb25412010-10-01 18:44:50 +00003076 ParsedType TrailingReturnType;
3077
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003078 // This parameter list may be empty.
Chris Lattner76c72282007-10-09 17:33:22 +00003079 if (Tok.is(tok::r_paren)) {
Ted Kremenek5eec2b02010-11-10 05:59:39 +00003080 if (RequiresArg)
Chris Lattner6d29c102008-11-18 07:48:38 +00003081 Diag(Tok, diag::err_argument_required_after_attribute);
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003082
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00003083 SourceLocation RParenLoc = ConsumeParen(); // Eat the closing ')'.
3084 SourceLocation EndLoc = RParenLoc;
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003085
3086 // cv-qualifier-seq[opt].
3087 DeclSpec DS;
Douglas Gregor54992352011-01-26 03:43:54 +00003088 SourceLocation RefQualifierLoc;
3089 bool RefQualifierIsLValueRef = true;
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00003090 bool hasExceptionSpec = false;
Sebastian Redlfb3f1792009-05-31 11:47:27 +00003091 SourceLocation ThrowLoc;
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00003092 bool hasAnyExceptionSpec = false;
John McCallba7bf592010-08-24 05:47:05 +00003093 llvm::SmallVector<ParsedType, 2> Exceptions;
Sebastian Redld6434562009-05-29 18:02:33 +00003094 llvm::SmallVector<SourceRange, 2> ExceptionRanges;
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003095 if (getLang().CPlusPlus) {
John McCall53fa7142010-12-24 02:08:15 +00003096 MaybeParseCXX0XAttributes(attrs);
3097
Chris Lattnercf0bab22008-12-18 07:02:59 +00003098 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003099 if (!DS.getSourceRange().getEnd().isInvalid())
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00003100 EndLoc = DS.getSourceRange().getEnd();
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003101
Douglas Gregor54992352011-01-26 03:43:54 +00003102 // Parse ref-qualifier[opt]
3103 if (Tok.is(tok::amp) || Tok.is(tok::ampamp)) {
3104 if (!getLang().CPlusPlus0x)
Douglas Gregora5271302011-01-26 20:35:32 +00003105 Diag(Tok, diag::ext_ref_qualifier);
Douglas Gregor54992352011-01-26 03:43:54 +00003106
3107 RefQualifierIsLValueRef = Tok.is(tok::amp);
3108 RefQualifierLoc = ConsumeToken();
3109 EndLoc = RefQualifierLoc;
3110 }
3111
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003112 // Parse exception-specification[opt].
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00003113 if (Tok.is(tok::kw_throw)) {
3114 hasExceptionSpec = true;
Sebastian Redlfb3f1792009-05-31 11:47:27 +00003115 ThrowLoc = Tok.getLocation();
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00003116 ParseExceptionSpecification(EndLoc, Exceptions, ExceptionRanges,
Sebastian Redld6434562009-05-29 18:02:33 +00003117 hasAnyExceptionSpec);
3118 assert(Exceptions.size() == ExceptionRanges.size() &&
3119 "Produced different number of exception types and ranges.");
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00003120 }
Douglas Gregor7fb25412010-10-01 18:44:50 +00003121
3122 // Parse trailing-return-type.
3123 if (getLang().CPlusPlus0x && Tok.is(tok::arrow)) {
3124 TrailingReturnType = ParseTrailingReturnType().get();
3125 }
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003126 }
3127
Chris Lattner371ed4e2008-04-06 06:57:35 +00003128 // Remember that we parsed a function type, and remember the attributes.
Chris Lattneracd58a32006-08-06 17:24:14 +00003129 // int() -> no prototype, no '...'.
John McCall53fa7142010-12-24 02:08:15 +00003130 D.AddTypeInfo(DeclaratorChunk::getFunction(attrs,
3131 /*prototype*/getLang().CPlusPlus,
Chris Lattner371ed4e2008-04-06 06:57:35 +00003132 /*variadic*/ false,
Douglas Gregor94349fd2009-02-18 07:07:28 +00003133 SourceLocation(),
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003134 /*arglist*/ 0, 0,
3135 DS.getTypeQualifiers(),
Douglas Gregor54992352011-01-26 03:43:54 +00003136 RefQualifierIsLValueRef,
3137 RefQualifierLoc,
Sebastian Redlfb3f1792009-05-31 11:47:27 +00003138 hasExceptionSpec, ThrowLoc,
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00003139 hasAnyExceptionSpec,
Sebastian Redld6434562009-05-29 18:02:33 +00003140 Exceptions.data(),
3141 ExceptionRanges.data(),
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00003142 Exceptions.size(),
Douglas Gregor7fb25412010-10-01 18:44:50 +00003143 LParenLoc, RParenLoc, D,
3144 TrailingReturnType),
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00003145 EndLoc);
Chris Lattner371ed4e2008-04-06 06:57:35 +00003146 return;
Sebastian Redld6434562009-05-29 18:02:33 +00003147 }
3148
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003149 // Alternatively, this parameter list may be an identifier list form for a
3150 // K&R-style function: void foo(a,b,c)
John Thompson22334602010-02-05 00:12:22 +00003151 if (!getLang().CPlusPlus && Tok.is(tok::identifier)
3152 && !TryAltiVecVectorToken()) {
John McCall1f476a12010-02-26 08:45:28 +00003153 if (TryAnnotateTypeOrScopeToken() || !Tok.is(tok::annot_typename)) {
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003154 // K&R identifier lists can't have typedefs as identifiers, per
3155 // C99 6.7.5.3p11.
Ted Kremenek5eec2b02010-11-10 05:59:39 +00003156 if (RequiresArg)
Steve Naroffb0486722009-01-28 19:16:40 +00003157 Diag(Tok, diag::err_argument_required_after_attribute);
Chris Lattner9453ab82010-05-14 17:23:36 +00003158
Steve Naroffb0486722009-01-28 19:16:40 +00003159 // Identifier list. Note that '(' identifier-list ')' is only allowed for
Chris Lattner9453ab82010-05-14 17:23:36 +00003160 // normal declarators, not for abstract-declarators. Get the first
3161 // identifier.
Chris Lattnerff895c12010-05-14 17:44:56 +00003162 Token FirstTok = Tok;
Chris Lattner9453ab82010-05-14 17:23:36 +00003163 ConsumeToken(); // eat the first identifier.
Chris Lattnerff895c12010-05-14 17:44:56 +00003164
3165 // Identifier lists follow a really simple grammar: the identifiers can
3166 // be followed *only* by a ", moreidentifiers" or ")". However, K&R
3167 // identifier lists are really rare in the brave new modern world, and it
3168 // is very common for someone to typo a type in a non-k&r style list. If
3169 // we are presented with something like: "void foo(intptr x, float y)",
3170 // we don't want to start parsing the function declarator as though it is
3171 // a K&R style declarator just because intptr is an invalid type.
3172 //
3173 // To handle this, we check to see if the token after the first identifier
3174 // is a "," or ")". Only if so, do we parse it as an identifier list.
3175 if (Tok.is(tok::comma) || Tok.is(tok::r_paren))
3176 return ParseFunctionDeclaratorIdentifierList(LParenLoc,
3177 FirstTok.getIdentifierInfo(),
3178 FirstTok.getLocation(), D);
3179
3180 // If we get here, the code is invalid. Push the first identifier back
3181 // into the token stream and parse the first argument as an (invalid)
3182 // normal argument declarator.
3183 PP.EnterToken(Tok);
3184 Tok = FirstTok;
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003185 }
Chris Lattner371ed4e2008-04-06 06:57:35 +00003186 }
Mike Stump11289f42009-09-09 15:08:12 +00003187
Chris Lattner371ed4e2008-04-06 06:57:35 +00003188 // Finally, a normal, non-empty parameter type list.
Mike Stump11289f42009-09-09 15:08:12 +00003189
Chris Lattner371ed4e2008-04-06 06:57:35 +00003190 // Build up an array of information about the parsed arguments.
3191 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00003192
3193 // Enter function-declaration scope, limiting any declarators to the
3194 // function prototype scope, including parameter declarators.
Chris Lattnerbd61a952009-03-05 00:00:31 +00003195 ParseScope PrototypeScope(this,
3196 Scope::FunctionPrototypeScope|Scope::DeclScope);
Mike Stump11289f42009-09-09 15:08:12 +00003197
Chris Lattner371ed4e2008-04-06 06:57:35 +00003198 bool IsVariadic = false;
Douglas Gregor94349fd2009-02-18 07:07:28 +00003199 SourceLocation EllipsisLoc;
Chris Lattner371ed4e2008-04-06 06:57:35 +00003200 while (1) {
3201 if (Tok.is(tok::ellipsis)) {
3202 IsVariadic = true;
Douglas Gregor94349fd2009-02-18 07:07:28 +00003203 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chris Lattner371ed4e2008-04-06 06:57:35 +00003204 break;
Chris Lattneracd58a32006-08-06 17:24:14 +00003205 }
Mike Stump11289f42009-09-09 15:08:12 +00003206
Chris Lattner371ed4e2008-04-06 06:57:35 +00003207 // Parse the declaration-specifiers.
John McCall28a6aea2009-11-04 02:18:39 +00003208 // Just use the ParsingDeclaration "scope" of the declarator.
Chris Lattner371ed4e2008-04-06 06:57:35 +00003209 DeclSpec DS;
John McCall53fa7142010-12-24 02:08:15 +00003210
3211 // Skip any Microsoft attributes before a param.
3212 if (getLang().Microsoft && Tok.is(tok::l_square))
3213 ParseMicrosoftAttributes(DS.getAttributes());
3214
3215 SourceLocation DSStart = Tok.getLocation();
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003216
3217 // If the caller parsed attributes for the first argument, add them now.
John McCall53fa7142010-12-24 02:08:15 +00003218 // Take them so that we only apply the attributes to the first parameter.
3219 DS.takeAttributesFrom(attrs);
3220
Chris Lattnerde39c3e2009-02-27 18:38:20 +00003221 ParseDeclarationSpecifiers(DS);
Mike Stump11289f42009-09-09 15:08:12 +00003222
Chris Lattner371ed4e2008-04-06 06:57:35 +00003223 // Parse the declarator. This is "PrototypeContext", because we must
3224 // accept either 'declarator' or 'abstract-declarator' here.
3225 Declarator ParmDecl(DS, Declarator::PrototypeContext);
3226 ParseDeclarator(ParmDecl);
3227
3228 // Parse GNU attributes, if present.
John McCall53fa7142010-12-24 02:08:15 +00003229 MaybeParseGNUAttributes(ParmDecl);
Mike Stump11289f42009-09-09 15:08:12 +00003230
Chris Lattner371ed4e2008-04-06 06:57:35 +00003231 // Remember this parsed parameter in ParamInfo.
3232 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
Mike Stump11289f42009-09-09 15:08:12 +00003233
Douglas Gregor4d87df52008-12-16 21:30:33 +00003234 // DefArgToks is used when the parsing of default arguments needs
3235 // to be delayed.
3236 CachedTokens *DefArgToks = 0;
3237
Chris Lattner371ed4e2008-04-06 06:57:35 +00003238 // If no parameter was specified, verify that *something* was specified,
3239 // otherwise we have a missing type and identifier.
Chris Lattnerde39c3e2009-02-27 18:38:20 +00003240 if (DS.isEmpty() && ParmDecl.getIdentifier() == 0 &&
3241 ParmDecl.getNumTypeObjects() == 0) {
Chris Lattner371ed4e2008-04-06 06:57:35 +00003242 // Completely missing, emit error.
3243 Diag(DSStart, diag::err_missing_param);
3244 } else {
3245 // Otherwise, we have something. Add it and let semantic analysis try
3246 // to grok it and add the result to the ParamInfo we are building.
Mike Stump11289f42009-09-09 15:08:12 +00003247
Chris Lattner371ed4e2008-04-06 06:57:35 +00003248 // Inform the actions module about the parameter declarator, so it gets
3249 // added to the current scope.
John McCall48871652010-08-21 09:40:31 +00003250 Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl);
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00003251
3252 // Parse the default argument, if any. We parse the default
3253 // arguments in all dialects; the semantic analysis in
3254 // ActOnParamDefaultArgument will reject the default argument in
3255 // C.
3256 if (Tok.is(tok::equal)) {
Douglas Gregor58354032008-12-24 00:01:03 +00003257 SourceLocation EqualLoc = Tok.getLocation();
3258
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00003259 // Parse the default argument
Douglas Gregor4d87df52008-12-16 21:30:33 +00003260 if (D.getContext() == Declarator::MemberContext) {
3261 // If we're inside a class definition, cache the tokens
3262 // corresponding to the default argument. We'll actually parse
3263 // them when we see the end of the class definition.
3264 // FIXME: Templates will require something similar.
3265 // FIXME: Can we use a smart pointer for Toks?
3266 DefArgToks = new CachedTokens;
3267
Mike Stump11289f42009-09-09 15:08:12 +00003268 if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks,
Argyrios Kyrtzidis8d7bdba2010-04-23 21:20:12 +00003269 /*StopAtSemi=*/true,
3270 /*ConsumeFinalToken=*/false)) {
Douglas Gregor4d87df52008-12-16 21:30:33 +00003271 delete DefArgToks;
3272 DefArgToks = 0;
Douglas Gregor58354032008-12-24 00:01:03 +00003273 Actions.ActOnParamDefaultArgumentError(Param);
Argyrios Kyrtzidis249179c2010-08-06 09:47:24 +00003274 } else {
3275 // Mark the end of the default argument so that we know when to
3276 // stop when we parse it later on.
3277 Token DefArgEnd;
3278 DefArgEnd.startToken();
3279 DefArgEnd.setKind(tok::cxx_defaultarg_end);
3280 DefArgEnd.setLocation(Tok.getLocation());
3281 DefArgToks->push_back(DefArgEnd);
Mike Stump11289f42009-09-09 15:08:12 +00003282 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc,
Anders Carlsson84613c42009-06-12 16:51:40 +00003283 (*DefArgToks)[1].getLocation());
Argyrios Kyrtzidis249179c2010-08-06 09:47:24 +00003284 }
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00003285 } else {
Douglas Gregor4d87df52008-12-16 21:30:33 +00003286 // Consume the '='.
Douglas Gregor58354032008-12-24 00:01:03 +00003287 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00003288
Douglas Gregor8a01b2a2010-09-11 20:24:53 +00003289 // The argument isn't actually potentially evaluated unless it is
3290 // used.
3291 EnterExpressionEvaluationContext Eval(Actions,
3292 Sema::PotentiallyEvaluatedIfUsed);
3293
John McCalldadc5752010-08-24 06:29:42 +00003294 ExprResult DefArgResult(ParseAssignmentExpression());
Douglas Gregor4d87df52008-12-16 21:30:33 +00003295 if (DefArgResult.isInvalid()) {
3296 Actions.ActOnParamDefaultArgumentError(Param);
3297 SkipUntil(tok::comma, tok::r_paren, true, true);
3298 } else {
3299 // Inform the actions module about the default argument
3300 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
John McCallb268a282010-08-23 23:25:46 +00003301 DefArgResult.take());
Douglas Gregor4d87df52008-12-16 21:30:33 +00003302 }
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00003303 }
3304 }
Mike Stump11289f42009-09-09 15:08:12 +00003305
3306 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
3307 ParmDecl.getIdentifierLoc(), Param,
Douglas Gregor4d87df52008-12-16 21:30:33 +00003308 DefArgToks));
Chris Lattner371ed4e2008-04-06 06:57:35 +00003309 }
3310
3311 // If the next token is a comma, consume it and keep reading arguments.
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00003312 if (Tok.isNot(tok::comma)) {
3313 if (Tok.is(tok::ellipsis)) {
3314 IsVariadic = true;
3315 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
3316
3317 if (!getLang().CPlusPlus) {
3318 // We have ellipsis without a preceding ',', which is ill-formed
3319 // in C. Complain and provide the fix.
3320 Diag(EllipsisLoc, diag::err_missing_comma_before_ellipsis)
Douglas Gregora771f462010-03-31 17:46:05 +00003321 << FixItHint::CreateInsertion(EllipsisLoc, ", ");
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00003322 }
3323 }
3324
3325 break;
3326 }
Mike Stump11289f42009-09-09 15:08:12 +00003327
Chris Lattner371ed4e2008-04-06 06:57:35 +00003328 // Consume the comma.
3329 ConsumeToken();
Chris Lattneracd58a32006-08-06 17:24:14 +00003330 }
Mike Stump11289f42009-09-09 15:08:12 +00003331
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003332 // If we have the closing ')', eat it.
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00003333 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
3334 SourceLocation EndLoc = RParenLoc;
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003335
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003336 DeclSpec DS;
Douglas Gregor54992352011-01-26 03:43:54 +00003337 SourceLocation RefQualifierLoc;
3338 bool RefQualifierIsLValueRef = true;
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00003339 bool hasExceptionSpec = false;
Sebastian Redlfb3f1792009-05-31 11:47:27 +00003340 SourceLocation ThrowLoc;
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00003341 bool hasAnyExceptionSpec = false;
John McCallba7bf592010-08-24 05:47:05 +00003342 llvm::SmallVector<ParsedType, 2> Exceptions;
Sebastian Redld6434562009-05-29 18:02:33 +00003343 llvm::SmallVector<SourceRange, 2> ExceptionRanges;
Alexis Hunt96d5c762009-11-21 08:43:09 +00003344
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003345 if (getLang().CPlusPlus) {
John McCall53fa7142010-12-24 02:08:15 +00003346 MaybeParseCXX0XAttributes(attrs);
3347
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003348 // Parse cv-qualifier-seq[opt].
Chris Lattnercf0bab22008-12-18 07:02:59 +00003349 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003350 if (!DS.getSourceRange().getEnd().isInvalid())
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00003351 EndLoc = DS.getSourceRange().getEnd();
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003352
Douglas Gregor54992352011-01-26 03:43:54 +00003353 // Parse ref-qualifier[opt]
3354 if (Tok.is(tok::amp) || Tok.is(tok::ampamp)) {
3355 if (!getLang().CPlusPlus0x)
Douglas Gregora5271302011-01-26 20:35:32 +00003356 Diag(Tok, diag::ext_ref_qualifier);
Douglas Gregor54992352011-01-26 03:43:54 +00003357
3358 RefQualifierIsLValueRef = Tok.is(tok::amp);
3359 RefQualifierLoc = ConsumeToken();
3360 EndLoc = RefQualifierLoc;
3361 }
3362
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003363 // Parse exception-specification[opt].
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00003364 if (Tok.is(tok::kw_throw)) {
3365 hasExceptionSpec = true;
Sebastian Redlfb3f1792009-05-31 11:47:27 +00003366 ThrowLoc = Tok.getLocation();
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00003367 ParseExceptionSpecification(EndLoc, Exceptions, ExceptionRanges,
Sebastian Redld6434562009-05-29 18:02:33 +00003368 hasAnyExceptionSpec);
3369 assert(Exceptions.size() == ExceptionRanges.size() &&
3370 "Produced different number of exception types and ranges.");
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00003371 }
Douglas Gregor7fb25412010-10-01 18:44:50 +00003372
3373 // Parse trailing-return-type.
3374 if (getLang().CPlusPlus0x && Tok.is(tok::arrow)) {
3375 TrailingReturnType = ParseTrailingReturnType().get();
3376 }
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003377 }
3378
Douglas Gregor7fb25412010-10-01 18:44:50 +00003379 // FIXME: We should leave the prototype scope before parsing the exception
3380 // specification, and then reenter it when parsing the trailing return type.
3381
3382 // Leave prototype scope.
3383 PrototypeScope.Exit();
3384
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00003385 // Remember that we parsed a function type, and remember the attributes.
John McCall53fa7142010-12-24 02:08:15 +00003386 D.AddTypeInfo(DeclaratorChunk::getFunction(attrs,
3387 /*proto*/true, IsVariadic,
Douglas Gregor94349fd2009-02-18 07:07:28 +00003388 EllipsisLoc,
Jay Foad7d0479f2009-05-21 09:52:38 +00003389 ParamInfo.data(), ParamInfo.size(),
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003390 DS.getTypeQualifiers(),
Douglas Gregor54992352011-01-26 03:43:54 +00003391 RefQualifierIsLValueRef,
3392 RefQualifierLoc,
Sebastian Redlfb3f1792009-05-31 11:47:27 +00003393 hasExceptionSpec, ThrowLoc,
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00003394 hasAnyExceptionSpec,
Sebastian Redld6434562009-05-29 18:02:33 +00003395 Exceptions.data(),
3396 ExceptionRanges.data(),
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00003397 Exceptions.size(),
Douglas Gregor7fb25412010-10-01 18:44:50 +00003398 LParenLoc, RParenLoc, D,
3399 TrailingReturnType),
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00003400 EndLoc);
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003401}
Chris Lattneracd58a32006-08-06 17:24:14 +00003402
Chris Lattner6c940e62008-04-06 06:34:08 +00003403/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
3404/// we found a K&R-style identifier list instead of a type argument list. The
Chris Lattner9453ab82010-05-14 17:23:36 +00003405/// first identifier has already been consumed, and the current token is the
3406/// token right after it.
Chris Lattner6c940e62008-04-06 06:34:08 +00003407///
3408/// identifier-list: [C99 6.7.5]
3409/// identifier
3410/// identifier-list ',' identifier
3411///
3412void Parser::ParseFunctionDeclaratorIdentifierList(SourceLocation LParenLoc,
Chris Lattner9453ab82010-05-14 17:23:36 +00003413 IdentifierInfo *FirstIdent,
3414 SourceLocation FirstIdentLoc,
Chris Lattner6c940e62008-04-06 06:34:08 +00003415 Declarator &D) {
3416 // Build up an array of information about the parsed arguments.
3417 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
3418 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
Mike Stump11289f42009-09-09 15:08:12 +00003419
Chris Lattner6c940e62008-04-06 06:34:08 +00003420 // If there was no identifier specified for the declarator, either we are in
3421 // an abstract-declarator, or we are in a parameter declarator which was found
3422 // to be abstract. In abstract-declarators, identifier lists are not valid:
3423 // diagnose this.
3424 if (!D.getIdentifier())
Chris Lattner9453ab82010-05-14 17:23:36 +00003425 Diag(FirstIdentLoc, diag::ext_ident_list_in_param);
Chris Lattner6c940e62008-04-06 06:34:08 +00003426
Chris Lattner9453ab82010-05-14 17:23:36 +00003427 // The first identifier was already read, and is known to be the first
3428 // identifier in the list. Remember this identifier in ParamInfo.
3429 ParamsSoFar.insert(FirstIdent);
John McCall48871652010-08-21 09:40:31 +00003430 ParamInfo.push_back(DeclaratorChunk::ParamInfo(FirstIdent, FirstIdentLoc, 0));
Mike Stump11289f42009-09-09 15:08:12 +00003431
Chris Lattner6c940e62008-04-06 06:34:08 +00003432 while (Tok.is(tok::comma)) {
3433 // Eat the comma.
3434 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00003435
Chris Lattner9186f552008-04-06 06:39:19 +00003436 // If this isn't an identifier, report the error and skip until ')'.
Chris Lattner6c940e62008-04-06 06:34:08 +00003437 if (Tok.isNot(tok::identifier)) {
3438 Diag(Tok, diag::err_expected_ident);
Chris Lattner9186f552008-04-06 06:39:19 +00003439 SkipUntil(tok::r_paren);
3440 return;
Chris Lattner6c940e62008-04-06 06:34:08 +00003441 }
Chris Lattner67b450c2008-04-06 06:47:48 +00003442
Chris Lattner6c940e62008-04-06 06:34:08 +00003443 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
Chris Lattner67b450c2008-04-06 06:47:48 +00003444
3445 // Reject 'typedef int y; int test(x, y)', but continue parsing.
Douglas Gregor0be31a22010-07-02 17:43:08 +00003446 if (Actions.getTypeName(*ParmII, Tok.getLocation(), getCurScope()))
Chris Lattnerebad6a22008-11-19 07:37:42 +00003447 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
Mike Stump11289f42009-09-09 15:08:12 +00003448
Chris Lattner6c940e62008-04-06 06:34:08 +00003449 // Verify that the argument identifier has not already been mentioned.
3450 if (!ParamsSoFar.insert(ParmII)) {
Chris Lattnerebad6a22008-11-19 07:37:42 +00003451 Diag(Tok, diag::err_param_redefinition) << ParmII;
Chris Lattner9186f552008-04-06 06:39:19 +00003452 } else {
3453 // Remember this identifier in ParamInfo.
Chris Lattner6c940e62008-04-06 06:34:08 +00003454 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Chris Lattner83f095c2009-03-28 19:18:32 +00003455 Tok.getLocation(),
John McCall48871652010-08-21 09:40:31 +00003456 0));
Chris Lattner9186f552008-04-06 06:39:19 +00003457 }
Mike Stump11289f42009-09-09 15:08:12 +00003458
Chris Lattner6c940e62008-04-06 06:34:08 +00003459 // Eat the identifier.
3460 ConsumeToken();
3461 }
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003462
3463 // If we have the closing ')', eat it and we're done.
3464 SourceLocation RLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
3465
Chris Lattner9186f552008-04-06 06:39:19 +00003466 // Remember that we parsed a function type, and remember the attributes. This
3467 // function type is always a K&R style function type, which is not varargs and
3468 // has no prototype.
John McCall53fa7142010-12-24 02:08:15 +00003469 D.AddTypeInfo(DeclaratorChunk::getFunction(ParsedAttributes(),
3470 /*proto*/false, /*varargs*/false,
Douglas Gregor94349fd2009-02-18 07:07:28 +00003471 SourceLocation(),
Chris Lattner9186f552008-04-06 06:39:19 +00003472 &ParamInfo[0], ParamInfo.size(),
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00003473 /*TypeQuals*/0,
Douglas Gregor54992352011-01-26 03:43:54 +00003474 true, SourceLocation(),
Sebastian Redlfb3f1792009-05-31 11:47:27 +00003475 /*exception*/false,
3476 SourceLocation(), false, 0, 0, 0,
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00003477 LParenLoc, RLoc, D),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003478 RLoc);
Chris Lattner6c940e62008-04-06 06:34:08 +00003479}
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003480
Chris Lattnere8074e62006-08-06 18:30:15 +00003481/// [C90] direct-declarator '[' constant-expression[opt] ']'
3482/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
3483/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
3484/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
3485/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
3486void Parser::ParseBracketDeclarator(Declarator &D) {
Chris Lattner04132372006-10-16 06:12:55 +00003487 SourceLocation StartLoc = ConsumeBracket();
Mike Stump11289f42009-09-09 15:08:12 +00003488
Chris Lattner84a11622008-12-18 07:27:21 +00003489 // C array syntax has many features, but by-far the most common is [] and [4].
3490 // This code does a fast path to handle some of the most obvious cases.
3491 if (Tok.getKind() == tok::r_square) {
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003492 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
John McCall53fa7142010-12-24 02:08:15 +00003493 ParsedAttributes attrs;
3494 MaybeParseCXX0XAttributes(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +00003495
Chris Lattner84a11622008-12-18 07:27:21 +00003496 // Remember that we parsed the empty array type.
John McCalldadc5752010-08-24 06:29:42 +00003497 ExprResult NumElements;
John McCall53fa7142010-12-24 02:08:15 +00003498 D.AddTypeInfo(DeclaratorChunk::getArray(0, attrs, false, false, 0,
Douglas Gregor04318252009-07-06 15:59:29 +00003499 StartLoc, EndLoc),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003500 EndLoc);
Chris Lattner84a11622008-12-18 07:27:21 +00003501 return;
3502 } else if (Tok.getKind() == tok::numeric_constant &&
3503 GetLookAheadToken(1).is(tok::r_square)) {
3504 // [4] is very common. Parse the numeric constant expression.
John McCalldadc5752010-08-24 06:29:42 +00003505 ExprResult ExprRes(Actions.ActOnNumericConstant(Tok));
Chris Lattner84a11622008-12-18 07:27:21 +00003506 ConsumeToken();
3507
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003508 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
John McCall53fa7142010-12-24 02:08:15 +00003509 ParsedAttributes attrs;
3510 MaybeParseCXX0XAttributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +00003511
Chris Lattner84a11622008-12-18 07:27:21 +00003512 // Remember that we parsed a array type, and remember its features.
John McCall53fa7142010-12-24 02:08:15 +00003513 D.AddTypeInfo(DeclaratorChunk::getArray(0, attrs, false, 0,
3514 ExprRes.release(),
Douglas Gregor04318252009-07-06 15:59:29 +00003515 StartLoc, EndLoc),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003516 EndLoc);
Chris Lattner84a11622008-12-18 07:27:21 +00003517 return;
3518 }
Mike Stump11289f42009-09-09 15:08:12 +00003519
Chris Lattnere8074e62006-08-06 18:30:15 +00003520 // If valid, this location is the position where we read the 'static' keyword.
3521 SourceLocation StaticLoc;
Chris Lattner76c72282007-10-09 17:33:22 +00003522 if (Tok.is(tok::kw_static))
Chris Lattneraf635312006-10-16 06:06:51 +00003523 StaticLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00003524
Chris Lattnere8074e62006-08-06 18:30:15 +00003525 // If there is a type-qualifier-list, read it now.
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00003526 // Type qualifiers in an array subscript are a C99 feature.
Chris Lattnere8074e62006-08-06 18:30:15 +00003527 DeclSpec DS;
Chris Lattnercf0bab22008-12-18 07:02:59 +00003528 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Mike Stump11289f42009-09-09 15:08:12 +00003529
Chris Lattnere8074e62006-08-06 18:30:15 +00003530 // If we haven't already read 'static', check to see if there is one after the
3531 // type-qualifier-list.
Chris Lattner76c72282007-10-09 17:33:22 +00003532 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Chris Lattneraf635312006-10-16 06:06:51 +00003533 StaticLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00003534
Chris Lattnere8074e62006-08-06 18:30:15 +00003535 // Handle "direct-declarator [ type-qual-list[opt] * ]".
Chris Lattnere8074e62006-08-06 18:30:15 +00003536 bool isStar = false;
John McCalldadc5752010-08-24 06:29:42 +00003537 ExprResult NumElements;
Mike Stump11289f42009-09-09 15:08:12 +00003538
Chris Lattner521ff2b2008-04-06 05:26:30 +00003539 // Handle the case where we have '[*]' as the array size. However, a leading
3540 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
3541 // the the token after the star is a ']'. Since stars in arrays are
3542 // infrequent, use of lookahead is not costly here.
3543 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattnerc439f0d2008-04-06 05:27:21 +00003544 ConsumeToken(); // Eat the '*'.
Chris Lattner1906f802006-08-06 19:14:46 +00003545
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00003546 if (StaticLoc.isValid()) {
Chris Lattner521ff2b2008-04-06 05:26:30 +00003547 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00003548 StaticLoc = SourceLocation(); // Drop the static.
3549 }
Chris Lattner521ff2b2008-04-06 05:26:30 +00003550 isStar = true;
Chris Lattner76c72282007-10-09 17:33:22 +00003551 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner84a11622008-12-18 07:27:21 +00003552 // Note, in C89, this production uses the constant-expr production instead
3553 // of assignment-expr. The only difference is that assignment-expr allows
3554 // things like '=' and '*='. Sema rejects these in C89 mode because they
3555 // are not i-c-e's, so we don't need to distinguish between the two here.
Mike Stump11289f42009-09-09 15:08:12 +00003556
Douglas Gregorc9c02ed2009-06-19 23:52:42 +00003557 // Parse the constant-expression or assignment-expression now (depending
3558 // on dialect).
3559 if (getLang().CPlusPlus)
3560 NumElements = ParseConstantExpression();
3561 else
3562 NumElements = ParseAssignmentExpression();
Chris Lattner62591722006-08-12 18:40:58 +00003563 }
Mike Stump11289f42009-09-09 15:08:12 +00003564
Chris Lattner62591722006-08-12 18:40:58 +00003565 // If there was an error parsing the assignment-expression, recover.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00003566 if (NumElements.isInvalid()) {
Chris Lattnercd2a8c52009-04-24 22:30:50 +00003567 D.setInvalidType(true);
Chris Lattner62591722006-08-12 18:40:58 +00003568 // If the expression was invalid, skip it.
3569 SkipUntil(tok::r_square);
3570 return;
Chris Lattnere8074e62006-08-06 18:30:15 +00003571 }
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003572
3573 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
3574
John McCall53fa7142010-12-24 02:08:15 +00003575 ParsedAttributes attrs;
3576 MaybeParseCXX0XAttributes(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +00003577
Chris Lattner84a11622008-12-18 07:27:21 +00003578 // Remember that we parsed a array type, and remember its features.
John McCall53fa7142010-12-24 02:08:15 +00003579 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(), attrs,
Chris Lattnercbc426d2006-12-02 06:43:02 +00003580 StaticLoc.isValid(), isStar,
Douglas Gregor04318252009-07-06 15:59:29 +00003581 NumElements.release(),
3582 StartLoc, EndLoc),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003583 EndLoc);
Chris Lattnere8074e62006-08-06 18:30:15 +00003584}
3585
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00003586/// [GNU] typeof-specifier:
3587/// typeof ( expressions )
3588/// typeof ( type-name )
3589/// [GNU/C++] typeof unary-expression
Steve Naroffad373bd2007-07-31 12:34:36 +00003590///
3591void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner76c72282007-10-09 17:33:22 +00003592 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00003593 Token OpTok = Tok;
Steve Naroffad373bd2007-07-31 12:34:36 +00003594 SourceLocation StartLoc = ConsumeToken();
3595
John McCalle8595032010-01-13 20:03:27 +00003596 const bool hasParens = Tok.is(tok::l_paren);
3597
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00003598 bool isCastExpr;
John McCallba7bf592010-08-24 05:47:05 +00003599 ParsedType CastTy;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00003600 SourceRange CastRange;
John McCalldadc5752010-08-24 06:29:42 +00003601 ExprResult Operand = ParseExprAfterTypeofSizeofAlignof(OpTok,
John McCall6caebb12010-08-25 02:45:51 +00003602 isCastExpr,
3603 CastTy,
3604 CastRange);
John McCalle8595032010-01-13 20:03:27 +00003605 if (hasParens)
3606 DS.setTypeofParensRange(CastRange);
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00003607
3608 if (CastRange.getEnd().isInvalid())
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00003609 // FIXME: Not accurate, the range gets one token more than it should.
3610 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00003611 else
3612 DS.SetRangeEnd(CastRange.getEnd());
Mike Stump11289f42009-09-09 15:08:12 +00003613
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00003614 if (isCastExpr) {
3615 if (!CastTy) {
3616 DS.SetTypeSpecError();
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00003617 return;
Douglas Gregor220cac52009-02-18 17:45:20 +00003618 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00003619
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00003620 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00003621 unsigned DiagID;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00003622 // Check for duplicate type specifiers (e.g. "int typeof(int)").
3623 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00003624 DiagID, CastTy))
3625 Diag(StartLoc, DiagID) << PrevSpec;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00003626 return;
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00003627 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00003628
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00003629 // If we get here, the operand to the typeof was an expresion.
3630 if (Operand.isInvalid()) {
3631 DS.SetTypeSpecError();
Steve Naroff4bd2f712007-08-02 02:53:48 +00003632 return;
Steve Naroffad373bd2007-07-31 12:34:36 +00003633 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00003634
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00003635 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00003636 unsigned DiagID;
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00003637 // Check for duplicate type specifiers (e.g. "int typeof(int)").
3638 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
John McCallba7bf592010-08-24 05:47:05 +00003639 DiagID, Operand.get()))
John McCall49bfce42009-08-03 20:12:06 +00003640 Diag(StartLoc, DiagID) << PrevSpec;
Steve Naroffad373bd2007-07-31 12:34:36 +00003641}
Chris Lattner73a9c7d2010-02-28 18:33:55 +00003642
3643
3644/// TryAltiVecVectorTokenOutOfLine - Out of line body that should only be called
3645/// from TryAltiVecVectorToken.
3646bool Parser::TryAltiVecVectorTokenOutOfLine() {
3647 Token Next = NextToken();
3648 switch (Next.getKind()) {
3649 default: return false;
3650 case tok::kw_short:
3651 case tok::kw_long:
3652 case tok::kw_signed:
3653 case tok::kw_unsigned:
3654 case tok::kw_void:
3655 case tok::kw_char:
3656 case tok::kw_int:
3657 case tok::kw_float:
3658 case tok::kw_double:
3659 case tok::kw_bool:
3660 case tok::kw___pixel:
3661 Tok.setKind(tok::kw___vector);
3662 return true;
3663 case tok::identifier:
3664 if (Next.getIdentifierInfo() == Ident_pixel) {
3665 Tok.setKind(tok::kw___vector);
3666 return true;
3667 }
3668 return false;
3669 }
3670}
3671
3672bool Parser::TryAltiVecTokenOutOfLine(DeclSpec &DS, SourceLocation Loc,
3673 const char *&PrevSpec, unsigned &DiagID,
3674 bool &isInvalid) {
3675 if (Tok.getIdentifierInfo() == Ident_vector) {
3676 Token Next = NextToken();
3677 switch (Next.getKind()) {
3678 case tok::kw_short:
3679 case tok::kw_long:
3680 case tok::kw_signed:
3681 case tok::kw_unsigned:
3682 case tok::kw_void:
3683 case tok::kw_char:
3684 case tok::kw_int:
3685 case tok::kw_float:
3686 case tok::kw_double:
3687 case tok::kw_bool:
3688 case tok::kw___pixel:
3689 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
3690 return true;
3691 case tok::identifier:
3692 if (Next.getIdentifierInfo() == Ident_pixel) {
3693 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
3694 return true;
3695 }
3696 break;
3697 default:
3698 break;
3699 }
Douglas Gregor9938e3b2010-06-16 15:28:57 +00003700 } else if ((Tok.getIdentifierInfo() == Ident_pixel) &&
Chris Lattner73a9c7d2010-02-28 18:33:55 +00003701 DS.isTypeAltiVecVector()) {
3702 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
3703 return true;
3704 }
3705 return false;
3706}
3707